2011-02-26 06:00:43 +08:00
|
|
|
//=== StackAddrEscapeChecker.cpp ----------------------------------*- C++ -*--//
|
2010-06-08 18:00:00 +08:00
|
|
|
//
|
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
|
2010-06-08 18:00:00 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2015-09-08 11:50:52 +08:00
|
|
|
// This file defines stack address leak checker, which checks if an invalid
|
2010-06-08 18:00:00 +08:00
|
|
|
// stack address is stored into a global or heap location. See CERT DCL30-C.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
[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"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/AST/ExprCXX.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
|
2011-03-01 09:16:21 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/Checker.h"
|
2011-02-18 05:39:17 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
2017-11-21 06:53:30 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
|
2011-02-24 05:04:54 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
2011-08-16 06:09:50 +08:00
|
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
|
2010-06-09 14:08:24 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2012-12-02 01:12:56 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-06-08 18:00:00 +08:00
|
|
|
using namespace clang;
|
2010-12-23 15:20:52 +08:00
|
|
|
using namespace ento;
|
2010-06-08 18:00:00 +08:00
|
|
|
|
|
|
|
namespace {
|
2017-11-21 06:53:30 +08:00
|
|
|
class StackAddrEscapeChecker
|
|
|
|
: public Checker<check::PreCall, check::PreStmt<ReturnStmt>,
|
|
|
|
check::EndFunction> {
|
|
|
|
mutable IdentifierInfo *dispatch_semaphore_tII;
|
2014-03-08 04:03:18 +08:00
|
|
|
mutable std::unique_ptr<BuiltinBug> BT_stackleak;
|
|
|
|
mutable std::unique_ptr<BuiltinBug> BT_returnstack;
|
2017-11-21 06:53:30 +08:00
|
|
|
mutable std::unique_ptr<BuiltinBug> BT_capturedstackasync;
|
|
|
|
mutable std::unique_ptr<BuiltinBug> BT_capturedstackret;
|
2010-06-08 18:00:00 +08:00
|
|
|
|
|
|
|
public:
|
2017-12-12 10:59:09 +08:00
|
|
|
enum CheckKind {
|
|
|
|
CK_StackAddrEscapeChecker,
|
|
|
|
CK_StackAddrAsyncEscapeChecker,
|
|
|
|
CK_NumCheckKinds
|
|
|
|
};
|
|
|
|
|
|
|
|
DefaultBool ChecksEnabled[CK_NumCheckKinds];
|
|
|
|
|
2017-11-21 06:53:30 +08:00
|
|
|
void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
|
2011-02-24 05:04:54 +08:00
|
|
|
void checkPreStmt(const ReturnStmt *RS, CheckerContext &C) const;
|
2018-07-17 04:47:45 +08:00
|
|
|
void checkEndFunction(const ReturnStmt *RS, CheckerContext &Ctx) const;
|
2017-11-21 06:53:30 +08:00
|
|
|
|
2010-06-09 14:08:24 +08:00
|
|
|
private:
|
2017-11-21 06:53:30 +08:00
|
|
|
void checkReturnedBlockCaptures(const BlockDataRegion &B,
|
|
|
|
CheckerContext &C) const;
|
|
|
|
void checkAsyncExecutedBlockCaptures(const BlockDataRegion &B,
|
|
|
|
CheckerContext &C) const;
|
2011-02-24 05:04:54 +08:00
|
|
|
void EmitStackError(CheckerContext &C, const MemRegion *R,
|
|
|
|
const Expr *RetE) const;
|
2017-11-21 06:53:30 +08:00
|
|
|
bool isSemaphoreCaptured(const BlockDecl &B) const;
|
2013-02-26 09:21:21 +08:00
|
|
|
static SourceRange genName(raw_ostream &os, const MemRegion *R,
|
|
|
|
ASTContext &Ctx);
|
2017-11-21 06:53:30 +08:00
|
|
|
static SmallVector<const MemRegion *, 4>
|
|
|
|
getCapturedStackRegions(const BlockDataRegion &B, CheckerContext &C);
|
|
|
|
static bool isArcManagedBlock(const MemRegion *R, CheckerContext &C);
|
|
|
|
static bool isNotInCurrentFrame(const MemRegion *R, CheckerContext &C);
|
2010-06-08 18:00:00 +08:00
|
|
|
};
|
2017-11-21 06:53:30 +08:00
|
|
|
} // namespace
|
2010-06-08 18:00:00 +08:00
|
|
|
|
2013-02-26 09:21:21 +08:00
|
|
|
SourceRange StackAddrEscapeChecker::genName(raw_ostream &os, const MemRegion *R,
|
|
|
|
ASTContext &Ctx) {
|
2017-11-21 06:53:30 +08:00
|
|
|
// Get the base region, stripping away fields and elements.
|
2010-06-09 14:08:24 +08:00
|
|
|
R = R->getBaseRegion();
|
2013-02-26 09:21:21 +08:00
|
|
|
SourceManager &SM = Ctx.getSourceManager();
|
2010-06-17 12:21:37 +08:00
|
|
|
SourceRange range;
|
|
|
|
os << "Address of ";
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2010-06-09 14:08:24 +08:00
|
|
|
// Check if the region is a compound literal.
|
2017-11-21 06:53:30 +08:00
|
|
|
if (const auto *CR = dyn_cast<CompoundLiteralRegion>(R)) {
|
2011-08-13 07:37:29 +08:00
|
|
|
const CompoundLiteralExpr *CL = CR->getLiteralExpr();
|
2010-06-17 12:21:37 +08:00
|
|
|
os << "stack memory associated with a compound literal "
|
|
|
|
"declared on line "
|
2018-08-10 05:08:08 +08:00
|
|
|
<< SM.getExpansionLineNumber(CL->getBeginLoc()) << " returned to caller";
|
2010-06-09 14:08:24 +08:00
|
|
|
range = CL->getSourceRange();
|
2017-11-21 06:53:30 +08:00
|
|
|
} else if (const auto *AR = dyn_cast<AllocaRegion>(R)) {
|
2011-08-13 07:37:29 +08:00
|
|
|
const Expr *ARE = AR->getExpr();
|
2018-08-10 05:08:08 +08:00
|
|
|
SourceLocation L = ARE->getBeginLoc();
|
2015-09-08 11:50:52 +08:00
|
|
|
range = ARE->getSourceRange();
|
2010-06-17 12:21:37 +08:00
|
|
|
os << "stack memory allocated by call to alloca() on line "
|
2011-07-26 05:09:52 +08:00
|
|
|
<< SM.getExpansionLineNumber(L);
|
2017-11-21 06:53:30 +08:00
|
|
|
} else if (const auto *BR = dyn_cast<BlockDataRegion>(R)) {
|
2010-06-09 14:08:24 +08:00
|
|
|
const BlockDecl *BD = BR->getCodeRegion()->getDecl();
|
2018-08-10 05:08:08 +08:00
|
|
|
SourceLocation L = BD->getBeginLoc();
|
2010-06-09 14:08:24 +08:00
|
|
|
range = BD->getSourceRange();
|
2010-06-17 12:21:37 +08:00
|
|
|
os << "stack-allocated block declared on line "
|
2011-07-26 05:09:52 +08:00
|
|
|
<< SM.getExpansionLineNumber(L);
|
2017-11-21 06:53:30 +08:00
|
|
|
} else if (const auto *VR = dyn_cast<VarRegion>(R)) {
|
|
|
|
os << "stack memory associated with local variable '" << VR->getString()
|
|
|
|
<< '\'';
|
2010-06-09 14:08:24 +08:00
|
|
|
range = VR->getDecl()->getSourceRange();
|
2017-11-21 06:53:30 +08:00
|
|
|
} else if (const auto *TOR = dyn_cast<CXXTempObjectRegion>(R)) {
|
2013-02-26 09:21:21 +08:00
|
|
|
QualType Ty = TOR->getValueType().getLocalUnqualifiedType();
|
|
|
|
os << "stack memory associated with temporary object of type '";
|
|
|
|
Ty.print(os, Ctx.getPrintingPolicy());
|
|
|
|
os << "'";
|
2011-08-26 08:41:31 +08:00
|
|
|
range = TOR->getExpr()->getSourceRange();
|
2017-11-21 06:53:30 +08:00
|
|
|
} else {
|
2011-09-23 13:06:16 +08:00
|
|
|
llvm_unreachable("Invalid region in ReturnStackAddressChecker.");
|
2015-09-08 11:50:52 +08:00
|
|
|
}
|
|
|
|
|
2010-06-17 12:21:37 +08:00
|
|
|
return range;
|
|
|
|
}
|
|
|
|
|
2017-11-21 06:53:30 +08:00
|
|
|
bool StackAddrEscapeChecker::isArcManagedBlock(const MemRegion *R,
|
|
|
|
CheckerContext &C) {
|
|
|
|
assert(R && "MemRegion should not be null");
|
|
|
|
return C.getASTContext().getLangOpts().ObjCAutoRefCount &&
|
|
|
|
isa<BlockDataRegion>(R);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StackAddrEscapeChecker::isNotInCurrentFrame(const MemRegion *R,
|
|
|
|
CheckerContext &C) {
|
|
|
|
const StackSpaceRegion *S = cast<StackSpaceRegion>(R->getMemorySpace());
|
2018-06-27 09:51:55 +08:00
|
|
|
return S->getStackFrame() != C.getStackFrame();
|
2017-11-21 06:53:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StackAddrEscapeChecker::isSemaphoreCaptured(const BlockDecl &B) const {
|
|
|
|
if (!dispatch_semaphore_tII)
|
|
|
|
dispatch_semaphore_tII = &B.getASTContext().Idents.get("dispatch_semaphore_t");
|
|
|
|
for (const auto &C : B.captures()) {
|
|
|
|
const auto *T = C.getVariable()->getType()->getAs<TypedefType>();
|
|
|
|
if (T && T->getDecl()->getIdentifier() == dispatch_semaphore_tII)
|
2018-07-31 03:24:48 +08:00
|
|
|
return true;
|
2017-11-21 06:53:30 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<const MemRegion *, 4>
|
|
|
|
StackAddrEscapeChecker::getCapturedStackRegions(const BlockDataRegion &B,
|
|
|
|
CheckerContext &C) {
|
|
|
|
SmallVector<const MemRegion *, 4> Regions;
|
|
|
|
BlockDataRegion::referenced_vars_iterator I = B.referenced_vars_begin();
|
|
|
|
BlockDataRegion::referenced_vars_iterator E = B.referenced_vars_end();
|
|
|
|
for (; I != E; ++I) {
|
|
|
|
SVal Val = C.getState()->getSVal(I.getCapturedRegion());
|
|
|
|
const MemRegion *Region = Val.getAsRegion();
|
|
|
|
if (Region && isa<StackSpaceRegion>(Region->getMemorySpace()))
|
|
|
|
Regions.push_back(Region);
|
|
|
|
}
|
|
|
|
return Regions;
|
|
|
|
}
|
2010-06-17 12:21:37 +08:00
|
|
|
|
2017-11-21 06:53:30 +08:00
|
|
|
void StackAddrEscapeChecker::EmitStackError(CheckerContext &C,
|
|
|
|
const MemRegion *R,
|
|
|
|
const Expr *RetE) const {
|
|
|
|
ExplodedNode *N = C.generateNonFatalErrorNode();
|
2010-06-17 12:21:37 +08:00
|
|
|
if (!N)
|
2010-06-09 14:08:24 +08:00
|
|
|
return;
|
2010-06-17 12:21:37 +08:00
|
|
|
if (!BT_returnstack)
|
2017-11-21 06:53:30 +08:00
|
|
|
BT_returnstack = llvm::make_unique<BuiltinBug>(
|
|
|
|
this, "Return of address to stack-allocated memory");
|
2010-06-17 12:21:37 +08:00
|
|
|
// Generate a report for this bug.
|
2017-11-21 06:53:30 +08:00
|
|
|
SmallString<128> buf;
|
2010-06-17 12:21:37 +08:00
|
|
|
llvm::raw_svector_ostream os(buf);
|
2013-02-26 09:21:21 +08:00
|
|
|
SourceRange range = genName(os, R, C.getASTContext());
|
2010-06-17 12:21:37 +08:00
|
|
|
os << " returned to caller";
|
2015-06-23 21:15:32 +08:00
|
|
|
auto report = llvm::make_unique<BugReport>(*BT_returnstack, os.str(), N);
|
2010-06-09 14:08:24 +08:00
|
|
|
report->addRange(RetE->getSourceRange());
|
|
|
|
if (range.isValid())
|
|
|
|
report->addRange(range);
|
2015-06-23 21:15:32 +08:00
|
|
|
C.emitReport(std::move(report));
|
2010-08-22 09:00:03 +08:00
|
|
|
}
|
2010-06-09 14:08:24 +08:00
|
|
|
|
2017-11-21 06:53:30 +08:00
|
|
|
void StackAddrEscapeChecker::checkAsyncExecutedBlockCaptures(
|
|
|
|
const BlockDataRegion &B, CheckerContext &C) const {
|
|
|
|
// There is a not-too-uncommon idiom
|
|
|
|
// where a block passed to dispatch_async captures a semaphore
|
|
|
|
// and then the thread (which called dispatch_async) is blocked on waiting
|
2018-07-31 03:24:48 +08:00
|
|
|
// for the completion of the execution of the block
|
|
|
|
// via dispatch_semaphore_wait. To avoid false-positives (for now)
|
|
|
|
// we ignore all the blocks which have captured
|
2017-11-21 06:53:30 +08:00
|
|
|
// a variable of the type "dispatch_semaphore_t".
|
|
|
|
if (isSemaphoreCaptured(*B.getDecl()))
|
|
|
|
return;
|
|
|
|
for (const MemRegion *Region : getCapturedStackRegions(B, C)) {
|
|
|
|
// The block passed to dispatch_async may capture another block
|
|
|
|
// created on the stack. However, there is no leak in this situaton,
|
|
|
|
// no matter if ARC or no ARC is enabled:
|
|
|
|
// dispatch_async copies the passed "outer" block (via Block_copy)
|
|
|
|
// and if the block has captured another "inner" block,
|
|
|
|
// the "inner" block will be copied as well.
|
|
|
|
if (isa<BlockDataRegion>(Region))
|
|
|
|
continue;
|
|
|
|
ExplodedNode *N = C.generateNonFatalErrorNode();
|
|
|
|
if (!N)
|
|
|
|
continue;
|
|
|
|
if (!BT_capturedstackasync)
|
|
|
|
BT_capturedstackasync = llvm::make_unique<BuiltinBug>(
|
|
|
|
this, "Address of stack-allocated memory is captured");
|
|
|
|
SmallString<128> Buf;
|
|
|
|
llvm::raw_svector_ostream Out(Buf);
|
|
|
|
SourceRange Range = genName(Out, Region, C.getASTContext());
|
|
|
|
Out << " is captured by an asynchronously-executed block";
|
|
|
|
auto Report =
|
|
|
|
llvm::make_unique<BugReport>(*BT_capturedstackasync, Out.str(), N);
|
|
|
|
if (Range.isValid())
|
|
|
|
Report->addRange(Range);
|
|
|
|
C.emitReport(std::move(Report));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackAddrEscapeChecker::checkReturnedBlockCaptures(
|
|
|
|
const BlockDataRegion &B, CheckerContext &C) const {
|
|
|
|
for (const MemRegion *Region : getCapturedStackRegions(B, C)) {
|
|
|
|
if (isArcManagedBlock(Region, C) || isNotInCurrentFrame(Region, C))
|
|
|
|
continue;
|
|
|
|
ExplodedNode *N = C.generateNonFatalErrorNode();
|
|
|
|
if (!N)
|
|
|
|
continue;
|
|
|
|
if (!BT_capturedstackret)
|
|
|
|
BT_capturedstackret = llvm::make_unique<BuiltinBug>(
|
|
|
|
this, "Address of stack-allocated memory is captured");
|
|
|
|
SmallString<128> Buf;
|
|
|
|
llvm::raw_svector_ostream Out(Buf);
|
|
|
|
SourceRange Range = genName(Out, Region, C.getASTContext());
|
|
|
|
Out << " is captured by a returned block";
|
|
|
|
auto Report =
|
|
|
|
llvm::make_unique<BugReport>(*BT_capturedstackret, Out.str(), N);
|
|
|
|
if (Range.isValid())
|
|
|
|
Report->addRange(Range);
|
|
|
|
C.emitReport(std::move(Report));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StackAddrEscapeChecker::checkPreCall(const CallEvent &Call,
|
|
|
|
CheckerContext &C) const {
|
2017-12-12 10:59:09 +08:00
|
|
|
if (!ChecksEnabled[CK_StackAddrAsyncEscapeChecker])
|
|
|
|
return;
|
2017-11-21 06:53:30 +08:00
|
|
|
if (!Call.isGlobalCFunction("dispatch_after") &&
|
|
|
|
!Call.isGlobalCFunction("dispatch_async"))
|
|
|
|
return;
|
|
|
|
for (unsigned Idx = 0, NumArgs = Call.getNumArgs(); Idx < NumArgs; ++Idx) {
|
|
|
|
if (const BlockDataRegion *B = dyn_cast_or_null<BlockDataRegion>(
|
|
|
|
Call.getArgSVal(Idx).getAsRegion()))
|
|
|
|
checkAsyncExecutedBlockCaptures(*B, C);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-26 06:00:43 +08:00
|
|
|
void StackAddrEscapeChecker::checkPreStmt(const ReturnStmt *RS,
|
2012-03-03 09:22:03 +08:00
|
|
|
CheckerContext &C) const {
|
2017-12-12 10:59:09 +08:00
|
|
|
if (!ChecksEnabled[CK_StackAddrEscapeChecker])
|
|
|
|
return;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2010-06-09 14:08:24 +08:00
|
|
|
const Expr *RetE = RS->getRetValue();
|
|
|
|
if (!RetE)
|
|
|
|
return;
|
2012-08-29 09:11:59 +08:00
|
|
|
RetE = RetE->IgnoreParens();
|
2012-08-28 01:50:07 +08:00
|
|
|
|
2018-01-18 04:27:29 +08:00
|
|
|
SVal V = C.getSVal(RetE);
|
2010-06-09 14:08:24 +08:00
|
|
|
const MemRegion *R = V.getAsRegion();
|
2012-03-03 09:22:03 +08:00
|
|
|
if (!R)
|
|
|
|
return;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2017-11-21 06:53:30 +08:00
|
|
|
if (const BlockDataRegion *B = dyn_cast<BlockDataRegion>(R))
|
|
|
|
checkReturnedBlockCaptures(*B, C);
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
if (!isa<StackSpaceRegion>(R->getMemorySpace()) ||
|
2017-11-21 06:53:30 +08:00
|
|
|
isNotInCurrentFrame(R, C) || isArcManagedBlock(R, C))
|
2012-03-03 09:22:03 +08:00
|
|
|
return;
|
|
|
|
|
2012-08-28 01:50:07 +08:00
|
|
|
// Returning a record by value is fine. (In this case, the returned
|
2012-08-29 09:11:59 +08:00
|
|
|
// expression will be a copy-constructor, possibly wrapped in an
|
|
|
|
// ExprWithCleanups node.)
|
|
|
|
if (const ExprWithCleanups *Cleanup = dyn_cast<ExprWithCleanups>(RetE))
|
|
|
|
RetE = Cleanup->getSubExpr();
|
2012-08-28 01:50:07 +08:00
|
|
|
if (isa<CXXConstructExpr>(RetE) && RetE->getType()->isRecordType())
|
|
|
|
return;
|
|
|
|
|
2015-12-04 03:41:24 +08:00
|
|
|
// The CK_CopyAndAutoreleaseBlockObject cast causes the block to be copied
|
|
|
|
// so the stack address is not escaping here.
|
|
|
|
if (auto *ICE = dyn_cast<ImplicitCastExpr>(RetE)) {
|
|
|
|
if (isa<BlockDataRegion>(R) &&
|
|
|
|
ICE->getCastKind() == CK_CopyAndAutoreleaseBlockObject) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-03 09:22:03 +08:00
|
|
|
EmitStackError(C, R, RetE);
|
2010-06-09 14:08:24 +08:00
|
|
|
}
|
2010-06-08 18:00:00 +08:00
|
|
|
|
2018-07-17 04:47:45 +08:00
|
|
|
void StackAddrEscapeChecker::checkEndFunction(const ReturnStmt *RS,
|
|
|
|
CheckerContext &Ctx) const {
|
2017-12-12 10:59:09 +08:00
|
|
|
if (!ChecksEnabled[CK_StackAddrEscapeChecker])
|
|
|
|
return;
|
|
|
|
|
2017-11-21 06:53:30 +08:00
|
|
|
ProgramStateRef State = Ctx.getState();
|
2010-06-17 08:24:44 +08:00
|
|
|
|
|
|
|
// Iterate over all bindings to global variables and see if it contains
|
|
|
|
// a memory region in the stack space.
|
|
|
|
class CallBack : public StoreManager::BindingsHandler {
|
|
|
|
private:
|
2011-10-26 03:56:48 +08:00
|
|
|
CheckerContext &Ctx;
|
2010-06-17 08:24:44 +08:00
|
|
|
const StackFrameContext *CurSFC;
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2017-11-21 06:53:30 +08:00
|
|
|
public:
|
|
|
|
SmallVector<std::pair<const MemRegion *, const MemRegion *>, 10> V;
|
2014-03-15 12:29:04 +08:00
|
|
|
|
2018-06-27 09:51:55 +08:00
|
|
|
CallBack(CheckerContext &CC) : Ctx(CC), CurSFC(CC.getStackFrame()) {}
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2017-11-21 06:53:30 +08:00
|
|
|
bool HandleBinding(StoreManager &SMgr, Store S, const MemRegion *Region,
|
|
|
|
SVal Val) override {
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2017-11-21 06:53:30 +08:00
|
|
|
if (!isa<GlobalsSpaceRegion>(Region->getMemorySpace()))
|
2011-06-16 07:02:42 +08:00
|
|
|
return true;
|
2017-11-21 06:53:30 +08:00
|
|
|
const MemRegion *VR = Val.getAsRegion();
|
|
|
|
if (VR && isa<StackSpaceRegion>(VR->getMemorySpace()) &&
|
|
|
|
!isArcManagedBlock(VR, Ctx) && !isNotInCurrentFrame(VR, Ctx))
|
|
|
|
V.emplace_back(Region, VR);
|
2010-06-17 08:24:44 +08:00
|
|
|
return true;
|
2010-06-08 18:00:00 +08:00
|
|
|
}
|
2010-06-17 08:24:44 +08:00
|
|
|
};
|
2015-09-08 11:50:52 +08:00
|
|
|
|
2017-11-21 06:53:30 +08:00
|
|
|
CallBack Cb(Ctx);
|
|
|
|
State->getStateManager().getStoreManager().iterBindings(State->getStore(),
|
|
|
|
Cb);
|
2010-06-17 12:21:37 +08:00
|
|
|
|
2017-11-21 06:53:30 +08:00
|
|
|
if (Cb.V.empty())
|
2010-06-17 08:24:44 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Generate an error node.
|
2017-11-21 06:53:30 +08:00
|
|
|
ExplodedNode *N = Ctx.generateNonFatalErrorNode(State);
|
2010-06-17 08:24:44 +08:00
|
|
|
if (!N)
|
|
|
|
return;
|
2010-06-17 12:21:37 +08:00
|
|
|
|
2010-06-17 08:24:44 +08:00
|
|
|
if (!BT_stackleak)
|
2017-11-21 06:53:30 +08:00
|
|
|
BT_stackleak = llvm::make_unique<BuiltinBug>(
|
|
|
|
this, "Stack address stored into global variable",
|
|
|
|
"Stack address was saved into a global variable. "
|
|
|
|
"This is dangerous because the address will become "
|
|
|
|
"invalid after returning from the function");
|
2014-02-12 05:49:21 +08:00
|
|
|
|
2017-11-21 06:53:30 +08:00
|
|
|
for (const auto &P : Cb.V) {
|
2010-06-17 12:21:37 +08:00
|
|
|
// Generate a report for this bug.
|
2017-11-21 06:53:30 +08:00
|
|
|
SmallString<128> Buf;
|
|
|
|
llvm::raw_svector_ostream Out(Buf);
|
|
|
|
SourceRange Range = genName(Out, P.second, Ctx.getASTContext());
|
|
|
|
Out << " is still referred to by the ";
|
|
|
|
if (isa<StaticGlobalSpaceRegion>(P.first->getMemorySpace()))
|
|
|
|
Out << "static";
|
2016-05-26 22:02:17 +08:00
|
|
|
else
|
2017-11-21 06:53:30 +08:00
|
|
|
Out << "global";
|
|
|
|
Out << " variable '";
|
|
|
|
const VarRegion *VR = cast<VarRegion>(P.first->getBaseRegion());
|
|
|
|
Out << *VR->getDecl()
|
|
|
|
<< "' upon returning to the caller. This will be a dangling reference";
|
|
|
|
auto Report = llvm::make_unique<BugReport>(*BT_stackleak, Out.str(), N);
|
|
|
|
if (Range.isValid())
|
|
|
|
Report->addRange(Range);
|
|
|
|
|
|
|
|
Ctx.emitReport(std::move(Report));
|
2010-06-17 12:21:37 +08:00
|
|
|
}
|
2010-06-08 18:00:00 +08:00
|
|
|
}
|
2011-02-24 05:04:54 +08:00
|
|
|
|
2017-12-12 10:59:09 +08:00
|
|
|
#define REGISTER_CHECKER(name) \
|
|
|
|
void ento::register##name(CheckerManager &Mgr) { \
|
|
|
|
StackAddrEscapeChecker *Chk = \
|
|
|
|
Mgr.registerChecker<StackAddrEscapeChecker>(); \
|
|
|
|
Chk->ChecksEnabled[StackAddrEscapeChecker::CK_##name] = true; \
|
2019-01-26 22:23:08 +08:00
|
|
|
} \
|
|
|
|
\
|
|
|
|
bool ento::shouldRegister##name(const LangOptions &LO) { \
|
|
|
|
return true; \
|
2017-12-12 10:59:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
REGISTER_CHECKER(StackAddrEscapeChecker)
|
|
|
|
REGISTER_CHECKER(StackAddrAsyncEscapeChecker)
|