1 //===- SValBuilder.cpp - Basic class for all SValBuilder implementations --===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines SValBuilder, the base class for all (complete) SValBuilder 10 // implementations. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h" 15 #include "clang/AST/ASTContext.h" 16 #include "clang/AST/Decl.h" 17 #include "clang/AST/DeclCXX.h" 18 #include "clang/AST/ExprCXX.h" 19 #include "clang/AST/ExprObjC.h" 20 #include "clang/AST/Stmt.h" 21 #include "clang/AST/Type.h" 22 #include "clang/Basic/LLVM.h" 23 #include "clang/Analysis/AnalysisDeclContext.h" 24 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" 25 #include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h" 26 #include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h" 27 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 28 #include "clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h" 29 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 30 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h" 31 #include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h" 32 #include "clang/StaticAnalyzer/Core/PathSensitive/Store.h" 33 #include "clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h" 34 #include "clang/StaticAnalyzer/Core/PathSensitive/SymbolManager.h" 35 #include "llvm/ADT/APSInt.h" 36 #include "llvm/ADT/None.h" 37 #include "llvm/ADT/Optional.h" 38 #include "llvm/Support/Casting.h" 39 #include "llvm/Support/Compiler.h" 40 #include <cassert> 41 #include <tuple> 42 43 using namespace clang; 44 using namespace ento; 45 46 //===----------------------------------------------------------------------===// 47 // Basic SVal creation. 48 //===----------------------------------------------------------------------===// 49 50 void SValBuilder::anchor() {} 51 52 DefinedOrUnknownSVal SValBuilder::makeZeroVal(QualType type) { 53 if (Loc::isLocType(type)) 54 return makeNull(); 55 56 if (type->isIntegralOrEnumerationType()) 57 return makeIntVal(0, type); 58 59 if (type->isArrayType() || type->isRecordType() || type->isVectorType() || 60 type->isAnyComplexType()) 61 return makeCompoundVal(type, BasicVals.getEmptySValList()); 62 63 // FIXME: Handle floats. 64 return UnknownVal(); 65 } 66 67 NonLoc SValBuilder::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op, 68 const llvm::APSInt& rhs, QualType type) { 69 // The Environment ensures we always get a persistent APSInt in 70 // BasicValueFactory, so we don't need to get the APSInt from 71 // BasicValueFactory again. 72 assert(lhs); 73 assert(!Loc::isLocType(type)); 74 return nonloc::SymbolVal(SymMgr.getSymIntExpr(lhs, op, rhs, type)); 75 } 76 77 NonLoc SValBuilder::makeNonLoc(const llvm::APSInt& lhs, 78 BinaryOperator::Opcode op, const SymExpr *rhs, 79 QualType type) { 80 assert(rhs); 81 assert(!Loc::isLocType(type)); 82 return nonloc::SymbolVal(SymMgr.getIntSymExpr(lhs, op, rhs, type)); 83 } 84 85 NonLoc SValBuilder::makeNonLoc(const SymExpr *lhs, BinaryOperator::Opcode op, 86 const SymExpr *rhs, QualType type) { 87 assert(lhs && rhs); 88 assert(!Loc::isLocType(type)); 89 return nonloc::SymbolVal(SymMgr.getSymSymExpr(lhs, op, rhs, type)); 90 } 91 92 NonLoc SValBuilder::makeNonLoc(const SymExpr *operand, 93 QualType fromTy, QualType toTy) { 94 assert(operand); 95 assert(!Loc::isLocType(toTy)); 96 return nonloc::SymbolVal(SymMgr.getCastSymbol(operand, fromTy, toTy)); 97 } 98 99 SVal SValBuilder::convertToArrayIndex(SVal val) { 100 if (val.isUnknownOrUndef()) 101 return val; 102 103 // Common case: we have an appropriately sized integer. 104 if (Optional<nonloc::ConcreteInt> CI = val.getAs<nonloc::ConcreteInt>()) { 105 const llvm::APSInt& I = CI->getValue(); 106 if (I.getBitWidth() == ArrayIndexWidth && I.isSigned()) 107 return val; 108 } 109 110 return evalCastFromNonLoc(val.castAs<NonLoc>(), ArrayIndexTy); 111 } 112 113 nonloc::ConcreteInt SValBuilder::makeBoolVal(const CXXBoolLiteralExpr *boolean){ 114 return makeTruthVal(boolean->getValue()); 115 } 116 117 DefinedOrUnknownSVal 118 SValBuilder::getRegionValueSymbolVal(const TypedValueRegion *region) { 119 QualType T = region->getValueType(); 120 121 if (T->isNullPtrType()) 122 return makeZeroVal(T); 123 124 if (!SymbolManager::canSymbolicate(T)) 125 return UnknownVal(); 126 127 SymbolRef sym = SymMgr.getRegionValueSymbol(region); 128 129 if (Loc::isLocType(T)) 130 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym)); 131 132 return nonloc::SymbolVal(sym); 133 } 134 135 DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const void *SymbolTag, 136 const Expr *Ex, 137 const LocationContext *LCtx, 138 unsigned Count) { 139 QualType T = Ex->getType(); 140 141 if (T->isNullPtrType()) 142 return makeZeroVal(T); 143 144 // Compute the type of the result. If the expression is not an R-value, the 145 // result should be a location. 146 QualType ExType = Ex->getType(); 147 if (Ex->isGLValue()) 148 T = LCtx->getAnalysisDeclContext()->getASTContext().getPointerType(ExType); 149 150 return conjureSymbolVal(SymbolTag, Ex, LCtx, T, Count); 151 } 152 153 DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const void *symbolTag, 154 const Expr *expr, 155 const LocationContext *LCtx, 156 QualType type, 157 unsigned count) { 158 if (type->isNullPtrType()) 159 return makeZeroVal(type); 160 161 if (!SymbolManager::canSymbolicate(type)) 162 return UnknownVal(); 163 164 SymbolRef sym = SymMgr.conjureSymbol(expr, LCtx, type, count, symbolTag); 165 166 if (Loc::isLocType(type)) 167 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym)); 168 169 return nonloc::SymbolVal(sym); 170 } 171 172 DefinedOrUnknownSVal SValBuilder::conjureSymbolVal(const Stmt *stmt, 173 const LocationContext *LCtx, 174 QualType type, 175 unsigned visitCount) { 176 if (type->isNullPtrType()) 177 return makeZeroVal(type); 178 179 if (!SymbolManager::canSymbolicate(type)) 180 return UnknownVal(); 181 182 SymbolRef sym = SymMgr.conjureSymbol(stmt, LCtx, type, visitCount); 183 184 if (Loc::isLocType(type)) 185 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym)); 186 187 return nonloc::SymbolVal(sym); 188 } 189 190 DefinedOrUnknownSVal 191 SValBuilder::getConjuredHeapSymbolVal(const Expr *E, 192 const LocationContext *LCtx, 193 unsigned VisitCount) { 194 QualType T = E->getType(); 195 assert(Loc::isLocType(T)); 196 assert(SymbolManager::canSymbolicate(T)); 197 if (T->isNullPtrType()) 198 return makeZeroVal(T); 199 200 SymbolRef sym = SymMgr.conjureSymbol(E, LCtx, T, VisitCount); 201 return loc::MemRegionVal(MemMgr.getSymbolicHeapRegion(sym)); 202 } 203 204 DefinedSVal SValBuilder::getMetadataSymbolVal(const void *symbolTag, 205 const MemRegion *region, 206 const Expr *expr, QualType type, 207 const LocationContext *LCtx, 208 unsigned count) { 209 assert(SymbolManager::canSymbolicate(type) && "Invalid metadata symbol type"); 210 211 SymbolRef sym = 212 SymMgr.getMetadataSymbol(region, expr, type, LCtx, count, symbolTag); 213 214 if (Loc::isLocType(type)) 215 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym)); 216 217 return nonloc::SymbolVal(sym); 218 } 219 220 DefinedOrUnknownSVal 221 SValBuilder::getDerivedRegionValueSymbolVal(SymbolRef parentSymbol, 222 const TypedValueRegion *region) { 223 QualType T = region->getValueType(); 224 225 if (T->isNullPtrType()) 226 return makeZeroVal(T); 227 228 if (!SymbolManager::canSymbolicate(T)) 229 return UnknownVal(); 230 231 SymbolRef sym = SymMgr.getDerivedSymbol(parentSymbol, region); 232 233 if (Loc::isLocType(T)) 234 return loc::MemRegionVal(MemMgr.getSymbolicRegion(sym)); 235 236 return nonloc::SymbolVal(sym); 237 } 238 239 DefinedSVal SValBuilder::getMemberPointer(const NamedDecl *ND) { 240 assert(!ND || isa<CXXMethodDecl>(ND) || isa<FieldDecl>(ND) || 241 isa<IndirectFieldDecl>(ND)); 242 243 if (const auto *MD = dyn_cast_or_null<CXXMethodDecl>(ND)) { 244 // Sema treats pointers to static member functions as have function pointer 245 // type, so return a function pointer for the method. 246 // We don't need to play a similar trick for static member fields 247 // because these are represented as plain VarDecls and not FieldDecls 248 // in the AST. 249 if (MD->isStatic()) 250 return getFunctionPointer(MD); 251 } 252 253 return nonloc::PointerToMember(ND); 254 } 255 256 DefinedSVal SValBuilder::getFunctionPointer(const FunctionDecl *func) { 257 return loc::MemRegionVal(MemMgr.getFunctionCodeRegion(func)); 258 } 259 260 DefinedSVal SValBuilder::getBlockPointer(const BlockDecl *block, 261 CanQualType locTy, 262 const LocationContext *locContext, 263 unsigned blockCount) { 264 const BlockCodeRegion *BC = 265 MemMgr.getBlockCodeRegion(block, locTy, locContext->getAnalysisDeclContext()); 266 const BlockDataRegion *BD = MemMgr.getBlockDataRegion(BC, locContext, 267 blockCount); 268 return loc::MemRegionVal(BD); 269 } 270 271 /// Return a memory region for the 'this' object reference. 272 loc::MemRegionVal SValBuilder::getCXXThis(const CXXMethodDecl *D, 273 const StackFrameContext *SFC) { 274 return loc::MemRegionVal( 275 getRegionManager().getCXXThisRegion(D->getThisType(), SFC)); 276 } 277 278 /// Return a memory region for the 'this' object reference. 279 loc::MemRegionVal SValBuilder::getCXXThis(const CXXRecordDecl *D, 280 const StackFrameContext *SFC) { 281 const Type *T = D->getTypeForDecl(); 282 QualType PT = getContext().getPointerType(QualType(T, 0)); 283 return loc::MemRegionVal(getRegionManager().getCXXThisRegion(PT, SFC)); 284 } 285 286 Optional<SVal> SValBuilder::getConstantVal(const Expr *E) { 287 E = E->IgnoreParens(); 288 289 switch (E->getStmtClass()) { 290 // Handle expressions that we treat differently from the AST's constant 291 // evaluator. 292 case Stmt::AddrLabelExprClass: 293 return makeLoc(cast<AddrLabelExpr>(E)); 294 295 case Stmt::CXXScalarValueInitExprClass: 296 case Stmt::ImplicitValueInitExprClass: 297 return makeZeroVal(E->getType()); 298 299 case Stmt::ObjCStringLiteralClass: { 300 const auto *SL = cast<ObjCStringLiteral>(E); 301 return makeLoc(getRegionManager().getObjCStringRegion(SL)); 302 } 303 304 case Stmt::StringLiteralClass: { 305 const auto *SL = cast<StringLiteral>(E); 306 return makeLoc(getRegionManager().getStringRegion(SL)); 307 } 308 309 case Stmt::PredefinedExprClass: { 310 const auto *PE = cast<PredefinedExpr>(E); 311 assert(PE->getFunctionName() && 312 "Since we analyze only instantiated functions, PredefinedExpr " 313 "should have a function name."); 314 return makeLoc(getRegionManager().getStringRegion(PE->getFunctionName())); 315 } 316 317 // Fast-path some expressions to avoid the overhead of going through the AST's 318 // constant evaluator 319 case Stmt::CharacterLiteralClass: { 320 const auto *C = cast<CharacterLiteral>(E); 321 return makeIntVal(C->getValue(), C->getType()); 322 } 323 324 case Stmt::CXXBoolLiteralExprClass: 325 return makeBoolVal(cast<CXXBoolLiteralExpr>(E)); 326 327 case Stmt::TypeTraitExprClass: { 328 const auto *TE = cast<TypeTraitExpr>(E); 329 return makeTruthVal(TE->getValue(), TE->getType()); 330 } 331 332 case Stmt::IntegerLiteralClass: 333 return makeIntVal(cast<IntegerLiteral>(E)); 334 335 case Stmt::ObjCBoolLiteralExprClass: 336 return makeBoolVal(cast<ObjCBoolLiteralExpr>(E)); 337 338 case Stmt::CXXNullPtrLiteralExprClass: 339 return makeNull(); 340 341 case Stmt::CStyleCastExprClass: 342 case Stmt::CXXFunctionalCastExprClass: 343 case Stmt::CXXConstCastExprClass: 344 case Stmt::CXXReinterpretCastExprClass: 345 case Stmt::CXXStaticCastExprClass: 346 case Stmt::ImplicitCastExprClass: { 347 const auto *CE = cast<CastExpr>(E); 348 switch (CE->getCastKind()) { 349 default: 350 break; 351 case CK_ArrayToPointerDecay: 352 case CK_IntegralToPointer: 353 case CK_NoOp: 354 case CK_BitCast: { 355 const Expr *SE = CE->getSubExpr(); 356 Optional<SVal> Val = getConstantVal(SE); 357 if (!Val) 358 return None; 359 return evalCast(*Val, CE->getType(), SE->getType()); 360 } 361 } 362 // FALLTHROUGH 363 LLVM_FALLTHROUGH; 364 } 365 366 // If we don't have a special case, fall back to the AST's constant evaluator. 367 default: { 368 // Don't try to come up with a value for materialized temporaries. 369 if (E->isGLValue()) 370 return None; 371 372 ASTContext &Ctx = getContext(); 373 Expr::EvalResult Result; 374 if (E->EvaluateAsInt(Result, Ctx)) 375 return makeIntVal(Result.Val.getInt()); 376 377 if (Loc::isLocType(E->getType())) 378 if (E->isNullPointerConstant(Ctx, Expr::NPC_ValueDependentIsNotNull)) 379 return makeNull(); 380 381 return None; 382 } 383 } 384 } 385 386 SVal SValBuilder::makeSymExprValNN(BinaryOperator::Opcode Op, 387 NonLoc LHS, NonLoc RHS, 388 QualType ResultTy) { 389 SymbolRef symLHS = LHS.getAsSymbol(); 390 SymbolRef symRHS = RHS.getAsSymbol(); 391 392 // TODO: When the Max Complexity is reached, we should conjure a symbol 393 // instead of generating an Unknown value and propagate the taint info to it. 394 const unsigned MaxComp = StateMgr.getOwningEngine() 395 .getAnalysisManager() 396 .options.MaxSymbolComplexity; 397 398 if (symLHS && symRHS && 399 (symLHS->computeComplexity() + symRHS->computeComplexity()) < MaxComp) 400 return makeNonLoc(symLHS, Op, symRHS, ResultTy); 401 402 if (symLHS && symLHS->computeComplexity() < MaxComp) 403 if (Optional<nonloc::ConcreteInt> rInt = RHS.getAs<nonloc::ConcreteInt>()) 404 return makeNonLoc(symLHS, Op, rInt->getValue(), ResultTy); 405 406 if (symRHS && symRHS->computeComplexity() < MaxComp) 407 if (Optional<nonloc::ConcreteInt> lInt = LHS.getAs<nonloc::ConcreteInt>()) 408 return makeNonLoc(lInt->getValue(), Op, symRHS, ResultTy); 409 410 return UnknownVal(); 411 } 412 413 SVal SValBuilder::evalBinOp(ProgramStateRef state, BinaryOperator::Opcode op, 414 SVal lhs, SVal rhs, QualType type) { 415 if (lhs.isUndef() || rhs.isUndef()) 416 return UndefinedVal(); 417 418 if (lhs.isUnknown() || rhs.isUnknown()) 419 return UnknownVal(); 420 421 if (lhs.getAs<nonloc::LazyCompoundVal>() || 422 rhs.getAs<nonloc::LazyCompoundVal>()) { 423 return UnknownVal(); 424 } 425 426 if (Optional<Loc> LV = lhs.getAs<Loc>()) { 427 if (Optional<Loc> RV = rhs.getAs<Loc>()) 428 return evalBinOpLL(state, op, *LV, *RV, type); 429 430 return evalBinOpLN(state, op, *LV, rhs.castAs<NonLoc>(), type); 431 } 432 433 if (Optional<Loc> RV = rhs.getAs<Loc>()) { 434 // Support pointer arithmetic where the addend is on the left 435 // and the pointer on the right. 436 assert(op == BO_Add); 437 438 // Commute the operands. 439 return evalBinOpLN(state, op, *RV, lhs.castAs<NonLoc>(), type); 440 } 441 442 return evalBinOpNN(state, op, lhs.castAs<NonLoc>(), rhs.castAs<NonLoc>(), 443 type); 444 } 445 446 ConditionTruthVal SValBuilder::areEqual(ProgramStateRef state, SVal lhs, 447 SVal rhs) { 448 return state->isNonNull(evalEQ(state, lhs, rhs)); 449 } 450 451 SVal SValBuilder::evalEQ(ProgramStateRef state, SVal lhs, SVal rhs) { 452 return evalBinOp(state, BO_EQ, lhs, rhs, getConditionType()); 453 } 454 455 DefinedOrUnknownSVal SValBuilder::evalEQ(ProgramStateRef state, 456 DefinedOrUnknownSVal lhs, 457 DefinedOrUnknownSVal rhs) { 458 return evalEQ(state, static_cast<SVal>(lhs), static_cast<SVal>(rhs)) 459 .castAs<DefinedOrUnknownSVal>(); 460 } 461 462 /// Recursively check if the pointer types are equal modulo const, volatile, 463 /// and restrict qualifiers. Also, assume that all types are similar to 'void'. 464 /// Assumes the input types are canonical. 465 static bool shouldBeModeledWithNoOp(ASTContext &Context, QualType ToTy, 466 QualType FromTy) { 467 while (Context.UnwrapSimilarTypes(ToTy, FromTy)) { 468 Qualifiers Quals1, Quals2; 469 ToTy = Context.getUnqualifiedArrayType(ToTy, Quals1); 470 FromTy = Context.getUnqualifiedArrayType(FromTy, Quals2); 471 472 // Make sure that non-cvr-qualifiers the other qualifiers (e.g., address 473 // spaces) are identical. 474 Quals1.removeCVRQualifiers(); 475 Quals2.removeCVRQualifiers(); 476 if (Quals1 != Quals2) 477 return false; 478 } 479 480 // If we are casting to void, the 'From' value can be used to represent the 481 // 'To' value. 482 // 483 // FIXME: Doing this after unwrapping the types doesn't make any sense. A 484 // cast from 'int**' to 'void**' is not special in the way that a cast from 485 // 'int*' to 'void*' is. 486 if (ToTy->isVoidType()) 487 return true; 488 489 if (ToTy != FromTy) 490 return false; 491 492 return true; 493 } 494 495 // Handles casts of type CK_IntegralCast. 496 // At the moment, this function will redirect to evalCast, except when the range 497 // of the original value is known to be greater than the max of the target type. 498 SVal SValBuilder::evalIntegralCast(ProgramStateRef state, SVal val, 499 QualType castTy, QualType originalTy) { 500 // No truncations if target type is big enough. 501 if (getContext().getTypeSize(castTy) >= getContext().getTypeSize(originalTy)) 502 return evalCast(val, castTy, originalTy); 503 504 SymbolRef se = val.getAsSymbol(); 505 if (!se) // Let evalCast handle non symbolic expressions. 506 return evalCast(val, castTy, originalTy); 507 508 // Find the maximum value of the target type. 509 APSIntType ToType(getContext().getTypeSize(castTy), 510 castTy->isUnsignedIntegerType()); 511 llvm::APSInt ToTypeMax = ToType.getMaxValue(); 512 NonLoc ToTypeMaxVal = 513 makeIntVal(ToTypeMax.isUnsigned() ? ToTypeMax.getZExtValue() 514 : ToTypeMax.getSExtValue(), 515 castTy) 516 .castAs<NonLoc>(); 517 // Check the range of the symbol being casted against the maximum value of the 518 // target type. 519 NonLoc FromVal = val.castAs<NonLoc>(); 520 QualType CmpTy = getConditionType(); 521 NonLoc CompVal = 522 evalBinOpNN(state, BO_LE, FromVal, ToTypeMaxVal, CmpTy).castAs<NonLoc>(); 523 ProgramStateRef IsNotTruncated, IsTruncated; 524 std::tie(IsNotTruncated, IsTruncated) = state->assume(CompVal); 525 if (!IsNotTruncated && IsTruncated) { 526 // Symbol is truncated so we evaluate it as a cast. 527 NonLoc CastVal = makeNonLoc(se, originalTy, castTy); 528 return CastVal; 529 } 530 return evalCast(val, castTy, originalTy); 531 } 532 533 // FIXME: should rewrite according to the cast kind. 534 SVal SValBuilder::evalCast(SVal val, QualType castTy, QualType originalTy) { 535 castTy = Context.getCanonicalType(castTy); 536 originalTy = Context.getCanonicalType(originalTy); 537 if (val.isUnknownOrUndef() || castTy == originalTy) 538 return val; 539 540 if (castTy->isBooleanType()) { 541 if (val.isUnknownOrUndef()) 542 return val; 543 if (val.isConstant()) 544 return makeTruthVal(!val.isZeroConstant(), castTy); 545 if (!Loc::isLocType(originalTy) && 546 !originalTy->isIntegralOrEnumerationType() && 547 !originalTy->isMemberPointerType()) 548 return UnknownVal(); 549 if (SymbolRef Sym = val.getAsSymbol(true)) { 550 BasicValueFactory &BVF = getBasicValueFactory(); 551 // FIXME: If we had a state here, we could see if the symbol is known to 552 // be zero, but we don't. 553 return makeNonLoc(Sym, BO_NE, BVF.getValue(0, Sym->getType()), castTy); 554 } 555 // Loc values are not always true, they could be weakly linked functions. 556 if (Optional<Loc> L = val.getAs<Loc>()) 557 return evalCastFromLoc(*L, castTy); 558 559 Loc L = val.castAs<nonloc::LocAsInteger>().getLoc(); 560 return evalCastFromLoc(L, castTy); 561 } 562 563 // For const casts, casts to void, just propagate the value. 564 if (!castTy->isVariableArrayType() && !originalTy->isVariableArrayType()) 565 if (shouldBeModeledWithNoOp(Context, Context.getPointerType(castTy), 566 Context.getPointerType(originalTy))) 567 return val; 568 569 // Check for casts from pointers to integers. 570 if (castTy->isIntegralOrEnumerationType() && Loc::isLocType(originalTy)) 571 return evalCastFromLoc(val.castAs<Loc>(), castTy); 572 573 // Check for casts from integers to pointers. 574 if (Loc::isLocType(castTy) && originalTy->isIntegralOrEnumerationType()) { 575 if (Optional<nonloc::LocAsInteger> LV = val.getAs<nonloc::LocAsInteger>()) { 576 if (const MemRegion *R = LV->getLoc().getAsRegion()) { 577 StoreManager &storeMgr = StateMgr.getStoreManager(); 578 R = storeMgr.castRegion(R, castTy); 579 return R ? SVal(loc::MemRegionVal(R)) : UnknownVal(); 580 } 581 return LV->getLoc(); 582 } 583 return dispatchCast(val, castTy); 584 } 585 586 // Just pass through function and block pointers. 587 if (originalTy->isBlockPointerType() || originalTy->isFunctionPointerType()) { 588 assert(Loc::isLocType(castTy)); 589 return val; 590 } 591 592 // Check for casts from array type to another type. 593 if (const auto *arrayT = 594 dyn_cast<ArrayType>(originalTy.getCanonicalType())) { 595 // We will always decay to a pointer. 596 QualType elemTy = arrayT->getElementType(); 597 val = StateMgr.ArrayToPointer(val.castAs<Loc>(), elemTy); 598 599 // Are we casting from an array to a pointer? If so just pass on 600 // the decayed value. 601 if (castTy->isPointerType() || castTy->isReferenceType()) 602 return val; 603 604 // Are we casting from an array to an integer? If so, cast the decayed 605 // pointer value to an integer. 606 assert(castTy->isIntegralOrEnumerationType()); 607 608 // FIXME: Keep these here for now in case we decide soon that we 609 // need the original decayed type. 610 // QualType elemTy = cast<ArrayType>(originalTy)->getElementType(); 611 // QualType pointerTy = C.getPointerType(elemTy); 612 return evalCastFromLoc(val.castAs<Loc>(), castTy); 613 } 614 615 // Check for casts from a region to a specific type. 616 if (const MemRegion *R = val.getAsRegion()) { 617 // Handle other casts of locations to integers. 618 if (castTy->isIntegralOrEnumerationType()) 619 return evalCastFromLoc(loc::MemRegionVal(R), castTy); 620 621 // FIXME: We should handle the case where we strip off view layers to get 622 // to a desugared type. 623 if (!Loc::isLocType(castTy)) { 624 // FIXME: There can be gross cases where one casts the result of a function 625 // (that returns a pointer) to some other value that happens to fit 626 // within that pointer value. We currently have no good way to 627 // model such operations. When this happens, the underlying operation 628 // is that the caller is reasoning about bits. Conceptually we are 629 // layering a "view" of a location on top of those bits. Perhaps 630 // we need to be more lazy about mutual possible views, even on an 631 // SVal? This may be necessary for bit-level reasoning as well. 632 return UnknownVal(); 633 } 634 635 // We get a symbolic function pointer for a dereference of a function 636 // pointer, but it is of function type. Example: 637 638 // struct FPRec { 639 // void (*my_func)(int * x); 640 // }; 641 // 642 // int bar(int x); 643 // 644 // int f1_a(struct FPRec* foo) { 645 // int x; 646 // (*foo->my_func)(&x); 647 // return bar(x)+1; // no-warning 648 // } 649 650 assert(Loc::isLocType(originalTy) || originalTy->isFunctionType() || 651 originalTy->isBlockPointerType() || castTy->isReferenceType()); 652 653 StoreManager &storeMgr = StateMgr.getStoreManager(); 654 655 // Delegate to store manager to get the result of casting a region to a 656 // different type. If the MemRegion* returned is NULL, this expression 657 // Evaluates to UnknownVal. 658 R = storeMgr.castRegion(R, castTy); 659 return R ? SVal(loc::MemRegionVal(R)) : UnknownVal(); 660 } 661 662 return dispatchCast(val, castTy); 663 } 664