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