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