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