Repository navigation
Conversation
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.
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)
|
|
Preview build of a063934: |
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.
Draft for a maintainer's decision. Does #937 ("An infallible
mallocthat 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
ulimit -v, a full collection that removes dead Symbols ends withpanic(main thread): Segmentation fault at address 0xBBADBEEFwhen malloc refuses a smaller table. Frames:HashTable::rehash,WeakGCMap::reconcileWeakReferencesAtGCEnd.HashTable::rehash()(Source/WTF/wtf/HashTable.h:1440) asksallocateTable(), which may not fail, for the smaller table. The table is valid without it.Fix
rehash()takes aFailureAction, asVectordoes. Growth passesCrash. Both shrinks passReportand keep the table when refused.testaotcases.mainfails all 7.Background
WTF::HashTableis behind everyHashMap,HashSetandWeakGCMap(a map that collections prune). Removal shrinks it below 1/6 full, into a second table.removeIfforWeakGCMapalone, as [JSC] AOT: handlers at the end of the stack, text constants in flow, samples in native code #934's growth is. Every other table keeps the request.Downsides
remove()below 1/6 asks again: removing 1,300,000 interned strings takes 3.0 s of CPU, not 0.17.0xBBADBEEF:WeakMap,WeakSet,add()into a table that deleted slots fill..text: +4,864 bytes without LTO, -4,240 with.Notes
The decision
mallocthat fails crashes on purpose again, as it does without compiled code and in upstream JavaScriptCore." Bun'stest/aot/heap-limit-cases.tssays of the endings it accepts: "A collection makes such requests on its own threads, and they end the process on purpose, as in JavaScriptCore ([JSC] AOT: remove the address space ballast #937)."MarkedBlockSet::remove(), which this change covers. Its notes put rows 2 and 3 inm_freeBlockIndices.append()(BlockDirectory::removeBlock()), which this change does not touch. The two changes were not measured together on builds.--aotis in no release.mfbt/HashTable.h,shrinkIfUnderloaded():(void)changeTableSize(capacity() / 2, DontReportFailure);).The programs
Linux x64, Bun at oven-sh/bun#48297's head with this engine built from source (release, C++ without LTO).
mainisab89cb619d, the base of this branch. 10 runs each underulimit -v 1433600,mainand this change in turn.bun build --compile --aotmainprogram 1600000 4000000xBBADBEEFin 10 of 10went on true, exit 0, in 10 of 10program 1600000 250000program 1600000 200000(no shrink is due)Bun.gc(true): 400,000 Symbols go, then the program makes small objectsSymbol()until there are 1,600,000ulimit -v 1310720and1835008bun program.ts 1600000 905000(no--aot)mainends at isrdx=0x4400010: 68 MiB and 16 bytes, the table of 2^22 slots.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 thattryExpand()asks for next finds memory, and a later collection shrinks the table to 2,097,152 slots.mainand in 2 of 20 with this change.Tests
WTF_HashTable:ShrinkIsOneRequestThatMayFail,ShrinkThatFindsNoMemoryLeavesTable,ShrinkThatFindsNoMemoryKeepsLifetimesBalanced,RemovalMakesNoRequestThatMayNotFailandRandomOperationsStayConsistentWhileShrinksFindNoMemory. Their table has aMallocthat counts requests and can refusetryMalloc(). Withmain's header the shrink goes throughmalloc(), and all five fail.heap-cage/tables/a-table-of-symbols-that-finds-no-memory-to-shrink-stays-as-it-wasmakes 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-wasdoes the same forMarkedBlockSet::remove(). Onmainboth end at0xBBADBEEFin 10 of 10 runs (run without the two reads ofWeakGCMap::size()andcapacity(), 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.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.testaot: 4,700 passed on x64 and 4,684 on arm64, 0 failed.rehash()has no defaultFailureAction. 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 toremove()andremoveIf().What the change costs
Local builds of Bun (release, C++ without LTO),
mainagainst this change.bloaty,straceandperfare not installed on this machine. The numbers are fromllvm-size,llvm-nm,llvm-objdump, a counter of refusedmmapcalls in anLD_PRELOADlibrary, andperf_event_open..text111,385,984 to 111,390,848 bytes (+4,864). The stripped binary grows by 4,096 bytes. 204rehashsymbols (126,731 bytes) become 204rehash<FailureAction::Report>(132,696 bytes, 29 bytes more each) and onerehash<FailureAction::Crash>(560 bytes).rehashbodies and 17 functions that have a shrink in line, all of compile phases (DFG, B3, the bytecode generator, the ahead-of-time compiler).jscof CI'sbun-webkit-linux-amd64-lto-aotlane,mainagainst this branch):.text39,444,851 to 39,440,611 bytes (-4,240). 47,568 of 47,718 function bodies are identical. 57rehashbodies (44,024 bytes) become 57rehash<FailureAction::Report>(42,583 bytes) and onerehash<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.HashSet<void*>, 10^6 operations, 3 runs each in turn (A/A spread at most 300 of 10^8): 118.5 for eachadd()before and after, 60.4 for eachremove()without a shrink before and after, 15 more for each shrink.tryMalloc()and onefree()(it was onemalloc()and onefree()). Growth is unchanged. A refused shrink is one request. The tests hold these counts.mmapcalls.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 refusedmmapcalls each), 2.9 to 3.1 s of CPU in that collection, against 0.17 to 0.21 s with memory.mainends at0xBBADBEEFthere.random()takes 25,452,660 (226).random()has one caller,CodeCacheMap::pruneSlowCase().HashTable.h: 17 lines added, 7 removed.Not in this change
WeakMapImpl(JSWeakMapandWeakSet) andRobinHoodHashTable(the RegExp cache) have arehash()of their own with the same request. Plain Bun with 1,600,000WeakMapkeys, of which 1,500,000 go:0xBBADBEEFin 6 of 6 runs onmainand in 5 of 6 with this change.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 throughtryExpand(), which may fail. SpiderMonkey has a rehash in place for this case (rehashTableInPlace()).MarkedBlockSet::remove()keeps its filter as it was: the filter is made again where the capacity changes.test/aot/workers-within-address-space-limit-cases.ts) need this engine. They come with the pin.