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/PathSensitive/ExprEngine.h" 1510e25031STed Kremenek #include "clang/AST/DeclCXX.h" 168fdb59f9STed Kremenek #include "clang/AST/StmtCXX.h" 1717a8757aSJordan Rose #include "clang/Basic/PrettyStackTrace.h" 18*3a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/CheckerManager.h" 19*3a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" 20*3a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.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. 35c9318304SJordan Rose SVal V = state->getSVal(tempExpr, LCtx); 3610e25031STed Kremenek 37fcdda361SJordan Rose // If the value is already a CXXTempObjectRegion, it is fine as it is. 38fcdda361SJordan Rose // Otherwise, create a new CXXTempObjectRegion, and copy the value into it. 39fcdda361SJordan Rose const MemRegion *MR = V.getAsRegion(); 40fcdda361SJordan Rose if (!MR || !isa<CXXTempObjectRegion>(MR)) { 4110e25031STed Kremenek const MemRegion *R = 42632e3b7eSTed Kremenek svalBuilder.getRegionManager().getCXXTempObjectRegion(ME, LCtx); 4310e25031STed Kremenek 44c9318304SJordan Rose SVal L = loc::MemRegionVal(R); 45c9318304SJordan Rose state = state->bindLoc(L, V); 46c9318304SJordan Rose V = L; 47c9318304SJordan Rose } 48c9318304SJordan Rose 49c9318304SJordan Rose Bldr.generateNode(ME, Pred, state->BindExpr(ME, LCtx, V)); 5010e25031STed Kremenek } 5110e25031STed Kremenek 52f3c12ac3SJordan Rose void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *CE, 5310e25031STed Kremenek ExplodedNode *Pred, 5410e25031STed Kremenek ExplodedNodeSet &destNodes) { 55b970505dSJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 5605375eb4SJordan Rose ProgramStateRef State = Pred->getState(); 5705375eb4SJordan Rose 58b970505dSJordan Rose const MemRegion *Target = 0; 59b970505dSJordan Rose 60b970505dSJordan Rose switch (CE->getConstructionKind()) { 61b970505dSJordan Rose case CXXConstructExpr::CK_Complete: { 6205375eb4SJordan Rose // See if we're constructing an existing region by looking at the next 6305375eb4SJordan Rose // element in the CFG. 64d94854a4STed Kremenek const CFGBlock *B = currBldrCtx->getBlock(); 65d94854a4STed Kremenek if (currStmtIdx + 1 < B->size()) { 66d94854a4STed Kremenek CFGElement Next = (*B)[currStmtIdx+1]; 6705375eb4SJordan Rose 6805375eb4SJordan Rose // Is this a constructor for a local variable? 6920edae87SJordan Rose if (const CFGStmt *StmtElem = dyn_cast<CFGStmt>(&Next)) { 7020edae87SJordan Rose if (const DeclStmt *DS = dyn_cast<DeclStmt>(StmtElem->getStmt())) { 7120edae87SJordan Rose if (const VarDecl *Var = dyn_cast<VarDecl>(DS->getSingleDecl())) { 7220edae87SJordan Rose if (Var->getInit()->IgnoreImplicit() == CE) { 7320edae87SJordan Rose QualType Ty = Var->getType(); 7420edae87SJordan Rose if (const ArrayType *AT = getContext().getAsArrayType(Ty)) { 7520edae87SJordan Rose // FIXME: Handle arrays, which run the same constructor for 7620edae87SJordan Rose // every element. This workaround will just run the first 7720edae87SJordan Rose // constructor (which should still invalidate the entire array). 7820edae87SJordan Rose SVal Base = State->getLValue(Var, LCtx); 7920edae87SJordan Rose Target = State->getLValue(AT->getElementType(), 8020edae87SJordan Rose getSValBuilder().makeZeroArrayIndex(), 8120edae87SJordan Rose Base).getAsRegion(); 8220edae87SJordan Rose } else { 8305375eb4SJordan Rose Target = State->getLValue(Var, LCtx).getAsRegion(); 8420edae87SJordan Rose } 8520edae87SJordan Rose } 8620edae87SJordan Rose } 8720edae87SJordan Rose } 8820edae87SJordan Rose } 8905375eb4SJordan Rose 9054529a34SJordan Rose // Is this a constructor for a member? 9154529a34SJordan Rose if (const CFGInitializer *InitElem = dyn_cast<CFGInitializer>(&Next)) { 9254529a34SJordan Rose const CXXCtorInitializer *Init = InitElem->getInitializer(); 9354529a34SJordan Rose assert(Init->isAnyMemberInitializer()); 9454529a34SJordan Rose 9554529a34SJordan Rose const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl()); 9654529a34SJordan Rose Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor, 9754529a34SJordan Rose LCtx->getCurrentStackFrame()); 9854529a34SJordan Rose SVal ThisVal = State->getSVal(ThisPtr); 9954529a34SJordan Rose 10054529a34SJordan Rose if (Init->isIndirectMemberInitializer()) { 10154529a34SJordan Rose SVal Field = State->getLValue(Init->getIndirectMember(), ThisVal); 10220edae87SJordan Rose Target = Field.getAsRegion(); 10354529a34SJordan Rose } else { 10454529a34SJordan Rose SVal Field = State->getLValue(Init->getMember(), ThisVal); 10520edae87SJordan Rose Target = Field.getAsRegion(); 10654529a34SJordan Rose } 10754529a34SJordan Rose } 10854529a34SJordan Rose 10905375eb4SJordan Rose // FIXME: This will eventually need to handle new-expressions as well. 11005375eb4SJordan Rose } 11105375eb4SJordan Rose 112c9318304SJordan Rose // If we couldn't find an existing region to construct into, assume we're 113c9318304SJordan Rose // constructing a temporary. 114c9318304SJordan Rose if (!Target) { 115c9318304SJordan Rose MemRegionManager &MRMgr = getSValBuilder().getRegionManager(); 116c9318304SJordan Rose Target = MRMgr.getCXXTempObjectRegion(CE, LCtx); 117c9318304SJordan Rose } 11805375eb4SJordan Rose 119b970505dSJordan Rose break; 120b970505dSJordan Rose } 121b970505dSJordan Rose case CXXConstructExpr::CK_NonVirtualBase: 122b970505dSJordan Rose case CXXConstructExpr::CK_VirtualBase: 123b970505dSJordan Rose case CXXConstructExpr::CK_Delegating: { 124b970505dSJordan Rose const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl()); 125b970505dSJordan Rose Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor, 126b970505dSJordan Rose LCtx->getCurrentStackFrame()); 12705375eb4SJordan Rose SVal ThisVal = State->getSVal(ThisPtr); 128b970505dSJordan Rose 129b970505dSJordan Rose if (CE->getConstructionKind() == CXXConstructExpr::CK_Delegating) { 130b970505dSJordan Rose Target = ThisVal.getAsRegion(); 131b970505dSJordan Rose } else { 132b970505dSJordan Rose // Cast to the base type. 133b970505dSJordan Rose QualType BaseTy = CE->getType(); 134b970505dSJordan Rose SVal BaseVal = getStoreManager().evalDerivedToBase(ThisVal, BaseTy); 13520edae87SJordan Rose Target = BaseVal.getAsRegion(); 136b970505dSJordan Rose } 137b970505dSJordan Rose break; 138b970505dSJordan Rose } 139b970505dSJordan Rose } 140b970505dSJordan Rose 141fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 142fcd016e5SJordan Rose CallEventRef<CXXConstructorCall> Call = 143fcd016e5SJordan Rose CEMgr.getCXXConstructorCall(CE, Target, State, LCtx); 14410e25031STed Kremenek 145f3c12ac3SJordan Rose ExplodedNodeSet DstPreVisit; 146f3c12ac3SJordan Rose getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, CE, *this); 147afe7c2c2SJordan Rose ExplodedNodeSet DstPreCall; 148afe7c2c2SJordan Rose getCheckerManager().runCheckersForPreCall(DstPreCall, DstPreVisit, 149fcd016e5SJordan Rose *Call, *this); 15010e25031STed Kremenek 151f3c12ac3SJordan Rose ExplodedNodeSet DstInvalidated; 152d94854a4STed Kremenek StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx); 153afe7c2c2SJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 154f3c12ac3SJordan Rose I != E; ++I) 155fcd016e5SJordan Rose defaultEvalCall(Bldr, *I, *Call); 15610e25031STed Kremenek 157afe7c2c2SJordan Rose ExplodedNodeSet DstPostCall; 158afe7c2c2SJordan Rose getCheckerManager().runCheckersForPostCall(DstPostCall, DstInvalidated, 159fcd016e5SJordan Rose *Call, *this); 160afe7c2c2SJordan Rose getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, CE, *this); 16110e25031STed Kremenek } 16210e25031STed Kremenek 163b970505dSJordan Rose void ExprEngine::VisitCXXDestructor(QualType ObjectType, 16410e25031STed Kremenek const MemRegion *Dest, 16510e25031STed Kremenek const Stmt *S, 1662bc9674bSJordan Rose bool IsBaseDtor, 16710e25031STed Kremenek ExplodedNode *Pred, 16810e25031STed Kremenek ExplodedNodeSet &Dst) { 169fcd016e5SJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 170fcd016e5SJordan Rose ProgramStateRef State = Pred->getState(); 171fcd016e5SJordan Rose 17220edae87SJordan Rose // FIXME: We need to run the same destructor on every element of the array. 17320edae87SJordan Rose // This workaround will just run the first destructor (which will still 17420edae87SJordan Rose // invalidate the entire array). 17520edae87SJordan Rose if (const ArrayType *AT = getContext().getAsArrayType(ObjectType)) { 17620edae87SJordan Rose ObjectType = AT->getElementType(); 177fcd016e5SJordan Rose Dest = State->getLValue(ObjectType, getSValBuilder().makeZeroArrayIndex(), 17820edae87SJordan Rose loc::MemRegionVal(Dest)).getAsRegion(); 17920edae87SJordan Rose } 18020edae87SJordan Rose 181b970505dSJordan Rose const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl(); 182b970505dSJordan Rose assert(RecordDecl && "Only CXXRecordDecls should have destructors"); 183b970505dSJordan Rose const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor(); 184b970505dSJordan Rose 185fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 186fcd016e5SJordan Rose CallEventRef<CXXDestructorCall> Call = 1872bc9674bSJordan Rose CEMgr.getCXXDestructorCall(DtorDecl, S, Dest, IsBaseDtor, State, LCtx); 188142adc49STed Kremenek 18917a8757aSJordan Rose PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(), 19017a8757aSJordan Rose Call->getSourceRange().getBegin(), 19117a8757aSJordan Rose "Error evaluating destructor"); 19217a8757aSJordan Rose 1934ee71b8aSJordan Rose ExplodedNodeSet DstPreCall; 1944ee71b8aSJordan Rose getCheckerManager().runCheckersForPreCall(DstPreCall, Pred, 195fcd016e5SJordan Rose *Call, *this); 19610e25031STed Kremenek 1974ee71b8aSJordan Rose ExplodedNodeSet DstInvalidated; 198d94854a4STed Kremenek StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx); 1994ee71b8aSJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 2004ee71b8aSJordan Rose I != E; ++I) 201fcd016e5SJordan Rose defaultEvalCall(Bldr, *I, *Call); 2024ee71b8aSJordan Rose 2034ee71b8aSJordan Rose ExplodedNodeSet DstPostCall; 2044ee71b8aSJordan Rose getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated, 205fcd016e5SJordan Rose *Call, *this); 20610e25031STed Kremenek } 20710e25031STed Kremenek 20810e25031STed Kremenek void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred, 20910e25031STed Kremenek ExplodedNodeSet &Dst) { 210a4ee064cSJordan Rose // FIXME: Much of this should eventually migrate to CXXAllocatorCall. 211a4ee064cSJordan Rose // Also, we need to decide how allocators actually work -- they're not 212a4ee064cSJordan Rose // really part of the CXXNewExpr because they happen BEFORE the 213a4ee064cSJordan Rose // CXXConstructExpr subexpression. See PR12014 for some discussion. 214d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 21510e25031STed Kremenek 216d94854a4STed Kremenek unsigned blockCount = currBldrCtx->blockCount(); 217d519cae8STed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 218d227833cSTed Kremenek DefinedOrUnknownSVal symVal = svalBuilder.conjureSymbolVal(0, CNE, LCtx, 219d227833cSTed Kremenek CNE->getType(), 220d227833cSTed Kremenek blockCount); 22144320367SJordan Rose ProgramStateRef State = Pred->getState(); 22210e25031STed Kremenek 223fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 224fcd016e5SJordan Rose CallEventRef<CXXAllocatorCall> Call = 225fcd016e5SJordan Rose CEMgr.getCXXAllocatorCall(CNE, State, LCtx); 226fcd016e5SJordan Rose 227a4ee064cSJordan Rose // Invalidate placement args. 228fcd016e5SJordan Rose // FIXME: Once we figure out how we want allocators to work, 229fcd016e5SJordan Rose // we should be using the usual pre-/(default-)eval-/post-call checks here. 230fcd016e5SJordan Rose State = Call->invalidateRegions(blockCount); 231a4ee064cSJordan Rose 2323957fd58SJordan Rose // If we're compiling with exceptions enabled, and this allocation function 2333957fd58SJordan Rose // is not declared as non-throwing, failures /must/ be signalled by 2343957fd58SJordan Rose // exceptions, and thus the return value will never be NULL. 2353957fd58SJordan Rose // C++11 [basic.stc.dynamic.allocation]p3. 2363957fd58SJordan Rose FunctionDecl *FD = CNE->getOperatorNew(); 2373957fd58SJordan Rose if (FD && getContext().getLangOpts().CXXExceptions) { 2383957fd58SJordan Rose QualType Ty = FD->getType(); 2393957fd58SJordan Rose if (const FunctionProtoType *ProtoType = Ty->getAs<FunctionProtoType>()) 2403957fd58SJordan Rose if (!ProtoType->isNothrow(getContext())) 2413957fd58SJordan Rose State = State->assume(symVal, true); 2423957fd58SJordan Rose } 2433957fd58SJordan Rose 24410e25031STed Kremenek if (CNE->isArray()) { 24510e25031STed Kremenek // FIXME: allocating an array requires simulating the constructors. 24610e25031STed Kremenek // For now, just return a symbolicated region. 247a4ee064cSJordan Rose const MemRegion *NewReg = cast<loc::MemRegionVal>(symVal).getRegion(); 248a4ee064cSJordan Rose QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType(); 249a4ee064cSJordan Rose const ElementRegion *EleReg = 250a4ee064cSJordan Rose getStoreManager().GetElementZeroRegion(NewReg, ObjTy); 25144320367SJordan Rose State = State->BindExpr(CNE, Pred->getLocationContext(), 252632e3b7eSTed Kremenek loc::MemRegionVal(EleReg)); 25344320367SJordan Rose Bldr.generateNode(CNE, Pred, State); 25410e25031STed Kremenek return; 25510e25031STed Kremenek } 25610e25031STed Kremenek 257a4ee064cSJordan Rose // FIXME: Once we have proper support for CXXConstructExprs inside 258a4ee064cSJordan Rose // CXXNewExpr, we need to make sure that the constructed object is not 259a4ee064cSJordan Rose // immediately invalidated here. (The placement call should happen before 260a4ee064cSJordan Rose // the constructor call anyway.) 26144320367SJordan Rose if (FD && FD->isReservedGlobalPlacementOperator()) { 26244320367SJordan Rose // Non-array placement new should always return the placement location. 26344320367SJordan Rose SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx); 264aaf83184SJordan Rose SVal Result = svalBuilder.evalCast(PlacementLoc, CNE->getType(), 265aaf83184SJordan Rose CNE->getPlacementArg(0)->getType()); 266aaf83184SJordan Rose State = State->BindExpr(CNE, LCtx, Result); 26744320367SJordan Rose } else { 268a4ee064cSJordan Rose State = State->BindExpr(CNE, LCtx, symVal); 26944320367SJordan Rose } 27044320367SJordan Rose 2715089f3b3SJordan Rose // If the type is not a record, we won't have a CXXConstructExpr as an 2725089f3b3SJordan Rose // initializer. Copy the value over. 2735089f3b3SJordan Rose if (const Expr *Init = CNE->getInitializer()) { 2745089f3b3SJordan Rose if (!isa<CXXConstructExpr>(Init)) { 2755089f3b3SJordan Rose QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType(); 2765089f3b3SJordan Rose (void)ObjTy; 2775089f3b3SJordan Rose assert(!ObjTy->isRecordType()); 2785089f3b3SJordan Rose SVal Location = State->getSVal(CNE, LCtx); 2795089f3b3SJordan Rose if (isa<Loc>(Location)) 2805089f3b3SJordan Rose State = State->bindLoc(cast<Loc>(Location), State->getSVal(Init, LCtx)); 2815089f3b3SJordan Rose } 2825089f3b3SJordan Rose } 2835089f3b3SJordan Rose 28444320367SJordan Rose Bldr.generateNode(CNE, Pred, State); 28510e25031STed Kremenek } 28610e25031STed Kremenek 28710e25031STed Kremenek void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE, 28810e25031STed Kremenek ExplodedNode *Pred, ExplodedNodeSet &Dst) { 289d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 29064aea652SErik Verbruggen ProgramStateRef state = Pred->getState(); 29164aea652SErik Verbruggen Bldr.generateNode(CDE, Pred, state); 29210e25031STed Kremenek } 29310e25031STed Kremenek 2948fdb59f9STed Kremenek void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS, 2958fdb59f9STed Kremenek ExplodedNode *Pred, 2968fdb59f9STed Kremenek ExplodedNodeSet &Dst) { 2978fdb59f9STed Kremenek const VarDecl *VD = CS->getExceptionDecl(); 298b1279b5cSTed Kremenek if (!VD) { 299b1279b5cSTed Kremenek Dst.Add(Pred); 300b1279b5cSTed Kremenek return; 301b1279b5cSTed Kremenek } 302b1279b5cSTed Kremenek 3038fdb59f9STed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 304d227833cSTed Kremenek SVal V = svalBuilder.conjureSymbolVal(CS, LCtx, VD->getType(), 305d94854a4STed Kremenek currBldrCtx->blockCount()); 3068fdb59f9STed Kremenek ProgramStateRef state = Pred->getState(); 3078fdb59f9STed Kremenek state = state->bindLoc(state->getLValue(VD, LCtx), V); 3088fdb59f9STed Kremenek 309d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 3108fdb59f9STed Kremenek Bldr.generateNode(CS, Pred, state); 3118fdb59f9STed Kremenek } 3128fdb59f9STed Kremenek 31310e25031STed Kremenek void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred, 31410e25031STed Kremenek ExplodedNodeSet &Dst) { 315d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 316e594034fSAnna Zaks 31710e25031STed Kremenek // Get the this object region from StoreManager. 318632e3b7eSTed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 31910e25031STed Kremenek const MemRegion *R = 32010e25031STed Kremenek svalBuilder.getRegionManager().getCXXThisRegion( 32110e25031STed Kremenek getContext().getCanonicalType(TE->getType()), 322632e3b7eSTed Kremenek LCtx); 32310e25031STed Kremenek 32449b1e38eSTed Kremenek ProgramStateRef state = Pred->getState(); 32510e25031STed Kremenek SVal V = state->getSVal(loc::MemRegionVal(R)); 326632e3b7eSTed Kremenek Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V)); 32710e25031STed Kremenek } 328