Skip to content

Truly independent web browser

ClangPlugins: Reject unvisited GC members of polymorphic subclasses

Tim Ledbetter9b9cbb17781c
diff --git a/Meta/ClangPlugins/LibJSGCPluginAction.cpp b/Meta/ClangPlugins/LibJSGCPluginAction.cpp
index fa2310cf7af..3c79524bd30 100644
--- a/Meta/ClangPlugins/LibJSGCPluginAction.cpp
+++ b/Meta/ClangPlugins/LibJSGCPluginAction.cpp
@@ -447,6 +447,8 @@ bool LibJSGCVisitor::VisitCXXRecordDecl(clang::CXXRecordDecl* record)
// when used as a member of a Cell (checked below for Cell types).
// However, if they DO have visit_edges, verify it visits all GC members.
if (!record_is_cell) {
+ validate_gc_members_of_polymorphic_record(*record);
+
// Check if this non-Cell type has a visit_edges method
clang::DeclarationName name = &m_context.Idents.get("visit_edges");
auto const* visit_edges_method = record->lookup(name).find_first<clang::CXXMethodDecl>();
@@ -1069,8 +1071,11 @@ bool LibJSGCVisitor::VisitClassTemplateDecl(clang::ClassTemplateDecl* class_temp
return true;
for (auto const* specialization : class_template->specializations()) {
- if (specialization->getSpecializationKind() == clang::TSK_ImplicitInstantiation && specialization->isCompleteDefinition())
- validate_engine_subclass_is_in_libjs(*specialization);
+ if (specialization->getSpecializationKind() != clang::TSK_ImplicitInstantiation || !specialization->isCompleteDefinition())
+ continue;
+ validate_engine_subclass_is_in_libjs(*specialization);
+ if (!record_inherits_from_cell(*specialization))
+ validate_gc_members_of_polymorphic_record(*specialization);
}
return true;
}
@@ -1197,6 +1202,29 @@ static bool type_holds_gc_pointer(clang::QualType type, std::set<clang::CXXRecor
return false;
}
+void LibJSGCVisitor::validate_gc_members_of_polymorphic_record(clang::CXXRecordDecl const& record)
+{
+ if (record.isDependentContext() || !record.isPolymorphic() || type_has_visit_edges_method(&record))
+ return;
+
+ bool has_polymorphic_base = false;
+ record.forallBases([&](clang::CXXRecordDecl const* base) {
+ has_polymorphic_base |= base->isPolymorphic();
+ return !has_polymorphic_base;
+ });
+ if (!has_polymorphic_base)
+ return;
+
+ auto& diag_engine = m_context.getDiagnostics();
+ auto diag_id = diag_engine.getCustomDiagID(clang::DiagnosticsEngine::Error, "Member %0 of polymorphic class %1 contains GC pointers but %1 has no visit_edges method");
+ auto record_name = name_for_diagnostic(m_context, record);
+ for (auto const* field : record.fields()) {
+ std::set<clang::CXXRecordDecl const*> visited;
+ if (type_holds_gc_pointer(field->getType(), visited))
+ diag_engine.Report(field->getLocation(), diag_id) << field->getName() << record_name;
+ }
+}
+
static bool is_container_with_heap_storage(std::string const& template_name)
{
static std::unordered_set<std::string> const template_names {
diff --git a/Meta/ClangPlugins/LibJSGCPluginAction.h b/Meta/ClangPlugins/LibJSGCPluginAction.h
index 3cf361d265e..9116f24f9cf 100644
--- a/Meta/ClangPlugins/LibJSGCPluginAction.h
+++ b/Meta/ClangPlugins/LibJSGCPluginAction.h
@@ -77,6 +77,7 @@ private:
void validate_record_macros(clang::CXXRecordDecl const&);
void validate_engine_subclass_is_in_libjs(clang::CXXRecordDecl const&);
+ void validate_gc_members_of_polymorphic_record(clang::CXXRecordDecl const&);
void validate_object_on_stack(clang::VarDecl const&, clang::SourceLocation);
void validate_type_on_stack(clang::QualType, clang::SourceLocation);
CellMacroExpectation get_record_cell_macro_expectation(clang::CXXRecordDecl const&);
diff --git a/Tests/ClangPlugins/LibJSGCTests/polymorphic_subclass_gc_members.cpp b/Tests/ClangPlugins/LibJSGCTests/polymorphic_subclass_gc_members.cpp
new file mode 100644
index 00000000000..9e4ce3ad477
--- /dev/null
+++ b/Tests/ClangPlugins/LibJSGCTests/polymorphic_subclass_gc_members.cpp
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2026, Tim Ledbetter <[email protected]>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+// RUN: %clang++ -Xclang -verify %plugin_opts% -c %s -o %t 2>&1
+
+#include <AK/Vector.h>
+#include <LibGC/Cell.h>
+#include <LibGC/Ptr.h>
+#include <LibGC/Root.h>
+#include <LibGC/Weak.h>
+
+struct Params {
+ virtual ~Params() = default;
+};
+
+struct ParamsWithCell : Params {
+ // expected-error@+1 {{Member cell of polymorphic class ParamsWithCell contains GC pointers but ParamsWithCell has no visit_edges method}}
+ GC::Ref<GC::Cell> cell;
+};
+
+struct ParamsWithCells : Params {
+ // expected-error@+1 {{Member cells of polymorphic class ParamsWithCells contains GC pointers but ParamsWithCells has no visit_edges method}}
+ Vector<GC::Ptr<GC::Cell>> cells;
+};
+
+struct DerivedParamsWithCell : ParamsWithCells {
+ // expected-error@+1 {{Member other_cell of polymorphic class DerivedParamsWithCell contains GC pointers but DerivedParamsWithCell has no visit_edges method}}
+ GC::Ptr<GC::Cell> other_cell;
+};
+
+template<typename T>
+struct TemplateParams : Params {
+ // expected-error@+1 {{Member cell of polymorphic class TemplateParams<GC::Ref<GC::Cell>> contains GC pointers but TemplateParams<GC::Ref<GC::Cell>> has no visit_edges method}}
+ T cell;
+};
+
+static_assert(sizeof(TemplateParams<GC::Ref<GC::Cell>>) > 0);
+static_assert(sizeof(TemplateParams<int>) > 0);
+
+struct VisitedParams {
+ virtual ~VisitedParams() = default;
+ virtual void visit_edges(GC::Cell::Visitor&) { }
+};
+
+struct VisitedParamsWithUnvisitedCell : VisitedParams {
+ // expected-error@+1 {{Member cell of polymorphic class VisitedParamsWithUnvisitedCell contains GC pointers but VisitedParamsWithUnvisitedCell has no visit_edges method}}
+ GC::Ref<GC::Cell> cell;
+};
+
+struct VisitedParamsWithCell : VisitedParams {
+ virtual void visit_edges(GC::Cell::Visitor& visitor) override
+ {
+ VisitedParams::visit_edges(visitor);
+ visitor.visit(cell);
+ }
+
+ GC::Ref<GC::Cell> cell;
+};
+
+struct ParamsWithRootedOrUntracedCells : Params {
+ GC::Root<GC::Cell> rooted_cell;
+ GC::RawPtr<GC::Cell> untraced_cell;
+ GC::Weak<GC::Cell> weak_cell;
+};
+
+struct PolymorphicHolder {
+ virtual ~PolymorphicHolder() = default;
+ GC::Ref<GC::Cell> cell;
+};
+
+struct Holder {
+ GC::Ref<GC::Cell> cell;
+};
+
+struct DerivedHolder : Holder {
+ GC::Ref<GC::Cell> other_cell;
+};