blob: de113bbd057e242f4c1ee206e42327691fdcc731 [file] [log] [blame]
George Karpenkov70c2ee32018-08-17 21:41:07 +00001//==-- RetainCountChecker.cpp - Checks for leaks and other issues -*- C++ -*--//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the methods for RetainCountChecker, which implements
11// a reference count checker for Core Foundation and Cocoa on (Mac OS X).
12//
13//===----------------------------------------------------------------------===//
14
15#include "RetainCountChecker.h"
16
17using namespace clang;
18using namespace ento;
George Karpenkov70c2ee32018-08-17 21:41:07 +000019using namespace retaincountchecker;
20using llvm::StrInStrNoCase;
21
22REGISTER_MAP_WITH_PROGRAMSTATE(RefBindings, SymbolRef, RefVal)
23
24namespace clang {
25namespace ento {
26namespace retaincountchecker {
27
28const RefVal *getRefBinding(ProgramStateRef State, SymbolRef Sym) {
29 return State->get<RefBindings>(Sym);
30}
31
32ProgramStateRef setRefBinding(ProgramStateRef State, SymbolRef Sym,
33 RefVal Val) {
George Karpenkovd5ef0d22018-08-29 20:28:33 +000034 assert(Sym != nullptr);
George Karpenkov70c2ee32018-08-17 21:41:07 +000035 return State->set<RefBindings>(Sym, Val);
36}
37
38ProgramStateRef removeRefBinding(ProgramStateRef State, SymbolRef Sym) {
39 return State->remove<RefBindings>(Sym);
40}
41
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);
George Karpenkovefef49c2018-08-21 03:09:02 +0000334
335 // Leave null if no receiver.
336 QualType ReceiverType;
337 if (const auto *MC = dyn_cast<ObjCMethodCall>(&Call)) {
338 if (MC->isInstanceMessage()) {
339 SVal ReceiverV = MC->getReceiverSVal();
340 if (SymbolRef Sym = ReceiverV.getAsLocSymbol())
341 if (const RefVal *T = getRefBinding(C.getState(), Sym))
342 ReceiverType = T->getType();
343 }
344 }
345
346 const RetainSummary *Summ = Summaries.getSummary(Call, ReceiverType);
George Karpenkov70c2ee32018-08-17 21:41:07 +0000347
348 if (C.wasInlined) {
349 processSummaryOfInlined(*Summ, Call, C);
350 return;
351 }
352 checkSummary(*Summ, Call, C);
353}
354
355/// GetReturnType - Used to get the return type of a message expression or
356/// function call with the intention of affixing that type to a tracked symbol.
357/// While the return type can be queried directly from RetEx, when
358/// invoking class methods we augment to the return type to be that of
359/// a pointer to the class (as opposed it just being id).
360// FIXME: We may be able to do this with related result types instead.
361// This function is probably overestimating.
362static QualType GetReturnType(const Expr *RetE, ASTContext &Ctx) {
363 QualType RetTy = RetE->getType();
364 // If RetE is not a message expression just return its type.
365 // If RetE is a message expression, return its types if it is something
366 /// more specific than id.
367 if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(RetE))
368 if (const ObjCObjectPointerType *PT = RetTy->getAs<ObjCObjectPointerType>())
369 if (PT->isObjCQualifiedIdType() || PT->isObjCIdType() ||
370 PT->isObjCClassType()) {
371 // At this point we know the return type of the message expression is
372 // id, id<...>, or Class. If we have an ObjCInterfaceDecl, we know this
373 // is a call to a class method whose type we can resolve. In such
374 // cases, promote the return type to XXX* (where XXX is the class).
375 const ObjCInterfaceDecl *D = ME->getReceiverInterface();
376 return !D ? RetTy :
377 Ctx.getObjCObjectPointerType(Ctx.getObjCInterfaceType(D));
378 }
379
380 return RetTy;
381}
382
George Karpenkov80c9e782018-08-22 01:16:49 +0000383static Optional<RefVal> refValFromRetEffect(RetEffect RE,
384 QualType ResultTy) {
385 if (RE.isOwned()) {
386 return RefVal::makeOwned(RE.getObjKind(), ResultTy);
387 } else if (RE.notOwned()) {
388 return RefVal::makeNotOwned(RE.getObjKind(), ResultTy);
389 }
390
391 return None;
392}
393
George Karpenkov70c2ee32018-08-17 21:41:07 +0000394// We don't always get the exact modeling of the function with regards to the
395// retain count checker even when the function is inlined. For example, we need
396// to stop tracking the symbols which were marked with StopTrackingHard.
397void RetainCountChecker::processSummaryOfInlined(const RetainSummary &Summ,
398 const CallEvent &CallOrMsg,
399 CheckerContext &C) const {
400 ProgramStateRef state = C.getState();
401
402 // Evaluate the effect of the arguments.
403 for (unsigned idx = 0, e = CallOrMsg.getNumArgs(); idx != e; ++idx) {
404 if (Summ.getArg(idx) == StopTrackingHard) {
405 SVal V = CallOrMsg.getArgSVal(idx);
406 if (SymbolRef Sym = V.getAsLocSymbol()) {
407 state = removeRefBinding(state, Sym);
408 }
409 }
410 }
411
412 // Evaluate the effect on the message receiver.
George Karpenkov6e9fd132018-08-22 01:17:09 +0000413 if (const auto *MsgInvocation = dyn_cast<ObjCMethodCall>(&CallOrMsg)) {
George Karpenkov70c2ee32018-08-17 21:41:07 +0000414 if (SymbolRef Sym = MsgInvocation->getReceiverSVal().getAsLocSymbol()) {
415 if (Summ.getReceiverEffect() == StopTrackingHard) {
416 state = removeRefBinding(state, Sym);
417 }
418 }
419 }
420
421 // Consult the summary for the return value.
422 RetEffect RE = Summ.getRetEffect();
George Karpenkov70c2ee32018-08-17 21:41:07 +0000423
George Karpenkovd5ef0d22018-08-29 20:28:33 +0000424 if (SymbolRef Sym = CallOrMsg.getReturnValue().getAsSymbol()) {
425 if (const auto *MCall = dyn_cast<CXXMemberCall>(&CallOrMsg)) {
426 if (Optional<RefVal> updatedRefVal =
427 refValFromRetEffect(RE, MCall->getResultType())) {
428 state = setRefBinding(state, Sym, *updatedRefVal);
429 }
430 }
431
432 if (RE.getKind() == RetEffect::NoRetHard)
433 state = removeRefBinding(state, Sym);
434 }
George Karpenkovab0011e2018-08-23 00:26:59 +0000435
George Karpenkov70c2ee32018-08-17 21:41:07 +0000436 C.addTransition(state);
437}
438
439static ProgramStateRef updateOutParameter(ProgramStateRef State,
440 SVal ArgVal,
441 ArgEffect Effect) {
442 auto *ArgRegion = dyn_cast_or_null<TypedValueRegion>(ArgVal.getAsRegion());
443 if (!ArgRegion)
444 return State;
445
446 QualType PointeeTy = ArgRegion->getValueType();
447 if (!coreFoundation::isCFObjectRef(PointeeTy))
448 return State;
449
450 SVal PointeeVal = State->getSVal(ArgRegion);
451 SymbolRef Pointee = PointeeVal.getAsLocSymbol();
452 if (!Pointee)
453 return State;
454
455 switch (Effect) {
456 case UnretainedOutParameter:
457 State = setRefBinding(State, Pointee,
458 RefVal::makeNotOwned(RetEffect::CF, PointeeTy));
459 break;
460 case RetainedOutParameter:
461 // Do nothing. Retained out parameters will either point to a +1 reference
462 // or NULL, but the way you check for failure differs depending on the API.
463 // Consequently, we don't have a good way to track them yet.
464 break;
465
466 default:
467 llvm_unreachable("only for out parameters");
468 }
469
470 return State;
471}
472
473void RetainCountChecker::checkSummary(const RetainSummary &Summ,
474 const CallEvent &CallOrMsg,
475 CheckerContext &C) const {
476 ProgramStateRef state = C.getState();
477
478 // Evaluate the effect of the arguments.
479 RefVal::Kind hasErr = (RefVal::Kind) 0;
480 SourceRange ErrorRange;
481 SymbolRef ErrorSym = nullptr;
482
483 for (unsigned idx = 0, e = CallOrMsg.getNumArgs(); idx != e; ++idx) {
484 SVal V = CallOrMsg.getArgSVal(idx);
485
486 ArgEffect Effect = Summ.getArg(idx);
487 if (Effect == RetainedOutParameter || Effect == UnretainedOutParameter) {
488 state = updateOutParameter(state, V, Effect);
489 } else if (SymbolRef Sym = V.getAsLocSymbol()) {
490 if (const RefVal *T = getRefBinding(state, Sym)) {
491 state = updateSymbol(state, Sym, *T, Effect, hasErr, C);
492 if (hasErr) {
493 ErrorRange = CallOrMsg.getArgSourceRange(idx);
494 ErrorSym = Sym;
495 break;
496 }
497 }
498 }
499 }
500
George Karpenkovab0011e2018-08-23 00:26:59 +0000501 // Evaluate the effect on the message receiver / `this` argument.
George Karpenkov70c2ee32018-08-17 21:41:07 +0000502 bool ReceiverIsTracked = false;
503 if (!hasErr) {
George Karpenkovab0011e2018-08-23 00:26:59 +0000504 if (const auto *MsgInvocation = dyn_cast<ObjCMethodCall>(&CallOrMsg)) {
George Karpenkov70c2ee32018-08-17 21:41:07 +0000505 if (SymbolRef Sym = MsgInvocation->getReceiverSVal().getAsLocSymbol()) {
506 if (const RefVal *T = getRefBinding(state, Sym)) {
507 ReceiverIsTracked = true;
508 state = updateSymbol(state, Sym, *T, Summ.getReceiverEffect(),
509 hasErr, C);
510 if (hasErr) {
511 ErrorRange = MsgInvocation->getOriginExpr()->getReceiverRange();
512 ErrorSym = Sym;
513 }
514 }
515 }
George Karpenkovab0011e2018-08-23 00:26:59 +0000516 } else if (const auto *MCall = dyn_cast<CXXMemberCall>(&CallOrMsg)) {
517 if (SymbolRef Sym = MCall->getCXXThisVal().getAsLocSymbol()) {
518 if (const RefVal *T = getRefBinding(state, Sym)) {
519 state = updateSymbol(state, Sym, *T, Summ.getThisEffect(),
520 hasErr, C);
521 if (hasErr) {
522 ErrorRange = MCall->getOriginExpr()->getSourceRange();
523 ErrorSym = Sym;
524 }
525 }
526 }
George Karpenkov70c2ee32018-08-17 21:41:07 +0000527 }
528 }
529
530 // Process any errors.
531 if (hasErr) {
532 processNonLeakError(state, ErrorRange, hasErr, ErrorSym, C);
533 return;
534 }
535
536 // Consult the summary for the return value.
537 RetEffect RE = Summ.getRetEffect();
538
539 if (RE.getKind() == RetEffect::OwnedWhenTrackedReceiver) {
540 if (ReceiverIsTracked)
541 RE = getSummaryManager(C).getObjAllocRetEffect();
542 else
543 RE = RetEffect::MakeNoRet();
544 }
545
George Karpenkov80c9e782018-08-22 01:16:49 +0000546 if (SymbolRef Sym = CallOrMsg.getReturnValue().getAsSymbol()) {
547 QualType ResultTy = CallOrMsg.getResultType();
548 if (RE.notOwned()) {
George Karpenkov70c2ee32018-08-17 21:41:07 +0000549 const Expr *Ex = CallOrMsg.getOriginExpr();
George Karpenkov70c2ee32018-08-17 21:41:07 +0000550 assert(Ex);
George Karpenkov80c9e782018-08-22 01:16:49 +0000551 ResultTy = GetReturnType(Ex, C.getASTContext());
George Karpenkov70c2ee32018-08-17 21:41:07 +0000552 }
George Karpenkov80c9e782018-08-22 01:16:49 +0000553 if (Optional<RefVal> updatedRefVal = refValFromRetEffect(RE, ResultTy))
554 state = setRefBinding(state, Sym, *updatedRefVal);
George Karpenkov70c2ee32018-08-17 21:41:07 +0000555 }
556
557 // This check is actually necessary; otherwise the statement builder thinks
558 // we've hit a previously-found path.
559 // Normally addTransition takes care of this, but we want the node pointer.
560 ExplodedNode *NewNode;
561 if (state == C.getState()) {
562 NewNode = C.getPredecessor();
563 } else {
564 NewNode = C.addTransition(state);
565 }
566
567 // Annotate the node with summary we used.
568 if (NewNode) {
569 // FIXME: This is ugly. See checkEndAnalysis for why it's necessary.
570 if (ShouldResetSummaryLog) {
571 SummaryLog.clear();
572 ShouldResetSummaryLog = false;
573 }
574 SummaryLog[NewNode] = &Summ;
575 }
576}
577
578ProgramStateRef
579RetainCountChecker::updateSymbol(ProgramStateRef state, SymbolRef sym,
580 RefVal V, ArgEffect E, RefVal::Kind &hasErr,
581 CheckerContext &C) const {
582 bool IgnoreRetainMsg = (bool)C.getASTContext().getLangOpts().ObjCAutoRefCount;
583 switch (E) {
584 default:
585 break;
586 case IncRefMsg:
587 E = IgnoreRetainMsg ? DoNothing : IncRef;
588 break;
589 case DecRefMsg:
590 E = IgnoreRetainMsg ? DoNothing: DecRef;
591 break;
592 case DecRefMsgAndStopTrackingHard:
593 E = IgnoreRetainMsg ? StopTracking : DecRefAndStopTrackingHard;
594 break;
George Karpenkovbc0cddf2018-08-17 21:42:59 +0000595 case MakeCollectable:
596 E = DoNothing;
George Karpenkov70c2ee32018-08-17 21:41:07 +0000597 }
598
599 // Handle all use-after-releases.
600 if (V.getKind() == RefVal::Released) {
601 V = V ^ RefVal::ErrorUseAfterRelease;
602 hasErr = V.getKind();
603 return setRefBinding(state, sym, V);
604 }
605
606 switch (E) {
607 case DecRefMsg:
608 case IncRefMsg:
George Karpenkovbc0cddf2018-08-17 21:42:59 +0000609 case MakeCollectable:
George Karpenkov70c2ee32018-08-17 21:41:07 +0000610 case DecRefMsgAndStopTrackingHard:
George Karpenkovbc0cddf2018-08-17 21:42:59 +0000611 llvm_unreachable("DecRefMsg/IncRefMsg/MakeCollectable already converted");
George Karpenkov70c2ee32018-08-17 21:41:07 +0000612
613 case UnretainedOutParameter:
614 case RetainedOutParameter:
615 llvm_unreachable("Applies to pointer-to-pointer parameters, which should "
616 "not have ref state.");
617
618 case Dealloc:
619 switch (V.getKind()) {
620 default:
621 llvm_unreachable("Invalid RefVal state for an explicit dealloc.");
622 case RefVal::Owned:
623 // The object immediately transitions to the released state.
624 V = V ^ RefVal::Released;
625 V.clearCounts();
626 return setRefBinding(state, sym, V);
627 case RefVal::NotOwned:
628 V = V ^ RefVal::ErrorDeallocNotOwned;
629 hasErr = V.getKind();
630 break;
631 }
632 break;
633
634 case MayEscape:
635 if (V.getKind() == RefVal::Owned) {
636 V = V ^ RefVal::NotOwned;
637 break;
638 }
639
640 // Fall-through.
641
642 case DoNothing:
643 return state;
644
645 case Autorelease:
646 // Update the autorelease counts.
647 V = V.autorelease();
648 break;
649
650 case StopTracking:
651 case StopTrackingHard:
652 return removeRefBinding(state, sym);
653
654 case IncRef:
655 switch (V.getKind()) {
656 default:
657 llvm_unreachable("Invalid RefVal state for a retain.");
658 case RefVal::Owned:
659 case RefVal::NotOwned:
660 V = V + 1;
661 break;
662 }
663 break;
664
665 case DecRef:
666 case DecRefBridgedTransferred:
667 case DecRefAndStopTrackingHard:
668 switch (V.getKind()) {
669 default:
670 // case 'RefVal::Released' handled above.
671 llvm_unreachable("Invalid RefVal state for a release.");
672
673 case RefVal::Owned:
674 assert(V.getCount() > 0);
675 if (V.getCount() == 1) {
676 if (E == DecRefBridgedTransferred ||
677 V.getIvarAccessHistory() ==
678 RefVal::IvarAccessHistory::AccessedDirectly)
679 V = V ^ RefVal::NotOwned;
680 else
681 V = V ^ RefVal::Released;
682 } else if (E == DecRefAndStopTrackingHard) {
683 return removeRefBinding(state, sym);
684 }
685
686 V = V - 1;
687 break;
688
689 case RefVal::NotOwned:
690 if (V.getCount() > 0) {
691 if (E == DecRefAndStopTrackingHard)
692 return removeRefBinding(state, sym);
693 V = V - 1;
694 } else if (V.getIvarAccessHistory() ==
695 RefVal::IvarAccessHistory::AccessedDirectly) {
696 // Assume that the instance variable was holding on the object at
697 // +1, and we just didn't know.
698 if (E == DecRefAndStopTrackingHard)
699 return removeRefBinding(state, sym);
700 V = V.releaseViaIvar() ^ RefVal::Released;
701 } else {
702 V = V ^ RefVal::ErrorReleaseNotOwned;
703 hasErr = V.getKind();
704 }
705 break;
706 }
707 break;
708 }
709 return setRefBinding(state, sym, V);
710}
711
712void RetainCountChecker::processNonLeakError(ProgramStateRef St,
713 SourceRange ErrorRange,
714 RefVal::Kind ErrorKind,
715 SymbolRef Sym,
716 CheckerContext &C) const {
717 // HACK: Ignore retain-count issues on values accessed through ivars,
718 // because of cases like this:
719 // [_contentView retain];
720 // [_contentView removeFromSuperview];
721 // [self addSubview:_contentView]; // invalidates 'self'
722 // [_contentView release];
723 if (const RefVal *RV = getRefBinding(St, Sym))
724 if (RV->getIvarAccessHistory() != RefVal::IvarAccessHistory::None)
725 return;
726
727 ExplodedNode *N = C.generateErrorNode(St);
728 if (!N)
729 return;
730
731 CFRefBug *BT;
732 switch (ErrorKind) {
733 default:
734 llvm_unreachable("Unhandled error.");
735 case RefVal::ErrorUseAfterRelease:
736 if (!useAfterRelease)
737 useAfterRelease.reset(new UseAfterRelease(this));
738 BT = useAfterRelease.get();
739 break;
740 case RefVal::ErrorReleaseNotOwned:
741 if (!releaseNotOwned)
742 releaseNotOwned.reset(new BadRelease(this));
743 BT = releaseNotOwned.get();
744 break;
745 case RefVal::ErrorDeallocNotOwned:
746 if (!deallocNotOwned)
747 deallocNotOwned.reset(new DeallocNotOwned(this));
748 BT = deallocNotOwned.get();
749 break;
750 }
751
752 assert(BT);
George Karpenkov6babf2a2018-09-21 20:36:21 +0000753 auto report = llvm::make_unique<CFRefReport>(
754 *BT, C.getASTContext().getLangOpts(), SummaryLog, N, Sym);
George Karpenkov70c2ee32018-08-17 21:41:07 +0000755 report->addRange(ErrorRange);
756 C.emitReport(std::move(report));
757}
758
759//===----------------------------------------------------------------------===//
760// Handle the return values of retain-count-related functions.
761//===----------------------------------------------------------------------===//
762
763bool RetainCountChecker::evalCall(const CallExpr *CE, CheckerContext &C) const {
764 // Get the callee. We're only interested in simple C functions.
765 ProgramStateRef state = C.getState();
766 const FunctionDecl *FD = C.getCalleeDecl(CE);
767 if (!FD)
768 return false;
769
George Karpenkovc4d6b932018-08-17 21:42:05 +0000770 RetainSummaryManager &SmrMgr = getSummaryManager(C);
771 QualType ResultTy = CE->getCallReturnType(C.getASTContext());
George Karpenkov70c2ee32018-08-17 21:41:07 +0000772
George Karpenkov70c2ee32018-08-17 21:41:07 +0000773 // See if the function has 'rc_ownership_trusted_implementation'
774 // annotate attribute. If it does, we will not inline it.
775 bool hasTrustedImplementationAnnotation = false;
776
George Karpenkovc4d6b932018-08-17 21:42:05 +0000777 // See if it's one of the specific functions we know how to eval.
George Karpenkovb1b791b2018-08-17 21:43:27 +0000778 if (!SmrMgr.canEval(CE, FD, hasTrustedImplementationAnnotation))
George Karpenkov70c2ee32018-08-17 21:41:07 +0000779 return false;
780
781 // Bind the return value.
782 const LocationContext *LCtx = C.getLocationContext();
783 SVal RetVal = state->getSVal(CE->getArg(0), LCtx);
784 if (RetVal.isUnknown() ||
785 (hasTrustedImplementationAnnotation && !ResultTy.isNull())) {
786 // If the receiver is unknown or the function has
787 // 'rc_ownership_trusted_implementation' annotate attribute, conjure a
788 // return value.
789 SValBuilder &SVB = C.getSValBuilder();
790 RetVal = SVB.conjureSymbolVal(nullptr, CE, LCtx, ResultTy, C.blockCount());
791 }
792 state = state->BindExpr(CE, LCtx, RetVal, false);
793
794 // FIXME: This should not be necessary, but otherwise the argument seems to be
795 // considered alive during the next statement.
796 if (const MemRegion *ArgRegion = RetVal.getAsRegion()) {
797 // Save the refcount status of the argument.
798 SymbolRef Sym = RetVal.getAsLocSymbol();
799 const RefVal *Binding = nullptr;
800 if (Sym)
801 Binding = getRefBinding(state, Sym);
802
803 // Invalidate the argument region.
804 state = state->invalidateRegions(
805 ArgRegion, CE, C.blockCount(), LCtx,
806 /*CausesPointerEscape*/ hasTrustedImplementationAnnotation);
807
808 // Restore the refcount status of the argument.
809 if (Binding)
810 state = setRefBinding(state, Sym, *Binding);
811 }
812
813 C.addTransition(state);
814 return true;
815}
816
817//===----------------------------------------------------------------------===//
818// Handle return statements.
819//===----------------------------------------------------------------------===//
820
821void RetainCountChecker::checkPreStmt(const ReturnStmt *S,
822 CheckerContext &C) const {
823
824 // Only adjust the reference count if this is the top-level call frame,
825 // and not the result of inlining. In the future, we should do
826 // better checking even for inlined calls, and see if they match
827 // with their expected semantics (e.g., the method should return a retained
828 // object, etc.).
829 if (!C.inTopFrame())
830 return;
831
832 const Expr *RetE = S->getRetValue();
833 if (!RetE)
834 return;
835
836 ProgramStateRef state = C.getState();
837 SymbolRef Sym =
838 state->getSValAsScalarOrLoc(RetE, C.getLocationContext()).getAsLocSymbol();
839 if (!Sym)
840 return;
841
842 // Get the reference count binding (if any).
843 const RefVal *T = getRefBinding(state, Sym);
844 if (!T)
845 return;
846
847 // Change the reference count.
848 RefVal X = *T;
849
850 switch (X.getKind()) {
851 case RefVal::Owned: {
852 unsigned cnt = X.getCount();
853 assert(cnt > 0);
854 X.setCount(cnt - 1);
855 X = X ^ RefVal::ReturnedOwned;
856 break;
857 }
858
859 case RefVal::NotOwned: {
860 unsigned cnt = X.getCount();
861 if (cnt) {
862 X.setCount(cnt - 1);
863 X = X ^ RefVal::ReturnedOwned;
864 }
865 else {
866 X = X ^ RefVal::ReturnedNotOwned;
867 }
868 break;
869 }
870
871 default:
872 return;
873 }
874
875 // Update the binding.
876 state = setRefBinding(state, Sym, X);
877 ExplodedNode *Pred = C.addTransition(state);
878
879 // At this point we have updated the state properly.
880 // Everything after this is merely checking to see if the return value has
881 // been over- or under-retained.
882
883 // Did we cache out?
884 if (!Pred)
885 return;
886
887 // Update the autorelease counts.
888 static CheckerProgramPointTag AutoreleaseTag(this, "Autorelease");
889 state = handleAutoreleaseCounts(state, Pred, &AutoreleaseTag, C, Sym, X);
890
891 // Did we cache out?
892 if (!state)
893 return;
894
895 // Get the updated binding.
896 T = getRefBinding(state, Sym);
897 assert(T);
898 X = *T;
899
900 // Consult the summary of the enclosing method.
901 RetainSummaryManager &Summaries = getSummaryManager(C);
902 const Decl *CD = &Pred->getCodeDecl();
903 RetEffect RE = RetEffect::MakeNoRet();
904
905 // FIXME: What is the convention for blocks? Is there one?
906 if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CD)) {
907 const RetainSummary *Summ = Summaries.getMethodSummary(MD);
908 RE = Summ->getRetEffect();
909 } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CD)) {
910 if (!isa<CXXMethodDecl>(FD)) {
911 const RetainSummary *Summ = Summaries.getFunctionSummary(FD);
912 RE = Summ->getRetEffect();
913 }
914 }
915
916 checkReturnWithRetEffect(S, C, Pred, RE, X, Sym, state);
917}
918
919void RetainCountChecker::checkReturnWithRetEffect(const ReturnStmt *S,
920 CheckerContext &C,
921 ExplodedNode *Pred,
922 RetEffect RE, RefVal X,
923 SymbolRef Sym,
924 ProgramStateRef state) const {
925 // HACK: Ignore retain-count issues on values accessed through ivars,
926 // because of cases like this:
927 // [_contentView retain];
928 // [_contentView removeFromSuperview];
929 // [self addSubview:_contentView]; // invalidates 'self'
930 // [_contentView release];
931 if (X.getIvarAccessHistory() != RefVal::IvarAccessHistory::None)
932 return;
933
934 // Any leaks or other errors?
935 if (X.isReturnedOwned() && X.getCount() == 0) {
936 if (RE.getKind() != RetEffect::NoRet) {
937 bool hasError = false;
938 if (!RE.isOwned()) {
939 // The returning type is a CF, we expect the enclosing method should
940 // return ownership.
941 hasError = true;
942 X = X ^ RefVal::ErrorLeakReturned;
943 }
944
945 if (hasError) {
946 // Generate an error node.
947 state = setRefBinding(state, Sym, X);
948
949 static CheckerProgramPointTag ReturnOwnLeakTag(this, "ReturnsOwnLeak");
950 ExplodedNode *N = C.addTransition(state, Pred, &ReturnOwnLeakTag);
951 if (N) {
952 const LangOptions &LOpts = C.getASTContext().getLangOpts();
George Karpenkov6babf2a2018-09-21 20:36:21 +0000953 C.emitReport(llvm::make_unique<CFRefLeakReport>(
954 *getLeakAtReturnBug(LOpts), LOpts, SummaryLog, N, Sym, C,
955 IncludeAllocationLine));
George Karpenkov70c2ee32018-08-17 21:41:07 +0000956 }
957 }
958 }
959 } else if (X.isReturnedNotOwned()) {
960 if (RE.isOwned()) {
961 if (X.getIvarAccessHistory() ==
962 RefVal::IvarAccessHistory::AccessedDirectly) {
963 // Assume the method was trying to transfer a +1 reference from a
964 // strong ivar to the caller.
965 state = setRefBinding(state, Sym,
966 X.releaseViaIvar() ^ RefVal::ReturnedOwned);
967 } else {
968 // Trying to return a not owned object to a caller expecting an
969 // owned object.
970 state = setRefBinding(state, Sym, X ^ RefVal::ErrorReturnedNotOwned);
971
972 static CheckerProgramPointTag
973 ReturnNotOwnedTag(this, "ReturnNotOwnedForOwned");
974
975 ExplodedNode *N = C.addTransition(state, Pred, &ReturnNotOwnedTag);
976 if (N) {
977 if (!returnNotOwnedForOwned)
978 returnNotOwnedForOwned.reset(new ReturnedNotOwnedForOwned(this));
979
George Karpenkov6babf2a2018-09-21 20:36:21 +0000980 C.emitReport(llvm::make_unique<CFRefReport>(
George Karpenkov70c2ee32018-08-17 21:41:07 +0000981 *returnNotOwnedForOwned, C.getASTContext().getLangOpts(),
George Karpenkov6babf2a2018-09-21 20:36:21 +0000982 SummaryLog, N, Sym));
George Karpenkov70c2ee32018-08-17 21:41:07 +0000983 }
984 }
985 }
986 }
987}
988
989//===----------------------------------------------------------------------===//
990// Check various ways a symbol can be invalidated.
991//===----------------------------------------------------------------------===//
992
993void RetainCountChecker::checkBind(SVal loc, SVal val, const Stmt *S,
994 CheckerContext &C) const {
995 // Are we storing to something that causes the value to "escape"?
996 bool escapes = true;
997
998 // A value escapes in three possible cases (this may change):
999 //
1000 // (1) we are binding to something that is not a memory region.
1001 // (2) we are binding to a memregion that does not have stack storage
1002 // (3) we are binding to a memregion with stack storage that the store
1003 // does not understand.
1004 ProgramStateRef state = C.getState();
1005
George Karpenkov6e9fd132018-08-22 01:17:09 +00001006 if (auto regionLoc = loc.getAs<loc::MemRegionVal>()) {
George Karpenkov70c2ee32018-08-17 21:41:07 +00001007 escapes = !regionLoc->getRegion()->hasStackStorage();
1008
1009 if (!escapes) {
1010 // To test (3), generate a new state with the binding added. If it is
1011 // the same state, then it escapes (since the store cannot represent
1012 // the binding).
1013 // Do this only if we know that the store is not supposed to generate the
1014 // same state.
1015 SVal StoredVal = state->getSVal(regionLoc->getRegion());
1016 if (StoredVal != val)
1017 escapes = (state == (state->bindLoc(*regionLoc, val, C.getLocationContext())));
1018 }
1019 if (!escapes) {
1020 // Case 4: We do not currently model what happens when a symbol is
1021 // assigned to a struct field, so be conservative here and let the symbol
1022 // go. TODO: This could definitely be improved upon.
1023 escapes = !isa<VarRegion>(regionLoc->getRegion());
1024 }
1025 }
1026
1027 // If we are storing the value into an auto function scope variable annotated
1028 // with (__attribute__((cleanup))), stop tracking the value to avoid leak
1029 // false positives.
George Karpenkov6e9fd132018-08-22 01:17:09 +00001030 if (const auto *LVR = dyn_cast_or_null<VarRegion>(loc.getAsRegion())) {
George Karpenkov70c2ee32018-08-17 21:41:07 +00001031 const VarDecl *VD = LVR->getDecl();
1032 if (VD->hasAttr<CleanupAttr>()) {
1033 escapes = true;
1034 }
1035 }
1036
1037 // If our store can represent the binding and we aren't storing to something
1038 // that doesn't have local storage then just return and have the simulation
1039 // state continue as is.
1040 if (!escapes)
1041 return;
1042
1043 // Otherwise, find all symbols referenced by 'val' that we are tracking
1044 // and stop tracking them.
1045 state = state->scanReachableSymbols<StopTrackingCallback>(val).getState();
1046 C.addTransition(state);
1047}
1048
1049ProgramStateRef RetainCountChecker::evalAssume(ProgramStateRef state,
George Karpenkov6e9fd132018-08-22 01:17:09 +00001050 SVal Cond,
1051 bool Assumption) const {
George Karpenkov70c2ee32018-08-17 21:41:07 +00001052 // FIXME: We may add to the interface of evalAssume the list of symbols
1053 // whose assumptions have changed. For now we just iterate through the
1054 // bindings and check if any of the tracked symbols are NULL. This isn't
1055 // too bad since the number of symbols we will track in practice are
1056 // probably small and evalAssume is only called at branches and a few
1057 // other places.
1058 RefBindingsTy B = state->get<RefBindings>();
1059
1060 if (B.isEmpty())
1061 return state;
1062
1063 bool changed = false;
1064 RefBindingsTy::Factory &RefBFactory = state->get_context<RefBindings>();
1065
1066 for (RefBindingsTy::iterator I = B.begin(), E = B.end(); I != E; ++I) {
1067 // Check if the symbol is null stop tracking the symbol.
1068 ConstraintManager &CMgr = state->getConstraintManager();
1069 ConditionTruthVal AllocFailed = CMgr.isNull(state, I.getKey());
1070 if (AllocFailed.isConstrainedTrue()) {
1071 changed = true;
1072 B = RefBFactory.remove(B, I.getKey());
1073 }
1074 }
1075
1076 if (changed)
1077 state = state->set<RefBindings>(B);
1078
1079 return state;
1080}
1081
1082ProgramStateRef
1083RetainCountChecker::checkRegionChanges(ProgramStateRef state,
1084 const InvalidatedSymbols *invalidated,
1085 ArrayRef<const MemRegion *> ExplicitRegions,
1086 ArrayRef<const MemRegion *> Regions,
1087 const LocationContext *LCtx,
1088 const CallEvent *Call) const {
1089 if (!invalidated)
1090 return state;
1091
1092 llvm::SmallPtrSet<SymbolRef, 8> WhitelistedSymbols;
1093 for (ArrayRef<const MemRegion *>::iterator I = ExplicitRegions.begin(),
1094 E = ExplicitRegions.end(); I != E; ++I) {
1095 if (const SymbolicRegion *SR = (*I)->StripCasts()->getAs<SymbolicRegion>())
1096 WhitelistedSymbols.insert(SR->getSymbol());
1097 }
1098
1099 for (InvalidatedSymbols::const_iterator I=invalidated->begin(),
1100 E = invalidated->end(); I!=E; ++I) {
1101 SymbolRef sym = *I;
1102 if (WhitelistedSymbols.count(sym))
1103 continue;
1104 // Remove any existing reference-count binding.
1105 state = removeRefBinding(state, sym);
1106 }
1107 return state;
1108}
1109
1110//===----------------------------------------------------------------------===//
1111// Handle dead symbols and end-of-path.
1112//===----------------------------------------------------------------------===//
1113
1114ProgramStateRef
1115RetainCountChecker::handleAutoreleaseCounts(ProgramStateRef state,
1116 ExplodedNode *Pred,
1117 const ProgramPointTag *Tag,
1118 CheckerContext &Ctx,
1119 SymbolRef Sym, RefVal V) const {
1120 unsigned ACnt = V.getAutoreleaseCount();
1121
1122 // No autorelease counts? Nothing to be done.
1123 if (!ACnt)
1124 return state;
1125
1126 unsigned Cnt = V.getCount();
1127
1128 // FIXME: Handle sending 'autorelease' to already released object.
1129
1130 if (V.getKind() == RefVal::ReturnedOwned)
1131 ++Cnt;
1132
1133 // If we would over-release here, but we know the value came from an ivar,
1134 // assume it was a strong ivar that's just been relinquished.
1135 if (ACnt > Cnt &&
1136 V.getIvarAccessHistory() == RefVal::IvarAccessHistory::AccessedDirectly) {
1137 V = V.releaseViaIvar();
1138 --ACnt;
1139 }
1140
1141 if (ACnt <= Cnt) {
1142 if (ACnt == Cnt) {
1143 V.clearCounts();
1144 if (V.getKind() == RefVal::ReturnedOwned)
1145 V = V ^ RefVal::ReturnedNotOwned;
1146 else
1147 V = V ^ RefVal::NotOwned;
1148 } else {
1149 V.setCount(V.getCount() - ACnt);
1150 V.setAutoreleaseCount(0);
1151 }
1152 return setRefBinding(state, Sym, V);
1153 }
1154
1155 // HACK: Ignore retain-count issues on values accessed through ivars,
1156 // because of cases like this:
1157 // [_contentView retain];
1158 // [_contentView removeFromSuperview];
1159 // [self addSubview:_contentView]; // invalidates 'self'
1160 // [_contentView release];
1161 if (V.getIvarAccessHistory() != RefVal::IvarAccessHistory::None)
1162 return state;
1163
1164 // Woah! More autorelease counts then retain counts left.
1165 // Emit hard error.
1166 V = V ^ RefVal::ErrorOverAutorelease;
1167 state = setRefBinding(state, Sym, V);
1168
1169 ExplodedNode *N = Ctx.generateSink(state, Pred, Tag);
1170 if (N) {
1171 SmallString<128> sbuf;
1172 llvm::raw_svector_ostream os(sbuf);
1173 os << "Object was autoreleased ";
1174 if (V.getAutoreleaseCount() > 1)
1175 os << V.getAutoreleaseCount() << " times but the object ";
1176 else
1177 os << "but ";
1178 os << "has a +" << V.getCount() << " retain count";
1179
1180 if (!overAutorelease)
1181 overAutorelease.reset(new OverAutorelease(this));
1182
1183 const LangOptions &LOpts = Ctx.getASTContext().getLangOpts();
George Karpenkov6babf2a2018-09-21 20:36:21 +00001184 Ctx.emitReport(llvm::make_unique<CFRefReport>(
1185 *overAutorelease, LOpts, SummaryLog, N, Sym, os.str()));
George Karpenkov70c2ee32018-08-17 21:41:07 +00001186 }
1187
1188 return nullptr;
1189}
1190
1191ProgramStateRef
1192RetainCountChecker::handleSymbolDeath(ProgramStateRef state,
1193 SymbolRef sid, RefVal V,
1194 SmallVectorImpl<SymbolRef> &Leaked) const {
1195 bool hasLeak;
1196
1197 // HACK: Ignore retain-count issues on values accessed through ivars,
1198 // because of cases like this:
1199 // [_contentView retain];
1200 // [_contentView removeFromSuperview];
1201 // [self addSubview:_contentView]; // invalidates 'self'
1202 // [_contentView release];
1203 if (V.getIvarAccessHistory() != RefVal::IvarAccessHistory::None)
1204 hasLeak = false;
1205 else if (V.isOwned())
1206 hasLeak = true;
1207 else if (V.isNotOwned() || V.isReturnedOwned())
1208 hasLeak = (V.getCount() > 0);
1209 else
1210 hasLeak = false;
1211
1212 if (!hasLeak)
1213 return removeRefBinding(state, sid);
1214
1215 Leaked.push_back(sid);
1216 return setRefBinding(state, sid, V ^ RefVal::ErrorLeak);
1217}
1218
1219ExplodedNode *
1220RetainCountChecker::processLeaks(ProgramStateRef state,
1221 SmallVectorImpl<SymbolRef> &Leaked,
1222 CheckerContext &Ctx,
1223 ExplodedNode *Pred) const {
1224 // Generate an intermediate node representing the leak point.
1225 ExplodedNode *N = Ctx.addTransition(state, Pred);
1226
1227 if (N) {
1228 for (SmallVectorImpl<SymbolRef>::iterator
1229 I = Leaked.begin(), E = Leaked.end(); I != E; ++I) {
1230
1231 const LangOptions &LOpts = Ctx.getASTContext().getLangOpts();
1232 CFRefBug *BT = Pred ? getLeakWithinFunctionBug(LOpts)
1233 : getLeakAtReturnBug(LOpts);
1234 assert(BT && "BugType not initialized.");
1235
George Karpenkov6babf2a2018-09-21 20:36:21 +00001236 Ctx.emitReport(llvm::make_unique<CFRefLeakReport>(
1237 *BT, LOpts, SummaryLog, N, *I, Ctx, IncludeAllocationLine));
George Karpenkov70c2ee32018-08-17 21:41:07 +00001238 }
1239 }
1240
1241 return N;
1242}
1243
George Karpenkovb1b791b2018-08-17 21:43:27 +00001244static bool isISLObjectRef(QualType Ty) {
1245 return StringRef(Ty.getAsString()).startswith("isl_");
George Karpenkov70c2ee32018-08-17 21:41:07 +00001246}
1247
1248void RetainCountChecker::checkBeginFunction(CheckerContext &Ctx) const {
1249 if (!Ctx.inTopFrame())
1250 return;
1251
George Karpenkovc4d6b932018-08-17 21:42:05 +00001252 RetainSummaryManager &SmrMgr = getSummaryManager(Ctx);
George Karpenkov70c2ee32018-08-17 21:41:07 +00001253 const LocationContext *LCtx = Ctx.getLocationContext();
1254 const FunctionDecl *FD = dyn_cast<FunctionDecl>(LCtx->getDecl());
1255
George Karpenkovc4d6b932018-08-17 21:42:05 +00001256 if (!FD || SmrMgr.isTrustedReferenceCountImplementation(FD))
George Karpenkov70c2ee32018-08-17 21:41:07 +00001257 return;
1258
1259 ProgramStateRef state = Ctx.getState();
George Karpenkovc4d6b932018-08-17 21:42:05 +00001260 const RetainSummary *FunctionSummary = SmrMgr.getFunctionSummary(FD);
George Karpenkov70c2ee32018-08-17 21:41:07 +00001261 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);
George Karpenkovb1b791b2018-08-17 21:43:27 +00001269 if (AE && *AE == DecRef && isISLObjectRef(Ty)) {
George Karpenkov6e9fd132018-08-22 01:17:09 +00001270 state = setRefBinding(
1271 state, Sym, RefVal::makeOwned(RetEffect::ObjKind::Generalized, Ty));
George Karpenkovb1b791b2018-08-17 21:43:27 +00001272 } else if (isISLObjectRef(Ty)) {
George Karpenkov70c2ee32018-08-17 21:41:07 +00001273 state = setRefBinding(
1274 state, Sym,
1275 RefVal::makeNotOwned(RetEffect::ObjKind::Generalized, Ty));
1276 }
1277 }
1278
1279 Ctx.addTransition(state);
1280}
1281
1282void 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
1318const ProgramPointTag *
1319RetainCountChecker::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
1331void 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
1382void 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//===----------------------------------------------------------------------===//
George Karpenkov70c2ee32018-08-17 21:41:07 +00001400// Checker registration.
1401//===----------------------------------------------------------------------===//
1402
1403void ento::registerRetainCountChecker(CheckerManager &Mgr) {
1404 Mgr.registerChecker<RetainCountChecker>(Mgr.getAnalyzerOptions());
1405}