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/StaticAnalyzer/Core/PathSensitive/SValBuilder.h" 16 #include "clang/AST/DeclCXX.h" 17 #include "clang/AST/ExprCXX.h" 18 #include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h" 19 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h" 20 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 21 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.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->isIntegralOrEnumerationType()) 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 (Optional<nonloc::ConcreteInt> CI = val.getAs<nonloc::ConcreteInt>()) { 82 const llvm::APSInt& I = CI->getValue(); 83 if (I.getBitWidth() == ArrayIndexWidth && I.isSigned()) 84 return val; 85 } 86 87 return evalCastFromNonLoc(val.castAs<NonLoc>(), 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 SValBuilder::conjureSymbolVal(const void *SymbolTag, 110 const Expr *Ex, 111 const LocationContext *LCtx, 112 unsigned Count) { 113 QualType T = Ex->getType(); 114 115 // Compute the type of the result. If the expression is not an R-value, the 116 // result should be a location. 117 QualType ExType = Ex->getType(); 118 if (Ex->isGLValue()) 119 T = LCtx->getAnalysisDeclContext()->getASTContext().getPointerType(ExType); 120 121 return conjureSymbolVal(SymbolTag, Ex, LCtx, T, Count); 122 } 123 124 DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const void *symbolTag, 125 const Expr *expr, 126 const LocationContext *LCtx, 127 QualType type, 128 unsigned count) { 129 if (!SymbolManager::canSymbolicate(type)) 130 return UnknownVal(); 131 132 SymbolRef sym = SymMgr.conjureSymbol(expr, LCtx, type, count, symbolTag); 133 134 if (Loc::isLocType(type)) 135 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym)); 136 137 return nonloc::SymbolVal(sym); 138 } 139 140 141 DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const Stmt *stmt, 142 const LocationContext *LCtx, 143 QualType type, 144 unsigned visitCount) { 145 if (!SymbolManager::canSymbolicate(type)) 146 return UnknownVal(); 147 148 SymbolRef sym = SymMgr.conjureSymbol(stmt, LCtx, type, visitCount); 149 150 if (Loc::isLocType(type)) 151 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym)); 152 153 return nonloc::SymbolVal(sym); 154 } 155 156 DefinedOrUnknownSVal 157 SValBuilder::getConjuredHeapSymbolVal(const Expr *E, 158 const LocationContext *LCtx, 159 unsigned VisitCount) { 160 QualType T = E->getType(); 161 assert(Loc::isLocType(T)); 162 assert(SymbolManager::canSymbolicate(T)); 163 164 SymbolRef sym = SymMgr.conjureSymbol(E, LCtx, T, VisitCount); 165 return loc::MemRegionVal(MemMgr.getSymbolicHeapRegion(sym)); 166 } 167 168 DefinedSVal SValBuilder::getMetadataSymbolVal(const void *symbolTag, 169 const MemRegion *region, 170 const Expr *expr, QualType type, 171 unsigned count) { 172 assert(SymbolManager::canSymbolicate(type) && "Invalid metadata symbol type"); 173 174 SymbolRef sym = 175 SymMgr.getMetadataSymbol(region, expr, type, count, symbolTag); 176 177 if (Loc::isLocType(type)) 178 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym)); 179 180 return nonloc::SymbolVal(sym); 181 } 182 183 DefinedOrUnknownSVal 184 SValBuilder::getDerivedRegionValueSymbolVal(SymbolRef parentSymbol, 185 const TypedValueRegion *region) { 186 QualType T = region->getValueType(); 187 188 if (!SymbolManager::canSymbolicate(T)) 189 return UnknownVal(); 190 191 SymbolRef sym = SymMgr.getDerivedSymbol(parentSymbol, region); 192 193 if (Loc::isLocType(T)) 194 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym)); 195 196 return nonloc::SymbolVal(sym); 197 } 198 199 DefinedSVal SValBuilder::getFunctionPointer(const FunctionDecl *func) { 200 return loc::MemRegionVal(MemMgr.getFunctionTextRegion(func)); 201 } 202 203 DefinedSVal SValBuilder::getBlockPointer(const BlockDecl *block, 204 CanQualType locTy, 205 const LocationContext *locContext, 206 unsigned blockCount) { 207 const BlockTextRegion *BC = 208 MemMgr.getBlockTextRegion(block, locTy, locContext->getAnalysisDeclContext()); 209 const BlockDataRegion *BD = MemMgr.getBlockDataRegion(BC, locContext, 210 blockCount); 211 return loc::MemRegionVal(BD); 212 } 213 214 /// Return a memory region for the 'this' object reference. 215 loc::MemRegionVal SValBuilder::getCXXThis(const CXXMethodDecl *D, 216 const StackFrameContext *SFC) { 217 return loc::MemRegionVal(getRegionManager(). 218 getCXXThisRegion(D->getThisType(getContext()), SFC)); 219 } 220 221 /// Return a memory region for the 'this' object reference. 222 loc::MemRegionVal SValBuilder::getCXXThis(const CXXRecordDecl *D, 223 const StackFrameContext *SFC) { 224 const Type *T = D->getTypeForDecl(); 225 QualType PT = getContext().getPointerType(QualType(T, 0)); 226 return loc::MemRegionVal(getRegionManager().getCXXThisRegion(PT, SFC)); 227 } 228 229 Optional<SVal> SValBuilder::getConstantVal(const Expr *E) { 230 E = E->IgnoreParens(); 231 232 switch (E->getStmtClass()) { 233 // Handle expressions that we treat differently from the AST's constant 234 // evaluator. 235 case Stmt::AddrLabelExprClass: 236 return makeLoc(cast<AddrLabelExpr>(E)); 237 238 case Stmt::CXXScalarValueInitExprClass: 239 case Stmt::ImplicitValueInitExprClass: 240 return makeZeroVal(E->getType()); 241 242 case Stmt::ObjCStringLiteralClass: { 243 const ObjCStringLiteral *SL = cast<ObjCStringLiteral>(E); 244 return makeLoc(getRegionManager().getObjCStringRegion(SL)); 245 } 246 247 case Stmt::StringLiteralClass: { 248 const StringLiteral *SL = cast<StringLiteral>(E); 249 return makeLoc(getRegionManager().getStringRegion(SL)); 250 } 251 252 // Fast-path some expressions to avoid the overhead of going through the AST's 253 // constant evaluator 254 case Stmt::CharacterLiteralClass: { 255 const CharacterLiteral *C = cast<CharacterLiteral>(E); 256 return makeIntVal(C->getValue(), C->getType()); 257 } 258 259 case Stmt::CXXBoolLiteralExprClass: 260 return makeBoolVal(cast<CXXBoolLiteralExpr>(E)); 261 262 case Stmt::TypeTraitExprClass: { 263 const TypeTraitExpr *TE = cast<TypeTraitExpr>(E); 264 return makeTruthVal(TE->getValue(), TE->getType()); 265 } 266 267 case Stmt::IntegerLiteralClass: 268 return makeIntVal(cast<IntegerLiteral>(E)); 269 270 case Stmt::ObjCBoolLiteralExprClass: 271 return makeBoolVal(cast<ObjCBoolLiteralExpr>(E)); 272 273 case Stmt::CXXNullPtrLiteralExprClass: 274 return makeNull(); 275 276 case Stmt::ImplicitCastExprClass: { 277 const CastExpr *CE = cast<CastExpr>(E); 278 switch (CE->getCastKind()) { 279 default: 280 break; 281 case CK_ArrayToPointerDecay: 282 case CK_BitCast: { 283 const Expr *SE = CE->getSubExpr(); 284 Optional<SVal> Val = getConstantVal(SE); 285 if (!Val) 286 return None; 287 return evalCast(*Val, CE->getType(), SE->getType()); 288 } 289 } 290 // FALLTHROUGH 291 } 292 293 // If we don't have a special case, fall back to the AST's constant evaluator. 294 default: { 295 // Don't try to come up with a value for materialized temporaries. 296 if (E->isGLValue()) 297 return None; 298 299 ASTContext &Ctx = getContext(); 300 llvm::APSInt Result; 301 if (E->EvaluateAsInt(Result, Ctx)) 302 return makeIntVal(Result); 303 304 if (Loc::isLocType(E->getType())) 305 if (E->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull)) 306 return makeNull(); 307 308 return None; 309 } 310 } 311 } 312 313 //===----------------------------------------------------------------------===// 314 315 SVal SValBuilder::makeSymExprValNN(ProgramStateRef State, 316 BinaryOperator::Opcode Op, 317 NonLoc LHS, NonLoc RHS, 318 QualType ResultTy) { 319 if (!State->isTainted(RHS) && !State->isTainted(LHS)) 320 return UnknownVal(); 321 322 const SymExpr *symLHS = LHS.getAsSymExpr(); 323 const SymExpr *symRHS = RHS.getAsSymExpr(); 324 // TODO: When the Max Complexity is reached, we should conjure a symbol 325 // instead of generating an Unknown value and propagate the taint info to it. 326 const unsigned MaxComp = 10000; // 100000 28X 327 328 if (symLHS && symRHS && 329 (symLHS->computeComplexity() + symRHS->computeComplexity()) < MaxComp) 330 return makeNonLoc(symLHS, Op, symRHS, ResultTy); 331 332 if (symLHS && symLHS->computeComplexity() < MaxComp) 333 if (Optional<nonloc::ConcreteInt> rInt = RHS.getAs<nonloc::ConcreteInt>()) 334 return makeNonLoc(symLHS, Op, rInt->getValue(), ResultTy); 335 336 if (symRHS && symRHS->computeComplexity() < MaxComp) 337 if (Optional<nonloc::ConcreteInt> lInt = LHS.getAs<nonloc::ConcreteInt>()) 338 return makeNonLoc(lInt->getValue(), Op, symRHS, ResultTy); 339 340 return UnknownVal(); 341 } 342 343 344 SVal SValBuilder::evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op, 345 SVal lhs, SVal rhs, QualType type) { 346 347 if (lhs.isUndef() || rhs.isUndef()) 348 return UndefinedVal(); 349 350 if (lhs.isUnknown() || rhs.isUnknown()) 351 return UnknownVal(); 352 353 if (Optional<Loc> LV = lhs.getAs<Loc>()) { 354 if (Optional<Loc> RV = rhs.getAs<Loc>()) 355 return evalBinOpLL(state, op, *LV, *RV, type); 356 357 return evalBinOpLN(state, op, *LV, rhs.castAs<NonLoc>(), type); 358 } 359 360 if (Optional<Loc> RV = rhs.getAs<Loc>()) { 361 // Support pointer arithmetic where the addend is on the left 362 // and the pointer on the right. 363 assert(op == BO_Add); 364 365 // Commute the operands. 366 return evalBinOpLN(state, op, *RV, lhs.castAs<NonLoc>(), type); 367 } 368 369 return evalBinOpNN(state, op, lhs.castAs<NonLoc>(), rhs.castAs<NonLoc>(), 370 type); 371 } 372 373 DefinedOrUnknownSVal SValBuilder::evalEQ(ProgramStateRef state, 374 DefinedOrUnknownSVal lhs, 375 DefinedOrUnknownSVal rhs) { 376 return evalBinOp(state, BO_EQ, lhs, rhs, getConditionType()) 377 .castAs<DefinedOrUnknownSVal>(); 378 } 379 380 /// Recursively check if the pointer types are equal modulo const, volatile, 381 /// and restrict qualifiers. Also, assume that all types are similar to 'void'. 382 /// Assumes the input types are canonical. 383 static bool shouldBeModeledWithNoOp(ASTContext &Context, QualType ToTy, 384 QualType FromTy) { 385 while (Context.UnwrapSimilarPointerTypes(ToTy, FromTy)) { 386 Qualifiers Quals1, Quals2; 387 ToTy = Context.getUnqualifiedArrayType(ToTy, Quals1); 388 FromTy = Context.getUnqualifiedArrayType(FromTy, Quals2); 389 390 // Make sure that non-cvr-qualifiers the other qualifiers (e.g., address 391 // spaces) are identical. 392 Quals1.removeCVRQualifiers(); 393 Quals2.removeCVRQualifiers(); 394 if (Quals1 != Quals2) 395 return false; 396 } 397 398 // If we are casting to void, the 'From' value can be used to represent the 399 // 'To' value. 400 if (ToTy->isVoidType()) 401 return true; 402 403 if (ToTy != FromTy) 404 return false; 405 406 return true; 407 } 408 409 // FIXME: should rewrite according to the cast kind. 410 SVal SValBuilder::evalCast(SVal val, QualType castTy, QualType originalTy) { 411 castTy = Context.getCanonicalType(castTy); 412 originalTy = Context.getCanonicalType(originalTy); 413 if (val.isUnknownOrUndef() || castTy == originalTy) 414 return val; 415 416 if (castTy->isBooleanType()) { 417 if (val.isUnknownOrUndef()) 418 return val; 419 if (val.isConstant()) 420 return makeTruthVal(!val.isZeroConstant(), castTy); 421 if (!Loc::isLocType(originalTy) && 422 !originalTy->isIntegralOrEnumerationType() && 423 !originalTy->isMemberPointerType()) 424 return UnknownVal(); 425 if (SymbolRef Sym = val.getAsSymbol(true)) { 426 BasicValueFactory &BVF = getBasicValueFactory(); 427 // FIXME: If we had a state here, we could see if the symbol is known to 428 // be zero, but we don't. 429 return makeNonLoc(Sym, BO_NE, BVF.getValue(0, Sym->getType()), castTy); 430 } 431 // Loc values are not always true, they could be weakly linked functions. 432 if (Optional<Loc> L = val.getAs<Loc>()) 433 return evalCastFromLoc(*L, castTy); 434 435 Loc L = val.castAs<nonloc::LocAsInteger>().getLoc(); 436 return evalCastFromLoc(L, castTy); 437 } 438 439 // For const casts, casts to void, just propagate the value. 440 if (!castTy->isVariableArrayType() && !originalTy->isVariableArrayType()) 441 if (shouldBeModeledWithNoOp(Context, Context.getPointerType(castTy), 442 Context.getPointerType(originalTy))) 443 return val; 444 445 // Check for casts from pointers to integers. 446 if (castTy->isIntegralOrEnumerationType() && Loc::isLocType(originalTy)) 447 return evalCastFromLoc(val.castAs<Loc>(), castTy); 448 449 // Check for casts from integers to pointers. 450 if (Loc::isLocType(castTy) && originalTy->isIntegralOrEnumerationType()) { 451 if (Optional<nonloc::LocAsInteger> LV = val.getAs<nonloc::LocAsInteger>()) { 452 if (const MemRegion *R = LV->getLoc().getAsRegion()) { 453 StoreManager &storeMgr = StateMgr.getStoreManager(); 454 R = storeMgr.castRegion(R, castTy); 455 return R ? SVal(loc::MemRegionVal(R)) : UnknownVal(); 456 } 457 return LV->getLoc(); 458 } 459 return dispatchCast(val, castTy); 460 } 461 462 // Just pass through function and block pointers. 463 if (originalTy->isBlockPointerType() || originalTy->isFunctionPointerType()) { 464 assert(Loc::isLocType(castTy)); 465 return val; 466 } 467 468 // Check for casts from array type to another type. 469 if (const ArrayType *arrayT = 470 dyn_cast<ArrayType>(originalTy.getCanonicalType())) { 471 // We will always decay to a pointer. 472 QualType elemTy = arrayT->getElementType(); 473 val = StateMgr.ArrayToPointer(val.castAs<Loc>(), elemTy); 474 475 // Are we casting from an array to a pointer? If so just pass on 476 // the decayed value. 477 if (castTy->isPointerType() || castTy->isReferenceType()) 478 return val; 479 480 // Are we casting from an array to an integer? If so, cast the decayed 481 // pointer value to an integer. 482 assert(castTy->isIntegralOrEnumerationType()); 483 484 // FIXME: Keep these here for now in case we decide soon that we 485 // need the original decayed type. 486 // QualType elemTy = cast<ArrayType>(originalTy)->getElementType(); 487 // QualType pointerTy = C.getPointerType(elemTy); 488 return evalCastFromLoc(val.castAs<Loc>(), castTy); 489 } 490 491 // Check for casts from a region to a specific type. 492 if (const MemRegion *R = val.getAsRegion()) { 493 // Handle other casts of locations to integers. 494 if (castTy->isIntegralOrEnumerationType()) 495 return evalCastFromLoc(loc::MemRegionVal(R), castTy); 496 497 // FIXME: We should handle the case where we strip off view layers to get 498 // to a desugared type. 499 if (!Loc::isLocType(castTy)) { 500 // FIXME: There can be gross cases where one casts the result of a function 501 // (that returns a pointer) to some other value that happens to fit 502 // within that pointer value. We currently have no good way to 503 // model such operations. When this happens, the underlying operation 504 // is that the caller is reasoning about bits. Conceptually we are 505 // layering a "view" of a location on top of those bits. Perhaps 506 // we need to be more lazy about mutual possible views, even on an 507 // SVal? This may be necessary for bit-level reasoning as well. 508 return UnknownVal(); 509 } 510 511 // We get a symbolic function pointer for a dereference of a function 512 // pointer, but it is of function type. Example: 513 514 // struct FPRec { 515 // void (*my_func)(int * x); 516 // }; 517 // 518 // int bar(int x); 519 // 520 // int f1_a(struct FPRec* foo) { 521 // int x; 522 // (*foo->my_func)(&x); 523 // return bar(x)+1; // no-warning 524 // } 525 526 assert(Loc::isLocType(originalTy) || originalTy->isFunctionType() || 527 originalTy->isBlockPointerType() || castTy->isReferenceType()); 528 529 StoreManager &storeMgr = StateMgr.getStoreManager(); 530 531 // Delegate to store manager to get the result of casting a region to a 532 // different type. If the MemRegion* returned is NULL, this expression 533 // Evaluates to UnknownVal. 534 R = storeMgr.castRegion(R, castTy); 535 return R ? SVal(loc::MemRegionVal(R)) : UnknownVal(); 536 } 537 538 return dispatchCast(val, castTy); 539 } 540