blob: 86936cadd5522fbfdb790682d70d21dac7cd314b [file] [log] [blame]
Ted Kremenek5eb4b602011-02-25 22:00:43 +00001//=== StackAddrEscapeChecker.cpp ----------------------------------*- C++ -*--//
Zhongxing Xu87e7fc52010-06-08 10:00:00 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Ted Kremenek3a0678e2015-09-08 03:50:52 +000010// This file defines stack address leak checker, which checks if an invalid
Zhongxing Xu87e7fc52010-06-08 10:00:00 +000011// stack address is stored into a global or heap location. See CERT DCL30-C.
12//
13//===----------------------------------------------------------------------===//
14
Argyrios Kyrtzidisa6d04d52011-02-15 07:42:33 +000015#include "ClangSACheckers.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/AST/ExprCXX.h"
17#include "clang/Basic/SourceManager.h"
18#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000019#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000020#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +000021#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
Argyrios Kyrtzidis506220f2011-02-23 21:04:54 +000022#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek001fd5b2011-08-15 22:09:50 +000023#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
Zhongxing Xu1225aac2010-06-09 06:08:24 +000024#include "llvm/ADT/SmallString.h"
Benjamin Kramer444a1302012-12-01 17:12:56 +000025#include "llvm/Support/raw_ostream.h"
Zhongxing Xu87e7fc52010-06-08 10:00:00 +000026using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000027using namespace ento;
Zhongxing Xu87e7fc52010-06-08 10:00:00 +000028
29namespace {
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +000030class StackAddrEscapeChecker
31 : public Checker<check::PreCall, check::PreStmt<ReturnStmt>,
32 check::EndFunction> {
33 mutable IdentifierInfo *dispatch_semaphore_tII;
Ahmed Charlesb8984322014-03-07 20:03:18 +000034 mutable std::unique_ptr<BuiltinBug> BT_stackleak;
35 mutable std::unique_ptr<BuiltinBug> BT_returnstack;
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +000036 mutable std::unique_ptr<BuiltinBug> BT_capturedstackasync;
37 mutable std::unique_ptr<BuiltinBug> BT_capturedstackret;
Zhongxing Xu87e7fc52010-06-08 10:00:00 +000038
39public:
Artem Dergacheve67a5752017-12-12 02:59:09 +000040 enum CheckKind {
41 CK_StackAddrEscapeChecker,
42 CK_StackAddrAsyncEscapeChecker,
43 CK_NumCheckKinds
44 };
45
46 DefaultBool ChecksEnabled[CK_NumCheckKinds];
47
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +000048 void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
Argyrios Kyrtzidis506220f2011-02-23 21:04:54 +000049 void checkPreStmt(const ReturnStmt *RS, CheckerContext &C) const;
Anna Zaks3fdcc0b2013-01-03 00:25:29 +000050 void checkEndFunction(CheckerContext &Ctx) const;
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +000051
Zhongxing Xu1225aac2010-06-09 06:08:24 +000052private:
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +000053 void checkReturnedBlockCaptures(const BlockDataRegion &B,
54 CheckerContext &C) const;
55 void checkAsyncExecutedBlockCaptures(const BlockDataRegion &B,
56 CheckerContext &C) const;
Argyrios Kyrtzidis506220f2011-02-23 21:04:54 +000057 void EmitStackError(CheckerContext &C, const MemRegion *R,
58 const Expr *RetE) const;
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +000059 bool isSemaphoreCaptured(const BlockDecl &B) const;
Jordan Rosec9487092013-02-26 01:21:21 +000060 static SourceRange genName(raw_ostream &os, const MemRegion *R,
61 ASTContext &Ctx);
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +000062 static SmallVector<const MemRegion *, 4>
63 getCapturedStackRegions(const BlockDataRegion &B, CheckerContext &C);
64 static bool isArcManagedBlock(const MemRegion *R, CheckerContext &C);
65 static bool isNotInCurrentFrame(const MemRegion *R, CheckerContext &C);
Zhongxing Xu87e7fc52010-06-08 10:00:00 +000066};
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +000067} // namespace
Zhongxing Xu87e7fc52010-06-08 10:00:00 +000068
Jordan Rosec9487092013-02-26 01:21:21 +000069SourceRange StackAddrEscapeChecker::genName(raw_ostream &os, const MemRegion *R,
70 ASTContext &Ctx) {
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +000071 // Get the base region, stripping away fields and elements.
Ted Kremenek5df037e2010-06-17 04:21:37 +000072 R = R->getBaseRegion();
Jordan Rosec9487092013-02-26 01:21:21 +000073 SourceManager &SM = Ctx.getSourceManager();
Ted Kremenek5df037e2010-06-17 04:21:37 +000074 SourceRange range;
75 os << "Address of ";
Ted Kremenek3a0678e2015-09-08 03:50:52 +000076
Ted Kremenek5df037e2010-06-17 04:21:37 +000077 // Check if the region is a compound literal.
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +000078 if (const auto *CR = dyn_cast<CompoundLiteralRegion>(R)) {
Ted Kremenek5ef32db2011-08-12 23:37:29 +000079 const CompoundLiteralExpr *CL = CR->getLiteralExpr();
Ted Kremenek5df037e2010-06-17 04:21:37 +000080 os << "stack memory associated with a compound literal "
81 "declared on line "
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +000082 << SM.getExpansionLineNumber(CL->getLocStart()) << " returned to caller";
Ted Kremenek5df037e2010-06-17 04:21:37 +000083 range = CL->getSourceRange();
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +000084 } else if (const auto *AR = dyn_cast<AllocaRegion>(R)) {
Ted Kremenek5ef32db2011-08-12 23:37:29 +000085 const Expr *ARE = AR->getExpr();
Ted Kremenek5df037e2010-06-17 04:21:37 +000086 SourceLocation L = ARE->getLocStart();
Ted Kremenek3a0678e2015-09-08 03:50:52 +000087 range = ARE->getSourceRange();
Ted Kremenek5df037e2010-06-17 04:21:37 +000088 os << "stack memory allocated by call to alloca() on line "
Chandler Carruthd48db212011-07-25 21:09:52 +000089 << SM.getExpansionLineNumber(L);
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +000090 } else if (const auto *BR = dyn_cast<BlockDataRegion>(R)) {
Ted Kremenek5df037e2010-06-17 04:21:37 +000091 const BlockDecl *BD = BR->getCodeRegion()->getDecl();
92 SourceLocation L = BD->getLocStart();
93 range = BD->getSourceRange();
94 os << "stack-allocated block declared on line "
Chandler Carruthd48db212011-07-25 21:09:52 +000095 << SM.getExpansionLineNumber(L);
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +000096 } else if (const auto *VR = dyn_cast<VarRegion>(R)) {
97 os << "stack memory associated with local variable '" << VR->getString()
98 << '\'';
Ted Kremenek5df037e2010-06-17 04:21:37 +000099 range = VR->getDecl()->getSourceRange();
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000100 } else if (const auto *TOR = dyn_cast<CXXTempObjectRegion>(R)) {
Jordan Rosec9487092013-02-26 01:21:21 +0000101 QualType Ty = TOR->getValueType().getLocalUnqualifiedType();
102 os << "stack memory associated with temporary object of type '";
103 Ty.print(os, Ctx.getPrintingPolicy());
104 os << "'";
Jeffrey Yasskin0e9cdbb2011-08-26 00:41:31 +0000105 range = TOR->getExpr()->getSourceRange();
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000106 } else {
David Blaikie83d382b2011-09-23 05:06:16 +0000107 llvm_unreachable("Invalid region in ReturnStackAddressChecker.");
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000108 }
109
Ted Kremenek5df037e2010-06-17 04:21:37 +0000110 return range;
111}
112
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000113bool StackAddrEscapeChecker::isArcManagedBlock(const MemRegion *R,
114 CheckerContext &C) {
115 assert(R && "MemRegion should not be null");
116 return C.getASTContext().getLangOpts().ObjCAutoRefCount &&
117 isa<BlockDataRegion>(R);
118}
Zhongxing Xu1225aac2010-06-09 06:08:24 +0000119
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000120bool StackAddrEscapeChecker::isNotInCurrentFrame(const MemRegion *R,
121 CheckerContext &C) {
122 const StackSpaceRegion *S = cast<StackSpaceRegion>(R->getMemorySpace());
George Karpenkovdd18b112018-06-27 01:51:55 +0000123 return S->getStackFrame() != C.getStackFrame();
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000124}
125
126bool StackAddrEscapeChecker::isSemaphoreCaptured(const BlockDecl &B) const {
127 if (!dispatch_semaphore_tII)
128 dispatch_semaphore_tII = &B.getASTContext().Idents.get("dispatch_semaphore_t");
129 for (const auto &C : B.captures()) {
130 const auto *T = C.getVariable()->getType()->getAs<TypedefType>();
131 if (T && T->getDecl()->getIdentifier() == dispatch_semaphore_tII)
132 return true;
133 }
134 return false;
135}
136
137SmallVector<const MemRegion *, 4>
138StackAddrEscapeChecker::getCapturedStackRegions(const BlockDataRegion &B,
139 CheckerContext &C) {
140 SmallVector<const MemRegion *, 4> Regions;
141 BlockDataRegion::referenced_vars_iterator I = B.referenced_vars_begin();
142 BlockDataRegion::referenced_vars_iterator E = B.referenced_vars_end();
143 for (; I != E; ++I) {
144 SVal Val = C.getState()->getSVal(I.getCapturedRegion());
145 const MemRegion *Region = Val.getAsRegion();
146 if (Region && isa<StackSpaceRegion>(Region->getMemorySpace()))
147 Regions.push_back(Region);
148 }
149 return Regions;
150}
151
152void StackAddrEscapeChecker::EmitStackError(CheckerContext &C,
153 const MemRegion *R,
154 const Expr *RetE) const {
155 ExplodedNode *N = C.generateNonFatalErrorNode();
Zhongxing Xu1225aac2010-06-09 06:08:24 +0000156 if (!N)
157 return;
Zhongxing Xu1225aac2010-06-09 06:08:24 +0000158 if (!BT_returnstack)
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000159 BT_returnstack = llvm::make_unique<BuiltinBug>(
160 this, "Return of address to stack-allocated memory");
Zhongxing Xu1225aac2010-06-09 06:08:24 +0000161 // Generate a report for this bug.
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000162 SmallString<128> buf;
Zhongxing Xu1225aac2010-06-09 06:08:24 +0000163 llvm::raw_svector_ostream os(buf);
Jordan Rosec9487092013-02-26 01:21:21 +0000164 SourceRange range = genName(os, R, C.getASTContext());
Ted Kremenek5df037e2010-06-17 04:21:37 +0000165 os << " returned to caller";
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000166 auto report = llvm::make_unique<BugReport>(*BT_returnstack, os.str(), N);
Zhongxing Xu1225aac2010-06-09 06:08:24 +0000167 report->addRange(RetE->getSourceRange());
168 if (range.isValid())
169 report->addRange(range);
Aaron Ballman8d3a7a52015-06-23 13:15:32 +0000170 C.emitReport(std::move(report));
Eli Friedman04831922010-08-22 01:00:03 +0000171}
Zhongxing Xu1225aac2010-06-09 06:08:24 +0000172
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000173void StackAddrEscapeChecker::checkAsyncExecutedBlockCaptures(
174 const BlockDataRegion &B, CheckerContext &C) const {
175 // There is a not-too-uncommon idiom
176 // where a block passed to dispatch_async captures a semaphore
177 // and then the thread (which called dispatch_async) is blocked on waiting
178 // for the completion of the execution of the block
179 // via dispatch_semaphore_wait. To avoid false-positives (for now)
180 // we ignore all the blocks which have captured
181 // a variable of the type "dispatch_semaphore_t".
182 if (isSemaphoreCaptured(*B.getDecl()))
183 return;
184 for (const MemRegion *Region : getCapturedStackRegions(B, C)) {
185 // The block passed to dispatch_async may capture another block
186 // created on the stack. However, there is no leak in this situaton,
187 // no matter if ARC or no ARC is enabled:
188 // dispatch_async copies the passed "outer" block (via Block_copy)
189 // and if the block has captured another "inner" block,
190 // the "inner" block will be copied as well.
191 if (isa<BlockDataRegion>(Region))
192 continue;
193 ExplodedNode *N = C.generateNonFatalErrorNode();
194 if (!N)
195 continue;
196 if (!BT_capturedstackasync)
197 BT_capturedstackasync = llvm::make_unique<BuiltinBug>(
198 this, "Address of stack-allocated memory is captured");
199 SmallString<128> Buf;
200 llvm::raw_svector_ostream Out(Buf);
201 SourceRange Range = genName(Out, Region, C.getASTContext());
202 Out << " is captured by an asynchronously-executed block";
203 auto Report =
204 llvm::make_unique<BugReport>(*BT_capturedstackasync, Out.str(), N);
205 if (Range.isValid())
206 Report->addRange(Range);
207 C.emitReport(std::move(Report));
208 }
209}
210
211void StackAddrEscapeChecker::checkReturnedBlockCaptures(
212 const BlockDataRegion &B, CheckerContext &C) const {
213 for (const MemRegion *Region : getCapturedStackRegions(B, C)) {
214 if (isArcManagedBlock(Region, C) || isNotInCurrentFrame(Region, C))
215 continue;
216 ExplodedNode *N = C.generateNonFatalErrorNode();
217 if (!N)
218 continue;
219 if (!BT_capturedstackret)
220 BT_capturedstackret = llvm::make_unique<BuiltinBug>(
221 this, "Address of stack-allocated memory is captured");
222 SmallString<128> Buf;
223 llvm::raw_svector_ostream Out(Buf);
224 SourceRange Range = genName(Out, Region, C.getASTContext());
225 Out << " is captured by a returned block";
226 auto Report =
227 llvm::make_unique<BugReport>(*BT_capturedstackret, Out.str(), N);
228 if (Range.isValid())
229 Report->addRange(Range);
230 C.emitReport(std::move(Report));
231 }
232}
233
234void StackAddrEscapeChecker::checkPreCall(const CallEvent &Call,
235 CheckerContext &C) const {
Artem Dergacheve67a5752017-12-12 02:59:09 +0000236 if (!ChecksEnabled[CK_StackAddrAsyncEscapeChecker])
237 return;
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000238 if (!Call.isGlobalCFunction("dispatch_after") &&
239 !Call.isGlobalCFunction("dispatch_async"))
240 return;
241 for (unsigned Idx = 0, NumArgs = Call.getNumArgs(); Idx < NumArgs; ++Idx) {
242 if (const BlockDataRegion *B = dyn_cast_or_null<BlockDataRegion>(
243 Call.getArgSVal(Idx).getAsRegion()))
244 checkAsyncExecutedBlockCaptures(*B, C);
245 }
246}
247
Ted Kremenek5eb4b602011-02-25 22:00:43 +0000248void StackAddrEscapeChecker::checkPreStmt(const ReturnStmt *RS,
Ted Kremenek868dbda2012-03-03 01:22:03 +0000249 CheckerContext &C) const {
Artem Dergacheve67a5752017-12-12 02:59:09 +0000250 if (!ChecksEnabled[CK_StackAddrEscapeChecker])
251 return;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000252
Zhongxing Xu1225aac2010-06-09 06:08:24 +0000253 const Expr *RetE = RS->getRetValue();
254 if (!RetE)
255 return;
Jordan Rose2c625dd2012-08-29 01:11:59 +0000256 RetE = RetE->IgnoreParens();
Jordan Rosec9318302012-08-27 17:50:07 +0000257
George Karpenkovd703ec92018-01-17 20:27:29 +0000258 SVal V = C.getSVal(RetE);
Zhongxing Xu1225aac2010-06-09 06:08:24 +0000259 const MemRegion *R = V.getAsRegion();
Ted Kremenek868dbda2012-03-03 01:22:03 +0000260 if (!R)
Zhongxing Xu1225aac2010-06-09 06:08:24 +0000261 return;
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000262
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000263 if (const BlockDataRegion *B = dyn_cast<BlockDataRegion>(R))
264 checkReturnedBlockCaptures(*B, C);
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000265
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000266 if (!isa<StackSpaceRegion>(R->getMemorySpace()) ||
267 isNotInCurrentFrame(R, C) || isArcManagedBlock(R, C))
Ted Kremenek868dbda2012-03-03 01:22:03 +0000268 return;
269
Jordan Rosec9318302012-08-27 17:50:07 +0000270 // Returning a record by value is fine. (In this case, the returned
Jordan Rose2c625dd2012-08-29 01:11:59 +0000271 // expression will be a copy-constructor, possibly wrapped in an
272 // ExprWithCleanups node.)
273 if (const ExprWithCleanups *Cleanup = dyn_cast<ExprWithCleanups>(RetE))
274 RetE = Cleanup->getSubExpr();
Jordan Rosec9318302012-08-27 17:50:07 +0000275 if (isa<CXXConstructExpr>(RetE) && RetE->getType()->isRecordType())
276 return;
277
Devin Coughlindfde6552015-12-03 19:41:24 +0000278 // The CK_CopyAndAutoreleaseBlockObject cast causes the block to be copied
279 // so the stack address is not escaping here.
280 if (auto *ICE = dyn_cast<ImplicitCastExpr>(RetE)) {
281 if (isa<BlockDataRegion>(R) &&
282 ICE->getCastKind() == CK_CopyAndAutoreleaseBlockObject) {
283 return;
284 }
285 }
286
Ted Kremenek868dbda2012-03-03 01:22:03 +0000287 EmitStackError(C, R, RetE);
Zhongxing Xu1225aac2010-06-09 06:08:24 +0000288}
Zhongxing Xu87e7fc52010-06-08 10:00:00 +0000289
Anna Zaks3fdcc0b2013-01-03 00:25:29 +0000290void StackAddrEscapeChecker::checkEndFunction(CheckerContext &Ctx) const {
Artem Dergacheve67a5752017-12-12 02:59:09 +0000291 if (!ChecksEnabled[CK_StackAddrEscapeChecker])
292 return;
293
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000294 ProgramStateRef State = Ctx.getState();
Zhongxing Xu87e7fc52010-06-08 10:00:00 +0000295
Ted Kremenek17504be2010-06-17 00:24:44 +0000296 // Iterate over all bindings to global variables and see if it contains
297 // a memory region in the stack space.
298 class CallBack : public StoreManager::BindingsHandler {
299 private:
Anna Zaks3eae3342011-10-25 19:56:48 +0000300 CheckerContext &Ctx;
Ted Kremenek17504be2010-06-17 00:24:44 +0000301 const StackFrameContext *CurSFC;
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000302
Ted Kremenek17504be2010-06-17 00:24:44 +0000303 public:
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000304 SmallVector<std::pair<const MemRegion *, const MemRegion *>, 10> V;
Zhongxing Xu87e7fc52010-06-08 10:00:00 +0000305
George Karpenkovdd18b112018-06-27 01:51:55 +0000306 CallBack(CheckerContext &CC) : Ctx(CC), CurSFC(CC.getStackFrame()) {}
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000307
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000308 bool HandleBinding(StoreManager &SMgr, Store S, const MemRegion *Region,
309 SVal Val) override {
Craig Topperfb6b25b2014-03-15 04:29:04 +0000310
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000311 if (!isa<GlobalsSpaceRegion>(Region->getMemorySpace()))
Ted Kremenek17504be2010-06-17 00:24:44 +0000312 return true;
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000313 const MemRegion *VR = Val.getAsRegion();
314 if (VR && isa<StackSpaceRegion>(VR->getMemorySpace()) &&
315 !isArcManagedBlock(VR, Ctx) && !isNotInCurrentFrame(VR, Ctx))
316 V.emplace_back(Region, VR);
Ted Kremenek17504be2010-06-17 00:24:44 +0000317 return true;
Zhongxing Xu87e7fc52010-06-08 10:00:00 +0000318 }
Ted Kremenek17504be2010-06-17 00:24:44 +0000319 };
Ted Kremenek3a0678e2015-09-08 03:50:52 +0000320
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000321 CallBack Cb(Ctx);
322 State->getStateManager().getStoreManager().iterBindings(State->getStore(),
323 Cb);
Ted Kremenek5df037e2010-06-17 04:21:37 +0000324
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000325 if (Cb.V.empty())
Ted Kremenek17504be2010-06-17 00:24:44 +0000326 return;
327
328 // Generate an error node.
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000329 ExplodedNode *N = Ctx.generateNonFatalErrorNode(State);
Ted Kremenek17504be2010-06-17 00:24:44 +0000330 if (!N)
331 return;
Ted Kremenek17504be2010-06-17 00:24:44 +0000332
Ted Kremenek5df037e2010-06-17 04:21:37 +0000333 if (!BT_stackleak)
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000334 BT_stackleak = llvm::make_unique<BuiltinBug>(
335 this, "Stack address stored into global variable",
336 "Stack address was saved into a global variable. "
337 "This is dangerous because the address will become "
338 "invalid after returning from the function");
Alexander Kornienko4aca9b12014-02-11 21:49:21 +0000339
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000340 for (const auto &P : Cb.V) {
Ted Kremenek5df037e2010-06-17 04:21:37 +0000341 // Generate a report for this bug.
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000342 SmallString<128> Buf;
343 llvm::raw_svector_ostream Out(Buf);
344 SourceRange Range = genName(Out, P.second, Ctx.getASTContext());
345 Out << " is still referred to by the ";
346 if (isa<StaticGlobalSpaceRegion>(P.first->getMemorySpace()))
347 Out << "static";
Sean Evesonc24501d2016-05-26 14:02:17 +0000348 else
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000349 Out << "global";
350 Out << " variable '";
351 const VarRegion *VR = cast<VarRegion>(P.first->getBaseRegion());
352 Out << *VR->getDecl()
353 << "' upon returning to the caller. This will be a dangling reference";
354 auto Report = llvm::make_unique<BugReport>(*BT_stackleak, Out.str(), N);
355 if (Range.isValid())
356 Report->addRange(Range);
Ted Kremenek5df037e2010-06-17 04:21:37 +0000357
Alexander Shaposhnikov8ee899d2017-11-20 22:53:30 +0000358 Ctx.emitReport(std::move(Report));
Ted Kremenek5df037e2010-06-17 04:21:37 +0000359 }
Zhongxing Xu87e7fc52010-06-08 10:00:00 +0000360}
Argyrios Kyrtzidis506220f2011-02-23 21:04:54 +0000361
Artem Dergacheve67a5752017-12-12 02:59:09 +0000362#define REGISTER_CHECKER(name) \
363 void ento::register##name(CheckerManager &Mgr) { \
364 StackAddrEscapeChecker *Chk = \
365 Mgr.registerChecker<StackAddrEscapeChecker>(); \
366 Chk->ChecksEnabled[StackAddrEscapeChecker::CK_##name] = true; \
367 }
368
369REGISTER_CHECKER(StackAddrEscapeChecker)
370REGISTER_CHECKER(StackAddrAsyncEscapeChecker)