110e25031STed Kremenek //===- ExprEngineCXX.cpp - ExprEngine support for C++ -----------*- C++ -*-===// 210e25031STed Kremenek // 310e25031STed Kremenek // The LLVM Compiler Infrastructure 410e25031STed Kremenek // 510e25031STed Kremenek // This file is distributed under the University of Illinois Open Source 610e25031STed Kremenek // License. See LICENSE.TXT for details. 710e25031STed Kremenek // 810e25031STed Kremenek //===----------------------------------------------------------------------===// 910e25031STed Kremenek // 1010e25031STed Kremenek // This file defines the C++ expression evaluation engine. 1110e25031STed Kremenek // 1210e25031STed Kremenek //===----------------------------------------------------------------------===// 1310e25031STed Kremenek 1410e25031STed Kremenek #include "clang/StaticAnalyzer/Core/CheckerManager.h" 1510e25031STed Kremenek #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" 1610e25031STed Kremenek #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 174f7df9beSJordan Rose #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 1810e25031STed Kremenek #include "clang/AST/DeclCXX.h" 198fdb59f9STed Kremenek #include "clang/AST/StmtCXX.h" 20*17a8757aSJordan Rose #include "clang/Basic/PrettyStackTrace.h" 2110e25031STed Kremenek 2210e25031STed Kremenek using namespace clang; 2310e25031STed Kremenek using namespace ento; 2410e25031STed Kremenek 2510e25031STed Kremenek void ExprEngine::CreateCXXTemporaryObject(const MaterializeTemporaryExpr *ME, 2610e25031STed Kremenek ExplodedNode *Pred, 2710e25031STed Kremenek ExplodedNodeSet &Dst) { 28e4e5712cSAnna Zaks StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 292a14c695STed Kremenek const Expr *tempExpr = ME->GetTemporaryExpr()->IgnoreParens(); 3049b1e38eSTed Kremenek ProgramStateRef state = Pred->getState(); 31632e3b7eSTed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 3210e25031STed Kremenek 3310e25031STed Kremenek // Bind the temporary object to the value of the expression. Then bind 3410e25031STed Kremenek // the expression to the location of the object. 35632e3b7eSTed Kremenek SVal V = state->getSVal(tempExpr, Pred->getLocationContext()); 3610e25031STed Kremenek 3710e25031STed Kremenek const MemRegion *R = 38632e3b7eSTed Kremenek svalBuilder.getRegionManager().getCXXTempObjectRegion(ME, LCtx); 3910e25031STed Kremenek 4010e25031STed Kremenek state = state->bindLoc(loc::MemRegionVal(R), V); 41632e3b7eSTed Kremenek Bldr.generateNode(ME, Pred, state->BindExpr(ME, LCtx, loc::MemRegionVal(R))); 4210e25031STed Kremenek } 4310e25031STed Kremenek 44f3c12ac3SJordan Rose void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *CE, 4510e25031STed Kremenek ExplodedNode *Pred, 4610e25031STed Kremenek ExplodedNodeSet &destNodes) { 47b970505dSJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 4805375eb4SJordan Rose ProgramStateRef State = Pred->getState(); 4905375eb4SJordan Rose 50b970505dSJordan Rose const MemRegion *Target = 0; 51b970505dSJordan Rose 52b970505dSJordan Rose switch (CE->getConstructionKind()) { 53b970505dSJordan Rose case CXXConstructExpr::CK_Complete: { 5405375eb4SJordan Rose // See if we're constructing an existing region by looking at the next 5505375eb4SJordan Rose // element in the CFG. 5605375eb4SJordan Rose const CFGBlock *B = currentBuilderContext->getBlock(); 5705375eb4SJordan Rose if (currentStmtIdx + 1 < B->size()) { 5805375eb4SJordan Rose CFGElement Next = (*B)[currentStmtIdx+1]; 5905375eb4SJordan Rose 6005375eb4SJordan Rose // Is this a constructor for a local variable? 6120edae87SJordan Rose if (const CFGStmt *StmtElem = dyn_cast<CFGStmt>(&Next)) { 6220edae87SJordan Rose if (const DeclStmt *DS = dyn_cast<DeclStmt>(StmtElem->getStmt())) { 6320edae87SJordan Rose if (const VarDecl *Var = dyn_cast<VarDecl>(DS->getSingleDecl())) { 6420edae87SJordan Rose if (Var->getInit()->IgnoreImplicit() == CE) { 6520edae87SJordan Rose QualType Ty = Var->getType(); 6620edae87SJordan Rose if (const ArrayType *AT = getContext().getAsArrayType(Ty)) { 6720edae87SJordan Rose // FIXME: Handle arrays, which run the same constructor for 6820edae87SJordan Rose // every element. This workaround will just run the first 6920edae87SJordan Rose // constructor (which should still invalidate the entire array). 7020edae87SJordan Rose SVal Base = State->getLValue(Var, LCtx); 7120edae87SJordan Rose Target = State->getLValue(AT->getElementType(), 7220edae87SJordan Rose getSValBuilder().makeZeroArrayIndex(), 7320edae87SJordan Rose Base).getAsRegion(); 7420edae87SJordan Rose } else { 7505375eb4SJordan Rose Target = State->getLValue(Var, LCtx).getAsRegion(); 7620edae87SJordan Rose } 7720edae87SJordan Rose } 7820edae87SJordan Rose } 7920edae87SJordan Rose } 8020edae87SJordan Rose } 8105375eb4SJordan Rose 8254529a34SJordan Rose // Is this a constructor for a member? 8354529a34SJordan Rose if (const CFGInitializer *InitElem = dyn_cast<CFGInitializer>(&Next)) { 8454529a34SJordan Rose const CXXCtorInitializer *Init = InitElem->getInitializer(); 8554529a34SJordan Rose assert(Init->isAnyMemberInitializer()); 8654529a34SJordan Rose 8754529a34SJordan Rose const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl()); 8854529a34SJordan Rose Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor, 8954529a34SJordan Rose LCtx->getCurrentStackFrame()); 9054529a34SJordan Rose SVal ThisVal = State->getSVal(ThisPtr); 9154529a34SJordan Rose 9254529a34SJordan Rose if (Init->isIndirectMemberInitializer()) { 9354529a34SJordan Rose SVal Field = State->getLValue(Init->getIndirectMember(), ThisVal); 9420edae87SJordan Rose Target = Field.getAsRegion(); 9554529a34SJordan Rose } else { 9654529a34SJordan Rose SVal Field = State->getLValue(Init->getMember(), ThisVal); 9720edae87SJordan Rose Target = Field.getAsRegion(); 9854529a34SJordan Rose } 9954529a34SJordan Rose } 10054529a34SJordan Rose 10105375eb4SJordan Rose // FIXME: This will eventually need to handle new-expressions as well. 10205375eb4SJordan Rose } 10305375eb4SJordan Rose 10405375eb4SJordan Rose // If we couldn't find an existing region to construct into, we'll just 105b970505dSJordan Rose // generate a symbolic region, which is fine. 10605375eb4SJordan Rose 107b970505dSJordan Rose break; 108b970505dSJordan Rose } 109b970505dSJordan Rose case CXXConstructExpr::CK_NonVirtualBase: 110b970505dSJordan Rose case CXXConstructExpr::CK_VirtualBase: 111b970505dSJordan Rose case CXXConstructExpr::CK_Delegating: { 112b970505dSJordan Rose const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl()); 113b970505dSJordan Rose Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor, 114b970505dSJordan Rose LCtx->getCurrentStackFrame()); 11505375eb4SJordan Rose SVal ThisVal = State->getSVal(ThisPtr); 116b970505dSJordan Rose 117b970505dSJordan Rose if (CE->getConstructionKind() == CXXConstructExpr::CK_Delegating) { 118b970505dSJordan Rose Target = ThisVal.getAsRegion(); 119b970505dSJordan Rose } else { 120b970505dSJordan Rose // Cast to the base type. 121b970505dSJordan Rose QualType BaseTy = CE->getType(); 122b970505dSJordan Rose SVal BaseVal = getStoreManager().evalDerivedToBase(ThisVal, BaseTy); 12320edae87SJordan Rose Target = BaseVal.getAsRegion(); 124b970505dSJordan Rose } 125b970505dSJordan Rose break; 126b970505dSJordan Rose } 127b970505dSJordan Rose } 128b970505dSJordan Rose 129fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 130fcd016e5SJordan Rose CallEventRef<CXXConstructorCall> Call = 131fcd016e5SJordan Rose CEMgr.getCXXConstructorCall(CE, Target, State, LCtx); 13210e25031STed Kremenek 133f3c12ac3SJordan Rose ExplodedNodeSet DstPreVisit; 134f3c12ac3SJordan Rose getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, CE, *this); 135afe7c2c2SJordan Rose ExplodedNodeSet DstPreCall; 136afe7c2c2SJordan Rose getCheckerManager().runCheckersForPreCall(DstPreCall, DstPreVisit, 137fcd016e5SJordan Rose *Call, *this); 13810e25031STed Kremenek 139f3c12ac3SJordan Rose ExplodedNodeSet DstInvalidated; 1403136cf9dSAnna Zaks StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currentBuilderContext); 141afe7c2c2SJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 142f3c12ac3SJordan Rose I != E; ++I) 143fcd016e5SJordan Rose defaultEvalCall(Bldr, *I, *Call); 14410e25031STed Kremenek 145afe7c2c2SJordan Rose ExplodedNodeSet DstPostCall; 146afe7c2c2SJordan Rose getCheckerManager().runCheckersForPostCall(DstPostCall, DstInvalidated, 147fcd016e5SJordan Rose *Call, *this); 148afe7c2c2SJordan Rose getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, CE, *this); 14910e25031STed Kremenek } 15010e25031STed Kremenek 151b970505dSJordan Rose void ExprEngine::VisitCXXDestructor(QualType ObjectType, 15210e25031STed Kremenek const MemRegion *Dest, 15310e25031STed Kremenek const Stmt *S, 15410e25031STed Kremenek ExplodedNode *Pred, 15510e25031STed Kremenek ExplodedNodeSet &Dst) { 156fcd016e5SJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 157fcd016e5SJordan Rose ProgramStateRef State = Pred->getState(); 158fcd016e5SJordan Rose 15920edae87SJordan Rose // FIXME: We need to run the same destructor on every element of the array. 16020edae87SJordan Rose // This workaround will just run the first destructor (which will still 16120edae87SJordan Rose // invalidate the entire array). 16220edae87SJordan Rose if (const ArrayType *AT = getContext().getAsArrayType(ObjectType)) { 16320edae87SJordan Rose ObjectType = AT->getElementType(); 164fcd016e5SJordan Rose Dest = State->getLValue(ObjectType, getSValBuilder().makeZeroArrayIndex(), 16520edae87SJordan Rose loc::MemRegionVal(Dest)).getAsRegion(); 16620edae87SJordan Rose } 16720edae87SJordan Rose 168b970505dSJordan Rose const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl(); 169b970505dSJordan Rose assert(RecordDecl && "Only CXXRecordDecls should have destructors"); 170b970505dSJordan Rose const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor(); 171b970505dSJordan Rose 172fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 173fcd016e5SJordan Rose CallEventRef<CXXDestructorCall> Call = 174fcd016e5SJordan Rose CEMgr.getCXXDestructorCall(DtorDecl, S, Dest, State, LCtx); 175142adc49STed Kremenek 176*17a8757aSJordan Rose PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(), 177*17a8757aSJordan Rose Call->getSourceRange().getBegin(), 178*17a8757aSJordan Rose "Error evaluating destructor"); 179*17a8757aSJordan Rose 1804ee71b8aSJordan Rose ExplodedNodeSet DstPreCall; 1814ee71b8aSJordan Rose getCheckerManager().runCheckersForPreCall(DstPreCall, Pred, 182fcd016e5SJordan Rose *Call, *this); 18310e25031STed Kremenek 1844ee71b8aSJordan Rose ExplodedNodeSet DstInvalidated; 1853136cf9dSAnna Zaks StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currentBuilderContext); 1864ee71b8aSJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 1874ee71b8aSJordan Rose I != E; ++I) 188fcd016e5SJordan Rose defaultEvalCall(Bldr, *I, *Call); 1894ee71b8aSJordan Rose 1904ee71b8aSJordan Rose ExplodedNodeSet DstPostCall; 1914ee71b8aSJordan Rose getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated, 192fcd016e5SJordan Rose *Call, *this); 19310e25031STed Kremenek } 19410e25031STed Kremenek 19510e25031STed Kremenek void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred, 19610e25031STed Kremenek ExplodedNodeSet &Dst) { 197a4ee064cSJordan Rose // FIXME: Much of this should eventually migrate to CXXAllocatorCall. 198a4ee064cSJordan Rose // Also, we need to decide how allocators actually work -- they're not 199a4ee064cSJordan Rose // really part of the CXXNewExpr because they happen BEFORE the 200a4ee064cSJordan Rose // CXXConstructExpr subexpression. See PR12014 for some discussion. 201e4e5712cSAnna Zaks StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 20210e25031STed Kremenek 203e594034fSAnna Zaks unsigned blockCount = currentBuilderContext->getCurrentBlockCount(); 204d519cae8STed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 20510e25031STed Kremenek DefinedOrUnknownSVal symVal = 206a4ee064cSJordan Rose svalBuilder.getConjuredSymbolVal(0, CNE, LCtx, CNE->getType(), blockCount); 20744320367SJordan Rose ProgramStateRef State = Pred->getState(); 20810e25031STed Kremenek 209fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 210fcd016e5SJordan Rose CallEventRef<CXXAllocatorCall> Call = 211fcd016e5SJordan Rose CEMgr.getCXXAllocatorCall(CNE, State, LCtx); 212fcd016e5SJordan Rose 213a4ee064cSJordan Rose // Invalidate placement args. 214fcd016e5SJordan Rose // FIXME: Once we figure out how we want allocators to work, 215fcd016e5SJordan Rose // we should be using the usual pre-/(default-)eval-/post-call checks here. 216fcd016e5SJordan Rose State = Call->invalidateRegions(blockCount); 217a4ee064cSJordan Rose 21810e25031STed Kremenek if (CNE->isArray()) { 21910e25031STed Kremenek // FIXME: allocating an array requires simulating the constructors. 22010e25031STed Kremenek // For now, just return a symbolicated region. 221a4ee064cSJordan Rose const MemRegion *NewReg = cast<loc::MemRegionVal>(symVal).getRegion(); 222a4ee064cSJordan Rose QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType(); 223a4ee064cSJordan Rose const ElementRegion *EleReg = 224a4ee064cSJordan Rose getStoreManager().GetElementZeroRegion(NewReg, ObjTy); 22544320367SJordan Rose State = State->BindExpr(CNE, Pred->getLocationContext(), 226632e3b7eSTed Kremenek loc::MemRegionVal(EleReg)); 22744320367SJordan Rose Bldr.generateNode(CNE, Pred, State); 22810e25031STed Kremenek return; 22910e25031STed Kremenek } 23010e25031STed Kremenek 231a4ee064cSJordan Rose // FIXME: Once we have proper support for CXXConstructExprs inside 232a4ee064cSJordan Rose // CXXNewExpr, we need to make sure that the constructed object is not 233a4ee064cSJordan Rose // immediately invalidated here. (The placement call should happen before 234a4ee064cSJordan Rose // the constructor call anyway.) 23544320367SJordan Rose FunctionDecl *FD = CNE->getOperatorNew(); 23644320367SJordan Rose if (FD && FD->isReservedGlobalPlacementOperator()) { 23744320367SJordan Rose // Non-array placement new should always return the placement location. 23844320367SJordan Rose SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx); 23944320367SJordan Rose State = State->BindExpr(CNE, LCtx, PlacementLoc); 24044320367SJordan Rose } else { 241a4ee064cSJordan Rose State = State->BindExpr(CNE, LCtx, symVal); 24244320367SJordan Rose } 24344320367SJordan Rose 2445089f3b3SJordan Rose // If the type is not a record, we won't have a CXXConstructExpr as an 2455089f3b3SJordan Rose // initializer. Copy the value over. 2465089f3b3SJordan Rose if (const Expr *Init = CNE->getInitializer()) { 2475089f3b3SJordan Rose if (!isa<CXXConstructExpr>(Init)) { 2485089f3b3SJordan Rose QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType(); 2495089f3b3SJordan Rose (void)ObjTy; 2505089f3b3SJordan Rose assert(!ObjTy->isRecordType()); 2515089f3b3SJordan Rose SVal Location = State->getSVal(CNE, LCtx); 2525089f3b3SJordan Rose if (isa<Loc>(Location)) 2535089f3b3SJordan Rose State = State->bindLoc(cast<Loc>(Location), State->getSVal(Init, LCtx)); 2545089f3b3SJordan Rose } 2555089f3b3SJordan Rose } 2565089f3b3SJordan Rose 25744320367SJordan Rose Bldr.generateNode(CNE, Pred, State); 25810e25031STed Kremenek } 25910e25031STed Kremenek 26010e25031STed Kremenek void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE, 26110e25031STed Kremenek ExplodedNode *Pred, ExplodedNodeSet &Dst) { 26264aea652SErik Verbruggen StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 26364aea652SErik Verbruggen ProgramStateRef state = Pred->getState(); 26464aea652SErik Verbruggen Bldr.generateNode(CDE, Pred, state); 26510e25031STed Kremenek } 26610e25031STed Kremenek 2678fdb59f9STed Kremenek void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS, 2688fdb59f9STed Kremenek ExplodedNode *Pred, 2698fdb59f9STed Kremenek ExplodedNodeSet &Dst) { 2708fdb59f9STed Kremenek const VarDecl *VD = CS->getExceptionDecl(); 271b1279b5cSTed Kremenek if (!VD) { 272b1279b5cSTed Kremenek Dst.Add(Pred); 273b1279b5cSTed Kremenek return; 274b1279b5cSTed Kremenek } 275b1279b5cSTed Kremenek 2768fdb59f9STed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 2778fdb59f9STed Kremenek SVal V = svalBuilder.getConjuredSymbolVal(CS, LCtx, VD->getType(), 2788fdb59f9STed Kremenek currentBuilderContext->getCurrentBlockCount()); 2798fdb59f9STed Kremenek ProgramStateRef state = Pred->getState(); 2808fdb59f9STed Kremenek state = state->bindLoc(state->getLValue(VD, LCtx), V); 2818fdb59f9STed Kremenek 2828fdb59f9STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 2838fdb59f9STed Kremenek Bldr.generateNode(CS, Pred, state); 2848fdb59f9STed Kremenek } 2858fdb59f9STed Kremenek 28610e25031STed Kremenek void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred, 28710e25031STed Kremenek ExplodedNodeSet &Dst) { 288e4e5712cSAnna Zaks StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 289e594034fSAnna Zaks 29010e25031STed Kremenek // Get the this object region from StoreManager. 291632e3b7eSTed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 29210e25031STed Kremenek const MemRegion *R = 29310e25031STed Kremenek svalBuilder.getRegionManager().getCXXThisRegion( 29410e25031STed Kremenek getContext().getCanonicalType(TE->getType()), 295632e3b7eSTed Kremenek LCtx); 29610e25031STed Kremenek 29749b1e38eSTed Kremenek ProgramStateRef state = Pred->getState(); 29810e25031STed Kremenek SVal V = state->getSVal(loc::MemRegionVal(R)); 299632e3b7eSTed Kremenek Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V)); 30010e25031STed Kremenek } 301