Repository navigation
Merge upstream v3.5.3 - #60
Merged
Merged
Conversation
release v3.5.2
439dc27 moved `mi_process_setup_auto_thread_done` into `mi_process_init_once` so it runs after `mi_thread_init` and can associate the current theap with the thread-done key. It now runs before `_mi_process_is_initialized = true`, and with `MI_TLS_RECURSE_GUARD` (always on for `MI_TLS_MODEL_LOCAL` on macOS) `_mi_theap_default()` still returns `_mi_theap_empty` at that point. The new `mi_assert_internal(mi_theap_is_initialized(theap))` fires in debug builds; release builds skip the association, so the first thread's theap is again never passed to `_mi_thread_done`. Repro (macOS): `cmake -DCMAKE_BUILD_TYPE=Debug -DMI_TLS_MODEL=LOCAL` then `./mimalloc-test-api` aborts at process init on v3.5.2 and passes on v3.5.1. Same with `-DMI_TLS_RECURSE_GUARD=ON` on Linux. Set the flag before `mi_process_setup_auto_thread_done`; the theap is fully initialized by then. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_016srh5svTkqfW21zGcU6ht9
…ize-aligned fix: safely free over-aligned small allocations
…before-thread-done Set _mi_process_is_initialized before mi_process_setup_auto_thread_done (fixes 439dc27 with MI_TLS_RECURSE_GUARD)
This reverts commit d184163.
… and remove the xtheap variants
Conflicts: - src/alloc-aligned.c: keep mi_theap_malloc_zero_block_start. Upstream removed mi_theap_malloc_zero_no_guarded, which only called _mi_theap_malloc_zero, so the helper calls that now. - src/arena.c: mi_forall_arenas starts at the requested arena (upstream) and mi_arena_start_idx has no heap argument (fork); the check for a zero block size (upstream) with the comment of the fork.
With a TLS model where the thread local heap of a new thread starts out as NULL (pthreads, Windows, macOS), mi_realloc(NULL,n), mi_reallocf, mi_strdup, mi_mallocn and mi_new crashed as the first call on a thread until v3.5.3 (microsoft#1398). test-api has mi_new, in a C++ build only.
Jarred-Sumner
force-pushed
the
claude/sync-upstream-v3.5.3
branch
from
October 4, 2026 21:10
5a22b40 to
b41d147
Compare
Jarred-Sumner
added a commit
to oven-sh/bun
that referenced
this pull request
Oct 4, 2026
Bumps mimalloc to oven-sh/mimalloc#60, which merges upstream v3.5.3 into the fork (it was at v3.5.2). - Fixes a crash when the first mimalloc call on a new thread is mi_realloc(NULL, n), mi_reallocf, mi_strdup, mi_mallocn or mi_new, with a TLS model where the thread local heap of a new thread starts out as NULL (macOS and Windows). - Fixes mi_free_size_aligned of a small block with an alignment above its size. Bun's build (MI_FREE_USE_PAGEMAP=1) was not affected. - No function is removed from mimalloc.h, and no signature that src/mimalloc_sys binds changes. Instruction counts for the allocation and free paths are the same before and after.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Merges upstream tag
v3.5.3(27 commits). The fork was atv3.5.2.What it fixes
A crash on the first call of a new thread (microsoft#1398). With a TLS model where the thread local heap of a new thread starts out as NULL (pthreads, Windows, macOS), some entry points did not handle that. Built with
-DMI_TLS_MODEL_PTHREADS=1on Linux, each as the first mimalloc call on a new thread:mi_malloc,mi_calloc,mi_malloc_aligned,mi_recalloc(NULL, ..)mi_realloc(NULL, n)mi_reallocf(NULL, n)mi_strdupmi_mallocnmi_newmi_free_size_alignedof a small block with an alignment above its size (microsoft#1400): 200 xmi_malloc_aligned(8, 16 KiB)freed that way is a segfault in a default release build before, and fine after. A build withMI_FREE_USE_PAGEMAP=1, which is what Bun uses, was not affected.Also: the chunk count in
mi_bchunks_unsafe_setNfor a range that does not start at a chunk boundary, and allocation from an exclusive child arena (microsoft#1404).Conflicts
Three hunks, and one place that merged cleanly but no longer compiled:
src/alloc-aligned.c: keepsmi_theap_malloc_zero_block_start. Upstream removedmi_theap_malloc_zero_no_guarded, which the helper calls withoutMI_SAMPLE. That function only called_mi_theap_malloc_zero(theap, size, zero, 0, ppage)(in both of its versions), so the helper calls that now.src/arena.c,mi_forall_arenas: starts at the requested arena (upstream), andmi_arena_start_idxhas no heap argument (fork).src/arena.c: the check for a zero block size (upstream) with the comment of the fork.New test
test-thread-first-call: 34 ways to allocate, each as the first call on a new thread. Upstream'stest-apihas this formi_new(in a C++ build only) andmi_malloc_aligned(0, 16). With the pthreads model it crashes atmi_mallocnbefore the merge, and passes after it. It runs on Windows as well.Cost
Instructions per operation (
perf stat, 20M operations of 16 to 1040 bytes, Bun's compile flags), before and after:mi_malloc/mi_freemalloc/freemi_callocmi_reallocmi_malloc_aligned(64)mi_heap_mallocmi_heap_callocmi_heap_reallocmi_free_size_alignedThe same with
-DMI_TLS_MODEL_PTHREADS=1(65.0 and 65.0 formi_malloc/mi_free, and so on). The benchmark formi_heap_new+mi_heap_destroyof #59: 1403M and 1401M instructions..textgrows by 918 bytes.Tested
All tests pass on Linux x64 in: debug, release, debug C++, release C++, secure, TSAN, ASAN, UBSAN, guarded, and debug and release with the pthreads TLS model.
Not in this PR: v3.5.4
58 more commits, 37 conflict hunks in 16 files. About 25 commits are a built-in pprof profiler, which overlaps
sample-profile.chere; about 15 are changes to the code generation ofmi_freeandmi_malloc, some of which revert each other; andpage_reclaim_on_freeacross threads becomes the default, which changes what memory a thread holds on to and has to be measured first. There is no crash fix in it.