ExprEngineCXX.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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. #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
  14. #include "clang/AST/DeclCXX.h"
  15. #include "clang/AST/StmtCXX.h"
  16. #include "clang/Basic/PrettyStackTrace.h"
  17. #include "clang/StaticAnalyzer/Core/CheckerManager.h"
  18. #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
  19. #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
  20. using namespace clang;
  21. using namespace ento;
  22. void ExprEngine::CreateCXXTemporaryObject(const MaterializeTemporaryExpr *ME,
  23. ExplodedNode *Pred,
  24. ExplodedNodeSet &Dst) {
  25. StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
  26. const Expr *tempExpr = ME->GetTemporaryExpr()->IgnoreParens();
  27. ProgramStateRef state = Pred->getState();
  28. const LocationContext *LCtx = Pred->getLocationContext();
  29. state = createTemporaryRegionIfNeeded(state, LCtx, tempExpr, ME);
  30. Bldr.generateNode(ME, Pred, state);
  31. }
  32. // FIXME: This is the sort of code that should eventually live in a Core
  33. // checker rather than as a special case in ExprEngine.
  34. void ExprEngine::performTrivialCopy(NodeBuilder &Bldr, ExplodedNode *Pred,
  35. const CallEvent &Call) {
  36. SVal ThisVal;
  37. bool AlwaysReturnsLValue;
  38. if (const CXXConstructorCall *Ctor = dyn_cast<CXXConstructorCall>(&Call)) {
  39. assert(Ctor->getDecl()->isTrivial());
  40. assert(Ctor->getDecl()->isCopyOrMoveConstructor());
  41. ThisVal = Ctor->getCXXThisVal();
  42. AlwaysReturnsLValue = false;
  43. } else {
  44. assert(cast<CXXMethodDecl>(Call.getDecl())->isTrivial());
  45. assert(cast<CXXMethodDecl>(Call.getDecl())->getOverloadedOperator() ==
  46. OO_Equal);
  47. ThisVal = cast<CXXInstanceCall>(Call).getCXXThisVal();
  48. AlwaysReturnsLValue = true;
  49. }
  50. const LocationContext *LCtx = Pred->getLocationContext();
  51. ExplodedNodeSet Dst;
  52. Bldr.takeNodes(Pred);
  53. SVal V = Call.getArgSVal(0);
  54. // If the value being copied is not unknown, load from its location to get
  55. // an aggregate rvalue.
  56. if (Optional<Loc> L = V.getAs<Loc>())
  57. V = Pred->getState()->getSVal(*L);
  58. else
  59. assert(V.isUnknownOrUndef());
  60. const Expr *CallExpr = Call.getOriginExpr();
  61. evalBind(Dst, CallExpr, Pred, ThisVal, V, true);
  62. PostStmt PS(CallExpr, LCtx);
  63. for (ExplodedNodeSet::iterator I = Dst.begin(), E = Dst.end();
  64. I != E; ++I) {
  65. ProgramStateRef State = (*I)->getState();
  66. if (AlwaysReturnsLValue)
  67. State = State->BindExpr(CallExpr, LCtx, ThisVal);
  68. else
  69. State = bindReturnValue(Call, LCtx, State);
  70. Bldr.generateNode(PS, State, *I);
  71. }
  72. }
  73. /// Returns a region representing the first element of a (possibly
  74. /// multi-dimensional) array.
  75. ///
  76. /// On return, \p Ty will be set to the base type of the array.
  77. ///
  78. /// If the type is not an array type at all, the original value is returned.
  79. static SVal makeZeroElementRegion(ProgramStateRef State, SVal LValue,
  80. QualType &Ty) {
  81. SValBuilder &SVB = State->getStateManager().getSValBuilder();
  82. ASTContext &Ctx = SVB.getContext();
  83. while (const ArrayType *AT = Ctx.getAsArrayType(Ty)) {
  84. Ty = AT->getElementType();
  85. LValue = State->getLValue(Ty, SVB.makeZeroArrayIndex(), LValue);
  86. }
  87. return LValue;
  88. }
  89. const MemRegion *
  90. ExprEngine::getRegionForConstructedObject(const CXXConstructExpr *CE,
  91. ExplodedNode *Pred) {
  92. const LocationContext *LCtx = Pred->getLocationContext();
  93. ProgramStateRef State = Pred->getState();
  94. // See if we're constructing an existing region by looking at the next
  95. // element in the CFG.
  96. if (auto Elem = findElementDirectlyInitializedByCurrentConstructor()) {
  97. if (Optional<CFGStmt> StmtElem = Elem->getAs<CFGStmt>()) {
  98. auto *DS = cast<DeclStmt>(StmtElem->getStmt());
  99. if (const auto *Var = dyn_cast<VarDecl>(DS->getSingleDecl())) {
  100. if (Var->getInit() && Var->getInit()->IgnoreImplicit() == CE) {
  101. SVal LValue = State->getLValue(Var, LCtx);
  102. QualType Ty = Var->getType();
  103. LValue = makeZeroElementRegion(State, LValue, Ty);
  104. return LValue.getAsRegion();
  105. }
  106. }
  107. } else if (Optional<CFGInitializer> InitElem = Elem->getAs<CFGInitializer>()) {
  108. const CXXCtorInitializer *Init = InitElem->getInitializer();
  109. assert(Init->isAnyMemberInitializer());
  110. const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
  111. Loc ThisPtr =
  112. getSValBuilder().getCXXThis(CurCtor, LCtx->getCurrentStackFrame());
  113. SVal ThisVal = State->getSVal(ThisPtr);
  114. const ValueDecl *Field;
  115. SVal FieldVal;
  116. if (Init->isIndirectMemberInitializer()) {
  117. Field = Init->getIndirectMember();
  118. FieldVal = State->getLValue(Init->getIndirectMember(), ThisVal);
  119. } else {
  120. Field = Init->getMember();
  121. FieldVal = State->getLValue(Init->getMember(), ThisVal);
  122. }
  123. QualType Ty = Field->getType();
  124. FieldVal = makeZeroElementRegion(State, FieldVal, Ty);
  125. return FieldVal.getAsRegion();
  126. }
  127. // FIXME: This will eventually need to handle new-expressions as well.
  128. // Don't forget to update the pre-constructor initialization code in
  129. // ExprEngine::VisitCXXConstructExpr.
  130. }
  131. // If we couldn't find an existing region to construct into, assume we're
  132. // constructing a temporary.
  133. MemRegionManager &MRMgr = getSValBuilder().getRegionManager();
  134. return MRMgr.getCXXTempObjectRegion(CE, LCtx);
  135. }
  136. /// Returns true if the initializer for \Elem can be a direct
  137. /// constructor.
  138. static bool canHaveDirectConstructor(CFGElement Elem){
  139. // DeclStmts and CXXCtorInitializers for fields can be directly constructed.
  140. if (Optional<CFGStmt> StmtElem = Elem.getAs<CFGStmt>()) {
  141. if (isa<DeclStmt>(StmtElem->getStmt())) {
  142. return true;
  143. }
  144. }
  145. if (Elem.getKind() == CFGElement::Initializer) {
  146. return true;
  147. }
  148. return false;
  149. }
  150. Optional<CFGElement>
  151. ExprEngine::findElementDirectlyInitializedByCurrentConstructor() {
  152. const NodeBuilderContext &CurrBldrCtx = getBuilderContext();
  153. // See if we're constructing an existing region by looking at the next
  154. // element in the CFG.
  155. const CFGBlock *B = CurrBldrCtx.getBlock();
  156. assert(isa<CXXConstructExpr>(((*B)[currStmtIdx]).castAs<CFGStmt>().getStmt()));
  157. unsigned int NextStmtIdx = currStmtIdx + 1;
  158. if (NextStmtIdx >= B->size())
  159. return None;
  160. CFGElement Next = (*B)[NextStmtIdx];
  161. // Is this a destructor? If so, we might be in the middle of an assignment
  162. // to a local or member: look ahead one more element to see what we find.
  163. while (Next.getAs<CFGImplicitDtor>() && NextStmtIdx + 1 < B->size()) {
  164. ++NextStmtIdx;
  165. Next = (*B)[NextStmtIdx];
  166. }
  167. if (canHaveDirectConstructor(Next))
  168. return Next;
  169. return None;
  170. }
  171. const CXXConstructExpr *
  172. ExprEngine::findDirectConstructorForCurrentCFGElement() {
  173. // Go backward in the CFG to see if the previous element (ignoring
  174. // destructors) was a CXXConstructExpr. If so, that constructor
  175. // was constructed directly into an existing region.
  176. // This process is essentially the inverse of that performed in
  177. // findElementDirectlyInitializedByCurrentConstructor().
  178. if (currStmtIdx == 0)
  179. return nullptr;
  180. const CFGBlock *B = getBuilderContext().getBlock();
  181. assert(canHaveDirectConstructor((*B)[currStmtIdx]));
  182. unsigned int PreviousStmtIdx = currStmtIdx - 1;
  183. CFGElement Previous = (*B)[PreviousStmtIdx];
  184. while (Previous.getAs<CFGImplicitDtor>() && PreviousStmtIdx > 0) {
  185. --PreviousStmtIdx;
  186. Previous = (*B)[PreviousStmtIdx];
  187. }
  188. if (Optional<CFGStmt> PrevStmtElem = Previous.getAs<CFGStmt>()) {
  189. if (auto *CtorExpr = dyn_cast<CXXConstructExpr>(PrevStmtElem->getStmt())) {
  190. return CtorExpr;
  191. }
  192. }
  193. return nullptr;
  194. }
  195. void ExprEngine::VisitCXXConstructExpr(const CXXConstructExpr *CE,
  196. ExplodedNode *Pred,
  197. ExplodedNodeSet &destNodes) {
  198. const LocationContext *LCtx = Pred->getLocationContext();
  199. ProgramStateRef State = Pred->getState();
  200. const MemRegion *Target = nullptr;
  201. // FIXME: Handle arrays, which run the same constructor for every element.
  202. // For now, we just run the first constructor (which should still invalidate
  203. // the entire array).
  204. switch (CE->getConstructionKind()) {
  205. case CXXConstructExpr::CK_Complete: {
  206. Target = getRegionForConstructedObject(CE, Pred);
  207. break;
  208. }
  209. case CXXConstructExpr::CK_VirtualBase:
  210. // Make sure we are not calling virtual base class initializers twice.
  211. // Only the most-derived object should initialize virtual base classes.
  212. if (const Stmt *Outer = LCtx->getCurrentStackFrame()->getCallSite()) {
  213. const CXXConstructExpr *OuterCtor = dyn_cast<CXXConstructExpr>(Outer);
  214. if (OuterCtor) {
  215. switch (OuterCtor->getConstructionKind()) {
  216. case CXXConstructExpr::CK_NonVirtualBase:
  217. case CXXConstructExpr::CK_VirtualBase:
  218. // Bail out!
  219. destNodes.Add(Pred);
  220. return;
  221. case CXXConstructExpr::CK_Complete:
  222. case CXXConstructExpr::CK_Delegating:
  223. break;
  224. }
  225. }
  226. }
  227. // FALLTHROUGH
  228. case CXXConstructExpr::CK_NonVirtualBase:
  229. case CXXConstructExpr::CK_Delegating: {
  230. const CXXMethodDecl *CurCtor = cast<CXXMethodDecl>(LCtx->getDecl());
  231. Loc ThisPtr = getSValBuilder().getCXXThis(CurCtor,
  232. LCtx->getCurrentStackFrame());
  233. SVal ThisVal = State->getSVal(ThisPtr);
  234. if (CE->getConstructionKind() == CXXConstructExpr::CK_Delegating) {
  235. Target = ThisVal.getAsRegion();
  236. } else {
  237. // Cast to the base type.
  238. bool IsVirtual =
  239. (CE->getConstructionKind() == CXXConstructExpr::CK_VirtualBase);
  240. SVal BaseVal = getStoreManager().evalDerivedToBase(ThisVal, CE->getType(),
  241. IsVirtual);
  242. Target = BaseVal.getAsRegion();
  243. }
  244. break;
  245. }
  246. }
  247. CallEventManager &CEMgr = getStateManager().getCallEventManager();
  248. CallEventRef<CXXConstructorCall> Call =
  249. CEMgr.getCXXConstructorCall(CE, Target, State, LCtx);
  250. ExplodedNodeSet DstPreVisit;
  251. getCheckerManager().runCheckersForPreStmt(DstPreVisit, Pred, CE, *this);
  252. ExplodedNodeSet PreInitialized;
  253. {
  254. StmtNodeBuilder Bldr(DstPreVisit, PreInitialized, *currBldrCtx);
  255. if (CE->requiresZeroInitialization()) {
  256. // Type of the zero doesn't matter.
  257. SVal ZeroVal = svalBuilder.makeZeroVal(getContext().CharTy);
  258. for (ExplodedNodeSet::iterator I = DstPreVisit.begin(),
  259. E = DstPreVisit.end();
  260. I != E; ++I) {
  261. ProgramStateRef State = (*I)->getState();
  262. // FIXME: Once we properly handle constructors in new-expressions, we'll
  263. // need to invalidate the region before setting a default value, to make
  264. // sure there aren't any lingering bindings around. This probably needs
  265. // to happen regardless of whether or not the object is zero-initialized
  266. // to handle random fields of a placement-initialized object picking up
  267. // old bindings. We might only want to do it when we need to, though.
  268. // FIXME: This isn't actually correct for arrays -- we need to zero-
  269. // initialize the entire array, not just the first element -- but our
  270. // handling of arrays everywhere else is weak as well, so this shouldn't
  271. // actually make things worse. Placement new makes this tricky as well,
  272. // since it's then possible to be initializing one part of a multi-
  273. // dimensional array.
  274. State = State->bindDefault(loc::MemRegionVal(Target), ZeroVal, LCtx);
  275. Bldr.generateNode(CE, *I, State, /*tag=*/nullptr,
  276. ProgramPoint::PreStmtKind);
  277. }
  278. }
  279. }
  280. ExplodedNodeSet DstPreCall;
  281. getCheckerManager().runCheckersForPreCall(DstPreCall, PreInitialized,
  282. *Call, *this);
  283. ExplodedNodeSet DstEvaluated;
  284. StmtNodeBuilder Bldr(DstPreCall, DstEvaluated, *currBldrCtx);
  285. bool IsArray = isa<ElementRegion>(Target);
  286. if (CE->getConstructor()->isTrivial() &&
  287. CE->getConstructor()->isCopyOrMoveConstructor() &&
  288. !IsArray) {
  289. // FIXME: Handle other kinds of trivial constructors as well.
  290. for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
  291. I != E; ++I)
  292. performTrivialCopy(Bldr, *I, *Call);
  293. } else {
  294. for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
  295. I != E; ++I)
  296. defaultEvalCall(Bldr, *I, *Call);
  297. }
  298. // If the CFG was contructed without elements for temporary destructors
  299. // and the just-called constructor created a temporary object then
  300. // stop exploration if the temporary object has a noreturn constructor.
  301. // This can lose coverage because the destructor, if it were present
  302. // in the CFG, would be called at the end of the full expression or
  303. // later (for life-time extended temporaries) -- but avoids infeasible
  304. // paths when no-return temporary destructors are used for assertions.
  305. const AnalysisDeclContext *ADC = LCtx->getAnalysisDeclContext();
  306. if (!ADC->getCFGBuildOptions().AddTemporaryDtors) {
  307. const MemRegion *Target = Call->getCXXThisVal().getAsRegion();
  308. if (Target && isa<CXXTempObjectRegion>(Target) &&
  309. Call->getDecl()->getParent()->isAnyDestructorNoReturn()) {
  310. for (ExplodedNode *N : DstEvaluated) {
  311. Bldr.generateSink(CE, N, N->getState());
  312. }
  313. // There is no need to run the PostCall and PostStmtchecker
  314. // callbacks because we just generated sinks on all nodes in th
  315. // frontier.
  316. return;
  317. }
  318. }
  319. ExplodedNodeSet DstPostCall;
  320. getCheckerManager().runCheckersForPostCall(DstPostCall, DstEvaluated,
  321. *Call, *this);
  322. getCheckerManager().runCheckersForPostStmt(destNodes, DstPostCall, CE, *this);
  323. }
  324. void ExprEngine::VisitCXXDestructor(QualType ObjectType,
  325. const MemRegion *Dest,
  326. const Stmt *S,
  327. bool IsBaseDtor,
  328. ExplodedNode *Pred,
  329. ExplodedNodeSet &Dst) {
  330. const LocationContext *LCtx = Pred->getLocationContext();
  331. ProgramStateRef State = Pred->getState();
  332. // FIXME: We need to run the same destructor on every element of the array.
  333. // This workaround will just run the first destructor (which will still
  334. // invalidate the entire array).
  335. SVal DestVal = UnknownVal();
  336. if (Dest)
  337. DestVal = loc::MemRegionVal(Dest);
  338. DestVal = makeZeroElementRegion(State, DestVal, ObjectType);
  339. Dest = DestVal.getAsRegion();
  340. const CXXRecordDecl *RecordDecl = ObjectType->getAsCXXRecordDecl();
  341. assert(RecordDecl && "Only CXXRecordDecls should have destructors");
  342. const CXXDestructorDecl *DtorDecl = RecordDecl->getDestructor();
  343. CallEventManager &CEMgr = getStateManager().getCallEventManager();
  344. CallEventRef<CXXDestructorCall> Call =
  345. CEMgr.getCXXDestructorCall(DtorDecl, S, Dest, IsBaseDtor, State, LCtx);
  346. PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
  347. Call->getSourceRange().getBegin(),
  348. "Error evaluating destructor");
  349. ExplodedNodeSet DstPreCall;
  350. getCheckerManager().runCheckersForPreCall(DstPreCall, Pred,
  351. *Call, *this);
  352. ExplodedNodeSet DstInvalidated;
  353. StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx);
  354. for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
  355. I != E; ++I)
  356. defaultEvalCall(Bldr, *I, *Call);
  357. ExplodedNodeSet DstPostCall;
  358. getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated,
  359. *Call, *this);
  360. }
  361. void ExprEngine::VisitCXXNewAllocatorCall(const CXXNewExpr *CNE,
  362. ExplodedNode *Pred,
  363. ExplodedNodeSet &Dst) {
  364. ProgramStateRef State = Pred->getState();
  365. const LocationContext *LCtx = Pred->getLocationContext();
  366. PrettyStackTraceLoc CrashInfo(getContext().getSourceManager(),
  367. CNE->getStartLoc(),
  368. "Error evaluating New Allocator Call");
  369. CallEventManager &CEMgr = getStateManager().getCallEventManager();
  370. CallEventRef<CXXAllocatorCall> Call =
  371. CEMgr.getCXXAllocatorCall(CNE, State, LCtx);
  372. ExplodedNodeSet DstPreCall;
  373. getCheckerManager().runCheckersForPreCall(DstPreCall, Pred,
  374. *Call, *this);
  375. ExplodedNodeSet DstInvalidated;
  376. StmtNodeBuilder Bldr(DstPreCall, DstInvalidated, *currBldrCtx);
  377. for (ExplodedNodeSet::iterator I = DstPreCall.begin(), E = DstPreCall.end();
  378. I != E; ++I)
  379. defaultEvalCall(Bldr, *I, *Call);
  380. getCheckerManager().runCheckersForPostCall(Dst, DstInvalidated,
  381. *Call, *this);
  382. }
  383. void ExprEngine::VisitCXXNewExpr(const CXXNewExpr *CNE, ExplodedNode *Pred,
  384. ExplodedNodeSet &Dst) {
  385. // FIXME: Much of this should eventually migrate to CXXAllocatorCall.
  386. // Also, we need to decide how allocators actually work -- they're not
  387. // really part of the CXXNewExpr because they happen BEFORE the
  388. // CXXConstructExpr subexpression. See PR12014 for some discussion.
  389. unsigned blockCount = currBldrCtx->blockCount();
  390. const LocationContext *LCtx = Pred->getLocationContext();
  391. DefinedOrUnknownSVal symVal = UnknownVal();
  392. FunctionDecl *FD = CNE->getOperatorNew();
  393. bool IsStandardGlobalOpNewFunction = false;
  394. if (FD && !isa<CXXMethodDecl>(FD) && !FD->isVariadic()) {
  395. if (FD->getNumParams() == 2) {
  396. QualType T = FD->getParamDecl(1)->getType();
  397. if (const IdentifierInfo *II = T.getBaseTypeIdentifier())
  398. // NoThrow placement new behaves as a standard new.
  399. IsStandardGlobalOpNewFunction = II->getName().equals("nothrow_t");
  400. }
  401. else
  402. // Placement forms are considered non-standard.
  403. IsStandardGlobalOpNewFunction = (FD->getNumParams() == 1);
  404. }
  405. // We assume all standard global 'operator new' functions allocate memory in
  406. // heap. We realize this is an approximation that might not correctly model
  407. // a custom global allocator.
  408. if (IsStandardGlobalOpNewFunction)
  409. symVal = svalBuilder.getConjuredHeapSymbolVal(CNE, LCtx, blockCount);
  410. else
  411. symVal = svalBuilder.conjureSymbolVal(nullptr, CNE, LCtx, CNE->getType(),
  412. blockCount);
  413. ProgramStateRef State = Pred->getState();
  414. CallEventManager &CEMgr = getStateManager().getCallEventManager();
  415. CallEventRef<CXXAllocatorCall> Call =
  416. CEMgr.getCXXAllocatorCall(CNE, State, LCtx);
  417. // Invalidate placement args.
  418. // FIXME: Once we figure out how we want allocators to work,
  419. // we should be using the usual pre-/(default-)eval-/post-call checks here.
  420. State = Call->invalidateRegions(blockCount);
  421. if (!State)
  422. return;
  423. // If this allocation function is not declared as non-throwing, failures
  424. // /must/ be signalled by exceptions, and thus the return value will never be
  425. // NULL. -fno-exceptions does not influence this semantics.
  426. // FIXME: GCC has a -fcheck-new option, which forces it to consider the case
  427. // where new can return NULL. If we end up supporting that option, we can
  428. // consider adding a check for it here.
  429. // C++11 [basic.stc.dynamic.allocation]p3.
  430. if (FD) {
  431. QualType Ty = FD->getType();
  432. if (const FunctionProtoType *ProtoType = Ty->getAs<FunctionProtoType>())
  433. if (!ProtoType->isNothrow(getContext()))
  434. State = State->assume(symVal, true);
  435. }
  436. StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
  437. if (CNE->isArray()) {
  438. // FIXME: allocating an array requires simulating the constructors.
  439. // For now, just return a symbolicated region.
  440. const MemRegion *NewReg = symVal.castAs<loc::MemRegionVal>().getRegion();
  441. QualType ObjTy = CNE->getType()->getAs<PointerType>()->getPointeeType();
  442. const ElementRegion *EleReg =
  443. getStoreManager().GetElementZeroRegion(NewReg, ObjTy);
  444. State = State->BindExpr(CNE, Pred->getLocationContext(),
  445. loc::MemRegionVal(EleReg));
  446. Bldr.generateNode(CNE, Pred, State);
  447. return;
  448. }
  449. // FIXME: Once we have proper support for CXXConstructExprs inside
  450. // CXXNewExpr, we need to make sure that the constructed object is not
  451. // immediately invalidated here. (The placement call should happen before
  452. // the constructor call anyway.)
  453. SVal Result = symVal;
  454. if (FD && FD->isReservedGlobalPlacementOperator()) {
  455. // Non-array placement new should always return the placement location.
  456. SVal PlacementLoc = State->getSVal(CNE->getPlacementArg(0), LCtx);
  457. Result = svalBuilder.evalCast(PlacementLoc, CNE->getType(),
  458. CNE->getPlacementArg(0)->getType());
  459. }
  460. // Bind the address of the object, then check to see if we cached out.
  461. State = State->BindExpr(CNE, LCtx, Result);
  462. ExplodedNode *NewN = Bldr.generateNode(CNE, Pred, State);
  463. if (!NewN)
  464. return;
  465. // If the type is not a record, we won't have a CXXConstructExpr as an
  466. // initializer. Copy the value over.
  467. if (const Expr *Init = CNE->getInitializer()) {
  468. if (!isa<CXXConstructExpr>(Init)) {
  469. assert(Bldr.getResults().size() == 1);
  470. Bldr.takeNodes(NewN);
  471. evalBind(Dst, CNE, NewN, Result, State->getSVal(Init, LCtx),
  472. /*FirstInit=*/IsStandardGlobalOpNewFunction);
  473. }
  474. }
  475. }
  476. void ExprEngine::VisitCXXDeleteExpr(const CXXDeleteExpr *CDE,
  477. ExplodedNode *Pred, ExplodedNodeSet &Dst) {
  478. StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
  479. ProgramStateRef state = Pred->getState();
  480. Bldr.generateNode(CDE, Pred, state);
  481. }
  482. void ExprEngine::VisitCXXCatchStmt(const CXXCatchStmt *CS,
  483. ExplodedNode *Pred,
  484. ExplodedNodeSet &Dst) {
  485. const VarDecl *VD = CS->getExceptionDecl();
  486. if (!VD) {
  487. Dst.Add(Pred);
  488. return;
  489. }
  490. const LocationContext *LCtx = Pred->getLocationContext();
  491. SVal V = svalBuilder.conjureSymbolVal(CS, LCtx, VD->getType(),
  492. currBldrCtx->blockCount());
  493. ProgramStateRef state = Pred->getState();
  494. state = state->bindLoc(state->getLValue(VD, LCtx), V, LCtx);
  495. StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
  496. Bldr.generateNode(CS, Pred, state);
  497. }
  498. void ExprEngine::VisitCXXThisExpr(const CXXThisExpr *TE, ExplodedNode *Pred,
  499. ExplodedNodeSet &Dst) {
  500. StmtNodeBuilder Bldr(Pred, Dst, *currBldrCtx);
  501. // Get the this object region from StoreManager.
  502. const LocationContext *LCtx = Pred->getLocationContext();
  503. const MemRegion *R =
  504. svalBuilder.getRegionManager().getCXXThisRegion(
  505. getContext().getCanonicalType(TE->getType()),
  506. LCtx);
  507. ProgramStateRef state = Pred->getState();
  508. SVal V = state->getSVal(loc::MemRegionVal(R));
  509. Bldr.generateNode(TE, Pred, state->BindExpr(TE, LCtx, V));
  510. }
  511. void ExprEngine::VisitLambdaExpr(const LambdaExpr *LE, ExplodedNode *Pred,
  512. ExplodedNodeSet &Dst) {
  513. const LocationContext *LocCtxt = Pred->getLocationContext();
  514. // Get the region of the lambda itself.
  515. const MemRegion *R = svalBuilder.getRegionManager().getCXXTempObjectRegion(
  516. LE, LocCtxt);
  517. SVal V = loc::MemRegionVal(R);
  518. ProgramStateRef State = Pred->getState();
  519. // If we created a new MemRegion for the lambda, we should explicitly bind
  520. // the captures.
  521. CXXRecordDecl::field_iterator CurField = LE->getLambdaClass()->field_begin();
  522. for (LambdaExpr::const_capture_init_iterator i = LE->capture_init_begin(),
  523. e = LE->capture_init_end();
  524. i != e; ++i, ++CurField) {
  525. FieldDecl *FieldForCapture = *CurField;
  526. SVal FieldLoc = State->getLValue(FieldForCapture, V);
  527. SVal InitVal;
  528. if (!FieldForCapture->hasCapturedVLAType()) {
  529. Expr *InitExpr = *i;
  530. assert(InitExpr && "Capture missing initialization expression");
  531. InitVal = State->getSVal(InitExpr, LocCtxt);
  532. } else {
  533. // The field stores the length of a captured variable-length array.
  534. // These captures don't have initialization expressions; instead we
  535. // get the length from the VLAType size expression.
  536. Expr *SizeExpr = FieldForCapture->getCapturedVLAType()->getSizeExpr();
  537. InitVal = State->getSVal(SizeExpr, LocCtxt);
  538. }
  539. State = State->bindLoc(FieldLoc, InitVal, LocCtxt);
  540. }
  541. // Decay the Loc into an RValue, because there might be a
  542. // MaterializeTemporaryExpr node above this one which expects the bound value
  543. // to be an RValue.
  544. SVal LambdaRVal = State->getSVal(R);
  545. ExplodedNodeSet Tmp;
  546. StmtNodeBuilder Bldr(Pred, Tmp, *currBldrCtx);
  547. // FIXME: is this the right program point kind?
  548. Bldr.generateNode(LE, Pred,
  549. State->BindExpr(LE, LocCtxt, LambdaRVal),
  550. nullptr, ProgramPoint::PostLValueKind);
  551. // FIXME: Move all post/pre visits to ::Visit().
  552. getCheckerManager().runCheckersForPostStmt(Dst, Tmp, LE, *this);
  553. }