From 94c88691a3f8aeaf27211e8a16d451542e0c298d Mon Sep 17 00:00:00 2001 From: Kleis Auke Wolthuizen Date: Wed, 22 Apr 2026 11:35:52 +0200 Subject: [PATCH] [mimalloc] Only use `__builtin_thread_pointer()` in multithreaded builds In single-threaded builds, `__builtin_thread_pointer()` may return 0, breaking mimalloc's non-null thread pointer assumption and causing a segfault with `-sSAFE_HEAP`. Only use it when threading is enabled. Fixes: #26742. --- tools/system_libs.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tools/system_libs.py b/tools/system_libs.py index 55cf0cb6b7b6a..53dab461c77fc 100644 --- a/tools/system_libs.py +++ b/tools/system_libs.py @@ -1889,8 +1889,6 @@ class libmimalloc(MTLibrary): '-DNDEBUG', # Emscripten uses musl libc internally '-DMI_LIBC_MUSL', - # enable use of `__builtin_thread_pointer()` - '-DMI_USE_BUILTIN_THREAD_POINTER', ] # malloc/free/calloc are runtime functions and can be generated during LTO @@ -1913,6 +1911,13 @@ class libmimalloc(MTLibrary): # Include sbrk.c in libc, it uses tracing and libc itself doesn't have a tracing variant. src_files += [utils.path_from_root('system/lib/libc/sbrk.c')] + def get_cflags(self): + cflags = super().get_cflags() + if self.is_mt: + # enable use of `__builtin_thread_pointer()` in multithreaded builds + cflags += ['-DMI_USE_BUILTIN_THREAD_POINTER'] + return cflags + def can_use(self): return super().can_use() and settings.MALLOC == 'mimalloc'