xref: /llvm-project/clang/lib/StaticAnalyzer/Checkers/StackAddrEscapeChecker.cpp (revision 8ee899d42ec647656823dcf991b162a8de2799c0)
1 //=== StackAddrEscapeChecker.cpp ----------------------------------*- C++ -*--//
2 //
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 //
10 // This file defines stack address leak checker, which checks if an invalid
11 // stack address is stored into a global or heap location. See CERT DCL30-C.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "ClangSACheckers.h"
16 #include "clang/AST/ExprCXX.h"
17 #include "clang/Basic/SourceManager.h"
18 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
19 #include "clang/StaticAnalyzer/Core/Checker.h"
20 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/Support/raw_ostream.h"
26 using namespace clang;
27 using namespace ento;
28 
29 namespace {
30 class StackAddrEscapeChecker
31     : public Checker<check::PreCall, check::PreStmt<ReturnStmt>,
32                      check::EndFunction> {
33   mutable IdentifierInfo *dispatch_semaphore_tII;
34   mutable std::unique_ptr<BuiltinBug> BT_stackleak;
35   mutable std::unique_ptr<BuiltinBug> BT_returnstack;
36   mutable std::unique_ptr<BuiltinBug> BT_capturedstackasync;
37   mutable std::unique_ptr<BuiltinBug> BT_capturedstackret;
38 
39 public:
40   void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
41   void checkPreStmt(const ReturnStmt *RS, CheckerContext &C) const;
42   void checkEndFunction(CheckerContext &Ctx) const;
43 
44 private:
45   void checkReturnedBlockCaptures(const BlockDataRegion &B,
46                                   CheckerContext &C) const;
47   void checkAsyncExecutedBlockCaptures(const BlockDataRegion &B,
48                                        CheckerContext &C) const;
49   void EmitStackError(CheckerContext &C, const MemRegion *R,
50                       const Expr *RetE) const;
51   bool isSemaphoreCaptured(const BlockDecl &B) const;
52   static SourceRange genName(raw_ostream &os, const MemRegion *R,
53                              ASTContext &Ctx);
54   static SmallVector<const MemRegion *, 4>
55   getCapturedStackRegions(const BlockDataRegion &B, CheckerContext &C);
56   static bool isArcManagedBlock(const MemRegion *R, CheckerContext &C);
57   static bool isNotInCurrentFrame(const MemRegion *R, CheckerContext &C);
58 };
59 } // namespace
60 
61 SourceRange StackAddrEscapeChecker::genName(raw_ostream &os, const MemRegion *R,
62                                             ASTContext &Ctx) {
63   // Get the base region, stripping away fields and elements.
64   R = R->getBaseRegion();
65   SourceManager &SM = Ctx.getSourceManager();
66   SourceRange range;
67   os << "Address of ";
68 
69   // Check if the region is a compound literal.
70   if (const auto *CR = dyn_cast<CompoundLiteralRegion>(R)) {
71     const CompoundLiteralExpr *CL = CR->getLiteralExpr();
72     os << "stack memory associated with a compound literal "
73           "declared on line "
74        << SM.getExpansionLineNumber(CL->getLocStart()) << " returned to caller";
75     range = CL->getSourceRange();
76   } else if (const auto *AR = dyn_cast<AllocaRegion>(R)) {
77     const Expr *ARE = AR->getExpr();
78     SourceLocation L = ARE->getLocStart();
79     range = ARE->getSourceRange();
80     os << "stack memory allocated by call to alloca() on line "
81        << SM.getExpansionLineNumber(L);
82   } else if (const auto *BR = dyn_cast<BlockDataRegion>(R)) {
83     const BlockDecl *BD = BR->getCodeRegion()->getDecl();
84     SourceLocation L = BD->getLocStart();
85     range = BD->getSourceRange();
86     os << "stack-allocated block declared on line "
87        << SM.getExpansionLineNumber(L);
88   } else if (const auto *VR = dyn_cast<VarRegion>(R)) {
89     os << "stack memory associated with local variable '" << VR->getString()
90        << '\'';
91     range = VR->getDecl()->getSourceRange();
92   } else if (const auto *TOR = dyn_cast<CXXTempObjectRegion>(R)) {
93     QualType Ty = TOR->getValueType().getLocalUnqualifiedType();
94     os << "stack memory associated with temporary object of type '";
95     Ty.print(os, Ctx.getPrintingPolicy());
96     os << "'";
97     range = TOR->getExpr()->getSourceRange();
98   } else {
99     llvm_unreachable("Invalid region in ReturnStackAddressChecker.");
100   }
101 
102   return range;
103 }
104 
105 bool StackAddrEscapeChecker::isArcManagedBlock(const MemRegion *R,
106                                                CheckerContext &C) {
107   assert(R && "MemRegion should not be null");
108   return C.getASTContext().getLangOpts().ObjCAutoRefCount &&
109          isa<BlockDataRegion>(R);
110 }
111 
112 bool StackAddrEscapeChecker::isNotInCurrentFrame(const MemRegion *R,
113                                                  CheckerContext &C) {
114   const StackSpaceRegion *S = cast<StackSpaceRegion>(R->getMemorySpace());
115   return S->getStackFrame() != C.getLocationContext()->getCurrentStackFrame();
116 }
117 
118 bool StackAddrEscapeChecker::isSemaphoreCaptured(const BlockDecl &B) const {
119   if (!dispatch_semaphore_tII)
120     dispatch_semaphore_tII = &B.getASTContext().Idents.get("dispatch_semaphore_t");
121   for (const auto &C : B.captures()) {
122     const auto *T = C.getVariable()->getType()->getAs<TypedefType>();
123     if (T && T->getDecl()->getIdentifier() == dispatch_semaphore_tII)
124       return true;
125   }
126   return false;
127 }
128 
129 SmallVector<const MemRegion *, 4>
130 StackAddrEscapeChecker::getCapturedStackRegions(const BlockDataRegion &B,
131                                                 CheckerContext &C) {
132   SmallVector<const MemRegion *, 4> Regions;
133   BlockDataRegion::referenced_vars_iterator I = B.referenced_vars_begin();
134   BlockDataRegion::referenced_vars_iterator E = B.referenced_vars_end();
135   for (; I != E; ++I) {
136     SVal Val = C.getState()->getSVal(I.getCapturedRegion());
137     const MemRegion *Region = Val.getAsRegion();
138     if (Region && isa<StackSpaceRegion>(Region->getMemorySpace()))
139       Regions.push_back(Region);
140   }
141   return Regions;
142 }
143 
144 void StackAddrEscapeChecker::EmitStackError(CheckerContext &C,
145                                             const MemRegion *R,
146                                             const Expr *RetE) const {
147   ExplodedNode *N = C.generateNonFatalErrorNode();
148   if (!N)
149     return;
150   if (!BT_returnstack)
151     BT_returnstack = llvm::make_unique<BuiltinBug>(
152         this, "Return of address to stack-allocated memory");
153   // Generate a report for this bug.
154   SmallString<128> buf;
155   llvm::raw_svector_ostream os(buf);
156   SourceRange range = genName(os, R, C.getASTContext());
157   os << " returned to caller";
158   auto report = llvm::make_unique<BugReport>(*BT_returnstack, os.str(), N);
159   report->addRange(RetE->getSourceRange());
160   if (range.isValid())
161     report->addRange(range);
162   C.emitReport(std::move(report));
163 }
164 
165 void StackAddrEscapeChecker::checkAsyncExecutedBlockCaptures(
166     const BlockDataRegion &B, CheckerContext &C) const {
167   // There is a not-too-uncommon idiom
168   // where a block passed to dispatch_async captures a semaphore
169   // and then the thread (which called dispatch_async) is blocked on waiting
170   // for the completion of the execution of the block
171   // via dispatch_semaphore_wait. To avoid false-positives (for now)
172   // we ignore all the blocks which have captured
173   // a variable of the type "dispatch_semaphore_t".
174   if (isSemaphoreCaptured(*B.getDecl()))
175     return;
176   for (const MemRegion *Region : getCapturedStackRegions(B, C)) {
177     // The block passed to dispatch_async may capture another block
178     // created on the stack. However, there is no leak in this situaton,
179     // no matter if ARC or no ARC is enabled:
180     // dispatch_async copies the passed "outer" block (via Block_copy)
181     // and if the block has captured another "inner" block,
182     // the "inner" block will be copied as well.
183     if (isa<BlockDataRegion>(Region))
184       continue;
185     ExplodedNode *N = C.generateNonFatalErrorNode();
186     if (!N)
187       continue;
188     if (!BT_capturedstackasync)
189       BT_capturedstackasync = llvm::make_unique<BuiltinBug>(
190           this, "Address of stack-allocated memory is captured");
191     SmallString<128> Buf;
192     llvm::raw_svector_ostream Out(Buf);
193     SourceRange Range = genName(Out, Region, C.getASTContext());
194     Out << " is captured by an asynchronously-executed block";
195     auto Report =
196         llvm::make_unique<BugReport>(*BT_capturedstackasync, Out.str(), N);
197     if (Range.isValid())
198       Report->addRange(Range);
199     C.emitReport(std::move(Report));
200   }
201 }
202 
203 void StackAddrEscapeChecker::checkReturnedBlockCaptures(
204     const BlockDataRegion &B, CheckerContext &C) const {
205   for (const MemRegion *Region : getCapturedStackRegions(B, C)) {
206     if (isArcManagedBlock(Region, C) || isNotInCurrentFrame(Region, C))
207       continue;
208     ExplodedNode *N = C.generateNonFatalErrorNode();
209     if (!N)
210       continue;
211     if (!BT_capturedstackret)
212       BT_capturedstackret = llvm::make_unique<BuiltinBug>(
213           this, "Address of stack-allocated memory is captured");
214     SmallString<128> Buf;
215     llvm::raw_svector_ostream Out(Buf);
216     SourceRange Range = genName(Out, Region, C.getASTContext());
217     Out << " is captured by a returned block";
218     auto Report =
219         llvm::make_unique<BugReport>(*BT_capturedstackret, Out.str(), N);
220     if (Range.isValid())
221       Report->addRange(Range);
222     C.emitReport(std::move(Report));
223   }
224 }
225 
226 void StackAddrEscapeChecker::checkPreCall(const CallEvent &Call,
227                                           CheckerContext &C) const {
228   if (!Call.isGlobalCFunction("dispatch_after") &&
229       !Call.isGlobalCFunction("dispatch_async"))
230     return;
231   for (unsigned Idx = 0, NumArgs = Call.getNumArgs(); Idx < NumArgs; ++Idx) {
232     if (const BlockDataRegion *B = dyn_cast_or_null<BlockDataRegion>(
233             Call.getArgSVal(Idx).getAsRegion()))
234       checkAsyncExecutedBlockCaptures(*B, C);
235   }
236 }
237 
238 void StackAddrEscapeChecker::checkPreStmt(const ReturnStmt *RS,
239                                           CheckerContext &C) const {
240 
241   const Expr *RetE = RS->getRetValue();
242   if (!RetE)
243     return;
244   RetE = RetE->IgnoreParens();
245 
246   const LocationContext *LCtx = C.getLocationContext();
247   SVal V = C.getState()->getSVal(RetE, LCtx);
248   const MemRegion *R = V.getAsRegion();
249   if (!R)
250     return;
251 
252   if (const BlockDataRegion *B = dyn_cast<BlockDataRegion>(R))
253     checkReturnedBlockCaptures(*B, C);
254 
255   if (!isa<StackSpaceRegion>(R->getMemorySpace()) ||
256       isNotInCurrentFrame(R, C) || isArcManagedBlock(R, C))
257     return;
258 
259   // Returning a record by value is fine. (In this case, the returned
260   // expression will be a copy-constructor, possibly wrapped in an
261   // ExprWithCleanups node.)
262   if (const ExprWithCleanups *Cleanup = dyn_cast<ExprWithCleanups>(RetE))
263     RetE = Cleanup->getSubExpr();
264   if (isa<CXXConstructExpr>(RetE) && RetE->getType()->isRecordType())
265     return;
266 
267   // The CK_CopyAndAutoreleaseBlockObject cast causes the block to be copied
268   // so the stack address is not escaping here.
269   if (auto *ICE = dyn_cast<ImplicitCastExpr>(RetE)) {
270     if (isa<BlockDataRegion>(R) &&
271         ICE->getCastKind() == CK_CopyAndAutoreleaseBlockObject) {
272       return;
273     }
274   }
275 
276   EmitStackError(C, R, RetE);
277 }
278 
279 void StackAddrEscapeChecker::checkEndFunction(CheckerContext &Ctx) const {
280   ProgramStateRef State = Ctx.getState();
281 
282   // Iterate over all bindings to global variables and see if it contains
283   // a memory region in the stack space.
284   class CallBack : public StoreManager::BindingsHandler {
285   private:
286     CheckerContext &Ctx;
287     const StackFrameContext *CurSFC;
288 
289   public:
290     SmallVector<std::pair<const MemRegion *, const MemRegion *>, 10> V;
291 
292     CallBack(CheckerContext &CC)
293         : Ctx(CC), CurSFC(CC.getLocationContext()->getCurrentStackFrame()) {}
294 
295     bool HandleBinding(StoreManager &SMgr, Store S, const MemRegion *Region,
296                        SVal Val) override {
297 
298       if (!isa<GlobalsSpaceRegion>(Region->getMemorySpace()))
299         return true;
300       const MemRegion *VR = Val.getAsRegion();
301       if (VR && isa<StackSpaceRegion>(VR->getMemorySpace()) &&
302           !isArcManagedBlock(VR, Ctx) && !isNotInCurrentFrame(VR, Ctx))
303         V.emplace_back(Region, VR);
304       return true;
305     }
306   };
307 
308   CallBack Cb(Ctx);
309   State->getStateManager().getStoreManager().iterBindings(State->getStore(),
310                                                           Cb);
311 
312   if (Cb.V.empty())
313     return;
314 
315   // Generate an error node.
316   ExplodedNode *N = Ctx.generateNonFatalErrorNode(State);
317   if (!N)
318     return;
319 
320   if (!BT_stackleak)
321     BT_stackleak = llvm::make_unique<BuiltinBug>(
322         this, "Stack address stored into global variable",
323         "Stack address was saved into a global variable. "
324         "This is dangerous because the address will become "
325         "invalid after returning from the function");
326 
327   for (const auto &P : Cb.V) {
328     // Generate a report for this bug.
329     SmallString<128> Buf;
330     llvm::raw_svector_ostream Out(Buf);
331     SourceRange Range = genName(Out, P.second, Ctx.getASTContext());
332     Out << " is still referred to by the ";
333     if (isa<StaticGlobalSpaceRegion>(P.first->getMemorySpace()))
334       Out << "static";
335     else
336       Out << "global";
337     Out << " variable '";
338     const VarRegion *VR = cast<VarRegion>(P.first->getBaseRegion());
339     Out << *VR->getDecl()
340         << "' upon returning to the caller.  This will be a dangling reference";
341     auto Report = llvm::make_unique<BugReport>(*BT_stackleak, Out.str(), N);
342     if (Range.isValid())
343       Report->addRange(Range);
344 
345     Ctx.emitReport(std::move(Report));
346   }
347 }
348 
349 void ento::registerStackAddrEscapeChecker(CheckerManager &Mgr) {
350   Mgr.registerChecker<StackAddrEscapeChecker>();
351 }
352