From 6cb6d084806c7323bcdf199c416bb9089b4e8a82 Mon Sep 17 00:00:00 2001 From: Dylan Conway Date: Sat, 8 Aug 2026 20:53:28 -0700 Subject: [PATCH 1/3] [JSC] ConservativeRoots: only apply the past-the-end butterfly rule in blocks that can hold butterflies genericAddPointer() ends with a rule for butterfly pointers that point just past the previous object: a candidate in [cellStart, cellStart + sizeof(IndexingHeader)] that did not resolve to a live cell marks the cell to its left. The equivalent cross-block case a few lines up is gated on mayHaveIndexingHeader(cellKind); the within-block case was not, so it also fired in plain JSCell blocks, where no butterfly can live and a pointer at or just past a (dead) cell's start says nothing about its left neighbour. Effect in a JSCell block today: any stack/register word equal to deadCell or deadCell+8 pins the live cell immediately before it. Observed in Bun: a callee-saved register spilled by a long-lived native frame held such a value for the whole program, so a JSArray allocated next to that slot survived every full collection until the frame unwound. Gate the rule on mayHaveIndexingHeader(cellKind) like its sibling. --- Source/JavaScriptCore/heap/ConservativeRoots.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Source/JavaScriptCore/heap/ConservativeRoots.cpp b/Source/JavaScriptCore/heap/ConservativeRoots.cpp index 66f4930ff6444..39ed81adb2d9f 100644 --- a/Source/JavaScriptCore/heap/ConservativeRoots.cpp +++ b/Source/JavaScriptCore/heap/ConservativeRoots.cpp @@ -186,8 +186,9 @@ inline void ConservativeRoots::genericAddPointer(char* pointer, HeapVersion mark return; // Also, a butterfly could point at the end of an object plus sizeof(IndexingHeader). In that - // case, this is pointing to the object to the right of the one we should be marking. - if (candidate->candidateAtomNumber(alignedPointer) > 0 && pointer <= alignedPointer + sizeof(IndexingHeader)) + // case, this is pointing to the object to the right of the one we should be marking. As with the + // previous-block case above, only blocks whose cells can carry an IndexingHeader have such pointers. + if (mayHaveIndexingHeader(cellKind) && candidate->candidateAtomNumber(alignedPointer) > 0 && pointer <= alignedPointer + sizeof(IndexingHeader)) tryPointer(alignedPointer - candidate->cellSize()); } From e501c5cbc6c5e6492b2b39b86eae29ec4657559c Mon Sep 17 00:00:00 2001 From: Dylan Conway Date: Sat, 8 Aug 2026 22:15:47 -0700 Subject: [PATCH 2/3] [JSC] ConservativeRoots: no past-the-end slack for precise allocations that cannot hold a butterfly either The precise-allocation branch of genericAddPointer() uses PreciseAllocation::contains(), which accepts pointers up to sizeof(IndexingHeader) past the end of the cell for the same butterfly-end-pointer reason. It applied that slack to every precise allocation, including lower-tier precise cells of plain JSCell types (the first few cells of each IsoSubspace), so a word equal to cellEnd..cellEnd+8 pinned such a cell. That is the case actually hit in Bun (a 16-byte lower-tier-precise JSArray at ...c8, word == cell+24): gate the slack on mayHaveIndexingHeader(cellKind) here too, and treat only [cell, cellEnd) as a reference otherwise. --- Source/JavaScriptCore/heap/ConservativeRoots.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Source/JavaScriptCore/heap/ConservativeRoots.cpp b/Source/JavaScriptCore/heap/ConservativeRoots.cpp index 39ed81adb2d9f..1fc49558215a4 100644 --- a/Source/JavaScriptCore/heap/ConservativeRoots.cpp +++ b/Source/JavaScriptCore/heap/ConservativeRoots.cpp @@ -117,8 +117,14 @@ inline void ConservativeRoots::genericAddPointer(char* pointer, HeapVersion mark [] (PreciseAllocation** ptr) -> PreciseAllocation* { return *ptr; }); if (result) { auto attemptLarge = [&] (PreciseAllocation* allocation) { - if (allocation->contains(pointer) && allocation->hasValidCell()) - markFoundGCPointer(allocation->cell(), allocation->attributes().cellKind); + // contains() accepts up to sizeof(IndexingHeader) past the end for butterfly end + // pointers; a cell that cannot carry an IndexingHeader is only referenced from within. + HeapCell::Kind kind = allocation->attributes().cellKind; + bool inBounds = mayHaveIndexingHeader(kind) + ? allocation->contains(pointer) + : (allocation->aboveLowerBound(pointer) && pointer < std::bit_cast(allocation->cell()) + allocation->cellSize()); + if (inBounds && allocation->hasValidCell()) + markFoundGCPointer(allocation->cell(), kind); }; if (result > m_heap.objectSpace().preciseAllocationsForThisCollectionBegin()) From 9b999ae9d4c4de5d5bc1d1f8414952cf5afccf95 Mon Sep 17 00:00:00 2001 From: Dylan Conway Date: Sun, 9 Aug 2026 00:05:45 -0700 Subject: [PATCH 3/3] [JSC] ConservativeRoots: keep the exact one-past-the-end pointer for non-butterfly precise cells Review feedback: dropping [end, end+8] for JSCell-kind precise allocations also dropped the exact one-past-the-end pointer, which compiler-rewritten loops over trailing in-cell storage (DirectArguments, JSLexicalEnvironment, JSBigInt digits, large JSFinalObject inline storage, Wasm instances) can legitimately materialise. Only the +1..+8 IndexingHeader slack is butterfly-specific, so accept [cell, end] and reject (end, end+8]. --- Source/JavaScriptCore/heap/ConservativeRoots.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/JavaScriptCore/heap/ConservativeRoots.cpp b/Source/JavaScriptCore/heap/ConservativeRoots.cpp index 1fc49558215a4..dbe4cd64534bf 100644 --- a/Source/JavaScriptCore/heap/ConservativeRoots.cpp +++ b/Source/JavaScriptCore/heap/ConservativeRoots.cpp @@ -118,11 +118,11 @@ inline void ConservativeRoots::genericAddPointer(char* pointer, HeapVersion mark if (result) { auto attemptLarge = [&] (PreciseAllocation* allocation) { // contains() accepts up to sizeof(IndexingHeader) past the end for butterfly end - // pointers; a cell that cannot carry an IndexingHeader is only referenced from within. + // pointers; for a cell that cannot carry an IndexingHeader accept at most one-past-the-end. HeapCell::Kind kind = allocation->attributes().cellKind; bool inBounds = mayHaveIndexingHeader(kind) ? allocation->contains(pointer) - : (allocation->aboveLowerBound(pointer) && pointer < std::bit_cast(allocation->cell()) + allocation->cellSize()); + : (allocation->aboveLowerBound(pointer) && pointer <= std::bit_cast(allocation->cell()) + allocation->cellSize()); if (inBounds && allocation->hasValidCell()) markFoundGCPointer(allocation->cell(), kind); };