From ef02af6d9b70c29bd842b49390d0c1926f89aa24 Mon Sep 17 00:00:00 2001 From: ldm0 Date: Tue, 15 Sep 2026 04:47:05 +0800 Subject: [PATCH] fix(realms): unwrap bound function and Proxy targets Bound functions wrapping foreign proxies can have a creation context that differs from the underlying target realm. Expose V8 GetBoundFunction and follow both internal target chains when resolving a callable realm, including revoked-Proxy errors without executing property traps. Cover mixed chains, prototype mutation, revoked proxies, all eight Wasm constructors, and DOMParser prototype fallback with unchanged document ownership and getter counts. Validation: cargo fmt --all; strict workspace/all-targets/all-features Clippy; full nextest (18,417 passed, 13 skipped); CLI build; 17/17 selected WPT passed. The Wasm NewTarget realm WPT improves from 112/120 to 120/120 subtests. --- .../extracted/window_and_frame_surfaces.rs | 19 ++++- .../script_vm/tests/dom_xhr/dom/dom_parser.rs | 35 ++++++++- moli-renderer-v8/src/util.rs | 75 +++++++++++++++++++ moli-v8-util/src/constructors.rs | 31 +++++--- vendor/v8-152.2.0/MOLI_VENDORING.md | 2 + vendor/v8-152.2.0/src/function.rs | 10 +++ vendor/v8-152.2.0/src/function_ext.cc | 14 ++++ 7 files changed, 174 insertions(+), 12 deletions(-) create mode 100644 vendor/v8-152.2.0/src/function_ext.cc diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/window_and_frame_surfaces.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/window_and_frame_surfaces.rs index cf9f57263ac..ee04fb20eeb 100644 --- a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/window_and_frame_surfaces.rs +++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/window_and_frame_surfaces.rs @@ -974,6 +974,23 @@ fn child_webassembly_constructors_use_newtarget_child_realm_default_prototype() bytes, new ProxyOther(bindOther.call(freshNewTarget(NaN)), {}) ), + boundProxyInterfaces: [ + ["Module", bytes], + ["Instance", new WebAssembly.Module(bytes)], + ["Memory", { initial: 0 }], + ["Table", { element: "anyfunc", initial: 0 }], + ["Global", { value: "i32" }], + ["CompileError"], ["LinkError"], ["RuntimeError"] + ].every(([name, argument]) => usesNewTargetDefaultPrototype( + name, argument, bindOther.call(new ProxyOther(freshNewTarget(false), {})) + )), + moduleMixedBoundProxy: usesNewTargetDefaultPrototype( + "Module", + bytes, + bindOther.call(new ProxyOther( + bindOther.call(new ProxyOther(freshNewTarget(null), {})), {} + )) + ), memoryProxy: usesNewTargetDefaultPrototype( "Memory", { initial: 0 }, @@ -994,7 +1011,7 @@ fn child_webassembly_constructors_use_newtarget_child_realm_default_prototype() assert_eq!( result, - r#"{"namespaceIsSeparate":true,"constructorFunctionPrototype":true,"moduleDirect":true,"moduleBound":true,"moduleProxy":true,"moduleProxyBound":true,"memoryProxy":true,"compileErrorDirect":true,"moduleTopRealm":true,"moduleIntrinsicSurvivesNamespaceReplacement":true}"# + r#"{"namespaceIsSeparate":true,"constructorFunctionPrototype":true,"moduleDirect":true,"moduleBound":true,"moduleProxy":true,"moduleProxyBound":true,"boundProxyInterfaces":true,"moduleMixedBoundProxy":true,"memoryProxy":true,"compileErrorDirect":true,"moduleTopRealm":true,"moduleIntrinsicSurvivesNamespaceReplacement":true}"# ); } #[test] diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/dom_parser.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/dom_parser.rs index 71a5b3d6d60..65cfae9802c 100644 --- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/dom_parser.rs +++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/dom_parser.rs @@ -189,6 +189,21 @@ fn dom_parser_non_object_new_target_prototype_uses_new_target_realm_default() { const ProxyTop = new child.Proxy(new Function(), {}); ProxyTop.prototype = 7; + const BoundProxyChild = Function.prototype.bind.call(ProxyChild); + BoundProxyChild.prototype = 7; + const BoundProxyTop = child.Function.prototype.bind.call(ProxyTop); + BoundProxyTop.prototype = 7; + let boundGetterCount = 0; + const GetterBoundProxyChild = new Proxy(BoundProxyChild, { + get(target, property, receiver) { + if (property === 'prototype') { + boundGetterCount += 1; + return 7; + } + return Reflect.get(target, property, receiver); + } + }); + let getterCount = 0; const GetterProxyChild = new Proxy(new child.Function(), { get(target, property, receiver) { @@ -249,7 +264,23 @@ fn dom_parser_non_object_new_target_prototype_uses_new_target_realm_default() { child.DOMParser.prototype, topUrl ), - getterCount + boundProxyChild: check( + Reflect.construct(DOMParser, [], BoundProxyChild), + child.DOMParser.prototype, + topUrl + ), + boundProxyTop: check( + Reflect.construct(child.DOMParser, [], BoundProxyTop), + DOMParser.prototype, + childUrl + ), + getterBoundProxyChild: check( + Reflect.construct(DOMParser, [], GetterBoundProxyChild), + child.DOMParser.prototype, + topUrl + ), + getterCount, + boundGetterCount }); })() "#, @@ -258,7 +289,7 @@ fn dom_parser_non_object_new_target_prototype_uses_new_target_realm_default() { assert_eq!( result, - r#"{"directTop":[true,true],"directChild":[true,true],"boundChild":[true,true],"boundTop":[true,true],"proxyChild":[true,true],"proxyTop":[true,true],"getterProxyChild":[true,true],"getterCount":1}"# + r#"{"directTop":[true,true],"directChild":[true,true],"boundChild":[true,true],"boundTop":[true,true],"proxyChild":[true,true],"proxyTop":[true,true],"getterProxyChild":[true,true],"boundProxyChild":[true,true],"boundProxyTop":[true,true],"getterBoundProxyChild":[true,true],"getterCount":1,"boundGetterCount":1}"# ); } diff --git a/moli-renderer-v8/src/util.rs b/moli-renderer-v8/src/util.rs index 6d02b5af930..b57fa5e7f13 100644 --- a/moli-renderer-v8/src/util.rs +++ b/moli-renderer-v8/src/util.rs @@ -1069,6 +1069,81 @@ mod tests { options.into() } + #[test] + fn callable_realm_follows_mixed_bound_and_proxy_targets_without_traps() { + ensure_v8(); + let mut isolate = v8::Isolate::new(v8::CreateParams::default()); + let scope = pin!(v8::HandleScope::new(&mut isolate)); + let scope = &mut scope.init(); + let target_context = v8::Context::new(scope, Default::default()); + let target = { + let scope = &mut v8::ContextScope::new(scope, target_context); + let code = super::v8str(scope, "(function Target() { throw 'called'; })"); + v8::Script::compile(scope, code, None) + .unwrap() + .run(scope) + .unwrap() + }; + let wrapper_context = v8::Context::new(scope, Default::default()); + let scope = &mut v8::ContextScope::new(scope, wrapper_context); + let global = wrapper_context.global(scope); + global + .set(scope, super::v8str(scope, "target").into(), target) + .unwrap(); + let code = super::v8str( + scope, + r#" +(() => { + let armed = false; + const handler = { + get(target, key, receiver) { + if (armed) throw new Error('unexpected property read'); + return Reflect.get(target, key, receiver); + }, + getPrototypeOf(target) { + if (armed) throw new Error('unexpected prototype read'); + return Reflect.getPrototypeOf(target); + } + }; + const proxy = new Proxy(target, handler); + const bound = Function.prototype.bind.call(proxy); + const mixed = Function.prototype.bind.call(new Proxy(bound, handler)); + Object.setPrototypeOf(bound, null); + const revocable = Proxy.revocable(target, {}); + const revokedBound = Function.prototype.bind.call(revocable.proxy); + revocable.revoke(); + armed = true; + return [target, proxy, bound, mixed, new Proxy(mixed, handler), revokedBound]; +})() +"#, + ); + let values = v8::Script::compile(scope, code, None) + .unwrap() + .run(scope) + .unwrap(); + let values = v8::Local::::try_from(values).unwrap(); + for index in 0..5 { + let value = values.get_index(scope, index).unwrap(); + assert_eq!( + super::callable_relevant_context(scope, value), + Some(target_context) + ); + } + let revoked = values.get_index(scope, 5).unwrap(); + let scope = pin!(v8::TryCatch::new(scope)); + let scope = &mut scope.init(); + assert!(super::callable_relevant_context(scope, revoked).is_none()); + let exception = scope.exception().unwrap(); + let exception = v8::Local::::try_from(exception).unwrap(); + let expected = super::global_constructor_prototype(scope, "TypeError").unwrap(); + assert!( + exception + .get_prototype(scope) + .unwrap() + .strict_equals(expected.into()) + ); + } + #[test] fn utf16_string_helpers_use_code_unit_offsets() { let value = "a\u{1f306}b"; diff --git a/moli-v8-util/src/constructors.rs b/moli-v8-util/src/constructors.rs index fd92852f157..232d8887be9 100644 --- a/moli-v8-util/src/constructors.rs +++ b/moli-v8-util/src/constructors.rs @@ -246,16 +246,29 @@ pub fn callable_relevant_context<'s>( scope: &mut v8::PinScope<'s, '_>, mut callable: v8::Local<'s, v8::Value>, ) -> Option> { - // V8's GetFunctionRealm unwraps Proxy chains. Bound functions are created - // in their target function's context, so their creation context already - // identifies the target realm through the public embedding API. - while callable.is_proxy() { - let proxy = v8::Local::::try_from(callable).ok()?; - callable = proxy.get_target(scope); + // GetFunctionRealm follows both bound targets and Proxy targets. A bound + // Proxy's creation context need not be the underlying function's realm. + // Read internal targets without invoking author-defined property traps. + loop { + if let Ok(proxy) = v8::Local::::try_from(callable) { + if proxy.is_revoked() { + throw_type_error(scope, "Cannot determine the realm of a revoked Proxy"); + return None; + } + callable = proxy.get_target(scope); + continue; + } + if let Ok(function) = v8::Local::::try_from(callable) { + let target = function.get_bound_function(scope); + if !target.is_undefined() { + callable = target; + continue; + } + } + return v8::Local::::try_from(callable) + .ok()? + .get_creation_context(scope); } - v8::Local::::try_from(callable) - .ok()? - .get_creation_context(scope) } pub fn new_target_realm_constructor_prototype<'s>( diff --git a/vendor/v8-152.2.0/MOLI_VENDORING.md b/vendor/v8-152.2.0/MOLI_VENDORING.md index 8aa995e16e3..31b3f62bafd 100644 --- a/vendor/v8-152.2.0/MOLI_VENDORING.md +++ b/vendor/v8-152.2.0/MOLI_VENDORING.md @@ -27,6 +27,8 @@ Moli extensions: and Web IDL bindings. - `src/context_ext.cc` exposes global detachment and backup-incumbent-context support used by Moli's browsing-context lifecycle. +- `src/function_ext.cc` exposes bound-function targets so Moli can resolve the + function realm through mixed bound-function and Proxy chains. - `src/inspector_context_ext.cc` and `src/inspector_session_ext.cc` expose the inspector context/session operations required by Moli's CDP implementation. - `src/module_ext.cc` exposes the synthetic-module export hook used to write diff --git a/vendor/v8-152.2.0/src/function.rs b/vendor/v8-152.2.0/src/function.rs index 2a3ebfe6974..bb78a9a4364 100644 --- a/vendor/v8-152.2.0/src/function.rs +++ b/vendor/v8-152.2.0/src/function.rs @@ -56,6 +56,7 @@ unsafe extern "C" { argv: *const *const Value, ) -> *const Object; fn v8__Function__GetName(this: *const Function) -> *const Value; + fn v8__Function__GetBoundFunction(this: *const Function) -> *const Value; fn v8__Function__SetName(this: *const Function, name: *const String); fn v8__Function__GetScriptColumnNumber(this: *const Function) -> int; fn v8__Function__GetScriptLineNumber(this: *const Function) -> int; @@ -1097,6 +1098,15 @@ impl Function { unsafe { scope.cast_local(|_| v8__Function__GetName(self)).unwrap() } } + /// Returns the target of a bound function, or undefined for other functions. + #[inline(always)] + pub fn get_bound_function<'s>( + &self, + scope: &PinScope<'s, '_>, + ) -> Local<'s, Value> { + unsafe { scope.cast_local(|_| v8__Function__GetBoundFunction(self)).unwrap() } + } + #[inline(always)] pub fn set_name(&self, name: Local) { unsafe { v8__Function__SetName(self, &*name) } diff --git a/vendor/v8-152.2.0/src/function_ext.cc b/vendor/v8-152.2.0/src/function_ext.cc new file mode 100644 index 00000000000..353260c4f3f --- /dev/null +++ b/vendor/v8-152.2.0/src/function_ext.cc @@ -0,0 +1,14 @@ +// Copyright 2026 the Moli authors. MIT license. + +#include "support.h" +#include "v8-function.h" + +using namespace support; + +extern "C" { + +const v8::Value* v8__Function__GetBoundFunction(const v8::Function& self) { + return local_to_ptr(ptr_to_local(&self)->GetBoundFunction()); +} + +} // extern "C"