Closed Bug 2058673 Opened 3 months ago Closed 3 months ago

Infallible-scope LifoAlloc allocation (ballast contract violation) in wasm inlining [@ FunctionCompiler::finishInlinedCallDirect]

Categories

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

defect

Tracking

()

RESOLVED FIXED
155 Branch
Tracking Status
firefox-esr115 --- unaffected
firefox-esr140 --- wontfix
firefox-esr153 --- wontfix
firefox153 --- wontfix
firefox154 --- wontfix
firefox155 --- fixed

People

(Reporter: bugmon, Assigned: yury)

References

(Blocks 2 open bugs)

Details

(4 keywords, Whiteboard: [adv-main155-])

Attachments

(4 files, 1 external file)

When Ion tiers up a WebAssembly function and inlines a direct call, FunctionCompiler::finishInlinedCallDirect (js/src/wasm/WasmIonCompile.cpp:2787-2796) builds the join block for the inlined callee's returns by allocating one jit::MPhi per result of the callee's function type. MPhi::New(TempAllocator&, MIRType) allocates through TempObject::operator new(TempAllocator&) -> TempAllocator::allocateInfallible -> LifoAlloc::allocInfallible (js/src/jit/JitAllocPolicy.h:42,133; js/src/jit/MIR.h:5951). The TempAllocator contract requires infallible allocations to be covered by a prior ensureBallast() (16 KiB ballast, JitAllocPolicy.h:31-35,70-73); this loop has no ensureBallast call and its iteration count is module-controlled (calleeFuncType.results().length(), up to MaxResults = 1000, js/src/wasm/WasmConstants.h:1197). A callee with a wide multi-value result signature therefore exhausts the ballast and LifoAlloc must allocate a fresh chunk while the allocator is in its infallible-by-default scope, failing the debug assertion at js/src/ds/LifoAlloc.cpp:163: 'fallibleScope_ ([OOM] Cannot allocate a new chunk in an infallible scope.)'. The dead 'if (!phi || ...)' null-check after MPhi::New shows the loop was written expecting the fallible allocation path (alloc().fallible()).

I reproduced the assertion on a current debug shell via the fuzzing-safe harness with fuzzing-safe prefs (-P wasm_lazy_tiering_synchronous -P wasm_lazy_tiering_level=9 -P wasm_inlining_level=9), and also with a reduced set (-P wasm_lazy_tiering_synchronous -P wasm_inlining_level=9, i.e. default tiering level). With fully default prefs the bug does not reproduce: the default inlining level 5 limits inlineable callee bodies to 160 bytecode bytes at depth 0 (baseSize table, js/src/wasm/WasmHeuristics.h:290-292), which caps the result count of an inlineable callee below what is needed to exhaust the 16 KiB ballast — I confirmed empirically that a 78-result callee at default level and 2M hot calls at fully default settings do not crash.

The violated invariant is the OOM-unsafety guard itself: fallibleScope_ exists only in DEBUG/JS_OOM_BREAKPOINT builds (js/src/ds/LifoAlloc.h:806-810), and in release builds LifoAlloc::allocInfallible under genuine allocation failure calls AutoEnterOOMUnsafeRegion::crash (js/src/ds/LifoAlloc.h:895-901), a deliberate abort. So the demonstrated release-build consequence is a controlled process abort under memory pressure during off-main-thread/synchronous tier-2 compilation, not memory corruption.

Build Info

Affected Code

File: js/src/wasm/WasmIonCompile.cpp, line 2784-2796

    // The join block has a phi node for every result of the inlined function
    // type. Each phi node has an operand for each of the returns of the
    // inlined function.
    for (uint32_t i = 0; i < calleeFuncType.results().length(); i++) {
      MPhi* phi = MPhi::New(alloc(), calleeFuncType.results()[i].toMIRType());
      if (!phi || !phi->reserveLength(calleeReturns.length())) {
        return false;
      }
      joinAfterCall->addPhi(phi);
      if (!results->append(phi)) {
        return false;
      }
    }

File: js/src/jit/JitAllocPolicy.h, line 41-43

  void* allocateInfallible(size_t bytes) {
    return lifoScope_.alloc().allocInfallible(bytes);
  }

File: js/src/ds/LifoAlloc.cpp, line 161-164

LifoAlloc::UniqueBumpChunk LifoAlloc::newChunkWithCapacity(size_t n,
                                                           bool oversize) {
  MOZ_ASSERT(fallibleScope_,
             "[OOM] Cannot allocate a new chunk in an infallible scope.");

File: js/src/ds/LifoAlloc.h, line 895-902

  MOZ_ALWAYS_INLINE
  void* allocInfallible(size_t n) {
    AutoEnterOOMUnsafeRegion oomUnsafe;
    if (void* result = allocImpl(n)) {
      return result;
    }
    oomUnsafe.crash("LifoAlloc::allocInfallible");
    return nullptr;
  }

The phi-creation loop uses the infallible TempAllocator path (MPhi::New(TempAllocator&) -> allocateInfallible) with a module-controlled iteration count and no ensureBallast coverage; the null-check is dead because this overload cannot return null.

Exploit Chain

  1. Craft a wasm module with a multi-value function signature (testcase: 100 params / 100 results; the spec/engine limit is MaxResults = 1000) where a small callee returning all results is called from a function that becomes hot.
  2. Instantiate the module and call the exported function repeatedly so lazy tiering requests tier-2 (Ion) compilation of the caller (WasmHandleRequestTierUp -> CompilePartialTier2).
  3. During Ion compilation the direct call is inlined (subject to InliningHeuristics; with wasm_inlining_level=9 the 100-result callee is admitted).
  4. FunctionCompiler::finishInlinedCallDirect creates one MPhi per callee result via the infallible TempAllocator path with no ensureBallast in the loop; ~100 phi allocations plus join/catch-block allocations exceed the 16 KiB ballast.
  5. LifoAlloc must allocate a new chunk while setAsInfallibleByDefault is in effect: debug builds fail the fallibleScope_ assertion at LifoAlloc.cpp:163 and abort; release builds silently allocate, or under genuine OOM abort via AutoEnterOOMUnsafeRegion::crash.

Steps to Reproduce

  1. Build the SpiderMonkey debug shell: build_firefox with /app/mozconfigs/mozconfig.js.linux.debug (build_dir /firefox/obj-js-debug).
  2. Run the testcase via js_shell_evaluator (always --fuzzing-safe) with flags ["-P", "wasm_lazy_tiering_synchronous", "-P", "wasm_lazy_tiering_level=9", "-P", "wasm_inlining_level=9"] and js_binary /firefox/obj-js-debug/dist/bin/js. All prefs are fuzzing-safe; a reduced set (-P wasm_lazy_tiering_synchronous -P wasm_inlining_level=9 with 500000 loop iterations) also reproduces.
  3. The testcase builds a 100-param/100-result module where export 'run' calls $mid which calls $multi (both pass through all values), then calls run() in a loop until tier-up.
  4. Observe: Assertion failure: fallibleScope_ ([OOM] Cannot allocate a new chunk in an infallible scope.), at js/src/ds/LifoAlloc.cpp:164, with the crash stack in finishInlinedCallDirect.

Security Impact

  • Severity: Low
  • Attacker capability: A wasm module with a wide multi-value signature can drive the Ion wasm compiler to perform unbounded infallible LifoAlloc allocations outside the ballast contract. The demonstrated impact is a debug-build assertion abort; in release builds the same path under genuine allocation failure ends in a deliberate MOZ_CRASH (AutoEnterOOMUnsafeRegion), i.e. a controlled process abort, not demonstrated memory corruption.
  • Preconditions: Debug build (or JS_OOM_BREAKPOINT) for the assertion itself. Reproduction requires wasm function inlining to admit a callee with a wide result signature: with default prefs (wasm_inlining_level=5) the inlining size budget (160 bytecode bytes at depth 0, WasmHeuristics.h baseSize) prevented reproduction in testing; a raised wasm_inlining_level pref (fuzzing-safe, non-default) was required. Lazy tiering itself is enabled by default (javascript.options.wasm_lazy_tiering=true).

ASAN Report

[29595] Assertion failure: fallibleScope_ ([OOM] Cannot allocate a new chunk in an infallible scope.), at js/src/ds/LifoAlloc.cpp:164
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==29595==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000 (pc 0x5b6a2bff20f9 bp 0x7ffd1c9138e0 sp 0x7ffd1c9138c0 T29595)
==29595==The signal is caused by a WRITE memory access.
==29595==Hint: address points to the zero page.
    #0 0x5b6a2bff20f9 in MOZ_CrashSequence(void*, long) /firefox/obj-js-debug/dist/include/mozilla/Assertions.h:261:3
    #1 0x5b6a2bff20f9 in js::LifoAlloc::newChunkWithCapacity(unsigned long, bool) js/src/ds/LifoAlloc.cpp:163:3
    #2 0x5b6a2bff223e in js::LifoAlloc::getOrCreateChunk(unsigned long) js/src/ds/LifoAlloc.cpp:219:30
    #3 0x5b6a2bff243a in js::LifoAlloc::allocImplColdPath(unsigned long) js/src/ds/LifoAlloc.cpp:229:30
    #4 0x5b6a2c6b9227 in js::LifoAlloc::allocImpl(unsigned long) /firefox/js/src/ds/LifoAlloc.h:798:12
    #5 0x5b6a2c6b9227 in js::LifoAlloc::allocInfallible(unsigned long) /firefox/js/src/ds/LifoAlloc.h:897:24
    #6 0x5b6a2d729aef in js::jit::TempAllocator::allocateInfallible(unsigned long) /firefox/js/src/jit/JitAllocPolicy.h:42:31
    #7 0x5b6a2d729aef in js::jit::TempObject::operator new(unsigned long, js::jit::TempAllocator&) /firefox/js/src/jit/JitAllocPolicy.h:133:18
    #8 0x5b6a2d729aef in js::jit::MPhi::New(js::jit::TempAllocator&, js::jit::MIRType) /firefox/js/src/jit/MIR.h:5951:12
    #9 0x5b6a2d729aef in (anonymous namespace)::FunctionCompiler::finishInlinedCallDirect((anonymous namespace)::FunctionCompiler&, mozilla::Vector<js::jit::MDefinition*, 8ul, js::SystemAllocPolicy>*) js/src/wasm/WasmIonCompile.cpp:2788:19
    #10 0x5b6a2d729aef in (anonymous namespace)::FunctionCompiler::emitInlineCall(js::wasm::FuncType const&, unsigned int, js::wasm::InliningHeuristics::CallKind, mozilla::Vector<js::jit::MDefinition*, 8ul, js::SystemAllocPolicy> const&, mozilla::Vector<js::jit::MDefinition*, 8ul, js::SystemAllocPolicy>*) js/src/wasm/WasmIonCompile.cpp:6432:10
    #11 0x5b6a2d6ddc62 in (anonymous namespace)::FunctionCompiler::emitCall() js/src/wasm/WasmIonCompile.cpp:6473:12
    #12 0x5b6a2d6cd1e7 in (anonymous namespace)::FunctionCompiler::emitBodyExprs() js/src/wasm/WasmIonCompile.cpp:9845:9
    #13 0x5b6a2d6906b9 in (anonymous namespace)::RootCompiler::generate() js/src/wasm/WasmIonCompile.cpp:10997:21
    #14 0x5b6a2d68f009 in js::wasm::IonCompileFunctions(...) js/src/wasm/WasmIonCompile.cpp:11152:23
    #15 0x5b6a2d64fbe6 in ExecuteCompileTask(js::wasm::CompileTask*, std::unique_ptr<char [], JS::FreePolicy>*) js/src/wasm/WasmGenerator.cpp:625:12
    #16 0x5b6a2d651cc1 in js::wasm::ModuleGenerator::locallyCompileCurrentTask() js/src/wasm/WasmGenerator.cpp:721:8
    #17 0x5b6a2d651cc1 in js::wasm::ModuleGenerator::finishFuncDefs() js/src/wasm/WasmGenerator.cpp:860:24
    #18 0x5b6a2d5db01e in js::wasm::CompilePartialTier2(...) js/src/wasm/WasmCompile.cpp:1141:13
    #19 0x5b6a2d5ba8d9 in WasmHandleRequestTierUp(js::wasm::Instance*) js/src/wasm/WasmBuiltins.cpp:751:15
    #20 0x3b0412db0c75  (<unknown module>)
SUMMARY: UndefinedBehaviorSanitizer: SEGV /firefox/obj-js-debug/dist/include/mozilla/Assertions.h:261:3 in MOZ_CrashSequence(void*, long)
==29595==ABORTING
Attached file testcase.js —
Attached file NOTES.md —
Attached patch iter_1.patch — — Splinter Review
Attached file crash_stack.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 crash is real (confirmed by ASAN), but the root cause analysis and suggested fix may contain errors.

Group: core-security → javascript-core-security
Severity: -- → S4
Priority: -- → P3
Attached file (secure) —

Two loops in WasmIonCompile.cpp allocate one MPhi per element infallibly on a
module-controlled trip count without a prior ensureBallast(), so a wide
signature exhausts the TempAllocator ballast and LifoAlloc asserts allocating a
fresh chunk in an infallible scope (release: a controlled OOM abort):

  • finishInlinedCallDirect: one phi per inlined callee result.
  • the loop-header builder: one phi per loop block-parameter.

Add ensureBallast() at the top of each loop, matching bug 2042728.

Assignee: nobody → ydelendik
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true
See Also: → 2042728
Blocks: 2059577

:yury, please update all release status flags to reflect the affected/unaffected/disabled state correctly. Which branches (beta, release, and/or ESR) are affected by this flaw?

Flags: needinfo?(ydelendik)
Group: javascript-core-security → core-security-release
Status: ASSIGNED → RESOLVED
Closed: 3 months ago
Resolution: --- → FIXED
Target Milestone: --- → 155 Branch
Flags: needinfo?(ydelendik) → in-testsuite+
Whiteboard: [adv-main155-]
Group: core-security-release
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: