Skip to content

Commit 7feec72

Browse files
authored
Fix mimalloc+asan (#20951)
Mimalloc defined some symbols as strong, but ASan (and other sanitizers I think) need to override them, so make them weak like dlmalloc and emmalloc do. Helps #20936
1 parent 9f19a52 commit 7feec72

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

system/lib/mimalloc/src/alloc-override.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ typedef struct mi_nothrow_s { int _tag; } mi_nothrow_t;
3535
#pragma GCC diagnostic ignored "-Wattributes" // or we get warnings that nodiscard is ignored on a forward
3636
#define MI_FORWARD(fun) __attribute__((alias(#fun), used, visibility("default"), copy(fun)));
3737
#else
38-
#define MI_FORWARD(fun) __attribute__((alias(#fun), used, visibility("default")));
38+
// XXX EMSCRIPTEN: Add "weak"
39+
#define MI_FORWARD(fun) __attribute__((alias(#fun), used, visibility("default"), weak));
3940
#endif
4041
#define MI_FORWARD1(fun,x) MI_FORWARD(fun)
4142
#define MI_FORWARD2(fun,x,y) MI_FORWARD(fun)
@@ -239,9 +240,11 @@ extern "C" {
239240
#endif
240241

241242
// No forwarding here due to aliasing/name mangling issues
243+
__attribute__((weak)) // XXX EMSCRIPTEN
242244
void* valloc(size_t size) { return mi_valloc(size); }
243245
void vfree(void* p) { mi_free(p); }
244246
size_t malloc_good_size(size_t size) { return mi_malloc_good_size(size); }
247+
__attribute__((weak)) // XXX EMSCRIPTEN
245248
int posix_memalign(void** p, size_t alignment, size_t size) { return mi_posix_memalign(p, alignment, size); }
246249

247250
// `aligned_alloc` is only available when __USE_ISOC11 is defined.
@@ -252,15 +255,18 @@ extern "C" {
252255
// Fortunately, in the case where `aligned_alloc` is declared as `static inline` it
253256
// uses internally `memalign`, `posix_memalign`, or `_aligned_malloc` so we can avoid overriding it ourselves.
254257
#if !defined(__GLIBC__) || __USE_ISOC11
258+
__attribute__((weak)) // XXX EMSCRIPTEN
255259
void* aligned_alloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); }
256260
#endif
257261
#endif
258262

259263
// no forwarding here due to aliasing/name mangling issues
260264
void cfree(void* p) { mi_free(p); }
261265
void* pvalloc(size_t size) { return mi_pvalloc(size); }
266+
__attribute__((weak)) // XXX EMSCRIPTEN
262267
void* reallocarray(void* p, size_t count, size_t size) { return mi_reallocarray(p, count, size); }
263268
int reallocarr(void* p, size_t count, size_t size) { return mi_reallocarr(p, count, size); }
269+
__attribute__((weak)) // XXX EMSCRIPTEN
264270
void* memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }
265271
void* _aligned_malloc(size_t alignment, size_t size) { return mi_aligned_alloc(alignment, size); }
266272

@@ -270,6 +276,7 @@ void* _aligned_malloc(size_t alignment, size_t size) { return mi_aligned_allo
270276
void* __libc_calloc(size_t count, size_t size) MI_FORWARD2(mi_calloc, count, size)
271277
void* __libc_realloc(void* p, size_t size) MI_FORWARD2(mi_realloc, p, size)
272278
void __libc_free(void* p) MI_FORWARD0(mi_free, p)
279+
__attribute__((weak)) // XXX EMSCRIPTEN
273280
void* __libc_memalign(size_t alignment, size_t size) { return mi_memalign(alignment, size); }
274281

275282
#ifdef __EMSCRIPTEN__ // emscripten adds some more on top of WASI

test/test_core.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9059,6 +9059,10 @@ def test_asan_no_error(self, name):
90599059
'vector': ('test_asan_vector.cpp', [
90609060
'AddressSanitizer: container-overflow on address'
90619061
]),
9062+
# some coverage for mimalloc as well
9063+
'use_after_free_c_mimalloc': ('test_asan_use_after_free.c', [
9064+
'AddressSanitizer: heap-use-after-free on address',
9065+
], ['-sMALLOC=mimalloc']),
90629066
})
90639067
def test_asan(self, name, expected_output, cflags=None):
90649068
if '-Oz' in self.emcc_args:

0 commit comments

Comments
 (0)