From c4aa450595a0249a8b53ae0aba1facf1669e3b2a Mon Sep 17 00:00:00 2001 From: Kleis Auke Wolthuizen Date: Sun, 1 Nov 2026 10:54:22 +0100 Subject: [PATCH] [mimalloc] Disable `-sABORTING_MALLOC` by default Allows mimalloc to handle allocation failures from its underlying emmalloc allocator and fall back gracefully. This is an alternative to the `mi_prim_heap_has_room()` check introduced in microsoft/mimalloc@0fec3da, which Emscripten does not use. --- test/test_core.py | 3 +-- tools/link.py | 5 ++++- tools/system_libs.py | 4 ++-- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/test/test_core.py b/test/test_core.py index b4810f185a707..de40cbd9834ec 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -8751,8 +8751,7 @@ def test_mallinfo(self): @parameterized({ '': ([],), 'emmalloc': (['-sMALLOC=emmalloc'],), - # TODO: investigate removing `-sABORTING_MALLOC=0`, as it's only needed for `wasm64_4gb.test_wrap_malloc_mimalloc` - 'mimalloc': (['-sMALLOC=mimalloc', '-sABORTING_MALLOC=0'],), + 'mimalloc': (['-sMALLOC=mimalloc'],), }) def test_wrap_malloc(self, args): self.do_runf('core/test_wrap_malloc.c', 'done\n', cflags=args) diff --git a/tools/link.py b/tools/link.py index 0bbca89235da4..81eb6f403e629 100644 --- a/tools/link.py +++ b/tools/link.py @@ -1516,11 +1516,14 @@ def limit_incoming_module_api(): if not settings.DECLARE_ASM_MODULE_EXPORTS: settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$assignWasmExports'] - if settings.ALLOW_MEMORY_GROWTH: + if settings.ALLOW_MEMORY_GROWTH or settings.MALLOC == 'mimalloc': # Setting ALLOW_MEMORY_GROWTH turns off ABORTING_MALLOC, as in that mode we default to # the behavior of trying to grow and returning 0 from malloc on failure, like # a standard system would. However, if the user sets the flag it # overrides that. + # Also default to non-aborting malloc behavior for -sMALLOC=mimalloc, + # so mimalloc can handle allocation failures from its underlying emmalloc + # allocator and fall back gracefully. default_setting('ABORTING_MALLOC', 0) if settings.EMBIND: diff --git a/tools/system_libs.py b/tools/system_libs.py index 24865d977c79d..14187efbb1ed5 100644 --- a/tools/system_libs.py +++ b/tools/system_libs.py @@ -1895,8 +1895,8 @@ class libmimalloc(MTLibrary): '-DMI_LIBC_MUSL', # enable use of `__builtin_thread_pointer()` '-DMI_USE_BUILTIN_THREAD_POINTER', - # always use the page map when free-ing a pointer, as we don't support - # large OS aligned allocations well + # always use the page map in `mi_free()`, as we don't support large + # aligned allocations well (needed for aligned page metadata) '-DMI_FREE_USE_PAGEMAP=1', # to reduce code size, disable environ customization, profiling and # nonessential stats