llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-static-analyzer-1
Author: Ryosuke Niwa (rniwa)
<details>
<summary>Changes</summary>
TrivialFunctionAnalysisVisitor had no VisitLambdaExpr, so every LambdaExpr fell
through to VisitStmt and was rejected. Any lambda written inside a nodelete
function was reported, even one as harmless as
callLambda([&] { obj->doTrivialWork(); });
The existing deleteInLambda tests passed only because everything failed.
Add VisitLambdaExpr. The capture initializers do run where the closure is
created, so visit them. The body does not, but a closure that could destruct an
object is not safe to hand out from a nodelete function either, so check it too
-- visiting it directly rather than through IsFunctionTrivial, since the body
is written inside the function being analyzed and the blame belongs on the
offending statement rather than on the whole lambda. A generic lambda's own
body is a dependent pattern with unresolved calls, so walk the instantiated
operator() specializations instead; one that is never instantiated cannot
destruct anything.
Separately, an implicit or =default destructor was decided from its body. The
calls to the base and member destructors are materialised at codegen, so the
AST holds either no body at all (when the destructor is never odr-used) or an
empty one. Reading the body therefore flips between blaming every such
destructor and clearing every such destructor, and the hasTrivialDestructor()
fast path only covered the cases that were already safe. Ask the subobjects
instead, via the CanTriviallyDestruct that the rest of this class already uses.
Closure types depend on this since their destructor is always implicit, but it
also recovers true positives that the empty synthesized body was hiding: a
temporary whose implicit destructor destroys a member that deletes was silent
before and now reports, with a note naming that member's destructor.
The two existing lambda expectations move to the 'delete' line, where the caret
now lands.
Co-authored with Claude.
rdar://178617685
---
Full diff: https://github.com/llvm/llvm-project/pull/240297.diff
2 Files Affected:
- (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
(+73-7)
- (modified) clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp
(+93-2)
``````````diff
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
index 76afe9ada06205..9cc782985c834f 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/PtrTypesSemantics.cpp
@@ -830,6 +830,34 @@ class TrivialFunctionAnalysisVisitor
return false; // Otherwise it's likely not trivial.
}
+ // The body of an implicit or =default destructor says nothing about what it
+ // does: the calls to the base and member destructors are materialised at
+ // codegen, so the AST holds either no body at all (when the destructor is
+ // never odr-used) or an empty one. Reading the body therefore flips between
+ // blaming every such destructor and clearing every such destructor; ask the
+ // subobjects instead. Closure types depend on this -- their destructor is
+ // always implicit.
+ bool CanTriviallyRunImplicitDtor(const CXXDestructorDecl *Dtor) {
+ const CXXRecordDecl *Cls = Dtor->getParent();
+ if (!Cls->hasDefinition())
+ return false;
+ if (Cls->hasTrivialDestructor())
+ return true;
+ for (const FieldDecl *F : Cls->fields()) {
+ if (!CanTriviallyDestruct(F->getType()))
+ return false;
+ }
+ for (const CXXBaseSpecifier &B : Cls->bases()) {
+ if (!CanTriviallyDestruct(B.getType()))
+ return false;
+ }
+ for (const CXXBaseSpecifier &B : Cls->vbases()) {
+ if (!CanTriviallyDestruct(B.getType()))
+ return false;
+ }
+ return true;
+ }
+
bool CanTriviallyConstruct(QualType Ty) {
if (Ty.isNull())
return false;
@@ -973,11 +1001,11 @@ class TrivialFunctionAnalysisVisitor
return false;
}
}
- // An implicit or =default special member runs no user code when it is
- // trivial in the C++ standard sense, so it cannot delete. Such a
- // member's synthesized body is typically absent from the AST until
- // codegen materialises it, which the generic null-body check below
- // would otherwise conservatively classify as non-trivial.
+ // An implicit or =default special member has no body of its own to read.
+ // A constructor is covered by the inits() walk above; a destructor has
+ // to be decided from its subobjects. The synthesized body is in any case
+ // absent from the AST until codegen materialises it, which the generic
+ // null-body check below would otherwise call non-trivial.
if (MethodDecl && !MethodDecl->isUserProvided()) {
if (CtorDecl) {
const CXXRecordDecl *RD = CtorDecl->getParent();
@@ -989,8 +1017,8 @@ class TrivialFunctionAnalysisVisitor
RD->hasTrivialMoveConstructor()))
return true;
}
- if (DtorDecl && DtorDecl->getParent()->hasTrivialDestructor())
- return true;
+ if (DtorDecl)
+ return CanTriviallyRunImplicitDtor(DtorDecl);
}
const Stmt *Body = D->getBody();
if (!Body)
@@ -1393,6 +1421,44 @@ class TrivialFunctionAnalysisVisitor
return Visit(BTE->getSubExpr());
}
+ bool VisitLambdaExpr(const LambdaExpr *LE) {
+ // Creating the closure object runs each capture's initializer here.
+ for (const Expr *Init : LE->capture_inits()) {
+ if (Init && !Visit(Init))
+ return false;
+ }
+
+ // The body does not run at this point, but a closure that could destruct
+ // an object is not safe to hand out from a nodelete function either, so
+ // check it here rather than relying on finding the call.
+ if (LE->isGenericLambda()) {
+ // A generic lambda's own body is a dependent pattern whose calls are
+ // unresolved. Only its instantiations describe code that runs; one that
+ // is never instantiated cannot destruct anything.
+ auto *CallOpTemplate =
+ LE->getLambdaClass()->getDependentLambdaCallOperator();
+ if (!CallOpTemplate)
+ return true;
+ for (const FunctionDecl *Spec : CallOpTemplate->specializations()) {
+ if (const Stmt *Body = Spec->getBody()) {
+ if (!Visit(Body))
+ return false;
+ }
+ }
+ return true;
+ }
+
+ const auto *CallOp = LE->getCallOperator();
+ if (!CallOp)
+ return true;
+ // The body is written inside the function being analyzed, so visit it
+ // directly: that keeps the blame on the offending statement itself instead
+ // of on the whole lambda. The parameters are destructed by whoever calls
+ // the closure, and that call site is checked separately.
+ const Stmt *Body = CallOp->getBody();
+ return !Body || Visit(Body);
+ }
+
bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *AILE) {
return Visit(AILE->getCommonExpr()) && Visit(AILE->getSubExpr());
}
diff --git a/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp
b/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp
index bffc04c7646512..bc5ecf2803cea8 100644
--- a/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/nodelete-annotation.cpp
@@ -532,18 +532,50 @@ void noopWithLambda(Callback) {
void [[clang::annotate_type("webkit.nodelete")]] deleteInLambda(SomeObject*
someObj) {
callLambda([&]() {
- // expected-warning@-1{{A function 'deleteInLambda' has
[[clang::annotate_type("webkit.nodelete")]] but it contains code that could
destruct an object}}
delete someObj;
+ // expected-warning@-1{{A function 'deleteInLambda' has
[[clang::annotate_type("webkit.nodelete")]] but it contains code that could
destruct an object}}
});
}
void [[clang::annotate_type("webkit.nodelete")]] deleteInLambda2(SomeObject*
someObj) {
noopWithLambda([&]() {
- // expected-warning@-1{{A function 'deleteInLambda2' has
[[clang::annotate_type("webkit.nodelete")]] but it contains code that could
destruct an object}}
delete someObj;
+ // expected-warning@-1{{A function 'deleteInLambda2' has
[[clang::annotate_type("webkit.nodelete")]] but it contains code that could
destruct an object}}
+ });
+}
+
+void [[clang::annotate_type("webkit.nodelete")]] trivialLambda(SomeObject*
someObj) {
+ callLambda([&]() {
+ someObj->doTrivialWork();
+ });
+}
+
+void [[clang::annotate_type("webkit.nodelete")]]
trivialLambdaNeverCalled(SomeObject* someObj) {
+ noopWithLambda([someObj]() {
+ someObj->doTrivialWork();
});
}
+template <typename Callback, typename Arg>
+void callLambdaWithArg(Callback callback, Arg arg) {
+ callback(arg);
+}
+
+void [[clang::annotate_type("webkit.nodelete")]]
trivialGenericLambda(SomeObject* someObj) {
+ callLambdaWithArg([](auto* obj) { obj->doTrivialWork(); }, someObj);
+}
+
+void [[clang::annotate_type("webkit.nodelete")]] uninstantiatedGenericLambda()
{
+ noopWithLambda([](auto* obj) { delete obj; });
+}
+
+void [[clang::annotate_type("webkit.nodelete")]]
deleteInGenericLambda(SomeObject* someObj) {
+ callLambdaWithArg([](auto* obj) {
+ delete obj;
+ // expected-warning@-1{{A function 'deleteInGenericLambda' has
[[clang::annotate_type("webkit.nodelete")]] but it contains code that could
destruct an object}}
+ }, someObj);
+}
+
void [[clang::annotate_type("webkit.nodelete")]] deleteIfNeeded(SomeObject*
someObj) {
if (someObj)
delete someObj;
@@ -978,3 +1010,62 @@ void [[clang::annotate_type("webkit.nodelete")]]
destructiveObjectInBraces() {
}
} // namespace std_initializer_list
+
+namespace implicit_destructor {
+
+// An implicit or =default destructor has no body in the AST, so deciding it
+// from the body alone classifies every non-trivially-destructible class as
+// unsafe. What it actually does is destroy the bases and members, so ask
+// those instead. Closure types hit this constantly: their destructor is
+// always implicit.
+
+struct SafeMember {
+ ~SafeMember() { m_count = 0; }
+ unsigned m_count { 0 };
+};
+
+struct DeletingMember {
+ ~DeletingMember() { delete m_obj; }
+ // expected-note@-1 3 {{Could destruct an object}}
+ SomeObject* m_obj { nullptr };
+};
+
+struct SafeAggregate { SafeMember member; };
+struct DeletingAggregate { DeletingMember member; }; // expected-note
{{Calling '~DeletingMember'}}
+struct DerivedFromSafe : SafeMember { };
+struct DerivedFromDeleting : DeletingMember { }; // expected-note {{Calling
'~DeletingMember'}}
+
+void [[clang::annotate_type("webkit.nodelete")]] takeSafe(const
SafeAggregate&) { }
+void [[clang::annotate_type("webkit.nodelete")]] takeDeleting(const
DeletingAggregate&) { }
+void [[clang::annotate_type("webkit.nodelete")]] takeDerivedFromSafe(const
DerivedFromSafe&) { }
+void [[clang::annotate_type("webkit.nodelete")]] takeDerivedFromDeleting(const
DerivedFromDeleting&) { }
+
+void [[clang::annotate_type("webkit.nodelete")]] safeTemporary() {
+ takeSafe(SafeAggregate { });
+}
+
+void [[clang::annotate_type("webkit.nodelete")]] deletingTemporary() {
+ takeDeleting(DeletingAggregate { });
+ // expected-warning@-1{{A function 'deletingTemporary' has
[[clang::annotate_type("webkit.nodelete")]] but it contains code that could
destruct an object}}
+}
+
+void [[clang::annotate_type("webkit.nodelete")]] safeBaseTemporary() {
+ takeDerivedFromSafe(DerivedFromSafe { });
+}
+
+void [[clang::annotate_type("webkit.nodelete")]] deletingBaseTemporary() {
+ takeDerivedFromDeleting(DerivedFromDeleting { });
+ // expected-warning@-1{{A function 'deletingBaseTemporary' has
[[clang::annotate_type("webkit.nodelete")]] but it contains code that could
destruct an object}}
+}
+
+void [[clang::annotate_type("webkit.nodelete")]] safeCaptureByValue() {
+ noopWithLambda([member = SafeMember { }]() { });
+}
+
+void [[clang::annotate_type("webkit.nodelete")]] deletingCaptureByValue() {
+ noopWithLambda([member = DeletingMember { }]() { });
+ // expected-note@-1 {{Calling '~DeletingMember'}}
+ // expected-warning@-2{{A function 'deletingCaptureByValue' has
[[clang::annotate_type("webkit.nodelete")]] but it contains code that could
destruct an object}}
+}
+
+} // namespace implicit_destructor
``````````
</details>
https://github.com/llvm/llvm-project/pull/240297
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits