Skip to content

Commit f8b6ea0

Browse files
authored
chore(deps): switch mimalloc3 to upstream microsoft/mimalloc (#82)
### Background On macOS, mimalloc v3 stores the per-thread heap pointer in 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 heap and crashes. We worked around this with a vendored `prim.h` patch in the `napi-rs/mimalloc` fork. Upstream now fixes it in [microsoft/mimalloc#1301](microsoft/mimalloc#1301) ([b83dee64](microsoft/mimalloc@b83dee6)) by exposing cmake options, so we can drop the fork. ### Changes - **`.gitmodules`** — `mimalloc3` url `napi-rs/mimalloc` → `microsoft/mimalloc`. - **`mimalloc3`** — `5853e09` (fork) → `b83dee64` (official `dev3`); drops the vendored `prim.h` patch. - **`build.rs`** — on v3 + macOS, enable the official options: ```rust if env::var_os("CARGO_FEATURE_V3").is_some() && target_os == "macos" { cmake_config .define("MI_TLS_MODEL_LOCAL", "ON") // -> MI_TLS_MODEL_THREAD_LOCAL=1 .define("MI_TLS_RECURSE_GUARD", "ON"); // -> MI_TLS_RECURSE_GUARD=1 } ``` - **`mimalloc` (v2)** — routine bump `02a2f5d` → `fef6b0d`. ### Notes - **Scope: v3 + macOS only.** Options exist only in v3; Linux already defaults to thread-local (no-op), Android/OpenBSD use pthreads, Windows uses its own TlsAlloc path. Add `target_os == "ios"` to cover iOS/tvOS. - **`MI_TLS_RECURSE_GUARD` is belt-and-suspenders** — upstream `prim.h` already auto-enables it on Apple; we set it explicitly for clarity. - **No regression of the dead-main-thread fix** (issue #1287 / rolldown#9722): the `MI_THREADID_INVALID` poison in `mi_tld_free` is present in `b83dee64`. ### Verification (macOS) - v3 build: `CMakeCache` `MI_TLS_MODEL_LOCAL:BOOL=ON` / `MI_TLS_RECURSE_GUARD:BOOL=ON`; compiled with `MI_TLS_MODEL_THREAD_LOCAL=1` + `MI_TLS_RECURSE_GUARD=1`. - Default (v2) build: options absent (gating works), builds clean. - `cargo test --features v3`: `mimalloc-safe` 6/6 pass; `libmimalloc-sys-test` runs.
1 parent 14128b9 commit f8b6ea0

4 files changed

Lines changed: 15 additions & 7 deletions

File tree

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
url = https://github.com/microsoft/mimalloc.git
44
[submodule "libmimalloc-sys/c_src/mimalloc3"]
55
path = libmimalloc-sys/c_src/mimalloc3
6-
url = https://github.com/napi-rs/mimalloc.git
6+
url = https://github.com/microsoft/mimalloc.git
77
branch = dev3

libmimalloc-sys/build.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,18 @@ fn main() {
7676
cmake_config.define("MI_LOCAL_DYNAMIC_TLS", "ON");
7777
}
7878

79-
// Note: the previous `-DMI_HAS_TLS_SLOT=0` workaround for Apple has been
80-
// replaced by a vendored source patch in our mimalloc fork (napi-rs/mimalloc
81-
// dev3) that routes Apple to `MI_TLS_MODEL_THREAD_LOCAL + MI_TLS_RECURSE_GUARD`
82-
// directly in `include/mimalloc/prim.h`. See that patch for the full rationale.
79+
// On macOS, mimalloc v3 defaults to a fixed TLS slot (TCB[108]/[109]) shared by
80+
// every image, so a second statically-linked copy (e.g. another napi addon)
81+
// adopts the first's per-thread heap and crashes. Force the thread-local model
82+
// so each image gets its own `__thread` storage (a v3-only cmake option from
83+
// microsoft/mimalloc b83dee64, issue #1301).
84+
if env::var_os("CARGO_FEATURE_V3").is_some() && target_os == "macos" {
85+
cmake_config
86+
.define("MI_TLS_MODEL_LOCAL", "ON")
87+
// macOS may allocate on a thread-local's first access; guard against
88+
// re-entering malloc until the process is initialized.
89+
.define("MI_TLS_RECURSE_GUARD", "ON");
90+
}
8391

8492
if (target_os == "linux" || target_os == "android")
8593
&& env::var_os("CARGO_FEATURE_NO_THP").is_some()

0 commit comments

Comments
 (0)