Skip to content

[WTF] HashTable: keep the table when a shrink finds no memory - #1044

Draft
robobun wants to merge 1 commit into
mainfrom
robobun/87a697c9/hashtable-shrink-may-fail
Draft

robobun wants to merge 1 commit into
mainfrom
robobun/87a697c9/hashtable-shrink-may-fail

Conversation

@robobun

@robobun robobun commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Draft for a maintainer's decision. Does #937 ("An infallible malloc that fails crashes on purpose") cover a request that only gives memory back? This change says no, for every table. #1034 is the other open answer. No user report.

Problem

  • Under ulimit -v, a full collection that removes dead Symbols ends with panic(main thread): Segmentation fault at address 0xBBADBEEF when malloc refuses a smaller table. Frames: HashTable::rehash, WeakGCMap::reconcileWeakReferencesAtGCEnd.
  • HashTable::rehash() (Source/WTF/wtf/HashTable.h:1440) asks allocateTable(), which may not fail, for the smaller table. The table is valid without it.

Fix

  • rehash() takes a FailureAction, as Vector does. Growth passes Crash. Both shrinks pass Report and keep the table when refused.
  • Correct: a table below its smallest load is valid.
  • Verified: 5 new TestWTF tests and 2 new testaot cases. main fails all 7.
  • Self-reviewed: 9 concerns raised, 8 addressed. Not done: fewer requests after a refusal.

Background

Downsides

  • Every table keeps its size where malloc refuses the smaller one, plain Bun included. The report's Symbol table keeps 142,606,352 bytes, not 71,303,184.
  • While malloc refuses, each remove() below 1/6 asks again: removing 1,300,000 interned strings takes 3.0 s of CPU, not 0.17.
  • Still ending at 0xBBADBEEF: WeakMap, WeakSet, add() into a table that deleted slots fill. .text: +4,864 bytes without LTO, -4,240 with.
Notes

The decision

The programs

Linux x64, Bun at oven-sh/bun#48297's head with this engine built from source (release, C++ without LTO). main is ab89cb619d, the base of this branch. 10 runs each under ulimit -v 1433600, main and this change in turn.

// program.ts
const [count, dropped] = process.argv.slice(2).map(Number);
const symbols: (symbol | null)[] = new Array(count);
for (let i = 0; i < count; i++) symbols[i] = Symbol();
const held: ArrayBuffer[] = [];
for (const megabytes of [64, 32, 16, 8]) {
  try { for (;;) held.push(new ArrayBuffer(megabytes * 1024 * 1024)); } catch {}
}
console.log("filled", held.length);
for (let i = 0; i < dropped; i++) symbols[i] = null;
Bun.gc(true);
console.log("went on", symbols[count - 1] !== undefined);
Program, built with bun build --compile --aot main This change
program 1600000 400000 0xBBADBEEF in 10 of 10 went on true, exit 0, in 10 of 10
program 1600000 250000 10 of 10 went on in 10 of 10
program 1600000 200000 (no shrink is due) went on in 10 of 10 went on in 10 of 10
No Bun.gc(true): 400,000 Symbols go, then the program makes small objects 10 of 10 went on in 10 of 10
1,560,000 Symbols held, 1,000,000 go, then Symbol() until there are 1,600,000 10 of 10 no error, went on in 10 of 10
The same under ulimit -v 1310720 and 1835008 5 of 5 each went on in 5 of 5 each
Plain bun program.ts 1600000 905000 (no --aot) 10 of 10 went on in 10 of 10
  • The request that main ends at is rdx=0x4400010: 68 MiB and 16 bytes, the table of 2^22 slots.
  • In the fifth row the collection is the one that MarkedBlockCage::willAddSymbol() starts where the table finds no room to grow. With this change the table keeps its 4,194,304 slots through that collection (572,864 keys, 1,000,000 deleted slots). The table of the same size that tryExpand() asks for next finds memory, and a later collection shrinks the table to 2,097,152 slots.
  • Plain Bun with nothing let go ends at another request that may not fail in 4 of 20 runs on main and in 2 of 20 with this change.

Tests

  • TestWTF, WTF_HashTable: ShrinkIsOneRequestThatMayFail, ShrinkThatFindsNoMemoryLeavesTable, ShrinkThatFindsNoMemoryKeepsLifetimesBalanced, RemovalMakesNoRequestThatMayNotFail and RandomOperationsStayConsistentWhileShrinksFindNoMemory. Their table has a Malloc that counts requests and can refuse tryMalloc(). With main's header the shrink goes through malloc(), and all five fail.
  • heap-cage/tables/a-table-of-symbols-that-finds-no-memory-to-shrink-stays-as-it-was makes 16,000 Symbols, lets 12,000 go, holds every large piece of malloc and runs a full collection. heap-cage/cage/set-of-blocks-without-memory-to-shrink-stays-as-it-was does the same for MarkedBlockSet::remove(). On main both end at 0xBBADBEEF in 10 of 10 runs (run without the two reads of WeakGCMap::size() and capacity(), which are new). With this change: the Symbol case passes in 1,000 of 1,000 runs one at a time and in 9,000 of 9,000 with 36 at once, and the case of the set of blocks in 1,000 of 1,000 and 3,000 of 3,000.
  • The Symbol case calls Thread::setSurvivesFailedCreation(), as a compiled program does. Without it the thread of the collection could not be started in 15 of 1,000 runs.
  • testaot native heap-cage: 65 passed (main: 63 passed, the 2 new cases failed). testaot native: 2,088 passed, 0 failed, 43 todo.
  • The whole TestWTF of the JSCOnly port, built here against this tree: 2,199 passed, 0 failed.
  • CI of this branch: 85 of 85 jobs passed. The five TestWTF tests pass on Linux x64 and arm64. testaot: 4,700 passed on x64 and 4,684 on arm64, 0 failed.
  • rehash() has no default FailureAction. A merge that adds a call without one does not compile. Update the hash table re-sizing policy to account for deleted buckets WebKit/WebKit#79883 (open upstream) adds a rehash of the same size to remove() and removeIf().

What the change costs

Local builds of Bun (release, C++ without LTO), main against this change. bloaty, strace and perf are not installed on this machine. The numbers are from llvm-size, llvm-nm, llvm-objdump, a counter of refused mmap calls in an LD_PRELOAD library, and perf_event_open.

  • Size: .text 111,385,984 to 111,390,848 bytes (+4,864). The stripped binary grows by 4,096 bytes. 204 rehash symbols (126,731 bytes) become 204 rehash<FailureAction::Report> (132,696 bytes, 29 bytes more each) and one rehash<FailureAction::Crash> (560 bytes).
  • Unchanged paths: 149,161 of 149,301 function bodies are identical once addresses and padding are taken out of the disassembly. The others are the 123 distinct rehash bodies and 17 functions that have a shrink in line, all of compile phases (DFG, B3, the bytecode generator, the ahead-of-time compiler).
  • With LTO (jsc of CI's bun-webkit-linux-amd64-lto-aot lane, main against this branch): .text 39,444,851 to 39,440,611 bytes (-4,240). 47,568 of 47,718 function bodies are identical. 57 rehash bodies (44,024 bytes) become 57 rehash<FailureAction::Report> (42,583 bytes) and one rehash<FailureAction::Crash> (651 bytes). The other 93 functions have a shrink in line and are 880 instructions shorter in total. An LTO build of Bun was not made here.
  • Instructions, HashSet<void*>, 10^6 operations, 3 runs each in turn (A/A spread at most 300 of 10^8): 118.5 for each add() before and after, 60.4 for each remove() without a shrink before and after, 15 more for each shrink.
  • Requests: a shrink with memory is one tryMalloc() and one free() (it was one malloc() and one free()). Growth is unchanged. A refused shrink is one request. The tests hold these counts.
  • One refused request in the first program: 16 refused mmap calls.
  • A table that could not shrink asks again at every remove() below 1/6. Plain Bun with 1,600,000 interned strings, of which a collection removes 1,300,000: 397,701 refused requests (22 refused mmap calls each), 2.9 to 3.1 s of CPU in that collection, against 0.17 to 0.21 s with memory. main ends at 0xBBADBEEF there.
  • Not done: a rule that asks again only where the key count passes the threshold of a smaller table. It bounds the refused requests of such a collection by the number of times the key count halves. It also makes a table with reserved capacity keep that capacity longer while malloc has memory, so it is a change for programs that never meet a limit.
  • The table of Symbols after a refused shrink keeps 8,388,608 slots (142,606,352 bytes). With memory it has 4,194,304 slots (71,303,184 bytes).
  • A table left at 2^20 slots with one key: one iteration takes 918,947 instructions (99 after a shrink), and one random() takes 25,452,660 (226). random() has one caller, CodeCacheMap::pruneSlowCase().
  • HashTable.h: 17 lines added, 7 removed.

Not in this change

  • WeakMapImpl (JS WeakMap and WeakSet) and RobinHoodHashTable (the RegExp cache) have a rehash() of their own with the same request. Plain Bun with 1,600,000 WeakMap keys, of which 1,500,000 go: 0xBBADBEEF in 6 of 6 runs on main and in 5 of 6 with this change.
  • Deleted slots count as full ones. A table that could not shrink keeps them until a rehash, and the rehash of the same size in add() is still a request that may not fail. Under a limit on address space the table of Symbols and the set of blocks ask through tryExpand(), which may fail. SpiderMonkey has a rehash in place for this case (rehashTableInPlace()).
  • After a refused shrink MarkedBlockSet::remove() keeps its filter as it was: the filter is made again where the capacity changes.
  • Bun's rows for these programs (test/aot/workers-within-address-space-limit-cases.ts) need this engine. They come with the pin.

remove() and removeIf() shrink a table once less than a sixth of its
slots are full. The shrink asked for the smaller table with a request
that may not fail, although the table is valid before the request. A
process with no memory for the smaller table ended there at 0xBBADBEEF.
Under a limit on address space the end of a full collection does this
for the table of Symbols and for the heap's set of blocks.

rehash() takes a FailureAction, as Vector does. shrink() and
shrinkToBestSize() pass FailureAction::Report: a refused request returns
before the table changes. Growth passes FailureAction::Crash. There is
no default, so a new call has to say which it is. With memory the
capacities and the number of requests stay the same.

A table below its smallest load is a valid table, so
checkTableConsistency() no longer asserts !shouldShrink().

Tests: five in TestWTF (WTF_HashTable) and two in testaot (heap-cage).
WeakGCMap::size() and capacity() are for the test of the table of
Symbols.
@robobun

robobun commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

Status of #1044: draft. It waits for a maintainer's decision on the question in the description (also asked on #1034).

How the crash was reproduced, Linux x64, Bun at oven-sh/bun#48297's head with this engine built from source:

bun build --compile --aot program.ts --outfile program   # program.ts is in the Notes of the description
(ulimit -c 0; ulimit -v 1433600; ./program 1600000 400000)
  • main (ab89cb619d): filled 17, then panic(main thread): Segmentation fault at address 0xBBADBEEF, exit 139, in 10 of 10 runs.
  • This branch (a06393497f): filled 17, went on true, exit 0, in 10 of 10 runs.
  • The two new testaot native heap-cage cases end at 0xBBADBEEF on main in 10 of 10 runs and pass on this branch.

@github-actions

Copy link
Copy Markdown

Preview build of a063934: autobuild-preview-pr-1044-a0639349

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.

1 participant