1 //===- ExprEngineCXX.cpp - ExprEngine support for C++ -----------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the C++ expression evaluation engine. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/StaticAnalyzer/Core/CheckerManager.h" 15 #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" 16 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 17 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 18 #include "clang/AST/DeclCXX.h" 19 #include "clang/AST/StmtCXX.h" 20 21 using namespace clang; 22 using namespace ento; 23 24 void ExprEngine::CreateCXXTemporaryObject(const MaterializeTemporaryExpr *ME, 25 ExplodedNode *Pred, 26 ExplodedNodeSet &Dst) { 27 StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 28 const Expr *tempExpr = ME->GetTemporaryExpr()->IgnoreParens(); 29 ProgramStateRef state = Pred->getState(); 30 const LocationContext *LCtx = Pred->getLocationContext(); 31 32 // Bind the temporary object to the value of the expression. Then bind 33 // the expression to the location of the object. 34 SVal V = state->getSVal(tempExpr, Pred->getLocationContext()); 35 36 const MemRegion *R = 37 svalBuilder.getRegionManager().getCXXTempObjectRegion(ME, LCtx); 38 39 state = state->bindLoc(loc::MemRegionVal(R), V); 40 Bldr.generateNode(ME, Pred, state->BindExpr(ME, LCtx, loc::MemRegionVal(R))); 41 } 42 43 void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *CE, 44 ExplodedNode *Pred, 45 ExplodedNodeSet &destNodes) { 46 const LocationContext *LCtx = Pred->getLocationContext(); 47 ProgramStateRef State = Pred->getState(); 48 49 const MemRegion *Target = 0; 50 51 switch (CE->getConstructionKind()) { 52 case CXXConstructExpr::CK_Complete: { 53 // See if we're constructing an existing region by looking at the next 54 // element in the CFG. 55 const CFGBlock *B = currentBuilderContext->getBlock(); 56 if (currentStmtIdx + 1 < B->size()) { 57 CFGElement Next = (*B)[currentStmtIdx+1]; 58 59 // Is this a constructor for a local variable? 60 if (const CFGStmt *StmtElem = dyn_cast<CFGStmt>(&Next)) { 61 if (const DeclStmt *DS = dyn_cast<DeclStmt>(StmtElem->getStmt())) { 62 if (const VarDecl *Var = dyn_cast<VarDecl>(DS->getSingleDecl())) { 63 if (Var->getInit()->IgnoreImplicit() == CE) { 64 QualType Ty = Var->getType(); 65 if (const ArrayType *AT = getContext().getAsArrayType(Ty)) { 66 // FIXME: Handle arrays, which run the same constructor for 67 // every element. This workaround will just run the first 68 // constructor (which should still invalidate the entire array). 69 SVal Base = State->getLValue(Var, LCtx); 70 Target = State->getLValue(AT->getElementType(), 71 getSValBuilder().makeZeroArrayIndex(), 72 Base).getAsRegion(); 73 } else { 74 Target = State->getLValue(Var, LCtx).getAsRegion(); 75 } 76 } 77 } 78 } 79 } 80 81 // Is this a constructor for a member? 82 if (const CFGInitializer *InitElem = dyn_cast<CFGInitializer>(&Next)) { 83 const CXXCtorInitializer *Init = InitElem->getInitializer(); 84 assert(Init->isAnyMemberInitializer()); 85 86 const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl()); 87 Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor, 88 LCtx->getCurrentStackFrame()); 89 SVal ThisVal = State->getSVal(ThisPtr); 90 91 if (Init->isIndirectMemberInitializer()) { 92 SVal Field = State->getLValue(Init->getIndirectMember(), ThisVal); 93 Target = Field.getAsRegion(); 94 } else { 95 SVal Field = State->getLValue(Init->getMember(), ThisVal); 96 Target = Field.getAsRegion(); 97 } 98 } 99 100 // FIXME: This will eventually need to handle new-expressions as well. 101 } 102 103 // If we couldn't find an existing region to construct into, we'll just 104 // generate a symbolic region, which is fine. 105 106 break; 107 } 108 case CXXConstructExpr::CK_NonVirtualBase: 109 case CXXConstructExpr::CK_VirtualBase: 110 case CXXConstructExpr::CK_Delegating: { 111 const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl()); 112 Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor, 113 LCtx->getCurrentStackFrame()); 114 SVal ThisVal = State->getSVal(ThisPtr); 115 116 if (CE->getConstructionKind() == CXXConstructExpr::CK_Delegating) { 117 Target = ThisVal.getAsRegion(); 118 } else { 119 // Cast to the base type. 120 QualType BaseTy = CE->getType(); 121 SVal BaseVal = getStoreManager().evalDerivedToBase(ThisVal, BaseTy); 122 Target = BaseVal.getAsRegion(); 123 } 124 break; 125 } 126 } 127 128 CallEventManager &CEMgr = getStateManager().getCallEventManager(); 129 CallEventRef<CXXConstructorCall> Call = 130 CEMgr.getCXXConstructorCall(CE, Target, State, LCtx); 131 132 ExplodedNodeSet DstPreVisit; 133 getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, CE, *this); 134 ExplodedNodeSet DstPreCall; 135 getCheckerManager().runCheckersForPreCall(DstPreCall, DstPreVisit, 136 *Call, *this); 137 138 ExplodedNodeSet DstInvalidated; 139 StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currentBuilderContext); 140 for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 141 I != E; ++I) 142 defaultEvalCall(Bldr, *I, *Call); 143 144 ExplodedNodeSet DstPostCall; 145 getCheckerManager().runCheckersForPostCall(DstPostCall, DstInvalidated, 146 *Call, *this); 147 getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, CE, *this); 148 } 149 150 void ExprEngine::VisitCXXDestructor(QualType ObjectType, 151 const MemRegion *Dest, 152 const Stmt *S, 153 ExplodedNode *Pred, 154 ExplodedNodeSet &Dst) { 155 const LocationContext *LCtx = Pred->getLocationContext(); 156 ProgramStateRef State = Pred->getState(); 157 158 // FIXME: We need to run the same destructor on every element of the array. 159 // This workaround will just run the first destructor (which will still 160 // invalidate the entire array). 161 if (const ArrayType *AT = getContext().getAsArrayType(ObjectType)) { 162 ObjectType = AT->getElementType(); 163 Dest = State->getLValue(ObjectType, getSValBuilder().makeZeroArrayIndex(), 164 loc::MemRegionVal(Dest)).getAsRegion(); 165 } 166 167 const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl(); 168 assert(RecordDecl && "Only CXXRecordDecls should have destructors"); 169 const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor(); 170 171 CallEventManager &CEMgr = getStateManager().getCallEventManager(); 172 CallEventRef<CXXDestructorCall> Call = 173 CEMgr.getCXXDestructorCall(DtorDecl, S, Dest, State, LCtx); 174 175 ExplodedNodeSet DstPreCall; 176 getCheckerManager().runCheckersForPreCall(DstPreCall, Pred, 177 *Call, *this); 178 179 ExplodedNodeSet DstInvalidated; 180 StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currentBuilderContext); 181 for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end(); 182 I != E; ++I) 183 defaultEvalCall(Bldr, *I, *Call); 184 185 ExplodedNodeSet DstPostCall; 186 getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated, 187 *Call, *this); 188 } 189 190 void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred, 191 ExplodedNodeSet &Dst) { 192 // FIXME: Much of this should eventually migrate to CXXAllocatorCall. 193 // Also, we need to decide how allocators actually work -- they're not 194 // really part of the CXXNewExpr because they happen BEFORE the 195 // CXXConstructExpr subexpression. See PR12014 for some discussion. 196 StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 197 198 unsigned blockCount = currentBuilderContext->getCurrentBlockCount(); 199 const LocationContext *LCtx = Pred->getLocationContext(); 200 DefinedOrUnknownSVal symVal = 201 svalBuilder.getConjuredSymbolVal(0, CNE, LCtx, CNE->getType(), blockCount); 202 ProgramStateRef State = Pred->getState(); 203 204 CallEventManager &CEMgr = getStateManager().getCallEventManager(); 205 CallEventRef<CXXAllocatorCall> Call = 206 CEMgr.getCXXAllocatorCall(CNE, State, LCtx); 207 208 // Invalidate placement args. 209 // FIXME: Once we figure out how we want allocators to work, 210 // we should be using the usual pre-/(default-)eval-/post-call checks here. 211 State = Call->invalidateRegions(blockCount); 212 213 if (CNE->isArray()) { 214 // FIXME: allocating an array requires simulating the constructors. 215 // For now, just return a symbolicated region. 216 const MemRegion *NewReg = cast<loc::MemRegionVal>(symVal).getRegion(); 217 QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType(); 218 const ElementRegion *EleReg = 219 getStoreManager().GetElementZeroRegion(NewReg, ObjTy); 220 State = State->BindExpr(CNE, Pred->getLocationContext(), 221 loc::MemRegionVal(EleReg)); 222 Bldr.generateNode(CNE, Pred, State); 223 return; 224 } 225 226 // FIXME: Once we have proper support for CXXConstructExprs inside 227 // CXXNewExpr, we need to make sure that the constructed object is not 228 // immediately invalidated here. (The placement call should happen before 229 // the constructor call anyway.) 230 FunctionDecl *FD = CNE->getOperatorNew(); 231 if (FD && FD->isReservedGlobalPlacementOperator()) { 232 // Non-array placement new should always return the placement location. 233 SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx); 234 State = State->BindExpr(CNE, LCtx, PlacementLoc); 235 } else { 236 State = State->BindExpr(CNE, LCtx, symVal); 237 } 238 239 // If the type is not a record, we won't have a CXXConstructExpr as an 240 // initializer. Copy the value over. 241 if (const Expr *Init = CNE->getInitializer()) { 242 if (!isa<CXXConstructExpr>(Init)) { 243 QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType(); 244 (void)ObjTy; 245 assert(!ObjTy->isRecordType()); 246 SVal Location = State->getSVal(CNE, LCtx); 247 if (isa<Loc>(Location)) 248 State = State->bindLoc(cast<Loc>(Location), State->getSVal(Init, LCtx)); 249 } 250 } 251 252 Bldr.generateNode(CNE, Pred, State); 253 } 254 255 void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE, 256 ExplodedNode *Pred, ExplodedNodeSet &Dst) { 257 StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 258 ProgramStateRef state = Pred->getState(); 259 Bldr.generateNode(CDE, Pred, state); 260 } 261 262 void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS, 263 ExplodedNode *Pred, 264 ExplodedNodeSet &Dst) { 265 const VarDecl *VD = CS->getExceptionDecl(); 266 if (!VD) { 267 Dst.Add(Pred); 268 return; 269 } 270 271 const LocationContext *LCtx = Pred->getLocationContext(); 272 SVal V = svalBuilder.getConjuredSymbolVal(CS, LCtx, VD->getType(), 273 currentBuilderContext->getCurrentBlockCount()); 274 ProgramStateRef state = Pred->getState(); 275 state = state->bindLoc(state->getLValue(VD, LCtx), V); 276 277 StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 278 Bldr.generateNode(CS, Pred, state); 279 } 280 281 void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred, 282 ExplodedNodeSet &Dst) { 283 StmtNodeBuilder Bldr(Pred, Dst, *currentBuilderContext); 284 285 // Get the this object region from StoreManager. 286 const LocationContext *LCtx = Pred->getLocationContext(); 287 const MemRegion *R = 288 svalBuilder.getRegionManager().getCXXThisRegion( 289 getContext().getCanonicalType(TE->getType()), 290 LCtx); 291 292 ProgramStateRef state = Pred->getState(); 293 SVal V = state->getSVal(loc::MemRegionVal(R)); 294 Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V)); 295 } 296