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 // FIXME: should rewrite according to the cast kind. 427 SVal SValBuilder::evalCast(SVal val, QualType castTy, QualType originalTy) { 428 castTy = Context.getCanonicalType(castTy); 429 originalTy = Context.getCanonicalType(originalTy); 430 if (val.isUnknownOrUndef() || castTy == originalTy) 431 return val; 432 433 if (castTy->isBooleanType()) { 434 if (val.isUnknownOrUndef()) 435 return val; 436 if (val.isConstant()) 437 return makeTruthVal(!val.isZeroConstant(), castTy); 438 if (!Loc::isLocType(originalTy) && 439 !originalTy->isIntegralOrEnumerationType() && 440 !originalTy->isMemberPointerType()) 441 return UnknownVal(); 442 if (SymbolRef Sym = val.getAsSymbol(true)) { 443 BasicValueFactory &BVF = getBasicValueFactory(); 444 // FIXME: If we had a state here, we could see if the symbol is known to 445 // be zero, but we don't. 446 return makeNonLoc(Sym, BO_NE, BVF.getValue(0, Sym->getType()), castTy); 447 } 448 // Loc values are not always true, they could be weakly linked functions. 449 if (Optional<Loc> L = val.getAs<Loc>()) 450 return evalCastFromLoc(*L, castTy); 451 452 Loc L = val.castAs<nonloc::LocAsInteger>().getLoc(); 453 return evalCastFromLoc(L, castTy); 454 } 455 456 // For const casts, casts to void, just propagate the value. 457 if (!castTy->isVariableArrayType() && !originalTy->isVariableArrayType()) 458 if (shouldBeModeledWithNoOp(Context, Context.getPointerType(castTy), 459 Context.getPointerType(originalTy))) 460 return val; 461 462 // Check for casts from pointers to integers. 463 if (castTy->isIntegralOrEnumerationType() && Loc::isLocType(originalTy)) 464 return evalCastFromLoc(val.castAs<Loc>(), castTy); 465 466 // Check for casts from integers to pointers. 467 if (Loc::isLocType(castTy) && originalTy->isIntegralOrEnumerationType()) { 468 if (Optional<nonloc::LocAsInteger> LV = val.getAs<nonloc::LocAsInteger>()) { 469 if (const MemRegion *R = LV->getLoc().getAsRegion()) { 470 StoreManager &storeMgr = StateMgr.getStoreManager(); 471 R = storeMgr.castRegion(R, castTy); 472 return R ? SVal(loc::MemRegionVal(R)) : UnknownVal(); 473 } 474 return LV->getLoc(); 475 } 476 return dispatchCast(val, castTy); 477 } 478 479 // Just pass through function and block pointers. 480 if (originalTy->isBlockPointerType() || originalTy->isFunctionPointerType()) { 481 assert(Loc::isLocType(castTy)); 482 return val; 483 } 484 485 // Check for casts from array type to another type. 486 if (const ArrayType *arrayT = 487 dyn_cast<ArrayType>(originalTy.getCanonicalType())) { 488 // We will always decay to a pointer. 489 QualType elemTy = arrayT->getElementType(); 490 val = StateMgr.ArrayToPointer(val.castAs<Loc>(), elemTy); 491 492 // Are we casting from an array to a pointer? If so just pass on 493 // the decayed value. 494 if (castTy->isPointerType() || castTy->isReferenceType()) 495 return val; 496 497 // Are we casting from an array to an integer? If so, cast the decayed 498 // pointer value to an integer. 499 assert(castTy->isIntegralOrEnumerationType()); 500 501 // FIXME: Keep these here for now in case we decide soon that we 502 // need the original decayed type. 503 // QualType elemTy = cast<ArrayType>(originalTy)->getElementType(); 504 // QualType pointerTy = C.getPointerType(elemTy); 505 return evalCastFromLoc(val.castAs<Loc>(), castTy); 506 } 507 508 // Check for casts from a region to a specific type. 509 if (const MemRegion *R = val.getAsRegion()) { 510 // Handle other casts of locations to integers. 511 if (castTy->isIntegralOrEnumerationType()) 512 return evalCastFromLoc(loc::MemRegionVal(R), castTy); 513 514 // FIXME: We should handle the case where we strip off view layers to get 515 // to a desugared type. 516 if (!Loc::isLocType(castTy)) { 517 // FIXME: There can be gross cases where one casts the result of a function 518 // (that returns a pointer) to some other value that happens to fit 519 // within that pointer value. We currently have no good way to 520 // model such operations. When this happens, the underlying operation 521 // is that the caller is reasoning about bits. Conceptually we are 522 // layering a "view" of a location on top of those bits. Perhaps 523 // we need to be more lazy about mutual possible views, even on an 524 // SVal? This may be necessary for bit-level reasoning as well. 525 return UnknownVal(); 526 } 527 528 // We get a symbolic function pointer for a dereference of a function 529 // pointer, but it is of function type. Example: 530 531 // struct FPRec { 532 // void (*my_func)(int * x); 533 // }; 534 // 535 // int bar(int x); 536 // 537 // int f1_a(struct FPRec* foo) { 538 // int x; 539 // (*foo->my_func)(&x); 540 // return bar(x)+1; // no-warning 541 // } 542 543 assert(Loc::isLocType(originalTy) || originalTy->isFunctionType() || 544 originalTy->isBlockPointerType() || castTy->isReferenceType()); 545 546 StoreManager &storeMgr = StateMgr.getStoreManager(); 547 548 // Delegate to store manager to get the result of casting a region to a 549 // different type. If the MemRegion* returned is NULL, this expression 550 // Evaluates to UnknownVal. 551 R = storeMgr.castRegion(R, castTy); 552 return R ? SVal(loc::MemRegionVal(R)) : UnknownVal(); 553 } 554 555 return dispatchCast(val, castTy); 556 } 557