|
30 | 30 | #define WIN32_LEAN_AND_MEAN |
31 | 31 | #endif |
32 | 32 | #include <windows.h> |
| 33 | +#include <errno.h> |
33 | 34 | #endif |
34 | 35 |
|
35 | 36 | /* ASan detection — mimalloc MI_OVERRIDE=0 under ASan, mi_process_info |
@@ -353,6 +354,34 @@ TEST(mem_rss_reflects_external_resident_memory) { |
353 | 354 | PASS(); |
354 | 355 | } |
355 | 356 |
|
| 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 | + |
356 | 385 | TEST(mem_collect_rss_still_positive) { |
357 | 386 | cbm_mem_init(0.5); |
358 | 387 | cbm_mem_collect(); |
@@ -1745,6 +1774,10 @@ SUITE(mem) { |
1745 | 1774 | RUN_TEST(mem_rss_reflects_external_resident_memory); |
1746 | 1775 | RUN_TEST(mem_collect_no_crash); |
1747 | 1776 | RUN_TEST(mem_collect_rss_still_positive); |
| 1777 | +#ifdef _WIN32 |
| 1778 | + /* Windows allocator wrappers */ |
| 1779 | + RUN_TEST(mem_win_aligned_free_returns_crt_block_to_crt); |
| 1780 | +#endif |
1748 | 1781 | /* Memory pressure simulation */ |
1749 | 1782 | RUN_TEST(mem_progressive_alloc_rss_increases); |
1750 | 1783 | RUN_TEST(mem_free_and_collect_no_crash); |
|
0 commit comments