← Back to Pull requests

fix: forbid using handles of one isolate in another #2090

Closed
unknown closed this pull request without merging on Oct 21, 2026 21:40 UTC
View on GitHub
unknown unknown commented on Oct 16, 2026 21:36 UTC
Author

A Local's lifetime ties it to a scope but not to that scope's
isolate. Signatures such as Object::set(&self, scope, key, value)
accept handles of any lifetime, so safe code can store a value of
isolate A in isolate B's heap:

#![forbid(unsafe_code)]
let a = &mut v8::Isolate::new(Default::default());
v8::scope!(let sa, a);
let from_a = v8::String::new(sa, "from-A").unwrap();

let b = &mut v8::Isolate::new(Default::default());
v8::scope!(let sb, b);
let ctx_b = v8::Context::new(sb, Default::default());
let sb = &mut v8::ContextScope::new(sb, ctx_b);
let obj = v8::Object::new(sb);
let key = v8::String::new(sb, "k").unwrap();
obj.set(sb, key.into(), from_a.into()); // B now points into A

sa.request_garbage_collection_for_testing(
  v8::GarbageCollectionType::Full,
);
obj.get(sb, key.into()); // dangling

This compiles and runs without a panic on 152.2.0 and on 140.1.1, so
it predates the scope rework in #1830. match_host() in handle.rs
already notes that scope-hosted handles can't be checked. Since the V8
14.2 upgrade, ContextScope::new compares against the current isolate
rather than the context's, so opening A's context in B's scope isn't
caught either.

All scopes open on a thread at the same time must belong to one
isolate. Opening a HandleScope, EscapableHandleScope or
CallbackScope while a scope of another isolate is open on the
thread panics. A handle can't outlive its scope, so no handle of
another isolate can be alive. The cost is one thread-local read and
write per scope open and close, and nothing per API call.
Debug builds also check on every Local deref that the value can
be used in the isolate owning the open scopes, via new
v8__Data__IsCompatibleWithIsolate in binding.cc. It finds the
isolate whose heap holds a value from V8's page metadata
(MemoryChunk → BasePage::heap() → Isolate::FromHeap). Smis
are compatible with any isolate. Read-only and writable shared
space values belong to an isolate group rather than an isolate,
and are compatible with any isolate of their group; with multiple
pointer compression cages, groups are told apart by cage. This also
catches handles that cross isolates without a second scope being
opened, e.g. TracedReference::get() with another isolate's scope.
v8__Data__MaybeGetIsolate and v8__Data__IsCompatibleWith (is a
value usable in another value's isolate) are added alongside, for
ContextScope::new, the GetIsolate impls and match_host() to
build on later.

Measured on an M-series Mac against V8 15.6, the thread-local check
adds ~0.7 ns to a JS→Rust callback round trip (3.00 → 3.72 ns) and
~0.2 ns to a HandleScope open/close (5.91 → 6.12 ns). The page lookup
costs ~0.45 ns per handle but runs on every deref, hence debug-only.

Handles that outlive scopes and APIs that take an isolate separately
from their handles are checked too:

Global::new() and Weak::new() resolve a Local's host to the
isolate owning the open scopes (the old match_host() TODO).
TracedReference and Eternal remember the isolate they were set
in, by its never-freed IsolateLiveness, and get() panics in
another isolate.
enqueue_microtask(), the snapshot creator's set_default_context()
and add_*(), String methods taking an Isolate,
ValueView::new(), the value (de)serializer helpers, V8Inspector,
V8InspectorSession and WasmStreaming::abort() check the isolate
they work on, and
MicrotaskQueue methods check that the queue belongs to it.

The rule relies on a Local not outliving its scope. A few producers
return a Local with a lifetime the caller picks (null(),
undefined(), Boolean::new(), which take any AsRef<Isolate>, and
some Module and ModuleRequest getters), and some wrappers that
hold handles have no lifetime at all (ScriptCompiler::Source,
PropertyDescriptor), so their handles can outlive their scope or
isolate. That predates this change, affects a single isolate too, and
needs signature changes, so it is left for a separate PR.

Code that keeps scopes of two isolates open on one thread at once now
panics even if it never mixes their handles. Nothing in this repo does
that. Callers that need it can open the second isolate's scopes inside
the unsafe allow_scopes_of_another_isolate().

Running the whole suite in one process (plain cargo test) aborts in
module_snapshot after isolate_drop_order leaks an entered isolate.
This also happens on main; nextest is unaffected.

Diff: 567 lines (+425 / -142) across 7 files Reviewer: nathanwhit Review duration: 7204m
All Vouch review confidence checks passed
Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.50 · 567 lines diff across 7 files
0.50
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.379
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #2090"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 49.7% (MEDIUM) · Higher = larger blast radius
49.7%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 19.6% · Safe to merge
19.6%