10b57cec5SDimitry Andric //=- RunLoopAutoreleaseLeakChecker.cpp --------------------------*- C++ -*-==//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //
80b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
90b57cec5SDimitry Andric //
100b57cec5SDimitry Andric // A checker for detecting leaks resulting from allocating temporary
110b57cec5SDimitry Andric // autoreleased objects before starting the main run loop.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric // Checks for two antipatterns:
140b57cec5SDimitry Andric // 1. ObjCMessageExpr followed by [[NSRunLoop mainRunLoop] run] in the same
150b57cec5SDimitry Andric // autorelease pool.
160b57cec5SDimitry Andric // 2. ObjCMessageExpr followed by [[NSRunLoop mainRunLoop] run] in no
170b57cec5SDimitry Andric // autorelease pool.
180b57cec5SDimitry Andric //
190b57cec5SDimitry Andric // Any temporary objects autoreleased in code called in those expressions
200b57cec5SDimitry Andric // will not be deallocated until the program exits, and are effectively leaks.
210b57cec5SDimitry Andric //
220b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
230b57cec5SDimitry Andric //
240b57cec5SDimitry Andric
250b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
260b57cec5SDimitry Andric #include "clang/AST/Decl.h"
270b57cec5SDimitry Andric #include "clang/AST/DeclObjC.h"
280b57cec5SDimitry Andric #include "clang/ASTMatchers/ASTMatchFinder.h"
290b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
300b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
310b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/Checker.h"
320b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/CheckerManager.h"
330b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
340b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
350b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
360b57cec5SDimitry Andric
370b57cec5SDimitry Andric using namespace clang;
380b57cec5SDimitry Andric using namespace ento;
390b57cec5SDimitry Andric using namespace ast_matchers;
400b57cec5SDimitry Andric
410b57cec5SDimitry Andric namespace {
420b57cec5SDimitry Andric
430b57cec5SDimitry Andric const char * RunLoopBind = "NSRunLoopM";
440b57cec5SDimitry Andric const char * RunLoopRunBind = "RunLoopRunM";
450b57cec5SDimitry Andric const char * OtherMsgBind = "OtherMessageSentM";
460b57cec5SDimitry Andric const char * AutoreleasePoolBind = "AutoreleasePoolM";
470b57cec5SDimitry Andric const char * OtherStmtAutoreleasePoolBind = "OtherAutoreleasePoolM";
480b57cec5SDimitry Andric
490b57cec5SDimitry Andric class RunLoopAutoreleaseLeakChecker : public Checker<check::ASTCodeBody> {
500b57cec5SDimitry Andric
510b57cec5SDimitry Andric public:
520b57cec5SDimitry Andric void checkASTCodeBody(const Decl *D,
530b57cec5SDimitry Andric AnalysisManager &AM,
540b57cec5SDimitry Andric BugReporter &BR) const;
550b57cec5SDimitry Andric
560b57cec5SDimitry Andric };
570b57cec5SDimitry Andric
580b57cec5SDimitry Andric } // end anonymous namespace
590b57cec5SDimitry Andric
60*fe6060f1SDimitry Andric /// \return Whether @c A occurs before @c B in traversal of
61*fe6060f1SDimitry Andric /// @c Parent.
620b57cec5SDimitry Andric /// Conceptually a very incomplete/unsound approximation of happens-before
630b57cec5SDimitry Andric /// relationship (A is likely to be evaluated before B),
640b57cec5SDimitry Andric /// but useful enough in this case.
seenBefore(const Stmt * Parent,const Stmt * A,const Stmt * B)650b57cec5SDimitry Andric static bool seenBefore(const Stmt *Parent, const Stmt *A, const Stmt *B) {
660b57cec5SDimitry Andric for (const Stmt *C : Parent->children()) {
670b57cec5SDimitry Andric if (!C) continue;
680b57cec5SDimitry Andric
690b57cec5SDimitry Andric if (C == A)
700b57cec5SDimitry Andric return true;
710b57cec5SDimitry Andric
720b57cec5SDimitry Andric if (C == B)
730b57cec5SDimitry Andric return false;
740b57cec5SDimitry Andric
750b57cec5SDimitry Andric return seenBefore(C, A, B);
760b57cec5SDimitry Andric }
770b57cec5SDimitry Andric return false;
780b57cec5SDimitry Andric }
790b57cec5SDimitry Andric
emitDiagnostics(BoundNodes & Match,const Decl * D,BugReporter & BR,AnalysisManager & AM,const RunLoopAutoreleaseLeakChecker * Checker)800b57cec5SDimitry Andric static void emitDiagnostics(BoundNodes &Match,
810b57cec5SDimitry Andric const Decl *D,
820b57cec5SDimitry Andric BugReporter &BR,
830b57cec5SDimitry Andric AnalysisManager &AM,
840b57cec5SDimitry Andric const RunLoopAutoreleaseLeakChecker *Checker) {
850b57cec5SDimitry Andric
860b57cec5SDimitry Andric assert(D->hasBody());
870b57cec5SDimitry Andric const Stmt *DeclBody = D->getBody();
880b57cec5SDimitry Andric
890b57cec5SDimitry Andric AnalysisDeclContext *ADC = AM.getAnalysisDeclContext(D);
900b57cec5SDimitry Andric
910b57cec5SDimitry Andric const auto *ME = Match.getNodeAs<ObjCMessageExpr>(OtherMsgBind);
920b57cec5SDimitry Andric assert(ME);
930b57cec5SDimitry Andric
940b57cec5SDimitry Andric const auto *AP =
950b57cec5SDimitry Andric Match.getNodeAs<ObjCAutoreleasePoolStmt>(AutoreleasePoolBind);
960b57cec5SDimitry Andric const auto *OAP =
970b57cec5SDimitry Andric Match.getNodeAs<ObjCAutoreleasePoolStmt>(OtherStmtAutoreleasePoolBind);
980b57cec5SDimitry Andric bool HasAutoreleasePool = (AP != nullptr);
990b57cec5SDimitry Andric
1000b57cec5SDimitry Andric const auto *RL = Match.getNodeAs<ObjCMessageExpr>(RunLoopBind);
1010b57cec5SDimitry Andric const auto *RLR = Match.getNodeAs<Stmt>(RunLoopRunBind);
1020b57cec5SDimitry Andric assert(RLR && "Run loop launch not found");
1030b57cec5SDimitry Andric assert(ME != RLR);
1040b57cec5SDimitry Andric
1050b57cec5SDimitry Andric // Launch of run loop occurs before the message-sent expression is seen.
1060b57cec5SDimitry Andric if (seenBefore(DeclBody, RLR, ME))
1070b57cec5SDimitry Andric return;
1080b57cec5SDimitry Andric
1090b57cec5SDimitry Andric if (HasAutoreleasePool && (OAP != AP))
1100b57cec5SDimitry Andric return;
1110b57cec5SDimitry Andric
1120b57cec5SDimitry Andric PathDiagnosticLocation Location = PathDiagnosticLocation::createBegin(
1130b57cec5SDimitry Andric ME, BR.getSourceManager(), ADC);
1140b57cec5SDimitry Andric SourceRange Range = ME->getSourceRange();
1150b57cec5SDimitry Andric
1160b57cec5SDimitry Andric BR.EmitBasicReport(ADC->getDecl(), Checker,
1170b57cec5SDimitry Andric /*Name=*/"Memory leak inside autorelease pool",
1180b57cec5SDimitry Andric /*BugCategory=*/"Memory",
1190b57cec5SDimitry Andric /*Name=*/
1200b57cec5SDimitry Andric (Twine("Temporary objects allocated in the") +
1210b57cec5SDimitry Andric " autorelease pool " +
1220b57cec5SDimitry Andric (HasAutoreleasePool ? "" : "of last resort ") +
1230b57cec5SDimitry Andric "followed by the launch of " +
1240b57cec5SDimitry Andric (RL ? "main run loop " : "xpc_main ") +
1250b57cec5SDimitry Andric "may never get released; consider moving them to a "
1260b57cec5SDimitry Andric "separate autorelease pool")
1270b57cec5SDimitry Andric .str(),
1280b57cec5SDimitry Andric Location, Range);
1290b57cec5SDimitry Andric }
1300b57cec5SDimitry Andric
getRunLoopRunM(StatementMatcher Extra=anything ())1310b57cec5SDimitry Andric static StatementMatcher getRunLoopRunM(StatementMatcher Extra = anything()) {
1320b57cec5SDimitry Andric StatementMatcher MainRunLoopM =
1330b57cec5SDimitry Andric objcMessageExpr(hasSelector("mainRunLoop"),
1340b57cec5SDimitry Andric hasReceiverType(asString("NSRunLoop")),
1350b57cec5SDimitry Andric Extra)
1360b57cec5SDimitry Andric .bind(RunLoopBind);
1370b57cec5SDimitry Andric
1380b57cec5SDimitry Andric StatementMatcher MainRunLoopRunM = objcMessageExpr(hasSelector("run"),
1390b57cec5SDimitry Andric hasReceiver(MainRunLoopM),
1400b57cec5SDimitry Andric Extra).bind(RunLoopRunBind);
1410b57cec5SDimitry Andric
1420b57cec5SDimitry Andric StatementMatcher XPCRunM =
1430b57cec5SDimitry Andric callExpr(callee(functionDecl(hasName("xpc_main")))).bind(RunLoopRunBind);
1440b57cec5SDimitry Andric return anyOf(MainRunLoopRunM, XPCRunM);
1450b57cec5SDimitry Andric }
1460b57cec5SDimitry Andric
getOtherMessageSentM(StatementMatcher Extra=anything ())1470b57cec5SDimitry Andric static StatementMatcher getOtherMessageSentM(StatementMatcher Extra = anything()) {
1480b57cec5SDimitry Andric return objcMessageExpr(unless(anyOf(equalsBoundNode(RunLoopBind),
1490b57cec5SDimitry Andric equalsBoundNode(RunLoopRunBind))),
1500b57cec5SDimitry Andric Extra)
1510b57cec5SDimitry Andric .bind(OtherMsgBind);
1520b57cec5SDimitry Andric }
1530b57cec5SDimitry Andric
1540b57cec5SDimitry Andric static void
checkTempObjectsInSamePool(const Decl * D,AnalysisManager & AM,BugReporter & BR,const RunLoopAutoreleaseLeakChecker * Chkr)1550b57cec5SDimitry Andric checkTempObjectsInSamePool(const Decl *D, AnalysisManager &AM, BugReporter &BR,
1560b57cec5SDimitry Andric const RunLoopAutoreleaseLeakChecker *Chkr) {
1570b57cec5SDimitry Andric StatementMatcher RunLoopRunM = getRunLoopRunM();
1580b57cec5SDimitry Andric StatementMatcher OtherMessageSentM = getOtherMessageSentM(
1590b57cec5SDimitry Andric hasAncestor(autoreleasePoolStmt().bind(OtherStmtAutoreleasePoolBind)));
1600b57cec5SDimitry Andric
1610b57cec5SDimitry Andric StatementMatcher RunLoopInAutorelease =
1620b57cec5SDimitry Andric autoreleasePoolStmt(
1630b57cec5SDimitry Andric hasDescendant(RunLoopRunM),
1640b57cec5SDimitry Andric hasDescendant(OtherMessageSentM)).bind(AutoreleasePoolBind);
1650b57cec5SDimitry Andric
1660b57cec5SDimitry Andric DeclarationMatcher GroupM = decl(hasDescendant(RunLoopInAutorelease));
1670b57cec5SDimitry Andric
1680b57cec5SDimitry Andric auto Matches = match(GroupM, *D, AM.getASTContext());
1690b57cec5SDimitry Andric for (BoundNodes Match : Matches)
1700b57cec5SDimitry Andric emitDiagnostics(Match, D, BR, AM, Chkr);
1710b57cec5SDimitry Andric }
1720b57cec5SDimitry Andric
1730b57cec5SDimitry Andric static void
checkTempObjectsInNoPool(const Decl * D,AnalysisManager & AM,BugReporter & BR,const RunLoopAutoreleaseLeakChecker * Chkr)1740b57cec5SDimitry Andric checkTempObjectsInNoPool(const Decl *D, AnalysisManager &AM, BugReporter &BR,
1750b57cec5SDimitry Andric const RunLoopAutoreleaseLeakChecker *Chkr) {
1760b57cec5SDimitry Andric
1770b57cec5SDimitry Andric auto NoPoolM = unless(hasAncestor(autoreleasePoolStmt()));
1780b57cec5SDimitry Andric
1790b57cec5SDimitry Andric StatementMatcher RunLoopRunM = getRunLoopRunM(NoPoolM);
1800b57cec5SDimitry Andric StatementMatcher OtherMessageSentM = getOtherMessageSentM(NoPoolM);
1810b57cec5SDimitry Andric
1820b57cec5SDimitry Andric DeclarationMatcher GroupM = functionDecl(
1830b57cec5SDimitry Andric isMain(),
1840b57cec5SDimitry Andric hasDescendant(RunLoopRunM),
1850b57cec5SDimitry Andric hasDescendant(OtherMessageSentM)
1860b57cec5SDimitry Andric );
1870b57cec5SDimitry Andric
1880b57cec5SDimitry Andric auto Matches = match(GroupM, *D, AM.getASTContext());
1890b57cec5SDimitry Andric
1900b57cec5SDimitry Andric for (BoundNodes Match : Matches)
1910b57cec5SDimitry Andric emitDiagnostics(Match, D, BR, AM, Chkr);
1920b57cec5SDimitry Andric
1930b57cec5SDimitry Andric }
1940b57cec5SDimitry Andric
checkASTCodeBody(const Decl * D,AnalysisManager & AM,BugReporter & BR) const1950b57cec5SDimitry Andric void RunLoopAutoreleaseLeakChecker::checkASTCodeBody(const Decl *D,
1960b57cec5SDimitry Andric AnalysisManager &AM,
1970b57cec5SDimitry Andric BugReporter &BR) const {
1980b57cec5SDimitry Andric checkTempObjectsInSamePool(D, AM, BR, this);
1990b57cec5SDimitry Andric checkTempObjectsInNoPool(D, AM, BR, this);
2000b57cec5SDimitry Andric }
2010b57cec5SDimitry Andric
registerRunLoopAutoreleaseLeakChecker(CheckerManager & mgr)2020b57cec5SDimitry Andric void ento::registerRunLoopAutoreleaseLeakChecker(CheckerManager &mgr) {
2030b57cec5SDimitry Andric mgr.registerChecker<RunLoopAutoreleaseLeakChecker>();
2040b57cec5SDimitry Andric }
2050b57cec5SDimitry Andric
shouldRegisterRunLoopAutoreleaseLeakChecker(const CheckerManager & mgr)2065ffd83dbSDimitry Andric bool ento::shouldRegisterRunLoopAutoreleaseLeakChecker(const CheckerManager &mgr) {
2070b57cec5SDimitry Andric return true;
2080b57cec5SDimitry Andric }
209