Skip to content
Memory corruption Disclosed report Advanced CVE-2026-16363

WebAssembly: missing stack map for `wasm:js-string.concat` `ThrowReported` path leaves wasm-gc refs stale

A WebAssembly builtin call that can fail (string concat) emits its failure trap after the recorded stack-map safepoint, so when the pending exception is wrapped into a wasm exception object and that allocation triggers GC, the wasm frame is traced at a PC with no stack map. Live wasm-gc reference locals are then skipped, leaving stale references that a later collection treats as real object pointers, leading to a crash and potentially controlled memory corruption.

Nebula Security Follow

Originally disclosed on Mozilla

  • Fixed in 22 days
  • Paid in 4 months
  • Disclosed after 22 days

Created attachment 9597524 poc_asan.js

WebAssembly: missing stack map for wasm:js-string.concat ThrowReported path leaves wasm-gc refs stale

Description

wasm:js-string.concat is compiled as a fallible builtin-instance-method call. If Instance::stringConcat returns null, the generated code branches to a Trap::ThrowReported failure path. This is reachable with normal JS exception state when ConcatStrings<CanGC> reports JSMSG_ALLOC_OVERFLOW for a result larger than JSString::MAX_LENGTH.

Instance::stringConcat returns null with an ordinary pending JS exception:

C++
void* Instance::stringConcat(Instance* instance, void* firstStringArg,
                             void* secondStringArg) {
  // .........
  Rooted<JSString*> firstString(cx, firstStringRef.toJSString());
  Rooted<JSString*> secondString(cx, secondStringRef.toJSString());
  JSString* result = ConcatStrings<CanGC>(cx, firstString, secondString);
  if (!result) {
    MOZ_ASSERT(cx->isExceptionPending());
    return nullptr;
  }
  return AnyRef::fromJSString(result).forCompiledCode();
}

The root cause is that the wasm stack map is recorded at the builtin call return PC, but the generated null-return failure trap has a different post-trap resume PC. During exception handling, SpiderMonkey wraps the pending JS exception into a WasmExceptionObject before searching wasm frames for a catch handler. That wrapper allocation can GC while the active wasm frame is described by the post-trap resume PC, where no stack map exists. Instance::traceFrame then returns early and skips live wasm reference locals.

In short:

Text
builtin call return PC has stack map
failure check emits Trap::ThrowReported after that PC
trap handling uses trapPC + WasmTrapInstructionLength as resume PC
GC during WasmExceptionObject wrapping traces at that resume PC
lookupStackMap(resumePC) fails
live wasm refs are not traced or updated
wasm catch resumes with stale typed refs

The important logic is in js/src/jit/MacroAssembler.cpp. The call return offset is saved before the failure check is emitted:

C++
CodeOffset MacroAssembler::wasmCallBuiltinInstanceMethod(
    const wasm::CallSiteDesc& desc, const ABIArg& instanceArg,
    wasm::SymbolicAddress builtin, wasm::FailureMode failureMode,
    wasm::Trap failureTrap) {
  // .........
  CodeOffset ret = call(desc, builtin);

  // .........

  wasmTrapOnFailedInstanceCall(ReturnReg, failureMode, failureTrap,
                               desc.toTrapSiteDesc());
  return ret;
}

Ion then marks the safepoint at that returned offset in js/src/jit/CodeGenerator.cpp:

C++
void CodeGenerator::visitWasmCall(LWasmCall* lir) {
  // .........
  switch (callee.which()) {
    // .........
    case wasm::CalleeDesc::BuiltinInstanceMethod:
      retOffset = masm.wasmCallBuiltinInstanceMethod(
          desc, callBase->instanceArg(), callee.builtin(),
          callBase->builtinMethodFailureMode(),
          callBase->builtinMethodFailureTrap());
      // .........
      break;
    // .........
  }

  // .........

  // Note the assembler offset for the associated LSafePoint.
  MOZ_ASSERT(!isReturnCall);
  markSafepointAt(retOffset.offset(), lir);
  // .........
}

Baseline does the same in js/src/wasm/WasmBaselineCompile.cpp.

But the failure trap is emitted after the call return address, in the same wasmTrapOnFailedInstanceCall helper:

C++
void MacroAssembler::wasmTrapOnFailedInstanceCall(
    Register resultRegister, wasm::FailureMode failureMode,
    wasm::Trap failureTrap, const wasm::TrapSiteDesc& trapSiteDesc) {
  Label noTrap;
  switch (failureMode) {
    // .........
    case wasm::FailureMode::FailOnNullPtr:
      branchTestPtr(Assembler::NonZero, resultRegister, resultRegister,
                    &noTrap);
      break;
    // .........
  }
  wasmTrap(failureTrap, trapSiteDesc);
  bind(&noTrap);
}

At runtime, js/src/vm/JitActivation.cpp records the resume PC after the trap instruction:

C++
void js::jit::JitActivation::startWasmTrap(wasm::Trap trap,
                                           const wasm::TrapSite& trapSite,
                                           const wasm::RegisterState& state) {
  // .........
  setWasmExitFP(fp);
  wasmTrapData_.emplace();
  wasmTrapData_->resumePC =
      ((uint8_t*)state.pc) + jit::WasmTrapInstructionLength;
  wasmTrapData_->unwoundPC = pc;
  wasmTrapData_->trap = trap;
  // .........
}

js/src/wasm/WasmInstance.cpp then traces the frame using an exact stack-map lookup at that PC:

C++
uintptr_t Instance::traceFrame(JSTracer* trc, const wasm::WasmFrameIter& wfi,
                               uint8_t* nextPC,
                               uintptr_t highestByteVisitedInPrevFrame) {
  const StackMap* map = code().lookupStackMap(nextPC);
  if (!map) {
    return 0;
  }
  // .........

  Frame* frame = wfi.frame();
  uintptr_t* stackWords =
      GetFrameScanStartForStackMap(frame, map, &highestByteVisitedInPrevFrame);

  // Hand refs off to the GC.
  for (uint32_t i = 0; i < map->header.numMappedWords; i++) {
    if (map->get(i) != StackMap::Kind::AnyRef) {
      continue;
    }

    TraceManuallyBarrieredEdge(trc, (AnyRef*)&stackWords[i],
                               "Instance::traceWasmFrame: normal word");
  }
  // .........
}

The GC can happen at this mismatched PC because HandleExceptionWasm wraps the pending exception before searching for the wasm catch handler:

C++
void wasm::HandleExceptionWasm(JSContext* cx, JitFrameIter& iter,
                               jit::ResumeFromException* rfe) {
  // .........
  iter.asWasm().setIsLeavingFrames();

  // Get (or wrap) the exception that we'll search for catch handlers for.
  Rooted<WasmExceptionObject*> wasmExn(cx,
                                       GetOrWrapWasmException(activation, cx));
  // .........
}

GetOrWrapWasmException filters true OOM/over-recursion, but not this JSMSG_ALLOC_OVERFLOW pending exception, and then calls the allocating wrapper:

C++
static WasmExceptionObject* GetOrWrapWasmException(JitActivation* activation,
                                                   JSContext* cx) {
  // .........
  if (activation->isWasmTrapping() &&
      activation->wasmTrapData().trap != Trap::ThrowReported
#ifdef ENABLE_WASM_JSPI
      && activation->wasmTrapData().trap != Trap::ThrowSuspendError
#endif
  ) {
    return nullptr;
  }

  if (cx->isThrowingOverRecursed() || cx->isThrowingOutOfMemory()) {
    return nullptr;
  }

  // .........

  RootedValue exn(cx);
  if (cx->getPendingException(&exn)) {
    // .........
    if (exn.isObject() && exn.toObject().is<WasmExceptionObject>()) {
      // .........
    } else {
      // Wrap all thrown JS values in a wasm exception. This is required so
      // that all exceptions have tags, and the 'null' JS value becomes a
      // non-null wasm exception.
      wasmExn = WasmExceptionObject::wrapJSValue(cx, exn);
    }
    // .........
  }
  // .........
}

This violates the GC invariant that every live wasm frame PC used for tracing must have stack-map metadata.

Reproduction

Please compile SpiderMonkey shell with the following config:

ac_add_options --enable-project=js
ac_add_options --enable-address-sanitizer
ac_add_options --disable-jemalloc
ac_add_options --disable-optimize
ac_add_options --disable-debug
ac_add_options --disable-install-strip

The attached PoC uses ordinary WebAssembly.Exception allocation pressure before the failing concat, then allocates JS values after the catch to trigger a later nursery collection with a valid stack map. That later GC traces the stale wasm frame word as an AnyRef. The overwrite target with controlled value.

Please run with:

Shell
/audit/ff/firefox/obj-js-asan-nodebug-noopt-x86_64-pc-linux-gnu/dist/bin/js poc_asan.js 

The ASAN report:

/audit/ff/firefox/obj-js-asan-no-debug-noopt-x86_64-pc-linux-gnu/dist/bin/js audit/webassembly/reference-types-and-wasm-gc-objects/js-string-concat-throwreported-trap-pc-missing-stackmap/poc_asan.js
AddressSanitizer:DEADLYSIGNAL
=================================================================
==2715986==ERROR: AddressSanitizer: SEGV on unknown address (pc 0x55bc3af4c6e8 bp 0x7fffcf983820 sp 0x7fffcf983800 T0)
==2715986==The signal is caused by a READ memory access.
==2715986==Hint: this fault was caused by a dereference of a high value address (see register values below).  Disassemble the provided pc to learn which register was used.
/audit/ff/firefox/obj-js-asan-no-debug-noopt-x86_64-pc-linux-gnu/dist/bin/llvm-symbolizer: error: '[anon:js-executable-memory]': No such file or directory
    #0 0x55bc3af4c6e8 in js::gc::HeaderWord::get() const /audit/ff/firefox/js/src/gc/Cell.h:113:23
    #1 0x55bc3af585ae in js::gc::TenuredCellWithNonGCPointer<JSClass const>::headerPtr() const /audit/ff/firefox/js/src/gc/Cell.h:763:54
    #2 0x55bc3af58544 in js::BaseShape::clasp() const /audit/ff/firefox/js/src/vm/Shape.h:242:41
    #3 0x55bc3af584bc in js::Shape::getObjectClass() const /audit/ff/firefox/js/src/vm/Shape.h:390:58
    #4 0x55bc3af5843c in JSObject::getClass() const /audit/ff/firefox/js/src/vm/JSObject.h:113:53
    #5 0x55bc3b0acbd4 in bool JSObject::is<js::PlainObject>() const /audit/ff/firefox/js/src/vm/JSObject.h:522:12
    #6 0x55bc3bea9c08 in js::gc::TenuringTracer::promoteObject(JSObject*) /audit/ff/firefox/obj-js-asan-no-debug-noopt-x86_64-pc-linux-gnu/js/src/gc/./../../../../js/src/gc/Tenuring.cpp:152:12
    #7 0x55bc3bea9c08 in js::gc::TenuringTracer::promoteOrForward(JSObject*) /audit/ff/firefox/obj-js-asan-no-debug-noopt-x86_64-pc-linux-gnu/js/src/gc/./../../../../js/src/gc/Tenuring.cpp:137:10
    #8 0x55bc3bea9ab1 in js::gc::TenuringTracer::onObjectEdge(JSObject**, char const*) /audit/ff/firefox/obj-js-asan-no-debug-noopt-x86_64-pc-linux-gnu/js/src/gc/./../../../../js/src/gc/Tenuring.cpp:120:11
    #9 0x55bc3be169d7 in js::gc::TraceEdgeInternal(JSTracer*, JSObject**, char const*) /audit/ff/firefox/js/src/gc/Tracer.h:111:1

    // .........

==2715986==Register values:
rax = 0x0828282828282f06  rbx = 0x00007fffcf9839e0  rcx = 0x00007baadfed62d0  rdx = 0x000055bc391efa60
rdi = 0x4141414141417830  rsi = 0x000010d3a7c33fa0  rbp = 0x00007fffcf983820  rsp = 0x00007fffcf983800
 r8 = 0x0000000000000000   r9 = 0x00007fffffffff01  r10 = 0x00007fffffffff01  r11 = 0x0000000000000001
r12 = 0x00007faae1734078  r13 = 0x0000000000000002  r14 = 0x0000000000000000  r15 = 0x00007baadff43930
AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /audit/ff/firefox/js/src/gc/Cell.h:113:23 in js::gc::HeaderWord::get() const
==2715986==ABORTING

To further exploit:

  1. Replace the stale wasm-gc object with a suitably shaped nursery object.
  2. Let a later valid stack map trace/promote the stale AnyRef.
  3. Resume wasm with the same local still statically typed as (ref $s). Use wasm-gc struct.set to write a controlled i64 at object + 16 + fieldIndex * 8.

The original report as published on Mozilla's Bugzilla, by Nebula Security. Images and videos are our own copies; raw HTML is shown as text. View it on Mozilla's Bugzilla.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The stack map is recorded at the builtin call's return PC, but the null-return failure trap resumes at trapPC plus the trap instruction length, a PC with no stack map, so GC during exception wrapping skips live wasm reference locals.

Impact

The stale reference can crash the engine and, per the reporter's outline, may allow a controlled write through wasm-gc struct.set, though the text only demonstrates a crash.

What the attacker needed

SpiderMonkey shell build; Attacker-supplied wasm module; Allocation pressure to force GC

How it was found

  1. Build the SpiderMonkey shell with the given configuration options.
  2. Run the attached proof-of-concept script under the shell.
  3. The script creates allocation pressure, triggers the failing concat, then allocates JS values to force a later nursery collection.
  4. The later collection traces the stale wasm frame word as an AnyRef and AddressSanitizer reports a crash.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

WebAssembly code can hold references to JavaScript objects, and the garbage collector (the part that frees unused memory) needs a map telling it which stack slots hold such references. Here the map was recorded for one instruction address, but the error path resumes at a slightly different address that has no map, so the collector skipped those slots. The program then used a reference that pointed at freed or moved memory, which crashed and could be abused to write attacker-chosen data.

What is Memory corruption?

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.

More by Nebula Security

View all

1 more disclosed report, best first.

More from Mozilla

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Memory corruption

The lesson behind this report.

Native code reads or writes memory it should not: overflows, use-after-free, type confusion. The core of browser, kernel and parser bugs.