Truly independent web browser
Meta/ClangPlugins: Reject unrooted GC pointer containers in parameters
Shannon Boothf2035e706547
diff --git a/Meta/ClangPlugins/LibJSGCPluginAction.cpp b/Meta/ClangPlugins/LibJSGCPluginAction.cppindex 47f6652e993..a1464e339de 100644--- a/Meta/ClangPlugins/LibJSGCPluginAction.cpp+++ b/Meta/ClangPlugins/LibJSGCPluginAction.cpp@@ -1315,10 +1315,17 @@ void LibJSGCVisitor::validate_object_on_stack(clang::VarDecl const& variable, clbool LibJSGCVisitor::VisitVarDecl(clang::VarDecl* variable){- // A container passed by value is usually moved into a visited member before anything can allocate.- if (!variable->hasLocalStorage() || variable->isImplicit() || llvm::isa<clang::ParmVarDecl>(variable))+ if (!variable->hasLocalStorage() || variable->isImplicit())return true;+ // A parameter passed by value is an object on the stack of the definition, like a local. A container that is moved+ // into a field is taken by rvalue reference instead, so that the caller keeps it until then.+ if (llvm::isa<clang::ParmVarDecl>(variable)) {+ auto const* function = llvm::dyn_cast<clang::FunctionDecl>(variable->getDeclContext());+ if (!function || !function->doesThisDeclarationHaveABody() || function->isImplicit())+ return true;+ }+validate_object_on_stack(*variable, variable->getLocation());return true;}diff --git a/Tests/ClangPlugins/LibJSGCTests/gc_pointer_container_on_stack.cpp b/Tests/ClangPlugins/LibJSGCTests/gc_pointer_container_on_stack.cppindex dbcec45450e..4c1c0c07b82 100644--- a/Tests/ClangPlugins/LibJSGCTests/gc_pointer_container_on_stack.cpp+++ b/Tests/ClangPlugins/LibJSGCTests/gc_pointer_container_on_stack.cpp@@ -104,11 +104,62 @@ void lambda_local()}();}+// expected-error@+1 {{'Vector<GC::Ref<GC::Cell>>' keeps GC pointers in heap memory that is not scanned for roots}}void parameter_by_value(Vector<GC::Ref<GC::Cell>> cells, State& state){state.cells = move(cells);}+// expected-error@+1 {{'Vector<GC::Ref<GC::Cell>>' keeps GC pointers in heap memory that is not scanned for roots}}+void state_by_value(State state)+{+ (void)state;+}++void declared_without_body(Vector<GC::Ref<GC::Cell>>);++class Holder {+public:+ // expected-error@+1 {{keeps GC pointers in heap memory that is not scanned for roots}}+ Holder(Vector<GC::Ref<GC::Cell>> cells)+ : m_cells(move(cells))+ {+ }++ Holder(Vector<GC::Ref<GC::Cell>>&& cells, int)+ : m_cells(move(cells))+ {+ }++ void set_cells(Vector<GC::Ref<GC::Cell>>&& cells) { m_cells = move(cells); }++ void declared_elsewhere(Vector<GC::Ref<GC::Cell>>);++private:+ Vector<GC::Ref<GC::Cell>> m_cells;+};++// expected-error@+1 {{keeps GC pointers in heap memory that is not scanned for roots}}+void Holder::declared_elsewhere(Vector<GC::Ref<GC::Cell>> cells)+{+ m_cells = move(cells);+}++void lambda_parameter()+{+ // expected-error@+1 {{keeps GC pointers in heap memory that is not scanned for roots}}+ auto lambda = [](Vector<GC::Ref<GC::Cell>> cells) { (void)cells; };+ (void)lambda;+}++void rooted_or_borrowed_parameters(GC::RootVector<GC::Ref<GC::Cell>> root_vector, Vector<GC::Ref<GC::Cell>> const& reference, Vector<GC::Ref<GC::Cell>>&& rvalue_reference, Span<GC::Ref<GC::Cell>> span)+{+ (void)root_vector;+ (void)reference;+ (void)rvalue_reference;+ (void)span;+}+void rooted_or_not_holding_gc_pointers(State& state){GC::RootVector<GC::Ref<GC::Cell>> root_vector;