1 //=- RunLoopAutoreleaseLeakChecker.cpp --------------------------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 // A checker for detecting leaks resulting from allocating temporary
12 // autoreleased objects before starting the main run loop.
14 // Checks for two antipatterns:
15 // 1. ObjCMessageExpr followed by [[NSRunLoop mainRunLoop] run] in the same
17 // 2. ObjCMessageExpr followed by [[NSRunLoop mainRunLoop] run] in no
20 // Any temporary objects autoreleased in code called in those expressions
21 // will not be deallocated until the program exits, and are effectively leaks.
23 //===----------------------------------------------------------------------===//
26 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
27 #include "clang/AST/Decl.h"
28 #include "clang/AST/DeclObjC.h"
29 #include "clang/ASTMatchers/ASTMatchFinder.h"
30 #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
31 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
32 #include "clang/StaticAnalyzer/Core/Checker.h"
33 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
34 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
35 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
36 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
38 using namespace clang;
40 using namespace ast_matchers;
44 const char * RunLoopBind = "NSRunLoopM";
45 const char * RunLoopRunBind = "RunLoopRunM";
46 const char * OtherMsgBind = "OtherMessageSentM";
47 const char * AutoreleasePoolBind = "AutoreleasePoolM";
48 const char * OtherStmtAutoreleasePoolBind = "OtherAutoreleasePoolM";
50 class RunLoopAutoreleaseLeakChecker : public Checker<check::ASTCodeBody> {
53 void checkASTCodeBody(const Decl *D,
55 BugReporter &BR) const;
59 } // end anonymous namespace
61 /// \return Whether {@code A} occurs before {@code B} in traversal of
63 /// Conceptually a very incomplete/unsound approximation of happens-before
64 /// relationship (A is likely to be evaluated before B),
65 /// but useful enough in this case.
66 static bool seenBefore(const Stmt *Parent, const Stmt *A, const Stmt *B) {
67 for (const Stmt *C : Parent->children()) {
76 return seenBefore(C, A, B);
81 static void emitDiagnostics(BoundNodes &Match,
85 const RunLoopAutoreleaseLeakChecker *Checker) {
88 const Stmt *DeclBody = D->getBody();
90 AnalysisDeclContext *ADC = AM.getAnalysisDeclContext(D);
92 const auto *ME = Match.getNodeAs<ObjCMessageExpr>(OtherMsgBind);
96 Match.getNodeAs<ObjCAutoreleasePoolStmt>(AutoreleasePoolBind);
98 Match.getNodeAs<ObjCAutoreleasePoolStmt>(OtherStmtAutoreleasePoolBind);
99 bool HasAutoreleasePool = (AP != nullptr);
101 const auto *RL = Match.getNodeAs<ObjCMessageExpr>(RunLoopBind);
102 const auto *RLR = Match.getNodeAs<Stmt>(RunLoopRunBind);
103 assert(RLR && "Run loop launch not found");
106 // Launch of run loop occurs before the message-sent expression is seen.
107 if (seenBefore(DeclBody, RLR, ME))
110 if (HasAutoreleasePool && (OAP != AP))
113 PathDiagnosticLocation Location = PathDiagnosticLocation::createBegin(
114 ME, BR.getSourceManager(), ADC);
115 SourceRange Range = ME->getSourceRange();
117 BR.EmitBasicReport(ADC->getDecl(), Checker,
118 /*Name=*/"Memory leak inside autorelease pool",
119 /*Category=*/"Memory",
121 (Twine("Temporary objects allocated in the") +
122 " autorelease pool " +
123 (HasAutoreleasePool ? "" : "of last resort ") +
124 "followed by the launch of " +
125 (RL ? "main run loop " : "xpc_main ") +
126 "may never get released; consider moving them to a "
127 "separate autorelease pool")
132 static StatementMatcher getRunLoopRunM(StatementMatcher Extra = anything()) {
133 StatementMatcher MainRunLoopM =
134 objcMessageExpr(hasSelector("mainRunLoop"),
135 hasReceiverType(asString("NSRunLoop")),
139 StatementMatcher MainRunLoopRunM = objcMessageExpr(hasSelector("run"),
140 hasReceiver(MainRunLoopM),
141 Extra).bind(RunLoopRunBind);
143 StatementMatcher XPCRunM =
144 callExpr(callee(functionDecl(hasName("xpc_main")))).bind(RunLoopRunBind);
145 return anyOf(MainRunLoopRunM, XPCRunM);
148 static StatementMatcher getOtherMessageSentM(StatementMatcher Extra = anything()) {
149 return objcMessageExpr(unless(anyOf(equalsBoundNode(RunLoopBind),
150 equalsBoundNode(RunLoopRunBind))),
156 checkTempObjectsInSamePool(const Decl *D, AnalysisManager &AM, BugReporter &BR,
157 const RunLoopAutoreleaseLeakChecker *Chkr) {
158 StatementMatcher RunLoopRunM = getRunLoopRunM();
159 StatementMatcher OtherMessageSentM = getOtherMessageSentM(
160 hasAncestor(autoreleasePoolStmt().bind(OtherStmtAutoreleasePoolBind)));
162 StatementMatcher RunLoopInAutorelease =
164 hasDescendant(RunLoopRunM),
165 hasDescendant(OtherMessageSentM)).bind(AutoreleasePoolBind);
167 DeclarationMatcher GroupM = decl(hasDescendant(RunLoopInAutorelease));
169 auto Matches = match(GroupM, *D, AM.getASTContext());
170 for (BoundNodes Match : Matches)
171 emitDiagnostics(Match, D, BR, AM, Chkr);
175 checkTempObjectsInNoPool(const Decl *D, AnalysisManager &AM, BugReporter &BR,
176 const RunLoopAutoreleaseLeakChecker *Chkr) {
178 auto NoPoolM = unless(hasAncestor(autoreleasePoolStmt()));
180 StatementMatcher RunLoopRunM = getRunLoopRunM(NoPoolM);
181 StatementMatcher OtherMessageSentM = getOtherMessageSentM(NoPoolM);
183 DeclarationMatcher GroupM = functionDecl(
185 hasDescendant(RunLoopRunM),
186 hasDescendant(OtherMessageSentM)
189 auto Matches = match(GroupM, *D, AM.getASTContext());
191 for (BoundNodes Match : Matches)
192 emitDiagnostics(Match, D, BR, AM, Chkr);
196 void RunLoopAutoreleaseLeakChecker::checkASTCodeBody(const Decl *D,
198 BugReporter &BR) const {
199 checkTempObjectsInSamePool(D, AM, BR, this);
200 checkTempObjectsInNoPool(D, AM, BR, this);
203 void ento::registerRunLoopAutoreleaseLeakChecker(CheckerManager &mgr) {
204 mgr.registerChecker<RunLoopAutoreleaseLeakChecker>();