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 33861a1740SJordan Rose state = createTemporaryRegionIfNeeded(state, LCtx, tempExpr, ME); 345772f82dSJordan Rose Bldr.generateNode(ME, Pred, state); 3510e25031STed Kremenek } 3610e25031STed Kremenek 373d7b7f52SJordan Rose // FIXME: This is the sort of code that should eventually live in a Core 383d7b7f52SJordan Rose // checker rather than as a special case in ExprEngine. 3988bb563cSJordan Rose void ExprEngine::performTrivialCopy(NodeBuilder &Bldr, ExplodedNode *Pred, 403d7b7f52SJordan Rose const CallEvent &Call) { 413d7b7f52SJordan Rose SVal ThisVal; 423d7b7f52SJordan Rose bool AlwaysReturnsLValue; 433d7b7f52SJordan Rose if (const CXXConstructorCall *Ctor = dyn_cast<CXXConstructorCall>(&Call)) { 443d7b7f52SJordan Rose assert(Ctor->getDecl()->isTrivial()); 453d7b7f52SJordan Rose assert(Ctor->getDecl()->isCopyOrMoveConstructor()); 463d7b7f52SJordan Rose ThisVal = Ctor->getCXXThisVal(); 473d7b7f52SJordan Rose AlwaysReturnsLValue = false; 483d7b7f52SJordan Rose } else { 493d7b7f52SJordan Rose assert(cast<CXXMethodDecl>(Call.getDecl())->isTrivial()); 503d7b7f52SJordan Rose assert(cast<CXXMethodDecl>(Call.getDecl())->getOverloadedOperator() == 513d7b7f52SJordan Rose OO_Equal); 523d7b7f52SJordan Rose ThisVal = cast<CXXInstanceCall>(Call).getCXXThisVal(); 533d7b7f52SJordan Rose AlwaysReturnsLValue = true; 543d7b7f52SJordan Rose } 5588bb563cSJordan Rose 5688bb563cSJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 5788bb563cSJordan Rose 5888bb563cSJordan Rose ExplodedNodeSet Dst; 5988bb563cSJordan Rose Bldr.takeNodes(Pred); 6088bb563cSJordan Rose 6188bb563cSJordan Rose SVal V = Call.getArgSVal(0); 6288bb563cSJordan Rose 633d7b7f52SJordan Rose // If the value being copied is not unknown, load from its location to get 643d7b7f52SJordan Rose // an aggregate rvalue. 6505785d16SDavid Blaikie if (Optional<Loc> L = V.getAs<Loc>()) 6688bb563cSJordan Rose V = Pred->getState()->getSVal(*L); 673d7b7f52SJordan Rose else 683d7b7f52SJordan Rose assert(V.isUnknown()); 6988bb563cSJordan Rose 703d7b7f52SJordan Rose const Expr *CallExpr = Call.getOriginExpr(); 713d7b7f52SJordan Rose evalBind(Dst, CallExpr, Pred, ThisVal, V, true); 7288bb563cSJordan Rose 733d7b7f52SJordan Rose PostStmt PS(CallExpr, LCtx); 7488bb563cSJordan Rose for (ExplodedNodeSet::iterator I = Dst.begin(), E = Dst.end(); 7588bb563cSJordan Rose I != E; ++I) { 7688bb563cSJordan Rose ProgramStateRef State = (*I)->getState(); 773d7b7f52SJordan Rose if (AlwaysReturnsLValue) 783d7b7f52SJordan Rose State = State->BindExpr(CallExpr, LCtx, ThisVal); 793d7b7f52SJordan Rose else 8088bb563cSJordan Rose State = bindReturnValue(Call, LCtx, State); 8188bb563cSJordan Rose Bldr.generateNode(PS, State, *I); 8288bb563cSJordan Rose } 8388bb563cSJordan Rose } 8488bb563cSJordan Rose 85bc74eb1cSJordan Rose 86bc74eb1cSJordan Rose /// Returns a region representing the first element of a (possibly 87bc74eb1cSJordan Rose /// multi-dimensional) array. 88bc74eb1cSJordan Rose /// 89bc74eb1cSJordan Rose /// On return, \p Ty will be set to the base type of the array. 90bc74eb1cSJordan Rose /// 91bc74eb1cSJordan Rose /// If the type is not an array type at all, the original value is returned. 92bc74eb1cSJordan Rose static SVal makeZeroElementRegion(ProgramStateRef State, SVal LValue, 93bc74eb1cSJordan Rose QualType &Ty) { 94bc74eb1cSJordan Rose SValBuilder &SVB = State->getStateManager().getSValBuilder(); 95bc74eb1cSJordan Rose ASTContext &Ctx = SVB.getContext(); 96bc74eb1cSJordan Rose 97bc74eb1cSJordan Rose while (const ArrayType *AT = Ctx.getAsArrayType(Ty)) { 98bc74eb1cSJordan Rose Ty = AT->getElementType(); 99bc74eb1cSJordan Rose LValue = State->getLValue(Ty, SVB.makeZeroArrayIndex(), LValue); 100bc74eb1cSJordan Rose } 101bc74eb1cSJordan Rose 102bc74eb1cSJordan Rose return LValue; 103bc74eb1cSJordan Rose } 104bc74eb1cSJordan Rose 10517f41608SJordan Rose 10617f41608SJordan Rose static const MemRegion *getRegionForConstructedObject( 10717f41608SJordan Rose const CXXConstructExpr *CE, ExplodedNode *Pred, ExprEngine &Eng, 10817f41608SJordan Rose unsigned int CurrStmtIdx) { 109b970505dSJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 11005375eb4SJordan Rose ProgramStateRef State = Pred->getState(); 11117f41608SJordan Rose const NodeBuilderContext &CurrBldrCtx = Eng.getBuilderContext(); 11205375eb4SJordan Rose 11305375eb4SJordan Rose // See if we're constructing an existing region by looking at the next 11405375eb4SJordan Rose // element in the CFG. 11517f41608SJordan Rose const CFGBlock *B = CurrBldrCtx.getBlock(); 1168a5a094eSJordan Rose unsigned int NextStmtIdx = CurrStmtIdx + 1; 1178a5a094eSJordan Rose if (NextStmtIdx < B->size()) { 1188a5a094eSJordan Rose CFGElement Next = (*B)[NextStmtIdx]; 1198a5a094eSJordan Rose 1208a5a094eSJordan Rose // Is this a destructor? If so, we might be in the middle of an assignment 1218a5a094eSJordan Rose // to a local or member: look ahead one more element to see what we find. 1228a5a094eSJordan Rose while (Next.getAs<CFGImplicitDtor>() && NextStmtIdx + 1 < B->size()) { 1238a5a094eSJordan Rose ++NextStmtIdx; 1248a5a094eSJordan Rose Next = (*B)[NextStmtIdx]; 1258a5a094eSJordan Rose } 12605375eb4SJordan Rose 12705375eb4SJordan Rose // Is this a constructor for a local variable? 12800be69abSDavid Blaikie if (Optional<CFGStmt> StmtElem = Next.getAs<CFGStmt>()) { 12900be69abSDavid Blaikie if (const DeclStmt *DS = dyn_cast<DeclStmt>(StmtElem->getStmt())) { 13020edae87SJordan Rose if (const VarDecl *Var = dyn_cast<VarDecl>(DS->getSingleDecl())) { 131bcd88973SJordan Rose if (Var->getInit() && Var->getInit()->IgnoreImplicit() == CE) { 132bc74eb1cSJordan Rose SVal LValue = State->getLValue(Var, LCtx); 13320edae87SJordan Rose QualType Ty = Var->getType(); 134bc74eb1cSJordan Rose LValue = makeZeroElementRegion(State, LValue, Ty); 13517f41608SJordan Rose return LValue.getAsRegion(); 13620edae87SJordan Rose } 13720edae87SJordan Rose } 13820edae87SJordan Rose } 13920edae87SJordan Rose } 14005375eb4SJordan Rose 14154529a34SJordan Rose // Is this a constructor for a member? 14200be69abSDavid Blaikie if (Optional<CFGInitializer> InitElem = Next.getAs<CFGInitializer>()) { 14300be69abSDavid Blaikie const CXXCtorInitializer *Init = InitElem->getInitializer(); 14454529a34SJordan Rose assert(Init->isAnyMemberInitializer()); 14554529a34SJordan Rose 14654529a34SJordan Rose const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl()); 14717f41608SJordan Rose Loc ThisPtr = Eng.getSValBuilder().getCXXThis(CurCtor, 14854529a34SJordan Rose LCtx->getCurrentStackFrame()); 14954529a34SJordan Rose SVal ThisVal = State->getSVal(ThisPtr); 15054529a34SJordan Rose 151bc74eb1cSJordan Rose const ValueDecl *Field; 152bc74eb1cSJordan Rose SVal FieldVal; 15354529a34SJordan Rose if (Init->isIndirectMemberInitializer()) { 154bc74eb1cSJordan Rose Field = Init->getIndirectMember(); 155bc74eb1cSJordan Rose FieldVal = State->getLValue(Init->getIndirectMember(), ThisVal); 15654529a34SJordan Rose } else { 157bc74eb1cSJordan Rose Field = Init->getMember(); 158bc74eb1cSJordan Rose FieldVal = State->getLValue(Init->getMember(), ThisVal); 15954529a34SJordan Rose } 160bc74eb1cSJordan Rose 161bc74eb1cSJordan Rose QualType Ty = Field->getType(); 162bc74eb1cSJordan Rose FieldVal = makeZeroElementRegion(State, FieldVal, Ty); 16317f41608SJordan Rose return FieldVal.getAsRegion(); 16454529a34SJordan Rose } 16554529a34SJordan Rose 16605375eb4SJordan Rose // FIXME: This will eventually need to handle new-expressions as well. 16717f41608SJordan Rose // Don't forget to update the pre-constructor initialization code in 16817f41608SJordan Rose // ExprEngine::VisitCXXConstructExpr. 16905375eb4SJordan Rose } 17005375eb4SJordan Rose 171c9318304SJordan Rose // If we couldn't find an existing region to construct into, assume we're 172c9318304SJordan Rose // constructing a temporary. 17317f41608SJordan Rose MemRegionManager &MRMgr = Eng.getSValBuilder().getRegionManager(); 17417f41608SJordan Rose return MRMgr.getCXXTempObjectRegion(CE, LCtx); 175c9318304SJordan Rose } 17605375eb4SJordan Rose 17717f41608SJordan Rose void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *CE, 17817f41608SJordan Rose ExplodedNode *Pred, 17917f41608SJordan Rose ExplodedNodeSet &destNodes) { 18017f41608SJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 18117f41608SJordan Rose ProgramStateRef State = Pred->getState(); 18217f41608SJordan Rose 1830dbb783cSCraig Topper const MemRegion *Target = nullptr; 18417f41608SJordan Rose 18517f41608SJordan Rose // FIXME: Handle arrays, which run the same constructor for every element. 18617f41608SJordan Rose // For now, we just run the first constructor (which should still invalidate 18717f41608SJordan Rose // the entire array). 18817f41608SJordan Rose 18917f41608SJordan Rose switch (CE->getConstructionKind()) { 19017f41608SJordan Rose case CXXConstructExpr::CK_Complete: { 19117f41608SJordan Rose Target = getRegionForConstructedObject(CE, Pred, *this, currStmtIdx); 192b970505dSJordan Rose break; 193b970505dSJordan Rose } 194b3b976f0SJordan Rose case CXXConstructExpr::CK_VirtualBase: 195b3b976f0SJordan Rose // Make sure we are not calling virtual base class initializers twice. 196b3b976f0SJordan Rose // Only the most-derived object should initialize virtual base classes. 197b3b976f0SJordan Rose if (const Stmt *Outer = LCtx->getCurrentStackFrame()->getCallSite()) { 198b3b976f0SJordan Rose const CXXConstructExpr *OuterCtor = dyn_cast<CXXConstructExpr>(Outer); 199b3b976f0SJordan Rose if (OuterCtor) { 200b3b976f0SJordan Rose switch (OuterCtor->getConstructionKind()) { 201b970505dSJordan Rose case CXXConstructExpr::CK_NonVirtualBase: 202b970505dSJordan Rose case CXXConstructExpr::CK_VirtualBase: 203b3b976f0SJordan Rose // Bail out! 204b3b976f0SJordan Rose destNodes.Add(Pred); 205b3b976f0SJordan Rose return; 206b3b976f0SJordan Rose case CXXConstructExpr::CK_Complete: 207b3b976f0SJordan Rose case CXXConstructExpr::CK_Delegating: 208b3b976f0SJordan Rose break; 209b3b976f0SJordan Rose } 210b3b976f0SJordan Rose } 211b3b976f0SJordan Rose } 212b3b976f0SJordan Rose // FALLTHROUGH 213b3b976f0SJordan Rose case CXXConstructExpr::CK_NonVirtualBase: 214b970505dSJordan Rose case CXXConstructExpr::CK_Delegating: { 215b970505dSJordan Rose const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl()); 216b970505dSJordan Rose Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor, 217b970505dSJordan Rose LCtx->getCurrentStackFrame()); 21805375eb4SJordan Rose SVal ThisVal = State->getSVal(ThisPtr); 219b970505dSJordan Rose 220b970505dSJordan Rose if (CE->getConstructionKind() == CXXConstructExpr::CK_Delegating) { 221b970505dSJordan Rose Target = ThisVal.getAsRegion(); 222b970505dSJordan Rose } else { 223b970505dSJordan Rose // Cast to the base type. 224599f85abSJordan Rose bool IsVirtual = 225599f85abSJordan Rose (CE->getConstructionKind() == CXXConstructExpr::CK_VirtualBase); 226599f85abSJordan Rose SVal BaseVal = getStoreManager().evalDerivedToBase(ThisVal, CE->getType(), 227599f85abSJordan Rose IsVirtual); 22820edae87SJordan Rose Target = BaseVal.getAsRegion(); 229b970505dSJordan Rose } 230b970505dSJordan Rose break; 231b970505dSJordan Rose } 232b970505dSJordan Rose } 233b970505dSJordan Rose 234fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 235fcd016e5SJordan Rose CallEventRef<CXXConstructorCall> Call = 236fcd016e5SJordan Rose CEMgr.getCXXConstructorCall(CE, Target, State, LCtx); 23710e25031STed Kremenek 238f3c12ac3SJordan Rose ExplodedNodeSet DstPreVisit; 239f3c12ac3SJordan Rose getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, CE, *this); 240b8e28654SJordan Rose 241b8e28654SJordan Rose ExplodedNodeSet PreInitialized; 242b8e28654SJordan Rose { 243b8e28654SJordan Rose StmtNodeBuilder Bldr(DstPreVisit, PreInitialized, *currBldrCtx); 244b8e28654SJordan Rose if (CE->requiresZeroInitialization()) { 245b8e28654SJordan Rose // Type of the zero doesn't matter. 246b8e28654SJordan Rose SVal ZeroVal = svalBuilder.makeZeroVal(getContext().CharTy); 247b8e28654SJordan Rose 248b8e28654SJordan Rose for (ExplodedNodeSet::iterator I = DstPreVisit.begin(), 249b8e28654SJordan Rose E = DstPreVisit.end(); 250b8e28654SJordan Rose I != E; ++I) { 251b8e28654SJordan Rose ProgramStateRef State = (*I)->getState(); 252b8e28654SJordan Rose // FIXME: Once we properly handle constructors in new-expressions, we'll 253b8e28654SJordan Rose // need to invalidate the region before setting a default value, to make 254b8e28654SJordan Rose // sure there aren't any lingering bindings around. This probably needs 255b8e28654SJordan Rose // to happen regardless of whether or not the object is zero-initialized 256b8e28654SJordan Rose // to handle random fields of a placement-initialized object picking up 257b8e28654SJordan Rose // old bindings. We might only want to do it when we need to, though. 258b8e28654SJordan Rose // FIXME: This isn't actually correct for arrays -- we need to zero- 259b8e28654SJordan Rose // initialize the entire array, not just the first element -- but our 260b8e28654SJordan Rose // handling of arrays everywhere else is weak as well, so this shouldn't 261b8e28654SJordan Rose // actually make things worse. Placement new makes this tricky as well, 262b8e28654SJordan Rose // since it's then possible to be initializing one part of a multi- 263b8e28654SJordan Rose // dimensional array. 264b8e28654SJordan Rose State = State->bindDefault(loc::MemRegionVal(Target), ZeroVal); 2650dbb783cSCraig Topper Bldr.generateNode(CE, *I, State, /*tag=*/nullptr, 2660dbb783cSCraig Topper ProgramPoint::PreStmtKind); 267b8e28654SJordan Rose } 268b8e28654SJordan Rose } 269b8e28654SJordan Rose } 270b8e28654SJordan Rose 271afe7c2c2SJordan Rose ExplodedNodeSet DstPreCall; 272b8e28654SJordan Rose getCheckerManager().runCheckersForPreCall(DstPreCall, PreInitialized, 273fcd016e5SJordan Rose *Call, *this); 27410e25031STed Kremenek 27588bb563cSJordan Rose ExplodedNodeSet DstEvaluated; 27688bb563cSJordan Rose StmtNodeBuilder Bldr(DstPreCall, DstEvaluated, *currBldrCtx); 27788bb563cSJordan Rose 278e83cb092SJordan Rose bool IsArray = isa<ElementRegion>(Target); 279e83cb092SJordan Rose if (CE->getConstructor()->isTrivial() && 280e83cb092SJordan Rose CE->getConstructor()->isCopyOrMoveConstructor() && 281e83cb092SJordan Rose !IsArray) { 282e83cb092SJordan Rose // FIXME: Handle other kinds of trivial constructors as well. 283e83cb092SJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 28488bb563cSJordan Rose I != E; ++I) 28588bb563cSJordan Rose performTrivialCopy(Bldr, *I, *Call); 28688bb563cSJordan Rose 28788bb563cSJordan Rose } else { 288afe7c2c2SJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 289f3c12ac3SJordan Rose I != E; ++I) 290fcd016e5SJordan Rose defaultEvalCall(Bldr, *I, *Call); 29188bb563cSJordan Rose } 29210e25031STed Kremenek 293afe7c2c2SJordan Rose ExplodedNodeSet DstPostCall; 29488bb563cSJordan Rose getCheckerManager().runCheckersForPostCall(DstPostCall, DstEvaluated, 295fcd016e5SJordan Rose *Call, *this); 296afe7c2c2SJordan Rose getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, CE, *this); 29710e25031STed Kremenek } 29810e25031STed Kremenek 299b970505dSJordan Rose void ExprEngine::VisitCXXDestructor(QualType ObjectType, 30010e25031STed Kremenek const MemRegion *Dest, 30110e25031STed Kremenek const Stmt *S, 3022bc9674bSJordan Rose bool IsBaseDtor, 30310e25031STed Kremenek ExplodedNode *Pred, 30410e25031STed Kremenek ExplodedNodeSet &Dst) { 305fcd016e5SJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 306fcd016e5SJordan Rose ProgramStateRef State = Pred->getState(); 307fcd016e5SJordan Rose 30820edae87SJordan Rose // FIXME: We need to run the same destructor on every element of the array. 30920edae87SJordan Rose // This workaround will just run the first destructor (which will still 31020edae87SJordan Rose // invalidate the entire array). 3111ccc43d5SJordan Rose SVal DestVal = UnknownVal(); 3121ccc43d5SJordan Rose if (Dest) 3131ccc43d5SJordan Rose DestVal = loc::MemRegionVal(Dest); 314bc74eb1cSJordan Rose DestVal = makeZeroElementRegion(State, DestVal, ObjectType); 315bc74eb1cSJordan Rose Dest = DestVal.getAsRegion(); 31620edae87SJordan Rose 317b970505dSJordan Rose const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl(); 318b970505dSJordan Rose assert(RecordDecl && "Only CXXRecordDecls should have destructors"); 319b970505dSJordan Rose const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor(); 320b970505dSJordan Rose 321fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 322fcd016e5SJordan Rose CallEventRef<CXXDestructorCall> Call = 3232bc9674bSJordan Rose CEMgr.getCXXDestructorCall(DtorDecl, S, Dest, IsBaseDtor, State, LCtx); 324142adc49STed Kremenek 32517a8757aSJordan Rose PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(), 32617a8757aSJordan Rose Call->getSourceRange().getBegin(), 32717a8757aSJordan Rose "Error evaluating destructor"); 32817a8757aSJordan Rose 3294ee71b8aSJordan Rose ExplodedNodeSet DstPreCall; 3304ee71b8aSJordan Rose getCheckerManager().runCheckersForPreCall(DstPreCall, Pred, 331fcd016e5SJordan Rose *Call, *this); 33210e25031STed Kremenek 3334ee71b8aSJordan Rose ExplodedNodeSet DstInvalidated; 334d94854a4STed Kremenek StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx); 3354ee71b8aSJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 3364ee71b8aSJordan Rose I != E; ++I) 337fcd016e5SJordan Rose defaultEvalCall(Bldr, *I, *Call); 3384ee71b8aSJordan Rose 3394ee71b8aSJordan Rose ExplodedNodeSet DstPostCall; 3404ee71b8aSJordan Rose getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated, 341fcd016e5SJordan Rose *Call, *this); 34210e25031STed Kremenek } 34310e25031STed Kremenek 3448b808d64SJordan Rose void ExprEngine::VisitCXXNewAllocatorCall(const CXXNewExpr *CNE, 3458b808d64SJordan Rose ExplodedNode *Pred, 3468b808d64SJordan Rose ExplodedNodeSet &Dst) { 3478b808d64SJordan Rose ProgramStateRef State = Pred->getState(); 3488b808d64SJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 3498b808d64SJordan Rose PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(), 3508b808d64SJordan Rose CNE->getStartLoc(), 3518b808d64SJordan Rose "Error evaluating New Allocator Call"); 3528b808d64SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 3538b808d64SJordan Rose CallEventRef<CXXAllocatorCall> Call = 3548b808d64SJordan Rose CEMgr.getCXXAllocatorCall(CNE, State, LCtx); 3558b808d64SJordan Rose 3568b808d64SJordan Rose ExplodedNodeSet DstPreCall; 3578b808d64SJordan Rose getCheckerManager().runCheckersForPreCall(DstPreCall, Pred, 3588b808d64SJordan Rose *Call, *this); 3598b808d64SJordan Rose 3608b808d64SJordan Rose ExplodedNodeSet DstInvalidated; 3618b808d64SJordan Rose StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx); 3628b808d64SJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 3638b808d64SJordan Rose I != E; ++I) 3648b808d64SJordan Rose defaultEvalCall(Bldr, *I, *Call); 3658b808d64SJordan Rose getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated, 3668b808d64SJordan Rose *Call, *this); 3678b808d64SJordan Rose } 3688b808d64SJordan Rose 3698b808d64SJordan Rose 37010e25031STed Kremenek void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred, 37110e25031STed Kremenek ExplodedNodeSet &Dst) { 372a4ee064cSJordan Rose // FIXME: Much of this should eventually migrate to CXXAllocatorCall. 373a4ee064cSJordan Rose // Also, we need to decide how allocators actually work -- they're not 374a4ee064cSJordan Rose // really part of the CXXNewExpr because they happen BEFORE the 375a4ee064cSJordan Rose // CXXConstructExpr subexpression. See PR12014 for some discussion. 37610e25031STed Kremenek 377d94854a4STed Kremenek unsigned blockCount = currBldrCtx->blockCount(); 378d519cae8STed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 3798b662704SAnton Yartsev DefinedOrUnknownSVal symVal = UnknownVal(); 3808b662704SAnton Yartsev FunctionDecl *FD = CNE->getOperatorNew(); 38110e25031STed Kremenek 3828b662704SAnton Yartsev bool IsStandardGlobalOpNewFunction = false; 3838b662704SAnton Yartsev if (FD && !isa<CXXMethodDecl>(FD) && !FD->isVariadic()) { 3848b662704SAnton Yartsev if (FD->getNumParams() == 2) { 3858b662704SAnton Yartsev QualType T = FD->getParamDecl(1)->getType(); 3868b662704SAnton Yartsev if (const IdentifierInfo *II = T.getBaseTypeIdentifier()) 3878b662704SAnton Yartsev // NoThrow placement new behaves as a standard new. 3888b662704SAnton Yartsev IsStandardGlobalOpNewFunction = II->getName().equals("nothrow_t"); 3898b662704SAnton Yartsev } 3908b662704SAnton Yartsev else 3918b662704SAnton Yartsev // Placement forms are considered non-standard. 3928b662704SAnton Yartsev IsStandardGlobalOpNewFunction = (FD->getNumParams() == 1); 3938b662704SAnton Yartsev } 3948b662704SAnton Yartsev 3958b662704SAnton Yartsev // We assume all standard global 'operator new' functions allocate memory in 3968b662704SAnton Yartsev // heap. We realize this is an approximation that might not correctly model 3978b662704SAnton Yartsev // a custom global allocator. 3988b662704SAnton Yartsev if (IsStandardGlobalOpNewFunction) 3998b662704SAnton Yartsev symVal = svalBuilder.getConjuredHeapSymbolVal(CNE, LCtx, blockCount); 4008b662704SAnton Yartsev else 4010dbb783cSCraig Topper symVal = svalBuilder.conjureSymbolVal(nullptr, CNE, LCtx, CNE->getType(), 4028b662704SAnton Yartsev blockCount); 4038b662704SAnton Yartsev 4048b662704SAnton Yartsev ProgramStateRef State = Pred->getState(); 405fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 406fcd016e5SJordan Rose CallEventRef<CXXAllocatorCall> Call = 407fcd016e5SJordan Rose CEMgr.getCXXAllocatorCall(CNE, State, LCtx); 408fcd016e5SJordan Rose 409a4ee064cSJordan Rose // Invalidate placement args. 410fcd016e5SJordan Rose // FIXME: Once we figure out how we want allocators to work, 411fcd016e5SJordan Rose // we should be using the usual pre-/(default-)eval-/post-call checks here. 412fcd016e5SJordan Rose State = Call->invalidateRegions(blockCount); 413b8cb8359SJordan Rose if (!State) 414b8cb8359SJordan Rose return; 415a4ee064cSJordan Rose 416fce1b03eSPavel Labath // If this allocation function is not declared as non-throwing, failures 417fce1b03eSPavel Labath // /must/ be signalled by exceptions, and thus the return value will never be 418fce1b03eSPavel Labath // NULL. -fno-exceptions does not influence this semantics. 419fce1b03eSPavel Labath // FIXME: GCC has a -fcheck-new option, which forces it to consider the case 420fce1b03eSPavel Labath // where new can return NULL. If we end up supporting that option, we can 421fce1b03eSPavel Labath // consider adding a check for it here. 4223957fd58SJordan Rose // C++11 [basic.stc.dynamic.allocation]p3. 423fce1b03eSPavel Labath if (FD) { 4243957fd58SJordan Rose QualType Ty = FD->getType(); 4253957fd58SJordan Rose if (const FunctionProtoType *ProtoType = Ty->getAs<FunctionProtoType>()) 4263957fd58SJordan Rose if (!ProtoType->isNothrow(getContext())) 4273957fd58SJordan Rose State = State->assume(symVal, true); 4283957fd58SJordan Rose } 4293957fd58SJordan Rose 430b8cb8359SJordan Rose StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 431b8cb8359SJordan Rose 43210e25031STed Kremenek if (CNE->isArray()) { 43310e25031STed Kremenek // FIXME: allocating an array requires simulating the constructors. 43410e25031STed Kremenek // For now, just return a symbolicated region. 4352fdacbc5SDavid Blaikie const MemRegion *NewReg = symVal.castAs<loc::MemRegionVal>().getRegion(); 436a4ee064cSJordan Rose QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType(); 437a4ee064cSJordan Rose const ElementRegion *EleReg = 438a4ee064cSJordan Rose getStoreManager().GetElementZeroRegion(NewReg, ObjTy); 43944320367SJordan Rose State = State->BindExpr(CNE, Pred->getLocationContext(), 440632e3b7eSTed Kremenek loc::MemRegionVal(EleReg)); 44144320367SJordan Rose Bldr.generateNode(CNE, Pred, State); 44210e25031STed Kremenek return; 44310e25031STed Kremenek } 44410e25031STed Kremenek 445a4ee064cSJordan Rose // FIXME: Once we have proper support for CXXConstructExprs inside 446a4ee064cSJordan Rose // CXXNewExpr, we need to make sure that the constructed object is not 447a4ee064cSJordan Rose // immediately invalidated here. (The placement call should happen before 448a4ee064cSJordan Rose // the constructor call anyway.) 449b8cb8359SJordan Rose SVal Result = symVal; 45044320367SJordan Rose if (FD && FD->isReservedGlobalPlacementOperator()) { 45144320367SJordan Rose // Non-array placement new should always return the placement location. 45244320367SJordan Rose SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx); 453b8cb8359SJordan Rose Result = svalBuilder.evalCast(PlacementLoc, CNE->getType(), 454aaf83184SJordan Rose CNE->getPlacementArg(0)->getType()); 45544320367SJordan Rose } 45644320367SJordan Rose 457b8cb8359SJordan Rose // Bind the address of the object, then check to see if we cached out. 458b8cb8359SJordan Rose State = State->BindExpr(CNE, LCtx, Result); 4598f6b4b04SJordan Rose ExplodedNode *NewN = Bldr.generateNode(CNE, Pred, State); 4608f6b4b04SJordan Rose if (!NewN) 4618f6b4b04SJordan Rose return; 4623503cb35SJordan Rose 4635089f3b3SJordan Rose // If the type is not a record, we won't have a CXXConstructExpr as an 4645089f3b3SJordan Rose // initializer. Copy the value over. 4655089f3b3SJordan Rose if (const Expr *Init = CNE->getInitializer()) { 4665089f3b3SJordan Rose if (!isa<CXXConstructExpr>(Init)) { 4673503cb35SJordan Rose assert(Bldr.getResults().size() == 1); 4688f6b4b04SJordan Rose Bldr.takeNodes(NewN); 469b8cb8359SJordan Rose evalBind(Dst, CNE, NewN, Result, State->getSVal(Init, LCtx), 470b8cb8359SJordan Rose /*FirstInit=*/IsStandardGlobalOpNewFunction); 4713503cb35SJordan Rose } 4723503cb35SJordan Rose } 47310e25031STed Kremenek } 47410e25031STed Kremenek 47510e25031STed Kremenek void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE, 47610e25031STed Kremenek ExplodedNode *Pred, ExplodedNodeSet &Dst) { 477d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 47864aea652SErik Verbruggen ProgramStateRef state = Pred->getState(); 47964aea652SErik Verbruggen Bldr.generateNode(CDE, Pred, state); 48010e25031STed Kremenek } 48110e25031STed Kremenek 4828fdb59f9STed Kremenek void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS, 4838fdb59f9STed Kremenek ExplodedNode *Pred, 4848fdb59f9STed Kremenek ExplodedNodeSet &Dst) { 4858fdb59f9STed Kremenek const VarDecl *VD = CS->getExceptionDecl(); 486b1279b5cSTed Kremenek if (!VD) { 487b1279b5cSTed Kremenek Dst.Add(Pred); 488b1279b5cSTed Kremenek return; 489b1279b5cSTed Kremenek } 490b1279b5cSTed Kremenek 4918fdb59f9STed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 492d227833cSTed Kremenek SVal V = svalBuilder.conjureSymbolVal(CS, LCtx, VD->getType(), 493d94854a4STed Kremenek currBldrCtx->blockCount()); 4948fdb59f9STed Kremenek ProgramStateRef state = Pred->getState(); 4958fdb59f9STed Kremenek state = state->bindLoc(state->getLValue(VD, LCtx), V); 4968fdb59f9STed Kremenek 497d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 4988fdb59f9STed Kremenek Bldr.generateNode(CS, Pred, state); 4998fdb59f9STed Kremenek } 5008fdb59f9STed Kremenek 50110e25031STed Kremenek void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred, 50210e25031STed Kremenek ExplodedNodeSet &Dst) { 503d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 504e594034fSAnna Zaks 50510e25031STed Kremenek // Get the this object region from StoreManager. 506632e3b7eSTed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 50710e25031STed Kremenek const MemRegion *R = 50810e25031STed Kremenek svalBuilder.getRegionManager().getCXXThisRegion( 50910e25031STed Kremenek getContext().getCanonicalType(TE->getType()), 510632e3b7eSTed Kremenek LCtx); 51110e25031STed Kremenek 51249b1e38eSTed Kremenek ProgramStateRef state = Pred->getState(); 51310e25031STed Kremenek SVal V = state->getSVal(loc::MemRegionVal(R)); 514632e3b7eSTed Kremenek Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V)); 51510e25031STed Kremenek } 51615843343SGabor Horvath 51715843343SGabor Horvath void ExprEngine::VisitLambdaExpr(const LambdaExpr *LE, ExplodedNode *Pred, 51815843343SGabor Horvath ExplodedNodeSet &Dst) { 51915843343SGabor Horvath const LocationContext *LocCtxt = Pred->getLocationContext(); 52015843343SGabor Horvath 52115843343SGabor Horvath // Get the region of the lambda itself. 52215843343SGabor Horvath const MemRegion *R = svalBuilder.getRegionManager().getCXXTempObjectRegion( 52315843343SGabor Horvath LE, LocCtxt); 52415843343SGabor Horvath SVal V = loc::MemRegionVal(R); 52515843343SGabor Horvath 52615843343SGabor Horvath ProgramStateRef State = Pred->getState(); 52715843343SGabor Horvath 52815843343SGabor Horvath // If we created a new MemRegion for the lambda, we should explicitly bind 52915843343SGabor Horvath // the captures. 53015843343SGabor Horvath CXXRecordDecl::field_iterator CurField = LE->getLambdaClass()->field_begin(); 53115843343SGabor Horvath for (LambdaExpr::const_capture_init_iterator i = LE->capture_init_begin(), 53215843343SGabor Horvath e = LE->capture_init_end(); 53315843343SGabor Horvath i != e; ++i, ++CurField) { 534*7bdca8b2SDevin Coughlin FieldDecl *FieldForCapture = *CurField; 535*7bdca8b2SDevin Coughlin SVal FieldLoc = State->getLValue(FieldForCapture, V); 536*7bdca8b2SDevin Coughlin 537*7bdca8b2SDevin Coughlin SVal InitVal; 538*7bdca8b2SDevin Coughlin if (!FieldForCapture->hasCapturedVLAType()) { 539*7bdca8b2SDevin Coughlin Expr *InitExpr = *i; 540*7bdca8b2SDevin Coughlin assert(InitExpr && "Capture missing initialization expression"); 541*7bdca8b2SDevin Coughlin InitVal = State->getSVal(InitExpr, LocCtxt); 542*7bdca8b2SDevin Coughlin } else { 543*7bdca8b2SDevin Coughlin // The field stores the length of a captured variable-length array. 544*7bdca8b2SDevin Coughlin // These captures don't have initialization expressions; instead we 545*7bdca8b2SDevin Coughlin // get the length from the VLAType size expression. 546*7bdca8b2SDevin Coughlin Expr *SizeExpr = FieldForCapture->getCapturedVLAType()->getSizeExpr(); 547*7bdca8b2SDevin Coughlin InitVal = State->getSVal(SizeExpr, LocCtxt); 548*7bdca8b2SDevin Coughlin } 549*7bdca8b2SDevin Coughlin 550*7bdca8b2SDevin Coughlin State = State->bindLoc(FieldLoc, InitVal); 55115843343SGabor Horvath } 55215843343SGabor Horvath 55315843343SGabor Horvath // Decay the Loc into an RValue, because there might be a 55415843343SGabor Horvath // MaterializeTemporaryExpr node above this one which expects the bound value 55515843343SGabor Horvath // to be an RValue. 55615843343SGabor Horvath SVal LambdaRVal = State->getSVal(R); 55715843343SGabor Horvath 55815843343SGabor Horvath ExplodedNodeSet Tmp; 55915843343SGabor Horvath StmtNodeBuilder Bldr(Pred, Tmp, *currBldrCtx); 56015843343SGabor Horvath // FIXME: is this the right program point kind? 56115843343SGabor Horvath Bldr.generateNode(LE, Pred, 56215843343SGabor Horvath State->BindExpr(LE, LocCtxt, LambdaRVal), 56315843343SGabor Horvath nullptr, ProgramPoint::PostLValueKind); 56415843343SGabor Horvath 56515843343SGabor Horvath // FIXME: Move all post/pre visits to ::Visit(). 56615843343SGabor Horvath getCheckerManager().runCheckersForPostStmt(Dst, Tmp, LE, *this); 56715843343SGabor Horvath } 568