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