Anna Zaks | 08be9b9 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 1 | //==--- MacOSKeychainAPIChecker.cpp ------------------------------*- C++ -*-==// |
Anna Zaks | 15f496c | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 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 | // This checker flags misuses of KeyChainAPI. In particular, the password data |
| 10 | // allocated/returned by SecKeychainItemCopyContent, |
| 11 | // SecKeychainFindGenericPassword, SecKeychainFindInternetPassword functions has |
| 12 | // to be freed using a call to SecKeychainItemFreeContent. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "ClangSACheckers.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 16 | #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" |
Anna Zaks | 15f496c | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 17 | #include "clang/StaticAnalyzer/Core/Checker.h" |
| 18 | #include "clang/StaticAnalyzer/Core/CheckerManager.h" |
| 19 | #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" |
Ted Kremenek | 001fd5b | 2011-08-15 22:09:50 +0000 | [diff] [blame] | 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" |
| 21 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Benjamin Kramer | 444a130 | 2012-12-01 17:12:56 +0000 | [diff] [blame] | 23 | #include "llvm/Support/raw_ostream.h" |
Anna Zaks | 15f496c | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace clang; |
| 26 | using namespace ento; |
| 27 | |
| 28 | namespace { |
| 29 | class MacOSKeychainAPIChecker : public Checker<check::PreStmt<CallExpr>, |
Anna Zaks | 15f496c | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 30 | check::PostStmt<CallExpr>, |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 31 | check::DeadSymbols> { |
Ahmed Charles | b898432 | 2014-03-07 20:03:18 +0000 | [diff] [blame] | 32 | mutable std::unique_ptr<BugType> BT; |
Anna Zaks | 388c18e | 2011-08-04 00:26:57 +0000 | [diff] [blame] | 33 | |
Anna Zaks | 15f496c | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 34 | public: |
Anna Zaks | c94894f | 2011-08-12 21:14:26 +0000 | [diff] [blame] | 35 | /// AllocationState is a part of the checker specific state together with the |
| 36 | /// MemRegion corresponding to the allocated data. |
| 37 | struct AllocationState { |
Anna Zaks | c94894f | 2011-08-12 21:14:26 +0000 | [diff] [blame] | 38 | /// The index of the allocator function. |
| 39 | unsigned int AllocatorIdx; |
Anna Zaks | 5be8a4d | 2011-08-25 00:59:06 +0000 | [diff] [blame] | 40 | SymbolRef Region; |
Anna Zaks | c94894f | 2011-08-12 21:14:26 +0000 | [diff] [blame] | 41 | |
| 42 | AllocationState(const Expr *E, unsigned int Idx, SymbolRef R) : |
Anna Zaks | c94894f | 2011-08-12 21:14:26 +0000 | [diff] [blame] | 43 | AllocatorIdx(Idx), |
Anna Zaks | 5be8a4d | 2011-08-25 00:59:06 +0000 | [diff] [blame] | 44 | Region(R) {} |
Anna Zaks | c94894f | 2011-08-12 21:14:26 +0000 | [diff] [blame] | 45 | |
| 46 | bool operator==(const AllocationState &X) const { |
Anna Zaks | 5be8a4d | 2011-08-25 00:59:06 +0000 | [diff] [blame] | 47 | return (AllocatorIdx == X.AllocatorIdx && |
| 48 | Region == X.Region); |
Anna Zaks | c94894f | 2011-08-12 21:14:26 +0000 | [diff] [blame] | 49 | } |
Anna Zaks | 5be8a4d | 2011-08-25 00:59:06 +0000 | [diff] [blame] | 50 | |
Anna Zaks | c94894f | 2011-08-12 21:14:26 +0000 | [diff] [blame] | 51 | void Profile(llvm::FoldingSetNodeID &ID) const { |
Anna Zaks | c94894f | 2011-08-12 21:14:26 +0000 | [diff] [blame] | 52 | ID.AddInteger(AllocatorIdx); |
Anna Zaks | 5be8a4d | 2011-08-25 00:59:06 +0000 | [diff] [blame] | 53 | ID.AddPointer(Region); |
Anna Zaks | c94894f | 2011-08-12 21:14:26 +0000 | [diff] [blame] | 54 | } |
| 55 | }; |
| 56 | |
Anna Zaks | 15f496c | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 57 | void checkPreStmt(const CallExpr *S, CheckerContext &C) const; |
Anna Zaks | 15f496c | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 58 | void checkPostStmt(const CallExpr *S, CheckerContext &C) const; |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 59 | void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const; |
Anna Zaks | 15f496c | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 60 | |
| 61 | private: |
Anna Zaks | f880cff | 2011-08-24 21:58:55 +0000 | [diff] [blame] | 62 | typedef std::pair<SymbolRef, const AllocationState*> AllocationPair; |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 63 | typedef SmallVector<AllocationPair, 2> AllocationPairVec; |
Anna Zaks | be46089 | 2011-08-24 20:52:46 +0000 | [diff] [blame] | 64 | |
| 65 | enum APIKind { |
Anna Zaks | 0897a23 | 2011-08-24 00:06:27 +0000 | [diff] [blame] | 66 | /// Denotes functions tracked by this checker. |
| 67 | ValidAPI = 0, |
| 68 | /// The functions commonly/mistakenly used in place of the given API. |
| 69 | ErrorAPI = 1, |
| 70 | /// The functions which may allocate the data. These are tracked to reduce |
| 71 | /// the false alarm rate. |
| 72 | PossibleAPI = 2 |
| 73 | }; |
Anna Zaks | 08be9b9 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 74 | /// Stores the information about the allocator and deallocator functions - |
| 75 | /// these are the functions the checker is tracking. |
| 76 | struct ADFunctionInfo { |
| 77 | const char* Name; |
| 78 | unsigned int Param; |
| 79 | unsigned int DeallocatorIdx; |
Anna Zaks | 0897a23 | 2011-08-24 00:06:27 +0000 | [diff] [blame] | 80 | APIKind Kind; |
Anna Zaks | 08be9b9 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 81 | }; |
| 82 | static const unsigned InvalidIdx = 100000; |
Anna Zaks | 0897a23 | 2011-08-24 00:06:27 +0000 | [diff] [blame] | 83 | static const unsigned FunctionsToTrackSize = 8; |
Anna Zaks | 08be9b9 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 84 | static const ADFunctionInfo FunctionsToTrack[FunctionsToTrackSize]; |
Anna Zaks | 177ecfa | 2011-08-05 23:52:45 +0000 | [diff] [blame] | 85 | /// The value, which represents no error return value for allocator functions. |
| 86 | static const unsigned NoErr = 0; |
Anna Zaks | 15f496c | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 87 | |
Anna Zaks | 08be9b9 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 88 | /// Given the function name, returns the index of the allocator/deallocator |
| 89 | /// function. |
Anna Zaks | be46089 | 2011-08-24 20:52:46 +0000 | [diff] [blame] | 90 | static unsigned getTrackedFunctionIndex(StringRef Name, bool IsAllocator); |
Anna Zaks | 388c18e | 2011-08-04 00:26:57 +0000 | [diff] [blame] | 91 | |
| 92 | inline void initBugType() const { |
| 93 | if (!BT) |
Alexander Kornienko | 4aca9b1 | 2014-02-11 21:49:21 +0000 | [diff] [blame] | 94 | BT.reset(new BugType(this, "Improper use of SecKeychain API", |
Anna Zaks | 8ef07e5 | 2013-04-03 19:28:22 +0000 | [diff] [blame] | 95 | "API Misuse (Apple)")); |
Anna Zaks | 388c18e | 2011-08-04 00:26:57 +0000 | [diff] [blame] | 96 | } |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 97 | |
Anna Zaks | 85913db | 2011-08-25 00:32:42 +0000 | [diff] [blame] | 98 | void generateDeallocatorMismatchReport(const AllocationPair &AP, |
Anna Zaks | bb16701 | 2011-08-23 23:47:36 +0000 | [diff] [blame] | 99 | const Expr *ArgExpr, |
Anna Zaks | 85913db | 2011-08-25 00:32:42 +0000 | [diff] [blame] | 100 | CheckerContext &C) const; |
Anna Zaks | bb16701 | 2011-08-23 23:47:36 +0000 | [diff] [blame] | 101 | |
Anna Zaks | 4b062cb | 2012-02-23 22:53:29 +0000 | [diff] [blame] | 102 | /// Find the allocation site for Sym on the path leading to the node N. |
Anna Zaks | a043d0c | 2013-01-08 00:25:29 +0000 | [diff] [blame] | 103 | const ExplodedNode *getAllocationNode(const ExplodedNode *N, SymbolRef Sym, |
| 104 | CheckerContext &C) const; |
Anna Zaks | 4b062cb | 2012-02-23 22:53:29 +0000 | [diff] [blame] | 105 | |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 106 | std::unique_ptr<BugReport> generateAllocatedDataNotReleasedReport( |
| 107 | const AllocationPair &AP, ExplodedNode *N, CheckerContext &C) const; |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 108 | |
| 109 | /// Check if RetSym evaluates to an error value in the current state. |
| 110 | bool definitelyReturnedError(SymbolRef RetSym, |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 111 | ProgramStateRef State, |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 112 | SValBuilder &Builder, |
| 113 | bool noError = false) const; |
| 114 | |
| 115 | /// Check if RetSym evaluates to a NoErr value in the current state. |
| 116 | bool definitelyDidnotReturnError(SymbolRef RetSym, |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 117 | ProgramStateRef State, |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 118 | SValBuilder &Builder) const { |
| 119 | return definitelyReturnedError(RetSym, State, Builder, true); |
| 120 | } |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 121 | |
Ted Kremenek | 1e809b4 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 122 | /// Mark an AllocationPair interesting for diagnostic reporting. |
| 123 | void markInteresting(BugReport *R, const AllocationPair &AP) const { |
| 124 | R->markInteresting(AP.first); |
| 125 | R->markInteresting(AP.second->Region); |
| 126 | } |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 127 | |
Anna Zaks | be46089 | 2011-08-24 20:52:46 +0000 | [diff] [blame] | 128 | /// The bug visitor which allows us to print extra diagnostics along the |
| 129 | /// BugReport path. For example, showing the allocation site of the leaked |
| 130 | /// region. |
Jordy Rose | f78877e | 2012-03-24 02:45:35 +0000 | [diff] [blame] | 131 | class SecKeychainBugVisitor |
| 132 | : public BugReporterVisitorImpl<SecKeychainBugVisitor> { |
Anna Zaks | be46089 | 2011-08-24 20:52:46 +0000 | [diff] [blame] | 133 | protected: |
| 134 | // The allocated region symbol tracked by the main analysis. |
| 135 | SymbolRef Sym; |
| 136 | |
| 137 | public: |
| 138 | SecKeychainBugVisitor(SymbolRef S) : Sym(S) {} |
Anna Zaks | be46089 | 2011-08-24 20:52:46 +0000 | [diff] [blame] | 139 | |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 140 | void Profile(llvm::FoldingSetNodeID &ID) const override { |
Anna Zaks | be46089 | 2011-08-24 20:52:46 +0000 | [diff] [blame] | 141 | static int X = 0; |
| 142 | ID.AddPointer(&X); |
| 143 | ID.AddPointer(Sym); |
| 144 | } |
| 145 | |
| 146 | PathDiagnosticPiece *VisitNode(const ExplodedNode *N, |
| 147 | const ExplodedNode *PrevN, |
| 148 | BugReporterContext &BRC, |
Craig Topper | fb6b25b | 2014-03-15 04:29:04 +0000 | [diff] [blame] | 149 | BugReport &BR) override; |
Anna Zaks | be46089 | 2011-08-24 20:52:46 +0000 | [diff] [blame] | 150 | }; |
Anna Zaks | 15f496c | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 151 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 152 | } |
Anna Zaks | 15f496c | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 153 | |
Anna Zaks | 5443a64 | 2011-08-15 23:23:15 +0000 | [diff] [blame] | 154 | /// ProgramState traits to store the currently allocated (and not yet freed) |
| 155 | /// symbols. This is a map from the allocated content symbol to the |
| 156 | /// corresponding AllocationState. |
Jordan Rose | 0c153cb | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 157 | REGISTER_MAP_WITH_PROGRAMSTATE(AllocatedData, |
| 158 | SymbolRef, |
| 159 | MacOSKeychainAPIChecker::AllocationState) |
Anna Zaks | 15f496c | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 160 | |
Anna Zaks | 388c18e | 2011-08-04 00:26:57 +0000 | [diff] [blame] | 161 | static bool isEnclosingFunctionParam(const Expr *E) { |
| 162 | E = E->IgnoreParenCasts(); |
| 163 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 164 | const ValueDecl *VD = DRE->getDecl(); |
| 165 | if (isa<ImplicitParamDecl>(VD) || isa<ParmVarDecl>(VD)) |
| 166 | return true; |
| 167 | } |
| 168 | return false; |
| 169 | } |
| 170 | |
Anna Zaks | 08be9b9 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 171 | const MacOSKeychainAPIChecker::ADFunctionInfo |
| 172 | MacOSKeychainAPIChecker::FunctionsToTrack[FunctionsToTrackSize] = { |
Anna Zaks | 0897a23 | 2011-08-24 00:06:27 +0000 | [diff] [blame] | 173 | {"SecKeychainItemCopyContent", 4, 3, ValidAPI}, // 0 |
| 174 | {"SecKeychainFindGenericPassword", 6, 3, ValidAPI}, // 1 |
| 175 | {"SecKeychainFindInternetPassword", 13, 3, ValidAPI}, // 2 |
| 176 | {"SecKeychainItemFreeContent", 1, InvalidIdx, ValidAPI}, // 3 |
| 177 | {"SecKeychainItemCopyAttributesAndData", 5, 5, ValidAPI}, // 4 |
| 178 | {"SecKeychainItemFreeAttributesAndData", 1, InvalidIdx, ValidAPI}, // 5 |
| 179 | {"free", 0, InvalidIdx, ErrorAPI}, // 6 |
| 180 | {"CFStringCreateWithBytesNoCopy", 1, InvalidIdx, PossibleAPI}, // 7 |
Anna Zaks | 08be9b9 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | unsigned MacOSKeychainAPIChecker::getTrackedFunctionIndex(StringRef Name, |
Anna Zaks | be46089 | 2011-08-24 20:52:46 +0000 | [diff] [blame] | 184 | bool IsAllocator) { |
Anna Zaks | 08be9b9 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 185 | for (unsigned I = 0; I < FunctionsToTrackSize; ++I) { |
| 186 | ADFunctionInfo FI = FunctionsToTrack[I]; |
| 187 | if (FI.Name != Name) |
| 188 | continue; |
| 189 | // Make sure the function is of the right type (allocator vs deallocator). |
| 190 | if (IsAllocator && (FI.DeallocatorIdx == InvalidIdx)) |
| 191 | return InvalidIdx; |
| 192 | if (!IsAllocator && (FI.DeallocatorIdx != InvalidIdx)) |
| 193 | return InvalidIdx; |
| 194 | |
| 195 | return I; |
| 196 | } |
| 197 | // The function is not tracked. |
| 198 | return InvalidIdx; |
| 199 | } |
| 200 | |
Anna Zaks | c94894f | 2011-08-12 21:14:26 +0000 | [diff] [blame] | 201 | static bool isBadDeallocationArgument(const MemRegion *Arg) { |
Jordy Rose | f80b2cc | 2012-03-11 00:08:24 +0000 | [diff] [blame] | 202 | if (!Arg) |
| 203 | return false; |
Anna Zaks | c94894f | 2011-08-12 21:14:26 +0000 | [diff] [blame] | 204 | if (isa<AllocaRegion>(Arg) || |
| 205 | isa<BlockDataRegion>(Arg) || |
| 206 | isa<TypedRegion>(Arg)) { |
| 207 | return true; |
| 208 | } |
| 209 | return false; |
| 210 | } |
Jordy Rose | f80b2cc | 2012-03-11 00:08:24 +0000 | [diff] [blame] | 211 | |
Anna Zaks | c52bed1 | 2011-08-05 00:37:00 +0000 | [diff] [blame] | 212 | /// Given the address expression, retrieve the value it's pointing to. Assume |
Anna Zaks | c94894f | 2011-08-12 21:14:26 +0000 | [diff] [blame] | 213 | /// that value is itself an address, and return the corresponding symbol. |
| 214 | static SymbolRef getAsPointeeSymbol(const Expr *Expr, |
| 215 | CheckerContext &C) { |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 216 | ProgramStateRef State = C.getState(); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 217 | SVal ArgV = State->getSVal(Expr, C.getLocationContext()); |
Anna Zaks | 177ecfa | 2011-08-05 23:52:45 +0000 | [diff] [blame] | 218 | |
David Blaikie | 05785d1 | 2013-02-20 22:23:23 +0000 | [diff] [blame] | 219 | if (Optional<loc::MemRegionVal> X = ArgV.getAs<loc::MemRegionVal>()) { |
Anna Zaks | c52bed1 | 2011-08-05 00:37:00 +0000 | [diff] [blame] | 220 | StoreManager& SM = C.getStoreManager(); |
Jordy Rose | f80b2cc | 2012-03-11 00:08:24 +0000 | [diff] [blame] | 221 | SymbolRef sym = SM.getBinding(State->getStore(), *X).getAsLocSymbol(); |
| 222 | if (sym) |
| 223 | return sym; |
Anna Zaks | c52bed1 | 2011-08-05 00:37:00 +0000 | [diff] [blame] | 224 | } |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 225 | return nullptr; |
Anna Zaks | c52bed1 | 2011-08-05 00:37:00 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 228 | // When checking for error code, we need to consider the following cases: |
| 229 | // 1) noErr / [0] |
| 230 | // 2) someErr / [1, inf] |
| 231 | // 3) unknown |
Sylvestre Ledru | 33b5baf | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 232 | // If noError, returns true iff (1). |
| 233 | // If !noError, returns true iff (2). |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 234 | bool MacOSKeychainAPIChecker::definitelyReturnedError(SymbolRef RetSym, |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 235 | ProgramStateRef State, |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 236 | SValBuilder &Builder, |
| 237 | bool noError) const { |
| 238 | DefinedOrUnknownSVal NoErrVal = Builder.makeIntVal(NoErr, |
| 239 | Builder.getSymbolManager().getType(RetSym)); |
| 240 | DefinedOrUnknownSVal NoErr = Builder.evalEQ(State, NoErrVal, |
| 241 | nonloc::SymbolVal(RetSym)); |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 242 | ProgramStateRef ErrState = State->assume(NoErr, noError); |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 243 | if (ErrState == State) { |
| 244 | return true; |
| 245 | } |
| 246 | |
| 247 | return false; |
| 248 | } |
| 249 | |
Anna Zaks | bb16701 | 2011-08-23 23:47:36 +0000 | [diff] [blame] | 250 | // Report deallocator mismatch. Remove the region from tracking - reporting a |
| 251 | // missing free error after this one is redundant. |
| 252 | void MacOSKeychainAPIChecker:: |
Anna Zaks | 85913db | 2011-08-25 00:32:42 +0000 | [diff] [blame] | 253 | generateDeallocatorMismatchReport(const AllocationPair &AP, |
Anna Zaks | bb16701 | 2011-08-23 23:47:36 +0000 | [diff] [blame] | 254 | const Expr *ArgExpr, |
Anna Zaks | 85913db | 2011-08-25 00:32:42 +0000 | [diff] [blame] | 255 | CheckerContext &C) const { |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 256 | ProgramStateRef State = C.getState(); |
Anna Zaks | 85913db | 2011-08-25 00:32:42 +0000 | [diff] [blame] | 257 | State = State->remove<AllocatedData>(AP.first); |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 258 | ExplodedNode *N = C.generateNonFatalErrorNode(State); |
Anna Zaks | bb16701 | 2011-08-23 23:47:36 +0000 | [diff] [blame] | 259 | |
| 260 | if (!N) |
| 261 | return; |
| 262 | initBugType(); |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 263 | SmallString<80> sbuf; |
Anna Zaks | bb16701 | 2011-08-23 23:47:36 +0000 | [diff] [blame] | 264 | llvm::raw_svector_ostream os(sbuf); |
Anna Zaks | 85913db | 2011-08-25 00:32:42 +0000 | [diff] [blame] | 265 | unsigned int PDeallocIdx = |
| 266 | FunctionsToTrack[AP.second->AllocatorIdx].DeallocatorIdx; |
Anna Zaks | bb16701 | 2011-08-23 23:47:36 +0000 | [diff] [blame] | 267 | |
| 268 | os << "Deallocator doesn't match the allocator: '" |
| 269 | << FunctionsToTrack[PDeallocIdx].Name << "' should be used."; |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 270 | auto Report = llvm::make_unique<BugReport>(*BT, os.str(), N); |
David Blaikie | 91e7902 | 2014-09-04 23:54:33 +0000 | [diff] [blame] | 271 | Report->addVisitor(llvm::make_unique<SecKeychainBugVisitor>(AP.first)); |
Anna Zaks | bb16701 | 2011-08-23 23:47:36 +0000 | [diff] [blame] | 272 | Report->addRange(ArgExpr->getSourceRange()); |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 273 | markInteresting(Report.get(), AP); |
| 274 | C.emitReport(std::move(Report)); |
Anna Zaks | bb16701 | 2011-08-23 23:47:36 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Anna Zaks | 15f496c | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 277 | void MacOSKeychainAPIChecker::checkPreStmt(const CallExpr *CE, |
| 278 | CheckerContext &C) const { |
Anna Zaks | c52bed1 | 2011-08-05 00:37:00 +0000 | [diff] [blame] | 279 | unsigned idx = InvalidIdx; |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 280 | ProgramStateRef State = C.getState(); |
Anna Zaks | 15f496c | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 281 | |
Jordan Rose | 6cd16c5 | 2012-07-10 23:13:01 +0000 | [diff] [blame] | 282 | const FunctionDecl *FD = C.getCalleeDecl(CE); |
| 283 | if (!FD || FD->getKind() != Decl::Function) |
| 284 | return; |
Ted Kremenek | 3a0678e | 2015-09-08 03:50:52 +0000 | [diff] [blame] | 285 | |
Jordan Rose | 6cd16c5 | 2012-07-10 23:13:01 +0000 | [diff] [blame] | 286 | StringRef funName = C.getCalleeName(FD); |
Anna Zaks | c6aa531 | 2011-12-01 05:57:37 +0000 | [diff] [blame] | 287 | if (funName.empty()) |
Anna Zaks | 15f496c | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 288 | return; |
Anna Zaks | 15f496c | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 289 | |
Anna Zaks | c52bed1 | 2011-08-05 00:37:00 +0000 | [diff] [blame] | 290 | // If it is a call to an allocator function, it could be a double allocation. |
| 291 | idx = getTrackedFunctionIndex(funName, true); |
| 292 | if (idx != InvalidIdx) { |
Anna Zaks | 33f0632 | 2015-02-05 01:02:56 +0000 | [diff] [blame] | 293 | unsigned paramIdx = FunctionsToTrack[idx].Param; |
| 294 | if (CE->getNumArgs() <= paramIdx) |
| 295 | return; |
| 296 | |
| 297 | const Expr *ArgExpr = CE->getArg(paramIdx); |
Anna Zaks | c94894f | 2011-08-12 21:14:26 +0000 | [diff] [blame] | 298 | if (SymbolRef V = getAsPointeeSymbol(ArgExpr, C)) |
Anna Zaks | c52bed1 | 2011-08-05 00:37:00 +0000 | [diff] [blame] | 299 | if (const AllocationState *AS = State->get<AllocatedData>(V)) { |
Anna Zaks | 5be8a4d | 2011-08-25 00:59:06 +0000 | [diff] [blame] | 300 | if (!definitelyReturnedError(AS->Region, State, C.getSValBuilder())) { |
Anna Zaks | 01ae1e1 | 2011-08-16 16:30:24 +0000 | [diff] [blame] | 301 | // Remove the value from the state. The new symbol will be added for |
| 302 | // tracking when the second allocator is processed in checkPostStmt(). |
| 303 | State = State->remove<AllocatedData>(V); |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 304 | ExplodedNode *N = C.generateNonFatalErrorNode(State); |
Anna Zaks | 01ae1e1 | 2011-08-16 16:30:24 +0000 | [diff] [blame] | 305 | if (!N) |
| 306 | return; |
| 307 | initBugType(); |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 308 | SmallString<128> sbuf; |
Anna Zaks | 01ae1e1 | 2011-08-16 16:30:24 +0000 | [diff] [blame] | 309 | llvm::raw_svector_ostream os(sbuf); |
| 310 | unsigned int DIdx = FunctionsToTrack[AS->AllocatorIdx].DeallocatorIdx; |
| 311 | os << "Allocated data should be released before another call to " |
| 312 | << "the allocator: missing a call to '" |
| 313 | << FunctionsToTrack[DIdx].Name |
| 314 | << "'."; |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 315 | auto Report = llvm::make_unique<BugReport>(*BT, os.str(), N); |
David Blaikie | 91e7902 | 2014-09-04 23:54:33 +0000 | [diff] [blame] | 316 | Report->addVisitor(llvm::make_unique<SecKeychainBugVisitor>(V)); |
Anna Zaks | 01ae1e1 | 2011-08-16 16:30:24 +0000 | [diff] [blame] | 317 | Report->addRange(ArgExpr->getSourceRange()); |
Ted Kremenek | 1e809b4 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 318 | Report->markInteresting(AS->Region); |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 319 | C.emitReport(std::move(Report)); |
Anna Zaks | 01ae1e1 | 2011-08-16 16:30:24 +0000 | [diff] [blame] | 320 | } |
Anna Zaks | c52bed1 | 2011-08-05 00:37:00 +0000 | [diff] [blame] | 321 | } |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | // Is it a call to one of deallocator functions? |
| 326 | idx = getTrackedFunctionIndex(funName, false); |
Anna Zaks | 08be9b9 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 327 | if (idx == InvalidIdx) |
Anna Zaks | 4aa34a53 | 2011-08-04 00:31:38 +0000 | [diff] [blame] | 328 | return; |
| 329 | |
Anna Zaks | 33f0632 | 2015-02-05 01:02:56 +0000 | [diff] [blame] | 330 | unsigned paramIdx = FunctionsToTrack[idx].Param; |
| 331 | if (CE->getNumArgs() <= paramIdx) |
| 332 | return; |
| 333 | |
Anna Zaks | c94894f | 2011-08-12 21:14:26 +0000 | [diff] [blame] | 334 | // Check the argument to the deallocator. |
Anna Zaks | 33f0632 | 2015-02-05 01:02:56 +0000 | [diff] [blame] | 335 | const Expr *ArgExpr = CE->getArg(paramIdx); |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 336 | SVal ArgSVal = State->getSVal(ArgExpr, C.getLocationContext()); |
Anna Zaks | c94894f | 2011-08-12 21:14:26 +0000 | [diff] [blame] | 337 | |
| 338 | // Undef is reported by another checker. |
| 339 | if (ArgSVal.isUndef()) |
| 340 | return; |
| 341 | |
Jordy Rose | f80b2cc | 2012-03-11 00:08:24 +0000 | [diff] [blame] | 342 | SymbolRef ArgSM = ArgSVal.getAsLocSymbol(); |
Anna Zaks | c94894f | 2011-08-12 21:14:26 +0000 | [diff] [blame] | 343 | |
Anna Zaks | c94894f | 2011-08-12 21:14:26 +0000 | [diff] [blame] | 344 | // If the argument is coming from the heap, globals, or unknown, do not |
| 345 | // report it. |
Jordy Rose | f80b2cc | 2012-03-11 00:08:24 +0000 | [diff] [blame] | 346 | bool RegionArgIsBad = false; |
| 347 | if (!ArgSM) { |
| 348 | if (!isBadDeallocationArgument(ArgSVal.getAsRegion())) |
| 349 | return; |
| 350 | RegionArgIsBad = true; |
| 351 | } |
Anna Zaks | 4aa34a53 | 2011-08-04 00:31:38 +0000 | [diff] [blame] | 352 | |
Anna Zaks | 0897a23 | 2011-08-24 00:06:27 +0000 | [diff] [blame] | 353 | // Is the argument to the call being tracked? |
| 354 | const AllocationState *AS = State->get<AllocatedData>(ArgSM); |
| 355 | if (!AS && FunctionsToTrack[idx].Kind != ValidAPI) { |
| 356 | return; |
| 357 | } |
Anna Zaks | 29f9b7a | 2011-08-15 18:42:00 +0000 | [diff] [blame] | 358 | // If trying to free data which has not been allocated yet, report as a bug. |
Anna Zaks | 5443a64 | 2011-08-15 23:23:15 +0000 | [diff] [blame] | 359 | // TODO: We might want a more precise diagnostic for double free |
| 360 | // (that would involve tracking all the freed symbols in the checker state). |
Anna Zaks | 0897a23 | 2011-08-24 00:06:27 +0000 | [diff] [blame] | 361 | if (!AS || RegionArgIsBad) { |
Anna Zaks | 4aa34a53 | 2011-08-04 00:31:38 +0000 | [diff] [blame] | 362 | // It is possible that this is a false positive - the argument might |
| 363 | // have entered as an enclosing function parameter. |
| 364 | if (isEnclosingFunctionParam(ArgExpr)) |
Anna Zaks | 15f496c | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 365 | return; |
Anna Zaks | 388c18e | 2011-08-04 00:26:57 +0000 | [diff] [blame] | 366 | |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 367 | ExplodedNode *N = C.generateNonFatalErrorNode(State); |
Anna Zaks | 4aa34a53 | 2011-08-04 00:31:38 +0000 | [diff] [blame] | 368 | if (!N) |
| 369 | return; |
| 370 | initBugType(); |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 371 | auto Report = llvm::make_unique<BugReport>( |
| 372 | *BT, "Trying to free data which has not been allocated.", N); |
Anna Zaks | 4aa34a53 | 2011-08-04 00:31:38 +0000 | [diff] [blame] | 373 | Report->addRange(ArgExpr->getSourceRange()); |
Ted Kremenek | 1e809b4 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 374 | if (AS) |
| 375 | Report->markInteresting(AS->Region); |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 376 | C.emitReport(std::move(Report)); |
Anna Zaks | 08be9b9 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 377 | return; |
Anna Zaks | 15f496c | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 378 | } |
Anna Zaks | 4aa34a53 | 2011-08-04 00:31:38 +0000 | [diff] [blame] | 379 | |
Anna Zaks | 0897a23 | 2011-08-24 00:06:27 +0000 | [diff] [blame] | 380 | // Process functions which might deallocate. |
| 381 | if (FunctionsToTrack[idx].Kind == PossibleAPI) { |
| 382 | |
| 383 | if (funName == "CFStringCreateWithBytesNoCopy") { |
| 384 | const Expr *DeallocatorExpr = CE->getArg(5)->IgnoreParenCasts(); |
| 385 | // NULL ~ default deallocator, so warn. |
| 386 | if (DeallocatorExpr->isNullPointerConstant(C.getASTContext(), |
| 387 | Expr::NPC_ValueDependentIsNotNull)) { |
Anna Zaks | 85913db | 2011-08-25 00:32:42 +0000 | [diff] [blame] | 388 | const AllocationPair AP = std::make_pair(ArgSM, AS); |
| 389 | generateDeallocatorMismatchReport(AP, ArgExpr, C); |
Anna Zaks | 0897a23 | 2011-08-24 00:06:27 +0000 | [diff] [blame] | 390 | return; |
| 391 | } |
| 392 | // One of the default allocators, so warn. |
| 393 | if (const DeclRefExpr *DE = dyn_cast<DeclRefExpr>(DeallocatorExpr)) { |
| 394 | StringRef DeallocatorName = DE->getFoundDecl()->getName(); |
| 395 | if (DeallocatorName == "kCFAllocatorDefault" || |
| 396 | DeallocatorName == "kCFAllocatorSystemDefault" || |
| 397 | DeallocatorName == "kCFAllocatorMalloc") { |
Anna Zaks | 85913db | 2011-08-25 00:32:42 +0000 | [diff] [blame] | 398 | const AllocationPair AP = std::make_pair(ArgSM, AS); |
| 399 | generateDeallocatorMismatchReport(AP, ArgExpr, C); |
Anna Zaks | 0897a23 | 2011-08-24 00:06:27 +0000 | [diff] [blame] | 400 | return; |
| 401 | } |
| 402 | // If kCFAllocatorNull, which does not deallocate, we still have to |
Anna Zaks | 030e65d | 2013-01-07 19:13:00 +0000 | [diff] [blame] | 403 | // find the deallocator. |
| 404 | if (DE->getFoundDecl()->getName() == "kCFAllocatorNull") |
Anna Zaks | 0897a23 | 2011-08-24 00:06:27 +0000 | [diff] [blame] | 405 | return; |
Anna Zaks | 0897a23 | 2011-08-24 00:06:27 +0000 | [diff] [blame] | 406 | } |
Anna Zaks | 030e65d | 2013-01-07 19:13:00 +0000 | [diff] [blame] | 407 | // In all other cases, assume the user supplied a correct deallocator |
| 408 | // that will free memory so stop tracking. |
| 409 | State = State->remove<AllocatedData>(ArgSM); |
| 410 | C.addTransition(State); |
| 411 | return; |
Anna Zaks | 0897a23 | 2011-08-24 00:06:27 +0000 | [diff] [blame] | 412 | } |
Anna Zaks | 030e65d | 2013-01-07 19:13:00 +0000 | [diff] [blame] | 413 | |
| 414 | llvm_unreachable("We know of no other possible APIs."); |
Anna Zaks | 0897a23 | 2011-08-24 00:06:27 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Anna Zaks | 5443a64 | 2011-08-15 23:23:15 +0000 | [diff] [blame] | 417 | // The call is deallocating a value we previously allocated, so remove it |
| 418 | // from the next state. |
| 419 | State = State->remove<AllocatedData>(ArgSM); |
| 420 | |
Anna Zaks | bb16701 | 2011-08-23 23:47:36 +0000 | [diff] [blame] | 421 | // Check if the proper deallocator is used. |
Anna Zaks | c686177 | 2011-08-04 21:53:01 +0000 | [diff] [blame] | 422 | unsigned int PDeallocIdx = FunctionsToTrack[AS->AllocatorIdx].DeallocatorIdx; |
Anna Zaks | 0897a23 | 2011-08-24 00:06:27 +0000 | [diff] [blame] | 423 | if (PDeallocIdx != idx || (FunctionsToTrack[idx].Kind == ErrorAPI)) { |
Anna Zaks | 85913db | 2011-08-25 00:32:42 +0000 | [diff] [blame] | 424 | const AllocationPair AP = std::make_pair(ArgSM, AS); |
| 425 | generateDeallocatorMismatchReport(AP, ArgExpr, C); |
Anna Zaks | c686177 | 2011-08-04 21:53:01 +0000 | [diff] [blame] | 426 | return; |
| 427 | } |
| 428 | |
Anna Zaks | 719051e | 2011-12-01 16:41:58 +0000 | [diff] [blame] | 429 | // If the buffer can be null and the return status can be an error, |
| 430 | // report a bad call to free. |
David Blaikie | 2fdacbc | 2013-02-20 05:52:05 +0000 | [diff] [blame] | 431 | if (State->assume(ArgSVal.castAs<DefinedSVal>(), false) && |
Anna Zaks | 719051e | 2011-12-01 16:41:58 +0000 | [diff] [blame] | 432 | !definitelyDidnotReturnError(AS->Region, State, C.getSValBuilder())) { |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 433 | ExplodedNode *N = C.generateNonFatalErrorNode(State); |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 434 | if (!N) |
| 435 | return; |
| 436 | initBugType(); |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 437 | auto Report = llvm::make_unique<BugReport>( |
| 438 | *BT, "Only call free if a valid (non-NULL) buffer was returned.", N); |
David Blaikie | 91e7902 | 2014-09-04 23:54:33 +0000 | [diff] [blame] | 439 | Report->addVisitor(llvm::make_unique<SecKeychainBugVisitor>(ArgSM)); |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 440 | Report->addRange(ArgExpr->getSourceRange()); |
Ted Kremenek | 1e809b4 | 2012-03-09 01:13:14 +0000 | [diff] [blame] | 441 | Report->markInteresting(AS->Region); |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 442 | C.emitReport(std::move(Report)); |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 443 | return; |
| 444 | } |
| 445 | |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 446 | C.addTransition(State); |
Anna Zaks | 15f496c | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | void MacOSKeychainAPIChecker::checkPostStmt(const CallExpr *CE, |
| 450 | CheckerContext &C) const { |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 451 | ProgramStateRef State = C.getState(); |
Jordan Rose | 6cd16c5 | 2012-07-10 23:13:01 +0000 | [diff] [blame] | 452 | const FunctionDecl *FD = C.getCalleeDecl(CE); |
| 453 | if (!FD || FD->getKind() != Decl::Function) |
| 454 | return; |
| 455 | |
| 456 | StringRef funName = C.getCalleeName(FD); |
Anna Zaks | 15f496c | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 457 | |
| 458 | // If a value has been allocated, add it to the set for tracking. |
Anna Zaks | 08be9b9 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 459 | unsigned idx = getTrackedFunctionIndex(funName, true); |
| 460 | if (idx == InvalidIdx) |
Anna Zaks | 4aa34a53 | 2011-08-04 00:31:38 +0000 | [diff] [blame] | 461 | return; |
Anna Zaks | 388c18e | 2011-08-04 00:26:57 +0000 | [diff] [blame] | 462 | |
Anna Zaks | 08be9b9 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 463 | const Expr *ArgExpr = CE->getArg(FunctionsToTrack[idx].Param); |
Anna Zaks | 59d741f | 2011-08-12 22:47:22 +0000 | [diff] [blame] | 464 | // If the argument entered as an enclosing function parameter, skip it to |
| 465 | // avoid false positives. |
Anna Zaks | 19a6667 | 2012-02-21 00:00:44 +0000 | [diff] [blame] | 466 | if (isEnclosingFunctionParam(ArgExpr) && |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 467 | C.getLocationContext()->getParent() == nullptr) |
Anna Zaks | 59d741f | 2011-08-12 22:47:22 +0000 | [diff] [blame] | 468 | return; |
| 469 | |
Anna Zaks | c94894f | 2011-08-12 21:14:26 +0000 | [diff] [blame] | 470 | if (SymbolRef V = getAsPointeeSymbol(ArgExpr, C)) { |
| 471 | // If the argument points to something that's not a symbolic region, it |
| 472 | // can be: |
Anna Zaks | 4aa34a53 | 2011-08-04 00:31:38 +0000 | [diff] [blame] | 473 | // - unknown (cannot reason about it) |
| 474 | // - undefined (already reported by other checker) |
Anna Zaks | 08be9b9 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 475 | // - constant (null - should not be tracked, |
| 476 | // other constant will generate a compiler warning) |
Anna Zaks | 4aa34a53 | 2011-08-04 00:31:38 +0000 | [diff] [blame] | 477 | // - goto (should be reported by other checker) |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 478 | |
| 479 | // The call return value symbol should stay alive for as long as the |
| 480 | // allocated value symbol, since our diagnostics depend on the value |
| 481 | // returned by the call. Ex: Data should only be freed if noErr was |
| 482 | // returned during allocation.) |
Ted Kremenek | 632e3b7 | 2012-01-06 22:09:28 +0000 | [diff] [blame] | 483 | SymbolRef RetStatusSymbol = |
| 484 | State->getSVal(CE, C.getLocationContext()).getAsSymbol(); |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 485 | C.getSymbolManager().addSymbolDependency(V, RetStatusSymbol); |
| 486 | |
| 487 | // Track the allocated value in the checker state. |
| 488 | State = State->set<AllocatedData>(V, AllocationState(ArgExpr, idx, |
Anna Zaks | c94894f | 2011-08-12 21:14:26 +0000 | [diff] [blame] | 489 | RetStatusSymbol)); |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 490 | assert(State); |
Anna Zaks | da4c8d6 | 2011-10-26 21:06:34 +0000 | [diff] [blame] | 491 | C.addTransition(State); |
Anna Zaks | 15f496c | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
| 494 | |
Anna Zaks | 6ca4fd5 | 2012-02-28 03:07:06 +0000 | [diff] [blame] | 495 | // TODO: This logic is the same as in Malloc checker. |
Anna Zaks | a043d0c | 2013-01-08 00:25:29 +0000 | [diff] [blame] | 496 | const ExplodedNode * |
| 497 | MacOSKeychainAPIChecker::getAllocationNode(const ExplodedNode *N, |
Anna Zaks | 4b062cb | 2012-02-23 22:53:29 +0000 | [diff] [blame] | 498 | SymbolRef Sym, |
| 499 | CheckerContext &C) const { |
Anna Zaks | 6ca4fd5 | 2012-02-28 03:07:06 +0000 | [diff] [blame] | 500 | const LocationContext *LeakContext = N->getLocationContext(); |
Anna Zaks | 4b062cb | 2012-02-23 22:53:29 +0000 | [diff] [blame] | 501 | // Walk the ExplodedGraph backwards and find the first node that referred to |
| 502 | // the tracked symbol. |
| 503 | const ExplodedNode *AllocNode = N; |
| 504 | |
| 505 | while (N) { |
| 506 | if (!N->getState()->get<AllocatedData>(Sym)) |
| 507 | break; |
Anna Zaks | 486a0ff | 2015-02-05 01:02:53 +0000 | [diff] [blame] | 508 | // Allocation node, is the last node in the current or parent context in |
| 509 | // which the symbol was tracked. |
| 510 | const LocationContext *NContext = N->getLocationContext(); |
| 511 | if (NContext == LeakContext || |
| 512 | NContext->isParentOf(LeakContext)) |
Anna Zaks | 6ca4fd5 | 2012-02-28 03:07:06 +0000 | [diff] [blame] | 513 | AllocNode = N; |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 514 | N = N->pred_empty() ? nullptr : *(N->pred_begin()); |
Anna Zaks | 4b062cb | 2012-02-23 22:53:29 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Anna Zaks | a043d0c | 2013-01-08 00:25:29 +0000 | [diff] [blame] | 517 | return AllocNode; |
Anna Zaks | 4b062cb | 2012-02-23 22:53:29 +0000 | [diff] [blame] | 518 | } |
| 519 | |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 520 | std::unique_ptr<BugReport> |
| 521 | MacOSKeychainAPIChecker::generateAllocatedDataNotReleasedReport( |
| 522 | const AllocationPair &AP, ExplodedNode *N, CheckerContext &C) const { |
Anna Zaks | f880cff | 2011-08-24 21:58:55 +0000 | [diff] [blame] | 523 | const ADFunctionInfo &FI = FunctionsToTrack[AP.second->AllocatorIdx]; |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 524 | initBugType(); |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 525 | SmallString<70> sbuf; |
Anna Zaks | 29f9b7a | 2011-08-15 18:42:00 +0000 | [diff] [blame] | 526 | llvm::raw_svector_ostream os(sbuf); |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 527 | os << "Allocated data is not released: missing a call to '" |
| 528 | << FunctionsToTrack[FI.DeallocatorIdx].Name << "'."; |
Anna Zaks | 4b062cb | 2012-02-23 22:53:29 +0000 | [diff] [blame] | 529 | |
| 530 | // Most bug reports are cached at the location where they occurred. |
| 531 | // With leaks, we want to unique them by the location where they were |
| 532 | // allocated, and only report a single path. |
Anna Zaks | 6ca4fd5 | 2012-02-28 03:07:06 +0000 | [diff] [blame] | 533 | PathDiagnosticLocation LocUsedForUniqueing; |
Anna Zaks | a043d0c | 2013-01-08 00:25:29 +0000 | [diff] [blame] | 534 | const ExplodedNode *AllocNode = getAllocationNode(N, AP.first, C); |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 535 | const Stmt *AllocStmt = nullptr; |
Anna Zaks | a043d0c | 2013-01-08 00:25:29 +0000 | [diff] [blame] | 536 | ProgramPoint P = AllocNode->getLocation(); |
David Blaikie | 87396b9 | 2013-02-21 22:23:56 +0000 | [diff] [blame] | 537 | if (Optional<CallExitEnd> Exit = P.getAs<CallExitEnd>()) |
Anna Zaks | a043d0c | 2013-01-08 00:25:29 +0000 | [diff] [blame] | 538 | AllocStmt = Exit->getCalleeContext()->getCallSite(); |
David Blaikie | 87396b9 | 2013-02-21 22:23:56 +0000 | [diff] [blame] | 539 | else if (Optional<clang::PostStmt> PS = P.getAs<clang::PostStmt>()) |
Anna Zaks | a043d0c | 2013-01-08 00:25:29 +0000 | [diff] [blame] | 540 | AllocStmt = PS->getStmt(); |
Anna Zaks | 4b062cb | 2012-02-23 22:53:29 +0000 | [diff] [blame] | 541 | |
Anna Zaks | a043d0c | 2013-01-08 00:25:29 +0000 | [diff] [blame] | 542 | if (AllocStmt) |
| 543 | LocUsedForUniqueing = PathDiagnosticLocation::createBegin(AllocStmt, |
| 544 | C.getSourceManager(), |
| 545 | AllocNode->getLocationContext()); |
| 546 | |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 547 | auto Report = |
| 548 | llvm::make_unique<BugReport>(*BT, os.str(), N, LocUsedForUniqueing, |
| 549 | AllocNode->getLocationContext()->getDecl()); |
Anna Zaks | a043d0c | 2013-01-08 00:25:29 +0000 | [diff] [blame] | 550 | |
David Blaikie | 91e7902 | 2014-09-04 23:54:33 +0000 | [diff] [blame] | 551 | Report->addVisitor(llvm::make_unique<SecKeychainBugVisitor>(AP.first)); |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 552 | markInteresting(Report.get(), AP); |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 553 | return Report; |
| 554 | } |
| 555 | |
| 556 | void MacOSKeychainAPIChecker::checkDeadSymbols(SymbolReaper &SR, |
| 557 | CheckerContext &C) const { |
Ted Kremenek | 49b1e38 | 2012-01-26 21:29:00 +0000 | [diff] [blame] | 558 | ProgramStateRef State = C.getState(); |
Jordan Rose | 0c153cb | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 559 | AllocatedDataTy ASet = State->get<AllocatedData>(); |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 560 | if (ASet.isEmpty()) |
| 561 | return; |
| 562 | |
| 563 | bool Changed = false; |
Anna Zaks | be46089 | 2011-08-24 20:52:46 +0000 | [diff] [blame] | 564 | AllocationPairVec Errors; |
Jordan Rose | 0c153cb | 2012-11-02 01:54:06 +0000 | [diff] [blame] | 565 | for (AllocatedDataTy::iterator I = ASet.begin(), E = ASet.end(); I != E; ++I) { |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 566 | if (SR.isLive(I->first)) |
| 567 | continue; |
| 568 | |
| 569 | Changed = true; |
| 570 | State = State->remove<AllocatedData>(I->first); |
| 571 | // If the allocated symbol is null or if the allocation call might have |
| 572 | // returned an error, do not report. |
Jordan Rose | 14fe9f3 | 2012-11-01 00:18:27 +0000 | [diff] [blame] | 573 | ConstraintManager &CMgr = State->getConstraintManager(); |
| 574 | ConditionTruthVal AllocFailed = CMgr.isNull(State, I.getKey()); |
| 575 | if (AllocFailed.isConstrainedTrue() || |
Anna Zaks | 5be8a4d | 2011-08-25 00:59:06 +0000 | [diff] [blame] | 576 | definitelyReturnedError(I->second.Region, State, C.getSValBuilder())) |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 577 | continue; |
Anna Zaks | f880cff | 2011-08-24 21:58:55 +0000 | [diff] [blame] | 578 | Errors.push_back(std::make_pair(I->first, &I->second)); |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 579 | } |
Anna Zaks | 4b062cb | 2012-02-23 22:53:29 +0000 | [diff] [blame] | 580 | if (!Changed) { |
| 581 | // Generate the new, cleaned up state. |
| 582 | C.addTransition(State); |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 583 | return; |
Anna Zaks | 4b062cb | 2012-02-23 22:53:29 +0000 | [diff] [blame] | 584 | } |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 585 | |
Anton Yartsev | 6a61922 | 2014-02-17 18:25:34 +0000 | [diff] [blame] | 586 | static CheckerProgramPointTag Tag(this, "DeadSymbolsLeak"); |
Devin Coughlin | e39bd40 | 2015-09-16 22:03:05 +0000 | [diff] [blame] | 587 | ExplodedNode *N = C.generateNonFatalErrorNode(C.getState(), &Tag); |
| 588 | if (!N) |
| 589 | return; |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 590 | |
| 591 | // Generate the error reports. |
Benjamin Kramer | 72e6431 | 2015-09-24 14:48:49 +0000 | [diff] [blame^] | 592 | for (const auto &P : Errors) |
Aaron Ballman | 8d3a7a5 | 2015-06-23 13:15:32 +0000 | [diff] [blame] | 593 | C.emitReport(generateAllocatedDataNotReleasedReport(P, N, C)); |
Anna Zaks | 4b062cb | 2012-02-23 22:53:29 +0000 | [diff] [blame] | 594 | |
| 595 | // Generate the new, cleaned up state. |
| 596 | C.addTransition(State, N); |
Anna Zaks | fdd0aca | 2011-08-12 21:56:43 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Anna Zaks | be46089 | 2011-08-24 20:52:46 +0000 | [diff] [blame] | 599 | |
| 600 | PathDiagnosticPiece *MacOSKeychainAPIChecker::SecKeychainBugVisitor::VisitNode( |
| 601 | const ExplodedNode *N, |
| 602 | const ExplodedNode *PrevN, |
| 603 | BugReporterContext &BRC, |
| 604 | BugReport &BR) { |
| 605 | const AllocationState *AS = N->getState()->get<AllocatedData>(Sym); |
| 606 | if (!AS) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 607 | return nullptr; |
Anna Zaks | be46089 | 2011-08-24 20:52:46 +0000 | [diff] [blame] | 608 | const AllocationState *ASPrev = PrevN->getState()->get<AllocatedData>(Sym); |
| 609 | if (ASPrev) |
Craig Topper | 0dbb783 | 2014-05-27 02:45:47 +0000 | [diff] [blame] | 610 | return nullptr; |
Anna Zaks | be46089 | 2011-08-24 20:52:46 +0000 | [diff] [blame] | 611 | |
| 612 | // (!ASPrev && AS) ~ We started tracking symbol in node N, it must be the |
| 613 | // allocation site. |
David Blaikie | 87396b9 | 2013-02-21 22:23:56 +0000 | [diff] [blame] | 614 | const CallExpr *CE = |
| 615 | cast<CallExpr>(N->getLocation().castAs<StmtPoint>().getStmt()); |
Anna Zaks | be46089 | 2011-08-24 20:52:46 +0000 | [diff] [blame] | 616 | const FunctionDecl *funDecl = CE->getDirectCallee(); |
| 617 | assert(funDecl && "We do not support indirect function calls as of now."); |
| 618 | StringRef funName = funDecl->getName(); |
| 619 | |
| 620 | // Get the expression of the corresponding argument. |
| 621 | unsigned Idx = getTrackedFunctionIndex(funName, true); |
| 622 | assert(Idx != InvalidIdx && "This should be a call to an allocator."); |
| 623 | const Expr *ArgExpr = CE->getArg(FunctionsToTrack[Idx].Param); |
Anna Zaks | 3a769bd | 2011-09-15 01:08:34 +0000 | [diff] [blame] | 624 | PathDiagnosticLocation Pos(ArgExpr, BRC.getSourceManager(), |
| 625 | N->getLocationContext()); |
Anna Zaks | be46089 | 2011-08-24 20:52:46 +0000 | [diff] [blame] | 626 | return new PathDiagnosticEventPiece(Pos, "Data is allocated here."); |
Anna Zaks | 15f496c | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | void ento::registerMacOSKeychainAPIChecker(CheckerManager &mgr) { |
| 630 | mgr.registerChecker<MacOSKeychainAPIChecker>(); |
| 631 | } |