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