ExplodedGraph.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. //=-- ExplodedGraph.cpp - Local, Path-Sens. "Exploded Graph" -*- 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 template classes ExplodedNode and ExplodedGraph,
  11. // which represent a path-sensitive, intra-procedural "exploded graph."
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h"
  15. #include "clang/AST/ParentMap.h"
  16. #include "clang/AST/Stmt.h"
  17. #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
  18. #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h"
  19. #include "llvm/ADT/DenseMap.h"
  20. #include "llvm/ADT/DenseSet.h"
  21. #include "llvm/ADT/SmallVector.h"
  22. #include "llvm/ADT/Statistic.h"
  23. #include <vector>
  24. using namespace clang;
  25. using namespace ento;
  26. //===----------------------------------------------------------------------===//
  27. // Node auditing.
  28. //===----------------------------------------------------------------------===//
  29. // An out of line virtual method to provide a home for the class vtable.
  30. ExplodedNode::Auditor::~Auditor() {}
  31. #ifndef NDEBUG
  32. static ExplodedNode::Auditor* NodeAuditor = nullptr;
  33. #endif
  34. void ExplodedNode::SetAuditor(ExplodedNode::Auditor* A) {
  35. #ifndef NDEBUG
  36. NodeAuditor = A;
  37. #endif
  38. }
  39. //===----------------------------------------------------------------------===//
  40. // Cleanup.
  41. //===----------------------------------------------------------------------===//
  42. ExplodedGraph::ExplodedGraph()
  43. : NumNodes(0), ReclaimNodeInterval(0) {}
  44. ExplodedGraph::~ExplodedGraph() {}
  45. //===----------------------------------------------------------------------===//
  46. // Node reclamation.
  47. //===----------------------------------------------------------------------===//
  48. bool ExplodedGraph::isInterestingLValueExpr(const Expr *Ex) {
  49. if (!Ex->isLValue())
  50. return false;
  51. return isa<DeclRefExpr>(Ex) ||
  52. isa<MemberExpr>(Ex) ||
  53. isa<ObjCIvarRefExpr>(Ex);
  54. }
  55. bool ExplodedGraph::shouldCollect(const ExplodedNode *node) {
  56. // First, we only consider nodes for reclamation of the following
  57. // conditions apply:
  58. //
  59. // (1) 1 predecessor (that has one successor)
  60. // (2) 1 successor (that has one predecessor)
  61. //
  62. // If a node has no successor it is on the "frontier", while a node
  63. // with no predecessor is a root.
  64. //
  65. // After these prerequisites, we discard all "filler" nodes that
  66. // are used only for intermediate processing, and are not essential
  67. // for analyzer history:
  68. //
  69. // (a) PreStmtPurgeDeadSymbols
  70. //
  71. // We then discard all other nodes where *all* of the following conditions
  72. // apply:
  73. //
  74. // (3) The ProgramPoint is for a PostStmt, but not a PostStore.
  75. // (4) There is no 'tag' for the ProgramPoint.
  76. // (5) The 'store' is the same as the predecessor.
  77. // (6) The 'GDM' is the same as the predecessor.
  78. // (7) The LocationContext is the same as the predecessor.
  79. // (8) Expressions that are *not* lvalue expressions.
  80. // (9) The PostStmt isn't for a non-consumed Stmt or Expr.
  81. // (10) The successor is neither a CallExpr StmtPoint nor a CallEnter or
  82. // PreImplicitCall (so that we would be able to find it when retrying a
  83. // call with no inlining).
  84. // FIXME: It may be safe to reclaim PreCall and PostCall nodes as well.
  85. // Conditions 1 and 2.
  86. if (node->pred_size() != 1 || node->succ_size() != 1)
  87. return false;
  88. const ExplodedNode *pred = *(node->pred_begin());
  89. if (pred->succ_size() != 1)
  90. return false;
  91. const ExplodedNode *succ = *(node->succ_begin());
  92. if (succ->pred_size() != 1)
  93. return false;
  94. // Now reclaim any nodes that are (by definition) not essential to
  95. // analysis history and are not consulted by any client code.
  96. ProgramPoint progPoint = node->getLocation();
  97. if (progPoint.getAs<PreStmtPurgeDeadSymbols>())
  98. return !progPoint.getTag();
  99. // Condition 3.
  100. if (!progPoint.getAs<PostStmt>() || progPoint.getAs<PostStore>())
  101. return false;
  102. // Condition 4.
  103. if (progPoint.getTag())
  104. return false;
  105. // Conditions 5, 6, and 7.
  106. ProgramStateRef state = node->getState();
  107. ProgramStateRef pred_state = pred->getState();
  108. if (state->store != pred_state->store || state->GDM != pred_state->GDM ||
  109. progPoint.getLocationContext() != pred->getLocationContext())
  110. return false;
  111. // All further checks require expressions. As per #3, we know that we have
  112. // a PostStmt.
  113. const Expr *Ex = dyn_cast<Expr>(progPoint.castAs<PostStmt>().getStmt());
  114. if (!Ex)
  115. return false;
  116. // Condition 8.
  117. // Do not collect nodes for "interesting" lvalue expressions since they are
  118. // used extensively for generating path diagnostics.
  119. if (isInterestingLValueExpr(Ex))
  120. return false;
  121. // Condition 9.
  122. // Do not collect nodes for non-consumed Stmt or Expr to ensure precise
  123. // diagnostic generation; specifically, so that we could anchor arrows
  124. // pointing to the beginning of statements (as written in code).
  125. ParentMap &PM = progPoint.getLocationContext()->getParentMap();
  126. if (!PM.isConsumedExpr(Ex))
  127. return false;
  128. // Condition 10.
  129. const ProgramPoint SuccLoc = succ->getLocation();
  130. if (Optional<StmtPoint> SP = SuccLoc.getAs<StmtPoint>())
  131. if (CallEvent::isCallStmt(SP->getStmt()))
  132. return false;
  133. // Condition 10, continuation.
  134. if (SuccLoc.getAs<CallEnter>() || SuccLoc.getAs<PreImplicitCall>())
  135. return false;
  136. return true;
  137. }
  138. void ExplodedGraph::collectNode(ExplodedNode *node) {
  139. // Removing a node means:
  140. // (a) changing the predecessors successor to the successor of this node
  141. // (b) changing the successors predecessor to the predecessor of this node
  142. // (c) Putting 'node' onto freeNodes.
  143. assert(node->pred_size() == 1 || node->succ_size() == 1);
  144. ExplodedNode *pred = *(node->pred_begin());
  145. ExplodedNode *succ = *(node->succ_begin());
  146. pred->replaceSuccessor(succ);
  147. succ->replacePredecessor(pred);
  148. FreeNodes.push_back(node);
  149. Nodes.RemoveNode(node);
  150. --NumNodes;
  151. node->~ExplodedNode();
  152. }
  153. void ExplodedGraph::reclaimRecentlyAllocatedNodes() {
  154. if (ChangedNodes.empty())
  155. return;
  156. // Only periodically reclaim nodes so that we can build up a set of
  157. // nodes that meet the reclamation criteria. Freshly created nodes
  158. // by definition have no successor, and thus cannot be reclaimed (see below).
  159. assert(ReclaimCounter > 0);
  160. if (--ReclaimCounter != 0)
  161. return;
  162. ReclaimCounter = ReclaimNodeInterval;
  163. for (NodeVector::iterator it = ChangedNodes.begin(), et = ChangedNodes.end();
  164. it != et; ++it) {
  165. ExplodedNode *node = *it;
  166. if (shouldCollect(node))
  167. collectNode(node);
  168. }
  169. ChangedNodes.clear();
  170. }
  171. //===----------------------------------------------------------------------===//
  172. // ExplodedNode.
  173. //===----------------------------------------------------------------------===//
  174. // An NodeGroup's storage type is actually very much like a TinyPtrVector:
  175. // it can be either a pointer to a single ExplodedNode, or a pointer to a
  176. // BumpVector allocated with the ExplodedGraph's allocator. This allows the
  177. // common case of single-node NodeGroups to be implemented with no extra memory.
  178. //
  179. // Consequently, each of the NodeGroup methods have up to four cases to handle:
  180. // 1. The flag is set and this group does not actually contain any nodes.
  181. // 2. The group is empty, in which case the storage value is null.
  182. // 3. The group contains a single node.
  183. // 4. The group contains more than one node.
  184. typedef BumpVector<ExplodedNode *> ExplodedNodeVector;
  185. typedef llvm::PointerUnion<ExplodedNode *, ExplodedNodeVector *> GroupStorage;
  186. void ExplodedNode::addPredecessor(ExplodedNode *V, ExplodedGraph &G) {
  187. assert (!V->isSink());
  188. Preds.addNode(V, G);
  189. V->Succs.addNode(this, G);
  190. #ifndef NDEBUG
  191. if (NodeAuditor) NodeAuditor->AddEdge(V, this);
  192. #endif
  193. }
  194. void ExplodedNode::NodeGroup::replaceNode(ExplodedNode *node) {
  195. assert(!getFlag());
  196. GroupStorage &Storage = reinterpret_cast<GroupStorage&>(P);
  197. assert(Storage.is<ExplodedNode *>());
  198. Storage = node;
  199. assert(Storage.is<ExplodedNode *>());
  200. }
  201. void ExplodedNode::NodeGroup::addNode(ExplodedNode *N, ExplodedGraph &G) {
  202. assert(!getFlag());
  203. GroupStorage &Storage = reinterpret_cast<GroupStorage&>(P);
  204. if (Storage.isNull()) {
  205. Storage = N;
  206. assert(Storage.is<ExplodedNode *>());
  207. return;
  208. }
  209. ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>();
  210. if (!V) {
  211. // Switch from single-node to multi-node representation.
  212. ExplodedNode *Old = Storage.get<ExplodedNode *>();
  213. BumpVectorContext &Ctx = G.getNodeAllocator();
  214. V = G.getAllocator().Allocate<ExplodedNodeVector>();
  215. new (V) ExplodedNodeVector(Ctx, 4);
  216. V->push_back(Old, Ctx);
  217. Storage = V;
  218. assert(!getFlag());
  219. assert(Storage.is<ExplodedNodeVector *>());
  220. }
  221. V->push_back(N, G.getNodeAllocator());
  222. }
  223. unsigned ExplodedNode::NodeGroup::size() const {
  224. if (getFlag())
  225. return 0;
  226. const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P);
  227. if (Storage.isNull())
  228. return 0;
  229. if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>())
  230. return V->size();
  231. return 1;
  232. }
  233. ExplodedNode * const *ExplodedNode::NodeGroup::begin() const {
  234. if (getFlag())
  235. return nullptr;
  236. const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P);
  237. if (Storage.isNull())
  238. return nullptr;
  239. if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>())
  240. return V->begin();
  241. return Storage.getAddrOfPtr1();
  242. }
  243. ExplodedNode * const *ExplodedNode::NodeGroup::end() const {
  244. if (getFlag())
  245. return nullptr;
  246. const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P);
  247. if (Storage.isNull())
  248. return nullptr;
  249. if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>())
  250. return V->end();
  251. return Storage.getAddrOfPtr1() + 1;
  252. }
  253. ExplodedNode *ExplodedGraph::getNode(const ProgramPoint &L,
  254. ProgramStateRef State,
  255. bool IsSink,
  256. bool* IsNew) {
  257. // Profile 'State' to determine if we already have an existing node.
  258. llvm::FoldingSetNodeID profile;
  259. void *InsertPos = nullptr;
  260. NodeTy::Profile(profile, L, State, IsSink);
  261. NodeTy* V = Nodes.FindNodeOrInsertPos(profile, InsertPos);
  262. if (!V) {
  263. if (!FreeNodes.empty()) {
  264. V = FreeNodes.back();
  265. FreeNodes.pop_back();
  266. }
  267. else {
  268. // Allocate a new node.
  269. V = (NodeTy*) getAllocator().Allocate<NodeTy>();
  270. }
  271. new (V) NodeTy(L, State, IsSink);
  272. if (ReclaimNodeInterval)
  273. ChangedNodes.push_back(V);
  274. // Insert the node into the node set and return it.
  275. Nodes.InsertNode(V, InsertPos);
  276. ++NumNodes;
  277. if (IsNew) *IsNew = true;
  278. }
  279. else
  280. if (IsNew) *IsNew = false;
  281. return V;
  282. }
  283. ExplodedGraph *
  284. ExplodedGraph::trim(ArrayRef<const NodeTy *> Sinks,
  285. InterExplodedGraphMap *ForwardMap,
  286. InterExplodedGraphMap *InverseMap) const{
  287. if (Nodes.empty())
  288. return nullptr;
  289. typedef llvm::DenseSet<const ExplodedNode*> Pass1Ty;
  290. Pass1Ty Pass1;
  291. typedef InterExplodedGraphMap Pass2Ty;
  292. InterExplodedGraphMap Pass2Scratch;
  293. Pass2Ty &Pass2 = ForwardMap ? *ForwardMap : Pass2Scratch;
  294. SmallVector<const ExplodedNode*, 10> WL1, WL2;
  295. // ===- Pass 1 (reverse DFS) -===
  296. for (ArrayRef<const NodeTy *>::iterator I = Sinks.begin(), E = Sinks.end();
  297. I != E; ++I) {
  298. if (*I)
  299. WL1.push_back(*I);
  300. }
  301. // Process the first worklist until it is empty.
  302. while (!WL1.empty()) {
  303. const ExplodedNode *N = WL1.pop_back_val();
  304. // Have we already visited this node? If so, continue to the next one.
  305. if (Pass1.count(N))
  306. continue;
  307. // Otherwise, mark this node as visited.
  308. Pass1.insert(N);
  309. // If this is a root enqueue it to the second worklist.
  310. if (N->Preds.empty()) {
  311. WL2.push_back(N);
  312. continue;
  313. }
  314. // Visit our predecessors and enqueue them.
  315. for (ExplodedNode::pred_iterator I = N->Preds.begin(), E = N->Preds.end();
  316. I != E; ++I)
  317. WL1.push_back(*I);
  318. }
  319. // We didn't hit a root? Return with a null pointer for the new graph.
  320. if (WL2.empty())
  321. return nullptr;
  322. // Create an empty graph.
  323. ExplodedGraph* G = MakeEmptyGraph();
  324. // ===- Pass 2 (forward DFS to construct the new graph) -===
  325. while (!WL2.empty()) {
  326. const ExplodedNode *N = WL2.pop_back_val();
  327. // Skip this node if we have already processed it.
  328. if (Pass2.find(N) != Pass2.end())
  329. continue;
  330. // Create the corresponding node in the new graph and record the mapping
  331. // from the old node to the new node.
  332. ExplodedNode *NewN = G->getNode(N->getLocation(), N->State, N->isSink(),
  333. nullptr);
  334. Pass2[N] = NewN;
  335. // Also record the reverse mapping from the new node to the old node.
  336. if (InverseMap) (*InverseMap)[NewN] = N;
  337. // If this node is a root, designate it as such in the graph.
  338. if (N->Preds.empty())
  339. G->addRoot(NewN);
  340. // In the case that some of the intended predecessors of NewN have already
  341. // been created, we should hook them up as predecessors.
  342. // Walk through the predecessors of 'N' and hook up their corresponding
  343. // nodes in the new graph (if any) to the freshly created node.
  344. for (ExplodedNode::pred_iterator I = N->Preds.begin(), E = N->Preds.end();
  345. I != E; ++I) {
  346. Pass2Ty::iterator PI = Pass2.find(*I);
  347. if (PI == Pass2.end())
  348. continue;
  349. NewN->addPredecessor(const_cast<ExplodedNode *>(PI->second), *G);
  350. }
  351. // In the case that some of the intended successors of NewN have already
  352. // been created, we should hook them up as successors. Otherwise, enqueue
  353. // the new nodes from the original graph that should have nodes created
  354. // in the new graph.
  355. for (ExplodedNode::succ_iterator I = N->Succs.begin(), E = N->Succs.end();
  356. I != E; ++I) {
  357. Pass2Ty::iterator PI = Pass2.find(*I);
  358. if (PI != Pass2.end()) {
  359. const_cast<ExplodedNode *>(PI->second)->addPredecessor(NewN, *G);
  360. continue;
  361. }
  362. // Enqueue nodes to the worklist that were marked during pass 1.
  363. if (Pass1.count(*I))
  364. WL2.push_back(*I);
  365. }
  366. }
  367. return G;
  368. }