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