Truly independent web browser
Meta: Close gaps in the check of claims of no cell edges
Andreas Klingc4890c693fc0
diff --git a/Meta/ClangPlugins/LibJSGCPluginAction.cpp b/Meta/ClangPlugins/LibJSGCPluginAction.cppindex 4ca68891d2c..c0ea9f4e10f 100644--- a/Meta/ClangPlugins/LibJSGCPluginAction.cpp+++ b/Meta/ClangPlugins/LibJSGCPluginAction.cpp@@ -379,7 +379,7 @@ bool LibJSGCVisitor::VisitCXXRecordDecl(clang::CXXRecordDecl* record)validate_engine_subclass_is_in_libjs(*record);remember_polymorphic_subclass(*record);- validate_claim_of_no_cell_edges(*record);+ remember_claim_of_no_cell_edges(*record);auto& diag_engine = m_context.getDiagnostics();std::vector<clang::FieldDecl const*> fields_that_need_visiting;@@ -1219,39 +1219,102 @@ static bool record_declares_no_cell_edges(clang::CXXRecordDecl const& record)};if (declares(&record))return true;- return !record.forallBases([&](clang::CXXRecordDecl const* base) { return !declares(base); });+ // NB: forallBases() gives up on a dependent base, so the bases are walked here, skipping those not known yet.+ for (auto const& base : record.bases()) {+ auto const* base_record = base.getType()->getAsCXXRecordDecl();+ base_record = base_record ? base_record->getDefinition() : nullptr;+ if (base_record && record_declares_no_cell_edges(*base_record))+ return true;+ }+ return false;+}++enum class ClaimViolation {+ GCPointer,+ OpaqueFunction,+};++static bool is_owning_pointer(std::string const& template_name)+{+ return template_name == "AK::NonnullOwnPtr" || template_name == "AK::NonnullRefPtr" || template_name == "AK::OwnPtr" || template_name == "AK::RefPtr";}-// Whether a value of this type holds an AK::Function, or a struct that holds one, whose captures no check can see.-static bool type_holds_opaque_function(clang::QualType type, std::set<clang::CXXRecordDecl const*>& visited)+// Whether a value of this type holds what a type declared to hold no cell edges must not: a GC pointer, which nothing+// would trace, or an AK::Function, whose captures no check can see. Roots and weak pointers need no tracing. A GC pointer+// is looked for in what the value owns too, unless the pointee declares the claim itself and is checked on its own.+// NB: An owned object's callbacks are its own API, such as those of libraries below LibGC, which can't keep a+// GC::Function. As with every AK::Function, what they capture must be rooted or weak.+static bool record_violates_claim_of_no_cell_edges(clang::CXXRecordDecl const&, ClaimViolation, std::set<clang::CXXRecordDecl const*>& visited, PolymorphicSubclasses const&);++static bool type_violates_claim_of_no_cell_edges(clang::QualType type, ClaimViolation violation, std::set<clang::CXXRecordDecl const*>& visited, PolymorphicSubclasses const& polymorphic_subclasses){if (auto const* array = type->getAsArrayTypeUnsafe())- return type_holds_opaque_function(array->getElementType(), visited);- if (type->isPointerType() || type->isReferenceType())- return false;+ return type_violates_claim_of_no_cell_edges(array->getElementType(), violation, visited, polymorphic_subclasses);++ // NB: A raw pointer to a cell, or to something with cell edges such as the mixin of a cell, is an edge that nothing+ // traces. Whether a pointee that is only declared is one can't be seen.+ if (type->isPointerType() || type->isReferenceType()) {+ if (violation != ClaimViolation::GCPointer)+ return false;+ auto const* pointee = type->getPointeeCXXRecordDecl();+ auto const* definition = pointee ? pointee->getDefinition() : nullptr;+ return definition && (record_inherits_from_cell(*definition) || (!record_is_in_ak_or_gc(*definition) && type_has_visit_edges_method(definition)));+ }if (auto instance = template_instance_of_type(type)) {if (instance->name == "AK::Function")- return true;+ return violation == ClaimViolation::OpaqueFunction;+ if (instance->name == "GC::Ptr" || instance->name == "GC::Ref" || instance->name == "GC::RawPtr" || instance->name == "GC::RawRef" || instance->name == "GC::Member" || instance->name == "GC::NonnullMember")+ return violation == ClaimViolation::GCPointer;+ if (is_owning_pointer(instance->name)) {+ if (violation != ClaimViolation::GCPointer || instance->type_arguments.empty())+ return false;+ auto const* pointee = instance->type_arguments.front()->getAsCXXRecordDecl();+ pointee = pointee ? pointee->getDefinition() : nullptr;+ if (pointee && record_declares_no_cell_edges(*pointee))+ return false;+ if (type_violates_claim_of_no_cell_edges(instance->type_arguments.front(), violation, visited, polymorphic_subclasses))+ return true;+ // NB: The pointer may own a subclass of its pointee.+ if (!pointee)+ return false;+ auto subclasses = polymorphic_subclasses.find(pointee->getCanonicalDecl());+ if (subclasses == polymorphic_subclasses.end())+ return false;+ for (auto const* subclass : subclasses->second) {+ if (record_declares_no_cell_edges(*subclass))+ continue;+ if (record_violates_claim_of_no_cell_edges(*subclass, violation, visited, polymorphic_subclasses))+ return true;+ }+ return false;+ }if (is_type_that_does_not_store_gc_pointers_by_value(instance->name))return false;for (auto const& type_argument : instance->type_arguments) {- if (type_holds_opaque_function(type_argument, visited))+ if (type_violates_claim_of_no_cell_edges(type_argument, violation, visited, polymorphic_subclasses))return true;}}auto const* record = type->getAsCXXRecordDecl();- if (!record || !record->isCompleteDefinition() || !visited.insert(record).second)+ return record && record_violates_claim_of_no_cell_edges(*record, violation, visited, polymorphic_subclasses);+}++static bool record_violates_claim_of_no_cell_edges(clang::CXXRecordDecl const& record, ClaimViolation violation, std::set<clang::CXXRecordDecl const*>& visited, PolymorphicSubclasses const& polymorphic_subclasses)+{+ if (!record.isCompleteDefinition() || !visited.insert(&record).second)return false;- if (record_is_in_ak_or_gc(*record) || record_inherits_from_cell(*record))+ if (record_is_nan_boxed_value(record))+ return violation == ClaimViolation::GCPointer;+ if (record_is_in_ak_or_gc(record) || record_inherits_from_cell(record))return false;- for (auto const& base : record->bases()) {- if (type_holds_opaque_function(base.getType(), visited))+ for (auto const& base : record.bases()) {+ if (type_violates_claim_of_no_cell_edges(base.getType(), violation, visited, polymorphic_subclasses))return true;}- for (auto const* field : record->fields()) {- if (type_holds_opaque_function(field->getType(), visited))+ for (auto const* field : record.fields()) {+ if (type_violates_claim_of_no_cell_edges(field->getType(), violation, visited, polymorphic_subclasses))return true;}return false;@@ -1260,10 +1323,13 @@ static bool type_holds_opaque_function(clang::QualType type, std::set<clang::CXX// A GC::Function captures a type that declares that it holds no cell edges without tracing it, so the type must not hold// a GC pointer, which nothing would keep alive. Roots and weak pointers are fine, as they need no tracing. Nor may it hold// an AK::Function, whose captures could hold anything.-void LibJSGCVisitor::validate_claim_of_no_cell_edges(clang::CXXRecordDecl const& record)+// NB: Claims are checked once the translation unit is complete, when every instantiation of a class template that makes+// the claim, and every subclass of what a claimed type owns, is known.+void LibJSGCVisitor::remember_claim_of_no_cell_edges(clang::CXXRecordDecl const& record){clang::CXXRecordDecl const* claimed_record = nullptr;bool callbacks_hold_roots = false;+ bool claim_covers_subclasses = false;if (auto const* specialization = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(&record)) {if (specialization->getSpecializationKind() != clang::TSK_ExplicitSpecialization || specialization->getSpecializedTemplate()->getQualifiedNameAsString() != "GC::IsUntracedCapture")return;@@ -1273,41 +1339,89 @@ void LibJSGCVisitor::validate_claim_of_no_cell_edges(clang::CXXRecordDecl const&if (auto const* variable = llvm::dyn_cast<clang::VarDecl>(declaration); variable && variable->getName() == "callbacks_hold_roots")callbacks_hold_roots = true;}+ // NB: A partial specialization declares the claim for whatever its arguments hold, which is checked where they+ // make claims of their own.auto const& arguments = specialization->getTemplateArgs();- if (arguments.size() != 1 || arguments[0].getKind() != clang::TemplateArgument::Type)+ if (llvm::isa<clang::ClassTemplatePartialSpecializationDecl>(specialization) || arguments.size() != 1 || arguments[0].getKind() != clang::TemplateArgument::Type || arguments[0].getAsType()->isDependentType())return;claimed_record = arguments[0].getAsType()->getAsCXXRecordDecl();- } else if (!record.isDependentContext() && record_declares_no_cell_edges(record)) {+ // NB: Unlike a claim made in the class, which its subclasses inherit and make themselves, this one covers a+ // counted or owned pointer to the class, which may point to a subclass.+ claim_covers_subclasses = true;+ } else if (record_declares_no_cell_edges(record)) {claimed_record = &record;}- if (!claimed_record)- return;- claimed_record = claimed_record->getDefinition();- if (!claimed_record || claimed_record->isDependentContext())- return;+ if (claimed_record)+ m_claims_of_no_cell_edges.push_back({ claimed_record, callbacks_hold_roots, claim_covers_subclasses });+}++void LibJSGCVisitor::validate_claims_of_no_cell_edges()+{+ std::set<clang::CXXRecordDecl const*> validated;+ auto validate = [&](clang::CXXRecordDecl const* record, bool callbacks_hold_roots, clang::CXXRecordDecl const* claiming_base = nullptr) {+ if (record && record->isCompleteDefinition() && !record->isDependentContext() && validated.insert(record).second)+ validate_claim_of_no_cell_edges(*record, callbacks_hold_roots, claiming_base);+ };+ for (auto const& claim : m_claims_of_no_cell_edges) {+ if (auto const* class_template = claim.record->getDescribedClassTemplate()) {+ for (auto const* specialization : class_template->specializations())+ validate(specialization, claim.callbacks_hold_roots);+ continue;+ }+ auto const* definition = claim.record->getDefinition();+ validate(definition, claim.callbacks_hold_roots);+ if (!claim.covers_subclasses || !definition)+ continue;+ // NB: A subclass that makes the claim itself is checked on its own.+ auto subclasses = m_polymorphic_subclasses.find(definition->getCanonicalDecl());+ if (subclasses == m_polymorphic_subclasses.end())+ continue;+ for (auto const* subclass : subclasses->second) {+ if (!record_declares_no_cell_edges(*subclass))+ validate(subclass, claim.callbacks_hold_roots, definition);+ }+ }+}+// NB: A subclass of a class whose counted or owned pointers are declared to hold no cell edges is checked with the claiming+// base, which is checked on its own.+void LibJSGCVisitor::validate_claim_of_no_cell_edges(clang::CXXRecordDecl const& record, bool callbacks_hold_roots, clang::CXXRecordDecl const* claiming_base)+{+ auto const* claimed_record = &record;auto& diag_engine = m_context.getDiagnostics();auto diag_id = diag_engine.getCustomDiagID(clang::DiagnosticsEngine::Error, "%0 is declared to hold no cell edges, but its member %1 holds a GC pointer");auto record_name = name_for_diagnostic(m_context, *claimed_record);+ if (claiming_base)+ record_name += " (through its base " + name_for_diagnostic(m_context, *claiming_base) + ")";auto function_diag_id = diag_engine.getCustomDiagID(clang::DiagnosticsEngine::Error, "%0 is declared to hold no cell edges, but its member %1 holds an AK::Function, whose captures can't be checked");+ auto violates = [&](clang::QualType type, ClaimViolation violation) {+ if (violation == ClaimViolation::OpaqueFunction && callbacks_hold_roots)+ return false;+ // NB: The claimed type is checked here already, wherever its members lead back to it, such as through the+ // template arguments of RefCounted<T>.+ std::set<clang::CXXRecordDecl const*> visited { claimed_record };+ if (claiming_base)+ visited.insert(claiming_base);+ return type_violates_claim_of_no_cell_edges(type, violation, visited, m_polymorphic_subclasses);+ };for (auto const* field : claimed_record->fields()) {- std::set<clang::CXXRecordDecl const*> visited;- if (type_holds_gc_pointer(field->getType(), visited))+ if (violates(field->getType(), ClaimViolation::GCPointer))diag_engine.Report(field->getLocation(), diag_id) << record_name << field->getName();- std::set<clang::CXXRecordDecl const*> visited_for_functions;- if (!callbacks_hold_roots && type_holds_opaque_function(field->getType(), visited_for_functions))+ if (violates(field->getType(), ClaimViolation::OpaqueFunction))diag_engine.Report(field->getLocation(), function_diag_id) << record_name << field->getName();}// NB: A base that declares the claim itself is checked on its own.auto base_diag_id = diag_engine.getCustomDiagID(clang::DiagnosticsEngine::Error, "%0 is declared to hold no cell edges, but its base %1 holds a GC pointer");+ auto base_function_diag_id = diag_engine.getCustomDiagID(clang::DiagnosticsEngine::Error, "%0 is declared to hold no cell edges, but its base %1 holds an AK::Function, whose captures can't be checked");for (auto const& base : claimed_record->bases()) {auto const* base_record = base.getType()->getAsCXXRecordDecl();if (base_record && record_declares_no_cell_edges(*base_record))continue;- std::set<clang::CXXRecordDecl const*> visited;- if (type_holds_gc_pointer(base.getType(), visited))+ if (violates(base.getType(), ClaimViolation::GCPointer))diag_engine.Report(base.getBeginLoc(), base_diag_id) << record_name << base.getType();+ if (violates(base.getType(), ClaimViolation::OpaqueFunction))+ diag_engine.Report(base.getBeginLoc(), base_function_diag_id) << record_name << base.getType();}}@@ -1385,11 +1499,6 @@ static bool is_value_wrapper(std::string const& template_name)return template_names.contains(template_name);}-static bool is_owning_pointer(std::string const& template_name)-{- return template_name == "AK::NonnullOwnPtr" || template_name == "AK::NonnullRefPtr" || template_name == "AK::OwnPtr" || template_name == "AK::RefPtr";-}-static std::optional<clang::QualType> find_container_with_unscanned_gc_pointers(clang::QualType, std::set<clang::CXXRecordDecl const*>& visited, PolymorphicSubclasses const* subclasses_of_owned_types = nullptr);static clang::FieldDecl const* find_owned_member_with_gc_pointers(clang::QualType owned_type, std::set<clang::CXXRecordDecl const*>& visited, PolymorphicSubclasses const& subclasses_of_owned_types)@@ -1569,6 +1678,7 @@ void LibJSGCASTConsumer::HandleTranslationUnit(clang::ASTContext& context){LibJSGCVisitor visitor { context, m_macro_map, m_detect_invalid_function_members };visitor.TraverseDecl(context.getTranslationUnitDecl());+ visitor.validate_claims_of_no_cell_edges();}char const* LibJSCellMacro::type_name(Type type)diff --git a/Meta/ClangPlugins/LibJSGCPluginAction.h b/Meta/ClangPlugins/LibJSGCPluginAction.hindex 20ab097ca15..e0b80b5b452 100644--- a/Meta/ClangPlugins/LibJSGCPluginAction.h+++ b/Meta/ClangPlugins/LibJSGCPluginAction.h@@ -66,6 +66,8 @@ public:bool VisitCXXMethodDecl(clang::CXXMethodDecl*);bool VisitCXXConstructExpr(clang::CXXConstructExpr*);bool VisitClassTemplateDecl(clang::ClassTemplateDecl*);++ void validate_claims_of_no_cell_edges();bool VisitVarDecl(clang::VarDecl*);bool VisitCXXForRangeStmt(clang::CXXForRangeStmt*);bool VisitFunctionDecl(clang::FunctionDecl*);@@ -80,7 +82,8 @@ 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_claim_of_no_cell_edges(clang::CXXRecordDecl const&);+ void remember_claim_of_no_cell_edges(clang::CXXRecordDecl const&);+ void validate_claim_of_no_cell_edges(clang::CXXRecordDecl const&, bool callbacks_hold_roots, clang::CXXRecordDecl const* claiming_base = nullptr);void remember_polymorphic_subclass(clang::CXXRecordDecl const&);void validate_object_on_stack(clang::VarDecl const&, clang::SourceLocation);void validate_type_on_stack(clang::QualType, clang::SourceLocation);@@ -90,6 +93,13 @@ private:LibJSCellMacroMap const& m_macro_map;bool m_detect_invalid_function_members;PolymorphicSubclasses m_polymorphic_subclasses;++ struct ClaimOfNoCellEdges {+ clang::CXXRecordDecl const* record;+ bool callbacks_hold_roots;+ bool covers_subclasses;+ };+ std::vector<ClaimOfNoCellEdges> m_claims_of_no_cell_edges;};class LibJSGCASTConsumer : public clang::ASTConsumer {diff --git a/Tests/ClangPlugins/LibJSGCTests/untraced_capture_holds_gc_pointer.cpp b/Tests/ClangPlugins/LibJSGCTests/untraced_capture_holds_gc_pointer.cppindex 674acbdc2c4..10767a9a7d2 100644--- a/Tests/ClangPlugins/LibJSGCTests/untraced_capture_holds_gc_pointer.cpp+++ b/Tests/ClangPlugins/LibJSGCTests/untraced_capture_holds_gc_pointer.cpp@@ -4,12 +4,16 @@* SPDX-License-Identifier: BSD-2-Clause*/-// RUN: %clang++ -Xclang -verify %plugin_opts% -c %s -o %t 2>&1+// RUN: %clang++ -Xclang -verify -ferror-limit=0 %plugin_opts% -c %s -o %t 2>&1#include <AK/Function.h>+#include <AK/OwnPtr.h>+#include <AK/RefCounted.h>+#include <AK/RefPtr.h>#include <AK/Vector.h>#include <LibGC/Cell.h>#include <LibGC/Forward.h>+#include <LibGC/Member.h>#include <LibGC/Ptr.h>#include <LibGC/Root.h>#include <LibGC/Weak.h>@@ -92,3 +96,131 @@ struct KeepsCallbacksWithRoots {};GC_UNTRACED_CAPTURE_WITH_ROOTED_CALLBACKS(KeepsCallbacksWithRoots);++struct OwnsCell {+ // expected-error@+1 {{OwnsCell is declared to hold no cell edges, but its member inner holds a GC pointer}}+ OwnPtr<Inner> inner;+};++GC_UNTRACED_CAPTURE(OwnsCell);++struct CountedCell : RefCounted<CountedCell> {+ GC::Ptr<GC::Cell> cell;+};++struct SharesCell {+ GC_HOLDS_NO_CELL_EDGES;++ // expected-error@+1 {{SharesCell is declared to hold no cell edges, but its member counted holds a GC pointer}}+ RefPtr<CountedCell> counted;+};++struct CountedPlainData : RefCounted<CountedPlainData> {+ GC_HOLDS_NO_CELL_EDGES;++ int value { 0 };+};++struct SharesPlainData {+ GC_HOLDS_NO_CELL_EDGES;++ RefPtr<CountedPlainData> counted;+};++struct HoldsRawCell {+ GC_HOLDS_NO_CELL_EDGES;++ // expected-error@+1 {{HoldsRawCell is declared to hold no cell edges, but its member cell holds a GC pointer}}+ GC::RawPtr<GC::Cell> cell;+};++struct HoldsMemberCell {+ GC_HOLDS_NO_CELL_EDGES;++ // expected-error@+1 {{HoldsMemberCell is declared to hold no cell edges, but its member cell holds a GC pointer}}+ GC::Member<GC::Cell> cell;+};++struct InheritsCallback {+ Function<void()> callback;+};++// expected-error@+1 {{DeclaresOverBaseWithCallback is declared to hold no cell edges, but its base 'InheritsCallback' holds an AK::Function, whose captures can't be checked}}+struct DeclaresOverBaseWithCallback : InheritsCallback {+ GC_HOLDS_NO_CELL_EDGES;+};++// An owned object's callbacks are its own API.+struct OwnsCallbackHolder {+ GC_HOLDS_NO_CELL_EDGES;++ OwnPtr<InheritsCallback> holder;+};++struct CountedAndClaimed : RefCounted<CountedAndClaimed> {+ GC_HOLDS_NO_CELL_EDGES;++ // expected-error@+1 {{CountedAndClaimed is declared to hold no cell edges, but its member cell holds a GC pointer}}+ GC::Ptr<GC::Cell> cell;+};++template<typename T>+struct ClaimedTemplate {+ GC_HOLDS_NO_CELL_EDGES;++ // expected-error@+1 {{ClaimedTemplate<GC::Ref<GC::Cell>> is declared to hold no cell edges, but its member value holds a GC pointer}}+ T value;+};++inline ClaimedTemplate<int> plain_instance;+inline ClaimedTemplate<GC::Ref<GC::Cell>>* cell_instance_pointer;+inline auto cell_instance_size = sizeof(ClaimedTemplate<GC::Ref<GC::Cell>>);++struct OwnedBase {+ virtual ~OwnedBase() = default;+};++struct OwnedSubclassWithCell : OwnedBase {+ // expected-error@+1 {{Member cell of polymorphic class OwnedSubclassWithCell contains GC pointers but OwnedSubclassWithCell has no visit_edges method}}+ GC::Ptr<GC::Cell> cell;+};++struct OwnsPolymorphicBase {+ GC_HOLDS_NO_CELL_EDGES;++ // expected-error@+1 {{OwnsPolymorphicBase is declared to hold no cell edges, but its member owned holds a GC pointer}}+ OwnPtr<OwnedBase> owned;+};++struct ClaimedPolymorphicBase {+ virtual ~ClaimedPolymorphicBase() = default;+ int value { 0 };+};+GC_UNTRACED_CAPTURE(ClaimedPolymorphicBase);++struct PlainSubclassOfClaimedBase : ClaimedPolymorphicBase {+ int more { 0 };+};++struct SubclassOfClaimedBaseWithCell : ClaimedPolymorphicBase {+ // expected-error@+2 {{Member cell of polymorphic class SubclassOfClaimedBaseWithCell contains GC pointers but SubclassOfClaimedBaseWithCell has no visit_edges method}}+ // expected-error@+1 {{SubclassOfClaimedBaseWithCell (through its base ClaimedPolymorphicBase) is declared to hold no cell edges, but its member cell holds a GC pointer}}+ GC::Ptr<GC::Cell> cell;+};++struct IndirectSubclassOfClaimedBaseWithCallback : PlainSubclassOfClaimedBase {+ // expected-error@+1 {{IndirectSubclassOfClaimedBaseWithCallback (through its base ClaimedPolymorphicBase) is declared to hold no cell edges, but its member callback holds an AK::Function, whose captures can't be checked}}+ Function<void()> callback;+};++struct MixinOfCell {+ virtual ~MixinOfCell() = default;+ void visit_edges(GC::Cell::Visitor&) { }+};++struct PointsToMixin {+ GC_HOLDS_NO_CELL_EDGES;++ // expected-error@+1 {{PointsToMixin is declared to hold no cell edges, but its member mixin holds a GC pointer}}+ MixinOfCell* mixin { nullptr };+};