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