Closed Bug 2078859 Opened 1 month ago Closed 1 month ago

Assertion failure: key->zoneFromAnyThread()->needsMarkingBarrier() && !key->isMarkedAny() in [@ js::StableCellHasher<JSScript*>::match] via MissingAllocSites::scriptMap lookup during incremental sweep

Categories

(Core :: JavaScript: GC, defect, P3)

defect

Tracking

()

RESOLVED FIXED
159 Branch
Tracking Status
firefox-esr153 --- wontfix
firefox157 --- wontfix
firefox158 --- wontfix
firefox159 --- fixed

People

(Reporter: bugmon, Assigned: jonco)

References

(Blocks 1 open bug)

Details

(Keywords: assertion, sec-other, testcase, Whiteboard: [adv-main159-])

Attachments

(4 files, 1 external file)

The DEBUG block in StableCellHasher<T>::match (js/src/gc/StableCellHasher-inl.h) asserts that a table key without a unique ID belongs to a zone with needsMarkingBarrier() set and is unmarked. That condition is wrong for the case the adjacent comment describes: Zone::changeGCState clears the marking barrier when a zone leaves the marking states, GCRuntime::sweepUniqueIds removes dead cells' IDs at the start of the sweep group, and JS::WeakCache<GCHashMap> tables are swept in later slices. A mutator lookupForAdd on MissingAllocSites::scriptMap whose hash equals that of a dead JSScript entry therefore calls match on an ID-less key in a Sweep-state zone and the assertion fails.

A script running in a debug, GC-zeal-enabled SpiderMonkey shell can abort the process on a debug assertion by enabling generateMissingAllocSites, forcing a unique-ID hash collision, and allocating from baseline code while an incremental GC is yielded before weak-cache sweeping. No memory corruption was demonstrated. The assertion is compiled only under DEBUG and the affected map only under JS_GC_ZEAL; with the assertion absent, the code following it returns false for the dead key. The testcase needs about 7 minutes and roughly 340 million unique-ID allocations to produce the collision.

Build Info

Affected Code

File: js/src/gc/StableCellHasher-inl.h, line 184-216

template <typename T>
/* static */ bool StableCellHasher<T>::match(const Key& k, const Lookup& l) {
  if (k == l) {
    return true;
  }

  if (!k || !l) {
    return false;
  }

  MOZ_ASSERT(CurrentThreadCanAccessZone(l->zoneFromAnyThread()) ||
             CurrentThreadIsPerformingGC());

#ifdef DEBUG
  // Incremental table sweeping means that existing table entries may no
  // longer have unique IDs. We fail the match in that case and the entry is
  // removed from the table later on.
  if (!gc::HasUniqueId(k)) {
    Key key = k;
    MOZ_ASSERT(key->zoneFromAnyThread()->needsMarkingBarrier() &&
               !key->isMarkedAny());
  }
  MOZ_ASSERT(gc::HasUniqueId(l));
#endif

  uint64_t keyId;
  if (!gc::MaybeGetUniqueId(k, &keyId)) {
    // Key is dead and cannot match lookup which must be live.
    return false;
  }

  return keyId == gc::GetUniqueIdInfallible(l);
}

File: js/src/gc/Zone.cpp, line 242-249

void Zone::changeGCState(GCRuntime* gc, GCState prev, GCState next) {
  MOZ_ASSERT(RuntimeHeapIsBusy());
  MOZ_ASSERT(gcState() == prev);
  MOZ_ASSERT_IF(isGCMarkingOrVerifyingPreBarriers(), needsMarkingBarrier_);

  gcState_ = next;
  setNeedsMarkingBarrier(gc, isGCMarkingOrVerifyingPreBarriers());
}

File: js/src/gc/Sweeping.cpp, line 1773-1785

    AutoRunParallelTask sweepUniqueIds(this, &GCRuntime::sweepUniqueIds,
                                       PhaseKind::SWEEP_UNIQUEIDS,
                                       GCUse::Sweeping, lock);
    AutoRunParallelTask sweepWeakPointers(
        this, &GCRuntime::sweepObjectsWithWeakPointers,
        PhaseKind::SWEEP_WEAK_POINTERS, GCUse::Sweeping, lock);

    WeakCacheToSweepVector immediateCaches;
    bool canSweepWeakCachesOffThread =
        PrepareWeakCacheSweeping(this, &immediateCaches);
    if (canSweepWeakCachesOffThread) {
      weakCachesToSweep.ref().emplace(currentSweepGroup);
    }

File: js/src/gc/Zone.h, line 77-92

#ifdef JS_GC_ZEAL

class MissingAllocSites {
 public:
  using SiteMap = JS::GCHashMap<uint32_t, UniquePtr<AllocSite>,
                                DefaultHasher<uint32_t>, SystemAllocPolicy>;

  using ScriptMap = JS::GCHashMap<WeakHeapPtr<JSScript*>, SiteMap,
                                  StableCellHasher<WeakHeapPtr<JSScript*>>,
                                  SystemAllocPolicy>;
  JS::WeakCache<ScriptMap> scriptMap;

  explicit MissingAllocSites(JS::Zone* zone) : scriptMap(zone) {}
};

#endif  // JS_GC_ZEAL

File: js/src/gc/Pretenuring.cpp, line 641-645

  auto scriptPtr = missingSites->scriptMap.lookupForAdd(script);
  if (!scriptPtr && !missingSites->scriptMap.add(
                        scriptPtr, script, MissingAllocSites::SiteMap())) {
    return nullptr;
  }

The assertion at StableCellHasher-inl.h:203-204 requires the key's zone to have the marking barrier enabled. Zone::changeGCState (Zone.cpp:242-249) sets the barrier only while isGCMarkingOrVerifyingPreBarriers() is true (MarkBlackOnly, MarkBlackAndGray, VerifyPreBarriers per js/public/shadow/Zone.h:113-117), so it is false in the Sweep state. In beginSweepingSweepGroup, sweepUniqueIds is started (Sweeping.cpp:1773) and removes dead cells from Zone::uniqueIds() (Sweeping.cpp:1438-1441), while PrepareWeakCacheSweeping (Sweeping.cpp:1631-1657) only installs a barrier tracer on caches that support incremental sweeping and defers their sweep. MissingAllocSites::scriptMap (Zone.h:79-90, JS_GC_ZEAL only) is such a cache, keyed by WeakHeapPtr<JSScript*> with StableCellHasher; JSScript is not a NativeObject, so HasUniqueId consults zone->uniqueIds() (StableCellHasher-inl.h:94-106). WeakCache::lookupForAdd (GCHashTable.h:492-499) calls the underlying map.lookupForAdd first, which runs match against any stored entry with an equal key hash, including dead ones. Key hashes are the low 32 bits of the unique ID (StableCellHasher-inl.h:127-132) passed through prepareHash (mfbt/HashTable.h:1709-1717), which multiplies by 0x9E3779B9 and clears the low bit, so two IDs 340573321 apart (the multiplicative inverse of 0x9E3779B9 mod 2^32) get equal stored hashes.

Exploit Chain

  1. Enable the testing parameter with gcparam("generateMissingAllocSites", 1) so that allocations made through the unknown alloc site from a baseline frame call GetOrCreateMissingAllocSite (Allocator.cpp:259-301).
  2. Create 4000 'early' functions, warm each into baseline, and call each once so it allocates via Object.keys; each script gets a scriptMap entry and a sequential unique ID.
  3. Consume about 340.57 million unique IDs by calling serialize() on arrays of fresh Date objects, so later IDs are 340573321 above the early ones and produce equal stored key hashes.
  4. Drop the early functions, call gczeal(19, 3000000) (YieldBeforeSweepingCaches) and spin until gcstate() is "Sweep": the dead scripts' unique IDs have been swept, the zone's marking barrier is off, and scriptMap still holds the dead entries behind its read barrier.
  5. Warm 6000 'late' functions into baseline and call each so it allocates; scriptMap.lookupForAdd for a late script probes a dead early entry with an equal key hash and StableCellHasher::match hits the assertion at StableCellHasher-inl.h:204.

Steps to Reproduce

  1. Start from an unmodified tree (git -C /firefox diff is empty); no patches, prefs, or extra shell flags are used.
  2. Build the SpiderMonkey debug shell with /app/mozconfigs/mozconfig.js.linux.debug (output /firefox/obj-js-debug/dist/bin/js).
  3. Run testcase.js with js_shell_evaluator using js_binary=/firefox/obj-js-debug/dist/bin/js and timeout=1800 (the evaluator adds --fuzzing-safe; the default 30 s timeout is too short, the run takes about 7 minutes).
  4. stdout ends with 'burn done 340568321 elapsed 409', 'in sweep window 3000000', 'late warmed Sweep'; stderr reports the assertion failure at js/src/gc/StableCellHasher-inl.h:204.

Security Impact

  • Severity: Low
  • Attacker capability: Abort a debug SpiderMonkey process on a DEBUG-only assertion. No out-of-bounds access, use-after-free, or other memory corruption was demonstrated; with the assertion compiled out, the code that follows it (StableCellHasher-inl.h:209-213) returns false for the ID-less key.
  • Preconditions: DEBUG build with JS_GC_ZEAL; access to the shell testing functions gcparam("generateMissingAllocSites", 1) and gczeal(19, ...); roughly 340 million unique-ID allocations (about 7 minutes) to force a hash collision between a dead and a live script.

ASAN Report

[7146] Assertion failure: key->zoneFromAnyThread()->needsMarkingBarrier() && !key->isMarkedAny(), at /firefox/js/src/gc/StableCellHasher-inl.h:204
#01: js::StableCellHasher<JSScript*>::match(JSScript* const&, JSScript* const&)[/firefox/obj-js-debug/dist/bin/js +0x2f3e012]
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==7146==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000 (pc 0x55efb568e02a bp 0x7ed4664b8e70 sp 0x7ed4664b8e50 T7146)
==7146==The signal is caused by a READ memory access.
==7146==Hint: address points to the zero page.
    #0 0x55efb568e02a in MOZ_CrashSequence(void*, long) /firefox/obj-js-debug/dist/include/mozilla/Assertions.h:261:3
    #1 0x55efb568e02a in js::StableCellHasher<JSScript*>::match(JSScript* const&, JSScript* const&) /firefox/js/src/gc/StableCellHasher-inl.h:203:5
    #2 0x55efb5792f8d in js::StableCellHasher<js::WeakHeapPtr<JSScript*>>::match(js::WeakHeapPtr<JSScript*> const&, JSScript* const&) /firefox/js/src/gc/Barrier.h:1380:1
    #3 0x55efb5792f8d in mozilla::detail::HashTable<...>::match(mozilla::HashMapEntry<js::WeakHeapPtr<JSScript*>, ...>&, JSScript* const&) /firefox/obj-js-debug/dist/include/mozilla/HashTable.h:1808:12
    #4 0x55efb5792f8d in mozilla::detail::HashTable<...>::lookup<(LookupReason)1>(JSScript* const&, unsigned int) const /firefox/obj-js-debug/dist/include/mozilla/HashTable.h:1838:37
    #5 0x55efb5792e39 in mozilla::detail::HashTable<...>::lookupForAdd(JSScript* const&) /firefox/obj-js-debug/dist/include/mozilla/HashTable.h:2186:19
    #6 0x55efb5788bd9 in mozilla::HashMap<js::WeakHeapPtr<JSScript*>, ...>::lookupForAdd(JSScript* const&) /firefox/obj-js-debug/dist/include/mozilla/HashTable.h:338:18
    #7 0x55efb5788bd9 in JS::WeakCache<JS::GCHashMap<js::WeakHeapPtr<JSScript*>, ...>>::lookupForAdd(JSScript* const&) /firefox/obj-js-debug/dist/include/js/GCHashTable.h:493:22
    #8 0x55efb57776b4 in js::gc::GetOrCreateMissingAllocSite(JSContext*, JSScript*, unsigned int, JS::TraceKind) /firefox/js/src/gc/Pretenuring.cpp:641:44
    #9 0x55efb565c091 in js::gc::CellAllocator::MaybeGenerateMissingAllocSite(JSContext*, JS::TraceKind, js::gc::AllocSite*) /firefox/js/src/gc/Allocator.cpp:295:7
    #10 0x55efb5021b85 in void* js::gc::CellAllocator::AllocNurseryOrTenuredCell<(JS::TraceKind)0, (js::AllowGC)1>(JSContext*, js::gc::AllocKind, unsigned long, js::gc::Heap, js::gc::AllocSite*) /firefox/js/src/gc/Allocator-inl.h:235:12
    #11 0x55efb5075a1a in js::ArrayObject* js::gc::CellAllocator::NewObject<js::ArrayObject, (js::AllowGC)1>(...) /firefox/js/src/gc/Allocator-inl.h:134:16
    #14 0x55efb5069d97 in js::ArrayObject::create(...) /firefox/js/src/vm/ArrayObject-inl.h:40:27
    #15 0x55efb5065289 in js::ArrayObject* NewArrayWithShape<4294967295u>(...) /firefox/js/src/builtin/Array.cpp:5350:22
    #16 0x55efb503c927 in js::ArrayObject* NewArray<4294967295u>(...) /firefox/js/src/builtin/Array.cpp:5424:10
    #17 0x55efb503ccb4 in js::NewDenseCopiedArray(JSContext*, unsigned int, JS::Value const*, js::NewObjectKind) /firefox/js/src/builtin/Array.cpp:5560:22
    #18 0x55efb50ffa4b in bool TryEnumerableOwnPropertiesNative<(EnumerableOwnPropertiesKind)0>(...) /firefox/js/src/builtin/Object.cpp:1876:7
    #19 0x55efb50ffa4b in js::obj_keys(JSContext*, unsigned int, JS::Value*) /firefox/js/src/builtin/Object.cpp:2014:8
    #20 0x55efb647e104 in CallJSNative(...) /firefox/js/src/vm/Interpreter.cpp:528:13
    #21 0x55efb645ab17 in js::InternalCallOrConstruct(...) /firefox/js/src/vm/Interpreter.cpp:624:12
    #22 0x55efb599e629 in js::jit::DoCallFallback(...) /firefox/js/src/jit/BaselineIC.cpp:1717:10
    #23 0x0fa07087e00e  ([anon:js-executable-memory]+0xc00e)
SUMMARY: UndefinedBehaviorSanitizer: SEGV /firefox/obj-js-debug/dist/include/mozilla/Assertions.h:261:3 in MOZ_CrashSequence(void*, long)
==7146==ABORTING
Attached patch fix.patch — — Splinter Review
Attached file crash_stack.txt —
Attached file testcase.js —
Attached file README.txt —

This bug was filed automatically by an AI-powered security testing tool. The analysis, testcase, and any suggested patches were generated by an AI model and have NOT been verified by a human.

Please review all details carefully before triaging. The issue was confirmed by an independent verifier, but for non-ASAN issues, the verifier has been made less strict. Additionally, the root cause analysis and suggested fix may contain errors.

Group: core-security → javascript-core-security
Blocks: sm-security
Severity: -- → S3
Priority: -- → P3

The assertion is slightly wrong. This is not a security issue.

Assignee: nobody → jcoppeard
Group: javascript-core-security

This is a rare case which is why this hasn't come up before. The idea is to
assert that if a unique ID is not found for a key it's because the key is dying
and is about to be swept. (The replacement assertion is essentially
IsAboutToBeFinalized(key), except that isn't implemented for Cell* which we
sometimes use as the template parameter).

The testcase did reproduce the problem but took ages to run so I didn't include it.

Pushed by jcoppeard@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/e852dda71be9 https://hg.mozilla.org/integration/autoland/rev/23997e4e901c Fix assertion about keys without unique IDs in StableCellHasher r=spidermonkey-reviewers,iain
Status: UNCONFIRMED → RESOLVED
Closed: 1 month ago
Resolution: --- → FIXED
Target Milestone: --- → 159 Branch
Whiteboard: [adv-main159-]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: