1 // SValBuilder.cpp - Basic class for all SValBuilder implementations -*- 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, the base class for all (complete) SValBuilder 11 // implementations. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "clang/AST/ExprCXX.h" 16 #include "clang/AST/DeclCXX.h" 17 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h" 18 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" 19 #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h" 20 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 21 #include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h" 22 23 using namespace clang; 24 using namespace ento; 25 26 //===----------------------------------------------------------------------===// 27 // Basic SVal creation. 28 //===----------------------------------------------------------------------===// 29 30 void SValBuilder::anchor() { } 31 32 DefinedOrUnknownSVal SValBuilder::makeZeroVal(QualType type) { 33 if (Loc::isLocType(type)) 34 return makeNull(); 35 36 if (type->isIntegerType()) 37 return makeIntVal(0, type); 38 39 // FIXME: Handle floats. 40 // FIXME: Handle structs. 41 return UnknownVal(); 42 } 43 44 NonLoc SValBuilder::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op, 45 const llvm::APSInt& rhs, QualType type) { 46 // The Environment ensures we always get a persistent APSInt in 47 // BasicValueFactory, so we don't need to get the APSInt from 48 // BasicValueFactory again. 49 assert(lhs); 50 assert(!Loc::isLocType(type)); 51 return nonloc::SymbolVal(SymMgr.getSymIntExpr(lhs, op, rhs, type)); 52 } 53 54 NonLoc SValBuilder::makeNonLoc(const llvm::APSInt& lhs, 55 BinaryOperator::Opcode op, const SymExpr *rhs, 56 QualType type) { 57 assert(rhs); 58 assert(!Loc::isLocType(type)); 59 return nonloc::SymbolVal(SymMgr.getIntSymExpr(lhs, op, rhs, type)); 60 } 61 62 NonLoc SValBuilder::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op, 63 const SymExpr *rhs, QualType type) { 64 assert(lhs && rhs); 65 assert(!Loc::isLocType(type)); 66 return nonloc::SymbolVal(SymMgr.getSymSymExpr(lhs, op, rhs, type)); 67 } 68 69 NonLoc SValBuilder::makeNonLoc(const SymExpr *operand, 70 QualType fromTy, QualType toTy) { 71 assert(operand); 72 assert(!Loc::isLocType(toTy)); 73 return nonloc::SymbolVal(SymMgr.getCastSymbol(operand, fromTy, toTy)); 74 } 75 76 SVal SValBuilder::convertToArrayIndex(SVal val) { 77 if (val.isUnknownOrUndef()) 78 return val; 79 80 // Common case: we have an appropriately sized integer. 81 if (nonloc::ConcreteInt* CI = dyn_cast<nonloc::ConcreteInt>(&val)) { 82 const llvm::APSInt& I = CI->getValue(); 83 if (I.getBitWidth() == ArrayIndexWidth && I.isSigned()) 84 return val; 85 } 86 87 return evalCastFromNonLoc(cast<NonLoc>(val), ArrayIndexTy); 88 } 89 90 nonloc::ConcreteInt SValBuilder::makeBoolVal(const CXXBoolLiteralExpr *boolean){ 91 return makeTruthVal(boolean->getValue()); 92 } 93 94 DefinedOrUnknownSVal 95 SValBuilder::getRegionValueSymbolVal(const TypedValueRegion* region) { 96 QualType T = region->getValueType(); 97 98 if (!SymbolManager::canSymbolicate(T)) 99 return UnknownVal(); 100 101 SymbolRef sym = SymMgr.getRegionValueSymbol(region); 102 103 if (Loc::isLocType(T)) 104 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym)); 105 106 return nonloc::SymbolVal(sym); 107 } 108 109 DefinedOrUnknownSVal 110 SValBuilder::getConjuredSymbolVal(const void *symbolTag, 111 const Expr *expr, 112 const LocationContext *LCtx, 113 unsigned count) { 114 QualType T = expr->getType(); 115 return getConjuredSymbolVal(symbolTag, expr, LCtx, T, count); 116 } 117 118 DefinedOrUnknownSVal 119 SValBuilder::getConjuredSymbolVal(const void *symbolTag, 120 const Expr *expr, 121 const LocationContext *LCtx, 122 QualType type, 123 unsigned count) { 124 if (!SymbolManager::canSymbolicate(type)) 125 return UnknownVal(); 126 127 SymbolRef sym = SymMgr.getConjuredSymbol(expr, LCtx, type, count, symbolTag); 128 129 if (Loc::isLocType(type)) 130 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym)); 131 132 return nonloc::SymbolVal(sym); 133 } 134 135 136 DefinedOrUnknownSVal 137 SValBuilder::getConjuredSymbolVal(const Stmt *stmt, 138 const LocationContext *LCtx, 139 QualType type, 140 unsigned visitCount) { 141 if (!SymbolManager::canSymbolicate(type)) 142 return UnknownVal(); 143 144 SymbolRef sym = SymMgr.getConjuredSymbol(stmt, LCtx, type, visitCount); 145 146 if (Loc::isLocType(type)) 147 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym)); 148 149 return nonloc::SymbolVal(sym); 150 } 151 152 DefinedOrUnknownSVal 153 SValBuilder::getConjuredHeapSymbolVal(const Expr *E, 154 const LocationContext *LCtx, 155 unsigned VisitCount) { 156 QualType T = E->getType(); 157 assert(Loc::isLocType(T)); 158 assert(SymbolManager::canSymbolicate(T)); 159 160 SymbolRef sym = SymMgr.getConjuredSymbol(E, LCtx, T, VisitCount); 161 return loc::MemRegionVal(MemMgr.getSymbolicHeapRegion(sym)); 162 } 163 164 DefinedSVal SValBuilder::getMetadataSymbolVal(const void *symbolTag, 165 const MemRegion *region, 166 const Expr *expr, QualType type, 167 unsigned count) { 168 assert(SymbolManager::canSymbolicate(type) && "Invalid metadata symbol type"); 169 170 SymbolRef sym = 171 SymMgr.getMetadataSymbol(region, expr, type, count, symbolTag); 172 173 if (Loc::isLocType(type)) 174 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym)); 175 176 return nonloc::SymbolVal(sym); 177 } 178 179 DefinedOrUnknownSVal 180 SValBuilder::getDerivedRegionValueSymbolVal(SymbolRef parentSymbol, 181 const TypedValueRegion *region) { 182 QualType T = region->getValueType(); 183 184 if (!SymbolManager::canSymbolicate(T)) 185 return UnknownVal(); 186 187 SymbolRef sym = SymMgr.getDerivedSymbol(parentSymbol, region); 188 189 if (Loc::isLocType(T)) 190 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym)); 191 192 return nonloc::SymbolVal(sym); 193 } 194 195 DefinedSVal SValBuilder::getFunctionPointer(const FunctionDecl *func) { 196 return loc::MemRegionVal(MemMgr.getFunctionTextRegion(func)); 197 } 198 199 DefinedSVal SValBuilder::getBlockPointer(const BlockDecl *block, 200 CanQualType locTy, 201 const LocationContext *locContext) { 202 const BlockTextRegion *BC = 203 MemMgr.getBlockTextRegion(block, locTy, locContext->getAnalysisDeclContext()); 204 const BlockDataRegion *BD = MemMgr.getBlockDataRegion(BC, locContext); 205 return loc::MemRegionVal(BD); 206 } 207 208 /// Return a memory region for the 'this' object reference. 209 loc::MemRegionVal SValBuilder::getCXXThis(const CXXMethodDecl *D, 210 const StackFrameContext *SFC) { 211 return loc::MemRegionVal(getRegionManager(). 212 getCXXThisRegion(D->getThisType(getContext()), SFC)); 213 } 214 215 /// Return a memory region for the 'this' object reference. 216 loc::MemRegionVal SValBuilder::getCXXThis(const CXXRecordDecl *D, 217 const StackFrameContext *SFC) { 218 const Type *T = D->getTypeForDecl(); 219 QualType PT = getContext().getPointerType(QualType(T, 0)); 220 return loc::MemRegionVal(getRegionManager().getCXXThisRegion(PT, SFC)); 221 } 222 223 //===----------------------------------------------------------------------===// 224 225 SVal SValBuilder::makeSymExprValNN(ProgramStateRef State, 226 BinaryOperator::Opcode Op, 227 NonLoc LHS, NonLoc RHS, 228 QualType ResultTy) { 229 if (!State->isTainted(RHS) && !State->isTainted(LHS)) 230 return UnknownVal(); 231 232 const SymExpr *symLHS = LHS.getAsSymExpr(); 233 const SymExpr *symRHS = RHS.getAsSymExpr(); 234 // TODO: When the Max Complexity is reached, we should conjure a symbol 235 // instead of generating an Unknown value and propagate the taint info to it. 236 const unsigned MaxComp = 10000; // 100000 28X 237 238 if (symLHS && symRHS && 239 (symLHS->computeComplexity() + symRHS->computeComplexity()) < MaxComp) 240 return makeNonLoc(symLHS, Op, symRHS, ResultTy); 241 242 if (symLHS && symLHS->computeComplexity() < MaxComp) 243 if (const nonloc::ConcreteInt *rInt = dyn_cast<nonloc::ConcreteInt>(&RHS)) 244 return makeNonLoc(symLHS, Op, rInt->getValue(), ResultTy); 245 246 if (symRHS && symRHS->computeComplexity() < MaxComp) 247 if (const nonloc::ConcreteInt *lInt = dyn_cast<nonloc::ConcreteInt>(&LHS)) 248 return makeNonLoc(lInt->getValue(), Op, symRHS, ResultTy); 249 250 return UnknownVal(); 251 } 252 253 254 SVal SValBuilder::evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op, 255 SVal lhs, SVal rhs, QualType type) { 256 257 if (lhs.isUndef() || rhs.isUndef()) 258 return UndefinedVal(); 259 260 if (lhs.isUnknown() || rhs.isUnknown()) 261 return UnknownVal(); 262 263 if (isa<Loc>(lhs)) { 264 if (isa<Loc>(rhs)) 265 return evalBinOpLL(state, op, cast<Loc>(lhs), cast<Loc>(rhs), type); 266 267 return evalBinOpLN(state, op, cast<Loc>(lhs), cast<NonLoc>(rhs), type); 268 } 269 270 if (isa<Loc>(rhs)) { 271 // Support pointer arithmetic where the addend is on the left 272 // and the pointer on the right. 273 assert(op == BO_Add); 274 275 // Commute the operands. 276 return evalBinOpLN(state, op, cast<Loc>(rhs), cast<NonLoc>(lhs), type); 277 } 278 279 return evalBinOpNN(state, op, cast<NonLoc>(lhs), cast<NonLoc>(rhs), type); 280 } 281 282 DefinedOrUnknownSVal SValBuilder::evalEQ(ProgramStateRef state, 283 DefinedOrUnknownSVal lhs, 284 DefinedOrUnknownSVal rhs) { 285 return cast<DefinedOrUnknownSVal>(evalBinOp(state, BO_EQ, lhs, rhs, 286 Context.IntTy)); 287 } 288 289 /// Recursively check if the pointer types are equal modulo const, volatile, 290 /// and restrict qualifiers. Assumes the input types are canonical. 291 /// TODO: This is based off of code in SemaCast; can we reuse it. 292 static bool haveSimilarTypes(ASTContext &Context, QualType T1, 293 QualType T2) { 294 while (Context.UnwrapSimilarPointerTypes(T1, T2)) { 295 Qualifiers Quals1, Quals2; 296 T1 = Context.getUnqualifiedArrayType(T1, Quals1); 297 T2 = Context.getUnqualifiedArrayType(T2, Quals2); 298 299 // Make sure that non cvr-qualifiers the other qualifiers (e.g., address 300 // spaces) are identical. 301 Quals1.removeCVRQualifiers(); 302 Quals2.removeCVRQualifiers(); 303 if (Quals1 != Quals2) 304 return false; 305 } 306 307 if (T1 != T2) 308 return false; 309 310 return true; 311 } 312 313 // FIXME: should rewrite according to the cast kind. 314 SVal SValBuilder::evalCast(SVal val, QualType castTy, QualType originalTy) { 315 castTy = Context.getCanonicalType(castTy); 316 originalTy = Context.getCanonicalType(originalTy); 317 if (val.isUnknownOrUndef() || castTy == originalTy) 318 return val; 319 320 // For const casts, just propagate the value. 321 if (!castTy->isVariableArrayType() && !originalTy->isVariableArrayType()) 322 if (haveSimilarTypes(Context, Context.getPointerType(castTy), 323 Context.getPointerType(originalTy))) 324 return val; 325 326 // Check for casts from pointers to integers. 327 if (castTy->isIntegerType() && Loc::isLocType(originalTy)) 328 return evalCastFromLoc(cast<Loc>(val), castTy); 329 330 // Check for casts from integers to pointers. 331 if (Loc::isLocType(castTy) && originalTy->isIntegerType()) { 332 if (nonloc::LocAsInteger *LV = dyn_cast<nonloc::LocAsInteger>(&val)) { 333 if (const MemRegion *R = LV->getLoc().getAsRegion()) { 334 StoreManager &storeMgr = StateMgr.getStoreManager(); 335 R = storeMgr.castRegion(R, castTy); 336 return R ? SVal(loc::MemRegionVal(R)) : UnknownVal(); 337 } 338 return LV->getLoc(); 339 } 340 return dispatchCast(val, castTy); 341 } 342 343 // Just pass through function and block pointers. 344 if (originalTy->isBlockPointerType() || originalTy->isFunctionPointerType()) { 345 assert(Loc::isLocType(castTy)); 346 return val; 347 } 348 349 // Check for casts from array type to another type. 350 if (originalTy->isArrayType()) { 351 // We will always decay to a pointer. 352 val = StateMgr.ArrayToPointer(cast<Loc>(val)); 353 354 // Are we casting from an array to a pointer? If so just pass on 355 // the decayed value. 356 if (castTy->isPointerType() || castTy->isReferenceType()) 357 return val; 358 359 // Are we casting from an array to an integer? If so, cast the decayed 360 // pointer value to an integer. 361 assert(castTy->isIntegerType()); 362 363 // FIXME: Keep these here for now in case we decide soon that we 364 // need the original decayed type. 365 // QualType elemTy = cast<ArrayType>(originalTy)->getElementType(); 366 // QualType pointerTy = C.getPointerType(elemTy); 367 return evalCastFromLoc(cast<Loc>(val), castTy); 368 } 369 370 // Check for casts from a region to a specific type. 371 if (const MemRegion *R = val.getAsRegion()) { 372 // Handle other casts of locations to integers. 373 if (castTy->isIntegerType()) 374 return evalCastFromLoc(loc::MemRegionVal(R), castTy); 375 376 // FIXME: We should handle the case where we strip off view layers to get 377 // to a desugared type. 378 if (!Loc::isLocType(castTy)) { 379 // FIXME: There can be gross cases where one casts the result of a function 380 // (that returns a pointer) to some other value that happens to fit 381 // within that pointer value. We currently have no good way to 382 // model such operations. When this happens, the underlying operation 383 // is that the caller is reasoning about bits. Conceptually we are 384 // layering a "view" of a location on top of those bits. Perhaps 385 // we need to be more lazy about mutual possible views, even on an 386 // SVal? This may be necessary for bit-level reasoning as well. 387 return UnknownVal(); 388 } 389 390 // We get a symbolic function pointer for a dereference of a function 391 // pointer, but it is of function type. Example: 392 393 // struct FPRec { 394 // void (*my_func)(int * x); 395 // }; 396 // 397 // int bar(int x); 398 // 399 // int f1_a(struct FPRec* foo) { 400 // int x; 401 // (*foo->my_func)(&x); 402 // return bar(x)+1; // no-warning 403 // } 404 405 assert(Loc::isLocType(originalTy) || originalTy->isFunctionType() || 406 originalTy->isBlockPointerType() || castTy->isReferenceType()); 407 408 StoreManager &storeMgr = StateMgr.getStoreManager(); 409 410 // Delegate to store manager to get the result of casting a region to a 411 // different type. If the MemRegion* returned is NULL, this expression 412 // Evaluates to UnknownVal. 413 R = storeMgr.castRegion(R, castTy); 414 return R ? SVal(loc::MemRegionVal(R)) : UnknownVal(); 415 } 416 417 return dispatchCast(val, castTy); 418 } 419