blob: a8957e1704eaeba60030c1549a348110e327daf0 [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;
19using namespace objc_retain;
20using namespace retaincountchecker;
21using llvm::StrInStrNoCase;
22
23REGISTER_MAP_WITH_PROGRAMSTATE(RefBindings, SymbolRef, RefVal)
24
25namespace clang {
26namespace ento {
27namespace retaincountchecker {
28
29const RefVal *getRefBinding(ProgramStateRef State, SymbolRef Sym) {
30 return State->get<RefBindings>(Sym);
31}
32
33ProgramStateRef setRefBinding(ProgramStateRef State, SymbolRef Sym,
34 RefVal Val) {
35 return State->set<RefBindings>(Sym, Val);
36}
37
38ProgramStateRef removeRefBinding(ProgramStateRef State, SymbolRef Sym) {
39 return State->remove<RefBindings>(Sym);
40}
41
42} // end namespace retaincountchecker
43} // end namespace ento
44} // end namespace clang
45
46void RefVal::print(raw_ostream &Out) const {
47 if (!T.isNull())
48 Out << "Tracked " << T.getAsString() << '/';
49
50 switch (getKind()) {
51 default: llvm_unreachable("Invalid RefVal kind");
52 case Owned: {
53 Out << "Owned";
54 unsigned cnt = getCount();
55 if (cnt) Out << " (+ " << cnt << ")";
56 break;
57 }
58
59 case NotOwned: {
60 Out << "NotOwned";
61 unsigned cnt = getCount();
62 if (cnt) Out << " (+ " << cnt << ")";
63 break;
64 }
65
66 case ReturnedOwned: {
67 Out << "ReturnedOwned";
68 unsigned cnt = getCount();
69 if (cnt) Out << " (+ " << cnt << ")";
70 break;
71 }
72
73 case ReturnedNotOwned: {
74 Out << "ReturnedNotOwned";
75 unsigned cnt = getCount();
76 if (cnt) Out << " (+ " << cnt << ")";
77 break;
78 }
79
80 case Released:
81 Out << "Released";
82 break;
83
84 case ErrorDeallocNotOwned:
85 Out << "-dealloc (not-owned)";
86 break;
87
88 case ErrorLeak:
89 Out << "Leaked";
90 break;
91
92 case ErrorLeakReturned:
93 Out << "Leaked (Bad naming)";
94 break;
95
96 case ErrorUseAfterRelease:
97 Out << "Use-After-Release [ERROR]";
98 break;
99
100 case ErrorReleaseNotOwned:
101 Out << "Release of Not-Owned [ERROR]";
102 break;
103
104 case RefVal::ErrorOverAutorelease:
105 Out << "Over-autoreleased";
106 break;
107
108 case RefVal::ErrorReturnedNotOwned:
109 Out << "Non-owned object returned instead of owned";
110 break;
111 }
112
113 switch (getIvarAccessHistory()) {
114 case IvarAccessHistory::None:
115 break;
116 case IvarAccessHistory::AccessedDirectly:
117 Out << " [direct ivar access]";
118 break;
119 case IvarAccessHistory::ReleasedAfterDirectAccess:
120 Out << " [released after direct ivar access]";
121 }
122
123 if (ACnt) {
124 Out << " [autorelease -" << ACnt << ']';
125 }
126}
127
128namespace {
129class StopTrackingCallback final : public SymbolVisitor {
130 ProgramStateRef state;
131public:
132 StopTrackingCallback(ProgramStateRef st) : state(std::move(st)) {}
133 ProgramStateRef getState() const { return state; }
134
135 bool VisitSymbol(SymbolRef sym) override {
136 state = state->remove<RefBindings>(sym);
137 return true;
138 }
139};
140} // end anonymous namespace
141
142//===----------------------------------------------------------------------===//
143// Handle statements that may have an effect on refcounts.
144//===----------------------------------------------------------------------===//
145
146void RetainCountChecker::checkPostStmt(const BlockExpr *BE,
147 CheckerContext &C) const {
148
149 // Scan the BlockDecRefExprs for any object the retain count checker
150 // may be tracking.
151 if (!BE->getBlockDecl()->hasCaptures())
152 return;
153
154 ProgramStateRef state = C.getState();
155 auto *R = cast<BlockDataRegion>(C.getSVal(BE).getAsRegion());
156
157 BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
158 E = R->referenced_vars_end();
159
160 if (I == E)
161 return;
162
163 // FIXME: For now we invalidate the tracking of all symbols passed to blocks
164 // via captured variables, even though captured variables result in a copy
165 // and in implicit increment/decrement of a retain count.
166 SmallVector<const MemRegion*, 10> Regions;
167 const LocationContext *LC = C.getLocationContext();
168 MemRegionManager &MemMgr = C.getSValBuilder().getRegionManager();
169
170 for ( ; I != E; ++I) {
171 const VarRegion *VR = I.getCapturedRegion();
172 if (VR->getSuperRegion() == R) {
173 VR = MemMgr.getVarRegion(VR->getDecl(), LC);
174 }
175 Regions.push_back(VR);
176 }
177
178 state =
179 state->scanReachableSymbols<StopTrackingCallback>(Regions.data(),
180 Regions.data() + Regions.size()).getState();
181 C.addTransition(state);
182}
183
184void RetainCountChecker::checkPostStmt(const CastExpr *CE,
185 CheckerContext &C) const {
186 const ObjCBridgedCastExpr *BE = dyn_cast<ObjCBridgedCastExpr>(CE);
187 if (!BE)
188 return;
189
190 ArgEffect AE = IncRef;
191
192 switch (BE->getBridgeKind()) {
193 case OBC_Bridge:
194 // Do nothing.
195 return;
196 case OBC_BridgeRetained:
197 AE = IncRef;
198 break;
199 case OBC_BridgeTransfer:
200 AE = DecRefBridgedTransferred;
201 break;
202 }
203
204 ProgramStateRef state = C.getState();
205 SymbolRef Sym = C.getSVal(CE).getAsLocSymbol();
206 if (!Sym)
207 return;
208 const RefVal* T = getRefBinding(state, Sym);
209 if (!T)
210 return;
211
212 RefVal::Kind hasErr = (RefVal::Kind) 0;
213 state = updateSymbol(state, Sym, *T, AE, hasErr, C);
214
215 if (hasErr) {
216 // FIXME: If we get an error during a bridge cast, should we report it?
217 return;
218 }
219
220 C.addTransition(state);
221}
222
223void RetainCountChecker::processObjCLiterals(CheckerContext &C,
224 const Expr *Ex) const {
225 ProgramStateRef state = C.getState();
226 const ExplodedNode *pred = C.getPredecessor();
227 for (const Stmt *Child : Ex->children()) {
228 SVal V = pred->getSVal(Child);
229 if (SymbolRef sym = V.getAsSymbol())
230 if (const RefVal* T = getRefBinding(state, sym)) {
231 RefVal::Kind hasErr = (RefVal::Kind) 0;
232 state = updateSymbol(state, sym, *T, MayEscape, hasErr, C);
233 if (hasErr) {
234 processNonLeakError(state, Child->getSourceRange(), hasErr, sym, C);
235 return;
236 }
237 }
238 }
239
240 // Return the object as autoreleased.
241 // RetEffect RE = RetEffect::MakeNotOwned(RetEffect::ObjC);
242 if (SymbolRef sym =
243 state->getSVal(Ex, pred->getLocationContext()).getAsSymbol()) {
244 QualType ResultTy = Ex->getType();
245 state = setRefBinding(state, sym,
246 RefVal::makeNotOwned(RetEffect::ObjC, ResultTy));
247 }
248
249 C.addTransition(state);
250}
251
252void RetainCountChecker::checkPostStmt(const ObjCArrayLiteral *AL,
253 CheckerContext &C) const {
254 // Apply the 'MayEscape' to all values.
255 processObjCLiterals(C, AL);
256}
257
258void RetainCountChecker::checkPostStmt(const ObjCDictionaryLiteral *DL,
259 CheckerContext &C) const {
260 // Apply the 'MayEscape' to all keys and values.
261 processObjCLiterals(C, DL);
262}
263
264void RetainCountChecker::checkPostStmt(const ObjCBoxedExpr *Ex,
265 CheckerContext &C) const {
266 const ExplodedNode *Pred = C.getPredecessor();
267 ProgramStateRef State = Pred->getState();
268
269 if (SymbolRef Sym = Pred->getSVal(Ex).getAsSymbol()) {
270 QualType ResultTy = Ex->getType();
271 State = setRefBinding(State, Sym,
272 RefVal::makeNotOwned(RetEffect::ObjC, ResultTy));
273 }
274
275 C.addTransition(State);
276}
277
278void RetainCountChecker::checkPostStmt(const ObjCIvarRefExpr *IRE,
279 CheckerContext &C) const {
280 Optional<Loc> IVarLoc = C.getSVal(IRE).getAs<Loc>();
281 if (!IVarLoc)
282 return;
283
284 ProgramStateRef State = C.getState();
285 SymbolRef Sym = State->getSVal(*IVarLoc).getAsSymbol();
286 if (!Sym || !dyn_cast_or_null<ObjCIvarRegion>(Sym->getOriginRegion()))
287 return;
288
289 // Accessing an ivar directly is unusual. If we've done that, be more
290 // forgiving about what the surrounding code is allowed to do.
291
292 QualType Ty = Sym->getType();
293 RetEffect::ObjKind Kind;
294 if (Ty->isObjCRetainableType())
295 Kind = RetEffect::ObjC;
296 else if (coreFoundation::isCFObjectRef(Ty))
297 Kind = RetEffect::CF;
298 else
299 return;
300
301 // If the value is already known to be nil, don't bother tracking it.
302 ConstraintManager &CMgr = State->getConstraintManager();
303 if (CMgr.isNull(State, Sym).isConstrainedTrue())
304 return;
305
306 if (const RefVal *RV = getRefBinding(State, Sym)) {
307 // If we've seen this symbol before, or we're only seeing it now because
308 // of something the analyzer has synthesized, don't do anything.
309 if (RV->getIvarAccessHistory() != RefVal::IvarAccessHistory::None ||
310 isSynthesizedAccessor(C.getStackFrame())) {
311 return;
312 }
313
314 // Note that this value has been loaded from an ivar.
315 C.addTransition(setRefBinding(State, Sym, RV->withIvarAccess()));
316 return;
317 }
318
319 RefVal PlusZero = RefVal::makeNotOwned(Kind, Ty);
320
321 // In a synthesized accessor, the effective retain count is +0.
322 if (isSynthesizedAccessor(C.getStackFrame())) {
323 C.addTransition(setRefBinding(State, Sym, PlusZero));
324 return;
325 }
326
327 State = setRefBinding(State, Sym, PlusZero.withIvarAccess());
328 C.addTransition(State);
329}
330
331void RetainCountChecker::checkPostCall(const CallEvent &Call,
332 CheckerContext &C) const {
333 RetainSummaryManager &Summaries = getSummaryManager(C);
334 const RetainSummary *Summ = Summaries.getSummary(Call, C.getState());
335
336 if (C.wasInlined) {
337 processSummaryOfInlined(*Summ, Call, C);
338 return;
339 }
340 checkSummary(*Summ, Call, C);
341}
342
343/// GetReturnType - Used to get the return type of a message expression or
344/// function call with the intention of affixing that type to a tracked symbol.
345/// While the return type can be queried directly from RetEx, when
346/// invoking class methods we augment to the return type to be that of
347/// a pointer to the class (as opposed it just being id).
348// FIXME: We may be able to do this with related result types instead.
349// This function is probably overestimating.
350static QualType GetReturnType(const Expr *RetE, ASTContext &Ctx) {
351 QualType RetTy = RetE->getType();
352 // If RetE is not a message expression just return its type.
353 // If RetE is a message expression, return its types if it is something
354 /// more specific than id.
355 if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(RetE))
356 if (const ObjCObjectPointerType *PT = RetTy->getAs<ObjCObjectPointerType>())
357 if (PT->isObjCQualifiedIdType() || PT->isObjCIdType() ||
358 PT->isObjCClassType()) {
359 // At this point we know the return type of the message expression is
360 // id, id<...>, or Class. If we have an ObjCInterfaceDecl, we know this
361 // is a call to a class method whose type we can resolve. In such
362 // cases, promote the return type to XXX* (where XXX is the class).
363 const ObjCInterfaceDecl *D = ME->getReceiverInterface();
364 return !D ? RetTy :
365 Ctx.getObjCObjectPointerType(Ctx.getObjCInterfaceType(D));
366 }
367
368 return RetTy;
369}
370
371// We don't always get the exact modeling of the function with regards to the
372// retain count checker even when the function is inlined. For example, we need
373// to stop tracking the symbols which were marked with StopTrackingHard.
374void RetainCountChecker::processSummaryOfInlined(const RetainSummary &Summ,
375 const CallEvent &CallOrMsg,
376 CheckerContext &C) const {
377 ProgramStateRef state = C.getState();
378
379 // Evaluate the effect of the arguments.
380 for (unsigned idx = 0, e = CallOrMsg.getNumArgs(); idx != e; ++idx) {
381 if (Summ.getArg(idx) == StopTrackingHard) {
382 SVal V = CallOrMsg.getArgSVal(idx);
383 if (SymbolRef Sym = V.getAsLocSymbol()) {
384 state = removeRefBinding(state, Sym);
385 }
386 }
387 }
388
389 // Evaluate the effect on the message receiver.
390 const ObjCMethodCall *MsgInvocation = dyn_cast<ObjCMethodCall>(&CallOrMsg);
391 if (MsgInvocation) {
392 if (SymbolRef Sym = MsgInvocation->getReceiverSVal().getAsLocSymbol()) {
393 if (Summ.getReceiverEffect() == StopTrackingHard) {
394 state = removeRefBinding(state, Sym);
395 }
396 }
397 }
398
399 // Consult the summary for the return value.
400 RetEffect RE = Summ.getRetEffect();
401 if (RE.getKind() == RetEffect::NoRetHard) {
402 SymbolRef Sym = CallOrMsg.getReturnValue().getAsSymbol();
403 if (Sym)
404 state = removeRefBinding(state, Sym);
405 }
406
407 C.addTransition(state);
408}
409
410static ProgramStateRef updateOutParameter(ProgramStateRef State,
411 SVal ArgVal,
412 ArgEffect Effect) {
413 auto *ArgRegion = dyn_cast_or_null<TypedValueRegion>(ArgVal.getAsRegion());
414 if (!ArgRegion)
415 return State;
416
417 QualType PointeeTy = ArgRegion->getValueType();
418 if (!coreFoundation::isCFObjectRef(PointeeTy))
419 return State;
420
421 SVal PointeeVal = State->getSVal(ArgRegion);
422 SymbolRef Pointee = PointeeVal.getAsLocSymbol();
423 if (!Pointee)
424 return State;
425
426 switch (Effect) {
427 case UnretainedOutParameter:
428 State = setRefBinding(State, Pointee,
429 RefVal::makeNotOwned(RetEffect::CF, PointeeTy));
430 break;
431 case RetainedOutParameter:
432 // Do nothing. Retained out parameters will either point to a +1 reference
433 // or NULL, but the way you check for failure differs depending on the API.
434 // Consequently, we don't have a good way to track them yet.
435 break;
436
437 default:
438 llvm_unreachable("only for out parameters");
439 }
440
441 return State;
442}
443
444void RetainCountChecker::checkSummary(const RetainSummary &Summ,
445 const CallEvent &CallOrMsg,
446 CheckerContext &C) const {
447 ProgramStateRef state = C.getState();
448
449 // Evaluate the effect of the arguments.
450 RefVal::Kind hasErr = (RefVal::Kind) 0;
451 SourceRange ErrorRange;
452 SymbolRef ErrorSym = nullptr;
453
454 for (unsigned idx = 0, e = CallOrMsg.getNumArgs(); idx != e; ++idx) {
455 SVal V = CallOrMsg.getArgSVal(idx);
456
457 ArgEffect Effect = Summ.getArg(idx);
458 if (Effect == RetainedOutParameter || Effect == UnretainedOutParameter) {
459 state = updateOutParameter(state, V, Effect);
460 } else if (SymbolRef Sym = V.getAsLocSymbol()) {
461 if (const RefVal *T = getRefBinding(state, Sym)) {
462 state = updateSymbol(state, Sym, *T, Effect, hasErr, C);
463 if (hasErr) {
464 ErrorRange = CallOrMsg.getArgSourceRange(idx);
465 ErrorSym = Sym;
466 break;
467 }
468 }
469 }
470 }
471
472 // Evaluate the effect on the message receiver.
473 bool ReceiverIsTracked = false;
474 if (!hasErr) {
475 const ObjCMethodCall *MsgInvocation = dyn_cast<ObjCMethodCall>(&CallOrMsg);
476 if (MsgInvocation) {
477 if (SymbolRef Sym = MsgInvocation->getReceiverSVal().getAsLocSymbol()) {
478 if (const RefVal *T = getRefBinding(state, Sym)) {
479 ReceiverIsTracked = true;
480 state = updateSymbol(state, Sym, *T, Summ.getReceiverEffect(),
481 hasErr, C);
482 if (hasErr) {
483 ErrorRange = MsgInvocation->getOriginExpr()->getReceiverRange();
484 ErrorSym = Sym;
485 }
486 }
487 }
488 }
489 }
490
491 // Process any errors.
492 if (hasErr) {
493 processNonLeakError(state, ErrorRange, hasErr, ErrorSym, C);
494 return;
495 }
496
497 // Consult the summary for the return value.
498 RetEffect RE = Summ.getRetEffect();
499
500 if (RE.getKind() == RetEffect::OwnedWhenTrackedReceiver) {
501 if (ReceiverIsTracked)
502 RE = getSummaryManager(C).getObjAllocRetEffect();
503 else
504 RE = RetEffect::MakeNoRet();
505 }
506
507 switch (RE.getKind()) {
508 default:
509 llvm_unreachable("Unhandled RetEffect.");
510
511 case RetEffect::NoRet:
512 case RetEffect::NoRetHard:
513 // No work necessary.
514 break;
515
516 case RetEffect::OwnedSymbol: {
517 SymbolRef Sym = CallOrMsg.getReturnValue().getAsSymbol();
518 if (!Sym)
519 break;
520
521 // Use the result type from the CallEvent as it automatically adjusts
522 // for methods/functions that return references.
523 QualType ResultTy = CallOrMsg.getResultType();
524 state = setRefBinding(state, Sym, RefVal::makeOwned(RE.getObjKind(),
525 ResultTy));
526
527 // FIXME: Add a flag to the checker where allocations are assumed to
528 // *not* fail.
529 break;
530 }
531
532 case RetEffect::NotOwnedSymbol: {
533 const Expr *Ex = CallOrMsg.getOriginExpr();
534 SymbolRef Sym = CallOrMsg.getReturnValue().getAsSymbol();
535 if (!Sym)
536 break;
537 assert(Ex);
538 // Use GetReturnType in order to give [NSFoo alloc] the type NSFoo *.
539 QualType ResultTy = GetReturnType(Ex, C.getASTContext());
540 state = setRefBinding(state, Sym, RefVal::makeNotOwned(RE.getObjKind(),
541 ResultTy));
542 break;
543 }
544 }
545
546 // This check is actually necessary; otherwise the statement builder thinks
547 // we've hit a previously-found path.
548 // Normally addTransition takes care of this, but we want the node pointer.
549 ExplodedNode *NewNode;
550 if (state == C.getState()) {
551 NewNode = C.getPredecessor();
552 } else {
553 NewNode = C.addTransition(state);
554 }
555
556 // Annotate the node with summary we used.
557 if (NewNode) {
558 // FIXME: This is ugly. See checkEndAnalysis for why it's necessary.
559 if (ShouldResetSummaryLog) {
560 SummaryLog.clear();
561 ShouldResetSummaryLog = false;
562 }
563 SummaryLog[NewNode] = &Summ;
564 }
565}
566
567ProgramStateRef
568RetainCountChecker::updateSymbol(ProgramStateRef state, SymbolRef sym,
569 RefVal V, ArgEffect E, RefVal::Kind &hasErr,
570 CheckerContext &C) const {
571 bool IgnoreRetainMsg = (bool)C.getASTContext().getLangOpts().ObjCAutoRefCount;
572 switch (E) {
573 default:
574 break;
575 case IncRefMsg:
576 E = IgnoreRetainMsg ? DoNothing : IncRef;
577 break;
578 case DecRefMsg:
579 E = IgnoreRetainMsg ? DoNothing: DecRef;
580 break;
581 case DecRefMsgAndStopTrackingHard:
582 E = IgnoreRetainMsg ? StopTracking : DecRefAndStopTrackingHard;
583 break;
George Karpenkovbc0cddf2018-08-17 21:42:59 +0000584 case MakeCollectable:
585 E = DoNothing;
George Karpenkov70c2ee32018-08-17 21:41:07 +0000586 }
587
588 // Handle all use-after-releases.
589 if (V.getKind() == RefVal::Released) {
590 V = V ^ RefVal::ErrorUseAfterRelease;
591 hasErr = V.getKind();
592 return setRefBinding(state, sym, V);
593 }
594
595 switch (E) {
596 case DecRefMsg:
597 case IncRefMsg:
George Karpenkovbc0cddf2018-08-17 21:42:59 +0000598 case MakeCollectable:
George Karpenkov70c2ee32018-08-17 21:41:07 +0000599 case DecRefMsgAndStopTrackingHard:
George Karpenkovbc0cddf2018-08-17 21:42:59 +0000600 llvm_unreachable("DecRefMsg/IncRefMsg/MakeCollectable already converted");
George Karpenkov70c2ee32018-08-17 21:41:07 +0000601
602 case UnretainedOutParameter:
603 case RetainedOutParameter:
604 llvm_unreachable("Applies to pointer-to-pointer parameters, which should "
605 "not have ref state.");
606
607 case Dealloc:
608 switch (V.getKind()) {
609 default:
610 llvm_unreachable("Invalid RefVal state for an explicit dealloc.");
611 case RefVal::Owned:
612 // The object immediately transitions to the released state.
613 V = V ^ RefVal::Released;
614 V.clearCounts();
615 return setRefBinding(state, sym, V);
616 case RefVal::NotOwned:
617 V = V ^ RefVal::ErrorDeallocNotOwned;
618 hasErr = V.getKind();
619 break;
620 }
621 break;
622
623 case MayEscape:
624 if (V.getKind() == RefVal::Owned) {
625 V = V ^ RefVal::NotOwned;
626 break;
627 }
628
629 // Fall-through.
630
631 case DoNothing:
632 return state;
633
634 case Autorelease:
635 // Update the autorelease counts.
636 V = V.autorelease();
637 break;
638
639 case StopTracking:
640 case StopTrackingHard:
641 return removeRefBinding(state, sym);
642
643 case IncRef:
644 switch (V.getKind()) {
645 default:
646 llvm_unreachable("Invalid RefVal state for a retain.");
647 case RefVal::Owned:
648 case RefVal::NotOwned:
649 V = V + 1;
650 break;
651 }
652 break;
653
654 case DecRef:
655 case DecRefBridgedTransferred:
656 case DecRefAndStopTrackingHard:
657 switch (V.getKind()) {
658 default:
659 // case 'RefVal::Released' handled above.
660 llvm_unreachable("Invalid RefVal state for a release.");
661
662 case RefVal::Owned:
663 assert(V.getCount() > 0);
664 if (V.getCount() == 1) {
665 if (E == DecRefBridgedTransferred ||
666 V.getIvarAccessHistory() ==
667 RefVal::IvarAccessHistory::AccessedDirectly)
668 V = V ^ RefVal::NotOwned;
669 else
670 V = V ^ RefVal::Released;
671 } else if (E == DecRefAndStopTrackingHard) {
672 return removeRefBinding(state, sym);
673 }
674
675 V = V - 1;
676 break;
677
678 case RefVal::NotOwned:
679 if (V.getCount() > 0) {
680 if (E == DecRefAndStopTrackingHard)
681 return removeRefBinding(state, sym);
682 V = V - 1;
683 } else if (V.getIvarAccessHistory() ==
684 RefVal::IvarAccessHistory::AccessedDirectly) {
685 // Assume that the instance variable was holding on the object at
686 // +1, and we just didn't know.
687 if (E == DecRefAndStopTrackingHard)
688 return removeRefBinding(state, sym);
689 V = V.releaseViaIvar() ^ RefVal::Released;
690 } else {
691 V = V ^ RefVal::ErrorReleaseNotOwned;
692 hasErr = V.getKind();
693 }
694 break;
695 }
696 break;
697 }
698 return setRefBinding(state, sym, V);
699}
700
701void RetainCountChecker::processNonLeakError(ProgramStateRef St,
702 SourceRange ErrorRange,
703 RefVal::Kind ErrorKind,
704 SymbolRef Sym,
705 CheckerContext &C) const {
706 // HACK: Ignore retain-count issues on values accessed through ivars,
707 // because of cases like this:
708 // [_contentView retain];
709 // [_contentView removeFromSuperview];
710 // [self addSubview:_contentView]; // invalidates 'self'
711 // [_contentView release];
712 if (const RefVal *RV = getRefBinding(St, Sym))
713 if (RV->getIvarAccessHistory() != RefVal::IvarAccessHistory::None)
714 return;
715
716 ExplodedNode *N = C.generateErrorNode(St);
717 if (!N)
718 return;
719
720 CFRefBug *BT;
721 switch (ErrorKind) {
722 default:
723 llvm_unreachable("Unhandled error.");
724 case RefVal::ErrorUseAfterRelease:
725 if (!useAfterRelease)
726 useAfterRelease.reset(new UseAfterRelease(this));
727 BT = useAfterRelease.get();
728 break;
729 case RefVal::ErrorReleaseNotOwned:
730 if (!releaseNotOwned)
731 releaseNotOwned.reset(new BadRelease(this));
732 BT = releaseNotOwned.get();
733 break;
734 case RefVal::ErrorDeallocNotOwned:
735 if (!deallocNotOwned)
736 deallocNotOwned.reset(new DeallocNotOwned(this));
737 BT = deallocNotOwned.get();
738 break;
739 }
740
741 assert(BT);
742 auto report = std::unique_ptr<BugReport>(
743 new CFRefReport(*BT, C.getASTContext().getLangOpts(),
744 SummaryLog, N, Sym));
745 report->addRange(ErrorRange);
746 C.emitReport(std::move(report));
747}
748
749//===----------------------------------------------------------------------===//
750// Handle the return values of retain-count-related functions.
751//===----------------------------------------------------------------------===//
752
753bool RetainCountChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
754 // Get the callee. We're only interested in simple C functions.
755 ProgramStateRef state = C.getState();
756 const FunctionDecl *FD = C.getCalleeDecl(CE);
757 if (!FD)
758 return false;
759
George Karpenkovc4d6b932018-08-17 21:42:05 +0000760 RetainSummaryManager &SmrMgr = getSummaryManager(C);
761 QualType ResultTy = CE->getCallReturnType(C.getASTContext());
George Karpenkov70c2ee32018-08-17 21:41:07 +0000762
George Karpenkov70c2ee32018-08-17 21:41:07 +0000763 // See if the function has 'rc_ownership_trusted_implementation'
764 // annotate attribute. If it does, we will not inline it.
765 bool hasTrustedImplementationAnnotation = false;
766
George Karpenkovc4d6b932018-08-17 21:42:05 +0000767 // See if it's one of the specific functions we know how to eval.
George Karpenkovb1b791b2018-08-17 21:43:27 +0000768 if (!SmrMgr.canEval(CE, FD, hasTrustedImplementationAnnotation))
George Karpenkov70c2ee32018-08-17 21:41:07 +0000769 return false;
770
771 // Bind the return value.
772 const LocationContext *LCtx = C.getLocationContext();
773 SVal RetVal = state->getSVal(CE->getArg(0), LCtx);
774 if (RetVal.isUnknown() ||
775 (hasTrustedImplementationAnnotation && !ResultTy.isNull())) {
776 // If the receiver is unknown or the function has
777 // 'rc_ownership_trusted_implementation' annotate attribute, conjure a
778 // return value.
779 SValBuilder &SVB = C.getSValBuilder();
780 RetVal = SVB.conjureSymbolVal(nullptr, CE, LCtx, ResultTy, C.blockCount());
781 }
782 state = state->BindExpr(CE, LCtx, RetVal, false);
783
784 // FIXME: This should not be necessary, but otherwise the argument seems to be
785 // considered alive during the next statement.
786 if (const MemRegion *ArgRegion = RetVal.getAsRegion()) {
787 // Save the refcount status of the argument.
788 SymbolRef Sym = RetVal.getAsLocSymbol();
789 const RefVal *Binding = nullptr;
790 if (Sym)
791 Binding = getRefBinding(state, Sym);
792
793 // Invalidate the argument region.
794 state = state->invalidateRegions(
795 ArgRegion, CE, C.blockCount(), LCtx,
796 /*CausesPointerEscape*/ hasTrustedImplementationAnnotation);
797
798 // Restore the refcount status of the argument.
799 if (Binding)
800 state = setRefBinding(state, Sym, *Binding);
801 }
802
803 C.addTransition(state);
804 return true;
805}
806
807//===----------------------------------------------------------------------===//
808// Handle return statements.
809//===----------------------------------------------------------------------===//
810
811void RetainCountChecker::checkPreStmt(const ReturnStmt *S,
812 CheckerContext &C) const {
813
814 // Only adjust the reference count if this is the top-level call frame,
815 // and not the result of inlining. In the future, we should do
816 // better checking even for inlined calls, and see if they match
817 // with their expected semantics (e.g., the method should return a retained
818 // object, etc.).
819 if (!C.inTopFrame())
820 return;
821
822 const Expr *RetE = S->getRetValue();
823 if (!RetE)
824 return;
825
826 ProgramStateRef state = C.getState();
827 SymbolRef Sym =
828 state->getSValAsScalarOrLoc(RetE, C.getLocationContext()).getAsLocSymbol();
829 if (!Sym)
830 return;
831
832 // Get the reference count binding (if any).
833 const RefVal *T = getRefBinding(state, Sym);
834 if (!T)
835 return;
836
837 // Change the reference count.
838 RefVal X = *T;
839
840 switch (X.getKind()) {
841 case RefVal::Owned: {
842 unsigned cnt = X.getCount();
843 assert(cnt > 0);
844 X.setCount(cnt - 1);
845 X = X ^ RefVal::ReturnedOwned;
846 break;
847 }
848
849 case RefVal::NotOwned: {
850 unsigned cnt = X.getCount();
851 if (cnt) {
852 X.setCount(cnt - 1);
853 X = X ^ RefVal::ReturnedOwned;
854 }
855 else {
856 X = X ^ RefVal::ReturnedNotOwned;
857 }
858 break;
859 }
860
861 default:
862 return;
863 }
864
865 // Update the binding.
866 state = setRefBinding(state, Sym, X);
867 ExplodedNode *Pred = C.addTransition(state);
868
869 // At this point we have updated the state properly.
870 // Everything after this is merely checking to see if the return value has
871 // been over- or under-retained.
872
873 // Did we cache out?
874 if (!Pred)
875 return;
876
877 // Update the autorelease counts.
878 static CheckerProgramPointTag AutoreleaseTag(this, "Autorelease");
879 state = handleAutoreleaseCounts(state, Pred, &AutoreleaseTag, C, Sym, X);
880
881 // Did we cache out?
882 if (!state)
883 return;
884
885 // Get the updated binding.
886 T = getRefBinding(state, Sym);
887 assert(T);
888 X = *T;
889
890 // Consult the summary of the enclosing method.
891 RetainSummaryManager &Summaries = getSummaryManager(C);
892 const Decl *CD = &Pred->getCodeDecl();
893 RetEffect RE = RetEffect::MakeNoRet();
894
895 // FIXME: What is the convention for blocks? Is there one?
896 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CD)) {
897 const RetainSummary *Summ = Summaries.getMethodSummary(MD);
898 RE = Summ->getRetEffect();
899 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CD)) {
900 if (!isa<CXXMethodDecl>(FD)) {
901 const RetainSummary *Summ = Summaries.getFunctionSummary(FD);
902 RE = Summ->getRetEffect();
903 }
904 }
905
906 checkReturnWithRetEffect(S, C, Pred, RE, X, Sym, state);
907}
908
909void RetainCountChecker::checkReturnWithRetEffect(const ReturnStmt *S,
910 CheckerContext &C,
911 ExplodedNode *Pred,
912 RetEffect RE, RefVal X,
913 SymbolRef Sym,
914 ProgramStateRef state) const {
915 // HACK: Ignore retain-count issues on values accessed through ivars,
916 // because of cases like this:
917 // [_contentView retain];
918 // [_contentView removeFromSuperview];
919 // [self addSubview:_contentView]; // invalidates 'self'
920 // [_contentView release];
921 if (X.getIvarAccessHistory() != RefVal::IvarAccessHistory::None)
922 return;
923
924 // Any leaks or other errors?
925 if (X.isReturnedOwned() && X.getCount() == 0) {
926 if (RE.getKind() != RetEffect::NoRet) {
927 bool hasError = false;
928 if (!RE.isOwned()) {
929 // The returning type is a CF, we expect the enclosing method should
930 // return ownership.
931 hasError = true;
932 X = X ^ RefVal::ErrorLeakReturned;
933 }
934
935 if (hasError) {
936 // Generate an error node.
937 state = setRefBinding(state, Sym, X);
938
939 static CheckerProgramPointTag ReturnOwnLeakTag(this, "ReturnsOwnLeak");
940 ExplodedNode *N = C.addTransition(state, Pred, &ReturnOwnLeakTag);
941 if (N) {
942 const LangOptions &LOpts = C.getASTContext().getLangOpts();
943 C.emitReport(std::unique_ptr<BugReport>(new CFRefLeakReport(
944 *getLeakAtReturnBug(LOpts), LOpts,
945 SummaryLog, N, Sym, C, IncludeAllocationLine)));
946 }
947 }
948 }
949 } else if (X.isReturnedNotOwned()) {
950 if (RE.isOwned()) {
951 if (X.getIvarAccessHistory() ==
952 RefVal::IvarAccessHistory::AccessedDirectly) {
953 // Assume the method was trying to transfer a +1 reference from a
954 // strong ivar to the caller.
955 state = setRefBinding(state, Sym,
956 X.releaseViaIvar() ^ RefVal::ReturnedOwned);
957 } else {
958 // Trying to return a not owned object to a caller expecting an
959 // owned object.
960 state = setRefBinding(state, Sym, X ^ RefVal::ErrorReturnedNotOwned);
961
962 static CheckerProgramPointTag
963 ReturnNotOwnedTag(this, "ReturnNotOwnedForOwned");
964
965 ExplodedNode *N = C.addTransition(state, Pred, &ReturnNotOwnedTag);
966 if (N) {
967 if (!returnNotOwnedForOwned)
968 returnNotOwnedForOwned.reset(new ReturnedNotOwnedForOwned(this));
969
970 C.emitReport(std::unique_ptr<BugReport>(new CFRefReport(
971 *returnNotOwnedForOwned, C.getASTContext().getLangOpts(),
972 SummaryLog, N, Sym)));
973 }
974 }
975 }
976 }
977}
978
979//===----------------------------------------------------------------------===//
980// Check various ways a symbol can be invalidated.
981//===----------------------------------------------------------------------===//
982
983void RetainCountChecker::checkBind(SVal loc, SVal val, const Stmt *S,
984 CheckerContext &C) const {
985 // Are we storing to something that causes the value to "escape"?
986 bool escapes = true;
987
988 // A value escapes in three possible cases (this may change):
989 //
990 // (1) we are binding to something that is not a memory region.
991 // (2) we are binding to a memregion that does not have stack storage
992 // (3) we are binding to a memregion with stack storage that the store
993 // does not understand.
994 ProgramStateRef state = C.getState();
995
996 if (Optional<loc::MemRegionVal> regionLoc = loc.getAs<loc::MemRegionVal>()) {
997 escapes = !regionLoc->getRegion()->hasStackStorage();
998
999 if (!escapes) {
1000 // To test (3), generate a new state with the binding added. If it is
1001 // the same state, then it escapes (since the store cannot represent
1002 // the binding).
1003 // Do this only if we know that the store is not supposed to generate the
1004 // same state.
1005 SVal StoredVal = state->getSVal(regionLoc->getRegion());
1006 if (StoredVal != val)
1007 escapes = (state == (state->bindLoc(*regionLoc, val, C.getLocationContext())));
1008 }
1009 if (!escapes) {
1010 // Case 4: We do not currently model what happens when a symbol is
1011 // assigned to a struct field, so be conservative here and let the symbol
1012 // go. TODO: This could definitely be improved upon.
1013 escapes = !isa<VarRegion>(regionLoc->getRegion());
1014 }
1015 }
1016
1017 // If we are storing the value into an auto function scope variable annotated
1018 // with (__attribute__((cleanup))), stop tracking the value to avoid leak
1019 // false positives.
1020 if (const VarRegion *LVR = dyn_cast_or_null<VarRegion>(loc.getAsRegion())) {
1021 const VarDecl *VD = LVR->getDecl();
1022 if (VD->hasAttr<CleanupAttr>()) {
1023 escapes = true;
1024 }
1025 }
1026
1027 // If our store can represent the binding and we aren't storing to something
1028 // that doesn't have local storage then just return and have the simulation
1029 // state continue as is.
1030 if (!escapes)
1031 return;
1032
1033 // Otherwise, find all symbols referenced by 'val' that we are tracking
1034 // and stop tracking them.
1035 state = state->scanReachableSymbols<StopTrackingCallback>(val).getState();
1036 C.addTransition(state);
1037}
1038
1039ProgramStateRef RetainCountChecker::evalAssume(ProgramStateRef state,
1040 SVal Cond,
1041 bool Assumption) const {
1042 // FIXME: We may add to the interface of evalAssume the list of symbols
1043 // whose assumptions have changed. For now we just iterate through the
1044 // bindings and check if any of the tracked symbols are NULL. This isn't
1045 // too bad since the number of symbols we will track in practice are
1046 // probably small and evalAssume is only called at branches and a few
1047 // other places.
1048 RefBindingsTy B = state->get<RefBindings>();
1049
1050 if (B.isEmpty())
1051 return state;
1052
1053 bool changed = false;
1054 RefBindingsTy::Factory &RefBFactory = state->get_context<RefBindings>();
1055
1056 for (RefBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1057 // Check if the symbol is null stop tracking the symbol.
1058 ConstraintManager &CMgr = state->getConstraintManager();
1059 ConditionTruthVal AllocFailed = CMgr.isNull(state, I.getKey());
1060 if (AllocFailed.isConstrainedTrue()) {
1061 changed = true;
1062 B = RefBFactory.remove(B, I.getKey());
1063 }
1064 }
1065
1066 if (changed)
1067 state = state->set<RefBindings>(B);
1068
1069 return state;
1070}
1071
1072ProgramStateRef
1073RetainCountChecker::checkRegionChanges(ProgramStateRef state,
1074 const InvalidatedSymbols *invalidated,
1075 ArrayRef<const MemRegion *> ExplicitRegions,
1076 ArrayRef<const MemRegion *> Regions,
1077 const LocationContext *LCtx,
1078 const CallEvent *Call) const {
1079 if (!invalidated)
1080 return state;
1081
1082 llvm::SmallPtrSet<SymbolRef, 8> WhitelistedSymbols;
1083 for (ArrayRef<const MemRegion *>::iterator I = ExplicitRegions.begin(),
1084 E = ExplicitRegions.end(); I != E; ++I) {
1085 if (const SymbolicRegion *SR = (*I)->StripCasts()->getAs<SymbolicRegion>())
1086 WhitelistedSymbols.insert(SR->getSymbol());
1087 }
1088
1089 for (InvalidatedSymbols::const_iterator I=invalidated->begin(),
1090 E = invalidated->end(); I!=E; ++I) {
1091 SymbolRef sym = *I;
1092 if (WhitelistedSymbols.count(sym))
1093 continue;
1094 // Remove any existing reference-count binding.
1095 state = removeRefBinding(state, sym);
1096 }
1097 return state;
1098}
1099
1100//===----------------------------------------------------------------------===//
1101// Handle dead symbols and end-of-path.
1102//===----------------------------------------------------------------------===//
1103
1104ProgramStateRef
1105RetainCountChecker::handleAutoreleaseCounts(ProgramStateRef state,
1106 ExplodedNode *Pred,
1107 const ProgramPointTag *Tag,
1108 CheckerContext &Ctx,
1109 SymbolRef Sym, RefVal V) const {
1110 unsigned ACnt = V.getAutoreleaseCount();
1111
1112 // No autorelease counts? Nothing to be done.
1113 if (!ACnt)
1114 return state;
1115
1116 unsigned Cnt = V.getCount();
1117
1118 // FIXME: Handle sending 'autorelease' to already released object.
1119
1120 if (V.getKind() == RefVal::ReturnedOwned)
1121 ++Cnt;
1122
1123 // If we would over-release here, but we know the value came from an ivar,
1124 // assume it was a strong ivar that's just been relinquished.
1125 if (ACnt > Cnt &&
1126 V.getIvarAccessHistory() == RefVal::IvarAccessHistory::AccessedDirectly) {
1127 V = V.releaseViaIvar();
1128 --ACnt;
1129 }
1130
1131 if (ACnt <= Cnt) {
1132 if (ACnt == Cnt) {
1133 V.clearCounts();
1134 if (V.getKind() == RefVal::ReturnedOwned)
1135 V = V ^ RefVal::ReturnedNotOwned;
1136 else
1137 V = V ^ RefVal::NotOwned;
1138 } else {
1139 V.setCount(V.getCount() - ACnt);
1140 V.setAutoreleaseCount(0);
1141 }
1142 return setRefBinding(state, Sym, V);
1143 }
1144
1145 // HACK: Ignore retain-count issues on values accessed through ivars,
1146 // because of cases like this:
1147 // [_contentView retain];
1148 // [_contentView removeFromSuperview];
1149 // [self addSubview:_contentView]; // invalidates 'self'
1150 // [_contentView release];
1151 if (V.getIvarAccessHistory() != RefVal::IvarAccessHistory::None)
1152 return state;
1153
1154 // Woah! More autorelease counts then retain counts left.
1155 // Emit hard error.
1156 V = V ^ RefVal::ErrorOverAutorelease;
1157 state = setRefBinding(state, Sym, V);
1158
1159 ExplodedNode *N = Ctx.generateSink(state, Pred, Tag);
1160 if (N) {
1161 SmallString<128> sbuf;
1162 llvm::raw_svector_ostream os(sbuf);
1163 os << "Object was autoreleased ";
1164 if (V.getAutoreleaseCount() > 1)
1165 os << V.getAutoreleaseCount() << " times but the object ";
1166 else
1167 os << "but ";
1168 os << "has a +" << V.getCount() << " retain count";
1169
1170 if (!overAutorelease)
1171 overAutorelease.reset(new OverAutorelease(this));
1172
1173 const LangOptions &LOpts = Ctx.getASTContext().getLangOpts();
1174 Ctx.emitReport(std::unique_ptr<BugReport>(
1175 new CFRefReport(*overAutorelease, LOpts,
1176 SummaryLog, N, Sym, os.str())));
1177 }
1178
1179 return nullptr;
1180}
1181
1182ProgramStateRef
1183RetainCountChecker::handleSymbolDeath(ProgramStateRef state,
1184 SymbolRef sid, RefVal V,
1185 SmallVectorImpl<SymbolRef> &Leaked) const {
1186 bool hasLeak;
1187
1188 // HACK: Ignore retain-count issues on values accessed through ivars,
1189 // because of cases like this:
1190 // [_contentView retain];
1191 // [_contentView removeFromSuperview];
1192 // [self addSubview:_contentView]; // invalidates 'self'
1193 // [_contentView release];
1194 if (V.getIvarAccessHistory() != RefVal::IvarAccessHistory::None)
1195 hasLeak = false;
1196 else if (V.isOwned())
1197 hasLeak = true;
1198 else if (V.isNotOwned() || V.isReturnedOwned())
1199 hasLeak = (V.getCount() > 0);
1200 else
1201 hasLeak = false;
1202
1203 if (!hasLeak)
1204 return removeRefBinding(state, sid);
1205
1206 Leaked.push_back(sid);
1207 return setRefBinding(state, sid, V ^ RefVal::ErrorLeak);
1208}
1209
1210ExplodedNode *
1211RetainCountChecker::processLeaks(ProgramStateRef state,
1212 SmallVectorImpl<SymbolRef> &Leaked,
1213 CheckerContext &Ctx,
1214 ExplodedNode *Pred) const {
1215 // Generate an intermediate node representing the leak point.
1216 ExplodedNode *N = Ctx.addTransition(state, Pred);
1217
1218 if (N) {
1219 for (SmallVectorImpl<SymbolRef>::iterator
1220 I = Leaked.begin(), E = Leaked.end(); I != E; ++I) {
1221
1222 const LangOptions &LOpts = Ctx.getASTContext().getLangOpts();
1223 CFRefBug *BT = Pred ? getLeakWithinFunctionBug(LOpts)
1224 : getLeakAtReturnBug(LOpts);
1225 assert(BT && "BugType not initialized.");
1226
1227 Ctx.emitReport(std::unique_ptr<BugReport>(
1228 new CFRefLeakReport(*BT, LOpts, SummaryLog, N, *I, Ctx,
1229 IncludeAllocationLine)));
1230 }
1231 }
1232
1233 return N;
1234}
1235
George Karpenkovb1b791b2018-08-17 21:43:27 +00001236static bool isISLObjectRef(QualType Ty) {
1237 return StringRef(Ty.getAsString()).startswith("isl_");
George Karpenkov70c2ee32018-08-17 21:41:07 +00001238}
1239
1240void RetainCountChecker::checkBeginFunction(CheckerContext &Ctx) const {
1241 if (!Ctx.inTopFrame())
1242 return;
1243
George Karpenkovc4d6b932018-08-17 21:42:05 +00001244 RetainSummaryManager &SmrMgr = getSummaryManager(Ctx);
George Karpenkov70c2ee32018-08-17 21:41:07 +00001245 const LocationContext *LCtx = Ctx.getLocationContext();
1246 const FunctionDecl *FD = dyn_cast<FunctionDecl>(LCtx->getDecl());
1247
George Karpenkovc4d6b932018-08-17 21:42:05 +00001248 if (!FD || SmrMgr.isTrustedReferenceCountImplementation(FD))
George Karpenkov70c2ee32018-08-17 21:41:07 +00001249 return;
1250
1251 ProgramStateRef state = Ctx.getState();
George Karpenkovc4d6b932018-08-17 21:42:05 +00001252 const RetainSummary *FunctionSummary = SmrMgr.getFunctionSummary(FD);
George Karpenkov70c2ee32018-08-17 21:41:07 +00001253 ArgEffects CalleeSideArgEffects = FunctionSummary->getArgEffects();
1254
1255 for (unsigned idx = 0, e = FD->getNumParams(); idx != e; ++idx) {
1256 const ParmVarDecl *Param = FD->getParamDecl(idx);
1257 SymbolRef Sym = state->getSVal(state->getRegion(Param, LCtx)).getAsSymbol();
1258
1259 QualType Ty = Param->getType();
1260 const ArgEffect *AE = CalleeSideArgEffects.lookup(idx);
George Karpenkovb1b791b2018-08-17 21:43:27 +00001261 if (AE && *AE == DecRef && isISLObjectRef(Ty)) {
George Karpenkov70c2ee32018-08-17 21:41:07 +00001262 state = setRefBinding(state, Sym, RefVal::makeOwned(RetEffect::ObjKind::Generalized, Ty));
George Karpenkovb1b791b2018-08-17 21:43:27 +00001263 } else if (isISLObjectRef(Ty)) {
George Karpenkov70c2ee32018-08-17 21:41:07 +00001264 state = setRefBinding(
1265 state, Sym,
1266 RefVal::makeNotOwned(RetEffect::ObjKind::Generalized, Ty));
1267 }
1268 }
1269
1270 Ctx.addTransition(state);
1271}
1272
1273void RetainCountChecker::checkEndFunction(const ReturnStmt *RS,
1274 CheckerContext &Ctx) const {
1275 ProgramStateRef state = Ctx.getState();
1276 RefBindingsTy B = state->get<RefBindings>();
1277 ExplodedNode *Pred = Ctx.getPredecessor();
1278
1279 // Don't process anything within synthesized bodies.
1280 const LocationContext *LCtx = Pred->getLocationContext();
1281 if (LCtx->getAnalysisDeclContext()->isBodyAutosynthesized()) {
1282 assert(!LCtx->inTopFrame());
1283 return;
1284 }
1285
1286 for (RefBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1287 state = handleAutoreleaseCounts(state, Pred, /*Tag=*/nullptr, Ctx,
1288 I->first, I->second);
1289 if (!state)
1290 return;
1291 }
1292
1293 // If the current LocationContext has a parent, don't check for leaks.
1294 // We will do that later.
1295 // FIXME: we should instead check for imbalances of the retain/releases,
1296 // and suggest annotations.
1297 if (LCtx->getParent())
1298 return;
1299
1300 B = state->get<RefBindings>();
1301 SmallVector<SymbolRef, 10> Leaked;
1302
1303 for (RefBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I)
1304 state = handleSymbolDeath(state, I->first, I->second, Leaked);
1305
1306 processLeaks(state, Leaked, Ctx, Pred);
1307}
1308
1309const ProgramPointTag *
1310RetainCountChecker::getDeadSymbolTag(SymbolRef sym) const {
1311 const CheckerProgramPointTag *&tag = DeadSymbolTags[sym];
1312 if (!tag) {
1313 SmallString<64> buf;
1314 llvm::raw_svector_ostream out(buf);
1315 out << "Dead Symbol : ";
1316 sym->dumpToStream(out);
1317 tag = new CheckerProgramPointTag(this, out.str());
1318 }
1319 return tag;
1320}
1321
1322void RetainCountChecker::checkDeadSymbols(SymbolReaper &SymReaper,
1323 CheckerContext &C) const {
1324 ExplodedNode *Pred = C.getPredecessor();
1325
1326 ProgramStateRef state = C.getState();
1327 RefBindingsTy B = state->get<RefBindings>();
1328 SmallVector<SymbolRef, 10> Leaked;
1329
1330 // Update counts from autorelease pools
1331 for (SymbolReaper::dead_iterator I = SymReaper.dead_begin(),
1332 E = SymReaper.dead_end(); I != E; ++I) {
1333 SymbolRef Sym = *I;
1334 if (const RefVal *T = B.lookup(Sym)){
1335 // Use the symbol as the tag.
1336 // FIXME: This might not be as unique as we would like.
1337 const ProgramPointTag *Tag = getDeadSymbolTag(Sym);
1338 state = handleAutoreleaseCounts(state, Pred, Tag, C, Sym, *T);
1339 if (!state)
1340 return;
1341
1342 // Fetch the new reference count from the state, and use it to handle
1343 // this symbol.
1344 state = handleSymbolDeath(state, *I, *getRefBinding(state, Sym), Leaked);
1345 }
1346 }
1347
1348 if (Leaked.empty()) {
1349 C.addTransition(state);
1350 return;
1351 }
1352
1353 Pred = processLeaks(state, Leaked, C, Pred);
1354
1355 // Did we cache out?
1356 if (!Pred)
1357 return;
1358
1359 // Now generate a new node that nukes the old bindings.
1360 // The only bindings left at this point are the leaked symbols.
1361 RefBindingsTy::Factory &F = state->get_context<RefBindings>();
1362 B = state->get<RefBindings>();
1363
1364 for (SmallVectorImpl<SymbolRef>::iterator I = Leaked.begin(),
1365 E = Leaked.end();
1366 I != E; ++I)
1367 B = F.remove(B, *I);
1368
1369 state = state->set<RefBindings>(B);
1370 C.addTransition(state, Pred);
1371}
1372
1373void RetainCountChecker::printState(raw_ostream &Out, ProgramStateRef State,
1374 const char *NL, const char *Sep) const {
1375
1376 RefBindingsTy B = State->get<RefBindings>();
1377
1378 if (B.isEmpty())
1379 return;
1380
1381 Out << Sep << NL;
1382
1383 for (RefBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1384 Out << I->first << " : ";
1385 I->second.print(Out);
1386 Out << NL;
1387 }
1388}
1389
1390//===----------------------------------------------------------------------===//
1391// Implementation of the CallEffects API.
1392//===----------------------------------------------------------------------===//
1393
1394namespace clang {
1395namespace ento {
1396namespace objc_retain {
1397
1398// This is a bit gross, but it allows us to populate CallEffects without
1399// creating a bunch of accessors. This kind is very localized, so the
1400// damage of this macro is limited.
1401#define createCallEffect(D, KIND)\
1402 ASTContext &Ctx = D->getASTContext();\
1403 LangOptions L = Ctx.getLangOpts();\
1404 RetainSummaryManager M(Ctx, L.ObjCAutoRefCount);\
1405 const RetainSummary *S = M.get ## KIND ## Summary(D);\
1406 CallEffects CE(S->getRetEffect());\
1407 CE.Receiver = S->getReceiverEffect();\
1408 unsigned N = D->param_size();\
1409 for (unsigned i = 0; i < N; ++i) {\
1410 CE.Args.push_back(S->getArg(i));\
1411 }
1412
1413CallEffects CallEffects::getEffect(const ObjCMethodDecl *MD) {
1414 createCallEffect(MD, Method);
1415 return CE;
1416}
1417
1418CallEffects CallEffects::getEffect(const FunctionDecl *FD) {
1419 createCallEffect(FD, Function);
1420 return CE;
1421}
1422
1423#undef createCallEffect
1424
1425} // end namespace objc_retain
1426} // end namespace ento
1427} // end namespace clang
1428
1429//===----------------------------------------------------------------------===//
1430// Checker registration.
1431//===----------------------------------------------------------------------===//
1432
1433void ento::registerRetainCountChecker(CheckerManager &Mgr) {
1434 Mgr.registerChecker<RetainCountChecker>(Mgr.getAnalyzerOptions());
1435}