blob: 0b369d795a9422ed7ed7addc67c69c16eb1d02bd [file] [log] [blame]
Anna Zaks083fcb22011-08-04 17:28:06 +00001//==--- MacOSKeychainAPIChecker.cpp ------------------------------*- C++ -*-==//
Anna Zaksf57be282011-08-01 22:40:01 +00002//
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 Zaks03826aa2011-08-04 00:26:57 +000018#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Anna Zaksf57be282011-08-01 22:40:01 +000019#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Ted Kremenek18c66fd2011-08-15 22:09:50 +000020#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
21#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
Anna Zaksf57be282011-08-01 22:40:01 +000022
23using namespace clang;
24using namespace ento;
25
26namespace {
27class MacOSKeychainAPIChecker : public Checker<check::PreStmt<CallExpr>,
28 check::PreStmt<ReturnStmt>,
29 check::PostStmt<CallExpr>,
Anna Zaks703ffb12011-08-12 21:56:43 +000030 check::EndPath,
31 check::DeadSymbols> {
Anna Zaks03826aa2011-08-04 00:26:57 +000032 mutable llvm::OwningPtr<BugType> BT;
33
Anna Zaksf57be282011-08-01 22:40:01 +000034public:
Anna Zaks864d2522011-08-12 21:14:26 +000035 /// 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 Zaksf57be282011-08-01 22:40:01 +000057 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 Zaks703ffb12011-08-12 21:56:43 +000061 void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
Anna Zaksf57be282011-08-01 22:40:01 +000062 void checkEndPath(EndOfFunctionNodeBuilder &B, ExprEngine &Eng) const;
63
64private:
Anna Zaks083fcb22011-08-04 17:28:06 +000065 /// Stores the information about the allocator and deallocator functions -
66 /// these are the functions the checker is tracking.
67 struct ADFunctionInfo {
68 const char* Name;
69 unsigned int Param;
70 unsigned int DeallocatorIdx;
71 };
72 static const unsigned InvalidIdx = 100000;
Anna Zaks76cbb752011-08-04 21:53:01 +000073 static const unsigned FunctionsToTrackSize = 6;
Anna Zaks083fcb22011-08-04 17:28:06 +000074 static const ADFunctionInfo FunctionsToTrack[FunctionsToTrackSize];
Anna Zaks5a58c6d2011-08-05 23:52:45 +000075 /// The value, which represents no error return value for allocator functions.
76 static const unsigned NoErr = 0;
Anna Zaksf57be282011-08-01 22:40:01 +000077
Anna Zaks083fcb22011-08-04 17:28:06 +000078 /// Given the function name, returns the index of the allocator/deallocator
79 /// function.
80 unsigned getTrackedFunctionIndex(StringRef Name, bool IsAllocator) const;
Anna Zaks03826aa2011-08-04 00:26:57 +000081
82 inline void initBugType() const {
83 if (!BT)
84 BT.reset(new BugType("Improper use of SecKeychain API", "Mac OS API"));
85 }
Anna Zaks703ffb12011-08-12 21:56:43 +000086
87 RangedBugReport *generateAllocatedDataNotReleasedReport(
88 const AllocationState &AS,
89 ExplodedNode *N) const;
90
91 /// Check if RetSym evaluates to an error value in the current state.
92 bool definitelyReturnedError(SymbolRef RetSym,
Ted Kremenek18c66fd2011-08-15 22:09:50 +000093 const ProgramState *State,
Anna Zaks703ffb12011-08-12 21:56:43 +000094 SValBuilder &Builder,
95 bool noError = false) const;
96
97 /// Check if RetSym evaluates to a NoErr value in the current state.
98 bool definitelyDidnotReturnError(SymbolRef RetSym,
Ted Kremenek18c66fd2011-08-15 22:09:50 +000099 const ProgramState *State,
Anna Zaks703ffb12011-08-12 21:56:43 +0000100 SValBuilder &Builder) const {
101 return definitelyReturnedError(RetSym, State, Builder, true);
102 }
103
Anna Zaksf57be282011-08-01 22:40:01 +0000104};
105}
106
Anna Zaks7d458b02011-08-15 23:23:15 +0000107/// ProgramState traits to store the currently allocated (and not yet freed)
108/// symbols. This is a map from the allocated content symbol to the
109/// corresponding AllocationState.
Anna Zaks864d2522011-08-12 21:14:26 +0000110typedef llvm::ImmutableMap<SymbolRef,
111 MacOSKeychainAPIChecker::AllocationState> AllocatedSetTy;
Anna Zaksf57be282011-08-01 22:40:01 +0000112
113namespace { struct AllocatedData {}; }
114namespace clang { namespace ento {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000115template<> struct ProgramStateTrait<AllocatedData>
116 : public ProgramStatePartialTrait<AllocatedSetTy > {
Anna Zaksf57be282011-08-01 22:40:01 +0000117 static void *GDMIndex() { static int index = 0; return &index; }
118};
119}}
120
Anna Zaks03826aa2011-08-04 00:26:57 +0000121static bool isEnclosingFunctionParam(const Expr *E) {
122 E = E->IgnoreParenCasts();
123 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
124 const ValueDecl *VD = DRE->getDecl();
125 if (isa<ImplicitParamDecl>(VD) || isa<ParmVarDecl>(VD))
126 return true;
127 }
128 return false;
129}
130
Anna Zaks083fcb22011-08-04 17:28:06 +0000131const MacOSKeychainAPIChecker::ADFunctionInfo
132 MacOSKeychainAPIChecker::FunctionsToTrack[FunctionsToTrackSize] = {
133 {"SecKeychainItemCopyContent", 4, 3}, // 0
134 {"SecKeychainFindGenericPassword", 6, 3}, // 1
135 {"SecKeychainFindInternetPassword", 13, 3}, // 2
136 {"SecKeychainItemFreeContent", 1, InvalidIdx}, // 3
Anna Zaks76cbb752011-08-04 21:53:01 +0000137 {"SecKeychainItemCopyAttributesAndData", 5, 5}, // 4
138 {"SecKeychainItemFreeAttributesAndData", 1, InvalidIdx}, // 5
Anna Zaks083fcb22011-08-04 17:28:06 +0000139};
140
141unsigned MacOSKeychainAPIChecker::getTrackedFunctionIndex(StringRef Name,
142 bool IsAllocator) const {
143 for (unsigned I = 0; I < FunctionsToTrackSize; ++I) {
144 ADFunctionInfo FI = FunctionsToTrack[I];
145 if (FI.Name != Name)
146 continue;
147 // Make sure the function is of the right type (allocator vs deallocator).
148 if (IsAllocator && (FI.DeallocatorIdx == InvalidIdx))
149 return InvalidIdx;
150 if (!IsAllocator && (FI.DeallocatorIdx != InvalidIdx))
151 return InvalidIdx;
152
153 return I;
154 }
155 // The function is not tracked.
156 return InvalidIdx;
157}
158
Anna Zaks864d2522011-08-12 21:14:26 +0000159static SymbolRef getSymbolForRegion(CheckerContext &C,
160 const MemRegion *R) {
161 if (!isa<SymbolicRegion>(R))
162 return 0;
163 return cast<SymbolicRegion>(R)->getSymbol();
Anna Zaks5a58c6d2011-08-05 23:52:45 +0000164}
165
Anna Zaks864d2522011-08-12 21:14:26 +0000166static bool isBadDeallocationArgument(const MemRegion *Arg) {
167 if (isa<AllocaRegion>(Arg) ||
168 isa<BlockDataRegion>(Arg) ||
169 isa<TypedRegion>(Arg)) {
170 return true;
171 }
172 return false;
173}
Anna Zaksca0b57e2011-08-05 00:37:00 +0000174/// Given the address expression, retrieve the value it's pointing to. Assume
Anna Zaks864d2522011-08-12 21:14:26 +0000175/// that value is itself an address, and return the corresponding symbol.
176static SymbolRef getAsPointeeSymbol(const Expr *Expr,
177 CheckerContext &C) {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000178 const ProgramState *State = C.getState();
Anna Zaksca0b57e2011-08-05 00:37:00 +0000179 SVal ArgV = State->getSVal(Expr);
Anna Zaks5a58c6d2011-08-05 23:52:45 +0000180
Anna Zaksca0b57e2011-08-05 00:37:00 +0000181 if (const loc::MemRegionVal *X = dyn_cast<loc::MemRegionVal>(&ArgV)) {
182 StoreManager& SM = C.getStoreManager();
183 const MemRegion *V = SM.Retrieve(State->getStore(), *X).getAsRegion();
Anna Zaks5a58c6d2011-08-05 23:52:45 +0000184 if (V)
Anna Zaks864d2522011-08-12 21:14:26 +0000185 return getSymbolForRegion(C, V);
Anna Zaksca0b57e2011-08-05 00:37:00 +0000186 }
187 return 0;
188}
189
Anna Zaks703ffb12011-08-12 21:56:43 +0000190// When checking for error code, we need to consider the following cases:
191// 1) noErr / [0]
192// 2) someErr / [1, inf]
193// 3) unknown
194// If noError, returns true iff (1).
195// If !noError, returns true iff (2).
196bool MacOSKeychainAPIChecker::definitelyReturnedError(SymbolRef RetSym,
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000197 const ProgramState *State,
Anna Zaks703ffb12011-08-12 21:56:43 +0000198 SValBuilder &Builder,
199 bool noError) const {
200 DefinedOrUnknownSVal NoErrVal = Builder.makeIntVal(NoErr,
201 Builder.getSymbolManager().getType(RetSym));
202 DefinedOrUnknownSVal NoErr = Builder.evalEQ(State, NoErrVal,
203 nonloc::SymbolVal(RetSym));
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000204 const ProgramState *ErrState = State->assume(NoErr, noError);
Anna Zaks703ffb12011-08-12 21:56:43 +0000205 if (ErrState == State) {
206 return true;
207 }
208
209 return false;
210}
211
Anna Zaksf57be282011-08-01 22:40:01 +0000212void MacOSKeychainAPIChecker::checkPreStmt(const CallExpr *CE,
213 CheckerContext &C) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000214 const ProgramState *State = C.getState();
Anna Zaksf57be282011-08-01 22:40:01 +0000215 const Expr *Callee = CE->getCallee();
216 SVal L = State->getSVal(Callee);
Anna Zaksca0b57e2011-08-05 00:37:00 +0000217 unsigned idx = InvalidIdx;
Anna Zaksf57be282011-08-01 22:40:01 +0000218
219 const FunctionDecl *funDecl = L.getAsFunctionDecl();
220 if (!funDecl)
221 return;
222 IdentifierInfo *funI = funDecl->getIdentifier();
223 if (!funI)
224 return;
225 StringRef funName = funI->getName();
226
Anna Zaksca0b57e2011-08-05 00:37:00 +0000227 // If it is a call to an allocator function, it could be a double allocation.
228 idx = getTrackedFunctionIndex(funName, true);
229 if (idx != InvalidIdx) {
230 const Expr *ArgExpr = CE->getArg(FunctionsToTrack[idx].Param);
Anna Zaks864d2522011-08-12 21:14:26 +0000231 if (SymbolRef V = getAsPointeeSymbol(ArgExpr, C))
Anna Zaksca0b57e2011-08-05 00:37:00 +0000232 if (const AllocationState *AS = State->get<AllocatedData>(V)) {
Anna Zaks7d458b02011-08-15 23:23:15 +0000233 // Remove the value from the state. The new symbol will be added for
234 // tracking when the second allocator is processed in checkPostStmt().
235 State = State->remove<AllocatedData>(V);
236 ExplodedNode *N = C.generateNode(State);
Anna Zaksca0b57e2011-08-05 00:37:00 +0000237 if (!N)
238 return;
239 initBugType();
Anna Zaks67f7fa42011-08-15 18:42:00 +0000240 llvm::SmallString<128> sbuf;
241 llvm::raw_svector_ostream os(sbuf);
Anna Zaksca0b57e2011-08-05 00:37:00 +0000242 unsigned int DIdx = FunctionsToTrack[AS->AllocatorIdx].DeallocatorIdx;
243 os << "Allocated data should be released before another call to "
244 << "the allocator: missing a call to '"
245 << FunctionsToTrack[DIdx].Name
246 << "'.";
247 RangedBugReport *Report = new RangedBugReport(*BT, os.str(), N);
248 Report->addRange(ArgExpr->getSourceRange());
249 C.EmitReport(Report);
250 }
251 return;
252 }
253
254 // Is it a call to one of deallocator functions?
255 idx = getTrackedFunctionIndex(funName, false);
Anna Zaks083fcb22011-08-04 17:28:06 +0000256 if (idx == InvalidIdx)
Anna Zaks08551b52011-08-04 00:31:38 +0000257 return;
258
Anna Zaks864d2522011-08-12 21:14:26 +0000259 // Check the argument to the deallocator.
Anna Zaks083fcb22011-08-04 17:28:06 +0000260 const Expr *ArgExpr = CE->getArg(FunctionsToTrack[idx].Param);
Anna Zaks864d2522011-08-12 21:14:26 +0000261 SVal ArgSVal = State->getSVal(ArgExpr);
262
263 // Undef is reported by another checker.
264 if (ArgSVal.isUndef())
265 return;
266
267 const MemRegion *Arg = ArgSVal.getAsRegion();
Anna Zaks08551b52011-08-04 00:31:38 +0000268 if (!Arg)
269 return;
Anna Zaks864d2522011-08-12 21:14:26 +0000270
271 SymbolRef ArgSM = getSymbolForRegion(C, Arg);
272 bool RegionArgIsBad = ArgSM ? false : isBadDeallocationArgument(Arg);
273 // If the argument is coming from the heap, globals, or unknown, do not
274 // report it.
275 if (!ArgSM && !RegionArgIsBad)
276 return;
Anna Zaks08551b52011-08-04 00:31:38 +0000277
Anna Zaks67f7fa42011-08-15 18:42:00 +0000278 // If trying to free data which has not been allocated yet, report as a bug.
Anna Zaks7d458b02011-08-15 23:23:15 +0000279 // TODO: We might want a more precise diagnostic for double free
280 // (that would involve tracking all the freed symbols in the checker state).
Anna Zaks5a58c6d2011-08-05 23:52:45 +0000281 const AllocationState *AS = State->get<AllocatedData>(ArgSM);
Anna Zaks864d2522011-08-12 21:14:26 +0000282 if (!AS || RegionArgIsBad) {
Anna Zaks08551b52011-08-04 00:31:38 +0000283 // It is possible that this is a false positive - the argument might
284 // have entered as an enclosing function parameter.
285 if (isEnclosingFunctionParam(ArgExpr))
Anna Zaksf57be282011-08-01 22:40:01 +0000286 return;
Anna Zaks03826aa2011-08-04 00:26:57 +0000287
Anna Zaks08551b52011-08-04 00:31:38 +0000288 ExplodedNode *N = C.generateNode(State);
289 if (!N)
290 return;
291 initBugType();
292 RangedBugReport *Report = new RangedBugReport(*BT,
293 "Trying to free data which has not been allocated.", N);
294 Report->addRange(ArgExpr->getSourceRange());
295 C.EmitReport(Report);
Anna Zaks083fcb22011-08-04 17:28:06 +0000296 return;
Anna Zaksf57be282011-08-01 22:40:01 +0000297 }
Anna Zaks08551b52011-08-04 00:31:38 +0000298
Anna Zaks7d458b02011-08-15 23:23:15 +0000299 // The call is deallocating a value we previously allocated, so remove it
300 // from the next state.
301 State = State->remove<AllocatedData>(ArgSM);
302
303 // Check if the proper deallocator is used. If not, report, but also stop
304 // tracking the allocated symbol to avoid reporting a missing free after the
305 // deallocator mismatch error.
Anna Zaks76cbb752011-08-04 21:53:01 +0000306 unsigned int PDeallocIdx = FunctionsToTrack[AS->AllocatorIdx].DeallocatorIdx;
307 if (PDeallocIdx != idx) {
Anna Zaks7d458b02011-08-15 23:23:15 +0000308 ExplodedNode *N = C.generateNode(State);
Anna Zaks76cbb752011-08-04 21:53:01 +0000309 if (!N)
310 return;
311 initBugType();
312
Anna Zaks67f7fa42011-08-15 18:42:00 +0000313 llvm::SmallString<80> sbuf;
314 llvm::raw_svector_ostream os(sbuf);
Anna Zaks76cbb752011-08-04 21:53:01 +0000315 os << "Allocator doesn't match the deallocator: '"
316 << FunctionsToTrack[PDeallocIdx].Name << "' should be used.";
317 RangedBugReport *Report = new RangedBugReport(*BT, os.str(), N);
318 Report->addRange(ArgExpr->getSourceRange());
319 C.EmitReport(Report);
320 return;
321 }
322
Anna Zaks703ffb12011-08-12 21:56:43 +0000323 // If the return status is undefined or is error, report a bad call to free.
324 if (!definitelyDidnotReturnError(AS->RetValue, State, C.getSValBuilder())) {
325 ExplodedNode *N = C.generateNode(State);
326 if (!N)
327 return;
328 initBugType();
329 RangedBugReport *Report = new RangedBugReport(*BT,
330 "Call to free data when error was returned during allocation.", N);
331 Report->addRange(ArgExpr->getSourceRange());
332 C.EmitReport(Report);
333 return;
334 }
335
Anna Zaks08551b52011-08-04 00:31:38 +0000336 C.addTransition(State);
Anna Zaksf57be282011-08-01 22:40:01 +0000337}
338
339void MacOSKeychainAPIChecker::checkPostStmt(const CallExpr *CE,
340 CheckerContext &C) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000341 const ProgramState *State = C.getState();
Anna Zaksf57be282011-08-01 22:40:01 +0000342 const Expr *Callee = CE->getCallee();
343 SVal L = State->getSVal(Callee);
Anna Zaksf57be282011-08-01 22:40:01 +0000344
345 const FunctionDecl *funDecl = L.getAsFunctionDecl();
346 if (!funDecl)
347 return;
348 IdentifierInfo *funI = funDecl->getIdentifier();
349 if (!funI)
350 return;
351 StringRef funName = funI->getName();
352
353 // If a value has been allocated, add it to the set for tracking.
Anna Zaks083fcb22011-08-04 17:28:06 +0000354 unsigned idx = getTrackedFunctionIndex(funName, true);
355 if (idx == InvalidIdx)
Anna Zaks08551b52011-08-04 00:31:38 +0000356 return;
Anna Zaks03826aa2011-08-04 00:26:57 +0000357
Anna Zaks083fcb22011-08-04 17:28:06 +0000358 const Expr *ArgExpr = CE->getArg(FunctionsToTrack[idx].Param);
Anna Zaks79c9c752011-08-12 22:47:22 +0000359 // If the argument entered as an enclosing function parameter, skip it to
360 // avoid false positives.
361 if (isEnclosingFunctionParam(ArgExpr))
362 return;
363
Anna Zaks864d2522011-08-12 21:14:26 +0000364 if (SymbolRef V = getAsPointeeSymbol(ArgExpr, C)) {
365 // If the argument points to something that's not a symbolic region, it
366 // can be:
Anna Zaks08551b52011-08-04 00:31:38 +0000367 // - unknown (cannot reason about it)
368 // - undefined (already reported by other checker)
Anna Zaks083fcb22011-08-04 17:28:06 +0000369 // - constant (null - should not be tracked,
370 // other constant will generate a compiler warning)
Anna Zaks08551b52011-08-04 00:31:38 +0000371 // - goto (should be reported by other checker)
Anna Zaks703ffb12011-08-12 21:56:43 +0000372
373 // The call return value symbol should stay alive for as long as the
374 // allocated value symbol, since our diagnostics depend on the value
375 // returned by the call. Ex: Data should only be freed if noErr was
376 // returned during allocation.)
Anna Zaks864d2522011-08-12 21:14:26 +0000377 SymbolRef RetStatusSymbol = State->getSVal(CE).getAsSymbol();
Anna Zaks703ffb12011-08-12 21:56:43 +0000378 C.getSymbolManager().addSymbolDependency(V, RetStatusSymbol);
379
380 // Track the allocated value in the checker state.
381 State = State->set<AllocatedData>(V, AllocationState(ArgExpr, idx,
Anna Zaks864d2522011-08-12 21:14:26 +0000382 RetStatusSymbol));
Anna Zaks703ffb12011-08-12 21:56:43 +0000383 assert(State);
384 C.addTransition(State);
Anna Zaksf57be282011-08-01 22:40:01 +0000385 }
386}
387
388void MacOSKeychainAPIChecker::checkPreStmt(const ReturnStmt *S,
389 CheckerContext &C) const {
390 const Expr *retExpr = S->getRetValue();
391 if (!retExpr)
392 return;
393
394 // Check if the value is escaping through the return.
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000395 const ProgramState *state = C.getState();
Anna Zaks03826aa2011-08-04 00:26:57 +0000396 const MemRegion *V = state->getSVal(retExpr).getAsRegion();
Anna Zaksf57be282011-08-01 22:40:01 +0000397 if (!V)
398 return;
Anna Zaks864d2522011-08-12 21:14:26 +0000399 state = state->remove<AllocatedData>(getSymbolForRegion(C, V));
Anna Zaksf57be282011-08-01 22:40:01 +0000400
Anna Zaks03826aa2011-08-04 00:26:57 +0000401 // Proceed from the new state.
402 C.addTransition(state);
Anna Zaksf57be282011-08-01 22:40:01 +0000403}
404
Anna Zaks703ffb12011-08-12 21:56:43 +0000405// TODO: The report has to mention the expression which contains the
406// allocated content as well as the point at which it has been allocated.
407RangedBugReport *MacOSKeychainAPIChecker::
408 generateAllocatedDataNotReleasedReport(const AllocationState &AS,
409 ExplodedNode *N) const {
410 const ADFunctionInfo &FI = FunctionsToTrack[AS.AllocatorIdx];
411 initBugType();
Anna Zaks67f7fa42011-08-15 18:42:00 +0000412 llvm::SmallString<70> sbuf;
413 llvm::raw_svector_ostream os(sbuf);
Anna Zaks703ffb12011-08-12 21:56:43 +0000414 os << "Allocated data is not released: missing a call to '"
415 << FunctionsToTrack[FI.DeallocatorIdx].Name << "'.";
416 RangedBugReport *Report = new RangedBugReport(*BT, os.str(), N);
417 Report->addRange(AS.Address->getSourceRange());
418 return Report;
419}
420
421void MacOSKeychainAPIChecker::checkDeadSymbols(SymbolReaper &SR,
422 CheckerContext &C) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000423 const ProgramState *State = C.getState();
Anna Zaks703ffb12011-08-12 21:56:43 +0000424 AllocatedSetTy ASet = State->get<AllocatedData>();
425 if (ASet.isEmpty())
426 return;
427
428 bool Changed = false;
429 llvm::SmallVector<const AllocationState*, 1> Errors;
430 for (AllocatedSetTy::iterator I = ASet.begin(), E = ASet.end(); I != E; ++I) {
431 if (SR.isLive(I->first))
432 continue;
433
434 Changed = true;
435 State = State->remove<AllocatedData>(I->first);
436 // If the allocated symbol is null or if the allocation call might have
437 // returned an error, do not report.
438 if (State->getSymVal(I->first) ||
439 definitelyReturnedError(I->second.RetValue, State, C.getSValBuilder()))
440 continue;
441 Errors.push_back(&I->second);
442 }
443 if (!Changed)
444 return;
445
446 // Generate the new, cleaned up state.
447 ExplodedNode *N = C.generateNode(State);
448 if (!N)
449 return;
450
451 // Generate the error reports.
452 for (llvm::SmallVector<const AllocationState*, 3>::iterator
453 I = Errors.begin(), E = Errors.end(); I != E; ++I) {
454 C.EmitReport(generateAllocatedDataNotReleasedReport(**I, N));
455 }
456}
457
458// TODO: Remove this after we ensure that checkDeadSymbols are always called.
Anna Zaksf57be282011-08-01 22:40:01 +0000459void MacOSKeychainAPIChecker::checkEndPath(EndOfFunctionNodeBuilder &B,
Anna Zaks03826aa2011-08-04 00:26:57 +0000460 ExprEngine &Eng) const {
Ted Kremenek18c66fd2011-08-15 22:09:50 +0000461 const ProgramState *state = B.getState();
Anna Zaksf57be282011-08-01 22:40:01 +0000462 AllocatedSetTy AS = state->get<AllocatedData>();
Anna Zaks703ffb12011-08-12 21:56:43 +0000463 if (AS.isEmpty())
Anna Zaks03826aa2011-08-04 00:26:57 +0000464 return;
Anna Zaksf57be282011-08-01 22:40:01 +0000465
466 // Anything which has been allocated but not freed (nor escaped) will be
467 // found here, so report it.
Anna Zaks703ffb12011-08-12 21:56:43 +0000468 bool Changed = false;
469 llvm::SmallVector<const AllocationState*, 1> Errors;
Anna Zaks03826aa2011-08-04 00:26:57 +0000470 for (AllocatedSetTy::iterator I = AS.begin(), E = AS.end(); I != E; ++I ) {
Anna Zaks703ffb12011-08-12 21:56:43 +0000471 Changed = true;
472 state = state->remove<AllocatedData>(I->first);
473 // If the allocated symbol is null or if error code was returned at
474 // allocation, do not report.
475 if (state->getSymVal(I.getKey()) ||
476 definitelyReturnedError(I->second.RetValue, state,
477 Eng.getSValBuilder())) {
478 continue;
479 }
480 Errors.push_back(&I->second);
Anna Zaksf57be282011-08-01 22:40:01 +0000481 }
Anna Zaks703ffb12011-08-12 21:56:43 +0000482
483 // If no change, do not generate a new state.
484 if (!Changed)
485 return;
486
487 ExplodedNode *N = B.generateNode(state);
488 if (!N)
489 return;
490
491 // Generate the error reports.
492 for (llvm::SmallVector<const AllocationState*, 3>::iterator
493 I = Errors.begin(), E = Errors.end(); I != E; ++I) {
494 Eng.getBugReporter().EmitReport(
495 generateAllocatedDataNotReleasedReport(**I, N));
496 }
497
Anna Zaksf57be282011-08-01 22:40:01 +0000498}
499
500void ento::registerMacOSKeychainAPIChecker(CheckerManager &mgr) {
501 mgr.registerChecker<MacOSKeychainAPIChecker>();
502}