110e25031STed Kremenek //===- ExprEngineCXX.cpp - ExprEngine support for C++ -----------*- C++ -*-===// 210e25031STed Kremenek // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 610e25031STed Kremenek // 710e25031STed Kremenek //===----------------------------------------------------------------------===// 810e25031STed Kremenek // 910e25031STed Kremenek // This file defines the C++ expression evaluation engine. 1010e25031STed Kremenek // 1110e25031STed Kremenek //===----------------------------------------------------------------------===// 1210e25031STed Kremenek 1310e25031STed Kremenek #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 144068481bSArtem Dergachev #include "clang/Analysis/ConstructionContext.h" 1510e25031STed Kremenek #include "clang/AST/DeclCXX.h" 168fdb59f9STed Kremenek #include "clang/AST/StmtCXX.h" 17e8ba3ec4SArtem Dergachev #include "clang/AST/ParentMap.h" 1817a8757aSJordan Rose #include "clang/Basic/PrettyStackTrace.h" 193a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/CheckerManager.h" 203a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" 213a02247dSChandler Carruth #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 2210e25031STed Kremenek 2310e25031STed Kremenek using namespace clang; 2410e25031STed Kremenek using namespace ento; 2510e25031STed Kremenek 2610e25031STed Kremenek void ExprEngine::CreateCXXTemporaryObject(const MaterializeTemporaryExpr *ME, 2710e25031STed Kremenek ExplodedNode *Pred, 2810e25031STed Kremenek ExplodedNodeSet &Dst) { 29d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 30b0561b33STyker const Expr *tempExpr = ME->getSubExpr()->IgnoreParens(); 3149b1e38eSTed Kremenek ProgramStateRef state = Pred->getState(); 32632e3b7eSTed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 3310e25031STed Kremenek 34861a1740SJordan Rose state = createTemporaryRegionIfNeeded(state, LCtx, tempExpr, ME); 355772f82dSJordan Rose Bldr.generateNode(ME, Pred, state); 3610e25031STed Kremenek } 3710e25031STed Kremenek 383d7b7f52SJordan Rose // FIXME: This is the sort of code that should eventually live in a Core 393d7b7f52SJordan Rose // checker rather than as a special case in ExprEngine. 4088bb563cSJordan Rose void ExprEngine::performTrivialCopy(NodeBuilder &Bldr, ExplodedNode *Pred, 413d7b7f52SJordan Rose const CallEvent &Call) { 423d7b7f52SJordan Rose SVal ThisVal; 433d7b7f52SJordan Rose bool AlwaysReturnsLValue; 444449e7f0SArtem Dergachev const CXXRecordDecl *ThisRD = nullptr; 453d7b7f52SJordan Rose if (const CXXConstructorCall *Ctor = dyn_cast<CXXConstructorCall>(&Call)) { 463d7b7f52SJordan Rose assert(Ctor->getDecl()->isTrivial()); 473d7b7f52SJordan Rose assert(Ctor->getDecl()->isCopyOrMoveConstructor()); 483d7b7f52SJordan Rose ThisVal = Ctor->getCXXThisVal(); 494449e7f0SArtem Dergachev ThisRD = Ctor->getDecl()->getParent(); 503d7b7f52SJordan Rose AlwaysReturnsLValue = false; 513d7b7f52SJordan Rose } else { 523d7b7f52SJordan Rose assert(cast<CXXMethodDecl>(Call.getDecl())->isTrivial()); 533d7b7f52SJordan Rose assert(cast<CXXMethodDecl>(Call.getDecl())->getOverloadedOperator() == 543d7b7f52SJordan Rose OO_Equal); 553d7b7f52SJordan Rose ThisVal = cast<CXXInstanceCall>(Call).getCXXThisVal(); 564449e7f0SArtem Dergachev ThisRD = cast<CXXMethodDecl>(Call.getDecl())->getParent(); 573d7b7f52SJordan Rose AlwaysReturnsLValue = true; 583d7b7f52SJordan Rose } 5988bb563cSJordan Rose 604449e7f0SArtem Dergachev assert(ThisRD); 614449e7f0SArtem Dergachev if (ThisRD->isEmpty()) { 624449e7f0SArtem Dergachev // Do nothing for empty classes. Otherwise it'd retrieve an UnknownVal 634449e7f0SArtem Dergachev // and bind it and RegionStore would think that the actual value 644449e7f0SArtem Dergachev // in this region at this offset is unknown. 654449e7f0SArtem Dergachev return; 664449e7f0SArtem Dergachev } 674449e7f0SArtem Dergachev 6888bb563cSJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 6988bb563cSJordan Rose 7088bb563cSJordan Rose ExplodedNodeSet Dst; 7188bb563cSJordan Rose Bldr.takeNodes(Pred); 7288bb563cSJordan Rose 7388bb563cSJordan Rose SVal V = Call.getArgSVal(0); 7488bb563cSJordan Rose 753d7b7f52SJordan Rose // If the value being copied is not unknown, load from its location to get 763d7b7f52SJordan Rose // an aggregate rvalue. 7705785d16SDavid Blaikie if (Optional<Loc> L = V.getAs<Loc>()) 7888bb563cSJordan Rose V = Pred->getState()->getSVal(*L); 793d7b7f52SJordan Rose else 8075f9d3acSArtem Dergachev assert(V.isUnknownOrUndef()); 8188bb563cSJordan Rose 823d7b7f52SJordan Rose const Expr *CallExpr = Call.getOriginExpr(); 833d7b7f52SJordan Rose evalBind(Dst, CallExpr, Pred, ThisVal, V, true); 8488bb563cSJordan Rose 853d7b7f52SJordan Rose PostStmt PS(CallExpr, LCtx); 8688bb563cSJordan Rose for (ExplodedNodeSet::iterator I = Dst.begin(), E = Dst.end(); 8788bb563cSJordan Rose I != E; ++I) { 8888bb563cSJordan Rose ProgramStateRef State = (*I)->getState(); 893d7b7f52SJordan Rose if (AlwaysReturnsLValue) 903d7b7f52SJordan Rose State = State->BindExpr(CallExpr, LCtx, ThisVal); 913d7b7f52SJordan Rose else 9288bb563cSJordan Rose State = bindReturnValue(Call, LCtx, State); 9388bb563cSJordan Rose Bldr.generateNode(PS, State, *I); 9488bb563cSJordan Rose } 9588bb563cSJordan Rose } 9688bb563cSJordan Rose 97b032e3ffSisuckatcs SVal ExprEngine::makeElementRegion(ProgramStateRef State, SVal LValue, 98b032e3ffSisuckatcs QualType &Ty, bool &IsArray, unsigned Idx) { 99bc74eb1cSJordan Rose SValBuilder &SVB = State->getStateManager().getSValBuilder(); 100bc74eb1cSJordan Rose ASTContext &Ctx = SVB.getContext(); 101bc74eb1cSJordan Rose 102b032e3ffSisuckatcs if (const ArrayType *AT = Ctx.getAsArrayType(Ty)) { 103b032e3ffSisuckatcs while (AT) { 104bc74eb1cSJordan Rose Ty = AT->getElementType(); 105b032e3ffSisuckatcs AT = dyn_cast<ArrayType>(AT->getElementType()); 106b032e3ffSisuckatcs } 107b032e3ffSisuckatcs LValue = State->getLValue(Ty, SVB.makeArrayIndex(Idx), LValue); 108690ab040SArtem Dergachev IsArray = true; 109bc74eb1cSJordan Rose } 110bc74eb1cSJordan Rose 111bc74eb1cSJordan Rose return LValue; 112bc74eb1cSJordan Rose } 113bc74eb1cSJordan Rose 114813734daSAdam Balogh SVal ExprEngine::computeObjectUnderConstruction( 115239452caSArtem Dergachev const Expr *E, ProgramStateRef State, const LocationContext *LCtx, 116b032e3ffSisuckatcs const ConstructionContext *CC, EvalCallOptions &CallOpts, unsigned Idx) { 11717906498SArtem Dergachev SValBuilder &SVB = getSValBuilder(); 11817906498SArtem Dergachev MemRegionManager &MRMgr = SVB.getRegionManager(); 11917906498SArtem Dergachev ASTContext &ACtx = SVB.getContext(); 12005375eb4SJordan Rose 121813734daSAdam Balogh // Compute the target region by exploring the construction context. 122661ab34aSArtem Dergachev if (CC) { 1234068481bSArtem Dergachev switch (CC->getKind()) { 12453b8ce0eSArtem Dergachev case ConstructionContext::CXX17ElidedCopyVariableKind: 1254068481bSArtem Dergachev case ConstructionContext::SimpleVariableKind: { 12653b8ce0eSArtem Dergachev const auto *DSCC = cast<VariableConstructionContext>(CC); 1274068481bSArtem Dergachev const auto *DS = DSCC->getDeclStmt(); 128e231bd34SArtem Dergachev const auto *Var = cast<VarDecl>(DS->getSingleDecl()); 12920edae87SJordan Rose QualType Ty = Var->getType(); 130b032e3ffSisuckatcs return makeElementRegion(State, State->getLValue(Var, LCtx), Ty, 131b032e3ffSisuckatcs CallOpts.IsArrayCtorOrDtor, Idx); 13220edae87SJordan Rose } 13353b8ce0eSArtem Dergachev case ConstructionContext::CXX17ElidedCopyConstructorInitializerKind: 134922455feSArtem Dergachev case ConstructionContext::SimpleConstructorInitializerKind: { 1354068481bSArtem Dergachev const auto *ICC = cast<ConstructorInitializerConstructionContext>(CC); 1364068481bSArtem Dergachev const auto *Init = ICC->getCXXCtorInitializer(); 13754529a34SJordan Rose const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl()); 138813734daSAdam Balogh Loc ThisPtr = SVB.getCXXThis(CurCtor, LCtx->getStackFrame()); 13954529a34SJordan Rose SVal ThisVal = State->getSVal(ThisPtr); 140facad21bSAdam Balogh if (Init->isBaseInitializer()) { 141facad21bSAdam Balogh const auto *ThisReg = cast<SubRegion>(ThisVal.getAsRegion()); 142facad21bSAdam Balogh const CXXRecordDecl *BaseClass = 143facad21bSAdam Balogh Init->getBaseClass()->getAsCXXRecordDecl(); 144facad21bSAdam Balogh const auto *BaseReg = 145facad21bSAdam Balogh MRMgr.getCXXBaseObjectRegion(BaseClass, ThisReg, 146facad21bSAdam Balogh Init->isBaseVirtual()); 147facad21bSAdam Balogh return SVB.makeLoc(BaseReg); 148facad21bSAdam Balogh } 149facad21bSAdam Balogh if (Init->isDelegatingInitializer()) 150facad21bSAdam Balogh return ThisVal; 15154529a34SJordan Rose 152bc74eb1cSJordan Rose const ValueDecl *Field; 153bc74eb1cSJordan Rose SVal FieldVal; 15454529a34SJordan Rose if (Init->isIndirectMemberInitializer()) { 155bc74eb1cSJordan Rose Field = Init->getIndirectMember(); 156bc74eb1cSJordan Rose FieldVal = State->getLValue(Init->getIndirectMember(), ThisVal); 15754529a34SJordan Rose } else { 158bc74eb1cSJordan Rose Field = Init->getMember(); 159bc74eb1cSJordan Rose FieldVal = State->getLValue(Init->getMember(), ThisVal); 16054529a34SJordan Rose } 161bc74eb1cSJordan Rose 162bc74eb1cSJordan Rose QualType Ty = Field->getType(); 163b032e3ffSisuckatcs return makeElementRegion(State, FieldVal, Ty, CallOpts.IsArrayCtorOrDtor, 164b032e3ffSisuckatcs Idx); 16554529a34SJordan Rose } 1664068481bSArtem Dergachev case ConstructionContext::NewAllocatedObjectKind: { 167549f9cd4SKristof Umann if (AMgr.getAnalyzerOptions().MayInlineCXXAllocator) { 1684068481bSArtem Dergachev const auto *NECC = cast<NewAllocatedObjectConstructionContext>(CC); 1694068481bSArtem Dergachev const auto *NE = NECC->getCXXNewExpr(); 170f83d5479SArtem Dergachev SVal V = *getObjectUnderConstruction(State, NE, LCtx); 171f83d5479SArtem Dergachev if (const SubRegion *MR = 172f83d5479SArtem Dergachev dyn_cast_or_null<SubRegion>(V.getAsRegion())) { 1734068481bSArtem Dergachev if (NE->isArray()) { 1744068481bSArtem Dergachev // TODO: In fact, we need to call the constructor for every 1754068481bSArtem Dergachev // allocated element, not just the first one! 1764068481bSArtem Dergachev CallOpts.IsArrayCtorOrDtor = true; 177b032e3ffSisuckatcs 178b032e3ffSisuckatcs auto R = MRMgr.getElementRegion(NE->getType()->getPointeeType(), 179b032e3ffSisuckatcs svalBuilder.makeArrayIndex(Idx), MR, 180b032e3ffSisuckatcs SVB.getContext()); 181b032e3ffSisuckatcs 182b032e3ffSisuckatcs return loc::MemRegionVal(R); 1834068481bSArtem Dergachev } 184813734daSAdam Balogh return V; 1854068481bSArtem Dergachev } 186f83d5479SArtem Dergachev // TODO: Detect when the allocator returns a null pointer. 187f83d5479SArtem Dergachev // Constructor shall not be called in this case. 1884068481bSArtem Dergachev } 1894068481bSArtem Dergachev break; 1904068481bSArtem Dergachev } 191f28d7f17SArtem Dergachev case ConstructionContext::SimpleReturnedValueKind: 192f28d7f17SArtem Dergachev case ConstructionContext::CXX17ElidedCopyReturnedValueKind: { 193239452caSArtem Dergachev // The temporary is to be managed by the parent stack frame. 194239452caSArtem Dergachev // So build it in the parent stack frame if we're not in the 195239452caSArtem Dergachev // top frame of the analysis. 196dd18b11bSGeorge Karpenkov const StackFrameContext *SFC = LCtx->getStackFrame(); 197239452caSArtem Dergachev if (const LocationContext *CallerLCtx = SFC->getParent()) { 198239452caSArtem Dergachev auto RTC = (*SFC->getCallSiteBlock())[SFC->getIndex()] 199239452caSArtem Dergachev .getAs<CFGCXXRecordTypedCall>(); 200239452caSArtem Dergachev if (!RTC) { 201239452caSArtem Dergachev // We were unable to find the correct construction context for the 202239452caSArtem Dergachev // call in the parent stack frame. This is equivalent to not being 203239452caSArtem Dergachev // able to find construction context at all. 204239452caSArtem Dergachev break; 205239452caSArtem Dergachev } 206b3fc9df4SArtem Dergachev if (isa<BlockInvocationContext>(CallerLCtx)) { 207b3fc9df4SArtem Dergachev // Unwrap block invocation contexts. They're mostly part of 208b3fc9df4SArtem Dergachev // the current stack frame. 209b3fc9df4SArtem Dergachev CallerLCtx = CallerLCtx->getParent(); 210b3fc9df4SArtem Dergachev assert(!isa<BlockInvocationContext>(CallerLCtx)); 211b3fc9df4SArtem Dergachev } 212813734daSAdam Balogh return computeObjectUnderConstruction( 213f28d7f17SArtem Dergachev cast<Expr>(SFC->getCallSite()), State, CallerLCtx, 214f28d7f17SArtem Dergachev RTC->getConstructionContext(), CallOpts); 215239452caSArtem Dergachev } else { 21617906498SArtem Dergachev // We are on the top frame of the analysis. We do not know where is the 21717906498SArtem Dergachev // object returned to. Conjure a symbolic region for the return value. 21817906498SArtem Dergachev // TODO: We probably need a new MemRegion kind to represent the storage 21917906498SArtem Dergachev // of that SymbolicRegion, so that we cound produce a fancy symbol 22017906498SArtem Dergachev // instead of an anonymous conjured symbol. 22117906498SArtem Dergachev // TODO: Do we need to track the region to avoid having it dead 22217906498SArtem Dergachev // too early? It does die too early, at least in C++17, but because 22317906498SArtem Dergachev // putting anything into a SymbolicRegion causes an immediate escape, 22417906498SArtem Dergachev // it doesn't cause any leak false positives. 22517906498SArtem Dergachev const auto *RCC = cast<ReturnedValueConstructionContext>(CC); 22617906498SArtem Dergachev // Make sure that this doesn't coincide with any other symbol 22717906498SArtem Dergachev // conjured for the returned expression. 22817906498SArtem Dergachev static const int TopLevelSymRegionTag = 0; 22917906498SArtem Dergachev const Expr *RetE = RCC->getReturnStmt()->getRetValue(); 23017906498SArtem Dergachev assert(RetE && "Void returns should not have a construction context"); 23117906498SArtem Dergachev QualType ReturnTy = RetE->getType(); 23217906498SArtem Dergachev QualType RegionTy = ACtx.getPointerType(ReturnTy); 233813734daSAdam Balogh return SVB.conjureSymbolVal(&TopLevelSymRegionTag, RetE, SFC, RegionTy, 234813734daSAdam Balogh currBldrCtx->blockCount()); 235239452caSArtem Dergachev } 236f28d7f17SArtem Dergachev llvm_unreachable("Unhandled return value construction context!"); 237239452caSArtem Dergachev } 2389a209ad1SArtem Dergachev case ConstructionContext::ElidedTemporaryObjectKind: { 239549f9cd4SKristof Umann assert(AMgr.getAnalyzerOptions().ShouldElideConstructors); 2409a209ad1SArtem Dergachev const auto *TCC = cast<ElidedTemporaryObjectConstructionContext>(CC); 2419a209ad1SArtem Dergachev 2429a209ad1SArtem Dergachev // Support pre-C++17 copy elision. We'll have the elidable copy 2439a209ad1SArtem Dergachev // constructor in the AST and in the CFG, but we'll skip it 2449a209ad1SArtem Dergachev // and construct directly into the final object. This call 2459a209ad1SArtem Dergachev // also sets the CallOpts flags for us. 246a657a32cSArtem Dergachev // If the elided copy/move constructor is not supported, there's still 247a657a32cSArtem Dergachev // benefit in trying to model the non-elided constructor. 248a657a32cSArtem Dergachev // Stash our state before trying to elide, as it'll get overwritten. 249a657a32cSArtem Dergachev ProgramStateRef PreElideState = State; 250a657a32cSArtem Dergachev EvalCallOptions PreElideCallOpts = CallOpts; 251a657a32cSArtem Dergachev 252813734daSAdam Balogh SVal V = computeObjectUnderConstruction( 253813734daSAdam Balogh TCC->getConstructorAfterElision(), State, LCtx, 254813734daSAdam Balogh TCC->getConstructionContextAfterElision(), CallOpts); 2559a209ad1SArtem Dergachev 256a657a32cSArtem Dergachev // FIXME: This definition of "copy elision has not failed" is unreliable. 257a657a32cSArtem Dergachev // It doesn't indicate that the constructor will actually be inlined 258813734daSAdam Balogh // later; this is still up to evalCall() to decide. 259813734daSAdam Balogh if (!CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion) 260813734daSAdam Balogh return V; 2619a209ad1SArtem Dergachev 262a657a32cSArtem Dergachev // Copy elision failed. Revert the changes and proceed as if we have 263a657a32cSArtem Dergachev // a simple temporary. 264a657a32cSArtem Dergachev CallOpts = PreElideCallOpts; 265813734daSAdam Balogh CallOpts.IsElidableCtorThatHasNotBeenElided = true; 266080b8709SSimon Pilgrim LLVM_FALLTHROUGH; 2679a209ad1SArtem Dergachev } 268ff267df0SArtem Dergachev case ConstructionContext::SimpleTemporaryObjectKind: { 269a657a32cSArtem Dergachev const auto *TCC = cast<TemporaryObjectConstructionContext>(CC); 270239452caSArtem Dergachev const MaterializeTemporaryExpr *MTE = TCC->getMaterializedTemporaryExpr(); 271239452caSArtem Dergachev 272813734daSAdam Balogh CallOpts.IsTemporaryCtorOrDtor = true; 273239452caSArtem Dergachev if (MTE) { 2748cd7961aSArtem Dergachev if (const ValueDecl *VD = MTE->getExtendingDecl()) { 275643102dfSArtem Dergachev assert(MTE->getStorageDuration() != SD_FullExpression); 276643102dfSArtem Dergachev if (!VD->getType()->isReferenceType()) { 277643102dfSArtem Dergachev // We're lifetime-extended by a surrounding aggregate. 278643102dfSArtem Dergachev // Automatic destructors aren't quite working in this case 279643102dfSArtem Dergachev // on the CFG side. We should warn the caller about that. 280310bca01SArtem Dergachev // FIXME: Is there a better way to retrieve this information from 281310bca01SArtem Dergachev // the MaterializeTemporaryExpr? 282310bca01SArtem Dergachev CallOpts.IsTemporaryLifetimeExtendedViaAggregate = true; 283310bca01SArtem Dergachev } 2848cd7961aSArtem Dergachev } 2854fb586c3SArtem Dergachev 2864fb586c3SArtem Dergachev if (MTE->getStorageDuration() == SD_Static || 2874fb586c3SArtem Dergachev MTE->getStorageDuration() == SD_Thread) 288813734daSAdam Balogh return loc::MemRegionVal(MRMgr.getCXXStaticTempObjectRegion(E)); 289239452caSArtem Dergachev } 290239452caSArtem Dergachev 291813734daSAdam Balogh return loc::MemRegionVal(MRMgr.getCXXTempObjectRegion(E, LCtx)); 2924068481bSArtem Dergachev } 293*996b092cSisuckatcs case ConstructionContext::LambdaCaptureKind: { 294*996b092cSisuckatcs CallOpts.IsTemporaryCtorOrDtor = true; 295*996b092cSisuckatcs 296*996b092cSisuckatcs const auto *LCC = cast<LambdaCaptureConstructionContext>(CC); 297*996b092cSisuckatcs 298*996b092cSisuckatcs SVal Base = loc::MemRegionVal( 299*996b092cSisuckatcs MRMgr.getCXXTempObjectRegion(LCC->getInitializer(), LCtx)); 300*996b092cSisuckatcs 301*996b092cSisuckatcs const auto *CE = dyn_cast_or_null<CXXConstructExpr>(E); 302*996b092cSisuckatcs if (getIndexOfElementToConstruct(State, CE, LCtx)) { 303*996b092cSisuckatcs CallOpts.IsArrayCtorOrDtor = true; 304*996b092cSisuckatcs Base = State->getLValue(E->getType(), svalBuilder.makeArrayIndex(Idx), 305*996b092cSisuckatcs Base); 306*996b092cSisuckatcs } 307*996b092cSisuckatcs 308*996b092cSisuckatcs return Base; 309*996b092cSisuckatcs } 310a657a32cSArtem Dergachev case ConstructionContext::ArgumentKind: { 3113ccf14ebSArtem Dergachev // Arguments are technically temporaries. 3123ccf14ebSArtem Dergachev CallOpts.IsTemporaryCtorOrDtor = true; 3133ccf14ebSArtem Dergachev 3143ccf14ebSArtem Dergachev const auto *ACC = cast<ArgumentConstructionContext>(CC); 3153ccf14ebSArtem Dergachev const Expr *E = ACC->getCallLikeExpr(); 3163ccf14ebSArtem Dergachev unsigned Idx = ACC->getIndex(); 3173ccf14ebSArtem Dergachev 3183ccf14ebSArtem Dergachev CallEventManager &CEMgr = getStateManager().getCallEventManager(); 3193ccf14ebSArtem Dergachev auto getArgLoc = [&](CallEventRef<> Caller) -> Optional<SVal> { 3207740c6d6SCsaba Dabis const LocationContext *FutureSFC = 3217740c6d6SCsaba Dabis Caller->getCalleeStackFrame(currBldrCtx->blockCount()); 3223ccf14ebSArtem Dergachev // Return early if we are unable to reliably foresee 3233ccf14ebSArtem Dergachev // the future stack frame. 3243ccf14ebSArtem Dergachev if (!FutureSFC) 3253ccf14ebSArtem Dergachev return None; 3263ccf14ebSArtem Dergachev 3273ccf14ebSArtem Dergachev // This should be equivalent to Caller->getDecl() for now, but 3283ccf14ebSArtem Dergachev // FutureSFC->getDecl() is likely to support better stuff (like 3293ccf14ebSArtem Dergachev // virtual functions) earlier. 3303ccf14ebSArtem Dergachev const Decl *CalleeD = FutureSFC->getDecl(); 3313ccf14ebSArtem Dergachev 3323ccf14ebSArtem Dergachev // FIXME: Support for variadic arguments is not implemented here yet. 3333ccf14ebSArtem Dergachev if (CallEvent::isVariadic(CalleeD)) 3343ccf14ebSArtem Dergachev return None; 3353ccf14ebSArtem Dergachev 3363ccf14ebSArtem Dergachev // Operator arguments do not correspond to operator parameters 3373ccf14ebSArtem Dergachev // because this-argument is implemented as a normal argument in 3383ccf14ebSArtem Dergachev // operator call expressions but not in operator declarations. 33998db1f99SAdam Balogh const TypedValueRegion *TVR = Caller->getParameterLocation( 3407740c6d6SCsaba Dabis *Caller->getAdjustedParameterIndex(Idx), currBldrCtx->blockCount()); 34198db1f99SAdam Balogh if (!TVR) 3423ccf14ebSArtem Dergachev return None; 3433ccf14ebSArtem Dergachev 34498db1f99SAdam Balogh return loc::MemRegionVal(TVR); 3453ccf14ebSArtem Dergachev }; 3463ccf14ebSArtem Dergachev 3473ccf14ebSArtem Dergachev if (const auto *CE = dyn_cast<CallExpr>(E)) { 3483ccf14ebSArtem Dergachev CallEventRef<> Caller = CEMgr.getSimpleCall(CE, State, LCtx); 349813734daSAdam Balogh if (Optional<SVal> V = getArgLoc(Caller)) 350813734daSAdam Balogh return *V; 3513ccf14ebSArtem Dergachev else 352a657a32cSArtem Dergachev break; 3533ccf14ebSArtem Dergachev } else if (const auto *CCE = dyn_cast<CXXConstructExpr>(E)) { 3543ccf14ebSArtem Dergachev // Don't bother figuring out the target region for the future 3553ccf14ebSArtem Dergachev // constructor because we won't need it. 3563ccf14ebSArtem Dergachev CallEventRef<> Caller = 3573ccf14ebSArtem Dergachev CEMgr.getCXXConstructorCall(CCE, /*Target=*/nullptr, State, LCtx); 358813734daSAdam Balogh if (Optional<SVal> V = getArgLoc(Caller)) 359813734daSAdam Balogh return *V; 3603ccf14ebSArtem Dergachev else 3613ccf14ebSArtem Dergachev break; 3623ccf14ebSArtem Dergachev } else if (const auto *ME = dyn_cast<ObjCMessageExpr>(E)) { 3633ccf14ebSArtem Dergachev CallEventRef<> Caller = CEMgr.getObjCMethodCall(ME, State, LCtx); 364813734daSAdam Balogh if (Optional<SVal> V = getArgLoc(Caller)) 365813734daSAdam Balogh return *V; 3663ccf14ebSArtem Dergachev else 3673ccf14ebSArtem Dergachev break; 368813734daSAdam Balogh } 369813734daSAdam Balogh } 370813734daSAdam Balogh } // switch (CC->getKind()) 3713ccf14ebSArtem Dergachev } 3723ccf14ebSArtem Dergachev 373c9318304SJordan Rose // If we couldn't find an existing region to construct into, assume we're 374690ab040SArtem Dergachev // constructing a temporary. Notify the caller of our failure. 375168e29f6SArtem Dergachev CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion = true; 376813734daSAdam Balogh return loc::MemRegionVal(MRMgr.getCXXTempObjectRegion(E, LCtx)); 377813734daSAdam Balogh } 378813734daSAdam Balogh 379813734daSAdam Balogh ProgramStateRef ExprEngine::updateObjectsUnderConstruction( 380813734daSAdam Balogh SVal V, const Expr *E, ProgramStateRef State, const LocationContext *LCtx, 381813734daSAdam Balogh const ConstructionContext *CC, const EvalCallOptions &CallOpts) { 382813734daSAdam Balogh if (CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion) { 383813734daSAdam Balogh // Sounds like we failed to find the target region and therefore 384813734daSAdam Balogh // copy elision failed. There's nothing we can do about it here. 385813734daSAdam Balogh return State; 386813734daSAdam Balogh } 387813734daSAdam Balogh 388813734daSAdam Balogh // See if we're constructing an existing region by looking at the 389813734daSAdam Balogh // current construction context. 390813734daSAdam Balogh assert(CC && "Computed target region without construction context?"); 391813734daSAdam Balogh switch (CC->getKind()) { 392813734daSAdam Balogh case ConstructionContext::CXX17ElidedCopyVariableKind: 393813734daSAdam Balogh case ConstructionContext::SimpleVariableKind: { 394813734daSAdam Balogh const auto *DSCC = cast<VariableConstructionContext>(CC); 395813734daSAdam Balogh return addObjectUnderConstruction(State, DSCC->getDeclStmt(), LCtx, V); 396813734daSAdam Balogh } 397813734daSAdam Balogh case ConstructionContext::CXX17ElidedCopyConstructorInitializerKind: 398813734daSAdam Balogh case ConstructionContext::SimpleConstructorInitializerKind: { 399813734daSAdam Balogh const auto *ICC = cast<ConstructorInitializerConstructionContext>(CC); 400facad21bSAdam Balogh const auto *Init = ICC->getCXXCtorInitializer(); 401facad21bSAdam Balogh // Base and delegating initializers handled above 402facad21bSAdam Balogh assert(Init->isAnyMemberInitializer() && 403facad21bSAdam Balogh "Base and delegating initializers should have been handled by" 404facad21bSAdam Balogh "computeObjectUnderConstruction()"); 4056a1bca87SBenjamin Kramer return addObjectUnderConstruction(State, Init, LCtx, V); 406813734daSAdam Balogh } 407813734daSAdam Balogh case ConstructionContext::NewAllocatedObjectKind: { 408813734daSAdam Balogh return State; 409813734daSAdam Balogh } 410813734daSAdam Balogh case ConstructionContext::SimpleReturnedValueKind: 411813734daSAdam Balogh case ConstructionContext::CXX17ElidedCopyReturnedValueKind: { 412813734daSAdam Balogh const StackFrameContext *SFC = LCtx->getStackFrame(); 413813734daSAdam Balogh const LocationContext *CallerLCtx = SFC->getParent(); 414813734daSAdam Balogh if (!CallerLCtx) { 415813734daSAdam Balogh // No extra work is necessary in top frame. 416813734daSAdam Balogh return State; 417813734daSAdam Balogh } 418813734daSAdam Balogh 419813734daSAdam Balogh auto RTC = (*SFC->getCallSiteBlock())[SFC->getIndex()] 420813734daSAdam Balogh .getAs<CFGCXXRecordTypedCall>(); 421813734daSAdam Balogh assert(RTC && "Could not have had a target region without it"); 422813734daSAdam Balogh if (isa<BlockInvocationContext>(CallerLCtx)) { 423813734daSAdam Balogh // Unwrap block invocation contexts. They're mostly part of 424813734daSAdam Balogh // the current stack frame. 425813734daSAdam Balogh CallerLCtx = CallerLCtx->getParent(); 426813734daSAdam Balogh assert(!isa<BlockInvocationContext>(CallerLCtx)); 427813734daSAdam Balogh } 428813734daSAdam Balogh 429813734daSAdam Balogh return updateObjectsUnderConstruction(V, 430813734daSAdam Balogh cast<Expr>(SFC->getCallSite()), State, CallerLCtx, 431813734daSAdam Balogh RTC->getConstructionContext(), CallOpts); 432813734daSAdam Balogh } 433813734daSAdam Balogh case ConstructionContext::ElidedTemporaryObjectKind: { 434813734daSAdam Balogh assert(AMgr.getAnalyzerOptions().ShouldElideConstructors); 435813734daSAdam Balogh if (!CallOpts.IsElidableCtorThatHasNotBeenElided) { 436813734daSAdam Balogh const auto *TCC = cast<ElidedTemporaryObjectConstructionContext>(CC); 437813734daSAdam Balogh State = updateObjectsUnderConstruction( 438813734daSAdam Balogh V, TCC->getConstructorAfterElision(), State, LCtx, 439813734daSAdam Balogh TCC->getConstructionContextAfterElision(), CallOpts); 440813734daSAdam Balogh 441813734daSAdam Balogh // Remember that we've elided the constructor. 442813734daSAdam Balogh State = addObjectUnderConstruction( 443813734daSAdam Balogh State, TCC->getConstructorAfterElision(), LCtx, V); 444813734daSAdam Balogh 445813734daSAdam Balogh // Remember that we've elided the destructor. 446813734daSAdam Balogh if (const auto *BTE = TCC->getCXXBindTemporaryExpr()) 447813734daSAdam Balogh State = elideDestructor(State, BTE, LCtx); 448813734daSAdam Balogh 449813734daSAdam Balogh // Instead of materialization, shamelessly return 450813734daSAdam Balogh // the final object destination. 451813734daSAdam Balogh if (const auto *MTE = TCC->getMaterializedTemporaryExpr()) 452813734daSAdam Balogh State = addObjectUnderConstruction(State, MTE, LCtx, V); 453813734daSAdam Balogh 454813734daSAdam Balogh return State; 455813734daSAdam Balogh } 456813734daSAdam Balogh // If we decided not to elide the constructor, proceed as if 457813734daSAdam Balogh // it's a simple temporary. 458813734daSAdam Balogh LLVM_FALLTHROUGH; 459813734daSAdam Balogh } 460813734daSAdam Balogh case ConstructionContext::SimpleTemporaryObjectKind: { 461813734daSAdam Balogh const auto *TCC = cast<TemporaryObjectConstructionContext>(CC); 462813734daSAdam Balogh if (const auto *BTE = TCC->getCXXBindTemporaryExpr()) 463813734daSAdam Balogh State = addObjectUnderConstruction(State, BTE, LCtx, V); 464813734daSAdam Balogh 465813734daSAdam Balogh if (const auto *MTE = TCC->getMaterializedTemporaryExpr()) 466813734daSAdam Balogh State = addObjectUnderConstruction(State, MTE, LCtx, V); 467813734daSAdam Balogh 468813734daSAdam Balogh return State; 469813734daSAdam Balogh } 470*996b092cSisuckatcs case ConstructionContext::LambdaCaptureKind: { 471*996b092cSisuckatcs const auto *LCC = cast<LambdaCaptureConstructionContext>(CC); 472*996b092cSisuckatcs 473*996b092cSisuckatcs // If we capture and array, we want to store the super region, not a 474*996b092cSisuckatcs // sub-region. 475*996b092cSisuckatcs if (const auto *EL = dyn_cast_or_null<ElementRegion>(V.getAsRegion())) 476*996b092cSisuckatcs V = loc::MemRegionVal(EL->getSuperRegion()); 477*996b092cSisuckatcs 478*996b092cSisuckatcs return addObjectUnderConstruction( 479*996b092cSisuckatcs State, {LCC->getLambdaExpr(), LCC->getIndex()}, LCtx, V); 480*996b092cSisuckatcs } 481813734daSAdam Balogh case ConstructionContext::ArgumentKind: { 482813734daSAdam Balogh const auto *ACC = cast<ArgumentConstructionContext>(CC); 483813734daSAdam Balogh if (const auto *BTE = ACC->getCXXBindTemporaryExpr()) 484813734daSAdam Balogh State = addObjectUnderConstruction(State, BTE, LCtx, V); 485813734daSAdam Balogh 486813734daSAdam Balogh return addObjectUnderConstruction( 487813734daSAdam Balogh State, {ACC->getCallLikeExpr(), ACC->getIndex()}, LCtx, V); 488813734daSAdam Balogh } 489813734daSAdam Balogh } 490813734daSAdam Balogh llvm_unreachable("Unhandled construction context!"); 491c9318304SJordan Rose } 49205375eb4SJordan Rose 4938a13326dSisuckatcs static ProgramStateRef 4948a13326dSisuckatcs bindRequiredArrayElementToEnvironment(ProgramStateRef State, 4958a13326dSisuckatcs const ArrayInitLoopExpr *AILE, 4968a13326dSisuckatcs const LocationContext *LCtx, SVal Idx) { 4978a13326dSisuckatcs // The ctor in this case is guaranteed to be a copy ctor, otherwise we hit a 4988a13326dSisuckatcs // compile time error. 4998a13326dSisuckatcs // 5008a13326dSisuckatcs // -ArrayInitLoopExpr <-- we're here 5018a13326dSisuckatcs // |-OpaqueValueExpr 5028a13326dSisuckatcs // | `-DeclRefExpr <-- match this 5038a13326dSisuckatcs // `-CXXConstructExpr 5048a13326dSisuckatcs // `-ImplicitCastExpr 5058a13326dSisuckatcs // `-ArraySubscriptExpr 5068a13326dSisuckatcs // |-ImplicitCastExpr 5078a13326dSisuckatcs // | `-OpaqueValueExpr 5088a13326dSisuckatcs // | `-DeclRefExpr 5098a13326dSisuckatcs // `-ArrayInitIndexExpr 5108a13326dSisuckatcs // 5118a13326dSisuckatcs // The resulting expression might look like the one below in an implicit 5128a13326dSisuckatcs // copy/move ctor. 5138a13326dSisuckatcs // 5148a13326dSisuckatcs // ArrayInitLoopExpr <-- we're here 5158a13326dSisuckatcs // |-OpaqueValueExpr 5168a13326dSisuckatcs // | `-MemberExpr <-- match this 5178a13326dSisuckatcs // | (`-CXXStaticCastExpr) <-- move ctor only 5188a13326dSisuckatcs // | `-DeclRefExpr 5198a13326dSisuckatcs // `-CXXConstructExpr 5208a13326dSisuckatcs // `-ArraySubscriptExpr 5218a13326dSisuckatcs // |-ImplicitCastExpr 5228a13326dSisuckatcs // | `-OpaqueValueExpr 5238a13326dSisuckatcs // | `-MemberExpr 5248a13326dSisuckatcs // | `-DeclRefExpr 5258a13326dSisuckatcs // `-ArrayInitIndexExpr 5268a13326dSisuckatcs // 5278a13326dSisuckatcs // HACK: There is no way we can put the index of the array element into the 5288a13326dSisuckatcs // CFG unless we unroll the loop, so we manually select and bind the required 5298a13326dSisuckatcs // parameter to the environment. 5308a13326dSisuckatcs const auto *CE = cast<CXXConstructExpr>(AILE->getSubExpr()); 5318a13326dSisuckatcs const auto *OVESrc = AILE->getCommonExpr()->getSourceExpr(); 5328a13326dSisuckatcs 5338a13326dSisuckatcs SVal Base = UnknownVal(); 5348a13326dSisuckatcs if (const auto *ME = dyn_cast<MemberExpr>(OVESrc)) 5358a13326dSisuckatcs Base = State->getSVal(ME, LCtx); 5368a13326dSisuckatcs else if (const auto *DRE = cast<DeclRefExpr>(OVESrc)) 5378a13326dSisuckatcs Base = State->getLValue(cast<VarDecl>(DRE->getDecl()), LCtx); 5388a13326dSisuckatcs else 5398a13326dSisuckatcs llvm_unreachable("ArrayInitLoopExpr contains unexpected source expression"); 5408a13326dSisuckatcs 5418a13326dSisuckatcs SVal NthElem = State->getLValue(CE->getType(), Idx, Base); 5428a13326dSisuckatcs 5438a13326dSisuckatcs return State->BindExpr(CE->getArg(0), LCtx, NthElem); 5448a13326dSisuckatcs } 5458a13326dSisuckatcs 546a82ffe9dSArtem Dergachev void ExprEngine::handleConstructor(const Expr *E, 54717f41608SJordan Rose ExplodedNode *Pred, 54817f41608SJordan Rose ExplodedNodeSet &destNodes) { 549a82ffe9dSArtem Dergachev const auto *CE = dyn_cast<CXXConstructExpr>(E); 550a82ffe9dSArtem Dergachev const auto *CIE = dyn_cast<CXXInheritedCtorInitExpr>(E); 551a82ffe9dSArtem Dergachev assert(CE || CIE); 552a82ffe9dSArtem Dergachev 55317f41608SJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 55417f41608SJordan Rose ProgramStateRef State = Pred->getState(); 55517f41608SJordan Rose 556f83d5479SArtem Dergachev SVal Target = UnknownVal(); 55717f41608SJordan Rose 558a82ffe9dSArtem Dergachev if (CE) { 5599a209ad1SArtem Dergachev if (Optional<SVal> ElidedTarget = 5609a209ad1SArtem Dergachev getObjectUnderConstruction(State, CE, LCtx)) { 561a82ffe9dSArtem Dergachev // We've previously modeled an elidable constructor by pretending that it 562a82ffe9dSArtem Dergachev // in fact constructs into the correct target. This constructor can 563a82ffe9dSArtem Dergachev // therefore be skipped. 5649a209ad1SArtem Dergachev Target = *ElidedTarget; 5659a209ad1SArtem Dergachev StmtNodeBuilder Bldr(Pred, destNodes, *currBldrCtx); 5669a209ad1SArtem Dergachev State = finishObjectConstruction(State, CE, LCtx); 5679a209ad1SArtem Dergachev if (auto L = Target.getAs<Loc>()) 5689a209ad1SArtem Dergachev State = State->BindExpr(CE, LCtx, State->getSVal(*L, CE->getType())); 5699a209ad1SArtem Dergachev Bldr.generateNode(CE, Pred, State); 5709a209ad1SArtem Dergachev return; 5719a209ad1SArtem Dergachev } 572a82ffe9dSArtem Dergachev } 5739a209ad1SArtem Dergachev 574690ab040SArtem Dergachev EvalCallOptions CallOpts; 575661ab34aSArtem Dergachev auto C = getCurrentCFGElement().getAs<CFGConstructor>(); 5764068481bSArtem Dergachev assert(C || getCurrentCFGElement().getAs<CFGStmt>()); 577661ab34aSArtem Dergachev const ConstructionContext *CC = C ? C->getConstructionContext() : nullptr; 578661ab34aSArtem Dergachev 579a82ffe9dSArtem Dergachev const CXXConstructExpr::ConstructionKind CK = 580a82ffe9dSArtem Dergachev CE ? CE->getConstructionKind() : CIE->getConstructionKind(); 581a82ffe9dSArtem Dergachev switch (CK) { 58217f41608SJordan Rose case CXXConstructExpr::CK_Complete: { 583a82ffe9dSArtem Dergachev // Inherited constructors are always base class constructors. 584a82ffe9dSArtem Dergachev assert(CE && !CIE && "A complete constructor is inherited?!"); 585a82ffe9dSArtem Dergachev 5868a13326dSisuckatcs // If the ctor is part of an ArrayInitLoopExpr, we want to handle it 5878a13326dSisuckatcs // differently. 5888a13326dSisuckatcs auto *AILE = CC ? CC->getArrayInitLoop() : nullptr; 5898a13326dSisuckatcs 590b032e3ffSisuckatcs unsigned Idx = 0; 5918a13326dSisuckatcs if (CE->getType()->isArrayType() || AILE) { 5928a13326dSisuckatcs Idx = getIndexOfElementToConstruct(State, CE, LCtx).getValueOr(0u); 593b032e3ffSisuckatcs State = setIndexOfElementToConstruct(State, CE, LCtx, Idx + 1); 594b032e3ffSisuckatcs } 595b032e3ffSisuckatcs 5968a13326dSisuckatcs if (AILE) { 5978a13326dSisuckatcs // Only set this once even though we loop through it multiple times. 5988a13326dSisuckatcs if (!getPendingInitLoop(State, CE, LCtx)) 5998a13326dSisuckatcs State = setPendingInitLoop(State, CE, LCtx, 6008a13326dSisuckatcs AILE->getArraySize().getLimitedValue()); 6018a13326dSisuckatcs 6028a13326dSisuckatcs State = bindRequiredArrayElementToEnvironment( 6038a13326dSisuckatcs State, AILE, LCtx, svalBuilder.makeArrayIndex(Idx)); 6048a13326dSisuckatcs } 6058a13326dSisuckatcs 606a82ffe9dSArtem Dergachev // The target region is found from construction context. 607239452caSArtem Dergachev std::tie(State, Target) = 608b032e3ffSisuckatcs handleConstructionContext(CE, State, LCtx, CC, CallOpts, Idx); 609b970505dSJordan Rose break; 610b970505dSJordan Rose } 611192a7474SArtem Dergachev case CXXConstructExpr::CK_VirtualBase: { 612b3b976f0SJordan Rose // Make sure we are not calling virtual base class initializers twice. 613b3b976f0SJordan Rose // Only the most-derived object should initialize virtual base classes. 614192a7474SArtem Dergachev const auto *OuterCtor = dyn_cast_or_null<CXXConstructExpr>( 615192a7474SArtem Dergachev LCtx->getStackFrame()->getCallSite()); 616192a7474SArtem Dergachev assert( 617192a7474SArtem Dergachev (!OuterCtor || 618192a7474SArtem Dergachev OuterCtor->getConstructionKind() == CXXConstructExpr::CK_Complete || 619192a7474SArtem Dergachev OuterCtor->getConstructionKind() == CXXConstructExpr::CK_Delegating) && 620192a7474SArtem Dergachev ("This virtual base should have already been initialized by " 621192a7474SArtem Dergachev "the most derived class!")); 622192a7474SArtem Dergachev (void)OuterCtor; 6234dc0b1acSReid Kleckner LLVM_FALLTHROUGH; 624192a7474SArtem Dergachev } 625b3b976f0SJordan Rose case CXXConstructExpr::CK_NonVirtualBase: 626e8ba3ec4SArtem Dergachev // In C++17, classes with non-virtual bases may be aggregates, so they would 627e8ba3ec4SArtem Dergachev // be initialized as aggregates without a constructor call, so we may have 628e8ba3ec4SArtem Dergachev // a base class constructed directly into an initializer list without 629e8ba3ec4SArtem Dergachev // having the derived-class constructor call on the previous stack frame. 630e8ba3ec4SArtem Dergachev // Initializer lists may be nested into more initializer lists that 631e8ba3ec4SArtem Dergachev // correspond to surrounding aggregate initializations. 632e8ba3ec4SArtem Dergachev // FIXME: For now this code essentially bails out. We need to find the 633e8ba3ec4SArtem Dergachev // correct target region and set it. 634e8ba3ec4SArtem Dergachev // FIXME: Instead of relying on the ParentMap, we should have the 635e8ba3ec4SArtem Dergachev // trigger-statement (InitListExpr in this case) passed down from CFG or 636e8ba3ec4SArtem Dergachev // otherwise always available during construction. 637d0ac215dSKazu Hirata if (isa_and_nonnull<InitListExpr>(LCtx->getParentMap().getParent(E))) { 638e8ba3ec4SArtem Dergachev MemRegionManager &MRMgr = getSValBuilder().getRegionManager(); 639a82ffe9dSArtem Dergachev Target = loc::MemRegionVal(MRMgr.getCXXTempObjectRegion(E, LCtx)); 640168e29f6SArtem Dergachev CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion = true; 641e8ba3ec4SArtem Dergachev break; 642e8ba3ec4SArtem Dergachev } 6434dc0b1acSReid Kleckner LLVM_FALLTHROUGH; 644b970505dSJordan Rose case CXXConstructExpr::CK_Delegating: { 645b970505dSJordan Rose const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl()); 646b970505dSJordan Rose Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor, 647dd18b11bSGeorge Karpenkov LCtx->getStackFrame()); 64805375eb4SJordan Rose SVal ThisVal = State->getSVal(ThisPtr); 649b970505dSJordan Rose 650a82ffe9dSArtem Dergachev if (CK == CXXConstructExpr::CK_Delegating) { 651f83d5479SArtem Dergachev Target = ThisVal; 652b970505dSJordan Rose } else { 653b970505dSJordan Rose // Cast to the base type. 654a82ffe9dSArtem Dergachev bool IsVirtual = (CK == CXXConstructExpr::CK_VirtualBase); 655a82ffe9dSArtem Dergachev SVal BaseVal = 656a82ffe9dSArtem Dergachev getStoreManager().evalDerivedToBase(ThisVal, E->getType(), IsVirtual); 657f83d5479SArtem Dergachev Target = BaseVal; 658b970505dSJordan Rose } 659b970505dSJordan Rose break; 660b970505dSJordan Rose } 661b970505dSJordan Rose } 662b970505dSJordan Rose 663239452caSArtem Dergachev if (State != Pred->getState()) { 664239452caSArtem Dergachev static SimpleProgramPointTag T("ExprEngine", 665239452caSArtem Dergachev "Prepare for object construction"); 666239452caSArtem Dergachev ExplodedNodeSet DstPrepare; 667239452caSArtem Dergachev StmtNodeBuilder BldrPrepare(Pred, DstPrepare, *currBldrCtx); 668a82ffe9dSArtem Dergachev BldrPrepare.generateNode(E, Pred, State, &T, ProgramPoint::PreStmtKind); 669239452caSArtem Dergachev assert(DstPrepare.size() <= 1); 670239452caSArtem Dergachev if (DstPrepare.size() == 0) 671239452caSArtem Dergachev return; 672239452caSArtem Dergachev Pred = *BldrPrepare.begin(); 673239452caSArtem Dergachev } 674239452caSArtem Dergachev 675a82ffe9dSArtem Dergachev const MemRegion *TargetRegion = Target.getAsRegion(); 676fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 677a82ffe9dSArtem Dergachev CallEventRef<> Call = 678a82ffe9dSArtem Dergachev CIE ? (CallEventRef<>)CEMgr.getCXXInheritedConstructorCall( 679a82ffe9dSArtem Dergachev CIE, TargetRegion, State, LCtx) 680a82ffe9dSArtem Dergachev : (CallEventRef<>)CEMgr.getCXXConstructorCall( 681a82ffe9dSArtem Dergachev CE, TargetRegion, State, LCtx); 68210e25031STed Kremenek 683f3c12ac3SJordan Rose ExplodedNodeSet DstPreVisit; 684a82ffe9dSArtem Dergachev getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, E, *this); 685b8e28654SJordan Rose 686b8e28654SJordan Rose ExplodedNodeSet PreInitialized; 687a82ffe9dSArtem Dergachev if (CE) { 688a82ffe9dSArtem Dergachev // FIXME: Is it possible and/or useful to do this before PreStmt? 689b8e28654SJordan Rose StmtNodeBuilder Bldr(DstPreVisit, PreInitialized, *currBldrCtx); 690b8e28654SJordan Rose for (ExplodedNodeSet::iterator I = DstPreVisit.begin(), 691b8e28654SJordan Rose E = DstPreVisit.end(); 692b8e28654SJordan Rose I != E; ++I) { 693b8e28654SJordan Rose ProgramStateRef State = (*I)->getState(); 694661ab34aSArtem Dergachev if (CE->requiresZeroInitialization()) { 695b8e28654SJordan Rose // FIXME: Once we properly handle constructors in new-expressions, we'll 696b8e28654SJordan Rose // need to invalidate the region before setting a default value, to make 697b8e28654SJordan Rose // sure there aren't any lingering bindings around. This probably needs 698b8e28654SJordan Rose // to happen regardless of whether or not the object is zero-initialized 699b8e28654SJordan Rose // to handle random fields of a placement-initialized object picking up 700b8e28654SJordan Rose // old bindings. We might only want to do it when we need to, though. 701b8e28654SJordan Rose // FIXME: This isn't actually correct for arrays -- we need to zero- 702b8e28654SJordan Rose // initialize the entire array, not just the first element -- but our 703b8e28654SJordan Rose // handling of arrays everywhere else is weak as well, so this shouldn't 704b8e28654SJordan Rose // actually make things worse. Placement new makes this tricky as well, 705b8e28654SJordan Rose // since it's then possible to be initializing one part of a multi- 706b8e28654SJordan Rose // dimensional array. 707f83d5479SArtem Dergachev State = State->bindDefaultZero(Target, LCtx); 708661ab34aSArtem Dergachev } 709661ab34aSArtem Dergachev 7100dbb783cSCraig Topper Bldr.generateNode(CE, *I, State, /*tag=*/nullptr, 7110dbb783cSCraig Topper ProgramPoint::PreStmtKind); 712b8e28654SJordan Rose } 713a82ffe9dSArtem Dergachev } else { 714a82ffe9dSArtem Dergachev PreInitialized = DstPreVisit; 715b8e28654SJordan Rose } 716b8e28654SJordan Rose 717afe7c2c2SJordan Rose ExplodedNodeSet DstPreCall; 718b8e28654SJordan Rose getCheckerManager().runCheckersForPreCall(DstPreCall, PreInitialized, 719fcd016e5SJordan Rose *Call, *this); 72010e25031STed Kremenek 72188bb563cSJordan Rose ExplodedNodeSet DstEvaluated; 72288bb563cSJordan Rose 723a82ffe9dSArtem Dergachev if (CE && CE->getConstructor()->isTrivial() && 724e83cb092SJordan Rose CE->getConstructor()->isCopyOrMoveConstructor() && 725168e29f6SArtem Dergachev !CallOpts.IsArrayCtorOrDtor) { 726b34b1e38SNithin Vadukkumchery Rajendrakumar StmtNodeBuilder Bldr(DstPreCall, DstEvaluated, *currBldrCtx); 727e83cb092SJordan Rose // FIXME: Handle other kinds of trivial constructors as well. 728e83cb092SJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 72988bb563cSJordan Rose I != E; ++I) 73088bb563cSJordan Rose performTrivialCopy(Bldr, *I, *Call); 73188bb563cSJordan Rose 73288bb563cSJordan Rose } else { 733afe7c2c2SJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 734f3c12ac3SJordan Rose I != E; ++I) 73537c1bf21SNithin Vadukkumchery Rajendrakumar getCheckerManager().runCheckersForEvalCall(DstEvaluated, *I, *Call, *this, 73637c1bf21SNithin Vadukkumchery Rajendrakumar CallOpts); 73788bb563cSJordan Rose } 73810e25031STed Kremenek 7392a8c18d9SAlexander Kornienko // If the CFG was constructed without elements for temporary destructors 7405b1ee2faSDevin Coughlin // and the just-called constructor created a temporary object then 7415b1ee2faSDevin Coughlin // stop exploration if the temporary object has a noreturn constructor. 7425b1ee2faSDevin Coughlin // This can lose coverage because the destructor, if it were present 7435b1ee2faSDevin Coughlin // in the CFG, would be called at the end of the full expression or 7445b1ee2faSDevin Coughlin // later (for life-time extended temporaries) -- but avoids infeasible 7455b1ee2faSDevin Coughlin // paths when no-return temporary destructors are used for assertions. 746b34b1e38SNithin Vadukkumchery Rajendrakumar ExplodedNodeSet DstEvaluatedPostProcessed; 747b34b1e38SNithin Vadukkumchery Rajendrakumar StmtNodeBuilder Bldr(DstEvaluated, DstEvaluatedPostProcessed, *currBldrCtx); 7485b1ee2faSDevin Coughlin const AnalysisDeclContext *ADC = LCtx->getAnalysisDeclContext(); 7495b1ee2faSDevin Coughlin if (!ADC->getCFGBuildOptions().AddTemporaryDtors) { 750f2be30deSKirstóf Umann if (llvm::isa_and_nonnull<CXXTempObjectRegion>(TargetRegion) && 751a82ffe9dSArtem Dergachev cast<CXXConstructorDecl>(Call->getDecl()) 752f2be30deSKirstóf Umann ->getParent() 753f2be30deSKirstóf Umann ->isAnyDestructorNoReturn()) { 7545b1ee2faSDevin Coughlin 7553da72051SArtem Dergachev // If we've inlined the constructor, then DstEvaluated would be empty. 7563da72051SArtem Dergachev // In this case we still want a sink, which could be implemented 7573da72051SArtem Dergachev // in processCallExit. But we don't have that implemented at the moment, 7583da72051SArtem Dergachev // so if you hit this assertion, see if you can avoid inlining 7593da72051SArtem Dergachev // the respective constructor when analyzer-config cfg-temporary-dtors 7603da72051SArtem Dergachev // is set to false. 7613da72051SArtem Dergachev // Otherwise there's nothing wrong with inlining such constructor. 7623da72051SArtem Dergachev assert(!DstEvaluated.empty() && 7633da72051SArtem Dergachev "We should not have inlined this constructor!"); 7643da72051SArtem Dergachev 7655b1ee2faSDevin Coughlin for (ExplodedNode *N : DstEvaluated) { 766a82ffe9dSArtem Dergachev Bldr.generateSink(E, N, N->getState()); 7675b1ee2faSDevin Coughlin } 7685b1ee2faSDevin Coughlin 7695b1ee2faSDevin Coughlin // There is no need to run the PostCall and PostStmt checker 7705b1ee2faSDevin Coughlin // callbacks because we just generated sinks on all nodes in th 7715b1ee2faSDevin Coughlin // frontier. 7725b1ee2faSDevin Coughlin return; 7735b1ee2faSDevin Coughlin } 7745b1ee2faSDevin Coughlin } 7755b1ee2faSDevin Coughlin 7763ccf14ebSArtem Dergachev ExplodedNodeSet DstPostArgumentCleanup; 777b34b1e38SNithin Vadukkumchery Rajendrakumar for (ExplodedNode *I : DstEvaluatedPostProcessed) 7783ccf14ebSArtem Dergachev finishArgumentConstruction(DstPostArgumentCleanup, I, *Call); 7793ccf14ebSArtem Dergachev 7803ccf14ebSArtem Dergachev // If there were other constructors called for object-type arguments 7813ccf14ebSArtem Dergachev // of this constructor, clean them up. 782afe7c2c2SJordan Rose ExplodedNodeSet DstPostCall; 7833ccf14ebSArtem Dergachev getCheckerManager().runCheckersForPostCall(DstPostCall, 7843ccf14ebSArtem Dergachev DstPostArgumentCleanup, 785fcd016e5SJordan Rose *Call, *this); 786a82ffe9dSArtem Dergachev getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, E, *this); 787a82ffe9dSArtem Dergachev } 788a82ffe9dSArtem Dergachev 789a82ffe9dSArtem Dergachev void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *CE, 790a82ffe9dSArtem Dergachev ExplodedNode *Pred, 791a82ffe9dSArtem Dergachev ExplodedNodeSet &Dst) { 792a82ffe9dSArtem Dergachev handleConstructor(CE, Pred, Dst); 793a82ffe9dSArtem Dergachev } 794a82ffe9dSArtem Dergachev 795a82ffe9dSArtem Dergachev void ExprEngine::VisitCXXInheritedCtorInitExpr( 796a82ffe9dSArtem Dergachev const CXXInheritedCtorInitExpr *CE, ExplodedNode *Pred, 797a82ffe9dSArtem Dergachev ExplodedNodeSet &Dst) { 798a82ffe9dSArtem Dergachev handleConstructor(CE, Pred, Dst); 79910e25031STed Kremenek } 80010e25031STed Kremenek 801b970505dSJordan Rose void ExprEngine::VisitCXXDestructor(QualType ObjectType, 80210e25031STed Kremenek const MemRegion *Dest, 80310e25031STed Kremenek const Stmt *S, 8042bc9674bSJordan Rose bool IsBaseDtor, 80510e25031STed Kremenek ExplodedNode *Pred, 806168e29f6SArtem Dergachev ExplodedNodeSet &Dst, 8078eb7a74bSArtem Dergachev EvalCallOptions &CallOpts) { 808097a0497SArtem Dergachev assert(S && "A destructor without a trigger!"); 809fcd016e5SJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 810fcd016e5SJordan Rose ProgramStateRef State = Pred->getState(); 811fcd016e5SJordan Rose 812b970505dSJordan Rose const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl(); 813b970505dSJordan Rose assert(RecordDecl && "Only CXXRecordDecls should have destructors"); 814b970505dSJordan Rose const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor(); 815097a0497SArtem Dergachev // FIXME: There should always be a Decl, otherwise the destructor call 816097a0497SArtem Dergachev // shouldn't have been added to the CFG in the first place. 817097a0497SArtem Dergachev if (!DtorDecl) { 818097a0497SArtem Dergachev // Skip the invalid destructor. We cannot simply return because 819097a0497SArtem Dergachev // it would interrupt the analysis instead. 820097a0497SArtem Dergachev static SimpleProgramPointTag T("ExprEngine", "SkipInvalidDestructor"); 821097a0497SArtem Dergachev // FIXME: PostImplicitCall with a null decl may crash elsewhere anyway. 822097a0497SArtem Dergachev PostImplicitCall PP(/*Decl=*/nullptr, S->getEndLoc(), LCtx, &T); 823097a0497SArtem Dergachev NodeBuilder Bldr(Pred, Dst, *currBldrCtx); 824097a0497SArtem Dergachev Bldr.generateNode(PP, Pred->getState(), Pred); 825097a0497SArtem Dergachev return; 826097a0497SArtem Dergachev } 827097a0497SArtem Dergachev 8288eb7a74bSArtem Dergachev if (!Dest) { 8298eb7a74bSArtem Dergachev // We're trying to destroy something that is not a region. This may happen 8308eb7a74bSArtem Dergachev // for a variety of reasons (unknown target region, concrete integer instead 8318eb7a74bSArtem Dergachev // of target region, etc.). The current code makes an attempt to recover. 8328eb7a74bSArtem Dergachev // FIXME: We probably don't really need to recover when we're dealing 8338eb7a74bSArtem Dergachev // with concrete integers specifically. 8348eb7a74bSArtem Dergachev CallOpts.IsCtorOrDtorWithImproperlyModeledTargetRegion = true; 8358eb7a74bSArtem Dergachev if (const Expr *E = dyn_cast_or_null<Expr>(S)) { 8368eb7a74bSArtem Dergachev Dest = MRMgr.getCXXTempObjectRegion(E, Pred->getLocationContext()); 8378eb7a74bSArtem Dergachev } else { 8388eb7a74bSArtem Dergachev static SimpleProgramPointTag T("ExprEngine", "SkipInvalidDestructor"); 8398eb7a74bSArtem Dergachev NodeBuilder Bldr(Pred, Dst, *currBldrCtx); 8408eb7a74bSArtem Dergachev Bldr.generateSink(Pred->getLocation().withTag(&T), 8418eb7a74bSArtem Dergachev Pred->getState(), Pred); 8428eb7a74bSArtem Dergachev return; 8438eb7a74bSArtem Dergachev } 8448eb7a74bSArtem Dergachev } 8458eb7a74bSArtem Dergachev 846fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 847fcd016e5SJordan Rose CallEventRef<CXXDestructorCall> Call = 8482bc9674bSJordan Rose CEMgr.getCXXDestructorCall(DtorDecl, S, Dest, IsBaseDtor, State, LCtx); 849142adc49STed Kremenek 85017a8757aSJordan Rose PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(), 85117a8757aSJordan Rose Call->getSourceRange().getBegin(), 85217a8757aSJordan Rose "Error evaluating destructor"); 85317a8757aSJordan Rose 8544ee71b8aSJordan Rose ExplodedNodeSet DstPreCall; 8554ee71b8aSJordan Rose getCheckerManager().runCheckersForPreCall(DstPreCall, Pred, 856fcd016e5SJordan Rose *Call, *this); 85710e25031STed Kremenek 8584ee71b8aSJordan Rose ExplodedNodeSet DstInvalidated; 859d94854a4STed Kremenek StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx); 8604ee71b8aSJordan Rose for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 8614ee71b8aSJordan Rose I != E; ++I) 862690ab040SArtem Dergachev defaultEvalCall(Bldr, *I, *Call, CallOpts); 8634ee71b8aSJordan Rose 8644ee71b8aSJordan Rose getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated, 865fcd016e5SJordan Rose *Call, *this); 86610e25031STed Kremenek } 86710e25031STed Kremenek 8688b808d64SJordan Rose void ExprEngine::VisitCXXNewAllocatorCall(const CXXNewExpr *CNE, 8698b808d64SJordan Rose ExplodedNode *Pred, 8708b808d64SJordan Rose ExplodedNodeSet &Dst) { 8718b808d64SJordan Rose ProgramStateRef State = Pred->getState(); 8728b808d64SJordan Rose const LocationContext *LCtx = Pred->getLocationContext(); 8738b808d64SJordan Rose PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(), 874a6e4358fSStephen Kelly CNE->getBeginLoc(), 8758b808d64SJordan Rose "Error evaluating New Allocator Call"); 8768b808d64SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 8778b808d64SJordan Rose CallEventRef<CXXAllocatorCall> Call = 8788b808d64SJordan Rose CEMgr.getCXXAllocatorCall(CNE, State, LCtx); 8798b808d64SJordan Rose 8808b808d64SJordan Rose ExplodedNodeSet DstPreCall; 8818b808d64SJordan Rose getCheckerManager().runCheckersForPreCall(DstPreCall, Pred, 8828b808d64SJordan Rose *Call, *this); 8838b808d64SJordan Rose 88455796302SArtem Dergachev ExplodedNodeSet DstPostCall; 88555796302SArtem Dergachev StmtNodeBuilder CallBldr(DstPreCall, DstPostCall, *currBldrCtx); 886f2be30deSKirstóf Umann for (ExplodedNode *I : DstPreCall) { 8871084de52SArtem Dergachev // FIXME: Provide evalCall for checkers? 88855796302SArtem Dergachev defaultEvalCall(CallBldr, I, *Call); 8891084de52SArtem Dergachev } 890beba5307SArtem Dergachev // If the call is inlined, DstPostCall will be empty and we bail out now. 89155796302SArtem Dergachev 89255796302SArtem Dergachev // Store return value of operator new() for future use, until the actual 89355796302SArtem Dergachev // CXXNewExpr gets processed. 89455796302SArtem Dergachev ExplodedNodeSet DstPostValue; 89555796302SArtem Dergachev StmtNodeBuilder ValueBldr(DstPostCall, DstPostValue, *currBldrCtx); 896f2be30deSKirstóf Umann for (ExplodedNode *I : DstPostCall) { 897beba5307SArtem Dergachev // FIXME: Because CNE serves as the "call site" for the allocator (due to 898beba5307SArtem Dergachev // lack of a better expression in the AST), the conjured return value symbol 899beba5307SArtem Dergachev // is going to be of the same type (C++ object pointer type). Technically 900beba5307SArtem Dergachev // this is not correct because the operator new's prototype always says that 901beba5307SArtem Dergachev // it returns a 'void *'. So we should change the type of the symbol, 902beba5307SArtem Dergachev // and then evaluate the cast over the symbolic pointer from 'void *' to 903beba5307SArtem Dergachev // the object pointer type. But without changing the symbol's type it 904beba5307SArtem Dergachev // is breaking too much to evaluate the no-op symbolic cast over it, so we 905beba5307SArtem Dergachev // skip it for now. 90655796302SArtem Dergachev ProgramStateRef State = I->getState(); 90750e0372fSArtem Dergachev SVal RetVal = State->getSVal(CNE, LCtx); 90850e0372fSArtem Dergachev 90950e0372fSArtem Dergachev // If this allocation function is not declared as non-throwing, failures 91050e0372fSArtem Dergachev // /must/ be signalled by exceptions, and thus the return value will never 91150e0372fSArtem Dergachev // be NULL. -fno-exceptions does not influence this semantics. 91250e0372fSArtem Dergachev // FIXME: GCC has a -fcheck-new option, which forces it to consider the case 91350e0372fSArtem Dergachev // where new can return NULL. If we end up supporting that option, we can 91450e0372fSArtem Dergachev // consider adding a check for it here. 91550e0372fSArtem Dergachev // C++11 [basic.stc.dynamic.allocation]p3. 91650e0372fSArtem Dergachev if (const FunctionDecl *FD = CNE->getOperatorNew()) { 91750e0372fSArtem Dergachev QualType Ty = FD->getType(); 91850e0372fSArtem Dergachev if (const auto *ProtoType = Ty->getAs<FunctionProtoType>()) 919eaf11ad7SRichard Smith if (!ProtoType->isNothrow()) 92050e0372fSArtem Dergachev State = State->assume(RetVal.castAs<DefinedOrUnknownSVal>(), true); 92150e0372fSArtem Dergachev } 92250e0372fSArtem Dergachev 923f83d5479SArtem Dergachev ValueBldr.generateNode( 924f83d5479SArtem Dergachev CNE, I, addObjectUnderConstruction(State, CNE, LCtx, RetVal)); 92555796302SArtem Dergachev } 92655796302SArtem Dergachev 92713b2026bSArtem Dergachev ExplodedNodeSet DstPostPostCallCallback; 92813b2026bSArtem Dergachev getCheckerManager().runCheckersForPostCall(DstPostPostCallCallback, 92913b2026bSArtem Dergachev DstPostValue, *Call, *this); 930f2be30deSKirstóf Umann for (ExplodedNode *I : DstPostPostCallCallback) { 931f2be30deSKirstóf Umann getCheckerManager().runCheckersForNewAllocator(*Call, Dst, I, *this); 93213b2026bSArtem Dergachev } 9338b808d64SJordan Rose } 9348b808d64SJordan Rose 93510e25031STed Kremenek void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred, 93610e25031STed Kremenek ExplodedNodeSet &Dst) { 937a4ee064cSJordan Rose // FIXME: Much of this should eventually migrate to CXXAllocatorCall. 938a4ee064cSJordan Rose // Also, we need to decide how allocators actually work -- they're not 939a4ee064cSJordan Rose // really part of the CXXNewExpr because they happen BEFORE the 940a4ee064cSJordan Rose // CXXConstructExpr subexpression. See PR12014 for some discussion. 94110e25031STed Kremenek 942d94854a4STed Kremenek unsigned blockCount = currBldrCtx->blockCount(); 943d519cae8STed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 94455796302SArtem Dergachev SVal symVal = UnknownVal(); 9458b662704SAnton Yartsev FunctionDecl *FD = CNE->getOperatorNew(); 94610e25031STed Kremenek 9471084de52SArtem Dergachev bool IsStandardGlobalOpNewFunction = 9481084de52SArtem Dergachev FD->isReplaceableGlobalAllocationFunction(); 9498b662704SAnton Yartsev 95055796302SArtem Dergachev ProgramStateRef State = Pred->getState(); 95155796302SArtem Dergachev 95255796302SArtem Dergachev // Retrieve the stored operator new() return value. 953549f9cd4SKristof Umann if (AMgr.getAnalyzerOptions().MayInlineCXXAllocator) { 954f83d5479SArtem Dergachev symVal = *getObjectUnderConstruction(State, CNE, LCtx); 955f83d5479SArtem Dergachev State = finishObjectConstruction(State, CNE, LCtx); 95655796302SArtem Dergachev } 95755796302SArtem Dergachev 9588b662704SAnton Yartsev // We assume all standard global 'operator new' functions allocate memory in 9598b662704SAnton Yartsev // heap. We realize this is an approximation that might not correctly model 9608b662704SAnton Yartsev // a custom global allocator. 96155796302SArtem Dergachev if (symVal.isUnknown()) { 9628b662704SAnton Yartsev if (IsStandardGlobalOpNewFunction) 9638b662704SAnton Yartsev symVal = svalBuilder.getConjuredHeapSymbolVal(CNE, LCtx, blockCount); 9648b662704SAnton Yartsev else 9650dbb783cSCraig Topper symVal = svalBuilder.conjureSymbolVal(nullptr, CNE, LCtx, CNE->getType(), 9668b662704SAnton Yartsev blockCount); 96755796302SArtem Dergachev } 9688b662704SAnton Yartsev 969fcd016e5SJordan Rose CallEventManager &CEMgr = getStateManager().getCallEventManager(); 970fcd016e5SJordan Rose CallEventRef<CXXAllocatorCall> Call = 971fcd016e5SJordan Rose CEMgr.getCXXAllocatorCall(CNE, State, LCtx); 972fcd016e5SJordan Rose 973549f9cd4SKristof Umann if (!AMgr.getAnalyzerOptions().MayInlineCXXAllocator) { 974a4ee064cSJordan Rose // Invalidate placement args. 975fcd016e5SJordan Rose // FIXME: Once we figure out how we want allocators to work, 97672649423SKristof Umann // we should be using the usual pre-/(default-)eval-/post-call checkers 97772649423SKristof Umann // here. 978fcd016e5SJordan Rose State = Call->invalidateRegions(blockCount); 979b8cb8359SJordan Rose if (!State) 980b8cb8359SJordan Rose return; 981a4ee064cSJordan Rose 982fce1b03eSPavel Labath // If this allocation function is not declared as non-throwing, failures 98350e0372fSArtem Dergachev // /must/ be signalled by exceptions, and thus the return value will never 98450e0372fSArtem Dergachev // be NULL. -fno-exceptions does not influence this semantics. 985fce1b03eSPavel Labath // FIXME: GCC has a -fcheck-new option, which forces it to consider the case 986fce1b03eSPavel Labath // where new can return NULL. If we end up supporting that option, we can 987fce1b03eSPavel Labath // consider adding a check for it here. 9883957fd58SJordan Rose // C++11 [basic.stc.dynamic.allocation]p3. 989ca94f28dSSimon Pilgrim if (const auto *ProtoType = FD->getType()->getAs<FunctionProtoType>()) 990eaf11ad7SRichard Smith if (!ProtoType->isNothrow()) 99155796302SArtem Dergachev if (auto dSymVal = symVal.getAs<DefinedOrUnknownSVal>()) 99255796302SArtem Dergachev State = State->assume(*dSymVal, true); 9933957fd58SJordan Rose } 9943957fd58SJordan Rose 995b8cb8359SJordan Rose StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 996b8cb8359SJordan Rose 997beba5307SArtem Dergachev SVal Result = symVal; 998beba5307SArtem Dergachev 99910e25031STed Kremenek if (CNE->isArray()) { 1000b032e3ffSisuckatcs 1001630f7dafSArtem Dergachev if (const auto *NewReg = cast_or_null<SubRegion>(symVal.getAsRegion())) { 1002b032e3ffSisuckatcs // If each element is initialized by their default constructor, the field 1003b032e3ffSisuckatcs // values are properly placed inside the required region, however if an 1004b032e3ffSisuckatcs // initializer list is used, this doesn't happen automatically. 1005b032e3ffSisuckatcs auto *Init = CNE->getInitializer(); 1006ae002f8bSKazu Hirata bool isInitList = isa_and_nonnull<InitListExpr>(Init); 1007b032e3ffSisuckatcs 1008b032e3ffSisuckatcs QualType ObjTy = 1009b032e3ffSisuckatcs isInitList ? Init->getType() : CNE->getType()->getPointeeType(); 1010a4ee064cSJordan Rose const ElementRegion *EleReg = 1011b032e3ffSisuckatcs MRMgr.getElementRegion(ObjTy, svalBuilder.makeArrayIndex(0), NewReg, 1012b032e3ffSisuckatcs svalBuilder.getContext()); 1013beba5307SArtem Dergachev Result = loc::MemRegionVal(EleReg); 1014b032e3ffSisuckatcs 1015b032e3ffSisuckatcs // If the array is list initialized, we bind the initializer list to the 1016b032e3ffSisuckatcs // memory region here, otherwise we would lose it. 1017b032e3ffSisuckatcs if (isInitList) { 1018b032e3ffSisuckatcs Bldr.takeNodes(Pred); 1019b032e3ffSisuckatcs Pred = Bldr.generateNode(CNE, Pred, State); 1020b032e3ffSisuckatcs 1021b032e3ffSisuckatcs SVal V = State->getSVal(Init, LCtx); 1022b032e3ffSisuckatcs ExplodedNodeSet evaluated; 1023b032e3ffSisuckatcs evalBind(evaluated, CNE, Pred, Result, V, true); 1024b032e3ffSisuckatcs 1025b032e3ffSisuckatcs Bldr.takeNodes(Pred); 1026b032e3ffSisuckatcs Bldr.addNodes(evaluated); 1027b032e3ffSisuckatcs 1028b032e3ffSisuckatcs Pred = *evaluated.begin(); 1029b032e3ffSisuckatcs State = Pred->getState(); 1030beba5307SArtem Dergachev } 1031b032e3ffSisuckatcs } 1032b032e3ffSisuckatcs 1033beba5307SArtem Dergachev State = State->BindExpr(CNE, Pred->getLocationContext(), Result); 103444320367SJordan Rose Bldr.generateNode(CNE, Pred, State); 103510e25031STed Kremenek return; 103610e25031STed Kremenek } 103710e25031STed Kremenek 1038a4ee064cSJordan Rose // FIXME: Once we have proper support for CXXConstructExprs inside 1039a4ee064cSJordan Rose // CXXNewExpr, we need to make sure that the constructed object is not 1040a4ee064cSJordan Rose // immediately invalidated here. (The placement call should happen before 1041a4ee064cSJordan Rose // the constructor call anyway.) 1042ca94f28dSSimon Pilgrim if (FD->isReservedGlobalPlacementOperator()) { 104344320367SJordan Rose // Non-array placement new should always return the placement location. 104444320367SJordan Rose SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx); 1045b8cb8359SJordan Rose Result = svalBuilder.evalCast(PlacementLoc, CNE->getType(), 1046aaf83184SJordan Rose CNE->getPlacementArg(0)->getType()); 104744320367SJordan Rose } 104844320367SJordan Rose 1049b8cb8359SJordan Rose // Bind the address of the object, then check to see if we cached out. 1050b8cb8359SJordan Rose State = State->BindExpr(CNE, LCtx, Result); 10518f6b4b04SJordan Rose ExplodedNode *NewN = Bldr.generateNode(CNE, Pred, State); 10528f6b4b04SJordan Rose if (!NewN) 10538f6b4b04SJordan Rose return; 10543503cb35SJordan Rose 10555089f3b3SJordan Rose // If the type is not a record, we won't have a CXXConstructExpr as an 10565089f3b3SJordan Rose // initializer. Copy the value over. 10575089f3b3SJordan Rose if (const Expr *Init = CNE->getInitializer()) { 10585089f3b3SJordan Rose if (!isa<CXXConstructExpr>(Init)) { 10593503cb35SJordan Rose assert(Bldr.getResults().size() == 1); 10608f6b4b04SJordan Rose Bldr.takeNodes(NewN); 1061b8cb8359SJordan Rose evalBind(Dst, CNE, NewN, Result, State->getSVal(Init, LCtx), 1062b8cb8359SJordan Rose /*FirstInit=*/IsStandardGlobalOpNewFunction); 10633503cb35SJordan Rose } 10643503cb35SJordan Rose } 106510e25031STed Kremenek } 106610e25031STed Kremenek 106710e25031STed Kremenek void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE, 106810e25031STed Kremenek ExplodedNode *Pred, ExplodedNodeSet &Dst) { 10699d69072fSKirstóf Umann 10709d69072fSKirstóf Umann CallEventManager &CEMgr = getStateManager().getCallEventManager(); 10719d69072fSKirstóf Umann CallEventRef<CXXDeallocatorCall> Call = CEMgr.getCXXDeallocatorCall( 10729d69072fSKirstóf Umann CDE, Pred->getState(), Pred->getLocationContext()); 10739d69072fSKirstóf Umann 10749d69072fSKirstóf Umann ExplodedNodeSet DstPreCall; 10759d69072fSKirstóf Umann getCheckerManager().runCheckersForPreCall(DstPreCall, Pred, *Call, *this); 10761ec1cdcfSFred Tingaud ExplodedNodeSet DstPostCall; 10779d69072fSKirstóf Umann 10781ec1cdcfSFred Tingaud if (AMgr.getAnalyzerOptions().MayInlineCXXAllocator) { 10791ec1cdcfSFred Tingaud StmtNodeBuilder Bldr(DstPreCall, DstPostCall, *currBldrCtx); 10801ec1cdcfSFred Tingaud for (ExplodedNode *I : DstPreCall) { 10811ec1cdcfSFred Tingaud defaultEvalCall(Bldr, I, *Call); 10821ec1cdcfSFred Tingaud } 10831ec1cdcfSFred Tingaud } else { 10841ec1cdcfSFred Tingaud DstPostCall = DstPreCall; 10851ec1cdcfSFred Tingaud } 10861ec1cdcfSFred Tingaud getCheckerManager().runCheckersForPostCall(Dst, DstPostCall, *Call, *this); 108710e25031STed Kremenek } 108810e25031STed Kremenek 10899d69072fSKirstóf Umann void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS, ExplodedNode *Pred, 10908fdb59f9STed Kremenek ExplodedNodeSet &Dst) { 10918fdb59f9STed Kremenek const VarDecl *VD = CS->getExceptionDecl(); 1092b1279b5cSTed Kremenek if (!VD) { 1093b1279b5cSTed Kremenek Dst.Add(Pred); 1094b1279b5cSTed Kremenek return; 1095b1279b5cSTed Kremenek } 1096b1279b5cSTed Kremenek 10978fdb59f9STed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 1098d227833cSTed Kremenek SVal V = svalBuilder.conjureSymbolVal(CS, LCtx, VD->getType(), 1099d94854a4STed Kremenek currBldrCtx->blockCount()); 11008fdb59f9STed Kremenek ProgramStateRef state = Pred->getState(); 1101b570195cSAnna Zaks state = state->bindLoc(state->getLValue(VD, LCtx), V, LCtx); 11028fdb59f9STed Kremenek 1103d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 11048fdb59f9STed Kremenek Bldr.generateNode(CS, Pred, state); 11058fdb59f9STed Kremenek } 11068fdb59f9STed Kremenek 110710e25031STed Kremenek void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred, 110810e25031STed Kremenek ExplodedNodeSet &Dst) { 1109d94854a4STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx); 1110e594034fSAnna Zaks 111110e25031STed Kremenek // Get the this object region from StoreManager. 1112632e3b7eSTed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 111310e25031STed Kremenek const MemRegion *R = 111410e25031STed Kremenek svalBuilder.getRegionManager().getCXXThisRegion( 111510e25031STed Kremenek getContext().getCanonicalType(TE->getType()), 1116632e3b7eSTed Kremenek LCtx); 111710e25031STed Kremenek 111849b1e38eSTed Kremenek ProgramStateRef state = Pred->getState(); 111910e25031STed Kremenek SVal V = state->getSVal(loc::MemRegionVal(R)); 1120632e3b7eSTed Kremenek Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V)); 112110e25031STed Kremenek } 112215843343SGabor Horvath 112315843343SGabor Horvath void ExprEngine::VisitLambdaExpr(const LambdaExpr *LE, ExplodedNode *Pred, 112415843343SGabor Horvath ExplodedNodeSet &Dst) { 112515843343SGabor Horvath const LocationContext *LocCtxt = Pred->getLocationContext(); 112615843343SGabor Horvath 112715843343SGabor Horvath // Get the region of the lambda itself. 112815843343SGabor Horvath const MemRegion *R = svalBuilder.getRegionManager().getCXXTempObjectRegion( 112915843343SGabor Horvath LE, LocCtxt); 113015843343SGabor Horvath SVal V = loc::MemRegionVal(R); 113115843343SGabor Horvath 113215843343SGabor Horvath ProgramStateRef State = Pred->getState(); 113315843343SGabor Horvath 113415843343SGabor Horvath // If we created a new MemRegion for the lambda, we should explicitly bind 113515843343SGabor Horvath // the captures. 1136*996b092cSisuckatcs unsigned Idx = 0; 113715843343SGabor Horvath CXXRecordDecl::field_iterator CurField = LE->getLambdaClass()->field_begin(); 113815843343SGabor Horvath for (LambdaExpr::const_capture_init_iterator i = LE->capture_init_begin(), 113915843343SGabor Horvath e = LE->capture_init_end(); 1140*996b092cSisuckatcs i != e; ++i, ++CurField, ++Idx) { 11417bdca8b2SDevin Coughlin FieldDecl *FieldForCapture = *CurField; 11427bdca8b2SDevin Coughlin SVal FieldLoc = State->getLValue(FieldForCapture, V); 11437bdca8b2SDevin Coughlin 11447bdca8b2SDevin Coughlin SVal InitVal; 11457bdca8b2SDevin Coughlin if (!FieldForCapture->hasCapturedVLAType()) { 11467bdca8b2SDevin Coughlin Expr *InitExpr = *i; 1147*996b092cSisuckatcs 1148*996b092cSisuckatcs if (const auto AILE = dyn_cast<ArrayInitLoopExpr>(InitExpr)) { 1149*996b092cSisuckatcs // If the AILE initializes a POD array, we need to keep it as the 1150*996b092cSisuckatcs // InitExpr. 1151*996b092cSisuckatcs if (dyn_cast<CXXConstructExpr>(AILE->getSubExpr())) 1152*996b092cSisuckatcs InitExpr = AILE->getSubExpr(); 1153*996b092cSisuckatcs } 1154*996b092cSisuckatcs 11557bdca8b2SDevin Coughlin assert(InitExpr && "Capture missing initialization expression"); 1156*996b092cSisuckatcs 1157*996b092cSisuckatcs if (dyn_cast<CXXConstructExpr>(InitExpr)) { 1158*996b092cSisuckatcs InitVal = *getObjectUnderConstruction(State, {LE, Idx}, LocCtxt); 1159*996b092cSisuckatcs InitVal = State->getSVal(InitVal.getAsRegion()); 1160*996b092cSisuckatcs 1161*996b092cSisuckatcs State = finishObjectConstruction(State, {LE, Idx}, LocCtxt); 1162*996b092cSisuckatcs } else 11637bdca8b2SDevin Coughlin InitVal = State->getSVal(InitExpr, LocCtxt); 1164*996b092cSisuckatcs 11657bdca8b2SDevin Coughlin } else { 1166*996b092cSisuckatcs 1167*996b092cSisuckatcs assert(!getObjectUnderConstruction(State, {LE, Idx}, LocCtxt) && 1168*996b092cSisuckatcs "VLA capture by value is a compile time error!"); 1169*996b092cSisuckatcs 11707bdca8b2SDevin Coughlin // The field stores the length of a captured variable-length array. 11717bdca8b2SDevin Coughlin // These captures don't have initialization expressions; instead we 11727bdca8b2SDevin Coughlin // get the length from the VLAType size expression. 11737bdca8b2SDevin Coughlin Expr *SizeExpr = FieldForCapture->getCapturedVLAType()->getSizeExpr(); 11747bdca8b2SDevin Coughlin InitVal = State->getSVal(SizeExpr, LocCtxt); 11757bdca8b2SDevin Coughlin } 11767bdca8b2SDevin Coughlin 1177b570195cSAnna Zaks State = State->bindLoc(FieldLoc, InitVal, LocCtxt); 117815843343SGabor Horvath } 117915843343SGabor Horvath 118015843343SGabor Horvath // Decay the Loc into an RValue, because there might be a 118115843343SGabor Horvath // MaterializeTemporaryExpr node above this one which expects the bound value 118215843343SGabor Horvath // to be an RValue. 118315843343SGabor Horvath SVal LambdaRVal = State->getSVal(R); 118415843343SGabor Horvath 118515843343SGabor Horvath ExplodedNodeSet Tmp; 118615843343SGabor Horvath StmtNodeBuilder Bldr(Pred, Tmp, *currBldrCtx); 118715843343SGabor Horvath // FIXME: is this the right program point kind? 118815843343SGabor Horvath Bldr.generateNode(LE, Pred, 118915843343SGabor Horvath State->BindExpr(LE, LocCtxt, LambdaRVal), 119015843343SGabor Horvath nullptr, ProgramPoint::PostLValueKind); 119115843343SGabor Horvath 119215843343SGabor Horvath // FIXME: Move all post/pre visits to ::Visit(). 119315843343SGabor Horvath getCheckerManager().runCheckersForPostStmt(Dst, Tmp, LE, *this); 119415843343SGabor Horvath } 1195