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