SValBuilder.h revision 6d6a83c3754b449ac24cb83bc6d3a50b10535061
1// SValBuilder.h - Construction of SVals from evaluating expressions -*- C++ -*- 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines SValBuilder, a class that defines the interface for 11// "symbolical evaluators" which construct an SVal from an expression. 12// 13//===----------------------------------------------------------------------===// 14 15#ifndef LLVM_CLANG_GR_SVALBUILDER 16#define LLVM_CLANG_GR_SVALBUILDER 17 18#include "clang/AST/Expr.h" 19#include "clang/AST/ExprCXX.h" 20#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" 21#include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h" 22#include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h" 23 24namespace clang { 25 26namespace ento { 27 28class ProgramState; 29 30class SValBuilder { 31protected: 32 ASTContext &Context; 33 34 /// Manager of APSInt values. 35 BasicValueFactory BasicVals; 36 37 /// Manages the creation of symbols. 38 SymbolManager SymMgr; 39 40 /// Manages the creation of memory regions. 41 MemRegionManager MemMgr; 42 43 ProgramStateManager &StateMgr; 44 45 /// The scalar type to use for array indices. 46 const QualType ArrayIndexTy; 47 48 /// The width of the scalar type used for array indices. 49 const unsigned ArrayIndexWidth; 50 51 virtual SVal evalCastFromNonLoc(NonLoc val, QualType castTy) = 0; 52 virtual SVal evalCastFromLoc(Loc val, QualType castTy) = 0; 53 54public: 55 // FIXME: Make these protected again once RegionStoreManager correctly 56 // handles loads from different bound value types. 57 virtual SVal dispatchCast(SVal val, QualType castTy) = 0; 58 59public: 60 SValBuilder(llvm::BumpPtrAllocator &alloc, ASTContext &context, 61 ProgramStateManager &stateMgr) 62 : Context(context), BasicVals(context, alloc), 63 SymMgr(context, BasicVals, alloc), 64 MemMgr(context, alloc), 65 StateMgr(stateMgr), 66 ArrayIndexTy(context.IntTy), 67 ArrayIndexWidth(context.getTypeSize(ArrayIndexTy)) {} 68 69 virtual ~SValBuilder() {} 70 71 bool haveSameType(const SymExpr *Sym1, const SymExpr *Sym2) { 72 return haveSameType(Sym1->getType(Context), Sym2->getType(Context)); 73 } 74 75 bool haveSameType(QualType Ty1, QualType Ty2) { 76 // FIXME: Remove the second disjunct when we support symbolic 77 // truncation/extension. 78 return (Context.getCanonicalType(Ty1) == Context.getCanonicalType(Ty2) || 79 (Ty2->isIntegerType() && Ty2->isIntegerType())); 80 } 81 82 SVal evalCast(SVal val, QualType castTy, QualType originalType); 83 84 virtual SVal evalMinus(NonLoc val) = 0; 85 86 virtual SVal evalComplement(NonLoc val) = 0; 87 88 /// Create a new value which represents a binary expression with two non 89 /// location operands. 90 virtual SVal evalBinOpNN(const ProgramState *state, BinaryOperator::Opcode op, 91 NonLoc lhs, NonLoc rhs, QualType resultTy) = 0; 92 93 /// Create a new value which represents a binary expression with two memory 94 /// location operands. 95 virtual SVal evalBinOpLL(const ProgramState *state, BinaryOperator::Opcode op, 96 Loc lhs, Loc rhs, QualType resultTy) = 0; 97 98 /// Create a new value which represents a binary expression with a memory 99 /// location and non location operands. For example, this would be used to 100 /// evaluate a pointer arithmetic operation. 101 virtual SVal evalBinOpLN(const ProgramState *state, BinaryOperator::Opcode op, 102 Loc lhs, NonLoc rhs, QualType resultTy) = 0; 103 104 /// Evaluates a given SVal. If the SVal has only one possible (integer) value, 105 /// that value is returned. Otherwise, returns NULL. 106 virtual const llvm::APSInt *getKnownValue(const ProgramState *state, SVal val) = 0; 107 108 /// Handles generation of the value in case the builder is not smart enough to 109 /// handle the given binary expression. Depending on the state, decides to 110 /// either keep the expression or forget the history and generate an 111 /// UnknownVal. 112 SVal makeGenericVal(const ProgramState *state, BinaryOperator::Opcode op, 113 NonLoc lhs, NonLoc rhs, QualType resultTy); 114 115 SVal evalBinOp(const ProgramState *state, BinaryOperator::Opcode op, 116 SVal lhs, SVal rhs, QualType type); 117 118 DefinedOrUnknownSVal evalEQ(const ProgramState *state, DefinedOrUnknownSVal lhs, 119 DefinedOrUnknownSVal rhs); 120 121 ASTContext &getContext() { return Context; } 122 const ASTContext &getContext() const { return Context; } 123 124 ProgramStateManager &getStateManager() { return StateMgr; } 125 126 QualType getConditionType() const { 127 return getContext().IntTy; 128 } 129 130 QualType getArrayIndexType() const { 131 return ArrayIndexTy; 132 } 133 134 BasicValueFactory &getBasicValueFactory() { return BasicVals; } 135 const BasicValueFactory &getBasicValueFactory() const { return BasicVals; } 136 137 SymbolManager &getSymbolManager() { return SymMgr; } 138 const SymbolManager &getSymbolManager() const { return SymMgr; } 139 140 MemRegionManager &getRegionManager() { return MemMgr; } 141 const MemRegionManager &getRegionManager() const { return MemMgr; } 142 143 // Forwarding methods to SymbolManager. 144 145 const SymbolConjured* getConjuredSymbol(const Stmt *stmt, QualType type, 146 unsigned visitCount, 147 const void *symbolTag = 0) { 148 return SymMgr.getConjuredSymbol(stmt, type, visitCount, symbolTag); 149 } 150 151 const SymbolConjured* getConjuredSymbol(const Expr *expr, unsigned visitCount, 152 const void *symbolTag = 0) { 153 return SymMgr.getConjuredSymbol(expr, visitCount, symbolTag); 154 } 155 156 /// Construct an SVal representing '0' for the specified type. 157 DefinedOrUnknownSVal makeZeroVal(QualType type); 158 159 /// Make a unique symbol for value of region. 160 DefinedOrUnknownSVal getRegionValueSymbolVal(const TypedValueRegion *region); 161 162 /// \brief Create a new symbol with a unique 'name'. 163 /// 164 /// We resort to conjured symbols when we cannot construct a derived symbol. 165 /// The advantage of symbols derived/built from other symbols is that we 166 /// preserve the relation between related(or even equivalent) expressions, so 167 /// conjured symbols should be used sparingly. 168 DefinedOrUnknownSVal getConjuredSymbolVal(const void *symbolTag, 169 const Expr *expr, unsigned count); 170 DefinedOrUnknownSVal getConjuredSymbolVal(const void *symbolTag, 171 const Expr *expr, QualType type, 172 unsigned count); 173 174 DefinedOrUnknownSVal getDerivedRegionValueSymbolVal( 175 SymbolRef parentSymbol, const TypedValueRegion *region); 176 177 DefinedSVal getMetadataSymbolVal( 178 const void *symbolTag, const MemRegion *region, 179 const Expr *expr, QualType type, unsigned count); 180 181 DefinedSVal getFunctionPointer(const FunctionDecl *func); 182 183 DefinedSVal getBlockPointer(const BlockDecl *block, CanQualType locTy, 184 const LocationContext *locContext); 185 186 NonLoc makeCompoundVal(QualType type, llvm::ImmutableList<SVal> vals) { 187 return nonloc::CompoundVal(BasicVals.getCompoundValData(type, vals)); 188 } 189 190 NonLoc makeLazyCompoundVal(const StoreRef &store, 191 const TypedValueRegion *region) { 192 return nonloc::LazyCompoundVal( 193 BasicVals.getLazyCompoundValData(store, region)); 194 } 195 196 NonLoc makeZeroArrayIndex() { 197 return nonloc::ConcreteInt(BasicVals.getValue(0, ArrayIndexTy)); 198 } 199 200 NonLoc makeArrayIndex(uint64_t idx) { 201 return nonloc::ConcreteInt(BasicVals.getValue(idx, ArrayIndexTy)); 202 } 203 204 SVal convertToArrayIndex(SVal val); 205 206 nonloc::ConcreteInt makeIntVal(const IntegerLiteral* integer) { 207 return nonloc::ConcreteInt( 208 BasicVals.getValue(integer->getValue(), 209 integer->getType()->isUnsignedIntegerOrEnumerationType())); 210 } 211 212 nonloc::ConcreteInt makeBoolVal(const CXXBoolLiteralExpr *boolean) { 213 return makeTruthVal(boolean->getValue()); 214 } 215 216 nonloc::ConcreteInt makeIntVal(const llvm::APSInt& integer) { 217 return nonloc::ConcreteInt(BasicVals.getValue(integer)); 218 } 219 220 loc::ConcreteInt makeIntLocVal(const llvm::APSInt &integer) { 221 return loc::ConcreteInt(BasicVals.getValue(integer)); 222 } 223 224 NonLoc makeIntVal(const llvm::APInt& integer, bool isUnsigned) { 225 return nonloc::ConcreteInt(BasicVals.getValue(integer, isUnsigned)); 226 } 227 228 DefinedSVal makeIntVal(uint64_t integer, QualType type) { 229 if (Loc::isLocType(type)) 230 return loc::ConcreteInt(BasicVals.getValue(integer, type)); 231 232 return nonloc::ConcreteInt(BasicVals.getValue(integer, type)); 233 } 234 235 NonLoc makeIntVal(uint64_t integer, bool isUnsigned) { 236 return nonloc::ConcreteInt(BasicVals.getIntValue(integer, isUnsigned)); 237 } 238 239 NonLoc makeIntValWithPtrWidth(uint64_t integer, bool isUnsigned) { 240 return nonloc::ConcreteInt( 241 BasicVals.getIntWithPtrWidth(integer, isUnsigned)); 242 } 243 244 NonLoc makeIntVal(uint64_t integer, unsigned bitWidth, bool isUnsigned) { 245 return nonloc::ConcreteInt( 246 BasicVals.getValue(integer, bitWidth, isUnsigned)); 247 } 248 249 NonLoc makeLocAsInteger(Loc loc, unsigned bits) { 250 return nonloc::LocAsInteger(BasicVals.getPersistentSValWithData(loc, bits)); 251 } 252 253 NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op, 254 const llvm::APSInt& rhs, QualType type); 255 256 NonLoc makeNonLoc(const llvm::APSInt& rhs, BinaryOperator::Opcode op, 257 const SymExpr *lhs, QualType type); 258 259 NonLoc makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op, 260 const SymExpr *rhs, QualType type); 261 262 /// \brief Create a NonLoc value for cast. 263 NonLoc makeNonLoc(const SymExpr *operand, QualType fromTy, QualType toTy); 264 265 nonloc::ConcreteInt makeTruthVal(bool b, QualType type) { 266 return nonloc::ConcreteInt(BasicVals.getTruthValue(b, type)); 267 } 268 269 nonloc::ConcreteInt makeTruthVal(bool b) { 270 return nonloc::ConcreteInt(BasicVals.getTruthValue(b)); 271 } 272 273 Loc makeNull() { 274 return loc::ConcreteInt(BasicVals.getZeroWithPtrWidth()); 275 } 276 277 Loc makeLoc(SymbolRef sym) { 278 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym)); 279 } 280 281 Loc makeLoc(const MemRegion* region) { 282 return loc::MemRegionVal(region); 283 } 284 285 Loc makeLoc(const AddrLabelExpr *expr) { 286 return loc::GotoLabel(expr->getLabel()); 287 } 288 289 Loc makeLoc(const llvm::APSInt& integer) { 290 return loc::ConcreteInt(BasicVals.getValue(integer)); 291 } 292 293}; 294 295SValBuilder* createSimpleSValBuilder(llvm::BumpPtrAllocator &alloc, 296 ASTContext &context, 297 ProgramStateManager &stateMgr); 298 299} // end GR namespace 300 301} // end clang namespace 302 303#endif 304