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