blob: 73de9f5b50927a1d8aa4212a9ecb72c9ccb33ac8 [file] [log] [blame]
George Karpenkov70c2ee32018-08-17 21:41:07 +00001//==-- RetainCountChecker.cpp - Checks for leaks and other issues -*- C++ -*--//
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//
10// This file defines the methods for RetainCountChecker, which implements
11// a reference count checker for Core Foundation and Cocoa on (Mac OS X).
12//
13//===----------------------------------------------------------------------===//
14
15#include "RetainCountChecker.h"
16
17using namespace clang;
18using namespace ento;
George Karpenkov70c2ee32018-08-17 21:41:07 +000019using namespace retaincountchecker;
20using llvm::StrInStrNoCase;
21
22REGISTER_MAP_WITH_PROGRAMSTATE(RefBindings, SymbolRef, RefVal)
23
24namespace clang {
25namespace ento {
26namespace retaincountchecker {
27
28const RefVal *getRefBinding(ProgramStateRef State, SymbolRef Sym) {
29 return State->get<RefBindings>(Sym);
30}
31
32ProgramStateRef setRefBinding(ProgramStateRef State, SymbolRef Sym,
33 RefVal Val) {
George Karpenkovd5ef0d22018-08-29 20:28:33 +000034 assert(Sym != nullptr);
George Karpenkov70c2ee32018-08-17 21:41:07 +000035 return State->set<RefBindings>(Sym, Val);
36}
37
38ProgramStateRef removeRefBinding(ProgramStateRef State, SymbolRef Sym) {
39 return State->remove<RefBindings>(Sym);
40}
41
George Karpenkov62db8862018-11-30 02:18:23 +000042class UseAfterRelease : public CFRefBug {
43public:
44 UseAfterRelease(const CheckerBase *checker)
45 : CFRefBug(checker, "Use-after-release") {}
46
47 const char *getDescription() const override {
48 return "Reference-counted object is used after it is released";
49 }
50};
51
52class BadRelease : public CFRefBug {
53public:
54 BadRelease(const CheckerBase *checker) : CFRefBug(checker, "Bad release") {}
55
56 const char *getDescription() const override {
57 return "Incorrect decrement of the reference count of an object that is "
58 "not owned at this point by the caller";
59 }
60};
61
62class DeallocNotOwned : public CFRefBug {
63public:
64 DeallocNotOwned(const CheckerBase *checker)
65 : CFRefBug(checker, "-dealloc sent to non-exclusively owned object") {}
66
67 const char *getDescription() const override {
68 return "-dealloc sent to object that may be referenced elsewhere";
69 }
70};
71
72class OverAutorelease : public CFRefBug {
73public:
74 OverAutorelease(const CheckerBase *checker)
75 : CFRefBug(checker, "Object autoreleased too many times") {}
76
77 const char *getDescription() const override {
78 return "Object autoreleased too many times";
79 }
80};
81
82class ReturnedNotOwnedForOwned : public CFRefBug {
83public:
84 ReturnedNotOwnedForOwned(const CheckerBase *checker)
85 : CFRefBug(checker, "Method should return an owned object") {}
86
87 const char *getDescription() const override {
88 return "Object with a +0 retain count returned to caller where a +1 "
89 "(owning) retain count is expected";
90 }
91};
92
93class Leak : public CFRefBug {
94public:
95 Leak(const CheckerBase *checker, StringRef name) : CFRefBug(checker, name) {
96 // Leaks should not be reported if they are post-dominated by a sink.
97 setSuppressOnSink(true);
98 }
99
100 const char *getDescription() const override { return ""; }
101
102 bool isLeak() const override { return true; }
103};
104
George Karpenkov70c2ee32018-08-17 21:41:07 +0000105} // end namespace retaincountchecker
106} // end namespace ento
107} // end namespace clang
108
109void RefVal::print(raw_ostream &Out) const {
110 if (!T.isNull())
George Karpenkov081c4772018-10-23 23:11:50 +0000111 Out << "Tracked " << T.getAsString() << " | ";
George Karpenkov70c2ee32018-08-17 21:41:07 +0000112
113 switch (getKind()) {
114 default: llvm_unreachable("Invalid RefVal kind");
115 case Owned: {
116 Out << "Owned";
117 unsigned cnt = getCount();
118 if (cnt) Out << " (+ " << cnt << ")";
119 break;
120 }
121
122 case NotOwned: {
123 Out << "NotOwned";
124 unsigned cnt = getCount();
125 if (cnt) Out << " (+ " << cnt << ")";
126 break;
127 }
128
129 case ReturnedOwned: {
130 Out << "ReturnedOwned";
131 unsigned cnt = getCount();
132 if (cnt) Out << " (+ " << cnt << ")";
133 break;
134 }
135
136 case ReturnedNotOwned: {
137 Out << "ReturnedNotOwned";
138 unsigned cnt = getCount();
139 if (cnt) Out << " (+ " << cnt << ")";
140 break;
141 }
142
143 case Released:
144 Out << "Released";
145 break;
146
147 case ErrorDeallocNotOwned:
148 Out << "-dealloc (not-owned)";
149 break;
150
151 case ErrorLeak:
152 Out << "Leaked";
153 break;
154
155 case ErrorLeakReturned:
156 Out << "Leaked (Bad naming)";
157 break;
158
159 case ErrorUseAfterRelease:
160 Out << "Use-After-Release [ERROR]";
161 break;
162
163 case ErrorReleaseNotOwned:
164 Out << "Release of Not-Owned [ERROR]";
165 break;
166
167 case RefVal::ErrorOverAutorelease:
168 Out << "Over-autoreleased";
169 break;
170
171 case RefVal::ErrorReturnedNotOwned:
172 Out << "Non-owned object returned instead of owned";
173 break;
174 }
175
176 switch (getIvarAccessHistory()) {
177 case IvarAccessHistory::None:
178 break;
179 case IvarAccessHistory::AccessedDirectly:
180 Out << " [direct ivar access]";
181 break;
182 case IvarAccessHistory::ReleasedAfterDirectAccess:
183 Out << " [released after direct ivar access]";
184 }
185
186 if (ACnt) {
187 Out << " [autorelease -" << ACnt << ']';
188 }
189}
190
191namespace {
192class StopTrackingCallback final : public SymbolVisitor {
193 ProgramStateRef state;
194public:
195 StopTrackingCallback(ProgramStateRef st) : state(std::move(st)) {}
196 ProgramStateRef getState() const { return state; }
197
198 bool VisitSymbol(SymbolRef sym) override {
199 state = state->remove<RefBindings>(sym);
200 return true;
201 }
202};
203} // end anonymous namespace
204
205//===----------------------------------------------------------------------===//
206// Handle statements that may have an effect on refcounts.
207//===----------------------------------------------------------------------===//
208
209void RetainCountChecker::checkPostStmt(const BlockExpr *BE,
210 CheckerContext &C) const {
211
212 // Scan the BlockDecRefExprs for any object the retain count checker
213 // may be tracking.
214 if (!BE->getBlockDecl()->hasCaptures())
215 return;
216
217 ProgramStateRef state = C.getState();
218 auto *R = cast<BlockDataRegion>(C.getSVal(BE).getAsRegion());
219
220 BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
221 E = R->referenced_vars_end();
222
223 if (I == E)
224 return;
225
226 // FIXME: For now we invalidate the tracking of all symbols passed to blocks
227 // via captured variables, even though captured variables result in a copy
228 // and in implicit increment/decrement of a retain count.
229 SmallVector<const MemRegion*, 10> Regions;
230 const LocationContext *LC = C.getLocationContext();
231 MemRegionManager &MemMgr = C.getSValBuilder().getRegionManager();
232
233 for ( ; I != E; ++I) {
234 const VarRegion *VR = I.getCapturedRegion();
235 if (VR->getSuperRegion() == R) {
236 VR = MemMgr.getVarRegion(VR->getDecl(), LC);
237 }
238 Regions.push_back(VR);
239 }
240
George Karpenkovd3e76752018-10-23 23:12:12 +0000241 state = state->scanReachableSymbols<StopTrackingCallback>(Regions).getState();
George Karpenkov70c2ee32018-08-17 21:41:07 +0000242 C.addTransition(state);
243}
244
245void RetainCountChecker::checkPostStmt(const CastExpr *CE,
246 CheckerContext &C) const {
247 const ObjCBridgedCastExpr *BE = dyn_cast<ObjCBridgedCastExpr>(CE);
248 if (!BE)
249 return;
250
George Karpenkov9cbcc212019-01-10 18:14:12 +0000251 ArgEffect AE = ArgEffect(IncRef, ObjKind::ObjC);
George Karpenkov70c2ee32018-08-17 21:41:07 +0000252
253 switch (BE->getBridgeKind()) {
254 case OBC_Bridge:
255 // Do nothing.
256 return;
257 case OBC_BridgeRetained:
George Karpenkov9cbcc212019-01-10 18:14:12 +0000258 AE = AE.withKind(IncRef);
George Karpenkov70c2ee32018-08-17 21:41:07 +0000259 break;
260 case OBC_BridgeTransfer:
George Karpenkov9cbcc212019-01-10 18:14:12 +0000261 AE = AE.withKind(DecRefBridgedTransferred);
George Karpenkov70c2ee32018-08-17 21:41:07 +0000262 break;
263 }
264
265 ProgramStateRef state = C.getState();
266 SymbolRef Sym = C.getSVal(CE).getAsLocSymbol();
267 if (!Sym)
268 return;
269 const RefVal* T = getRefBinding(state, Sym);
270 if (!T)
271 return;
272
273 RefVal::Kind hasErr = (RefVal::Kind) 0;
274 state = updateSymbol(state, Sym, *T, AE, hasErr, C);
275
276 if (hasErr) {
277 // FIXME: If we get an error during a bridge cast, should we report it?
278 return;
279 }
280
281 C.addTransition(state);
282}
283
284void RetainCountChecker::processObjCLiterals(CheckerContext &C,
285 const Expr *Ex) const {
286 ProgramStateRef state = C.getState();
287 const ExplodedNode *pred = C.getPredecessor();
288 for (const Stmt *Child : Ex->children()) {
289 SVal V = pred->getSVal(Child);
290 if (SymbolRef sym = V.getAsSymbol())
291 if (const RefVal* T = getRefBinding(state, sym)) {
292 RefVal::Kind hasErr = (RefVal::Kind) 0;
George Karpenkov9cbcc212019-01-10 18:14:12 +0000293 state = updateSymbol(state, sym, *T,
294 ArgEffect(MayEscape, ObjKind::ObjC), hasErr, C);
George Karpenkov70c2ee32018-08-17 21:41:07 +0000295 if (hasErr) {
296 processNonLeakError(state, Child->getSourceRange(), hasErr, sym, C);
297 return;
298 }
299 }
300 }
301
302 // Return the object as autoreleased.
George Karpenkov7e3016d2019-01-10 18:13:46 +0000303 // RetEffect RE = RetEffect::MakeNotOwned(ObjKind::ObjC);
George Karpenkov70c2ee32018-08-17 21:41:07 +0000304 if (SymbolRef sym =
305 state->getSVal(Ex, pred->getLocationContext()).getAsSymbol()) {
306 QualType ResultTy = Ex->getType();
307 state = setRefBinding(state, sym,
George Karpenkov7e3016d2019-01-10 18:13:46 +0000308 RefVal::makeNotOwned(ObjKind::ObjC, ResultTy));
George Karpenkov70c2ee32018-08-17 21:41:07 +0000309 }
310
311 C.addTransition(state);
312}
313
314void RetainCountChecker::checkPostStmt(const ObjCArrayLiteral *AL,
315 CheckerContext &C) const {
316 // Apply the 'MayEscape' to all values.
317 processObjCLiterals(C, AL);
318}
319
320void RetainCountChecker::checkPostStmt(const ObjCDictionaryLiteral *DL,
321 CheckerContext &C) const {
322 // Apply the 'MayEscape' to all keys and values.
323 processObjCLiterals(C, DL);
324}
325
326void RetainCountChecker::checkPostStmt(const ObjCBoxedExpr *Ex,
327 CheckerContext &C) const {
328 const ExplodedNode *Pred = C.getPredecessor();
329 ProgramStateRef State = Pred->getState();
330
331 if (SymbolRef Sym = Pred->getSVal(Ex).getAsSymbol()) {
332 QualType ResultTy = Ex->getType();
333 State = setRefBinding(State, Sym,
George Karpenkov7e3016d2019-01-10 18:13:46 +0000334 RefVal::makeNotOwned(ObjKind::ObjC, ResultTy));
George Karpenkov70c2ee32018-08-17 21:41:07 +0000335 }
336
337 C.addTransition(State);
338}
339
340void RetainCountChecker::checkPostStmt(const ObjCIvarRefExpr *IRE,
341 CheckerContext &C) const {
342 Optional<Loc> IVarLoc = C.getSVal(IRE).getAs<Loc>();
343 if (!IVarLoc)
344 return;
345
346 ProgramStateRef State = C.getState();
347 SymbolRef Sym = State->getSVal(*IVarLoc).getAsSymbol();
348 if (!Sym || !dyn_cast_or_null<ObjCIvarRegion>(Sym->getOriginRegion()))
349 return;
350
351 // Accessing an ivar directly is unusual. If we've done that, be more
352 // forgiving about what the surrounding code is allowed to do.
353
354 QualType Ty = Sym->getType();
George Karpenkov7e3016d2019-01-10 18:13:46 +0000355 ObjKind Kind;
George Karpenkov70c2ee32018-08-17 21:41:07 +0000356 if (Ty->isObjCRetainableType())
George Karpenkov7e3016d2019-01-10 18:13:46 +0000357 Kind = ObjKind::ObjC;
George Karpenkov70c2ee32018-08-17 21:41:07 +0000358 else if (coreFoundation::isCFObjectRef(Ty))
George Karpenkov7e3016d2019-01-10 18:13:46 +0000359 Kind = ObjKind::CF;
George Karpenkov70c2ee32018-08-17 21:41:07 +0000360 else
361 return;
362
363 // If the value is already known to be nil, don't bother tracking it.
364 ConstraintManager &CMgr = State->getConstraintManager();
365 if (CMgr.isNull(State, Sym).isConstrainedTrue())
366 return;
367
368 if (const RefVal *RV = getRefBinding(State, Sym)) {
369 // If we've seen this symbol before, or we're only seeing it now because
370 // of something the analyzer has synthesized, don't do anything.
371 if (RV->getIvarAccessHistory() != RefVal::IvarAccessHistory::None ||
372 isSynthesizedAccessor(C.getStackFrame())) {
373 return;
374 }
375
376 // Note that this value has been loaded from an ivar.
377 C.addTransition(setRefBinding(State, Sym, RV->withIvarAccess()));
378 return;
379 }
380
381 RefVal PlusZero = RefVal::makeNotOwned(Kind, Ty);
382
383 // In a synthesized accessor, the effective retain count is +0.
384 if (isSynthesizedAccessor(C.getStackFrame())) {
385 C.addTransition(setRefBinding(State, Sym, PlusZero));
386 return;
387 }
388
389 State = setRefBinding(State, Sym, PlusZero.withIvarAccess());
390 C.addTransition(State);
391}
392
393void RetainCountChecker::checkPostCall(const CallEvent &Call,
394 CheckerContext &C) const {
395 RetainSummaryManager &Summaries = getSummaryManager(C);
George Karpenkovefef49c2018-08-21 03:09:02 +0000396
397 // Leave null if no receiver.
398 QualType ReceiverType;
399 if (const auto *MC = dyn_cast<ObjCMethodCall>(&Call)) {
400 if (MC->isInstanceMessage()) {
401 SVal ReceiverV = MC->getReceiverSVal();
402 if (SymbolRef Sym = ReceiverV.getAsLocSymbol())
403 if (const RefVal *T = getRefBinding(C.getState(), Sym))
404 ReceiverType = T->getType();
405 }
406 }
407
408 const RetainSummary *Summ = Summaries.getSummary(Call, ReceiverType);
George Karpenkov70c2ee32018-08-17 21:41:07 +0000409
410 if (C.wasInlined) {
411 processSummaryOfInlined(*Summ, Call, C);
412 return;
413 }
414 checkSummary(*Summ, Call, C);
415}
416
George Karpenkov62db8862018-11-30 02:18:23 +0000417void RetainCountChecker::checkEndAnalysis(ExplodedGraph &G, BugReporter &BR,
418 ExprEngine &Eng) const {
419 // FIXME: This is a hack to make sure the summary log gets cleared between
420 // analyses of different code bodies.
421 //
422 // Why is this necessary? Because a checker's lifetime is tied to a
423 // translation unit, but an ExplodedGraph's lifetime is just a code body.
424 // Once in a blue moon, a new ExplodedNode will have the same address as an
425 // old one with an associated summary, and the bug report visitor gets very
426 // confused. (To make things worse, the summary lifetime is currently also
427 // tied to a code body, so we get a crash instead of incorrect results.)
428 //
429 // Why is this a bad solution? Because if the lifetime of the ExplodedGraph
430 // changes, things will start going wrong again. Really the lifetime of this
431 // log needs to be tied to either the specific nodes in it or the entire
432 // ExplodedGraph, not to a specific part of the code being analyzed.
433 //
434 // (Also, having stateful local data means that the same checker can't be
435 // used from multiple threads, but a lot of checkers have incorrect
436 // assumptions about that anyway. So that wasn't a priority at the time of
437 // this fix.)
438 //
439 // This happens at the end of analysis, but bug reports are emitted /after/
440 // this point. So we can't just clear the summary log now. Instead, we mark
441 // that the next time we access the summary log, it should be cleared.
442
443 // If we never reset the summary log during /this/ code body analysis,
444 // there were no new summaries. There might still have been summaries from
445 // the /last/ analysis, so clear them out to make sure the bug report
446 // visitors don't get confused.
447 if (ShouldResetSummaryLog)
448 SummaryLog.clear();
449
450 ShouldResetSummaryLog = !SummaryLog.empty();
451}
452
453CFRefBug *
454RetainCountChecker::getLeakWithinFunctionBug(const LangOptions &LOpts) const {
455 if (!leakWithinFunction)
456 leakWithinFunction.reset(new Leak(this, "Leak"));
457 return leakWithinFunction.get();
458}
459
460CFRefBug *
461RetainCountChecker::getLeakAtReturnBug(const LangOptions &LOpts) const {
462 if (!leakAtReturn)
463 leakAtReturn.reset(new Leak(this, "Leak of returned object"));
464 return leakAtReturn.get();
465}
466
George Karpenkov70c2ee32018-08-17 21:41:07 +0000467/// GetReturnType - Used to get the return type of a message expression or
468/// function call with the intention of affixing that type to a tracked symbol.
469/// While the return type can be queried directly from RetEx, when
470/// invoking class methods we augment to the return type to be that of
471/// a pointer to the class (as opposed it just being id).
472// FIXME: We may be able to do this with related result types instead.
473// This function is probably overestimating.
474static QualType GetReturnType(const Expr *RetE, ASTContext &Ctx) {
475 QualType RetTy = RetE->getType();
476 // If RetE is not a message expression just return its type.
477 // If RetE is a message expression, return its types if it is something
478 /// more specific than id.
479 if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(RetE))
480 if (const ObjCObjectPointerType *PT = RetTy->getAs<ObjCObjectPointerType>())
481 if (PT->isObjCQualifiedIdType() || PT->isObjCIdType() ||
482 PT->isObjCClassType()) {
483 // At this point we know the return type of the message expression is
484 // id, id<...>, or Class. If we have an ObjCInterfaceDecl, we know this
485 // is a call to a class method whose type we can resolve. In such
486 // cases, promote the return type to XXX* (where XXX is the class).
487 const ObjCInterfaceDecl *D = ME->getReceiverInterface();
488 return !D ? RetTy :
489 Ctx.getObjCObjectPointerType(Ctx.getObjCInterfaceType(D));
490 }
491
492 return RetTy;
493}
494
George Karpenkov80c9e782018-08-22 01:16:49 +0000495static Optional<RefVal> refValFromRetEffect(RetEffect RE,
496 QualType ResultTy) {
497 if (RE.isOwned()) {
498 return RefVal::makeOwned(RE.getObjKind(), ResultTy);
499 } else if (RE.notOwned()) {
500 return RefVal::makeNotOwned(RE.getObjKind(), ResultTy);
501 }
502
503 return None;
504}
505
George Karpenkov255b0582018-12-21 19:13:40 +0000506static bool isPointerToObject(QualType QT) {
507 QualType PT = QT->getPointeeType();
508 if (!PT.isNull())
509 if (PT->getAsCXXRecordDecl())
510 return true;
511 return false;
512}
513
514/// Whether the tracked value should be escaped on a given call.
515/// OSObjects are escaped when passed to void * / etc.
George Karpenkov9cbcc212019-01-10 18:14:12 +0000516static bool shouldEscapeOSArgumentOnCall(const CallEvent &CE, unsigned ArgIdx,
George Karpenkov255b0582018-12-21 19:13:40 +0000517 const RefVal *TrackedValue) {
George Karpenkov7e3016d2019-01-10 18:13:46 +0000518 if (TrackedValue->getObjKind() != ObjKind::OS)
George Karpenkov255b0582018-12-21 19:13:40 +0000519 return false;
520 if (ArgIdx >= CE.parameters().size())
521 return false;
522 return !isPointerToObject(CE.parameters()[ArgIdx]->getType());
523}
524
George Karpenkov70c2ee32018-08-17 21:41:07 +0000525// We don't always get the exact modeling of the function with regards to the
526// retain count checker even when the function is inlined. For example, we need
527// to stop tracking the symbols which were marked with StopTrackingHard.
528void RetainCountChecker::processSummaryOfInlined(const RetainSummary &Summ,
529 const CallEvent &CallOrMsg,
530 CheckerContext &C) const {
531 ProgramStateRef state = C.getState();
532
533 // Evaluate the effect of the arguments.
534 for (unsigned idx = 0, e = CallOrMsg.getNumArgs(); idx != e; ++idx) {
George Karpenkov255b0582018-12-21 19:13:40 +0000535 SVal V = CallOrMsg.getArgSVal(idx);
536
537 if (SymbolRef Sym = V.getAsLocSymbol()) {
George Karpenkov585a2102019-01-10 18:13:59 +0000538 bool ShouldRemoveBinding = Summ.getArg(idx).getKind() == StopTrackingHard;
George Karpenkov255b0582018-12-21 19:13:40 +0000539 if (const RefVal *T = getRefBinding(state, Sym))
George Karpenkov9cbcc212019-01-10 18:14:12 +0000540 if (shouldEscapeOSArgumentOnCall(CallOrMsg, idx, T))
George Karpenkov255b0582018-12-21 19:13:40 +0000541 ShouldRemoveBinding = true;
542
543 if (ShouldRemoveBinding)
George Karpenkov70c2ee32018-08-17 21:41:07 +0000544 state = removeRefBinding(state, Sym);
George Karpenkov70c2ee32018-08-17 21:41:07 +0000545 }
546 }
547
548 // Evaluate the effect on the message receiver.
George Karpenkov6e9fd132018-08-22 01:17:09 +0000549 if (const auto *MsgInvocation = dyn_cast<ObjCMethodCall>(&CallOrMsg)) {
George Karpenkov70c2ee32018-08-17 21:41:07 +0000550 if (SymbolRef Sym = MsgInvocation->getReceiverSVal().getAsLocSymbol()) {
George Karpenkov585a2102019-01-10 18:13:59 +0000551 if (Summ.getReceiverEffect().getKind() == StopTrackingHard) {
George Karpenkov70c2ee32018-08-17 21:41:07 +0000552 state = removeRefBinding(state, Sym);
553 }
554 }
555 }
556
557 // Consult the summary for the return value.
558 RetEffect RE = Summ.getRetEffect();
George Karpenkov70c2ee32018-08-17 21:41:07 +0000559
George Karpenkovd5ef0d22018-08-29 20:28:33 +0000560 if (SymbolRef Sym = CallOrMsg.getReturnValue().getAsSymbol()) {
George Karpenkovd5ef0d22018-08-29 20:28:33 +0000561 if (RE.getKind() == RetEffect::NoRetHard)
562 state = removeRefBinding(state, Sym);
563 }
George Karpenkovab0011e2018-08-23 00:26:59 +0000564
George Karpenkov70c2ee32018-08-17 21:41:07 +0000565 C.addTransition(state);
566}
567
568static ProgramStateRef updateOutParameter(ProgramStateRef State,
569 SVal ArgVal,
George Karpenkov585a2102019-01-10 18:13:59 +0000570 ArgEffectKind Effect) {
George Karpenkov70c2ee32018-08-17 21:41:07 +0000571 auto *ArgRegion = dyn_cast_or_null<TypedValueRegion>(ArgVal.getAsRegion());
572 if (!ArgRegion)
573 return State;
574
575 QualType PointeeTy = ArgRegion->getValueType();
576 if (!coreFoundation::isCFObjectRef(PointeeTy))
577 return State;
578
579 SVal PointeeVal = State->getSVal(ArgRegion);
580 SymbolRef Pointee = PointeeVal.getAsLocSymbol();
581 if (!Pointee)
582 return State;
583
584 switch (Effect) {
585 case UnretainedOutParameter:
586 State = setRefBinding(State, Pointee,
George Karpenkov7e3016d2019-01-10 18:13:46 +0000587 RefVal::makeNotOwned(ObjKind::CF, PointeeTy));
George Karpenkov70c2ee32018-08-17 21:41:07 +0000588 break;
589 case RetainedOutParameter:
590 // Do nothing. Retained out parameters will either point to a +1 reference
591 // or NULL, but the way you check for failure differs depending on the API.
592 // Consequently, we don't have a good way to track them yet.
593 break;
594
595 default:
596 llvm_unreachable("only for out parameters");
597 }
598
599 return State;
600}
601
602void RetainCountChecker::checkSummary(const RetainSummary &Summ,
603 const CallEvent &CallOrMsg,
604 CheckerContext &C) const {
605 ProgramStateRef state = C.getState();
606
607 // Evaluate the effect of the arguments.
608 RefVal::Kind hasErr = (RefVal::Kind) 0;
609 SourceRange ErrorRange;
610 SymbolRef ErrorSym = nullptr;
611
612 for (unsigned idx = 0, e = CallOrMsg.getNumArgs(); idx != e; ++idx) {
613 SVal V = CallOrMsg.getArgSVal(idx);
614
George Karpenkov9cbcc212019-01-10 18:14:12 +0000615 ArgEffect Effect = Summ.getArg(idx);
616 if (Effect.getKind() == RetainedOutParameter ||
617 Effect.getKind() == UnretainedOutParameter) {
618 state = updateOutParameter(state, V, Effect.getKind());
George Karpenkov70c2ee32018-08-17 21:41:07 +0000619 } else if (SymbolRef Sym = V.getAsLocSymbol()) {
620 if (const RefVal *T = getRefBinding(state, Sym)) {
George Karpenkov041c9fa2018-12-08 01:18:40 +0000621
George Karpenkov9cbcc212019-01-10 18:14:12 +0000622 if (shouldEscapeOSArgumentOnCall(CallOrMsg, idx, T))
623 Effect = ArgEffect(StopTrackingHard, ObjKind::OS);
George Karpenkov041c9fa2018-12-08 01:18:40 +0000624
George Karpenkov70c2ee32018-08-17 21:41:07 +0000625 state = updateSymbol(state, Sym, *T, Effect, hasErr, C);
626 if (hasErr) {
627 ErrorRange = CallOrMsg.getArgSourceRange(idx);
628 ErrorSym = Sym;
629 break;
630 }
631 }
632 }
633 }
634
George Karpenkovab0011e2018-08-23 00:26:59 +0000635 // Evaluate the effect on the message receiver / `this` argument.
George Karpenkov70c2ee32018-08-17 21:41:07 +0000636 bool ReceiverIsTracked = false;
637 if (!hasErr) {
George Karpenkovab0011e2018-08-23 00:26:59 +0000638 if (const auto *MsgInvocation = dyn_cast<ObjCMethodCall>(&CallOrMsg)) {
George Karpenkov70c2ee32018-08-17 21:41:07 +0000639 if (SymbolRef Sym = MsgInvocation->getReceiverSVal().getAsLocSymbol()) {
640 if (const RefVal *T = getRefBinding(state, Sym)) {
641 ReceiverIsTracked = true;
George Karpenkov585a2102019-01-10 18:13:59 +0000642 state = updateSymbol(state, Sym, *T,
George Karpenkov9cbcc212019-01-10 18:14:12 +0000643 Summ.getReceiverEffect(), hasErr, C);
George Karpenkov70c2ee32018-08-17 21:41:07 +0000644 if (hasErr) {
645 ErrorRange = MsgInvocation->getOriginExpr()->getReceiverRange();
646 ErrorSym = Sym;
647 }
648 }
649 }
George Karpenkovab0011e2018-08-23 00:26:59 +0000650 } else if (const auto *MCall = dyn_cast<CXXMemberCall>(&CallOrMsg)) {
651 if (SymbolRef Sym = MCall->getCXXThisVal().getAsLocSymbol()) {
652 if (const RefVal *T = getRefBinding(state, Sym)) {
George Karpenkov9cbcc212019-01-10 18:14:12 +0000653 state = updateSymbol(state, Sym, *T, Summ.getThisEffect(),
George Karpenkovab0011e2018-08-23 00:26:59 +0000654 hasErr, C);
655 if (hasErr) {
656 ErrorRange = MCall->getOriginExpr()->getSourceRange();
657 ErrorSym = Sym;
658 }
659 }
660 }
George Karpenkov70c2ee32018-08-17 21:41:07 +0000661 }
662 }
663
664 // Process any errors.
665 if (hasErr) {
666 processNonLeakError(state, ErrorRange, hasErr, ErrorSym, C);
667 return;
668 }
669
670 // Consult the summary for the return value.
671 RetEffect RE = Summ.getRetEffect();
672
673 if (RE.getKind() == RetEffect::OwnedWhenTrackedReceiver) {
674 if (ReceiverIsTracked)
675 RE = getSummaryManager(C).getObjAllocRetEffect();
676 else
677 RE = RetEffect::MakeNoRet();
678 }
679
George Karpenkov80c9e782018-08-22 01:16:49 +0000680 if (SymbolRef Sym = CallOrMsg.getReturnValue().getAsSymbol()) {
681 QualType ResultTy = CallOrMsg.getResultType();
682 if (RE.notOwned()) {
George Karpenkov70c2ee32018-08-17 21:41:07 +0000683 const Expr *Ex = CallOrMsg.getOriginExpr();
George Karpenkov70c2ee32018-08-17 21:41:07 +0000684 assert(Ex);
George Karpenkov80c9e782018-08-22 01:16:49 +0000685 ResultTy = GetReturnType(Ex, C.getASTContext());
George Karpenkov70c2ee32018-08-17 21:41:07 +0000686 }
George Karpenkov80c9e782018-08-22 01:16:49 +0000687 if (Optional<RefVal> updatedRefVal = refValFromRetEffect(RE, ResultTy))
688 state = setRefBinding(state, Sym, *updatedRefVal);
George Karpenkov70c2ee32018-08-17 21:41:07 +0000689 }
690
691 // This check is actually necessary; otherwise the statement builder thinks
692 // we've hit a previously-found path.
693 // Normally addTransition takes care of this, but we want the node pointer.
694 ExplodedNode *NewNode;
695 if (state == C.getState()) {
696 NewNode = C.getPredecessor();
697 } else {
698 NewNode = C.addTransition(state);
699 }
700
701 // Annotate the node with summary we used.
702 if (NewNode) {
703 // FIXME: This is ugly. See checkEndAnalysis for why it's necessary.
704 if (ShouldResetSummaryLog) {
705 SummaryLog.clear();
706 ShouldResetSummaryLog = false;
707 }
708 SummaryLog[NewNode] = &Summ;
709 }
710}
711
George Karpenkov585a2102019-01-10 18:13:59 +0000712ProgramStateRef RetainCountChecker::updateSymbol(ProgramStateRef state,
713 SymbolRef sym, RefVal V,
George Karpenkov9cbcc212019-01-10 18:14:12 +0000714 ArgEffect AE,
George Karpenkov585a2102019-01-10 18:13:59 +0000715 RefVal::Kind &hasErr,
716 CheckerContext &C) const {
George Karpenkov70c2ee32018-08-17 21:41:07 +0000717 bool IgnoreRetainMsg = (bool)C.getASTContext().getLangOpts().ObjCAutoRefCount;
George Karpenkov9cbcc212019-01-10 18:14:12 +0000718 if (AE.getObjKind() == ObjKind::ObjC && IgnoreRetainMsg) {
719 switch (AE.getKind()) {
720 default:
721 break;
722 case IncRef:
723 AE = AE.withKind(DoNothing);
724 break;
725 case DecRef:
726 AE = AE.withKind(DoNothing);
727 break;
728 case DecRefAndStopTrackingHard:
729 AE = AE.withKind(StopTracking);
730 break;
731 }
George Karpenkov70c2ee32018-08-17 21:41:07 +0000732 }
George Karpenkov9cbcc212019-01-10 18:14:12 +0000733 if (AE.getKind() == MakeCollectable)
734 AE = AE.withKind(DoNothing);
George Karpenkov70c2ee32018-08-17 21:41:07 +0000735
736 // Handle all use-after-releases.
737 if (V.getKind() == RefVal::Released) {
738 V = V ^ RefVal::ErrorUseAfterRelease;
739 hasErr = V.getKind();
740 return setRefBinding(state, sym, V);
741 }
742
George Karpenkov9cbcc212019-01-10 18:14:12 +0000743 switch (AE.getKind()) {
George Karpenkovbc0cddf2018-08-17 21:42:59 +0000744 case MakeCollectable:
George Karpenkov9cbcc212019-01-10 18:14:12 +0000745 llvm_unreachable("MakeCollectable already converted");
George Karpenkov70c2ee32018-08-17 21:41:07 +0000746
747 case UnretainedOutParameter:
748 case RetainedOutParameter:
749 llvm_unreachable("Applies to pointer-to-pointer parameters, which should "
750 "not have ref state.");
751
752 case Dealloc:
753 switch (V.getKind()) {
754 default:
755 llvm_unreachable("Invalid RefVal state for an explicit dealloc.");
756 case RefVal::Owned:
757 // The object immediately transitions to the released state.
758 V = V ^ RefVal::Released;
759 V.clearCounts();
760 return setRefBinding(state, sym, V);
761 case RefVal::NotOwned:
762 V = V ^ RefVal::ErrorDeallocNotOwned;
763 hasErr = V.getKind();
764 break;
765 }
766 break;
767
768 case MayEscape:
769 if (V.getKind() == RefVal::Owned) {
770 V = V ^ RefVal::NotOwned;
771 break;
772 }
773
Reid Kleckner4dc0b1a2018-11-01 19:54:45 +0000774 LLVM_FALLTHROUGH;
George Karpenkov70c2ee32018-08-17 21:41:07 +0000775
776 case DoNothing:
777 return state;
778
779 case Autorelease:
780 // Update the autorelease counts.
781 V = V.autorelease();
782 break;
783
784 case StopTracking:
785 case StopTrackingHard:
786 return removeRefBinding(state, sym);
787
788 case IncRef:
789 switch (V.getKind()) {
790 default:
791 llvm_unreachable("Invalid RefVal state for a retain.");
792 case RefVal::Owned:
793 case RefVal::NotOwned:
794 V = V + 1;
795 break;
796 }
797 break;
798
799 case DecRef:
800 case DecRefBridgedTransferred:
801 case DecRefAndStopTrackingHard:
802 switch (V.getKind()) {
803 default:
804 // case 'RefVal::Released' handled above.
805 llvm_unreachable("Invalid RefVal state for a release.");
806
807 case RefVal::Owned:
808 assert(V.getCount() > 0);
809 if (V.getCount() == 1) {
George Karpenkov9cbcc212019-01-10 18:14:12 +0000810 if (AE.getKind() == DecRefBridgedTransferred ||
George Karpenkov70c2ee32018-08-17 21:41:07 +0000811 V.getIvarAccessHistory() ==
812 RefVal::IvarAccessHistory::AccessedDirectly)
813 V = V ^ RefVal::NotOwned;
814 else
815 V = V ^ RefVal::Released;
George Karpenkov9cbcc212019-01-10 18:14:12 +0000816 } else if (AE.getKind() == DecRefAndStopTrackingHard) {
George Karpenkov70c2ee32018-08-17 21:41:07 +0000817 return removeRefBinding(state, sym);
818 }
819
820 V = V - 1;
821 break;
822
823 case RefVal::NotOwned:
824 if (V.getCount() > 0) {
George Karpenkov9cbcc212019-01-10 18:14:12 +0000825 if (AE.getKind() == DecRefAndStopTrackingHard)
George Karpenkov70c2ee32018-08-17 21:41:07 +0000826 return removeRefBinding(state, sym);
827 V = V - 1;
828 } else if (V.getIvarAccessHistory() ==
829 RefVal::IvarAccessHistory::AccessedDirectly) {
830 // Assume that the instance variable was holding on the object at
831 // +1, and we just didn't know.
George Karpenkov9cbcc212019-01-10 18:14:12 +0000832 if (AE.getKind() == DecRefAndStopTrackingHard)
George Karpenkov70c2ee32018-08-17 21:41:07 +0000833 return removeRefBinding(state, sym);
834 V = V.releaseViaIvar() ^ RefVal::Released;
835 } else {
836 V = V ^ RefVal::ErrorReleaseNotOwned;
837 hasErr = V.getKind();
838 }
839 break;
840 }
841 break;
842 }
843 return setRefBinding(state, sym, V);
844}
845
846void RetainCountChecker::processNonLeakError(ProgramStateRef St,
847 SourceRange ErrorRange,
848 RefVal::Kind ErrorKind,
849 SymbolRef Sym,
850 CheckerContext &C) const {
851 // HACK: Ignore retain-count issues on values accessed through ivars,
852 // because of cases like this:
853 // [_contentView retain];
854 // [_contentView removeFromSuperview];
855 // [self addSubview:_contentView]; // invalidates 'self'
856 // [_contentView release];
857 if (const RefVal *RV = getRefBinding(St, Sym))
858 if (RV->getIvarAccessHistory() != RefVal::IvarAccessHistory::None)
859 return;
860
861 ExplodedNode *N = C.generateErrorNode(St);
862 if (!N)
863 return;
864
865 CFRefBug *BT;
866 switch (ErrorKind) {
867 default:
868 llvm_unreachable("Unhandled error.");
869 case RefVal::ErrorUseAfterRelease:
870 if (!useAfterRelease)
871 useAfterRelease.reset(new UseAfterRelease(this));
872 BT = useAfterRelease.get();
873 break;
874 case RefVal::ErrorReleaseNotOwned:
875 if (!releaseNotOwned)
876 releaseNotOwned.reset(new BadRelease(this));
877 BT = releaseNotOwned.get();
878 break;
879 case RefVal::ErrorDeallocNotOwned:
880 if (!deallocNotOwned)
881 deallocNotOwned.reset(new DeallocNotOwned(this));
882 BT = deallocNotOwned.get();
883 break;
884 }
885
886 assert(BT);
George Karpenkov6babf2a2018-09-21 20:36:21 +0000887 auto report = llvm::make_unique<CFRefReport>(
888 *BT, C.getASTContext().getLangOpts(), SummaryLog, N, Sym);
George Karpenkov70c2ee32018-08-17 21:41:07 +0000889 report->addRange(ErrorRange);
890 C.emitReport(std::move(report));
891}
892
893//===----------------------------------------------------------------------===//
894// Handle the return values of retain-count-related functions.
895//===----------------------------------------------------------------------===//
896
897bool RetainCountChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
898 // Get the callee. We're only interested in simple C functions.
899 ProgramStateRef state = C.getState();
900 const FunctionDecl *FD = C.getCalleeDecl(CE);
901 if (!FD)
902 return false;
903
George Karpenkovc4d6b932018-08-17 21:42:05 +0000904 RetainSummaryManager &SmrMgr = getSummaryManager(C);
905 QualType ResultTy = CE->getCallReturnType(C.getASTContext());
George Karpenkov70c2ee32018-08-17 21:41:07 +0000906
George Karpenkov70c2ee32018-08-17 21:41:07 +0000907 // See if the function has 'rc_ownership_trusted_implementation'
908 // annotate attribute. If it does, we will not inline it.
909 bool hasTrustedImplementationAnnotation = false;
910
George Karpenkov41dc8de2018-10-11 22:59:16 +0000911 const LocationContext *LCtx = C.getLocationContext();
912
George Karpenkov3c2ed8f2018-10-25 23:38:07 +0000913 using BehaviorSummary = RetainSummaryManager::BehaviorSummary;
914 Optional<BehaviorSummary> BSmr =
915 SmrMgr.canEval(CE, FD, hasTrustedImplementationAnnotation);
916
George Karpenkovc4d6b932018-08-17 21:42:05 +0000917 // See if it's one of the specific functions we know how to eval.
George Karpenkov3c2ed8f2018-10-25 23:38:07 +0000918 if (!BSmr)
George Karpenkov70c2ee32018-08-17 21:41:07 +0000919 return false;
920
921 // Bind the return value.
George Karpenkov3c2ed8f2018-10-25 23:38:07 +0000922 if (BSmr == BehaviorSummary::Identity ||
923 BSmr == BehaviorSummary::IdentityOrZero) {
George Karpenkov48de5822018-10-23 23:11:30 +0000924 SVal RetVal = state->getSVal(CE->getArg(0), LCtx);
925
George Karpenkov70c2ee32018-08-17 21:41:07 +0000926 // If the receiver is unknown or the function has
927 // 'rc_ownership_trusted_implementation' annotate attribute, conjure a
928 // return value.
George Karpenkov48de5822018-10-23 23:11:30 +0000929 if (RetVal.isUnknown() ||
930 (hasTrustedImplementationAnnotation && !ResultTy.isNull())) {
931 SValBuilder &SVB = C.getSValBuilder();
932 RetVal =
933 SVB.conjureSymbolVal(nullptr, CE, LCtx, ResultTy, C.blockCount());
934 }
George Karpenkov3c2ed8f2018-10-25 23:38:07 +0000935 state = state->BindExpr(CE, LCtx, RetVal, /*Invalidate=*/false);
936
937 if (BSmr == BehaviorSummary::IdentityOrZero) {
938 // Add a branch where the output is zero.
939 ProgramStateRef NullOutputState = C.getState();
940
941 // Assume that output is zero on the other branch.
942 NullOutputState = NullOutputState->BindExpr(
943 CE, LCtx, C.getSValBuilder().makeNull(), /*Invalidate=*/false);
944
945 C.addTransition(NullOutputState);
946
947 // And on the original branch assume that both input and
948 // output are non-zero.
949 if (auto L = RetVal.getAs<DefinedOrUnknownSVal>())
950 state = state->assume(*L, /*Assumption=*/true);
951
952 }
George Karpenkov70c2ee32018-08-17 21:41:07 +0000953 }
George Karpenkov70c2ee32018-08-17 21:41:07 +0000954
George Karpenkov70c2ee32018-08-17 21:41:07 +0000955 C.addTransition(state);
956 return true;
957}
958
George Karpenkov04553e52018-09-21 20:37:20 +0000959ExplodedNode * RetainCountChecker::processReturn(const ReturnStmt *S,
960 CheckerContext &C) const {
961 ExplodedNode *Pred = C.getPredecessor();
George Karpenkov70c2ee32018-08-17 21:41:07 +0000962
963 // Only adjust the reference count if this is the top-level call frame,
964 // and not the result of inlining. In the future, we should do
965 // better checking even for inlined calls, and see if they match
966 // with their expected semantics (e.g., the method should return a retained
967 // object, etc.).
968 if (!C.inTopFrame())
George Karpenkov04553e52018-09-21 20:37:20 +0000969 return Pred;
970
971 if (!S)
972 return Pred;
George Karpenkov70c2ee32018-08-17 21:41:07 +0000973
974 const Expr *RetE = S->getRetValue();
975 if (!RetE)
George Karpenkov04553e52018-09-21 20:37:20 +0000976 return Pred;
George Karpenkov70c2ee32018-08-17 21:41:07 +0000977
978 ProgramStateRef state = C.getState();
979 SymbolRef Sym =
980 state->getSValAsScalarOrLoc(RetE, C.getLocationContext()).getAsLocSymbol();
981 if (!Sym)
George Karpenkov04553e52018-09-21 20:37:20 +0000982 return Pred;
George Karpenkov70c2ee32018-08-17 21:41:07 +0000983
984 // Get the reference count binding (if any).
985 const RefVal *T = getRefBinding(state, Sym);
986 if (!T)
George Karpenkov04553e52018-09-21 20:37:20 +0000987 return Pred;
George Karpenkov70c2ee32018-08-17 21:41:07 +0000988
989 // Change the reference count.
990 RefVal X = *T;
991
992 switch (X.getKind()) {
993 case RefVal::Owned: {
994 unsigned cnt = X.getCount();
995 assert(cnt > 0);
996 X.setCount(cnt - 1);
997 X = X ^ RefVal::ReturnedOwned;
998 break;
999 }
1000
1001 case RefVal::NotOwned: {
1002 unsigned cnt = X.getCount();
1003 if (cnt) {
1004 X.setCount(cnt - 1);
1005 X = X ^ RefVal::ReturnedOwned;
George Karpenkov04553e52018-09-21 20:37:20 +00001006 } else {
George Karpenkov70c2ee32018-08-17 21:41:07 +00001007 X = X ^ RefVal::ReturnedNotOwned;
1008 }
1009 break;
1010 }
1011
1012 default:
George Karpenkov04553e52018-09-21 20:37:20 +00001013 return Pred;
George Karpenkov70c2ee32018-08-17 21:41:07 +00001014 }
1015
1016 // Update the binding.
1017 state = setRefBinding(state, Sym, X);
George Karpenkov04553e52018-09-21 20:37:20 +00001018 Pred = C.addTransition(state);
George Karpenkov70c2ee32018-08-17 21:41:07 +00001019
1020 // At this point we have updated the state properly.
1021 // Everything after this is merely checking to see if the return value has
1022 // been over- or under-retained.
1023
1024 // Did we cache out?
1025 if (!Pred)
George Karpenkov04553e52018-09-21 20:37:20 +00001026 return nullptr;
George Karpenkov70c2ee32018-08-17 21:41:07 +00001027
1028 // Update the autorelease counts.
1029 static CheckerProgramPointTag AutoreleaseTag(this, "Autorelease");
George Karpenkov04553e52018-09-21 20:37:20 +00001030 state = handleAutoreleaseCounts(state, Pred, &AutoreleaseTag, C, Sym, X, S);
George Karpenkov70c2ee32018-08-17 21:41:07 +00001031
George Karpenkov04553e52018-09-21 20:37:20 +00001032 // Have we generated a sink node?
George Karpenkov70c2ee32018-08-17 21:41:07 +00001033 if (!state)
George Karpenkov04553e52018-09-21 20:37:20 +00001034 return nullptr;
George Karpenkov70c2ee32018-08-17 21:41:07 +00001035
1036 // Get the updated binding.
1037 T = getRefBinding(state, Sym);
1038 assert(T);
1039 X = *T;
1040
1041 // Consult the summary of the enclosing method.
1042 RetainSummaryManager &Summaries = getSummaryManager(C);
1043 const Decl *CD = &Pred->getCodeDecl();
1044 RetEffect RE = RetEffect::MakeNoRet();
1045
1046 // FIXME: What is the convention for blocks? Is there one?
1047 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CD)) {
1048 const RetainSummary *Summ = Summaries.getMethodSummary(MD);
1049 RE = Summ->getRetEffect();
1050 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CD)) {
1051 if (!isa<CXXMethodDecl>(FD)) {
1052 const RetainSummary *Summ = Summaries.getFunctionSummary(FD);
1053 RE = Summ->getRetEffect();
1054 }
1055 }
1056
George Karpenkov04553e52018-09-21 20:37:20 +00001057 return checkReturnWithRetEffect(S, C, Pred, RE, X, Sym, state);
George Karpenkov70c2ee32018-08-17 21:41:07 +00001058}
1059
George Karpenkov04553e52018-09-21 20:37:20 +00001060ExplodedNode * RetainCountChecker::checkReturnWithRetEffect(const ReturnStmt *S,
George Karpenkov70c2ee32018-08-17 21:41:07 +00001061 CheckerContext &C,
1062 ExplodedNode *Pred,
1063 RetEffect RE, RefVal X,
1064 SymbolRef Sym,
1065 ProgramStateRef state) const {
1066 // HACK: Ignore retain-count issues on values accessed through ivars,
1067 // because of cases like this:
1068 // [_contentView retain];
1069 // [_contentView removeFromSuperview];
1070 // [self addSubview:_contentView]; // invalidates 'self'
1071 // [_contentView release];
1072 if (X.getIvarAccessHistory() != RefVal::IvarAccessHistory::None)
George Karpenkov04553e52018-09-21 20:37:20 +00001073 return Pred;
George Karpenkov70c2ee32018-08-17 21:41:07 +00001074
1075 // Any leaks or other errors?
1076 if (X.isReturnedOwned() && X.getCount() == 0) {
1077 if (RE.getKind() != RetEffect::NoRet) {
George Karpenkov70c2ee32018-08-17 21:41:07 +00001078 if (!RE.isOwned()) {
George Karpenkov04553e52018-09-21 20:37:20 +00001079
George Karpenkov70c2ee32018-08-17 21:41:07 +00001080 // The returning type is a CF, we expect the enclosing method should
1081 // return ownership.
George Karpenkov70c2ee32018-08-17 21:41:07 +00001082 X = X ^ RefVal::ErrorLeakReturned;
George Karpenkov70c2ee32018-08-17 21:41:07 +00001083
George Karpenkov70c2ee32018-08-17 21:41:07 +00001084 // Generate an error node.
1085 state = setRefBinding(state, Sym, X);
1086
1087 static CheckerProgramPointTag ReturnOwnLeakTag(this, "ReturnsOwnLeak");
1088 ExplodedNode *N = C.addTransition(state, Pred, &ReturnOwnLeakTag);
1089 if (N) {
1090 const LangOptions &LOpts = C.getASTContext().getLangOpts();
George Karpenkov04553e52018-09-21 20:37:20 +00001091 auto R = llvm::make_unique<CFRefLeakReport>(
George Karpenkov936a9c92018-12-07 20:21:37 +00001092 *getLeakAtReturnBug(LOpts), LOpts, SummaryLog, N, Sym, C);
George Karpenkov04553e52018-09-21 20:37:20 +00001093 C.emitReport(std::move(R));
George Karpenkov70c2ee32018-08-17 21:41:07 +00001094 }
George Karpenkov04553e52018-09-21 20:37:20 +00001095 return N;
George Karpenkov70c2ee32018-08-17 21:41:07 +00001096 }
1097 }
1098 } else if (X.isReturnedNotOwned()) {
1099 if (RE.isOwned()) {
1100 if (X.getIvarAccessHistory() ==
1101 RefVal::IvarAccessHistory::AccessedDirectly) {
1102 // Assume the method was trying to transfer a +1 reference from a
1103 // strong ivar to the caller.
1104 state = setRefBinding(state, Sym,
1105 X.releaseViaIvar() ^ RefVal::ReturnedOwned);
1106 } else {
1107 // Trying to return a not owned object to a caller expecting an
1108 // owned object.
1109 state = setRefBinding(state, Sym, X ^ RefVal::ErrorReturnedNotOwned);
1110
1111 static CheckerProgramPointTag
1112 ReturnNotOwnedTag(this, "ReturnNotOwnedForOwned");
1113
1114 ExplodedNode *N = C.addTransition(state, Pred, &ReturnNotOwnedTag);
1115 if (N) {
1116 if (!returnNotOwnedForOwned)
1117 returnNotOwnedForOwned.reset(new ReturnedNotOwnedForOwned(this));
1118
George Karpenkov04553e52018-09-21 20:37:20 +00001119 auto R = llvm::make_unique<CFRefReport>(
George Karpenkov70c2ee32018-08-17 21:41:07 +00001120 *returnNotOwnedForOwned, C.getASTContext().getLangOpts(),
George Karpenkov04553e52018-09-21 20:37:20 +00001121 SummaryLog, N, Sym);
1122 C.emitReport(std::move(R));
George Karpenkov70c2ee32018-08-17 21:41:07 +00001123 }
George Karpenkov04553e52018-09-21 20:37:20 +00001124 return N;
George Karpenkov70c2ee32018-08-17 21:41:07 +00001125 }
1126 }
1127 }
George Karpenkov04553e52018-09-21 20:37:20 +00001128 return Pred;
George Karpenkov70c2ee32018-08-17 21:41:07 +00001129}
1130
1131//===----------------------------------------------------------------------===//
1132// Check various ways a symbol can be invalidated.
1133//===----------------------------------------------------------------------===//
1134
1135void RetainCountChecker::checkBind(SVal loc, SVal val, const Stmt *S,
1136 CheckerContext &C) const {
1137 // Are we storing to something that causes the value to "escape"?
1138 bool escapes = true;
1139
1140 // A value escapes in three possible cases (this may change):
1141 //
1142 // (1) we are binding to something that is not a memory region.
1143 // (2) we are binding to a memregion that does not have stack storage
1144 // (3) we are binding to a memregion with stack storage that the store
1145 // does not understand.
1146 ProgramStateRef state = C.getState();
1147
George Karpenkov6e9fd132018-08-22 01:17:09 +00001148 if (auto regionLoc = loc.getAs<loc::MemRegionVal>()) {
George Karpenkov70c2ee32018-08-17 21:41:07 +00001149 escapes = !regionLoc->getRegion()->hasStackStorage();
1150
1151 if (!escapes) {
1152 // To test (3), generate a new state with the binding added. If it is
1153 // the same state, then it escapes (since the store cannot represent
1154 // the binding).
1155 // Do this only if we know that the store is not supposed to generate the
1156 // same state.
1157 SVal StoredVal = state->getSVal(regionLoc->getRegion());
1158 if (StoredVal != val)
1159 escapes = (state == (state->bindLoc(*regionLoc, val, C.getLocationContext())));
1160 }
1161 if (!escapes) {
1162 // Case 4: We do not currently model what happens when a symbol is
1163 // assigned to a struct field, so be conservative here and let the symbol
1164 // go. TODO: This could definitely be improved upon.
1165 escapes = !isa<VarRegion>(regionLoc->getRegion());
1166 }
1167 }
1168
1169 // If we are storing the value into an auto function scope variable annotated
1170 // with (__attribute__((cleanup))), stop tracking the value to avoid leak
1171 // false positives.
George Karpenkov6e9fd132018-08-22 01:17:09 +00001172 if (const auto *LVR = dyn_cast_or_null<VarRegion>(loc.getAsRegion())) {
George Karpenkov70c2ee32018-08-17 21:41:07 +00001173 const VarDecl *VD = LVR->getDecl();
1174 if (VD->hasAttr<CleanupAttr>()) {
1175 escapes = true;
1176 }
1177 }
1178
1179 // If our store can represent the binding and we aren't storing to something
1180 // that doesn't have local storage then just return and have the simulation
1181 // state continue as is.
1182 if (!escapes)
1183 return;
1184
1185 // Otherwise, find all symbols referenced by 'val' that we are tracking
1186 // and stop tracking them.
1187 state = state->scanReachableSymbols<StopTrackingCallback>(val).getState();
1188 C.addTransition(state);
1189}
1190
1191ProgramStateRef RetainCountChecker::evalAssume(ProgramStateRef state,
George Karpenkov6e9fd132018-08-22 01:17:09 +00001192 SVal Cond,
1193 bool Assumption) const {
George Karpenkov70c2ee32018-08-17 21:41:07 +00001194 // FIXME: We may add to the interface of evalAssume the list of symbols
1195 // whose assumptions have changed. For now we just iterate through the
1196 // bindings and check if any of the tracked symbols are NULL. This isn't
1197 // too bad since the number of symbols we will track in practice are
1198 // probably small and evalAssume is only called at branches and a few
1199 // other places.
1200 RefBindingsTy B = state->get<RefBindings>();
1201
1202 if (B.isEmpty())
1203 return state;
1204
1205 bool changed = false;
1206 RefBindingsTy::Factory &RefBFactory = state->get_context<RefBindings>();
1207
1208 for (RefBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1209 // Check if the symbol is null stop tracking the symbol.
1210 ConstraintManager &CMgr = state->getConstraintManager();
1211 ConditionTruthVal AllocFailed = CMgr.isNull(state, I.getKey());
1212 if (AllocFailed.isConstrainedTrue()) {
1213 changed = true;
1214 B = RefBFactory.remove(B, I.getKey());
1215 }
1216 }
1217
1218 if (changed)
1219 state = state->set<RefBindings>(B);
1220
1221 return state;
1222}
1223
1224ProgramStateRef
1225RetainCountChecker::checkRegionChanges(ProgramStateRef state,
1226 const InvalidatedSymbols *invalidated,
1227 ArrayRef<const MemRegion *> ExplicitRegions,
1228 ArrayRef<const MemRegion *> Regions,
1229 const LocationContext *LCtx,
1230 const CallEvent *Call) const {
1231 if (!invalidated)
1232 return state;
1233
1234 llvm::SmallPtrSet<SymbolRef, 8> WhitelistedSymbols;
1235 for (ArrayRef<const MemRegion *>::iterator I = ExplicitRegions.begin(),
1236 E = ExplicitRegions.end(); I != E; ++I) {
1237 if (const SymbolicRegion *SR = (*I)->StripCasts()->getAs<SymbolicRegion>())
1238 WhitelistedSymbols.insert(SR->getSymbol());
1239 }
1240
George Karpenkov6fd5c86d2018-10-31 17:38:29 +00001241 for (SymbolRef sym :
1242 llvm::make_range(invalidated->begin(), invalidated->end())) {
George Karpenkov70c2ee32018-08-17 21:41:07 +00001243 if (WhitelistedSymbols.count(sym))
1244 continue;
1245 // Remove any existing reference-count binding.
1246 state = removeRefBinding(state, sym);
1247 }
1248 return state;
1249}
1250
George Karpenkov70c2ee32018-08-17 21:41:07 +00001251ProgramStateRef
1252RetainCountChecker::handleAutoreleaseCounts(ProgramStateRef state,
1253 ExplodedNode *Pred,
1254 const ProgramPointTag *Tag,
1255 CheckerContext &Ctx,
George Karpenkov04553e52018-09-21 20:37:20 +00001256 SymbolRef Sym,
1257 RefVal V,
1258 const ReturnStmt *S) const {
George Karpenkov70c2ee32018-08-17 21:41:07 +00001259 unsigned ACnt = V.getAutoreleaseCount();
1260
1261 // No autorelease counts? Nothing to be done.
1262 if (!ACnt)
1263 return state;
1264
1265 unsigned Cnt = V.getCount();
1266
1267 // FIXME: Handle sending 'autorelease' to already released object.
1268
1269 if (V.getKind() == RefVal::ReturnedOwned)
1270 ++Cnt;
1271
1272 // If we would over-release here, but we know the value came from an ivar,
1273 // assume it was a strong ivar that's just been relinquished.
1274 if (ACnt > Cnt &&
1275 V.getIvarAccessHistory() == RefVal::IvarAccessHistory::AccessedDirectly) {
1276 V = V.releaseViaIvar();
1277 --ACnt;
1278 }
1279
1280 if (ACnt <= Cnt) {
1281 if (ACnt == Cnt) {
1282 V.clearCounts();
George Karpenkov04553e52018-09-21 20:37:20 +00001283 if (V.getKind() == RefVal::ReturnedOwned) {
George Karpenkov70c2ee32018-08-17 21:41:07 +00001284 V = V ^ RefVal::ReturnedNotOwned;
George Karpenkov04553e52018-09-21 20:37:20 +00001285 } else {
George Karpenkov70c2ee32018-08-17 21:41:07 +00001286 V = V ^ RefVal::NotOwned;
George Karpenkov04553e52018-09-21 20:37:20 +00001287 }
George Karpenkov70c2ee32018-08-17 21:41:07 +00001288 } else {
1289 V.setCount(V.getCount() - ACnt);
1290 V.setAutoreleaseCount(0);
1291 }
1292 return setRefBinding(state, Sym, V);
1293 }
1294
1295 // HACK: Ignore retain-count issues on values accessed through ivars,
1296 // because of cases like this:
1297 // [_contentView retain];
1298 // [_contentView removeFromSuperview];
1299 // [self addSubview:_contentView]; // invalidates 'self'
1300 // [_contentView release];
1301 if (V.getIvarAccessHistory() != RefVal::IvarAccessHistory::None)
1302 return state;
1303
1304 // Woah! More autorelease counts then retain counts left.
1305 // Emit hard error.
1306 V = V ^ RefVal::ErrorOverAutorelease;
1307 state = setRefBinding(state, Sym, V);
1308
1309 ExplodedNode *N = Ctx.generateSink(state, Pred, Tag);
1310 if (N) {
1311 SmallString<128> sbuf;
1312 llvm::raw_svector_ostream os(sbuf);
1313 os << "Object was autoreleased ";
1314 if (V.getAutoreleaseCount() > 1)
1315 os << V.getAutoreleaseCount() << " times but the object ";
1316 else
1317 os << "but ";
1318 os << "has a +" << V.getCount() << " retain count";
1319
1320 if (!overAutorelease)
1321 overAutorelease.reset(new OverAutorelease(this));
1322
1323 const LangOptions &LOpts = Ctx.getASTContext().getLangOpts();
George Karpenkov04553e52018-09-21 20:37:20 +00001324 auto R = llvm::make_unique<CFRefReport>(*overAutorelease, LOpts, SummaryLog,
1325 N, Sym, os.str());
1326 Ctx.emitReport(std::move(R));
George Karpenkov70c2ee32018-08-17 21:41:07 +00001327 }
1328
1329 return nullptr;
1330}
1331
1332ProgramStateRef
1333RetainCountChecker::handleSymbolDeath(ProgramStateRef state,
1334 SymbolRef sid, RefVal V,
1335 SmallVectorImpl<SymbolRef> &Leaked) const {
1336 bool hasLeak;
1337
1338 // HACK: Ignore retain-count issues on values accessed through ivars,
1339 // because of cases like this:
1340 // [_contentView retain];
1341 // [_contentView removeFromSuperview];
1342 // [self addSubview:_contentView]; // invalidates 'self'
1343 // [_contentView release];
1344 if (V.getIvarAccessHistory() != RefVal::IvarAccessHistory::None)
1345 hasLeak = false;
1346 else if (V.isOwned())
1347 hasLeak = true;
1348 else if (V.isNotOwned() || V.isReturnedOwned())
1349 hasLeak = (V.getCount() > 0);
1350 else
1351 hasLeak = false;
1352
1353 if (!hasLeak)
1354 return removeRefBinding(state, sid);
1355
1356 Leaked.push_back(sid);
1357 return setRefBinding(state, sid, V ^ RefVal::ErrorLeak);
1358}
1359
1360ExplodedNode *
1361RetainCountChecker::processLeaks(ProgramStateRef state,
1362 SmallVectorImpl<SymbolRef> &Leaked,
1363 CheckerContext &Ctx,
1364 ExplodedNode *Pred) const {
1365 // Generate an intermediate node representing the leak point.
1366 ExplodedNode *N = Ctx.addTransition(state, Pred);
1367
1368 if (N) {
1369 for (SmallVectorImpl<SymbolRef>::iterator
1370 I = Leaked.begin(), E = Leaked.end(); I != E; ++I) {
1371
1372 const LangOptions &LOpts = Ctx.getASTContext().getLangOpts();
1373 CFRefBug *BT = Pred ? getLeakWithinFunctionBug(LOpts)
1374 : getLeakAtReturnBug(LOpts);
1375 assert(BT && "BugType not initialized.");
1376
George Karpenkov6babf2a2018-09-21 20:36:21 +00001377 Ctx.emitReport(llvm::make_unique<CFRefLeakReport>(
George Karpenkov936a9c92018-12-07 20:21:37 +00001378 *BT, LOpts, SummaryLog, N, *I, Ctx));
George Karpenkov70c2ee32018-08-17 21:41:07 +00001379 }
1380 }
1381
1382 return N;
1383}
1384
George Karpenkovb1b791b2018-08-17 21:43:27 +00001385static bool isISLObjectRef(QualType Ty) {
1386 return StringRef(Ty.getAsString()).startswith("isl_");
George Karpenkov70c2ee32018-08-17 21:41:07 +00001387}
1388
1389void RetainCountChecker::checkBeginFunction(CheckerContext &Ctx) const {
1390 if (!Ctx.inTopFrame())
1391 return;
1392
George Karpenkovc4d6b932018-08-17 21:42:05 +00001393 RetainSummaryManager &SmrMgr = getSummaryManager(Ctx);
George Karpenkov70c2ee32018-08-17 21:41:07 +00001394 const LocationContext *LCtx = Ctx.getLocationContext();
1395 const FunctionDecl *FD = dyn_cast<FunctionDecl>(LCtx->getDecl());
1396
George Karpenkovc4d6b932018-08-17 21:42:05 +00001397 if (!FD || SmrMgr.isTrustedReferenceCountImplementation(FD))
George Karpenkov70c2ee32018-08-17 21:41:07 +00001398 return;
1399
1400 ProgramStateRef state = Ctx.getState();
George Karpenkovc4d6b932018-08-17 21:42:05 +00001401 const RetainSummary *FunctionSummary = SmrMgr.getFunctionSummary(FD);
George Karpenkov70c2ee32018-08-17 21:41:07 +00001402 ArgEffects CalleeSideArgEffects = FunctionSummary->getArgEffects();
1403
1404 for (unsigned idx = 0, e = FD->getNumParams(); idx != e; ++idx) {
1405 const ParmVarDecl *Param = FD->getParamDecl(idx);
1406 SymbolRef Sym = state->getSVal(state->getRegion(Param, LCtx)).getAsSymbol();
1407
1408 QualType Ty = Param->getType();
1409 const ArgEffect *AE = CalleeSideArgEffects.lookup(idx);
George Karpenkov585a2102019-01-10 18:13:59 +00001410 if (AE && AE->getKind() == DecRef && isISLObjectRef(Ty)) {
George Karpenkov6e9fd132018-08-22 01:17:09 +00001411 state = setRefBinding(
George Karpenkov7e3016d2019-01-10 18:13:46 +00001412 state, Sym, RefVal::makeOwned(ObjKind::Generalized, Ty));
George Karpenkovb1b791b2018-08-17 21:43:27 +00001413 } else if (isISLObjectRef(Ty)) {
George Karpenkov70c2ee32018-08-17 21:41:07 +00001414 state = setRefBinding(
1415 state, Sym,
George Karpenkov7e3016d2019-01-10 18:13:46 +00001416 RefVal::makeNotOwned(ObjKind::Generalized, Ty));
George Karpenkov70c2ee32018-08-17 21:41:07 +00001417 }
1418 }
1419
1420 Ctx.addTransition(state);
1421}
1422
1423void RetainCountChecker::checkEndFunction(const ReturnStmt *RS,
1424 CheckerContext &Ctx) const {
George Karpenkov04553e52018-09-21 20:37:20 +00001425 ExplodedNode *Pred = processReturn(RS, Ctx);
1426
1427 // Created state cached out.
1428 if (!Pred) {
1429 return;
1430 }
1431
1432 ProgramStateRef state = Pred->getState();
George Karpenkov70c2ee32018-08-17 21:41:07 +00001433 RefBindingsTy B = state->get<RefBindings>();
George Karpenkov70c2ee32018-08-17 21:41:07 +00001434
1435 // Don't process anything within synthesized bodies.
1436 const LocationContext *LCtx = Pred->getLocationContext();
1437 if (LCtx->getAnalysisDeclContext()->isBodyAutosynthesized()) {
1438 assert(!LCtx->inTopFrame());
1439 return;
1440 }
1441
1442 for (RefBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1443 state = handleAutoreleaseCounts(state, Pred, /*Tag=*/nullptr, Ctx,
1444 I->first, I->second);
1445 if (!state)
1446 return;
1447 }
1448
1449 // If the current LocationContext has a parent, don't check for leaks.
1450 // We will do that later.
1451 // FIXME: we should instead check for imbalances of the retain/releases,
1452 // and suggest annotations.
1453 if (LCtx->getParent())
1454 return;
1455
1456 B = state->get<RefBindings>();
1457 SmallVector<SymbolRef, 10> Leaked;
1458
1459 for (RefBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I)
1460 state = handleSymbolDeath(state, I->first, I->second, Leaked);
1461
1462 processLeaks(state, Leaked, Ctx, Pred);
1463}
1464
George Karpenkov70c2ee32018-08-17 21:41:07 +00001465void RetainCountChecker::checkDeadSymbols(SymbolReaper &SymReaper,
1466 CheckerContext &C) const {
1467 ExplodedNode *Pred = C.getPredecessor();
1468
1469 ProgramStateRef state = C.getState();
1470 RefBindingsTy B = state->get<RefBindings>();
1471 SmallVector<SymbolRef, 10> Leaked;
1472
1473 // Update counts from autorelease pools
Artem Dergachevbbc6d682018-11-30 03:27:50 +00001474 for (const auto &I: state->get<RefBindings>()) {
1475 SymbolRef Sym = I.first;
1476 if (SymReaper.isDead(Sym)) {
Artem Dergachev65b4d7d2018-10-15 17:47:56 +00001477 static CheckerProgramPointTag Tag(this, "DeadSymbolAutorelease");
Artem Dergachevbbc6d682018-11-30 03:27:50 +00001478 const RefVal &V = I.second;
1479 state = handleAutoreleaseCounts(state, Pred, &Tag, C, Sym, V);
George Karpenkov70c2ee32018-08-17 21:41:07 +00001480 if (!state)
1481 return;
1482
1483 // Fetch the new reference count from the state, and use it to handle
1484 // this symbol.
Artem Dergachevbbc6d682018-11-30 03:27:50 +00001485 state = handleSymbolDeath(state, Sym, *getRefBinding(state, Sym), Leaked);
George Karpenkov70c2ee32018-08-17 21:41:07 +00001486 }
1487 }
1488
1489 if (Leaked.empty()) {
1490 C.addTransition(state);
1491 return;
1492 }
1493
1494 Pred = processLeaks(state, Leaked, C, Pred);
1495
1496 // Did we cache out?
1497 if (!Pred)
1498 return;
1499
1500 // Now generate a new node that nukes the old bindings.
1501 // The only bindings left at this point are the leaked symbols.
1502 RefBindingsTy::Factory &F = state->get_context<RefBindings>();
1503 B = state->get<RefBindings>();
1504
1505 for (SmallVectorImpl<SymbolRef>::iterator I = Leaked.begin(),
1506 E = Leaked.end();
1507 I != E; ++I)
1508 B = F.remove(B, *I);
1509
1510 state = state->set<RefBindings>(B);
1511 C.addTransition(state, Pred);
1512}
1513
1514void RetainCountChecker::printState(raw_ostream &Out, ProgramStateRef State,
1515 const char *NL, const char *Sep) const {
1516
1517 RefBindingsTy B = State->get<RefBindings>();
1518
1519 if (B.isEmpty())
1520 return;
1521
1522 Out << Sep << NL;
1523
1524 for (RefBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1525 Out << I->first << " : ";
1526 I->second.print(Out);
1527 Out << NL;
1528 }
1529}
1530
1531//===----------------------------------------------------------------------===//
George Karpenkov70c2ee32018-08-17 21:41:07 +00001532// Checker registration.
1533//===----------------------------------------------------------------------===//
1534
1535void ento::registerRetainCountChecker(CheckerManager &Mgr) {
Kristof Umannc83b0dd2018-11-02 15:48:10 +00001536 auto *Chk = Mgr.registerChecker<RetainCountChecker>();
George Karpenkov27db3302018-12-07 20:21:51 +00001537 Chk->TrackObjCAndCFObjects = true;
1538}
Kristof Umannc83b0dd2018-11-02 15:48:10 +00001539
George Karpenkovd1081ec2018-12-11 01:13:58 +00001540// FIXME: remove this, hack for backwards compatibility:
1541// it should be possible to enable the NS/CF retain count checker as
1542// osx.cocoa.RetainCount, and it should be possible to disable
1543// osx.OSObjectRetainCount using osx.cocoa.RetainCount:CheckOSObject=false.
1544static bool hasPrevCheckOSObjectOptionDisabled(AnalyzerOptions &Options) {
1545 auto I = Options.Config.find("osx.cocoa.RetainCount:CheckOSObject");
1546 if (I != Options.Config.end())
1547 return I->getValue() == "false";
1548 return false;
1549}
1550
George Karpenkov27db3302018-12-07 20:21:51 +00001551void ento::registerOSObjectRetainCountChecker(CheckerManager &Mgr) {
1552 auto *Chk = Mgr.registerChecker<RetainCountChecker>();
George Karpenkovd1081ec2018-12-11 01:13:58 +00001553 if (!hasPrevCheckOSObjectOptionDisabled(Mgr.getAnalyzerOptions()))
1554 Chk->TrackOSObjects = true;
George Karpenkov70c2ee32018-08-17 21:41:07 +00001555}