From e7364eb05470c391721ec40d2a9f5929e728a603 Mon Sep 17 00:00:00 2001 From: shulaoda <165626830+shulaoda@users.noreply.github.com> Date: Tue, 23 Jun 2026 02:31:47 +0800 Subject: [PATCH] chore(deps): switch mimalloc3 to upstream microsoft/mimalloc --- .gitmodules | 2 +- libmimalloc-sys/build.rs | 16 ++++++++++++---- libmimalloc-sys/c_src/mimalloc | 2 +- libmimalloc-sys/c_src/mimalloc3 | 2 +- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/.gitmodules b/.gitmodules index 58b35de..c1a25fb 100644 --- a/.gitmodules +++ b/.gitmodules @@ -3,5 +3,5 @@ url = https://github.com/microsoft/mimalloc.git [submodule "libmimalloc-sys/c_src/mimalloc3"] path = libmimalloc-sys/c_src/mimalloc3 - url = https://github.com/napi-rs/mimalloc.git + url = https://github.com/microsoft/mimalloc.git branch = dev3 diff --git a/libmimalloc-sys/build.rs b/libmimalloc-sys/build.rs index 6048cb3..7a69f4b 100644 --- a/libmimalloc-sys/build.rs +++ b/libmimalloc-sys/build.rs @@ -76,10 +76,18 @@ fn main() { cmake_config.define("MI_LOCAL_DYNAMIC_TLS", "ON"); } - // Note: the previous `-DMI_HAS_TLS_SLOT=0` workaround for Apple has been - // replaced by a vendored source patch in our mimalloc fork (napi-rs/mimalloc - // dev3) that routes Apple to `MI_TLS_MODEL_THREAD_LOCAL + MI_TLS_RECURSE_GUARD` - // directly in `include/mimalloc/prim.h`. See that patch for the full rationale. + // On macOS, mimalloc v3 defaults to a fixed TLS slot (TCB[108]/[109]) shared by + // every image, so a second statically-linked copy (e.g. another napi addon) + // adopts the first's per-thread heap and crashes. Force the thread-local model + // so each image gets its own `__thread` storage (a v3-only cmake option from + // microsoft/mimalloc b83dee64, issue #1301). + if env::var_os("CARGO_FEATURE_V3").is_some() && target_os == "macos" { + cmake_config + .define("MI_TLS_MODEL_LOCAL", "ON") + // macOS may allocate on a thread-local's first access; guard against + // re-entering malloc until the process is initialized. + .define("MI_TLS_RECURSE_GUARD", "ON"); + } if (target_os == "linux" || target_os == "android") && env::var_os("CARGO_FEATURE_NO_THP").is_some() diff --git a/libmimalloc-sys/c_src/mimalloc b/libmimalloc-sys/c_src/mimalloc index 02a2f5d..fef6b0d 160000 --- a/libmimalloc-sys/c_src/mimalloc +++ b/libmimalloc-sys/c_src/mimalloc @@ -1 +1 @@ -Subproject commit 02a2f5df9d7d46d30263b83832eebeeab62dc5fe +Subproject commit fef6b0dd70f9d7fa0750b0d0b9fbb471203b94cd diff --git a/libmimalloc-sys/c_src/mimalloc3 b/libmimalloc-sys/c_src/mimalloc3 index 5853e09..b83dee6 160000 --- a/libmimalloc-sys/c_src/mimalloc3 +++ b/libmimalloc-sys/c_src/mimalloc3 @@ -1 +1 @@ -Subproject commit 5853e0990ec2eaf8bef332d97be14e7d79964a3d +Subproject commit b83dee64041b07b9bf855ce6fc3753886a7bd9a5