Coverage Report

Created: 2019-07-24 05:18

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