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/CheckerManager.h" 1510e25031STed Kremenek #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" 1610e25031STed Kremenek #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 17742920c8SJordan Rose #include "clang/StaticAnalyzer/Core/PathSensitive/Calls.h" 1810e25031STed Kremenek #include "clang/AST/DeclCXX.h" 198fdb59f9STed Kremenek #include "clang/AST/StmtCXX.h" 2010e25031STed Kremenek 2110e25031STed Kremenek using namespace clang; 2210e25031STed Kremenek using namespace ento; 2310e25031STed Kremenek 2410e25031STed Kremenek void ExprEngine::CreateCXXTemporaryObject(const MaterializeTemporaryExpr *ME, 2510e25031STed Kremenek ExplodedNode *Pred, 2610e25031STed Kremenek ExplodedNodeSet &Dst) { 27e4e5712cSAnna Zaks StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 282a14c695STed Kremenek const Expr *tempExpr = ME->GetTemporaryExpr()->IgnoreParens(); 2949b1e38eSTed Kremenek ProgramStateRef state = Pred->getState(); 30632e3b7eSTed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 3110e25031STed Kremenek 3210e25031STed Kremenek // Bind the temporary object to the value of the expression. Then bind 3310e25031STed Kremenek // the expression to the location of the object. 34632e3b7eSTed Kremenek SVal V = state->getSVal(tempExpr, Pred->getLocationContext()); 3510e25031STed Kremenek 3610e25031STed Kremenek const MemRegion *R = 37632e3b7eSTed Kremenek svalBuilder.getRegionManager().getCXXTempObjectRegion(ME, LCtx); 3810e25031STed Kremenek 3910e25031STed Kremenek state = state->bindLoc(loc::MemRegionVal(R), V); 40632e3b7eSTed Kremenek Bldr.generateNode(ME, Pred, state->BindExpr(ME, LCtx, loc::MemRegionVal(R))); 4110e25031STed Kremenek } 4210e25031STed Kremenek 4311764ab4SBenjamin Kramer void ExprEngine::VisitCXXTemporaryObjectExpr(const CXXTemporaryObjectExpr *expr, 4411764ab4SBenjamin Kramer ExplodedNode *Pred, 4511764ab4SBenjamin Kramer ExplodedNodeSet &Dst) { 4611764ab4SBenjamin Kramer VisitCXXConstructExpr(expr, 0, Pred, Dst); 4711764ab4SBenjamin Kramer } 4811764ab4SBenjamin Kramer 49*f3c12ac3SJordan Rose void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *CE, 5010e25031STed Kremenek const MemRegion *Dest, 5110e25031STed Kremenek ExplodedNode *Pred, 5210e25031STed Kremenek ExplodedNodeSet &destNodes) { 53*f3c12ac3SJordan Rose CXXConstructorCall Call(CE, Dest, Pred->getState(), 5410e25031STed Kremenek Pred->getLocationContext()); 5510e25031STed Kremenek 56*f3c12ac3SJordan Rose ExplodedNodeSet DstPreVisit; 57*f3c12ac3SJordan Rose getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, CE, *this); 5810e25031STed Kremenek 59*f3c12ac3SJordan Rose ExplodedNodeSet DstInvalidated; 60*f3c12ac3SJordan Rose for (ExplodedNodeSet::iterator I = DstPreVisit.begin(), E = DstPreVisit.end(); 61*f3c12ac3SJordan Rose I != E; ++I) 62*f3c12ac3SJordan Rose defaultEvalCall(DstInvalidated, *I, Call); 6310e25031STed Kremenek 64*f3c12ac3SJordan Rose getCheckerManager().runCheckersForPostStmt(destNodes, DstInvalidated, 65*f3c12ac3SJordan Rose CE, *this); 6610e25031STed Kremenek } 6710e25031STed Kremenek 6810e25031STed Kremenek void ExprEngine::VisitCXXDestructor(const CXXDestructorDecl *DD, 6910e25031STed Kremenek const MemRegion *Dest, 7010e25031STed Kremenek const Stmt *S, 7110e25031STed Kremenek ExplodedNode *Pred, 7210e25031STed Kremenek ExplodedNodeSet &Dst) { 73e4e5712cSAnna Zaks StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 7410e25031STed Kremenek if (!(DD->doesThisDeclarationHaveABody() && AMgr.shouldInlineCall())) 7510e25031STed Kremenek return; 76142adc49STed Kremenek 7710e25031STed Kremenek // Create the context for 'this' region. 78142adc49STed Kremenek const StackFrameContext *SFC = 7981ce1c8aSTed Kremenek AnalysisDeclContexts.getContext(DD)-> 80142adc49STed Kremenek getStackFrame(Pred->getLocationContext(), S, 81e594034fSAnna Zaks currentBuilderContext->getBlock(), currentStmtIdx); 8210e25031STed Kremenek 8310e25031STed Kremenek CallEnter PP(S, SFC, Pred->getLocationContext()); 8449b1e38eSTed Kremenek ProgramStateRef state = Pred->getState(); 8572b3452cSTed Kremenek state = state->bindLoc(svalBuilder.getCXXThis(DD->getParent(), SFC), 8672b3452cSTed Kremenek loc::MemRegionVal(Dest)); 87e594034fSAnna Zaks Bldr.generateNode(PP, Pred, state); 8810e25031STed Kremenek } 8910e25031STed Kremenek 9044320367SJordan Rose static bool isPointerToConst(const ParmVarDecl *ParamDecl) { 9144320367SJordan Rose // FIXME: Copied from ExprEngineCallAndReturn.cpp 9244320367SJordan Rose QualType PointeeTy = ParamDecl->getOriginalType()->getPointeeType(); 9344320367SJordan Rose if (PointeeTy != QualType() && PointeeTy.isConstQualified() && 9444320367SJordan Rose !PointeeTy->isAnyPointerType() && !PointeeTy->isReferenceType()) { 9544320367SJordan Rose return true; 9644320367SJordan Rose } 9744320367SJordan Rose return false; 9844320367SJordan Rose } 9944320367SJordan Rose 10010e25031STed Kremenek void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred, 10110e25031STed Kremenek ExplodedNodeSet &Dst) { 102e4e5712cSAnna Zaks StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 10310e25031STed Kremenek 104e594034fSAnna Zaks unsigned blockCount = currentBuilderContext->getCurrentBlockCount(); 105d519cae8STed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 10610e25031STed Kremenek DefinedOrUnknownSVal symVal = 107d519cae8STed Kremenek svalBuilder.getConjuredSymbolVal(NULL, CNE, LCtx, CNE->getType(), blockCount); 10810e25031STed Kremenek const MemRegion *NewReg = cast<loc::MemRegionVal>(symVal).getRegion(); 10910e25031STed Kremenek QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType(); 11010e25031STed Kremenek const ElementRegion *EleReg = 11110e25031STed Kremenek getStoreManager().GetElementZeroRegion(NewReg, ObjTy); 11244320367SJordan Rose ProgramStateRef State = Pred->getState(); 11310e25031STed Kremenek 11410e25031STed Kremenek if (CNE->isArray()) { 11510e25031STed Kremenek // FIXME: allocating an array requires simulating the constructors. 11610e25031STed Kremenek // For now, just return a symbolicated region. 11744320367SJordan Rose State = State->BindExpr(CNE, Pred->getLocationContext(), 118632e3b7eSTed Kremenek loc::MemRegionVal(EleReg)); 11944320367SJordan Rose Bldr.generateNode(CNE, Pred, State); 12010e25031STed Kremenek return; 12110e25031STed Kremenek } 12210e25031STed Kremenek 12344320367SJordan Rose FunctionDecl *FD = CNE->getOperatorNew(); 12444320367SJordan Rose if (FD && FD->isReservedGlobalPlacementOperator()) { 12544320367SJordan Rose // Non-array placement new should always return the placement location. 12644320367SJordan Rose SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx); 12744320367SJordan Rose State = State->BindExpr(CNE, LCtx, PlacementLoc); 12844320367SJordan Rose // FIXME: Once we have proper support for CXXConstructExprs inside 12944320367SJordan Rose // CXXNewExpr, we need to make sure that the constructed object is not 13044320367SJordan Rose // immediately invalidated here. (The placement call should happen before 13144320367SJordan Rose // the constructor call anyway.) 13244320367SJordan Rose } 13344320367SJordan Rose 13444320367SJordan Rose // Invalidate placement args. 13544320367SJordan Rose 13644320367SJordan Rose // FIXME: This is largely copied from invalidateArguments, because 13744320367SJordan Rose // CallOrObjCMessage is not general enough to handle new-expressions yet. 13844320367SJordan Rose SmallVector<const MemRegion *, 4> RegionsToInvalidate; 13944320367SJordan Rose 14044320367SJordan Rose unsigned Index = 0; 14144320367SJordan Rose for (CXXNewExpr::const_arg_iterator I = CNE->placement_arg_begin(), 14244320367SJordan Rose E = CNE->placement_arg_end(); 14344320367SJordan Rose I != E; ++I) { 14444320367SJordan Rose // Pre-increment the argument index to skip over the implicit size arg. 14544320367SJordan Rose ++Index; 14644320367SJordan Rose if (FD && Index < FD->getNumParams()) 14744320367SJordan Rose if (isPointerToConst(FD->getParamDecl(Index))) 14844320367SJordan Rose continue; 14944320367SJordan Rose 15044320367SJordan Rose SVal V = State->getSVal(*I, LCtx); 15144320367SJordan Rose 15244320367SJordan Rose // If we are passing a location wrapped as an integer, unwrap it and 15344320367SJordan Rose // invalidate the values referred by the location. 15444320367SJordan Rose if (nonloc::LocAsInteger *Wrapped = dyn_cast<nonloc::LocAsInteger>(&V)) 15544320367SJordan Rose V = Wrapped->getLoc(); 15644320367SJordan Rose else if (!isa<Loc>(V)) 15744320367SJordan Rose continue; 15844320367SJordan Rose 15944320367SJordan Rose if (const MemRegion *R = V.getAsRegion()) { 16044320367SJordan Rose // Invalidate the value of the variable passed by reference. 16144320367SJordan Rose 16244320367SJordan Rose // Are we dealing with an ElementRegion? If the element type is 16344320367SJordan Rose // a basic integer type (e.g., char, int) and the underlying region 16444320367SJordan Rose // is a variable region then strip off the ElementRegion. 16544320367SJordan Rose // FIXME: We really need to think about this for the general case 16644320367SJordan Rose // as sometimes we are reasoning about arrays and other times 16744320367SJordan Rose // about (char*), etc., is just a form of passing raw bytes. 16844320367SJordan Rose // e.g., void *p = alloca(); foo((char*)p); 16944320367SJordan Rose if (const ElementRegion *ER = dyn_cast<ElementRegion>(R)) { 17044320367SJordan Rose // Checking for 'integral type' is probably too promiscuous, but 17144320367SJordan Rose // we'll leave it in for now until we have a systematic way of 17244320367SJordan Rose // handling all of these cases. Eventually we need to come up 17344320367SJordan Rose // with an interface to StoreManager so that this logic can be 17444320367SJordan Rose // appropriately delegated to the respective StoreManagers while 17544320367SJordan Rose // still allowing us to do checker-specific logic (e.g., 17644320367SJordan Rose // invalidating reference counts), probably via callbacks. 17744320367SJordan Rose if (ER->getElementType()->isIntegralOrEnumerationType()) { 17844320367SJordan Rose const MemRegion *superReg = ER->getSuperRegion(); 17944320367SJordan Rose if (isa<VarRegion>(superReg) || isa<FieldRegion>(superReg) || 18044320367SJordan Rose isa<ObjCIvarRegion>(superReg)) 18144320367SJordan Rose R = cast<TypedRegion>(superReg); 18244320367SJordan Rose } 18344320367SJordan Rose // FIXME: What about layers of ElementRegions? 18444320367SJordan Rose } 18544320367SJordan Rose 18644320367SJordan Rose // Mark this region for invalidation. We batch invalidate regions 18744320367SJordan Rose // below for efficiency. 18844320367SJordan Rose RegionsToInvalidate.push_back(R); 18944320367SJordan Rose } else { 19044320367SJordan Rose // Nuke all other arguments passed by reference. 19144320367SJordan Rose // FIXME: is this necessary or correct? This handles the non-Region 19244320367SJordan Rose // cases. Is it ever valid to store to these? 19344320367SJordan Rose State = State->unbindLoc(cast<Loc>(V)); 19444320367SJordan Rose } 19544320367SJordan Rose } 19644320367SJordan Rose 19744320367SJordan Rose // Invalidate designated regions using the batch invalidation API. 19844320367SJordan Rose 19944320367SJordan Rose // FIXME: We can have collisions on the conjured symbol if the 20044320367SJordan Rose // expression *I also creates conjured symbols. We probably want 20144320367SJordan Rose // to identify conjured symbols by an expression pair: the enclosing 20244320367SJordan Rose // expression (the context) and the expression itself. This should 20344320367SJordan Rose // disambiguate conjured symbols. 20444320367SJordan Rose unsigned Count = currentBuilderContext->getCurrentBlockCount(); 20544320367SJordan Rose 20644320367SJordan Rose // NOTE: Even if RegionsToInvalidate is empty, we may still invalidate 20744320367SJordan Rose // global variables. 20844320367SJordan Rose State = State->invalidateRegions(RegionsToInvalidate, CNE, Count, LCtx); 20944320367SJordan Rose Bldr.generateNode(CNE, Pred, State); 21044320367SJordan Rose return; 21144320367SJordan Rose 21244320367SJordan Rose // FIXME: The below code is long-since dead. However, constructor handling 21344320367SJordan Rose // in new-expressions is far from complete. See PR12014 for more details. 2146047f07eSSebastian Redl #if 0 21510e25031STed Kremenek // Evaluate constructor arguments. 21610e25031STed Kremenek const FunctionProtoType *FnType = NULL; 21710e25031STed Kremenek const CXXConstructorDecl *CD = CNE->getConstructor(); 21810e25031STed Kremenek if (CD) 21910e25031STed Kremenek FnType = CD->getType()->getAs<FunctionProtoType>(); 22010e25031STed Kremenek ExplodedNodeSet argsEvaluated; 221e594034fSAnna Zaks Bldr.takeNodes(Pred); 22210e25031STed Kremenek evalArguments(CNE->constructor_arg_begin(), CNE->constructor_arg_end(), 22310e25031STed Kremenek FnType, Pred, argsEvaluated); 224e594034fSAnna Zaks Bldr.addNodes(argsEvaluated); 22510e25031STed Kremenek 22610e25031STed Kremenek // Initialize the object region and bind the 'new' expression. 22710e25031STed Kremenek for (ExplodedNodeSet::iterator I = argsEvaluated.begin(), 22810e25031STed Kremenek E = argsEvaluated.end(); I != E; ++I) { 22910e25031STed Kremenek 23049b1e38eSTed Kremenek ProgramStateRef state = (*I)->getState(); 23110e25031STed Kremenek 23210e25031STed Kremenek // Accumulate list of regions that are invalidated. 23310e25031STed Kremenek // FIXME: Eventually we should unify the logic for constructor 23410e25031STed Kremenek // processing in one place. 23510e25031STed Kremenek SmallVector<const MemRegion*, 10> regionsToInvalidate; 23610e25031STed Kremenek for (CXXNewExpr::const_arg_iterator 23710e25031STed Kremenek ai = CNE->constructor_arg_begin(), ae = CNE->constructor_arg_end(); 23810e25031STed Kremenek ai != ae; ++ai) 23910e25031STed Kremenek { 240632e3b7eSTed Kremenek SVal val = state->getSVal(*ai, (*I)->getLocationContext()); 24110e25031STed Kremenek if (const MemRegion *region = val.getAsRegion()) 24210e25031STed Kremenek regionsToInvalidate.push_back(region); 24310e25031STed Kremenek } 24410e25031STed Kremenek 24510e25031STed Kremenek if (ObjTy->isRecordType()) { 24610e25031STed Kremenek regionsToInvalidate.push_back(EleReg); 24710e25031STed Kremenek // Invalidate the regions. 2488158ef0dSAnna Zaks // TODO: Pass the call to new information as the last argument, to limit 2498158ef0dSAnna Zaks // the globals which will get invalidated. 2501fad6631SJordy Rose state = state->invalidateRegions(regionsToInvalidate, 2518158ef0dSAnna Zaks CNE, blockCount, 0, 0); 25210e25031STed Kremenek 25310e25031STed Kremenek } else { 25410e25031STed Kremenek // Invalidate the regions. 2558158ef0dSAnna Zaks // TODO: Pass the call to new information as the last argument, to limit 2568158ef0dSAnna Zaks // the globals which will get invalidated. 2571fad6631SJordy Rose state = state->invalidateRegions(regionsToInvalidate, 2588158ef0dSAnna Zaks CNE, blockCount, 0, 0); 25910e25031STed Kremenek 26010e25031STed Kremenek if (CNE->hasInitializer()) { 261632e3b7eSTed Kremenek SVal V = state->getSVal(*CNE->constructor_arg_begin(), 262632e3b7eSTed Kremenek (*I)->getLocationContext()); 26310e25031STed Kremenek state = state->bindLoc(loc::MemRegionVal(EleReg), V); 26410e25031STed Kremenek } else { 26510e25031STed Kremenek // Explicitly set to undefined, because currently we retrieve symbolic 26610e25031STed Kremenek // value from symbolic region. 26710e25031STed Kremenek state = state->bindLoc(loc::MemRegionVal(EleReg), UndefinedVal()); 26810e25031STed Kremenek } 26910e25031STed Kremenek } 270632e3b7eSTed Kremenek state = state->BindExpr(CNE, (*I)->getLocationContext(), 271632e3b7eSTed Kremenek loc::MemRegionVal(EleReg)); 272e594034fSAnna Zaks Bldr.generateNode(CNE, *I, state); 27310e25031STed Kremenek } 2746047f07eSSebastian Redl #endif 27510e25031STed Kremenek } 27610e25031STed Kremenek 27710e25031STed Kremenek void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE, 27810e25031STed Kremenek ExplodedNode *Pred, ExplodedNodeSet &Dst) { 27964aea652SErik Verbruggen StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 28064aea652SErik Verbruggen ProgramStateRef state = Pred->getState(); 28164aea652SErik Verbruggen Bldr.generateNode(CDE, Pred, state); 28210e25031STed Kremenek } 28310e25031STed Kremenek 2848fdb59f9STed Kremenek void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS, 2858fdb59f9STed Kremenek ExplodedNode *Pred, 2868fdb59f9STed Kremenek ExplodedNodeSet &Dst) { 2878fdb59f9STed Kremenek const VarDecl *VD = CS->getExceptionDecl(); 288b1279b5cSTed Kremenek if (!VD) { 289b1279b5cSTed Kremenek Dst.Add(Pred); 290b1279b5cSTed Kremenek return; 291b1279b5cSTed Kremenek } 292b1279b5cSTed Kremenek 2938fdb59f9STed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 2948fdb59f9STed Kremenek SVal V = svalBuilder.getConjuredSymbolVal(CS, LCtx, VD->getType(), 2958fdb59f9STed Kremenek currentBuilderContext->getCurrentBlockCount()); 2968fdb59f9STed Kremenek ProgramStateRef state = Pred->getState(); 2978fdb59f9STed Kremenek state = state->bindLoc(state->getLValue(VD, LCtx), V); 2988fdb59f9STed Kremenek 2998fdb59f9STed Kremenek StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 3008fdb59f9STed Kremenek Bldr.generateNode(CS, Pred, state); 3018fdb59f9STed Kremenek } 3028fdb59f9STed Kremenek 30310e25031STed Kremenek void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred, 30410e25031STed Kremenek ExplodedNodeSet &Dst) { 305e4e5712cSAnna Zaks StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 306e594034fSAnna Zaks 30710e25031STed Kremenek // Get the this object region from StoreManager. 308632e3b7eSTed Kremenek const LocationContext *LCtx = Pred->getLocationContext(); 30910e25031STed Kremenek const MemRegion *R = 31010e25031STed Kremenek svalBuilder.getRegionManager().getCXXThisRegion( 31110e25031STed Kremenek getContext().getCanonicalType(TE->getType()), 312632e3b7eSTed Kremenek LCtx); 31310e25031STed Kremenek 31449b1e38eSTed Kremenek ProgramStateRef state = Pred->getState(); 31510e25031STed Kremenek SVal V = state->getSVal(loc::MemRegionVal(R)); 316632e3b7eSTed Kremenek Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V)); 31710e25031STed Kremenek } 318