From aaa7f83176ebd5ffd99aae4a93ca42c160fbf0ec Mon Sep 17 00:00:00 2001 From: Sam <16504129+sagudev@users.noreply.github.com> Date: Mon, 29 Sep 2025 10:57:23 +0200 Subject: [PATCH] crown: Check closure bodies for unrooted types and fix root TransmitBodyPromise* (#39534) We should also check closure bodies for unrooted expressions. Testing: New crown tests Fixes: #37331 Fixes (partial): #37330 --------- Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --- components/script/body.rs | 15 ++++++++++----- support/crown/src/unrooted_must_root.rs | 4 ++-- .../compile-fail/unrooted_must_root_body.rs | 14 ++++++++++++++ .../unrooted_must_root_closure_body.rs | 16 ++++++++++++++++ 4 files changed, 42 insertions(+), 7 deletions(-) create mode 100644 support/crown/tests/compile-fail/unrooted_must_root_body.rs create mode 100644 support/crown/tests/compile-fail/unrooted_must_root_closure_body.rs diff --git a/components/script/body.rs b/components/script/body.rs index 8313c7b8f46..7a763071d87 100644 --- a/components/script/body.rs +++ b/components/script/body.rs @@ -234,6 +234,7 @@ impl TransmitBodyConnectHandler { task!(setup_native_body_promise_handler: move || { let rooted_stream = stream.root(); let global = rooted_stream.global(); + let cx = GlobalScope::get_cx(); // Step 4, the result of reading a chunk from body’s stream with reader. let promise = rooted_stream.read_a_chunk(CanGc::note()); @@ -241,20 +242,20 @@ impl TransmitBodyConnectHandler { // Step 5, the parallel steps waiting for and handling the result of the read promise, // are a combination of the promise native handler here, // and the corresponding IPC route in `component::net::http_loader`. - let promise_handler = Box::new(TransmitBodyPromiseHandler { + rooted!(in(*cx) let mut promise_handler = Some(TransmitBodyPromiseHandler { bytes_sender: bytes_sender.clone(), stream: Dom::from_ref(&rooted_stream.clone()), control_sender: control_sender.clone(), - }); + })); - let rejection_handler = Box::new(TransmitBodyPromiseRejectionHandler { + rooted!(in(*cx) let mut rejection_handler = Some(TransmitBodyPromiseRejectionHandler { bytes_sender, stream: Dom::from_ref(&rooted_stream.clone()), control_sender, - }); + })); let handler = - PromiseNativeHandler::new(&global, Some(promise_handler), Some(rejection_handler), CanGc::note()); + PromiseNativeHandler::new(&global, promise_handler.take().map(|h| Box::new(h) as Box<_>), rejection_handler.take().map(|h| Box::new(h) as Box<_>), CanGc::note()); let realm = enter_realm(&*global); let comp = InRealm::Entered(&realm); @@ -278,6 +279,8 @@ struct TransmitBodyPromiseHandler { control_sender: IpcSender, } +impl js::gc::Rootable for TransmitBodyPromiseHandler {} + impl Callback for TransmitBodyPromiseHandler { /// Step 5 of fn callback(&self, cx: JSContext, v: HandleValue, _realm: InRealm, can_gc: CanGc) { @@ -332,6 +335,8 @@ struct TransmitBodyPromiseRejectionHandler { control_sender: IpcSender, } +impl js::gc::Rootable for TransmitBodyPromiseRejectionHandler {} + impl Callback for TransmitBodyPromiseRejectionHandler { /// fn callback(&self, _cx: JSContext, _v: HandleValue, _realm: InRealm, can_gc: CanGc) { diff --git a/support/crown/src/unrooted_must_root.rs b/support/crown/src/unrooted_must_root.rs index 94f07ef4f93..7b4b1a5b2b0 100644 --- a/support/crown/src/unrooted_must_root.rs +++ b/support/crown/src/unrooted_must_root.rs @@ -399,10 +399,10 @@ impl<'tcx> LateLintPass<'tcx> for UnrootedPass { n.as_str() == "default" || n.as_str() == "Wrap" }, - visit::FnKind::Closure => return, + visit::FnKind::Closure => false, }; - if !in_derive_expn(span) { + if !in_derive_expn(span) && !matches!(kind, visit::FnKind::Closure) { let sig = cx.tcx.type_of(def_id).skip_binder().fn_sig(cx.tcx); for (arg, ty) in decl.inputs.iter().zip(sig.inputs().skip_binder().iter()) { diff --git a/support/crown/tests/compile-fail/unrooted_must_root_body.rs b/support/crown/tests/compile-fail/unrooted_must_root_body.rs new file mode 100644 index 00000000000..5713eef4053 --- /dev/null +++ b/support/crown/tests/compile-fail/unrooted_must_root_body.rs @@ -0,0 +1,14 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +//@rustc-env:RUSTC_BOOTSTRAP=1 + +#[crown::unrooted_must_root_lint::must_root] +struct Foo(i32); + +fn foo2() { + let foo = Foo(0); + //~^ ERROR: Expression of type Foo must be rooted +} + +fn main() {} diff --git a/support/crown/tests/compile-fail/unrooted_must_root_closure_body.rs b/support/crown/tests/compile-fail/unrooted_must_root_closure_body.rs new file mode 100644 index 00000000000..78c5a71435d --- /dev/null +++ b/support/crown/tests/compile-fail/unrooted_must_root_closure_body.rs @@ -0,0 +1,16 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +//@rustc-env:RUSTC_BOOTSTRAP=1 + +#[crown::unrooted_must_root_lint::must_root] +struct Foo(i32); + +fn foo2() { + || { + let foo = Foo(0); + //~^ ERROR: Expression of type Foo must be rooted + }; +} + +fn main() {}