Skip to content

Merge upstream v3.6.0 - #64

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

Jarred-Sumner merged 239 commits into
bun-dev3-v2from
claude/sync-upstream-v3.6.0

Conversation

@Jarred-Sumner

@Jarred-Sumner Jarred-Sumner commented Oct 29, 2026 •

Copy link
Copy Markdown
Collaborator

Merges upstream v3.6.0 into the fork. Includes the commits of #63, which should land first.

Upstream reorganized the tree in this range (src/alloc/, src/arena/, src/heap/, src/prim/, src/util/, a split internal.h), so most of the fork's changes had to be ported onto new files. src/arena.c was split in three.

Defaults that differ from v3.6.0

Setting v3.6.0 Here Why
Cache offsets (MI_PAGE_CACHE_OFFSET) Every block Only blocks above 512 KiB, which have a page of their own (MI_PAGE_CACHE_OFFSET_MIN_SIZE) See below
arena_purge_immediate_size 32 MiB Never The scavenger thread purges, so that a free does not wait for the kernel. This is what the fork did before
page_reclaim_on_free 1 0 The fork's value, and upstream's again since 5cae473

Cache offsets

An offset takes the blocks of a page off the OS pages, so the purge of free runs inside a page in use cannot discard a free block as a whole. A page with one block has no free runs. macOS arm64 (16 KiB OS pages), MI_FREE_USE_PAGEMAP=1.

Memory kept after 7 of every 8 blocks are freed, 24 MB live, one run each:

Block size Off Here Every block
12 KiB 54.2 MB 54.2 MB 58.6 MB
24 KiB 38.1 MB 38.1 MB 50.7 MB
48 KiB 24.7 MB 24.8 MB 43.3 MB
96 KiB 27.3 MB 27.3 MB 32.4 MB
400000 B 28.2 MB 28.2 MB 29.7 MB
1 MiB 24.7 MB 25.1 MB 25.1 MB

Arrays read in lockstep at the same index, median of 15 interleaved runs:

Arrays Off Here Every block
16 x 1 MiB 4.4 ms 3.5 ms 3.5 ms
32 x 1 MiB 17.1 ms 11.0 ms 11.4 ms
64 x 256 KiB 8.6 ms 9.0 ms 5.6 ms

Option numbers

The fork's options come after upstream's. Upstream added six, so snapshot_on_exit .. purge_holes_large_floor_epochs move from 48..55 to 54..61. purge_holes is now 56. Options 0..47 keep their numbers.

Problems that showed no conflict

Problem Resolution
Upstream moved half of bitmap.c to a new file, which merged as pure upstream: the exclusive arena fallback and three memory order fixes were gone Ported
Cache offsets with the fork's 64 KiB natural alignment: mi_malloc_aligned(16K, 16K) came back aligned to 128 bytes Power of two sizes up to 64 KiB get no offset
stats_merge_threshold merges the stats of a theap that mi_heap_delete has detached (heap == NULL) Only for an initialized theap
mi_atomic_addi64_relaxed now takes an _Atomic pointer; the fork calls it on plain counters mi_atomic_volatile_addi64_relaxed
mi_malloc_size and mi_malloc_usable_size defined twice Upstream's kept
MIMALLOC_SNAPSHOT_PATH test was inverted (old) Fixed
The macOS zone enumerator read heaps as a list; it is an array of shards (old) Walks every shard

Taken from upstream over the fork's version

The mi_free and mi_malloc fast paths, ProcessPrng with the RtlGenRandom fallback, _mi_theap_abandon_owned_pages, _mi_fork_init as the one place that registers fork handlers. The child handler repairs locks only: upstream's walk over every theap stays an explicit _mi_fork_child(), which its doc says is unsafe for a fork that other threads do not know of.

16 KiB blocks aligned to 16 KiB

16384 blocks, all touched, all but 1 in 64 freed. macOS arm64, MI_FREE_USE_PAGEMAP=1, footprint, one run each.

Build Misaligned Over-allocated Peak After free Per survivor
Fork before 0 0 257 MB 5.0 MB 20.2 KB
This branch 0 0 257 MB 5.0 MB 20.1 KB
v3.6.0 0 16384 505 MB 120.6 MB 482 KB

Found by CI

Failure Cause Resolution
test-fork, Linux debug: the child aborts on theap==NULL in _mi_thread_init_with_heap The debug check for a recursive thread init matches on the thread id. The theaps of threads that the fork left behind stay in the list, and a new thread of the child often gets the id of one of them. The fork had the same check before the merge In a forked child a debug build makes a new theap, as a release build does
Secure builds on Windows: "failed to commit guard page" VirtualProtect on a decommitted page (from #63) No protect or unprotect on Windows
test-pprof under the thread sanitizer See below Not run under the thread sanitizer

Known limitation: pprof

Upstream's pprof snapshot walks its tables with relaxed and plain reads, on the assumption that sampling has stopped. The fork's mi_profiler_stop does not wait: a thread notices it at its next look and samples until then. So a snapshot can race with on_alloc on another thread. This is not fixed here. pprof is opt-in (MIMALLOC_PROFILE).

Not verified

Beyond that program, the tree has only been through -fsyntax-only (macOS, Linux glibc and musl, mingw; C and C++). The test suite runs in this PR's CI only. None of the three fixes above could be reproduced on macOS. The MSVC and clang-cl branches have not been compiled at all.

daanx and others added 19 commits October 23, 2026 10:04
Fix data race on the thread-done pthread key at process exit
…mpare

Fix -Wsign-compare warning when reading hpage_pmd_size
…a-wrapper

[Rust] Add initial bindings for Arena APIs
(cherry picked from microsoft/mimalloc dc0bfd7; the file is src/arena/bitmap.c there)
(cherry picked from microsoft/mimalloc 0e853a0; the file is src/arena/bitmap.c there)
…or new

mimalloc implements the C++ `operator new` family (alloc-override.c) and is
Bun's global allocator on Linux. Bun compiles without exceptions and installs
no `std::new_handler`, so the no-exceptions arm of mi_try_new_handler reaches
`h == NULL`, cannot throw, and returns false. mi_theap_try_new then returns
NULL to a caller the standard promised memory, and the caller writes through
it: a refused `std::string` growth faults at address 0x0.

With -DMI_NEW_OOM=<function> that arm calls the named noreturn function
instead. `new (std::nothrow)` still returns NULL.
The arenas give each chunk the bin of the first thing that is placed in it, and
a page of one slice is never placed in a chunk of another bin, to reduce
fragmentation. A request that finds nothing in one arena goes on to the next,
or has a new arena reserved.

A heap with an exclusive arena (mi_heap_new_in_arena()) has nowhere else to go.
In an arena of two chunks, once a page of eight slices was the first in the
second chunk, requests for small blocks failed when the first chunk was full,
with most of the arena free.

Where the search in an exclusive arena has found nothing, it goes through the
chunks of all bins. A search that finds room runs the same instructions as
before, and so does every search in another arena.
A guard page was only decommitted. In a release build with MI_SECURE below 3, decommit is an madvise and leaves the protection as it is, so the guard page at the end of the arena info stayed readable and writable.

Protect it as well, and unprotect it before it is committed again.

Upstream dev3 has _mi_os_guard() for this since 5c73531: take that one when it is merged.
MI_NO_GETENV reads nothing from the environment. An embedder may want the options that set sizes and pace to stay settable, and none of those that print, write files or change what is checked.

With -DMI_GETENV_ALLOWED="name,name" only the listed names are read. mi_is_read_from_environment() answers the same question to the embedder.
It places pages by their address in the arena, which guard pages change, like the two purge-holes tests. With MIMALLOC_GUARDED_SAMPLE_RATE=1024, which ctest sets in a debug build, it stopped at 3 of 64 MiB.
EFAULT is for corrupted meta data, and the default error handler aborts on it in a secure or debug build. These three sites only found no memory: with ENOMEM they return NULL, and the caller reports out of memory the way it does for every other allocation.
Upstream's layout, names and refactors; the fork's changes are ported onto them.

Defaults that differ from v3.6.0:
- MI_PAGE_CACHE_OFFSET is 0. With an offset the blocks of a page do not start on an OS page, and the purge of free runs inside pages in use can never discard the first one.
- page_reclaim_on_free is 0, as before and as upstream has it again since 5cae473.

The fork's options come after upstream's, so purge_holes moves from 50 to 56.
The debug check looks for a theap with the id of the current thread. The theaps of threads that the fork left behind stay in the list of their heap, and on Linux a new thread of the child often gets the stack, and so the id, of one of them. A release build has no such check and makes a new theap: a debug build now does the same. Found by upstream's new test-fork.
VirtualProtect fails on a page that is not committed, so every guard page was reported as an error and the tests of a secure build failed on Windows. A decommitted page is not accessible there, which is what the protection is for on unix.
Upstream's pprof snapshot walks its tables with relaxed and plain reads, on the assumption that sampling has stopped. The fork's mi_profiler_stop does not wait: a thread notices it at its next look, and samples until then. The sanitizer reports the read of a location that another thread has just made.
v3.6.0 purges a freed arena range of 32 MiB or more at once, on the thread that frees it. The fork has a thread for that, so that a free does not wait for the kernel. arena_purge_immediate_size is now LONG_MAX by default, which is what the fork did before the merge.
The merge had turned MI_PAGE_CACHE_OFFSET off. Measured on macOS arm64 (16 KiB OS pages), MI_FREE_USE_PAGEMAP=1:

Memory kept after 7 of every 8 blocks are freed, 24 MB live, off / all blocks:
  24 KiB 38.1 / 50.7 MB, 48 KiB 24.7 / 43.3 MB, 96 KiB 27.3 / 32.4 MB, 400000 B 28.2 / 29.7 MB
An offset takes the blocks off the OS pages, so the purge of free runs inside a page in use cannot discard a free block as a whole.

Arrays read in lockstep, median of 15 runs, off / all blocks:
  16 x 1 MiB 4.4 / 3.5 ms, 32 x 1 MiB 17.1 / 11.0 ms, 64 x 256 KiB 8.6 / 5.6 ms

A block above MI_LARGE_MAX_OBJ_SIZE (512 KiB) has a page of its own, which has no free runs. With offsets for those alone the memory kept is that of 'off' at every size, and the 1 MiB arrays are as fast as with offsets for all. The 256 KiB arrays stay as slow as with 'off'.
@Jarred-Sumner
Jarred-Sumner merged commit 5945551 into bun-dev3-v2 Oct 29, 2026
23 checks passed
Jarred-Sumner added a commit to oven-sh/bun that referenced this pull request Oct 29, 2026
oven-sh/mimalloc#63 and oven-sh/mimalloc#64 are merged. This is the head of the fork's branch. Its tree is that of 916b51e524f9, which the fork's CI tested.
Jarred-Sumner added a commit to oven-sh/bun that referenced this pull request Oct 29, 2026
…malloc to v3.6.0 and ICU to 79.1 (#47281)

Three things that go together. All three dependencies' PRs are merged,
and both pins are commits of their main branches.

| | What | From |
|---|---|---|
| 1 | The address space ballast is removed, and `patches/mimalloc/` with
it | oven-sh/WebKit#937, oven-sh/mimalloc#63 |
| 2 | mimalloc goes from v3.5.3 to v3.6.0 | oven-sh/mimalloc#64 |
| 3 | ICU goes from 78.3 to 79.1 (Unicode 18.0, CLDR 49) |
oven-sh/WebKit#936 |

**About 3:** oven-sh/WebKit#936 merged just before oven-sh/WebKit#937,
so no commit of WebKit's `main` has one without the other. It changes
some locale text (`fr` hours, `th` short time, `zh-Hant` short zone
names) and makes `EST5EDT`, `CST6CDT`, `MST7MDT` and `PST8PDT` resolve
to the zones the IANA database links them to.

What people will see, all of it CLDR 49 (an ICU 79.1 that is not Bun's
gives the same). Node has ICU 78.3, so Bun differs from Node in these
until Node moves:

| | ICU 78.3 | ICU 79.1 |
|---|---|---|
| `toLocaleDateString("ko")` | `1970. 1. 1.` | `1970/1/1` |
| `en`, `{ month: "short", year: "2-digit" }` | `Jan 70` | `Jan ’70` |
| `timeZoneName: "long"`, `Europe/London` and `Europe/Dublin` in winter,
every locale tried | `Greenwich Mean Time` | `GMT+00:00` |
| `timeZoneName: "long"`, `Pacific/Honolulu` | `Hawaii-Aleutian Standard
Time` | `Hawaii Time` |

#47314 is the same ICU upgrade by itself. The test changes here that
overlap with it are the same lines.

The `Intl` snapshots run only on the ICU version they were made with, so
they are regenerated (one value differs, the Korean date). They exist to
catch a fault in Bun's compressed ICU data, so each value was also
compared with a Node that links an ICU 79.1 whose data is not
compressed: 385 of 389 are the same, and the other 4 differ in the case
of the first letter of a display name, as they did with ICU 78.3.

## 1. The ballast and the patches

### What

**The address space ballast is removed.** Under a limit on address space
(`ulimit -v`), an executable compiled with `--aot` kept 8 MB of address
space unused. When a `malloc` that may not fail found no memory, it
released 1 MB and tried again, so that the thread could reach a point
where it throws a `RangeError`. mimalloc is not built for that, and it
took ten patches, each fixing what the one before exposed. It was still
best effort: a request larger than what was left of the reserve crashed
anyway.

Such a `malloc` crashes on purpose again, as it does in plain Bun and in
upstream JavaScriptCore. Allocations that can fail (strings, arrays,
typed arrays, `ArrayBuffer`) still throw a `RangeError`.

**`patches/mimalloc/` is gone.** We maintain a fork, so what is worth
keeping is a commit there.

| Patches | Count | Outcome |
|---|---|---|
| The ballast | 10 | Deleted |
| `hand-out-last-freed`, `hand-out-resident-slices` | 2 | Deleted, see
below |
| `new-oom-hook`, `look-in-every-bin-of-an-exclusive-arena`,
`secure-guard-pages-are-protected`, `out-of-memory-is-no-corruption` | 4
| Commits in the fork |
| `environment-sets-sizes-and-pace-only` | 1 | A generic
`MI_GETENV_ALLOWED` in the fork. The same 16 names now live in
`scripts/build/deps/mimalloc.ts`. No change in behavior. |

### What stays

The cage reserved in segments under a limit, the `RangeError` at the
object limit, the reserve of the structure heap, and `new Worker()`
throwing a `TypeError` when there is no room for another thread.

### The two `hand-out` patches

They made the allocator hand out the memory that was freed last while an
`--aot` build runs. Because `aot` is on by default, every thread-local
`mi_free` in every Bun process paid for the test: 23 → 26 instructions
on arm64, one of them a load of a global.

`bun build --compile --aot` of `typescript/lib/_tsc.js`, macOS arm64,
canary `8d5672c55`, six runs each in turn, switched off with
`MIMALLOC_HAND_OUT_LAST_FREED=0`. Min / median / max:

| | On | Off |
|---|---|---|
| Peak footprint (MB) | 553 / 585 / 676 | 523 / 530 / 542 |
| Instructions (G) | 382.2 / 382.8 / 384.3 | 381.7 / 382.6 / 383.1 |
| Cycles (G) | 175.8 / 176.8 / 182.2 | 177.7 / 179.2 / 179.9 |
| System CPU (ms) | 1171 / 1418 / 1760 | 1213 / 1457 / 1484 |

**Not measured on Linux, or on a large program.** If they show a real
gain there, they can come back as fork commits behind a define.

### Tests removed

All of them assert survival once address space is gone, which is no
longer promised.

| File | Removed |
|---|---|
| `test/aot/heap-limit-cases.ts` | 5 tests ("... where no address space
is left", "... gets back only what malloc keeps, goes on", "... shapes
of objects and of address space in one stretch ...") |
| `test/aot/workers-within-address-space-limit-cases.ts` | 5 groups, 15
cases ("... find no room at once ...", "... has taken all the room, and
the program goes on", "wave after wave ..."), and their 4 fixtures |
| `test/aot/two-builds-give-the-same-bytes.test.ts` | 1 test that used
the deleted option as its switch |
| `test/internal/build-hardened-flags.test.ts` | 1 test about the
`hand-out` patch list. The others now check the `MI_GETENV_ALLOWED`
define. |

## 2. mimalloc v3.6.0

| Change | Why |
|---|---|
| `MIMALLOC_COMMIT` | The merge of upstream `v3.6.0` into the fork |
| `purge_holes = 56` in `src/mimalloc_sys/mimalloc.rs` | The fork's
options come after upstream's, and upstream added six. 50 is now
`profile_time_interval` |

Every other option number in that enum was compared with the merged
header and is unchanged.

To go back to v3.5.3 with the same fork commits, pin
`6da53c355e1332db3161e0749111926ed35bc986` and set `purge_holes = 50`.

What differs from upstream's defaults, and why, is in
oven-sh/mimalloc#64: cache offsets only for blocks above 512 KiB, no
purge on the thread that frees, `page_reclaim_on_free` at 0.

### Memory against the previous pin

Linux x64, 64 cores, CI builds of this branch with v3.5.3 (`6da53c35`)
and with v3.6.0 at an earlier commit (`eab19c23`, before the two changes
to the defaults), 5 interleaved rounds, medians in MB. The two builds
differ in mimalloc alone.

| Workload | Peak, v3.5.3 | Peak, v3.6.0 | After going idle, v3.5.3 |
After going idle, v3.6.0 |
|---|---|---|---|---|
| Startup | 16.7 | 16.7 | 14.4 | 14.6 |
| 3M objects, 1 in 2000 kept | 583.4 | 578.9 | 96.6 | 91.4 |
| Buffers of mixed sizes | 3170.5 | 3168.9 | 33.3 | 33.0 |
| 48 MiB buffers in a loop | 66.5 | 66.3 | 18.8 | 18.7 |
| 4 waves of 16 Workers | 582.1 | 587.9 | 36.9 | 36.1 |
| `Bun.serve` under load | 54.4 | 52.6 | 32.7 | 32.4 |
| Bundling a 17 MB file | 825.7 | 810.2 | | |

The ranges of the five runs do not overlap for the bundle peak and for
the objects after going idle. The Workers peak is noise (568 to 620
against 560 to 660).

### Costs

| | v3.5.3 | v3.6.0 |
|---|---|---|
| `mi_malloc`, arm64 instructions | 22 | 23 |
| `mi_free`, arm64 instructions | 35 | 35 |
| `sizeof(mi_theap_t)` | 8240 | 8920 |
| Binary size | | +24 KB Linux x64, +48 KB macOS arm64, +114 KB Windows
x64 |

### Not measured

Speed (the machine was busy, so the timings are rough and show no
difference), memory on macOS, Windows or arm64, and a real application.

## Testing

- macOS arm64, release build against the WebKit branch: it links, none
of the removed symbols are in the binary, allocation-heavy JS and a
Worker run, a huge `ArrayBuffer` is still a catchable `RangeError`, and
a program built with `--compile --aot` runs. That build had the ballast
removed but predates the `hand-out` removal and the move to the fork
commit.
- `test/internal/build-hardened-flags.test.ts` passes (11 of 11), and
the build scripts typecheck.
- CI on the final commit (build 134939): 180 jobs passed and none
failed. That is every build lane, every Linux test job (Debian, Ubuntu
and Alpine, x64 and arm64), the 20 ASAN jobs, the hardened lane,
`cargo-test`, and Windows x64 and arm64.
- **Not run:** the macOS test jobs. No machine picked them up in any
build of this PR. The macOS build lanes pass, and the fork's own CI
passes on macOS.
- Five tests needed a retry (`testFFI`, `no-leak-records-at-exit`,
`native-contract`, `decorators`). Each needed one as often in build
134852, which is `main` with a one-line fix.
- `test/js/bun/wasm/compile-rss.test.ts` failed once and passed on retry
in an earlier build with v3.6.0 on Alpine arm64: 10.8 MiB kept against a
limit of 10. It did not in the final build. Its fixture, 24 runs per
version on Linux x64, shows no difference (idle median -0.7 against -0.8
MiB, none over the limit).
- The engine's own tests passed on Linux x64 and arm64 in
oven-sh/WebKit#937, the hardened AOT lanes among them.
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.