Closed Bug 2038020 Opened 6 months ago Closed 6 months ago

Crash [@ wasm_text_to_binary] — wast crate error formatter panics "Formatting argument out of range" on malformed WAT with many funcs

Categories

(Core :: JavaScript: WebAssembly, defect)

defect

Tracking

()

RESOLVED DUPLICATE of bug 2000288

People

(Reporter: david.lie, Unassigned)

Details

(Keywords: ai-involved)

Attachments

(1 file)

519 bytes, text/javascript
Details
Attached file poc.js —

When wasmTextToBinary() is called with a WAT module that contains a large number
of function definitions followed by a stray module-level token, the wast Rust
crate's error formatter panics:

Hit MOZ_CRASH(Formatting argument out of range)
  at third_party/rust/wast/src/error.rs:150

The panic is routed through mozglue_static::panic_hook to MOZ_Crash, producing
a deterministic process abort. Unlike debug-only assertions, MOZ_CRASH fires in
both debug and release builds. Reproduces with and without --fuzzing-safe.


Steps to reproduce

Attached poc.js:

var code = "(module ";
for (var i = 0; i < 1000; i++) {
    code += "(func (param i32) (result i32) (i32.add (i32.const 1) (local.get 0))) ";
}
code += "0";   // stray token at module level
wasmTextToBinary(code);

Run:

./js --fuzzing-safe poc.js

No additional flags are needed. The 1 000-function preamble is required to reach
the argument-index overflow in the formatter; the trailing token ("0") can be any
identifier or literal that makes the WAT syntactically invalid at module scope.
Three tokens have been confirmed: "0", "true", "Symbol(x)".


Actual results

[PID] Hit MOZ_CRASH(Formatting argument out of range)
  at /…/third_party/rust/wast/src/error.rs:150
#04: std::panicking::panic_with_hook
#09: <wast::error::Error as core::fmt::Display>::fmt  (wast/src/error.rs:150)
    #3 mozglue_static::panic_hook  mozglue/static/rust/lib.rs:99

Full call chain from the shell API boundary:

wasmTextToBinary (shell/js.cpp:8778)
  → js::wasm::TextToBinary (shell/WasmTesting.cpp:45)
    → wasm_text_to_binary (js/src/rust/wasm.rs:37)
      → <wast::Error as Display>::fmt (wast/src/error.rs:150)
        → panic("Formatting argument out of range")
          → mozglue_static::panic_hook → MOZ_Crash

The UBSan SEGV that follows is the deliberate MOZ_CrashSequence null write, not
an independent fault.


Expected results

wasmTextToBinary(badInput) should throw a JavaScript exception with a human-readable
parse-error message — not abort the process.


Root cause

The error formatter <wast::error::Error as fmt::Display>::fmt in
third_party/rust/wast/src/error.rs uses:

write!(
    f,
    "… {marker:>0$} …",
    text.col + 1,   // positional arg 0 (used as alignment width)
    …
    marker = "^",
)

The format specifier {marker:>0$} references positional argument 0 as the
alignment width. When the parse error originates at a column position that makes
the Rust format runtime miscalculate the argument index,
core::fmt::rt::Argument::from_usize is called with an out-of-range index and
panics.

This is a bug in the vendored wast crate (upstream: bytecodealliance/wasm-tools,
wast crate). An upstream fix — rewriting {marker:>0$} to use a named width
argument — is the preferred resolution. The vendored copy should be updated when
the fix is available.


Build / repro environment

  • Engine: SpiderMonkey JavaScript-C142.0a1
  • Tree HEAD: 5836a062726f ("Bug 1960567 — remove last C++ scriptable APIs for legacy telemetry")
  • Platform: Linux x86_64

The report indicates this was tested on a nearly year-old build. Is this reproducible with the current tip?

Component: JavaScript Engine: JIT → JavaScript: WebAssembly
Flags: needinfo?(david.lie)
Keywords: ai-involved

I'm getting a regular SyntaxError running the attached script.

$ ./obj-x86_64-pc-linux-gnu/dist/bin/js /tmp/poc.js
/tmp/poc.js:12:17 SyntaxError: wasm text error: expected `(` at <anon>:1:70009
Stack:
  @/tmp/poc.js:12:17
$ ./obj-x86_64-pc-linux-gnu/dist/bin/js --fuzzing-safe /tmp/poc.js
/tmp/poc.js:12:17 SyntaxError: wasm text error: expected `(` at <anon>:1:70009
Stack:
  @/tmp/poc.js:12:17

The commit is:

Commit ID: 10725540f638ba15d8138079dab01ecbac808be5
Change ID: psmyzywumwsrzmuoqlzynryoprumyntk
Bookmarks: main main@git main@origin main@upstream
Author   : Daniel Holbert <dholbert@cs.stanford.edu> (2026-05-07 14:33:48)
Committer: dholbert@mozilla.com <dholbert@mozilla.com> (2026-05-07 14:35:57)

    Bug 2037754: Use 'MakeAndAddRef' instead of 'new' when allocating runnable for nsContentUtils::AddScriptRunner. r=dom-core,smaug

However ESR140.10.1 provided by js140 on Arch Linux does panic:

$ js140 --version
JavaScript-C140.10.1
$ js140 /tmp/poc.js
Segmentation fault         (core dumped) js140 /tmp/poc.js

THe coredump provides a similar backtrace:

#0  MOZ_CrashSequence (aAddress=0x0, aLine=<optimized out>) at /usr/src/debug/js140/firefox-140.10.1/obj/dist/include/mozilla/Assertions.h:268
#1  MOZ_Crash (aFilename=<optimized out>, aLine=150, aReason=0x7ffffbf87850 "Formatting argument out of range")
    at /usr/src/debug/js140/firefox-140.10.1/obj/dist/include/mozilla/Assertions.h:381
#2  RustMozCrash (aFilename=<optimized out>, aLine=150, aReason=0x7ffffbf87850 "Formatting argument out of range") at wrappers.cpp:18
#3  0x0000555642435384 in mozglue_static::panic_hook (info=<optimized out>) at mozglue/static/rust/lib.rs:99
#4  0x000055564243513c in core::ops::function::FnOnce::call_once<fn(&std::panic::PanicHookInfo), (&std::panic::PanicHookInfo)> ()
    at /usr/src/debug/rust/rustc-1.95.0-src/library/core/src/ops/function.rs:166
#5  core::ops::function::FnOnce::call_once<fn(&std::panic::PanicHookInfo), (&std::panic::PanicHookInfo)> ()
    at /usr/src/debug/rust/rustc-1.95.0-src/library/core/src/ops/function.rs:250
#6  0x0000555641c37ae0 in std::panicking::panic_with_hook ()
#7  0x0000555641c11b0c in std::panicking::panic_handler::{closure#0} ()
#8  0x0000555641c07ad8 in std::sys::backtrace::__rust_end_short_backtrace::<std::panicking::panic_handler::{closure#0}, !> ()
#9  0x0000555641c13b08 in __rustc::rust_begin_unwind ()
#10 0x0000555641c92b98 in core::panicking::panic_fmt ()
#11 0x000055564239de98 in core::fmt::rt::Argument::from_usize (x=<optimized out>) at /usr/src/debug/rust/rustc-1.95.0-src/library/core/src/fmt/rt.rs:132
#12 wast::error::{impl#1}::fmt (self=<optimized out>, f=<optimized out>) at src/error.rs:150
#13 0x0000555641c91e2c in core::fmt::write ()
#14 0x0000555641c82fc8 in alloc::fmt::format::format_inner ()
#15 0x00005556423580dc in alloc::fmt::format::{closure#0} () at /usr/src/debug/rust/rustc-1.95.0-src/library/alloc/src/fmt.rs:659
#16 core::option::Option<&str>::map_or_else<&str, alloc::string::String, alloc::fmt::format::{closure_env#0}, fn(&str) -> alloc::string::String> (self=..., default=...,
    f=<optimized out>) at /usr/src/debug/rust/rustc-1.95.0-src/library/core/src/option.rs:1278
#17 alloc::fmt::format (args=...) at /usr/src/debug/rust/rustc-1.95.0-src/library/alloc/src/fmt.rs:659
#18 jsrust::wasm::wasm_text_to_binary (text=<optimized out>, text_len=<optimized out>, out_bytes=0x7ffffbf88248, out_bytes_len=0x7ffffbf88240, out_error=0x7ffffbf88238,
    out_error_len=0x7ffffbf88230) at js/src/rust/wasm.rs:37
#19 0x0000555641cb57d4 in js::wasm::TextToBinary (text=0x5556424d1000 <icu_77::gTZGNLock> u"", textLen=150, bytes=0x7ffffbf882a8, error=0x7ffffbf882a0)
    at /usr/src/debug/js140/firefox-140.10.1/js/src/shell/WasmTesting.cpp:45
#20 0x0000555641cc8208 in WasmTextToBinary (cx=0x555677f2db40, argc=<optimized out>, vp=<optimized out>) at /usr/src/debug/js140/firefox-140.10.1/js/src/shell/js.cpp:8771
#21 0x0000555641858df4 in CallJSNative (cx=0x555677f2db40, native=0x555641cc8120 <WasmTextToBinary(JSContext*, unsigned int, JS::Value*)>, reason=js::CallReason::Call, args=...)
    at /usr/src/debug/js140/firefox-140.10.1/js/src/vm/Interpreter.cpp:494
#22 js::InternalCallOrConstruct (cx=0x555677f2db40, args=..., construct=js::NO_CONSTRUCT, reason=js::CallReason::Call)
    at /usr/src/debug/js140/firefox-140.10.1/js/src/vm/Interpreter.cpp:590
#23 InternalCall (cx=0x555677f2db40, args=..., reason=js::CallReason::Call) at /usr/src/debug/js140/firefox-140.10.1/js/src/vm/Interpreter.cpp:657
#24 js::CallFromStack (cx=0x555677f2db40, args=..., reason=js::CallReason::Call) at /usr/src/debug/js140/firefox-140.10.1/js/src/vm/Interpreter.cpp:662
#25 js::jit::DoCallFallback (cx=<optimized out>, frame=<optimized out>, stub=<optimized out>, argc=<optimized out>, vp=<optimized out>, res=...)
    at /usr/src/debug/js140/firefox-140.10.1/js/src/jit/BaselineIC.cpp:1705
#26 0x00001e0fc81c914c in ?? ()

This is a duplicate and has been fixed updating wasm-tools by bug 2000288. Please re-open if it's still reproducible.

Status: UNCONFIRMED → RESOLVED
Closed: 6 months ago
Duplicate of bug: 2000288
Flags: needinfo?(david.lie)
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: