Assertion failure: retainedBytes_ >= nbytes @ gc/Scheduling.h:689
Categories
(Core :: JavaScript: GC, defect, P2)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox155 | --- | fixed |
People
(Reporter: x0e, Assigned: jonco)
References
(Blocks 1 open bug)
Details
(Keywords: reporter-external)
Attachments
(1 external file)
Steps to reproduce:
engine version : 9f2e8c25f0b40fdce8d2f2ca40281e8711815a6d
engine version date : 2026-07-23T15:46:19Z
build date : 2026-07-24T10:32:44
Shell args:
--fuzzing-safe <input.js>
Test input:
for (const v43 of [false, true])
f();
while ((() => {}))
gcslice(
10000,
);
function ccwToRegistry() {
return evalcx("new FinalizationRegistry(value => {})",
newGlobal({newCompartment : true}));
}
function f() {
let registry = ccwToRegistry();
for (let i94 = 0; i94 < 500; ++i94) {
let heldValue = {};
oomTest(() => registry.register(heldValue, 1));
}
}
Backtrace:
[2966871] Assertion failure: retainedBytes_ >= nbytes, at spidermonkey/js/src/gc/Scheduling.h:689
#01: ???[targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/dist/bin/js +0x24b970c]
#02: ???[targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/dist/bin/js +0x1e5c14b]
#03: ???[targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/dist/bin/js +0x1e2f0fc]
#04: ???[targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/dist/bin/js +0x24982ff]
#05: ???[targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/dist/bin/js +0x247ebb5]
#06: ???[targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/dist/bin/js +0x245ee06]
#07: ???[targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/dist/bin/js +0x246241b]
#08: ???[targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/dist/bin/js +0x24913f7]
#09: ???[targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/dist/bin/js +0x24a65cc]
#10: ???[targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/dist/bin/js +0x2490e71]
#11: ???[targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/dist/bin/js +0x24a65cc]
#12: ???[targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/dist/bin/js +0x2490729]
#13: ???[targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/dist/bin/js +0x2464c03]
#14: ???[targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/dist/bin/js +0x2373e54]
#15: ???[targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/dist/bin/js +0x2377902]
#16: ???[targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/dist/bin/js +0x2379275]
#17: ???[targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/dist/bin/js +0x1fc2d02]
#18: ??? (???:???)
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==2966871==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000 (pc 0x6207fb707724 bp 0x7ffea91958e0 sp 0x7ffea91958e0 T2966871)
==2966871==The signal is caused by a WRITE memory access.
==2966871==Hint: address points to the zero page.
/opt/swift/usr/bin/llvm-symbolizer: error: '[anon:js-executable-memory]': No such file or directory
#0 0x6207fb707724 in MOZ_CrashSequence(void*, long) targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/dist/include/mozilla/Assertions.h:261:3
#1 0x6207fb707724 in js::gc::HeapSize::removeBytes(unsigned long, bool) targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/js/src/gc/spidermonkey/js/src/gc/Scheduling.h:689:7
#2 0x6207fb707724 in js::ZoneAllocator::decNonGCMemory(void*, unsigned long, js::MemoryUse, bool) targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/js/src/gc/spidermonkey/js/src/gc/ZoneAllocator.h:133:20
#3 0x6207fb0aa14a in void js::TrackedAllocPolicy<(js::TrackingKind)0>::free_<js::HeapPtr<js::FinalizationRecordObject*>>(js::HeapPtr<js::FinalizationRecordObject*>*, unsigned long) targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/js/src/builtin/spidermonkey/js/src/gc/ZoneAllocator.h:281:7
#4 0x6207fb0aa14a in mozilla::Vector<js::HeapPtr<js::FinalizationRecordObject*>, 1ul, js::TrackedAllocPolicy<(js::TrackingKind)0>>::~Vector() targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/dist/include/mozilla/Vector.h:1023:11
#5 0x6207fb07d0fb in JS::GCVector<js::HeapPtr<js::FinalizationRecordObject*>, 1ul, js::TrackedAllocPolicy<(js::TrackingKind)0>>::~GCVector() targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/dist/include/js/GCVector.h:42:7
#6 0x6207fb07d0fb in void JS::GCContext::delete_<JS::GCVector<js::HeapPtr<js::FinalizationRecordObject*>, 1ul, js::TrackedAllocPolicy<(js::TrackingKind)0>>>(js::gc::Cell*, JS::GCVector<js::HeapPtr<js::FinalizationRecordObject*>, 1ul, js::TrackedAllocPolicy<(js::TrackingKind)0>>*, unsigned long, js::MemoryUse) targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/js/src/builtin/spidermonkey/js/src/gc/GCContext.h:169:11
#7 0x6207fb07d0fb in void JS::GCContext::delete_<JS::GCVector<js::HeapPtr<js::FinalizationRecordObject*>, 1ul, js::TrackedAllocPolicy<(js::TrackingKind)0>>>(js::gc::Cell*, JS::GCVector<js::HeapPtr<js::FinalizationRecordObject*>, 1ul, js::TrackedAllocPolicy<(js::TrackingKind)0>>*, js::MemoryUse) targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/js/src/builtin/spidermonkey/js/src/gc/GCContext.h:157:5
#8 0x6207fb07d0fb in js::FinalizationQueueObject::finalize(JS::GCContext*, JSObject*) targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/js/src/builtin/./../../../../../../.worktree-repro/js/src/builtin/FinalizationRegistryObject.cpp:694:8
#9 0x6207fb6e62fe in unsigned long js::gc::Arena::finalize<JSObject, (js::gc::FinalizeKind)1>(JS::GCContext*, js::gc::AllocKind, unsigned long) targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/js/src/gc/./../../../../../../.worktree-repro/js/src/gc/Sweeping.cpp:165:12
#10 0x6207fb6ccbb4 in bool FinalizeTypedArenas<JSObject, (js::gc::FinalizeKind)1, (js::gc::ReleaseEmpty)0>(JS::GCContext*, js::gc::ArenaList&, js::gc::SortedArenaList&, js::gc::AllocKind, JS::SliceBudget&) targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/js/src/gc/./../../../../../../.worktree-repro/js/src/gc/Sweeping.cpp:223:16
#11 0x6207fb6ace05 in js::gc::ArenaLists::foregroundFinalize(JS::GCContext*, js::gc::AllocKind, JS::SliceBudget&, js::gc::SortedArenaList&) targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/js/src/gc/./../../../../../../.worktree-repro/js/src/gc/Sweeping.cpp:2033:8
#12 0x6207fb6b041a in js::gc::GCRuntime::finalizeAllocKind(JS::GCContext*, JS::SliceBudget&) targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/js/src/gc/./../../../../../../.worktree-repro/js/src/gc/Sweeping.cpp:2332:19
#13 0x6207fb6df3f6 in sweepaction::SweepActionForEach<ContainerIter<mozilla::EnumSet<js::gc::AllocKind, unsigned long>>, mozilla::EnumSet<js::gc::AllocKind, unsigned long>>::run(js::gc::SweepAction::Args&) targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/js/src/gc/./../../../../../../.worktree-repro/js/src/gc/Sweeping.cpp:2577:19
#14 0x6207fb6f45cb in sweepaction::SweepActionSequence::run(js::gc::SweepAction::Args&) targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/js/src/gc/./../../../../../../.worktree-repro/js/src/gc/Sweeping.cpp:2542:23
#15 0x6207fb6dee70 in sweepaction::SweepActionForEach<js::gc::SweepGroupZonesIter, JSRuntime*>::run(js::gc::SweepAction::Args&) targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/js/src/gc/./../../../../../../.worktree-repro/js/src/gc/Sweeping.cpp:2577:19
#16 0x6207fb6f45cb in sweepaction::SweepActionSequence::run(js::gc::SweepAction::Args&) targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/js/src/gc/./../../../../../../.worktree-repro/js/src/gc/Sweeping.cpp:2542:23
#17 0x6207fb6de728 in sweepaction::SweepActionForEach<js::gc::SweepGroupsIter, JSRuntime*>::run(js::gc::SweepAction::Args&) targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/js/src/gc/./../../../../../../.worktree-repro/js/src/gc/Sweeping.cpp:2577:19
#18 0x6207fb6b2c02 in js::gc::GCRuntime::sweepPhase(JS::SliceBudget&) targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/js/src/gc/./../../../../../../.worktree-repro/js/src/gc/Sweeping.cpp:2754:55
#19 0x6207fb5c1e53 in js::gc::GCRuntime::incrementalSlice(JS::SliceBudget&, JS::GCReason, bool) targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/js/src/gc/./../../../../../../.worktree-repro/js/src/gc/GC.cpp:4487:11
#20 0x6207fb5c5901 in js::gc::GCRuntime::gcCycle(bool, JS::SliceBudget const&, JS::GCReason) targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/js/src/gc/./../../../../../../.worktree-repro/js/src/gc/GC.cpp:5076:3
#21 0x6207fb5c7274 in js::gc::GCRuntime::collect(bool, JS::SliceBudget const&, JS::GCReason) targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/js/src/gc/./../../../../../../.worktree-repro/js/src/gc/GC.cpp:5260:9
#22 0x6207fb210d01 in GCSlice(JSContext*, unsigned int, JS::Value*) targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/js/src/builtin/./../../../../../../.worktree-repro/js/src/builtin/TestingFunctions.cpp:3276:12
#23 0x1551d2e02232 ([anon:js-executable-memory]+0x1232)
==2966871==Register values:
rax = 0x0000000000000000 rbx = 0x000072731b454300 rcx = 0x00000000000002b1 rdx = 0x0000000000000000
rdi = 0x000072731c909780 rsi = 0x0000000000000000 rbp = 0x00007ffea91958e0 rsp = 0x00007ffea91958e0
r8 = 0x0000000000000000 r9 = 0x0000000000000000 r10 = 0x0000000000000000 r11 = 0x000072731c6ce880
r12 = 0xfffe4b4b4b4b4b4b r13 = 0x00002f7cac01a088 r14 = 0x0000727319c03000 r15 = 0x000072731b454328
UndefinedBehaviorSanitizer can not provide additional info.
SUMMARY: UndefinedBehaviorSanitizer: SEGV targets/spidermonkey/artifacts/builds/reproducebuild-9f2e8c25f0b4/dist/include/mozilla/Assertions.h:261:3 in MOZ_CrashSequence(void*, long)
==2966871==ABORTING
Updated•4 months ago
|
Comment 1•4 months ago
|
||
The rating assigned by our bug rating tool (currently under development) is:
Rating: N/A
Explanation: Debug-only MOZ_ASSERT (retainedBytes_ >= nbytes) in HeapSize::removeBytes, hit while sweeping/finalizing a FinalizationRegistry record vector. It diagnoses an inconsistency in the GC's per-zone memory-accounting counter, caused by the simulated-OOM path (oomTest around FinalizationRegistry.register) leaving the tracked byte count out of sync. retainedBytes_ is only a size_t GC-scheduling heuristic, not a length/index/pointer; the actual free frees the correct pointer/size, so there is no OOB, UAF or double-free. In a release build the worst case is a counter underflow affecting only GC scheduling. Bad state, but not exploitable -> not security sensitive.
Comment 2•4 months ago
|
||
Reduced:
function f() {
let registry = evalcx("new FinalizationRegistry(v => {})",
newGlobal({newCompartment: true}));
for (let i = 0; i < 500; ++i) registry.register({}, 1);
}
f();
for (let i = 0; i < 100; ++i) gcslice(10000);
Root cause
FinalizationQueueObject owns a recordsToBeCleanedUp vector allocated with CellAllocPolicy. The only appender to that vector in the entire tree is FinalizationQueueObject::queueRecordToBeCleanedUp (FinalizationRegistryObject.cpp:761), and its only caller is FinalizationObservers::traceWeakFinalizationRegistryEdges (FinalizationObservers.cpp:467) — which runs during GC sweeping.
That collides with how retainedBytes_ is maintained (Scheduling.h:640-693):
updateOnGCStart()snapshotsretainedBytes_ = bytes()at GC start.addBytes()defaults toupdateRetainedSize = false, so anything allocated during a GC is added tobytes_but deliberately not toretainedBytes_.TrackedAllocPolicy<Cell>::decMemory(Zone.cpp:137-148) subtracts fromretainedBytes_exactly whengcx->isFinalizing().
So the vector's heap buffer is, by construction, never counted in retainedBytes_ — it can only come into existence mid-sweep. But FinalizationQueueObject::finalize (FinalizationRegistryObject.cpp:692-696) frees it via gcx->delete_ while finalizing, which takes the updateRetainedSize = true path.
Any queue that receives its first record and is finalized in the same GC therefore subtracts bytes from retainedBytes_ that were never added. Confirmed by the stack:
HeapSize::removeBytes Scheduling.h:689
ZoneAllocator::decNonGCMemory ZoneAllocator.h:133
TrackedAllocPolicy<Cell>::free_<HeapPtr<FinalizationRecordObject*>>
~Vector / ~GCVector
JS::GCContext::delete_<...> use=FinalizationRegistryRecordVector
FinalizationQueueObject::finalize FinalizationRegistryObject.cpp:694
js::gc::Arena::finalize<JSObject, 1> Sweeping.cpp:165
A queue that survives one GC becomes consistent again, because the next updateOnGCStart folds the buffer into the snapshot. That's why this needs the specific dead-in-first-GC window, which the compartment boundary and incremental slicing supply.
The 300-registration threshold isn't part of the bug — the subtraction is always wrong, but it only trips the assert once the freed buffer exceeds the zone's whole current retainedBytes_. Vector capacity doubles to 512 entries (4 KB) somewhere between n=250 and n=300, which is where it crosses this fresh zone's total.
Security relevance: no
Unlike a debug-only assert, the underflow itself does execute in release builds — retainedBytes_ is mozilla::Atomic<size_t> (Scheduling.h:633), so it wraps to ~2^64. But it's pure accounting. It has exactly two consumers:
Statistics.cpp:1119—bytesSurvivedtelemetry.ZoneAllocator::updateGCStartThresholds(Zone.cpp:62-71) →MallocHeapThreshold::updateStartThreshold→computeZoneTriggerBytes.
Nothing uses it for allocation sizing, indexing, or pointer arithmetic. The worst outcome is a garbage malloc-GC start threshold for that zone — either wildly too high (zone stops triggering malloc-driven GCs, memory bloat) or, in the much more common non-wrapping case, drift downward causing more frequent GCs. That's a correctness and memory-growth bug, not memory unsafety.
Updated•4 months ago
|
| Assignee | ||
Updated•4 months ago
|
| Assignee | ||
Comment 3•4 months ago
|
||
Doing this creates problems because the GC assumes that a container can't be
both grow and be freed in the same GC cycle (because if it is freed then there
were no references to it and so nothing could access it to grow it).
Unfortuantely this is baked in to the memory tracking. The simplest thing to do
here is to not count this memory against the zone. In most cases it is
transient anyway: finalization registry records are added to it during sweeping
and then removed when they have been passed to the callback.
Comment 5•4 months ago
|
||
| bugherder | ||
Description
•