← All reports

[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

Severity: High | Component: JavaScriptCore B3 ReduceStrength (OMG Wasm tier, Wasm GC ref-type checks) | af957bf | Bugzilla 308231

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

@@ -3604,6 +3604,8 @@ class ReduceStrength {
SUPPRESS_UNCOUNTED_LOCAL const Wasm::RTT* targetRTT = cast->targetRTT();
if (!Wasm::typeIndexIsType(static_cast<Wasm::TypeIndex>(toHeapType))) {
if (rtt->isSubRTT(*targetRTT)) {
+ // shouldNegate can only be set on WasmRefTest.
+ ASSERT(!cast->shouldNegate());
replaceWithIdentity(structNew);
break;
}
@@ -3669,7 +3671,8 @@ class ReduceStrength {
int32_t toHeapType = cast->targetHeapType();
SUPPRESS_UNCOUNTED_LOCAL const Wasm::RTT* targetRTT = cast->targetRTT();
if (!Wasm::typeIndexIsType(static_cast<Wasm::TypeIndex>(toHeapType))) {
- replaceWithNewValue(m_proc.addIntConstant(m_value, !!rtt->isSubRTT(*targetRTT)));
+ const bool isSubtype = rtt->isSubRTT(*targetRTT);
+ replaceWithNewValue(m_proc.addIntConstant(m_value, cast->shouldNegate() ? !isSubtype : isSubtype));
break;
}
}

JSTests/wasm/gc/br_on_cast_fail.js

+// (type (;0;) (struct (field (mut i32))))
+// (type (;1;) (struct (field (mut i32)) (field (mut i64)) (field (mut i64))))
...
+// (func (;0;) (type 3) (result i32)
+// block (type 2) (result (ref struct)) ;; label = @1
+// i32.const 42
+// struct.new 0
+// br_on_cast_fail 0 (;@1;) (ref struct) (ref 1)
+// drop
+// i32.const 0
+// return
+// end
+// drop
+// i32.const 1
+// )
...
+for (let i = 0; i < 200000; i++) {
+ fail();
+ pass();
+}
+
+if (fail() === 0 || pass() === 0)
+ throw new Error("br_cast_on_fail incorrect");

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:

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.

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.

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:

  1. Declare two struct types where the target type (type 1) is strictly larger than, and not a supertype of, the allocated type (type 0).
  2. In a function, allocate with struct.new 0 and pass the result directly to br_on_cast_fail ... (ref struct) (ref 1), so the B3 operand of the negated test is the WasmStructNew itself.
  3. Call the function enough times (the test uses 200,000 iterations) to tier it up from BBQ to OMG, where ReduceStrength runs.
  4. 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).
  5. 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.

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.