ExplodedGraph.cpp 18 KB

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