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" 183a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/CheckerManager.h" 193a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" 203a02247dSChandler 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 33c9318304SJordan Rose SVal V = state->getSVal(tempExpr, LCtx); 3410e25031STed Kremenek 35fcdda361SJordan Rose // If the value is already a CXXTempObjectRegion, it is fine as it is. 36fcdda361SJordan Rose // Otherwise, create a new CXXTempObjectRegion, and copy the value into it. 37861a1740SJordan Rose // This is an optimization for when an rvalue is constructed and then 38861a1740SJordan Rose // immediately materialized. 39fcdda361SJordan Rose const MemRegion *MR = V.getAsRegion(); 40861a1740SJordan Rose if (const CXXTempObjectRegion *TR = 41861a1740SJordan Rose dyn_cast_or_null<CXXTempObjectRegion>(MR)) { 42861a1740SJordan Rose if (getContext().hasSameUnqualifiedType(TR->getValueType(), ME->getType())) 435772f82dSJordan Rose state = state->BindExpr(ME, LCtx, V); 44861a1740SJordan Rose } 4510e25031STed Kremenek 46861a1740SJordan Rose if (state == Pred->getState()) 47861a1740SJordan Rose state = createTemporaryRegionIfNeeded(state, LCtx, tempExpr, ME); 485772f82dSJordan Rose Bldr.generateNode(ME, Pred, state); 4910e25031STed Kremenek } 5010e25031STed Kremenek 513d7b7f52SJordan Rose // FIXME: This is the sort of code that should eventually live in a Core 523d7b7f52SJordan Rose // checker rather than as a special case in ExprEngine. 5388bb563cSJordan Rose void ExprEngine::performTrivialCopy(NodeBuilder &Bldr, ExplodedNode *Pred, 543d7b7f52SJordan Rose const CallEvent &Call) { 553d7b7f52SJordan Rose SVal ThisVal; 563d7b7f52SJordan Rose bool AlwaysReturnsLValue; 573d7b7f52SJordan Rose if (const CXXConstructorCall *Ctor = dyn_cast<CXXConstructorCall>(&Call)) { 583d7b7f52SJordan Rose assert(Ctor->getDecl()->isTrivial()); 593d7b7f52SJordan Rose assert(Ctor->getDecl()->isCopyOrMoveConstructor()); 603d7b7f52SJordan Rose ThisVal = Ctor->getCXXThisVal(); 613d7b7f52SJordan Rose AlwaysReturnsLValue = false; 623d7b7f52SJordan Rose } else { 633d7b7f52SJordan Rose assert(cast<CXXMethodDecl>(Call.getDecl())->isTrivial()); 643d7b7f52SJordan Rose assert(cast<CXXMethodDecl>(Call.getDecl())->getOverloadedOperator() == 653d7b7f52SJordan Rose OO_Equal); 663d7b7f52SJordan Rose ThisVal = cast<CXXInstanceCall>(Call).getCXXThisVal(); 673d7b7f52SJordan Rose AlwaysReturnsLValue = true; 683d7b7f52SJordan Rose } 6988bb563cSJordan Rose 7088bb563cSJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 7188bb563cSJordan Rose 7288bb563cSJordan Rose ExplodedNodeSet Dst; 7388bb563cSJordan Rose Bldr.takeNodes(Pred); 7488bb563cSJordan Rose 7588bb563cSJordan Rose SVal V = Call.getArgSVal(0); 7688bb563cSJordan Rose 773d7b7f52SJordan Rose // If the value being copied is not unknown, load from its location to get 783d7b7f52SJordan Rose // an aggregate rvalue. 7905785d16SDavid Blaikie if (Optional<Loc> L = V.getAs<Loc>()) 8088bb563cSJordan Rose V = Pred->getState()->getSVal(*L); 813d7b7f52SJordan Rose else 823d7b7f52SJordan Rose assert(V.isUnknown()); 8388bb563cSJordan Rose 843d7b7f52SJordan Rose const Expr *CallExpr = Call.getOriginExpr(); 853d7b7f52SJordan Rose evalBind(Dst, CallExpr, Pred, ThisVal, V, true); 8688bb563cSJordan Rose 873d7b7f52SJordan Rose PostStmt PS(CallExpr, LCtx); 8888bb563cSJordan Rose for (ExplodedNodeSet::iterator I = Dst.begin(), E = Dst.end(); 8988bb563cSJordan Rose I != E; ++I) { 9088bb563cSJordan Rose ProgramStateRef State = (*I)->getState(); 913d7b7f52SJordan Rose if (AlwaysReturnsLValue) 923d7b7f52SJordan Rose State = State->BindExpr(CallExpr, LCtx, ThisVal); 933d7b7f52SJordan Rose else 9488bb563cSJordan Rose State = bindReturnValue(Call, LCtx, State); 9588bb563cSJordan Rose Bldr.generateNode(PS, State, *I); 9688bb563cSJordan Rose } 9788bb563cSJordan Rose } 9888bb563cSJordan Rose 99bc74eb1cSJordan Rose 100bc74eb1cSJordan Rose /// Returns a region representing the first element of a (possibly 101bc74eb1cSJordan Rose /// multi-dimensional) array. 102bc74eb1cSJordan Rose /// 103bc74eb1cSJordan Rose /// On return, \p Ty will be set to the base type of the array. 104bc74eb1cSJordan Rose /// 105bc74eb1cSJordan Rose /// If the type is not an array type at all, the original value is returned. 106bc74eb1cSJordan Rose static SVal makeZeroElementRegion(ProgramStateRef State, SVal LValue, 107bc74eb1cSJordan Rose QualType &Ty) { 108bc74eb1cSJordan Rose SValBuilder &SVB = State->getStateManager().getSValBuilder(); 109bc74eb1cSJordan Rose ASTContext &Ctx = SVB.getContext(); 110bc74eb1cSJordan Rose 111bc74eb1cSJordan Rose while (const ArrayType *AT = Ctx.getAsArrayType(Ty)) { 112bc74eb1cSJordan Rose Ty = AT->getElementType(); 113bc74eb1cSJordan Rose LValue = State->getLValue(Ty, SVB.makeZeroArrayIndex(), LValue); 114bc74eb1cSJordan Rose } 115bc74eb1cSJordan Rose 116bc74eb1cSJordan Rose return LValue; 117bc74eb1cSJordan Rose } 118bc74eb1cSJordan Rose 119f3c12ac3SJordan Rose void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *CE, 12010e25031STed Kremenek ExplodedNode *Pred, 12110e25031STed Kremenek ExplodedNodeSet &destNodes) { 122b970505dSJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 12305375eb4SJordan Rose ProgramStateRef State = Pred->getState(); 12405375eb4SJordan Rose 125b970505dSJordan Rose const MemRegion *Target = 0; 126bc74eb1cSJordan Rose 127bc74eb1cSJordan Rose // FIXME: Handle arrays, which run the same constructor for every element. 128bc74eb1cSJordan Rose // For now, we just run the first constructor (which should still invalidate 129bc74eb1cSJordan Rose // the entire array). 130b970505dSJordan Rose 131b970505dSJordan Rose switch (CE->getConstructionKind()) { 132b970505dSJordan Rose case CXXConstructExpr::CK_Complete: { 13305375eb4SJordan Rose // See if we're constructing an existing region by looking at the next 13405375eb4SJordan Rose // element in the CFG. 135d94854a4STed Kremenek const CFGBlock *B = currBldrCtx->getBlock(); 136d94854a4STed Kremenek if (currStmtIdx + 1 < B->size()) { 137d94854a4STed Kremenek CFGElement Next = (*B)[currStmtIdx+1]; 13805375eb4SJordan Rose 13905375eb4SJordan Rose // Is this a constructor for a local variable? 14000be69abSDavid Blaikie if (Optional<CFGStmt> StmtElem = Next.getAs<CFGStmt>()) { 14100be69abSDavid Blaikie if (const DeclStmt *DS = dyn_cast<DeclStmt>(StmtElem->getStmt())) { 14220edae87SJordan Rose if (const VarDecl *Var = dyn_cast<VarDecl>(DS->getSingleDecl())) { 14320edae87SJordan Rose if (Var->getInit()->IgnoreImplicit() == CE) { 144bc74eb1cSJordan Rose SVal LValue = State->getLValue(Var, LCtx); 14520edae87SJordan Rose QualType Ty = Var->getType(); 146bc74eb1cSJordan Rose LValue = makeZeroElementRegion(State, LValue, Ty); 147bc74eb1cSJordan Rose Target = LValue.getAsRegion(); 14820edae87SJordan Rose } 14920edae87SJordan Rose } 15020edae87SJordan Rose } 15120edae87SJordan Rose } 15205375eb4SJordan Rose 15354529a34SJordan Rose // Is this a constructor for a member? 15400be69abSDavid Blaikie if (Optional<CFGInitializer> InitElem = Next.getAs<CFGInitializer>()) { 15500be69abSDavid Blaikie const CXXCtorInitializer *Init = InitElem->getInitializer(); 15654529a34SJordan Rose assert(Init->isAnyMemberInitializer()); 15754529a34SJordan Rose 15854529a34SJordan Rose const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl()); 15954529a34SJordan Rose Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor, 16054529a34SJordan Rose LCtx->getCurrentStackFrame()); 16154529a34SJordan Rose SVal ThisVal = State->getSVal(ThisPtr); 16254529a34SJordan Rose 163bc74eb1cSJordan Rose const ValueDecl *Field; 164bc74eb1cSJordan Rose SVal FieldVal; 16554529a34SJordan Rose if (Init->isIndirectMemberInitializer()) { 166bc74eb1cSJordan Rose Field = Init->getIndirectMember(); 167bc74eb1cSJordan Rose FieldVal = State->getLValue(Init->getIndirectMember(), ThisVal); 16854529a34SJordan Rose } else { 169bc74eb1cSJordan Rose Field = Init->getMember(); 170bc74eb1cSJordan Rose FieldVal = State->getLValue(Init->getMember(), ThisVal); 17154529a34SJordan Rose } 172bc74eb1cSJordan Rose 173bc74eb1cSJordan Rose QualType Ty = Field->getType(); 174bc74eb1cSJordan Rose FieldVal = makeZeroElementRegion(State, FieldVal, Ty); 175bc74eb1cSJordan Rose Target = FieldVal.getAsRegion(); 17654529a34SJordan Rose } 17754529a34SJordan Rose 17805375eb4SJordan Rose // FIXME: This will eventually need to handle new-expressions as well. 179*b8e28654SJordan Rose // Don't forget to update the pre-constructor initialization code below. 18005375eb4SJordan Rose } 18105375eb4SJordan Rose 182c9318304SJordan Rose // If we couldn't find an existing region to construct into, assume we're 183c9318304SJordan Rose // constructing a temporary. 184c9318304SJordan Rose if (!Target) { 185c9318304SJordan Rose MemRegionManager &MRMgr = getSValBuilder().getRegionManager(); 186c9318304SJordan Rose Target = MRMgr.getCXXTempObjectRegion(CE, LCtx); 187c9318304SJordan Rose } 18805375eb4SJordan Rose 189b970505dSJordan Rose break; 190b970505dSJordan Rose } 191b3b976f0SJordan Rose case CXXConstructExpr::CK_VirtualBase: 192b3b976f0SJordan Rose // Make sure we are not calling virtual base class initializers twice. 193b3b976f0SJordan Rose // Only the most-derived object should initialize virtual base classes. 194b3b976f0SJordan Rose if (const Stmt *Outer = LCtx->getCurrentStackFrame()->getCallSite()) { 195b3b976f0SJordan Rose const CXXConstructExpr *OuterCtor = dyn_cast<CXXConstructExpr>(Outer); 196b3b976f0SJordan Rose if (OuterCtor) { 197b3b976f0SJordan Rose switch (OuterCtor->getConstructionKind()) { 198b970505dSJordan Rose case CXXConstructExpr::CK_NonVirtualBase: 199b970505dSJordan Rose case CXXConstructExpr::CK_VirtualBase: 200b3b976f0SJordan Rose // Bail out! 201b3b976f0SJordan Rose destNodes.Add(Pred); 202b3b976f0SJordan Rose return; 203b3b976f0SJordan Rose case CXXConstructExpr::CK_Complete: 204b3b976f0SJordan Rose case CXXConstructExpr::CK_Delegating: 205b3b976f0SJordan Rose break; 206b3b976f0SJordan Rose } 207b3b976f0SJordan Rose } 208b3b976f0SJordan Rose } 209b3b976f0SJordan Rose // FALLTHROUGH 210b3b976f0SJordan Rose case CXXConstructExpr::CK_NonVirtualBase: 211b970505dSJordan Rose case CXXConstructExpr::CK_Delegating: { 212b970505dSJordan Rose const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl()); 213b970505dSJordan Rose Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor, 214b970505dSJordan Rose LCtx->getCurrentStackFrame()); 21505375eb4SJordan Rose SVal ThisVal = State->getSVal(ThisPtr); 216b970505dSJordan Rose 217b970505dSJordan Rose if (CE->getConstructionKind() == CXXConstructExpr::CK_Delegating) { 218b970505dSJordan Rose Target = ThisVal.getAsRegion(); 219b970505dSJordan Rose } else { 220b970505dSJordan Rose // Cast to the base type. 221599f85abSJordan Rose bool IsVirtual = 222599f85abSJordan Rose (CE->getConstructionKind() == CXXConstructExpr::CK_VirtualBase); 223599f85abSJordan Rose SVal BaseVal = getStoreManager().evalDerivedToBase(ThisVal, CE->getType(), 224599f85abSJordan Rose IsVirtual); 22520edae87SJordan Rose Target = BaseVal.getAsRegion(); 226b970505dSJordan Rose } 227b970505dSJordan Rose break; 228b970505dSJordan Rose } 229b970505dSJordan Rose } 230b970505dSJordan Rose 231fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 232fcd016e5SJordan Rose CallEventRef<CXXConstructorCall> Call = 233fcd016e5SJordan Rose CEMgr.getCXXConstructorCall(CE, Target, State, LCtx); 23410e25031STed Kremenek 235f3c12ac3SJordan Rose ExplodedNodeSet DstPreVisit; 236f3c12ac3SJordan Rose getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, CE, *this); 237*b8e28654SJordan Rose 238*b8e28654SJordan Rose ExplodedNodeSet PreInitialized; 239*b8e28654SJordan Rose { 240*b8e28654SJordan Rose StmtNodeBuilder Bldr(DstPreVisit, PreInitialized, *currBldrCtx); 241*b8e28654SJordan Rose if (CE->requiresZeroInitialization()) { 242*b8e28654SJordan Rose // Type of the zero doesn't matter. 243*b8e28654SJordan Rose SVal ZeroVal = svalBuilder.makeZeroVal(getContext().CharTy); 244*b8e28654SJordan Rose 245*b8e28654SJordan Rose for (ExplodedNodeSet::iterator I = DstPreVisit.begin(), 246*b8e28654SJordan Rose E = DstPreVisit.end(); 247*b8e28654SJordan Rose I != E; ++I) { 248*b8e28654SJordan Rose ProgramStateRef State = (*I)->getState(); 249*b8e28654SJordan Rose // FIXME: Once we properly handle constructors in new-expressions, we'll 250*b8e28654SJordan Rose // need to invalidate the region before setting a default value, to make 251*b8e28654SJordan Rose // sure there aren't any lingering bindings around. This probably needs 252*b8e28654SJordan Rose // to happen regardless of whether or not the object is zero-initialized 253*b8e28654SJordan Rose // to handle random fields of a placement-initialized object picking up 254*b8e28654SJordan Rose // old bindings. We might only want to do it when we need to, though. 255*b8e28654SJordan Rose // FIXME: This isn't actually correct for arrays -- we need to zero- 256*b8e28654SJordan Rose // initialize the entire array, not just the first element -- but our 257*b8e28654SJordan Rose // handling of arrays everywhere else is weak as well, so this shouldn't 258*b8e28654SJordan Rose // actually make things worse. Placement new makes this tricky as well, 259*b8e28654SJordan Rose // since it's then possible to be initializing one part of a multi- 260*b8e28654SJordan Rose // dimensional array. 261*b8e28654SJordan Rose State = State->bindDefault(loc::MemRegionVal(Target), ZeroVal); 262*b8e28654SJordan Rose Bldr.generateNode(CE, *I, State, /*tag=*/0, ProgramPoint::PreStmtKind); 263*b8e28654SJordan Rose } 264*b8e28654SJordan Rose } 265*b8e28654SJordan Rose } 266*b8e28654SJordan Rose 267afe7c2c2SJordan Rose ExplodedNodeSet DstPreCall; 268*b8e28654SJordan Rose getCheckerManager().runCheckersForPreCall(DstPreCall, PreInitialized, 269fcd016e5SJordan Rose *Call, *this); 27010e25031STed Kremenek 27188bb563cSJordan Rose ExplodedNodeSet DstEvaluated; 27288bb563cSJordan Rose StmtNodeBuilder Bldr(DstPreCall, DstEvaluated, *currBldrCtx); 27388bb563cSJordan Rose 274e83cb092SJordan Rose bool IsArray = isa<ElementRegion>(Target); 275e83cb092SJordan Rose if (CE->getConstructor()->isTrivial() && 276e83cb092SJordan Rose CE->getConstructor()->isCopyOrMoveConstructor() && 277e83cb092SJordan Rose !IsArray) { 278e83cb092SJordan Rose // FIXME: Handle other kinds of trivial constructors as well. 279e83cb092SJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 28088bb563cSJordan Rose I != E; ++I) 28188bb563cSJordan Rose performTrivialCopy(Bldr, *I, *Call); 28288bb563cSJordan Rose 28388bb563cSJordan Rose } else { 284afe7c2c2SJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 285f3c12ac3SJordan Rose I != E; ++I) 286fcd016e5SJordan Rose defaultEvalCall(Bldr, *I, *Call); 28788bb563cSJordan Rose } 28810e25031STed Kremenek 289afe7c2c2SJordan Rose ExplodedNodeSet DstPostCall; 29088bb563cSJordan Rose getCheckerManager().runCheckersForPostCall(DstPostCall, DstEvaluated, 291fcd016e5SJordan Rose *Call, *this); 292afe7c2c2SJordan Rose getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, CE, *this); 29310e25031STed Kremenek } 29410e25031STed Kremenek 295b970505dSJordan Rose void ExprEngine::VisitCXXDestructor(QualType ObjectType, 29610e25031STed Kremenek const MemRegion *Dest, 29710e25031STed Kremenek const Stmt *S, 2982bc9674bSJordan Rose bool IsBaseDtor, 29910e25031STed Kremenek ExplodedNode *Pred, 30010e25031STed Kremenek ExplodedNodeSet &Dst) { 301fcd016e5SJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 302fcd016e5SJordan Rose ProgramStateRef State = Pred->getState(); 303fcd016e5SJordan Rose 30420edae87SJordan Rose // FIXME: We need to run the same destructor on every element of the array. 30520edae87SJordan Rose // This workaround will just run the first destructor (which will still 30620edae87SJordan Rose // invalidate the entire array). 307bc74eb1cSJordan Rose SVal DestVal = loc::MemRegionVal(Dest); 308bc74eb1cSJordan Rose DestVal = makeZeroElementRegion(State, DestVal, ObjectType); 309bc74eb1cSJordan Rose Dest = DestVal.getAsRegion(); 31020edae87SJordan Rose 311b970505dSJordan Rose const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl(); 312b970505dSJordan Rose assert(RecordDecl && "Only CXXRecordDecls should have destructors"); 313b970505dSJordan Rose const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor(); 314b970505dSJordan Rose 315fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 316fcd016e5SJordan Rose CallEventRef<CXXDestructorCall> Call = 3172bc9674bSJordan Rose CEMgr.getCXXDestructorCall(DtorDecl, S, Dest, IsBaseDtor, State, LCtx); 318142adc49STed Kremenek 31917a8757aSJordan Rose PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(), 32017a8757aSJordan Rose Call->getSourceRange().getBegin(), 32117a8757aSJordan Rose "Error evaluating destructor"); 32217a8757aSJordan Rose 3234ee71b8aSJordan Rose ExplodedNodeSet DstPreCall; 3244ee71b8aSJordan Rose getCheckerManager().runCheckersForPreCall(DstPreCall, Pred, 325fcd016e5SJordan Rose *Call, *this); 32610e25031STed Kremenek 3274ee71b8aSJordan Rose ExplodedNodeSet DstInvalidated; 328d94854a4STed Kremenek StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx); 3294ee71b8aSJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 3304ee71b8aSJordan Rose I != E; ++I) 331fcd016e5SJordan Rose defaultEvalCall(Bldr, *I, *Call); 3324ee71b8aSJordan Rose 3334ee71b8aSJordan Rose ExplodedNodeSet DstPostCall; 3344ee71b8aSJordan Rose getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated, 335fcd016e5SJordan Rose *Call, *this); 33610e25031STed Kremenek } 33710e25031STed Kremenek 33810e25031STed Kremenek void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred, 33910e25031STed Kremenek ExplodedNodeSet &Dst) { 340a4ee064cSJordan Rose // FIXME: Much of this should eventually migrate to CXXAllocatorCall. 341a4ee064cSJordan Rose // Also, we need to decide how allocators actually work -- they're not 342a4ee064cSJordan Rose // really part of the CXXNewExpr because they happen BEFORE the 343a4ee064cSJordan Rose // CXXConstructExpr subexpression. See PR12014 for some discussion. 34410e25031STed Kremenek 345d94854a4STed Kremenek unsigned blockCount = currBldrCtx->blockCount(); 346d519cae8STed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 3478b662704SAnton Yartsev DefinedOrUnknownSVal symVal = UnknownVal(); 3488b662704SAnton Yartsev FunctionDecl *FD = CNE->getOperatorNew(); 34910e25031STed Kremenek 3508b662704SAnton Yartsev bool IsStandardGlobalOpNewFunction = false; 3518b662704SAnton Yartsev if (FD && !isa<CXXMethodDecl>(FD) && !FD->isVariadic()) { 3528b662704SAnton Yartsev if (FD->getNumParams() == 2) { 3538b662704SAnton Yartsev QualType T = FD->getParamDecl(1)->getType(); 3548b662704SAnton Yartsev if (const IdentifierInfo *II = T.getBaseTypeIdentifier()) 3558b662704SAnton Yartsev // NoThrow placement new behaves as a standard new. 3568b662704SAnton Yartsev IsStandardGlobalOpNewFunction = II->getName().equals("nothrow_t"); 3578b662704SAnton Yartsev } 3588b662704SAnton Yartsev else 3598b662704SAnton Yartsev // Placement forms are considered non-standard. 3608b662704SAnton Yartsev IsStandardGlobalOpNewFunction = (FD->getNumParams() == 1); 3618b662704SAnton Yartsev } 3628b662704SAnton Yartsev 3638b662704SAnton Yartsev // We assume all standard global 'operator new' functions allocate memory in 3648b662704SAnton Yartsev // heap. We realize this is an approximation that might not correctly model 3658b662704SAnton Yartsev // a custom global allocator. 3668b662704SAnton Yartsev if (IsStandardGlobalOpNewFunction) 3678b662704SAnton Yartsev symVal = svalBuilder.getConjuredHeapSymbolVal(CNE, LCtx, blockCount); 3688b662704SAnton Yartsev else 3698b662704SAnton Yartsev symVal = svalBuilder.conjureSymbolVal(0, CNE, LCtx, CNE->getType(), 3708b662704SAnton Yartsev blockCount); 3718b662704SAnton Yartsev 3728b662704SAnton Yartsev ProgramStateRef State = Pred->getState(); 373fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 374fcd016e5SJordan Rose CallEventRef<CXXAllocatorCall> Call = 375fcd016e5SJordan Rose CEMgr.getCXXAllocatorCall(CNE, State, LCtx); 376fcd016e5SJordan Rose 377a4ee064cSJordan Rose // Invalidate placement args. 378fcd016e5SJordan Rose // FIXME: Once we figure out how we want allocators to work, 379fcd016e5SJordan Rose // we should be using the usual pre-/(default-)eval-/post-call checks here. 380fcd016e5SJordan Rose State = Call->invalidateRegions(blockCount); 381b8cb8359SJordan Rose if (!State) 382b8cb8359SJordan Rose return; 383a4ee064cSJordan Rose 3843957fd58SJordan Rose // If we're compiling with exceptions enabled, and this allocation function 3853957fd58SJordan Rose // is not declared as non-throwing, failures /must/ be signalled by 3863957fd58SJordan Rose // exceptions, and thus the return value will never be NULL. 3873957fd58SJordan Rose // C++11 [basic.stc.dynamic.allocation]p3. 3883957fd58SJordan Rose if (FD && getContext().getLangOpts().CXXExceptions) { 3893957fd58SJordan Rose QualType Ty = FD->getType(); 3903957fd58SJordan Rose if (const FunctionProtoType *ProtoType = Ty->getAs<FunctionProtoType>()) 3913957fd58SJordan Rose if (!ProtoType->isNothrow(getContext())) 3923957fd58SJordan Rose State = State->assume(symVal, true); 3933957fd58SJordan Rose } 3943957fd58SJordan Rose 395b8cb8359SJordan Rose StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 396b8cb8359SJordan Rose 39710e25031STed Kremenek if (CNE->isArray()) { 39810e25031STed Kremenek // FIXME: allocating an array requires simulating the constructors. 39910e25031STed Kremenek // For now, just return a symbolicated region. 4002fdacbc5SDavid Blaikie const MemRegion *NewReg = symVal.castAs<loc::MemRegionVal>().getRegion(); 401a4ee064cSJordan Rose QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType(); 402a4ee064cSJordan Rose const ElementRegion *EleReg = 403a4ee064cSJordan Rose getStoreManager().GetElementZeroRegion(NewReg, ObjTy); 40444320367SJordan Rose State = State->BindExpr(CNE, Pred->getLocationContext(), 405632e3b7eSTed Kremenek loc::MemRegionVal(EleReg)); 40644320367SJordan Rose Bldr.generateNode(CNE, Pred, State); 40710e25031STed Kremenek return; 40810e25031STed Kremenek } 40910e25031STed Kremenek 410a4ee064cSJordan Rose // FIXME: Once we have proper support for CXXConstructExprs inside 411a4ee064cSJordan Rose // CXXNewExpr, we need to make sure that the constructed object is not 412a4ee064cSJordan Rose // immediately invalidated here. (The placement call should happen before 413a4ee064cSJordan Rose // the constructor call anyway.) 414b8cb8359SJordan Rose SVal Result = symVal; 41544320367SJordan Rose if (FD && FD->isReservedGlobalPlacementOperator()) { 41644320367SJordan Rose // Non-array placement new should always return the placement location. 41744320367SJordan Rose SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx); 418b8cb8359SJordan Rose Result = svalBuilder.evalCast(PlacementLoc, CNE->getType(), 419aaf83184SJordan Rose CNE->getPlacementArg(0)->getType()); 42044320367SJordan Rose } 42144320367SJordan Rose 422b8cb8359SJordan Rose // Bind the address of the object, then check to see if we cached out. 423b8cb8359SJordan Rose State = State->BindExpr(CNE, LCtx, Result); 4248f6b4b04SJordan Rose ExplodedNode *NewN = Bldr.generateNode(CNE, Pred, State); 4258f6b4b04SJordan Rose if (!NewN) 4268f6b4b04SJordan Rose return; 4273503cb35SJordan Rose 4285089f3b3SJordan Rose // If the type is not a record, we won't have a CXXConstructExpr as an 4295089f3b3SJordan Rose // initializer. Copy the value over. 4305089f3b3SJordan Rose if (const Expr *Init = CNE->getInitializer()) { 4315089f3b3SJordan Rose if (!isa<CXXConstructExpr>(Init)) { 4323503cb35SJordan Rose assert(Bldr.getResults().size() == 1); 4338f6b4b04SJordan Rose Bldr.takeNodes(NewN); 4345089f3b3SJordan Rose 4353503cb35SJordan Rose assert(!CNE->getType()->getPointeeCXXRecordDecl()); 436b8cb8359SJordan Rose evalBind(Dst, CNE, NewN, Result, State->getSVal(Init, LCtx), 437b8cb8359SJordan Rose /*FirstInit=*/IsStandardGlobalOpNewFunction); 4383503cb35SJordan Rose } 4393503cb35SJordan Rose } 44010e25031STed Kremenek } 44110e25031STed Kremenek 44210e25031STed Kremenek void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE, 44310e25031STed Kremenek ExplodedNode *Pred, ExplodedNodeSet &Dst) { 444d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 44564aea652SErik Verbruggen ProgramStateRef state = Pred->getState(); 44664aea652SErik Verbruggen Bldr.generateNode(CDE, Pred, state); 44710e25031STed Kremenek } 44810e25031STed Kremenek 4498fdb59f9STed Kremenek void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS, 4508fdb59f9STed Kremenek ExplodedNode *Pred, 4518fdb59f9STed Kremenek ExplodedNodeSet &Dst) { 4528fdb59f9STed Kremenek const VarDecl *VD = CS->getExceptionDecl(); 453b1279b5cSTed Kremenek if (!VD) { 454b1279b5cSTed Kremenek Dst.Add(Pred); 455b1279b5cSTed Kremenek return; 456b1279b5cSTed Kremenek } 457b1279b5cSTed Kremenek 4588fdb59f9STed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 459d227833cSTed Kremenek SVal V = svalBuilder.conjureSymbolVal(CS, LCtx, VD->getType(), 460d94854a4STed Kremenek currBldrCtx->blockCount()); 4618fdb59f9STed Kremenek ProgramStateRef state = Pred->getState(); 4628fdb59f9STed Kremenek state = state->bindLoc(state->getLValue(VD, LCtx), V); 4638fdb59f9STed Kremenek 464d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 4658fdb59f9STed Kremenek Bldr.generateNode(CS, Pred, state); 4668fdb59f9STed Kremenek } 4678fdb59f9STed Kremenek 46810e25031STed Kremenek void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred, 46910e25031STed Kremenek ExplodedNodeSet &Dst) { 470d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 471e594034fSAnna Zaks 47210e25031STed Kremenek // Get the this object region from StoreManager. 473632e3b7eSTed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 47410e25031STed Kremenek const MemRegion *R = 47510e25031STed Kremenek svalBuilder.getRegionManager().getCXXThisRegion( 47610e25031STed Kremenek getContext().getCanonicalType(TE->getType()), 477632e3b7eSTed Kremenek LCtx); 47810e25031STed Kremenek 47949b1e38eSTed Kremenek ProgramStateRef state = Pred->getState(); 48010e25031STed Kremenek SVal V = state->getSVal(loc::MemRegionVal(R)); 481632e3b7eSTed Kremenek Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V)); 48210e25031STed Kremenek } 483