Truly independent web browser
Meta: Reject GC pointer dereferences in the Drop of what cells own
Shannon Boothfe12399edab6
diff --git a/Meta/RustLints/README.md b/Meta/RustLints/README.mdindex 8be01969b30..613f668e81b 100644--- a/Meta/RustLints/README.md+++ b/Meta/RustLints/README.md@@ -11,6 +11,9 @@ plugin:type that implements `Trace`.- `untraced_gc_pointer`: a field marked `#[gc(untraced)]`, which `#[derive(Trace)]` skips, whose type can hold a GCpointer. A field whose cells something else keeps alive says what with `#[gc(untraced = "...")]`.+- `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 other+ cells belongs in `Finalize`, which runs while every cell that dies is intact.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 8cd0f1ca5b2..462d704eac9 100644--- a/Meta/RustLints/src/gc_lints.rs+++ b/Meta/RustLints/src/gc_lints.rs@@ -15,10 +15,10 @@ use rustc_errors::DiagDecorator;use rustc_hir::def::DefKind;use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};use rustc_hir::intravisit::FnKind;-use rustc_hir::{Body, ByRef, Expr, ExprKind, FnDecl, FnRetTy, HirId, Item, ItemKind, Node, Pat, PatKind};+use rustc_hir::{Body, ByRef, Expr, ExprKind, FnDecl, FnRetTy, HirId, Item, ItemKind, Node, Pat, PatKind, UnOp};use rustc_lint::{LateContext, LateLintPass, Lint};use rustc_middle::lint::LintLevelSource;-use rustc_middle::ty::adjustment::{Adjust, PointerCoercion};+use rustc_middle::ty::adjustment::{Adjust, DerefAdjustKind, PointerCoercion};use rustc_middle::ty::print::with_forced_trimmed_paths;use rustc_middle::ty::{self, Ty, TyCtxt};use rustc_session::{declare_lint, impl_lint_pass};@@ -52,7 +52,21 @@ declare_lint! {"GC pointers in a field that `#[derive(Trace)]` skips"}-pub static LINTS: &[&Lint] = &[GC_CONTAINER_ON_STACK, ROOT_IN_TRACED_TYPE, UNTRACED_GC_POINTER];+declare_lint! {+ /// The `Drop` of a type that cells own dereferences a GC pointer. Cells are dropped while the collector sweeps, by+ /// when the cells they point to may be gone. Do what needs other cells in `Finalize`, which runs while every cell+ /// that dies is still intact.+ pub GC_POINTER_IN_DROP,+ Deny,+ "GC pointer dereferenced while a cell is dropped"+}++pub static LINTS: &[&Lint] = &[+ GC_CONTAINER_ON_STACK,+ ROOT_IN_TRACED_TYPE,+ UNTRACED_GC_POINTER,+ GC_POINTER_IN_DROP,+];#[derive(Clone, Copy, PartialEq, Eq)]enum Kind {@@ -89,6 +103,8 @@ pub struct GcLints<'tcx> {holds_no_cells_types: FxHashSet<DefId>,keeps_alive_types: FxHashSet<DefId>,defers_collections_types: FxHashSet<DefId>,+ /// The local types that cells of this crate own, which are dropped with them.+ types_in_cells: Option<FxHashSet<DefId>>,/// Whether each type has a field that defers collections, which its values cannot outlive.deferral_scoped: FxHashMap<DefId, bool>,reaches_gc_pointer: FxHashMap<Ty<'tcx>, bool>,@@ -102,6 +118,7 @@ impl_lint_pass!(GcLints<'_> => [GC_CONTAINER_ON_STACK,ROOT_IN_TRACED_TYPE,UNTRACED_GC_POINTER,+ GC_POINTER_IN_DROP,]);const LIBJS_CRATE: &str = "libjs_rust";@@ -382,6 +399,68 @@ impl<'tcx> GcLints<'tcx> {false}+ /// The local types that the cells of this crate own by value or through heap memory, which are dropped when the+ /// collector sweeps the cells.+ fn types_in_cells(&mut self, cx: &LateContext<'tcx>) -> &FxHashSet<DefId> {+ if self.types_in_cells.is_none() {+ let mut found = FxHashSet::default();+ let mut pending: Vec<Ty<'tcx>> = self+ .cell_types+ .iter()+ .filter(|def_id| def_id.is_local())+ .map(|&def_id| cx.tcx.type_of(def_id).instantiate_identity().skip_normalization())+ .collect();+ let mut seen = FxHashSet::default();+ while let Some(ty) = pending.pop() {+ if !seen.insert(ty) {+ continue;+ }+ if let ty::Adt(adt, _) = ty.kind()+ && adt.did().is_local()+ {+ found.insert(adt.did());+ }+ pending.extend(+ self.components(cx, ty, Target::UnscannedGcPointer)+ .into_iter()+ .map(|(component, _)| component),+ );+ }+ self.types_in_cells = Some(found);+ }+ self.types_in_cells.as_ref().expect("computed above")+ }++ /// Whether the expression dereferences a pointer to a cell, as calling a method of the cell through a `Gc` does.+ fn derefs_gc_pointer(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -> bool {+ let results = cx.typeck_results();+ let is_cell =+ |this: &Self, ty: Ty<'tcx>| matches!(ty.kind(), ty::Adt(adt, _) if this.cell_types.contains(&adt.did()));+ if let ExprKind::Unary(UnOp::Deref, _) = expr.kind+ && is_cell(self, results.expr_ty(expr))+ {+ return true;+ }+ results.expr_adjustments(expr).iter().any(|adjustment| {+ matches!(adjustment.kind, Adjust::Deref(DerefAdjustKind::Overloaded(_))) && is_cell(self, adjustment.target)+ })+ }++ /// The type whose `Drop::drop` the expression `hir_id` is in, if it is one that cells own.+ fn enclosing_drop_of_type_in_cells(&mut self, cx: &LateContext<'tcx>, hir_id: HirId) -> Option<DefId> {+ let tcx = cx.tcx;+ let owner = hir_id.owner.def_id;+ let impl_id = tcx.trait_impl_of_assoc(owner.to_def_id())?;+ if Some(tcx.impl_trait_id(impl_id)) != tcx.lang_items().drop_trait() {+ return None;+ }+ let ty::Adt(adt, _) = tcx.type_of(impl_id).instantiate_identity().skip_normalization().kind() else {+ return None;+ };+ let did = adt.did();+ self.types_in_cells(cx).contains(&did).then_some(did)+ }+fn emit(&mut self, cx: &LateContext<'_>, lint: &'static Lint, hir_id: HirId, span: Span, message: String) {let spec = cx.tcx.lint_level_spec_at_node(lint, hir_id);if (spec.is_allow() || spec.is_expect())@@ -586,6 +665,20 @@ impl<'tcx> LateLintPass<'tcx> for GcLints<'tcx> {if !self.enabled {return;}+ if self.derefs_gc_pointer(cx, expr)+ && let Some(dropped) = self.enclosing_drop_of_type_in_cells(cx, expr.hir_id)+ {+ self.emit(+ cx,+ GC_POINTER_IN_DROP,+ expr.hir_id,+ expr.span,+ format!(+ "the Drop of '{}', which cells own, dereferences a GC pointer, whose cell may be swept already",+ cx.tcx.def_path_str(dropped)+ ),+ );+ }// Boxing a closure into a `dyn Fn` hides its captures from the type of whatever holds it next, unless the trait// object still shows them by implementing Trace.let results = cx.typeck_results();diff --git a/Meta/RustLints/tests/ui/auxiliary/libjs_rust.rs b/Meta/RustLints/tests/ui/auxiliary/libjs_rust.rsindex 2919c994fdb..57cc4116653 100644--- a/Meta/RustLints/tests/ui/auxiliary/libjs_rust.rs+++ b/Meta/RustLints/tests/ui/auxiliary/libjs_rust.rs@@ -17,6 +17,15 @@ pub mod layout {}impl<T: ?Sized> Copy for Gc<T> {}++ impl<T: crate::gc::class::GcCell> core::ops::Deref for Gc<T> {+ type Target = T;++ fn deref(&self) -> &T {+ // SAFETY: Stand-in.+ unsafe { self.0.as_ref() }+ }+ }}pub mod value {@@ -152,3 +161,7 @@ unsafe impl Trace for Cell {}unsafe impl gc::class::GcCell for Cell {}++impl Cell {+ pub fn method(&self) {}+}diff --git a/Meta/RustLints/tests/ui/gc_pointer_in_drop.rs b/Meta/RustLints/tests/ui/gc_pointer_in_drop.rsnew file mode 100644index 00000000000..bdfa487d3a7--- /dev/null+++ b/Meta/RustLints/tests/ui/gc_pointer_in_drop.rs@@ -0,0 +1,47 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#![allow(unused, clippy::all)]++use js::{Cell, Gc, Visitor};++pub struct Owner {+ cell: Gc<Cell>,+ nested: Vec<Nested>,+}++unsafe impl js::Trace for Owner {+ fn trace(&self, _: &mut Visitor) {}+}++unsafe impl js::gc::class::GcCell for Owner {}++impl Drop for Owner {+ fn drop(&mut self) {+ let copy = self.cell;+ let cell: &Cell = &self.cell; //~ ERROR the Drop of 'Owner'+ }+}++pub struct Nested {+ cell: Gc<Cell>,+}++impl Drop for Nested {+ fn drop(&mut self) {+ self.cell.method(); //~ ERROR the Drop of 'Nested'+ }+}++pub struct NotInACell {+ cell: Gc<Cell>,+}++impl Drop for NotInACell {+ fn drop(&mut self) {+ self.cell.method();+ }+}