ExplodedGraph.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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/PointerUnion.h"
  28. #include "llvm/ADT/SmallVector.h"
  29. #include "llvm/Support/Casting.h"
  30. #include <cassert>
  31. #include <memory>
  32. #include <optional>
  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, MemberExpr, ObjCIvarRefExpr, ArraySubscriptExpr>(Ex);
  47. }
  48. bool ExplodedGraph::shouldCollect(const ExplodedNode *node) {
  49. // First, we only consider nodes for reclamation of the following
  50. // conditions apply:
  51. //
  52. // (1) 1 predecessor (that has one successor)
  53. // (2) 1 successor (that has one predecessor)
  54. //
  55. // If a node has no successor it is on the "frontier", while a node
  56. // with no predecessor is a root.
  57. //
  58. // After these prerequisites, we discard all "filler" nodes that
  59. // are used only for intermediate processing, and are not essential
  60. // for analyzer history:
  61. //
  62. // (a) PreStmtPurgeDeadSymbols
  63. //
  64. // We then discard all other nodes where *all* of the following conditions
  65. // apply:
  66. //
  67. // (3) The ProgramPoint is for a PostStmt, but not a PostStore.
  68. // (4) There is no 'tag' for the ProgramPoint.
  69. // (5) The 'store' is the same as the predecessor.
  70. // (6) The 'GDM' is the same as the predecessor.
  71. // (7) The LocationContext is the same as the predecessor.
  72. // (8) Expressions that are *not* lvalue expressions.
  73. // (9) The PostStmt isn't for a non-consumed Stmt or Expr.
  74. // (10) The successor is neither a CallExpr StmtPoint nor a CallEnter or
  75. // PreImplicitCall (so that we would be able to find it when retrying a
  76. // call with no inlining).
  77. // FIXME: It may be safe to reclaim PreCall and PostCall nodes as well.
  78. // Conditions 1 and 2.
  79. if (node->pred_size() != 1 || node->succ_size() != 1)
  80. return false;
  81. const ExplodedNode *pred = *(node->pred_begin());
  82. if (pred->succ_size() != 1)
  83. return false;
  84. const ExplodedNode *succ = *(node->succ_begin());
  85. if (succ->pred_size() != 1)
  86. return false;
  87. // Now reclaim any nodes that are (by definition) not essential to
  88. // analysis history and are not consulted by any client code.
  89. ProgramPoint progPoint = node->getLocation();
  90. if (progPoint.getAs<PreStmtPurgeDeadSymbols>())
  91. return !progPoint.getTag();
  92. // Condition 3.
  93. if (!progPoint.getAs<PostStmt>() || progPoint.getAs<PostStore>())
  94. return false;
  95. // Condition 4.
  96. if (progPoint.getTag())
  97. return false;
  98. // Conditions 5, 6, and 7.
  99. ProgramStateRef state = node->getState();
  100. ProgramStateRef pred_state = pred->getState();
  101. if (state->store != pred_state->store || state->GDM != pred_state->GDM ||
  102. progPoint.getLocationContext() != pred->getLocationContext())
  103. return false;
  104. // All further checks require expressions. As per #3, we know that we have
  105. // a PostStmt.
  106. const Expr *Ex = dyn_cast<Expr>(progPoint.castAs<PostStmt>().getStmt());
  107. if (!Ex)
  108. return false;
  109. // Condition 8.
  110. // Do not collect nodes for "interesting" lvalue expressions since they are
  111. // used extensively for generating path diagnostics.
  112. if (isInterestingLValueExpr(Ex))
  113. return false;
  114. // Condition 9.
  115. // Do not collect nodes for non-consumed Stmt or Expr to ensure precise
  116. // diagnostic generation; specifically, so that we could anchor arrows
  117. // pointing to the beginning of statements (as written in code).
  118. const ParentMap &PM = progPoint.getLocationContext()->getParentMap();
  119. if (!PM.isConsumedExpr(Ex))
  120. return false;
  121. // Condition 10.
  122. const ProgramPoint SuccLoc = succ->getLocation();
  123. if (std::optional<StmtPoint> SP = SuccLoc.getAs<StmtPoint>())
  124. if (CallEvent::isCallStmt(SP->getStmt()))
  125. return false;
  126. // Condition 10, continuation.
  127. if (SuccLoc.getAs<CallEnter>() || SuccLoc.getAs<PreImplicitCall>())
  128. return false;
  129. return true;
  130. }
  131. void ExplodedGraph::collectNode(ExplodedNode *node) {
  132. // Removing a node means:
  133. // (a) changing the predecessors successor to the successor of this node
  134. // (b) changing the successors predecessor to the predecessor of this node
  135. // (c) Putting 'node' onto freeNodes.
  136. assert(node->pred_size() == 1 || node->succ_size() == 1);
  137. ExplodedNode *pred = *(node->pred_begin());
  138. ExplodedNode *succ = *(node->succ_begin());
  139. pred->replaceSuccessor(succ);
  140. succ->replacePredecessor(pred);
  141. FreeNodes.push_back(node);
  142. Nodes.RemoveNode(node);
  143. --NumNodes;
  144. node->~ExplodedNode();
  145. }
  146. void ExplodedGraph::reclaimRecentlyAllocatedNodes() {
  147. if (ChangedNodes.empty())
  148. return;
  149. // Only periodically reclaim nodes so that we can build up a set of
  150. // nodes that meet the reclamation criteria. Freshly created nodes
  151. // by definition have no successor, and thus cannot be reclaimed (see below).
  152. assert(ReclaimCounter > 0);
  153. if (--ReclaimCounter != 0)
  154. return;
  155. ReclaimCounter = ReclaimNodeInterval;
  156. for (const auto node : ChangedNodes)
  157. if (shouldCollect(node))
  158. collectNode(node);
  159. ChangedNodes.clear();
  160. }
  161. //===----------------------------------------------------------------------===//
  162. // ExplodedNode.
  163. //===----------------------------------------------------------------------===//
  164. // An NodeGroup's storage type is actually very much like a TinyPtrVector:
  165. // it can be either a pointer to a single ExplodedNode, or a pointer to a
  166. // BumpVector allocated with the ExplodedGraph's allocator. This allows the
  167. // common case of single-node NodeGroups to be implemented with no extra memory.
  168. //
  169. // Consequently, each of the NodeGroup methods have up to four cases to handle:
  170. // 1. The flag is set and this group does not actually contain any nodes.
  171. // 2. The group is empty, in which case the storage value is null.
  172. // 3. The group contains a single node.
  173. // 4. The group contains more than one node.
  174. using ExplodedNodeVector = BumpVector<ExplodedNode *>;
  175. using GroupStorage = llvm::PointerUnion<ExplodedNode *, ExplodedNodeVector *>;
  176. void ExplodedNode::addPredecessor(ExplodedNode *V, ExplodedGraph &G) {
  177. assert(!V->isSink());
  178. Preds.addNode(V, G);
  179. V->Succs.addNode(this, G);
  180. }
  181. void ExplodedNode::NodeGroup::replaceNode(ExplodedNode *node) {
  182. assert(!getFlag());
  183. GroupStorage &Storage = reinterpret_cast<GroupStorage&>(P);
  184. assert(Storage.is<ExplodedNode *>());
  185. Storage = node;
  186. assert(Storage.is<ExplodedNode *>());
  187. }
  188. void ExplodedNode::NodeGroup::addNode(ExplodedNode *N, ExplodedGraph &G) {
  189. assert(!getFlag());
  190. GroupStorage &Storage = reinterpret_cast<GroupStorage&>(P);
  191. if (Storage.isNull()) {
  192. Storage = N;
  193. assert(Storage.is<ExplodedNode *>());
  194. return;
  195. }
  196. ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>();
  197. if (!V) {
  198. // Switch from single-node to multi-node representation.
  199. ExplodedNode *Old = Storage.get<ExplodedNode *>();
  200. BumpVectorContext &Ctx = G.getNodeAllocator();
  201. V = G.getAllocator().Allocate<ExplodedNodeVector>();
  202. new (V) ExplodedNodeVector(Ctx, 4);
  203. V->push_back(Old, Ctx);
  204. Storage = V;
  205. assert(!getFlag());
  206. assert(Storage.is<ExplodedNodeVector *>());
  207. }
  208. V->push_back(N, G.getNodeAllocator());
  209. }
  210. unsigned ExplodedNode::NodeGroup::size() const {
  211. if (getFlag())
  212. return 0;
  213. const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P);
  214. if (Storage.isNull())
  215. return 0;
  216. if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>())
  217. return V->size();
  218. return 1;
  219. }
  220. ExplodedNode * const *ExplodedNode::NodeGroup::begin() const {
  221. if (getFlag())
  222. return nullptr;
  223. const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P);
  224. if (Storage.isNull())
  225. return nullptr;
  226. if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>())
  227. return V->begin();
  228. return Storage.getAddrOfPtr1();
  229. }
  230. ExplodedNode * const *ExplodedNode::NodeGroup::end() const {
  231. if (getFlag())
  232. return nullptr;
  233. const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P);
  234. if (Storage.isNull())
  235. return nullptr;
  236. if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>())
  237. return V->end();
  238. return Storage.getAddrOfPtr1() + 1;
  239. }
  240. bool ExplodedNode::isTrivial() const {
  241. return pred_size() == 1 && succ_size() == 1 &&
  242. getFirstPred()->getState()->getID() == getState()->getID() &&
  243. getFirstPred()->succ_size() == 1;
  244. }
  245. const CFGBlock *ExplodedNode::getCFGBlock() const {
  246. ProgramPoint P = getLocation();
  247. if (auto BEP = P.getAs<BlockEntrance>())
  248. return BEP->getBlock();
  249. // Find the node's current statement in the CFG.
  250. // FIXME: getStmtForDiagnostics() does nasty things in order to provide
  251. // a valid statement for body farms, do we need this behavior here?
  252. if (const Stmt *S = getStmtForDiagnostics())
  253. return getLocationContext()
  254. ->getAnalysisDeclContext()
  255. ->getCFGStmtMap()
  256. ->getBlock(S);
  257. return nullptr;
  258. }
  259. static const LocationContext *
  260. findTopAutosynthesizedParentContext(const LocationContext *LC) {
  261. assert(LC->getAnalysisDeclContext()->isBodyAutosynthesized());
  262. const LocationContext *ParentLC = LC->getParent();
  263. assert(ParentLC && "We don't start analysis from autosynthesized code");
  264. while (ParentLC->getAnalysisDeclContext()->isBodyAutosynthesized()) {
  265. LC = ParentLC;
  266. ParentLC = LC->getParent();
  267. assert(ParentLC && "We don't start analysis from autosynthesized code");
  268. }
  269. return LC;
  270. }
  271. const Stmt *ExplodedNode::getStmtForDiagnostics() const {
  272. // We cannot place diagnostics on autosynthesized code.
  273. // Put them onto the call site through which we jumped into autosynthesized
  274. // code for the first time.
  275. const LocationContext *LC = getLocationContext();
  276. if (LC->getAnalysisDeclContext()->isBodyAutosynthesized()) {
  277. // It must be a stack frame because we only autosynthesize functions.
  278. return cast<StackFrameContext>(findTopAutosynthesizedParentContext(LC))
  279. ->getCallSite();
  280. }
  281. // Otherwise, see if the node's program point directly points to a statement.
  282. // FIXME: Refactor into a ProgramPoint method?
  283. ProgramPoint P = getLocation();
  284. if (auto SP = P.getAs<StmtPoint>())
  285. return SP->getStmt();
  286. if (auto BE = P.getAs<BlockEdge>())
  287. return BE->getSrc()->getTerminatorStmt();
  288. if (auto CE = P.getAs<CallEnter>())
  289. return CE->getCallExpr();
  290. if (auto CEE = P.getAs<CallExitEnd>())
  291. return CEE->getCalleeContext()->getCallSite();
  292. if (auto PIPP = P.getAs<PostInitializer>())
  293. return PIPP->getInitializer()->getInit();
  294. if (auto CEB = P.getAs<CallExitBegin>())
  295. return CEB->getReturnStmt();
  296. if (auto FEP = P.getAs<FunctionExitPoint>())
  297. return FEP->getStmt();
  298. return nullptr;
  299. }
  300. const Stmt *ExplodedNode::getNextStmtForDiagnostics() const {
  301. for (const ExplodedNode *N = getFirstSucc(); N; N = N->getFirstSucc()) {
  302. if (const Stmt *S = N->getStmtForDiagnostics()) {
  303. // Check if the statement is '?' or '&&'/'||'. These are "merges",
  304. // not actual statement points.
  305. switch (S->getStmtClass()) {
  306. case Stmt::ChooseExprClass:
  307. case Stmt::BinaryConditionalOperatorClass:
  308. case Stmt::ConditionalOperatorClass:
  309. continue;
  310. case Stmt::BinaryOperatorClass: {
  311. BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode();
  312. if (Op == BO_LAnd || Op == BO_LOr)
  313. continue;
  314. break;
  315. }
  316. default:
  317. break;
  318. }
  319. // We found the statement, so return it.
  320. return S;
  321. }
  322. }
  323. return nullptr;
  324. }
  325. const Stmt *ExplodedNode::getPreviousStmtForDiagnostics() const {
  326. for (const ExplodedNode *N = getFirstPred(); N; N = N->getFirstPred())
  327. if (const Stmt *S = N->getStmtForDiagnostics())
  328. return S;
  329. return nullptr;
  330. }
  331. const Stmt *ExplodedNode::getCurrentOrPreviousStmtForDiagnostics() const {
  332. if (const Stmt *S = getStmtForDiagnostics())
  333. return S;
  334. return getPreviousStmtForDiagnostics();
  335. }
  336. ExplodedNode *ExplodedGraph::getNode(const ProgramPoint &L,
  337. ProgramStateRef State,
  338. bool IsSink,
  339. bool* IsNew) {
  340. // Profile 'State' to determine if we already have an existing node.
  341. llvm::FoldingSetNodeID profile;
  342. void *InsertPos = nullptr;
  343. NodeTy::Profile(profile, L, State, IsSink);
  344. NodeTy* V = Nodes.FindNodeOrInsertPos(profile, InsertPos);
  345. if (!V) {
  346. if (!FreeNodes.empty()) {
  347. V = FreeNodes.back();
  348. FreeNodes.pop_back();
  349. }
  350. else {
  351. // Allocate a new node.
  352. V = (NodeTy*) getAllocator().Allocate<NodeTy>();
  353. }
  354. ++NumNodes;
  355. new (V) NodeTy(L, State, NumNodes, IsSink);
  356. if (ReclaimNodeInterval)
  357. ChangedNodes.push_back(V);
  358. // Insert the node into the node set and return it.
  359. Nodes.InsertNode(V, InsertPos);
  360. if (IsNew) *IsNew = true;
  361. }
  362. else
  363. if (IsNew) *IsNew = false;
  364. return V;
  365. }
  366. ExplodedNode *ExplodedGraph::createUncachedNode(const ProgramPoint &L,
  367. ProgramStateRef State,
  368. int64_t Id,
  369. bool IsSink) {
  370. NodeTy *V = (NodeTy *) getAllocator().Allocate<NodeTy>();
  371. new (V) NodeTy(L, State, Id, IsSink);
  372. return V;
  373. }
  374. std::unique_ptr<ExplodedGraph>
  375. ExplodedGraph::trim(ArrayRef<const NodeTy *> Sinks,
  376. InterExplodedGraphMap *ForwardMap,
  377. InterExplodedGraphMap *InverseMap) const {
  378. if (Nodes.empty())
  379. return nullptr;
  380. using Pass1Ty = llvm::DenseSet<const ExplodedNode *>;
  381. Pass1Ty Pass1;
  382. using Pass2Ty = InterExplodedGraphMap;
  383. InterExplodedGraphMap Pass2Scratch;
  384. Pass2Ty &Pass2 = ForwardMap ? *ForwardMap : Pass2Scratch;
  385. SmallVector<const ExplodedNode*, 10> WL1, WL2;
  386. // ===- Pass 1 (reverse DFS) -===
  387. for (const auto Sink : Sinks)
  388. if (Sink)
  389. WL1.push_back(Sink);
  390. // Process the first worklist until it is empty.
  391. while (!WL1.empty()) {
  392. const ExplodedNode *N = WL1.pop_back_val();
  393. // Have we already visited this node? If so, continue to the next one.
  394. if (!Pass1.insert(N).second)
  395. continue;
  396. // If this is a root enqueue it to the second worklist.
  397. if (N->Preds.empty()) {
  398. WL2.push_back(N);
  399. continue;
  400. }
  401. // Visit our predecessors and enqueue them.
  402. WL1.append(N->Preds.begin(), N->Preds.end());
  403. }
  404. // We didn't hit a root? Return with a null pointer for the new graph.
  405. if (WL2.empty())
  406. return nullptr;
  407. // Create an empty graph.
  408. std::unique_ptr<ExplodedGraph> G = MakeEmptyGraph();
  409. // ===- Pass 2 (forward DFS to construct the new graph) -===
  410. while (!WL2.empty()) {
  411. const ExplodedNode *N = WL2.pop_back_val();
  412. // Skip this node if we have already processed it.
  413. if (Pass2.find(N) != Pass2.end())
  414. continue;
  415. // Create the corresponding node in the new graph and record the mapping
  416. // from the old node to the new node.
  417. ExplodedNode *NewN = G->createUncachedNode(N->getLocation(), N->State,
  418. N->getID(), N->isSink());
  419. Pass2[N] = NewN;
  420. // Also record the reverse mapping from the new node to the old node.
  421. if (InverseMap) (*InverseMap)[NewN] = N;
  422. // If this node is a root, designate it as such in the graph.
  423. if (N->Preds.empty())
  424. G->addRoot(NewN);
  425. // In the case that some of the intended predecessors of NewN have already
  426. // been created, we should hook them up as predecessors.
  427. // Walk through the predecessors of 'N' and hook up their corresponding
  428. // nodes in the new graph (if any) to the freshly created node.
  429. for (ExplodedNode::pred_iterator I = N->Preds.begin(), E = N->Preds.end();
  430. I != E; ++I) {
  431. Pass2Ty::iterator PI = Pass2.find(*I);
  432. if (PI == Pass2.end())
  433. continue;
  434. NewN->addPredecessor(const_cast<ExplodedNode *>(PI->second), *G);
  435. }
  436. // In the case that some of the intended successors of NewN have already
  437. // been created, we should hook them up as successors. Otherwise, enqueue
  438. // the new nodes from the original graph that should have nodes created
  439. // in the new graph.
  440. for (ExplodedNode::succ_iterator I = N->Succs.begin(), E = N->Succs.end();
  441. I != E; ++I) {
  442. Pass2Ty::iterator PI = Pass2.find(*I);
  443. if (PI != Pass2.end()) {
  444. const_cast<ExplodedNode *>(PI->second)->addPredecessor(NewN, *G);
  445. continue;
  446. }
  447. // Enqueue nodes to the worklist that were marked during pass 1.
  448. if (Pass1.count(*I))
  449. WL2.push_back(*I);
  450. }
  451. }
  452. return G;
  453. }