ExplodedGraph.cpp 15 KB

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