2018-07-25 09:27:15 +08:00
|
|
|
//=- RunLoopAutoreleaseLeakChecker.cpp --------------------------*- C++ -*-==//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-07-25 09:27:15 +08:00
|
|
|
//
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// A checker for detecting leaks resulting from allocating temporary
|
|
|
|
// autoreleased objects before starting the main run loop.
|
|
|
|
//
|
|
|
|
// Checks for two antipatterns:
|
|
|
|
// 1. ObjCMessageExpr followed by [[NSRunLoop mainRunLoop] run] in the same
|
|
|
|
// autorelease pool.
|
|
|
|
// 2. ObjCMessageExpr followed by [[NSRunLoop mainRunLoop] run] in no
|
|
|
|
// autorelease pool.
|
|
|
|
//
|
|
|
|
// Any temporary objects autoreleased in code called in those expressions
|
|
|
|
// will not be deallocated until the program exits, and are effectively leaks.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
|
[analyzer][NFC] Move CheckerRegistry from the Core directory to Frontend
ClangCheckerRegistry is a very non-obvious, poorly documented, weird concept.
It derives from CheckerRegistry, and is placed in lib/StaticAnalyzer/Frontend,
whereas it's base is located in lib/StaticAnalyzer/Core. It was, from what I can
imagine, used to circumvent the problem that the registry functions of the
checkers are located in the clangStaticAnalyzerCheckers library, but that
library depends on clangStaticAnalyzerCore. However, clangStaticAnalyzerFrontend
depends on both of those libraries.
One can make the observation however, that CheckerRegistry has no place in Core,
it isn't used there at all! The only place where it is used is Frontend, which
is where it ultimately belongs.
This move implies that since
include/clang/StaticAnalyzer/Checkers/ClangCheckers.h only contained a single function:
class CheckerRegistry;
void registerBuiltinCheckers(CheckerRegistry ®istry);
it had to re purposed, as CheckerRegistry is no longer available to
clangStaticAnalyzerCheckers. It was renamed to BuiltinCheckerRegistration.h,
which actually describes it a lot better -- it does not contain the registration
functions for checkers, but only those generated by the tblgen files.
Differential Revision: https://reviews.llvm.org/D54436
llvm-svn: 349275
2018-12-16 00:23:51 +08:00
|
|
|
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
|
2018-07-25 09:27:15 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
|
|
|
#include "clang/AST/DeclObjC.h"
|
|
|
|
#include "clang/ASTMatchers/ASTMatchFinder.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace ento;
|
|
|
|
using namespace ast_matchers;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char * RunLoopBind = "NSRunLoopM";
|
|
|
|
const char * RunLoopRunBind = "RunLoopRunM";
|
|
|
|
const char * OtherMsgBind = "OtherMessageSentM";
|
|
|
|
const char * AutoreleasePoolBind = "AutoreleasePoolM";
|
2018-07-31 06:18:21 +08:00
|
|
|
const char * OtherStmtAutoreleasePoolBind = "OtherAutoreleasePoolM";
|
2018-07-25 09:27:15 +08:00
|
|
|
|
2018-07-31 05:44:15 +08:00
|
|
|
class RunLoopAutoreleaseLeakChecker : public Checker<check::ASTCodeBody> {
|
2018-07-25 09:27:15 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
void checkASTCodeBody(const Decl *D,
|
|
|
|
AnalysisManager &AM,
|
|
|
|
BugReporter &BR) const;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2018-12-11 09:14:17 +08:00
|
|
|
/// \return Whether {@code A} occurs before {@code B} in traversal of
|
|
|
|
/// {@code Parent}.
|
|
|
|
/// Conceptually a very incomplete/unsound approximation of happens-before
|
|
|
|
/// relationship (A is likely to be evaluated before B),
|
|
|
|
/// but useful enough in this case.
|
|
|
|
static bool seenBefore(const Stmt *Parent, const Stmt *A, const Stmt *B) {
|
2018-07-25 09:27:15 +08:00
|
|
|
for (const Stmt *C : Parent->children()) {
|
2018-07-31 05:44:15 +08:00
|
|
|
if (!C) continue;
|
|
|
|
|
2018-07-25 09:27:15 +08:00
|
|
|
if (C == A)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (C == B)
|
|
|
|
return false;
|
|
|
|
|
2018-12-11 09:14:17 +08:00
|
|
|
return seenBefore(C, A, B);
|
2018-07-25 09:27:15 +08:00
|
|
|
}
|
2018-12-11 09:14:17 +08:00
|
|
|
return false;
|
2018-07-25 09:27:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void emitDiagnostics(BoundNodes &Match,
|
|
|
|
const Decl *D,
|
|
|
|
BugReporter &BR,
|
|
|
|
AnalysisManager &AM,
|
|
|
|
const RunLoopAutoreleaseLeakChecker *Checker) {
|
|
|
|
|
|
|
|
assert(D->hasBody());
|
|
|
|
const Stmt *DeclBody = D->getBody();
|
|
|
|
|
|
|
|
AnalysisDeclContext *ADC = AM.getAnalysisDeclContext(D);
|
|
|
|
|
|
|
|
const auto *ME = Match.getNodeAs<ObjCMessageExpr>(OtherMsgBind);
|
|
|
|
assert(ME);
|
|
|
|
|
|
|
|
const auto *AP =
|
|
|
|
Match.getNodeAs<ObjCAutoreleasePoolStmt>(AutoreleasePoolBind);
|
2018-07-31 06:18:21 +08:00
|
|
|
const auto *OAP =
|
|
|
|
Match.getNodeAs<ObjCAutoreleasePoolStmt>(OtherStmtAutoreleasePoolBind);
|
2018-07-25 09:27:15 +08:00
|
|
|
bool HasAutoreleasePool = (AP != nullptr);
|
|
|
|
|
|
|
|
const auto *RL = Match.getNodeAs<ObjCMessageExpr>(RunLoopBind);
|
|
|
|
const auto *RLR = Match.getNodeAs<Stmt>(RunLoopRunBind);
|
|
|
|
assert(RLR && "Run loop launch not found");
|
|
|
|
assert(ME != RLR);
|
2018-07-31 06:18:21 +08:00
|
|
|
|
|
|
|
// Launch of run loop occurs before the message-sent expression is seen.
|
|
|
|
if (seenBefore(DeclBody, RLR, ME))
|
2018-07-25 09:27:15 +08:00
|
|
|
return;
|
|
|
|
|
2018-07-31 06:18:21 +08:00
|
|
|
if (HasAutoreleasePool && (OAP != AP))
|
2018-07-25 09:27:15 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
PathDiagnosticLocation Location = PathDiagnosticLocation::createBegin(
|
|
|
|
ME, BR.getSourceManager(), ADC);
|
|
|
|
SourceRange Range = ME->getSourceRange();
|
|
|
|
|
|
|
|
BR.EmitBasicReport(ADC->getDecl(), Checker,
|
|
|
|
/*Name=*/"Memory leak inside autorelease pool",
|
|
|
|
/*Category=*/"Memory",
|
|
|
|
/*Name=*/
|
|
|
|
(Twine("Temporary objects allocated in the") +
|
|
|
|
" autorelease pool " +
|
|
|
|
(HasAutoreleasePool ? "" : "of last resort ") +
|
|
|
|
"followed by the launch of " +
|
|
|
|
(RL ? "main run loop " : "xpc_main ") +
|
|
|
|
"may never get released; consider moving them to a "
|
|
|
|
"separate autorelease pool")
|
|
|
|
.str(),
|
|
|
|
Location, Range);
|
|
|
|
}
|
|
|
|
|
|
|
|
static StatementMatcher getRunLoopRunM(StatementMatcher Extra = anything()) {
|
|
|
|
StatementMatcher MainRunLoopM =
|
|
|
|
objcMessageExpr(hasSelector("mainRunLoop"),
|
|
|
|
hasReceiverType(asString("NSRunLoop")),
|
|
|
|
Extra)
|
|
|
|
.bind(RunLoopBind);
|
|
|
|
|
|
|
|
StatementMatcher MainRunLoopRunM = objcMessageExpr(hasSelector("run"),
|
|
|
|
hasReceiver(MainRunLoopM),
|
|
|
|
Extra).bind(RunLoopRunBind);
|
|
|
|
|
|
|
|
StatementMatcher XPCRunM =
|
|
|
|
callExpr(callee(functionDecl(hasName("xpc_main")))).bind(RunLoopRunBind);
|
|
|
|
return anyOf(MainRunLoopRunM, XPCRunM);
|
|
|
|
}
|
|
|
|
|
|
|
|
static StatementMatcher getOtherMessageSentM(StatementMatcher Extra = anything()) {
|
|
|
|
return objcMessageExpr(unless(anyOf(equalsBoundNode(RunLoopBind),
|
|
|
|
equalsBoundNode(RunLoopRunBind))),
|
|
|
|
Extra)
|
|
|
|
.bind(OtherMsgBind);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
checkTempObjectsInSamePool(const Decl *D, AnalysisManager &AM, BugReporter &BR,
|
|
|
|
const RunLoopAutoreleaseLeakChecker *Chkr) {
|
|
|
|
StatementMatcher RunLoopRunM = getRunLoopRunM();
|
2018-07-31 06:18:21 +08:00
|
|
|
StatementMatcher OtherMessageSentM = getOtherMessageSentM(
|
|
|
|
hasAncestor(autoreleasePoolStmt().bind(OtherStmtAutoreleasePoolBind)));
|
2018-07-25 09:27:15 +08:00
|
|
|
|
|
|
|
StatementMatcher RunLoopInAutorelease =
|
|
|
|
autoreleasePoolStmt(
|
|
|
|
hasDescendant(RunLoopRunM),
|
|
|
|
hasDescendant(OtherMessageSentM)).bind(AutoreleasePoolBind);
|
|
|
|
|
|
|
|
DeclarationMatcher GroupM = decl(hasDescendant(RunLoopInAutorelease));
|
|
|
|
|
|
|
|
auto Matches = match(GroupM, *D, AM.getASTContext());
|
|
|
|
for (BoundNodes Match : Matches)
|
|
|
|
emitDiagnostics(Match, D, BR, AM, Chkr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
checkTempObjectsInNoPool(const Decl *D, AnalysisManager &AM, BugReporter &BR,
|
|
|
|
const RunLoopAutoreleaseLeakChecker *Chkr) {
|
|
|
|
|
|
|
|
auto NoPoolM = unless(hasAncestor(autoreleasePoolStmt()));
|
|
|
|
|
|
|
|
StatementMatcher RunLoopRunM = getRunLoopRunM(NoPoolM);
|
|
|
|
StatementMatcher OtherMessageSentM = getOtherMessageSentM(NoPoolM);
|
|
|
|
|
|
|
|
DeclarationMatcher GroupM = functionDecl(
|
|
|
|
isMain(),
|
|
|
|
hasDescendant(RunLoopRunM),
|
|
|
|
hasDescendant(OtherMessageSentM)
|
|
|
|
);
|
|
|
|
|
|
|
|
auto Matches = match(GroupM, *D, AM.getASTContext());
|
|
|
|
|
|
|
|
for (BoundNodes Match : Matches)
|
|
|
|
emitDiagnostics(Match, D, BR, AM, Chkr);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void RunLoopAutoreleaseLeakChecker::checkASTCodeBody(const Decl *D,
|
|
|
|
AnalysisManager &AM,
|
|
|
|
BugReporter &BR) const {
|
|
|
|
checkTempObjectsInSamePool(D, AM, BR, this);
|
|
|
|
checkTempObjectsInNoPool(D, AM, BR, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ento::registerRunLoopAutoreleaseLeakChecker(CheckerManager &mgr) {
|
|
|
|
mgr.registerChecker<RunLoopAutoreleaseLeakChecker>();
|
|
|
|
}
|
2019-01-26 22:23:08 +08:00
|
|
|
|
|
|
|
bool ento::shouldRegisterRunLoopAutoreleaseLeakChecker(const LangOptions &LO) {
|
|
|
|
return true;
|
|
|
|
}
|