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" 2017a8757aSJordan 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) { 28d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 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. 35*c9318304SJordan Rose SVal V = state->getSVal(tempExpr, LCtx); 3610e25031STed Kremenek 37*c9318304SJordan Rose // If the object is a record, the constructor will have already created 38*c9318304SJordan Rose // a temporary object region. If it is not, we need to copy the value over. 39*c9318304SJordan Rose if (!ME->getType()->isRecordType()) { 4010e25031STed Kremenek const MemRegion *R = 41632e3b7eSTed Kremenek svalBuilder.getRegionManager().getCXXTempObjectRegion(ME, LCtx); 4210e25031STed Kremenek 43*c9318304SJordan Rose SVal L = loc::MemRegionVal(R); 44*c9318304SJordan Rose state = state->bindLoc(L, V); 45*c9318304SJordan Rose V = L; 46*c9318304SJordan Rose } 47*c9318304SJordan Rose 48*c9318304SJordan Rose Bldr.generateNode(ME, Pred, state->BindExpr(ME, LCtx, V)); 4910e25031STed Kremenek } 5010e25031STed Kremenek 51f3c12ac3SJordan Rose void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *CE, 5210e25031STed Kremenek ExplodedNode *Pred, 5310e25031STed Kremenek ExplodedNodeSet &destNodes) { 54b970505dSJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 5505375eb4SJordan Rose ProgramStateRef State = Pred->getState(); 5605375eb4SJordan Rose 57b970505dSJordan Rose const MemRegion *Target = 0; 58b970505dSJordan Rose 59b970505dSJordan Rose switch (CE->getConstructionKind()) { 60b970505dSJordan Rose case CXXConstructExpr::CK_Complete: { 6105375eb4SJordan Rose // See if we're constructing an existing region by looking at the next 6205375eb4SJordan Rose // element in the CFG. 63d94854a4STed Kremenek const CFGBlock *B = currBldrCtx->getBlock(); 64d94854a4STed Kremenek if (currStmtIdx + 1 < B->size()) { 65d94854a4STed Kremenek CFGElement Next = (*B)[currStmtIdx+1]; 6605375eb4SJordan Rose 6705375eb4SJordan Rose // Is this a constructor for a local variable? 6820edae87SJordan Rose if (const CFGStmt *StmtElem = dyn_cast<CFGStmt>(&Next)) { 6920edae87SJordan Rose if (const DeclStmt *DS = dyn_cast<DeclStmt>(StmtElem->getStmt())) { 7020edae87SJordan Rose if (const VarDecl *Var = dyn_cast<VarDecl>(DS->getSingleDecl())) { 7120edae87SJordan Rose if (Var->getInit()->IgnoreImplicit() == CE) { 7220edae87SJordan Rose QualType Ty = Var->getType(); 7320edae87SJordan Rose if (const ArrayType *AT = getContext().getAsArrayType(Ty)) { 7420edae87SJordan Rose // FIXME: Handle arrays, which run the same constructor for 7520edae87SJordan Rose // every element. This workaround will just run the first 7620edae87SJordan Rose // constructor (which should still invalidate the entire array). 7720edae87SJordan Rose SVal Base = State->getLValue(Var, LCtx); 7820edae87SJordan Rose Target = State->getLValue(AT->getElementType(), 7920edae87SJordan Rose getSValBuilder().makeZeroArrayIndex(), 8020edae87SJordan Rose Base).getAsRegion(); 8120edae87SJordan Rose } else { 8205375eb4SJordan Rose Target = State->getLValue(Var, LCtx).getAsRegion(); 8320edae87SJordan Rose } 8420edae87SJordan Rose } 8520edae87SJordan Rose } 8620edae87SJordan Rose } 8720edae87SJordan Rose } 8805375eb4SJordan Rose 8954529a34SJordan Rose // Is this a constructor for a member? 9054529a34SJordan Rose if (const CFGInitializer *InitElem = dyn_cast<CFGInitializer>(&Next)) { 9154529a34SJordan Rose const CXXCtorInitializer *Init = InitElem->getInitializer(); 9254529a34SJordan Rose assert(Init->isAnyMemberInitializer()); 9354529a34SJordan Rose 9454529a34SJordan Rose const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl()); 9554529a34SJordan Rose Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor, 9654529a34SJordan Rose LCtx->getCurrentStackFrame()); 9754529a34SJordan Rose SVal ThisVal = State->getSVal(ThisPtr); 9854529a34SJordan Rose 9954529a34SJordan Rose if (Init->isIndirectMemberInitializer()) { 10054529a34SJordan Rose SVal Field = State->getLValue(Init->getIndirectMember(), ThisVal); 10120edae87SJordan Rose Target = Field.getAsRegion(); 10254529a34SJordan Rose } else { 10354529a34SJordan Rose SVal Field = State->getLValue(Init->getMember(), ThisVal); 10420edae87SJordan Rose Target = Field.getAsRegion(); 10554529a34SJordan Rose } 10654529a34SJordan Rose } 10754529a34SJordan Rose 10805375eb4SJordan Rose // FIXME: This will eventually need to handle new-expressions as well. 10905375eb4SJordan Rose } 11005375eb4SJordan Rose 111*c9318304SJordan Rose // If we couldn't find an existing region to construct into, assume we're 112*c9318304SJordan Rose // constructing a temporary. 113*c9318304SJordan Rose if (!Target) { 114*c9318304SJordan Rose MemRegionManager &MRMgr = getSValBuilder().getRegionManager(); 115*c9318304SJordan Rose Target = MRMgr.getCXXTempObjectRegion(CE, LCtx); 116*c9318304SJordan Rose } 11705375eb4SJordan Rose 118b970505dSJordan Rose break; 119b970505dSJordan Rose } 120b970505dSJordan Rose case CXXConstructExpr::CK_NonVirtualBase: 121b970505dSJordan Rose case CXXConstructExpr::CK_VirtualBase: 122b970505dSJordan Rose case CXXConstructExpr::CK_Delegating: { 123b970505dSJordan Rose const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl()); 124b970505dSJordan Rose Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor, 125b970505dSJordan Rose LCtx->getCurrentStackFrame()); 12605375eb4SJordan Rose SVal ThisVal = State->getSVal(ThisPtr); 127b970505dSJordan Rose 128b970505dSJordan Rose if (CE->getConstructionKind() == CXXConstructExpr::CK_Delegating) { 129b970505dSJordan Rose Target = ThisVal.getAsRegion(); 130b970505dSJordan Rose } else { 131b970505dSJordan Rose // Cast to the base type. 132b970505dSJordan Rose QualType BaseTy = CE->getType(); 133b970505dSJordan Rose SVal BaseVal = getStoreManager().evalDerivedToBase(ThisVal, BaseTy); 13420edae87SJordan Rose Target = BaseVal.getAsRegion(); 135b970505dSJordan Rose } 136b970505dSJordan Rose break; 137b970505dSJordan Rose } 138b970505dSJordan Rose } 139b970505dSJordan Rose 140fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 141fcd016e5SJordan Rose CallEventRef<CXXConstructorCall> Call = 142fcd016e5SJordan Rose CEMgr.getCXXConstructorCall(CE, Target, State, LCtx); 14310e25031STed Kremenek 144f3c12ac3SJordan Rose ExplodedNodeSet DstPreVisit; 145f3c12ac3SJordan Rose getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, CE, *this); 146afe7c2c2SJordan Rose ExplodedNodeSet DstPreCall; 147afe7c2c2SJordan Rose getCheckerManager().runCheckersForPreCall(DstPreCall, DstPreVisit, 148fcd016e5SJordan Rose *Call, *this); 14910e25031STed Kremenek 150f3c12ac3SJordan Rose ExplodedNodeSet DstInvalidated; 151d94854a4STed Kremenek StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx); 152afe7c2c2SJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 153f3c12ac3SJordan Rose I != E; ++I) 154fcd016e5SJordan Rose defaultEvalCall(Bldr, *I, *Call); 15510e25031STed Kremenek 156afe7c2c2SJordan Rose ExplodedNodeSet DstPostCall; 157afe7c2c2SJordan Rose getCheckerManager().runCheckersForPostCall(DstPostCall, DstInvalidated, 158fcd016e5SJordan Rose *Call, *this); 159afe7c2c2SJordan Rose getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, CE, *this); 16010e25031STed Kremenek } 16110e25031STed Kremenek 162b970505dSJordan Rose void ExprEngine::VisitCXXDestructor(QualType ObjectType, 16310e25031STed Kremenek const MemRegion *Dest, 16410e25031STed Kremenek const Stmt *S, 16510e25031STed Kremenek ExplodedNode *Pred, 16610e25031STed Kremenek ExplodedNodeSet &Dst) { 167fcd016e5SJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 168fcd016e5SJordan Rose ProgramStateRef State = Pred->getState(); 169fcd016e5SJordan Rose 17020edae87SJordan Rose // FIXME: We need to run the same destructor on every element of the array. 17120edae87SJordan Rose // This workaround will just run the first destructor (which will still 17220edae87SJordan Rose // invalidate the entire array). 17320edae87SJordan Rose if (const ArrayType *AT = getContext().getAsArrayType(ObjectType)) { 17420edae87SJordan Rose ObjectType = AT->getElementType(); 175fcd016e5SJordan Rose Dest = State->getLValue(ObjectType, getSValBuilder().makeZeroArrayIndex(), 17620edae87SJordan Rose loc::MemRegionVal(Dest)).getAsRegion(); 17720edae87SJordan Rose } 17820edae87SJordan Rose 179b970505dSJordan Rose const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl(); 180b970505dSJordan Rose assert(RecordDecl && "Only CXXRecordDecls should have destructors"); 181b970505dSJordan Rose const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor(); 182b970505dSJordan Rose 183fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 184fcd016e5SJordan Rose CallEventRef<CXXDestructorCall> Call = 185fcd016e5SJordan Rose CEMgr.getCXXDestructorCall(DtorDecl, S, Dest, State, LCtx); 186142adc49STed Kremenek 18717a8757aSJordan Rose PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(), 18817a8757aSJordan Rose Call->getSourceRange().getBegin(), 18917a8757aSJordan Rose "Error evaluating destructor"); 19017a8757aSJordan Rose 1914ee71b8aSJordan Rose ExplodedNodeSet DstPreCall; 1924ee71b8aSJordan Rose getCheckerManager().runCheckersForPreCall(DstPreCall, Pred, 193fcd016e5SJordan Rose *Call, *this); 19410e25031STed Kremenek 1954ee71b8aSJordan Rose ExplodedNodeSet DstInvalidated; 196d94854a4STed Kremenek StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx); 1974ee71b8aSJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 1984ee71b8aSJordan Rose I != E; ++I) 199fcd016e5SJordan Rose defaultEvalCall(Bldr, *I, *Call); 2004ee71b8aSJordan Rose 2014ee71b8aSJordan Rose ExplodedNodeSet DstPostCall; 2024ee71b8aSJordan Rose getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated, 203fcd016e5SJordan Rose *Call, *this); 20410e25031STed Kremenek } 20510e25031STed Kremenek 20610e25031STed Kremenek void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred, 20710e25031STed Kremenek ExplodedNodeSet &Dst) { 208a4ee064cSJordan Rose // FIXME: Much of this should eventually migrate to CXXAllocatorCall. 209a4ee064cSJordan Rose // Also, we need to decide how allocators actually work -- they're not 210a4ee064cSJordan Rose // really part of the CXXNewExpr because they happen BEFORE the 211a4ee064cSJordan Rose // CXXConstructExpr subexpression. See PR12014 for some discussion. 212d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 21310e25031STed Kremenek 214d94854a4STed Kremenek unsigned blockCount = currBldrCtx->blockCount(); 215d519cae8STed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 216d227833cSTed Kremenek DefinedOrUnknownSVal symVal = svalBuilder.conjureSymbolVal(0, CNE, LCtx, 217d227833cSTed Kremenek CNE->getType(), 218d227833cSTed Kremenek blockCount); 21944320367SJordan Rose ProgramStateRef State = Pred->getState(); 22010e25031STed Kremenek 221fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 222fcd016e5SJordan Rose CallEventRef<CXXAllocatorCall> Call = 223fcd016e5SJordan Rose CEMgr.getCXXAllocatorCall(CNE, State, LCtx); 224fcd016e5SJordan Rose 225a4ee064cSJordan Rose // Invalidate placement args. 226fcd016e5SJordan Rose // FIXME: Once we figure out how we want allocators to work, 227fcd016e5SJordan Rose // we should be using the usual pre-/(default-)eval-/post-call checks here. 228fcd016e5SJordan Rose State = Call->invalidateRegions(blockCount); 229a4ee064cSJordan Rose 23010e25031STed Kremenek if (CNE->isArray()) { 23110e25031STed Kremenek // FIXME: allocating an array requires simulating the constructors. 23210e25031STed Kremenek // For now, just return a symbolicated region. 233a4ee064cSJordan Rose const MemRegion *NewReg = cast<loc::MemRegionVal>(symVal).getRegion(); 234a4ee064cSJordan Rose QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType(); 235a4ee064cSJordan Rose const ElementRegion *EleReg = 236a4ee064cSJordan Rose getStoreManager().GetElementZeroRegion(NewReg, ObjTy); 23744320367SJordan Rose State = State->BindExpr(CNE, Pred->getLocationContext(), 238632e3b7eSTed Kremenek loc::MemRegionVal(EleReg)); 23944320367SJordan Rose Bldr.generateNode(CNE, Pred, State); 24010e25031STed Kremenek return; 24110e25031STed Kremenek } 24210e25031STed Kremenek 243a4ee064cSJordan Rose // FIXME: Once we have proper support for CXXConstructExprs inside 244a4ee064cSJordan Rose // CXXNewExpr, we need to make sure that the constructed object is not 245a4ee064cSJordan Rose // immediately invalidated here. (The placement call should happen before 246a4ee064cSJordan Rose // the constructor call anyway.) 24744320367SJordan Rose FunctionDecl *FD = CNE->getOperatorNew(); 24844320367SJordan Rose if (FD && FD->isReservedGlobalPlacementOperator()) { 24944320367SJordan Rose // Non-array placement new should always return the placement location. 25044320367SJordan Rose SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx); 25144320367SJordan Rose State = State->BindExpr(CNE, LCtx, PlacementLoc); 25244320367SJordan Rose } else { 253a4ee064cSJordan Rose State = State->BindExpr(CNE, LCtx, symVal); 25444320367SJordan Rose } 25544320367SJordan Rose 2565089f3b3SJordan Rose // If the type is not a record, we won't have a CXXConstructExpr as an 2575089f3b3SJordan Rose // initializer. Copy the value over. 2585089f3b3SJordan Rose if (const Expr *Init = CNE->getInitializer()) { 2595089f3b3SJordan Rose if (!isa<CXXConstructExpr>(Init)) { 2605089f3b3SJordan Rose QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType(); 2615089f3b3SJordan Rose (void)ObjTy; 2625089f3b3SJordan Rose assert(!ObjTy->isRecordType()); 2635089f3b3SJordan Rose SVal Location = State->getSVal(CNE, LCtx); 2645089f3b3SJordan Rose if (isa<Loc>(Location)) 2655089f3b3SJordan Rose State = State->bindLoc(cast<Loc>(Location), State->getSVal(Init, LCtx)); 2665089f3b3SJordan Rose } 2675089f3b3SJordan Rose } 2685089f3b3SJordan Rose 26944320367SJordan Rose Bldr.generateNode(CNE, Pred, State); 27010e25031STed Kremenek } 27110e25031STed Kremenek 27210e25031STed Kremenek void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE, 27310e25031STed Kremenek ExplodedNode *Pred, ExplodedNodeSet &Dst) { 274d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 27564aea652SErik Verbruggen ProgramStateRef state = Pred->getState(); 27664aea652SErik Verbruggen Bldr.generateNode(CDE, Pred, state); 27710e25031STed Kremenek } 27810e25031STed Kremenek 2798fdb59f9STed Kremenek void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS, 2808fdb59f9STed Kremenek ExplodedNode *Pred, 2818fdb59f9STed Kremenek ExplodedNodeSet &Dst) { 2828fdb59f9STed Kremenek const VarDecl *VD = CS->getExceptionDecl(); 283b1279b5cSTed Kremenek if (!VD) { 284b1279b5cSTed Kremenek Dst.Add(Pred); 285b1279b5cSTed Kremenek return; 286b1279b5cSTed Kremenek } 287b1279b5cSTed Kremenek 2888fdb59f9STed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 289d227833cSTed Kremenek SVal V = svalBuilder.conjureSymbolVal(CS, LCtx, VD->getType(), 290d94854a4STed Kremenek currBldrCtx->blockCount()); 2918fdb59f9STed Kremenek ProgramStateRef state = Pred->getState(); 2928fdb59f9STed Kremenek state = state->bindLoc(state->getLValue(VD, LCtx), V); 2938fdb59f9STed Kremenek 294d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 2958fdb59f9STed Kremenek Bldr.generateNode(CS, Pred, state); 2968fdb59f9STed Kremenek } 2978fdb59f9STed Kremenek 29810e25031STed Kremenek void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred, 29910e25031STed Kremenek ExplodedNodeSet &Dst) { 300d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 301e594034fSAnna Zaks 30210e25031STed Kremenek // Get the this object region from StoreManager. 303632e3b7eSTed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 30410e25031STed Kremenek const MemRegion *R = 30510e25031STed Kremenek svalBuilder.getRegionManager().getCXXThisRegion( 30610e25031STed Kremenek getContext().getCanonicalType(TE->getType()), 307632e3b7eSTed Kremenek LCtx); 30810e25031STed Kremenek 30949b1e38eSTed Kremenek ProgramStateRef state = Pred->getState(); 31010e25031STed Kremenek SVal V = state->getSVal(loc::MemRegionVal(R)); 311632e3b7eSTed Kremenek Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V)); 31210e25031STed Kremenek } 313