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