Coverage Report

Created: 2022-01-18 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Checkers/MacOSKeychainAPIChecker.cpp
Line
Count
Source (jump to first uncovered line)
1
//==--- MacOSKeychainAPIChecker.cpp ------------------------------*- 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
// This checker flags misuses of KeyChainAPI. In particular, the password data
9
// allocated/returned by SecKeychainItemCopyContent,
10
// SecKeychainFindGenericPassword, SecKeychainFindInternetPassword functions has
11
// to be freed using a call to SecKeychainItemFreeContent.
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
15
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
16
#include "clang/StaticAnalyzer/Core/Checker.h"
17
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
18
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
19
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
20
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
21
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
22
#include "llvm/ADT/SmallString.h"
23
#include "llvm/Support/raw_ostream.h"
24
25
using namespace clang;
26
using namespace ento;
27
28
namespace {
29
class MacOSKeychainAPIChecker : public Checker<check::PreStmt<CallExpr>,
30
                                               check::PostStmt<CallExpr>,
31
                                               check::DeadSymbols,
32
                                               check::PointerEscape,
33
                                               eval::Assume> {
34
  mutable std::unique_ptr<BugType> BT;
35
36
public:
37
  /// AllocationState is a part of the checker specific state together with the
38
  /// MemRegion corresponding to the allocated data.
39
  struct AllocationState {
40
    /// The index of the allocator function.
41
    unsigned int AllocatorIdx;
42
    SymbolRef Region;
43
44
    AllocationState(const Expr *E, unsigned int Idx, SymbolRef R) :
45
      AllocatorIdx(Idx),
46
28
      Region(R) {}
47
48
2
    bool operator==(const AllocationState &X) const {
49
2
      return (AllocatorIdx == X.AllocatorIdx &&
50
2
              Region == X.Region);
51
2
    }
52
53
31
    void Profile(llvm::FoldingSetNodeID &ID) const {
54
31
      ID.AddInteger(AllocatorIdx);
55
31
      ID.AddPointer(Region);
56
31
    }
57
  };
58
59
  void checkPreStmt(const CallExpr *S, CheckerContext &C) const;
60
  void checkPostStmt(const CallExpr *S, CheckerContext &C) const;
61
  void checkDeadSymbols(SymbolReaper &SR, CheckerContext &C) const;
62
  ProgramStateRef checkPointerEscape(ProgramStateRef State,
63
                                     const InvalidatedSymbols &Escaped,
64
                                     const CallEvent *Call,
65
                                     PointerEscapeKind Kind) const;
66
  ProgramStateRef evalAssume(ProgramStateRef state, SVal Cond,
67
                             bool Assumption) const;
68
  void printState(raw_ostream &Out, ProgramStateRef State,
69
                  const char *NL, const char *Sep) const override;
70
71
private:
72
  typedef std::pair<SymbolRef, const AllocationState*> AllocationPair;
73
  typedef SmallVector<AllocationPair, 2> AllocationPairVec;
74
75
  enum APIKind {
76
    /// Denotes functions tracked by this checker.
77
    ValidAPI = 0,
78
    /// The functions commonly/mistakenly used in place of the given API.
79
    ErrorAPI = 1,
80
    /// The functions which may allocate the data. These are tracked to reduce
81
    /// the false alarm rate.
82
    PossibleAPI = 2
83
  };
84
  /// Stores the information about the allocator and deallocator functions -
85
  /// these are the functions the checker is tracking.
86
  struct ADFunctionInfo {
87
    const char* Name;
88
    unsigned int Param;
89
    unsigned int DeallocatorIdx;
90
    APIKind Kind;
91
  };
92
  static const unsigned InvalidIdx = 100000;
93
  static const unsigned FunctionsToTrackSize = 8;
94
  static const ADFunctionInfo FunctionsToTrack[FunctionsToTrackSize];
95
  /// The value, which represents no error return value for allocator functions.
96
  static const unsigned NoErr = 0;
97
98
  /// Given the function name, returns the index of the allocator/deallocator
99
  /// function.
100
  static unsigned getTrackedFunctionIndex(StringRef Name, bool IsAllocator);
101
102
14
  inline void initBugType() const {
103
14
    if (!BT)
104
2
      BT.reset(new BugType(this, "Improper use of SecKeychain API",
105
2
                           "API Misuse (Apple)"));
106
14
  }
107
108
  void generateDeallocatorMismatchReport(const AllocationPair &AP,
109
                                         const Expr *ArgExpr,
110
                                         CheckerContext &C) const;
111
112
  /// Find the allocation site for Sym on the path leading to the node N.
113
  const ExplodedNode *getAllocationNode(const ExplodedNode *N, SymbolRef Sym,
114
                                        CheckerContext &C) const;
115
116
  std::unique_ptr<PathSensitiveBugReport>
117
  generateAllocatedDataNotReleasedReport(const AllocationPair &AP,
118
                                         ExplodedNode *N,
119
                                         CheckerContext &C) const;
120
121
  /// Mark an AllocationPair interesting for diagnostic reporting.
122
  void markInteresting(PathSensitiveBugReport *R,
123
13
                       const AllocationPair &AP) const {
124
13
    R->markInteresting(AP.first);
125
13
    R->markInteresting(AP.second->Region);
126
13
  }
127
128
  /// The bug visitor which allows us to print extra diagnostics along the
129
  /// BugReport path. For example, showing the allocation site of the leaked
130
  /// region.
131
  class SecKeychainBugVisitor : public BugReporterVisitor {
132
  protected:
133
    // The allocated region symbol tracked by the main analysis.
134
    SymbolRef Sym;
135
136
  public:
137
14
    SecKeychainBugVisitor(SymbolRef S) : Sym(S) {}
138
139
14
    void Profile(llvm::FoldingSetNodeID &ID) const override {
140
14
      static int X = 0;
141
14
      ID.AddPointer(&X);
142
14
      ID.AddPointer(Sym);
143
14
    }
144
145
    PathDiagnosticPieceRef VisitNode(const ExplodedNode *N,
146
                                     BugReporterContext &BRC,
147
                                     PathSensitiveBugReport &BR) override;
148
  };
149
};
150
}
151
152
/// ProgramState traits to store the currently allocated (and not yet freed)
153
/// symbols. This is a map from the allocated content symbol to the
154
/// corresponding AllocationState.
155
REGISTER_MAP_WITH_PROGRAMSTATE(AllocatedData,
156
                               SymbolRef,
157
                               MacOSKeychainAPIChecker::AllocationState)
158
159
31
static bool isEnclosingFunctionParam(const Expr *E) {
160
31
  E = E->IgnoreParenCasts();
161
31
  if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) {
162
5
    const ValueDecl *VD = DRE->getDecl();
163
5
    if (isa<ImplicitParamDecl, ParmVarDecl>(VD))
164
4
      return true;
165
5
  }
166
27
  return false;
167
31
}
168
169
const MacOSKeychainAPIChecker::ADFunctionInfo
170
  MacOSKeychainAPIChecker::FunctionsToTrack[FunctionsToTrackSize] = {
171
    {"SecKeychainItemCopyContent", 4, 3, ValidAPI},                       // 0
172
    {"SecKeychainFindGenericPassword", 6, 3, ValidAPI},                   // 1
173
    {"SecKeychainFindInternetPassword", 13, 3, ValidAPI},                 // 2
174
    {"SecKeychainItemFreeContent", 1, InvalidIdx, ValidAPI},              // 3
175
    {"SecKeychainItemCopyAttributesAndData", 5, 5, ValidAPI},             // 4
176
    {"SecKeychainItemFreeAttributesAndData", 1, InvalidIdx, ValidAPI},    // 5
177
    {"free", 0, InvalidIdx, ErrorAPI},                                    // 6
178
    {"CFStringCreateWithBytesNoCopy", 1, InvalidIdx, PossibleAPI},        // 7
179
};
180
181
unsigned MacOSKeychainAPIChecker::getTrackedFunctionIndex(StringRef Name,
182
555
                                                          bool IsAllocator) {
183
4.16k
  for (unsigned I = 0; I < FunctionsToTrackSize; 
++I3.60k
) {
184
3.75k
    ADFunctionInfo FI = FunctionsToTrack[I];
185
3.75k
    if (FI.Name != Name)
186
3.60k
      continue;
187
    // Make sure the function is of the right type (allocator vs deallocator).
188
150
    if (IsAllocator && 
(FI.DeallocatorIdx == InvalidIdx)125
)
189
50
      return InvalidIdx;
190
100
    if (!IsAllocator && 
(FI.DeallocatorIdx != InvalidIdx)25
)
191
0
      return InvalidIdx;
192
193
100
    return I;
194
100
  }
195
  // The function is not tracked.
196
405
  return InvalidIdx;
197
555
}
198
199
0
static bool isBadDeallocationArgument(const MemRegion *Arg) {
200
0
  if (!Arg)
201
0
    return false;
202
0
  return isa<AllocaRegion, BlockDataRegion, TypedRegion>(Arg);
203
0
}
204
205
/// Given the address expression, retrieve the value it's pointing to. Assume
206
/// that value is itself an address, and return the corresponding symbol.
207
static SymbolRef getAsPointeeSymbol(const Expr *Expr,
208
61
                                    CheckerContext &C) {
209
61
  ProgramStateRef State = C.getState();
210
61
  SVal ArgV = C.getSVal(Expr);
211
212
61
  if (Optional<loc::MemRegionVal> X = ArgV.getAs<loc::MemRegionVal>()) {
213
57
    StoreManager& SM = C.getStoreManager();
214
57
    SymbolRef sym = SM.getBinding(State->getStore(), *X).getAsLocSymbol();
215
57
    if (sym)
216
31
      return sym;
217
57
  }
218
30
  return nullptr;
219
61
}
220
221
// Report deallocator mismatch. Remove the region from tracking - reporting a
222
// missing free error after this one is redundant.
223
void MacOSKeychainAPIChecker::
224
  generateDeallocatorMismatchReport(const AllocationPair &AP,
225
                                    const Expr *ArgExpr,
226
4
                                    CheckerContext &C) const {
227
4
  ProgramStateRef State = C.getState();
228
4
  State = State->remove<AllocatedData>(AP.first);
229
4
  ExplodedNode *N = C.generateNonFatalErrorNode(State);
230
231
4
  if (!N)
232
0
    return;
233
4
  initBugType();
234
4
  SmallString<80> sbuf;
235
4
  llvm::raw_svector_ostream os(sbuf);
236
4
  unsigned int PDeallocIdx =
237
4
               FunctionsToTrack[AP.second->AllocatorIdx].DeallocatorIdx;
238
239
4
  os << "Deallocator doesn't match the allocator: '"
240
4
     << FunctionsToTrack[PDeallocIdx].Name << "' should be used.";
241
4
  auto Report = std::make_unique<PathSensitiveBugReport>(*BT, os.str(), N);
242
4
  Report->addVisitor(std::make_unique<SecKeychainBugVisitor>(AP.first));
243
4
  Report->addRange(ArgExpr->getSourceRange());
244
4
  markInteresting(Report.get(), AP);
245
4
  C.emitReport(std::move(Report));
246
4
}
247
248
void MacOSKeychainAPIChecker::checkPreStmt(const CallExpr *CE,
249
370
                                           CheckerContext &C) const {
250
370
  unsigned idx = InvalidIdx;
251
370
  ProgramStateRef State = C.getState();
252
253
370
  const FunctionDecl *FD = C.getCalleeDecl(CE);
254
370
  if (!FD || 
FD->getKind() != Decl::Function365
)
255
179
    return;
256
257
191
  StringRef funName = C.getCalleeName(FD);
258
191
  if (funName.empty())
259
0
    return;
260
261
  // If it is a call to an allocator function, it could be a double allocation.
262
191
  idx = getTrackedFunctionIndex(funName, true);
263
191
  if (idx != InvalidIdx) {
264
31
    unsigned paramIdx = FunctionsToTrack[idx].Param;
265
31
    if (CE->getNumArgs() <= paramIdx)
266
0
      return;
267
268
31
    const Expr *ArgExpr = CE->getArg(paramIdx);
269
31
    if (SymbolRef V = getAsPointeeSymbol(ArgExpr, C))
270
3
      if (const AllocationState *AS = State->get<AllocatedData>(V)) {
271
        // Remove the value from the state. The new symbol will be added for
272
        // tracking when the second allocator is processed in checkPostStmt().
273
1
        State = State->remove<AllocatedData>(V);
274
1
        ExplodedNode *N = C.generateNonFatalErrorNode(State);
275
1
        if (!N)
276
0
          return;
277
1
        initBugType();
278
1
        SmallString<128> sbuf;
279
1
        llvm::raw_svector_ostream os(sbuf);
280
1
        unsigned int DIdx = FunctionsToTrack[AS->AllocatorIdx].DeallocatorIdx;
281
1
        os << "Allocated data should be released before another call to "
282
1
            << "the allocator: missing a call to '"
283
1
            << FunctionsToTrack[DIdx].Name
284
1
            << "'.";
285
1
        auto Report =
286
1
            std::make_unique<PathSensitiveBugReport>(*BT, os.str(), N);
287
1
        Report->addVisitor(std::make_unique<SecKeychainBugVisitor>(V));
288
1
        Report->addRange(ArgExpr->getSourceRange());
289
1
        Report->markInteresting(AS->Region);
290
1
        C.emitReport(std::move(Report));
291
1
      }
292
31
    return;
293
31
  }
294
295
  // Is it a call to one of deallocator functions?
296
160
  idx = getTrackedFunctionIndex(funName, false);
297
160
  if (idx == InvalidIdx)
298
135
    return;
299
300
25
  unsigned paramIdx = FunctionsToTrack[idx].Param;
301
25
  if (CE->getNumArgs() <= paramIdx)
302
1
    return;
303
304
  // Check the argument to the deallocator.
305
24
  const Expr *ArgExpr = CE->getArg(paramIdx);
306
24
  SVal ArgSVal = C.getSVal(ArgExpr);
307
308
  // Undef is reported by another checker.
309
24
  if (ArgSVal.isUndef())
310
1
    return;
311
312
23
  SymbolRef ArgSM = ArgSVal.getAsLocSymbol();
313
314
  // If the argument is coming from the heap, globals, or unknown, do not
315
  // report it.
316
23
  bool RegionArgIsBad = false;
317
23
  if (!ArgSM) {
318
0
    if (!isBadDeallocationArgument(ArgSVal.getAsRegion()))
319
0
      return;
320
0
    RegionArgIsBad = true;
321
0
  }
322
323
  // Is the argument to the call being tracked?
324
23
  const AllocationState *AS = State->get<AllocatedData>(ArgSM);
325
23
  if (!AS)
326
4
    return;
327
328
  // TODO: We might want to report double free here.
329
  // (that would involve tracking all the freed symbols in the checker state).
330
19
  if (RegionArgIsBad) {
331
    // It is possible that this is a false positive - the argument might
332
    // have entered as an enclosing function parameter.
333
0
    if (isEnclosingFunctionParam(ArgExpr))
334
0
      return;
335
336
0
    ExplodedNode *N = C.generateNonFatalErrorNode(State);
337
0
    if (!N)
338
0
      return;
339
0
    initBugType();
340
0
    auto Report = std::make_unique<PathSensitiveBugReport>(
341
0
        *BT, "Trying to free data which has not been allocated.", N);
342
0
    Report->addRange(ArgExpr->getSourceRange());
343
0
    if (AS)
344
0
      Report->markInteresting(AS->Region);
345
0
    C.emitReport(std::move(Report));
346
0
    return;
347
0
  }
348
349
  // Process functions which might deallocate.
350
19
  if (FunctionsToTrack[idx].Kind == PossibleAPI) {
351
352
5
    if (funName == "CFStringCreateWithBytesNoCopy") {
353
5
      const Expr *DeallocatorExpr = CE->getArg(5)->IgnoreParenCasts();
354
      // NULL ~ default deallocator, so warn.
355
5
      if (DeallocatorExpr->isNullPointerConstant(C.getASTContext(),
356
5
          Expr::NPC_ValueDependentIsNotNull)) {
357
1
        const AllocationPair AP = std::make_pair(ArgSM, AS);
358
1
        generateDeallocatorMismatchReport(AP, ArgExpr, C);
359
1
        return;
360
1
      }
361
      // One of the default allocators, so warn.
362
4
      if (const DeclRefExpr *DE = dyn_cast<DeclRefExpr>(DeallocatorExpr)) {
363
3
        StringRef DeallocatorName = DE->getFoundDecl()->getName();
364
3
        if (DeallocatorName == "kCFAllocatorDefault" ||
365
3
            
DeallocatorName == "kCFAllocatorSystemDefault"2
||
366
3
            
DeallocatorName == "kCFAllocatorMalloc"2
) {
367
1
          const AllocationPair AP = std::make_pair(ArgSM, AS);
368
1
          generateDeallocatorMismatchReport(AP, ArgExpr, C);
369
1
          return;
370
1
        }
371
        // If kCFAllocatorNull, which does not deallocate, we still have to
372
        // find the deallocator.
373
2
        if (DE->getFoundDecl()->getName() == "kCFAllocatorNull")
374
1
          return;
375
2
      }
376
      // In all other cases, assume the user supplied a correct deallocator
377
      // that will free memory so stop tracking.
378
2
      State = State->remove<AllocatedData>(ArgSM);
379
2
      C.addTransition(State);
380
2
      return;
381
4
    }
382
383
0
    llvm_unreachable("We know of no other possible APIs.");
384
0
  }
385
386
  // The call is deallocating a value we previously allocated, so remove it
387
  // from the next state.
388
14
  State = State->remove<AllocatedData>(ArgSM);
389
390
  // Check if the proper deallocator is used.
391
14
  unsigned int PDeallocIdx = FunctionsToTrack[AS->AllocatorIdx].DeallocatorIdx;
392
14
  if (PDeallocIdx != idx || 
(FunctionsToTrack[idx].Kind == ErrorAPI)12
) {
393
2
    const AllocationPair AP = std::make_pair(ArgSM, AS);
394
2
    generateDeallocatorMismatchReport(AP, ArgExpr, C);
395
2
    return;
396
2
  }
397
398
12
  C.addTransition(State);
399
12
}
400
401
void MacOSKeychainAPIChecker::checkPostStmt(const CallExpr *CE,
402
373
                                            CheckerContext &C) const {
403
373
  ProgramStateRef State = C.getState();
404
373
  const FunctionDecl *FD = C.getCalleeDecl(CE);
405
373
  if (!FD || 
FD->getKind() != Decl::Function368
)
406
182
    return;
407
408
191
  StringRef funName = C.getCalleeName(FD);
409
410
  // If a value has been allocated, add it to the set for tracking.
411
191
  unsigned idx = getTrackedFunctionIndex(funName, true);
412
191
  if (idx == InvalidIdx)
413
160
    return;
414
415
31
  const Expr *ArgExpr = CE->getArg(FunctionsToTrack[idx].Param);
416
  // If the argument entered as an enclosing function parameter, skip it to
417
  // avoid false positives.
418
31
  if (isEnclosingFunctionParam(ArgExpr) &&
419
31
      
C.getLocationContext()->getParent() == nullptr4
)
420
1
    return;
421
422
30
  if (SymbolRef V = getAsPointeeSymbol(ArgExpr, C)) {
423
    // If the argument points to something that's not a symbolic region, it
424
    // can be:
425
    //  - unknown (cannot reason about it)
426
    //  - undefined (already reported by other checker)
427
    //  - constant (null - should not be tracked,
428
    //              other constant will generate a compiler warning)
429
    //  - goto (should be reported by other checker)
430
431
    // The call return value symbol should stay alive for as long as the
432
    // allocated value symbol, since our diagnostics depend on the value
433
    // returned by the call. Ex: Data should only be freed if noErr was
434
    // returned during allocation.)
435
28
    SymbolRef RetStatusSymbol = C.getSVal(CE).getAsSymbol();
436
28
    C.getSymbolManager().addSymbolDependency(V, RetStatusSymbol);
437
438
    // Track the allocated value in the checker state.
439
28
    State = State->set<AllocatedData>(V, AllocationState(ArgExpr, idx,
440
28
                                                         RetStatusSymbol));
441
28
    assert(State);
442
0
    C.addTransition(State);
443
28
  }
444
30
}
445
446
// TODO: This logic is the same as in Malloc checker.
447
const ExplodedNode *
448
MacOSKeychainAPIChecker::getAllocationNode(const ExplodedNode *N,
449
                                           SymbolRef Sym,
450
9
                                           CheckerContext &C) const {
451
9
  const LocationContext *LeakContext = N->getLocationContext();
452
  // Walk the ExplodedGraph backwards and find the first node that referred to
453
  // the tracked symbol.
454
9
  const ExplodedNode *AllocNode = N;
455
456
303
  while (N) {
457
303
    if (!N->getState()->get<AllocatedData>(Sym))
458
9
      break;
459
    // Allocation node, is the last node in the current or parent context in
460
    // which the symbol was tracked.
461
294
    const LocationContext *NContext = N->getLocationContext();
462
294
    if (NContext == LeakContext ||
463
294
        
NContext->isParentOf(LeakContext)74
)
464
220
      AllocNode = N;
465
294
    N = N->pred_empty() ? 
nullptr0
: *(N->pred_begin());
466
294
  }
467
468
9
  return AllocNode;
469
9
}
470
471
std::unique_ptr<PathSensitiveBugReport>
472
MacOSKeychainAPIChecker::generateAllocatedDataNotReleasedReport(
473
9
    const AllocationPair &AP, ExplodedNode *N, CheckerContext &C) const {
474
9
  const ADFunctionInfo &FI = FunctionsToTrack[AP.second->AllocatorIdx];
475
9
  initBugType();
476
9
  SmallString<70> sbuf;
477
9
  llvm::raw_svector_ostream os(sbuf);
478
9
  os << "Allocated data is not released: missing a call to '"
479
9
      << FunctionsToTrack[FI.DeallocatorIdx].Name << "'.";
480
481
  // Most bug reports are cached at the location where they occurred.
482
  // With leaks, we want to unique them by the location where they were
483
  // allocated, and only report a single path.
484
9
  PathDiagnosticLocation LocUsedForUniqueing;
485
9
  const ExplodedNode *AllocNode = getAllocationNode(N, AP.first, C);
486
9
  const Stmt *AllocStmt = AllocNode->getStmtForDiagnostics();
487
488
9
  if (AllocStmt)
489
9
    LocUsedForUniqueing = PathDiagnosticLocation::createBegin(AllocStmt,
490
9
                                              C.getSourceManager(),
491
9
                                              AllocNode->getLocationContext());
492
493
9
  auto Report = std::make_unique<PathSensitiveBugReport>(
494
9
      *BT, os.str(), N, LocUsedForUniqueing,
495
9
      AllocNode->getLocationContext()->getDecl());
496
497
9
  Report->addVisitor(std::make_unique<SecKeychainBugVisitor>(AP.first));
498
9
  markInteresting(Report.get(), AP);
499
9
  return Report;
500
9
}
501
502
/// If the return symbol is assumed to be error, remove the allocated info
503
/// from consideration.
504
ProgramStateRef MacOSKeychainAPIChecker::evalAssume(ProgramStateRef State,
505
                                                    SVal Cond,
506
1.42k
                                                    bool Assumption) const {
507
1.42k
  AllocatedDataTy AMap = State->get<AllocatedData>();
508
1.42k
  if (AMap.isEmpty())
509
1.35k
    return State;
510
511
72
  auto *CondBSE = dyn_cast_or_null<BinarySymExpr>(Cond.getAsSymbol());
512
72
  if (!CondBSE)
513
22
    return State;
514
50
  BinaryOperator::Opcode OpCode = CondBSE->getOpcode();
515
50
  if (OpCode != BO_EQ && 
OpCode != BO_NE6
)
516
0
    return State;
517
518
  // Match for a restricted set of patterns for cmparison of error codes.
519
  // Note, the comparisons of type '0 == st' are transformed into SymIntExpr.
520
50
  SymbolRef ReturnSymbol = nullptr;
521
50
  if (auto *SIE = dyn_cast<SymIntExpr>(CondBSE)) {
522
50
    const llvm::APInt &RHS = SIE->getRHS();
523
50
    bool ErrorIsReturned = (OpCode == BO_EQ && 
RHS != NoErr44
) ||
524
50
                           
(44
OpCode == BO_NE44
&&
RHS == NoErr6
);
525
50
    if (!Assumption)
526
25
      ErrorIsReturned = !ErrorIsReturned;
527
50
    if (ErrorIsReturned)
528
25
      ReturnSymbol = SIE->getLHS();
529
50
  }
530
531
50
  if (ReturnSymbol)
532
51
    
for (auto I = AMap.begin(), E = AMap.end(); 25
I != E;
++I26
) {
533
26
      if (ReturnSymbol == I->second.Region)
534
20
        State = State->remove<AllocatedData>(I->first);
535
26
    }
536
537
50
  return State;
538
50
}
539
540
void MacOSKeychainAPIChecker::checkDeadSymbols(SymbolReaper &SR,
541
1.84k
                                               CheckerContext &C) const {
542
1.84k
  ProgramStateRef State = C.getState();
543
1.84k
  AllocatedDataTy AMap = State->get<AllocatedData>();
544
1.84k
  if (AMap.isEmpty())
545
1.73k
    return;
546
547
116
  bool Changed = false;
548
116
  AllocationPairVec Errors;
549
235
  for (auto I = AMap.begin(), E = AMap.end(); I != E; 
++I119
) {
550
119
    if (!SR.isDead(I->first))
551
106
      continue;
552
553
13
    Changed = true;
554
13
    State = State->remove<AllocatedData>(I->first);
555
    // If the allocated symbol is null do not report.
556
13
    ConstraintManager &CMgr = State->getConstraintManager();
557
13
    ConditionTruthVal AllocFailed = CMgr.isNull(State, I.getKey());
558
13
    if (AllocFailed.isConstrainedTrue())
559
4
      continue;
560
9
    Errors.push_back(std::make_pair(I->first, &I->second));
561
9
  }
562
116
  if (!Changed) {
563
    // Generate the new, cleaned up state.
564
103
    C.addTransition(State);
565
103
    return;
566
103
  }
567
568
13
  static CheckerProgramPointTag Tag(this, "DeadSymbolsLeak");
569
13
  ExplodedNode *N = C.generateNonFatalErrorNode(C.getState(), &Tag);
570
13
  if (!N)
571
0
    return;
572
573
  // Generate the error reports.
574
13
  for (const auto &P : Errors)
575
9
    C.emitReport(generateAllocatedDataNotReleasedReport(P, N, C));
576
577
  // Generate the new, cleaned up state.
578
13
  C.addTransition(State, N);
579
13
}
580
581
ProgramStateRef MacOSKeychainAPIChecker::checkPointerEscape(
582
    ProgramStateRef State, const InvalidatedSymbols &Escaped,
583
300
    const CallEvent *Call, PointerEscapeKind Kind) const {
584
  // FIXME: This branch doesn't make any sense at all, but it is an overfitted
585
  // replacement for a previous overfitted code that was making even less sense.
586
300
  if (!Call || 
Call->getDecl()266
)
587
295
    return State;
588
589
5
  for (auto I : State->get<AllocatedData>()) {
590
2
    SymbolRef Sym = I.first;
591
2
    if (Escaped.count(Sym))
592
0
      State = State->remove<AllocatedData>(Sym);
593
594
    // This checker is special. Most checkers in fact only track symbols of
595
    // SymbolConjured type, eg. symbols returned from functions such as
596
    // malloc(). This checker tracks symbols returned as out-parameters.
597
    //
598
    // When a function is evaluated conservatively, the out-parameter's pointee
599
    // base region gets invalidated with a SymbolConjured. If the base region is
600
    // larger than the region we're interested in, the value we're interested in
601
    // would be SymbolDerived based on that SymbolConjured. However, such
602
    // SymbolDerived will never be listed in the Escaped set when the base
603
    // region is invalidated because ExprEngine doesn't know which symbols
604
    // were derived from a given symbol, while there can be infinitely many
605
    // valid symbols derived from any given symbol.
606
    //
607
    // Hence the extra boilerplate: remove the derived symbol when its parent
608
    // symbol escapes.
609
    //
610
2
    if (const auto *SD = dyn_cast<SymbolDerived>(Sym)) {
611
2
      SymbolRef ParentSym = SD->getParentSymbol();
612
2
      if (Escaped.count(ParentSym))
613
2
        State = State->remove<AllocatedData>(Sym);
614
2
    }
615
2
  }
616
5
  return State;
617
300
}
618
619
PathDiagnosticPieceRef
620
MacOSKeychainAPIChecker::SecKeychainBugVisitor::VisitNode(
621
    const ExplodedNode *N, BugReporterContext &BRC,
622
1.06k
    PathSensitiveBugReport &BR) {
623
1.06k
  const AllocationState *AS = N->getState()->get<AllocatedData>(Sym);
624
1.06k
  if (!AS)
625
657
    return nullptr;
626
412
  const AllocationState *ASPrev =
627
412
      N->getFirstPred()->getState()->get<AllocatedData>(Sym);
628
412
  if (ASPrev)
629
399
    return nullptr;
630
631
  // (!ASPrev && AS) ~ We started tracking symbol in node N, it must be the
632
  // allocation site.
633
13
  const CallExpr *CE =
634
13
      cast<CallExpr>(N->getLocation().castAs<StmtPoint>().getStmt());
635
13
  const FunctionDecl *funDecl = CE->getDirectCallee();
636
13
  assert(funDecl && "We do not support indirect function calls as of now.");
637
0
  StringRef funName = funDecl->getName();
638
639
  // Get the expression of the corresponding argument.
640
13
  unsigned Idx = getTrackedFunctionIndex(funName, true);
641
13
  assert(Idx != InvalidIdx && "This should be a call to an allocator.");
642
0
  const Expr *ArgExpr = CE->getArg(FunctionsToTrack[Idx].Param);
643
13
  PathDiagnosticLocation Pos(ArgExpr, BRC.getSourceManager(),
644
13
                             N->getLocationContext());
645
13
  return std::make_shared<PathDiagnosticEventPiece>(Pos,
646
13
                                                    "Data is allocated here.");
647
412
}
648
649
void MacOSKeychainAPIChecker::printState(raw_ostream &Out,
650
                                         ProgramStateRef State,
651
                                         const char *NL,
652
0
                                         const char *Sep) const {
653
654
0
  AllocatedDataTy AMap = State->get<AllocatedData>();
655
656
0
  if (!AMap.isEmpty()) {
657
0
    Out << Sep << "KeychainAPIChecker :" << NL;
658
0
    for (auto I = AMap.begin(), E = AMap.end(); I != E; ++I) {
659
0
      I.getKey()->dumpToStream(Out);
660
0
    }
661
0
  }
662
0
}
663
664
665
48
void ento::registerMacOSKeychainAPIChecker(CheckerManager &mgr) {
666
48
  mgr.registerChecker<MacOSKeychainAPIChecker>();
667
48
}
668
669
96
bool ento::shouldRegisterMacOSKeychainAPIChecker(const CheckerManager &mgr) {
670
96
  return true;
671
96
}