ExplodedGraph.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. bool ExplodedNode::isTrivial() const {
  243. return pred_size() == 1 && succ_size() == 1 &&
  244. getFirstPred()->getState()->getID() == getState()->getID() &&
  245. getFirstPred()->succ_size() == 1;
  246. }
  247. const CFGBlock *ExplodedNode::getCFGBlock() const {
  248. ProgramPoint P = getLocation();
  249. if (auto BEP = P.getAs<BlockEntrance>())
  250. return BEP->getBlock();
  251. // Find the node's current statement in the CFG.
  252. // FIXME: getStmtForDiagnostics() does nasty things in order to provide
  253. // a valid statement for body farms, do we need this behavior here?
  254. if (const Stmt *S = getStmtForDiagnostics())
  255. return getLocationContext()
  256. ->getAnalysisDeclContext()
  257. ->getCFGStmtMap()
  258. ->getBlock(S);
  259. return nullptr;
  260. }
  261. static const LocationContext *
  262. findTopAutosynthesizedParentContext(const LocationContext *LC) {
  263. assert(LC->getAnalysisDeclContext()->isBodyAutosynthesized());
  264. const LocationContext *ParentLC = LC->getParent();
  265. assert(ParentLC && "We don't start analysis from autosynthesized code");
  266. while (ParentLC->getAnalysisDeclContext()->isBodyAutosynthesized()) {
  267. LC = ParentLC;
  268. ParentLC = LC->getParent();
  269. assert(ParentLC && "We don't start analysis from autosynthesized code");
  270. }
  271. return LC;
  272. }
  273. const Stmt *ExplodedNode::getStmtForDiagnostics() const {
  274. // We cannot place diagnostics on autosynthesized code.
  275. // Put them onto the call site through which we jumped into autosynthesized
  276. // code for the first time.
  277. const LocationContext *LC = getLocationContext();
  278. if (LC->getAnalysisDeclContext()->isBodyAutosynthesized()) {
  279. // It must be a stack frame because we only autosynthesize functions.
  280. return cast<StackFrameContext>(findTopAutosynthesizedParentContext(LC))
  281. ->getCallSite();
  282. }
  283. // Otherwise, see if the node's program point directly points to a statement.
  284. // FIXME: Refactor into a ProgramPoint method?
  285. ProgramPoint P = getLocation();
  286. if (auto SP = P.getAs<StmtPoint>())
  287. return SP->getStmt();
  288. if (auto BE = P.getAs<BlockEdge>())
  289. return BE->getSrc()->getTerminatorStmt();
  290. if (auto CE = P.getAs<CallEnter>())
  291. return CE->getCallExpr();
  292. if (auto CEE = P.getAs<CallExitEnd>())
  293. return CEE->getCalleeContext()->getCallSite();
  294. if (auto PIPP = P.getAs<PostInitializer>())
  295. return PIPP->getInitializer()->getInit();
  296. if (auto CEB = P.getAs<CallExitBegin>())
  297. return CEB->getReturnStmt();
  298. if (auto FEP = P.getAs<FunctionExitPoint>())
  299. return FEP->getStmt();
  300. return nullptr;
  301. }
  302. const Stmt *ExplodedNode::getNextStmtForDiagnostics() const {
  303. for (const ExplodedNode *N = getFirstSucc(); N; N = N->getFirstSucc()) {
  304. if (const Stmt *S = N->getStmtForDiagnostics()) {
  305. // Check if the statement is '?' or '&&'/'||'. These are "merges",
  306. // not actual statement points.
  307. switch (S->getStmtClass()) {
  308. case Stmt::ChooseExprClass:
  309. case Stmt::BinaryConditionalOperatorClass:
  310. case Stmt::ConditionalOperatorClass:
  311. continue;
  312. case Stmt::BinaryOperatorClass: {
  313. BinaryOperatorKind Op = cast<BinaryOperator>(S)->getOpcode();
  314. if (Op == BO_LAnd || Op == BO_LOr)
  315. continue;
  316. break;
  317. }
  318. default:
  319. break;
  320. }
  321. // We found the statement, so return it.
  322. return S;
  323. }
  324. }
  325. return nullptr;
  326. }
  327. const Stmt *ExplodedNode::getPreviousStmtForDiagnostics() const {
  328. for (const ExplodedNode *N = getFirstPred(); N; N = N->getFirstPred())
  329. if (const Stmt *S = N->getStmtForDiagnostics())
  330. return S;
  331. return nullptr;
  332. }
  333. const Stmt *ExplodedNode::getCurrentOrPreviousStmtForDiagnostics() const {
  334. if (const Stmt *S = getStmtForDiagnostics())
  335. return S;
  336. return getPreviousStmtForDiagnostics();
  337. }
  338. ExplodedNode *ExplodedGraph::getNode(const ProgramPoint &L,
  339. ProgramStateRef State,
  340. bool IsSink,
  341. bool* IsNew) {
  342. // Profile 'State' to determine if we already have an existing node.
  343. llvm::FoldingSetNodeID profile;
  344. void *InsertPos = nullptr;
  345. NodeTy::Profile(profile, L, State, IsSink);
  346. NodeTy* V = Nodes.FindNodeOrInsertPos(profile, InsertPos);
  347. if (!V) {
  348. if (!FreeNodes.empty()) {
  349. V = FreeNodes.back();
  350. FreeNodes.pop_back();
  351. }
  352. else {
  353. // Allocate a new node.
  354. V = (NodeTy*) getAllocator().Allocate<NodeTy>();
  355. }
  356. ++NumNodes;
  357. new (V) NodeTy(L, State, NumNodes, IsSink);
  358. if (ReclaimNodeInterval)
  359. ChangedNodes.push_back(V);
  360. // Insert the node into the node set and return it.
  361. Nodes.InsertNode(V, InsertPos);
  362. if (IsNew) *IsNew = true;
  363. }
  364. else
  365. if (IsNew) *IsNew = false;
  366. return V;
  367. }
  368. ExplodedNode *ExplodedGraph::createUncachedNode(const ProgramPoint &L,
  369. ProgramStateRef State,
  370. int64_t Id,
  371. bool IsSink) {
  372. NodeTy *V = (NodeTy *) getAllocator().Allocate<NodeTy>();
  373. new (V) NodeTy(L, State, Id, IsSink);
  374. return V;
  375. }
  376. std::unique_ptr<ExplodedGraph>
  377. ExplodedGraph::trim(ArrayRef<const NodeTy *> Sinks,
  378. InterExplodedGraphMap *ForwardMap,
  379. InterExplodedGraphMap *InverseMap) const {
  380. if (Nodes.empty())
  381. return nullptr;
  382. using Pass1Ty = llvm::DenseSet<const ExplodedNode *>;
  383. Pass1Ty Pass1;
  384. using Pass2Ty = InterExplodedGraphMap;
  385. InterExplodedGraphMap Pass2Scratch;
  386. Pass2Ty &Pass2 = ForwardMap ? *ForwardMap : Pass2Scratch;
  387. SmallVector<const ExplodedNode*, 10> WL1, WL2;
  388. // ===- Pass 1 (reverse DFS) -===
  389. for (const auto Sink : Sinks)
  390. if (Sink)
  391. WL1.push_back(Sink);
  392. // Process the first worklist until it is empty.
  393. while (!WL1.empty()) {
  394. const ExplodedNode *N = WL1.pop_back_val();
  395. // Have we already visited this node? If so, continue to the next one.
  396. if (!Pass1.insert(N).second)
  397. continue;
  398. // If this is a root enqueue it to the second worklist.
  399. if (N->Preds.empty()) {
  400. WL2.push_back(N);
  401. continue;
  402. }
  403. // Visit our predecessors and enqueue them.
  404. WL1.append(N->Preds.begin(), N->Preds.end());
  405. }
  406. // We didn't hit a root? Return with a null pointer for the new graph.
  407. if (WL2.empty())
  408. return nullptr;
  409. // Create an empty graph.
  410. std::unique_ptr<ExplodedGraph> G = MakeEmptyGraph();
  411. // ===- Pass 2 (forward DFS to construct the new graph) -===
  412. while (!WL2.empty()) {
  413. const ExplodedNode *N = WL2.pop_back_val();
  414. // Skip this node if we have already processed it.
  415. if (Pass2.find(N) != Pass2.end())
  416. continue;
  417. // Create the corresponding node in the new graph and record the mapping
  418. // from the old node to the new node.
  419. ExplodedNode *NewN = G->createUncachedNode(N->getLocation(), N->State,
  420. N->getID(), N->isSink());
  421. Pass2[N] = NewN;
  422. // Also record the reverse mapping from the new node to the old node.
  423. if (InverseMap) (*InverseMap)[NewN] = N;
  424. // If this node is a root, designate it as such in the graph.
  425. if (N->Preds.empty())
  426. G->addRoot(NewN);
  427. // In the case that some of the intended predecessors of NewN have already
  428. // been created, we should hook them up as predecessors.
  429. // Walk through the predecessors of 'N' and hook up their corresponding
  430. // nodes in the new graph (if any) to the freshly created node.
  431. for (ExplodedNode::pred_iterator I = N->Preds.begin(), E = N->Preds.end();
  432. I != E; ++I) {
  433. Pass2Ty::iterator PI = Pass2.find(*I);
  434. if (PI == Pass2.end())
  435. continue;
  436. NewN->addPredecessor(const_cast<ExplodedNode *>(PI->second), *G);
  437. }
  438. // In the case that some of the intended successors of NewN have already
  439. // been created, we should hook them up as successors. Otherwise, enqueue
  440. // the new nodes from the original graph that should have nodes created
  441. // in the new graph.
  442. for (ExplodedNode::succ_iterator I = N->Succs.begin(), E = N->Succs.end();
  443. I != E; ++I) {
  444. Pass2Ty::iterator PI = Pass2.find(*I);
  445. if (PI != Pass2.end()) {
  446. const_cast<ExplodedNode *>(PI->second)->addPredecessor(NewN, *G);
  447. continue;
  448. }
  449. // Enqueue nodes to the worklist that were marked during pass 1.
  450. if (Pass1.count(*I))
  451. WL2.push_back(*I);
  452. }
  453. }
  454. return G;
  455. }