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" 154068481bSArtem Dergachev #include "clang/Analysis/ConstructionContext.h" 1610e25031STed Kremenek #include "clang/AST/DeclCXX.h" 178fdb59f9STed Kremenek #include "clang/AST/StmtCXX.h" 18e8ba3ec4SArtem Dergachev #include "clang/AST/ParentMap.h" 1917a8757aSJordan Rose #include "clang/Basic/PrettyStackTrace.h" 203a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/CheckerManager.h" 213a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" 223a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 2310e25031STed Kremenek 2410e25031STed Kremenek using namespace clang; 2510e25031STed Kremenek using namespace ento; 2610e25031STed Kremenek 2710e25031STed Kremenek void ExprEngine::CreateCXXTemporaryObject(const MaterializeTemporaryExpr *ME, 2810e25031STed Kremenek ExplodedNode *Pred, 2910e25031STed Kremenek ExplodedNodeSet &Dst) { 30d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 312a14c695STed Kremenek const Expr *tempExpr = ME->GetTemporaryExpr()->IgnoreParens(); 3249b1e38eSTed Kremenek ProgramStateRef state = Pred->getState(); 33632e3b7eSTed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 3410e25031STed Kremenek 35861a1740SJordan Rose state = createTemporaryRegionIfNeeded(state, LCtx, tempExpr, ME); 365772f82dSJordan Rose Bldr.generateNode(ME, Pred, state); 3710e25031STed Kremenek } 3810e25031STed Kremenek 393d7b7f52SJordan Rose // FIXME: This is the sort of code that should eventually live in a Core 403d7b7f52SJordan Rose // checker rather than as a special case in ExprEngine. 4188bb563cSJordan Rose void ExprEngine::performTrivialCopy(NodeBuilder &Bldr, ExplodedNode *Pred, 423d7b7f52SJordan Rose const CallEvent &Call) { 433d7b7f52SJordan Rose SVal ThisVal; 443d7b7f52SJordan Rose bool AlwaysReturnsLValue; 454449e7f0SArtem Dergachev const CXXRecordDecl *ThisRD = nullptr; 463d7b7f52SJordan Rose if (const CXXConstructorCall *Ctor = dyn_cast<CXXConstructorCall>(&Call)) { 473d7b7f52SJordan Rose assert(Ctor->getDecl()->isTrivial()); 483d7b7f52SJordan Rose assert(Ctor->getDecl()->isCopyOrMoveConstructor()); 493d7b7f52SJordan Rose ThisVal = Ctor->getCXXThisVal(); 504449e7f0SArtem Dergachev ThisRD = Ctor->getDecl()->getParent(); 513d7b7f52SJordan Rose AlwaysReturnsLValue = false; 523d7b7f52SJordan Rose } else { 533d7b7f52SJordan Rose assert(cast<CXXMethodDecl>(Call.getDecl())->isTrivial()); 543d7b7f52SJordan Rose assert(cast<CXXMethodDecl>(Call.getDecl())->getOverloadedOperator() == 553d7b7f52SJordan Rose OO_Equal); 563d7b7f52SJordan Rose ThisVal = cast<CXXInstanceCall>(Call).getCXXThisVal(); 574449e7f0SArtem Dergachev ThisRD = cast<CXXMethodDecl>(Call.getDecl())->getParent(); 583d7b7f52SJordan Rose AlwaysReturnsLValue = true; 593d7b7f52SJordan Rose } 6088bb563cSJordan Rose 614449e7f0SArtem Dergachev assert(ThisRD); 624449e7f0SArtem Dergachev if (ThisRD->isEmpty()) { 634449e7f0SArtem Dergachev // Do nothing for empty classes. Otherwise it'd retrieve an UnknownVal 644449e7f0SArtem Dergachev // and bind it and RegionStore would think that the actual value 654449e7f0SArtem Dergachev // in this region at this offset is unknown. 664449e7f0SArtem Dergachev return; 674449e7f0SArtem Dergachev } 684449e7f0SArtem Dergachev 6988bb563cSJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 7088bb563cSJordan Rose 7188bb563cSJordan Rose ExplodedNodeSet Dst; 7288bb563cSJordan Rose Bldr.takeNodes(Pred); 7388bb563cSJordan Rose 7488bb563cSJordan Rose SVal V = Call.getArgSVal(0); 7588bb563cSJordan Rose 763d7b7f52SJordan Rose // If the value being copied is not unknown, load from its location to get 773d7b7f52SJordan Rose // an aggregate rvalue. 7805785d16SDavid Blaikie if (Optional<Loc> L = V.getAs<Loc>()) 7988bb563cSJordan Rose V = Pred->getState()->getSVal(*L); 803d7b7f52SJordan Rose else 8175f9d3acSArtem Dergachev assert(V.isUnknownOrUndef()); 8288bb563cSJordan Rose 833d7b7f52SJordan Rose const Expr *CallExpr = Call.getOriginExpr(); 843d7b7f52SJordan Rose evalBind(Dst, CallExpr, Pred, ThisVal, V, true); 8588bb563cSJordan Rose 863d7b7f52SJordan Rose PostStmt PS(CallExpr, LCtx); 8788bb563cSJordan Rose for (ExplodedNodeSet::iterator I = Dst.begin(), E = Dst.end(); 8888bb563cSJordan Rose I != E; ++I) { 8988bb563cSJordan Rose ProgramStateRef State = (*I)->getState(); 903d7b7f52SJordan Rose if (AlwaysReturnsLValue) 913d7b7f52SJordan Rose State = State->BindExpr(CallExpr, LCtx, ThisVal); 923d7b7f52SJordan Rose else 9388bb563cSJordan Rose State = bindReturnValue(Call, LCtx, State); 9488bb563cSJordan Rose Bldr.generateNode(PS, State, *I); 9588bb563cSJordan Rose } 9688bb563cSJordan Rose } 9788bb563cSJordan Rose 98bc74eb1cSJordan Rose 99168e29f6SArtem Dergachev SVal ExprEngine::makeZeroElementRegion(ProgramStateRef State, SVal LValue, 100690ab040SArtem Dergachev QualType &Ty, bool &IsArray) { 101bc74eb1cSJordan Rose SValBuilder &SVB = State->getStateManager().getSValBuilder(); 102bc74eb1cSJordan Rose ASTContext &Ctx = SVB.getContext(); 103bc74eb1cSJordan Rose 104bc74eb1cSJordan Rose while (const ArrayType *AT = Ctx.getAsArrayType(Ty)) { 105bc74eb1cSJordan Rose Ty = AT->getElementType(); 106bc74eb1cSJordan Rose LValue = State->getLValue(Ty, SVB.makeZeroArrayIndex(), LValue); 107690ab040SArtem Dergachev IsArray = true; 108bc74eb1cSJordan Rose } 109bc74eb1cSJordan Rose 110bc74eb1cSJordan Rose return LValue; 111bc74eb1cSJordan Rose } 112bc74eb1cSJordan Rose 113239452caSArtem Dergachev std::pair<ProgramStateRef, SVal> ExprEngine::prepareForObjectConstruction( 114239452caSArtem Dergachev const Expr *E, ProgramStateRef State, const LocationContext *LCtx, 115239452caSArtem Dergachev const ConstructionContext *CC, EvalCallOptions &CallOpts) { 11694020268SArtem Dergachev MemRegionManager &MRMgr = getSValBuilder().getRegionManager(); 11705375eb4SJordan Rose 118afb158c2SArtem Dergachev // See if we're constructing an existing region by looking at the 119afb158c2SArtem Dergachev // current construction context. 120661ab34aSArtem Dergachev if (CC) { 1214068481bSArtem Dergachev switch (CC->getKind()) { 12253b8ce0eSArtem Dergachev case ConstructionContext::CXX17ElidedCopyVariableKind: 1234068481bSArtem Dergachev case ConstructionContext::SimpleVariableKind: { 12453b8ce0eSArtem Dergachev const auto *DSCC = cast<VariableConstructionContext>(CC); 1254068481bSArtem Dergachev const auto *DS = DSCC->getDeclStmt(); 126e231bd34SArtem Dergachev const auto *Var = cast<VarDecl>(DS->getSingleDecl()); 127bc74eb1cSJordan Rose SVal LValue = State->getLValue(Var, LCtx); 12820edae87SJordan Rose QualType Ty = Var->getType(); 1291fe52474SArtem Dergachev LValue = 1301fe52474SArtem Dergachev makeZeroElementRegion(State, LValue, Ty, CallOpts.IsArrayCtorOrDtor); 1311fe52474SArtem Dergachev State = 1321fe52474SArtem Dergachev addObjectUnderConstruction(State, DSCC->getDeclStmt(), LCtx, LValue); 1331fe52474SArtem Dergachev return std::make_pair(State, LValue); 13420edae87SJordan Rose } 13553b8ce0eSArtem Dergachev case ConstructionContext::CXX17ElidedCopyConstructorInitializerKind: 136922455feSArtem Dergachev case ConstructionContext::SimpleConstructorInitializerKind: { 1374068481bSArtem Dergachev const auto *ICC = cast<ConstructorInitializerConstructionContext>(CC); 1384068481bSArtem Dergachev const auto *Init = ICC->getCXXCtorInitializer(); 13954529a34SJordan Rose assert(Init->isAnyMemberInitializer()); 14054529a34SJordan Rose const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl()); 141412c0af2SDevin Coughlin Loc ThisPtr = 142dd18b11bSGeorge Karpenkov getSValBuilder().getCXXThis(CurCtor, LCtx->getStackFrame()); 14354529a34SJordan Rose SVal ThisVal = State->getSVal(ThisPtr); 14454529a34SJordan Rose 145bc74eb1cSJordan Rose const ValueDecl *Field; 146bc74eb1cSJordan Rose SVal FieldVal; 14754529a34SJordan Rose if (Init->isIndirectMemberInitializer()) { 148bc74eb1cSJordan Rose Field = Init->getIndirectMember(); 149bc74eb1cSJordan Rose FieldVal = State->getLValue(Init->getIndirectMember(), ThisVal); 15054529a34SJordan Rose } else { 151bc74eb1cSJordan Rose Field = Init->getMember(); 152bc74eb1cSJordan Rose FieldVal = State->getLValue(Init->getMember(), ThisVal); 15354529a34SJordan Rose } 154bc74eb1cSJordan Rose 155bc74eb1cSJordan Rose QualType Ty = Field->getType(); 156690ab040SArtem Dergachev FieldVal = makeZeroElementRegion(State, FieldVal, Ty, 157168e29f6SArtem Dergachev CallOpts.IsArrayCtorOrDtor); 158a84374dcSArtem Dergachev State = addObjectUnderConstruction(State, Init, LCtx, FieldVal); 159239452caSArtem Dergachev return std::make_pair(State, FieldVal); 16054529a34SJordan Rose } 1614068481bSArtem Dergachev case ConstructionContext::NewAllocatedObjectKind: { 1624068481bSArtem Dergachev if (AMgr.getAnalyzerOptions().mayInlineCXXAllocator()) { 1634068481bSArtem Dergachev const auto *NECC = cast<NewAllocatedObjectConstructionContext>(CC); 1644068481bSArtem Dergachev const auto *NE = NECC->getCXXNewExpr(); 165f83d5479SArtem Dergachev SVal V = *getObjectUnderConstruction(State, NE, LCtx); 166f83d5479SArtem Dergachev if (const SubRegion *MR = 167f83d5479SArtem Dergachev dyn_cast_or_null<SubRegion>(V.getAsRegion())) { 1684068481bSArtem Dergachev if (NE->isArray()) { 1694068481bSArtem Dergachev // TODO: In fact, we need to call the constructor for every 1704068481bSArtem Dergachev // allocated element, not just the first one! 1714068481bSArtem Dergachev CallOpts.IsArrayCtorOrDtor = true; 172239452caSArtem Dergachev return std::make_pair( 173239452caSArtem Dergachev State, loc::MemRegionVal(getStoreManager().GetElementZeroRegion( 174239452caSArtem Dergachev MR, NE->getType()->getPointeeType()))); 1754068481bSArtem Dergachev } 176239452caSArtem Dergachev return std::make_pair(State, V); 1774068481bSArtem Dergachev } 178f83d5479SArtem Dergachev // TODO: Detect when the allocator returns a null pointer. 179f83d5479SArtem Dergachev // Constructor shall not be called in this case. 1804068481bSArtem Dergachev } 1814068481bSArtem Dergachev break; 1824068481bSArtem Dergachev } 183f28d7f17SArtem Dergachev case ConstructionContext::SimpleReturnedValueKind: 184f28d7f17SArtem Dergachev case ConstructionContext::CXX17ElidedCopyReturnedValueKind: { 185239452caSArtem Dergachev // The temporary is to be managed by the parent stack frame. 186239452caSArtem Dergachev // So build it in the parent stack frame if we're not in the 187239452caSArtem Dergachev // top frame of the analysis. 188dd18b11bSGeorge Karpenkov const StackFrameContext *SFC = LCtx->getStackFrame(); 189239452caSArtem Dergachev if (const LocationContext *CallerLCtx = SFC->getParent()) { 190239452caSArtem Dergachev auto RTC = (*SFC->getCallSiteBlock())[SFC->getIndex()] 191239452caSArtem Dergachev .getAs<CFGCXXRecordTypedCall>(); 192239452caSArtem Dergachev if (!RTC) { 193239452caSArtem Dergachev // We were unable to find the correct construction context for the 194239452caSArtem Dergachev // call in the parent stack frame. This is equivalent to not being 195239452caSArtem Dergachev // able to find construction context at all. 196239452caSArtem Dergachev break; 197239452caSArtem Dergachev } 198f28d7f17SArtem Dergachev return prepareForObjectConstruction( 199f28d7f17SArtem Dergachev cast<Expr>(SFC->getCallSite()), State, CallerLCtx, 200f28d7f17SArtem Dergachev RTC->getConstructionContext(), CallOpts); 201239452caSArtem Dergachev } else { 202239452caSArtem Dergachev // We are on the top frame of the analysis. 203239452caSArtem Dergachev // TODO: What exactly happens when we are? Does the temporary object 204239452caSArtem Dergachev // live long enough in the region store in this case? Would checkers 205239452caSArtem Dergachev // think that this object immediately goes out of scope? 206239452caSArtem Dergachev CallOpts.IsTemporaryCtorOrDtor = true; 207239452caSArtem Dergachev SVal V = loc::MemRegionVal(MRMgr.getCXXTempObjectRegion(E, LCtx)); 208239452caSArtem Dergachev return std::make_pair(State, V); 209239452caSArtem Dergachev } 210f28d7f17SArtem Dergachev llvm_unreachable("Unhandled return value construction context!"); 211239452caSArtem Dergachev } 212*9a209ad1SArtem Dergachev case ConstructionContext::ElidedTemporaryObjectKind: { 213ff267df0SArtem Dergachev assert(AMgr.getAnalyzerOptions().shouldElideConstructors()); 214*9a209ad1SArtem Dergachev const auto *TCC = cast<ElidedTemporaryObjectConstructionContext>(CC); 215*9a209ad1SArtem Dergachev const CXXBindTemporaryExpr *BTE = TCC->getCXXBindTemporaryExpr(); 216*9a209ad1SArtem Dergachev const MaterializeTemporaryExpr *MTE = TCC->getMaterializedTemporaryExpr(); 217*9a209ad1SArtem Dergachev const CXXConstructExpr *CE = TCC->getConstructorAfterElision(); 218*9a209ad1SArtem Dergachev 219*9a209ad1SArtem Dergachev // Support pre-C++17 copy elision. We'll have the elidable copy 220*9a209ad1SArtem Dergachev // constructor in the AST and in the CFG, but we'll skip it 221*9a209ad1SArtem Dergachev // and construct directly into the final object. This call 222*9a209ad1SArtem Dergachev // also sets the CallOpts flags for us. 223*9a209ad1SArtem Dergachev SVal V; 224*9a209ad1SArtem Dergachev std::tie(State, V) = prepareForObjectConstruction( 225*9a209ad1SArtem Dergachev CE, State, LCtx, TCC->getConstructionContextAfterElision(), CallOpts); 226*9a209ad1SArtem Dergachev 227*9a209ad1SArtem Dergachev // Remember that we've elided the constructor. 228*9a209ad1SArtem Dergachev State = addObjectUnderConstruction(State, CE, LCtx, V); 229*9a209ad1SArtem Dergachev 230*9a209ad1SArtem Dergachev // Remember that we've elided the destructor. 231*9a209ad1SArtem Dergachev if (BTE) 232*9a209ad1SArtem Dergachev State = elideDestructor(State, BTE, LCtx); 233*9a209ad1SArtem Dergachev 234*9a209ad1SArtem Dergachev // Instead of materialization, shamelessly return 235*9a209ad1SArtem Dergachev // the final object destination. 236*9a209ad1SArtem Dergachev if (MTE) 237*9a209ad1SArtem Dergachev State = addObjectUnderConstruction(State, MTE, LCtx, V); 238*9a209ad1SArtem Dergachev 239*9a209ad1SArtem Dergachev return std::make_pair(State, V); 240*9a209ad1SArtem Dergachev } 241ff267df0SArtem Dergachev case ConstructionContext::SimpleTemporaryObjectKind: { 242*9a209ad1SArtem Dergachev const auto *TCC = cast<SimpleTemporaryObjectConstructionContext>(CC); 243239452caSArtem Dergachev const CXXBindTemporaryExpr *BTE = TCC->getCXXBindTemporaryExpr(); 244239452caSArtem Dergachev const MaterializeTemporaryExpr *MTE = TCC->getMaterializedTemporaryExpr(); 245d18639bdSArtem Dergachev SVal V = UnknownVal(); 246239452caSArtem Dergachev 247239452caSArtem Dergachev if (MTE) { 2488cd7961aSArtem Dergachev if (const ValueDecl *VD = MTE->getExtendingDecl()) { 249643102dfSArtem Dergachev assert(MTE->getStorageDuration() != SD_FullExpression); 250643102dfSArtem Dergachev if (!VD->getType()->isReferenceType()) { 251643102dfSArtem Dergachev // We're lifetime-extended by a surrounding aggregate. 252643102dfSArtem Dergachev // Automatic destructors aren't quite working in this case 253643102dfSArtem Dergachev // on the CFG side. We should warn the caller about that. 254310bca01SArtem Dergachev // FIXME: Is there a better way to retrieve this information from 255310bca01SArtem Dergachev // the MaterializeTemporaryExpr? 256310bca01SArtem Dergachev CallOpts.IsTemporaryLifetimeExtendedViaAggregate = true; 257310bca01SArtem Dergachev } 2588cd7961aSArtem Dergachev } 2594fb586c3SArtem Dergachev 2604fb586c3SArtem Dergachev if (MTE->getStorageDuration() == SD_Static || 2614fb586c3SArtem Dergachev MTE->getStorageDuration() == SD_Thread) 2624fb586c3SArtem Dergachev V = loc::MemRegionVal(MRMgr.getCXXStaticTempObjectRegion(E)); 263239452caSArtem Dergachev } 264239452caSArtem Dergachev 2654fb586c3SArtem Dergachev if (V.isUnknown()) 2664fb586c3SArtem Dergachev V = loc::MemRegionVal(MRMgr.getCXXTempObjectRegion(E, LCtx)); 267239452caSArtem Dergachev 268239452caSArtem Dergachev if (BTE) 269239452caSArtem Dergachev State = addObjectUnderConstruction(State, BTE, LCtx, V); 270239452caSArtem Dergachev 271239452caSArtem Dergachev if (MTE) 272239452caSArtem Dergachev State = addObjectUnderConstruction(State, MTE, LCtx, V); 273239452caSArtem Dergachev 2744068481bSArtem Dergachev CallOpts.IsTemporaryCtorOrDtor = true; 275239452caSArtem Dergachev return std::make_pair(State, V); 2764068481bSArtem Dergachev } 2774068481bSArtem Dergachev } 27805375eb4SJordan Rose } 279c9318304SJordan Rose // If we couldn't find an existing region to construct into, assume we're 280690ab040SArtem Dergachev // constructing a temporary. Notify the caller of our failure. 281168e29f6SArtem Dergachev CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion = true; 282239452caSArtem Dergachev return std::make_pair( 283239452caSArtem Dergachev State, loc::MemRegionVal(MRMgr.getCXXTempObjectRegion(E, LCtx))); 284c9318304SJordan Rose } 28505375eb4SJordan Rose 28617f41608SJordan Rose void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *CE, 28717f41608SJordan Rose ExplodedNode *Pred, 28817f41608SJordan Rose ExplodedNodeSet &destNodes) { 28917f41608SJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 29017f41608SJordan Rose ProgramStateRef State = Pred->getState(); 29117f41608SJordan Rose 292f83d5479SArtem Dergachev SVal Target = UnknownVal(); 29317f41608SJordan Rose 294*9a209ad1SArtem Dergachev if (Optional<SVal> ElidedTarget = 295*9a209ad1SArtem Dergachev getObjectUnderConstruction(State, CE, LCtx)) { 296*9a209ad1SArtem Dergachev // We've previously modeled an elidable constructor by pretending that it in 297*9a209ad1SArtem Dergachev // fact constructs into the correct target. This constructor can therefore 298*9a209ad1SArtem Dergachev // be skipped. 299*9a209ad1SArtem Dergachev Target = *ElidedTarget; 300*9a209ad1SArtem Dergachev StmtNodeBuilder Bldr(Pred, destNodes, *currBldrCtx); 301*9a209ad1SArtem Dergachev State = finishObjectConstruction(State, CE, LCtx); 302*9a209ad1SArtem Dergachev if (auto L = Target.getAs<Loc>()) 303*9a209ad1SArtem Dergachev State = State->BindExpr(CE, LCtx, State->getSVal(*L, CE->getType())); 304*9a209ad1SArtem Dergachev Bldr.generateNode(CE, Pred, State); 305*9a209ad1SArtem Dergachev return; 306*9a209ad1SArtem Dergachev } 307*9a209ad1SArtem Dergachev 30817f41608SJordan Rose // FIXME: Handle arrays, which run the same constructor for every element. 30917f41608SJordan Rose // For now, we just run the first constructor (which should still invalidate 31017f41608SJordan Rose // the entire array). 31117f41608SJordan Rose 312690ab040SArtem Dergachev EvalCallOptions CallOpts; 313661ab34aSArtem Dergachev auto C = getCurrentCFGElement().getAs<CFGConstructor>(); 3144068481bSArtem Dergachev assert(C || getCurrentCFGElement().getAs<CFGStmt>()); 315661ab34aSArtem Dergachev const ConstructionContext *CC = C ? C->getConstructionContext() : nullptr; 316661ab34aSArtem Dergachev 31717f41608SJordan Rose switch (CE->getConstructionKind()) { 31817f41608SJordan Rose case CXXConstructExpr::CK_Complete: { 319239452caSArtem Dergachev std::tie(State, Target) = 320239452caSArtem Dergachev prepareForObjectConstruction(CE, State, LCtx, CC, CallOpts); 321b970505dSJordan Rose break; 322b970505dSJordan Rose } 323b3b976f0SJordan Rose case CXXConstructExpr::CK_VirtualBase: 324b3b976f0SJordan Rose // Make sure we are not calling virtual base class initializers twice. 325b3b976f0SJordan Rose // Only the most-derived object should initialize virtual base classes. 326dd18b11bSGeorge Karpenkov if (const Stmt *Outer = LCtx->getStackFrame()->getCallSite()) { 327b3b976f0SJordan Rose const CXXConstructExpr *OuterCtor = dyn_cast<CXXConstructExpr>(Outer); 328b3b976f0SJordan Rose if (OuterCtor) { 329b3b976f0SJordan Rose switch (OuterCtor->getConstructionKind()) { 330b970505dSJordan Rose case CXXConstructExpr::CK_NonVirtualBase: 331b970505dSJordan Rose case CXXConstructExpr::CK_VirtualBase: 332b3b976f0SJordan Rose // Bail out! 333b3b976f0SJordan Rose destNodes.Add(Pred); 334b3b976f0SJordan Rose return; 335b3b976f0SJordan Rose case CXXConstructExpr::CK_Complete: 336b3b976f0SJordan Rose case CXXConstructExpr::CK_Delegating: 337b3b976f0SJordan Rose break; 338b3b976f0SJordan Rose } 339b3b976f0SJordan Rose } 340b3b976f0SJordan Rose } 341b3b976f0SJordan Rose // FALLTHROUGH 342b3b976f0SJordan Rose case CXXConstructExpr::CK_NonVirtualBase: 343e8ba3ec4SArtem Dergachev // In C++17, classes with non-virtual bases may be aggregates, so they would 344e8ba3ec4SArtem Dergachev // be initialized as aggregates without a constructor call, so we may have 345e8ba3ec4SArtem Dergachev // a base class constructed directly into an initializer list without 346e8ba3ec4SArtem Dergachev // having the derived-class constructor call on the previous stack frame. 347e8ba3ec4SArtem Dergachev // Initializer lists may be nested into more initializer lists that 348e8ba3ec4SArtem Dergachev // correspond to surrounding aggregate initializations. 349e8ba3ec4SArtem Dergachev // FIXME: For now this code essentially bails out. We need to find the 350e8ba3ec4SArtem Dergachev // correct target region and set it. 351e8ba3ec4SArtem Dergachev // FIXME: Instead of relying on the ParentMap, we should have the 352e8ba3ec4SArtem Dergachev // trigger-statement (InitListExpr in this case) passed down from CFG or 353e8ba3ec4SArtem Dergachev // otherwise always available during construction. 354e8ba3ec4SArtem Dergachev if (dyn_cast_or_null<InitListExpr>(LCtx->getParentMap().getParent(CE))) { 355e8ba3ec4SArtem Dergachev MemRegionManager &MRMgr = getSValBuilder().getRegionManager(); 356f83d5479SArtem Dergachev Target = loc::MemRegionVal(MRMgr.getCXXTempObjectRegion(CE, LCtx)); 357168e29f6SArtem Dergachev CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion = true; 358e8ba3ec4SArtem Dergachev break; 359e8ba3ec4SArtem Dergachev } 360e8ba3ec4SArtem Dergachev // FALLTHROUGH 361b970505dSJordan Rose case CXXConstructExpr::CK_Delegating: { 362b970505dSJordan Rose const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl()); 363b970505dSJordan Rose Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor, 364dd18b11bSGeorge Karpenkov LCtx->getStackFrame()); 36505375eb4SJordan Rose SVal ThisVal = State->getSVal(ThisPtr); 366b970505dSJordan Rose 367b970505dSJordan Rose if (CE->getConstructionKind() == CXXConstructExpr::CK_Delegating) { 368f83d5479SArtem Dergachev Target = ThisVal; 369b970505dSJordan Rose } else { 370b970505dSJordan Rose // Cast to the base type. 371599f85abSJordan Rose bool IsVirtual = 372599f85abSJordan Rose (CE->getConstructionKind() == CXXConstructExpr::CK_VirtualBase); 373599f85abSJordan Rose SVal BaseVal = getStoreManager().evalDerivedToBase(ThisVal, CE->getType(), 374599f85abSJordan Rose IsVirtual); 375f83d5479SArtem Dergachev Target = BaseVal; 376b970505dSJordan Rose } 377b970505dSJordan Rose break; 378b970505dSJordan Rose } 379b970505dSJordan Rose } 380b970505dSJordan Rose 381239452caSArtem Dergachev if (State != Pred->getState()) { 382239452caSArtem Dergachev static SimpleProgramPointTag T("ExprEngine", 383239452caSArtem Dergachev "Prepare for object construction"); 384239452caSArtem Dergachev ExplodedNodeSet DstPrepare; 385239452caSArtem Dergachev StmtNodeBuilder BldrPrepare(Pred, DstPrepare, *currBldrCtx); 386239452caSArtem Dergachev BldrPrepare.generateNode(CE, Pred, State, &T, ProgramPoint::PreStmtKind); 387239452caSArtem Dergachev assert(DstPrepare.size() <= 1); 388239452caSArtem Dergachev if (DstPrepare.size() == 0) 389239452caSArtem Dergachev return; 390239452caSArtem Dergachev Pred = *BldrPrepare.begin(); 391239452caSArtem Dergachev } 392239452caSArtem Dergachev 393fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 394fcd016e5SJordan Rose CallEventRef<CXXConstructorCall> Call = 395f83d5479SArtem Dergachev CEMgr.getCXXConstructorCall(CE, Target.getAsRegion(), State, LCtx); 39610e25031STed Kremenek 397f3c12ac3SJordan Rose ExplodedNodeSet DstPreVisit; 398f3c12ac3SJordan Rose getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, CE, *this); 399b8e28654SJordan Rose 400661ab34aSArtem Dergachev // FIXME: Is it possible and/or useful to do this before PreStmt? 401b8e28654SJordan Rose ExplodedNodeSet PreInitialized; 402b8e28654SJordan Rose { 403b8e28654SJordan Rose StmtNodeBuilder Bldr(DstPreVisit, PreInitialized, *currBldrCtx); 404b8e28654SJordan Rose for (ExplodedNodeSet::iterator I = DstPreVisit.begin(), 405b8e28654SJordan Rose E = DstPreVisit.end(); 406b8e28654SJordan Rose I != E; ++I) { 407b8e28654SJordan Rose ProgramStateRef State = (*I)->getState(); 408661ab34aSArtem Dergachev if (CE->requiresZeroInitialization()) { 409b8e28654SJordan Rose // FIXME: Once we properly handle constructors in new-expressions, we'll 410b8e28654SJordan Rose // need to invalidate the region before setting a default value, to make 411b8e28654SJordan Rose // sure there aren't any lingering bindings around. This probably needs 412b8e28654SJordan Rose // to happen regardless of whether or not the object is zero-initialized 413b8e28654SJordan Rose // to handle random fields of a placement-initialized object picking up 414b8e28654SJordan Rose // old bindings. We might only want to do it when we need to, though. 415b8e28654SJordan Rose // FIXME: This isn't actually correct for arrays -- we need to zero- 416b8e28654SJordan Rose // initialize the entire array, not just the first element -- but our 417b8e28654SJordan Rose // handling of arrays everywhere else is weak as well, so this shouldn't 418b8e28654SJordan Rose // actually make things worse. Placement new makes this tricky as well, 419b8e28654SJordan Rose // since it's then possible to be initializing one part of a multi- 420b8e28654SJordan Rose // dimensional array. 421f83d5479SArtem Dergachev State = State->bindDefaultZero(Target, LCtx); 422661ab34aSArtem Dergachev } 423661ab34aSArtem Dergachev 4240dbb783cSCraig Topper Bldr.generateNode(CE, *I, State, /*tag=*/nullptr, 4250dbb783cSCraig Topper ProgramPoint::PreStmtKind); 426b8e28654SJordan Rose } 427b8e28654SJordan Rose } 428b8e28654SJordan Rose 429afe7c2c2SJordan Rose ExplodedNodeSet DstPreCall; 430b8e28654SJordan Rose getCheckerManager().runCheckersForPreCall(DstPreCall, PreInitialized, 431fcd016e5SJordan Rose *Call, *this); 43210e25031STed Kremenek 43388bb563cSJordan Rose ExplodedNodeSet DstEvaluated; 43488bb563cSJordan Rose StmtNodeBuilder Bldr(DstPreCall, DstEvaluated, *currBldrCtx); 43588bb563cSJordan Rose 436e83cb092SJordan Rose if (CE->getConstructor()->isTrivial() && 437e83cb092SJordan Rose CE->getConstructor()->isCopyOrMoveConstructor() && 438168e29f6SArtem Dergachev !CallOpts.IsArrayCtorOrDtor) { 439e83cb092SJordan Rose // FIXME: Handle other kinds of trivial constructors as well. 440e83cb092SJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 44188bb563cSJordan Rose I != E; ++I) 44288bb563cSJordan Rose performTrivialCopy(Bldr, *I, *Call); 44388bb563cSJordan Rose 44488bb563cSJordan Rose } else { 445afe7c2c2SJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 446f3c12ac3SJordan Rose I != E; ++I) 447690ab040SArtem Dergachev defaultEvalCall(Bldr, *I, *Call, CallOpts); 44888bb563cSJordan Rose } 44910e25031STed Kremenek 4502a8c18d9SAlexander Kornienko // If the CFG was constructed without elements for temporary destructors 4515b1ee2faSDevin Coughlin // and the just-called constructor created a temporary object then 4525b1ee2faSDevin Coughlin // stop exploration if the temporary object has a noreturn constructor. 4535b1ee2faSDevin Coughlin // This can lose coverage because the destructor, if it were present 4545b1ee2faSDevin Coughlin // in the CFG, would be called at the end of the full expression or 4555b1ee2faSDevin Coughlin // later (for life-time extended temporaries) -- but avoids infeasible 4565b1ee2faSDevin Coughlin // paths when no-return temporary destructors are used for assertions. 4575b1ee2faSDevin Coughlin const AnalysisDeclContext *ADC = LCtx->getAnalysisDeclContext(); 4585b1ee2faSDevin Coughlin if (!ADC->getCFGBuildOptions().AddTemporaryDtors) { 4595b1ee2faSDevin Coughlin const MemRegion *Target = Call->getCXXThisVal().getAsRegion(); 4605b1ee2faSDevin Coughlin if (Target && isa<CXXTempObjectRegion>(Target) && 4615b1ee2faSDevin Coughlin Call->getDecl()->getParent()->isAnyDestructorNoReturn()) { 4625b1ee2faSDevin Coughlin 4633da72051SArtem Dergachev // If we've inlined the constructor, then DstEvaluated would be empty. 4643da72051SArtem Dergachev // In this case we still want a sink, which could be implemented 4653da72051SArtem Dergachev // in processCallExit. But we don't have that implemented at the moment, 4663da72051SArtem Dergachev // so if you hit this assertion, see if you can avoid inlining 4673da72051SArtem Dergachev // the respective constructor when analyzer-config cfg-temporary-dtors 4683da72051SArtem Dergachev // is set to false. 4693da72051SArtem Dergachev // Otherwise there's nothing wrong with inlining such constructor. 4703da72051SArtem Dergachev assert(!DstEvaluated.empty() && 4713da72051SArtem Dergachev "We should not have inlined this constructor!"); 4723da72051SArtem Dergachev 4735b1ee2faSDevin Coughlin for (ExplodedNode *N : DstEvaluated) { 4745b1ee2faSDevin Coughlin Bldr.generateSink(CE, N, N->getState()); 4755b1ee2faSDevin Coughlin } 4765b1ee2faSDevin Coughlin 4775b1ee2faSDevin Coughlin // There is no need to run the PostCall and PostStmt checker 4785b1ee2faSDevin Coughlin // callbacks because we just generated sinks on all nodes in th 4795b1ee2faSDevin Coughlin // frontier. 4805b1ee2faSDevin Coughlin return; 4815b1ee2faSDevin Coughlin } 4825b1ee2faSDevin Coughlin } 4835b1ee2faSDevin Coughlin 484afe7c2c2SJordan Rose ExplodedNodeSet DstPostCall; 48588bb563cSJordan Rose getCheckerManager().runCheckersForPostCall(DstPostCall, DstEvaluated, 486fcd016e5SJordan Rose *Call, *this); 487afe7c2c2SJordan Rose getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, CE, *this); 48810e25031STed Kremenek } 48910e25031STed Kremenek 490b970505dSJordan Rose void ExprEngine::VisitCXXDestructor(QualType ObjectType, 49110e25031STed Kremenek const MemRegion *Dest, 49210e25031STed Kremenek const Stmt *S, 4932bc9674bSJordan Rose bool IsBaseDtor, 49410e25031STed Kremenek ExplodedNode *Pred, 495168e29f6SArtem Dergachev ExplodedNodeSet &Dst, 496168e29f6SArtem Dergachev const EvalCallOptions &CallOpts) { 497fcd016e5SJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 498fcd016e5SJordan Rose ProgramStateRef State = Pred->getState(); 499fcd016e5SJordan Rose 500b970505dSJordan Rose const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl(); 501b970505dSJordan Rose assert(RecordDecl && "Only CXXRecordDecls should have destructors"); 502b970505dSJordan Rose const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor(); 503b970505dSJordan Rose 504fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 505fcd016e5SJordan Rose CallEventRef<CXXDestructorCall> Call = 5062bc9674bSJordan Rose CEMgr.getCXXDestructorCall(DtorDecl, S, Dest, IsBaseDtor, State, LCtx); 507142adc49STed Kremenek 50817a8757aSJordan Rose PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(), 50917a8757aSJordan Rose Call->getSourceRange().getBegin(), 51017a8757aSJordan Rose "Error evaluating destructor"); 51117a8757aSJordan Rose 5124ee71b8aSJordan Rose ExplodedNodeSet DstPreCall; 5134ee71b8aSJordan Rose getCheckerManager().runCheckersForPreCall(DstPreCall, Pred, 514fcd016e5SJordan Rose *Call, *this); 51510e25031STed Kremenek 5164ee71b8aSJordan Rose ExplodedNodeSet DstInvalidated; 517d94854a4STed Kremenek StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx); 5184ee71b8aSJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 5194ee71b8aSJordan Rose I != E; ++I) 520690ab040SArtem Dergachev defaultEvalCall(Bldr, *I, *Call, CallOpts); 5214ee71b8aSJordan Rose 5224ee71b8aSJordan Rose ExplodedNodeSet DstPostCall; 5234ee71b8aSJordan Rose getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated, 524fcd016e5SJordan Rose *Call, *this); 52510e25031STed Kremenek } 52610e25031STed Kremenek 5278b808d64SJordan Rose void ExprEngine::VisitCXXNewAllocatorCall(const CXXNewExpr *CNE, 5288b808d64SJordan Rose ExplodedNode *Pred, 5298b808d64SJordan Rose ExplodedNodeSet &Dst) { 5308b808d64SJordan Rose ProgramStateRef State = Pred->getState(); 5318b808d64SJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 5328b808d64SJordan Rose PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(), 5338b808d64SJordan Rose CNE->getStartLoc(), 5348b808d64SJordan Rose "Error evaluating New Allocator Call"); 5358b808d64SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 5368b808d64SJordan Rose CallEventRef<CXXAllocatorCall> Call = 5378b808d64SJordan Rose CEMgr.getCXXAllocatorCall(CNE, State, LCtx); 5388b808d64SJordan Rose 5398b808d64SJordan Rose ExplodedNodeSet DstPreCall; 5408b808d64SJordan Rose getCheckerManager().runCheckersForPreCall(DstPreCall, Pred, 5418b808d64SJordan Rose *Call, *this); 5428b808d64SJordan Rose 54355796302SArtem Dergachev ExplodedNodeSet DstPostCall; 54455796302SArtem Dergachev StmtNodeBuilder CallBldr(DstPreCall, DstPostCall, *currBldrCtx); 5451084de52SArtem Dergachev for (auto I : DstPreCall) { 5461084de52SArtem Dergachev // FIXME: Provide evalCall for checkers? 54755796302SArtem Dergachev defaultEvalCall(CallBldr, I, *Call); 5481084de52SArtem Dergachev } 549beba5307SArtem Dergachev // If the call is inlined, DstPostCall will be empty and we bail out now. 55055796302SArtem Dergachev 55155796302SArtem Dergachev // Store return value of operator new() for future use, until the actual 55255796302SArtem Dergachev // CXXNewExpr gets processed. 55355796302SArtem Dergachev ExplodedNodeSet DstPostValue; 55455796302SArtem Dergachev StmtNodeBuilder ValueBldr(DstPostCall, DstPostValue, *currBldrCtx); 55555796302SArtem Dergachev for (auto I : DstPostCall) { 556beba5307SArtem Dergachev // FIXME: Because CNE serves as the "call site" for the allocator (due to 557beba5307SArtem Dergachev // lack of a better expression in the AST), the conjured return value symbol 558beba5307SArtem Dergachev // is going to be of the same type (C++ object pointer type). Technically 559beba5307SArtem Dergachev // this is not correct because the operator new's prototype always says that 560beba5307SArtem Dergachev // it returns a 'void *'. So we should change the type of the symbol, 561beba5307SArtem Dergachev // and then evaluate the cast over the symbolic pointer from 'void *' to 562beba5307SArtem Dergachev // the object pointer type. But without changing the symbol's type it 563beba5307SArtem Dergachev // is breaking too much to evaluate the no-op symbolic cast over it, so we 564beba5307SArtem Dergachev // skip it for now. 56555796302SArtem Dergachev ProgramStateRef State = I->getState(); 56650e0372fSArtem Dergachev SVal RetVal = State->getSVal(CNE, LCtx); 56750e0372fSArtem Dergachev 56850e0372fSArtem Dergachev // If this allocation function is not declared as non-throwing, failures 56950e0372fSArtem Dergachev // /must/ be signalled by exceptions, and thus the return value will never 57050e0372fSArtem Dergachev // be NULL. -fno-exceptions does not influence this semantics. 57150e0372fSArtem Dergachev // FIXME: GCC has a -fcheck-new option, which forces it to consider the case 57250e0372fSArtem Dergachev // where new can return NULL. If we end up supporting that option, we can 57350e0372fSArtem Dergachev // consider adding a check for it here. 57450e0372fSArtem Dergachev // C++11 [basic.stc.dynamic.allocation]p3. 57550e0372fSArtem Dergachev if (const FunctionDecl *FD = CNE->getOperatorNew()) { 57650e0372fSArtem Dergachev QualType Ty = FD->getType(); 57750e0372fSArtem Dergachev if (const auto *ProtoType = Ty->getAs<FunctionProtoType>()) 578eaf11ad7SRichard Smith if (!ProtoType->isNothrow()) 57950e0372fSArtem Dergachev State = State->assume(RetVal.castAs<DefinedOrUnknownSVal>(), true); 58050e0372fSArtem Dergachev } 58150e0372fSArtem Dergachev 582f83d5479SArtem Dergachev ValueBldr.generateNode( 583f83d5479SArtem Dergachev CNE, I, addObjectUnderConstruction(State, CNE, LCtx, RetVal)); 58455796302SArtem Dergachev } 58555796302SArtem Dergachev 58613b2026bSArtem Dergachev ExplodedNodeSet DstPostPostCallCallback; 58713b2026bSArtem Dergachev getCheckerManager().runCheckersForPostCall(DstPostPostCallCallback, 58813b2026bSArtem Dergachev DstPostValue, *Call, *this); 58913b2026bSArtem Dergachev for (auto I : DstPostPostCallCallback) { 59013b2026bSArtem Dergachev getCheckerManager().runCheckersForNewAllocator( 591f83d5479SArtem Dergachev CNE, *getObjectUnderConstruction(I->getState(), CNE, LCtx), Dst, I, 592f83d5479SArtem Dergachev *this); 59313b2026bSArtem Dergachev } 5948b808d64SJordan Rose } 5958b808d64SJordan Rose 59610e25031STed Kremenek void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred, 59710e25031STed Kremenek ExplodedNodeSet &Dst) { 598a4ee064cSJordan Rose // FIXME: Much of this should eventually migrate to CXXAllocatorCall. 599a4ee064cSJordan Rose // Also, we need to decide how allocators actually work -- they're not 600a4ee064cSJordan Rose // really part of the CXXNewExpr because they happen BEFORE the 601a4ee064cSJordan Rose // CXXConstructExpr subexpression. See PR12014 for some discussion. 60210e25031STed Kremenek 603d94854a4STed Kremenek unsigned blockCount = currBldrCtx->blockCount(); 604d519cae8STed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 60555796302SArtem Dergachev SVal symVal = UnknownVal(); 6068b662704SAnton Yartsev FunctionDecl *FD = CNE->getOperatorNew(); 60710e25031STed Kremenek 6081084de52SArtem Dergachev bool IsStandardGlobalOpNewFunction = 6091084de52SArtem Dergachev FD->isReplaceableGlobalAllocationFunction(); 6108b662704SAnton Yartsev 61155796302SArtem Dergachev ProgramStateRef State = Pred->getState(); 61255796302SArtem Dergachev 61355796302SArtem Dergachev // Retrieve the stored operator new() return value. 61455796302SArtem Dergachev if (AMgr.getAnalyzerOptions().mayInlineCXXAllocator()) { 615f83d5479SArtem Dergachev symVal = *getObjectUnderConstruction(State, CNE, LCtx); 616f83d5479SArtem Dergachev State = finishObjectConstruction(State, CNE, LCtx); 61755796302SArtem Dergachev } 61855796302SArtem Dergachev 6198b662704SAnton Yartsev // We assume all standard global 'operator new' functions allocate memory in 6208b662704SAnton Yartsev // heap. We realize this is an approximation that might not correctly model 6218b662704SAnton Yartsev // a custom global allocator. 62255796302SArtem Dergachev if (symVal.isUnknown()) { 6238b662704SAnton Yartsev if (IsStandardGlobalOpNewFunction) 6248b662704SAnton Yartsev symVal = svalBuilder.getConjuredHeapSymbolVal(CNE, LCtx, blockCount); 6258b662704SAnton Yartsev else 6260dbb783cSCraig Topper symVal = svalBuilder.conjureSymbolVal(nullptr, CNE, LCtx, CNE->getType(), 6278b662704SAnton Yartsev blockCount); 62855796302SArtem Dergachev } 6298b662704SAnton Yartsev 630fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 631fcd016e5SJordan Rose CallEventRef<CXXAllocatorCall> Call = 632fcd016e5SJordan Rose CEMgr.getCXXAllocatorCall(CNE, State, LCtx); 633fcd016e5SJordan Rose 63455796302SArtem Dergachev if (!AMgr.getAnalyzerOptions().mayInlineCXXAllocator()) { 635a4ee064cSJordan Rose // Invalidate placement args. 636fcd016e5SJordan Rose // FIXME: Once we figure out how we want allocators to work, 637fcd016e5SJordan Rose // we should be using the usual pre-/(default-)eval-/post-call checks here. 638fcd016e5SJordan Rose State = Call->invalidateRegions(blockCount); 639b8cb8359SJordan Rose if (!State) 640b8cb8359SJordan Rose return; 641a4ee064cSJordan Rose 642fce1b03eSPavel Labath // If this allocation function is not declared as non-throwing, failures 64350e0372fSArtem Dergachev // /must/ be signalled by exceptions, and thus the return value will never 64450e0372fSArtem Dergachev // be NULL. -fno-exceptions does not influence this semantics. 645fce1b03eSPavel Labath // FIXME: GCC has a -fcheck-new option, which forces it to consider the case 646fce1b03eSPavel Labath // where new can return NULL. If we end up supporting that option, we can 647fce1b03eSPavel Labath // consider adding a check for it here. 6483957fd58SJordan Rose // C++11 [basic.stc.dynamic.allocation]p3. 649fce1b03eSPavel Labath if (FD) { 6503957fd58SJordan Rose QualType Ty = FD->getType(); 65150e0372fSArtem Dergachev if (const auto *ProtoType = Ty->getAs<FunctionProtoType>()) 652eaf11ad7SRichard Smith if (!ProtoType->isNothrow()) 65355796302SArtem Dergachev if (auto dSymVal = symVal.getAs<DefinedOrUnknownSVal>()) 65455796302SArtem Dergachev State = State->assume(*dSymVal, true); 6553957fd58SJordan Rose } 65650e0372fSArtem Dergachev } 6573957fd58SJordan Rose 658b8cb8359SJordan Rose StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 659b8cb8359SJordan Rose 660beba5307SArtem Dergachev SVal Result = symVal; 661beba5307SArtem Dergachev 66210e25031STed Kremenek if (CNE->isArray()) { 66310e25031STed Kremenek // FIXME: allocating an array requires simulating the constructors. 66410e25031STed Kremenek // For now, just return a symbolicated region. 6651084de52SArtem Dergachev if (const SubRegion *NewReg = 6661084de52SArtem Dergachev dyn_cast_or_null<SubRegion>(symVal.getAsRegion())) { 667a4ee064cSJordan Rose QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType(); 668a4ee064cSJordan Rose const ElementRegion *EleReg = 669a4ee064cSJordan Rose getStoreManager().GetElementZeroRegion(NewReg, ObjTy); 670beba5307SArtem Dergachev Result = loc::MemRegionVal(EleReg); 671beba5307SArtem Dergachev } 672beba5307SArtem Dergachev State = State->BindExpr(CNE, Pred->getLocationContext(), Result); 67344320367SJordan Rose Bldr.generateNode(CNE, Pred, State); 67410e25031STed Kremenek return; 67510e25031STed Kremenek } 67610e25031STed Kremenek 677a4ee064cSJordan Rose // FIXME: Once we have proper support for CXXConstructExprs inside 678a4ee064cSJordan Rose // CXXNewExpr, we need to make sure that the constructed object is not 679a4ee064cSJordan Rose // immediately invalidated here. (The placement call should happen before 680a4ee064cSJordan Rose // the constructor call anyway.) 68144320367SJordan Rose if (FD && FD->isReservedGlobalPlacementOperator()) { 68244320367SJordan Rose // Non-array placement new should always return the placement location. 68344320367SJordan Rose SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx); 684b8cb8359SJordan Rose Result = svalBuilder.evalCast(PlacementLoc, CNE->getType(), 685aaf83184SJordan Rose CNE->getPlacementArg(0)->getType()); 68644320367SJordan Rose } 68744320367SJordan Rose 688b8cb8359SJordan Rose // Bind the address of the object, then check to see if we cached out. 689b8cb8359SJordan Rose State = State->BindExpr(CNE, LCtx, Result); 6908f6b4b04SJordan Rose ExplodedNode *NewN = Bldr.generateNode(CNE, Pred, State); 6918f6b4b04SJordan Rose if (!NewN) 6928f6b4b04SJordan Rose return; 6933503cb35SJordan Rose 6945089f3b3SJordan Rose // If the type is not a record, we won't have a CXXConstructExpr as an 6955089f3b3SJordan Rose // initializer. Copy the value over. 6965089f3b3SJordan Rose if (const Expr *Init = CNE->getInitializer()) { 6975089f3b3SJordan Rose if (!isa<CXXConstructExpr>(Init)) { 6983503cb35SJordan Rose assert(Bldr.getResults().size() == 1); 6998f6b4b04SJordan Rose Bldr.takeNodes(NewN); 700b8cb8359SJordan Rose evalBind(Dst, CNE, NewN, Result, State->getSVal(Init, LCtx), 701b8cb8359SJordan Rose /*FirstInit=*/IsStandardGlobalOpNewFunction); 7023503cb35SJordan Rose } 7033503cb35SJordan Rose } 70410e25031STed Kremenek } 70510e25031STed Kremenek 70610e25031STed Kremenek void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE, 70710e25031STed Kremenek ExplodedNode *Pred, ExplodedNodeSet &Dst) { 708d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 70964aea652SErik Verbruggen ProgramStateRef state = Pred->getState(); 71064aea652SErik Verbruggen Bldr.generateNode(CDE, Pred, state); 71110e25031STed Kremenek } 71210e25031STed Kremenek 7138fdb59f9STed Kremenek void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS, 7148fdb59f9STed Kremenek ExplodedNode *Pred, 7158fdb59f9STed Kremenek ExplodedNodeSet &Dst) { 7168fdb59f9STed Kremenek const VarDecl *VD = CS->getExceptionDecl(); 717b1279b5cSTed Kremenek if (!VD) { 718b1279b5cSTed Kremenek Dst.Add(Pred); 719b1279b5cSTed Kremenek return; 720b1279b5cSTed Kremenek } 721b1279b5cSTed Kremenek 7228fdb59f9STed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 723d227833cSTed Kremenek SVal V = svalBuilder.conjureSymbolVal(CS, LCtx, VD->getType(), 724d94854a4STed Kremenek currBldrCtx->blockCount()); 7258fdb59f9STed Kremenek ProgramStateRef state = Pred->getState(); 726b570195cSAnna Zaks state = state->bindLoc(state->getLValue(VD, LCtx), V, LCtx); 7278fdb59f9STed Kremenek 728d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 7298fdb59f9STed Kremenek Bldr.generateNode(CS, Pred, state); 7308fdb59f9STed Kremenek } 7318fdb59f9STed Kremenek 73210e25031STed Kremenek void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred, 73310e25031STed Kremenek ExplodedNodeSet &Dst) { 734d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 735e594034fSAnna Zaks 73610e25031STed Kremenek // Get the this object region from StoreManager. 737632e3b7eSTed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 73810e25031STed Kremenek const MemRegion *R = 73910e25031STed Kremenek svalBuilder.getRegionManager().getCXXThisRegion( 74010e25031STed Kremenek getContext().getCanonicalType(TE->getType()), 741632e3b7eSTed Kremenek LCtx); 74210e25031STed Kremenek 74349b1e38eSTed Kremenek ProgramStateRef state = Pred->getState(); 74410e25031STed Kremenek SVal V = state->getSVal(loc::MemRegionVal(R)); 745632e3b7eSTed Kremenek Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V)); 74610e25031STed Kremenek } 74715843343SGabor Horvath 74815843343SGabor Horvath void ExprEngine::VisitLambdaExpr(const LambdaExpr *LE, ExplodedNode *Pred, 74915843343SGabor Horvath ExplodedNodeSet &Dst) { 75015843343SGabor Horvath const LocationContext *LocCtxt = Pred->getLocationContext(); 75115843343SGabor Horvath 75215843343SGabor Horvath // Get the region of the lambda itself. 75315843343SGabor Horvath const MemRegion *R = svalBuilder.getRegionManager().getCXXTempObjectRegion( 75415843343SGabor Horvath LE, LocCtxt); 75515843343SGabor Horvath SVal V = loc::MemRegionVal(R); 75615843343SGabor Horvath 75715843343SGabor Horvath ProgramStateRef State = Pred->getState(); 75815843343SGabor Horvath 75915843343SGabor Horvath // If we created a new MemRegion for the lambda, we should explicitly bind 76015843343SGabor Horvath // the captures. 76115843343SGabor Horvath CXXRecordDecl::field_iterator CurField = LE->getLambdaClass()->field_begin(); 76215843343SGabor Horvath for (LambdaExpr::const_capture_init_iterator i = LE->capture_init_begin(), 76315843343SGabor Horvath e = LE->capture_init_end(); 76415843343SGabor Horvath i != e; ++i, ++CurField) { 7657bdca8b2SDevin Coughlin FieldDecl *FieldForCapture = *CurField; 7667bdca8b2SDevin Coughlin SVal FieldLoc = State->getLValue(FieldForCapture, V); 7677bdca8b2SDevin Coughlin 7687bdca8b2SDevin Coughlin SVal InitVal; 7697bdca8b2SDevin Coughlin if (!FieldForCapture->hasCapturedVLAType()) { 7707bdca8b2SDevin Coughlin Expr *InitExpr = *i; 7717bdca8b2SDevin Coughlin assert(InitExpr && "Capture missing initialization expression"); 7727bdca8b2SDevin Coughlin InitVal = State->getSVal(InitExpr, LocCtxt); 7737bdca8b2SDevin Coughlin } else { 7747bdca8b2SDevin Coughlin // The field stores the length of a captured variable-length array. 7757bdca8b2SDevin Coughlin // These captures don't have initialization expressions; instead we 7767bdca8b2SDevin Coughlin // get the length from the VLAType size expression. 7777bdca8b2SDevin Coughlin Expr *SizeExpr = FieldForCapture->getCapturedVLAType()->getSizeExpr(); 7787bdca8b2SDevin Coughlin InitVal = State->getSVal(SizeExpr, LocCtxt); 7797bdca8b2SDevin Coughlin } 7807bdca8b2SDevin Coughlin 781b570195cSAnna Zaks State = State->bindLoc(FieldLoc, InitVal, LocCtxt); 78215843343SGabor Horvath } 78315843343SGabor Horvath 78415843343SGabor Horvath // Decay the Loc into an RValue, because there might be a 78515843343SGabor Horvath // MaterializeTemporaryExpr node above this one which expects the bound value 78615843343SGabor Horvath // to be an RValue. 78715843343SGabor Horvath SVal LambdaRVal = State->getSVal(R); 78815843343SGabor Horvath 78915843343SGabor Horvath ExplodedNodeSet Tmp; 79015843343SGabor Horvath StmtNodeBuilder Bldr(Pred, Tmp, *currBldrCtx); 79115843343SGabor Horvath // FIXME: is this the right program point kind? 79215843343SGabor Horvath Bldr.generateNode(LE, Pred, 79315843343SGabor Horvath State->BindExpr(LE, LocCtxt, LambdaRVal), 79415843343SGabor Horvath nullptr, ProgramPoint::PostLValueKind); 79515843343SGabor Horvath 79615843343SGabor Horvath // FIXME: Move all post/pre visits to ::Visit(). 79715843343SGabor Horvath getCheckerManager().runCheckersForPostStmt(Dst, Tmp, LE, *this); 79815843343SGabor Horvath } 799