1 //===- Store.h - Interface for maps from Locations to Values ----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defined the types Store and StoreManager.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_STORE_H
14 #define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_STORE_H
15
16 #include "clang/AST/Type.h"
17 #include "clang/Basic/LLVM.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
22 #include "clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h"
23 #include "clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h"
24 #include "llvm/ADT/ArrayRef.h"
25 #include "llvm/ADT/DenseSet.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include <cassert>
28 #include <cstdint>
29 #include <memory>
30 #include <optional>
31
32 namespace clang {
33
34 class ASTContext;
35 class CastExpr;
36 class CompoundLiteralExpr;
37 class CXXBasePath;
38 class Decl;
39 class Expr;
40 class LocationContext;
41 class ObjCIvarDecl;
42 class StackFrameContext;
43
44 namespace ento {
45
46 class CallEvent;
47 class ProgramStateManager;
48 class ScanReachableSymbols;
49 class SymbolReaper;
50
51 using InvalidatedSymbols = llvm::DenseSet<SymbolRef>;
52
53 struct BindResult {
54 StoreRef ResultingStore;
55
56 // If during the bind operation we exhaust the allowed binding budget, we set
57 // this to the beginning of the escaped part of the region.
58 llvm::SmallVector<SVal, 0> FailedToBindValues;
59 };
60
61 class StoreManager {
62 protected:
63 SValBuilder &svalBuilder;
64 ProgramStateManager &StateMgr;
65
66 /// MRMgr - Manages region objects associated with this StoreManager.
67 MemRegionManager &MRMgr;
68 ASTContext &Ctx;
69
70 StoreManager(ProgramStateManager &stateMgr);
71
72 public:
73 virtual ~StoreManager() = default;
74
75 /// Return the value bound to specified location in a given state.
76 /// \param[in] store The store in which to make the lookup.
77 /// \param[in] loc The symbolic memory location.
78 /// \param[in] T An optional type that provides a hint indicating the
79 /// expected type of the returned value. This is used if the value is
80 /// lazily computed.
81 /// \return The value bound to the location \c loc.
82 virtual SVal getBinding(Store store, Loc loc, QualType T = QualType()) = 0;
83
84 /// Return the default value bound to a region in a given store. The default
85 /// binding is the value of sub-regions that were not initialized separately
86 /// from their base region. For example, if the structure is zero-initialized
87 /// upon construction, this method retrieves the concrete zero value, even if
88 /// some or all fields were later overwritten manually. Default binding may be
89 /// an unknown, undefined, concrete, or symbolic value.
90 /// \param[in] store The store in which to make the lookup.
91 /// \param[in] R The region to find the default binding for.
92 /// \return The default value bound to the region in the store, if a default
93 /// binding exists.
94 virtual std::optional<SVal> getDefaultBinding(Store store,
95 const MemRegion *R) = 0;
96
97 /// Return the default value bound to a LazyCompoundVal. The default binding
98 /// is used to represent the value of any fields or elements within the
99 /// structure represented by the LazyCompoundVal which were not initialized
100 /// explicitly separately from the whole structure. Default binding may be an
101 /// unknown, undefined, concrete, or symbolic value.
102 /// \param[in] lcv The lazy compound value.
103 /// \return The default value bound to the LazyCompoundVal \c lcv, if a
104 /// default binding exists.
getDefaultBinding(nonloc::LazyCompoundVal lcv)105 std::optional<SVal> getDefaultBinding(nonloc::LazyCompoundVal lcv) {
106 return getDefaultBinding(lcv.getStore(), lcv.getRegion());
107 }
108
109 /// Return a store with the specified value bound to the given location.
110 /// \param[in] store The store in which to make the binding.
111 /// \param[in] loc The symbolic memory location.
112 /// \param[in] val The value to bind to location \c loc.
113 /// \return A StoreRef object that contains the same
114 /// bindings as \c store with the addition of having the value specified
115 /// by \c val bound to the location given for \c loc.
116 virtual BindResult Bind(Store store, Loc loc, SVal val) = 0;
117
118 /// Return a store with the specified value bound to all sub-regions of the
119 /// region. The region must not have previous bindings. If you need to
120 /// invalidate existing bindings, consider invalidateRegions().
121 virtual BindResult BindDefaultInitial(Store store, const MemRegion *R,
122 SVal V) = 0;
123
124 /// Return a store with in which all values within the given region are
125 /// reset to zero. This method is allowed to overwrite previous bindings.
126 virtual BindResult BindDefaultZero(Store store, const MemRegion *R) = 0;
127
128 /// Create a new store with the specified binding removed.
129 /// \param ST the original store, that is the basis for the new store.
130 /// \param L the location whose binding should be removed.
131 virtual StoreRef killBinding(Store ST, Loc L) = 0;
132
133 /// getInitialStore - Returns the initial "empty" store representing the
134 /// value bindings upon entry to an analyzed function.
135 virtual StoreRef getInitialStore(const LocationContext *InitLoc) = 0;
136
137 /// getRegionManager - Returns the internal RegionManager object that is
138 /// used to query and manipulate MemRegion objects.
getRegionManager()139 MemRegionManager& getRegionManager() { return MRMgr; }
140
getSValBuilder()141 SValBuilder& getSValBuilder() { return svalBuilder; }
142
getLValueVar(const VarDecl * VD,const LocationContext * LC)143 virtual Loc getLValueVar(const VarDecl *VD, const LocationContext *LC) {
144 return svalBuilder.makeLoc(MRMgr.getVarRegion(VD, LC));
145 }
146
getLValueCompoundLiteral(const CompoundLiteralExpr * CL,const LocationContext * LC)147 Loc getLValueCompoundLiteral(const CompoundLiteralExpr *CL,
148 const LocationContext *LC) {
149 return loc::MemRegionVal(MRMgr.getCompoundLiteralRegion(CL, LC));
150 }
151
152 virtual SVal getLValueIvar(const ObjCIvarDecl *decl, SVal base);
153
getLValueField(const FieldDecl * D,SVal Base)154 virtual SVal getLValueField(const FieldDecl *D, SVal Base) {
155 return getLValueFieldOrIvar(D, Base);
156 }
157
158 virtual SVal getLValueElement(QualType elementType, NonLoc offset, SVal Base);
159
160 /// ArrayToPointer - Used by ExprEngine::VistCast to handle implicit
161 /// conversions between arrays and pointers.
162 virtual SVal ArrayToPointer(Loc Array, QualType ElementTy) = 0;
163
164 /// Evaluates a chain of derived-to-base casts through the path specified in
165 /// \p Cast.
166 SVal evalDerivedToBase(SVal Derived, const CastExpr *Cast);
167
168 /// Evaluates a chain of derived-to-base casts through the specified path.
169 SVal evalDerivedToBase(SVal Derived, const CXXBasePath &CastPath);
170
171 /// Evaluates a derived-to-base cast through a single level of derivation.
172 SVal evalDerivedToBase(SVal Derived, QualType DerivedPtrType,
173 bool IsVirtual);
174
175 /// Attempts to do a down cast. Used to model BaseToDerived and C++
176 /// dynamic_cast.
177 /// The callback may result in the following 3 scenarios:
178 /// - Successful cast (ex: derived is subclass of base).
179 /// - Failed cast (ex: derived is definitely not a subclass of base).
180 /// The distinction of this case from the next one is necessary to model
181 /// dynamic_cast.
182 /// - We don't know (base is a symbolic region and we don't have
183 /// enough info to determine if the cast will succeed at run time).
184 /// The function returns an optional with SVal representing the derived class
185 /// in case of a successful cast and `std::nullopt` otherwise.
186 std::optional<SVal> evalBaseToDerived(SVal Base, QualType DerivedPtrType);
187
188 const ElementRegion *GetElementZeroRegion(const SubRegion *R, QualType T);
189
190 /// castRegion - Used by ExprEngine::VisitCast to handle casts from
191 /// a MemRegion* to a specific location type. 'R' is the region being
192 /// casted and 'CastToTy' the result type of the cast.
193 std::optional<const MemRegion *> castRegion(const MemRegion *region,
194 QualType CastToTy);
195
196 virtual StoreRef removeDeadBindings(Store store, const StackFrameContext *LCtx,
197 SymbolReaper &SymReaper) = 0;
198
199 virtual bool includedInBindings(Store store,
200 const MemRegion *region) const = 0;
201
202 /// If the StoreManager supports it, increment the reference count of
203 /// the specified Store object.
incrementReferenceCount(Store store)204 virtual void incrementReferenceCount(Store store) {}
205
206 /// If the StoreManager supports it, decrement the reference count of
207 /// the specified Store object. If the reference count hits 0, the memory
208 /// associated with the object is recycled.
decrementReferenceCount(Store store)209 virtual void decrementReferenceCount(Store store) {}
210
211 using InvalidatedRegions = SmallVector<const MemRegion *, 8>;
212
213 /// invalidateRegions - Clears out the specified regions from the store,
214 /// marking their values as unknown. Depending on the store, this may also
215 /// invalidate additional regions that may have changed based on accessing
216 /// the given regions. If \p Call is non-null, then this also invalidates
217 /// non-static globals (but if \p Call is from a system header, then this is
218 /// limited to globals declared in system headers).
219 ///
220 /// Instead of calling this method directly, you should probably use
221 /// \c ProgramState::invalidateRegions, which calls this and then ensures that
222 /// the relevant checker callbacks are triggered.
223 ///
224 /// \param[in] store The initial store.
225 /// \param[in] Values The values to invalidate.
226 /// \param[in] Elem The current CFG Element being evaluated. Used to conjure
227 /// symbols to mark the values of invalidated regions.
228 /// \param[in] Count The current block count. Used to conjure
229 /// symbols to mark the values of invalidated regions.
230 /// \param[in] Call The call expression which will be used to determine which
231 /// globals should get invalidated.
232 /// \param[in,out] IS A set to fill with any symbols that are no longer
233 /// accessible. Pass \c NULL if this information will not be used.
234 /// \param[in] ITraits Information about invalidation for a particular
235 /// region/symbol.
236 /// \param[in,out] InvalidatedTopLevel A vector to fill with regions
237 //// explicitly being invalidated. Pass \c NULL if this
238 /// information will not be used.
239 /// \param[in,out] Invalidated A vector to fill with any regions being
240 /// invalidated. This should include any regions explicitly invalidated
241 /// even if they do not currently have bindings. Pass \c NULL if this
242 /// information will not be used.
243 virtual StoreRef invalidateRegions(
244 Store store, ArrayRef<SVal> Values, ConstCFGElementRef Elem,
245 unsigned Count, const LocationContext *LCtx, const CallEvent *Call,
246 InvalidatedSymbols &IS, RegionAndSymbolInvalidationTraits &ITraits,
247 InvalidatedRegions *TopLevelRegions, InvalidatedRegions *Invalidated) = 0;
248
249 /// enterStackFrame - Let the StoreManager to do something when execution
250 /// engine is about to execute into a callee.
251 BindResult enterStackFrame(Store store, const CallEvent &Call,
252 const StackFrameContext *CalleeCtx);
253
254 /// Finds the transitive closure of symbols within the given region.
255 ///
256 /// Returns false if the visitor aborted the scan.
257 virtual bool scanReachableSymbols(Store S, const MemRegion *R,
258 ScanReachableSymbols &Visitor) = 0;
259
260 virtual void printJson(raw_ostream &Out, Store S, const char *NL,
261 unsigned int Space, bool IsDot) const = 0;
262
263 class BindingsHandler {
264 public:
265 virtual ~BindingsHandler();
266
267 /// \return whether the iteration should continue.
268 virtual bool HandleBinding(StoreManager& SMgr, Store store,
269 const MemRegion *region, SVal val) = 0;
270 };
271
272 class FindUniqueBinding : public BindingsHandler {
273 SymbolRef Sym;
274 const MemRegion* Binding = nullptr;
275 bool First = true;
276
277 public:
FindUniqueBinding(SymbolRef sym)278 FindUniqueBinding(SymbolRef sym) : Sym(sym) {}
279
280 explicit operator bool() { return First && Binding; }
281
282 bool HandleBinding(StoreManager& SMgr, Store store, const MemRegion* R,
283 SVal val) override;
getRegion()284 const MemRegion *getRegion() { return Binding; }
285 };
286
287 /// iterBindings - Iterate over the bindings in the Store.
288 virtual void iterBindings(Store store, BindingsHandler& f) = 0;
289
290 protected:
291 const ElementRegion *MakeElementRegion(const SubRegion *baseRegion,
292 QualType pointeeTy,
293 uint64_t index = 0);
294
295 private:
296 SVal getLValueFieldOrIvar(const Decl *decl, SVal base);
297 };
298
StoreRef(Store store,StoreManager & smgr)299 inline StoreRef::StoreRef(Store store, StoreManager & smgr)
300 : store(store), mgr(smgr) {
301 if (store)
302 mgr.incrementReferenceCount(store);
303 }
304
StoreRef(const StoreRef & sr)305 inline StoreRef::StoreRef(const StoreRef &sr)
306 : store(sr.store), mgr(sr.mgr)
307 {
308 if (store)
309 mgr.incrementReferenceCount(store);
310 }
311
~StoreRef()312 inline StoreRef::~StoreRef() {
313 if (store)
314 mgr.decrementReferenceCount(store);
315 }
316
317 inline StoreRef &StoreRef::operator=(StoreRef const &newStore) {
318 assert(&newStore.mgr == &mgr);
319 if (store != newStore.store) {
320 mgr.incrementReferenceCount(newStore.store);
321 mgr.decrementReferenceCount(store);
322 store = newStore.getStore();
323 }
324 return *this;
325 }
326
327 // FIXME: Do we need to pass ProgramStateManager anymore?
328 std::unique_ptr<StoreManager>
329 CreateRegionStoreManager(ProgramStateManager &StMgr);
330
331 } // namespace ento
332
333 } // namespace clang
334
335 #endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_STORE_H
336