1 //== RegionStore.cpp - Field-sensitive store model --------------*- C++ -*--==//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines a basic region store model. In this model, we do have field
10 // sensitivity. But we assume nothing about the heap shape. So recursive data
11 // structures are largely ignored. Basically we do 1-limiting analysis.
12 // Parameter pointers are assumed with no aliasing. Pointee objects of
13 // parameters are created lazily.
15 //===----------------------------------------------------------------------===//
17 #include "clang/AST/Attr.h"
18 #include "clang/AST/CharUnits.h"
19 #include "clang/ASTMatchers/ASTMatchFinder.h"
20 #include "clang/Analysis/Analyses/LiveVariables.h"
21 #include "clang/Analysis/AnalysisDeclContext.h"
22 #include "clang/Basic/JsonSupport.h"
23 #include "clang/Basic/TargetInfo.h"
24 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
25 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
26 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
27 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
28 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
29 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramStateTrait.h"
30 #include "llvm/ADT/ImmutableMap.h"
31 #include "llvm/ADT/STLExtras.h"
32 #include "llvm/Support/raw_ostream.h"
36 using namespace clang
;
39 //===----------------------------------------------------------------------===//
40 // Representation of binding keys.
41 //===----------------------------------------------------------------------===//
46 enum Kind
{ Default
= 0x0, Direct
= 0x1 };
48 enum { Symbolic
= 0x2 };
50 llvm::PointerIntPair
<const MemRegion
*, 2> P
;
53 /// Create a key for a binding to region \p r, which has a symbolic offset
54 /// from region \p Base.
55 explicit BindingKey(const SubRegion
*r
, const SubRegion
*Base
, Kind k
)
56 : P(r
, k
| Symbolic
), Data(reinterpret_cast<uintptr_t>(Base
)) {
57 assert(r
&& Base
&& "Must have known regions.");
58 assert(getConcreteOffsetRegion() == Base
&& "Failed to store base region");
61 /// Create a key for a binding at \p offset from base region \p r.
62 explicit BindingKey(const MemRegion
*r
, uint64_t offset
, Kind k
)
63 : P(r
, k
), Data(offset
) {
64 assert(r
&& "Must have known regions.");
65 assert(getOffset() == offset
&& "Failed to store offset");
66 assert((r
== r
->getBaseRegion() ||
67 isa
<ObjCIvarRegion
, CXXDerivedObjectRegion
>(r
)) &&
72 bool isDirect() const { return P
.getInt() & Direct
; }
73 bool hasSymbolicOffset() const { return P
.getInt() & Symbolic
; }
75 const MemRegion
*getRegion() const { return P
.getPointer(); }
76 uint64_t getOffset() const {
77 assert(!hasSymbolicOffset());
81 const SubRegion
*getConcreteOffsetRegion() const {
82 assert(hasSymbolicOffset());
83 return reinterpret_cast<const SubRegion
*>(static_cast<uintptr_t>(Data
));
86 const MemRegion
*getBaseRegion() const {
87 if (hasSymbolicOffset())
88 return getConcreteOffsetRegion()->getBaseRegion();
89 return getRegion()->getBaseRegion();
92 void Profile(llvm::FoldingSetNodeID
& ID
) const {
93 ID
.AddPointer(P
.getOpaqueValue());
97 static BindingKey
Make(const MemRegion
*R
, Kind k
);
99 bool operator<(const BindingKey
&X
) const {
100 if (P
.getOpaqueValue() < X
.P
.getOpaqueValue())
102 if (P
.getOpaqueValue() > X
.P
.getOpaqueValue())
104 return Data
< X
.Data
;
107 bool operator==(const BindingKey
&X
) const {
108 return P
.getOpaqueValue() == X
.P
.getOpaqueValue() &&
112 LLVM_DUMP_METHOD
void dump() const;
114 } // end anonymous namespace
116 BindingKey
BindingKey::Make(const MemRegion
*R
, Kind k
) {
117 const RegionOffset
&RO
= R
->getAsOffset();
118 if (RO
.hasSymbolicOffset())
119 return BindingKey(cast
<SubRegion
>(R
), cast
<SubRegion
>(RO
.getRegion()), k
);
121 return BindingKey(RO
.getRegion(), RO
.getOffset(), k
);
125 static inline raw_ostream
&operator<<(raw_ostream
&Out
, BindingKey K
) {
126 Out
<< "\"kind\": \"" << (K
.isDirect() ? "Direct" : "Default")
127 << "\", \"offset\": ";
129 if (!K
.hasSymbolicOffset())
130 Out
<< K
.getOffset();
139 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
140 void BindingKey::dump() const { llvm::errs() << *this; }
143 //===----------------------------------------------------------------------===//
144 // Actual Store type.
145 //===----------------------------------------------------------------------===//
147 typedef llvm::ImmutableMap
<BindingKey
, SVal
> ClusterBindings
;
148 typedef llvm::ImmutableMapRef
<BindingKey
, SVal
> ClusterBindingsRef
;
149 typedef std::pair
<BindingKey
, SVal
> BindingPair
;
151 typedef llvm::ImmutableMap
<const MemRegion
*, ClusterBindings
>
155 class RegionBindingsRef
: public llvm::ImmutableMapRef
<const MemRegion
*,
157 ClusterBindings::Factory
*CBFactory
;
159 // This flag indicates whether the current bindings are within the analysis
160 // that has started from main(). It affects how we perform loads from
161 // global variables that have initializers: if we have observed the
162 // program execution from the start and we know that these variables
163 // have not been overwritten yet, we can be sure that their initializers
164 // are still relevant. This flag never gets changed when the bindings are
165 // updated, so it could potentially be moved into RegionStoreManager
166 // (as if it's the same bindings but a different loading procedure)
167 // however that would have made the manager needlessly stateful.
171 typedef llvm::ImmutableMapRef
<const MemRegion
*, ClusterBindings
>
174 RegionBindingsRef(ClusterBindings::Factory
&CBFactory
,
175 const RegionBindings::TreeTy
*T
,
176 RegionBindings::TreeTy::Factory
*F
,
178 : llvm::ImmutableMapRef
<const MemRegion
*, ClusterBindings
>(T
, F
),
179 CBFactory(&CBFactory
), IsMainAnalysis(IsMainAnalysis
) {}
181 RegionBindingsRef(const ParentTy
&P
,
182 ClusterBindings::Factory
&CBFactory
,
184 : llvm::ImmutableMapRef
<const MemRegion
*, ClusterBindings
>(P
),
185 CBFactory(&CBFactory
), IsMainAnalysis(IsMainAnalysis
) {}
187 RegionBindingsRef
add(key_type_ref K
, data_type_ref D
) const {
188 return RegionBindingsRef(static_cast<const ParentTy
*>(this)->add(K
, D
),
189 *CBFactory
, IsMainAnalysis
);
192 RegionBindingsRef
remove(key_type_ref K
) const {
193 return RegionBindingsRef(static_cast<const ParentTy
*>(this)->remove(K
),
194 *CBFactory
, IsMainAnalysis
);
197 RegionBindingsRef
addBinding(BindingKey K
, SVal V
) const;
199 RegionBindingsRef
addBinding(const MemRegion
*R
,
200 BindingKey::Kind k
, SVal V
) const;
202 const SVal
*lookup(BindingKey K
) const;
203 const SVal
*lookup(const MemRegion
*R
, BindingKey::Kind k
) const;
204 using llvm::ImmutableMapRef
<const MemRegion
*, ClusterBindings
>::lookup
;
206 RegionBindingsRef
removeBinding(BindingKey K
);
208 RegionBindingsRef
removeBinding(const MemRegion
*R
,
211 RegionBindingsRef
removeBinding(const MemRegion
*R
) {
212 return removeBinding(R
, BindingKey::Direct
).
213 removeBinding(R
, BindingKey::Default
);
216 std::optional
<SVal
> getDirectBinding(const MemRegion
*R
) const;
218 /// getDefaultBinding - Returns an SVal* representing an optional default
219 /// binding associated with a region and its subregions.
220 std::optional
<SVal
> getDefaultBinding(const MemRegion
*R
) const;
222 /// Return the internal tree as a Store.
223 Store
asStore() const {
224 llvm::PointerIntPair
<Store
, 1, bool> Ptr
= {
225 asImmutableMap().getRootWithoutRetain(), IsMainAnalysis
};
226 return reinterpret_cast<Store
>(Ptr
.getOpaqueValue());
229 bool isMainAnalysis() const {
230 return IsMainAnalysis
;
233 void printJson(raw_ostream
&Out
, const char *NL
= "\n",
234 unsigned int Space
= 0, bool IsDot
= false) const {
235 for (iterator I
= begin(), E
= end(); I
!= E
; ++I
) {
236 // TODO: We might need a .printJson for I.getKey() as well.
237 Indent(Out
, Space
, IsDot
)
238 << "{ \"cluster\": \"" << I
.getKey() << "\", \"pointer\": \""
239 << (const void *)I
.getKey() << "\", \"items\": [" << NL
;
242 const ClusterBindings
&CB
= I
.getData();
243 for (ClusterBindings::iterator CI
= CB
.begin(), CE
= CB
.end(); CI
!= CE
;
245 Indent(Out
, Space
, IsDot
) << "{ " << CI
.getKey() << ", \"value\": ";
246 CI
.getData().printJson(Out
, /*AddQuotes=*/true);
248 if (std::next(CI
) != CE
)
254 Indent(Out
, Space
, IsDot
) << "]}";
255 if (std::next(I
) != E
)
261 LLVM_DUMP_METHOD
void dump() const { printJson(llvm::errs()); }
263 } // end anonymous namespace
265 typedef const RegionBindingsRef
& RegionBindingsConstRef
;
268 RegionBindingsRef::getDirectBinding(const MemRegion
*R
) const {
269 const SVal
*V
= lookup(R
, BindingKey::Direct
);
270 return V
? std::optional
<SVal
>(*V
) : std::nullopt
;
274 RegionBindingsRef::getDefaultBinding(const MemRegion
*R
) const {
275 const SVal
*V
= lookup(R
, BindingKey::Default
);
276 return V
? std::optional
<SVal
>(*V
) : std::nullopt
;
279 RegionBindingsRef
RegionBindingsRef::addBinding(BindingKey K
, SVal V
) const {
280 const MemRegion
*Base
= K
.getBaseRegion();
282 const ClusterBindings
*ExistingCluster
= lookup(Base
);
283 ClusterBindings Cluster
=
284 (ExistingCluster
? *ExistingCluster
: CBFactory
->getEmptyMap());
286 ClusterBindings NewCluster
= CBFactory
->add(Cluster
, K
, V
);
287 return add(Base
, NewCluster
);
291 RegionBindingsRef
RegionBindingsRef::addBinding(const MemRegion
*R
,
294 return addBinding(BindingKey::Make(R
, k
), V
);
297 const SVal
*RegionBindingsRef::lookup(BindingKey K
) const {
298 const ClusterBindings
*Cluster
= lookup(K
.getBaseRegion());
301 return Cluster
->lookup(K
);
304 const SVal
*RegionBindingsRef::lookup(const MemRegion
*R
,
305 BindingKey::Kind k
) const {
306 return lookup(BindingKey::Make(R
, k
));
309 RegionBindingsRef
RegionBindingsRef::removeBinding(BindingKey K
) {
310 const MemRegion
*Base
= K
.getBaseRegion();
311 const ClusterBindings
*Cluster
= lookup(Base
);
315 ClusterBindings NewCluster
= CBFactory
->remove(*Cluster
, K
);
316 if (NewCluster
.isEmpty())
318 return add(Base
, NewCluster
);
321 RegionBindingsRef
RegionBindingsRef::removeBinding(const MemRegion
*R
,
323 return removeBinding(BindingKey::Make(R
, k
));
326 //===----------------------------------------------------------------------===//
327 // Main RegionStore logic.
328 //===----------------------------------------------------------------------===//
331 class InvalidateRegionsWorker
;
333 class RegionStoreManager
: public StoreManager
{
335 RegionBindings::Factory RBFactory
;
336 mutable ClusterBindings::Factory CBFactory
;
338 typedef std::vector
<SVal
> SValListTy
;
340 typedef llvm::DenseMap
<const LazyCompoundValData
*,
341 SValListTy
> LazyBindingsMapTy
;
342 LazyBindingsMapTy LazyBindingsMap
;
344 /// The largest number of fields a struct can have and still be
345 /// considered "small".
347 /// This is currently used to decide whether or not it is worth "forcing" a
348 /// LazyCompoundVal on bind.
350 /// This is controlled by 'region-store-small-struct-limit' option.
351 /// To disable all small-struct-dependent behavior, set the option to "0".
352 unsigned SmallStructLimit
;
354 /// The largest number of element an array can have and still be
355 /// considered "small".
357 /// This is currently used to decide whether or not it is worth "forcing" a
358 /// LazyCompoundVal on bind.
360 /// This is controlled by 'region-store-small-struct-limit' option.
361 /// To disable all small-struct-dependent behavior, set the option to "0".
362 unsigned SmallArrayLimit
;
364 /// A helper used to populate the work list with the given set of
366 void populateWorkList(InvalidateRegionsWorker
&W
,
367 ArrayRef
<SVal
> Values
,
368 InvalidatedRegions
*TopLevelRegions
);
371 RegionStoreManager(ProgramStateManager
&mgr
)
372 : StoreManager(mgr
), RBFactory(mgr
.getAllocator()),
373 CBFactory(mgr
.getAllocator()), SmallStructLimit(0), SmallArrayLimit(0) {
374 ExprEngine
&Eng
= StateMgr
.getOwningEngine();
375 AnalyzerOptions
&Options
= Eng
.getAnalysisManager().options
;
376 SmallStructLimit
= Options
.RegionStoreSmallStructLimit
;
377 SmallArrayLimit
= Options
.RegionStoreSmallArrayLimit
;
380 /// setImplicitDefaultValue - Set the default binding for the provided
381 /// MemRegion to the value implicitly defined for compound literals when
382 /// the value is not specified.
383 RegionBindingsRef
setImplicitDefaultValue(RegionBindingsConstRef B
,
384 const MemRegion
*R
, QualType T
);
386 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
387 /// type. 'Array' represents the lvalue of the array being decayed
388 /// to a pointer, and the returned SVal represents the decayed
389 /// version of that lvalue (i.e., a pointer to the first element of
390 /// the array). This is called by ExprEngine when evaluating
391 /// casts from arrays to pointers.
392 SVal
ArrayToPointer(Loc Array
, QualType ElementTy
) override
;
394 /// Creates the Store that correctly represents memory contents before
395 /// the beginning of the analysis of the given top-level stack frame.
396 StoreRef
getInitialStore(const LocationContext
*InitLoc
) override
{
397 bool IsMainAnalysis
= false;
398 if (const auto *FD
= dyn_cast
<FunctionDecl
>(InitLoc
->getDecl()))
399 IsMainAnalysis
= FD
->isMain() && !Ctx
.getLangOpts().CPlusPlus
;
400 return StoreRef(RegionBindingsRef(
401 RegionBindingsRef::ParentTy(RBFactory
.getEmptyMap(), RBFactory
),
402 CBFactory
, IsMainAnalysis
).asStore(), *this);
405 //===-------------------------------------------------------------------===//
406 // Binding values to regions.
407 //===-------------------------------------------------------------------===//
408 RegionBindingsRef
invalidateGlobalRegion(MemRegion::Kind K
,
411 const LocationContext
*LCtx
,
413 InvalidatedRegions
*Invalidated
);
415 StoreRef
invalidateRegions(Store store
,
416 ArrayRef
<SVal
> Values
,
417 const Expr
*E
, unsigned Count
,
418 const LocationContext
*LCtx
,
419 const CallEvent
*Call
,
420 InvalidatedSymbols
&IS
,
421 RegionAndSymbolInvalidationTraits
&ITraits
,
422 InvalidatedRegions
*Invalidated
,
423 InvalidatedRegions
*InvalidatedTopLevel
) override
;
425 bool scanReachableSymbols(Store S
, const MemRegion
*R
,
426 ScanReachableSymbols
&Callbacks
) override
;
428 RegionBindingsRef
removeSubRegionBindings(RegionBindingsConstRef B
,
431 getConstantValFromConstArrayInitializer(RegionBindingsConstRef B
,
432 const ElementRegion
*R
);
434 getSValFromInitListExpr(const InitListExpr
*ILE
,
435 const SmallVector
<uint64_t, 2> &ConcreteOffsets
,
437 SVal
getSValFromStringLiteral(const StringLiteral
*SL
, uint64_t Offset
,
440 public: // Part of public interface to class.
442 StoreRef
Bind(Store store
, Loc LV
, SVal V
) override
{
443 return StoreRef(bind(getRegionBindings(store
), LV
, V
).asStore(), *this);
446 RegionBindingsRef
bind(RegionBindingsConstRef B
, Loc LV
, SVal V
);
448 // BindDefaultInitial is only used to initialize a region with
450 StoreRef
BindDefaultInitial(Store store
, const MemRegion
*R
,
452 RegionBindingsRef B
= getRegionBindings(store
);
453 // Use other APIs when you have to wipe the region that was initialized
455 assert(!(B
.getDefaultBinding(R
) || B
.getDirectBinding(R
)) &&
456 "Double initialization!");
457 B
= B
.addBinding(BindingKey::Make(R
, BindingKey::Default
), V
);
458 return StoreRef(B
.asImmutableMap().getRootWithoutRetain(), *this);
461 // BindDefaultZero is used for zeroing constructors that may accidentally
462 // overwrite existing bindings.
463 StoreRef
BindDefaultZero(Store store
, const MemRegion
*R
) override
{
464 // FIXME: The offsets of empty bases can be tricky because of
465 // of the so called "empty base class optimization".
466 // If a base class has been optimized out
467 // we should not try to create a binding, otherwise we should.
468 // Unfortunately, at the moment ASTRecordLayout doesn't expose
469 // the actual sizes of the empty bases
470 // and trying to infer them from offsets/alignments
471 // seems to be error-prone and non-trivial because of the trailing padding.
472 // As a temporary mitigation we don't create bindings for empty bases.
473 if (const auto *BR
= dyn_cast
<CXXBaseObjectRegion
>(R
))
474 if (BR
->getDecl()->isEmpty())
475 return StoreRef(store
, *this);
477 RegionBindingsRef B
= getRegionBindings(store
);
478 SVal V
= svalBuilder
.makeZeroVal(Ctx
.CharTy
);
479 B
= removeSubRegionBindings(B
, cast
<SubRegion
>(R
));
480 B
= B
.addBinding(BindingKey::Make(R
, BindingKey::Default
), V
);
481 return StoreRef(B
.asImmutableMap().getRootWithoutRetain(), *this);
484 /// Attempt to extract the fields of \p LCV and bind them to the struct region
487 /// This path is used when it seems advantageous to "force" loading the values
488 /// within a LazyCompoundVal to bind memberwise to the struct region, rather
489 /// than using a Default binding at the base of the entire region. This is a
490 /// heuristic attempting to avoid building long chains of LazyCompoundVals.
492 /// \returns The updated store bindings, or \c std::nullopt if binding
493 /// non-lazily would be too expensive.
494 std::optional
<RegionBindingsRef
>
495 tryBindSmallStruct(RegionBindingsConstRef B
, const TypedValueRegion
*R
,
496 const RecordDecl
*RD
, nonloc::LazyCompoundVal LCV
);
498 /// BindStruct - Bind a compound value to a structure.
499 RegionBindingsRef
bindStruct(RegionBindingsConstRef B
,
500 const TypedValueRegion
* R
, SVal V
);
502 /// BindVector - Bind a compound value to a vector.
503 RegionBindingsRef
bindVector(RegionBindingsConstRef B
,
504 const TypedValueRegion
* R
, SVal V
);
506 std::optional
<RegionBindingsRef
>
507 tryBindSmallArray(RegionBindingsConstRef B
, const TypedValueRegion
*R
,
508 const ArrayType
*AT
, nonloc::LazyCompoundVal LCV
);
510 RegionBindingsRef
bindArray(RegionBindingsConstRef B
,
511 const TypedValueRegion
* R
,
514 /// Clears out all bindings in the given region and assigns a new value
515 /// as a Default binding.
516 RegionBindingsRef
bindAggregate(RegionBindingsConstRef B
,
517 const TypedRegion
*R
,
520 /// Create a new store with the specified binding removed.
521 /// \param ST the original store, that is the basis for the new store.
522 /// \param L the location whose binding should be removed.
523 StoreRef
killBinding(Store ST
, Loc L
) override
;
525 void incrementReferenceCount(Store store
) override
{
526 getRegionBindings(store
).manualRetain();
529 /// If the StoreManager supports it, decrement the reference count of
530 /// the specified Store object. If the reference count hits 0, the memory
531 /// associated with the object is recycled.
532 void decrementReferenceCount(Store store
) override
{
533 getRegionBindings(store
).manualRelease();
536 bool includedInBindings(Store store
, const MemRegion
*region
) const override
;
538 /// Return the value bound to specified location in a given state.
540 /// The high level logic for this method is this:
543 /// return L's binding
544 /// else if L is in killset
547 /// if L is on stack or heap
551 SVal
getBinding(Store S
, Loc L
, QualType T
) override
{
552 return getBinding(getRegionBindings(S
), L
, T
);
555 std::optional
<SVal
> getDefaultBinding(Store S
, const MemRegion
*R
) override
{
556 RegionBindingsRef B
= getRegionBindings(S
);
557 // Default bindings are always applied over a base region so look up the
558 // base region's default binding, otherwise the lookup will fail when R
559 // is at an offset from R->getBaseRegion().
560 return B
.getDefaultBinding(R
->getBaseRegion());
563 SVal
getBinding(RegionBindingsConstRef B
, Loc L
, QualType T
= QualType());
565 SVal
getBindingForElement(RegionBindingsConstRef B
, const ElementRegion
*R
);
567 SVal
getBindingForField(RegionBindingsConstRef B
, const FieldRegion
*R
);
569 SVal
getBindingForObjCIvar(RegionBindingsConstRef B
, const ObjCIvarRegion
*R
);
571 SVal
getBindingForVar(RegionBindingsConstRef B
, const VarRegion
*R
);
573 SVal
getBindingForLazySymbol(const TypedValueRegion
*R
);
575 SVal
getBindingForFieldOrElementCommon(RegionBindingsConstRef B
,
576 const TypedValueRegion
*R
,
579 SVal
getLazyBinding(const SubRegion
*LazyBindingRegion
,
580 RegionBindingsRef LazyBinding
);
582 /// Get bindings for the values in a struct and return a CompoundVal, used
583 /// when doing struct copy:
586 /// y's value is retrieved by this method.
587 SVal
getBindingForStruct(RegionBindingsConstRef B
, const TypedValueRegion
*R
);
588 SVal
getBindingForArray(RegionBindingsConstRef B
, const TypedValueRegion
*R
);
589 NonLoc
createLazyBinding(RegionBindingsConstRef B
, const TypedValueRegion
*R
);
591 /// Used to lazily generate derived symbols for bindings that are defined
592 /// implicitly by default bindings in a super region.
594 /// Note that callers may need to specially handle LazyCompoundVals, which
595 /// are returned as is in case the caller needs to treat them differently.
597 getBindingForDerivedDefaultValue(RegionBindingsConstRef B
,
598 const MemRegion
*superR
,
599 const TypedValueRegion
*R
, QualType Ty
);
601 /// Get the state and region whose binding this region \p R corresponds to.
603 /// If there is no lazy binding for \p R, the returned value will have a null
604 /// \c second. Note that a null pointer can represents a valid Store.
605 std::pair
<Store
, const SubRegion
*>
606 findLazyBinding(RegionBindingsConstRef B
, const SubRegion
*R
,
607 const SubRegion
*originalRegion
);
609 /// Returns the cached set of interesting SVals contained within a lazy
612 /// The precise value of "interesting" is determined for the purposes of
613 /// RegionStore's internal analysis. It must always contain all regions and
614 /// symbols, but may omit constants and other kinds of SVal.
616 /// In contrast to compound values, LazyCompoundVals are also added
617 /// to the 'interesting values' list in addition to the child interesting
619 const SValListTy
&getInterestingValues(nonloc::LazyCompoundVal LCV
);
621 //===------------------------------------------------------------------===//
623 //===------------------------------------------------------------------===//
625 /// removeDeadBindings - Scans the RegionStore of 'state' for dead values.
626 /// It returns a new Store with these values removed.
627 StoreRef
removeDeadBindings(Store store
, const StackFrameContext
*LCtx
,
628 SymbolReaper
& SymReaper
) override
;
630 //===------------------------------------------------------------------===//
632 //===------------------------------------------------------------------===//
634 RegionBindingsRef
getRegionBindings(Store store
) const {
635 llvm::PointerIntPair
<Store
, 1, bool> Ptr
;
636 Ptr
.setFromOpaqueValue(const_cast<void *>(store
));
637 return RegionBindingsRef(
639 static_cast<const RegionBindings::TreeTy
*>(Ptr
.getPointer()),
640 RBFactory
.getTreeFactory(),
644 void printJson(raw_ostream
&Out
, Store S
, const char *NL
= "\n",
645 unsigned int Space
= 0, bool IsDot
= false) const override
;
647 void iterBindings(Store store
, BindingsHandler
& f
) override
{
648 RegionBindingsRef B
= getRegionBindings(store
);
649 for (const auto &[Region
, Cluster
] : B
) {
650 for (const auto &[Key
, Value
] : Cluster
) {
653 if (const SubRegion
*R
= dyn_cast
<SubRegion
>(Key
.getRegion())) {
654 // FIXME: Possibly incorporate the offset?
655 if (!f
.HandleBinding(*this, store
, R
, Value
))
663 } // end anonymous namespace
665 //===----------------------------------------------------------------------===//
666 // RegionStore creation.
667 //===----------------------------------------------------------------------===//
669 std::unique_ptr
<StoreManager
>
670 ento::CreateRegionStoreManager(ProgramStateManager
&StMgr
) {
671 return std::make_unique
<RegionStoreManager
>(StMgr
);
674 //===----------------------------------------------------------------------===//
675 // Region Cluster analysis.
676 //===----------------------------------------------------------------------===//
679 /// Used to determine which global regions are automatically included in the
680 /// initial worklist of a ClusterAnalysis.
681 enum GlobalsFilterKind
{
682 /// Don't include any global regions.
684 /// Only include system globals.
686 /// Include all global regions.
690 template <typename DERIVED
>
691 class ClusterAnalysis
{
693 typedef llvm::DenseMap
<const MemRegion
*, const ClusterBindings
*> ClusterMap
;
694 typedef const MemRegion
* WorkListElement
;
695 typedef SmallVector
<WorkListElement
, 10> WorkList
;
697 llvm::SmallPtrSet
<const ClusterBindings
*, 16> Visited
;
701 RegionStoreManager
&RM
;
703 SValBuilder
&svalBuilder
;
709 const ClusterBindings
*getCluster(const MemRegion
*R
) {
713 /// Returns true if all clusters in the given memspace should be initially
714 /// included in the cluster analysis. Subclasses may provide their
715 /// own implementation.
716 bool includeEntireMemorySpace(const MemRegion
*Base
) {
721 ClusterAnalysis(RegionStoreManager
&rm
, ProgramStateManager
&StateMgr
,
723 : RM(rm
), Ctx(StateMgr
.getContext()),
724 svalBuilder(StateMgr
.getSValBuilder()), B(std::move(b
)) {}
726 RegionBindingsRef
getRegionBindings() const { return B
; }
728 bool isVisited(const MemRegion
*R
) {
729 return Visited
.count(getCluster(R
));
732 void GenerateClusters() {
733 // Scan the entire set of bindings and record the region clusters.
734 for (RegionBindingsRef::iterator RI
= B
.begin(), RE
= B
.end();
736 const MemRegion
*Base
= RI
.getKey();
738 const ClusterBindings
&Cluster
= RI
.getData();
739 assert(!Cluster
.isEmpty() && "Empty clusters should be removed");
740 static_cast<DERIVED
*>(this)->VisitAddedToCluster(Base
, Cluster
);
742 // If the base's memspace should be entirely invalidated, add the cluster
743 // to the workspace up front.
744 if (static_cast<DERIVED
*>(this)->includeEntireMemorySpace(Base
))
745 AddToWorkList(WorkListElement(Base
), &Cluster
);
749 bool AddToWorkList(WorkListElement E
, const ClusterBindings
*C
) {
750 if (C
&& !Visited
.insert(C
).second
)
756 bool AddToWorkList(const MemRegion
*R
) {
757 return static_cast<DERIVED
*>(this)->AddToWorkList(R
);
761 while (!WL
.empty()) {
762 WorkListElement E
= WL
.pop_back_val();
763 const MemRegion
*BaseR
= E
;
765 static_cast<DERIVED
*>(this)->VisitCluster(BaseR
, getCluster(BaseR
));
769 void VisitAddedToCluster(const MemRegion
*baseR
, const ClusterBindings
&C
) {}
770 void VisitCluster(const MemRegion
*baseR
, const ClusterBindings
*C
) {}
772 void VisitCluster(const MemRegion
*BaseR
, const ClusterBindings
*C
,
774 static_cast<DERIVED
*>(this)->VisitCluster(BaseR
, C
);
779 //===----------------------------------------------------------------------===//
780 // Binding invalidation.
781 //===----------------------------------------------------------------------===//
783 bool RegionStoreManager::scanReachableSymbols(Store S
, const MemRegion
*R
,
784 ScanReachableSymbols
&Callbacks
) {
785 assert(R
== R
->getBaseRegion() && "Should only be called for base regions");
786 RegionBindingsRef B
= getRegionBindings(S
);
787 const ClusterBindings
*Cluster
= B
.lookup(R
);
792 for (ClusterBindings::iterator RI
= Cluster
->begin(), RE
= Cluster
->end();
794 if (!Callbacks
.scan(RI
.getData()))
801 static inline bool isUnionField(const FieldRegion
*FR
) {
802 return FR
->getDecl()->getParent()->isUnion();
805 typedef SmallVector
<const FieldDecl
*, 8> FieldVector
;
807 static void getSymbolicOffsetFields(BindingKey K
, FieldVector
&Fields
) {
808 assert(K
.hasSymbolicOffset() && "Not implemented for concrete offset keys");
810 const MemRegion
*Base
= K
.getConcreteOffsetRegion();
811 const MemRegion
*R
= K
.getRegion();
814 if (const FieldRegion
*FR
= dyn_cast
<FieldRegion
>(R
))
815 if (!isUnionField(FR
))
816 Fields
.push_back(FR
->getDecl());
818 R
= cast
<SubRegion
>(R
)->getSuperRegion();
822 static bool isCompatibleWithFields(BindingKey K
, const FieldVector
&Fields
) {
823 assert(K
.hasSymbolicOffset() && "Not implemented for concrete offset keys");
828 FieldVector FieldsInBindingKey
;
829 getSymbolicOffsetFields(K
, FieldsInBindingKey
);
831 ptrdiff_t Delta
= FieldsInBindingKey
.size() - Fields
.size();
833 return std::equal(FieldsInBindingKey
.begin() + Delta
,
834 FieldsInBindingKey
.end(),
837 return std::equal(FieldsInBindingKey
.begin(), FieldsInBindingKey
.end(),
838 Fields
.begin() - Delta
);
841 /// Collects all bindings in \p Cluster that may refer to bindings within
844 /// Each binding is a pair whose \c first is the key (a BindingKey) and whose
845 /// \c second is the value (an SVal).
847 /// The \p IncludeAllDefaultBindings parameter specifies whether to include
848 /// default bindings that may extend beyond \p Top itself, e.g. if \p Top is
849 /// an aggregate within a larger aggregate with a default binding.
851 collectSubRegionBindings(SmallVectorImpl
<BindingPair
> &Bindings
,
852 SValBuilder
&SVB
, const ClusterBindings
&Cluster
,
853 const SubRegion
*Top
, BindingKey TopKey
,
854 bool IncludeAllDefaultBindings
) {
855 FieldVector FieldsInSymbolicSubregions
;
856 if (TopKey
.hasSymbolicOffset()) {
857 getSymbolicOffsetFields(TopKey
, FieldsInSymbolicSubregions
);
858 Top
= TopKey
.getConcreteOffsetRegion();
859 TopKey
= BindingKey::Make(Top
, BindingKey::Default
);
862 // Find the length (in bits) of the region being invalidated.
863 uint64_t Length
= UINT64_MAX
;
864 SVal Extent
= Top
->getMemRegionManager().getStaticSize(Top
, SVB
);
865 if (std::optional
<nonloc::ConcreteInt
> ExtentCI
=
866 Extent
.getAs
<nonloc::ConcreteInt
>()) {
867 const llvm::APSInt
&ExtentInt
= ExtentCI
->getValue();
868 assert(ExtentInt
.isNonNegative() || ExtentInt
.isUnsigned());
869 // Extents are in bytes but region offsets are in bits. Be careful!
870 Length
= ExtentInt
.getLimitedValue() * SVB
.getContext().getCharWidth();
871 } else if (const FieldRegion
*FR
= dyn_cast
<FieldRegion
>(Top
)) {
872 if (FR
->getDecl()->isBitField())
873 Length
= FR
->getDecl()->getBitWidthValue(SVB
.getContext());
876 for (const auto &StoreEntry
: Cluster
) {
877 BindingKey NextKey
= StoreEntry
.first
;
878 if (NextKey
.getRegion() == TopKey
.getRegion()) {
879 // FIXME: This doesn't catch the case where we're really invalidating a
880 // region with a symbolic offset. Example:
884 if (NextKey
.getOffset() > TopKey
.getOffset() &&
885 NextKey
.getOffset() - TopKey
.getOffset() < Length
) {
886 // Case 1: The next binding is inside the region we're invalidating.
888 Bindings
.push_back(StoreEntry
);
890 } else if (NextKey
.getOffset() == TopKey
.getOffset()) {
891 // Case 2: The next binding is at the same offset as the region we're
892 // invalidating. In this case, we need to leave default bindings alone,
893 // since they may be providing a default value for a regions beyond what
894 // we're invalidating.
895 // FIXME: This is probably incorrect; consider invalidating an outer
896 // struct whose first field is bound to a LazyCompoundVal.
897 if (IncludeAllDefaultBindings
|| NextKey
.isDirect())
898 Bindings
.push_back(StoreEntry
);
901 } else if (NextKey
.hasSymbolicOffset()) {
902 const MemRegion
*Base
= NextKey
.getConcreteOffsetRegion();
903 if (Top
->isSubRegionOf(Base
) && Top
!= Base
) {
904 // Case 3: The next key is symbolic and we just changed something within
905 // its concrete region. We don't know if the binding is still valid, so
906 // we'll be conservative and include it.
907 if (IncludeAllDefaultBindings
|| NextKey
.isDirect())
908 if (isCompatibleWithFields(NextKey
, FieldsInSymbolicSubregions
))
909 Bindings
.push_back(StoreEntry
);
910 } else if (const SubRegion
*BaseSR
= dyn_cast
<SubRegion
>(Base
)) {
911 // Case 4: The next key is symbolic, but we changed a known
912 // super-region. In this case the binding is certainly included.
913 if (BaseSR
->isSubRegionOf(Top
))
914 if (isCompatibleWithFields(NextKey
, FieldsInSymbolicSubregions
))
915 Bindings
.push_back(StoreEntry
);
922 collectSubRegionBindings(SmallVectorImpl
<BindingPair
> &Bindings
,
923 SValBuilder
&SVB
, const ClusterBindings
&Cluster
,
924 const SubRegion
*Top
, bool IncludeAllDefaultBindings
) {
925 collectSubRegionBindings(Bindings
, SVB
, Cluster
, Top
,
926 BindingKey::Make(Top
, BindingKey::Default
),
927 IncludeAllDefaultBindings
);
931 RegionStoreManager::removeSubRegionBindings(RegionBindingsConstRef B
,
932 const SubRegion
*Top
) {
933 BindingKey TopKey
= BindingKey::Make(Top
, BindingKey::Default
);
934 const MemRegion
*ClusterHead
= TopKey
.getBaseRegion();
936 if (Top
== ClusterHead
) {
937 // We can remove an entire cluster's bindings all in one go.
938 return B
.remove(Top
);
941 const ClusterBindings
*Cluster
= B
.lookup(ClusterHead
);
943 // If we're invalidating a region with a symbolic offset, we need to make
944 // sure we don't treat the base region as uninitialized anymore.
945 if (TopKey
.hasSymbolicOffset()) {
946 const SubRegion
*Concrete
= TopKey
.getConcreteOffsetRegion();
947 return B
.addBinding(Concrete
, BindingKey::Default
, UnknownVal());
952 SmallVector
<BindingPair
, 32> Bindings
;
953 collectSubRegionBindings(Bindings
, svalBuilder
, *Cluster
, Top
, TopKey
,
954 /*IncludeAllDefaultBindings=*/false);
956 ClusterBindingsRef
Result(*Cluster
, CBFactory
);
957 for (BindingKey Key
: llvm::make_first_range(Bindings
))
958 Result
= Result
.remove(Key
);
960 // If we're invalidating a region with a symbolic offset, we need to make sure
961 // we don't treat the base region as uninitialized anymore.
962 // FIXME: This isn't very precise; see the example in
963 // collectSubRegionBindings.
964 if (TopKey
.hasSymbolicOffset()) {
965 const SubRegion
*Concrete
= TopKey
.getConcreteOffsetRegion();
966 Result
= Result
.add(BindingKey::Make(Concrete
, BindingKey::Default
),
970 if (Result
.isEmpty())
971 return B
.remove(ClusterHead
);
972 return B
.add(ClusterHead
, Result
.asImmutableMap());
976 class InvalidateRegionsWorker
: public ClusterAnalysis
<InvalidateRegionsWorker
>
980 const LocationContext
*LCtx
;
981 InvalidatedSymbols
&IS
;
982 RegionAndSymbolInvalidationTraits
&ITraits
;
983 StoreManager::InvalidatedRegions
*Regions
;
984 GlobalsFilterKind GlobalsFilter
;
986 InvalidateRegionsWorker(RegionStoreManager
&rm
,
987 ProgramStateManager
&stateMgr
,
989 const Expr
*ex
, unsigned count
,
990 const LocationContext
*lctx
,
991 InvalidatedSymbols
&is
,
992 RegionAndSymbolInvalidationTraits
&ITraitsIn
,
993 StoreManager::InvalidatedRegions
*r
,
994 GlobalsFilterKind GFK
)
995 : ClusterAnalysis
<InvalidateRegionsWorker
>(rm
, stateMgr
, b
),
996 Ex(ex
), Count(count
), LCtx(lctx
), IS(is
), ITraits(ITraitsIn
), Regions(r
),
997 GlobalsFilter(GFK
) {}
999 void VisitCluster(const MemRegion
*baseR
, const ClusterBindings
*C
);
1000 void VisitBinding(SVal V
);
1002 using ClusterAnalysis::AddToWorkList
;
1004 bool AddToWorkList(const MemRegion
*R
);
1006 /// Returns true if all clusters in the memory space for \p Base should be
1008 bool includeEntireMemorySpace(const MemRegion
*Base
);
1010 /// Returns true if the memory space of the given region is one of the global
1011 /// regions specially included at the start of invalidation.
1012 bool isInitiallyIncludedGlobalRegion(const MemRegion
*R
);
1016 bool InvalidateRegionsWorker::AddToWorkList(const MemRegion
*R
) {
1017 bool doNotInvalidateSuperRegion
= ITraits
.hasTrait(
1018 R
, RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion
);
1019 const MemRegion
*BaseR
= doNotInvalidateSuperRegion
? R
: R
->getBaseRegion();
1020 return AddToWorkList(WorkListElement(BaseR
), getCluster(BaseR
));
1023 void InvalidateRegionsWorker::VisitBinding(SVal V
) {
1024 // A symbol? Mark it touched by the invalidation.
1025 if (SymbolRef Sym
= V
.getAsSymbol())
1028 if (const MemRegion
*R
= V
.getAsRegion()) {
1033 // Is it a LazyCompoundVal? All references get invalidated as well.
1034 if (std::optional
<nonloc::LazyCompoundVal
> LCS
=
1035 V
.getAs
<nonloc::LazyCompoundVal
>()) {
1037 // `getInterestingValues()` returns SVals contained within LazyCompoundVals,
1038 // so there is no need to visit them.
1039 for (SVal V
: RM
.getInterestingValues(*LCS
))
1040 if (!isa
<nonloc::LazyCompoundVal
>(V
))
1047 void InvalidateRegionsWorker::VisitCluster(const MemRegion
*baseR
,
1048 const ClusterBindings
*C
) {
1050 bool PreserveRegionsContents
=
1051 ITraits
.hasTrait(baseR
,
1052 RegionAndSymbolInvalidationTraits::TK_PreserveContents
);
1055 for (SVal Val
: llvm::make_second_range(*C
))
1058 // Invalidate regions contents.
1059 if (!PreserveRegionsContents
)
1060 B
= B
.remove(baseR
);
1063 if (const auto *TO
= dyn_cast
<TypedValueRegion
>(baseR
)) {
1064 if (const auto *RD
= TO
->getValueType()->getAsCXXRecordDecl()) {
1066 // Lambdas can affect all static local variables without explicitly
1068 // We invalidate all static locals referenced inside the lambda body.
1069 if (RD
->isLambda() && RD
->getLambdaCallOperator()->getBody()) {
1070 using namespace ast_matchers
;
1072 const char *DeclBind
= "DeclBind";
1073 StatementMatcher RefToStatic
= stmt(hasDescendant(declRefExpr(
1074 to(varDecl(hasStaticStorageDuration()).bind(DeclBind
)))));
1076 match(RefToStatic
, *RD
->getLambdaCallOperator()->getBody(),
1077 RD
->getASTContext());
1079 for (BoundNodes
&Match
: Matches
) {
1080 auto *VD
= Match
.getNodeAs
<VarDecl
>(DeclBind
);
1081 const VarRegion
*ToInvalidate
=
1082 RM
.getRegionManager().getVarRegion(VD
, LCtx
);
1083 AddToWorkList(ToInvalidate
);
1089 // BlockDataRegion? If so, invalidate captured variables that are passed
1091 if (const BlockDataRegion
*BR
= dyn_cast
<BlockDataRegion
>(baseR
)) {
1092 for (auto Var
: BR
->referenced_vars()) {
1093 const VarRegion
*VR
= Var
.getCapturedRegion();
1094 const VarDecl
*VD
= VR
->getDecl();
1095 if (VD
->hasAttr
<BlocksAttr
>() || !VD
->hasLocalStorage()) {
1098 else if (Loc::isLocType(VR
->getValueType())) {
1099 // Map the current bindings to a Store to retrieve the value
1100 // of the binding. If that binding itself is a region, we should
1101 // invalidate that region. This is because a block may capture
1102 // a pointer value, but the thing pointed by that pointer may
1104 SVal V
= RM
.getBinding(B
, loc::MemRegionVal(VR
));
1105 if (std::optional
<Loc
> L
= V
.getAs
<Loc
>()) {
1106 if (const MemRegion
*LR
= L
->getAsRegion())
1115 if (const SymbolicRegion
*SR
= dyn_cast
<SymbolicRegion
>(baseR
))
1116 IS
.insert(SR
->getSymbol());
1118 // Nothing else should be done in the case when we preserve regions context.
1119 if (PreserveRegionsContents
)
1122 // Otherwise, we have a normal data region. Record that we touched the region.
1124 Regions
->push_back(baseR
);
1126 if (isa
<AllocaRegion
, SymbolicRegion
>(baseR
)) {
1127 // Invalidate the region by setting its default value to
1128 // conjured symbol. The type of the symbol is irrelevant.
1129 DefinedOrUnknownSVal V
=
1130 svalBuilder
.conjureSymbolVal(baseR
, Ex
, LCtx
, Ctx
.IntTy
, Count
);
1131 B
= B
.addBinding(baseR
, BindingKey::Default
, V
);
1135 if (!baseR
->isBoundable())
1138 const TypedValueRegion
*TR
= cast
<TypedValueRegion
>(baseR
);
1139 QualType T
= TR
->getValueType();
1141 if (isInitiallyIncludedGlobalRegion(baseR
)) {
1142 // If the region is a global and we are invalidating all globals,
1143 // erasing the entry is good enough. This causes all globals to be lazily
1144 // symbolicated from the same base symbol.
1148 if (T
->isRecordType()) {
1149 // Invalidate the region by setting its default value to
1150 // conjured symbol. The type of the symbol is irrelevant.
1151 DefinedOrUnknownSVal V
= svalBuilder
.conjureSymbolVal(baseR
, Ex
, LCtx
,
1153 B
= B
.addBinding(baseR
, BindingKey::Default
, V
);
1157 if (const ArrayType
*AT
= Ctx
.getAsArrayType(T
)) {
1158 bool doNotInvalidateSuperRegion
= ITraits
.hasTrait(
1160 RegionAndSymbolInvalidationTraits::TK_DoNotInvalidateSuperRegion
);
1162 if (doNotInvalidateSuperRegion
) {
1163 // We are not doing blank invalidation of the whole array region so we
1164 // have to manually invalidate each elements.
1165 std::optional
<uint64_t> NumElements
;
1167 // Compute lower and upper offsets for region within array.
1168 if (const ConstantArrayType
*CAT
= dyn_cast
<ConstantArrayType
>(AT
))
1169 NumElements
= CAT
->getSize().getZExtValue();
1170 if (!NumElements
) // We are not dealing with a constant size array
1171 goto conjure_default
;
1172 QualType ElementTy
= AT
->getElementType();
1173 uint64_t ElemSize
= Ctx
.getTypeSize(ElementTy
);
1174 const RegionOffset
&RO
= baseR
->getAsOffset();
1175 const MemRegion
*SuperR
= baseR
->getBaseRegion();
1176 if (RO
.hasSymbolicOffset()) {
1177 // If base region has a symbolic offset,
1178 // we revert to invalidating the super region.
1180 AddToWorkList(SuperR
);
1181 goto conjure_default
;
1184 uint64_t LowerOffset
= RO
.getOffset();
1185 uint64_t UpperOffset
= LowerOffset
+ *NumElements
* ElemSize
;
1186 bool UpperOverflow
= UpperOffset
< LowerOffset
;
1188 // Invalidate regions which are within array boundaries,
1189 // or have a symbolic offset.
1191 goto conjure_default
;
1193 const ClusterBindings
*C
= B
.lookup(SuperR
);
1195 goto conjure_default
;
1197 for (const auto &[BK
, V
] : *C
) {
1198 std::optional
<uint64_t> ROffset
=
1199 BK
.hasSymbolicOffset() ? std::optional
<uint64_t>() : BK
.getOffset();
1201 // Check offset is not symbolic and within array's boundaries.
1202 // Handles arrays of 0 elements and of 0-sized elements as well.
1204 ((*ROffset
>= LowerOffset
&& *ROffset
< UpperOffset
) ||
1206 (*ROffset
>= LowerOffset
|| *ROffset
< UpperOffset
)) ||
1207 (LowerOffset
== UpperOffset
&& *ROffset
== LowerOffset
))) {
1208 B
= B
.removeBinding(BK
);
1209 // Bound symbolic regions need to be invalidated for dead symbol
1211 const MemRegion
*R
= V
.getAsRegion();
1212 if (isa_and_nonnull
<SymbolicRegion
>(R
))
1218 // Set the default value of the array to conjured symbol.
1219 DefinedOrUnknownSVal V
=
1220 svalBuilder
.conjureSymbolVal(baseR
, Ex
, LCtx
,
1221 AT
->getElementType(), Count
);
1222 B
= B
.addBinding(baseR
, BindingKey::Default
, V
);
1226 DefinedOrUnknownSVal V
= svalBuilder
.conjureSymbolVal(baseR
, Ex
, LCtx
,
1228 assert(SymbolManager::canSymbolicate(T
) || V
.isUnknown());
1229 B
= B
.addBinding(baseR
, BindingKey::Direct
, V
);
1232 bool InvalidateRegionsWorker::isInitiallyIncludedGlobalRegion(
1233 const MemRegion
*R
) {
1234 switch (GlobalsFilter
) {
1237 case GFK_SystemOnly
:
1238 return isa
<GlobalSystemSpaceRegion
>(R
->getMemorySpace());
1240 return isa
<NonStaticGlobalSpaceRegion
>(R
->getMemorySpace());
1243 llvm_unreachable("unknown globals filter");
1246 bool InvalidateRegionsWorker::includeEntireMemorySpace(const MemRegion
*Base
) {
1247 if (isInitiallyIncludedGlobalRegion(Base
))
1250 const MemSpaceRegion
*MemSpace
= Base
->getMemorySpace();
1251 return ITraits
.hasTrait(MemSpace
,
1252 RegionAndSymbolInvalidationTraits::TK_EntireMemSpace
);
1256 RegionStoreManager::invalidateGlobalRegion(MemRegion::Kind K
,
1259 const LocationContext
*LCtx
,
1260 RegionBindingsRef B
,
1261 InvalidatedRegions
*Invalidated
) {
1262 // Bind the globals memory space to a new symbol that we will use to derive
1263 // the bindings for all globals.
1264 const GlobalsSpaceRegion
*GS
= MRMgr
.getGlobalsRegion(K
);
1265 SVal V
= svalBuilder
.conjureSymbolVal(/* symbolTag = */ (const void*) GS
, Ex
, LCtx
,
1266 /* type does not matter */ Ctx
.IntTy
,
1269 B
= B
.removeBinding(GS
)
1270 .addBinding(BindingKey::Make(GS
, BindingKey::Default
), V
);
1272 // Even if there are no bindings in the global scope, we still need to
1273 // record that we touched it.
1275 Invalidated
->push_back(GS
);
1280 void RegionStoreManager::populateWorkList(InvalidateRegionsWorker
&W
,
1281 ArrayRef
<SVal
> Values
,
1282 InvalidatedRegions
*TopLevelRegions
) {
1283 for (SVal V
: Values
) {
1284 if (auto LCS
= V
.getAs
<nonloc::LazyCompoundVal
>()) {
1285 for (SVal S
: getInterestingValues(*LCS
))
1286 if (const MemRegion
*R
= S
.getAsRegion())
1292 if (const MemRegion
*R
= V
.getAsRegion()) {
1293 if (TopLevelRegions
)
1294 TopLevelRegions
->push_back(R
);
1302 RegionStoreManager::invalidateRegions(Store store
,
1303 ArrayRef
<SVal
> Values
,
1304 const Expr
*Ex
, unsigned Count
,
1305 const LocationContext
*LCtx
,
1306 const CallEvent
*Call
,
1307 InvalidatedSymbols
&IS
,
1308 RegionAndSymbolInvalidationTraits
&ITraits
,
1309 InvalidatedRegions
*TopLevelRegions
,
1310 InvalidatedRegions
*Invalidated
) {
1311 GlobalsFilterKind GlobalsFilter
;
1313 if (Call
->isInSystemHeader())
1314 GlobalsFilter
= GFK_SystemOnly
;
1316 GlobalsFilter
= GFK_All
;
1318 GlobalsFilter
= GFK_None
;
1321 RegionBindingsRef B
= getRegionBindings(store
);
1322 InvalidateRegionsWorker
W(*this, StateMgr
, B
, Ex
, Count
, LCtx
, IS
, ITraits
,
1323 Invalidated
, GlobalsFilter
);
1325 // Scan the bindings and generate the clusters.
1326 W
.GenerateClusters();
1328 // Add the regions to the worklist.
1329 populateWorkList(W
, Values
, TopLevelRegions
);
1333 // Return the new bindings.
1334 B
= W
.getRegionBindings();
1336 // For calls, determine which global regions should be invalidated and
1337 // invalidate them. (Note that function-static and immutable globals are never
1338 // invalidated by this.)
1339 // TODO: This could possibly be more precise with modules.
1340 switch (GlobalsFilter
) {
1342 B
= invalidateGlobalRegion(MemRegion::GlobalInternalSpaceRegionKind
,
1343 Ex
, Count
, LCtx
, B
, Invalidated
);
1345 case GFK_SystemOnly
:
1346 B
= invalidateGlobalRegion(MemRegion::GlobalSystemSpaceRegionKind
,
1347 Ex
, Count
, LCtx
, B
, Invalidated
);
1353 return StoreRef(B
.asStore(), *this);
1356 //===----------------------------------------------------------------------===//
1357 // Location and region casting.
1358 //===----------------------------------------------------------------------===//
1360 /// ArrayToPointer - Emulates the "decay" of an array to a pointer
1361 /// type. 'Array' represents the lvalue of the array being decayed
1362 /// to a pointer, and the returned SVal represents the decayed
1363 /// version of that lvalue (i.e., a pointer to the first element of
1364 /// the array). This is called by ExprEngine when evaluating casts
1365 /// from arrays to pointers.
1366 SVal
RegionStoreManager::ArrayToPointer(Loc Array
, QualType T
) {
1367 if (isa
<loc::ConcreteInt
>(Array
))
1370 if (!isa
<loc::MemRegionVal
>(Array
))
1371 return UnknownVal();
1373 const SubRegion
*R
=
1374 cast
<SubRegion
>(Array
.castAs
<loc::MemRegionVal
>().getRegion());
1375 NonLoc ZeroIdx
= svalBuilder
.makeZeroArrayIndex();
1376 return loc::MemRegionVal(MRMgr
.getElementRegion(T
, ZeroIdx
, R
, Ctx
));
1379 //===----------------------------------------------------------------------===//
1380 // Loading values from regions.
1381 //===----------------------------------------------------------------------===//
1383 SVal
RegionStoreManager::getBinding(RegionBindingsConstRef B
, Loc L
, QualType T
) {
1384 assert(!isa
<UnknownVal
>(L
) && "location unknown");
1385 assert(!isa
<UndefinedVal
>(L
) && "location undefined");
1387 // For access to concrete addresses, return UnknownVal. Checks
1388 // for null dereferences (and similar errors) are done by checkers, not
1390 // FIXME: We can consider lazily symbolicating such memory, but we really
1391 // should defer this when we can reason easily about symbolicating arrays
1393 if (L
.getAs
<loc::ConcreteInt
>()) {
1394 return UnknownVal();
1396 if (!L
.getAs
<loc::MemRegionVal
>()) {
1397 return UnknownVal();
1400 const MemRegion
*MR
= L
.castAs
<loc::MemRegionVal
>().getRegion();
1402 if (isa
<BlockDataRegion
>(MR
)) {
1403 return UnknownVal();
1406 // Auto-detect the binding type.
1408 if (const auto *TVR
= dyn_cast
<TypedValueRegion
>(MR
))
1409 T
= TVR
->getValueType();
1410 else if (const auto *TR
= dyn_cast
<TypedRegion
>(MR
))
1411 T
= TR
->getLocationType()->getPointeeType();
1412 else if (const auto *SR
= dyn_cast
<SymbolicRegion
>(MR
))
1413 T
= SR
->getPointeeStaticType();
1415 assert(!T
.isNull() && "Unable to auto-detect binding type!");
1416 assert(!T
->isVoidType() && "Attempting to dereference a void pointer!");
1418 if (!isa
<TypedValueRegion
>(MR
))
1419 MR
= GetElementZeroRegion(cast
<SubRegion
>(MR
), T
);
1421 // FIXME: Perhaps this method should just take a 'const MemRegion*' argument
1422 // instead of 'Loc', and have the other Loc cases handled at a higher level.
1423 const TypedValueRegion
*R
= cast
<TypedValueRegion
>(MR
);
1424 QualType RTy
= R
->getValueType();
1426 // FIXME: we do not yet model the parts of a complex type, so treat the
1427 // whole thing as "unknown".
1428 if (RTy
->isAnyComplexType())
1429 return UnknownVal();
1431 // FIXME: We should eventually handle funny addressing. e.g.:
1435 // char *q = (char*) p;
1436 // char c = *q; // returns the first byte of 'x'.
1438 // Such funny addressing will occur due to layering of regions.
1439 if (RTy
->isStructureOrClassType())
1440 return getBindingForStruct(B
, R
);
1442 // FIXME: Handle unions.
1443 if (RTy
->isUnionType())
1444 return createLazyBinding(B
, R
);
1446 if (RTy
->isArrayType()) {
1447 if (RTy
->isConstantArrayType())
1448 return getBindingForArray(B
, R
);
1450 return UnknownVal();
1453 // FIXME: handle Vector types.
1454 if (RTy
->isVectorType())
1455 return UnknownVal();
1457 if (const FieldRegion
* FR
= dyn_cast
<FieldRegion
>(R
))
1458 return svalBuilder
.evalCast(getBindingForField(B
, FR
), T
, QualType
{});
1460 if (const ElementRegion
* ER
= dyn_cast
<ElementRegion
>(R
)) {
1461 // FIXME: Here we actually perform an implicit conversion from the loaded
1462 // value to the element type. Eventually we want to compose these values
1463 // more intelligently. For example, an 'element' can encompass multiple
1464 // bound regions (e.g., several bound bytes), or could be a subset of
1466 return svalBuilder
.evalCast(getBindingForElement(B
, ER
), T
, QualType
{});
1469 if (const ObjCIvarRegion
*IVR
= dyn_cast
<ObjCIvarRegion
>(R
)) {
1470 // FIXME: Here we actually perform an implicit conversion from the loaded
1471 // value to the ivar type. What we should model is stores to ivars
1472 // that blow past the extent of the ivar. If the address of the ivar is
1473 // reinterpretted, it is possible we stored a different value that could
1474 // fit within the ivar. Either we need to cast these when storing them
1475 // or reinterpret them lazily (as we do here).
1476 return svalBuilder
.evalCast(getBindingForObjCIvar(B
, IVR
), T
, QualType
{});
1479 if (const VarRegion
*VR
= dyn_cast
<VarRegion
>(R
)) {
1480 // FIXME: Here we actually perform an implicit conversion from the loaded
1481 // value to the variable type. What we should model is stores to variables
1482 // that blow past the extent of the variable. If the address of the
1483 // variable is reinterpretted, it is possible we stored a different value
1484 // that could fit within the variable. Either we need to cast these when
1485 // storing them or reinterpret them lazily (as we do here).
1486 return svalBuilder
.evalCast(getBindingForVar(B
, VR
), T
, QualType
{});
1489 const SVal
*V
= B
.lookup(R
, BindingKey::Direct
);
1491 // Check if the region has a binding.
1495 // The location does not have a bound value. This means that it has
1496 // the value it had upon its creation and/or entry to the analyzed
1497 // function/method. These are either symbolic values or 'undefined'.
1498 if (R
->hasStackNonParametersStorage()) {
1499 // All stack variables are considered to have undefined values
1500 // upon creation. All heap allocated blocks are considered to
1501 // have undefined values as well unless they are explicitly bound
1502 // to specific values.
1503 return UndefinedVal();
1506 // All other values are symbolic.
1507 return svalBuilder
.getRegionValueSymbolVal(R
);
1510 static QualType
getUnderlyingType(const SubRegion
*R
) {
1512 if (const TypedValueRegion
*TVR
= dyn_cast
<TypedValueRegion
>(R
))
1513 RegionTy
= TVR
->getValueType();
1515 if (const SymbolicRegion
*SR
= dyn_cast
<SymbolicRegion
>(R
))
1516 RegionTy
= SR
->getSymbol()->getType();
1521 /// Checks to see if store \p B has a lazy binding for region \p R.
1523 /// If \p AllowSubregionBindings is \c false, a lazy binding will be rejected
1524 /// if there are additional bindings within \p R.
1526 /// Note that unlike RegionStoreManager::findLazyBinding, this will not search
1527 /// for lazy bindings for super-regions of \p R.
1528 static std::optional
<nonloc::LazyCompoundVal
>
1529 getExistingLazyBinding(SValBuilder
&SVB
, RegionBindingsConstRef B
,
1530 const SubRegion
*R
, bool AllowSubregionBindings
) {
1531 std::optional
<SVal
> V
= B
.getDefaultBinding(R
);
1533 return std::nullopt
;
1535 std::optional
<nonloc::LazyCompoundVal
> LCV
=
1536 V
->getAs
<nonloc::LazyCompoundVal
>();
1538 return std::nullopt
;
1540 // If the LCV is for a subregion, the types might not match, and we shouldn't
1541 // reuse the binding.
1542 QualType RegionTy
= getUnderlyingType(R
);
1543 if (!RegionTy
.isNull() &&
1544 !RegionTy
->isVoidPointerType()) {
1545 QualType SourceRegionTy
= LCV
->getRegion()->getValueType();
1546 if (!SVB
.getContext().hasSameUnqualifiedType(RegionTy
, SourceRegionTy
))
1547 return std::nullopt
;
1550 if (!AllowSubregionBindings
) {
1551 // If there are any other bindings within this region, we shouldn't reuse
1552 // the top-level binding.
1553 SmallVector
<BindingPair
, 16> Bindings
;
1554 collectSubRegionBindings(Bindings
, SVB
, *B
.lookup(R
->getBaseRegion()), R
,
1555 /*IncludeAllDefaultBindings=*/true);
1556 if (Bindings
.size() > 1)
1557 return std::nullopt
;
1563 std::pair
<Store
, const SubRegion
*>
1564 RegionStoreManager::findLazyBinding(RegionBindingsConstRef B
,
1566 const SubRegion
*originalRegion
) {
1567 if (originalRegion
!= R
) {
1568 if (std::optional
<nonloc::LazyCompoundVal
> V
=
1569 getExistingLazyBinding(svalBuilder
, B
, R
, true))
1570 return std::make_pair(V
->getStore(), V
->getRegion());
1573 typedef std::pair
<Store
, const SubRegion
*> StoreRegionPair
;
1574 StoreRegionPair Result
= StoreRegionPair();
1576 if (const ElementRegion
*ER
= dyn_cast
<ElementRegion
>(R
)) {
1577 Result
= findLazyBinding(B
, cast
<SubRegion
>(ER
->getSuperRegion()),
1581 Result
.second
= MRMgr
.getElementRegionWithSuper(ER
, Result
.second
);
1583 } else if (const FieldRegion
*FR
= dyn_cast
<FieldRegion
>(R
)) {
1584 Result
= findLazyBinding(B
, cast
<SubRegion
>(FR
->getSuperRegion()),
1588 Result
.second
= MRMgr
.getFieldRegionWithSuper(FR
, Result
.second
);
1590 } else if (const CXXBaseObjectRegion
*BaseReg
=
1591 dyn_cast
<CXXBaseObjectRegion
>(R
)) {
1592 // C++ base object region is another kind of region that we should blast
1593 // through to look for lazy compound value. It is like a field region.
1594 Result
= findLazyBinding(B
, cast
<SubRegion
>(BaseReg
->getSuperRegion()),
1598 Result
.second
= MRMgr
.getCXXBaseObjectRegionWithSuper(BaseReg
,
1605 /// This is a helper function for `getConstantValFromConstArrayInitializer`.
1607 /// Return an array of extents of the declared array type.
1609 /// E.g. for `int x[1][2][3];` returns { 1, 2, 3 }.
1610 static SmallVector
<uint64_t, 2>
1611 getConstantArrayExtents(const ConstantArrayType
*CAT
) {
1612 assert(CAT
&& "ConstantArrayType should not be null");
1613 CAT
= cast
<ConstantArrayType
>(CAT
->getCanonicalTypeInternal());
1614 SmallVector
<uint64_t, 2> Extents
;
1616 Extents
.push_back(CAT
->getSize().getZExtValue());
1617 } while ((CAT
= dyn_cast
<ConstantArrayType
>(CAT
->getElementType())));
1621 /// This is a helper function for `getConstantValFromConstArrayInitializer`.
1623 /// Return an array of offsets from nested ElementRegions and a root base
1624 /// region. The array is never empty and a base region is never null.
1626 /// E.g. for `Element{Element{Element{VarRegion},1},2},3}` returns { 3, 2, 1 }.
1627 /// This represents an access through indirection: `arr[1][2][3];`
1629 /// \param ER The given (possibly nested) ElementRegion.
1631 /// \note The result array is in the reverse order of indirection expression:
1632 /// arr[1][2][3] -> { 3, 2, 1 }. This helps to provide complexity O(n), where n
1633 /// is a number of indirections. It may not affect performance in real-life
1635 static std::pair
<SmallVector
<SVal
, 2>, const MemRegion
*>
1636 getElementRegionOffsetsWithBase(const ElementRegion
*ER
) {
1637 assert(ER
&& "ConstantArrayType should not be null");
1638 const MemRegion
*Base
;
1639 SmallVector
<SVal
, 2> SValOffsets
;
1641 SValOffsets
.push_back(ER
->getIndex());
1642 Base
= ER
->getSuperRegion();
1643 ER
= dyn_cast
<ElementRegion
>(Base
);
1645 return {SValOffsets
, Base
};
1648 /// This is a helper function for `getConstantValFromConstArrayInitializer`.
1650 /// Convert array of offsets from `SVal` to `uint64_t` in consideration of
1651 /// respective array extents.
1652 /// \param SrcOffsets [in] The array of offsets of type `SVal` in reversed
1653 /// order (expectedly received from `getElementRegionOffsetsWithBase`).
1654 /// \param ArrayExtents [in] The array of extents.
1655 /// \param DstOffsets [out] The array of offsets of type `uint64_t`.
1657 /// - `std::nullopt` for successful convertion.
1658 /// - `UndefinedVal` or `UnknownVal` otherwise. It's expected that this SVal
1659 /// will be returned as a suitable value of the access operation.
1660 /// which should be returned as a correct
1663 /// const int arr[10][20][30] = {}; // ArrayExtents { 10, 20, 30 }
1664 /// int x1 = arr[4][5][6]; // SrcOffsets { NonLoc(6), NonLoc(5), NonLoc(4) }
1665 /// // DstOffsets { 4, 5, 6 }
1666 /// // returns std::nullopt
1667 /// int x2 = arr[42][5][-6]; // returns UndefinedVal
1668 /// int x3 = arr[4][5][x2]; // returns UnknownVal
1669 static std::optional
<SVal
>
1670 convertOffsetsFromSvalToUnsigneds(const SmallVector
<SVal
, 2> &SrcOffsets
,
1671 const SmallVector
<uint64_t, 2> ArrayExtents
,
1672 SmallVector
<uint64_t, 2> &DstOffsets
) {
1673 // Check offsets for being out of bounds.
1674 // C++20 [expr.add] 7.6.6.4 (excerpt):
1675 // If P points to an array element i of an array object x with n
1676 // elements, where i < 0 or i > n, the behavior is undefined.
1677 // Dereferencing is not allowed on the "one past the last
1678 // element", when i == n.
1680 // const int arr[3][2] = {{1, 2}, {3, 4}};
1686 // arr[1][-1]; // UB
1689 // arr[-2][0]; // UB
1690 DstOffsets
.resize(SrcOffsets
.size());
1691 auto ExtentIt
= ArrayExtents
.begin();
1692 auto OffsetIt
= DstOffsets
.begin();
1693 // Reverse `SValOffsets` to make it consistent with `ArrayExtents`.
1694 for (SVal V
: llvm::reverse(SrcOffsets
)) {
1695 if (auto CI
= V
.getAs
<nonloc::ConcreteInt
>()) {
1696 // When offset is out of array's bounds, result is UB.
1697 const llvm::APSInt
&Offset
= CI
->getValue();
1698 if (Offset
.isNegative() || Offset
.uge(*(ExtentIt
++)))
1699 return UndefinedVal();
1700 // Store index in a reversive order.
1701 *(OffsetIt
++) = Offset
.getZExtValue();
1704 // Symbolic index presented. Return Unknown value.
1705 // FIXME: We also need to take ElementRegions with symbolic indexes into
1707 return UnknownVal();
1709 return std::nullopt
;
1712 std::optional
<SVal
> RegionStoreManager::getConstantValFromConstArrayInitializer(
1713 RegionBindingsConstRef B
, const ElementRegion
*R
) {
1714 assert(R
&& "ElementRegion should not be null");
1716 // Treat an n-dimensional array.
1717 SmallVector
<SVal
, 2> SValOffsets
;
1718 const MemRegion
*Base
;
1719 std::tie(SValOffsets
, Base
) = getElementRegionOffsetsWithBase(R
);
1720 const VarRegion
*VR
= dyn_cast
<VarRegion
>(Base
);
1722 return std::nullopt
;
1724 assert(!SValOffsets
.empty() && "getElementRegionOffsets guarantees the "
1725 "offsets vector is not empty.");
1727 // Check if the containing array has an initialized value that we can trust.
1728 // We can trust a const value or a value of a global initializer in main().
1729 const VarDecl
*VD
= VR
->getDecl();
1730 if (!VD
->getType().isConstQualified() &&
1731 !R
->getElementType().isConstQualified() &&
1732 (!B
.isMainAnalysis() || !VD
->hasGlobalStorage()))
1733 return std::nullopt
;
1735 // Array's declaration should have `ConstantArrayType` type, because only this
1736 // type contains an array extent. It may happen that array type can be of
1737 // `IncompleteArrayType` type. To get the declaration of `ConstantArrayType`
1738 // type, we should find the declaration in the redeclarations chain that has
1739 // the initialization expression.
1740 // NOTE: `getAnyInitializer` has an out-parameter, which returns a new `VD`
1741 // from which an initializer is obtained. We replace current `VD` with the new
1742 // `VD`. If the return value of the function is null than `VD` won't be
1744 const Expr
*Init
= VD
->getAnyInitializer(VD
);
1745 // NOTE: If `Init` is non-null, then a new `VD` is non-null for sure. So check
1746 // `Init` for null only and don't worry about the replaced `VD`.
1748 return std::nullopt
;
1750 // Array's declaration should have ConstantArrayType type, because only this
1751 // type contains an array extent.
1752 const ConstantArrayType
*CAT
= Ctx
.getAsConstantArrayType(VD
->getType());
1754 return std::nullopt
;
1756 // Get array extents.
1757 SmallVector
<uint64_t, 2> Extents
= getConstantArrayExtents(CAT
);
1759 // The number of offsets should equal to the numbers of extents,
1760 // otherwise wrong type punning occurred. For instance:
1761 // int arr[1][2][3];
1762 // auto ptr = (int(*)[42])arr;
1763 // auto x = ptr[4][2]; // UB
1764 // FIXME: Should return UndefinedVal.
1765 if (SValOffsets
.size() != Extents
.size())
1766 return std::nullopt
;
1768 SmallVector
<uint64_t, 2> ConcreteOffsets
;
1769 if (std::optional
<SVal
> V
= convertOffsetsFromSvalToUnsigneds(
1770 SValOffsets
, Extents
, ConcreteOffsets
))
1773 // Handle InitListExpr.
1775 // const char arr[4][2] = { { 1, 2 }, { 3 }, 4, 5 };
1776 if (const auto *ILE
= dyn_cast
<InitListExpr
>(Init
))
1777 return getSValFromInitListExpr(ILE
, ConcreteOffsets
, R
->getElementType());
1779 // Handle StringLiteral.
1781 // const char arr[] = "abc";
1782 if (const auto *SL
= dyn_cast
<StringLiteral
>(Init
))
1783 return getSValFromStringLiteral(SL
, ConcreteOffsets
.front(),
1784 R
->getElementType());
1786 // FIXME: Handle CompoundLiteralExpr.
1788 return std::nullopt
;
1791 /// Returns an SVal, if possible, for the specified position of an
1792 /// initialization list.
1794 /// \param ILE The given initialization list.
1795 /// \param Offsets The array of unsigned offsets. E.g. for the expression
1796 /// `int x = arr[1][2][3];` an array should be { 1, 2, 3 }.
1797 /// \param ElemT The type of the result SVal expression.
1798 /// \return Optional SVal for the particular position in the initialization
1799 /// list. E.g. for the list `{{1, 2},[3, 4],{5, 6}, {}}` offsets:
1800 /// - {1, 1} returns SVal{4}, because it's the second position in the second
1802 /// - {3, 0} returns SVal{0}, because there's no explicit value at this
1803 /// position in the sublist.
1805 /// NOTE: Inorder to get a valid SVal, a caller shall guarantee valid offsets
1806 /// for the given initialization list. Otherwise SVal can be an equivalent to 0
1807 /// or lead to assertion.
1808 std::optional
<SVal
> RegionStoreManager::getSValFromInitListExpr(
1809 const InitListExpr
*ILE
, const SmallVector
<uint64_t, 2> &Offsets
,
1811 assert(ILE
&& "InitListExpr should not be null");
1813 for (uint64_t Offset
: Offsets
) {
1814 // C++20 [dcl.init.string] 9.4.2.1:
1815 // An array of ordinary character type [...] can be initialized by [...]
1816 // an appropriately-typed string-literal enclosed in braces.
1818 // const char arr[] = { "abc" };
1819 if (ILE
->isStringLiteralInit())
1820 if (const auto *SL
= dyn_cast
<StringLiteral
>(ILE
->getInit(0)))
1821 return getSValFromStringLiteral(SL
, Offset
, ElemT
);
1823 // C++20 [expr.add] 9.4.17.5 (excerpt):
1824 // i-th array element is value-initialized for each k < i ≤ n,
1825 // where k is an expression-list size and n is an array extent.
1826 if (Offset
>= ILE
->getNumInits())
1827 return svalBuilder
.makeZeroVal(ElemT
);
1829 const Expr
*E
= ILE
->getInit(Offset
);
1830 const auto *IL
= dyn_cast
<InitListExpr
>(E
);
1832 // Return a constant value, if it is presented.
1833 // FIXME: Support other SVals.
1834 return svalBuilder
.getConstantVal(E
);
1836 // Go to the nested initializer list.
1842 // FIXME: Unhandeled InitListExpr sub-expression, possibly constructing an
1844 return std::nullopt
;
1847 /// Returns an SVal, if possible, for the specified position in a string
1850 /// \param SL The given string literal.
1851 /// \param Offset The unsigned offset. E.g. for the expression
1852 /// `char x = str[42];` an offset should be 42.
1853 /// E.g. for the string "abc" offset:
1854 /// - 1 returns SVal{b}, because it's the second position in the string.
1855 /// - 42 returns SVal{0}, because there's no explicit value at this
1856 /// position in the string.
1857 /// \param ElemT The type of the result SVal expression.
1859 /// NOTE: We return `0` for every offset >= the literal length for array
1860 /// declarations, like:
1861 /// const char str[42] = "123"; // Literal length is 4.
1862 /// char c = str[41]; // Offset is 41.
1863 /// FIXME: Nevertheless, we can't do the same for pointer declaraions, like:
1864 /// const char * const str = "123"; // Literal length is 4.
1865 /// char c = str[41]; // Offset is 41. Returns `0`, but Undef
1867 /// It should be properly handled before reaching this point.
1868 /// The main problem is that we can't distinguish between these declarations,
1869 /// because in case of array we can get the Decl from VarRegion, but in case
1870 /// of pointer the region is a StringRegion, which doesn't contain a Decl.
1871 /// Possible solution could be passing an array extent along with the offset.
1872 SVal
RegionStoreManager::getSValFromStringLiteral(const StringLiteral
*SL
,
1875 assert(SL
&& "StringLiteral should not be null");
1876 // C++20 [dcl.init.string] 9.4.2.3:
1877 // If there are fewer initializers than there are array elements, each
1878 // element not explicitly initialized shall be zero-initialized [dcl.init].
1879 uint32_t Code
= (Offset
>= SL
->getLength()) ? 0 : SL
->getCodeUnit(Offset
);
1880 return svalBuilder
.makeIntVal(Code
, ElemT
);
1883 static std::optional
<SVal
> getDerivedSymbolForBinding(
1884 RegionBindingsConstRef B
, const TypedValueRegion
*BaseRegion
,
1885 const TypedValueRegion
*SubReg
, const ASTContext
&Ctx
, SValBuilder
&SVB
) {
1887 QualType BaseTy
= BaseRegion
->getValueType();
1888 QualType Ty
= SubReg
->getValueType();
1889 if (BaseTy
->isScalarType() && Ty
->isScalarType()) {
1890 if (Ctx
.getTypeSizeInChars(BaseTy
) >= Ctx
.getTypeSizeInChars(Ty
)) {
1891 if (const std::optional
<SVal
> &ParentValue
=
1892 B
.getDirectBinding(BaseRegion
)) {
1893 if (SymbolRef ParentValueAsSym
= ParentValue
->getAsSymbol())
1894 return SVB
.getDerivedRegionValueSymbolVal(ParentValueAsSym
, SubReg
);
1896 if (ParentValue
->isUndef())
1897 return UndefinedVal();
1899 // Other cases: give up. We are indexing into a larger object
1900 // that has some value, but we don't know how to handle that yet.
1901 return UnknownVal();
1905 return std::nullopt
;
1908 SVal
RegionStoreManager::getBindingForElement(RegionBindingsConstRef B
,
1909 const ElementRegion
* R
) {
1910 // Check if the region has a binding.
1911 if (const std::optional
<SVal
> &V
= B
.getDirectBinding(R
))
1914 const MemRegion
* superR
= R
->getSuperRegion();
1916 // Check if the region is an element region of a string literal.
1917 if (const StringRegion
*StrR
= dyn_cast
<StringRegion
>(superR
)) {
1918 // FIXME: Handle loads from strings where the literal is treated as
1919 // an integer, e.g., *((unsigned int*)"hello"). Such loads are UB according
1920 // to C++20 7.2.1.11 [basic.lval].
1921 QualType T
= Ctx
.getAsArrayType(StrR
->getValueType())->getElementType();
1922 if (!Ctx
.hasSameUnqualifiedType(T
, R
->getElementType()))
1923 return UnknownVal();
1924 if (const auto CI
= R
->getIndex().getAs
<nonloc::ConcreteInt
>()) {
1925 const llvm::APSInt
&Idx
= CI
->getValue();
1927 return UndefinedVal();
1928 const StringLiteral
*SL
= StrR
->getStringLiteral();
1929 return getSValFromStringLiteral(SL
, Idx
.getZExtValue(), T
);
1931 } else if (isa
<ElementRegion
, VarRegion
>(superR
)) {
1932 if (std::optional
<SVal
> V
= getConstantValFromConstArrayInitializer(B
, R
))
1936 // Check for loads from a code text region. For such loads, just give up.
1937 if (isa
<CodeTextRegion
>(superR
))
1938 return UnknownVal();
1940 // Handle the case where we are indexing into a larger scalar object.
1941 // For example, this handles:
1945 // FIXME: This is a hack, and doesn't do anything really intelligent yet.
1946 const RegionRawOffset
&O
= R
->getAsArrayOffset();
1948 // If we cannot reason about the offset, return an unknown value.
1950 return UnknownVal();
1952 if (const TypedValueRegion
*baseR
= dyn_cast
<TypedValueRegion
>(O
.getRegion()))
1953 if (auto V
= getDerivedSymbolForBinding(B
, baseR
, R
, Ctx
, svalBuilder
))
1956 return getBindingForFieldOrElementCommon(B
, R
, R
->getElementType());
1959 SVal
RegionStoreManager::getBindingForField(RegionBindingsConstRef B
,
1960 const FieldRegion
* R
) {
1962 // Check if the region has a binding.
1963 if (const std::optional
<SVal
> &V
= B
.getDirectBinding(R
))
1966 // If the containing record was initialized, try to get its constant value.
1967 const FieldDecl
*FD
= R
->getDecl();
1968 QualType Ty
= FD
->getType();
1969 const MemRegion
* superR
= R
->getSuperRegion();
1970 if (const auto *VR
= dyn_cast
<VarRegion
>(superR
)) {
1971 const VarDecl
*VD
= VR
->getDecl();
1972 QualType RecordVarTy
= VD
->getType();
1973 unsigned Index
= FD
->getFieldIndex();
1974 // Either the record variable or the field has an initializer that we can
1975 // trust. We trust initializers of constants and, additionally, respect
1976 // initializers of globals when analyzing main().
1977 if (RecordVarTy
.isConstQualified() || Ty
.isConstQualified() ||
1978 (B
.isMainAnalysis() && VD
->hasGlobalStorage()))
1979 if (const Expr
*Init
= VD
->getAnyInitializer())
1980 if (const auto *InitList
= dyn_cast
<InitListExpr
>(Init
)) {
1981 if (Index
< InitList
->getNumInits()) {
1982 if (const Expr
*FieldInit
= InitList
->getInit(Index
))
1983 if (std::optional
<SVal
> V
= svalBuilder
.getConstantVal(FieldInit
))
1986 return svalBuilder
.makeZeroVal(Ty
);
1991 // Handle the case where we are accessing into a larger scalar object.
1992 // For example, this handles:
1998 // unsigned bits0 : 1;
1999 // unsigned bits2 : 2; // <-- header
2000 // unsigned bits4 : 4;
2002 // int parse(parse_t *p) {
2003 // unsigned copy = p->bits2;
2004 // header *bits = (header *)©
2005 // return bits->b; <-- here
2007 if (const auto *Base
= dyn_cast
<TypedValueRegion
>(R
->getBaseRegion()))
2008 if (auto V
= getDerivedSymbolForBinding(B
, Base
, R
, Ctx
, svalBuilder
))
2011 return getBindingForFieldOrElementCommon(B
, R
, Ty
);
2014 std::optional
<SVal
> RegionStoreManager::getBindingForDerivedDefaultValue(
2015 RegionBindingsConstRef B
, const MemRegion
*superR
,
2016 const TypedValueRegion
*R
, QualType Ty
) {
2018 if (const std::optional
<SVal
> &D
= B
.getDefaultBinding(superR
)) {
2019 const SVal
&val
= *D
;
2020 if (SymbolRef parentSym
= val
.getAsSymbol())
2021 return svalBuilder
.getDerivedRegionValueSymbolVal(parentSym
, R
);
2023 if (val
.isZeroConstant())
2024 return svalBuilder
.makeZeroVal(Ty
);
2026 if (val
.isUnknownOrUndef())
2029 // Lazy bindings are usually handled through getExistingLazyBinding().
2030 // We should unify these two code paths at some point.
2031 if (isa
<nonloc::LazyCompoundVal
, nonloc::CompoundVal
>(val
))
2034 llvm_unreachable("Unknown default value");
2037 return std::nullopt
;
2040 SVal
RegionStoreManager::getLazyBinding(const SubRegion
*LazyBindingRegion
,
2041 RegionBindingsRef LazyBinding
) {
2043 if (const ElementRegion
*ER
= dyn_cast
<ElementRegion
>(LazyBindingRegion
))
2044 Result
= getBindingForElement(LazyBinding
, ER
);
2046 Result
= getBindingForField(LazyBinding
,
2047 cast
<FieldRegion
>(LazyBindingRegion
));
2049 // FIXME: This is a hack to deal with RegionStore's inability to distinguish a
2050 // default value for /part/ of an aggregate from a default value for the
2051 // /entire/ aggregate. The most common case of this is when struct Outer
2052 // has as its first member a struct Inner, which is copied in from a stack
2053 // variable. In this case, even if the Outer's default value is symbolic, 0,
2054 // or unknown, it gets overridden by the Inner's default value of undefined.
2056 // This is a general problem -- if the Inner is zero-initialized, the Outer
2057 // will now look zero-initialized. The proper way to solve this is with a
2058 // new version of RegionStore that tracks the extent of a binding as well
2061 // This hack only takes care of the undefined case because that can very
2062 // quickly result in a warning.
2063 if (Result
.isUndef())
2064 Result
= UnknownVal();
2070 RegionStoreManager::getBindingForFieldOrElementCommon(RegionBindingsConstRef B
,
2071 const TypedValueRegion
*R
,
2074 // At this point we have already checked in either getBindingForElement or
2075 // getBindingForField if 'R' has a direct binding.
2078 Store lazyBindingStore
= nullptr;
2079 const SubRegion
*lazyBindingRegion
= nullptr;
2080 std::tie(lazyBindingStore
, lazyBindingRegion
) = findLazyBinding(B
, R
, R
);
2081 if (lazyBindingRegion
)
2082 return getLazyBinding(lazyBindingRegion
,
2083 getRegionBindings(lazyBindingStore
));
2085 // Record whether or not we see a symbolic index. That can completely
2086 // be out of scope of our lookup.
2087 bool hasSymbolicIndex
= false;
2089 // FIXME: This is a hack to deal with RegionStore's inability to distinguish a
2090 // default value for /part/ of an aggregate from a default value for the
2091 // /entire/ aggregate. The most common case of this is when struct Outer
2092 // has as its first member a struct Inner, which is copied in from a stack
2093 // variable. In this case, even if the Outer's default value is symbolic, 0,
2094 // or unknown, it gets overridden by the Inner's default value of undefined.
2096 // This is a general problem -- if the Inner is zero-initialized, the Outer
2097 // will now look zero-initialized. The proper way to solve this is with a
2098 // new version of RegionStore that tracks the extent of a binding as well
2101 // This hack only takes care of the undefined case because that can very
2102 // quickly result in a warning.
2103 bool hasPartialLazyBinding
= false;
2105 const SubRegion
*SR
= R
;
2107 const MemRegion
*Base
= SR
->getSuperRegion();
2108 if (std::optional
<SVal
> D
=
2109 getBindingForDerivedDefaultValue(B
, Base
, R
, Ty
)) {
2110 if (D
->getAs
<nonloc::LazyCompoundVal
>()) {
2111 hasPartialLazyBinding
= true;
2118 if (const ElementRegion
*ER
= dyn_cast
<ElementRegion
>(Base
)) {
2119 NonLoc index
= ER
->getIndex();
2120 if (!index
.isConstant())
2121 hasSymbolicIndex
= true;
2124 // If our super region is a field or element itself, walk up the region
2125 // hierarchy to see if there is a default value installed in an ancestor.
2126 SR
= dyn_cast
<SubRegion
>(Base
);
2129 if (R
->hasStackNonParametersStorage()) {
2130 if (isa
<ElementRegion
>(R
)) {
2131 // Currently we don't reason specially about Clang-style vectors. Check
2132 // if superR is a vector and if so return Unknown.
2133 if (const TypedValueRegion
*typedSuperR
=
2134 dyn_cast
<TypedValueRegion
>(R
->getSuperRegion())) {
2135 if (typedSuperR
->getValueType()->isVectorType())
2136 return UnknownVal();
2140 // FIXME: We also need to take ElementRegions with symbolic indexes into
2141 // account. This case handles both directly accessing an ElementRegion
2142 // with a symbolic offset, but also fields within an element with
2143 // a symbolic offset.
2144 if (hasSymbolicIndex
)
2145 return UnknownVal();
2147 // Additionally allow introspection of a block's internal layout.
2148 // Try to get direct binding if all other attempts failed thus far.
2149 // Else, return UndefinedVal()
2150 if (!hasPartialLazyBinding
&& !isa
<BlockDataRegion
>(R
->getBaseRegion())) {
2151 if (const std::optional
<SVal
> &V
= B
.getDefaultBinding(R
))
2153 return UndefinedVal();
2157 // All other values are symbolic.
2158 return svalBuilder
.getRegionValueSymbolVal(R
);
2161 SVal
RegionStoreManager::getBindingForObjCIvar(RegionBindingsConstRef B
,
2162 const ObjCIvarRegion
* R
) {
2163 // Check if the region has a binding.
2164 if (const std::optional
<SVal
> &V
= B
.getDirectBinding(R
))
2167 const MemRegion
*superR
= R
->getSuperRegion();
2169 // Check if the super region has a default binding.
2170 if (const std::optional
<SVal
> &V
= B
.getDefaultBinding(superR
)) {
2171 if (SymbolRef parentSym
= V
->getAsSymbol())
2172 return svalBuilder
.getDerivedRegionValueSymbolVal(parentSym
, R
);
2174 // Other cases: give up.
2175 return UnknownVal();
2178 return getBindingForLazySymbol(R
);
2181 SVal
RegionStoreManager::getBindingForVar(RegionBindingsConstRef B
,
2182 const VarRegion
*R
) {
2184 // Check if the region has a binding.
2185 if (std::optional
<SVal
> V
= B
.getDirectBinding(R
))
2188 if (std::optional
<SVal
> V
= B
.getDefaultBinding(R
))
2191 // Lazily derive a value for the VarRegion.
2192 const VarDecl
*VD
= R
->getDecl();
2193 const MemSpaceRegion
*MS
= R
->getMemorySpace();
2195 // Arguments are always symbolic.
2196 if (isa
<StackArgumentsSpaceRegion
>(MS
))
2197 return svalBuilder
.getRegionValueSymbolVal(R
);
2199 // Is 'VD' declared constant? If so, retrieve the constant value.
2200 if (VD
->getType().isConstQualified()) {
2201 if (const Expr
*Init
= VD
->getAnyInitializer()) {
2202 if (std::optional
<SVal
> V
= svalBuilder
.getConstantVal(Init
))
2205 // If the variable is const qualified and has an initializer but
2206 // we couldn't evaluate initializer to a value, treat the value as
2208 return UnknownVal();
2212 // This must come after the check for constants because closure-captured
2213 // constant variables may appear in UnknownSpaceRegion.
2214 if (isa
<UnknownSpaceRegion
>(MS
))
2215 return svalBuilder
.getRegionValueSymbolVal(R
);
2217 if (isa
<GlobalsSpaceRegion
>(MS
)) {
2218 QualType T
= VD
->getType();
2220 // If we're in main(), then global initializers have not become stale yet.
2221 if (B
.isMainAnalysis())
2222 if (const Expr
*Init
= VD
->getAnyInitializer())
2223 if (std::optional
<SVal
> V
= svalBuilder
.getConstantVal(Init
))
2226 // Function-scoped static variables are default-initialized to 0; if they
2227 // have an initializer, it would have been processed by now.
2228 // FIXME: This is only true when we're starting analysis from main().
2229 // We're losing a lot of coverage here.
2230 if (isa
<StaticGlobalSpaceRegion
>(MS
))
2231 return svalBuilder
.makeZeroVal(T
);
2233 if (std::optional
<SVal
> V
= getBindingForDerivedDefaultValue(B
, MS
, R
, T
)) {
2234 assert(!V
->getAs
<nonloc::LazyCompoundVal
>());
2238 return svalBuilder
.getRegionValueSymbolVal(R
);
2241 return UndefinedVal();
2244 SVal
RegionStoreManager::getBindingForLazySymbol(const TypedValueRegion
*R
) {
2245 // All other values are symbolic.
2246 return svalBuilder
.getRegionValueSymbolVal(R
);
2249 const RegionStoreManager::SValListTy
&
2250 RegionStoreManager::getInterestingValues(nonloc::LazyCompoundVal LCV
) {
2251 // First, check the cache.
2252 LazyBindingsMapTy::iterator I
= LazyBindingsMap
.find(LCV
.getCVData());
2253 if (I
!= LazyBindingsMap
.end())
2256 // If we don't have a list of values cached, start constructing it.
2259 const SubRegion
*LazyR
= LCV
.getRegion();
2260 RegionBindingsRef B
= getRegionBindings(LCV
.getStore());
2262 // If this region had /no/ bindings at the time, there are no interesting
2263 // values to return.
2264 const ClusterBindings
*Cluster
= B
.lookup(LazyR
->getBaseRegion());
2266 return (LazyBindingsMap
[LCV
.getCVData()] = std::move(List
));
2268 SmallVector
<BindingPair
, 32> Bindings
;
2269 collectSubRegionBindings(Bindings
, svalBuilder
, *Cluster
, LazyR
,
2270 /*IncludeAllDefaultBindings=*/true);
2271 for (SVal V
: llvm::make_second_range(Bindings
)) {
2272 if (V
.isUnknownOrUndef() || V
.isConstant())
2275 if (auto InnerLCV
= V
.getAs
<nonloc::LazyCompoundVal
>()) {
2276 const SValListTy
&InnerList
= getInterestingValues(*InnerLCV
);
2277 List
.insert(List
.end(), InnerList
.begin(), InnerList
.end());
2283 return (LazyBindingsMap
[LCV
.getCVData()] = std::move(List
));
2286 NonLoc
RegionStoreManager::createLazyBinding(RegionBindingsConstRef B
,
2287 const TypedValueRegion
*R
) {
2288 if (std::optional
<nonloc::LazyCompoundVal
> V
=
2289 getExistingLazyBinding(svalBuilder
, B
, R
, false))
2292 return svalBuilder
.makeLazyCompoundVal(StoreRef(B
.asStore(), *this), R
);
2295 static bool isRecordEmpty(const RecordDecl
*RD
) {
2296 if (!RD
->field_empty())
2298 if (const CXXRecordDecl
*CRD
= dyn_cast
<CXXRecordDecl
>(RD
))
2299 return CRD
->getNumBases() == 0;
2303 SVal
RegionStoreManager::getBindingForStruct(RegionBindingsConstRef B
,
2304 const TypedValueRegion
*R
) {
2305 const RecordDecl
*RD
= R
->getValueType()->castAs
<RecordType
>()->getDecl();
2306 if (!RD
->getDefinition() || isRecordEmpty(RD
))
2307 return UnknownVal();
2309 return createLazyBinding(B
, R
);
2312 SVal
RegionStoreManager::getBindingForArray(RegionBindingsConstRef B
,
2313 const TypedValueRegion
*R
) {
2314 assert(Ctx
.getAsConstantArrayType(R
->getValueType()) &&
2315 "Only constant array types can have compound bindings.");
2317 return createLazyBinding(B
, R
);
2320 bool RegionStoreManager::includedInBindings(Store store
,
2321 const MemRegion
*region
) const {
2322 RegionBindingsRef B
= getRegionBindings(store
);
2323 region
= region
->getBaseRegion();
2325 // Quick path: if the base is the head of a cluster, the region is live.
2326 if (B
.lookup(region
))
2329 // Slow path: if the region is the VALUE of any binding, it is live.
2330 for (RegionBindingsRef::iterator RI
= B
.begin(), RE
= B
.end(); RI
!= RE
; ++RI
) {
2331 const ClusterBindings
&Cluster
= RI
.getData();
2332 for (ClusterBindings::iterator CI
= Cluster
.begin(), CE
= Cluster
.end();
2334 const SVal
&D
= CI
.getData();
2335 if (const MemRegion
*R
= D
.getAsRegion())
2336 if (R
->getBaseRegion() == region
)
2344 //===----------------------------------------------------------------------===//
2345 // Binding values to regions.
2346 //===----------------------------------------------------------------------===//
2348 StoreRef
RegionStoreManager::killBinding(Store ST
, Loc L
) {
2349 if (std::optional
<loc::MemRegionVal
> LV
= L
.getAs
<loc::MemRegionVal
>())
2350 if (const MemRegion
* R
= LV
->getRegion())
2351 return StoreRef(getRegionBindings(ST
).removeBinding(R
)
2353 .getRootWithoutRetain(),
2356 return StoreRef(ST
, *this);
2360 RegionStoreManager::bind(RegionBindingsConstRef B
, Loc L
, SVal V
) {
2361 if (L
.getAs
<loc::ConcreteInt
>())
2364 // If we get here, the location should be a region.
2365 const MemRegion
*R
= L
.castAs
<loc::MemRegionVal
>().getRegion();
2367 // Check if the region is a struct region.
2368 if (const TypedValueRegion
* TR
= dyn_cast
<TypedValueRegion
>(R
)) {
2369 QualType Ty
= TR
->getValueType();
2370 if (Ty
->isArrayType())
2371 return bindArray(B
, TR
, V
);
2372 if (Ty
->isStructureOrClassType())
2373 return bindStruct(B
, TR
, V
);
2374 if (Ty
->isVectorType())
2375 return bindVector(B
, TR
, V
);
2376 if (Ty
->isUnionType())
2377 return bindAggregate(B
, TR
, V
);
2380 // Binding directly to a symbolic region should be treated as binding
2382 if (const SymbolicRegion
*SR
= dyn_cast
<SymbolicRegion
>(R
))
2383 R
= GetElementZeroRegion(SR
, SR
->getPointeeStaticType());
2385 assert((!isa
<CXXThisRegion
>(R
) || !B
.lookup(R
)) &&
2386 "'this' pointer is not an l-value and is not assignable");
2388 // Clear out bindings that may overlap with this binding.
2389 RegionBindingsRef NewB
= removeSubRegionBindings(B
, cast
<SubRegion
>(R
));
2391 // LazyCompoundVals should be always bound as 'default' bindings.
2392 auto KeyKind
= isa
<nonloc::LazyCompoundVal
>(V
) ? BindingKey::Default
2393 : BindingKey::Direct
;
2394 return NewB
.addBinding(BindingKey::Make(R
, KeyKind
), V
);
2398 RegionStoreManager::setImplicitDefaultValue(RegionBindingsConstRef B
,
2403 if (Loc::isLocType(T
))
2404 V
= svalBuilder
.makeNullWithType(T
);
2405 else if (T
->isIntegralOrEnumerationType())
2406 V
= svalBuilder
.makeZeroVal(T
);
2407 else if (T
->isStructureOrClassType() || T
->isArrayType()) {
2408 // Set the default value to a zero constant when it is a structure
2409 // or array. The type doesn't really matter.
2410 V
= svalBuilder
.makeZeroVal(Ctx
.IntTy
);
2413 // We can't represent values of this type, but we still need to set a value
2414 // to record that the region has been initialized.
2415 // If this assertion ever fires, a new case should be added above -- we
2416 // should know how to default-initialize any value we can symbolicate.
2417 assert(!SymbolManager::canSymbolicate(T
) && "This type is representable");
2421 return B
.addBinding(R
, BindingKey::Default
, V
);
2424 std::optional
<RegionBindingsRef
> RegionStoreManager::tryBindSmallArray(
2425 RegionBindingsConstRef B
, const TypedValueRegion
*R
, const ArrayType
*AT
,
2426 nonloc::LazyCompoundVal LCV
) {
2428 auto CAT
= dyn_cast
<ConstantArrayType
>(AT
);
2430 // If we don't know the size, create a lazyCompoundVal instead.
2432 return std::nullopt
;
2434 QualType Ty
= CAT
->getElementType();
2435 if (!(Ty
->isScalarType() || Ty
->isReferenceType()))
2436 return std::nullopt
;
2438 // If the array is too big, create a LCV instead.
2439 uint64_t ArrSize
= CAT
->getSize().getLimitedValue();
2440 if (ArrSize
> SmallArrayLimit
)
2441 return std::nullopt
;
2443 RegionBindingsRef NewB
= B
;
2445 for (uint64_t i
= 0; i
< ArrSize
; ++i
) {
2446 auto Idx
= svalBuilder
.makeArrayIndex(i
);
2447 const ElementRegion
*SrcER
=
2448 MRMgr
.getElementRegion(Ty
, Idx
, LCV
.getRegion(), Ctx
);
2449 SVal V
= getBindingForElement(getRegionBindings(LCV
.getStore()), SrcER
);
2451 const ElementRegion
*DstER
= MRMgr
.getElementRegion(Ty
, Idx
, R
, Ctx
);
2452 NewB
= bind(NewB
, loc::MemRegionVal(DstER
), V
);
2459 RegionStoreManager::bindArray(RegionBindingsConstRef B
,
2460 const TypedValueRegion
* R
,
2463 const ArrayType
*AT
=cast
<ArrayType
>(Ctx
.getCanonicalType(R
->getValueType()));
2464 QualType ElementTy
= AT
->getElementType();
2465 std::optional
<uint64_t> Size
;
2467 if (const ConstantArrayType
* CAT
= dyn_cast
<ConstantArrayType
>(AT
))
2468 Size
= CAT
->getSize().getZExtValue();
2470 // Check if the init expr is a literal. If so, bind the rvalue instead.
2471 // FIXME: It's not responsibility of the Store to transform this lvalue
2472 // to rvalue. ExprEngine or maybe even CFG should do this before binding.
2473 if (std::optional
<loc::MemRegionVal
> MRV
= Init
.getAs
<loc::MemRegionVal
>()) {
2474 SVal V
= getBinding(B
.asStore(), *MRV
, R
->getValueType());
2475 return bindAggregate(B
, R
, V
);
2478 // Handle lazy compound values.
2479 if (std::optional
<nonloc::LazyCompoundVal
> LCV
=
2480 Init
.getAs
<nonloc::LazyCompoundVal
>()) {
2481 if (std::optional
<RegionBindingsRef
> NewB
=
2482 tryBindSmallArray(B
, R
, AT
, *LCV
))
2485 return bindAggregate(B
, R
, Init
);
2488 if (Init
.isUnknown())
2489 return bindAggregate(B
, R
, UnknownVal());
2491 // Remaining case: explicit compound values.
2492 const nonloc::CompoundVal
& CV
= Init
.castAs
<nonloc::CompoundVal
>();
2493 nonloc::CompoundVal::iterator VI
= CV
.begin(), VE
= CV
.end();
2496 RegionBindingsRef
NewB(B
);
2498 for (; Size
? i
< *Size
: true; ++i
, ++VI
) {
2499 // The init list might be shorter than the array length.
2503 const NonLoc
&Idx
= svalBuilder
.makeArrayIndex(i
);
2504 const ElementRegion
*ER
= MRMgr
.getElementRegion(ElementTy
, Idx
, R
, Ctx
);
2506 if (ElementTy
->isStructureOrClassType())
2507 NewB
= bindStruct(NewB
, ER
, *VI
);
2508 else if (ElementTy
->isArrayType())
2509 NewB
= bindArray(NewB
, ER
, *VI
);
2511 NewB
= bind(NewB
, loc::MemRegionVal(ER
), *VI
);
2514 // If the init list is shorter than the array length (or the array has
2515 // variable length), set the array default value. Values that are already set
2516 // are not overwritten.
2517 if (!Size
|| i
< *Size
)
2518 NewB
= setImplicitDefaultValue(NewB
, R
, ElementTy
);
2523 RegionBindingsRef
RegionStoreManager::bindVector(RegionBindingsConstRef B
,
2524 const TypedValueRegion
* R
,
2526 QualType T
= R
->getValueType();
2527 const VectorType
*VT
= T
->castAs
<VectorType
>(); // Use castAs for typedefs.
2529 // Handle lazy compound values and symbolic values.
2530 if (isa
<nonloc::LazyCompoundVal
, nonloc::SymbolVal
>(V
))
2531 return bindAggregate(B
, R
, V
);
2533 // We may get non-CompoundVal accidentally due to imprecise cast logic or
2534 // that we are binding symbolic struct value. Kill the field values, and if
2535 // the value is symbolic go and bind it as a "default" binding.
2536 if (!isa
<nonloc::CompoundVal
>(V
)) {
2537 return bindAggregate(B
, R
, UnknownVal());
2540 QualType ElemType
= VT
->getElementType();
2541 nonloc::CompoundVal CV
= V
.castAs
<nonloc::CompoundVal
>();
2542 nonloc::CompoundVal::iterator VI
= CV
.begin(), VE
= CV
.end();
2543 unsigned index
= 0, numElements
= VT
->getNumElements();
2544 RegionBindingsRef
NewB(B
);
2546 for ( ; index
!= numElements
; ++index
) {
2550 NonLoc Idx
= svalBuilder
.makeArrayIndex(index
);
2551 const ElementRegion
*ER
= MRMgr
.getElementRegion(ElemType
, Idx
, R
, Ctx
);
2553 if (ElemType
->isArrayType())
2554 NewB
= bindArray(NewB
, ER
, *VI
);
2555 else if (ElemType
->isStructureOrClassType())
2556 NewB
= bindStruct(NewB
, ER
, *VI
);
2558 NewB
= bind(NewB
, loc::MemRegionVal(ER
), *VI
);
2563 std::optional
<RegionBindingsRef
> RegionStoreManager::tryBindSmallStruct(
2564 RegionBindingsConstRef B
, const TypedValueRegion
*R
, const RecordDecl
*RD
,
2565 nonloc::LazyCompoundVal LCV
) {
2568 if (const CXXRecordDecl
*Class
= dyn_cast
<CXXRecordDecl
>(RD
))
2569 if (Class
->getNumBases() != 0 || Class
->getNumVBases() != 0)
2570 return std::nullopt
;
2572 for (const auto *FD
: RD
->fields()) {
2573 if (FD
->isUnnamedBitfield())
2576 // If there are too many fields, or if any of the fields are aggregates,
2577 // just use the LCV as a default binding.
2578 if (Fields
.size() == SmallStructLimit
)
2579 return std::nullopt
;
2581 QualType Ty
= FD
->getType();
2583 // Zero length arrays are basically no-ops, so we also ignore them here.
2584 if (Ty
->isConstantArrayType() &&
2585 Ctx
.getConstantArrayElementCount(Ctx
.getAsConstantArrayType(Ty
)) == 0)
2588 if (!(Ty
->isScalarType() || Ty
->isReferenceType()))
2589 return std::nullopt
;
2591 Fields
.push_back(FD
);
2594 RegionBindingsRef NewB
= B
;
2596 for (const FieldDecl
*Field
: Fields
) {
2597 const FieldRegion
*SourceFR
= MRMgr
.getFieldRegion(Field
, LCV
.getRegion());
2598 SVal V
= getBindingForField(getRegionBindings(LCV
.getStore()), SourceFR
);
2600 const FieldRegion
*DestFR
= MRMgr
.getFieldRegion(Field
, R
);
2601 NewB
= bind(NewB
, loc::MemRegionVal(DestFR
), V
);
2607 RegionBindingsRef
RegionStoreManager::bindStruct(RegionBindingsConstRef B
,
2608 const TypedValueRegion
*R
,
2610 QualType T
= R
->getValueType();
2611 assert(T
->isStructureOrClassType());
2613 const RecordType
* RT
= T
->castAs
<RecordType
>();
2614 const RecordDecl
*RD
= RT
->getDecl();
2616 if (!RD
->isCompleteDefinition())
2619 // Handle lazy compound values and symbolic values.
2620 if (std::optional
<nonloc::LazyCompoundVal
> LCV
=
2621 V
.getAs
<nonloc::LazyCompoundVal
>()) {
2622 if (std::optional
<RegionBindingsRef
> NewB
=
2623 tryBindSmallStruct(B
, R
, RD
, *LCV
))
2625 return bindAggregate(B
, R
, V
);
2627 if (isa
<nonloc::SymbolVal
>(V
))
2628 return bindAggregate(B
, R
, V
);
2630 // We may get non-CompoundVal accidentally due to imprecise cast logic or
2631 // that we are binding symbolic struct value. Kill the field values, and if
2632 // the value is symbolic go and bind it as a "default" binding.
2633 if (V
.isUnknown() || !isa
<nonloc::CompoundVal
>(V
))
2634 return bindAggregate(B
, R
, UnknownVal());
2636 // The raw CompoundVal is essentially a symbolic InitListExpr: an (immutable)
2637 // list of other values. It appears pretty much only when there's an actual
2638 // initializer list expression in the program, and the analyzer tries to
2639 // unwrap it as soon as possible.
2640 // This code is where such unwrap happens: when the compound value is put into
2641 // the object that it was supposed to initialize (it's an *initializer* list,
2642 // after all), instead of binding the whole value to the whole object, we bind
2643 // sub-values to sub-objects. Sub-values may themselves be compound values,
2644 // and in this case the procedure becomes recursive.
2645 // FIXME: The annoying part about compound values is that they don't carry
2646 // any sort of information about which value corresponds to which sub-object.
2647 // It's simply a list of values in the middle of nowhere; we expect to match
2648 // them to sub-objects, essentially, "by index": first value binds to
2649 // the first field, second value binds to the second field, etc.
2650 // It would have been much safer to organize non-lazy compound values as
2651 // a mapping from fields/bases to values.
2652 const nonloc::CompoundVal
& CV
= V
.castAs
<nonloc::CompoundVal
>();
2653 nonloc::CompoundVal::iterator VI
= CV
.begin(), VE
= CV
.end();
2655 RegionBindingsRef
NewB(B
);
2657 // In C++17 aggregates may have base classes, handle those as well.
2658 // They appear before fields in the initializer list / compound value.
2659 if (const auto *CRD
= dyn_cast
<CXXRecordDecl
>(RD
)) {
2660 // If the object was constructed with a constructor, its value is a
2661 // LazyCompoundVal. If it's a raw CompoundVal, it means that we're
2662 // performing aggregate initialization. The only exception from this
2663 // rule is sending an Objective-C++ message that returns a C++ object
2664 // to a nil receiver; in this case the semantics is to return a
2665 // zero-initialized object even if it's a C++ object that doesn't have
2666 // this sort of constructor; the CompoundVal is empty in this case.
2667 assert((CRD
->isAggregate() || (Ctx
.getLangOpts().ObjC
&& VI
== VE
)) &&
2668 "Non-aggregates are constructed with a constructor!");
2670 for (const auto &B
: CRD
->bases()) {
2671 // (Multiple inheritance is fine though.)
2672 assert(!B
.isVirtual() && "Aggregates cannot have virtual base classes!");
2677 QualType BTy
= B
.getType();
2678 assert(BTy
->isStructureOrClassType() && "Base classes must be classes!");
2680 const CXXRecordDecl
*BRD
= BTy
->getAsCXXRecordDecl();
2681 assert(BRD
&& "Base classes must be C++ classes!");
2683 const CXXBaseObjectRegion
*BR
=
2684 MRMgr
.getCXXBaseObjectRegion(BRD
, R
, /*IsVirtual=*/false);
2686 NewB
= bindStruct(NewB
, BR
, *VI
);
2692 RecordDecl::field_iterator FI
, FE
;
2694 for (FI
= RD
->field_begin(), FE
= RD
->field_end(); FI
!= FE
; ++FI
) {
2699 // Skip any unnamed bitfields to stay in sync with the initializers.
2700 if (FI
->isUnnamedBitfield())
2703 QualType FTy
= FI
->getType();
2704 const FieldRegion
* FR
= MRMgr
.getFieldRegion(*FI
, R
);
2706 if (FTy
->isArrayType())
2707 NewB
= bindArray(NewB
, FR
, *VI
);
2708 else if (FTy
->isStructureOrClassType())
2709 NewB
= bindStruct(NewB
, FR
, *VI
);
2711 NewB
= bind(NewB
, loc::MemRegionVal(FR
), *VI
);
2715 // There may be fewer values in the initialize list than the fields of struct.
2717 NewB
= NewB
.addBinding(R
, BindingKey::Default
,
2718 svalBuilder
.makeIntVal(0, false));
2725 RegionStoreManager::bindAggregate(RegionBindingsConstRef B
,
2726 const TypedRegion
*R
,
2728 // Remove the old bindings, using 'R' as the root of all regions
2729 // we will invalidate. Then add the new binding.
2730 return removeSubRegionBindings(B
, R
).addBinding(R
, BindingKey::Default
, Val
);
2733 //===----------------------------------------------------------------------===//
2735 //===----------------------------------------------------------------------===//
2738 class RemoveDeadBindingsWorker
2739 : public ClusterAnalysis
<RemoveDeadBindingsWorker
> {
2740 SmallVector
<const SymbolicRegion
*, 12> Postponed
;
2741 SymbolReaper
&SymReaper
;
2742 const StackFrameContext
*CurrentLCtx
;
2745 RemoveDeadBindingsWorker(RegionStoreManager
&rm
,
2746 ProgramStateManager
&stateMgr
,
2747 RegionBindingsRef b
, SymbolReaper
&symReaper
,
2748 const StackFrameContext
*LCtx
)
2749 : ClusterAnalysis
<RemoveDeadBindingsWorker
>(rm
, stateMgr
, b
),
2750 SymReaper(symReaper
), CurrentLCtx(LCtx
) {}
2752 // Called by ClusterAnalysis.
2753 void VisitAddedToCluster(const MemRegion
*baseR
, const ClusterBindings
&C
);
2754 void VisitCluster(const MemRegion
*baseR
, const ClusterBindings
*C
);
2755 using ClusterAnalysis
<RemoveDeadBindingsWorker
>::VisitCluster
;
2757 using ClusterAnalysis::AddToWorkList
;
2759 bool AddToWorkList(const MemRegion
*R
);
2761 bool UpdatePostponed();
2762 void VisitBinding(SVal V
);
2766 bool RemoveDeadBindingsWorker::AddToWorkList(const MemRegion
*R
) {
2767 const MemRegion
*BaseR
= R
->getBaseRegion();
2768 return AddToWorkList(WorkListElement(BaseR
), getCluster(BaseR
));
2771 void RemoveDeadBindingsWorker::VisitAddedToCluster(const MemRegion
*baseR
,
2772 const ClusterBindings
&C
) {
2774 if (const VarRegion
*VR
= dyn_cast
<VarRegion
>(baseR
)) {
2775 if (SymReaper
.isLive(VR
))
2776 AddToWorkList(baseR
, &C
);
2781 if (const SymbolicRegion
*SR
= dyn_cast
<SymbolicRegion
>(baseR
)) {
2782 if (SymReaper
.isLive(SR
->getSymbol()))
2783 AddToWorkList(SR
, &C
);
2785 Postponed
.push_back(SR
);
2790 if (isa
<NonStaticGlobalSpaceRegion
>(baseR
)) {
2791 AddToWorkList(baseR
, &C
);
2795 // CXXThisRegion in the current or parent location context is live.
2796 if (const CXXThisRegion
*TR
= dyn_cast
<CXXThisRegion
>(baseR
)) {
2797 const auto *StackReg
=
2798 cast
<StackArgumentsSpaceRegion
>(TR
->getSuperRegion());
2799 const StackFrameContext
*RegCtx
= StackReg
->getStackFrame();
2801 (RegCtx
== CurrentLCtx
|| RegCtx
->isParentOf(CurrentLCtx
)))
2802 AddToWorkList(TR
, &C
);
2806 void RemoveDeadBindingsWorker::VisitCluster(const MemRegion
*baseR
,
2807 const ClusterBindings
*C
) {
2811 // Mark the symbol for any SymbolicRegion with live bindings as live itself.
2812 // This means we should continue to track that symbol.
2813 if (const SymbolicRegion
*SymR
= dyn_cast
<SymbolicRegion
>(baseR
))
2814 SymReaper
.markLive(SymR
->getSymbol());
2816 for (const auto &[Key
, Val
] : *C
) {
2817 // Element index of a binding key is live.
2818 SymReaper
.markElementIndicesLive(Key
.getRegion());
2824 void RemoveDeadBindingsWorker::VisitBinding(SVal V
) {
2825 // Is it a LazyCompoundVal? All referenced regions are live as well.
2826 // The LazyCompoundVal itself is not live but should be readable.
2827 if (auto LCS
= V
.getAs
<nonloc::LazyCompoundVal
>()) {
2828 SymReaper
.markLazilyCopied(LCS
->getRegion());
2830 for (SVal V
: RM
.getInterestingValues(*LCS
)) {
2831 if (auto DepLCS
= V
.getAs
<nonloc::LazyCompoundVal
>())
2832 SymReaper
.markLazilyCopied(DepLCS
->getRegion());
2840 // If V is a region, then add it to the worklist.
2841 if (const MemRegion
*R
= V
.getAsRegion()) {
2843 SymReaper
.markLive(R
);
2845 // All regions captured by a block are also live.
2846 if (const BlockDataRegion
*BR
= dyn_cast
<BlockDataRegion
>(R
)) {
2847 for (auto Var
: BR
->referenced_vars())
2848 AddToWorkList(Var
.getCapturedRegion());
2853 // Update the set of live symbols.
2854 for (SymbolRef Sym
: V
.symbols())
2855 SymReaper
.markLive(Sym
);
2858 bool RemoveDeadBindingsWorker::UpdatePostponed() {
2859 // See if any postponed SymbolicRegions are actually live now, after
2860 // having done a scan.
2861 bool Changed
= false;
2863 for (const SymbolicRegion
*SR
: Postponed
) {
2864 if (SymReaper
.isLive(SR
->getSymbol())) {
2865 Changed
|= AddToWorkList(SR
);
2873 StoreRef
RegionStoreManager::removeDeadBindings(Store store
,
2874 const StackFrameContext
*LCtx
,
2875 SymbolReaper
& SymReaper
) {
2876 RegionBindingsRef B
= getRegionBindings(store
);
2877 RemoveDeadBindingsWorker
W(*this, StateMgr
, B
, SymReaper
, LCtx
);
2878 W
.GenerateClusters();
2880 // Enqueue the region roots onto the worklist.
2881 for (const MemRegion
*Reg
: SymReaper
.regions()) {
2882 W
.AddToWorkList(Reg
);
2885 do W
.RunWorkList(); while (W
.UpdatePostponed());
2887 // We have now scanned the store, marking reachable regions and symbols
2888 // as live. We now remove all the regions that are dead from the store
2889 // as well as update DSymbols with the set symbols that are now dead.
2890 for (const MemRegion
*Base
: llvm::make_first_range(B
)) {
2891 // If the cluster has been visited, we know the region has been marked.
2892 // Otherwise, remove the dead entry.
2893 if (!W
.isVisited(Base
))
2897 return StoreRef(B
.asStore(), *this);
2900 //===----------------------------------------------------------------------===//
2902 //===----------------------------------------------------------------------===//
2904 void RegionStoreManager::printJson(raw_ostream
&Out
, Store S
, const char *NL
,
2905 unsigned int Space
, bool IsDot
) const {
2906 RegionBindingsRef Bindings
= getRegionBindings(S
);
2908 Indent(Out
, Space
, IsDot
) << "\"store\": ";
2910 if (Bindings
.isEmpty()) {
2911 Out
<< "null," << NL
;
2915 Out
<< "{ \"pointer\": \"" << Bindings
.asStore() << "\", \"items\": [" << NL
;
2916 Bindings
.printJson(Out
, NL
, Space
+ 1, IsDot
);
2917 Indent(Out
, Space
, IsDot
) << "]}," << NL
;