Coverage Report

Created: 2023-05-31 04:38

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Checkers/RetainCountChecker/RetainCountChecker.cpp
Line
Count
Source (jump to first uncovered line)
1
//==-- RetainCountChecker.cpp - Checks for leaks and other issues -*- C++ -*--//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
//  This file defines the methods for RetainCountChecker, which implements
10
//  a reference count checker for Core Foundation and Cocoa on (Mac OS X).
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "RetainCountChecker.h"
15
#include "clang/StaticAnalyzer/Core/Checker.h"
16
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
17
#include <optional>
18
19
using namespace clang;
20
using namespace ento;
21
using namespace retaincountchecker;
22
23
REGISTER_MAP_WITH_PROGRAMSTATE(RefBindings, SymbolRef, RefVal)
24
25
namespace clang {
26
namespace ento {
27
namespace retaincountchecker {
28
29
54.7k
const RefVal *getRefBinding(ProgramStateRef State, SymbolRef Sym) {
30
54.7k
  return State->get<RefBindings>(Sym);
31
54.7k
}
32
33
} // end namespace retaincountchecker
34
} // end namespace ento
35
} // end namespace clang
36
37
static ProgramStateRef setRefBinding(ProgramStateRef State, SymbolRef Sym,
38
4.23k
                                     RefVal Val) {
39
4.23k
  assert(Sym != nullptr);
40
4.23k
  return State->set<RefBindings>(Sym, Val);
41
4.23k
}
42
43
6.32k
static ProgramStateRef removeRefBinding(ProgramStateRef State, SymbolRef Sym) {
44
6.32k
  return State->remove<RefBindings>(Sym);
45
6.32k
}
46
47
0
void RefVal::print(raw_ostream &Out) const {
48
0
  if (!T.isNull())
49
0
    Out << "Tracked " << T << " | ";
50
51
0
  switch (getKind()) {
52
0
    default: llvm_unreachable("Invalid RefVal kind");
53
0
    case Owned: {
54
0
      Out << "Owned";
55
0
      unsigned cnt = getCount();
56
0
      if (cnt) Out << " (+ " << cnt << ")";
57
0
      break;
58
0
    }
59
60
0
    case NotOwned: {
61
0
      Out << "NotOwned";
62
0
      unsigned cnt = getCount();
63
0
      if (cnt) Out << " (+ " << cnt << ")";
64
0
      break;
65
0
    }
66
67
0
    case ReturnedOwned: {
68
0
      Out << "ReturnedOwned";
69
0
      unsigned cnt = getCount();
70
0
      if (cnt) Out << " (+ " << cnt << ")";
71
0
      break;
72
0
    }
73
74
0
    case ReturnedNotOwned: {
75
0
      Out << "ReturnedNotOwned";
76
0
      unsigned cnt = getCount();
77
0
      if (cnt) Out << " (+ " << cnt << ")";
78
0
      break;
79
0
    }
80
81
0
    case Released:
82
0
      Out << "Released";
83
0
      break;
84
85
0
    case ErrorDeallocNotOwned:
86
0
      Out << "-dealloc (not-owned)";
87
0
      break;
88
89
0
    case ErrorLeak:
90
0
      Out << "Leaked";
91
0
      break;
92
93
0
    case ErrorLeakReturned:
94
0
      Out << "Leaked (Bad naming)";
95
0
      break;
96
97
0
    case ErrorUseAfterRelease:
98
0
      Out << "Use-After-Release [ERROR]";
99
0
      break;
100
101
0
    case ErrorReleaseNotOwned:
102
0
      Out << "Release of Not-Owned [ERROR]";
103
0
      break;
104
105
0
    case RefVal::ErrorOverAutorelease:
106
0
      Out << "Over-autoreleased";
107
0
      break;
108
109
0
    case RefVal::ErrorReturnedNotOwned:
110
0
      Out << "Non-owned object returned instead of owned";
111
0
      break;
112
0
  }
113
114
0
  switch (getIvarAccessHistory()) {
115
0
  case IvarAccessHistory::None:
116
0
    break;
117
0
  case IvarAccessHistory::AccessedDirectly:
118
0
    Out << " [direct ivar access]";
119
0
    break;
120
0
  case IvarAccessHistory::ReleasedAfterDirectAccess:
121
0
    Out << " [released after direct ivar access]";
122
0
  }
123
124
0
  if (ACnt) {
125
0
    Out << " [autorelease -" << ACnt << ']';
126
0
  }
127
0
}
128
129
namespace {
130
class StopTrackingCallback final : public SymbolVisitor {
131
  ProgramStateRef state;
132
public:
133
330
  StopTrackingCallback(ProgramStateRef st) : state(std::move(st)) {}
134
330
  ProgramStateRef getState() const { return state; }
135
136
198
  bool VisitSymbol(SymbolRef sym) override {
137
198
    state = removeRefBinding(state, sym);
138
198
    return true;
139
198
  }
140
};
141
} // end anonymous namespace
142
143
//===----------------------------------------------------------------------===//
144
// Handle statements that may have an effect on refcounts.
145
//===----------------------------------------------------------------------===//
146
147
void RetainCountChecker::checkPostStmt(const BlockExpr *BE,
148
63
                                       CheckerContext &C) const {
149
150
  // Scan the BlockDecRefExprs for any object the retain count checker
151
  // may be tracking.
152
63
  if (!BE->getBlockDecl()->hasCaptures())
153
38
    return;
154
155
25
  ProgramStateRef state = C.getState();
156
25
  auto *R = cast<BlockDataRegion>(C.getSVal(BE).getAsRegion());
157
158
25
  BlockDataRegion::referenced_vars_iterator I = R->referenced_vars_begin(),
159
25
                                            E = R->referenced_vars_end();
160
161
25
  if (I == E)
162
0
    return;
163
164
  // FIXME: For now we invalidate the tracking of all symbols passed to blocks
165
  // via captured variables, even though captured variables result in a copy
166
  // and in implicit increment/decrement of a retain count.
167
25
  SmallVector<const MemRegion*, 10> Regions;
168
25
  const LocationContext *LC = C.getLocationContext();
169
25
  MemRegionManager &MemMgr = C.getSValBuilder().getRegionManager();
170
171
54
  for ( ; I != E; 
++I29
) {
172
29
    const VarRegion *VR = I.getCapturedRegion();
173
29
    if (VR->getSuperRegion() == R) {
174
19
      VR = MemMgr.getVarRegion(VR->getDecl(), LC);
175
19
    }
176
29
    Regions.push_back(VR);
177
29
  }
178
179
25
  state = state->scanReachableSymbols<StopTrackingCallback>(Regions).getState();
180
25
  C.addTransition(state);
181
25
}
182
183
void RetainCountChecker::checkPostStmt(const CastExpr *CE,
184
10.2k
                                       CheckerContext &C) const {
185
10.2k
  const ObjCBridgedCastExpr *BE = dyn_cast<ObjCBridgedCastExpr>(CE);
186
10.2k
  if (!BE)
187
10.2k
    return;
188
189
19
  QualType QT = CE->getType();
190
19
  ObjKind K;
191
19
  if (QT->isObjCObjectPointerType()) {
192
10
    K = ObjKind::ObjC;
193
10
  } else {
194
9
    K = ObjKind::CF;
195
9
  }
196
197
19
  ArgEffect AE = ArgEffect(IncRef, K);
198
199
19
  switch (BE->getBridgeKind()) {
200
11
    case OBC_Bridge:
201
      // Do nothing.
202
11
      return;
203
3
    case OBC_BridgeRetained:
204
3
      AE = AE.withKind(IncRef);
205
3
      break;
206
5
    case OBC_BridgeTransfer:
207
5
      AE = AE.withKind(DecRefBridgedTransferred);
208
5
      break;
209
19
  }
210
211
8
  ProgramStateRef state = C.getState();
212
8
  SymbolRef Sym = C.getSVal(CE).getAsLocSymbol();
213
8
  if (!Sym)
214
0
    return;
215
8
  const RefVal* T = getRefBinding(state, Sym);
216
8
  if (!T)
217
3
    return;
218
219
5
  RefVal::Kind hasErr = (RefVal::Kind) 0;
220
5
  state = updateSymbol(state, Sym, *T, AE, hasErr, C);
221
222
5
  if (hasErr) {
223
    // FIXME: If we get an error during a bridge cast, should we report it?
224
0
    return;
225
0
  }
226
227
5
  C.addTransition(state);
228
5
}
229
230
void RetainCountChecker::processObjCLiterals(CheckerContext &C,
231
34
                                             const Expr *Ex) const {
232
34
  ProgramStateRef state = C.getState();
233
34
  const ExplodedNode *pred = C.getPredecessor();
234
34
  for (const Stmt *Child : Ex->children()) {
235
25
    SVal V = pred->getSVal(Child);
236
25
    if (SymbolRef sym = V.getAsSymbol())
237
21
      if (const RefVal* T = getRefBinding(state, sym)) {
238
13
        RefVal::Kind hasErr = (RefVal::Kind) 0;
239
13
        state = updateSymbol(state, sym, *T,
240
13
                             ArgEffect(MayEscape, ObjKind::ObjC), hasErr, C);
241
13
        if (hasErr) {
242
0
          processNonLeakError(state, Child->getSourceRange(), hasErr, sym, C);
243
0
          return;
244
0
        }
245
13
      }
246
25
  }
247
248
  // Return the object as autoreleased.
249
  //  RetEffect RE = RetEffect::MakeNotOwned(ObjKind::ObjC);
250
34
  if (SymbolRef sym =
251
34
        state->getSVal(Ex, pred->getLocationContext()).getAsSymbol()) {
252
34
    QualType ResultTy = Ex->getType();
253
34
    state = setRefBinding(state, sym,
254
34
                          RefVal::makeNotOwned(ObjKind::ObjC, ResultTy));
255
34
  }
256
257
34
  C.addTransition(state);
258
34
}
259
260
void RetainCountChecker::checkPostStmt(const ObjCArrayLiteral *AL,
261
29
                                       CheckerContext &C) const {
262
  // Apply the 'MayEscape' to all values.
263
29
  processObjCLiterals(C, AL);
264
29
}
265
266
void RetainCountChecker::checkPostStmt(const ObjCDictionaryLiteral *DL,
267
5
                                       CheckerContext &C) const {
268
  // Apply the 'MayEscape' to all keys and values.
269
5
  processObjCLiterals(C, DL);
270
5
}
271
272
void RetainCountChecker::checkPostStmt(const ObjCBoxedExpr *Ex,
273
25
                                       CheckerContext &C) const {
274
25
  const ExplodedNode *Pred = C.getPredecessor();
275
25
  ProgramStateRef State = Pred->getState();
276
277
25
  if (SymbolRef Sym = Pred->getSVal(Ex).getAsSymbol()) {
278
25
    QualType ResultTy = Ex->getType();
279
25
    State = setRefBinding(State, Sym,
280
25
                          RefVal::makeNotOwned(ObjKind::ObjC, ResultTy));
281
25
  }
282
283
25
  C.addTransition(State);
284
25
}
285
286
void RetainCountChecker::checkPostStmt(const ObjCIvarRefExpr *IRE,
287
486
                                       CheckerContext &C) const {
288
486
  std::optional<Loc> IVarLoc = C.getSVal(IRE).getAs<Loc>();
289
486
  if (!IVarLoc)
290
0
    return;
291
292
486
  ProgramStateRef State = C.getState();
293
486
  SymbolRef Sym = State->getSVal(*IVarLoc).getAsSymbol();
294
486
  if (!Sym || 
!isa_and_nonnull<ObjCIvarRegion>(Sym->getOriginRegion())425
)
295
89
    return;
296
297
  // Accessing an ivar directly is unusual. If we've done that, be more
298
  // forgiving about what the surrounding code is allowed to do.
299
300
397
  QualType Ty = Sym->getType();
301
397
  ObjKind Kind;
302
397
  if (Ty->isObjCRetainableType())
303
262
    Kind = ObjKind::ObjC;
304
135
  else if (coreFoundation::isCFObjectRef(Ty))
305
15
    Kind = ObjKind::CF;
306
120
  else
307
120
    return;
308
309
  // If the value is already known to be nil, don't bother tracking it.
310
277
  ConstraintManager &CMgr = State->getConstraintManager();
311
277
  if (CMgr.isNull(State, Sym).isConstrainedTrue())
312
16
    return;
313
314
261
  if (const RefVal *RV = getRefBinding(State, Sym)) {
315
    // If we've seen this symbol before, or we're only seeing it now because
316
    // of something the analyzer has synthesized, don't do anything.
317
129
    if (RV->getIvarAccessHistory() != RefVal::IvarAccessHistory::None ||
318
129
        
isSynthesizedAccessor(C.getStackFrame())27
) {
319
120
      return;
320
120
    }
321
322
    // Note that this value has been loaded from an ivar.
323
9
    C.addTransition(setRefBinding(State, Sym, RV->withIvarAccess()));
324
9
    return;
325
129
  }
326
327
132
  RefVal PlusZero = RefVal::makeNotOwned(Kind, Ty);
328
329
  // In a synthesized accessor, the effective retain count is +0.
330
132
  if (isSynthesizedAccessor(C.getStackFrame())) {
331
32
    C.addTransition(setRefBinding(State, Sym, PlusZero));
332
32
    return;
333
32
  }
334
335
100
  State = setRefBinding(State, Sym, PlusZero.withIvarAccess());
336
100
  C.addTransition(State);
337
100
}
338
339
4.42k
static bool isReceiverUnconsumedSelf(const CallEvent &Call) {
340
4.42k
  if (const auto *MC = dyn_cast<ObjCMethodCall>(&Call)) {
341
342
    // Check if the message is not consumed, we know it will not be used in
343
    // an assignment, ex: "self = [super init]".
344
2.32k
    return MC->getMethodFamily() == OMF_init && 
MC->isReceiverSelfOrSuper()537
&&
345
2.32k
           !Call.getLocationContext()
346
82
                ->getAnalysisDeclContext()
347
82
                ->getParentMap()
348
82
                .isConsumedExpr(Call.getOriginExpr());
349
2.32k
  }
350
2.10k
  return false;
351
4.42k
}
352
353
const static RetainSummary *getSummary(RetainSummaryManager &Summaries,
354
                                       const CallEvent &Call,
355
4.42k
                                       QualType ReceiverType) {
356
4.42k
  const Expr *CE = Call.getOriginExpr();
357
4.42k
  AnyCall C =
358
4.42k
      CE ? 
*AnyCall::forExpr(CE)4.41k
359
4.42k
         : 
AnyCall(cast<CXXDestructorDecl>(Call.getDecl()))13
;
360
4.42k
  return Summaries.getSummary(C, Call.hasNonZeroCallbackArg(),
361
4.42k
                              isReceiverUnconsumedSelf(Call), ReceiverType);
362
4.42k
}
363
364
void RetainCountChecker::checkPostCall(const CallEvent &Call,
365
4.42k
                                       CheckerContext &C) const {
366
4.42k
  RetainSummaryManager &Summaries = getSummaryManager(C);
367
368
  // Leave null if no receiver.
369
4.42k
  QualType ReceiverType;
370
4.42k
  if (const auto *MC = dyn_cast<ObjCMethodCall>(&Call)) {
371
2.32k
    if (MC->isInstanceMessage()) {
372
1.73k
      SVal ReceiverV = MC->getReceiverSVal();
373
1.73k
      if (SymbolRef Sym = ReceiverV.getAsLocSymbol())
374
1.73k
        if (const RefVal *T = getRefBinding(C.getState(), Sym))
375
1.19k
          ReceiverType = T->getType();
376
1.73k
    }
377
2.32k
  }
378
379
4.42k
  const RetainSummary *Summ = getSummary(Summaries, Call, ReceiverType);
380
381
4.42k
  if (C.wasInlined) {
382
699
    processSummaryOfInlined(*Summ, Call, C);
383
699
    return;
384
699
  }
385
3.73k
  checkSummary(*Summ, Call, C);
386
3.73k
}
387
388
/// GetReturnType - Used to get the return type of a message expression or
389
///  function call with the intention of affixing that type to a tracked symbol.
390
///  While the return type can be queried directly from RetEx, when
391
///  invoking class methods we augment to the return type to be that of
392
///  a pointer to the class (as opposed it just being id).
393
// FIXME: We may be able to do this with related result types instead.
394
// This function is probably overestimating.
395
249
static QualType GetReturnType(const Expr *RetE, ASTContext &Ctx) {
396
249
  QualType RetTy = RetE->getType();
397
  // If RetE is not a message expression just return its type.
398
  // If RetE is a message expression, return its types if it is something
399
  /// more specific than id.
400
249
  if (const ObjCMessageExpr *ME = dyn_cast<ObjCMessageExpr>(RetE))
401
182
    if (const ObjCObjectPointerType *PT = RetTy->getAs<ObjCObjectPointerType>())
402
182
      if (PT->isObjCQualifiedIdType() || PT->isObjCIdType() ||
403
182
          
PT->isObjCClassType()103
) {
404
        // At this point we know the return type of the message expression is
405
        // id, id<...>, or Class. If we have an ObjCInterfaceDecl, we know this
406
        // is a call to a class method whose type we can resolve.  In such
407
        // cases, promote the return type to XXX* (where XXX is the class).
408
81
        const ObjCInterfaceDecl *D = ME->getReceiverInterface();
409
81
        return !D ? 
RetTy13
:
410
81
                    
Ctx.getObjCObjectPointerType(Ctx.getObjCInterfaceType(D))68
;
411
81
      }
412
413
168
  return RetTy;
414
249
}
415
416
static std::optional<RefVal> refValFromRetEffect(RetEffect RE,
417
2.41k
                                                 QualType ResultTy) {
418
2.41k
  if (RE.isOwned()) {
419
1.33k
    return RefVal::makeOwned(RE.getObjKind(), ResultTy);
420
1.33k
  } else 
if (1.07k
RE.notOwned()1.07k
) {
421
249
    return RefVal::makeNotOwned(RE.getObjKind(), ResultTy);
422
249
  }
423
424
828
  return std::nullopt;
425
2.41k
}
426
427
47
static bool isPointerToObject(QualType QT) {
428
47
  QualType PT = QT->getPointeeType();
429
47
  if (!PT.isNull())
430
47
    if (PT->getAsCXXRecordDecl())
431
42
      return true;
432
5
  return false;
433
47
}
434
435
/// Whether the tracked value should be escaped on a given call.
436
/// OSObjects are escaped when passed to void * / etc.
437
static bool shouldEscapeOSArgumentOnCall(const CallEvent &CE, unsigned ArgIdx,
438
633
                                       const RefVal *TrackedValue) {
439
633
  if (TrackedValue->getObjKind() != ObjKind::OS)
440
585
    return false;
441
48
  if (ArgIdx >= CE.parameters().size())
442
1
    return false;
443
47
  return !isPointerToObject(CE.parameters()[ArgIdx]->getType());
444
48
}
445
446
// We don't always get the exact modeling of the function with regards to the
447
// retain count checker even when the function is inlined. For example, we need
448
// to stop tracking the symbols which were marked with StopTrackingHard.
449
void RetainCountChecker::processSummaryOfInlined(const RetainSummary &Summ,
450
                                                 const CallEvent &CallOrMsg,
451
699
                                                 CheckerContext &C) const {
452
699
  ProgramStateRef state = C.getState();
453
454
  // Evaluate the effect of the arguments.
455
1.11k
  for (unsigned idx = 0, e = CallOrMsg.getNumArgs(); idx != e; 
++idx418
) {
456
418
    SVal V = CallOrMsg.getArgSVal(idx);
457
458
418
    if (SymbolRef Sym = V.getAsLocSymbol()) {
459
194
      bool ShouldRemoveBinding = Summ.getArg(idx).getKind() == StopTrackingHard;
460
194
      if (const RefVal *T = getRefBinding(state, Sym))
461
64
        if (shouldEscapeOSArgumentOnCall(CallOrMsg, idx, T))
462
1
          ShouldRemoveBinding = true;
463
464
194
      if (ShouldRemoveBinding)
465
7
        state = removeRefBinding(state, Sym);
466
194
    }
467
418
  }
468
469
  // Evaluate the effect on the message receiver.
470
699
  if (const auto *MsgInvocation = dyn_cast<ObjCMethodCall>(&CallOrMsg)) {
471
336
    if (SymbolRef Sym = MsgInvocation->getReceiverSVal().getAsLocSymbol()) {
472
318
      if (Summ.getReceiverEffect().getKind() == StopTrackingHard) {
473
1
        state = removeRefBinding(state, Sym);
474
1
      }
475
318
    }
476
336
  }
477
478
  // Consult the summary for the return value.
479
699
  RetEffect RE = Summ.getRetEffect();
480
481
699
  if (SymbolRef Sym = CallOrMsg.getReturnValue().getAsSymbol()) {
482
244
    if (RE.getKind() == RetEffect::NoRetHard)
483
2
      state = removeRefBinding(state, Sym);
484
244
  }
485
486
699
  C.addTransition(state);
487
699
}
488
489
2.25k
static bool isSmartPtrField(const MemRegion *MR) {
490
2.25k
  const auto *TR = dyn_cast<TypedValueRegion>(
491
2.25k
    cast<SubRegion>(MR)->getSuperRegion());
492
2.25k
  return TR && 
RetainSummaryManager::isKnownSmartPointer(TR->getValueType())86
;
493
2.25k
}
494
495
496
/// A value escapes in these possible cases:
497
///
498
/// - binding to something that is not a memory region.
499
/// - binding to a memregion that does not have stack storage
500
/// - binding to a variable that has a destructor attached using CleanupAttr
501
///
502
/// We do not currently model what happens when a symbol is
503
/// assigned to a struct field, unless it is a known smart pointer
504
/// implementation, about which we know that it is inlined.
505
/// FIXME: This could definitely be improved upon.
506
2.25k
static bool shouldEscapeRegion(const MemRegion *R) {
507
2.25k
  if (isSmartPtrField(R))
508
3
    return false;
509
510
2.25k
  const auto *VR = dyn_cast<VarRegion>(R);
511
512
2.25k
  if (!R->hasStackStorage() || 
!VR1.94k
)
513
390
    return true;
514
515
1.86k
  const VarDecl *VD = VR->getDecl();
516
1.86k
  if (!VD->hasAttr<CleanupAttr>())
517
1.85k
    return false; // CleanupAttr attaches destructors, which cause escaping.
518
7
  return true;
519
1.86k
}
520
521
static SmallVector<ProgramStateRef, 2>
522
updateOutParameters(ProgramStateRef State, const RetainSummary &Summ,
523
3.60k
                    const CallEvent &CE) {
524
525
3.60k
  SVal L = CE.getReturnValue();
526
527
  // Splitting is required to support out parameters,
528
  // as out parameters might be created only on the "success" branch.
529
  // We want to avoid eagerly splitting unless out parameters are actually
530
  // needed.
531
3.60k
  bool SplitNecessary = false;
532
3.60k
  for (auto &P : Summ.getArgEffects())
533
1.09k
    if (P.second.getKind() == RetainedOutParameterOnNonZero ||
534
1.09k
        
P.second.getKind() == RetainedOutParameterOnZero1.08k
)
535
16
      SplitNecessary = true;
536
537
3.60k
  ProgramStateRef AssumeNonZeroReturn = State;
538
3.60k
  ProgramStateRef AssumeZeroReturn = State;
539
540
3.60k
  if (SplitNecessary) {
541
14
    if (!CE.getResultType()->isScalarType()) {
542
      // Structures cannot be assumed. This probably deserves
543
      // a compiler warning for invalid annotations.
544
1
      return {State};
545
1
    }
546
13
    if (auto DL = L.getAs<DefinedOrUnknownSVal>()) {
547
13
      AssumeNonZeroReturn = AssumeNonZeroReturn->assume(*DL, true);
548
13
      AssumeZeroReturn = AssumeZeroReturn->assume(*DL, false);
549
13
    }
550
13
  }
551
552
6.08k
  
for (unsigned idx = 0, e = CE.getNumArgs(); 3.60k
idx != e;
++idx2.47k
) {
553
2.47k
    SVal ArgVal = CE.getArgSVal(idx);
554
2.47k
    ArgEffect AE = Summ.getArg(idx);
555
556
2.47k
    auto *ArgRegion = dyn_cast_or_null<TypedValueRegion>(ArgVal.getAsRegion());
557
2.47k
    if (!ArgRegion)
558
2.02k
      continue;
559
560
458
    QualType PointeeTy = ArgRegion->getValueType();
561
458
    SVal PointeeVal = State->getSVal(ArgRegion);
562
458
    SymbolRef Pointee = PointeeVal.getAsLocSymbol();
563
458
    if (!Pointee)
564
311
      continue;
565
566
147
    if (shouldEscapeRegion(ArgRegion))
567
92
      continue;
568
569
55
    auto makeNotOwnedParameter = [&](ProgramStateRef St) {
570
16
      return setRefBinding(St, Pointee,
571
16
                           RefVal::makeNotOwned(AE.getObjKind(), PointeeTy));
572
16
    };
573
55
    auto makeOwnedParameter = [&](ProgramStateRef St) {
574
26
      return setRefBinding(St, Pointee,
575
26
                           RefVal::makeOwned(ObjKind::OS, PointeeTy));
576
26
    };
577
578
55
    switch (AE.getKind()) {
579
8
    case UnretainedOutParameter:
580
8
      AssumeNonZeroReturn = makeNotOwnedParameter(AssumeNonZeroReturn);
581
8
      AssumeZeroReturn = makeNotOwnedParameter(AssumeZeroReturn);
582
8
      break;
583
6
    case RetainedOutParameter:
584
6
      AssumeNonZeroReturn = makeOwnedParameter(AssumeNonZeroReturn);
585
6
      AssumeZeroReturn = makeOwnedParameter(AssumeZeroReturn);
586
6
      break;
587
10
    case RetainedOutParameterOnNonZero:
588
10
      AssumeNonZeroReturn = makeOwnedParameter(AssumeNonZeroReturn);
589
10
      break;
590
4
    case RetainedOutParameterOnZero:
591
4
      AssumeZeroReturn = makeOwnedParameter(AssumeZeroReturn);
592
4
      break;
593
27
    default:
594
27
      break;
595
55
    }
596
55
  }
597
598
3.60k
  if (SplitNecessary) {
599
13
    return {AssumeNonZeroReturn, AssumeZeroReturn};
600
3.59k
  } else {
601
3.59k
    assert(AssumeZeroReturn == AssumeNonZeroReturn);
602
3.59k
    return {AssumeZeroReturn};
603
3.59k
  }
604
3.60k
}
605
606
void RetainCountChecker::checkSummary(const RetainSummary &Summ,
607
                                      const CallEvent &CallOrMsg,
608
3.73k
                                      CheckerContext &C) const {
609
3.73k
  ProgramStateRef state = C.getState();
610
611
  // Evaluate the effect of the arguments.
612
3.73k
  RefVal::Kind hasErr = (RefVal::Kind) 0;
613
3.73k
  SourceRange ErrorRange;
614
3.73k
  SymbolRef ErrorSym = nullptr;
615
616
  // Helper tag for providing diagnostics: indicate whether dealloc was sent
617
  // at this location.
618
3.73k
  bool DeallocSent = false;
619
620
6.22k
  for (unsigned idx = 0, e = CallOrMsg.getNumArgs(); idx != e; 
++idx2.49k
) {
621
2.55k
    SVal V = CallOrMsg.getArgSVal(idx);
622
623
2.55k
    ArgEffect Effect = Summ.getArg(idx);
624
2.55k
    if (SymbolRef Sym = V.getAsLocSymbol()) {
625
1.06k
      if (const RefVal *T = getRefBinding(state, Sym)) {
626
627
569
        if (shouldEscapeOSArgumentOnCall(CallOrMsg, idx, T))
628
4
          Effect = ArgEffect(StopTrackingHard, ObjKind::OS);
629
630
569
        state = updateSymbol(state, Sym, *T, Effect, hasErr, C);
631
569
        if (hasErr) {
632
59
          ErrorRange = CallOrMsg.getArgSourceRange(idx);
633
59
          ErrorSym = Sym;
634
59
          break;
635
510
        } else if (Effect.getKind() == Dealloc) {
636
0
          DeallocSent = true;
637
0
        }
638
569
      }
639
1.06k
    }
640
2.55k
  }
641
642
  // Evaluate the effect on the message receiver / `this` argument.
643
3.73k
  bool ReceiverIsTracked = false;
644
3.73k
  if (!hasErr) {
645
3.67k
    if (const auto *MsgInvocation = dyn_cast<ObjCMethodCall>(&CallOrMsg)) {
646
1.98k
      if (SymbolRef Sym = MsgInvocation->getReceiverSVal().getAsLocSymbol()) {
647
1.41k
        if (const RefVal *T = getRefBinding(state, Sym)) {
648
1.12k
          ReceiverIsTracked = true;
649
1.12k
          state = updateSymbol(state, Sym, *T,
650
1.12k
                               Summ.getReceiverEffect(), hasErr, C);
651
1.12k
          if (hasErr) {
652
57
            ErrorRange = MsgInvocation->getOriginExpr()->getReceiverRange();
653
57
            ErrorSym = Sym;
654
1.06k
          } else if (Summ.getReceiverEffect().getKind() == Dealloc) {
655
8
            DeallocSent = true;
656
8
          }
657
1.12k
        }
658
1.41k
      }
659
1.98k
    } else 
if (const auto *1.68k
MCall1.68k
= dyn_cast<CXXMemberCall>(&CallOrMsg)) {
660
114
      if (SymbolRef Sym = MCall->getCXXThisVal().getAsLocSymbol()) {
661
112
        if (const RefVal *T = getRefBinding(state, Sym)) {
662
91
          state = updateSymbol(state, Sym, *T, Summ.getThisEffect(),
663
91
                               hasErr, C);
664
91
          if (hasErr) {
665
7
            ErrorRange = MCall->getOriginExpr()->getSourceRange();
666
7
            ErrorSym = Sym;
667
7
          }
668
91
        }
669
112
      }
670
114
    }
671
3.67k
  }
672
673
  // Process any errors.
674
3.73k
  if (hasErr) {
675
123
    processNonLeakError(state, ErrorRange, hasErr, ErrorSym, C);
676
123
    return;
677
123
  }
678
679
  // Consult the summary for the return value.
680
3.60k
  RetEffect RE = Summ.getRetEffect();
681
682
3.60k
  if (RE.getKind() == RetEffect::OwnedWhenTrackedReceiver) {
683
445
    if (ReceiverIsTracked)
684
401
      RE = getSummaryManager(C).getObjAllocRetEffect();
685
44
    else
686
44
      RE = RetEffect::MakeNoRet();
687
445
  }
688
689
3.60k
  if (SymbolRef Sym = CallOrMsg.getReturnValue().getAsSymbol()) {
690
2.41k
    QualType ResultTy = CallOrMsg.getResultType();
691
2.41k
    if (RE.notOwned()) {
692
249
      const Expr *Ex = CallOrMsg.getOriginExpr();
693
249
      assert(Ex);
694
249
      ResultTy = GetReturnType(Ex, C.getASTContext());
695
249
    }
696
2.41k
    if (std::optional<RefVal> updatedRefVal = refValFromRetEffect(RE, ResultTy))
697
1.58k
      state = setRefBinding(state, Sym, *updatedRefVal);
698
2.41k
  }
699
700
3.60k
  SmallVector<ProgramStateRef, 2> Out =
701
3.60k
      updateOutParameters(state, Summ, CallOrMsg);
702
703
3.62k
  for (ProgramStateRef St : Out) {
704
3.62k
    if (DeallocSent) {
705
8
      C.addTransition(St, C.getPredecessor(), &getDeallocSentTag());
706
3.61k
    } else {
707
3.61k
      C.addTransition(St);
708
3.61k
    }
709
3.62k
  }
710
3.60k
}
711
712
ProgramStateRef RetainCountChecker::updateSymbol(ProgramStateRef state,
713
                                                 SymbolRef sym, RefVal V,
714
                                                 ArgEffect AE,
715
                                                 RefVal::Kind &hasErr,
716
1.80k
                                                 CheckerContext &C) const {
717
1.80k
  bool IgnoreRetainMsg = (bool)C.getASTContext().getLangOpts().ObjCAutoRefCount;
718
1.80k
  if (AE.getObjKind() == ObjKind::ObjC && 
IgnoreRetainMsg1.14k
) {
719
27
    switch (AE.getKind()) {
720
14
    default:
721
14
      break;
722
14
    case IncRef:
723
0
      AE = AE.withKind(DoNothing);
724
0
      break;
725
13
    case DecRef:
726
13
      AE = AE.withKind(DoNothing);
727
13
      break;
728
0
    case DecRefAndStopTrackingHard:
729
0
      AE = AE.withKind(StopTracking);
730
0
      break;
731
27
    }
732
27
  }
733
734
  // Handle all use-after-releases.
735
1.80k
  if (V.getKind() == RefVal::Released) {
736
71
    V = V ^ RefVal::ErrorUseAfterRelease;
737
71
    hasErr = V.getKind();
738
71
    return setRefBinding(state, sym, V);
739
71
  }
740
741
1.73k
  switch (AE.getKind()) {
742
0
    case UnretainedOutParameter:
743
0
    case RetainedOutParameter:
744
0
    case RetainedOutParameterOnZero:
745
0
    case RetainedOutParameterOnNonZero:
746
0
      llvm_unreachable("Applies to pointer-to-pointer parameters, which should "
747
0
                       "not have ref state.");
748
749
11
    case Dealloc: // NB. we only need to add a note in a non-error case.
750
11
      switch (V.getKind()) {
751
0
        default:
752
0
          llvm_unreachable("Invalid RefVal state for an explicit dealloc.");
753
10
        case RefVal::Owned:
754
          // The object immediately transitions to the released state.
755
10
          V = V ^ RefVal::Released;
756
10
          V.clearCounts();
757
10
          return setRefBinding(state, sym, V);
758
1
        case RefVal::NotOwned:
759
1
          V = V ^ RefVal::ErrorDeallocNotOwned;
760
1
          hasErr = V.getKind();
761
1
          break;
762
11
      }
763
1
      break;
764
765
158
    case MayEscape:
766
158
      if (V.getKind() == RefVal::Owned) {
767
91
        V = V ^ RefVal::NotOwned;
768
91
        break;
769
91
      }
770
771
158
      
[[fallthrough]];67
772
773
282
    case DoNothing:
774
282
      return state;
775
776
159
    case Autorelease:
777
      // Update the autorelease counts.
778
159
      V = V.autorelease();
779
159
      break;
780
781
16
    case StopTracking:
782
52
    case StopTrackingHard:
783
52
      return removeRefBinding(state, sym);
784
785
226
    case IncRef:
786
226
      switch (V.getKind()) {
787
0
        default:
788
0
          llvm_unreachable("Invalid RefVal state for a retain.");
789
87
        case RefVal::Owned:
790
226
        case RefVal::NotOwned:
791
226
          V = V + 1;
792
226
          break;
793
226
      }
794
226
      break;
795
796
891
    case DecRef:
797
895
    case DecRefBridgedTransferred:
798
910
    case DecRefAndStopTrackingHard:
799
910
      switch (V.getKind()) {
800
0
        default:
801
          // case 'RefVal::Released' handled above.
802
0
          llvm_unreachable("Invalid RefVal state for a release.");
803
804
693
        case RefVal::Owned:
805
693
          assert(V.getCount() > 0);
806
693
          if (V.getCount() == 1) {
807
653
            if (AE.getKind() == DecRefBridgedTransferred ||
808
653
                V.getIvarAccessHistory() ==
809
649
                  RefVal::IvarAccessHistory::AccessedDirectly)
810
4
              V = V ^ RefVal::NotOwned;
811
649
            else
812
649
              V = V ^ RefVal::Released;
813
653
          } else 
if (40
AE.getKind() == DecRefAndStopTrackingHard40
) {
814
0
            return removeRefBinding(state, sym);
815
0
          }
816
817
693
          V = V - 1;
818
693
          break;
819
820
217
        case RefVal::NotOwned:
821
217
          if (V.getCount() > 0) {
822
128
            if (AE.getKind() == DecRefAndStopTrackingHard)
823
6
              return removeRefBinding(state, sym);
824
122
            V = V - 1;
825
122
          } else 
if (89
V.getIvarAccessHistory() ==
826
89
                       RefVal::IvarAccessHistory::AccessedDirectly) {
827
            // Assume that the instance variable was holding on the object at
828
            // +1, and we just didn't know.
829
38
            if (AE.getKind() == DecRefAndStopTrackingHard)
830
0
              return removeRefBinding(state, sym);
831
38
            V = V.releaseViaIvar() ^ RefVal::Released;
832
51
          } else {
833
51
            V = V ^ RefVal::ErrorReleaseNotOwned;
834
51
            hasErr = V.getKind();
835
51
          }
836
211
          break;
837
910
      }
838
904
      break;
839
1.73k
  }
840
1.38k
  return setRefBinding(state, sym, V);
841
1.73k
}
842
843
const RefCountBug &
844
RetainCountChecker::errorKindToBugKind(RefVal::Kind ErrorKind,
845
111
                                       SymbolRef Sym) const {
846
111
  switch (ErrorKind) {
847
59
    case RefVal::ErrorUseAfterRelease:
848
59
      return *UseAfterRelease;
849
51
    case RefVal::ErrorReleaseNotOwned:
850
51
      return *ReleaseNotOwned;
851
1
    case RefVal::ErrorDeallocNotOwned:
852
1
      if (Sym->getType()->getPointeeCXXRecordDecl())
853
1
        return *FreeNotOwned;
854
0
      return *DeallocNotOwned;
855
0
    default:
856
0
      llvm_unreachable("Unhandled error.");
857
111
  }
858
111
}
859
860
void RetainCountChecker::processNonLeakError(ProgramStateRef St,
861
                                             SourceRange ErrorRange,
862
                                             RefVal::Kind ErrorKind,
863
                                             SymbolRef Sym,
864
123
                                             CheckerContext &C) const {
865
  // HACK: Ignore retain-count issues on values accessed through ivars,
866
  // because of cases like this:
867
  //   [_contentView retain];
868
  //   [_contentView removeFromSuperview];
869
  //   [self addSubview:_contentView]; // invalidates 'self'
870
  //   [_contentView release];
871
123
  if (const RefVal *RV = getRefBinding(St, Sym))
872
123
    if (RV->getIvarAccessHistory() != RefVal::IvarAccessHistory::None)
873
12
      return;
874
875
111
  ExplodedNode *N = C.generateErrorNode(St);
876
111
  if (!N)
877
0
    return;
878
879
111
  auto report = std::make_unique<RefCountReport>(
880
111
      errorKindToBugKind(ErrorKind, Sym),
881
111
      C.getASTContext().getLangOpts(), N, Sym);
882
111
  report->addRange(ErrorRange);
883
111
  C.emitReport(std::move(report));
884
111
}
885
886
//===----------------------------------------------------------------------===//
887
// Handle the return values of retain-count-related functions.
888
//===----------------------------------------------------------------------===//
889
890
bool RetainCountChecker::evalCall(const CallEvent &Call,
891
2.02k
                                  CheckerContext &C) const {
892
2.02k
  ProgramStateRef state = C.getState();
893
2.02k
  const auto *FD = dyn_cast_or_null<FunctionDecl>(Call.getDecl());
894
2.02k
  if (!FD)
895
39
    return false;
896
897
1.98k
  const auto *CE = dyn_cast_or_null<CallExpr>(Call.getOriginExpr());
898
1.98k
  if (!CE)
899
84
    return false;
900
901
1.89k
  RetainSummaryManager &SmrMgr = getSummaryManager(C);
902
1.89k
  QualType ResultTy = Call.getResultType();
903
904
  // See if the function has 'rc_ownership_trusted_implementation'
905
  // annotate attribute. If it does, we will not inline it.
906
1.89k
  bool hasTrustedImplementationAnnotation = false;
907
908
1.89k
  const LocationContext *LCtx = C.getLocationContext();
909
910
1.89k
  using BehaviorSummary = RetainSummaryManager::BehaviorSummary;
911
1.89k
  std::optional<BehaviorSummary> BSmr =
912
1.89k
      SmrMgr.canEval(CE, FD, hasTrustedImplementationAnnotation);
913
914
  // See if it's one of the specific functions we know how to eval.
915
1.89k
  if (!BSmr)
916
1.67k
    return false;
917
918
  // Bind the return value.
919
226
  if (BSmr == BehaviorSummary::Identity ||
920
226
      
BSmr == BehaviorSummary::IdentityOrZero77
||
921
226
      
BSmr == BehaviorSummary::IdentityThis66
) {
922
923
161
    const Expr *BindReturnTo =
924
161
        (BSmr == BehaviorSummary::IdentityThis)
925
161
            ? 
cast<CXXMemberCallExpr>(CE)->getImplicitObjectArgument()1
926
161
            : 
CE->getArg(0)160
;
927
161
    SVal RetVal = state->getSVal(BindReturnTo, LCtx);
928
929
    // If the receiver is unknown or the function has
930
    // 'rc_ownership_trusted_implementation' annotate attribute, conjure a
931
    // return value.
932
    // FIXME: this branch is very strange.
933
161
    if (RetVal.isUnknown() ||
934
161
        (hasTrustedImplementationAnnotation && 
!ResultTy.isNull()10
)) {
935
10
      SValBuilder &SVB = C.getSValBuilder();
936
10
      RetVal =
937
10
          SVB.conjureSymbolVal(nullptr, CE, LCtx, ResultTy, C.blockCount());
938
10
    }
939
940
    // Bind the value.
941
161
    state = state->BindExpr(CE, LCtx, RetVal, /*Invalidate=*/false);
942
943
161
    if (BSmr == BehaviorSummary::IdentityOrZero) {
944
      // Add a branch where the output is zero.
945
11
      ProgramStateRef NullOutputState = C.getState();
946
947
      // Assume that output is zero on the other branch.
948
11
      NullOutputState = NullOutputState->BindExpr(
949
11
          CE, LCtx, C.getSValBuilder().makeNullWithType(ResultTy),
950
11
          /*Invalidate=*/false);
951
11
      C.addTransition(NullOutputState, &getCastFailTag());
952
953
      // And on the original branch assume that both input and
954
      // output are non-zero.
955
11
      if (auto L = RetVal.getAs<DefinedOrUnknownSVal>())
956
11
        state = state->assume(*L, /*assumption=*/true);
957
958
11
    }
959
161
  }
960
961
226
  C.addTransition(state);
962
226
  return true;
963
1.89k
}
964
965
ExplodedNode * RetainCountChecker::processReturn(const ReturnStmt *S,
966
2.12k
                                                 CheckerContext &C) const {
967
2.12k
  ExplodedNode *Pred = C.getPredecessor();
968
969
  // Only adjust the reference count if this is the top-level call frame,
970
  // and not the result of inlining.  In the future, we should do
971
  // better checking even for inlined calls, and see if they match
972
  // with their expected semantics (e.g., the method should return a retained
973
  // object, etc.).
974
2.12k
  if (!C.inTopFrame())
975
699
    return Pred;
976
977
1.42k
  if (!S)
978
876
    return Pred;
979
980
550
  const Expr *RetE = S->getRetValue();
981
550
  if (!RetE)
982
59
    return Pred;
983
984
491
  ProgramStateRef state = C.getState();
985
  // We need to dig down to the symbolic base here because various
986
  // custom allocators do sometimes return the symbol with an offset.
987
491
  SymbolRef Sym = state->getSValAsScalarOrLoc(RetE, C.getLocationContext())
988
491
                      .getAsLocSymbol(/*IncludeBaseRegions=*/true);
989
491
  if (!Sym)
990
166
    return Pred;
991
992
  // Get the reference count binding (if any).
993
325
  const RefVal *T = getRefBinding(state, Sym);
994
325
  if (!T)
995
79
    return Pred;
996
997
  // Change the reference count.
998
246
  RefVal X = *T;
999
1000
246
  switch (X.getKind()) {
1001
192
    case RefVal::Owned: {
1002
192
      unsigned cnt = X.getCount();
1003
192
      assert(cnt > 0);
1004
192
      X.setCount(cnt - 1);
1005
192
      X = X ^ RefVal::ReturnedOwned;
1006
192
      break;
1007
192
    }
1008
1009
51
    case RefVal::NotOwned: {
1010
51
      unsigned cnt = X.getCount();
1011
51
      if (cnt) {
1012
7
        X.setCount(cnt - 1);
1013
7
        X = X ^ RefVal::ReturnedOwned;
1014
44
      } else {
1015
44
        X = X ^ RefVal::ReturnedNotOwned;
1016
44
      }
1017
51
      break;
1018
192
    }
1019
1020
3
    default:
1021
3
      return Pred;
1022
246
  }
1023
1024
  // Update the binding.
1025
243
  state = setRefBinding(state, Sym, X);
1026
243
  Pred = C.addTransition(state);
1027
1028
  // At this point we have updated the state properly.
1029
  // Everything after this is merely checking to see if the return value has
1030
  // been over- or under-retained.
1031
1032
  // Did we cache out?
1033
243
  if (!Pred)
1034
0
    return nullptr;
1035
1036
  // Update the autorelease counts.
1037
243
  static CheckerProgramPointTag AutoreleaseTag(this, "Autorelease");
1038
243
  state = handleAutoreleaseCounts(state, Pred, &AutoreleaseTag, C, Sym, X, S);
1039
1040
  // Have we generated a sink node?
1041
243
  if (!state)
1042
12
    return nullptr;
1043
1044
  // Get the updated binding.
1045
231
  T = getRefBinding(state, Sym);
1046
231
  assert(T);
1047
231
  X = *T;
1048
1049
  // Consult the summary of the enclosing method.
1050
231
  RetainSummaryManager &Summaries = getSummaryManager(C);
1051
231
  const Decl *CD = &Pred->getCodeDecl();
1052
231
  RetEffect RE = RetEffect::MakeNoRet();
1053
1054
  // FIXME: What is the convention for blocks? Is there one?
1055
231
  if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CD)) {
1056
131
    const RetainSummary *Summ = Summaries.getSummary(AnyCall(MD));
1057
131
    RE = Summ->getRetEffect();
1058
131
  } else 
if (const FunctionDecl *100
FD100
= dyn_cast<FunctionDecl>(CD)) {
1059
100
    if (!isa<CXXMethodDecl>(FD)) {
1060
99
      const RetainSummary *Summ = Summaries.getSummary(AnyCall(FD));
1061
99
      RE = Summ->getRetEffect();
1062
99
    }
1063
100
  }
1064
1065
231
  return checkReturnWithRetEffect(S, C, Pred, RE, X, Sym, state);
1066
231
}
1067
1068
ExplodedNode * RetainCountChecker::checkReturnWithRetEffect(const ReturnStmt *S,
1069
                                                  CheckerContext &C,
1070
                                                  ExplodedNode *Pred,
1071
                                                  RetEffect RE, RefVal X,
1072
                                                  SymbolRef Sym,
1073
231
                                                  ProgramStateRef state) const {
1074
  // HACK: Ignore retain-count issues on values accessed through ivars,
1075
  // because of cases like this:
1076
  //   [_contentView retain];
1077
  //   [_contentView removeFromSuperview];
1078
  //   [self addSubview:_contentView]; // invalidates 'self'
1079
  //   [_contentView release];
1080
231
  if (X.getIvarAccessHistory() != RefVal::IvarAccessHistory::None)
1081
14
    return Pred;
1082
1083
  // Any leaks or other errors?
1084
217
  if (X.isReturnedOwned() && 
X.getCount() == 0150
) {
1085
140
    if (RE.getKind() != RetEffect::NoRet) {
1086
125
      if (!RE.isOwned()) {
1087
1088
        // The returning type is a CF, we expect the enclosing method should
1089
        // return ownership.
1090
53
        X = X ^ RefVal::ErrorLeakReturned;
1091
1092
        // Generate an error node.
1093
53
        state = setRefBinding(state, Sym, X);
1094
1095
53
        static CheckerProgramPointTag ReturnOwnLeakTag(this, "ReturnsOwnLeak");
1096
53
        ExplodedNode *N = C.addTransition(state, Pred, &ReturnOwnLeakTag);
1097
53
        if (N) {
1098
53
          const LangOptions &LOpts = C.getASTContext().getLangOpts();
1099
53
          auto R =
1100
53
              std::make_unique<RefLeakReport>(*LeakAtReturn, LOpts, N, Sym, C);
1101
53
          C.emitReport(std::move(R));
1102
53
        }
1103
53
        return N;
1104
53
      }
1105
125
    }
1106
140
  } else 
if (77
X.isReturnedNotOwned()77
) {
1107
67
    if (RE.isOwned()) {
1108
25
      if (X.getIvarAccessHistory() ==
1109
25
            RefVal::IvarAccessHistory::AccessedDirectly) {
1110
        // Assume the method was trying to transfer a +1 reference from a
1111
        // strong ivar to the caller.
1112
0
        state = setRefBinding(state, Sym,
1113
0
                              X.releaseViaIvar() ^ RefVal::ReturnedOwned);
1114
25
      } else {
1115
        // Trying to return a not owned object to a caller expecting an
1116
        // owned object.
1117
25
        state = setRefBinding(state, Sym, X ^ RefVal::ErrorReturnedNotOwned);
1118
1119
25
        static CheckerProgramPointTag
1120
25
            ReturnNotOwnedTag(this, "ReturnNotOwnedForOwned");
1121
1122
25
        ExplodedNode *N = C.addTransition(state, Pred, &ReturnNotOwnedTag);
1123
25
        if (N) {
1124
25
          auto R = std::make_unique<RefCountReport>(
1125
25
              *ReturnNotOwnedForOwned, C.getASTContext().getLangOpts(), N, Sym);
1126
25
          C.emitReport(std::move(R));
1127
25
        }
1128
25
        return N;
1129
25
      }
1130
25
    }
1131
67
  }
1132
139
  return Pred;
1133
217
}
1134
1135
//===----------------------------------------------------------------------===//
1136
// Check various ways a symbol can be invalidated.
1137
//===----------------------------------------------------------------------===//
1138
1139
void RetainCountChecker::checkBind(SVal loc, SVal val, const Stmt *S,
1140
2.11k
                                   CheckerContext &C) const {
1141
2.11k
  ProgramStateRef state = C.getState();
1142
2.11k
  const MemRegion *MR = loc.getAsRegion();
1143
1144
  // Find all symbols referenced by 'val' that we are tracking
1145
  // and stop tracking them.
1146
2.11k
  if (MR && shouldEscapeRegion(MR)) {
1147
305
    state = state->scanReachableSymbols<StopTrackingCallback>(val).getState();
1148
305
    C.addTransition(state);
1149
305
  }
1150
2.11k
}
1151
1152
ProgramStateRef RetainCountChecker::evalAssume(ProgramStateRef state,
1153
                                               SVal Cond,
1154
14.8k
                                               bool Assumption) const {
1155
  // FIXME: We may add to the interface of evalAssume the list of symbols
1156
  //  whose assumptions have changed.  For now we just iterate through the
1157
  //  bindings and check if any of the tracked symbols are NULL.  This isn't
1158
  //  too bad since the number of symbols we will track in practice are
1159
  //  probably small and evalAssume is only called at branches and a few
1160
  //  other places.
1161
14.8k
  RefBindingsTy B = state->get<RefBindings>();
1162
1163
14.8k
  if (B.isEmpty())
1164
7.35k
    return state;
1165
1166
7.47k
  bool changed = false;
1167
7.47k
  RefBindingsTy::Factory &RefBFactory = state->get_context<RefBindings>();
1168
7.47k
  ConstraintManager &CMgr = state->getConstraintManager();
1169
1170
9.47k
  for (auto &I : B) {
1171
    // Check if the symbol is null stop tracking the symbol.
1172
9.47k
    ConditionTruthVal AllocFailed = CMgr.isNull(state, I.first);
1173
9.47k
    if (AllocFailed.isConstrainedTrue()) {
1174
1.13k
      changed = true;
1175
1.13k
      B = RefBFactory.remove(B, I.first);
1176
1.13k
    }
1177
9.47k
  }
1178
1179
7.47k
  if (changed)
1180
1.13k
    state = state->set<RefBindings>(B);
1181
1182
7.47k
  return state;
1183
14.8k
}
1184
1185
ProgramStateRef RetainCountChecker::checkRegionChanges(
1186
    ProgramStateRef state, const InvalidatedSymbols *invalidated,
1187
    ArrayRef<const MemRegion *> ExplicitRegions,
1188
    ArrayRef<const MemRegion *> Regions, const LocationContext *LCtx,
1189
5.98k
    const CallEvent *Call) const {
1190
5.98k
  if (!invalidated)
1191
2.67k
    return state;
1192
1193
3.30k
  llvm::SmallPtrSet<SymbolRef, 8> AllowedSymbols;
1194
1195
3.30k
  for (const MemRegion *I : ExplicitRegions)
1196
2.76k
    if (const SymbolicRegion *SR = I->StripCasts()->getAs<SymbolicRegion>())
1197
2.32k
      AllowedSymbols.insert(SR->getSymbol());
1198
1199
7.13k
  for (SymbolRef sym : *invalidated) {
1200
7.13k
    if (AllowedSymbols.count(sym))
1201
2.32k
      continue;
1202
    // Remove any existing reference-count binding.
1203
4.80k
    state = removeRefBinding(state, sym);
1204
4.80k
  }
1205
3.30k
  return state;
1206
5.98k
}
1207
1208
ProgramStateRef
1209
RetainCountChecker::handleAutoreleaseCounts(ProgramStateRef state,
1210
                                            ExplodedNode *Pred,
1211
                                            const ProgramPointTag *Tag,
1212
                                            CheckerContext &Ctx,
1213
                                            SymbolRef Sym,
1214
                                            RefVal V,
1215
2.26k
                                            const ReturnStmt *S) const {
1216
2.26k
  unsigned ACnt = V.getAutoreleaseCount();
1217
1218
  // No autorelease counts?  Nothing to be done.
1219
2.26k
  if (!ACnt)
1220
2.10k
    return state;
1221
1222
166
  unsigned Cnt = V.getCount();
1223
1224
  // FIXME: Handle sending 'autorelease' to already released object.
1225
1226
166
  if (V.getKind() == RefVal::ReturnedOwned)
1227
86
    ++Cnt;
1228
1229
  // If we would over-release here, but we know the value came from an ivar,
1230
  // assume it was a strong ivar that's just been relinquished.
1231
166
  if (ACnt > Cnt &&
1232
166
      
V.getIvarAccessHistory() == RefVal::IvarAccessHistory::AccessedDirectly42
) {
1233
10
    V = V.releaseViaIvar();
1234
10
    --ACnt;
1235
10
  }
1236
1237
166
  if (ACnt <= Cnt) {
1238
128
    if (ACnt == Cnt) {
1239
108
      V.clearCounts();
1240
108
      if (V.getKind() == RefVal::ReturnedOwned) {
1241
75
        V = V ^ RefVal::ReturnedNotOwned;
1242
75
      } else {
1243
33
        V = V ^ RefVal::NotOwned;
1244
33
      }
1245
108
    } else {
1246
20
      V.setCount(V.getCount() - ACnt);
1247
20
      V.setAutoreleaseCount(0);
1248
20
    }
1249
128
    return setRefBinding(state, Sym, V);
1250
128
  }
1251
1252
  // HACK: Ignore retain-count issues on values accessed through ivars,
1253
  // because of cases like this:
1254
  //   [_contentView retain];
1255
  //   [_contentView removeFromSuperview];
1256
  //   [self addSubview:_contentView]; // invalidates 'self'
1257
  //   [_contentView release];
1258
38
  if (V.getIvarAccessHistory() != RefVal::IvarAccessHistory::None)
1259
6
    return state;
1260
1261
  // Woah!  More autorelease counts then retain counts left.
1262
  // Emit hard error.
1263
32
  V = V ^ RefVal::ErrorOverAutorelease;
1264
32
  state = setRefBinding(state, Sym, V);
1265
1266
32
  ExplodedNode *N = Ctx.generateSink(state, Pred, Tag);
1267
32
  if (N) {
1268
32
    SmallString<128> sbuf;
1269
32
    llvm::raw_svector_ostream os(sbuf);
1270
32
    os << "Object was autoreleased ";
1271
32
    if (V.getAutoreleaseCount() > 1)
1272
17
      os << V.getAutoreleaseCount() << " times but the object ";
1273
15
    else
1274
15
      os << "but ";
1275
32
    os << "has a +" << V.getCount() << " retain count";
1276
1277
32
    const LangOptions &LOpts = Ctx.getASTContext().getLangOpts();
1278
32
    auto R = std::make_unique<RefCountReport>(*OverAutorelease, LOpts, N, Sym,
1279
32
                                              os.str());
1280
32
    Ctx.emitReport(std::move(R));
1281
32
  }
1282
1283
32
  return nullptr;
1284
38
}
1285
1286
ProgramStateRef
1287
RetainCountChecker::handleSymbolDeath(ProgramStateRef state,
1288
                                      SymbolRef sid, RefVal V,
1289
1.64k
                                    SmallVectorImpl<SymbolRef> &Leaked) const {
1290
1.64k
  bool hasLeak;
1291
1292
  // HACK: Ignore retain-count issues on values accessed through ivars,
1293
  // because of cases like this:
1294
  //   [_contentView retain];
1295
  //   [_contentView removeFromSuperview];
1296
  //   [self addSubview:_contentView]; // invalidates 'self'
1297
  //   [_contentView release];
1298
1.64k
  if (V.getIvarAccessHistory() != RefVal::IvarAccessHistory::None)
1299
112
    hasLeak = false;
1300
1.52k
  else if (V.isOwned())
1301
306
    hasLeak = true;
1302
1.22k
  else if (V.isNotOwned() || 
V.isReturnedOwned()829
)
1303
490
    hasLeak = (V.getCount() > 0);
1304
732
  else
1305
732
    hasLeak = false;
1306
1307
1.64k
  if (!hasLeak)
1308
1.25k
    return removeRefBinding(state, sid);
1309
1310
386
  Leaked.push_back(sid);
1311
386
  return setRefBinding(state, sid, V ^ RefVal::ErrorLeak);
1312
1.64k
}
1313
1314
ExplodedNode *
1315
RetainCountChecker::processLeaks(ProgramStateRef state,
1316
                                 SmallVectorImpl<SymbolRef> &Leaked,
1317
                                 CheckerContext &Ctx,
1318
1.77k
                                 ExplodedNode *Pred) const {
1319
  // Generate an intermediate node representing the leak point.
1320
1.77k
  ExplodedNode *N = Ctx.addTransition(state, Pred);
1321
1.77k
  const LangOptions &LOpts = Ctx.getASTContext().getLangOpts();
1322
1323
1.77k
  if (N) {
1324
1.77k
    for (SymbolRef L : Leaked) {
1325
386
      const RefCountBug &BT = Pred ? *LeakWithinFunction : 
*LeakAtReturn0
;
1326
386
      Ctx.emitReport(std::make_unique<RefLeakReport>(BT, LOpts, N, L, Ctx));
1327
386
    }
1328
1.77k
  }
1329
1330
1.77k
  return N;
1331
1.77k
}
1332
1333
2.10k
void RetainCountChecker::checkBeginFunction(CheckerContext &Ctx) const {
1334
2.10k
  if (!Ctx.inTopFrame())
1335
645
    return;
1336
1337
1.46k
  RetainSummaryManager &SmrMgr = getSummaryManager(Ctx);
1338
1.46k
  const LocationContext *LCtx = Ctx.getLocationContext();
1339
1.46k
  const Decl *D = LCtx->getDecl();
1340
1.46k
  std::optional<AnyCall> C = AnyCall::forDecl(D);
1341
1342
1.46k
  if (!C || 
SmrMgr.isTrustedReferenceCountImplementation(D)1.42k
)
1343
39
    return;
1344
1345
1.42k
  ProgramStateRef state = Ctx.getState();
1346
1.42k
  const RetainSummary *FunctionSummary = SmrMgr.getSummary(*C);
1347
1.42k
  ArgEffects CalleeSideArgEffects = FunctionSummary->getArgEffects();
1348
1349
2.25k
  for (unsigned idx = 0, e = C->param_size(); idx != e; 
++idx827
) {
1350
827
    const ParmVarDecl *Param = C->parameters()[idx];
1351
827
    SymbolRef Sym = state->getSVal(state->getRegion(Param, LCtx)).getAsSymbol();
1352
1353
827
    QualType Ty = Param->getType();
1354
827
    const ArgEffect *AE = CalleeSideArgEffects.lookup(idx);
1355
827
    if (AE) {
1356
327
      ObjKind K = AE->getObjKind();
1357
327
      if (K == ObjKind::Generalized || 
K == ObjKind::OS313
||
1358
327
          
(289
TrackNSCFStartParam289
&&
(43
K == ObjKind::ObjC43
||
K == ObjKind::CF27
))) {
1359
81
        RefVal NewVal = AE->getKind() == DecRef ? 
RefVal::makeOwned(K, Ty)15
1360
81
                                                : 
RefVal::makeNotOwned(K, Ty)66
;
1361
81
        state = setRefBinding(state, Sym, NewVal);
1362
81
      }
1363
327
    }
1364
827
  }
1365
1366
1.42k
  Ctx.addTransition(state);
1367
1.42k
}
1368
1369
void RetainCountChecker::checkEndFunction(const ReturnStmt *RS,
1370
2.12k
                                          CheckerContext &Ctx) const {
1371
2.12k
  ExplodedNode *Pred = processReturn(RS, Ctx);
1372
1373
  // Created state cached out.
1374
2.12k
  if (!Pred) {
1375
12
    return;
1376
12
  }
1377
1378
2.11k
  ProgramStateRef state = Pred->getState();
1379
2.11k
  RefBindingsTy B = state->get<RefBindings>();
1380
1381
  // Don't process anything within synthesized bodies.
1382
2.11k
  const LocationContext *LCtx = Pred->getLocationContext();
1383
2.11k
  if (LCtx->getAnalysisDeclContext()->isBodyAutosynthesized()) {
1384
254
    assert(!LCtx->inTopFrame());
1385
254
    return;
1386
254
  }
1387
1388
1.85k
  for (auto &I : B) {
1389
606
    state = handleAutoreleaseCounts(state, Pred, /*Tag=*/nullptr, Ctx,
1390
606
                                    I.first, I.second);
1391
606
    if (!state)
1392
0
      return;
1393
606
  }
1394
1395
  // If the current LocationContext has a parent, don't check for leaks.
1396
  // We will do that later.
1397
  // FIXME: we should instead check for imbalances of the retain/releases,
1398
  // and suggest annotations.
1399
1.85k
  if (LCtx->getParent())
1400
445
    return;
1401
1402
1.41k
  B = state->get<RefBindings>();
1403
1.41k
  SmallVector<SymbolRef, 10> Leaked;
1404
1405
1.41k
  for (auto &I : B)
1406
243
    state = handleSymbolDeath(state, I.first, I.second, Leaked);
1407
1408
1.41k
  processLeaks(state, Leaked, Ctx, Pred);
1409
1.41k
}
1410
1411
void RetainCountChecker::checkDeadSymbols(SymbolReaper &SymReaper,
1412
13.0k
                                          CheckerContext &C) const {
1413
13.0k
  ExplodedNode *Pred = C.getPredecessor();
1414
1415
13.0k
  ProgramStateRef state = C.getState();
1416
13.0k
  SmallVector<SymbolRef, 10> Leaked;
1417
1418
  // Update counts from autorelease pools
1419
13.0k
  for (const auto &I: state->get<RefBindings>()) {
1420
7.99k
    SymbolRef Sym = I.first;
1421
7.99k
    if (SymReaper.isDead(Sym)) {
1422
1.41k
      static CheckerProgramPointTag Tag(this, "DeadSymbolAutorelease");
1423
1.41k
      const RefVal &V = I.second;
1424
1.41k
      state = handleAutoreleaseCounts(state, Pred, &Tag, C, Sym, V);
1425
1.41k
      if (!state)
1426
20
        return;
1427
1428
      // Fetch the new reference count from the state, and use it to handle
1429
      // this symbol.
1430
1.39k
      state = handleSymbolDeath(state, Sym, *getRefBinding(state, Sym), Leaked);
1431
1.39k
    }
1432
7.99k
  }
1433
1434
13.0k
  if (Leaked.empty()) {
1435
12.6k
    C.addTransition(state);
1436
12.6k
    return;
1437
12.6k
  }
1438
1439
365
  Pred = processLeaks(state, Leaked, C, Pred);
1440
1441
  // Did we cache out?
1442
365
  if (!Pred)
1443
0
    return;
1444
1445
  // Now generate a new node that nukes the old bindings.
1446
  // The only bindings left at this point are the leaked symbols.
1447
365
  RefBindingsTy::Factory &F = state->get_context<RefBindings>();
1448
365
  RefBindingsTy B = state->get<RefBindings>();
1449
1450
365
  for (SymbolRef L : Leaked)
1451
375
    B = F.remove(B, L);
1452
1453
365
  state = state->set<RefBindings>(B);
1454
365
  C.addTransition(state, Pred);
1455
365
}
1456
1457
void RetainCountChecker::printState(raw_ostream &Out, ProgramStateRef State,
1458
0
                                    const char *NL, const char *Sep) const {
1459
1460
0
  RefBindingsTy B = State->get<RefBindings>();
1461
1462
0
  if (B.isEmpty())
1463
0
    return;
1464
1465
0
  Out << Sep << NL;
1466
1467
0
  for (auto &I : B) {
1468
0
    Out << I.first << " : ";
1469
0
    I.second.print(Out);
1470
0
    Out << NL;
1471
0
  }
1472
0
}
1473
1474
//===----------------------------------------------------------------------===//
1475
// Checker registration.
1476
//===----------------------------------------------------------------------===//
1477
1478
std::unique_ptr<CheckerProgramPointTag> RetainCountChecker::DeallocSentTag;
1479
std::unique_ptr<CheckerProgramPointTag> RetainCountChecker::CastFailTag;
1480
1481
95
void ento::registerRetainCountBase(CheckerManager &Mgr) {
1482
95
  auto *Chk = Mgr.registerChecker<RetainCountChecker>();
1483
95
  Chk->DeallocSentTag =
1484
95
      std::make_unique<CheckerProgramPointTag>(Chk, "DeallocSent");
1485
95
  Chk->CastFailTag =
1486
95
      std::make_unique<CheckerProgramPointTag>(Chk, "DynamicCastFail");
1487
95
}
1488
1489
372
bool ento::shouldRegisterRetainCountBase(const CheckerManager &mgr) {
1490
372
  return true;
1491
372
}
1492
94
void ento::registerRetainCountChecker(CheckerManager &Mgr) {
1493
94
  auto *Chk = Mgr.getChecker<RetainCountChecker>();
1494
94
  Chk->TrackObjCAndCFObjects = true;
1495
94
  Chk->TrackNSCFStartParam = Mgr.getAnalyzerOptions().getCheckerBooleanOption(
1496
94
      Mgr.getCurrentCheckerName(), "TrackNSCFStartParam");
1497
1498
94
#define INIT_BUGTYPE(KIND)                                                     \
1499
752
  Chk->KIND = std::make_unique<RefCountBug>(Mgr.getCurrentCheckerName(),       \
1500
752
                                            RefCountBug::KIND);
1501
  // TODO: Ideally, we should have a checker for each of these bug types.
1502
94
  INIT_BUGTYPE(UseAfterRelease)
1503
94
  INIT_BUGTYPE(ReleaseNotOwned)
1504
94
  INIT_BUGTYPE(DeallocNotOwned)
1505
94
  INIT_BUGTYPE(FreeNotOwned)
1506
94
  INIT_BUGTYPE(OverAutorelease)
1507
94
  INIT_BUGTYPE(ReturnNotOwnedForOwned)
1508
94
  INIT_BUGTYPE(LeakWithinFunction)
1509
94
  INIT_BUGTYPE(LeakAtReturn)
1510
94
#undef INIT_BUGTYPE
1511
94
}
1512
1513
192
bool ento::shouldRegisterRetainCountChecker(const CheckerManager &mgr) {
1514
192
  return true;
1515
192
}
1516
1517
45
void ento::registerOSObjectRetainCountChecker(CheckerManager &Mgr) {
1518
45
  auto *Chk = Mgr.getChecker<RetainCountChecker>();
1519
45
  Chk->TrackOSObjects = true;
1520
1521
  // FIXME: We want bug reports to always have the same checker name associated
1522
  // with them, yet here, if RetainCountChecker is disabled but
1523
  // OSObjectRetainCountChecker is enabled, the checker names will be different.
1524
  // This hack will make it so that the checker name depends on which checker is
1525
  // enabled rather than on the registration order.
1526
  // For the most part, we want **non-hidden checkers** to be associated with
1527
  // diagnostics, and **hidden checker options** with the fine-tuning of
1528
  // modeling. Following this logic, OSObjectRetainCountChecker should be the
1529
  // latter, but we can't just remove it for backward compatibility reasons.
1530
45
#define LAZY_INIT_BUGTYPE(KIND)                                                \
1531
360
  if (!Chk->KIND)                                                              \
1532
360
    Chk->KIND = std::make_unique<RefCountBug>(Mgr.getCurrentCheckerName(),     \
1533
360
                                              RefCountBug::KIND);
1534
45
  LAZY_INIT_BUGTYPE(UseAfterRelease)
1535
45
  LAZY_INIT_BUGTYPE(ReleaseNotOwned)
1536
45
  LAZY_INIT_BUGTYPE(DeallocNotOwned)
1537
45
  LAZY_INIT_BUGTYPE(FreeNotOwned)
1538
45
  LAZY_INIT_BUGTYPE(OverAutorelease)
1539
45
  LAZY_INIT_BUGTYPE(ReturnNotOwnedForOwned)
1540
45
  LAZY_INIT_BUGTYPE(LeakWithinFunction)
1541
45
  LAZY_INIT_BUGTYPE(LeakAtReturn)
1542
45
#undef LAZY_INIT_BUGTYPE
1543
45
}
1544
1545
90
bool ento::shouldRegisterOSObjectRetainCountChecker(const CheckerManager &mgr) {
1546
90
  return true;
1547
90
}