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