Coverage Report

Created: 2023-09-30 09:22

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Core/ProgramState.cpp
Line
Count
Source (jump to first uncovered line)
1
//= ProgramState.cpp - Path-Sensitive "State" for tracking values --*- 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 implements ProgramState and ProgramStateManager.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
14
#include "clang/Analysis/CFG.h"
15
#include "clang/Basic/JsonSupport.h"
16
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
17
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
18
#include "clang/StaticAnalyzer/Core/PathSensitive/DynamicType.h"
19
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
20
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
21
#include "llvm/Support/raw_ostream.h"
22
#include <optional>
23
24
using namespace clang;
25
using namespace ento;
26
27
namespace clang { namespace  ento {
28
/// Increments the number of times this state is referenced.
29
30
130M
void ProgramStateRetain(const ProgramState *state) {
31
130M
  ++const_cast<ProgramState*>(state)->refCount;
32
130M
}
33
34
/// Decrement the number of times this state is referenced.
35
120M
void ProgramStateRelease(const ProgramState *state) {
36
120M
  assert(state->refCount > 0);
37
120M
  ProgramState *s = const_cast<ProgramState*>(state);
38
120M
  if (--s->refCount == 0) {
39
627k
    ProgramStateManager &Mgr = s->getStateManager();
40
627k
    Mgr.StateSet.RemoveNode(s);
41
627k
    s->~ProgramState();
42
627k
    Mgr.freeStates.push_back(s);
43
627k
  }
44
120M
}
45
}}
46
47
ProgramState::ProgramState(ProgramStateManager *mgr, const Environment& env,
48
                 StoreRef st, GenericDataMap gdm)
49
16.2k
  : stateMgr(mgr),
50
16.2k
    Env(env),
51
16.2k
    store(st.getStore()),
52
16.2k
    GDM(gdm),
53
16.2k
    refCount(0) {
54
16.2k
  stateMgr->getStoreManager().incrementReferenceCount(store);
55
16.2k
}
56
57
ProgramState::ProgramState(const ProgramState &RHS)
58
4.64M
    : stateMgr(RHS.stateMgr), Env(RHS.Env), store(RHS.store), GDM(RHS.GDM),
59
4.64M
      PosteriorlyOverconstrained(RHS.PosteriorlyOverconstrained), refCount(0) {
60
4.64M
  stateMgr->getStoreManager().incrementReferenceCount(store);
61
4.64M
}
62
63
3.22M
ProgramState::~ProgramState() {
64
3.22M
  if (store)
65
2.81M
    stateMgr->getStoreManager().decrementReferenceCount(store);
66
3.22M
}
67
68
3.62k
int64_t ProgramState::getID() const {
69
3.62k
  return getStateManager().Alloc.identifyKnownAlignedObject<ProgramState>(this);
70
3.62k
}
71
72
ProgramStateManager::ProgramStateManager(ASTContext &Ctx,
73
                                         StoreManagerCreator CreateSMgr,
74
                                         ConstraintManagerCreator CreateCMgr,
75
                                         llvm::BumpPtrAllocator &alloc,
76
                                         ExprEngine *ExprEng)
77
16.2k
  : Eng(ExprEng), EnvMgr(alloc), GDMFactory(alloc),
78
16.2k
    svalBuilder(createSimpleSValBuilder(alloc, Ctx, *this)),
79
16.2k
    CallEventMgr(new CallEventManager(alloc)), Alloc(alloc) {
80
16.2k
  StoreMgr = (*CreateSMgr)(*this);
81
16.2k
  ConstraintMgr = (*CreateCMgr)(*this, ExprEng);
82
16.2k
}
83
84
85
16.2k
ProgramStateManager::~ProgramStateManager() {
86
16.2k
  for (GDMContextsTy::iterator I=GDMContexts.begin(), E=GDMContexts.end();
87
139k
       I!=E; 
++I123k
)
88
123k
    I->second.second(I->second.first);
89
16.2k
}
90
91
ProgramStateRef ProgramStateManager::removeDeadBindingsFromEnvironmentAndStore(
92
    ProgramStateRef state, const StackFrameContext *LCtx,
93
427k
    SymbolReaper &SymReaper) {
94
95
  // This code essentially performs a "mark-and-sweep" of the VariableBindings.
96
  // The roots are any Block-level exprs and Decls that our liveness algorithm
97
  // tells us are live.  We then see what Decls they may reference, and keep
98
  // those around.  This code more than likely can be made faster, and the
99
  // frequency of which this method is called should be experimented with
100
  // for optimum performance.
101
427k
  ProgramState NewState = *state;
102
103
427k
  NewState.Env = EnvMgr.removeDeadBindings(NewState.Env, SymReaper, state);
104
105
  // Clean up the store.
106
427k
  StoreRef newStore = StoreMgr->removeDeadBindings(NewState.getStore(), LCtx,
107
427k
                                                   SymReaper);
108
427k
  NewState.setStore(newStore);
109
427k
  SymReaper.setReapedStore(newStore);
110
111
427k
  return getPersistentState(NewState);
112
427k
}
113
114
ProgramStateRef ProgramState::bindLoc(Loc LV,
115
                                      SVal V,
116
                                      const LocationContext *LCtx,
117
164k
                                      bool notifyChanges) const {
118
164k
  ProgramStateManager &Mgr = getStateManager();
119
164k
  ProgramStateRef newState = makeWithStore(Mgr.StoreMgr->Bind(getStore(),
120
164k
                                                             LV, V));
121
164k
  const MemRegion *MR = LV.getAsRegion();
122
164k
  if (MR && 
notifyChanges164k
)
123
111k
    return Mgr.getOwningEngine().processRegionChange(newState, MR, LCtx);
124
125
53.6k
  return newState;
126
164k
}
127
128
ProgramStateRef
129
ProgramState::bindDefaultInitial(SVal loc, SVal V,
130
1.76k
                                 const LocationContext *LCtx) const {
131
1.76k
  ProgramStateManager &Mgr = getStateManager();
132
1.76k
  const MemRegion *R = loc.castAs<loc::MemRegionVal>().getRegion();
133
1.76k
  const StoreRef &newStore = Mgr.StoreMgr->BindDefaultInitial(getStore(), R, V);
134
1.76k
  ProgramStateRef new_state = makeWithStore(newStore);
135
1.76k
  return Mgr.getOwningEngine().processRegionChange(new_state, R, LCtx);
136
1.76k
}
137
138
ProgramStateRef
139
2.07k
ProgramState::bindDefaultZero(SVal loc, const LocationContext *LCtx) const {
140
2.07k
  ProgramStateManager &Mgr = getStateManager();
141
2.07k
  const MemRegion *R = loc.castAs<loc::MemRegionVal>().getRegion();
142
2.07k
  const StoreRef &newStore = Mgr.StoreMgr->BindDefaultZero(getStore(), R);
143
2.07k
  ProgramStateRef new_state = makeWithStore(newStore);
144
2.07k
  return Mgr.getOwningEngine().processRegionChange(new_state, R, LCtx);
145
2.07k
}
146
147
typedef ArrayRef<const MemRegion *> RegionList;
148
typedef ArrayRef<SVal> ValueList;
149
150
ProgramStateRef
151
ProgramState::invalidateRegions(RegionList Regions,
152
                             const Expr *E, unsigned Count,
153
                             const LocationContext *LCtx,
154
                             bool CausedByPointerEscape,
155
                             InvalidatedSymbols *IS,
156
                             const CallEvent *Call,
157
3.10k
                             RegionAndSymbolInvalidationTraits *ITraits) const {
158
3.10k
  SmallVector<SVal, 8> Values;
159
3.10k
  for (const MemRegion *Reg : Regions)
160
3.20k
    Values.push_back(loc::MemRegionVal(Reg));
161
162
3.10k
  return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape,
163
3.10k
                               IS, ITraits, Call);
164
3.10k
}
165
166
ProgramStateRef
167
ProgramState::invalidateRegions(ValueList Values,
168
                             const Expr *E, unsigned Count,
169
                             const LocationContext *LCtx,
170
                             bool CausedByPointerEscape,
171
                             InvalidatedSymbols *IS,
172
                             const CallEvent *Call,
173
35.7k
                             RegionAndSymbolInvalidationTraits *ITraits) const {
174
175
35.7k
  return invalidateRegionsImpl(Values, E, Count, LCtx, CausedByPointerEscape,
176
35.7k
                               IS, ITraits, Call);
177
35.7k
}
178
179
ProgramStateRef
180
ProgramState::invalidateRegionsImpl(ValueList Values,
181
                                    const Expr *E, unsigned Count,
182
                                    const LocationContext *LCtx,
183
                                    bool CausedByPointerEscape,
184
                                    InvalidatedSymbols *IS,
185
                                    RegionAndSymbolInvalidationTraits *ITraits,
186
38.8k
                                    const CallEvent *Call) const {
187
38.8k
  ProgramStateManager &Mgr = getStateManager();
188
38.8k
  ExprEngine &Eng = Mgr.getOwningEngine();
189
190
38.8k
  InvalidatedSymbols InvalidatedSyms;
191
38.8k
  if (!IS)
192
38.8k
    IS = &InvalidatedSyms;
193
194
38.8k
  RegionAndSymbolInvalidationTraits ITraitsLocal;
195
38.8k
  if (!ITraits)
196
64
    ITraits = &ITraitsLocal;
197
198
38.8k
  StoreManager::InvalidatedRegions TopLevelInvalidated;
199
38.8k
  StoreManager::InvalidatedRegions Invalidated;
200
38.8k
  const StoreRef &newStore
201
38.8k
  = Mgr.StoreMgr->invalidateRegions(getStore(), Values, E, Count, LCtx, Call,
202
38.8k
                                    *IS, *ITraits, &TopLevelInvalidated,
203
38.8k
                                    &Invalidated);
204
205
38.8k
  ProgramStateRef newState = makeWithStore(newStore);
206
207
38.8k
  if (CausedByPointerEscape) {
208
36.2k
    newState = Eng.notifyCheckersOfPointerEscape(newState, IS,
209
36.2k
                                                 TopLevelInvalidated,
210
36.2k
                                                 Call,
211
36.2k
                                                 *ITraits);
212
36.2k
  }
213
214
38.8k
  return Eng.processRegionChanges(newState, IS, TopLevelInvalidated,
215
38.8k
                                  Invalidated, LCtx, Call);
216
38.8k
}
217
218
177
ProgramStateRef ProgramState::killBinding(Loc LV) const {
219
177
  Store OldStore = getStore();
220
177
  const StoreRef &newStore =
221
177
    getStateManager().StoreMgr->killBinding(OldStore, LV);
222
223
177
  if (newStore.getStore() == OldStore)
224
6
    return this;
225
226
171
  return makeWithStore(newStore);
227
177
}
228
229
ProgramStateRef
230
ProgramState::enterStackFrame(const CallEvent &Call,
231
35.1k
                              const StackFrameContext *CalleeCtx) const {
232
35.1k
  const StoreRef &NewStore =
233
35.1k
    getStateManager().StoreMgr->enterStackFrame(getStore(), Call, CalleeCtx);
234
35.1k
  return makeWithStore(NewStore);
235
35.1k
}
236
237
2.64k
SVal ProgramState::getSelfSVal(const LocationContext *LCtx) const {
238
2.64k
  const ImplicitParamDecl *SelfDecl = LCtx->getSelfDecl();
239
2.64k
  if (!SelfDecl)
240
408
    return SVal();
241
2.23k
  return getSVal(getRegion(SelfDecl, LCtx));
242
2.64k
}
243
244
0
SVal ProgramState::getSValAsScalarOrLoc(const MemRegion *R) const {
245
  // We only want to do fetches from regions that we can actually bind
246
  // values.  For example, SymbolicRegions of type 'id<...>' cannot
247
  // have direct bindings (but their can be bindings on their subregions).
248
0
  if (!R->isBoundable())
249
0
    return UnknownVal();
250
251
0
  if (const TypedValueRegion *TR = dyn_cast<TypedValueRegion>(R)) {
252
0
    QualType T = TR->getValueType();
253
0
    if (Loc::isLocType(T) || T->isIntegralOrEnumerationType())
254
0
      return getSVal(R);
255
0
  }
256
257
0
  return UnknownVal();
258
0
}
259
260
359k
SVal ProgramState::getSVal(Loc location, QualType T) const {
261
359k
  SVal V = getRawSVal(location, T);
262
263
  // If 'V' is a symbolic value that is *perfectly* constrained to
264
  // be a constant value, use that value instead to lessen the burden
265
  // on later analysis stages (so we have less symbolic values to reason
266
  // about).
267
  // We only go into this branch if we can convert the APSInt value we have
268
  // to the type of T, which is not always the case (e.g. for void).
269
359k
  if (!T.isNull() && 
(250k
T->isIntegralOrEnumerationType()250k
||
Loc::isLocType(T)105k
)) {
270
237k
    if (SymbolRef sym = V.getAsSymbol()) {
271
151k
      if (const llvm::APSInt *Int = getStateManager()
272
151k
                                    .getConstraintManager()
273
151k
                                    .getSymVal(this, sym)) {
274
        // FIXME: Because we don't correctly model (yet) sign-extension
275
        // and truncation of symbolic values, we need to convert
276
        // the integer value to the correct signedness and bitwidth.
277
        //
278
        // This shows up in the following:
279
        //
280
        //   char foo();
281
        //   unsigned x = foo();
282
        //   if (x == 54)
283
        //     ...
284
        //
285
        //  The symbolic value stored to 'x' is actually the conjured
286
        //  symbol for the call to foo(); the type of that symbol is 'char',
287
        //  not unsigned.
288
8.16k
        const llvm::APSInt &NewV = getBasicVals().Convert(T, *Int);
289
290
8.16k
        if (V.getAs<Loc>())
291
827
          return loc::ConcreteInt(NewV);
292
7.33k
        else
293
7.33k
          return nonloc::ConcreteInt(NewV);
294
8.16k
      }
295
151k
    }
296
237k
  }
297
298
351k
  return V;
299
359k
}
300
301
ProgramStateRef ProgramState::BindExpr(const Stmt *S,
302
                                           const LocationContext *LCtx,
303
1.16M
                                           SVal V, bool Invalidate) const{
304
1.16M
  Environment NewEnv =
305
1.16M
    getStateManager().EnvMgr.bindExpr(Env, EnvironmentEntry(S, LCtx), V,
306
1.16M
                                      Invalidate);
307
1.16M
  if (NewEnv == Env)
308
36.3k
    return this;
309
310
1.12M
  ProgramState NewSt = *this;
311
1.12M
  NewSt.Env = NewEnv;
312
1.12M
  return getStateManager().getPersistentState(NewSt);
313
1.16M
}
314
315
[[nodiscard]] std::pair<ProgramStateRef, ProgramStateRef>
316
ProgramState::assumeInBoundDual(DefinedOrUnknownSVal Idx,
317
                                DefinedOrUnknownSVal UpperBound,
318
2.42k
                                QualType indexTy) const {
319
2.42k
  if (Idx.isUnknown() || UpperBound.isUnknown())
320
2
    return {this, this};
321
322
  // Build an expression for 0 <= Idx < UpperBound.
323
  // This is the same as Idx + MIN < UpperBound + MIN, if overflow is allowed.
324
  // FIXME: This should probably be part of SValBuilder.
325
2.42k
  ProgramStateManager &SM = getStateManager();
326
2.42k
  SValBuilder &svalBuilder = SM.getSValBuilder();
327
2.42k
  ASTContext &Ctx = svalBuilder.getContext();
328
329
  // Get the offset: the minimum value of the array index type.
330
2.42k
  BasicValueFactory &BVF = svalBuilder.getBasicValueFactory();
331
2.42k
  if (indexTy.isNull())
332
2.41k
    indexTy = svalBuilder.getArrayIndexType();
333
2.42k
  nonloc::ConcreteInt Min(BVF.getMinValue(indexTy));
334
335
  // Adjust the index.
336
2.42k
  SVal newIdx = svalBuilder.evalBinOpNN(this, BO_Add,
337
2.42k
                                        Idx.castAs<NonLoc>(), Min, indexTy);
338
2.42k
  if (newIdx.isUnknownOrUndef())
339
0
    return {this, this};
340
341
  // Adjust the upper bound.
342
2.42k
  SVal newBound =
343
2.42k
    svalBuilder.evalBinOpNN(this, BO_Add, UpperBound.castAs<NonLoc>(),
344
2.42k
                            Min, indexTy);
345
346
2.42k
  if (newBound.isUnknownOrUndef())
347
0
    return {this, this};
348
349
  // Build the actual comparison.
350
2.42k
  SVal inBound = svalBuilder.evalBinOpNN(this, BO_LT, newIdx.castAs<NonLoc>(),
351
2.42k
                                         newBound.castAs<NonLoc>(), Ctx.IntTy);
352
2.42k
  if (inBound.isUnknownOrUndef())
353
0
    return {this, this};
354
355
  // Finally, let the constraint manager take care of it.
356
2.42k
  ConstraintManager &CM = SM.getConstraintManager();
357
2.42k
  return CM.assumeDual(this, inBound.castAs<DefinedSVal>());
358
2.42k
}
359
360
ProgramStateRef ProgramState::assumeInBound(DefinedOrUnknownSVal Idx,
361
                                            DefinedOrUnknownSVal UpperBound,
362
                                            bool Assumption,
363
45
                                            QualType indexTy) const {
364
45
  std::pair<ProgramStateRef, ProgramStateRef> R =
365
45
      assumeInBoundDual(Idx, UpperBound, indexTy);
366
45
  return Assumption ? R.first : 
R.second0
;
367
45
}
368
369
26.4k
ConditionTruthVal ProgramState::isNonNull(SVal V) const {
370
26.4k
  ConditionTruthVal IsNull = isNull(V);
371
26.4k
  if (IsNull.isUnderconstrained())
372
7.33k
    return IsNull;
373
19.0k
  return ConditionTruthVal(!IsNull.getValue());
374
26.4k
}
375
376
0
ConditionTruthVal ProgramState::areEqual(SVal Lhs, SVal Rhs) const {
377
0
  return stateMgr->getSValBuilder().areEqual(this, Lhs, Rhs);
378
0
}
379
380
78.1k
ConditionTruthVal ProgramState::isNull(SVal V) const {
381
78.1k
  if (V.isZeroConstant())
382
1.37k
    return true;
383
384
76.7k
  if (V.isConstant())
385
20.2k
    return false;
386
387
56.4k
  SymbolRef Sym = V.getAsSymbol(/* IncludeBaseRegion */ true);
388
56.4k
  if (!Sym)
389
14.1k
    return ConditionTruthVal();
390
391
42.2k
  return getStateManager().ConstraintMgr->isNull(this, Sym);
392
56.4k
}
393
394
16.2k
ProgramStateRef ProgramStateManager::getInitialState(const LocationContext *InitLoc) {
395
16.2k
  ProgramState State(this,
396
16.2k
                EnvMgr.getInitialEnvironment(),
397
16.2k
                StoreMgr->getInitialStore(InitLoc),
398
16.2k
                GDMFactory.getEmptyMap());
399
400
16.2k
  return getPersistentState(State);
401
16.2k
}
402
403
ProgramStateRef ProgramStateManager::getPersistentStateWithGDM(
404
                                                     ProgramStateRef FromState,
405
427k
                                                     ProgramStateRef GDMState) {
406
427k
  ProgramState NewState(*FromState);
407
427k
  NewState.GDM = GDMState->GDM;
408
427k
  return getPersistentState(NewState);
409
427k
}
410
411
2.59M
ProgramStateRef ProgramStateManager::getPersistentState(ProgramState &State) {
412
413
2.59M
  llvm::FoldingSetNodeID ID;
414
2.59M
  State.Profile(ID);
415
2.59M
  void *InsertPos;
416
417
2.59M
  if (ProgramState *I = StateSet.FindNodeOrInsertPos(ID, InsertPos))
418
535k
    return I;
419
420
2.06M
  ProgramState *newState = nullptr;
421
2.06M
  if (!freeStates.empty()) {
422
605k
    newState = freeStates.back();
423
605k
    freeStates.pop_back();
424
605k
  }
425
1.45M
  else {
426
1.45M
    newState = Alloc.Allocate<ProgramState>();
427
1.45M
  }
428
2.06M
  new (newState) ProgramState(State);
429
2.06M
  StateSet.InsertNode(newState, InsertPos);
430
2.06M
  return newState;
431
2.59M
}
432
433
242k
ProgramStateRef ProgramState::makeWithStore(const StoreRef &store) const {
434
242k
  ProgramState NewSt(*this);
435
242k
  NewSt.setStore(store);
436
242k
  return getStateManager().getPersistentState(NewSt);
437
242k
}
438
439
76
ProgramStateRef ProgramState::cloneAsPosteriorlyOverconstrained() const {
440
76
  ProgramState NewSt(*this);
441
76
  NewSt.PosteriorlyOverconstrained = true;
442
76
  return getStateManager().getPersistentState(NewSt);
443
76
}
444
445
670k
void ProgramState::setStore(const StoreRef &newStore) {
446
670k
  Store newStoreStore = newStore.getStore();
447
670k
  if (newStoreStore)
448
595k
    stateMgr->getStoreManager().incrementReferenceCount(newStoreStore);
449
670k
  if (store)
450
581k
    stateMgr->getStoreManager().decrementReferenceCount(store);
451
670k
  store = newStoreStore;
452
670k
}
453
454
//===----------------------------------------------------------------------===//
455
//  State pretty-printing.
456
//===----------------------------------------------------------------------===//
457
458
void ProgramState::printJson(raw_ostream &Out, const LocationContext *LCtx,
459
                             const char *NL, unsigned int Space,
460
158
                             bool IsDot) const {
461
158
  Indent(Out, Space, IsDot) << "\"program_state\": {" << NL;
462
158
  ++Space;
463
464
158
  ProgramStateManager &Mgr = getStateManager();
465
466
  // Print the store.
467
158
  Mgr.getStoreManager().printJson(Out, getStore(), NL, Space, IsDot);
468
469
  // Print out the environment.
470
158
  Env.printJson(Out, Mgr.getContext(), LCtx, NL, Space, IsDot);
471
472
  // Print out the constraints.
473
158
  Mgr.getConstraintManager().printJson(Out, this, NL, Space, IsDot);
474
475
  // Print out the tracked dynamic types.
476
158
  printDynamicTypeInfoJson(Out, this, NL, Space, IsDot);
477
478
  // Print checker-specific data.
479
158
  Mgr.getOwningEngine().printJson(Out, this, LCtx, NL, Space, IsDot);
480
481
158
  --Space;
482
158
  Indent(Out, Space, IsDot) << '}';
483
158
}
484
485
void ProgramState::printDOT(raw_ostream &Out, const LocationContext *LCtx,
486
100
                            unsigned int Space) const {
487
100
  printJson(Out, LCtx, /*NL=*/"\\l", Space, /*IsDot=*/true);
488
100
}
489
490
58
LLVM_DUMP_METHOD void ProgramState::dump() const {
491
58
  printJson(llvm::errs());
492
58
}
493
494
39.7k
AnalysisManager& ProgramState::getAnalysisManager() const {
495
39.7k
  return stateMgr->getOwningEngine().getAnalysisManager();
496
39.7k
}
497
498
//===----------------------------------------------------------------------===//
499
// Generic Data Map.
500
//===----------------------------------------------------------------------===//
501
502
32.1M
void *const* ProgramState::FindGDM(void *K) const {
503
32.1M
  return GDM.lookup(K);
504
32.1M
}
505
506
void*
507
ProgramStateManager::FindGDMContext(void *K,
508
                               void *(*CreateContext)(llvm::BumpPtrAllocator&),
509
4.35M
                               void (*DeleteContext)(void*)) {
510
511
4.35M
  std::pair<void*, void (*)(void*)>& p = GDMContexts[K];
512
4.35M
  if (!p.first) {
513
123k
    p.first = CreateContext(Alloc);
514
123k
    p.second = DeleteContext;
515
123k
  }
516
517
4.35M
  return p.first;
518
4.35M
}
519
520
515k
ProgramStateRef ProgramStateManager::addGDM(ProgramStateRef St, void *Key, void *Data){
521
515k
  ProgramState::GenericDataMap M1 = St->getGDM();
522
515k
  ProgramState::GenericDataMap M2 = GDMFactory.add(M1, Key, Data);
523
524
515k
  if (M1 == M2)
525
160k
    return St;
526
527
354k
  ProgramState NewSt = *St;
528
354k
  NewSt.GDM = M2;
529
354k
  return getPersistentState(NewSt);
530
515k
}
531
532
65
ProgramStateRef ProgramStateManager::removeGDM(ProgramStateRef state, void *Key) {
533
65
  ProgramState::GenericDataMap OldM = state->getGDM();
534
65
  ProgramState::GenericDataMap NewM = GDMFactory.remove(OldM, Key);
535
536
65
  if (NewM == OldM)
537
0
    return state;
538
539
65
  ProgramState NewState = *state;
540
65
  NewState.GDM = NewM;
541
65
  return getPersistentState(NewState);
542
65
}
543
544
45.1k
bool ScanReachableSymbols::scan(nonloc::LazyCompoundVal val) {
545
45.1k
  bool wasVisited = !visited.insert(val.getCVData()).second;
546
45.1k
  if (wasVisited)
547
11
    return true;
548
549
45.1k
  StoreManager &StoreMgr = state->getStateManager().getStoreManager();
550
  // FIXME: We don't really want to use getBaseRegion() here because pointer
551
  // arithmetic doesn't apply, but scanReachableSymbols only accepts base
552
  // regions right now.
553
45.1k
  const MemRegion *R = val.getRegion()->getBaseRegion();
554
45.1k
  return StoreMgr.scanReachableSymbols(val.getStore(), R, *this);
555
45.1k
}
556
557
1.99k
bool ScanReachableSymbols::scan(nonloc::CompoundVal val) {
558
1.99k
  for (SVal V : val)
559
3.01k
    if (!scan(V))
560
0
      return false;
561
562
1.99k
  return true;
563
1.99k
}
564
565
295k
bool ScanReachableSymbols::scan(const SymExpr *sym) {
566
795k
  for (SymbolRef SubSym : sym->symbols()) {
567
795k
    bool wasVisited = !visited.insert(SubSym).second;
568
795k
    if (wasVisited)
569
221k
      continue;
570
571
573k
    if (!visitor.VisitSymbol(SubSym))
572
0
      return false;
573
573k
  }
574
575
295k
  return true;
576
295k
}
577
578
1.76M
bool ScanReachableSymbols::scan(SVal val) {
579
1.76M
  if (std::optional<loc::MemRegionVal> X = val.getAs<loc::MemRegionVal>())
580
1.01M
    return scan(X->getRegion());
581
582
753k
  if (std::optional<nonloc::LazyCompoundVal> X =
583
753k
          val.getAs<nonloc::LazyCompoundVal>())
584
45.1k
    return scan(*X);
585
586
708k
  if (std::optional<nonloc::LocAsInteger> X = val.getAs<nonloc::LocAsInteger>())
587
1.34k
    return scan(X->getLoc());
588
589
706k
  if (SymbolRef Sym = val.getAsSymbol())
590
295k
    return scan(Sym);
591
592
411k
  if (std::optional<nonloc::CompoundVal> X = val.getAs<nonloc::CompoundVal>())
593
1.99k
    return scan(*X);
594
595
409k
  return true;
596
411k
}
597
598
1.94M
bool ScanReachableSymbols::scan(const MemRegion *R) {
599
1.94M
  if (isa<MemSpaceRegion>(R))
600
749k
    return true;
601
602
1.19M
  bool wasVisited = !visited.insert(R).second;
603
1.19M
  if (wasVisited)
604
264k
    return true;
605
606
927k
  if (!visitor.VisitMemRegion(R))
607
0
    return false;
608
609
  // If this is a symbolic region, visit the symbol for the region.
610
927k
  if (const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R))
611
125k
    if (!visitor.VisitSymbol(SR->getSymbol()))
612
0
      return false;
613
614
  // If this is a subregion, also visit the parent regions.
615
927k
  if (const SubRegion *SR = dyn_cast<SubRegion>(R)) {
616
927k
    const MemRegion *Super = SR->getSuperRegion();
617
927k
    if (!scan(Super))
618
0
      return false;
619
620
    // When we reach the topmost region, scan all symbols in it.
621
927k
    if (isa<MemSpaceRegion>(Super)) {
622
749k
      StoreManager &StoreMgr = state->getStateManager().getStoreManager();
623
749k
      if (!StoreMgr.scanReachableSymbols(state->getStore(), SR, *this))
624
0
        return false;
625
749k
    }
626
927k
  }
627
628
  // Regions captured by a block are also implicitly reachable.
629
927k
  if (const BlockDataRegion *BDR = dyn_cast<BlockDataRegion>(R)) {
630
1.48k
    for (auto Var : BDR->referenced_vars()) {
631
1.48k
      if (!scan(Var.getCapturedRegion()))
632
0
        return false;
633
1.48k
    }
634
1.47k
  }
635
636
927k
  return true;
637
927k
}
638
639
26.0k
bool ProgramState::scanReachableSymbols(SVal val, SymbolVisitor& visitor) const {
640
26.0k
  ScanReachableSymbols S(this, visitor);
641
26.0k
  return S.scan(val);
642
26.0k
}
643
644
bool ProgramState::scanReachableSymbols(
645
    llvm::iterator_range<region_iterator> Reachable,
646
30
    SymbolVisitor &visitor) const {
647
30
  ScanReachableSymbols S(this, visitor);
648
34
  for (const MemRegion *R : Reachable) {
649
34
    if (!S.scan(R))
650
0
      return false;
651
34
  }
652
30
  return true;
653
30
}