fix: forbid using handles of one isolate in another #2090
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
nathanwhit
A
Local's lifetime ties it to a scope but not to that scope'sisolate. 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:
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()inhandle.rsalready notes that scope-hosted handles can't be checked. Since the V8
14.2 upgrade,
ContextScope::newcompares against the current isolaterather 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,EscapableHandleScopeorCallbackScopewhile a scope of another isolate is open on thethread 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
Localderef that the value canbe used in the isolate owning the open scopes, via new
v8__Data__IsCompatibleWithIsolateinbinding.cc. It finds theisolate whose heap holds a value from V8's page metadata
(
MemoryChunk→BasePage::heap()→Isolate::FromHeap). Smisare 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__MaybeGetIsolateandv8__Data__IsCompatibleWith(is avalue usable in another value's isolate) are added alongside, for
ContextScope::new, theGetIsolateimpls andmatch_host()tobuild 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
HandleScopeopen/close (5.91 → 6.12 ns). The page lookupcosts ~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()andWeak::new()resolve aLocal's host to theisolate owning the open scopes (the old
match_host()TODO).TracedReferenceandEternalremember the isolate they were setin, by its never-freed
IsolateLiveness, andget()panics inanother isolate.
enqueue_microtask(), the snapshot creator'sset_default_context()and
add_*(),Stringmethods taking anIsolate,ValueView::new(), the value (de)serializer helpers,V8Inspector,V8InspectorSessionandWasmStreaming::abort()check the isolatethey work on, and
MicrotaskQueuemethods check that the queue belongs to it.The rule relies on a
Localnot outliving its scope. A few producersreturn a
Localwith a lifetime the caller picks (null(),undefined(),Boolean::new(), which take anyAsRef<Isolate>, andsome
ModuleandModuleRequestgetters), and some wrappers thathold handles have no lifetime at all (
ScriptCompiler::Source,PropertyDescriptor), so their handles can outlive their scope orisolate. 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
unsafeallow_scopes_of_another_isolate().Running the whole suite in one process (plain
cargo test) aborts inmodule_snapshotafterisolate_drop_orderleaks an entered isolate.This also happens on
main; nextest is unaffected.