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