Symbols-as-WeakMap-keys gray-marking invariant violation: atom-marking bitmap under-approximates a live (black) Symbol key, enabling use-after-free [@ WeakMapBase::checkMarkingForZone]
Categories
(Core :: JavaScript: GC, defect, P1)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox-esr115 | --- | unaffected |
| firefox-esr140 | --- | unaffected |
| firefox-esr153 | --- | fixed |
| firefox154 | --- | wontfix |
| firefox155 | --- | fixed |
| firefox156 | --- | verified |
People
(Reporter: bugmon, Assigned: jonco)
References
(Regression)
Details
(4 keywords, Whiteboard: [bugmon:bisected,confirmed])
Attachments
(3 files, 3 external files)
WeakMap supports JS Symbols as keys (WeakMapObject::set, gated by experimental_symbols_as_weakmap_keys which is on by default in the shell/nightly). Because Symbols live in the shared atoms zone, the GC keeps a Symbol WeakMap key alive across partial (zone) GCs using a per-zone 'atom-marking bitmap' that is supposed to be an over-approximation of the color at which each zone references each atoms-zone cell. The invariant is: for any Symbol used as a WeakMap key, the per-zone atom-marking bitmap color must be >= the Symbol's actual mark color (js/src/gc/Verifier.cpp:1222-1234).
Under a sequence of cross-zone gray-rooting of Symbol WeakMap keys combined with incremental and zone GCs (including aborted incremental slices), the atom-marking bitmap ends up recording a key as only GRAY while the key is actually BLACK — the over-approximation UNDER-approximates. This is detected by the always-on DEBUG check WeakMapBase::checkMarkingForZone, invoked from MaybeCheckWeakMapMarking during endMarkingSweepGroup, which fires MOZ_RELEASE_ASSERT at js/src/gc/Sweeping.cpp:1263 and aborts (UBSan reports the SEGV in MOZ_CrashSequence).
The security consequence is a use-after-free: GCRuntime::isSymbolReferencedByUncollectedZone (js/src/gc/AtomMarking.cpp:112-137) and WeakMap::markEntry (js/src/gc/WeakMap-inl.h:217-230) consult this bitmap to decide whether a Symbol WeakMap key (and hence its value) must be retained when the key is referenced from a zone that is not part of the current sweep group. With the bitmap under-marked, a subsequent zone GC that collects the WeakMap's zone + atoms zone but leaves a referencing zone uncollected can wrongly judge a live Symbol key collectable, sweep the WeakMap entry, and free its still-reachable value. This is the same bug class as the sec-high regressions Bug 1979359 and Bug 1985765 and the still-embargoed follow-ups (Bug 2028416 / Bug 2029295) regressed by Bug 1994871 ('Support Symbols as WeakMap keys in the cycle collector', which made symbols/prop-maps grayable). The defective marking lives in js/src/gc, but it is reachable only because WeakMapObject.cpp accepts Symbol keys and arms the machinery via WeakMap::writeBarrier -> keyKindBarrier -> setMayHaveSymbolKeys.
Build Info
- Branch: main
- Revision: 1f7030c8de8f2b349c7d91d7b5a3253c109a1cc1
- Timestamp: 2026-05-21T16:50:48+00:00
Affected Code
File: js/src/builtin/WeakMapObject.cpp, line 146-172
static bool SetWeakMapEntryImpl(JSContext* cx, Handle<WeakMapObject*> mapObj,
Handle<Value> keyVal, Handle<Value> value) {
if (!EnsureValidWeakMapKey(cx, keyVal)) { // CanBeHeldWeakly: Symbols allowed
return false;
}
return WeakCollectionPutEntryInternal(cx, mapObj, keyVal, value);
}
/* static */ MOZ_ALWAYS_INLINE bool WeakMapObject::set_impl(
JSContext* cx, const CallArgs& args) {
MOZ_ASSERT(WeakMapObject::is(args.thisv()));
Rooted<WeakMapObject*> map(cx, &args.thisv().toObject().as<WeakMapObject>());
if (!SetWeakMapEntryImpl(cx, map, args.get(0), args.get(1))) {
return false;
}
args.rval().set(args.thisv());
return true;
}
File: js/src/gc/WeakMap.h, line 543-555
void writeBarrier(const Key& key, const Value& value) {
keyKindBarrier(key);
nurseryEntryBarrier(key, value);
}
void keyKindBarrier(const JS::Value& key) {
if (key.isSymbol() && !mayHaveSymbolKeys) {
setMayHaveSymbolKeys(); // arms the symbol-keyed GC marking path for this zone
}
if (key.isObject()) {
keyKindBarrier(&key.toObject());
}
}
File: js/src/gc/WeakMap-inl.h, line 217-230
bool keyIsSymbol = gc::detail::IsSymbol(key.get());
MOZ_ASSERT(keyIsSymbol == (keyCell->getTraceKind() == JS::TraceKind::Symbol));
if (keyIsSymbol && keyColor < markColor) {
// For symbols, also check whether it is referenced by an uncollected
// zone, and if so mark it now.
auto* sym = static_cast<JS::Symbol*>(keyCell);
gc::GCRuntime* gc = &marker->runtime()->gc;
if (gc->isSymbolReferencedByUncollectedZone(sym, marker->markColor())) {
TraceEdge(trc, &key, "WeakMap symbol key");
keyColor = markColor;
marked = true;
}
}
File: js/src/gc/AtomMarking-inl.h, line 112-137
inline bool GCRuntime::isSymbolReferencedByUncollectedZone(JS::Symbol* sym,
MarkColor color) {
MOZ_ASSERT(sym->zone()->isAtomsZone());
if (!atomsUsedByUncollectedZones.ref()) {
return false;
}
size_t bit = AtomMarkingRuntime::getAtomBit(sym);
size_t blackBit = bit + size_t(ColorBit::BlackBit);
size_t grayOrBlackBit = bit + size_t(ColorBit::GrayOrBlackBit);
const DenseBitmap& bitmap = *atomsUsedByUncollectedZones.ref();
if (grayOrBlackBit >= bitmap.count()) {
return false;
}
if (bitmap.getBit(blackBit)) {
return true;
}
// gray path: relies on the bitmap being a correct over-approximation
return color == MarkColor::Gray && bitmap.getBit(grayOrBlackBit);
}
File: js/src/gc/Verifier.cpp, line 1222-1234
// Symbols key must be marked in the atom marking bitmap for the zone.
if (key->is<JS::Symbol>()) {
GCRuntime* gc = &mapRuntime->gc;
CellColor bitmapColor =
gc->atomMarking.getAtomMarkColor(zone, key->as<JS::Symbol>());
if (bitmapColor < keyColor) { // VIOLATED: bitmap=gray, key=black
fprintf(stderr, "Atom marking bitmap is less marked than symbol key %p\n", key);
fprintf(stderr, "(key %p is %s, bitmap is %s)\n", key,
CellColorName(keyColor), CellColorName(bitmapColor));
ok = false;
}
}
The feature entry point is WeakMapObject.cpp's Symbol-key support; the broken invariant is in the GC atom-marking machinery it arms. (1) WeakMapObject::set accepts any CanBeHeldWeakly value, including Symbols, and stores it via the weak-collection put path. (2) WeakMap::put -> writeBarrier -> keyKindBarrier sets the zone-wide 'may have symbol keys' flag, enabling the symbol-keyed GC marking path. (3) That path, isSymbolReferencedByUncollectedZone, reads the per-zone atom-marking bitmap whose over-approximation invariant is violated, as caught by checkMarking.
Exploit Chain
- Insert Symbols as WeakMap keys via WeakMap.prototype.set (or getOrInsert); this calls WeakMap::writeBarrier -> keyKindBarrier -> setMayHaveSymbolKeys, marking the WeakMap's zone as possibly containing Symbol keys and enabling the symbol-keyed GC marking path.
- Hold the same Symbol keys via GRAY references from one or more OTHER zones (e.g. an object stored in grayRoot(), or a property key on a gray object), so each zone's atom-marking over-approximation bitmap records the Symbol as gray.
- Drive a mixture of incremental GC slices (startgc/gcslice), aborted incremental marking (abortgc), and per-zone GCs (schedulezone + gc('zone')) so the Symbol becomes BLACK (strongly reachable) while at least one zone's atom-marking bitmap is left recording it as only GRAY — i.e. the over-approximation under-approximates the real color.
- On the next sweep, MaybeCheckWeakMapMarking -> WeakMapBase::checkMarkingForZone detects bitmapColor < keyColor for the Symbol WeakMap key and aborts (debug). On a non-instrumented build the inconsistency is silent.
- Latent UAF: a subsequent zone GC collecting the WeakMap's zone + atoms zone (but leaving a referencing zone uncollected) consults the under-marked bitmap in isSymbolReferencedByUncollectedZone, wrongly concludes the still-live Symbol key is collectable, sweeps the WeakMap entry, and frees its value (e.g. an object owning an ArrayBuffer) while references to it remain — a use-after-free that can be leveraged for memory corruption.
Steps to Reproduce
- Build the SpiderMonkey debug shell (obj-js-debug) which enables JS_GC_ZEAL and the DEBUG MaybeCheckWeakMapMarking invariant check.
- Symbols-as-WeakMap-keys is enabled by default in the shell build, so no extra flag is required (the evaluator runs with --fuzzing-safe).
- Run: /firefox/obj-js-debug/dist/bin/js --fuzzing-safe weakmap_gray_symbol.js
- Observe the abort: 'Atom marking bitmap is less marked than symbol key 0x... (key is black, bitmap is gray)' followed by 'Assertion failure: WeakMapBase::checkMarkingForZone(zone), at js/src/gc/Sweeping.cpp:1263'. The testcase is deterministic (seeded xorshift PRNG) and reproduces every run.
Security Impact
- Severity: High
- Attacker capability: Attacker-controlled script (via WeakMap.prototype.set/getOrInsert with Symbol keys plus ordinary multi-realm/GC behavior) can drive the GC into a state where the atom-marking over-approximation bitmap under-records a live Symbol WeakMap key's color. A subsequent zone GC can then prematurely free the WeakMap entry's value while it is still reachable, producing a use-after-free of an attacker-influenced heap object — a primitive that typically leads to memory corruption and potential remote code execution in the content process.
- Preconditions: Requires the 'Symbols as WeakMap keys' feature to be enabled (experimental_symbols_as_weakmap_keys; on by default in the JS shell and on Nightly, off by default on release/beta). No special privileges or user interaction are needed beyond running script. The debug build surfaces the broken invariant as an assertion abort; on a release/opt build the invariant violation is silent and is the precursor to the use-after-free.
ASAN Report
Atom marking bitmap is less marked than symbol key 0x2a7c22e10070
(key 0x2a7c22e10070 is black, bitmap is gray)
Atom marking bitmap is less marked than symbol key 0x2a7c22e10170
(key 0x2a7c22e10170 is black, bitmap is gray)
[2132] Assertion failure: WeakMapBase::checkMarkingForZone(zone), at js/src/gc/Sweeping.cpp:1263
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==2132==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000 (pc ... bp ... sp ... T2132)
==2132==The signal is caused by a WRITE memory access.
==2132==Hint: address points to the zero page.
#0 MOZ_CrashSequence(void*, long) /firefox/obj-js-debug/dist/include/mozilla/Assertions.h:261:3
#1 MaybeCheckWeakMapMarking(js::gc::GCRuntime*) js/src/gc/Sweeping.cpp:1263:7
#2 js::gc::GCRuntime::endMarkingSweepGroup(JS::GCContext*, JS::SliceBudget&) js/src/gc/Sweeping.cpp:1379:3
#3 sweepaction::SweepActionSequence::run(js::gc::SweepAction::Args&) js/src/gc/Sweeping.cpp:2534:23
#4 sweepaction::SweepActionForEach<js::gc::SweepGroupsIter, JSRuntime*>::run(...) js/src/gc/Sweeping.cpp:2569:19
#5 js::gc::GCRuntime::sweepPhase(JS::SliceBudget&) js/src/gc/Sweeping.cpp:2771:55
#6 js::gc::GCRuntime::incrementalSlice(JS::SliceBudget&, JS::GCReason, bool) js/src/gc/GC.cpp:4489:11
#7 js::gc::GCRuntime::gcCycle(bool, JS::SliceBudget const&, JS::GCReason) js/src/gc/GC.cpp:5055:3
#8 js::gc::GCRuntime::collect(bool, JS::SliceBudget const&, JS::GCReason) js/src/gc/GC.cpp:5245:9
#9 js::gc::GCRuntime::gc(JS::GCOptions, JS::GCReason) js/src/gc/GC.cpp:5325:3
#10 JS::NonIncrementalGC(JSContext*, JS::GCOptions, JS::GCReason) js/src/gc/GCAPI.cpp:298:21
#11 GC(JSContext*, unsigned int, JS::Value*) js/src/builtin/TestingFunctions.cpp:818:3
SUMMARY: UndefinedBehaviorSanitizer: SEGV /firefox/obj-js-debug/dist/include/mozilla/Assertions.h:261:3 in MOZ_CrashSequence(void*, long)
| Reporter | ||
Comment 1•5 months ago
|
||
| Reporter | ||
Comment 2•5 months ago
|
||
| Reporter | ||
Comment 3•5 months ago
|
||
Updated•5 months ago
|
Updated•5 months ago
|
| Reporter | ||
Comment 5•5 months ago
|
||
Verified bug as reproducible on mozilla-central 20260527085321-7a75e5d0dc02.
The bug appears to have been introduced in the following build range:
Start: 786b8e8325e27d0a140dee01e8c1ee4d408e39db (20260501155519)
End: 2bd1df969f76b581b333daea253a3c176b9ff9ab (20260501140046)
Pushlog: https://hg.mozilla.org/integration/autoland/pushloghtml?fromchange=786b8e8325e27d0a140dee01e8c1ee4d408e39db&tochange=2bd1df969f76b581b333daea253a3c176b9ff9ab
| Assignee | ||
Updated•5 months ago
|
| Assignee | ||
Updated•5 months ago
|
| Assignee | ||
Comment 6•5 months ago
|
||
This is an incorrect check. It's complaining that a symbol weak map key is marked gray in the atom marking bitmap for a particular zone while the symbol itself is marked black. This is fine however, since references from other zones may result in the symbol being more marked than the atom marking state for any particular zone.
The testcase is interesting because the AI essentially wrote a fuzzer to find this. The reduced test case it came up with was pretty simple though.
| Assignee | ||
Comment 7•5 months ago
|
||
This removes the incorrect check (that actually wasn't running until recently
because of another bug) and adds more checks that are hopefuly correct this time.
Comment 8•5 months ago
|
||
Based on comment #5, this bug contains a bisection range found by bugmon. However, the Regressed by field is still not filled.
:jonco, if possible, could you fill the Regressed by field and investigate this regression?
For more information, please visit BugBot documentation.
Updated•5 months ago
|
Comment 10•3 months ago
|
||
| bugherder | ||
| Reporter | ||
Comment 11•3 months ago
|
||
Verified bug as fixed on rev mozilla-central 20260814041239-4b0288d5dc87.
Removing bugmon keyword as no further action possible. Please review the bug and re-add the keyword for further analysis.
Updated•3 months ago
|
Comment 13•3 months ago
|
||
The patch landed in nightly and beta is affected, along with ESR.
:jonco, is this bug important enough to require an uplift?
- If yes, please nominate the patch for beta and ESR approvals.
- See https://wiki.mozilla.org/Release_Management/Requesting_an_Uplift for documentation on how to request an uplift.
- If no, please set
status-firefox155and the ESR status flag(s) towontfix.
For more information, please visit BugBot documentation.
| Assignee | ||
Comment 14•3 months ago
|
||
This bug results in spurious assertion failures only. I'll request uplift to beta to quiet any test failures but not to any ESR branch unless we start to see these show up in testing.
Comment 15•3 months ago
|
||
firefox-beta Uplift Approval Request
- User impact if declined/Reason for urgency: No user impact, the patch prevents spurious assertion failures in testing only.
- Code covered by automated testing?: yes
- Fix verified in Nightly?: yes
- Needs manual QE testing?: no
- Steps to reproduce for manual QE testing:
- Risk associated with taking this patch: low
- Explanation of risk level: This only affects debug builds and has been on central for 5 days without issues.
- String changes made/needed?: None.
- Is Android affected?: yes
| Assignee | ||
Comment 16•3 months ago
|
||
This removes the incorrect check (that actually wasn't running until recently
because of another bug) and adds more checks that are hopefuly correct this time.
Original Revision: https://phabricator.services.mozilla.com/D303321
| Assignee | ||
Comment 17•3 months ago
|
||
(In reply to Jon Coppeard (:jonco) from comment #14)
It looks like this will be required on ESR153 so we can uplift other fixes.
| Assignee | ||
Comment 18•3 months ago
|
||
This removes the incorrect check (that actually wasn't running until recently
because of another bug) and adds more checks that are hopefuly correct this time.
Updated•3 months ago
|
Comment 19•3 months ago
|
||
firefox-esr153 Uplift Approval Request
- User impact if declined/Reason for urgency: No user impact but required by later uplifts. Fixes spurious assertions.
- Code covered by automated testing?: yes
- Fix verified in Nightly?: yes
- Needs manual QE testing?: yes
- Steps to reproduce for manual QE testing: None
- Risk associated with taking this patch: low
- Explanation of risk level: This changes debug-only code. This has been on central for 5 days without issues.
- String changes made/needed?: None.
- Is Android affected?: yes
Updated•3 months ago
|
Updated•3 months ago
|
Comment 20•3 months ago
|
||
| uplift | ||
Updated•2 months ago
|
Updated•2 months ago
|
Comment 21•2 months ago
|
||
| uplift | ||
Updated•2 months ago
|
Description
•