[JSC] Respect shouldNegate() on WasmRefTypeCheckValue nodes in OMG
CVE: CVE-2026-86922 · Safari 27 · Released September 14, 2026 Impact: Processing maliciously crafted web content may lead to arbitrary code execution Apple's description: A type confusion issue was addressed with improved state management. Credit: Nathaniel Oh (@calysteon), Kwak Kiyong / kakaogames
High. A dropped negation flag flips br_on_cast_fail's branch, so OMG code takes the "cast succeeded" path while holding a struct that is not the target type. Escalation needs fallthrough code that touches fields the real object doesn't have. Validation lets a crafted module do that.
WebAssembly's GC proposal gives modules typed heap structs, and JavaScriptCore's optimizing Wasm tier, OMG, trusts those static types enough to compile field accesses down to fixed-offset loads and stores. When OMG lowers a reference type test, it emits a WasmRefTypeCheckValue node into B3 IR, and B3's ReduceStrength phase may fold that test to a constant when the operand's exact runtime type is known at compile time. Such a fold is only sound if the constant it produces equals what the dynamic check would have produced. That includes the node's shouldNegate() polarity flag, which turns "is a subtype" into "is not a subtype".
The angle: A crafted Wasm GC module can steer optimized code onto the cast-succeeded path with an object of the wrong struct type, setting up mis-typed field access relative to that object.
Source/JavaScriptCore/b3/B3ReduceStrength.cpp
JSTests/wasm/gc/br_on_cast_fail.js
Patch Details
The commit touches two WasmRefTypeCheckValue folding rules in ReduceStrength. Both rules fire only for one shape: the reference being checked comes directly from a WasmStructNew. In that case the object's exact RTT is known at compile time and can be compared against cast->targetRTT() with rtt->isSubRTT(*targetRTT). Both also sit inside the !Wasm::typeIndexIsType(...) arm, which is the branch where the target heap type is a concrete type index rather than an abstract heap type.
Ref-test fold (the fix). The second hunk folds a ref test into an integer constant. Before the patch it emitted m_proc.addIntConstant(m_value, !!rtt->isSubRTT(*targetRTT)), which is the raw subtype answer, and never consulted cast->shouldNegate(). After the patch it computes isSubtype once and emits cast->shouldNegate() ? !isSubtype : isSubtype. A negated test now folds to the correct polarity.
Ref-cast fold (defensive). The first hunk replaces a ref cast with its structNew operand (replaceWithIdentity(structNew)) when the subtype relation holds. It gains ASSERT(!cast->shouldNegate()) and a comment stating that shouldNegate can only be set on WasmRefTest. Runtime behaviour does not change. The hunk writes down the invariant that only the test form carries negation, so a negated cast reaching this rule is a lowering bug.
Regression test. JSTests/wasm/gc/br_on_cast_fail.js defines a recursive type group with two unrelated struct types. Type 0 has one i32 field. Type 1 has an i32 field followed by two i64 fields. Two exported functions each run br_on_cast_fail against (ref 1) on a freshly constructed struct:
failbuilds a type-0 struct. The cast should fail, the branch should be taken, and the function returns 1.passbuilds a type-1 struct. The cast should succeed, execution should fall through, and the function returns 1.
The 200,000-iteration loop forces both functions to tier up to OMG. The final check fails if either returns 0, which is the value each function produces on the wrong path.
Background
WebAssembly GC structs and RTTs.
The WebAssembly GC proposal adds heap-allocated struct types with declared, typed fields. Each struct type has a runtime type descriptor (RTT) that records where the type sits in the module's declared subtype hierarchy. isSubRTT answers whether one RTT is a subtype of another.
br_on_cast_fail.
br_on_cast_fail label rt1 rt2 tests whether its operand reference is of type rt2. If the test fails, it branches to label, carrying the value at type rt1. If the test succeeds, it falls through, and the validator types the value left on the stack as rt2. The two successors therefore see the same reference at two different static types.
Static types and elided checks.
Wasm validation guarantees static types before any code runs. Compiled code can rely on that guarantee: struct.get/struct.set on a value statically typed as a concrete struct access fields at fixed offsets, with no runtime RTT check.
JSC's Wasm tiers. JavaScriptCore compiles Wasm in tiers. BBQ is the baseline compiler. OMG is the optimizing compiler, which lowers Wasm to B3, JSC's low-level SSA IR. Hot functions tier up from BBQ to OMG.
ReduceStrength.
ReduceStrength is a B3 phase that performs constant folding, canonicalization and peephole simplifications, iterating until nothing changes. Each rule pattern-matches a value and its operands and either replaces it with a simpler value (replaceWithNewValue, replaceWithIdentity) or leaves it alone.
WasmRefTypeCheckValue.
This B3 value class represents Wasm reference type checks in two forms. The test form produces a 0/1 integer. The cast form produces the reference itself or traps. Both carry a target heap type (targetHeapType()) and RTT (targetRTT()), plus a shouldNegate() flag that inverts a test's result.
WasmStructNew.
WasmStructNew is the B3 value that allocates a struct. Its RTT is statically known, which lets strength reduction answer subtype questions about the freshly allocated object at compile time.
Analysis
This is a logic error in a compiler optimization that produces Wasm GC type confusion: a constant fold of a predicate node computed the un-negated answer for a negated node.
fail(): struct.new 0 ──► br_on_cast_fail 0 (ref struct) (ref 1)
RTT = type 0 OMG: WasmRefTest(target=type 1, shouldNegate=true)
Before fix (OMG): After fix (OMG):
isSubRTT(type0, type1) = false isSubtype = false
fold → const 0 (negate ignored) fold → const !false = 1
└─► branch NOT taken └─► branch taken → @1
└─► fallthrough: value value at (ref struct) ✓
typed (ref 1) ✗ type-0 obj
BBQ / un-folded path: dynamic test, correct polarity either way
The left column of the diagram is the whole bug. OMG expresses br_on_cast_fail as a negated WasmRefTest: the branch condition is "the cast failed", so the node's result must be 1 when the operand is not a subtype of the target. The commit message, the test name and the new ASSERT comment ("shouldNegate can only be set on WasmRefTest") all point to this lowering. In fail, the operand comes straight from struct.new 0, so ReduceStrength sees a WasmStructNew operand with a concrete target and takes the fold. isSubRTT(type0, type1) is false. The correct folded value for the negated node is !false = 1. The pre-fix rule emitted the raw isSubtype, which is 0. The branch is no longer taken and execution falls through to the success successor.
The fallthrough is the dangerous half, because of what it means in Wasm's type system. The validator has already typed the value on the fallthrough edge as (ref 1), the target type, which has three fields. The object actually sitting in that register is a type-0 struct with one i32 field. OMG trusts validated static types and emits no runtime RTT check on struct.get/struct.set against a concretely typed reference. Any field access on that path would therefore be laid out against type 1's definition while the storage underneath belongs to type 0. In the regression test the value is immediately dropped, so the only observable symptom is that fail() returns 0 instead of 1. Wasm validation places no such restriction on a crafted module: the fallthrough code is free to struct.get or struct.set the i64 fields that type-0 objects do not have.
The opposite direction is the pass function, and its failure is benign. A type-1 struct checked against (ref 1) is a subtype, so the raw fold produced 1, the negated branch was wrongly taken, and the value was delivered to the label at the weaker type (ref struct). That is a correctness bug, because the function returns the wrong answer. It is not memory-unsafe, because code at (ref struct) cannot assume any concrete field layout. The two directions are asymmetric: the inversion only causes memory unsafety when it moves an object to the successor where the validator granted a stronger type.
The trigger sequence, as the regression test exercises it:
- Declare two struct types where the target type (type 1) is strictly larger than, and not a supertype of, the allocated type (type 0).
- In a function, allocate with
struct.new 0and pass the result directly tobr_on_cast_fail ... (ref struct) (ref 1), so the B3 operand of the negated test is theWasmStructNewitself. - Call the function enough times (the test uses 200,000 iterations) to tier it up from BBQ to OMG, where
ReduceStrengthruns. - In OMG code, the negated test folds to
0. The branch falls through and the type-0 object arrives on the path statically typed(ref 1). - Any field access on that path against type 1's layout is the type confusion. The test stops at observing the wrong return value.
The tier-up requirement explains why the bug is invisible to anything that does not drive functions hot. BBQ does not run this ReduceStrength fold, so baseline code performs the dynamic test with correct polarity. Only the OMG-compiled body inverts. The fold's narrow shape, a ref test directly on a WasmStructNew, is rare in compiler-generated or hand-written Wasm but trivial to produce in a crafted module.
The bug is reachable from web content through an attacker-supplied Wasm GC module once the target function tiers up to OMG. The inverted fold is visible directly in the diff, and the regression test shows the success-path fallthrough being taken for a non-subtype object whose validator-assigned type is the larger target struct. The immediate effect is wrong control flow. If the fallthrough code accesses the target type's extra fields, the effect would become a relative out-of-bounds or mis-typed access over Wasm GC struct storage in the WebContent process. The primitive's ceiling is a relative read/write bounded by the size difference between the target struct's field layout and the real object's layout, with field-kind confusion wherever the two definitions disagree. If the target type declares reference-typed fields where the real object holds integers, or the reverse, this could become a reference/integer confusion. That depends on the struct definitions the attacker chooses, and the regression test, which drops the value, does not exercise it. An attacker controlling the module bytes could plausibly turn that into the memory-safety violation behind Apple's "arbitrary code execution" impact. All of this stays inside the WebContent sandbox; impact beyond the renderer would require a separate escape.
The fix restores the invariant at the fold site: the constant now depends on both the subtype relation and the node's polarity, so fail folds to 1, takes the branch, and the type-0 object only ever reaches code that sees it as (ref struct). The ref-cast hunk takes a different approach. It does not make the cast fold polarity-aware; it asserts that the cast form is never negated. That assertion only protects debug builds. If a future lowering set shouldNegate on a cast, release builds would reintroduce a silent mismatch in that fold, with no polarity check at all.
Dropping one polarity flag in a compile-time fold put a type-0 struct on the path where the validator had already promised type 1, and OMG trusts that promise for field offsets.
Insight
Takeaway: every rewrite of a flagged IR node must be a function of all its flags. Grep each fold and CSE key for targetRTT()/targetHeapType() reads that never touch shouldNegate(); a match is a fold that computes the result as if the flag were unset.
Audit directions
-
Rewrite rules that read a node's primary semantics but ignore a modifier flag on the same node. The invariant is that every fold of a flagged node must depend on all of its flags, not just its opcode and operands. Narrow: grep
B3ReduceStrength.cpp,B3PureCSE/B3ValueKey, and the other B3 phases for every use ofWasmRefTypeCheckValue, and confirm each one readsshouldNegate()and nullability (allowNull-style accessors). Also check thatValueKeyhashing distinguishes negated from non-negated tests. A match is any fold or CSE key that readstargetRTT()/targetHeapType()without also readingshouldNegate(). Wider: the same shape appears in DFG/FTL nodes with mode flags (array modes,ArithMode,ClobberizeMode) and in B3 memory values withsigned/fencekinds. A match is a switch-on-opcode fold whose result does not depend on the node's flag accessors. Widest: any compiler IR that encodes predicate inversion or variant behaviour as a flag rather than a distinct opcode, such as LLVMicmppredicates ornsw/nuwflags dropped during InstCombine, and Cranelift or V8 Turbofan nodes with operator parameters. The question to carry: does every peephole that matches this opcode also match on, or preserve, its parameters? -
Control-flow polarity across every Wasm GC instruction whose fallthrough or result type is narrower than its input:
br_on_cast,br_on_cast_fail,ref.cast,ref.as_non_null,br_on_null/br_on_non_null. The class is polarity errors that land the program on the successor where the validator granted a stronger type. Narrow: trace how OMG IR generation for these opcodes constructsWasmRefTypeCheckValueand which branch consumes its result. A match is any lowering where the "taken" edge corresponds to the negated test and a downstream fold does not account for it. Wider: run differential tier testing, comparing BBQ and OMG results for the same module. Any divergence on a cast-branch opcode is a candidate. Widest: in any typed bytecode VM (JVMcheckcast/instanceoffolding in C2 or Graal, .NET RyuJIT cast elimination), inverting a type-test branch is a type confusion, because the verifier types each successor differently. Audit cast-elimination passes for which successor inherits the narrowed type. -
Compile-time subtype shortcuts that trust a statically known allocation site, like the
WasmStructNewRTT shortcut used in both hunks. The invariant is that a fold may only replace a dynamic check if it reproduces the check's full result, including inversion, null handling, and thetypeIndexIsTypeabstract-heap-type branch. Narrow: inspect thetypeIndexIsType/ abstract-heap-type path next to these hunks, and theWasmArrayNewanalogues if present. A match is a fold that handles only one arm of the concrete-versus-abstract target split, or that never considers a null/non-null target. Wider: any JIT allocation-sinking or escape-analysis pass that answers type queries from the allocation node (DFGNewObjectstructure folding, FTL allocation sinking). A match is a type-query fold whose answer comes from the allocation and is not re-derived from the query node's full parameters. Widest: "proof from allocation site" optimizations must model the query exactly. The same class appears in Graal partial escape analysis and in V8 Turbofan'sCheckMapselimination on freshly allocated objects.