Skip to content

Commit 71ec2e6

Browse files
committed
test(windows): pin the CRT aligned-block route of the aligned-free wrapper
__wrap__aligned_free hands a block mimalloc did not make back to the CRT. A block from the CRT's _aligned_malloc starts past the CRT's bookkeeping header, so its matching deallocator is _aligned_free; plain free receives an interior pointer. This branch routes such blocks to __real__aligned_free. The new Windows-only mem test allocates through __real__aligned_malloc (the CRT's own allocator under --wrap), asserts mimalloc does not own the block, frees it through the wrapper by name, and checks errno and HeapValidate on the process heap. Freeing the interior pointer with plain free is a bad-free report under ASan; on an uninstrumented heap the free fails with errno set or the process terminates. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 9dca8d2 commit 71ec2e6

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

‎tests/test_mem.c‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#define WIN32_LEAN_AND_MEAN
3131
#endif
3232
#include <windows.h>
33+
#include <errno.h>
3334
#endif
3435

3536
/* ASan detection — mimalloc MI_OVERRIDE=0 under ASan, mi_process_info
@@ -353,6 +354,34 @@ TEST(mem_rss_reflects_external_resident_memory) {
353354
PASS();
354355
}
355356

357+
#ifdef _WIN32
358+
/* A block the CRT's _aligned_malloc made starts past the CRT's bookkeeping
359+
* header, so the aligned-free wrapper must return it through _aligned_free.
360+
* Plain free receives an interior pointer: ASan reports a bad free, and the
361+
* Windows heap either rejects the free (errno) or terminates the process.
362+
* --wrap makes __real__aligned_malloc the CRT's own allocator, so the block is
363+
* genuinely foreign to mimalloc. The wrapper is called by name: the routing
364+
* inside it is what this pins, independent of which references the linker
365+
* rewrites. */
366+
void *__real__aligned_malloc(size_t size, size_t alignment);
367+
void __wrap__aligned_free(void *block);
368+
369+
TEST(mem_win_aligned_free_returns_crt_block_to_crt) {
370+
enum { CRT_BLOCK_SIZE = 256, CRT_BLOCK_ALIGNMENT = 64 };
371+
unsigned char *block = __real__aligned_malloc(CRT_BLOCK_SIZE, CRT_BLOCK_ALIGNMENT);
372+
ASSERT_NOT_NULL(block);
373+
/* Without a CRT-owned block the wrapper takes its mimalloc path instead. */
374+
ASSERT_FALSE(mi_is_in_heap_region(block));
375+
ASSERT_EQ((uintptr_t)block % CRT_BLOCK_ALIGNMENT, 0);
376+
memset(block, 0xA5, CRT_BLOCK_SIZE);
377+
errno = 0;
378+
__wrap__aligned_free(block);
379+
ASSERT_EQ(errno, 0);
380+
ASSERT_TRUE(HeapValidate(GetProcessHeap(), 0, NULL));
381+
PASS();
382+
}
383+
#endif
384+
356385
TEST(mem_collect_rss_still_positive) {
357386
cbm_mem_init(0.5);
358387
cbm_mem_collect();
@@ -1744,6 +1773,10 @@ SUITE(mem) {
17441773
RUN_TEST(mem_rss_reflects_external_resident_memory);
17451774
RUN_TEST(mem_collect_no_crash);
17461775
RUN_TEST(mem_collect_rss_still_positive);
1776+
#ifdef _WIN32
1777+
/* Windows allocator wrappers */
1778+
RUN_TEST(mem_win_aligned_free_returns_crt_block_to_crt);
1779+
#endif
17471780
/* Memory pressure simulation */
17481781
RUN_TEST(mem_progressive_alloc_rss_increases);
17491782
RUN_TEST(mem_free_and_collect_no_crash);

0 commit comments

Comments
 (0)