clang 23.0.0git
SValBuilder.h
Go to the documentation of this file.
1// SValBuilder.h - Construction of SVals from evaluating expressions -*- 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 defines SValBuilder, a class that defines the interface for
10// "symbolical evaluators" which construct an SVal from an expression.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SVALBUILDER_H
15#define LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SVALBUILDER_H
16
19#include "clang/AST/Expr.h"
20#include "clang/AST/ExprObjC.h"
21#include "clang/AST/Type.h"
22#include "clang/Analysis/CFG.h"
23#include "clang/Basic/LLVM.h"
31#include "llvm/ADT/ImmutableList.h"
32#include <cstdint>
33#include <optional>
34
35namespace clang {
36
37class AnalyzerOptions;
38class BlockDecl;
40class CXXMethodDecl;
41class CXXRecordDecl;
42class DeclaratorDecl;
43class FunctionDecl;
44class StackFrame;
45class Stmt;
46
47namespace ento {
48
49class CallEvent;
52class StoreRef;
54 virtual void anchor();
55
56protected:
58
59 /// Manager of APSInt values.
61
62 /// Manages the creation of symbols.
64
65 /// Manages the creation of memory regions.
67
69
71
72 /// The scalar type to use for array indices.
74
75 /// The width of the scalar type used for array indices.
76 const unsigned ArrayIndexWidth;
77
78public:
79 SValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context,
80 ProgramStateManager &stateMgr);
81
82 virtual ~SValBuilder() = default;
83
84 SVal evalCast(SVal V, QualType CastTy, QualType OriginalTy);
85
86 // Handles casts of type CK_IntegralCast.
88 QualType originalType);
89
92
93 /// Create a new value which represents a binary expression with two non-
94 /// location operands.
96 NonLoc lhs, NonLoc rhs, QualType resultTy) = 0;
97
98 /// Create a new value which represents a binary expression with two memory
99 /// location operands.
101 Loc lhs, Loc rhs, QualType resultTy) = 0;
102
103 /// Create a new value which represents a binary expression with a memory
104 /// location and non-location operands. For example, this would be used to
105 /// evaluate a pointer arithmetic operation.
107 Loc lhs, NonLoc rhs, QualType resultTy) = 0;
108
109 /// Evaluates a given SVal. If the SVal has only one possible (integer) value,
110 /// that value is returned. Otherwise, returns NULL.
111 virtual const llvm::APSInt *getKnownValue(ProgramStateRef state, SVal val) = 0;
112
113 /// Tries to get the minimal possible (integer) value of a given SVal. This
114 /// always returns the value of a ConcreteInt, but may return NULL if the
115 /// value is symbolic and the constraint manager cannot provide a useful
116 /// answer.
117 virtual const llvm::APSInt *getMinValue(ProgramStateRef state, SVal val) = 0;
118
119 /// Tries to get the maximal possible (integer) value of a given SVal. This
120 /// always returns the value of a ConcreteInt, but may return NULL if the
121 /// value is symbolic and the constraint manager cannot provide a useful
122 /// answer.
123 virtual const llvm::APSInt *getMaxValue(ProgramStateRef state, SVal val) = 0;
124
125 /// Simplify symbolic expressions within a given SVal. Return an SVal
126 /// that represents the same value, but is hopefully easier to work with
127 /// than the original SVal.
128 virtual SVal simplifySVal(ProgramStateRef State, SVal Val) = 0;
129
130 /// Constructs a symbolic expression for two non-location values.
132 NonLoc lhs, NonLoc rhs, QualType resultTy);
133
135 SVal operand, QualType type);
136
138 SVal lhs, SVal rhs, QualType type);
139
140 /// \return Whether values in \p lhs and \p rhs are equal at \p state.
142
143 SVal evalEQ(ProgramStateRef state, SVal lhs, SVal rhs);
144
147
149 const ASTContext &getContext() const { return Context; }
150
152
154 return Context.getLangOpts().CPlusPlus ? Context.BoolTy : Context.IntTy;
155 }
156
158 return ArrayIndexTy;
159 }
160
163
165 const SymbolManager &getSymbolManager() const { return SymMgr; }
166
168 const MemRegionManager &getRegionManager() const { return MemMgr; }
169
170 const AnalyzerOptions &getAnalyzerOptions() const { return AnOpts; }
171
172 // Forwarding methods to SymbolManager.
173
175 const StackFrame *SF, QualType type,
176 unsigned visitCount,
177 const void *symbolTag = nullptr) {
178 return SymMgr.conjureSymbol(Elem, SF, type, visitCount, symbolTag);
179 }
180
181 /// Construct an SVal representing '0' for the specified type.
183
184 /// Make a unique symbol for value of region.
186
187 /// Create a new symbol with a unique 'name'.
188 ///
189 /// We resort to conjured symbols when we cannot construct a derived symbol.
190 /// The advantage of symbols derived/built from other symbols is that we
191 /// preserve the relation between related(or even equivalent) expressions, so
192 /// conjured symbols should be used sparingly.
193 DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag,
195 const StackFrame *SF, unsigned count);
196 DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag,
198 const StackFrame *SF, QualType type,
199 unsigned count);
201 const StackFrame *SF, QualType type,
202 unsigned visitCount);
204 unsigned visitCount,
205 const void *symbolTag = nullptr);
207 unsigned visitCount,
208 const void *symbolTag = nullptr);
209
210 /// Conjure a symbol representing heap allocated memory region.
212 const StackFrame *SF, QualType type,
213 unsigned Count);
214
215 /// Create an SVal representing the result of an alloca()-like call, that is,
216 /// an AllocaRegion on the stack.
217 ///
218 /// After calling this function, it's a good idea to set the extent of the
219 /// returned AllocaRegion.
221 unsigned Count);
222
224 SymbolRef parentSymbol, const TypedValueRegion *region);
225
226 DefinedSVal getMetadataSymbolVal(const void *symbolTag,
227 const MemRegion *region, const Expr *expr,
228 QualType type, const StackFrame *SF,
229 unsigned count);
230
232
234
236 const StackFrame *SF, unsigned blockCount);
237
238 /// Returns the value of \p E, if it can be determined in a non-path-sensitive
239 /// manner.
240 ///
241 /// If \p E is not a constant or cannot be modeled, returns \c std::nullopt.
242 std::optional<SVal> getConstantVal(const Expr *E);
243
244 NonLoc makeCompoundVal(QualType type, llvm::ImmutableList<SVal> vals) {
245 return nonloc::CompoundVal(BasicVals.getCompoundValData(type, vals));
246 }
247
249 const TypedValueRegion *region) {
251 BasicVals.getLazyCompoundValData(store, region));
252 }
253
257
261
265
266 NonLoc makeArrayIndex(uint64_t idx) {
267 return nonloc::ConcreteInt(BasicVals.getValue(idx, ArrayIndexTy));
268 }
269
271
273 return nonloc::ConcreteInt(
274 BasicVals.getValue(integer->getValue(),
276 }
277
279 return makeTruthVal(boolean->getValue(), boolean->getType());
280 }
281
283
284 nonloc::ConcreteInt makeIntVal(const llvm::APSInt& integer) {
285 return nonloc::ConcreteInt(BasicVals.getValue(integer));
286 }
287
288 loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer) {
289 return loc::ConcreteInt(BasicVals.getValue(integer));
290 }
291
292 NonLoc makeIntVal(const llvm::APInt& integer, bool isUnsigned) {
293 return nonloc::ConcreteInt(BasicVals.getValue(integer, isUnsigned));
294 }
295
296 DefinedSVal makeIntVal(uint64_t integer, QualType type) {
297 if (Loc::isLocType(type))
298 return loc::ConcreteInt(BasicVals.getValue(integer, type));
299
300 return nonloc::ConcreteInt(BasicVals.getValue(integer, type));
301 }
302
303 NonLoc makeIntVal(uint64_t integer, bool isUnsigned) {
304 return nonloc::ConcreteInt(BasicVals.getIntValue(integer, isUnsigned));
305 }
306
307 NonLoc makeIntValWithWidth(QualType ptrType, uint64_t integer) {
308 return nonloc::ConcreteInt(BasicVals.getValue(integer, ptrType));
309 }
310
311 NonLoc makeLocAsInteger(Loc loc, unsigned bits) {
312 return nonloc::LocAsInteger(BasicVals.getPersistentSValWithData(loc, bits));
313 }
314
316 APSIntPtr rhs, QualType type);
317
319 const SymExpr *lhs, QualType type);
320
322 const SymExpr *rhs, QualType type);
323
325 QualType type);
326
327 /// Create a NonLoc value for cast.
328 nonloc::SymbolVal makeNonLoc(const SymExpr *operand, QualType fromTy,
329 QualType toTy);
330
332 return nonloc::ConcreteInt(BasicVals.getTruthValue(b, type));
333 }
334
336 return nonloc::ConcreteInt(BasicVals.getTruthValue(b));
337 }
338
339 /// Create NULL pointer, with proper pointer bit-width for given address
340 /// space.
341 /// \param type pointer type.
343 type =
344 type->isAtomicType() ? type->getAs<AtomicType>()->getValueType() : type;
345
346 // We cannot use the `isAnyPointerType()`.
347 assert((type->isObjCObjectPointerType() || Loc::isLocType(type)) &&
348 "makeNullWithType must use pointer type");
349
350 // The `sizeof(T&)` is `sizeof(T)`, thus we replace the reference with a
351 // pointer. Here we assume that references are actually implemented by
352 // pointers under-the-hood.
353 type = type->isReferenceType()
354 ? Context.getPointerType(type->getPointeeType())
355 : type;
356 return loc::ConcreteInt(BasicVals.getZeroWithTypeSize(type));
357 }
358
360 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym));
361 }
362
364 return loc::MemRegionVal(region);
365 }
366
368 return loc::GotoLabel(expr->getLabel());
369 }
370
371 loc::ConcreteInt makeLoc(const llvm::APSInt &integer) {
372 return loc::ConcreteInt(BasicVals.getValue(integer));
373 }
374
375 /// Return MemRegionVal on success cast, otherwise return std::nullopt.
376 std::optional<loc::MemRegionVal>
378
379 /// Make an SVal that represents the given symbol. This follows the convention
380 /// of representing Loc-type symbols (symbolic pointers and references)
381 /// as Loc values wrapping the symbol rather than as plain symbol values.
383 if (Loc::isLocType(Sym->getType()))
384 return makeLoc(Sym);
385 return nonloc::SymbolVal(Sym);
386 }
387
388 /// Return a memory region for the 'this' object reference.
390
391 /// Return a memory region for the 'this' object reference.
393};
394
395SValBuilder* createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc,
396 ASTContext &context,
397 ProgramStateManager &stateMgr);
398
399} // namespace ento
400
401} // namespace clang
402
403#endif // LLVM_CLANG_STATICANALYZER_CORE_PATHSENSITIVE_SVALBUILDER_H
Defines the clang::ASTContext interface.
#define V(N, I)
static bool isUnsigned(SValBuilder &SVB, NonLoc Value)
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the clang::LangOptions interface.
C Language Family Type Representation.
llvm::APInt getValue() const
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:223
AddrLabelExpr - The GNU address of label extension, representing &&label.
Definition Expr.h:4553
Stores options for the analyzer from the command line.
BinaryOperatorKind Opcode
Definition Expr.h:4046
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition Decl.h:4694
A boolean literal, per ([C++ lex.bool] Boolean literals).
Definition ExprCXX.h:727
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2145
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
Represents a ValueDecl that came out of a declarator.
Definition Decl.h:780
This represents one expression.
Definition Expr.h:112
QualType getType() const
Definition Expr.h:144
Represents a function declaration or definition.
Definition Decl.h:2018
This represents a decl that may have a name.
Definition Decl.h:274
ObjCBoolLiteralExpr - Objective-C Boolean Literal.
Definition ExprObjC.h:119
A (possibly-)qualified type.
Definition TypeBase.h:937
It represents a stack frame of the call stack.
Stmt - This represents one statement.
Definition Stmt.h:86
bool isUnsignedIntegerOrEnumerationType() const
Determines whether this is an integer type that is unsigned or an enumeration types whose underlying ...
Definition Type.cpp:2356
UnaryOperatorKind Opcode
Definition Expr.h:2261
A safe wrapper around APSInt objects allocated and owned by BasicValueFactory.
Definition APSIntPtr.h:19
Represents an abstract call to a function or method along a particular path.
Definition CallEvent.h:152
static bool isLocType(QualType T)
Definition SVals.h:262
MemRegion - The root abstract class for all memory regions.
Definition MemRegion.h:97
DefinedOrUnknownSVal makeZeroVal(QualType type)
Construct an SVal representing '0' for the specified type.
DefinedSVal getMemberPointer(const NamedDecl *ND)
NonLoc makePointerToMember(const DeclaratorDecl *DD)
SVal evalMinus(NonLoc val)
SVal evalComplement(NonLoc val)
virtual const llvm::APSInt * getKnownValue(ProgramStateRef state, SVal val)=0
Evaluates a given SVal.
BasicValueFactory & getBasicValueFactory()
loc::ConcreteInt makeLoc(const llvm::APSInt &integer)
const SymbolConjured * conjureSymbol(ConstCFGElementRef Elem, const StackFrame *SF, QualType type, unsigned visitCount, const void *symbolTag=nullptr)
NonLoc makeCompoundVal(QualType type, llvm::ImmutableList< SVal > vals)
SymbolManager SymMgr
Manages the creation of symbols.
Definition SValBuilder.h:63
virtual SVal evalBinOpLN(ProgramStateRef state, BinaryOperator::Opcode op, Loc lhs, NonLoc rhs, QualType resultTy)=0
Create a new value which represents a binary expression with a memory location and non-location opera...
MemRegionManager & getRegionManager()
ProgramStateManager & getStateManager()
SVal makeSymExprValNN(BinaryOperator::Opcode op, NonLoc lhs, NonLoc rhs, QualType resultTy)
Constructs a symbolic expression for two non-location values.
virtual SVal evalBinOpLL(ProgramStateRef state, BinaryOperator::Opcode op, Loc lhs, Loc rhs, QualType resultTy)=0
Create a new value which represents a binary expression with two memory location operands.
const unsigned ArrayIndexWidth
The width of the scalar type used for array indices.
Definition SValBuilder.h:76
DefinedSVal getFunctionPointer(const FunctionDecl *func)
const QualType ArrayIndexTy
The scalar type to use for array indices.
Definition SValBuilder.h:73
NonLoc makePointerToMember(const PointerToMemberData *PTMD)
NonLoc makeIntValWithWidth(QualType ptrType, uint64_t integer)
loc::GotoLabel makeLoc(const AddrLabelExpr *expr)
NonLoc makeArrayIndex(uint64_t idx)
ASTContext & getContext()
nonloc::ConcreteInt makeIntVal(const IntegerLiteral *integer)
SVal convertToArrayIndex(SVal val)
QualType getArrayIndexType() const
loc::MemRegionVal makeLoc(SymbolRef sym)
virtual const llvm::APSInt * getMinValue(ProgramStateRef state, SVal val)=0
Tries to get the minimal possible (integer) value of a given SVal.
DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy, const StackFrame *SF, unsigned blockCount)
const BasicValueFactory & getBasicValueFactory() const
virtual SVal evalBinOpNN(ProgramStateRef state, BinaryOperator::Opcode op, NonLoc lhs, NonLoc rhs, QualType resultTy)=0
Create a new value which represents a binary expression with two non- location operands.
DefinedSVal makeSymbolVal(SymbolRef Sym)
Make an SVal that represents the given symbol.
SVal evalCast(SVal V, QualType CastTy, QualType OriginalTy)
Cast a given SVal to another SVal using given QualType's.
NonLoc makeIntVal(uint64_t integer, bool isUnsigned)
const AnalyzerOptions & getAnalyzerOptions() const
nonloc::ConcreteInt makeTruthVal(bool b)
DefinedSVal makeIntVal(uint64_t integer, QualType type)
BasicValueFactory BasicVals
Manager of APSInt values.
Definition SValBuilder.h:60
ConditionTruthVal areEqual(ProgramStateRef state, SVal lhs, SVal rhs)
virtual SVal simplifySVal(ProgramStateRef State, SVal Val)=0
Simplify symbolic expressions within a given SVal.
QualType getConditionType() const
MemRegionManager MemMgr
Manages the creation of memory regions.
Definition SValBuilder.h:66
SVal evalEQ(ProgramStateRef state, SVal lhs, SVal rhs)
SVal evalUnaryOp(ProgramStateRef state, UnaryOperator::Opcode opc, SVal operand, QualType type)
DefinedOrUnknownSVal getDerivedRegionValueSymbolVal(SymbolRef parentSymbol, const TypedValueRegion *region)
nonloc::ConcreteInt makeTruthVal(bool b, QualType type)
const MemRegionManager & getRegionManager() const
virtual ~SValBuilder()=default
loc::ConcreteInt makeNullWithType(QualType type)
Create NULL pointer, with proper pointer bit-width for given address space.
loc::MemRegionVal getAllocaRegionVal(const Expr *E, const StackFrame *SF, unsigned Count)
Create an SVal representing the result of an alloca()-like call, that is, an AllocaRegion on the stac...
virtual const llvm::APSInt * getMaxValue(ProgramStateRef state, SVal val)=0
Tries to get the maximal possible (integer) value of a given SVal.
NonLoc makeIntVal(const llvm::APInt &integer, bool isUnsigned)
ProgramStateManager & StateMgr
Definition SValBuilder.h:68
DefinedSVal getConjuredHeapSymbolVal(ConstCFGElementRef elem, const StackFrame *SF, QualType type, unsigned Count)
Conjure a symbol representing heap allocated memory region.
loc::MemRegionVal makeLoc(const MemRegion *region)
std::optional< SVal > getConstantVal(const Expr *E)
Returns the value of E, if it can be determined in a non-path-sensitive manner.
NonLoc makeLocAsInteger(Loc loc, unsigned bits)
const ASTContext & getContext() const
SVal evalIntegralCast(ProgramStateRef state, SVal val, QualType castTy, QualType originalType)
SymbolManager & getSymbolManager()
DefinedOrUnknownSVal getRegionValueSymbolVal(const TypedValueRegion *region)
Make a unique symbol for value of region.
loc::MemRegionVal getCXXThis(const CXXMethodDecl *D, const StackFrame *SF)
Return a memory region for the 'this' object reference.
SVal evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op, SVal lhs, SVal rhs, QualType type)
loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer)
DefinedSVal getMetadataSymbolVal(const void *symbolTag, const MemRegion *region, const Expr *expr, QualType type, const StackFrame *SF, unsigned count)
NonLoc makeLazyCompoundVal(const StoreRef &store, const TypedValueRegion *region)
DefinedOrUnknownSVal conjureSymbolVal(const void *symbolTag, ConstCFGElementRef elem, const StackFrame *SF, unsigned count)
Create a new symbol with a unique 'name'.
nonloc::SymbolVal makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op, APSIntPtr rhs, QualType type)
const AnalyzerOptions & AnOpts
Definition SValBuilder.h:70
const SymbolManager & getSymbolManager() const
std::optional< loc::MemRegionVal > getCastedMemRegionVal(const MemRegion *region, QualType type)
Return MemRegionVal on success cast, otherwise return std::nullopt.
nonloc::ConcreteInt makeBoolVal(const ObjCBoolLiteralExpr *boolean)
nonloc::ConcreteInt makeIntVal(const llvm::APSInt &integer)
SValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context, ProgramStateManager &stateMgr)
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition SVals.h:56
Symbolic value.
Definition SymExpr.h:32
virtual QualType getType() const =0
A symbol representing the result of an expression in the case when we do not know anything about what...
TypedValueRegion - An abstract class representing regions having a typed value.
Definition MemRegion.h:562
The simplest example of a concrete compound value is nonloc::CompoundVal, which represents a concrete...
Definition SVals.h:339
Value representing integer constant.
Definition SVals.h:300
While nonloc::CompoundVal covers a few simple use cases, nonloc::LazyCompoundVal is a more performant...
Definition SVals.h:389
Value representing pointer-to-member.
Definition SVals.h:434
Represents symbolic expression that isn't a location.
Definition SVals.h:279
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
const internal::VariadicDynCastAllOfMatcher< Stmt, Expr > expr
Matches expressions.
SValBuilder * createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context, ProgramStateManager &stateMgr)
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
const SymExpr * SymbolRef
Definition SymExpr.h:133
The JSON file list parser is used to communicate input to InstallAPI.
CanQual< Type > CanQualType
Represents a canonical, potentially-qualified type.
CFGBlock::ConstCFGElementRef ConstCFGElementRef
Definition CFG.h:1248