Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 1 | //=== MallocChecker.cpp - A malloc/free checker -------------------*- 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 malloc/free checker, which checks for potential memory |
| 11 | // leaks, double free, and use-after-free problems. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 15 | #include "ClangSACheckers.h" |
Anna Zaks | f0dfc9c | 2012-02-17 22:35:31 +0000 | [diff] [blame] | 16 | #include "InterCheckerAPI.h" |
Argyrios Kyrtzidis | ec8605f | 2011-03-01 01:16:21 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/Checker.h" |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 18 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 19 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 21 | #include "clang/StaticAnalyzer/Core/PathSensitive/ObjCMessage.h" |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 22 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" |
| 23 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" |
Ted Kremenek | 9b66371 | 2011-02-10 01:03:03 +0000 | [diff] [blame] | 24 | #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h" |
Anna Zaks | 15d0ae1 | 2012-02-11 23:46:36 +0000 | [diff] [blame] | 25 | #include "clang/Basic/SourceManager.h" |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/ImmutableMap.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | 00bd44d | 2012-02-04 12:31:12 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/STLExtras.h" |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 29 | #include <climits> |
| 30 | |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 31 | using namespace clang; |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 32 | using namespace ento; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 33 | |
| 34 | namespace { |
| 35 | |
Zhongxing Xu | 7fb1464 | 2009-12-11 00:55:44 +0000 | [diff] [blame] | 36 | class RefState { |
Ted Kremenek | dde201b | 2010-08-06 21:12:55 +0000 | [diff] [blame] | 37 | enum Kind { AllocateUnchecked, AllocateFailed, Released, Escaped, |
| 38 | Relinquished } K; |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 39 | const Stmt *S; |
| 40 | |
Zhongxing Xu | 7fb1464 | 2009-12-11 00:55:44 +0000 | [diff] [blame] | 41 | public: |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 42 | RefState(Kind k, const Stmt *s) : K(k), S(s) {} |
| 43 | |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 44 | bool isAllocated() const { return K == AllocateUnchecked; } |
Chris Lattner | fae9622 | 2010-09-03 04:34:38 +0000 | [diff] [blame] | 45 | //bool isFailed() const { return K == AllocateFailed; } |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 46 | bool isReleased() const { return K == Released; } |
Chris Lattner | fae9622 | 2010-09-03 04:34:38 +0000 | [diff] [blame] | 47 | //bool isEscaped() const { return K == Escaped; } |
| 48 | //bool isRelinquished() const { return K == Relinquished; } |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 49 | const Stmt *getStmt() const { return S; } |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 50 | |
| 51 | bool operator==(const RefState &X) const { |
| 52 | return K == X.K && S == X.S; |
| 53 | } |
| 54 | |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 55 | static RefState getAllocateUnchecked(const Stmt *s) { |
| 56 | return RefState(AllocateUnchecked, s); |
| 57 | } |
| 58 | static RefState getAllocateFailed() { |
| 59 | return RefState(AllocateFailed, 0); |
| 60 | } |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 61 | static RefState getReleased(const Stmt *s) { return RefState(Released, s); } |
| 62 | static RefState getEscaped(const Stmt *s) { return RefState(Escaped, s); } |
Ted Kremenek | dde201b | 2010-08-06 21:12:55 +0000 | [diff] [blame] | 63 | static RefState getRelinquished(const Stmt *s) { |
| 64 | return RefState(Relinquished, s); |
| 65 | } |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 66 | |
| 67 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 68 | ID.AddInteger(K); |
| 69 | ID.AddPointer(S); |
| 70 | } |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 73 | struct ReallocPair { |
| 74 | SymbolRef ReallocatedSym; |
| 75 | bool IsFreeOnFailure; |
| 76 | ReallocPair(SymbolRef S, bool F) : ReallocatedSym(S), IsFreeOnFailure(F) {} |
| 77 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 78 | ID.AddInteger(IsFreeOnFailure); |
| 79 | ID.AddPointer(ReallocatedSym); |
| 80 | } |
| 81 | bool operator==(const ReallocPair &X) const { |
| 82 | return ReallocatedSym == X.ReallocatedSym && |
| 83 | IsFreeOnFailure == X.IsFreeOnFailure; |
| 84 | } |
| 85 | }; |
| 86 | |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 87 | class MallocChecker : public Checker<check::DeadSymbols, |
Ted Kremenek | e3659a7 | 2012-01-04 23:48:37 +0000 | [diff] [blame] | 88 | check::EndPath, |
| 89 | check::PreStmt<ReturnStmt>, |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 90 | check::PreStmt<CallExpr>, |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 91 | check::PostStmt<CallExpr>, |
Ted Kremenek | e3659a7 | 2012-01-04 23:48:37 +0000 | [diff] [blame] | 92 | check::Location, |
| 93 | check::Bind, |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 94 | eval::Assume, |
| 95 | check::RegionChanges> |
Ted Kremenek | e3659a7 | 2012-01-04 23:48:37 +0000 | [diff] [blame] | 96 | { |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 97 | mutable OwningPtr<BugType> BT_DoubleFree; |
| 98 | mutable OwningPtr<BugType> BT_Leak; |
| 99 | mutable OwningPtr<BugType> BT_UseFree; |
| 100 | mutable OwningPtr<BugType> BT_BadFree; |
Anna Zaks | b16ce45 | 2012-02-15 00:11:22 +0000 | [diff] [blame] | 101 | mutable IdentifierInfo *II_malloc, *II_free, *II_realloc, *II_calloc, |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 102 | *II_valloc, *II_reallocf, *II_strndup, *II_strdup; |
| 103 | |
| 104 | static const unsigned InvalidArgIndex = UINT_MAX; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 105 | |
| 106 | public: |
Anna Zaks | b16ce45 | 2012-02-15 00:11:22 +0000 | [diff] [blame] | 107 | MallocChecker() : II_malloc(0), II_free(0), II_realloc(0), II_calloc(0), |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 108 | II_valloc(0), II_reallocf(0), II_strndup(0), II_strdup(0) {} |
Anna Zaks | 231361a | 2012-02-08 23:16:52 +0000 | [diff] [blame] | 109 | |
| 110 | /// In pessimistic mode, the checker assumes that it does not know which |
| 111 | /// functions might free the memory. |
| 112 | struct ChecksFilter { |
| 113 | DefaultBool CMallocPessimistic; |
| 114 | DefaultBool CMallocOptimistic; |
| 115 | }; |
| 116 | |
| 117 | ChecksFilter Filter; |
| 118 | |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 119 | void checkPreStmt(const CallExpr *S, CheckerContext &C) const; |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 120 | void checkPostStmt(const CallExpr *CE, CheckerContext &C) const; |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 121 | void checkDeadSymbols(SymbolReaper &SymReaper, CheckerContext &C) const; |
Anna Zaks | af498a2 | 2011-10-25 19:56:48 +0000 | [diff] [blame] | 122 | void checkEndPath(CheckerContext &C) const; |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 123 | void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 124 | ProgramStateRef evalAssume(ProgramStateRef state, SVal Cond, |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 125 | bool Assumption) const; |
Anna Zaks | 390909c | 2011-10-06 00:43:15 +0000 | [diff] [blame] | 126 | void checkLocation(SVal l, bool isLoad, const Stmt *S, |
| 127 | CheckerContext &C) const; |
| 128 | void checkBind(SVal location, SVal val, const Stmt*S, |
| 129 | CheckerContext &C) const; |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 130 | ProgramStateRef |
| 131 | checkRegionChanges(ProgramStateRef state, |
| 132 | const StoreManager::InvalidatedSymbols *invalidated, |
| 133 | ArrayRef<const MemRegion *> ExplicitRegions, |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 134 | ArrayRef<const MemRegion *> Regions, |
| 135 | const CallOrObjCMessage *Call) const; |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 136 | bool wantsRegionChangeUpdate(ProgramStateRef state) const { |
| 137 | return true; |
| 138 | } |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 139 | |
Zhongxing Xu | 7b76096 | 2009-11-13 07:25:27 +0000 | [diff] [blame] | 140 | private: |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 141 | void initIdentifierInfo(ASTContext &C) const; |
| 142 | |
| 143 | /// Check if this is one of the functions which can allocate/reallocate memory |
| 144 | /// pointed to by one of its arguments. |
| 145 | bool isMemFunction(const FunctionDecl *FD, ASTContext &C) const; |
| 146 | |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 147 | static void MallocMem(CheckerContext &C, const CallExpr *CE, |
| 148 | unsigned SizeIdx); |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 149 | static void MallocMemReturnsAttr(CheckerContext &C, const CallExpr *CE, |
| 150 | const OwnershipAttr* Att); |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 151 | static ProgramStateRef MallocMemAux(CheckerContext &C, const CallExpr *CE, |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 152 | const Expr *SizeEx, SVal Init, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 153 | ProgramStateRef state) { |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 154 | return MallocMemAux(C, CE, |
| 155 | state->getSVal(SizeEx, C.getLocationContext()), |
| 156 | Init, state); |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 157 | } |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 158 | static ProgramStateRef MallocMemAux(CheckerContext &C, const CallExpr *CE, |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 159 | SVal SizeEx, SVal Init, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 160 | ProgramStateRef state); |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 161 | |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 162 | void FreeMem(CheckerContext &C, const CallExpr *CE) const; |
Jordy Rose | 2a47992 | 2010-08-12 08:54:03 +0000 | [diff] [blame] | 163 | void FreeMemAttr(CheckerContext &C, const CallExpr *CE, |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 164 | const OwnershipAttr* Att) const; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 165 | ProgramStateRef FreeMemAux(CheckerContext &C, const CallExpr *CE, |
| 166 | ProgramStateRef state, unsigned Num, |
Ted Kremenek | e3659a7 | 2012-01-04 23:48:37 +0000 | [diff] [blame] | 167 | bool Hold) const; |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 168 | |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 169 | void ReallocMem(CheckerContext &C, const CallExpr *CE, |
| 170 | bool FreesMemOnFailure) const; |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 171 | static void CallocMem(CheckerContext &C, const CallExpr *CE); |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 172 | |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 173 | bool checkEscape(SymbolRef Sym, const Stmt *S, CheckerContext &C) const; |
| 174 | bool checkUseAfterFree(SymbolRef Sym, CheckerContext &C, |
| 175 | const Stmt *S = 0) const; |
| 176 | |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 177 | /// Check if the function is not known to us. So, for example, we could |
| 178 | /// conservatively assume it can free/reallocate it's pointer arguments. |
| 179 | bool hasUnknownBehavior(const FunctionDecl *FD, ProgramStateRef State) const; |
| 180 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 181 | static bool SummarizeValue(raw_ostream &os, SVal V); |
| 182 | static bool SummarizeRegion(raw_ostream &os, const MemRegion *MR); |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 183 | void ReportBadFree(CheckerContext &C, SVal ArgVal, SourceRange range) const; |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 184 | |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 185 | void reportLeak(SymbolRef Sym, ExplodedNode *N, CheckerContext &C) const; |
| 186 | |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 187 | /// The bug visitor which allows us to print extra diagnostics along the |
| 188 | /// BugReport path. For example, showing the allocation site of the leaked |
| 189 | /// region. |
| 190 | class MallocBugVisitor : public BugReporterVisitor { |
| 191 | protected: |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 192 | enum NotificationMode { |
| 193 | Normal, |
| 194 | Complete, |
| 195 | ReallocationFailed |
| 196 | }; |
| 197 | |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 198 | // The allocated region symbol tracked by the main analysis. |
| 199 | SymbolRef Sym; |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 200 | NotificationMode Mode; |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 201 | |
| 202 | public: |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 203 | MallocBugVisitor(SymbolRef S) : Sym(S), Mode(Normal) {} |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 204 | virtual ~MallocBugVisitor() {} |
| 205 | |
| 206 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 207 | static int X = 0; |
| 208 | ID.AddPointer(&X); |
| 209 | ID.AddPointer(Sym); |
| 210 | } |
| 211 | |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 212 | inline bool isAllocated(const RefState *S, const RefState *SPrev, |
| 213 | const Stmt *Stmt) { |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 214 | // Did not track -> allocated. Other state (released) -> allocated. |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 215 | return (Stmt && isa<CallExpr>(Stmt) && |
| 216 | (S && S->isAllocated()) && (!SPrev || !SPrev->isAllocated())); |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 219 | inline bool isReleased(const RefState *S, const RefState *SPrev, |
| 220 | const Stmt *Stmt) { |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 221 | // Did not track -> released. Other state (allocated) -> released. |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 222 | return (Stmt && isa<CallExpr>(Stmt) && |
| 223 | (S && S->isReleased()) && (!SPrev || !SPrev->isReleased())); |
| 224 | } |
| 225 | |
| 226 | inline bool isReallocFailedCheck(const RefState *S, const RefState *SPrev, |
| 227 | const Stmt *Stmt) { |
| 228 | // If the expression is not a call, and the state change is |
| 229 | // released -> allocated, it must be the realloc return value |
| 230 | // check. If we have to handle more cases here, it might be cleaner just |
| 231 | // to track this extra bit in the state itself. |
| 232 | return ((!Stmt || !isa<CallExpr>(Stmt)) && |
| 233 | (S && S->isAllocated()) && (SPrev && !SPrev->isAllocated())); |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | PathDiagnosticPiece *VisitNode(const ExplodedNode *N, |
| 237 | const ExplodedNode *PrevN, |
| 238 | BugReporterContext &BRC, |
| 239 | BugReport &BR); |
| 240 | }; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 241 | }; |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 242 | } // end anonymous namespace |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 243 | |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 244 | typedef llvm::ImmutableMap<SymbolRef, RefState> RegionStateTy; |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 245 | typedef llvm::ImmutableMap<SymbolRef, ReallocPair > ReallocMap; |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 246 | class RegionState {}; |
| 247 | class ReallocPairs {}; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 248 | namespace clang { |
Ted Kremenek | 9ef6537 | 2010-12-23 07:20:52 +0000 | [diff] [blame] | 249 | namespace ento { |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 250 | template <> |
Ted Kremenek | 18c66fd | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 251 | struct ProgramStateTrait<RegionState> |
| 252 | : public ProgramStatePartialTrait<RegionStateTy> { |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 253 | static void *GDMIndex() { static int x; return &x; } |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 254 | }; |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 255 | |
| 256 | template <> |
| 257 | struct ProgramStateTrait<ReallocPairs> |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 258 | : public ProgramStatePartialTrait<ReallocMap> { |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 259 | static void *GDMIndex() { static int x; return &x; } |
| 260 | }; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 261 | } |
Argyrios Kyrtzidis | 5a4f98f | 2010-12-22 18:53:20 +0000 | [diff] [blame] | 262 | } |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 263 | |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 264 | namespace { |
| 265 | class StopTrackingCallback : public SymbolVisitor { |
| 266 | ProgramStateRef state; |
| 267 | public: |
| 268 | StopTrackingCallback(ProgramStateRef st) : state(st) {} |
| 269 | ProgramStateRef getState() const { return state; } |
| 270 | |
| 271 | bool VisitSymbol(SymbolRef sym) { |
| 272 | state = state->remove<RegionState>(sym); |
| 273 | return true; |
| 274 | } |
| 275 | }; |
| 276 | } // end anonymous namespace |
| 277 | |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 278 | void MallocChecker::initIdentifierInfo(ASTContext &Ctx) const { |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 279 | if (!II_malloc) |
| 280 | II_malloc = &Ctx.Idents.get("malloc"); |
| 281 | if (!II_free) |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 282 | II_free = &Ctx.Idents.get("free"); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 283 | if (!II_realloc) |
| 284 | II_realloc = &Ctx.Idents.get("realloc"); |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 285 | if (!II_reallocf) |
| 286 | II_reallocf = &Ctx.Idents.get("reallocf"); |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 287 | if (!II_calloc) |
| 288 | II_calloc = &Ctx.Idents.get("calloc"); |
Anna Zaks | b16ce45 | 2012-02-15 00:11:22 +0000 | [diff] [blame] | 289 | if (!II_valloc) |
| 290 | II_valloc = &Ctx.Idents.get("valloc"); |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 291 | if (!II_strdup) |
| 292 | II_strdup = &Ctx.Idents.get("strdup"); |
| 293 | if (!II_strndup) |
| 294 | II_strndup = &Ctx.Idents.get("strndup"); |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 297 | bool MallocChecker::isMemFunction(const FunctionDecl *FD, ASTContext &C) const { |
Anna Zaks | 1d6cc6a | 2012-02-15 02:12:00 +0000 | [diff] [blame] | 298 | if (!FD) |
| 299 | return false; |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 300 | IdentifierInfo *FunI = FD->getIdentifier(); |
| 301 | if (!FunI) |
| 302 | return false; |
| 303 | |
Anna Zaks | 1d6cc6a | 2012-02-15 02:12:00 +0000 | [diff] [blame] | 304 | initIdentifierInfo(C); |
| 305 | |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 306 | if (FunI == II_malloc || FunI == II_free || FunI == II_realloc || |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 307 | FunI == II_reallocf || FunI == II_calloc || FunI == II_valloc || |
| 308 | FunI == II_strdup || FunI == II_strndup) |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 309 | return true; |
| 310 | |
| 311 | if (Filter.CMallocOptimistic && FD->hasAttrs() && |
| 312 | FD->specific_attr_begin<OwnershipAttr>() != |
| 313 | FD->specific_attr_end<OwnershipAttr>()) |
| 314 | return true; |
| 315 | |
| 316 | |
| 317 | return false; |
| 318 | } |
| 319 | |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 320 | void MallocChecker::checkPostStmt(const CallExpr *CE, CheckerContext &C) const { |
| 321 | const FunctionDecl *FD = C.getCalleeDecl(CE); |
| 322 | if (!FD) |
| 323 | return; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 324 | |
Anna Zaks | b16ce45 | 2012-02-15 00:11:22 +0000 | [diff] [blame] | 325 | initIdentifierInfo(C.getASTContext()); |
| 326 | IdentifierInfo *FunI = FD->getIdentifier(); |
| 327 | if (!FunI) |
| 328 | return; |
| 329 | |
| 330 | if (FunI == II_malloc || FunI == II_valloc) { |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 331 | MallocMem(C, CE, 0); |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 332 | return; |
Anna Zaks | b16ce45 | 2012-02-15 00:11:22 +0000 | [diff] [blame] | 333 | } else if (FunI == II_realloc) { |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 334 | ReallocMem(C, CE, false); |
| 335 | return; |
| 336 | } else if (FunI == II_reallocf) { |
| 337 | ReallocMem(C, CE, true); |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 338 | return; |
Anna Zaks | b16ce45 | 2012-02-15 00:11:22 +0000 | [diff] [blame] | 339 | } else if (FunI == II_calloc) { |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 340 | CallocMem(C, CE); |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 341 | return; |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 342 | } else if (FunI == II_free) { |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 343 | FreeMem(C, CE); |
| 344 | return; |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 345 | } else if (FunI == II_strdup) { |
| 346 | MallocMem(C, CE, InvalidArgIndex); |
| 347 | return; |
| 348 | } else if (FunI == II_strndup) { |
| 349 | MallocMem(C, CE, 1); |
| 350 | return; |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 353 | if (Filter.CMallocOptimistic) |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 354 | // Check all the attributes, if there are any. |
| 355 | // There can be multiple of these attributes. |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 356 | if (FD->hasAttrs()) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 357 | for (specific_attr_iterator<OwnershipAttr> |
| 358 | i = FD->specific_attr_begin<OwnershipAttr>(), |
| 359 | e = FD->specific_attr_end<OwnershipAttr>(); |
| 360 | i != e; ++i) { |
| 361 | switch ((*i)->getOwnKind()) { |
| 362 | case OwnershipAttr::Returns: { |
| 363 | MallocMemReturnsAttr(C, CE, *i); |
Anna Zaks | 15d0ae1 | 2012-02-11 23:46:36 +0000 | [diff] [blame] | 364 | return; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 365 | } |
| 366 | case OwnershipAttr::Takes: |
| 367 | case OwnershipAttr::Holds: { |
| 368 | FreeMemAttr(C, CE, *i); |
Anna Zaks | 15d0ae1 | 2012-02-11 23:46:36 +0000 | [diff] [blame] | 369 | return; |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 370 | } |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
| 373 | } |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 376 | void MallocChecker::MallocMem(CheckerContext &C, const CallExpr *CE, |
| 377 | unsigned SizeIdx) { |
| 378 | SVal Undef = UndefinedVal(); |
| 379 | ProgramStateRef State; |
| 380 | if (SizeIdx != InvalidArgIndex) |
| 381 | State = MallocMemAux(C, CE, CE->getArg(SizeIdx), Undef, C.getState()); |
| 382 | else |
| 383 | State = MallocMemAux(C, CE, Undef, Undef, C.getState()); |
| 384 | |
| 385 | C.addTransition(State); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 388 | void MallocChecker::MallocMemReturnsAttr(CheckerContext &C, const CallExpr *CE, |
| 389 | const OwnershipAttr* Att) { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 390 | if (Att->getModule() != "malloc") |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 391 | return; |
| 392 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 393 | OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 394 | if (I != E) { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 395 | ProgramStateRef state = |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 396 | MallocMemAux(C, CE, CE->getArg(*I), UndefinedVal(), C.getState()); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 397 | C.addTransition(state); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 398 | return; |
| 399 | } |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 400 | ProgramStateRef state = MallocMemAux(C, CE, UnknownVal(), UndefinedVal(), |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 401 | C.getState()); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 402 | C.addTransition(state); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 405 | ProgramStateRef MallocChecker::MallocMemAux(CheckerContext &C, |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 406 | const CallExpr *CE, |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 407 | SVal Size, SVal Init, |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 408 | ProgramStateRef state) { |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 409 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Zhongxing Xu | a49c6b7 | 2009-12-11 03:09:01 +0000 | [diff] [blame] | 410 | |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 411 | // Get the return value. |
| 412 | SVal retVal = state->getSVal(CE, C.getLocationContext()); |
Zhongxing Xu | a49c6b7 | 2009-12-11 03:09:01 +0000 | [diff] [blame] | 413 | |
Anna Zaks | b16ce45 | 2012-02-15 00:11:22 +0000 | [diff] [blame] | 414 | // We expect the malloc functions to return a pointer. |
| 415 | if (!isa<Loc>(retVal)) |
| 416 | return 0; |
| 417 | |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 418 | // Fill the region with the initialization value. |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 419 | state = state->bindDefault(retVal, Init); |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 420 | |
Jordy Rose | 32f2656 | 2010-07-04 00:00:41 +0000 | [diff] [blame] | 421 | // Set the region's extent equal to the Size parameter. |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 422 | const SymbolicRegion *R = |
| 423 | dyn_cast_or_null<SymbolicRegion>(retVal.getAsRegion()); |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 424 | if (!R) |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 425 | return 0; |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 426 | if (isa<DefinedOrUnknownSVal>(Size)) { |
| 427 | DefinedOrUnknownSVal Extent = R->getExtent(svalBuilder); |
| 428 | DefinedOrUnknownSVal DefinedSize = cast<DefinedOrUnknownSVal>(Size); |
| 429 | DefinedOrUnknownSVal extentMatchesSize = |
| 430 | svalBuilder.evalEQ(state, Extent, DefinedSize); |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 431 | |
Anna Zaks | 60a1fa4 | 2012-02-22 03:14:20 +0000 | [diff] [blame] | 432 | state = state->assume(extentMatchesSize, true); |
| 433 | assert(state); |
| 434 | } |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 435 | |
| 436 | SymbolRef Sym = retVal.getAsLocSymbol(); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 437 | assert(Sym); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 438 | |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 439 | // Set the symbol's state to Allocated. |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 440 | return state->set<RegionState>(Sym, RefState::getAllocateUnchecked(CE)); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 443 | void MallocChecker::FreeMem(CheckerContext &C, const CallExpr *CE) const { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 444 | ProgramStateRef state = FreeMemAux(C, CE, C.getState(), 0, false); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 445 | |
| 446 | if (state) |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 447 | C.addTransition(state); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 450 | void MallocChecker::FreeMemAttr(CheckerContext &C, const CallExpr *CE, |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 451 | const OwnershipAttr* Att) const { |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 452 | if (Att->getModule() != "malloc") |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 453 | return; |
| 454 | |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 455 | for (OwnershipAttr::args_iterator I = Att->args_begin(), E = Att->args_end(); |
| 456 | I != E; ++I) { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 457 | ProgramStateRef state = |
Ted Kremenek | e3659a7 | 2012-01-04 23:48:37 +0000 | [diff] [blame] | 458 | FreeMemAux(C, CE, C.getState(), *I, |
| 459 | Att->getOwnKind() == OwnershipAttr::Holds); |
Sean Hunt | cf807c4 | 2010-08-18 23:23:40 +0000 | [diff] [blame] | 460 | if (state) |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 461 | C.addTransition(state); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 465 | ProgramStateRef MallocChecker::FreeMemAux(CheckerContext &C, |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 466 | const CallExpr *CE, |
| 467 | ProgramStateRef state, |
| 468 | unsigned Num, |
| 469 | bool Hold) const { |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 470 | const Expr *ArgExpr = CE->getArg(Num); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 471 | SVal ArgVal = state->getSVal(ArgExpr, C.getLocationContext()); |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 472 | if (!isa<DefinedOrUnknownSVal>(ArgVal)) |
| 473 | return 0; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 474 | DefinedOrUnknownSVal location = cast<DefinedOrUnknownSVal>(ArgVal); |
| 475 | |
| 476 | // Check for null dereferences. |
| 477 | if (!isa<Loc>(location)) |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 478 | return 0; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 479 | |
Anna Zaks | b276bd9 | 2012-02-14 00:26:13 +0000 | [diff] [blame] | 480 | // The explicit NULL case, no operation is performed. |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 481 | ProgramStateRef notNullState, nullState; |
Ted Kremenek | 28f47b9 | 2010-12-01 22:16:56 +0000 | [diff] [blame] | 482 | llvm::tie(notNullState, nullState) = state->assume(location); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 483 | if (nullState && !notNullState) |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 484 | return 0; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 485 | |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 486 | // Unknown values could easily be okay |
| 487 | // Undefined values are handled elsewhere |
| 488 | if (ArgVal.isUnknownOrUndef()) |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 489 | return 0; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 490 | |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 491 | const MemRegion *R = ArgVal.getAsRegion(); |
| 492 | |
| 493 | // Nonlocs can't be freed, of course. |
| 494 | // Non-region locations (labels and fixed addresses) also shouldn't be freed. |
| 495 | if (!R) { |
| 496 | ReportBadFree(C, ArgVal, ArgExpr->getSourceRange()); |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 497 | return 0; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | R = R->StripCasts(); |
| 501 | |
| 502 | // Blocks might show up as heap data, but should not be free()d |
| 503 | if (isa<BlockDataRegion>(R)) { |
| 504 | ReportBadFree(C, ArgVal, ArgExpr->getSourceRange()); |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 505 | return 0; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | const MemSpaceRegion *MS = R->getMemorySpace(); |
| 509 | |
| 510 | // Parameters, locals, statics, and globals shouldn't be freed. |
| 511 | if (!(isa<UnknownSpaceRegion>(MS) || isa<HeapSpaceRegion>(MS))) { |
| 512 | // FIXME: at the time this code was written, malloc() regions were |
| 513 | // represented by conjured symbols, which are all in UnknownSpaceRegion. |
| 514 | // This means that there isn't actually anything from HeapSpaceRegion |
| 515 | // that should be freed, even though we allow it here. |
| 516 | // Of course, free() can work on memory allocated outside the current |
| 517 | // function, so UnknownSpaceRegion is always a possibility. |
| 518 | // False negatives are better than false positives. |
| 519 | |
| 520 | ReportBadFree(C, ArgVal, ArgExpr->getSourceRange()); |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 521 | return 0; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R); |
| 525 | // Various cases could lead to non-symbol values here. |
| 526 | // For now, ignore them. |
| 527 | if (!SR) |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 528 | return 0; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 529 | |
| 530 | SymbolRef Sym = SR->getSymbol(); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 531 | const RefState *RS = state->get<RegionState>(Sym); |
Zhongxing Xu | 7e3cda9 | 2010-01-18 03:27:34 +0000 | [diff] [blame] | 532 | |
| 533 | // If the symbol has not been tracked, return. This is possible when free() is |
| 534 | // called on a pointer that does not get its pointee directly from malloc(). |
| 535 | // Full support of this requires inter-procedural analysis. |
| 536 | if (!RS) |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 537 | return 0; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 538 | |
| 539 | // Check double free. |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 540 | if (RS->isReleased()) { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 541 | if (ExplodedNode *N = C.generateSink()) { |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 542 | if (!BT_DoubleFree) |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 543 | BT_DoubleFree.reset( |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 544 | new BugType("Double free", "Memory Error")); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 545 | BugReport *R = new BugReport(*BT_DoubleFree, |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 546 | "Attempt to free released memory", N); |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 547 | R->addRange(ArgExpr->getSourceRange()); |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 548 | R->addVisitor(new MallocBugVisitor(Sym)); |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 549 | C.EmitReport(R); |
| 550 | } |
Anna Zaks | b319e02 | 2012-02-08 20:13:28 +0000 | [diff] [blame] | 551 | return 0; |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | // Normal free. |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 555 | if (Hold) |
Anna Zaks | b276bd9 | 2012-02-14 00:26:13 +0000 | [diff] [blame] | 556 | return state->set<RegionState>(Sym, RefState::getRelinquished(CE)); |
| 557 | return state->set<RegionState>(Sym, RefState::getReleased(CE)); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 560 | bool MallocChecker::SummarizeValue(raw_ostream &os, SVal V) { |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 561 | if (nonloc::ConcreteInt *IntVal = dyn_cast<nonloc::ConcreteInt>(&V)) |
| 562 | os << "an integer (" << IntVal->getValue() << ")"; |
| 563 | else if (loc::ConcreteInt *ConstAddr = dyn_cast<loc::ConcreteInt>(&V)) |
| 564 | os << "a constant address (" << ConstAddr->getValue() << ")"; |
| 565 | else if (loc::GotoLabel *Label = dyn_cast<loc::GotoLabel>(&V)) |
Chris Lattner | 6810630 | 2011-02-17 05:38:27 +0000 | [diff] [blame] | 566 | os << "the address of the label '" << Label->getLabel()->getName() << "'"; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 567 | else |
| 568 | return false; |
| 569 | |
| 570 | return true; |
| 571 | } |
| 572 | |
Ted Kremenek | 9c378f7 | 2011-08-12 23:37:29 +0000 | [diff] [blame] | 573 | bool MallocChecker::SummarizeRegion(raw_ostream &os, |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 574 | const MemRegion *MR) { |
| 575 | switch (MR->getKind()) { |
| 576 | case MemRegion::FunctionTextRegionKind: { |
| 577 | const FunctionDecl *FD = cast<FunctionTextRegion>(MR)->getDecl(); |
| 578 | if (FD) |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 579 | os << "the address of the function '" << *FD << '\''; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 580 | else |
| 581 | os << "the address of a function"; |
| 582 | return true; |
| 583 | } |
| 584 | case MemRegion::BlockTextRegionKind: |
| 585 | os << "block text"; |
| 586 | return true; |
| 587 | case MemRegion::BlockDataRegionKind: |
| 588 | // FIXME: where the block came from? |
| 589 | os << "a block"; |
| 590 | return true; |
| 591 | default: { |
| 592 | const MemSpaceRegion *MS = MR->getMemorySpace(); |
| 593 | |
Anna Zaks | eb31a76 | 2012-01-04 23:54:01 +0000 | [diff] [blame] | 594 | if (isa<StackLocalsSpaceRegion>(MS)) { |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 595 | const VarRegion *VR = dyn_cast<VarRegion>(MR); |
| 596 | const VarDecl *VD; |
| 597 | if (VR) |
| 598 | VD = VR->getDecl(); |
| 599 | else |
| 600 | VD = NULL; |
| 601 | |
| 602 | if (VD) |
| 603 | os << "the address of the local variable '" << VD->getName() << "'"; |
| 604 | else |
| 605 | os << "the address of a local stack variable"; |
| 606 | return true; |
| 607 | } |
Anna Zaks | eb31a76 | 2012-01-04 23:54:01 +0000 | [diff] [blame] | 608 | |
| 609 | if (isa<StackArgumentsSpaceRegion>(MS)) { |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 610 | const VarRegion *VR = dyn_cast<VarRegion>(MR); |
| 611 | const VarDecl *VD; |
| 612 | if (VR) |
| 613 | VD = VR->getDecl(); |
| 614 | else |
| 615 | VD = NULL; |
| 616 | |
| 617 | if (VD) |
| 618 | os << "the address of the parameter '" << VD->getName() << "'"; |
| 619 | else |
| 620 | os << "the address of a parameter"; |
| 621 | return true; |
| 622 | } |
Anna Zaks | eb31a76 | 2012-01-04 23:54:01 +0000 | [diff] [blame] | 623 | |
| 624 | if (isa<GlobalsSpaceRegion>(MS)) { |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 625 | const VarRegion *VR = dyn_cast<VarRegion>(MR); |
| 626 | const VarDecl *VD; |
| 627 | if (VR) |
| 628 | VD = VR->getDecl(); |
| 629 | else |
| 630 | VD = NULL; |
| 631 | |
| 632 | if (VD) { |
| 633 | if (VD->isStaticLocal()) |
| 634 | os << "the address of the static variable '" << VD->getName() << "'"; |
| 635 | else |
| 636 | os << "the address of the global variable '" << VD->getName() << "'"; |
| 637 | } else |
| 638 | os << "the address of a global variable"; |
| 639 | return true; |
| 640 | } |
Anna Zaks | eb31a76 | 2012-01-04 23:54:01 +0000 | [diff] [blame] | 641 | |
| 642 | return false; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 643 | } |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | void MallocChecker::ReportBadFree(CheckerContext &C, SVal ArgVal, |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 648 | SourceRange range) const { |
Ted Kremenek | d048c6e | 2010-12-20 21:19:09 +0000 | [diff] [blame] | 649 | if (ExplodedNode *N = C.generateSink()) { |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 650 | if (!BT_BadFree) |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 651 | BT_BadFree.reset(new BugType("Bad free", "Memory Error")); |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 652 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 653 | SmallString<100> buf; |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 654 | llvm::raw_svector_ostream os(buf); |
| 655 | |
| 656 | const MemRegion *MR = ArgVal.getAsRegion(); |
| 657 | if (MR) { |
| 658 | while (const ElementRegion *ER = dyn_cast<ElementRegion>(MR)) |
| 659 | MR = ER->getSuperRegion(); |
| 660 | |
| 661 | // Special case for alloca() |
| 662 | if (isa<AllocaRegion>(MR)) |
| 663 | os << "Argument to free() was allocated by alloca(), not malloc()"; |
| 664 | else { |
| 665 | os << "Argument to free() is "; |
| 666 | if (SummarizeRegion(os, MR)) |
| 667 | os << ", which is not memory allocated by malloc()"; |
| 668 | else |
| 669 | os << "not memory allocated by malloc()"; |
| 670 | } |
| 671 | } else { |
| 672 | os << "Argument to free() is "; |
| 673 | if (SummarizeValue(os, ArgVal)) |
| 674 | os << ", which is not memory allocated by malloc()"; |
| 675 | else |
| 676 | os << "not memory allocated by malloc()"; |
| 677 | } |
| 678 | |
Anna Zaks | e172e8b | 2011-08-17 23:00:25 +0000 | [diff] [blame] | 679 | BugReport *R = new BugReport(*BT_BadFree, os.str(), N); |
Jordy Rose | 43859f6 | 2010-06-07 19:32:37 +0000 | [diff] [blame] | 680 | R->addRange(range); |
| 681 | C.EmitReport(R); |
| 682 | } |
| 683 | } |
| 684 | |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 685 | void MallocChecker::ReallocMem(CheckerContext &C, const CallExpr *CE, |
| 686 | bool FreesOnFail) const { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 687 | ProgramStateRef state = C.getState(); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 688 | const Expr *arg0Expr = CE->getArg(0); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 689 | const LocationContext *LCtx = C.getLocationContext(); |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 690 | SVal Arg0Val = state->getSVal(arg0Expr, LCtx); |
| 691 | if (!isa<DefinedOrUnknownSVal>(Arg0Val)) |
| 692 | return; |
| 693 | DefinedOrUnknownSVal arg0Val = cast<DefinedOrUnknownSVal>(Arg0Val); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 694 | |
Ted Kremenek | 846eabd | 2010-12-01 21:28:31 +0000 | [diff] [blame] | 695 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 696 | |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 697 | DefinedOrUnknownSVal PtrEQ = |
| 698 | svalBuilder.evalEQ(state, arg0Val, svalBuilder.makeNull()); |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 699 | |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 700 | // Get the size argument. If there is no size arg then give up. |
| 701 | const Expr *Arg1 = CE->getArg(1); |
| 702 | if (!Arg1) |
| 703 | return; |
| 704 | |
| 705 | // Get the value of the size argument. |
Anna Zaks | e9ef562 | 2012-02-10 01:11:00 +0000 | [diff] [blame] | 706 | SVal Arg1ValG = state->getSVal(Arg1, LCtx); |
| 707 | if (!isa<DefinedOrUnknownSVal>(Arg1ValG)) |
| 708 | return; |
| 709 | DefinedOrUnknownSVal Arg1Val = cast<DefinedOrUnknownSVal>(Arg1ValG); |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 710 | |
| 711 | // Compare the size argument to 0. |
| 712 | DefinedOrUnknownSVal SizeZero = |
| 713 | svalBuilder.evalEQ(state, Arg1Val, |
| 714 | svalBuilder.makeIntValWithPtrWidth(0, false)); |
| 715 | |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 716 | ProgramStateRef StatePtrIsNull, StatePtrNotNull; |
| 717 | llvm::tie(StatePtrIsNull, StatePtrNotNull) = state->assume(PtrEQ); |
| 718 | ProgramStateRef StateSizeIsZero, StateSizeNotZero; |
| 719 | llvm::tie(StateSizeIsZero, StateSizeNotZero) = state->assume(SizeZero); |
| 720 | // We only assume exceptional states if they are definitely true; if the |
| 721 | // state is under-constrained, assume regular realloc behavior. |
| 722 | bool PrtIsNull = StatePtrIsNull && !StatePtrNotNull; |
| 723 | bool SizeIsZero = StateSizeIsZero && !StateSizeNotZero; |
| 724 | |
Lenny Maiorani | 4d8d803 | 2011-04-27 14:49:29 +0000 | [diff] [blame] | 725 | // If the ptr is NULL and the size is not 0, the call is equivalent to |
| 726 | // malloc(size). |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 727 | if ( PrtIsNull && !SizeIsZero) { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 728 | ProgramStateRef stateMalloc = MallocMemAux(C, CE, CE->getArg(1), |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 729 | UndefinedVal(), StatePtrIsNull); |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 730 | C.addTransition(stateMalloc); |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 731 | return; |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 734 | if (PrtIsNull && SizeIsZero) |
| 735 | return; |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 736 | |
Anna Zaks | 30838b9 | 2012-02-13 20:57:07 +0000 | [diff] [blame] | 737 | // Get the from and to pointer symbols as in toPtr = realloc(fromPtr, size). |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 738 | assert(!PrtIsNull); |
Anna Zaks | 30838b9 | 2012-02-13 20:57:07 +0000 | [diff] [blame] | 739 | SymbolRef FromPtr = arg0Val.getAsSymbol(); |
| 740 | SVal RetVal = state->getSVal(CE, LCtx); |
| 741 | SymbolRef ToPtr = RetVal.getAsSymbol(); |
| 742 | if (!FromPtr || !ToPtr) |
| 743 | return; |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 744 | |
| 745 | // If the size is 0, free the memory. |
| 746 | if (SizeIsZero) |
| 747 | if (ProgramStateRef stateFree = FreeMemAux(C, CE, StateSizeIsZero,0,false)){ |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 748 | // The semantics of the return value are: |
| 749 | // If size was equal to 0, either NULL or a pointer suitable to be passed |
| 750 | // to free() is returned. |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 751 | stateFree = stateFree->set<ReallocPairs>(ToPtr, |
| 752 | ReallocPair(FromPtr, FreesOnFail)); |
Anna Zaks | b276bd9 | 2012-02-14 00:26:13 +0000 | [diff] [blame] | 753 | C.getSymbolManager().addSymbolDependency(ToPtr, FromPtr); |
Anna Zaks | 30838b9 | 2012-02-13 20:57:07 +0000 | [diff] [blame] | 754 | C.addTransition(stateFree); |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 755 | return; |
| 756 | } |
| 757 | |
| 758 | // Default behavior. |
| 759 | if (ProgramStateRef stateFree = FreeMemAux(C, CE, state, 0, false)) { |
| 760 | // FIXME: We should copy the content of the original buffer. |
| 761 | ProgramStateRef stateRealloc = MallocMemAux(C, CE, CE->getArg(1), |
| 762 | UnknownVal(), stateFree); |
Anna Zaks | 30838b9 | 2012-02-13 20:57:07 +0000 | [diff] [blame] | 763 | if (!stateRealloc) |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 764 | return; |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 765 | stateRealloc = stateRealloc->set<ReallocPairs>(ToPtr, |
| 766 | ReallocPair(FromPtr, FreesOnFail)); |
Anna Zaks | b276bd9 | 2012-02-14 00:26:13 +0000 | [diff] [blame] | 767 | C.getSymbolManager().addSymbolDependency(ToPtr, FromPtr); |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 768 | C.addTransition(stateRealloc); |
| 769 | return; |
Zhongxing Xu | d9c84c8 | 2009-12-12 12:29:38 +0000 | [diff] [blame] | 770 | } |
Zhongxing Xu | 589c0f2 | 2009-11-12 08:38:56 +0000 | [diff] [blame] | 771 | } |
Zhongxing Xu | 7b76096 | 2009-11-13 07:25:27 +0000 | [diff] [blame] | 772 | |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 773 | void MallocChecker::CallocMem(CheckerContext &C, const CallExpr *CE) { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 774 | ProgramStateRef state = C.getState(); |
Ted Kremenek | 846eabd | 2010-12-01 21:28:31 +0000 | [diff] [blame] | 775 | SValBuilder &svalBuilder = C.getSValBuilder(); |
Ted Kremenek | 5eca482 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 776 | const LocationContext *LCtx = C.getLocationContext(); |
| 777 | SVal count = state->getSVal(CE->getArg(0), LCtx); |
| 778 | SVal elementSize = state->getSVal(CE->getArg(1), LCtx); |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 779 | SVal TotalSize = svalBuilder.evalBinOp(state, BO_Mul, count, elementSize, |
| 780 | svalBuilder.getContext().getSizeType()); |
| 781 | SVal zeroVal = svalBuilder.makeZeroVal(svalBuilder.getContext().CharTy); |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 782 | |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 783 | C.addTransition(MallocMemAux(C, CE, TotalSize, zeroVal, state)); |
Zhongxing Xu | a5ce966 | 2010-06-01 03:01:33 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 786 | void MallocChecker::reportLeak(SymbolRef Sym, ExplodedNode *N, |
| 787 | CheckerContext &C) const { |
| 788 | assert(N); |
| 789 | if (!BT_Leak) { |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 790 | BT_Leak.reset(new BugType("Memory leak", "Memory Error")); |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 791 | // Leaks should not be reported if they are post-dominated by a sink: |
| 792 | // (1) Sinks are higher importance bugs. |
| 793 | // (2) NoReturnFunctionChecker uses sink nodes to represent paths ending |
| 794 | // with __noreturn functions such as assert() or exit(). We choose not |
| 795 | // to report leaks on such paths. |
| 796 | BT_Leak->setSuppressOnSink(true); |
| 797 | } |
| 798 | |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 799 | BugReport *R = new BugReport(*BT_Leak, |
| 800 | "Memory is never released; potential memory leak", N); |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 801 | R->addVisitor(new MallocBugVisitor(Sym)); |
| 802 | C.EmitReport(R); |
| 803 | } |
| 804 | |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 805 | void MallocChecker::checkDeadSymbols(SymbolReaper &SymReaper, |
| 806 | CheckerContext &C) const |
Ted Kremenek | c8413fd | 2010-12-02 07:49:45 +0000 | [diff] [blame] | 807 | { |
Zhongxing Xu | 173ff56 | 2010-08-15 08:19:57 +0000 | [diff] [blame] | 808 | if (!SymReaper.hasDeadSymbols()) |
| 809 | return; |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 810 | |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 811 | ProgramStateRef state = C.getState(); |
Zhongxing Xu | 173ff56 | 2010-08-15 08:19:57 +0000 | [diff] [blame] | 812 | RegionStateTy RS = state->get<RegionState>(); |
Jordy Rose | 9076014 | 2010-08-18 04:33:47 +0000 | [diff] [blame] | 813 | RegionStateTy::Factory &F = state->get_context<RegionState>(); |
Zhongxing Xu | 173ff56 | 2010-08-15 08:19:57 +0000 | [diff] [blame] | 814 | |
Ted Kremenek | 217470e | 2011-07-28 23:07:51 +0000 | [diff] [blame] | 815 | bool generateReport = false; |
Anna Zaks | f8c17b7 | 2012-02-09 06:48:19 +0000 | [diff] [blame] | 816 | llvm::SmallVector<SymbolRef, 2> Errors; |
Zhongxing Xu | 173ff56 | 2010-08-15 08:19:57 +0000 | [diff] [blame] | 817 | for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) { |
| 818 | if (SymReaper.isDead(I->first)) { |
Anna Zaks | f8c17b7 | 2012-02-09 06:48:19 +0000 | [diff] [blame] | 819 | if (I->second.isAllocated()) { |
Ted Kremenek | 217470e | 2011-07-28 23:07:51 +0000 | [diff] [blame] | 820 | generateReport = true; |
Anna Zaks | f8c17b7 | 2012-02-09 06:48:19 +0000 | [diff] [blame] | 821 | Errors.push_back(I->first); |
| 822 | } |
Jordy Rose | 9076014 | 2010-08-18 04:33:47 +0000 | [diff] [blame] | 823 | // Remove the dead symbol from the map. |
Ted Kremenek | 3baf672 | 2010-11-24 00:54:37 +0000 | [diff] [blame] | 824 | RS = F.remove(RS, I->first); |
Ted Kremenek | 217470e | 2011-07-28 23:07:51 +0000 | [diff] [blame] | 825 | |
Zhongxing Xu | fc7ac8f | 2009-11-13 07:48:11 +0000 | [diff] [blame] | 826 | } |
| 827 | } |
Ted Kremenek | 217470e | 2011-07-28 23:07:51 +0000 | [diff] [blame] | 828 | |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 829 | // Cleanup the Realloc Pairs Map. |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 830 | ReallocMap RP = state->get<ReallocPairs>(); |
| 831 | for (ReallocMap::iterator I = RP.begin(), E = RP.end(); I != E; ++I) { |
| 832 | if (SymReaper.isDead(I->first) || |
| 833 | SymReaper.isDead(I->second.ReallocatedSym)) { |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 834 | state = state->remove<ReallocPairs>(I->first); |
| 835 | } |
| 836 | } |
| 837 | |
Anna Zaks | 0bd6b11 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 838 | ExplodedNode *N = C.addTransition(state->set<RegionState>(RS)); |
Ted Kremenek | 217470e | 2011-07-28 23:07:51 +0000 | [diff] [blame] | 839 | |
Ted Kremenek | 217470e | 2011-07-28 23:07:51 +0000 | [diff] [blame] | 840 | if (N && generateReport) { |
Anna Zaks | f8c17b7 | 2012-02-09 06:48:19 +0000 | [diff] [blame] | 841 | for (llvm::SmallVector<SymbolRef, 2>::iterator |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 842 | I = Errors.begin(), E = Errors.end(); I != E; ++I) { |
| 843 | reportLeak(*I, N, C); |
Anna Zaks | f8c17b7 | 2012-02-09 06:48:19 +0000 | [diff] [blame] | 844 | } |
Ted Kremenek | 217470e | 2011-07-28 23:07:51 +0000 | [diff] [blame] | 845 | } |
Zhongxing Xu | 7b76096 | 2009-11-13 07:25:27 +0000 | [diff] [blame] | 846 | } |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 847 | |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 848 | void MallocChecker::checkEndPath(CheckerContext &C) const { |
| 849 | ProgramStateRef state = C.getState(); |
Jordy Rose | 09cef09 | 2010-08-18 04:26:59 +0000 | [diff] [blame] | 850 | RegionStateTy M = state->get<RegionState>(); |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 851 | |
Anna Zaks | a19581a | 2012-02-20 22:25:23 +0000 | [diff] [blame] | 852 | // If inside inlined call, skip it. |
| 853 | if (C.getLocationContext()->getParent() != 0) |
| 854 | return; |
| 855 | |
Jordy Rose | 09cef09 | 2010-08-18 04:26:59 +0000 | [diff] [blame] | 856 | for (RegionStateTy::iterator I = M.begin(), E = M.end(); I != E; ++I) { |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 857 | RefState RS = I->second; |
| 858 | if (RS.isAllocated()) { |
Anna Zaks | da04677 | 2012-02-11 21:02:40 +0000 | [diff] [blame] | 859 | ExplodedNode *N = C.addTransition(state); |
| 860 | if (N) |
| 861 | reportLeak(I->first, N, C); |
Zhongxing Xu | 243fde9 | 2009-11-17 07:54:15 +0000 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | } |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 865 | |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 866 | bool MallocChecker::checkEscape(SymbolRef Sym, const Stmt *S, |
| 867 | CheckerContext &C) const { |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 868 | ProgramStateRef state = C.getState(); |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 869 | const RefState *RS = state->get<RegionState>(Sym); |
| 870 | if (!RS) |
| 871 | return false; |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 872 | |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 873 | if (RS->isAllocated()) { |
| 874 | state = state->set<RegionState>(Sym, RefState::getEscaped(S)); |
| 875 | C.addTransition(state); |
| 876 | return true; |
| 877 | } |
| 878 | return false; |
| 879 | } |
| 880 | |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 881 | void MallocChecker::checkPreStmt(const CallExpr *CE, CheckerContext &C) const { |
| 882 | if (isMemFunction(C.getCalleeDecl(CE), C.getASTContext())) |
| 883 | return; |
| 884 | |
| 885 | // Check use after free, when a freed pointer is passed to a call. |
| 886 | ProgramStateRef State = C.getState(); |
| 887 | for (CallExpr::const_arg_iterator I = CE->arg_begin(), |
| 888 | E = CE->arg_end(); I != E; ++I) { |
| 889 | const Expr *A = *I; |
| 890 | if (A->getType().getTypePtr()->isAnyPointerType()) { |
| 891 | SymbolRef Sym = State->getSVal(A, C.getLocationContext()).getAsSymbol(); |
| 892 | if (!Sym) |
| 893 | continue; |
| 894 | if (checkUseAfterFree(Sym, C, A)) |
| 895 | return; |
| 896 | } |
| 897 | } |
| 898 | } |
| 899 | |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 900 | void MallocChecker::checkPreStmt(const ReturnStmt *S, CheckerContext &C) const { |
| 901 | const Expr *E = S->getRetValue(); |
| 902 | if (!E) |
| 903 | return; |
Anna Zaks | 0860cd0 | 2012-02-11 21:44:39 +0000 | [diff] [blame] | 904 | |
| 905 | // Check if we are returning a symbol. |
Anna Zaks | d9ab7bb | 2012-02-22 02:36:01 +0000 | [diff] [blame] | 906 | SVal RetVal = C.getState()->getSVal(E, C.getLocationContext()); |
| 907 | SymbolRef Sym = RetVal.getAsSymbol(); |
| 908 | if (!Sym) |
| 909 | // If we are returning a field of the allocated struct or an array element, |
| 910 | // the callee could still free the memory. |
| 911 | // TODO: This logic should be a part of generic symbol escape callback. |
| 912 | if (const MemRegion *MR = RetVal.getAsRegion()) |
| 913 | if (isa<FieldRegion>(MR) || isa<ElementRegion>(MR)) |
| 914 | if (const SymbolicRegion *BMR = |
| 915 | dyn_cast<SymbolicRegion>(MR->getBaseRegion())) |
| 916 | Sym = BMR->getSymbol(); |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 917 | if (!Sym) |
| 918 | return; |
| 919 | |
Anna Zaks | 0860cd0 | 2012-02-11 21:44:39 +0000 | [diff] [blame] | 920 | // Check if we are returning freed memory. |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 921 | if (checkUseAfterFree(Sym, C, E)) |
Anna Zaks | 15d0ae1 | 2012-02-11 23:46:36 +0000 | [diff] [blame] | 922 | return; |
Anna Zaks | 0860cd0 | 2012-02-11 21:44:39 +0000 | [diff] [blame] | 923 | |
Anna Zaks | a19581a | 2012-02-20 22:25:23 +0000 | [diff] [blame] | 924 | // If this function body is not inlined, check if the symbol is escaping. |
| 925 | if (C.getLocationContext()->getParent() == 0) |
| 926 | checkEscape(Sym, E, C); |
Zhongxing Xu | 4985e3e | 2009-11-17 08:58:18 +0000 | [diff] [blame] | 927 | } |
Zhongxing Xu | b94b81a | 2009-12-31 06:13:07 +0000 | [diff] [blame] | 928 | |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 929 | bool MallocChecker::checkUseAfterFree(SymbolRef Sym, CheckerContext &C, |
| 930 | const Stmt *S) const { |
| 931 | assert(Sym); |
| 932 | const RefState *RS = C.getState()->get<RegionState>(Sym); |
| 933 | if (RS && RS->isReleased()) { |
Anna Zaks | 15d0ae1 | 2012-02-11 23:46:36 +0000 | [diff] [blame] | 934 | if (ExplodedNode *N = C.generateSink()) { |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 935 | if (!BT_UseFree) |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 936 | BT_UseFree.reset(new BugType("Use-after-free", "Memory Error")); |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 937 | |
Anna Zaks | febdc32 | 2012-02-16 22:26:12 +0000 | [diff] [blame] | 938 | BugReport *R = new BugReport(*BT_UseFree, |
| 939 | "Use of memory after it is freed",N); |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 940 | if (S) |
| 941 | R->addRange(S->getSourceRange()); |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 942 | R->addVisitor(new MallocBugVisitor(Sym)); |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 943 | C.EmitReport(R); |
| 944 | return true; |
| 945 | } |
| 946 | } |
| 947 | return false; |
| 948 | } |
| 949 | |
Zhongxing Xu | c802378 | 2010-03-10 04:58:55 +0000 | [diff] [blame] | 950 | // Check if the location is a freed symbolic region. |
Anna Zaks | 390909c | 2011-10-06 00:43:15 +0000 | [diff] [blame] | 951 | void MallocChecker::checkLocation(SVal l, bool isLoad, const Stmt *S, |
| 952 | CheckerContext &C) const { |
Zhongxing Xu | c802378 | 2010-03-10 04:58:55 +0000 | [diff] [blame] | 953 | SymbolRef Sym = l.getLocSymbolInBase(); |
Anna Zaks | 91c2a11 | 2012-02-08 23:16:56 +0000 | [diff] [blame] | 954 | if (Sym) |
| 955 | checkUseAfterFree(Sym, C); |
Zhongxing Xu | c802378 | 2010-03-10 04:58:55 +0000 | [diff] [blame] | 956 | } |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 957 | |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 958 | //===----------------------------------------------------------------------===// |
| 959 | // Check various ways a symbol can be invalidated. |
| 960 | // TODO: This logic (the next 3 functions) is copied/similar to the |
| 961 | // RetainRelease checker. We might want to factor this out. |
| 962 | //===----------------------------------------------------------------------===// |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 963 | |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 964 | // Stop tracking symbols when a value escapes as a result of checkBind. |
| 965 | // A value escapes in three possible cases: |
| 966 | // (1) we are binding to something that is not a memory region. |
| 967 | // (2) we are binding to a memregion that does not have stack storage |
| 968 | // (3) we are binding to a memregion with stack storage that the store |
| 969 | // does not understand. |
| 970 | void MallocChecker::checkBind(SVal loc, SVal val, const Stmt *S, |
| 971 | CheckerContext &C) const { |
| 972 | // Are we storing to something that causes the value to "escape"? |
| 973 | bool escapes = true; |
Ted Kremenek | 8bef823 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 974 | ProgramStateRef state = C.getState(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 975 | |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 976 | if (loc::MemRegionVal *regionLoc = dyn_cast<loc::MemRegionVal>(&loc)) { |
| 977 | escapes = !regionLoc->getRegion()->hasStackStorage(); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 978 | |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 979 | if (!escapes) { |
| 980 | // To test (3), generate a new state with the binding added. If it is |
| 981 | // the same state, then it escapes (since the store cannot represent |
| 982 | // the binding). |
| 983 | escapes = (state == (state->bindLoc(*regionLoc, val))); |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 984 | } |
Anna Zaks | ac59300 | 2012-02-16 03:40:57 +0000 | [diff] [blame] | 985 | if (!escapes) { |
| 986 | // Case 4: We do not currently model what happens when a symbol is |
| 987 | // assigned to a struct field, so be conservative here and let the symbol |
| 988 | // go. TODO: This could definitely be improved upon. |
| 989 | escapes = !isa<VarRegion>(regionLoc->getRegion()); |
| 990 | } |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 991 | } |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 992 | |
| 993 | // If our store can represent the binding and we aren't storing to something |
| 994 | // that doesn't have local storage then just return and have the simulation |
| 995 | // state continue as is. |
| 996 | if (!escapes) |
| 997 | return; |
| 998 | |
| 999 | // Otherwise, find all symbols referenced by 'val' that we are tracking |
| 1000 | // and stop tracking them. |
| 1001 | state = state->scanReachableSymbols<StopTrackingCallback>(val).getState(); |
| 1002 | C.addTransition(state); |
| 1003 | } |
| 1004 | |
| 1005 | // If a symbolic region is assumed to NULL (or another constant), stop tracking |
| 1006 | // it - assuming that allocation failed on this path. |
| 1007 | ProgramStateRef MallocChecker::evalAssume(ProgramStateRef state, |
| 1008 | SVal Cond, |
| 1009 | bool Assumption) const { |
| 1010 | RegionStateTy RS = state->get<RegionState>(); |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 1011 | for (RegionStateTy::iterator I = RS.begin(), E = RS.end(); I != E; ++I) { |
| 1012 | // If the symbol is assumed to NULL or another constant, this will |
| 1013 | // return an APSInt*. |
| 1014 | if (state->getSymVal(I.getKey())) |
| 1015 | state = state->remove<RegionState>(I.getKey()); |
| 1016 | } |
| 1017 | |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 1018 | // Realloc returns 0 when reallocation fails, which means that we should |
| 1019 | // restore the state of the pointer being reallocated. |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 1020 | ReallocMap RP = state->get<ReallocPairs>(); |
| 1021 | for (ReallocMap::iterator I = RP.begin(), E = RP.end(); I != E; ++I) { |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 1022 | // If the symbol is assumed to NULL or another constant, this will |
| 1023 | // return an APSInt*. |
| 1024 | if (state->getSymVal(I.getKey())) { |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 1025 | SymbolRef ReallocSym = I.getData().ReallocatedSym; |
| 1026 | const RefState *RS = state->get<RegionState>(ReallocSym); |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 1027 | if (RS) { |
Anna Zaks | 40add29 | 2012-02-15 00:11:25 +0000 | [diff] [blame] | 1028 | if (RS->isReleased() && ! I.getData().IsFreeOnFailure) |
| 1029 | state = state->set<RegionState>(ReallocSym, |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 1030 | RefState::getAllocateUnchecked(RS->getStmt())); |
Anna Zaks | c8bb3be | 2012-02-13 18:05:39 +0000 | [diff] [blame] | 1031 | } |
| 1032 | state = state->remove<ReallocPairs>(I.getKey()); |
| 1033 | } |
| 1034 | } |
| 1035 | |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 1036 | return state; |
| 1037 | } |
| 1038 | |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 1039 | // Check if the function is not known to us. So, for example, we could |
| 1040 | // conservatively assume it can free/reallocate it's pointer arguments. |
| 1041 | // (We assume that the pointers cannot escape through calls to system |
| 1042 | // functions not handled by this checker.) |
| 1043 | bool MallocChecker::hasUnknownBehavior(const FunctionDecl *FD, |
| 1044 | ProgramStateRef State) const { |
| 1045 | ASTContext &ASTC = State->getStateManager().getContext(); |
| 1046 | |
| 1047 | // If it's one of the allocation functions we can reason about, we model it's |
| 1048 | // behavior explicitly. |
| 1049 | if (isMemFunction(FD, ASTC)) { |
| 1050 | return false; |
| 1051 | } |
| 1052 | |
| 1053 | // If it's a system call, we know it does not free the memory. |
| 1054 | SourceManager &SM = ASTC.getSourceManager(); |
| 1055 | if (SM.isInSystemHeader(FD->getLocation())) { |
| 1056 | return false; |
| 1057 | } |
| 1058 | |
| 1059 | // Otherwise, assume that the function can free memory. |
| 1060 | return true; |
| 1061 | } |
| 1062 | |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 1063 | // If the symbol we are tracking is invalidated, but not explicitly (ex: the &p |
| 1064 | // escapes, when we are tracking p), do not track the symbol as we cannot reason |
| 1065 | // about it anymore. |
| 1066 | ProgramStateRef |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 1067 | MallocChecker::checkRegionChanges(ProgramStateRef State, |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 1068 | const StoreManager::InvalidatedSymbols *invalidated, |
| 1069 | ArrayRef<const MemRegion *> ExplicitRegions, |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 1070 | ArrayRef<const MemRegion *> Regions, |
| 1071 | const CallOrObjCMessage *Call) const { |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 1072 | if (!invalidated) |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 1073 | return State; |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 1074 | llvm::SmallPtrSet<SymbolRef, 8> WhitelistedSymbols; |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 1075 | |
Anna Zaks | 1d6cc6a | 2012-02-15 02:12:00 +0000 | [diff] [blame] | 1076 | const FunctionDecl *FD = (Call ? |
| 1077 | dyn_cast_or_null<FunctionDecl>(Call->getDecl()) :0); |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 1078 | |
| 1079 | // If it's a call which might free or reallocate memory, we assume that all |
| 1080 | // regions (explicit and implicit) escaped. Otherwise, whitelist explicit |
| 1081 | // pointers; we still can track them. |
| 1082 | if (!(FD && hasUnknownBehavior(FD, State))) { |
| 1083 | for (ArrayRef<const MemRegion *>::iterator I = ExplicitRegions.begin(), |
| 1084 | E = ExplicitRegions.end(); I != E; ++I) { |
| 1085 | if (const SymbolicRegion *R = (*I)->StripCasts()->getAs<SymbolicRegion>()) |
| 1086 | WhitelistedSymbols.insert(R->getSymbol()); |
| 1087 | } |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
| 1090 | for (StoreManager::InvalidatedSymbols::const_iterator I=invalidated->begin(), |
| 1091 | E = invalidated->end(); I!=E; ++I) { |
| 1092 | SymbolRef sym = *I; |
| 1093 | if (WhitelistedSymbols.count(sym)) |
| 1094 | continue; |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 1095 | // The symbol escaped. |
| 1096 | if (const RefState *RS = State->get<RegionState>(sym)) |
| 1097 | State = State->set<RegionState>(sym, RefState::getEscaped(RS->getStmt())); |
Anna Zaks | 4fb5487 | 2012-02-11 21:02:35 +0000 | [diff] [blame] | 1098 | } |
Anna Zaks | 66c4040 | 2012-02-14 21:55:24 +0000 | [diff] [blame] | 1099 | return State; |
Ted Kremenek | dd0e490 | 2010-07-31 01:52:11 +0000 | [diff] [blame] | 1100 | } |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 1101 | |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 1102 | PathDiagnosticPiece * |
| 1103 | MallocChecker::MallocBugVisitor::VisitNode(const ExplodedNode *N, |
| 1104 | const ExplodedNode *PrevN, |
| 1105 | BugReporterContext &BRC, |
| 1106 | BugReport &BR) { |
| 1107 | const RefState *RS = N->getState()->get<RegionState>(Sym); |
| 1108 | const RefState *RSPrev = PrevN->getState()->get<RegionState>(Sym); |
| 1109 | if (!RS && !RSPrev) |
| 1110 | return 0; |
| 1111 | |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 1112 | const Stmt *S = 0; |
| 1113 | const char *Msg = 0; |
| 1114 | |
| 1115 | // Retrieve the associated statement. |
| 1116 | ProgramPoint ProgLoc = N->getLocation(); |
| 1117 | if (isa<StmtPoint>(ProgLoc)) |
| 1118 | S = cast<StmtPoint>(ProgLoc).getStmt(); |
| 1119 | // If an assumption was made on a branch, it should be caught |
| 1120 | // here by looking at the state transition. |
| 1121 | if (isa<BlockEdge>(ProgLoc)) { |
| 1122 | const CFGBlock *srcBlk = cast<BlockEdge>(ProgLoc).getSrc(); |
| 1123 | S = srcBlk->getTerminator(); |
| 1124 | } |
| 1125 | if (!S) |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 1126 | return 0; |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 1127 | |
| 1128 | // Find out if this is an interesting point and what is the kind. |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 1129 | if (Mode == Normal) { |
| 1130 | if (isAllocated(RS, RSPrev, S)) |
| 1131 | Msg = "Memory is allocated"; |
| 1132 | else if (isReleased(RS, RSPrev, S)) |
| 1133 | Msg = "Memory is released"; |
| 1134 | else if (isReallocFailedCheck(RS, RSPrev, S)) { |
| 1135 | Mode = ReallocationFailed; |
| 1136 | Msg = "Reallocation failed"; |
| 1137 | } |
| 1138 | |
| 1139 | // We are in a special mode if a reallocation failed later in the path. |
| 1140 | } else if (Mode == ReallocationFailed) { |
| 1141 | // Generate a special diagnostic for the first realloc we find. |
| 1142 | if (!isAllocated(RS, RSPrev, S) && !isReleased(RS, RSPrev, S)) |
| 1143 | return 0; |
| 1144 | |
| 1145 | // Check that the name of the function is realloc. |
| 1146 | const CallExpr *CE = dyn_cast<CallExpr>(S); |
| 1147 | if (!CE) |
| 1148 | return 0; |
| 1149 | const FunctionDecl *funDecl = CE->getDirectCallee(); |
| 1150 | if (!funDecl) |
| 1151 | return 0; |
| 1152 | StringRef FunName = funDecl->getName(); |
| 1153 | if (!(FunName.equals("realloc") || FunName.equals("reallocf"))) |
| 1154 | return 0; |
| 1155 | Msg = "Attempt to reallocate memory"; |
| 1156 | Mode = Normal; |
| 1157 | } |
| 1158 | |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 1159 | if (!Msg) |
| 1160 | return 0; |
| 1161 | |
| 1162 | // Generate the extra diagnostic. |
Anna Zaks | fe57160 | 2012-02-16 22:26:07 +0000 | [diff] [blame] | 1163 | PathDiagnosticLocation Pos(S, BRC.getSourceManager(), |
Anna Zaks | ff3b9fd | 2012-02-09 06:25:51 +0000 | [diff] [blame] | 1164 | N->getLocationContext()); |
| 1165 | return new PathDiagnosticEventPiece(Pos, Msg); |
| 1166 | } |
| 1167 | |
| 1168 | |
Anna Zaks | 231361a | 2012-02-08 23:16:52 +0000 | [diff] [blame] | 1169 | #define REGISTER_CHECKER(name) \ |
| 1170 | void ento::register##name(CheckerManager &mgr) {\ |
Anna Zaks | f0dfc9c | 2012-02-17 22:35:31 +0000 | [diff] [blame] | 1171 | registerCStringCheckerBasic(mgr); \ |
Anna Zaks | 231361a | 2012-02-08 23:16:52 +0000 | [diff] [blame] | 1172 | mgr.registerChecker<MallocChecker>()->Filter.C##name = true;\ |
Argyrios Kyrtzidis | 312dbec | 2011-02-28 01:26:35 +0000 | [diff] [blame] | 1173 | } |
Anna Zaks | 231361a | 2012-02-08 23:16:52 +0000 | [diff] [blame] | 1174 | |
| 1175 | REGISTER_CHECKER(MallocPessimistic) |
| 1176 | REGISTER_CHECKER(MallocOptimistic) |