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