Skip to content

Merge upstream v3.5.3 - #60

Merged
Jarred-Sumner merged 29 commits into
bun-dev3-v2from
claude/sync-upstream-v3.5.3
Oct 4, 2026
Merged

Jarred-Sumner merged 29 commits into
bun-dev3-v2from
claude/sync-upstream-v3.5.3

Conversation

@Jarred-Sumner

Copy link
Copy Markdown
Collaborator

Merges upstream tag v3.5.3 (27 commits). The fork was at v3.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=1 on Linux, each as the first mimalloc call on a new thread:

before, release before, debug after
mi_malloc, mi_calloc, mi_malloc_aligned, mi_recalloc(NULL, ..) ok ok ok
mi_realloc(NULL, n) segfault assertion ok
mi_reallocf(NULL, n) segfault assertion ok
mi_strdup segfault assertion ok
mi_mallocn segfault assertion ok
mi_new segfault assertion ok

mi_free_size_aligned of a small block with an alignment above its size (microsoft#1400): 200 x mi_malloc_aligned(8, 16 KiB) freed that way is a segfault in a default release build before, and fine after. A build with MI_FREE_USE_PAGEMAP=1, which is what Bun uses, was not affected.

Also: the chunk count in mi_bchunks_unsafe_setN for 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: keeps mi_theap_malloc_zero_block_start. Upstream removed mi_theap_malloc_zero_no_guarded, which the helper calls without MI_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), and mi_arena_start_idx has 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's test-api has this for mi_new (in a C++ build only) and mi_malloc_aligned(0, 16). With the pthreads model it crashes at mi_mallocn before 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:

before after
mi_malloc / mi_free 52.5 52.5
malloc / free 53.0 53.0
mi_calloc 73.4 73.4
mi_realloc 169.0 169.0
mi_malloc_aligned(64) 70.3 70.3
mi_heap_malloc 55.0 55.0
mi_heap_calloc 79.4 78.4
mi_heap_realloc 177.0 177.0
mi_free_size_aligned 58.4 58.4
start and exit, in total 3190409 3190412

The same with -DMI_TLS_MODEL_PTHREADS=1 (65.0 and 65.0 for mi_malloc / mi_free, and so on). The benchmark for mi_heap_new + mi_heap_destroy of #59: 1403M and 1401M instructions. .text grows 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.c here; about 15 are changes to the code generation of mi_free and mi_malloc, some of which revert each other; and page_reclaim_on_free across 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.

daanx and others added 28 commits September 12, 2026 14:08
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)
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
Jarred-Sumner force-pushed the claude/sync-upstream-v3.5.3 branch from 5a22b40 to b41d147 Compare October 4, 2026 21:10
@Jarred-Sumner
Jarred-Sumner merged commit 92ef658 into bun-dev3-v2 Oct 4, 2026
23 checks passed
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants