clang API Documentation
00001 //=-- ExprEngineC.cpp - ExprEngine support for C expressions ----*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines ExprEngine's support for C expressions. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "clang/StaticAnalyzer/Core/CheckerManager.h" 00015 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 00016 00017 using namespace clang; 00018 using namespace ento; 00019 using llvm::APSInt; 00020 00021 void ExprEngine::VisitBinaryOperator(const BinaryOperator* B, 00022 ExplodedNode *Pred, 00023 ExplodedNodeSet &Dst) { 00024 00025 Expr *LHS = B->getLHS()->IgnoreParens(); 00026 Expr *RHS = B->getRHS()->IgnoreParens(); 00027 00028 // FIXME: Prechecks eventually go in ::Visit(). 00029 ExplodedNodeSet CheckedSet; 00030 ExplodedNodeSet Tmp2; 00031 getCheckerManager().runCheckersForPreStmt(CheckedSet, Pred, B, *this); 00032 00033 // With both the LHS and RHS evaluated, process the operation itself. 00034 for (ExplodedNodeSet::iterator it=CheckedSet.begin(), ei=CheckedSet.end(); 00035 it != ei; ++it) { 00036 00037 ProgramStateRef state = (*it)->getState(); 00038 const LocationContext *LCtx = (*it)->getLocationContext(); 00039 SVal LeftV = state->getSVal(LHS, LCtx); 00040 SVal RightV = state->getSVal(RHS, LCtx); 00041 00042 BinaryOperator::Opcode Op = B->getOpcode(); 00043 00044 if (Op == BO_Assign) { 00045 // EXPERIMENTAL: "Conjured" symbols. 00046 // FIXME: Handle structs. 00047 if (RightV.isUnknown()) { 00048 unsigned Count = currentBuilderContext->getCurrentBlockCount(); 00049 RightV = svalBuilder.getConjuredSymbolVal(NULL, B->getRHS(), LCtx, Count); 00050 } 00051 // Simulate the effects of a "store": bind the value of the RHS 00052 // to the L-Value represented by the LHS. 00053 SVal ExprVal = B->isGLValue() ? LeftV : RightV; 00054 evalStore(Tmp2, B, LHS, *it, state->BindExpr(B, LCtx, ExprVal), 00055 LeftV, RightV); 00056 continue; 00057 } 00058 00059 if (!B->isAssignmentOp()) { 00060 StmtNodeBuilder Bldr(*it, Tmp2, *currentBuilderContext); 00061 00062 if (B->isAdditiveOp()) { 00063 // If one of the operands is a location, conjure a symbol for the other 00064 // one (offset) if it's unknown so that memory arithmetic always 00065 // results in an ElementRegion. 00066 // TODO: This can be removed after we enable history tracking with 00067 // SymSymExpr. 00068 unsigned Count = currentBuilderContext->getCurrentBlockCount(); 00069 if (isa<Loc>(LeftV) && 00070 RHS->getType()->isIntegerType() && RightV.isUnknown()) { 00071 RightV = svalBuilder.getConjuredSymbolVal(RHS, LCtx, 00072 RHS->getType(), Count); 00073 } 00074 if (isa<Loc>(RightV) && 00075 LHS->getType()->isIntegerType() && LeftV.isUnknown()) { 00076 LeftV = svalBuilder.getConjuredSymbolVal(LHS, LCtx, 00077 LHS->getType(), Count); 00078 } 00079 } 00080 00081 // Process non-assignments except commas or short-circuited 00082 // logical expressions (LAnd and LOr). 00083 SVal Result = evalBinOp(state, Op, LeftV, RightV, B->getType()); 00084 if (Result.isUnknown()) { 00085 Bldr.generateNode(B, *it, state); 00086 continue; 00087 } 00088 00089 state = state->BindExpr(B, LCtx, Result); 00090 Bldr.generateNode(B, *it, state); 00091 continue; 00092 } 00093 00094 assert (B->isCompoundAssignmentOp()); 00095 00096 switch (Op) { 00097 default: 00098 llvm_unreachable("Invalid opcode for compound assignment."); 00099 case BO_MulAssign: Op = BO_Mul; break; 00100 case BO_DivAssign: Op = BO_Div; break; 00101 case BO_RemAssign: Op = BO_Rem; break; 00102 case BO_AddAssign: Op = BO_Add; break; 00103 case BO_SubAssign: Op = BO_Sub; break; 00104 case BO_ShlAssign: Op = BO_Shl; break; 00105 case BO_ShrAssign: Op = BO_Shr; break; 00106 case BO_AndAssign: Op = BO_And; break; 00107 case BO_XorAssign: Op = BO_Xor; break; 00108 case BO_OrAssign: Op = BO_Or; break; 00109 } 00110 00111 // Perform a load (the LHS). This performs the checks for 00112 // null dereferences, and so on. 00113 ExplodedNodeSet Tmp; 00114 SVal location = LeftV; 00115 evalLoad(Tmp, B, LHS, *it, state, location); 00116 00117 for (ExplodedNodeSet::iterator I = Tmp.begin(), E = Tmp.end(); I != E; 00118 ++I) { 00119 00120 state = (*I)->getState(); 00121 const LocationContext *LCtx = (*I)->getLocationContext(); 00122 SVal V = state->getSVal(LHS, LCtx); 00123 00124 // Get the computation type. 00125 QualType CTy = 00126 cast<CompoundAssignOperator>(B)->getComputationResultType(); 00127 CTy = getContext().getCanonicalType(CTy); 00128 00129 QualType CLHSTy = 00130 cast<CompoundAssignOperator>(B)->getComputationLHSType(); 00131 CLHSTy = getContext().getCanonicalType(CLHSTy); 00132 00133 QualType LTy = getContext().getCanonicalType(LHS->getType()); 00134 00135 // Promote LHS. 00136 V = svalBuilder.evalCast(V, CLHSTy, LTy); 00137 00138 // Compute the result of the operation. 00139 SVal Result = svalBuilder.evalCast(evalBinOp(state, Op, V, RightV, CTy), 00140 B->getType(), CTy); 00141 00142 // EXPERIMENTAL: "Conjured" symbols. 00143 // FIXME: Handle structs. 00144 00145 SVal LHSVal; 00146 00147 if (Result.isUnknown()) { 00148 00149 unsigned Count = currentBuilderContext->getCurrentBlockCount(); 00150 00151 // The symbolic value is actually for the type of the left-hand side 00152 // expression, not the computation type, as this is the value the 00153 // LValue on the LHS will bind to. 00154 LHSVal = svalBuilder.getConjuredSymbolVal(NULL, B->getRHS(), LCtx, 00155 LTy, Count); 00156 00157 // However, we need to convert the symbol to the computation type. 00158 Result = svalBuilder.evalCast(LHSVal, CTy, LTy); 00159 } 00160 else { 00161 // The left-hand side may bind to a different value then the 00162 // computation type. 00163 LHSVal = svalBuilder.evalCast(Result, LTy, CTy); 00164 } 00165 00166 // In C++, assignment and compound assignment operators return an 00167 // lvalue. 00168 if (B->isGLValue()) 00169 state = state->BindExpr(B, LCtx, location); 00170 else 00171 state = state->BindExpr(B, LCtx, Result); 00172 00173 evalStore(Tmp2, B, LHS, *I, state, location, LHSVal); 00174 } 00175 } 00176 00177 // FIXME: postvisits eventually go in ::Visit() 00178 getCheckerManager().runCheckersForPostStmt(Dst, Tmp2, B, *this); 00179 } 00180 00181 void ExprEngine::VisitBlockExpr(const BlockExpr *BE, ExplodedNode *Pred, 00182 ExplodedNodeSet &Dst) { 00183 00184 CanQualType T = getContext().getCanonicalType(BE->getType()); 00185 00186 // Get the value of the block itself. 00187 SVal V = svalBuilder.getBlockPointer(BE->getBlockDecl(), T, 00188 Pred->getLocationContext()); 00189 00190 ProgramStateRef State = Pred->getState(); 00191 00192 // If we created a new MemRegion for the block, we should explicitly bind 00193 // the captured variables. 00194 if (const BlockDataRegion *BDR = 00195 dyn_cast_or_null<BlockDataRegion>(V.getAsRegion())) { 00196 00197 BlockDataRegion::referenced_vars_iterator I = BDR->referenced_vars_begin(), 00198 E = BDR->referenced_vars_end(); 00199 00200 for (; I != E; ++I) { 00201 const MemRegion *capturedR = I.getCapturedRegion(); 00202 const MemRegion *originalR = I.getOriginalRegion(); 00203 if (capturedR != originalR) { 00204 SVal originalV = State->getSVal(loc::MemRegionVal(originalR)); 00205 State = State->bindLoc(loc::MemRegionVal(capturedR), originalV); 00206 } 00207 } 00208 } 00209 00210 ExplodedNodeSet Tmp; 00211 StmtNodeBuilder Bldr(Pred, Tmp, *currentBuilderContext); 00212 Bldr.generateNode(BE, Pred, 00213 State->BindExpr(BE, Pred->getLocationContext(), V), 00214 false, 0, 00215 ProgramPoint::PostLValueKind); 00216 00217 // FIXME: Move all post/pre visits to ::Visit(). 00218 getCheckerManager().runCheckersForPostStmt(Dst, Tmp, BE, *this); 00219 } 00220 00221 void ExprEngine::VisitCast(const CastExpr *CastE, const Expr *Ex, 00222 ExplodedNode *Pred, ExplodedNodeSet &Dst) { 00223 00224 ExplodedNodeSet dstPreStmt; 00225 getCheckerManager().runCheckersForPreStmt(dstPreStmt, Pred, CastE, *this); 00226 00227 if (CastE->getCastKind() == CK_LValueToRValue) { 00228 for (ExplodedNodeSet::iterator I = dstPreStmt.begin(), E = dstPreStmt.end(); 00229 I!=E; ++I) { 00230 ExplodedNode *subExprNode = *I; 00231 ProgramStateRef state = subExprNode->getState(); 00232 const LocationContext *LCtx = subExprNode->getLocationContext(); 00233 evalLoad(Dst, CastE, CastE, subExprNode, state, state->getSVal(Ex, LCtx)); 00234 } 00235 return; 00236 } 00237 00238 // All other casts. 00239 QualType T = CastE->getType(); 00240 QualType ExTy = Ex->getType(); 00241 00242 if (const ExplicitCastExpr *ExCast=dyn_cast_or_null<ExplicitCastExpr>(CastE)) 00243 T = ExCast->getTypeAsWritten(); 00244 00245 StmtNodeBuilder Bldr(dstPreStmt, Dst, *currentBuilderContext); 00246 for (ExplodedNodeSet::iterator I = dstPreStmt.begin(), E = dstPreStmt.end(); 00247 I != E; ++I) { 00248 00249 Pred = *I; 00250 00251 switch (CastE->getCastKind()) { 00252 case CK_LValueToRValue: 00253 llvm_unreachable("LValueToRValue casts handled earlier."); 00254 case CK_ToVoid: 00255 continue; 00256 // The analyzer doesn't do anything special with these casts, 00257 // since it understands retain/release semantics already. 00258 case CK_ARCProduceObject: 00259 case CK_ARCConsumeObject: 00260 case CK_ARCReclaimReturnedObject: 00261 case CK_ARCExtendBlockObject: // Fall-through. 00262 case CK_CopyAndAutoreleaseBlockObject: 00263 // The analyser can ignore atomic casts for now, although some future 00264 // checkers may want to make certain that you're not modifying the same 00265 // value through atomic and nonatomic pointers. 00266 case CK_AtomicToNonAtomic: 00267 case CK_NonAtomicToAtomic: 00268 // True no-ops. 00269 case CK_NoOp: 00270 case CK_FunctionToPointerDecay: { 00271 // Copy the SVal of Ex to CastE. 00272 ProgramStateRef state = Pred->getState(); 00273 const LocationContext *LCtx = Pred->getLocationContext(); 00274 SVal V = state->getSVal(Ex, LCtx); 00275 state = state->BindExpr(CastE, LCtx, V); 00276 Bldr.generateNode(CastE, Pred, state); 00277 continue; 00278 } 00279 case CK_Dependent: 00280 case CK_ArrayToPointerDecay: 00281 case CK_BitCast: 00282 case CK_LValueBitCast: 00283 case CK_IntegralCast: 00284 case CK_NullToPointer: 00285 case CK_IntegralToPointer: 00286 case CK_PointerToIntegral: 00287 case CK_PointerToBoolean: 00288 case CK_IntegralToBoolean: 00289 case CK_IntegralToFloating: 00290 case CK_FloatingToIntegral: 00291 case CK_FloatingToBoolean: 00292 case CK_FloatingCast: 00293 case CK_FloatingRealToComplex: 00294 case CK_FloatingComplexToReal: 00295 case CK_FloatingComplexToBoolean: 00296 case CK_FloatingComplexCast: 00297 case CK_FloatingComplexToIntegralComplex: 00298 case CK_IntegralRealToComplex: 00299 case CK_IntegralComplexToReal: 00300 case CK_IntegralComplexToBoolean: 00301 case CK_IntegralComplexCast: 00302 case CK_IntegralComplexToFloatingComplex: 00303 case CK_CPointerToObjCPointerCast: 00304 case CK_BlockPointerToObjCPointerCast: 00305 case CK_AnyPointerToBlockPointerCast: 00306 case CK_ObjCObjectLValueCast: { 00307 // Delegate to SValBuilder to process. 00308 ProgramStateRef state = Pred->getState(); 00309 const LocationContext *LCtx = Pred->getLocationContext(); 00310 SVal V = state->getSVal(Ex, LCtx); 00311 V = svalBuilder.evalCast(V, T, ExTy); 00312 state = state->BindExpr(CastE, LCtx, V); 00313 Bldr.generateNode(CastE, Pred, state); 00314 continue; 00315 } 00316 case CK_DerivedToBase: 00317 case CK_UncheckedDerivedToBase: { 00318 // For DerivedToBase cast, delegate to the store manager. 00319 ProgramStateRef state = Pred->getState(); 00320 const LocationContext *LCtx = Pred->getLocationContext(); 00321 SVal val = state->getSVal(Ex, LCtx); 00322 val = getStoreManager().evalDerivedToBase(val, T); 00323 state = state->BindExpr(CastE, LCtx, val); 00324 Bldr.generateNode(CastE, Pred, state); 00325 continue; 00326 } 00327 // Handle C++ dyn_cast. 00328 case CK_Dynamic: { 00329 ProgramStateRef state = Pred->getState(); 00330 const LocationContext *LCtx = Pred->getLocationContext(); 00331 SVal val = state->getSVal(Ex, LCtx); 00332 00333 // Compute the type of the result. 00334 QualType resultType = CastE->getType(); 00335 if (CastE->isGLValue()) 00336 resultType = getContext().getPointerType(resultType); 00337 00338 bool Failed = false; 00339 00340 // Check if the value being cast evaluates to 0. 00341 if (val.isZeroConstant()) 00342 Failed = true; 00343 // Else, evaluate the cast. 00344 else 00345 val = getStoreManager().evalDynamicCast(val, T, Failed); 00346 00347 if (Failed) { 00348 if (T->isReferenceType()) { 00349 // A bad_cast exception is thrown if input value is a reference. 00350 // Currently, we model this, by generating a sink. 00351 Bldr.generateNode(CastE, Pred, state, true); 00352 continue; 00353 } else { 00354 // If the cast fails on a pointer, bind to 0. 00355 state = state->BindExpr(CastE, LCtx, svalBuilder.makeNull()); 00356 } 00357 } else { 00358 // If we don't know if the cast succeeded, conjure a new symbol. 00359 if (val.isUnknown()) { 00360 DefinedOrUnknownSVal NewSym = svalBuilder.getConjuredSymbolVal(NULL, 00361 CastE, LCtx, resultType, 00362 currentBuilderContext->getCurrentBlockCount()); 00363 state = state->BindExpr(CastE, LCtx, NewSym); 00364 } else 00365 // Else, bind to the derived region value. 00366 state = state->BindExpr(CastE, LCtx, val); 00367 } 00368 Bldr.generateNode(CastE, Pred, state); 00369 continue; 00370 } 00371 // Various C++ casts that are not handled yet. 00372 case CK_ToUnion: 00373 case CK_BaseToDerived: 00374 case CK_NullToMemberPointer: 00375 case CK_BaseToDerivedMemberPointer: 00376 case CK_DerivedToBaseMemberPointer: 00377 case CK_ReinterpretMemberPointer: 00378 case CK_UserDefinedConversion: 00379 case CK_ConstructorConversion: 00380 case CK_VectorSplat: 00381 case CK_MemberPointerToBoolean: { 00382 // Recover some path-sensitivty by conjuring a new value. 00383 QualType resultType = CastE->getType(); 00384 if (CastE->isGLValue()) 00385 resultType = getContext().getPointerType(resultType); 00386 const LocationContext *LCtx = Pred->getLocationContext(); 00387 SVal result = svalBuilder.getConjuredSymbolVal(NULL, CastE, LCtx, 00388 resultType, currentBuilderContext->getCurrentBlockCount()); 00389 ProgramStateRef state = Pred->getState()->BindExpr(CastE, LCtx, 00390 result); 00391 Bldr.generateNode(CastE, Pred, state); 00392 continue; 00393 } 00394 } 00395 } 00396 } 00397 00398 void ExprEngine::VisitCompoundLiteralExpr(const CompoundLiteralExpr *CL, 00399 ExplodedNode *Pred, 00400 ExplodedNodeSet &Dst) { 00401 StmtNodeBuilder B(Pred, Dst, *currentBuilderContext); 00402 00403 const InitListExpr *ILE 00404 = cast<InitListExpr>(CL->getInitializer()->IgnoreParens()); 00405 00406 ProgramStateRef state = Pred->getState(); 00407 SVal ILV = state->getSVal(ILE, Pred->getLocationContext()); 00408 const LocationContext *LC = Pred->getLocationContext(); 00409 state = state->bindCompoundLiteral(CL, LC, ILV); 00410 00411 if (CL->isGLValue()) 00412 B.generateNode(CL, Pred, state->BindExpr(CL, LC, state->getLValue(CL, LC))); 00413 else 00414 B.generateNode(CL, Pred, state->BindExpr(CL, LC, ILV)); 00415 } 00416 00417 void ExprEngine::VisitDeclStmt(const DeclStmt *DS, ExplodedNode *Pred, 00418 ExplodedNodeSet &Dst) { 00419 00420 // FIXME: static variables may have an initializer, but the second 00421 // time a function is called those values may not be current. 00422 // This may need to be reflected in the CFG. 00423 00424 // Assumption: The CFG has one DeclStmt per Decl. 00425 const Decl *D = *DS->decl_begin(); 00426 00427 if (!D || !isa<VarDecl>(D)) { 00428 //TODO:AZ: remove explicit insertion after refactoring is done. 00429 Dst.insert(Pred); 00430 return; 00431 } 00432 00433 // FIXME: all pre/post visits should eventually be handled by ::Visit(). 00434 ExplodedNodeSet dstPreVisit; 00435 getCheckerManager().runCheckersForPreStmt(dstPreVisit, Pred, DS, *this); 00436 00437 StmtNodeBuilder B(dstPreVisit, Dst, *currentBuilderContext); 00438 const VarDecl *VD = dyn_cast<VarDecl>(D); 00439 for (ExplodedNodeSet::iterator I = dstPreVisit.begin(), E = dstPreVisit.end(); 00440 I!=E; ++I) { 00441 ExplodedNode *N = *I; 00442 ProgramStateRef state = N->getState(); 00443 00444 // Decls without InitExpr are not initialized explicitly. 00445 const LocationContext *LC = N->getLocationContext(); 00446 00447 if (const Expr *InitEx = VD->getInit()) { 00448 SVal InitVal = state->getSVal(InitEx, Pred->getLocationContext()); 00449 00450 // We bound the temp obj region to the CXXConstructExpr. Now recover 00451 // the lazy compound value when the variable is not a reference. 00452 if (AMgr.getLangOpts().CPlusPlus && VD->getType()->isRecordType() && 00453 !VD->getType()->isReferenceType() && isa<loc::MemRegionVal>(InitVal)){ 00454 InitVal = state->getSVal(cast<loc::MemRegionVal>(InitVal).getRegion()); 00455 assert(isa<nonloc::LazyCompoundVal>(InitVal)); 00456 } 00457 00458 // Recover some path-sensitivity if a scalar value evaluated to 00459 // UnknownVal. 00460 if (InitVal.isUnknown()) { 00461 QualType Ty = InitEx->getType(); 00462 if (InitEx->isGLValue()) { 00463 Ty = getContext().getPointerType(Ty); 00464 } 00465 00466 InitVal = svalBuilder.getConjuredSymbolVal(NULL, InitEx, LC, Ty, 00467 currentBuilderContext->getCurrentBlockCount()); 00468 } 00469 B.takeNodes(N); 00470 ExplodedNodeSet Dst2; 00471 evalBind(Dst2, DS, N, state->getLValue(VD, LC), InitVal, true); 00472 B.addNodes(Dst2); 00473 } 00474 else { 00475 B.generateNode(DS, N,state->bindDeclWithNoInit(state->getRegion(VD, LC))); 00476 } 00477 } 00478 } 00479 00480 void ExprEngine::VisitLogicalExpr(const BinaryOperator* B, ExplodedNode *Pred, 00481 ExplodedNodeSet &Dst) { 00482 assert(B->getOpcode() == BO_LAnd || 00483 B->getOpcode() == BO_LOr); 00484 00485 StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 00486 ProgramStateRef state = Pred->getState(); 00487 const LocationContext *LCtx = Pred->getLocationContext(); 00488 SVal X = state->getSVal(B, LCtx); 00489 assert(X.isUndef()); 00490 00491 const Expr *Ex = (const Expr*) cast<UndefinedVal>(X).getData(); 00492 assert(Ex); 00493 00494 if (Ex == B->getRHS()) { 00495 X = state->getSVal(Ex, LCtx); 00496 00497 // Handle undefined values. 00498 if (X.isUndef()) { 00499 Bldr.generateNode(B, Pred, state->BindExpr(B, LCtx, X)); 00500 return; 00501 } 00502 00503 DefinedOrUnknownSVal XD = cast<DefinedOrUnknownSVal>(X); 00504 00505 // We took the RHS. Because the value of the '&&' or '||' expression must 00506 // evaluate to 0 or 1, we must assume the value of the RHS evaluates to 0 00507 // or 1. Alternatively, we could take a lazy approach, and calculate this 00508 // value later when necessary. We don't have the machinery in place for 00509 // this right now, and since most logical expressions are used for branches, 00510 // the payoff is not likely to be large. Instead, we do eager evaluation. 00511 if (ProgramStateRef newState = state->assume(XD, true)) 00512 Bldr.generateNode(B, Pred, 00513 newState->BindExpr(B, LCtx, 00514 svalBuilder.makeIntVal(1U, B->getType()))); 00515 00516 if (ProgramStateRef newState = state->assume(XD, false)) 00517 Bldr.generateNode(B, Pred, 00518 newState->BindExpr(B, LCtx, 00519 svalBuilder.makeIntVal(0U, B->getType()))); 00520 } 00521 else { 00522 // We took the LHS expression. Depending on whether we are '&&' or 00523 // '||' we know what the value of the expression is via properties of 00524 // the short-circuiting. 00525 X = svalBuilder.makeIntVal(B->getOpcode() == BO_LAnd ? 0U : 1U, 00526 B->getType()); 00527 Bldr.generateNode(B, Pred, state->BindExpr(B, LCtx, X)); 00528 } 00529 } 00530 00531 void ExprEngine::VisitInitListExpr(const InitListExpr *IE, 00532 ExplodedNode *Pred, 00533 ExplodedNodeSet &Dst) { 00534 StmtNodeBuilder B(Pred, Dst, *currentBuilderContext); 00535 00536 ProgramStateRef state = Pred->getState(); 00537 const LocationContext *LCtx = Pred->getLocationContext(); 00538 QualType T = getContext().getCanonicalType(IE->getType()); 00539 unsigned NumInitElements = IE->getNumInits(); 00540 00541 if (T->isArrayType() || T->isRecordType() || T->isVectorType()) { 00542 llvm::ImmutableList<SVal> vals = getBasicVals().getEmptySValList(); 00543 00544 // Handle base case where the initializer has no elements. 00545 // e.g: static int* myArray[] = {}; 00546 if (NumInitElements == 0) { 00547 SVal V = svalBuilder.makeCompoundVal(T, vals); 00548 B.generateNode(IE, Pred, state->BindExpr(IE, LCtx, V)); 00549 return; 00550 } 00551 00552 for (InitListExpr::const_reverse_iterator it = IE->rbegin(), 00553 ei = IE->rend(); it != ei; ++it) { 00554 vals = getBasicVals().consVals(state->getSVal(cast<Expr>(*it), LCtx), 00555 vals); 00556 } 00557 00558 B.generateNode(IE, Pred, 00559 state->BindExpr(IE, LCtx, 00560 svalBuilder.makeCompoundVal(T, vals))); 00561 return; 00562 } 00563 00564 if (Loc::isLocType(T) || T->isIntegerType()) { 00565 assert(IE->getNumInits() == 1); 00566 const Expr *initEx = IE->getInit(0); 00567 B.generateNode(IE, Pred, state->BindExpr(IE, LCtx, 00568 state->getSVal(initEx, LCtx))); 00569 return; 00570 } 00571 00572 llvm_unreachable("unprocessed InitListExpr type"); 00573 } 00574 00575 void ExprEngine::VisitGuardedExpr(const Expr *Ex, 00576 const Expr *L, 00577 const Expr *R, 00578 ExplodedNode *Pred, 00579 ExplodedNodeSet &Dst) { 00580 StmtNodeBuilder B(Pred, Dst, *currentBuilderContext); 00581 00582 ProgramStateRef state = Pred->getState(); 00583 const LocationContext *LCtx = Pred->getLocationContext(); 00584 SVal X = state->getSVal(Ex, LCtx); 00585 assert (X.isUndef()); 00586 const Expr *SE = (Expr*) cast<UndefinedVal>(X).getData(); 00587 assert(SE); 00588 X = state->getSVal(SE, LCtx); 00589 00590 // Make sure that we invalidate the previous binding. 00591 B.generateNode(Ex, Pred, state->BindExpr(Ex, LCtx, X, true)); 00592 } 00593 00594 void ExprEngine:: 00595 VisitOffsetOfExpr(const OffsetOfExpr *OOE, 00596 ExplodedNode *Pred, ExplodedNodeSet &Dst) { 00597 StmtNodeBuilder B(Pred, Dst, *currentBuilderContext); 00598 APSInt IV; 00599 if (OOE->EvaluateAsInt(IV, getContext())) { 00600 assert(IV.getBitWidth() == getContext().getTypeSize(OOE->getType())); 00601 assert(OOE->getType()->isIntegerType()); 00602 assert(IV.isSigned() == OOE->getType()->isSignedIntegerOrEnumerationType()); 00603 SVal X = svalBuilder.makeIntVal(IV); 00604 B.generateNode(OOE, Pred, 00605 Pred->getState()->BindExpr(OOE, Pred->getLocationContext(), 00606 X)); 00607 } 00608 // FIXME: Handle the case where __builtin_offsetof is not a constant. 00609 } 00610 00611 00612 void ExprEngine:: 00613 VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *Ex, 00614 ExplodedNode *Pred, 00615 ExplodedNodeSet &Dst) { 00616 StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 00617 00618 QualType T = Ex->getTypeOfArgument(); 00619 00620 if (Ex->getKind() == UETT_SizeOf) { 00621 if (!T->isIncompleteType() && !T->isConstantSizeType()) { 00622 assert(T->isVariableArrayType() && "Unknown non-constant-sized type."); 00623 00624 // FIXME: Add support for VLA type arguments and VLA expressions. 00625 // When that happens, we should probably refactor VLASizeChecker's code. 00626 return; 00627 } 00628 else if (T->getAs<ObjCObjectType>()) { 00629 // Some code tries to take the sizeof an ObjCObjectType, relying that 00630 // the compiler has laid out its representation. Just report Unknown 00631 // for these. 00632 return; 00633 } 00634 } 00635 00636 APSInt Value = Ex->EvaluateKnownConstInt(getContext()); 00637 CharUnits amt = CharUnits::fromQuantity(Value.getZExtValue()); 00638 00639 ProgramStateRef state = Pred->getState(); 00640 state = state->BindExpr(Ex, Pred->getLocationContext(), 00641 svalBuilder.makeIntVal(amt.getQuantity(), 00642 Ex->getType())); 00643 Bldr.generateNode(Ex, Pred, state); 00644 } 00645 00646 void ExprEngine::VisitUnaryOperator(const UnaryOperator* U, 00647 ExplodedNode *Pred, 00648 ExplodedNodeSet &Dst) { 00649 StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 00650 switch (U->getOpcode()) { 00651 default: { 00652 Bldr.takeNodes(Pred); 00653 ExplodedNodeSet Tmp; 00654 VisitIncrementDecrementOperator(U, Pred, Tmp); 00655 Bldr.addNodes(Tmp); 00656 } 00657 break; 00658 case UO_Real: { 00659 const Expr *Ex = U->getSubExpr()->IgnoreParens(); 00660 00661 // FIXME: We don't have complex SValues yet. 00662 if (Ex->getType()->isAnyComplexType()) { 00663 // Just report "Unknown." 00664 break; 00665 } 00666 00667 // For all other types, UO_Real is an identity operation. 00668 assert (U->getType() == Ex->getType()); 00669 ProgramStateRef state = Pred->getState(); 00670 const LocationContext *LCtx = Pred->getLocationContext(); 00671 Bldr.generateNode(U, Pred, state->BindExpr(U, LCtx, 00672 state->getSVal(Ex, LCtx))); 00673 break; 00674 } 00675 00676 case UO_Imag: { 00677 const Expr *Ex = U->getSubExpr()->IgnoreParens(); 00678 // FIXME: We don't have complex SValues yet. 00679 if (Ex->getType()->isAnyComplexType()) { 00680 // Just report "Unknown." 00681 break; 00682 } 00683 // For all other types, UO_Imag returns 0. 00684 ProgramStateRef state = Pred->getState(); 00685 const LocationContext *LCtx = Pred->getLocationContext(); 00686 SVal X = svalBuilder.makeZeroVal(Ex->getType()); 00687 Bldr.generateNode(U, Pred, state->BindExpr(U, LCtx, X)); 00688 break; 00689 } 00690 00691 case UO_Plus: 00692 assert(!U->isGLValue()); 00693 // FALL-THROUGH. 00694 case UO_Deref: 00695 case UO_AddrOf: 00696 case UO_Extension: { 00697 // FIXME: We can probably just have some magic in Environment::getSVal() 00698 // that propagates values, instead of creating a new node here. 00699 // 00700 // Unary "+" is a no-op, similar to a parentheses. We still have places 00701 // where it may be a block-level expression, so we need to 00702 // generate an extra node that just propagates the value of the 00703 // subexpression. 00704 const Expr *Ex = U->getSubExpr()->IgnoreParens(); 00705 ProgramStateRef state = Pred->getState(); 00706 const LocationContext *LCtx = Pred->getLocationContext(); 00707 Bldr.generateNode(U, Pred, state->BindExpr(U, LCtx, 00708 state->getSVal(Ex, LCtx))); 00709 break; 00710 } 00711 00712 case UO_LNot: 00713 case UO_Minus: 00714 case UO_Not: { 00715 assert (!U->isGLValue()); 00716 const Expr *Ex = U->getSubExpr()->IgnoreParens(); 00717 ProgramStateRef state = Pred->getState(); 00718 const LocationContext *LCtx = Pred->getLocationContext(); 00719 00720 // Get the value of the subexpression. 00721 SVal V = state->getSVal(Ex, LCtx); 00722 00723 if (V.isUnknownOrUndef()) { 00724 Bldr.generateNode(U, Pred, state->BindExpr(U, LCtx, V)); 00725 break; 00726 } 00727 00728 switch (U->getOpcode()) { 00729 default: 00730 llvm_unreachable("Invalid Opcode."); 00731 case UO_Not: 00732 // FIXME: Do we need to handle promotions? 00733 state = state->BindExpr(U, LCtx, evalComplement(cast<NonLoc>(V))); 00734 break; 00735 case UO_Minus: 00736 // FIXME: Do we need to handle promotions? 00737 state = state->BindExpr(U, LCtx, evalMinus(cast<NonLoc>(V))); 00738 break; 00739 case UO_LNot: 00740 // C99 6.5.3.3: "The expression !E is equivalent to (0==E)." 00741 // 00742 // Note: technically we do "E == 0", but this is the same in the 00743 // transfer functions as "0 == E". 00744 SVal Result; 00745 if (isa<Loc>(V)) { 00746 Loc X = svalBuilder.makeNull(); 00747 Result = evalBinOp(state, BO_EQ, cast<Loc>(V), X, 00748 U->getType()); 00749 } 00750 else { 00751 nonloc::ConcreteInt X(getBasicVals().getValue(0, Ex->getType())); 00752 Result = evalBinOp(state, BO_EQ, cast<NonLoc>(V), X, 00753 U->getType()); 00754 } 00755 00756 state = state->BindExpr(U, LCtx, Result); 00757 break; 00758 } 00759 Bldr.generateNode(U, Pred, state); 00760 break; 00761 } 00762 } 00763 00764 } 00765 00766 void ExprEngine::VisitIncrementDecrementOperator(const UnaryOperator* U, 00767 ExplodedNode *Pred, 00768 ExplodedNodeSet &Dst) { 00769 // Handle ++ and -- (both pre- and post-increment). 00770 assert (U->isIncrementDecrementOp()); 00771 const Expr *Ex = U->getSubExpr()->IgnoreParens(); 00772 00773 const LocationContext *LCtx = Pred->getLocationContext(); 00774 ProgramStateRef state = Pred->getState(); 00775 SVal loc = state->getSVal(Ex, LCtx); 00776 00777 // Perform a load. 00778 ExplodedNodeSet Tmp; 00779 evalLoad(Tmp, U, Ex, Pred, state, loc); 00780 00781 ExplodedNodeSet Dst2; 00782 StmtNodeBuilder Bldr(Tmp, Dst2, *currentBuilderContext); 00783 for (ExplodedNodeSet::iterator I=Tmp.begin(), E=Tmp.end();I!=E;++I) { 00784 00785 state = (*I)->getState(); 00786 assert(LCtx == (*I)->getLocationContext()); 00787 SVal V2_untested = state->getSVal(Ex, LCtx); 00788 00789 // Propagate unknown and undefined values. 00790 if (V2_untested.isUnknownOrUndef()) { 00791 Bldr.generateNode(U, *I, state->BindExpr(U, LCtx, V2_untested)); 00792 continue; 00793 } 00794 DefinedSVal V2 = cast<DefinedSVal>(V2_untested); 00795 00796 // Handle all other values. 00797 BinaryOperator::Opcode Op = U->isIncrementOp() ? BO_Add : BO_Sub; 00798 00799 // If the UnaryOperator has non-location type, use its type to create the 00800 // constant value. If the UnaryOperator has location type, create the 00801 // constant with int type and pointer width. 00802 SVal RHS; 00803 00804 if (U->getType()->isAnyPointerType()) 00805 RHS = svalBuilder.makeArrayIndex(1); 00806 else 00807 RHS = svalBuilder.makeIntVal(1, U->getType()); 00808 00809 SVal Result = evalBinOp(state, Op, V2, RHS, U->getType()); 00810 00811 // Conjure a new symbol if necessary to recover precision. 00812 if (Result.isUnknown()){ 00813 DefinedOrUnknownSVal SymVal = 00814 svalBuilder.getConjuredSymbolVal(NULL, Ex, LCtx, 00815 currentBuilderContext->getCurrentBlockCount()); 00816 Result = SymVal; 00817 00818 // If the value is a location, ++/-- should always preserve 00819 // non-nullness. Check if the original value was non-null, and if so 00820 // propagate that constraint. 00821 if (Loc::isLocType(U->getType())) { 00822 DefinedOrUnknownSVal Constraint = 00823 svalBuilder.evalEQ(state, V2,svalBuilder.makeZeroVal(U->getType())); 00824 00825 if (!state->assume(Constraint, true)) { 00826 // It isn't feasible for the original value to be null. 00827 // Propagate this constraint. 00828 Constraint = svalBuilder.evalEQ(state, SymVal, 00829 svalBuilder.makeZeroVal(U->getType())); 00830 00831 00832 state = state->assume(Constraint, false); 00833 assert(state); 00834 } 00835 } 00836 } 00837 00838 // Since the lvalue-to-rvalue conversion is explicit in the AST, 00839 // we bind an l-value if the operator is prefix and an lvalue (in C++). 00840 if (U->isGLValue()) 00841 state = state->BindExpr(U, LCtx, loc); 00842 else 00843 state = state->BindExpr(U, LCtx, U->isPostfix() ? V2 : Result); 00844 00845 // Perform the store. 00846 Bldr.takeNodes(*I); 00847 ExplodedNodeSet Dst3; 00848 evalStore(Dst3, U, U, *I, state, loc, Result); 00849 Bldr.addNodes(Dst3); 00850 } 00851 Dst.insert(Dst2); 00852 }