Truly independent web browser
Meta: Reject untraced types that keep GC pointers in heap memory
Shannon Boothb49c2786fd27
diff --git a/Meta/RustLints/README.md b/Meta/RustLints/README.mdindex 613f668e81b..62f6c26deda 100644--- a/Meta/RustLints/README.md+++ b/Meta/RustLints/README.md@@ -14,6 +14,9 @@ plugin:- `gc_pointer_in_drop`: the `Drop` of a type that the crate's cells own, by value or through heap memory, dereferences a`Gc`. Cells are dropped while the collector sweeps, when the cells they point to may be gone already. What needs othercells belongs in `Finalize`, which runs while every cell that dies is intact.+- `gc_container_in_untraced_type`: a type that does not implement `Trace` keeps a GC pointer in a field through heap+ memory, an `Rc` or a `Box` included. Nothing traces its values wherever they are held: implement `Trace` for it, so+ that its owner traces it, or tie it to a deferral.A GC pointer is a reference to a `GcCell`, a value of a type of the crate that implements `Trace` by hand, as `Gc`,`Value` and `PropertyKey` do, or a trait object whose trait requires `Trace`. A type that hides cell pointers in itsdiff --git a/Meta/RustLints/src/gc_lints.rs b/Meta/RustLints/src/gc_lints.rsindex 462d704eac9..d858da8c36e 100644--- a/Meta/RustLints/src/gc_lints.rs+++ b/Meta/RustLints/src/gc_lints.rs@@ -61,11 +61,21 @@ declare_lint! {"GC pointer dereferenced while a cell is dropped"}+declare_lint! {+ /// A type that does not implement `Trace` keeps GC pointers in a field, in heap memory the collector does not+ /// scan. Nothing traces a value of it, wherever it is held. Implement `Trace` for it, so that whatever holds it+ /// traces it, or give it a `&DeferGc` field, which ties its values to a deferral.+ pub GC_CONTAINER_IN_UNTRACED_TYPE,+ Deny,+ "GC pointers kept in heap memory by a type that is not traced"+}+pub static LINTS: &[&Lint] = &[GC_CONTAINER_ON_STACK,ROOT_IN_TRACED_TYPE,UNTRACED_GC_POINTER,GC_POINTER_IN_DROP,+ GC_CONTAINER_IN_UNTRACED_TYPE,];#[derive(Clone, Copy, PartialEq, Eq)]@@ -119,6 +129,7 @@ impl_lint_pass!(GcLints<'_> => [ROOT_IN_TRACED_TYPE,UNTRACED_GC_POINTER,GC_POINTER_IN_DROP,+ GC_CONTAINER_IN_UNTRACED_TYPE,]);const LIBJS_CRATE: &str = "libjs_rust";@@ -704,6 +715,30 @@ impl<'tcx> LateLintPass<'tcx> for GcLints<'tcx> {}let def_id = item.owner_id.to_def_id();if !self.traced_types.contains(&def_id) {+ // NB: The GC's own types, like Root, are what the markers say they are, whatever their fields hold.+ if self.kind(cx.tcx, def_id) != Kind::Other || self.is_deferral_scoped(cx.tcx, def_id) {+ return;+ }+ for field in cx.tcx.adt_def(def_id).all_fields() {+ let ty = normalize(+ cx,+ cx.tcx.type_of(field.did).instantiate_identity().skip_normalization(),+ );+ if let Some(container) = self.unscanned_container(cx, ty) {+ let field_id = cx.tcx.local_def_id_to_hir_id(field.did.expect_local());+ self.emit(+ cx,+ GC_CONTAINER_IN_UNTRACED_TYPE,+ field_id,+ cx.tcx.def_span(field.did),+ with_forced_trimmed_paths!(format!(+ "'{}' does not implement Trace but keeps GC pointers in '{container}', which the \+ collector does not scan",+ cx.tcx.def_path_str(def_id)+ )),+ );+ }+ }return;}let tcx = cx.tcx;diff --git a/Meta/RustLints/tests/ui/gc_container_in_untraced_type.rs b/Meta/RustLints/tests/ui/gc_container_in_untraced_type.rsnew file mode 100644index 00000000000..284d5a0ffde--- /dev/null+++ b/Meta/RustLints/tests/ui/gc_container_in_untraced_type.rs@@ -0,0 +1,58 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#![allow(unused, clippy::all)]++use std::rc::Rc;++use js::{Cell, DeferGc, Deferred, Gc, GcWeak, MarkedVec, Value, Visitor};++pub struct Untraced {+ cells: Vec<Gc<Cell>>, //~ ERROR 'Untraced' does not implement Trace but keeps GC pointers in 'Vec<Gc<Cell>>'+}++pub struct Shared {+ values: Rc<[Value]>, //~ ERROR 'Shared' does not implement Trace but keeps GC pointers in 'Rc<[Value]>'+}++pub enum Choice {+ Cells(Box<[Gc<Cell>]>), //~ ERROR 'Choice' does not implement Trace but keeps GC pointers in 'Box<[Gc<Cell>]>'+ Nothing,+}++pub struct Direct {+ cell: Gc<Cell>,+ weak: Vec<GcWeak<Cell>>,+}++pub struct Rooted<'vm> {+ cells: MarkedVec<'vm, Gc<Cell>>,+}++pub struct Traced {+ cells: Vec<Gc<Cell>>,+}++unsafe impl js::Trace for Traced {+ fn trace(&self, _: &mut Visitor) {}+}++pub struct TiedToADeferral<'d> {+ defer_gc: &'d DeferGc<'d>,+ cells: Vec<Gc<Cell>>,+}++pub struct HoldingADeferred<'d> {+ covered: Deferred<'d, Vec<Gc<Cell>>>,+ cells: Vec<Gc<Cell>>,+}++pub fn tied_locals(defer_gc: &DeferGc<'_>, vm: &js::Vm, cell: Gc<Cell>) {+ let tied = TiedToADeferral {+ defer_gc,+ cells: vec![cell],+ };+}diff --git a/Meta/RustLints/tests/ui/gc_container_on_stack.rs b/Meta/RustLints/tests/ui/gc_container_on_stack.rsindex 90ee0c65a4c..274f058a603 100644--- a/Meta/RustLints/tests/ui/gc_container_on_stack.rs+++ b/Meta/RustLints/tests/ui/gc_container_on_stack.rs@@ -7,6 +7,7 @@// Mirrors Tests/ClangPlugins/LibJSGCTests/gc_pointer_container_on_stack.cpp.#![allow(unused, clippy::all)]+#![allow(gc_container_in_untraced_type, reason = "the fixtures are only held where the test says")]use std::cell::RefCell;use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};