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" |
| 20 | #include "clang/StaticAnalyzer/Core/PathSensitive/GRState.h" |
| 21 | #include "clang/StaticAnalyzer/Core/PathSensitive/GRStateTrait.h" |
| 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>, |
| 30 | check::EndPath > { |
Anna Zaks | 03826aa | 2011-08-04 00:26:57 +0000 | [diff] [blame] | 31 | mutable llvm::OwningPtr<BugType> BT; |
| 32 | |
Anna Zaks | f57be28 | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 33 | public: |
Anna Zaks | 864d252 | 2011-08-12 21:14:26 +0000 | [diff] [blame^] | 34 | /// AllocationState is a part of the checker specific state together with the |
| 35 | /// MemRegion corresponding to the allocated data. |
| 36 | struct AllocationState { |
| 37 | const Expr *Address; |
| 38 | /// The index of the allocator function. |
| 39 | unsigned int AllocatorIdx; |
| 40 | SymbolRef RetValue; |
| 41 | |
| 42 | AllocationState(const Expr *E, unsigned int Idx, SymbolRef R) : |
| 43 | Address(E), |
| 44 | AllocatorIdx(Idx), |
| 45 | RetValue(R) {} |
| 46 | |
| 47 | bool operator==(const AllocationState &X) const { |
| 48 | return Address == X.Address; |
| 49 | } |
| 50 | void Profile(llvm::FoldingSetNodeID &ID) const { |
| 51 | ID.AddPointer(Address); |
| 52 | ID.AddInteger(AllocatorIdx); |
| 53 | } |
| 54 | }; |
| 55 | |
Anna Zaks | f57be28 | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 56 | void checkPreStmt(const CallExpr *S, CheckerContext &C) const; |
| 57 | void checkPreStmt(const ReturnStmt *S, CheckerContext &C) const; |
| 58 | void checkPostStmt(const CallExpr *S, CheckerContext &C) const; |
| 59 | |
| 60 | void checkEndPath(EndOfFunctionNodeBuilder &B, ExprEngine &Eng) const; |
| 61 | |
| 62 | private: |
Anna Zaks | 083fcb2 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 63 | /// Stores the information about the allocator and deallocator functions - |
| 64 | /// these are the functions the checker is tracking. |
| 65 | struct ADFunctionInfo { |
| 66 | const char* Name; |
| 67 | unsigned int Param; |
| 68 | unsigned int DeallocatorIdx; |
| 69 | }; |
| 70 | static const unsigned InvalidIdx = 100000; |
Anna Zaks | 76cbb75 | 2011-08-04 21:53:01 +0000 | [diff] [blame] | 71 | static const unsigned FunctionsToTrackSize = 6; |
Anna Zaks | 083fcb2 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 72 | static const ADFunctionInfo FunctionsToTrack[FunctionsToTrackSize]; |
Anna Zaks | 5a58c6d | 2011-08-05 23:52:45 +0000 | [diff] [blame] | 73 | /// The value, which represents no error return value for allocator functions. |
| 74 | static const unsigned NoErr = 0; |
Anna Zaks | f57be28 | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 75 | |
Anna Zaks | 083fcb2 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 76 | /// Given the function name, returns the index of the allocator/deallocator |
| 77 | /// function. |
| 78 | unsigned getTrackedFunctionIndex(StringRef Name, bool IsAllocator) const; |
Anna Zaks | 03826aa | 2011-08-04 00:26:57 +0000 | [diff] [blame] | 79 | |
| 80 | inline void initBugType() const { |
| 81 | if (!BT) |
| 82 | BT.reset(new BugType("Improper use of SecKeychain API", "Mac OS API")); |
| 83 | } |
Anna Zaks | f57be28 | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 84 | }; |
| 85 | } |
| 86 | |
Anna Zaks | 083fcb2 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 87 | /// GRState traits to store the currently allocated (and not yet freed) symbols. |
Anna Zaks | 864d252 | 2011-08-12 21:14:26 +0000 | [diff] [blame^] | 88 | /// This is a map from the allocated content symbol to the corresponding |
| 89 | /// AllocationState. |
| 90 | typedef llvm::ImmutableMap<SymbolRef, |
| 91 | MacOSKeychainAPIChecker::AllocationState> AllocatedSetTy; |
Anna Zaks | f57be28 | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 92 | |
| 93 | namespace { struct AllocatedData {}; } |
| 94 | namespace clang { namespace ento { |
| 95 | template<> struct GRStateTrait<AllocatedData> |
| 96 | : public GRStatePartialTrait<AllocatedSetTy > { |
| 97 | static void *GDMIndex() { static int index = 0; return &index; } |
| 98 | }; |
| 99 | }} |
| 100 | |
Anna Zaks | 03826aa | 2011-08-04 00:26:57 +0000 | [diff] [blame] | 101 | static bool isEnclosingFunctionParam(const Expr *E) { |
| 102 | E = E->IgnoreParenCasts(); |
| 103 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 104 | const ValueDecl *VD = DRE->getDecl(); |
| 105 | if (isa<ImplicitParamDecl>(VD) || isa<ParmVarDecl>(VD)) |
| 106 | return true; |
| 107 | } |
| 108 | return false; |
| 109 | } |
| 110 | |
Anna Zaks | 083fcb2 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 111 | const MacOSKeychainAPIChecker::ADFunctionInfo |
| 112 | MacOSKeychainAPIChecker::FunctionsToTrack[FunctionsToTrackSize] = { |
| 113 | {"SecKeychainItemCopyContent", 4, 3}, // 0 |
| 114 | {"SecKeychainFindGenericPassword", 6, 3}, // 1 |
| 115 | {"SecKeychainFindInternetPassword", 13, 3}, // 2 |
| 116 | {"SecKeychainItemFreeContent", 1, InvalidIdx}, // 3 |
Anna Zaks | 76cbb75 | 2011-08-04 21:53:01 +0000 | [diff] [blame] | 117 | {"SecKeychainItemCopyAttributesAndData", 5, 5}, // 4 |
| 118 | {"SecKeychainItemFreeAttributesAndData", 1, InvalidIdx}, // 5 |
Anna Zaks | 083fcb2 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | unsigned MacOSKeychainAPIChecker::getTrackedFunctionIndex(StringRef Name, |
| 122 | bool IsAllocator) const { |
| 123 | for (unsigned I = 0; I < FunctionsToTrackSize; ++I) { |
| 124 | ADFunctionInfo FI = FunctionsToTrack[I]; |
| 125 | if (FI.Name != Name) |
| 126 | continue; |
| 127 | // Make sure the function is of the right type (allocator vs deallocator). |
| 128 | if (IsAllocator && (FI.DeallocatorIdx == InvalidIdx)) |
| 129 | return InvalidIdx; |
| 130 | if (!IsAllocator && (FI.DeallocatorIdx != InvalidIdx)) |
| 131 | return InvalidIdx; |
| 132 | |
| 133 | return I; |
| 134 | } |
| 135 | // The function is not tracked. |
| 136 | return InvalidIdx; |
| 137 | } |
| 138 | |
Anna Zaks | 864d252 | 2011-08-12 21:14:26 +0000 | [diff] [blame^] | 139 | static SymbolRef getSymbolForRegion(CheckerContext &C, |
| 140 | const MemRegion *R) { |
| 141 | if (!isa<SymbolicRegion>(R)) |
| 142 | return 0; |
| 143 | return cast<SymbolicRegion>(R)->getSymbol(); |
Anna Zaks | 5a58c6d | 2011-08-05 23:52:45 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Anna Zaks | 864d252 | 2011-08-12 21:14:26 +0000 | [diff] [blame^] | 146 | static bool isBadDeallocationArgument(const MemRegion *Arg) { |
| 147 | if (isa<AllocaRegion>(Arg) || |
| 148 | isa<BlockDataRegion>(Arg) || |
| 149 | isa<TypedRegion>(Arg)) { |
| 150 | return true; |
| 151 | } |
| 152 | return false; |
| 153 | } |
Anna Zaks | ca0b57e | 2011-08-05 00:37:00 +0000 | [diff] [blame] | 154 | /// Given the address expression, retrieve the value it's pointing to. Assume |
Anna Zaks | 864d252 | 2011-08-12 21:14:26 +0000 | [diff] [blame^] | 155 | /// that value is itself an address, and return the corresponding symbol. |
| 156 | static SymbolRef getAsPointeeSymbol(const Expr *Expr, |
| 157 | CheckerContext &C) { |
Anna Zaks | ca0b57e | 2011-08-05 00:37:00 +0000 | [diff] [blame] | 158 | const GRState *State = C.getState(); |
| 159 | SVal ArgV = State->getSVal(Expr); |
Anna Zaks | 5a58c6d | 2011-08-05 23:52:45 +0000 | [diff] [blame] | 160 | |
Anna Zaks | ca0b57e | 2011-08-05 00:37:00 +0000 | [diff] [blame] | 161 | if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(&ArgV)) { |
| 162 | StoreManager& SM = C.getStoreManager(); |
| 163 | const MemRegion *V = SM.Retrieve(State->getStore(), *X).getAsRegion(); |
Anna Zaks | 5a58c6d | 2011-08-05 23:52:45 +0000 | [diff] [blame] | 164 | if (V) |
Anna Zaks | 864d252 | 2011-08-12 21:14:26 +0000 | [diff] [blame^] | 165 | return getSymbolForRegion(C, V); |
Anna Zaks | ca0b57e | 2011-08-05 00:37:00 +0000 | [diff] [blame] | 166 | } |
| 167 | return 0; |
| 168 | } |
| 169 | |
Anna Zaks | f57be28 | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 170 | void MacOSKeychainAPIChecker::checkPreStmt(const CallExpr *CE, |
| 171 | CheckerContext &C) const { |
| 172 | const GRState *State = C.getState(); |
| 173 | const Expr *Callee = CE->getCallee(); |
| 174 | SVal L = State->getSVal(Callee); |
Anna Zaks | ca0b57e | 2011-08-05 00:37:00 +0000 | [diff] [blame] | 175 | unsigned idx = InvalidIdx; |
Anna Zaks | f57be28 | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 176 | |
| 177 | const FunctionDecl *funDecl = L.getAsFunctionDecl(); |
| 178 | if (!funDecl) |
| 179 | return; |
| 180 | IdentifierInfo *funI = funDecl->getIdentifier(); |
| 181 | if (!funI) |
| 182 | return; |
| 183 | StringRef funName = funI->getName(); |
| 184 | |
Anna Zaks | ca0b57e | 2011-08-05 00:37:00 +0000 | [diff] [blame] | 185 | // If it is a call to an allocator function, it could be a double allocation. |
| 186 | idx = getTrackedFunctionIndex(funName, true); |
| 187 | if (idx != InvalidIdx) { |
| 188 | const Expr *ArgExpr = CE->getArg(FunctionsToTrack[idx].Param); |
Anna Zaks | 864d252 | 2011-08-12 21:14:26 +0000 | [diff] [blame^] | 189 | if (SymbolRef V = getAsPointeeSymbol(ArgExpr, C)) |
Anna Zaks | ca0b57e | 2011-08-05 00:37:00 +0000 | [diff] [blame] | 190 | if (const AllocationState *AS = State->get<AllocatedData>(V)) { |
| 191 | ExplodedNode *N = C.generateSink(State); |
| 192 | if (!N) |
| 193 | return; |
| 194 | initBugType(); |
| 195 | std::string sbuf; |
| 196 | llvm::raw_string_ostream os(sbuf); |
| 197 | unsigned int DIdx = FunctionsToTrack[AS->AllocatorIdx].DeallocatorIdx; |
| 198 | os << "Allocated data should be released before another call to " |
| 199 | << "the allocator: missing a call to '" |
| 200 | << FunctionsToTrack[DIdx].Name |
| 201 | << "'."; |
| 202 | RangedBugReport *Report = new RangedBugReport(*BT, os.str(), N); |
| 203 | Report->addRange(ArgExpr->getSourceRange()); |
| 204 | C.EmitReport(Report); |
| 205 | } |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | // Is it a call to one of deallocator functions? |
| 210 | idx = getTrackedFunctionIndex(funName, false); |
Anna Zaks | 083fcb2 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 211 | if (idx == InvalidIdx) |
Anna Zaks | 08551b5 | 2011-08-04 00:31:38 +0000 | [diff] [blame] | 212 | return; |
| 213 | |
Anna Zaks | 864d252 | 2011-08-12 21:14:26 +0000 | [diff] [blame^] | 214 | // Check the argument to the deallocator. |
Anna Zaks | 083fcb2 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 215 | const Expr *ArgExpr = CE->getArg(FunctionsToTrack[idx].Param); |
Anna Zaks | 864d252 | 2011-08-12 21:14:26 +0000 | [diff] [blame^] | 216 | SVal ArgSVal = State->getSVal(ArgExpr); |
| 217 | |
| 218 | // Undef is reported by another checker. |
| 219 | if (ArgSVal.isUndef()) |
| 220 | return; |
| 221 | |
| 222 | const MemRegion *Arg = ArgSVal.getAsRegion(); |
Anna Zaks | 08551b5 | 2011-08-04 00:31:38 +0000 | [diff] [blame] | 223 | if (!Arg) |
| 224 | return; |
Anna Zaks | 864d252 | 2011-08-12 21:14:26 +0000 | [diff] [blame^] | 225 | |
| 226 | SymbolRef ArgSM = getSymbolForRegion(C, Arg); |
| 227 | bool RegionArgIsBad = ArgSM ? false : isBadDeallocationArgument(Arg); |
| 228 | // If the argument is coming from the heap, globals, or unknown, do not |
| 229 | // report it. |
| 230 | if (!ArgSM && !RegionArgIsBad) |
| 231 | return; |
Anna Zaks | 08551b5 | 2011-08-04 00:31:38 +0000 | [diff] [blame] | 232 | |
| 233 | // If trying to free data which has not been allocated yet, report as bug. |
Anna Zaks | 5a58c6d | 2011-08-05 23:52:45 +0000 | [diff] [blame] | 234 | const AllocationState *AS = State->get<AllocatedData>(ArgSM); |
Anna Zaks | 864d252 | 2011-08-12 21:14:26 +0000 | [diff] [blame^] | 235 | if (!AS || RegionArgIsBad) { |
Anna Zaks | 08551b5 | 2011-08-04 00:31:38 +0000 | [diff] [blame] | 236 | // It is possible that this is a false positive - the argument might |
| 237 | // have entered as an enclosing function parameter. |
| 238 | if (isEnclosingFunctionParam(ArgExpr)) |
Anna Zaks | f57be28 | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 239 | return; |
Anna Zaks | 03826aa | 2011-08-04 00:26:57 +0000 | [diff] [blame] | 240 | |
Anna Zaks | 08551b5 | 2011-08-04 00:31:38 +0000 | [diff] [blame] | 241 | ExplodedNode *N = C.generateNode(State); |
| 242 | if (!N) |
| 243 | return; |
| 244 | initBugType(); |
| 245 | RangedBugReport *Report = new RangedBugReport(*BT, |
| 246 | "Trying to free data which has not been allocated.", N); |
| 247 | Report->addRange(ArgExpr->getSourceRange()); |
| 248 | C.EmitReport(Report); |
Anna Zaks | 083fcb2 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 249 | return; |
Anna Zaks | f57be28 | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 250 | } |
Anna Zaks | 08551b5 | 2011-08-04 00:31:38 +0000 | [diff] [blame] | 251 | |
Anna Zaks | 76cbb75 | 2011-08-04 21:53:01 +0000 | [diff] [blame] | 252 | // Check if the proper deallocator is used. |
| 253 | unsigned int PDeallocIdx = FunctionsToTrack[AS->AllocatorIdx].DeallocatorIdx; |
| 254 | if (PDeallocIdx != idx) { |
| 255 | ExplodedNode *N = C.generateSink(State); |
| 256 | if (!N) |
| 257 | return; |
| 258 | initBugType(); |
| 259 | |
| 260 | std::string sbuf; |
| 261 | llvm::raw_string_ostream os(sbuf); |
| 262 | os << "Allocator doesn't match the deallocator: '" |
| 263 | << FunctionsToTrack[PDeallocIdx].Name << "' should be used."; |
| 264 | RangedBugReport *Report = new RangedBugReport(*BT, os.str(), N); |
| 265 | Report->addRange(ArgExpr->getSourceRange()); |
| 266 | C.EmitReport(Report); |
| 267 | return; |
| 268 | } |
| 269 | |
Anna Zaks | 864d252 | 2011-08-12 21:14:26 +0000 | [diff] [blame^] | 270 | // The call is deallocating a value we previously allocated, so remove it |
| 271 | // from the next state. |
Anna Zaks | 5a58c6d | 2011-08-05 23:52:45 +0000 | [diff] [blame] | 272 | State = State->remove<AllocatedData>(ArgSM); |
Anna Zaks | 08551b5 | 2011-08-04 00:31:38 +0000 | [diff] [blame] | 273 | C.addTransition(State); |
Anna Zaks | f57be28 | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | void MacOSKeychainAPIChecker::checkPostStmt(const CallExpr *CE, |
| 277 | CheckerContext &C) const { |
| 278 | const GRState *State = C.getState(); |
| 279 | const Expr *Callee = CE->getCallee(); |
| 280 | SVal L = State->getSVal(Callee); |
Anna Zaks | f57be28 | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 281 | |
| 282 | const FunctionDecl *funDecl = L.getAsFunctionDecl(); |
| 283 | if (!funDecl) |
| 284 | return; |
| 285 | IdentifierInfo *funI = funDecl->getIdentifier(); |
| 286 | if (!funI) |
| 287 | return; |
| 288 | StringRef funName = funI->getName(); |
| 289 | |
| 290 | // 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] | 291 | unsigned idx = getTrackedFunctionIndex(funName, true); |
| 292 | if (idx == InvalidIdx) |
Anna Zaks | 08551b5 | 2011-08-04 00:31:38 +0000 | [diff] [blame] | 293 | return; |
Anna Zaks | 03826aa | 2011-08-04 00:26:57 +0000 | [diff] [blame] | 294 | |
Anna Zaks | 083fcb2 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 295 | const Expr *ArgExpr = CE->getArg(FunctionsToTrack[idx].Param); |
Anna Zaks | 864d252 | 2011-08-12 21:14:26 +0000 | [diff] [blame^] | 296 | if (SymbolRef V = getAsPointeeSymbol(ArgExpr, C)) { |
| 297 | // If the argument points to something that's not a symbolic region, it |
| 298 | // can be: |
Anna Zaks | 08551b5 | 2011-08-04 00:31:38 +0000 | [diff] [blame] | 299 | // - unknown (cannot reason about it) |
| 300 | // - undefined (already reported by other checker) |
Anna Zaks | 083fcb2 | 2011-08-04 17:28:06 +0000 | [diff] [blame] | 301 | // - constant (null - should not be tracked, |
| 302 | // other constant will generate a compiler warning) |
Anna Zaks | 08551b5 | 2011-08-04 00:31:38 +0000 | [diff] [blame] | 303 | // - goto (should be reported by other checker) |
Anna Zaks | 864d252 | 2011-08-12 21:14:26 +0000 | [diff] [blame^] | 304 | |
Anna Zaks | 08551b5 | 2011-08-04 00:31:38 +0000 | [diff] [blame] | 305 | // We only need to track the value if the function returned noErr(0), so |
Anna Zaks | 76cbb75 | 2011-08-04 21:53:01 +0000 | [diff] [blame] | 306 | // bind the return value of the function to 0 and proceed from the no error |
| 307 | // state. |
Anna Zaks | 08551b5 | 2011-08-04 00:31:38 +0000 | [diff] [blame] | 308 | SValBuilder &Builder = C.getSValBuilder(); |
Anna Zaks | 5a58c6d | 2011-08-05 23:52:45 +0000 | [diff] [blame] | 309 | SVal NoErrVal = Builder.makeIntVal(NoErr, CE->getCallReturnType()); |
| 310 | const GRState *NoErr = State->BindExpr(CE, NoErrVal); |
Anna Zaks | ca0b57e | 2011-08-05 00:37:00 +0000 | [diff] [blame] | 311 | // Add the symbolic value V, which represents the location of the allocated |
| 312 | // data, to the set. |
Anna Zaks | 864d252 | 2011-08-12 21:14:26 +0000 | [diff] [blame^] | 313 | SymbolRef RetStatusSymbol = State->getSVal(CE).getAsSymbol(); |
| 314 | NoErr = NoErr->set<AllocatedData>(V, AllocationState(ArgExpr, idx, |
| 315 | RetStatusSymbol)); |
Anna Zaks | 5a58c6d | 2011-08-05 23:52:45 +0000 | [diff] [blame] | 316 | |
Anna Zaks | 76cbb75 | 2011-08-04 21:53:01 +0000 | [diff] [blame] | 317 | assert(NoErr); |
| 318 | C.addTransition(NoErr); |
Anna Zaks | 08551b5 | 2011-08-04 00:31:38 +0000 | [diff] [blame] | 319 | |
Anna Zaks | 76cbb75 | 2011-08-04 21:53:01 +0000 | [diff] [blame] | 320 | // Generate a transition to explore the state space when there is an error. |
| 321 | // In this case, we do not track the allocated data. |
| 322 | SVal ReturnedError = Builder.evalBinOpNN(State, BO_NE, |
Anna Zaks | 5a58c6d | 2011-08-05 23:52:45 +0000 | [diff] [blame] | 323 | cast<NonLoc>(NoErrVal), |
Anna Zaks | 76cbb75 | 2011-08-04 21:53:01 +0000 | [diff] [blame] | 324 | cast<NonLoc>(State->getSVal(CE)), |
| 325 | CE->getCallReturnType()); |
| 326 | const GRState *Err = State->assume(cast<NonLoc>(ReturnedError), true); |
| 327 | assert(Err); |
| 328 | C.addTransition(Err); |
Anna Zaks | f57be28 | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
| 332 | void MacOSKeychainAPIChecker::checkPreStmt(const ReturnStmt *S, |
| 333 | CheckerContext &C) const { |
| 334 | const Expr *retExpr = S->getRetValue(); |
| 335 | if (!retExpr) |
| 336 | return; |
| 337 | |
| 338 | // Check if the value is escaping through the return. |
| 339 | const GRState *state = C.getState(); |
Anna Zaks | 03826aa | 2011-08-04 00:26:57 +0000 | [diff] [blame] | 340 | const MemRegion *V = state->getSVal(retExpr).getAsRegion(); |
Anna Zaks | f57be28 | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 341 | if (!V) |
| 342 | return; |
Anna Zaks | 864d252 | 2011-08-12 21:14:26 +0000 | [diff] [blame^] | 343 | state = state->remove<AllocatedData>(getSymbolForRegion(C, V)); |
Anna Zaks | f57be28 | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 344 | |
Anna Zaks | 03826aa | 2011-08-04 00:26:57 +0000 | [diff] [blame] | 345 | // Proceed from the new state. |
| 346 | C.addTransition(state); |
Anna Zaks | f57be28 | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | void MacOSKeychainAPIChecker::checkEndPath(EndOfFunctionNodeBuilder &B, |
Anna Zaks | 03826aa | 2011-08-04 00:26:57 +0000 | [diff] [blame] | 350 | ExprEngine &Eng) const { |
Anna Zaks | f57be28 | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 351 | const GRState *state = B.getState(); |
| 352 | AllocatedSetTy AS = state->get<AllocatedData>(); |
Anna Zaks | 03826aa | 2011-08-04 00:26:57 +0000 | [diff] [blame] | 353 | ExplodedNode *N = B.generateNode(state); |
| 354 | if (!N) |
| 355 | return; |
| 356 | initBugType(); |
Anna Zaks | f57be28 | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 357 | |
| 358 | // Anything which has been allocated but not freed (nor escaped) will be |
| 359 | // found here, so report it. |
Anna Zaks | 03826aa | 2011-08-04 00:26:57 +0000 | [diff] [blame] | 360 | for (AllocatedSetTy::iterator I = AS.begin(), E = AS.end(); I != E; ++I ) { |
Anna Zaks | 76cbb75 | 2011-08-04 21:53:01 +0000 | [diff] [blame] | 361 | const ADFunctionInfo &FI = FunctionsToTrack[I->second.AllocatorIdx]; |
| 362 | |
| 363 | std::string sbuf; |
| 364 | llvm::raw_string_ostream os(sbuf); |
| 365 | os << "Allocated data is not released: missing a call to '" |
| 366 | << FunctionsToTrack[FI.DeallocatorIdx].Name << "'."; |
| 367 | RangedBugReport *Report = new RangedBugReport(*BT, os.str(), N); |
Anna Zaks | 03826aa | 2011-08-04 00:26:57 +0000 | [diff] [blame] | 368 | // TODO: The report has to mention the expression which contains the |
| 369 | // allocated content as well as the point at which it has been allocated. |
| 370 | // Currently, the next line is useless. |
| 371 | Report->addRange(I->second.Address->getSourceRange()); |
| 372 | Eng.getBugReporter().EmitReport(Report); |
Anna Zaks | f57be28 | 2011-08-01 22:40:01 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
| 376 | void ento::registerMacOSKeychainAPIChecker(CheckerManager &mgr) { |
| 377 | mgr.registerChecker<MacOSKeychainAPIChecker>(); |
| 378 | } |