ExprEngineC.cpp revision a2c8d2edfff1573450c6feba876830dd746ffaad
1//=-- ExprEngineC.cpp - ExprEngine support for C expressions ----*- 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 ExprEngine's support for C expressions. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/StaticAnalyzer/Core/CheckerManager.h" 15#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 16 17using namespace clang; 18using namespace ento; 19using llvm::APSInt; 20 21void ExprEngine::VisitBinaryOperator(const BinaryOperator* B, 22 ExplodedNode *Pred, 23 ExplodedNodeSet &Dst) { 24 25 Expr *LHS = B->getLHS()->IgnoreParens(); 26 Expr *RHS = B->getRHS()->IgnoreParens(); 27 28 // FIXME: Prechecks eventually go in ::Visit(). 29 ExplodedNodeSet CheckedSet; 30 ExplodedNodeSet Tmp2; 31 getCheckerManager().runCheckersForPreStmt(CheckedSet, Pred, B, *this); 32 33 // With both the LHS and RHS evaluated, process the operation itself. 34 for (ExplodedNodeSet::iterator it=CheckedSet.begin(), ei=CheckedSet.end(); 35 it != ei; ++it) { 36 37 ProgramStateRef state = (*it)->getState(); 38 const LocationContext *LCtx = (*it)->getLocationContext(); 39 SVal LeftV = state->getSVal(LHS, LCtx); 40 SVal RightV = state->getSVal(RHS, LCtx); 41 42 BinaryOperator::Opcode Op = B->getOpcode(); 43 44 if (Op == BO_Assign) { 45 // EXPERIMENTAL: "Conjured" symbols. 46 // FIXME: Handle structs. 47 if (RightV.isUnknown()) { 48 unsigned Count = currentBuilderContext->getCurrentBlockCount(); 49 RightV = svalBuilder.getConjuredSymbolVal(NULL, B->getRHS(), LCtx, Count); 50 } 51 // Simulate the effects of a "store": bind the value of the RHS 52 // to the L-Value represented by the LHS. 53 SVal ExprVal = B->isLValue() ? LeftV : RightV; 54 evalStore(Tmp2, B, LHS, *it, state->BindExpr(B, LCtx, ExprVal), 55 LeftV, RightV); 56 continue; 57 } 58 59 if (!B->isAssignmentOp()) { 60 StmtNodeBuilder Bldr(*it, Tmp2, *currentBuilderContext); 61 // Process non-assignments except commas or short-circuited 62 // logical expressions (LAnd and LOr). 63 SVal Result = evalBinOp(state, Op, LeftV, RightV, B->getType()); 64 if (Result.isUnknown()) { 65 Bldr.generateNode(B, *it, state); 66 continue; 67 } 68 69 state = state->BindExpr(B, LCtx, Result); 70 Bldr.generateNode(B, *it, state); 71 continue; 72 } 73 74 assert (B->isCompoundAssignmentOp()); 75 76 switch (Op) { 77 default: 78 llvm_unreachable("Invalid opcode for compound assignment."); 79 case BO_MulAssign: Op = BO_Mul; break; 80 case BO_DivAssign: Op = BO_Div; break; 81 case BO_RemAssign: Op = BO_Rem; break; 82 case BO_AddAssign: Op = BO_Add; break; 83 case BO_SubAssign: Op = BO_Sub; break; 84 case BO_ShlAssign: Op = BO_Shl; break; 85 case BO_ShrAssign: Op = BO_Shr; break; 86 case BO_AndAssign: Op = BO_And; break; 87 case BO_XorAssign: Op = BO_Xor; break; 88 case BO_OrAssign: Op = BO_Or; break; 89 } 90 91 // Perform a load (the LHS). This performs the checks for 92 // null dereferences, and so on. 93 ExplodedNodeSet Tmp; 94 SVal location = LeftV; 95 evalLoad(Tmp, B, LHS, *it, state, location); 96 97 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; 98 ++I) { 99 100 state = (*I)->getState(); 101 const LocationContext *LCtx = (*I)->getLocationContext(); 102 SVal V = state->getSVal(LHS, LCtx); 103 104 // Get the computation type. 105 QualType CTy = 106 cast<CompoundAssignOperator>(B)->getComputationResultType(); 107 CTy = getContext().getCanonicalType(CTy); 108 109 QualType CLHSTy = 110 cast<CompoundAssignOperator>(B)->getComputationLHSType(); 111 CLHSTy = getContext().getCanonicalType(CLHSTy); 112 113 QualType LTy = getContext().getCanonicalType(LHS->getType()); 114 115 // Promote LHS. 116 V = svalBuilder.evalCast(V, CLHSTy, LTy); 117 118 // Compute the result of the operation. 119 SVal Result = svalBuilder.evalCast(evalBinOp(state, Op, V, RightV, CTy), 120 B->getType(), CTy); 121 122 // EXPERIMENTAL: "Conjured" symbols. 123 // FIXME: Handle structs. 124 125 SVal LHSVal; 126 127 if (Result.isUnknown()) { 128 129 unsigned Count = currentBuilderContext->getCurrentBlockCount(); 130 131 // The symbolic value is actually for the type of the left-hand side 132 // expression, not the computation type, as this is the value the 133 // LValue on the LHS will bind to. 134 LHSVal = svalBuilder.getConjuredSymbolVal(NULL, B->getRHS(), LCtx, 135 LTy, Count); 136 137 // However, we need to convert the symbol to the computation type. 138 Result = svalBuilder.evalCast(LHSVal, CTy, LTy); 139 } 140 else { 141 // The left-hand side may bind to a different value then the 142 // computation type. 143 LHSVal = svalBuilder.evalCast(Result, LTy, CTy); 144 } 145 146 // In C++, assignment and compound assignment operators return an 147 // lvalue. 148 if (B->isLValue()) 149 state = state->BindExpr(B, LCtx, location); 150 else 151 state = state->BindExpr(B, LCtx, Result); 152 153 evalStore(Tmp2, B, LHS, *I, state, location, LHSVal); 154 } 155 } 156 157 // FIXME: postvisits eventually go in ::Visit() 158 getCheckerManager().runCheckersForPostStmt(Dst, Tmp2, B, *this); 159} 160 161void ExprEngine::VisitBlockExpr(const BlockExpr *BE, ExplodedNode *Pred, 162 ExplodedNodeSet &Dst) { 163 164 CanQualType T = getContext().getCanonicalType(BE->getType()); 165 SVal V = svalBuilder.getBlockPointer(BE->getBlockDecl(), T, 166 Pred->getLocationContext()); 167 168 ExplodedNodeSet Tmp; 169 StmtNodeBuilder Bldr(Pred, Tmp, *currentBuilderContext); 170 Bldr.generateNode(BE, Pred, 171 Pred->getState()->BindExpr(BE, Pred->getLocationContext(), 172 V), 173 false, 0, 174 ProgramPoint::PostLValueKind); 175 176 // FIXME: Move all post/pre visits to ::Visit(). 177 getCheckerManager().runCheckersForPostStmt(Dst, Tmp, BE, *this); 178} 179 180void ExprEngine::VisitCast(const CastExpr *CastE, const Expr *Ex, 181 ExplodedNode *Pred, ExplodedNodeSet &Dst) { 182 183 ExplodedNodeSet dstPreStmt; 184 getCheckerManager().runCheckersForPreStmt(dstPreStmt, Pred, CastE, *this); 185 186 if (CastE->getCastKind() == CK_LValueToRValue) { 187 for (ExplodedNodeSet::iterator I = dstPreStmt.begin(), E = dstPreStmt.end(); 188 I!=E; ++I) { 189 ExplodedNode *subExprNode = *I; 190 ProgramStateRef state = subExprNode->getState(); 191 const LocationContext *LCtx = subExprNode->getLocationContext(); 192 evalLoad(Dst, CastE, CastE, subExprNode, state, state->getSVal(Ex, LCtx)); 193 } 194 return; 195 } 196 197 // All other casts. 198 QualType T = CastE->getType(); 199 QualType ExTy = Ex->getType(); 200 201 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE)) 202 T = ExCast->getTypeAsWritten(); 203 204 StmtNodeBuilder Bldr(dstPreStmt, Dst, *currentBuilderContext); 205 for (ExplodedNodeSet::iterator I = dstPreStmt.begin(), E = dstPreStmt.end(); 206 I != E; ++I) { 207 208 Pred = *I; 209 210 switch (CastE->getCastKind()) { 211 case CK_LValueToRValue: 212 llvm_unreachable("LValueToRValue casts handled earlier."); 213 case CK_ToVoid: 214 continue; 215 // The analyzer doesn't do anything special with these casts, 216 // since it understands retain/release semantics already. 217 case CK_ARCProduceObject: 218 case CK_ARCConsumeObject: 219 case CK_ARCReclaimReturnedObject: 220 case CK_ARCExtendBlockObject: // Fall-through. 221 case CK_CopyAndAutoreleaseBlockObject: 222 // The analyser can ignore atomic casts for now, although some future 223 // checkers may want to make certain that you're not modifying the same 224 // value through atomic and nonatomic pointers. 225 case CK_AtomicToNonAtomic: 226 case CK_NonAtomicToAtomic: 227 // True no-ops. 228 case CK_NoOp: 229 case CK_FunctionToPointerDecay: { 230 // Copy the SVal of Ex to CastE. 231 ProgramStateRef state = Pred->getState(); 232 const LocationContext *LCtx = Pred->getLocationContext(); 233 SVal V = state->getSVal(Ex, LCtx); 234 state = state->BindExpr(CastE, LCtx, V); 235 Bldr.generateNode(CastE, Pred, state); 236 continue; 237 } 238 case CK_Dependent: 239 case CK_ArrayToPointerDecay: 240 case CK_BitCast: 241 case CK_LValueBitCast: 242 case CK_IntegralCast: 243 case CK_NullToPointer: 244 case CK_IntegralToPointer: 245 case CK_PointerToIntegral: 246 case CK_PointerToBoolean: 247 case CK_IntegralToBoolean: 248 case CK_IntegralToFloating: 249 case CK_FloatingToIntegral: 250 case CK_FloatingToBoolean: 251 case CK_FloatingCast: 252 case CK_FloatingRealToComplex: 253 case CK_FloatingComplexToReal: 254 case CK_FloatingComplexToBoolean: 255 case CK_FloatingComplexCast: 256 case CK_FloatingComplexToIntegralComplex: 257 case CK_IntegralRealToComplex: 258 case CK_IntegralComplexToReal: 259 case CK_IntegralComplexToBoolean: 260 case CK_IntegralComplexCast: 261 case CK_IntegralComplexToFloatingComplex: 262 case CK_CPointerToObjCPointerCast: 263 case CK_BlockPointerToObjCPointerCast: 264 case CK_AnyPointerToBlockPointerCast: 265 case CK_ObjCObjectLValueCast: { 266 // Delegate to SValBuilder to process. 267 ProgramStateRef state = Pred->getState(); 268 const LocationContext *LCtx = Pred->getLocationContext(); 269 SVal V = state->getSVal(Ex, LCtx); 270 V = svalBuilder.evalCast(V, T, ExTy); 271 state = state->BindExpr(CastE, LCtx, V); 272 Bldr.generateNode(CastE, Pred, state); 273 continue; 274 } 275 case CK_DerivedToBase: 276 case CK_UncheckedDerivedToBase: { 277 // For DerivedToBase cast, delegate to the store manager. 278 ProgramStateRef state = Pred->getState(); 279 const LocationContext *LCtx = Pred->getLocationContext(); 280 SVal val = state->getSVal(Ex, LCtx); 281 val = getStoreManager().evalDerivedToBase(val, T); 282 state = state->BindExpr(CastE, LCtx, val); 283 Bldr.generateNode(CastE, Pred, state); 284 continue; 285 } 286 // Handle C++ dyn_cast. 287 case CK_Dynamic: { 288 ProgramStateRef state = Pred->getState(); 289 const LocationContext *LCtx = Pred->getLocationContext(); 290 SVal val = state->getSVal(Ex, LCtx); 291 292 // Compute the type of the result. 293 QualType resultType = CastE->getType(); 294 if (CastE->isLValue()) 295 resultType = getContext().getPointerType(resultType); 296 297 bool Failed = false; 298 299 // Check if the value being cast evaluates to 0. 300 if (val.isZeroConstant()) 301 Failed = true; 302 // Else, evaluate the cast. 303 else 304 val = getStoreManager().evalDynamicCast(val, T, Failed); 305 306 if (Failed) { 307 if (T->isReferenceType()) { 308 // A bad_cast exception is thrown if input value is a reference. 309 // Currently, we model this, by generating a sink. 310 Bldr.generateNode(CastE, Pred, state, true); 311 continue; 312 } else { 313 // If the cast fails on a pointer, conjure symbol constrained to 0. 314 DefinedOrUnknownSVal NewSym = svalBuilder.getConjuredSymbolVal(NULL, 315 CastE, LCtx, resultType, 316 currentBuilderContext->getCurrentBlockCount()); 317 DefinedOrUnknownSVal Constraint = svalBuilder.evalEQ(state, 318 NewSym, svalBuilder.makeZeroVal(resultType)); 319 state = state->assume(Constraint, true); 320 state = state->BindExpr(CastE, LCtx, NewSym); 321 } 322 } else { 323 // If we don't know if the cast succeeded, conjure a new symbol. 324 if (val.isUnknown()) { 325 DefinedOrUnknownSVal NewSym = svalBuilder.getConjuredSymbolVal(NULL, 326 CastE, LCtx, resultType, 327 currentBuilderContext->getCurrentBlockCount()); 328 state = state->BindExpr(CastE, LCtx, NewSym); 329 } else 330 // Else, bind to the derived region value. 331 state = state->BindExpr(CastE, LCtx, val); 332 } 333 Bldr.generateNode(CastE, Pred, state); 334 continue; 335 } 336 // Various C++ casts that are not handled yet. 337 case CK_ToUnion: 338 case CK_BaseToDerived: 339 case CK_NullToMemberPointer: 340 case CK_BaseToDerivedMemberPointer: 341 case CK_DerivedToBaseMemberPointer: 342 case CK_ReinterpretMemberPointer: 343 case CK_UserDefinedConversion: 344 case CK_ConstructorConversion: 345 case CK_VectorSplat: 346 case CK_MemberPointerToBoolean: { 347 // Recover some path-sensitivty by conjuring a new value. 348 QualType resultType = CastE->getType(); 349 if (CastE->isLValue()) 350 resultType = getContext().getPointerType(resultType); 351 const LocationContext *LCtx = Pred->getLocationContext(); 352 SVal result = svalBuilder.getConjuredSymbolVal(NULL, CastE, LCtx, 353 resultType, currentBuilderContext->getCurrentBlockCount()); 354 ProgramStateRef state = Pred->getState()->BindExpr(CastE, LCtx, 355 result); 356 Bldr.generateNode(CastE, Pred, state); 357 continue; 358 } 359 } 360 } 361} 362 363void ExprEngine::VisitCompoundLiteralExpr(const CompoundLiteralExpr *CL, 364 ExplodedNode *Pred, 365 ExplodedNodeSet &Dst) { 366 StmtNodeBuilder B(Pred, Dst, *currentBuilderContext); 367 368 const InitListExpr *ILE 369 = cast<InitListExpr>(CL->getInitializer()->IgnoreParens()); 370 371 ProgramStateRef state = Pred->getState(); 372 SVal ILV = state->getSVal(ILE, Pred->getLocationContext()); 373 const LocationContext *LC = Pred->getLocationContext(); 374 state = state->bindCompoundLiteral(CL, LC, ILV); 375 376 if (CL->isLValue()) 377 B.generateNode(CL, Pred, state->BindExpr(CL, LC, state->getLValue(CL, LC))); 378 else 379 B.generateNode(CL, Pred, state->BindExpr(CL, LC, ILV)); 380} 381 382void ExprEngine::VisitDeclStmt(const DeclStmt *DS, ExplodedNode *Pred, 383 ExplodedNodeSet &Dst) { 384 385 // FIXME: static variables may have an initializer, but the second 386 // time a function is called those values may not be current. 387 // This may need to be reflected in the CFG. 388 389 // Assumption: The CFG has one DeclStmt per Decl. 390 const Decl *D = *DS->decl_begin(); 391 392 if (!D || !isa<VarDecl>(D)) { 393 //TODO:AZ: remove explicit insertion after refactoring is done. 394 Dst.insert(Pred); 395 return; 396 } 397 398 // FIXME: all pre/post visits should eventually be handled by ::Visit(). 399 ExplodedNodeSet dstPreVisit; 400 getCheckerManager().runCheckersForPreStmt(dstPreVisit, Pred, DS, *this); 401 402 StmtNodeBuilder B(dstPreVisit, Dst, *currentBuilderContext); 403 const VarDecl *VD = dyn_cast<VarDecl>(D); 404 for (ExplodedNodeSet::iterator I = dstPreVisit.begin(), E = dstPreVisit.end(); 405 I!=E; ++I) { 406 ExplodedNode *N = *I; 407 ProgramStateRef state = N->getState(); 408 409 // Decls without InitExpr are not initialized explicitly. 410 const LocationContext *LC = N->getLocationContext(); 411 412 if (const Expr *InitEx = VD->getInit()) { 413 SVal InitVal = state->getSVal(InitEx, Pred->getLocationContext()); 414 415 // We bound the temp obj region to the CXXConstructExpr. Now recover 416 // the lazy compound value when the variable is not a reference. 417 if (AMgr.getLangOpts().CPlusPlus && VD->getType()->isRecordType() && 418 !VD->getType()->isReferenceType() && isa<loc::MemRegionVal>(InitVal)){ 419 InitVal = state->getSVal(cast<loc::MemRegionVal>(InitVal).getRegion()); 420 assert(isa<nonloc::LazyCompoundVal>(InitVal)); 421 } 422 423 // Recover some path-sensitivity if a scalar value evaluated to 424 // UnknownVal. 425 if (InitVal.isUnknown()) { 426 QualType Ty = InitEx->getType(); 427 if (InitEx->isLValue()) { 428 Ty = getContext().getPointerType(Ty); 429 } 430 431 InitVal = svalBuilder.getConjuredSymbolVal(NULL, InitEx, LC, Ty, 432 currentBuilderContext->getCurrentBlockCount()); 433 } 434 B.takeNodes(N); 435 ExplodedNodeSet Dst2; 436 evalBind(Dst2, DS, N, state->getLValue(VD, LC), InitVal, true); 437 B.addNodes(Dst2); 438 } 439 else { 440 B.generateNode(DS, N,state->bindDeclWithNoInit(state->getRegion(VD, LC))); 441 } 442 } 443} 444 445void ExprEngine::VisitLogicalExpr(const BinaryOperator* B, ExplodedNode *Pred, 446 ExplodedNodeSet &Dst) { 447 assert(B->getOpcode() == BO_LAnd || 448 B->getOpcode() == BO_LOr); 449 450 StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 451 ProgramStateRef state = Pred->getState(); 452 const LocationContext *LCtx = Pred->getLocationContext(); 453 SVal X = state->getSVal(B, LCtx); 454 assert(X.isUndef()); 455 456 const Expr *Ex = (const Expr*) cast<UndefinedVal>(X).getData(); 457 assert(Ex); 458 459 if (Ex == B->getRHS()) { 460 X = state->getSVal(Ex, LCtx); 461 462 // Handle undefined values. 463 if (X.isUndef()) { 464 Bldr.generateNode(B, Pred, state->BindExpr(B, LCtx, X)); 465 return; 466 } 467 468 DefinedOrUnknownSVal XD = cast<DefinedOrUnknownSVal>(X); 469 470 // We took the RHS. Because the value of the '&&' or '||' expression must 471 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0 472 // or 1. Alternatively, we could take a lazy approach, and calculate this 473 // value later when necessary. We don't have the machinery in place for 474 // this right now, and since most logical expressions are used for branches, 475 // the payoff is not likely to be large. Instead, we do eager evaluation. 476 if (ProgramStateRef newState = state->assume(XD, true)) 477 Bldr.generateNode(B, Pred, 478 newState->BindExpr(B, LCtx, 479 svalBuilder.makeIntVal(1U, B->getType()))); 480 481 if (ProgramStateRef newState = state->assume(XD, false)) 482 Bldr.generateNode(B, Pred, 483 newState->BindExpr(B, LCtx, 484 svalBuilder.makeIntVal(0U, B->getType()))); 485 } 486 else { 487 // We took the LHS expression. Depending on whether we are '&&' or 488 // '||' we know what the value of the expression is via properties of 489 // the short-circuiting. 490 X = svalBuilder.makeIntVal(B->getOpcode() == BO_LAnd ? 0U : 1U, 491 B->getType()); 492 Bldr.generateNode(B, Pred, state->BindExpr(B, LCtx, X)); 493 } 494} 495 496void ExprEngine::VisitInitListExpr(const InitListExpr *IE, 497 ExplodedNode *Pred, 498 ExplodedNodeSet &Dst) { 499 StmtNodeBuilder B(Pred, Dst, *currentBuilderContext); 500 501 ProgramStateRef state = Pred->getState(); 502 const LocationContext *LCtx = Pred->getLocationContext(); 503 QualType T = getContext().getCanonicalType(IE->getType()); 504 unsigned NumInitElements = IE->getNumInits(); 505 506 if (T->isArrayType() || T->isRecordType() || T->isVectorType()) { 507 llvm::ImmutableList<SVal> vals = getBasicVals().getEmptySValList(); 508 509 // Handle base case where the initializer has no elements. 510 // e.g: static int* myArray[] = {}; 511 if (NumInitElements == 0) { 512 SVal V = svalBuilder.makeCompoundVal(T, vals); 513 B.generateNode(IE, Pred, state->BindExpr(IE, LCtx, V)); 514 return; 515 } 516 517 for (InitListExpr::const_reverse_iterator it = IE->rbegin(), 518 ei = IE->rend(); it != ei; ++it) { 519 vals = getBasicVals().consVals(state->getSVal(cast<Expr>(*it), LCtx), 520 vals); 521 } 522 523 B.generateNode(IE, Pred, 524 state->BindExpr(IE, LCtx, 525 svalBuilder.makeCompoundVal(T, vals))); 526 return; 527 } 528 529 if (Loc::isLocType(T) || T->isIntegerType()) { 530 assert(IE->getNumInits() == 1); 531 const Expr *initEx = IE->getInit(0); 532 B.generateNode(IE, Pred, state->BindExpr(IE, LCtx, 533 state->getSVal(initEx, LCtx))); 534 return; 535 } 536 537 llvm_unreachable("unprocessed InitListExpr type"); 538} 539 540void ExprEngine::VisitGuardedExpr(const Expr *Ex, 541 const Expr *L, 542 const Expr *R, 543 ExplodedNode *Pred, 544 ExplodedNodeSet &Dst) { 545 StmtNodeBuilder B(Pred, Dst, *currentBuilderContext); 546 547 ProgramStateRef state = Pred->getState(); 548 const LocationContext *LCtx = Pred->getLocationContext(); 549 SVal X = state->getSVal(Ex, LCtx); 550 assert (X.isUndef()); 551 const Expr *SE = (Expr*) cast<UndefinedVal>(X).getData(); 552 assert(SE); 553 X = state->getSVal(SE, LCtx); 554 555 // Make sure that we invalidate the previous binding. 556 B.generateNode(Ex, Pred, state->BindExpr(Ex, LCtx, X, true)); 557} 558 559void ExprEngine:: 560VisitOffsetOfExpr(const OffsetOfExpr *OOE, 561 ExplodedNode *Pred, ExplodedNodeSet &Dst) { 562 StmtNodeBuilder B(Pred, Dst, *currentBuilderContext); 563 APSInt IV; 564 if (OOE->EvaluateAsInt(IV, getContext())) { 565 assert(IV.getBitWidth() == getContext().getTypeSize(OOE->getType())); 566 assert(OOE->getType()->isIntegerType()); 567 assert(IV.isSigned() == OOE->getType()->isSignedIntegerOrEnumerationType()); 568 SVal X = svalBuilder.makeIntVal(IV); 569 B.generateNode(OOE, Pred, 570 Pred->getState()->BindExpr(OOE, Pred->getLocationContext(), 571 X)); 572 } 573 // FIXME: Handle the case where __builtin_offsetof is not a constant. 574} 575 576 577void ExprEngine:: 578VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Ex, 579 ExplodedNode *Pred, 580 ExplodedNodeSet &Dst) { 581 StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 582 583 QualType T = Ex->getTypeOfArgument(); 584 585 if (Ex->getKind() == UETT_SizeOf) { 586 if (!T->isIncompleteType() && !T->isConstantSizeType()) { 587 assert(T->isVariableArrayType() && "Unknown non-constant-sized type."); 588 589 // FIXME: Add support for VLA type arguments and VLA expressions. 590 // When that happens, we should probably refactor VLASizeChecker's code. 591 return; 592 } 593 else if (T->getAs<ObjCObjectType>()) { 594 // Some code tries to take the sizeof an ObjCObjectType, relying that 595 // the compiler has laid out its representation. Just report Unknown 596 // for these. 597 return; 598 } 599 } 600 601 APSInt Value = Ex->EvaluateKnownConstInt(getContext()); 602 CharUnits amt = CharUnits::fromQuantity(Value.getZExtValue()); 603 604 ProgramStateRef state = Pred->getState(); 605 state = state->BindExpr(Ex, Pred->getLocationContext(), 606 svalBuilder.makeIntVal(amt.getQuantity(), 607 Ex->getType())); 608 Bldr.generateNode(Ex, Pred, state); 609} 610 611void ExprEngine::VisitUnaryOperator(const UnaryOperator* U, 612 ExplodedNode *Pred, 613 ExplodedNodeSet &Dst) { 614 StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 615 switch (U->getOpcode()) { 616 default: { 617 Bldr.takeNodes(Pred); 618 ExplodedNodeSet Tmp; 619 VisitIncrementDecrementOperator(U, Pred, Tmp); 620 Bldr.addNodes(Tmp); 621 } 622 break; 623 case UO_Real: { 624 const Expr *Ex = U->getSubExpr()->IgnoreParens(); 625 626 // FIXME: We don't have complex SValues yet. 627 if (Ex->getType()->isAnyComplexType()) { 628 // Just report "Unknown." 629 break; 630 } 631 632 // For all other types, UO_Real is an identity operation. 633 assert (U->getType() == Ex->getType()); 634 ProgramStateRef state = Pred->getState(); 635 const LocationContext *LCtx = Pred->getLocationContext(); 636 Bldr.generateNode(U, Pred, state->BindExpr(U, LCtx, 637 state->getSVal(Ex, LCtx))); 638 break; 639 } 640 641 case UO_Imag: { 642 const Expr *Ex = U->getSubExpr()->IgnoreParens(); 643 // FIXME: We don't have complex SValues yet. 644 if (Ex->getType()->isAnyComplexType()) { 645 // Just report "Unknown." 646 break; 647 } 648 // For all other types, UO_Imag returns 0. 649 ProgramStateRef state = Pred->getState(); 650 const LocationContext *LCtx = Pred->getLocationContext(); 651 SVal X = svalBuilder.makeZeroVal(Ex->getType()); 652 Bldr.generateNode(U, Pred, state->BindExpr(U, LCtx, X)); 653 break; 654 } 655 656 case UO_Plus: 657 assert(!U->isLValue()); 658 // FALL-THROUGH. 659 case UO_Deref: 660 case UO_AddrOf: 661 case UO_Extension: { 662 // FIXME: We can probably just have some magic in Environment::getSVal() 663 // that propagates values, instead of creating a new node here. 664 // 665 // Unary "+" is a no-op, similar to a parentheses. We still have places 666 // where it may be a block-level expression, so we need to 667 // generate an extra node that just propagates the value of the 668 // subexpression. 669 const Expr *Ex = U->getSubExpr()->IgnoreParens(); 670 ProgramStateRef state = Pred->getState(); 671 const LocationContext *LCtx = Pred->getLocationContext(); 672 Bldr.generateNode(U, Pred, state->BindExpr(U, LCtx, 673 state->getSVal(Ex, LCtx))); 674 break; 675 } 676 677 case UO_LNot: 678 case UO_Minus: 679 case UO_Not: { 680 assert (!U->isLValue()); 681 const Expr *Ex = U->getSubExpr()->IgnoreParens(); 682 ProgramStateRef state = Pred->getState(); 683 const LocationContext *LCtx = Pred->getLocationContext(); 684 685 // Get the value of the subexpression. 686 SVal V = state->getSVal(Ex, LCtx); 687 688 if (V.isUnknownOrUndef()) { 689 Bldr.generateNode(U, Pred, state->BindExpr(U, LCtx, V)); 690 break; 691 } 692 693 switch (U->getOpcode()) { 694 default: 695 llvm_unreachable("Invalid Opcode."); 696 case UO_Not: 697 // FIXME: Do we need to handle promotions? 698 state = state->BindExpr(U, LCtx, evalComplement(cast<NonLoc>(V))); 699 break; 700 case UO_Minus: 701 // FIXME: Do we need to handle promotions? 702 state = state->BindExpr(U, LCtx, evalMinus(cast<NonLoc>(V))); 703 break; 704 case UO_LNot: 705 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)." 706 // 707 // Note: technically we do "E == 0", but this is the same in the 708 // transfer functions as "0 == E". 709 SVal Result; 710 if (isa<Loc>(V)) { 711 Loc X = svalBuilder.makeNull(); 712 Result = evalBinOp(state, BO_EQ, cast<Loc>(V), X, 713 U->getType()); 714 } 715 else { 716 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType())); 717 Result = evalBinOp(state, BO_EQ, cast<NonLoc>(V), X, 718 U->getType()); 719 } 720 721 state = state->BindExpr(U, LCtx, Result); 722 break; 723 } 724 Bldr.generateNode(U, Pred, state); 725 break; 726 } 727 } 728 729} 730 731void ExprEngine::VisitIncrementDecrementOperator(const UnaryOperator* U, 732 ExplodedNode *Pred, 733 ExplodedNodeSet &Dst) { 734 // Handle ++ and -- (both pre- and post-increment). 735 assert (U->isIncrementDecrementOp()); 736 const Expr *Ex = U->getSubExpr()->IgnoreParens(); 737 738 const LocationContext *LCtx = Pred->getLocationContext(); 739 ProgramStateRef state = Pred->getState(); 740 SVal loc = state->getSVal(Ex, LCtx); 741 742 // Perform a load. 743 ExplodedNodeSet Tmp; 744 evalLoad(Tmp, U, Ex, Pred, state, loc); 745 746 ExplodedNodeSet Dst2; 747 StmtNodeBuilder Bldr(Tmp, Dst2, *currentBuilderContext); 748 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end();I!=E;++I) { 749 750 state = (*I)->getState(); 751 assert(LCtx == (*I)->getLocationContext()); 752 SVal V2_untested = state->getSVal(Ex, LCtx); 753 754 // Propagate unknown and undefined values. 755 if (V2_untested.isUnknownOrUndef()) { 756 Bldr.generateNode(U, *I, state->BindExpr(U, LCtx, V2_untested)); 757 continue; 758 } 759 DefinedSVal V2 = cast<DefinedSVal>(V2_untested); 760 761 // Handle all other values. 762 BinaryOperator::Opcode Op = U->isIncrementOp() ? BO_Add : BO_Sub; 763 764 // If the UnaryOperator has non-location type, use its type to create the 765 // constant value. If the UnaryOperator has location type, create the 766 // constant with int type and pointer width. 767 SVal RHS; 768 769 if (U->getType()->isAnyPointerType()) 770 RHS = svalBuilder.makeArrayIndex(1); 771 else 772 RHS = svalBuilder.makeIntVal(1, U->getType()); 773 774 SVal Result = evalBinOp(state, Op, V2, RHS, U->getType()); 775 776 // Conjure a new symbol if necessary to recover precision. 777 if (Result.isUnknown()){ 778 DefinedOrUnknownSVal SymVal = 779 svalBuilder.getConjuredSymbolVal(NULL, Ex, LCtx, 780 currentBuilderContext->getCurrentBlockCount()); 781 Result = SymVal; 782 783 // If the value is a location, ++/-- should always preserve 784 // non-nullness. Check if the original value was non-null, and if so 785 // propagate that constraint. 786 if (Loc::isLocType(U->getType())) { 787 DefinedOrUnknownSVal Constraint = 788 svalBuilder.evalEQ(state, V2,svalBuilder.makeZeroVal(U->getType())); 789 790 if (!state->assume(Constraint, true)) { 791 // It isn't feasible for the original value to be null. 792 // Propagate this constraint. 793 Constraint = svalBuilder.evalEQ(state, SymVal, 794 svalBuilder.makeZeroVal(U->getType())); 795 796 797 state = state->assume(Constraint, false); 798 assert(state); 799 } 800 } 801 } 802 803 // Since the lvalue-to-rvalue conversion is explicit in the AST, 804 // we bind an l-value if the operator is prefix and an lvalue (in C++). 805 if (U->isLValue()) 806 state = state->BindExpr(U, LCtx, loc); 807 else 808 state = state->BindExpr(U, LCtx, U->isPostfix() ? V2 : Result); 809 810 // Perform the store. 811 Bldr.takeNodes(*I); 812 ExplodedNodeSet Dst3; 813 evalStore(Dst3, U, U, *I, state, loc, Result); 814 Bldr.addNodes(Dst3); 815 } 816 Dst.insert(Dst2); 817} 818