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