Skip to content

Commit e1a28d3

Browse files
committed
[JSC] Fix leftover marks after the marking version wraps, and test the wrap
https://bugs.webkit.org/show_bug.cgi?id=328420 rdar://190682109 Reviewed by Keith Miller. When a heap's marking version wraps, every block is reset to nullVersion, and a block whose marks were current keeps them as the survivors. Since 181146@main (2016), staleMarksStillRecordSurvivors(), previously named marksConveyLivenessDuringMarking(), has treated nullVersion as recording survivors in every Full collection, not just the one that wrapped. A block that collection does not reach keeps its old marks through sweeping and reuse, and the next Full collection takes them for survivors. If the block is being allocated from, that trips a Debug assert in resetStaleMarks(), or a release assert if the block is swept during marking. If not, its freed cells count as live, so a stale stack pointer can make the conservative scan visit one. Make initialVersion 1, which is nextVersion(nullVersion), and drop the nullVersion case. Now one rule covers every Full collection: a block's stale marks record the survivors only if its version is one behind the heap's. A wrap keeps this true: it sets every block to nullVersion, one behind initialVersion, and clears the marks of blocks without survivors. Between wraps, versions only increase, so an old version never matches a new one. The wrap was never tested, because it takes about 2^32 Full collections: 50 days even at one per millisecond, and over a century at one per second. Add an option, initialMarkingVersion, to start near it. The collect-continuously stress modes now start up to 32 Full collections before the wrap, and NoCJITValidatePhases, which also runs in Debug builds, wraps on each test's second Full collection, so the first has populated the marks. Without the fix, over 300 existing stress tests fail in these modes. The new test hits the Debug assert; JS cannot create the stale stack pointer the other case needs. Test: JSTests/stress/marking-version-wrap-reused-block.js * JSTests/stress/marking-version-wrap-reused-block.js: Added. (makeObject): * Source/JavaScriptCore/heap/MarkedBlock.cpp: (JSC::MarkedBlock::resetMarksForVersionWrap): * Source/JavaScriptCore/heap/MarkedBlock.h: * Source/JavaScriptCore/heap/MarkedBlockInlines.h: (JSC::MarkedBlock::staleMarksStillRecordSurvivors): * Source/JavaScriptCore/heap/MarkedSpace.cpp: (JSC::startingMarkingVersion): (JSC::MarkedSpace::MarkedSpace): (JSC::MarkedSpace::adoptMarkingVersion): * Source/JavaScriptCore/heap/MarkedSpace.h: (JSC::MarkedSpace::nextVersion): * Source/JavaScriptCore/runtime/OptionsList.h: * Tools/Scripts/run-jsc-stress-tests: Canonical link: https://commits.webkit.org/324475@main
1 parent a5ec4c1 commit e1a28d3

8 files changed

Lines changed: 65 additions & 21 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//@ runDefault("--initialMarkingVersion=4294967294")
2+
3+
// Starts each heap two Full collections short of the marking version's wrap. The wrap collection resets every block to
4+
// nullVersion, keeping the marks of blocks whose marks were current, as the survivors of the collection before. A block
5+
// that collection does not reach has no live cells left, and allocation reuses it with those marks still in it; the next
6+
// Full collection must not take them for survivors. fullGC() leaves sweeping to allocation, so the empty blocks stay.
7+
8+
function makeObject(i) {
9+
return { a0: i, a1: i, a2: i, a3: i, a4: i, a5: i, a6: i, a7: i, a8: i, a9: i,
10+
a10: i, a11: i, a12: i, a13: i, a14: i, a15: i, a16: i, a17: i, a18: i, a19: i };
11+
}
12+
13+
let objects = [];
14+
for (let i = 0; i < 10000; ++i)
15+
objects.push(makeObject(i));
16+
fullGC(); // Marks these objects' blocks at the last version before the wrap.
17+
18+
objects = null;
19+
fullGC(); // The wrap collection, which reaches none of those blocks.
20+
21+
let survivors = [];
22+
for (let i = 0; i < 1000; ++i)
23+
survivors.push(makeObject(i));
24+
fullGC(); // Reaches the new objects, in reused blocks that still hold the old marks.
25+
26+
for (let i = 0; i < survivors.length; ++i) {
27+
if (survivors[i].a19 !== i)
28+
throw new Error("Bad object at " + i);
29+
}

‎Source/JavaScriptCore/heap/MarkedBlock.cpp‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,16 +358,20 @@ void MarkedBlock::resetNewlyAllocatedForVersionWrap()
358358
header().m_newlyAllocatedVersion = MarkedSpace::nullVersion;
359359
}
360360

361-
// Called when the heap's marking version is about to wrap, at a collections's Begin. Keeps the marks of the
361+
// Called when the heap's marking version is about to wrap, at a collection's Begin. Keeps the marks of the
362362
// survivors, those from the version about to be replaced, clears any older stale marks,
363-
// and sets the version to nullVersion which staleMarksStillRecordSurvivors() has a special case for.
363+
// and sets the version to nullVersion, one behind initialVersion, the version this collection moves to.
364364
void MarkedBlock::resetMarksForVersionWrap()
365365
{
366366
if (areMarksStale())
367367
header().m_marks.clearAll();
368368
header().m_markingVersion = MarkedSpace::nullVersion;
369369
}
370370

371+
// staleMarksStillRecordSurvivors() relies on this to find the survivors of the blocks resetMarksForVersionWrap()
372+
// leaves at nullVersion.
373+
static_assert(MarkedSpace::nextVersion(MarkedSpace::nullVersion) == MarkedSpace::initialVersion);
374+
371375
#if ASSERT_ENABLED
372376
void MarkedBlock::assertMarksNotStale()
373377
{

‎Source/JavaScriptCore/heap/MarkedBlock.h‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,9 @@ class MarkedBlock {
328328
// so far; during a Full collection, only what that collection has marked so far.
329329
// - Stale but still the survivors: only during a Full collection, whose Begin
330330
// makes every block's marks stale while the survivors are still live. Marks one
331-
// version behind, or at nullVersion after a wrap, keep recording survivors
332-
// until resetStaleMarks() moves them into m_newlyAllocated (see
333-
// staleMarksStillRecordSurvivors()). An Eden collection never needs this, since
334-
// it does not advance the marking version.
331+
// version behind keep recording survivors until resetStaleMarks() moves them
332+
// into m_newlyAllocated (see staleMarksStillRecordSurvivors()). An Eden collection
333+
// never needs this, since it does not advance the marking version.
335334
// - Otherwise: meaningless.
336335
//
337336
// m_newlyAllocated, usable while current and the block is not free-listed, holds

‎Source/JavaScriptCore/heap/MarkedBlockInlines.h‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,18 @@ inline bool MarkedBlock::staleMarksStillRecordSurvivors(HeapVersion myMarkingVer
7575
{
7676
// Whether this block's stale marks still record the survivors. Only during a Full
7777
// collection, whose Begin made them stale:
78-
// - nullVersion: yes. A new block has no survivors and marks are clear; a block reset
79-
// at a version wrap kept the marks as survivors.
8078
// - Exactly one version behind: yes. These are the marks made since the previous Full
81-
// collection began, which are the survivors.
79+
// collection began, which are the survivors. That includes a block this collection's
80+
// version wrap reset to nullVersion, one behind initialVersion; a new block counts
81+
// too in that collection, which is harmless, since its marks are clear.
8282
// - Anything older: no. The block has no survivors.
8383
// An Eden collection does not advance the marking version, so marks stale then
8484
// predate the last Full collection. Between collections, marks one version behind
8585
// mean the last Full collection did not mark that block. Neither records survivors.
8686
ASSERT(space()->isMarking());
8787
if (heap()->collectionScope() != CollectionScope::Full)
8888
return false;
89-
return myMarkingVersion == MarkedSpace::nullVersion
90-
|| MarkedSpace::nextVersion(myMarkingVersion) == markingVersion;
89+
return MarkedSpace::nextVersion(myMarkingVersion) == markingVersion;
9190
}
9291

9392
inline bool MarkedBlock::Handle::isAllocated()

‎Source/JavaScriptCore/heap/MarkedSpace.cpp‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,16 @@ void MarkedSpace::initializeSizeClassForStepSize()
179179
});
180180
}
181181

182+
// Options::initialMarkingVersion() lets tests start a heap near the version wrap.
183+
static HeapVersion startingMarkingVersion()
184+
{
185+
if (unsigned version = Options::initialMarkingVersion())
186+
return version;
187+
return MarkedSpace::initialVersion;
188+
}
189+
182190
MarkedSpace::MarkedSpace(JSC::Heap* heap)
191+
: m_markingVersion(startingMarkingVersion())
183192
{
184193
ASSERT_UNUSED(heap, heap == &this->heap());
185194
initializeSizeClassForStepSize();
@@ -463,12 +472,12 @@ void MarkedSpace::endMarking()
463472
}
464473

465474
// For a space that has never been marked: each collection advances either the marking version or
466-
// the Eden version, so both are still initial, and every block is at nullVersion, which matches
467-
// neither the old version nor the new one, so no block's marks change meaning.
475+
// the Eden version, so both are still at their start versions, and every block is at nullVersion,
476+
// which matches neither the old version nor the new one, so no block's marks change meaning.
468477
void MarkedSpace::adoptMarkingVersion(HeapVersion version)
469478
{
470479
ASSERT(!m_isMarking);
471-
ASSERT(m_markingVersion == initialVersion && m_edenVersion == initialVersion);
480+
ASSERT(m_markingVersion == startingMarkingVersion() && m_edenVersion == initialVersion);
472481
m_markingVersion = version;
473482
}
474483

‎Source/JavaScriptCore/heap/MarkedSpace.h‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ class MarkedSpace {
6868
// We have an extra size class for size zero.
6969
static constexpr size_t numSizeClasses = largeCutoff / sizeStep + 1;
7070

71-
// Versions identify which collection a block's bits belong to. A heap's versions are
72-
// never nullVersion or nextVersion(nullVersion): they start at initialVersion and
73-
// wrap back to it. So a block at nullVersion (new, or reset at a wrap) can never look
74-
// current, or look one version behind, by accident.
71+
// Versions identify which collection a block's bits belong to. A heap's versions start
72+
// at initialVersion and wrap back to it, never reaching nullVersion, so a block at
73+
// nullVersion (new, or reset at a wrap) never looks current. initialVersion is
74+
// nextVersion(nullVersion), so a block a wrap resets is one version behind the
75+
// version the wrap moves to, and its kept marks still record the survivors.
7576
static constexpr HeapVersion nullVersion = 0;
76-
static constexpr HeapVersion initialVersion = 2;
77+
static constexpr HeapVersion initialVersion = 1;
7778

78-
static HeapVersion nextVersion(HeapVersion version)
79+
static constexpr HeapVersion nextVersion(HeapVersion version)
7980
{
8081
version++;
8182
if (version == nullVersion)

‎Source/JavaScriptCore/runtime/OptionsList.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ bool hasCapacityToUseLargeGigacage();
209209
v(Bool, useConcurrentGC, true, Normal, nullptr) \
210210
v(Bool, collectContinuously, false, Normal, nullptr) \
211211
v(Double, collectContinuouslyPeriodMS, 1, Normal, nullptr) \
212+
v(Unsigned, initialMarkingVersion, 0, Restricted, "If nonzero, the marking version each heap starts at. A value near the maximum makes the version wrap within a few Full collections."_s) \
212213
v(Bool, forceFencedBarrier, false, Normal, nullptr) \
213214
v(Bool, verboseVisitRace, false, Normal, nullptr) \
214215
v(Bool, verboseHeapSnapshotLogging, true, Normal, nullptr) \

‎Tools/Scripts/run-jsc-stress-tests‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ def shouldCollectContinuously?
793793
$buildType == "release" or $forceCollectContinuously
794794
end
795795

796-
COLLECT_CONTINUOUSLY_OPTIONS = shouldCollectContinuously? ? ["--collectContinuously=true", "--useGenerationalGC=false", "--verifyGC=true"] : []
796+
COLLECT_CONTINUOUSLY_OPTIONS = shouldCollectContinuously? ? ["--collectContinuously=true", "--useGenerationalGC=false", "--verifyGC=true", "--initialMarkingVersion=#{2**32 - 1 - rand(32)}"] : []
797797

798798
$serialPlans = Set.new
799799
$exclusivePlans = Set.new
@@ -1423,6 +1423,8 @@ BASE_MODES = [
14231423
"--useSourceProviderCache=false",
14241424
"--useRandomizingExecutableIslandAllocation=true",
14251425
"--useLLIntICs=false",
1426+
# Wrap on the second Full collection, so the first has populated the marks.
1427+
"--initialMarkingVersion=#{2**32 - 2}",
14261428
] +
14271429
NO_CJIT_OPTIONS
14281430
],

0 commit comments

Comments
 (0)