Coverage Report

Created: 2020-02-25 14:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Core/Store.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- Store.cpp - Interface for maps from Locations to Values ------------===//
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 defined the types Store and StoreManager.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
14
#include "clang/AST/ASTContext.h"
15
#include "clang/AST/CXXInheritance.h"
16
#include "clang/AST/CharUnits.h"
17
#include "clang/AST/Decl.h"
18
#include "clang/AST/DeclCXX.h"
19
#include "clang/AST/DeclObjC.h"
20
#include "clang/AST/Expr.h"
21
#include "clang/AST/Type.h"
22
#include "clang/Basic/LLVM.h"
23
#include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h"
24
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
25
#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
26
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
27
#include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
28
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
29
#include "clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h"
30
#include "clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h"
31
#include "llvm/ADT/APSInt.h"
32
#include "llvm/ADT/Optional.h"
33
#include "llvm/ADT/SmallVector.h"
34
#include "llvm/Support/Casting.h"
35
#include "llvm/Support/ErrorHandling.h"
36
#include <cassert>
37
#include <cstdint>
38
39
using namespace clang;
40
using namespace ento;
41
42
StoreManager::StoreManager(ProgramStateManager &stateMgr)
43
    : svalBuilder(stateMgr.getSValBuilder()), StateMgr(stateMgr),
44
12.6k
      MRMgr(svalBuilder.getRegionManager()), Ctx(stateMgr.getContext()) {}
45
46
StoreRef StoreManager::enterStackFrame(Store OldStore,
47
                                       const CallEvent &Call,
48
63.7k
                                       const StackFrameContext *LCtx) {
49
63.7k
  StoreRef Store = StoreRef(OldStore, *this);
50
63.7k
51
63.7k
  SmallVector<CallEvent::FrameBindingTy, 16> InitialBindings;
52
63.7k
  Call.getInitialStackFrameContents(LCtx, InitialBindings);
53
63.7k
54
63.7k
  for (const auto &I : InitialBindings)
55
78.8k
    Store = Bind(Store.getStore(), I.first.castAs<Loc>(), I.second);
56
63.7k
57
63.7k
  return Store;
58
63.7k
}
59
60
const ElementRegion *StoreManager::MakeElementRegion(const SubRegion *Base,
61
                                                     QualType EleTy,
62
26.1k
                                                     uint64_t index) {
63
26.1k
  NonLoc idx = svalBuilder.makeArrayIndex(index);
64
26.1k
  return MRMgr.getElementRegion(EleTy, idx, Base, svalBuilder.getContext());
65
26.1k
}
66
67
const ElementRegion *StoreManager::GetElementZeroRegion(const SubRegion *R,
68
7.79k
                                                        QualType T) {
69
7.79k
  NonLoc idx = svalBuilder.makeZeroArrayIndex();
70
7.79k
  assert(!T.isNull());
71
7.79k
  return MRMgr.getElementRegion(T, idx, R, Ctx);
72
7.79k
}
73
74
27.8k
const MemRegion *StoreManager::castRegion(const MemRegion *R, QualType CastToTy) {
75
27.8k
  ASTContext &Ctx = StateMgr.getContext();
76
27.8k
77
27.8k
  // Handle casts to Objective-C objects.
78
27.8k
  if (CastToTy->isObjCObjectPointerType())
79
1.19k
    return R->StripCasts();
80
26.6k
81
26.6k
  if (CastToTy->isBlockPointerType()) {
82
3
    // FIXME: We may need different solutions, depending on the symbol
83
3
    // involved.  Blocks can be casted to/from 'id', as they can be treated
84
3
    // as Objective-C objects.  This could possibly be handled by enhancing
85
3
    // our reasoning of downcasts of symbolic objects.
86
3
    if (isa<CodeTextRegion>(R) || isa<SymbolicRegion>(R))
87
2
      return R;
88
1
89
1
    // We don't know what to make of it.  Return a NULL region, which
90
1
    // will be interpreted as UnknownVal.
91
1
    return nullptr;
92
1
  }
93
26.6k
94
26.6k
  // Now assume we are casting from pointer to pointer. Other cases should
95
26.6k
  // already be handled.
96
26.6k
  QualType PointeeTy = CastToTy->getPointeeType();
97
26.6k
  QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
98
26.6k
99
26.6k
  // Handle casts to void*.  We just pass the region through.
100
26.6k
  if (CanonPointeeTy.getLocalUnqualifiedType() == Ctx.VoidTy)
101
178
    return R;
102
26.4k
103
26.4k
  // Handle casts from compatible types.
104
26.4k
  if (R->isBoundable())
105
26.4k
    if (const auto *TR = dyn_cast<TypedValueRegion>(R)) {
106
20.9k
      QualType ObjTy = Ctx.getCanonicalType(TR->getValueType());
107
20.9k
      if (CanonPointeeTy == ObjTy)
108
380
        return R;
109
26.1k
    }
110
26.1k
111
26.1k
  // Process region cast according to the kind of the region being cast.
112
26.1k
  switch (R->getKind()) {
113
0
    case MemRegion::CXXThisRegionKind:
114
0
    case MemRegion::CodeSpaceRegionKind:
115
0
    case MemRegion::StackLocalsSpaceRegionKind:
116
0
    case MemRegion::StackArgumentsSpaceRegionKind:
117
0
    case MemRegion::HeapSpaceRegionKind:
118
0
    case MemRegion::UnknownSpaceRegionKind:
119
0
    case MemRegion::StaticGlobalSpaceRegionKind:
120
0
    case MemRegion::GlobalInternalSpaceRegionKind:
121
0
    case MemRegion::GlobalSystemSpaceRegionKind:
122
0
    case MemRegion::GlobalImmutableSpaceRegionKind: {
123
0
      llvm_unreachable("Invalid region cast");
124
0
    }
125
0
126
25.6k
    case MemRegion::FunctionCodeRegionKind:
127
25.6k
    case MemRegion::BlockCodeRegionKind:
128
25.6k
    case MemRegion::BlockDataRegionKind:
129
25.6k
    case MemRegion::StringRegionKind:
130
25.6k
      // FIXME: Need to handle arbitrary downcasts.
131
25.6k
    case MemRegion::SymbolicRegionKind:
132
25.6k
    case MemRegion::AllocaRegionKind:
133
25.6k
    case MemRegion::CompoundLiteralRegionKind:
134
25.6k
    case MemRegion::FieldRegionKind:
135
25.6k
    case MemRegion::ObjCIvarRegionKind:
136
25.6k
    case MemRegion::ObjCStringRegionKind:
137
25.6k
    case MemRegion::VarRegionKind:
138
25.6k
    case MemRegion::CXXTempObjectRegionKind:
139
25.6k
    case MemRegion::CXXBaseObjectRegionKind:
140
25.6k
    case MemRegion::CXXDerivedObjectRegionKind:
141
25.6k
      return MakeElementRegion(cast<SubRegion>(R), PointeeTy);
142
25.6k
143
25.6k
    case MemRegion::ElementRegionKind: {
144
427
      // If we are casting from an ElementRegion to another type, the
145
427
      // algorithm is as follows:
146
427
      //
147
427
      // (1) Compute the "raw offset" of the ElementRegion from the
148
427
      //     base region.  This is done by calling 'getAsRawOffset()'.
149
427
      //
150
427
      // (2a) If we get a 'RegionRawOffset' after calling
151
427
      //      'getAsRawOffset()', determine if the absolute offset
152
427
      //      can be exactly divided into chunks of the size of the
153
427
      //      casted-pointee type.  If so, create a new ElementRegion with
154
427
      //      the pointee-cast type as the new ElementType and the index
155
427
      //      being the offset divded by the chunk size.  If not, create
156
427
      //      a new ElementRegion at offset 0 off the raw offset region.
157
427
      //
158
427
      // (2b) If we don't a get a 'RegionRawOffset' after calling
159
427
      //      'getAsRawOffset()', it means that we are at offset 0.
160
427
      //
161
427
      // FIXME: Handle symbolic raw offsets.
162
427
163
427
      const ElementRegion *elementR = cast<ElementRegion>(R);
164
427
      const RegionRawOffset &rawOff = elementR->getAsArrayOffset();
165
427
      const MemRegion *baseR = rawOff.getRegion();
166
427
167
427
      // If we cannot compute a raw offset, throw up our hands and return
168
427
      // a NULL MemRegion*.
169
427
      if (!baseR)
170
13
        return nullptr;
171
414
172
414
      CharUnits off = rawOff.getOffset();
173
414
174
414
      if (off.isZero()) {
175
172
        // Edge case: we are at 0 bytes off the beginning of baseR.  We
176
172
        // check to see if type we are casting to is the same as the base
177
172
        // region.  If so, just return the base region.
178
172
        if (const auto *TR = dyn_cast<TypedValueRegion>(baseR)) {
179
98
          QualType ObjTy = Ctx.getCanonicalType(TR->getValueType());
180
98
          QualType CanonPointeeTy = Ctx.getCanonicalType(PointeeTy);
181
98
          if (CanonPointeeTy == ObjTy)
182
6
            return baseR;
183
166
        }
184
166
185
166
        // Otherwise, create a new ElementRegion at offset 0.
186
166
        return MakeElementRegion(cast<SubRegion>(baseR), PointeeTy);
187
166
      }
188
242
189
242
      // We have a non-zero offset from the base region.  We want to determine
190
242
      // if the offset can be evenly divided by sizeof(PointeeTy).  If so,
191
242
      // we create an ElementRegion whose index is that value.  Otherwise, we
192
242
      // create two ElementRegions, one that reflects a raw offset and the other
193
242
      // that reflects the cast.
194
242
195
242
      // Compute the index for the new ElementRegion.
196
242
      int64_t newIndex = 0;
197
242
      const MemRegion *newSuperR = nullptr;
198
242
199
242
      // We can only compute sizeof(PointeeTy) if it is a complete type.
200
242
      if (!PointeeTy->isIncompleteType()) {
201
240
        // Compute the size in **bytes**.
202
240
        CharUnits pointeeTySize = Ctx.getTypeSizeInChars(PointeeTy);
203
240
        if (!pointeeTySize.isZero()) {
204
238
          // Is the offset a multiple of the size?  If so, we can layer the
205
238
          // ElementRegion (with elementType == PointeeTy) directly on top of
206
238
          // the base region.
207
238
          if (off % pointeeTySize == 0) {
208
230
            newIndex = off / pointeeTySize;
209
230
            newSuperR = baseR;
210
230
          }
211
238
        }
212
240
      }
213
242
214
242
      if (!newSuperR) {
215
12
        // Create an intermediate ElementRegion to represent the raw byte.
216
12
        // This will be the super region of the final ElementRegion.
217
12
        newSuperR = MakeElementRegion(cast<SubRegion>(baseR), Ctx.CharTy,
218
12
                                      off.getQuantity());
219
12
      }
220
242
221
242
      return MakeElementRegion(cast<SubRegion>(newSuperR), PointeeTy, newIndex);
222
242
    }
223
0
  }
224
0
225
0
  llvm_unreachable("unreachable");
226
0
}
227
228
829
static bool regionMatchesCXXRecordType(SVal V, QualType Ty) {
229
829
  const MemRegion *MR = V.getAsRegion();
230
829
  if (!MR)
231
7
    return true;
232
822
233
822
  const auto *TVR = dyn_cast<TypedValueRegion>(MR);
234
822
  if (!TVR)
235
236
    return true;
236
586
237
586
  const CXXRecordDecl *RD = TVR->getValueType()->getAsCXXRecordDecl();
238
586
  if (!RD)
239
0
    return true;
240
586
241
586
  const CXXRecordDecl *Expected = Ty->getPointeeCXXRecordDecl();
242
586
  if (!Expected)
243
421
    Expected = Ty->getAsCXXRecordDecl();
244
586
245
586
  return Expected->getCanonicalDecl() == RD->getCanonicalDecl();
246
586
}
247
248
829
SVal StoreManager::evalDerivedToBase(SVal Derived, const CastExpr *Cast) {
249
829
  // Sanity check to avoid doing the wrong thing in the face of
250
829
  // reinterpret_cast.
251
829
  if (!regionMatchesCXXRecordType(Derived, Cast->getSubExpr()->getType()))
252
1
    return UnknownVal();
253
828
254
828
  // Walk through the cast path to create nested CXXBaseRegions.
255
828
  SVal Result = Derived;
256
828
  for (CastExpr::path_const_iterator I = Cast->path_begin(),
257
828
                                     E = Cast->path_end();
258
1.75k
       I != E; 
++I925
) {
259
925
    Result = evalDerivedToBase(Result, (*I)->getType(), (*I)->isVirtual());
260
925
  }
261
828
  return Result;
262
828
}
263
264
10
SVal StoreManager::evalDerivedToBase(SVal Derived, const CXXBasePath &Path) {
265
10
  // Walk through the path to create nested CXXBaseRegions.
266
10
  SVal Result = Derived;
267
10
  for (const auto &I : Path)
268
12
    Result = evalDerivedToBase(Result, I.Base->getType(),
269
12
                               I.Base->isVirtual());
270
10
  return Result;
271
10
}
272
273
SVal StoreManager::evalDerivedToBase(SVal Derived, QualType BaseType,
274
1.81k
                                     bool IsVirtual) {
275
1.81k
  const MemRegion *DerivedReg = Derived.getAsRegion();
276
1.81k
  if (!DerivedReg)
277
11
    return Derived;
278
1.80k
279
1.80k
  const CXXRecordDecl *BaseDecl = BaseType->getPointeeCXXRecordDecl();
280
1.80k
  if (!BaseDecl)
281
1.80k
    BaseDecl = BaseType->getAsCXXRecordDecl();
282
1.80k
  assert(BaseDecl && "not a C++ object?");
283
1.80k
284
1.80k
  if (const auto *AlreadyDerivedReg =
285
13
          dyn_cast<CXXDerivedObjectRegion>(DerivedReg)) {
286
13
    if (const auto *SR =
287
13
            dyn_cast<SymbolicRegion>(AlreadyDerivedReg->getSuperRegion()))
288
13
      if (SR->getSymbol()->getType()->getPointeeCXXRecordDecl() == BaseDecl)
289
10
        return loc::MemRegionVal(SR);
290
3
291
3
    DerivedReg = AlreadyDerivedReg->getSuperRegion();
292
3
  }
293
1.80k
294
1.80k
  const MemRegion *BaseReg = MRMgr.getCXXBaseObjectRegion(
295
1.79k
      BaseDecl, cast<SubRegion>(DerivedReg), IsVirtual);
296
1.79k
297
1.79k
  return loc::MemRegionVal(BaseReg);
298
1.80k
}
299
300
/// Returns the static type of the given region, if it represents a C++ class
301
/// object.
302
///
303
/// This handles both fully-typed regions, where the dynamic type is known, and
304
/// symbolic regions, where the dynamic type is merely bounded (and even then,
305
/// only ostensibly!), but does not take advantage of any dynamic type info.
306
268
static const CXXRecordDecl *getCXXRecordType(const MemRegion *MR) {
307
268
  if (const auto *TVR = dyn_cast<TypedValueRegion>(MR))
308
177
    return TVR->getValueType()->getAsCXXRecordDecl();
309
91
  if (const auto *SR = dyn_cast<SymbolicRegion>(MR))
310
91
    return SR->getSymbol()->getType()->getPointeeCXXRecordDecl();
311
0
  return nullptr;
312
0
}
313
314
SVal StoreManager::attemptDownCast(SVal Base, QualType TargetType,
315
142
                                   bool &Failed) {
316
142
  Failed = false;
317
142
318
142
  const MemRegion *MR = Base.getAsRegion();
319
142
  if (!MR)
320
0
    return UnknownVal();
321
142
322
142
  // Assume the derived class is a pointer or a reference to a CXX record.
323
142
  TargetType = TargetType->getPointeeType();
324
142
  assert(!TargetType.isNull());
325
142
  const CXXRecordDecl *TargetClass = TargetType->getAsCXXRecordDecl();
326
142
  if (!TargetClass && 
!TargetType->isVoidType()1
)
327
0
    return UnknownVal();
328
142
329
142
  // Drill down the CXXBaseObject chains, which represent upcasts (casts from
330
142
  // derived to base).
331
268
  
while (const CXXRecordDecl *142
MRClass = getCXXRecordType(MR)) {
332
266
    // If found the derived class, the cast succeeds.
333
266
    if (MRClass == TargetClass)
334
94
      return loc::MemRegionVal(MR);
335
172
336
172
    // We skip over incomplete types. They must be the result of an earlier
337
172
    // reinterpret_cast, as one can only dynamic_cast between types in the same
338
172
    // class hierarchy.
339
172
    if (!TargetType->isVoidType() && 
MRClass->hasDefinition()171
) {
340
168
      // Static upcasts are marked as DerivedToBase casts by Sema, so this will
341
168
      // only happen when multiple or virtual inheritance is involved.
342
168
      CXXBasePaths Paths(/*FindAmbiguities=*/false, /*RecordPaths=*/true,
343
168
                         /*DetectVirtual=*/false);
344
168
      if (MRClass->isDerivedFrom(TargetClass, Paths))
345
9
        return evalDerivedToBase(loc::MemRegionVal(MR), Paths.front());
346
163
    }
347
163
348
163
    if (const auto *BaseR = dyn_cast<CXXBaseObjectRegion>(MR)) {
349
117
      // Drill down the chain to get the derived classes.
350
117
      MR = BaseR->getSuperRegion();
351
117
      continue;
352
117
    }
353
46
354
46
    // If this is a cast to void*, return the region.
355
46
    if (TargetType->isVoidType())
356
1
      return loc::MemRegionVal(MR);
357
45
358
45
    // Strange use of reinterpret_cast can give us paths we don't reason
359
45
    // about well, by putting in ElementRegions where we'd expect
360
45
    // CXXBaseObjectRegions. If it's a valid reinterpret_cast (i.e. if the
361
45
    // derived class has a zero offset from the base class), then it's safe
362
45
    // to strip the cast; if it's invalid, -Wreinterpret-base-class should
363
45
    // catch it. In the interest of performance, the analyzer will silently
364
45
    // do the wrong thing in the invalid case (because offsets for subregions
365
45
    // will be wrong).
366
45
    const MemRegion *Uncasted = MR->StripCasts(/*IncludeBaseCasts=*/false);
367
45
    if (Uncasted == MR) {
368
36
      // We reached the bottom of the hierarchy and did not find the derived
369
36
      // class. We must be casting the base to derived, so the cast should
370
36
      // fail.
371
36
      break;
372
36
    }
373
9
374
9
    MR = Uncasted;
375
9
  }
376
142
377
142
  // If we're casting a symbolic base pointer to a derived class, use
378
142
  // CXXDerivedObjectRegion to represent the cast. If it's a pointer to an
379
142
  // unrelated type, it must be a weird reinterpret_cast and we have to
380
142
  // be fine with ElementRegion. TODO: Should we instead make
381
142
  // Derived{TargetClass, Element{SourceClass, SR}}?
382
142
  
if (const auto *38
SR38
= dyn_cast<SymbolicRegion>(MR)) {
383
26
    QualType T = SR->getSymbol()->getType();
384
26
    const CXXRecordDecl *SourceClass = T->getPointeeCXXRecordDecl();
385
26
    if (TargetClass && SourceClass && 
TargetClass->isDerivedFrom(SourceClass)25
)
386
21
      return loc::MemRegionVal(
387
21
          MRMgr.getCXXDerivedObjectRegion(TargetClass, SR));
388
5
    return loc::MemRegionVal(GetElementZeroRegion(SR, TargetType));
389
5
  }
390
12
391
12
  // We failed if the region we ended up with has perfect type info.
392
12
  Failed = isa<TypedValueRegion>(MR);
393
12
  return UnknownVal();
394
12
}
395
396
40.6k
static bool hasSameUnqualifiedPointeeType(QualType ty1, QualType ty2) {
397
40.6k
  return ty1->getPointeeType().getCanonicalType().getTypePtr() ==
398
40.6k
         ty2->getPointeeType().getCanonicalType().getTypePtr();
399
40.6k
}
400
401
/// CastRetrievedVal - Used by subclasses of StoreManager to implement
402
///  implicit casts that arise from loads from regions that are reinterpreted
403
///  as another region.
404
SVal StoreManager::CastRetrievedVal(SVal V, const TypedValueRegion *R,
405
432k
                                    QualType castTy) {
406
432k
  if (castTy.isNull() || V.isUnknownOrUndef())
407
89.6k
    return V;
408
343k
409
343k
  // The dispatchCast() call below would convert the int into a float.
410
343k
  // What we want, however, is a bit-by-bit reinterpretation of the int
411
343k
  // as a float, which usually yields nothing garbage. For now skip casts
412
343k
  // from ints to floats.
413
343k
  // TODO: What other combinations of types are affected?
414
343k
  if (castTy->isFloatingType()) {
415
50
    SymbolRef Sym = V.getAsSymbol();
416
50
    if (Sym && 
!Sym->getType()->isFloatingType()34
)
417
25
      return UnknownVal();
418
343k
  }
419
343k
420
343k
  // When retrieving symbolic pointer and expecting a non-void pointer,
421
343k
  // wrap them into element regions of the expected type if necessary.
422
343k
  // SValBuilder::dispatchCast() doesn't do that, but it is necessary to
423
343k
  // make sure that the retrieved value makes sense, because there's no other
424
343k
  // cast in the AST that would tell us to cast it to the correct pointer type.
425
343k
  // We might need to do that for non-void pointers as well.
426
343k
  // FIXME: We really need a single good function to perform casts for us
427
343k
  // correctly every time we need it.
428
343k
  if (castTy->isPointerType() && 
!castTy->isVoidPointerType()177k
)
429
146k
    if (const auto *SR = dyn_cast_or_null<SymbolicRegion>(V.getAsRegion())) {
430
40.6k
      QualType sr = SR->getSymbol()->getType();
431
40.6k
      if (!hasSameUnqualifiedPointeeType(sr, castTy))
432
13
          return loc::MemRegionVal(castRegion(SR, castTy));
433
343k
    }
434
343k
435
343k
  return svalBuilder.dispatchCast(V, castTy);
436
343k
}
437
438
66.4k
SVal StoreManager::getLValueFieldOrIvar(const Decl *D, SVal Base) {
439
66.4k
  if (Base.isUnknownOrUndef())
440
10
    return Base;
441
66.4k
442
66.4k
  Loc BaseL = Base.castAs<Loc>();
443
66.4k
  const SubRegion* BaseR = nullptr;
444
66.4k
445
66.4k
  switch (BaseL.getSubKind()) {
446
66.3k
  case loc::MemRegionValKind:
447
66.3k
    BaseR = cast<SubRegion>(BaseL.castAs<loc::MemRegionVal>().getRegion());
448
66.3k
    break;
449
0
450
0
  case loc::GotoLabelKind:
451
0
    // These are anormal cases. Flag an undefined value.
452
0
    return UndefinedVal();
453
0
454
64
  case loc::ConcreteIntKind:
455
64
    // While these seem funny, this can happen through casts.
456
64
    // FIXME: What we should return is the field offset, not base. For example,
457
64
    //  add the field offset to the integer value.  That way things
458
64
    //  like this work properly:  &(((struct foo *) 0xa)->f)
459
64
    //  However, that's not easy to fix without reducing our abilities
460
64
    //  to catch null pointer dereference. Eg., ((struct foo *)0x0)->f = 7
461
64
    //  is a null dereference even though we're dereferencing offset of f
462
64
    //  rather than null. Coming up with an approach that computes offsets
463
64
    //  over null pointers properly while still being able to catch null
464
64
    //  dereferences might be worth it.
465
64
    return Base;
466
0
467
0
  default:
468
0
    llvm_unreachable("Unhandled Base.");
469
66.3k
  }
470
66.3k
471
66.3k
  // NOTE: We must have this check first because ObjCIvarDecl is a subclass
472
66.3k
  // of FieldDecl.
473
66.3k
  if (const auto *ID = dyn_cast<ObjCIvarDecl>(D))
474
1.44k
    return loc::MemRegionVal(MRMgr.getObjCIvarRegion(ID, BaseR));
475
64.9k
476
64.9k
  return loc::MemRegionVal(MRMgr.getFieldRegion(cast<FieldDecl>(D), BaseR));
477
64.9k
}
478
479
1.46k
SVal StoreManager::getLValueIvar(const ObjCIvarDecl *decl, SVal base) {
480
1.46k
  return getLValueFieldOrIvar(decl, base);
481
1.46k
}
482
483
SVal StoreManager::getLValueElement(QualType elementType, NonLoc Offset,
484
10.0k
                                    SVal Base) {
485
10.0k
  // If the base is an unknown or undefined value, just return it back.
486
10.0k
  // FIXME: For absolute pointer addresses, we just return that value back as
487
10.0k
  //  well, although in reality we should return the offset added to that
488
10.0k
  //  value. See also the similar FIXME in getLValueFieldOrIvar().
489
10.0k
  if (Base.isUnknownOrUndef() || 
Base.getAs<loc::ConcreteInt>()10.0k
)
490
44
    return Base;
491
10.0k
492
10.0k
  if (Base.getAs<loc::GotoLabel>())
493
2
    return UnknownVal();
494
10.0k
495
10.0k
  const SubRegion *BaseRegion =
496
10.0k
      Base.castAs<loc::MemRegionVal>().getRegionAs<SubRegion>();
497
10.0k
498
10.0k
  // Pointer of any type can be cast and used as array base.
499
10.0k
  const auto *ElemR = dyn_cast<ElementRegion>(BaseRegion);
500
10.0k
501
10.0k
  // Convert the offset to the appropriate size and signedness.
502
10.0k
  Offset = svalBuilder.convertToArrayIndex(Offset).castAs<NonLoc>();
503
10.0k
504
10.0k
  if (!ElemR) {
505
3.38k
    // If the base region is not an ElementRegion, create one.
506
3.38k
    // This can happen in the following example:
507
3.38k
    //
508
3.38k
    //   char *p = __builtin_alloc(10);
509
3.38k
    //   p[1] = 8;
510
3.38k
    //
511
3.38k
    //  Observe that 'p' binds to an AllocaRegion.
512
3.38k
    return loc::MemRegionVal(MRMgr.getElementRegion(elementType, Offset,
513
3.38k
                                                    BaseRegion, Ctx));
514
3.38k
  }
515
6.65k
516
6.65k
  SVal BaseIdx = ElemR->getIndex();
517
6.65k
518
6.65k
  if (!BaseIdx.getAs<nonloc::ConcreteInt>())
519
32
    return UnknownVal();
520
6.62k
521
6.62k
  const llvm::APSInt &BaseIdxI =
522
6.62k
      BaseIdx.castAs<nonloc::ConcreteInt>().getValue();
523
6.62k
524
6.62k
  // Only allow non-integer offsets if the base region has no offset itself.
525
6.62k
  // FIXME: This is a somewhat arbitrary restriction. We should be using
526
6.62k
  // SValBuilder here to add the two offsets without checking their types.
527
6.62k
  if (!Offset.getAs<nonloc::ConcreteInt>()) {
528
238
    if (isa<ElementRegion>(BaseRegion->StripCasts()))
529
0
      return UnknownVal();
530
238
531
238
    return loc::MemRegionVal(MRMgr.getElementRegion(
532
238
        elementType, Offset, cast<SubRegion>(ElemR->getSuperRegion()), Ctx));
533
238
  }
534
6.38k
535
6.38k
  const llvm::APSInt& OffI = Offset.castAs<nonloc::ConcreteInt>().getValue();
536
6.38k
  assert(BaseIdxI.isSigned());
537
6.38k
538
6.38k
  // Compute the new index.
539
6.38k
  nonloc::ConcreteInt NewIdx(svalBuilder.getBasicValueFactory().getValue(BaseIdxI +
540
6.38k
                                                                    OffI));
541
6.38k
542
6.38k
  // Construct the new ElementRegion.
543
6.38k
  const SubRegion *ArrayR = cast<SubRegion>(ElemR->getSuperRegion());
544
6.38k
  return loc::MemRegionVal(MRMgr.getElementRegion(elementType, NewIdx, ArrayR,
545
6.38k
                                                  Ctx));
546
6.38k
}
547
548
113k
StoreManager::BindingsHandler::~BindingsHandler() = default;
549
550
bool StoreManager::FindUniqueBinding::HandleBinding(StoreManager& SMgr,
551
                                                    Store store,
552
                                                    const MemRegion* R,
553
26.0k
                                                    SVal val) {
554
26.0k
  SymbolRef SymV = val.getAsLocSymbol();
555
26.0k
  if (!SymV || 
SymV != Sym15.5k
)
556
15.9k
    return true;
557
10.0k
558
10.0k
  if (Binding) {
559
544
    First = false;
560
544
    return false;
561
544
  }
562
9.55k
  else
563
9.55k
    Binding = R;
564
10.0k
565
10.0k
  
return true9.55k
;
566
10.0k
}