Skip to content

Instantly share code, notes, and snippets.

@Atomics-hub
Created April 1, 2026 22:49
Show Gist options
  • Select an option

  • Save Atomics-hub/baf37d7f7402a5011bbed61208872339 to your computer and use it in GitHub Desktop.

Select an option

Save Atomics-hub/baf37d7f7402a5011bbed61208872339 to your computer and use it in GitHub Desktop.
V8 Maglev JIT: Uninitialized SpeculationMode in SaveCallSpeculationScope (CL 7651434)
// PoC: Uninitialized SpeculationMode in SaveCallSpeculationScope
//
// Triggers uninitialized read of saved_mode_ in the destructor of
// MaglevGraphBuilder::SaveCallSpeculationScope during Maglev JIT compilation.
//
// The destructor restores builder_->current_speculation_mode_ from the member
// saved_mode_, which is never initialized by the constructor.
//
// Reproduction:
// 1. Build d8 with MSAN:
// gn gen out/msan --args='is_msan=true is_debug=false v8_enable_maglev=true'
// autoninja -C out/msan d8
// 2. Run:
// out/msan/d8 --maglev --no-turbofan poc.js
// 3. Expected: MSAN reports use-of-uninitialized-value in
// SaveCallSpeculationScope::~SaveCallSpeculationScope()
//
// Alternatively with debug build + v8 tracing:
// out/debug/d8 --maglev --no-turbofan --trace-maglev-graph-building poc.js
// === Path 1: Function.prototype.call ===
// TryReduceBuiltin creates SaveCallSpeculationScope (outer)
// TryReduceFunctionPrototypeCall creates SaveCallSpeculationScope (inner)
// Both destructors read uninitialized saved_mode_
function triggerViaCall(f, x) {
return f.call(null, x);
}
// === Path 2: Function.prototype.apply ===
// TryReduceFunctionPrototypeApplyCallWithReceiver creates SaveCallSpeculationScope
function triggerViaApply(f, x) {
return f.apply(null, [x]);
}
// === Path 3: Array.prototype.forEach callback ===
// Array iteration template creates SaveCallSpeculationScope for callback reduction
function triggerViaForEach(arr, cb) {
arr.forEach(cb);
}
// === Path 4: new Array() constructor ===
// BuildConstruct creates SaveCallSpeculationScope
function triggerViaConstruct(n) {
return new Array(n);
}
// === Path 5: instanceof (@@hasInstance) ===
// HasInstance path creates SaveCallSpeculationScope
function triggerViaInstanceOf(obj, ctor) {
return obj instanceof ctor;
}
// Warmup all paths to trigger Maglev compilation
// Maglev typically compiles after ~1000 invocations
// Path 1: Function.prototype.call
for (let i = 0; i < 50000; i++) {
triggerViaCall(Math.abs, -42);
}
if (triggerViaCall(Math.abs, -7) !== 7) throw "Path 1 failed";
// Path 2: Function.prototype.apply
for (let i = 0; i < 50000; i++) {
triggerViaApply(Math.abs, -42);
}
if (triggerViaApply(Math.abs, -7) !== 7) throw "Path 2 failed";
// Path 3: Array.prototype.forEach
let sum = 0;
const add = (x) => { sum += x; };
for (let i = 0; i < 50000; i++) {
sum = 0;
triggerViaForEach([1, 2, 3], add);
}
sum = 0;
triggerViaForEach([1, 2, 3], add);
if (sum !== 6) throw "Path 3 failed";
// Path 4: new Array()
for (let i = 0; i < 50000; i++) {
triggerViaConstruct(10);
}
let arr = triggerViaConstruct(5);
if (arr.length !== 5) throw "Path 4 failed";
// Path 5: instanceof
class Foo {}
let foo = new Foo();
for (let i = 0; i < 50000; i++) {
triggerViaInstanceOf(foo, Foo);
}
if (!triggerViaInstanceOf(foo, Foo)) throw "Path 5 failed";
const log = typeof print === "function" ? print : console.log;
log("[*] All 5 SaveCallSpeculationScope paths exercised during Maglev compilation");
log("[*] Run with MSAN build to detect uninitialized read of saved_mode_");
log("[*] Bug: src/maglev/maglev-graph-builder.cc:392 — SpeculationMode saved_mode_ never initialized");

V8 Maglev JIT: Uninitialized SpeculationMode in SaveCallSpeculationScope

Component: V8 Maglev JIT compiler (src/maglev/maglev-graph-builder.cc)
Affects: All platforms running Chrome/V8 with Maglev enabled (default since Chrome 117)
Status: Fixed by Victor Gomes (CL 7651434)
Chromium issue: 489577364

Summary

MaglevGraphBuilder::SaveCallSpeculationScope saves and restores current_speculation_feedback_ but fails to save current_speculation_mode_ before overwriting it. The destructor restores current_speculation_mode_ from the uninitialized member saved_mode_, which is undefined behavior (CWE-457).

SpeculationMode controls whether Maglev emits speculative type-specialized code or safe generic code:

enum class SpeculationMode {
  kAllowSpeculation = 0,                // Emit speculative type-specialized code
  kDisallowBoundsCheckSpeculation = 1,  // Allow speculation but not bounds check elision
  kDisallowSpeculation = 3              // Emit safe generic code only
};

The dangerous value kAllowSpeculation is 0 — the most common value in uninitialized stack memory. V8 explicitly removes -ftrivial-auto-var-init in release builds (gni/v8.gni:405-410).

Root Cause

File: src/maglev/maglev-graph-builder.cc, lines 362-393

class V8_NODISCARD MaglevGraphBuilder::SaveCallSpeculationScope {
 public:
  explicit SaveCallSpeculationScope(
      MaglevGraphBuilder* builder,
      compiler::FeedbackSource feedback_source = compiler::FeedbackSource())
      : builder_(builder) {
    saved_ = builder_->current_speculation_feedback();    // Saves feedback OK
    // BUG: saved_mode_ is NEVER set to builder_->current_speculation_mode_
    SpeculationMode mode = MaglevGraphBuilder::GetSpeculationMode(
        builder_->broker(), feedback_source);
    if (mode != SpeculationMode::kDisallowSpeculation) {
      builder_->reducer_.set_current_speculation_feedback(feedback_source);
      builder_->current_speculation_mode_ = mode;         // Overwrites mode
    } else {
      builder->reducer_.set_current_speculation_feedback(
          compiler::FeedbackSource());
      builder_->current_speculation_mode_ =
          SpeculationMode::kDisallowSpeculation;           // Overwrites mode
    }
  }
  ~SaveCallSpeculationScope() {
    builder_->reducer_.set_current_speculation_feedback(saved_);  // Restores feedback OK
    builder_->current_speculation_mode_ = saved_mode_;            // Restores from UNINIT
  }

 private:
  MaglevGraphBuilder* builder_;
  compiler::FeedbackSource saved_;
  SpeculationMode saved_mode_;        // NEVER INITIALIZED
};

The constructor overwrites builder_->current_speculation_mode_ but never saves the old value into saved_mode_ first. Contrast with saved_ which correctly saves and restores current_speculation_feedback().

Impact

CanSpeculateCall() is the primary guard at 30+ locations throughout the Maglev graph builder:

bool CanSpeculateCall() const {
    return current_speculation_mode_ == SpeculationMode::kAllowSpeculation;
}

Affected paths include speculative reductions for:

  • Array.prototype.push/pop/at — array type specialization, bounds check elision
  • DataView get/set — typed array access
  • Math.abs and other Math builtins — integer overflow checks
  • String.prototype.charAt/charCodeAt — uses extended mode check
  • Function.prototype.apply — call target speculation
  • All Array iteration builtins (forEach, map, filter, some, every, find, reduce, includes)

The scope is instantiated at 6 locations (lines 9156, 10350, 11482, 12186, 13552, 13948).

The V8 team assessed this as a bug, not a vulnerability — the deopt guards in generated code are the real safety net, so the worst case scenario is a deopt loop rather than type confusion. The speculation mode only controls whether optimization is attempted, not whether safety checks exist in the generated code. See Victor Gomes' comment.

Reproduction

# Build V8 with MSAN
gn gen out/msan --args='is_msan=true is_debug=false v8_enable_maglev=true target_cpu="x64"'
autoninja -C out/msan d8

# Run PoC — triggers all 6 SaveCallSpeculationScope paths
out/msan/d8 --maglev --no-turbofan poc.js

Expected MSAN output:

WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 MaglevGraphBuilder::SaveCallSpeculationScope::~SaveCallSpeculationScope()
       src/maglev/maglev-graph-builder.cc:384

Fix

One line added to the constructor (CL 7651434):

     saved_ = builder_->current_speculation_feedback();
+    saved_mode_ = builder_->current_speculation_mode_;
     SpeculationMode mode = MaglevGraphBuilder::GetSpeculationMode(

Timeline

  • 2026-03-03: Reported to Chrome VRP
  • 2026-03-06: Triaged, assigned P1/S1 (Milestone 145)
  • 2026-03-10: Downgraded to P2/S2 Bug. Fixed by Victor Gomes
  • 2026-03-11: Added to reward-topanel hotlist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment