Ted Kremenek | f5d2ef4 | 2011-02-25 22:00:43 +0000 | [diff] [blame] | 1 | //=== StackAddrEscapeChecker.cpp ----------------------------------*- C++ -*--// |
Zhongxing Xu | 1622a54 | 2010-06-08 10:00:00 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 324f918 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Zhongxing Xu | 1622a54 | 2010-06-08 10:00:00 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Ted Kremenek | 99bb39a | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 9 | // This file defines stack address leak checker, which checks if an invalid |
Zhongxing Xu | 1622a54 | 2010-06-08 10:00:00 +0000 | [diff] [blame] | 10 | // stack address is stored into a global or heap location. See CERT DCL30-C. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Kristof Umann | 308797c | 2018-12-15 16:23:51 +0000 | [diff] [blame] | 14 | #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "clang/AST/ExprCXX.h" |
| 16 | #include "clang/Basic/SourceManager.h" |
| 17 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 695fb50 | 2011-02-17 21:39:17 +0000 | [diff] [blame] | 19 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
Argyrios Kyrtzidis | af5800a | 2011-02-23 21:04:54 +0000 | [diff] [blame] | 21 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 22 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" |
Zhongxing Xu | 9b14683 | 2010-06-09 06:08:24 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | a93d0f2 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Zhongxing Xu | 1622a54 | 2010-06-08 10:00:00 +0000 | [diff] [blame] | 25 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 26 | using namespace ento; |
Zhongxing Xu | 1622a54 | 2010-06-08 10:00:00 +0000 | [diff] [blame] | 27 | |
| 28 | namespace { |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 29 | class StackAddrEscapeChecker |
| 30 | : public Checker<check::PreCall, check::PreStmt<ReturnStmt>, |
| 31 | check::EndFunction> { |
| 32 | mutable IdentifierInfo *dispatch_semaphore_tII; |
Ahmed Charles | 70639e8 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 33 | mutable std::unique_ptr<BuiltinBug> BT_stackleak; |
| 34 | mutable std::unique_ptr<BuiltinBug> BT_returnstack; |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 35 | mutable std::unique_ptr<BuiltinBug> BT_capturedstackasync; |
| 36 | mutable std::unique_ptr<BuiltinBug> BT_capturedstackret; |
Zhongxing Xu | 1622a54 | 2010-06-08 10:00:00 +0000 | [diff] [blame] | 37 | |
| 38 | public: |
Artem Dergachev | bdf9e46 | 2017-12-12 02:59:09 +0000 | [diff] [blame] | 39 | enum CheckKind { |
| 40 | CK_StackAddrEscapeChecker, |
| 41 | CK_StackAddrAsyncEscapeChecker, |
| 42 | CK_NumCheckKinds |
| 43 | }; |
| 44 | |
| 45 | DefaultBool ChecksEnabled[CK_NumCheckKinds]; |
| 46 | |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 47 | void checkPreCall(const CallEvent &Call, CheckerContext &C) const; |
Argyrios Kyrtzidis | af5800a | 2011-02-23 21:04:54 +0000 | [diff] [blame] | 48 | void checkPreStmt(const ReturnStmt *RS, CheckerContext &C) const; |
Reka Kovacs | 3ad62f5 | 2018-07-16 20:47:45 +0000 | [diff] [blame] | 49 | void checkEndFunction(const ReturnStmt *RS, CheckerContext &Ctx) const; |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 50 | |
Zhongxing Xu | 9b14683 | 2010-06-09 06:08:24 +0000 | [diff] [blame] | 51 | private: |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 52 | void checkReturnedBlockCaptures(const BlockDataRegion &B, |
| 53 | CheckerContext &C) const; |
| 54 | void checkAsyncExecutedBlockCaptures(const BlockDataRegion &B, |
| 55 | CheckerContext &C) const; |
Argyrios Kyrtzidis | af5800a | 2011-02-23 21:04:54 +0000 | [diff] [blame] | 56 | void EmitStackError(CheckerContext &C, const MemRegion *R, |
| 57 | const Expr *RetE) const; |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 58 | bool isSemaphoreCaptured(const BlockDecl &B) const; |
Jordan Rose | a0e6e6d | 2013-02-26 01:21:21 +0000 | [diff] [blame] | 59 | static SourceRange genName(raw_ostream &os, const MemRegion *R, |
| 60 | ASTContext &Ctx); |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 61 | static SmallVector<const MemRegion *, 4> |
| 62 | getCapturedStackRegions(const BlockDataRegion &B, CheckerContext &C); |
| 63 | static bool isArcManagedBlock(const MemRegion *R, CheckerContext &C); |
| 64 | static bool isNotInCurrentFrame(const MemRegion *R, CheckerContext &C); |
Zhongxing Xu | 1622a54 | 2010-06-08 10:00:00 +0000 | [diff] [blame] | 65 | }; |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 66 | } // namespace |
Zhongxing Xu | 1622a54 | 2010-06-08 10:00:00 +0000 | [diff] [blame] | 67 | |
Jordan Rose | a0e6e6d | 2013-02-26 01:21:21 +0000 | [diff] [blame] | 68 | SourceRange StackAddrEscapeChecker::genName(raw_ostream &os, const MemRegion *R, |
| 69 | ASTContext &Ctx) { |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 70 | // Get the base region, stripping away fields and elements. |
Ted Kremenek | a816615 | 2010-06-17 04:21:37 +0000 | [diff] [blame] | 71 | R = R->getBaseRegion(); |
Jordan Rose | a0e6e6d | 2013-02-26 01:21:21 +0000 | [diff] [blame] | 72 | SourceManager &SM = Ctx.getSourceManager(); |
Ted Kremenek | a816615 | 2010-06-17 04:21:37 +0000 | [diff] [blame] | 73 | SourceRange range; |
| 74 | os << "Address of "; |
Ted Kremenek | 99bb39a | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 75 | |
Ted Kremenek | a816615 | 2010-06-17 04:21:37 +0000 | [diff] [blame] | 76 | // Check if the region is a compound literal. |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 77 | if (const auto *CR = dyn_cast<CompoundLiteralRegion>(R)) { |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 78 | const CompoundLiteralExpr *CL = CR->getLiteralExpr(); |
Ted Kremenek | a816615 | 2010-06-17 04:21:37 +0000 | [diff] [blame] | 79 | os << "stack memory associated with a compound literal " |
| 80 | "declared on line " |
Stephen Kelly | d7b659b | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 81 | << SM.getExpansionLineNumber(CL->getBeginLoc()) << " returned to caller"; |
Ted Kremenek | a816615 | 2010-06-17 04:21:37 +0000 | [diff] [blame] | 82 | range = CL->getSourceRange(); |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 83 | } else if (const auto *AR = dyn_cast<AllocaRegion>(R)) { |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 84 | const Expr *ARE = AR->getExpr(); |
Stephen Kelly | d7b659b | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 85 | SourceLocation L = ARE->getBeginLoc(); |
Ted Kremenek | 99bb39a | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 86 | range = ARE->getSourceRange(); |
Ted Kremenek | a816615 | 2010-06-17 04:21:37 +0000 | [diff] [blame] | 87 | os << "stack memory allocated by call to alloca() on line " |
Chandler Carruth | 6421162 | 2011-07-25 21:09:52 +0000 | [diff] [blame] | 88 | << SM.getExpansionLineNumber(L); |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 89 | } else if (const auto *BR = dyn_cast<BlockDataRegion>(R)) { |
Ted Kremenek | a816615 | 2010-06-17 04:21:37 +0000 | [diff] [blame] | 90 | const BlockDecl *BD = BR->getCodeRegion()->getDecl(); |
Stephen Kelly | d7b659b | 2018-08-09 21:08:08 +0000 | [diff] [blame] | 91 | SourceLocation L = BD->getBeginLoc(); |
Ted Kremenek | a816615 | 2010-06-17 04:21:37 +0000 | [diff] [blame] | 92 | range = BD->getSourceRange(); |
| 93 | os << "stack-allocated block declared on line " |
Chandler Carruth | 6421162 | 2011-07-25 21:09:52 +0000 | [diff] [blame] | 94 | << SM.getExpansionLineNumber(L); |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 95 | } else if (const auto *VR = dyn_cast<VarRegion>(R)) { |
| 96 | os << "stack memory associated with local variable '" << VR->getString() |
| 97 | << '\''; |
Ted Kremenek | a816615 | 2010-06-17 04:21:37 +0000 | [diff] [blame] | 98 | range = VR->getDecl()->getSourceRange(); |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 99 | } else if (const auto *TOR = dyn_cast<CXXTempObjectRegion>(R)) { |
Jordan Rose | a0e6e6d | 2013-02-26 01:21:21 +0000 | [diff] [blame] | 100 | QualType Ty = TOR->getValueType().getLocalUnqualifiedType(); |
| 101 | os << "stack memory associated with temporary object of type '"; |
| 102 | Ty.print(os, Ctx.getPrintingPolicy()); |
| 103 | os << "'"; |
Jeffrey Yasskin | 782f63e | 2011-08-26 00:41:31 +0000 | [diff] [blame] | 104 | range = TOR->getExpr()->getSourceRange(); |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 105 | } else { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 106 | llvm_unreachable("Invalid region in ReturnStackAddressChecker."); |
Ted Kremenek | 99bb39a | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Ted Kremenek | a816615 | 2010-06-17 04:21:37 +0000 | [diff] [blame] | 109 | return range; |
| 110 | } |
| 111 | |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 112 | bool StackAddrEscapeChecker::isArcManagedBlock(const MemRegion *R, |
| 113 | CheckerContext &C) { |
| 114 | assert(R && "MemRegion should not be null"); |
| 115 | return C.getASTContext().getLangOpts().ObjCAutoRefCount && |
| 116 | isa<BlockDataRegion>(R); |
| 117 | } |
Zhongxing Xu | 9b14683 | 2010-06-09 06:08:24 +0000 | [diff] [blame] | 118 | |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 119 | bool StackAddrEscapeChecker::isNotInCurrentFrame(const MemRegion *R, |
| 120 | CheckerContext &C) { |
| 121 | const StackSpaceRegion *S = cast<StackSpaceRegion>(R->getMemorySpace()); |
George Karpenkov | 3387634 | 2018-06-27 01:51:55 +0000 | [diff] [blame] | 122 | return S->getStackFrame() != C.getStackFrame(); |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | bool StackAddrEscapeChecker::isSemaphoreCaptured(const BlockDecl &B) const { |
| 126 | if (!dispatch_semaphore_tII) |
| 127 | dispatch_semaphore_tII = &B.getASTContext().Idents.get("dispatch_semaphore_t"); |
| 128 | for (const auto &C : B.captures()) { |
| 129 | const auto *T = C.getVariable()->getType()->getAs<TypedefType>(); |
| 130 | if (T && T->getDecl()->getIdentifier() == dispatch_semaphore_tII) |
Fangrui Song | abdbb60 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 131 | return true; |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 132 | } |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | SmallVector<const MemRegion *, 4> |
| 137 | StackAddrEscapeChecker::getCapturedStackRegions(const BlockDataRegion &B, |
| 138 | CheckerContext &C) { |
| 139 | SmallVector<const MemRegion *, 4> Regions; |
| 140 | BlockDataRegion::referenced_vars_iterator I = B.referenced_vars_begin(); |
| 141 | BlockDataRegion::referenced_vars_iterator E = B.referenced_vars_end(); |
| 142 | for (; I != E; ++I) { |
| 143 | SVal Val = C.getState()->getSVal(I.getCapturedRegion()); |
| 144 | const MemRegion *Region = Val.getAsRegion(); |
| 145 | if (Region && isa<StackSpaceRegion>(Region->getMemorySpace())) |
| 146 | Regions.push_back(Region); |
| 147 | } |
| 148 | return Regions; |
| 149 | } |
| 150 | |
| 151 | void StackAddrEscapeChecker::EmitStackError(CheckerContext &C, |
| 152 | const MemRegion *R, |
| 153 | const Expr *RetE) const { |
| 154 | ExplodedNode *N = C.generateNonFatalErrorNode(); |
Zhongxing Xu | 9b14683 | 2010-06-09 06:08:24 +0000 | [diff] [blame] | 155 | if (!N) |
| 156 | return; |
Zhongxing Xu | 9b14683 | 2010-06-09 06:08:24 +0000 | [diff] [blame] | 157 | if (!BT_returnstack) |
Jonas Devlieghere | 49a6b09 | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 158 | BT_returnstack = std::make_unique<BuiltinBug>( |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 159 | this, "Return of address to stack-allocated memory"); |
Zhongxing Xu | 9b14683 | 2010-06-09 06:08:24 +0000 | [diff] [blame] | 160 | // Generate a report for this bug. |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 161 | SmallString<128> buf; |
Zhongxing Xu | 9b14683 | 2010-06-09 06:08:24 +0000 | [diff] [blame] | 162 | llvm::raw_svector_ostream os(buf); |
Jordan Rose | a0e6e6d | 2013-02-26 01:21:21 +0000 | [diff] [blame] | 163 | SourceRange range = genName(os, R, C.getASTContext()); |
Ted Kremenek | a816615 | 2010-06-17 04:21:37 +0000 | [diff] [blame] | 164 | os << " returned to caller"; |
Artem Dergachev | 9e6519f | 2019-09-09 20:34:40 +0000 | [diff] [blame] | 165 | auto report = |
| 166 | std::make_unique<PathSensitiveBugReport>(*BT_returnstack, os.str(), N); |
Zhongxing Xu | 9b14683 | 2010-06-09 06:08:24 +0000 | [diff] [blame] | 167 | report->addRange(RetE->getSourceRange()); |
| 168 | if (range.isValid()) |
| 169 | report->addRange(range); |
Aaron Ballman | e1c2ad6 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 170 | C.emitReport(std::move(report)); |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 171 | } |
Zhongxing Xu | 9b14683 | 2010-06-09 06:08:24 +0000 | [diff] [blame] | 172 | |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 173 | void 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 |
Fangrui Song | abdbb60 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 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 |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 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) |
Jonas Devlieghere | 49a6b09 | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 197 | BT_capturedstackasync = std::make_unique<BuiltinBug>( |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 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"; |
Artem Dergachev | 9e6519f | 2019-09-09 20:34:40 +0000 | [diff] [blame] | 203 | auto Report = std::make_unique<PathSensitiveBugReport>( |
| 204 | *BT_capturedstackasync, Out.str(), N); |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 205 | if (Range.isValid()) |
| 206 | Report->addRange(Range); |
| 207 | C.emitReport(std::move(Report)); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | void 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) |
Jonas Devlieghere | 49a6b09 | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 220 | BT_capturedstackret = std::make_unique<BuiltinBug>( |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 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"; |
Artem Dergachev | 9e6519f | 2019-09-09 20:34:40 +0000 | [diff] [blame] | 226 | auto Report = std::make_unique<PathSensitiveBugReport>(*BT_capturedstackret, |
| 227 | Out.str(), N); |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 228 | if (Range.isValid()) |
| 229 | Report->addRange(Range); |
| 230 | C.emitReport(std::move(Report)); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | void StackAddrEscapeChecker::checkPreCall(const CallEvent &Call, |
| 235 | CheckerContext &C) const { |
Artem Dergachev | bdf9e46 | 2017-12-12 02:59:09 +0000 | [diff] [blame] | 236 | if (!ChecksEnabled[CK_StackAddrAsyncEscapeChecker]) |
| 237 | return; |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 238 | 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 Kremenek | f5d2ef4 | 2011-02-25 22:00:43 +0000 | [diff] [blame] | 248 | void StackAddrEscapeChecker::checkPreStmt(const ReturnStmt *RS, |
Ted Kremenek | 7e86783 | 2012-03-03 01:22:03 +0000 | [diff] [blame] | 249 | CheckerContext &C) const { |
Artem Dergachev | bdf9e46 | 2017-12-12 02:59:09 +0000 | [diff] [blame] | 250 | if (!ChecksEnabled[CK_StackAddrEscapeChecker]) |
| 251 | return; |
Ted Kremenek | 99bb39a | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 252 | |
Zhongxing Xu | 9b14683 | 2010-06-09 06:08:24 +0000 | [diff] [blame] | 253 | const Expr *RetE = RS->getRetValue(); |
| 254 | if (!RetE) |
| 255 | return; |
Jordan Rose | 73212df | 2012-08-29 01:11:59 +0000 | [diff] [blame] | 256 | RetE = RetE->IgnoreParens(); |
Jordan Rose | c210cb7 | 2012-08-27 17:50:07 +0000 | [diff] [blame] | 257 | |
George Karpenkov | b340ee9 | 2018-01-17 20:27:29 +0000 | [diff] [blame] | 258 | SVal V = C.getSVal(RetE); |
Zhongxing Xu | 9b14683 | 2010-06-09 06:08:24 +0000 | [diff] [blame] | 259 | const MemRegion *R = V.getAsRegion(); |
Ted Kremenek | 7e86783 | 2012-03-03 01:22:03 +0000 | [diff] [blame] | 260 | if (!R) |
Zhongxing Xu | 9b14683 | 2010-06-09 06:08:24 +0000 | [diff] [blame] | 261 | return; |
Ted Kremenek | 99bb39a | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 262 | |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 263 | if (const BlockDataRegion *B = dyn_cast<BlockDataRegion>(R)) |
| 264 | checkReturnedBlockCaptures(*B, C); |
Ted Kremenek | 99bb39a | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 265 | |
Fangrui Song | abdbb60 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 266 | if (!isa<StackSpaceRegion>(R->getMemorySpace()) || |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 267 | isNotInCurrentFrame(R, C) || isArcManagedBlock(R, C)) |
Ted Kremenek | 7e86783 | 2012-03-03 01:22:03 +0000 | [diff] [blame] | 268 | return; |
| 269 | |
Jordan Rose | c210cb7 | 2012-08-27 17:50:07 +0000 | [diff] [blame] | 270 | // Returning a record by value is fine. (In this case, the returned |
Jordan Rose | 73212df | 2012-08-29 01:11:59 +0000 | [diff] [blame] | 271 | // 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 Rose | c210cb7 | 2012-08-27 17:50:07 +0000 | [diff] [blame] | 275 | if (isa<CXXConstructExpr>(RetE) && RetE->getType()->isRecordType()) |
| 276 | return; |
| 277 | |
Devin Coughlin | a2892f0 | 2015-12-03 19:41:24 +0000 | [diff] [blame] | 278 | // 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 Kremenek | 7e86783 | 2012-03-03 01:22:03 +0000 | [diff] [blame] | 287 | EmitStackError(C, R, RetE); |
Zhongxing Xu | 9b14683 | 2010-06-09 06:08:24 +0000 | [diff] [blame] | 288 | } |
Zhongxing Xu | 1622a54 | 2010-06-08 10:00:00 +0000 | [diff] [blame] | 289 | |
Reka Kovacs | 3ad62f5 | 2018-07-16 20:47:45 +0000 | [diff] [blame] | 290 | void StackAddrEscapeChecker::checkEndFunction(const ReturnStmt *RS, |
| 291 | CheckerContext &Ctx) const { |
Artem Dergachev | bdf9e46 | 2017-12-12 02:59:09 +0000 | [diff] [blame] | 292 | if (!ChecksEnabled[CK_StackAddrEscapeChecker]) |
| 293 | return; |
| 294 | |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 295 | ProgramStateRef State = Ctx.getState(); |
Zhongxing Xu | 1622a54 | 2010-06-08 10:00:00 +0000 | [diff] [blame] | 296 | |
Ted Kremenek | 551bd1f | 2010-06-17 00:24:44 +0000 | [diff] [blame] | 297 | // Iterate over all bindings to global variables and see if it contains |
| 298 | // a memory region in the stack space. |
| 299 | class CallBack : public StoreManager::BindingsHandler { |
| 300 | private: |
Anna Zaks | af498a2 | 2011-10-25 19:56:48 +0000 | [diff] [blame] | 301 | CheckerContext &Ctx; |
Ted Kremenek | 551bd1f | 2010-06-17 00:24:44 +0000 | [diff] [blame] | 302 | const StackFrameContext *CurSFC; |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 303 | |
Ted Kremenek | 551bd1f | 2010-06-17 00:24:44 +0000 | [diff] [blame] | 304 | public: |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 305 | SmallVector<std::pair<const MemRegion *, const MemRegion *>, 10> V; |
Zhongxing Xu | 1622a54 | 2010-06-08 10:00:00 +0000 | [diff] [blame] | 306 | |
George Karpenkov | 3387634 | 2018-06-27 01:51:55 +0000 | [diff] [blame] | 307 | CallBack(CheckerContext &CC) : Ctx(CC), CurSFC(CC.getStackFrame()) {} |
Ted Kremenek | 99bb39a | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 308 | |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 309 | bool HandleBinding(StoreManager &SMgr, Store S, const MemRegion *Region, |
| 310 | SVal Val) override { |
Craig Topper | 83daac8 | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 311 | |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 312 | if (!isa<GlobalsSpaceRegion>(Region->getMemorySpace())) |
Ted Kremenek | 551bd1f | 2010-06-17 00:24:44 +0000 | [diff] [blame] | 313 | return true; |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 314 | const MemRegion *VR = Val.getAsRegion(); |
| 315 | if (VR && isa<StackSpaceRegion>(VR->getMemorySpace()) && |
| 316 | !isArcManagedBlock(VR, Ctx) && !isNotInCurrentFrame(VR, Ctx)) |
| 317 | V.emplace_back(Region, VR); |
Ted Kremenek | 551bd1f | 2010-06-17 00:24:44 +0000 | [diff] [blame] | 318 | return true; |
Zhongxing Xu | 1622a54 | 2010-06-08 10:00:00 +0000 | [diff] [blame] | 319 | } |
Ted Kremenek | 551bd1f | 2010-06-17 00:24:44 +0000 | [diff] [blame] | 320 | }; |
Ted Kremenek | 99bb39a | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 321 | |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 322 | CallBack Cb(Ctx); |
| 323 | State->getStateManager().getStoreManager().iterBindings(State->getStore(), |
| 324 | Cb); |
Ted Kremenek | a816615 | 2010-06-17 04:21:37 +0000 | [diff] [blame] | 325 | |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 326 | if (Cb.V.empty()) |
Ted Kremenek | 551bd1f | 2010-06-17 00:24:44 +0000 | [diff] [blame] | 327 | return; |
| 328 | |
| 329 | // Generate an error node. |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 330 | ExplodedNode *N = Ctx.generateNonFatalErrorNode(State); |
Ted Kremenek | 551bd1f | 2010-06-17 00:24:44 +0000 | [diff] [blame] | 331 | if (!N) |
| 332 | return; |
Ted Kremenek | 551bd1f | 2010-06-17 00:24:44 +0000 | [diff] [blame] | 333 | |
Ted Kremenek | a816615 | 2010-06-17 04:21:37 +0000 | [diff] [blame] | 334 | if (!BT_stackleak) |
Jonas Devlieghere | 49a6b09 | 2019-08-14 23:04:18 +0000 | [diff] [blame] | 335 | BT_stackleak = std::make_unique<BuiltinBug>( |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 336 | this, "Stack address stored into global variable", |
| 337 | "Stack address was saved into a global variable. " |
| 338 | "This is dangerous because the address will become " |
| 339 | "invalid after returning from the function"); |
Alexander Kornienko | 15c01b2 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 340 | |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 341 | for (const auto &P : Cb.V) { |
Ted Kremenek | a816615 | 2010-06-17 04:21:37 +0000 | [diff] [blame] | 342 | // Generate a report for this bug. |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 343 | SmallString<128> Buf; |
| 344 | llvm::raw_svector_ostream Out(Buf); |
| 345 | SourceRange Range = genName(Out, P.second, Ctx.getASTContext()); |
| 346 | Out << " is still referred to by the "; |
| 347 | if (isa<StaticGlobalSpaceRegion>(P.first->getMemorySpace())) |
| 348 | Out << "static"; |
Sean Eveson | 2283ffc | 2016-05-26 14:02:17 +0000 | [diff] [blame] | 349 | else |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 350 | Out << "global"; |
| 351 | Out << " variable '"; |
| 352 | const VarRegion *VR = cast<VarRegion>(P.first->getBaseRegion()); |
| 353 | Out << *VR->getDecl() |
| 354 | << "' upon returning to the caller. This will be a dangling reference"; |
Artem Dergachev | 9e6519f | 2019-09-09 20:34:40 +0000 | [diff] [blame] | 355 | auto Report = |
| 356 | std::make_unique<PathSensitiveBugReport>(*BT_stackleak, Out.str(), N); |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 357 | if (Range.isValid()) |
| 358 | Report->addRange(Range); |
Ted Kremenek | a816615 | 2010-06-17 04:21:37 +0000 | [diff] [blame] | 359 | |
Alexander Shaposhnikov | 94ab672 | 2017-11-20 22:53:30 +0000 | [diff] [blame] | 360 | Ctx.emitReport(std::move(Report)); |
Ted Kremenek | a816615 | 2010-06-17 04:21:37 +0000 | [diff] [blame] | 361 | } |
Zhongxing Xu | 1622a54 | 2010-06-08 10:00:00 +0000 | [diff] [blame] | 362 | } |
Argyrios Kyrtzidis | af5800a | 2011-02-23 21:04:54 +0000 | [diff] [blame] | 363 | |
Kristof Umann | 0492ebd | 2019-01-26 20:06:54 +0000 | [diff] [blame] | 364 | void ento::registerStackAddrEscapeBase(CheckerManager &mgr) { |
| 365 | mgr.registerChecker<StackAddrEscapeChecker>(); |
| 366 | } |
| 367 | |
| 368 | bool ento::shouldRegisterStackAddrEscapeBase(const LangOptions &LO) { |
| 369 | return true; |
| 370 | } |
| 371 | |
Kristof Umann | 8abad3c | 2019-01-26 21:41:50 +0000 | [diff] [blame] | 372 | #define REGISTER_CHECKER(name) \ |
| 373 | void ento::register##name(CheckerManager &Mgr) { \ |
| 374 | StackAddrEscapeChecker *Chk = \ |
| 375 | Mgr.getChecker<StackAddrEscapeChecker>(); \ |
| 376 | Chk->ChecksEnabled[StackAddrEscapeChecker::CK_##name] = true; \ |
Kristof Umann | 9b220eb | 2019-01-26 14:23:08 +0000 | [diff] [blame] | 377 | } \ |
| 378 | \ |
| 379 | bool ento::shouldRegister##name(const LangOptions &LO) { \ |
| 380 | return true; \ |
Artem Dergachev | bdf9e46 | 2017-12-12 02:59:09 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | REGISTER_CHECKER(StackAddrEscapeChecker) |
| 384 | REGISTER_CHECKER(StackAddrAsyncEscapeChecker) |