ExprEngineCXX.cpp 13 KB

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