Skip to content

Commit 023e997

Browse files
authored
Merge pull request DeusData#2281 from knewstimek/pr/windows-aligned-delete-fix
fix(windows): use matching deallocator for CRT aligned blocks
2 parents 46c01c4 + 67695d4 commit 023e997

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

‎src/foundation/mem_override_win.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ void *__real_malloc(size_t size);
5656
void *__real_calloc(size_t count, size_t size);
5757
void *__real_realloc(void *block, size_t size);
5858
void __real_free(void *block);
59+
void __real__aligned_free(void *block);
5960
char *__real_strdup(const char *text);
6061
size_t __real__msize(void *block);
6162
/* Wrappers must call __real_* for anything that is itself wrapped: a plain
@@ -155,7 +156,9 @@ void __wrap__aligned_free(void *block) {
155156
mi_free(block);
156157
return;
157158
}
158-
__real_free(block);
159+
/* A CRT aligned block has its own bookkeeping header. Plain free is not
160+
* its matching deallocator. */
161+
__real__aligned_free(block);
159162
}
160163

161164
void *__wrap_realloc(void *block, size_t size) {

‎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();
@@ -1745,6 +1774,10 @@ SUITE(mem) {
17451774
RUN_TEST(mem_rss_reflects_external_resident_memory);
17461775
RUN_TEST(mem_collect_no_crash);
17471776
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
17481781
/* Memory pressure simulation */
17491782
RUN_TEST(mem_progressive_alloc_rss_increases);
17501783
RUN_TEST(mem_free_and_collect_no_crash);

0 commit comments

Comments
 (0)