Skip to content

Truly independent web browser

Meta/ClangPlugins: Reject returning unrooted GC pointer containers

Shannon Booth3443a5712acd
diff --git a/Meta/ClangPlugins/LibJSGCPluginAction.cpp b/Meta/ClangPlugins/LibJSGCPluginAction.cpp
index a1464e339de..fa2310cf7af 100644
--- a/Meta/ClangPlugins/LibJSGCPluginAction.cpp
+++ b/Meta/ClangPlugins/LibJSGCPluginAction.cpp
@@ -1303,6 +1303,11 @@ void LibJSGCVisitor::validate_object_on_stack(clang::VarDecl const& variable, cl
if (type.isNull())
return;
+ validate_type_on_stack(type, location);
+}
+
+void LibJSGCVisitor::validate_type_on_stack(clang::QualType type, clang::SourceLocation location)
+{
std::set<clang::CXXRecordDecl const*> visited;
auto container = find_container_with_unscanned_gc_pointers(type, visited);
if (!container)
@@ -1341,6 +1346,23 @@ bool LibJSGCVisitor::VisitCXXForRangeStmt(clang::CXXForRangeStmt* for_range)
return true;
}
+// A container returned by value becomes a temporary or a local of the caller.
+bool LibJSGCVisitor::VisitFunctionDecl(clang::FunctionDecl* function)
+{
+ if (function->doesThisDeclarationHaveABody() && !function->isImplicit() && !function->getReturnType()->isReferenceType())
+ validate_type_on_stack(function->getReturnType(), function->getLocation());
+ return true;
+}
+
+// The call operator of a lambda is only visited through its body.
+bool LibJSGCVisitor::VisitLambdaExpr(clang::LambdaExpr* lambda)
+{
+ auto return_type = lambda->getCallOperator()->getReturnType();
+ if (!return_type->isReferenceType())
+ validate_type_on_stack(return_type, lambda->getBeginLoc());
+ return true;
+}
+
LibJSGCASTConsumer::LibJSGCASTConsumer(clang::CompilerInstance& compiler, bool detect_invalid_function_members)
: m_compiler(compiler)
, m_detect_invalid_function_members(detect_invalid_function_members)
diff --git a/Meta/ClangPlugins/LibJSGCPluginAction.h b/Meta/ClangPlugins/LibJSGCPluginAction.h
index ba637c132b9..3cf361d265e 100644
--- a/Meta/ClangPlugins/LibJSGCPluginAction.h
+++ b/Meta/ClangPlugins/LibJSGCPluginAction.h
@@ -66,6 +66,8 @@ public:
bool VisitClassTemplateDecl(clang::ClassTemplateDecl*);
bool VisitVarDecl(clang::VarDecl*);
bool VisitCXXForRangeStmt(clang::CXXForRangeStmt*);
+ bool VisitFunctionDecl(clang::FunctionDecl*);
+ bool VisitLambdaExpr(clang::LambdaExpr*);
private:
struct CellMacroExpectation {
@@ -76,6 +78,7 @@ private:
void validate_record_macros(clang::CXXRecordDecl const&);
void validate_engine_subclass_is_in_libjs(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&);
clang::ASTContext& m_context;
diff --git a/Tests/ClangPlugins/LibJSGCTests/gc_pointer_container_on_stack.cpp b/Tests/ClangPlugins/LibJSGCTests/gc_pointer_container_on_stack.cpp
index 4c1c0c07b82..4abb8bb7d45 100644
--- a/Tests/ClangPlugins/LibJSGCTests/gc_pointer_container_on_stack.cpp
+++ b/Tests/ClangPlugins/LibJSGCTests/gc_pointer_container_on_stack.cpp
@@ -181,6 +181,46 @@ void rooted_or_not_holding_gc_pointers(State& state)
(void)cell;
}
+// expected-error@+1 {{'Vector<GC::Ref<GC::Cell>>' keeps GC pointers in heap memory that is not scanned for roots}}
+Vector<GC::Ref<GC::Cell>> return_cells()
+{
+ return {};
+}
+
+// expected-error@+1 {{keeps GC pointers in heap memory that is not scanned for roots}}
+Optional<Vector<GC::Ref<GC::Cell>>> return_optional_cells()
+{
+ return {};
+}
+
+// expected-error@+1 {{'Vector<GC::Ref<GC::Cell>>' keeps GC pointers in heap memory that is not scanned for roots}}
+State return_state()
+{
+ return {};
+}
+
+void return_from_lambda()
+{
+ // expected-error@+1 {{keeps GC pointers in heap memory that is not scanned for roots}}
+ auto lambda = [] { return Vector<GC::Ref<GC::Cell>> {}; };
+ (void)lambda;
+}
+
+GC::RootVector<GC::Ref<GC::Cell>> return_root_vector()
+{
+ return {};
+}
+
+Vector<GC::Ref<GC::Cell>> const& return_reference(State& state)
+{
+ return state.cells;
+}
+
+Span<GC::Ref<GC::Cell>> return_span(State& state)
+{
+ return state.cells.span();
+}
+
void range_over_temporary()
{
// expected-error@+1 {{'Vector<GC::Ref<GC::Cell>>' keeps GC pointers in heap memory that is not scanned for roots}}