CoreEngine.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. //==- CoreEngine.cpp - Path-Sensitive Dataflow Engine ------------*- 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 a generic engine for intraprocedural, path-sensitive,
  11. // dataflow analysis via graph reachability engine.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
  15. #include "clang/StaticAnalyzer/Core/PathSensitive/CoreEngine.h"
  16. #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
  17. #include "clang/Index/TranslationUnit.h"
  18. #include "clang/AST/Expr.h"
  19. #include "clang/AST/StmtCXX.h"
  20. #include "llvm/Support/Casting.h"
  21. #include "llvm/ADT/DenseMap.h"
  22. using namespace clang;
  23. using namespace ento;
  24. //===----------------------------------------------------------------------===//
  25. // Worklist classes for exploration of reachable states.
  26. //===----------------------------------------------------------------------===//
  27. WorkList::Visitor::~Visitor() {}
  28. namespace {
  29. class DFS : public WorkList {
  30. SmallVector<WorkListUnit,20> Stack;
  31. public:
  32. virtual bool hasWork() const {
  33. return !Stack.empty();
  34. }
  35. virtual void enqueue(const WorkListUnit& U) {
  36. Stack.push_back(U);
  37. }
  38. virtual WorkListUnit dequeue() {
  39. assert (!Stack.empty());
  40. const WorkListUnit& U = Stack.back();
  41. Stack.pop_back(); // This technically "invalidates" U, but we are fine.
  42. return U;
  43. }
  44. virtual bool visitItemsInWorkList(Visitor &V) {
  45. for (SmallVectorImpl<WorkListUnit>::iterator
  46. I = Stack.begin(), E = Stack.end(); I != E; ++I) {
  47. if (V.visit(*I))
  48. return true;
  49. }
  50. return false;
  51. }
  52. };
  53. class BFS : public WorkList {
  54. std::deque<WorkListUnit> Queue;
  55. public:
  56. virtual bool hasWork() const {
  57. return !Queue.empty();
  58. }
  59. virtual void enqueue(const WorkListUnit& U) {
  60. Queue.push_front(U);
  61. }
  62. virtual WorkListUnit dequeue() {
  63. WorkListUnit U = Queue.front();
  64. Queue.pop_front();
  65. return U;
  66. }
  67. virtual bool visitItemsInWorkList(Visitor &V) {
  68. for (std::deque<WorkListUnit>::iterator
  69. I = Queue.begin(), E = Queue.end(); I != E; ++I) {
  70. if (V.visit(*I))
  71. return true;
  72. }
  73. return false;
  74. }
  75. };
  76. } // end anonymous namespace
  77. // Place the dstor for WorkList here because it contains virtual member
  78. // functions, and we the code for the dstor generated in one compilation unit.
  79. WorkList::~WorkList() {}
  80. WorkList *WorkList::makeDFS() { return new DFS(); }
  81. WorkList *WorkList::makeBFS() { return new BFS(); }
  82. namespace {
  83. class BFSBlockDFSContents : public WorkList {
  84. std::deque<WorkListUnit> Queue;
  85. SmallVector<WorkListUnit,20> Stack;
  86. public:
  87. virtual bool hasWork() const {
  88. return !Queue.empty() || !Stack.empty();
  89. }
  90. virtual void enqueue(const WorkListUnit& U) {
  91. if (isa<BlockEntrance>(U.getNode()->getLocation()))
  92. Queue.push_front(U);
  93. else
  94. Stack.push_back(U);
  95. }
  96. virtual WorkListUnit dequeue() {
  97. // Process all basic blocks to completion.
  98. if (!Stack.empty()) {
  99. const WorkListUnit& U = Stack.back();
  100. Stack.pop_back(); // This technically "invalidates" U, but we are fine.
  101. return U;
  102. }
  103. assert(!Queue.empty());
  104. // Don't use const reference. The subsequent pop_back() might make it
  105. // unsafe.
  106. WorkListUnit U = Queue.front();
  107. Queue.pop_front();
  108. return U;
  109. }
  110. virtual bool visitItemsInWorkList(Visitor &V) {
  111. for (SmallVectorImpl<WorkListUnit>::iterator
  112. I = Stack.begin(), E = Stack.end(); I != E; ++I) {
  113. if (V.visit(*I))
  114. return true;
  115. }
  116. for (std::deque<WorkListUnit>::iterator
  117. I = Queue.begin(), E = Queue.end(); I != E; ++I) {
  118. if (V.visit(*I))
  119. return true;
  120. }
  121. return false;
  122. }
  123. };
  124. } // end anonymous namespace
  125. WorkList* WorkList::makeBFSBlockDFSContents() {
  126. return new BFSBlockDFSContents();
  127. }
  128. //===----------------------------------------------------------------------===//
  129. // Core analysis engine.
  130. //===----------------------------------------------------------------------===//
  131. /// ExecuteWorkList - Run the worklist algorithm for a maximum number of steps.
  132. bool CoreEngine::ExecuteWorkList(const LocationContext *L, unsigned Steps,
  133. ProgramStateRef InitState) {
  134. if (G->num_roots() == 0) { // Initialize the analysis by constructing
  135. // the root if none exists.
  136. const CFGBlock *Entry = &(L->getCFG()->getEntry());
  137. assert (Entry->empty() &&
  138. "Entry block must be empty.");
  139. assert (Entry->succ_size() == 1 &&
  140. "Entry block must have 1 successor.");
  141. // Get the solitary successor.
  142. const CFGBlock *Succ = *(Entry->succ_begin());
  143. // Construct an edge representing the
  144. // starting location in the function.
  145. BlockEdge StartLoc(Entry, Succ, L);
  146. // Set the current block counter to being empty.
  147. WList->setBlockCounter(BCounterFactory.GetEmptyCounter());
  148. if (!InitState)
  149. // Generate the root.
  150. generateNode(StartLoc, SubEng.getInitialState(L), 0);
  151. else
  152. generateNode(StartLoc, InitState, 0);
  153. }
  154. // Check if we have a steps limit
  155. bool UnlimitedSteps = Steps == 0;
  156. while (WList->hasWork()) {
  157. if (!UnlimitedSteps) {
  158. if (Steps == 0)
  159. break;
  160. --Steps;
  161. }
  162. const WorkListUnit& WU = WList->dequeue();
  163. // Set the current block counter.
  164. WList->setBlockCounter(WU.getBlockCounter());
  165. // Retrieve the node.
  166. ExplodedNode *Node = WU.getNode();
  167. // Dispatch on the location type.
  168. switch (Node->getLocation().getKind()) {
  169. case ProgramPoint::BlockEdgeKind:
  170. HandleBlockEdge(cast<BlockEdge>(Node->getLocation()), Node);
  171. break;
  172. case ProgramPoint::BlockEntranceKind:
  173. HandleBlockEntrance(cast<BlockEntrance>(Node->getLocation()), Node);
  174. break;
  175. case ProgramPoint::BlockExitKind:
  176. assert (false && "BlockExit location never occur in forward analysis.");
  177. break;
  178. case ProgramPoint::CallEnterKind:
  179. SubEng.processCallEnter(cast<CallEnter>(Node->getLocation()), Node);
  180. break;
  181. case ProgramPoint::CallExitKind:
  182. SubEng.processCallExit(Node);
  183. break;
  184. default:
  185. assert(isa<PostStmt>(Node->getLocation()) ||
  186. isa<PostInitializer>(Node->getLocation()));
  187. HandlePostStmt(WU.getBlock(), WU.getIndex(), Node);
  188. break;
  189. }
  190. }
  191. SubEng.processEndWorklist(hasWorkRemaining());
  192. return WList->hasWork();
  193. }
  194. void CoreEngine::ExecuteWorkListWithInitialState(const LocationContext *L,
  195. unsigned Steps,
  196. ProgramStateRef InitState,
  197. ExplodedNodeSet &Dst) {
  198. ExecuteWorkList(L, Steps, InitState);
  199. for (ExplodedGraph::eop_iterator I = G->eop_begin(),
  200. E = G->eop_end(); I != E; ++I) {
  201. Dst.Add(*I);
  202. }
  203. }
  204. void CoreEngine::HandleBlockEdge(const BlockEdge &L, ExplodedNode *Pred) {
  205. const CFGBlock *Blk = L.getDst();
  206. NodeBuilderContext BuilderCtx(*this, Blk, Pred);
  207. // Check if we are entering the EXIT block.
  208. if (Blk == &(L.getLocationContext()->getCFG()->getExit())) {
  209. assert (L.getLocationContext()->getCFG()->getExit().size() == 0
  210. && "EXIT block cannot contain Stmts.");
  211. // Process the final state transition.
  212. SubEng.processEndOfFunction(BuilderCtx);
  213. // This path is done. Don't enqueue any more nodes.
  214. return;
  215. }
  216. // Call into the SubEngine to process entering the CFGBlock.
  217. ExplodedNodeSet dstNodes;
  218. BlockEntrance BE(Blk, Pred->getLocationContext());
  219. NodeBuilderWithSinks nodeBuilder(Pred, dstNodes, BuilderCtx, BE);
  220. SubEng.processCFGBlockEntrance(nodeBuilder);
  221. // Auto-generate a node.
  222. if (!nodeBuilder.hasGeneratedNodes()) {
  223. nodeBuilder.generateNode(Pred->State, Pred);
  224. }
  225. // Enqueue nodes onto the worklist.
  226. enqueue(dstNodes);
  227. // Make sink nodes as exhausted.
  228. const SmallVectorImpl<ExplodedNode*> &Sinks = nodeBuilder.getSinks();
  229. for (SmallVectorImpl<ExplodedNode*>::const_iterator
  230. I =Sinks.begin(), E = Sinks.end(); I != E; ++I) {
  231. blocksExhausted.push_back(std::make_pair(L, *I));
  232. }
  233. }
  234. void CoreEngine::HandleBlockEntrance(const BlockEntrance &L,
  235. ExplodedNode *Pred) {
  236. // Increment the block counter.
  237. BlockCounter Counter = WList->getBlockCounter();
  238. Counter = BCounterFactory.IncrementCount(Counter,
  239. Pred->getLocationContext()->getCurrentStackFrame(),
  240. L.getBlock()->getBlockID());
  241. WList->setBlockCounter(Counter);
  242. // Process the entrance of the block.
  243. if (CFGElement E = L.getFirstElement()) {
  244. NodeBuilderContext Ctx(*this, L.getBlock(), Pred);
  245. SubEng.processCFGElement(E, Pred, 0, &Ctx);
  246. }
  247. else
  248. HandleBlockExit(L.getBlock(), Pred);
  249. }
  250. void CoreEngine::HandleBlockExit(const CFGBlock * B, ExplodedNode *Pred) {
  251. if (const Stmt *Term = B->getTerminator()) {
  252. switch (Term->getStmtClass()) {
  253. default:
  254. llvm_unreachable("Analysis for this terminator not implemented.");
  255. case Stmt::BinaryOperatorClass: // '&&' and '||'
  256. HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred);
  257. return;
  258. case Stmt::BinaryConditionalOperatorClass:
  259. case Stmt::ConditionalOperatorClass:
  260. HandleBranch(cast<AbstractConditionalOperator>(Term)->getCond(),
  261. Term, B, Pred);
  262. return;
  263. // FIXME: Use constant-folding in CFG construction to simplify this
  264. // case.
  265. case Stmt::ChooseExprClass:
  266. HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred);
  267. return;
  268. case Stmt::DoStmtClass:
  269. HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred);
  270. return;
  271. case Stmt::CXXForRangeStmtClass:
  272. HandleBranch(cast<CXXForRangeStmt>(Term)->getCond(), Term, B, Pred);
  273. return;
  274. case Stmt::ForStmtClass:
  275. HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred);
  276. return;
  277. case Stmt::ContinueStmtClass:
  278. case Stmt::BreakStmtClass:
  279. case Stmt::GotoStmtClass:
  280. break;
  281. case Stmt::IfStmtClass:
  282. HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred);
  283. return;
  284. case Stmt::IndirectGotoStmtClass: {
  285. // Only 1 successor: the indirect goto dispatch block.
  286. assert (B->succ_size() == 1);
  287. IndirectGotoNodeBuilder
  288. builder(Pred, B, cast<IndirectGotoStmt>(Term)->getTarget(),
  289. *(B->succ_begin()), this);
  290. SubEng.processIndirectGoto(builder);
  291. return;
  292. }
  293. case Stmt::ObjCForCollectionStmtClass: {
  294. // In the case of ObjCForCollectionStmt, it appears twice in a CFG:
  295. //
  296. // (1) inside a basic block, which represents the binding of the
  297. // 'element' variable to a value.
  298. // (2) in a terminator, which represents the branch.
  299. //
  300. // For (1), subengines will bind a value (i.e., 0 or 1) indicating
  301. // whether or not collection contains any more elements. We cannot
  302. // just test to see if the element is nil because a container can
  303. // contain nil elements.
  304. HandleBranch(Term, Term, B, Pred);
  305. return;
  306. }
  307. case Stmt::SwitchStmtClass: {
  308. SwitchNodeBuilder builder(Pred, B, cast<SwitchStmt>(Term)->getCond(),
  309. this);
  310. SubEng.processSwitch(builder);
  311. return;
  312. }
  313. case Stmt::WhileStmtClass:
  314. HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred);
  315. return;
  316. }
  317. }
  318. assert (B->succ_size() == 1 &&
  319. "Blocks with no terminator should have at most 1 successor.");
  320. generateNode(BlockEdge(B, *(B->succ_begin()), Pred->getLocationContext()),
  321. Pred->State, Pred);
  322. }
  323. void CoreEngine::HandleBranch(const Stmt *Cond, const Stmt *Term,
  324. const CFGBlock * B, ExplodedNode *Pred) {
  325. assert(B->succ_size() == 2);
  326. NodeBuilderContext Ctx(*this, B, Pred);
  327. ExplodedNodeSet Dst;
  328. SubEng.processBranch(Cond, Term, Ctx, Pred, Dst,
  329. *(B->succ_begin()), *(B->succ_begin()+1));
  330. // Enqueue the new frontier onto the worklist.
  331. enqueue(Dst);
  332. }
  333. void CoreEngine::HandlePostStmt(const CFGBlock *B, unsigned StmtIdx,
  334. ExplodedNode *Pred) {
  335. assert(B);
  336. assert(!B->empty());
  337. if (StmtIdx == B->size())
  338. HandleBlockExit(B, Pred);
  339. else {
  340. NodeBuilderContext Ctx(*this, B, Pred);
  341. SubEng.processCFGElement((*B)[StmtIdx], Pred, StmtIdx, &Ctx);
  342. }
  343. }
  344. /// generateNode - Utility method to generate nodes, hook up successors,
  345. /// and add nodes to the worklist.
  346. void CoreEngine::generateNode(const ProgramPoint &Loc,
  347. ProgramStateRef State,
  348. ExplodedNode *Pred) {
  349. bool IsNew;
  350. ExplodedNode *Node = G->getNode(Loc, State, false, &IsNew);
  351. if (Pred)
  352. Node->addPredecessor(Pred, *G); // Link 'Node' with its predecessor.
  353. else {
  354. assert (IsNew);
  355. G->addRoot(Node); // 'Node' has no predecessor. Make it a root.
  356. }
  357. // Only add 'Node' to the worklist if it was freshly generated.
  358. if (IsNew) WList->enqueue(Node);
  359. }
  360. void CoreEngine::enqueueStmtNode(ExplodedNode *N,
  361. const CFGBlock *Block, unsigned Idx) {
  362. assert(Block);
  363. assert (!N->isSink());
  364. // Check if this node entered a callee.
  365. if (isa<CallEnter>(N->getLocation())) {
  366. // Still use the index of the CallExpr. It's needed to create the callee
  367. // StackFrameContext.
  368. WList->enqueue(N, Block, Idx);
  369. return;
  370. }
  371. // Do not create extra nodes. Move to the next CFG element.
  372. if (isa<PostInitializer>(N->getLocation())) {
  373. WList->enqueue(N, Block, Idx+1);
  374. return;
  375. }
  376. const CFGStmt *CS = (*Block)[Idx].getAs<CFGStmt>();
  377. const Stmt *St = CS ? CS->getStmt() : 0;
  378. PostStmt Loc(St, N->getLocationContext());
  379. if (Loc == N->getLocation()) {
  380. // Note: 'N' should be a fresh node because otherwise it shouldn't be
  381. // a member of Deferred.
  382. WList->enqueue(N, Block, Idx+1);
  383. return;
  384. }
  385. bool IsNew;
  386. ExplodedNode *Succ = G->getNode(Loc, N->getState(), false, &IsNew);
  387. Succ->addPredecessor(N, *G);
  388. if (IsNew)
  389. WList->enqueue(Succ, Block, Idx+1);
  390. }
  391. ExplodedNode *CoreEngine::generateCallExitNode(ExplodedNode *N) {
  392. // Create a CallExit node and enqueue it.
  393. const StackFrameContext *LocCtx
  394. = cast<StackFrameContext>(N->getLocationContext());
  395. const Stmt *CE = LocCtx->getCallSite();
  396. // Use the the callee location context.
  397. CallExit Loc(CE, LocCtx);
  398. bool isNew;
  399. ExplodedNode *Node = G->getNode(Loc, N->getState(), false, &isNew);
  400. Node->addPredecessor(N, *G);
  401. return isNew ? Node : 0;
  402. }
  403. void CoreEngine::enqueue(ExplodedNodeSet &Set) {
  404. for (ExplodedNodeSet::iterator I = Set.begin(),
  405. E = Set.end(); I != E; ++I) {
  406. WList->enqueue(*I);
  407. }
  408. }
  409. void CoreEngine::enqueue(ExplodedNodeSet &Set,
  410. const CFGBlock *Block, unsigned Idx) {
  411. for (ExplodedNodeSet::iterator I = Set.begin(),
  412. E = Set.end(); I != E; ++I) {
  413. enqueueStmtNode(*I, Block, Idx);
  414. }
  415. }
  416. void CoreEngine::enqueueEndOfFunction(ExplodedNodeSet &Set) {
  417. for (ExplodedNodeSet::iterator I = Set.begin(), E = Set.end(); I != E; ++I) {
  418. ExplodedNode *N = *I;
  419. // If we are in an inlined call, generate CallExit node.
  420. if (N->getLocationContext()->getParent()) {
  421. N = generateCallExitNode(N);
  422. if (N)
  423. WList->enqueue(N);
  424. } else
  425. G->addEndOfPath(N);
  426. }
  427. }
  428. void NodeBuilder::anchor() { }
  429. ExplodedNode* NodeBuilder::generateNodeImpl(const ProgramPoint &Loc,
  430. ProgramStateRef State,
  431. ExplodedNode *FromN,
  432. bool MarkAsSink) {
  433. HasGeneratedNodes = true;
  434. bool IsNew;
  435. ExplodedNode *N = C.Eng.G->getNode(Loc, State, MarkAsSink, &IsNew);
  436. N->addPredecessor(FromN, *C.Eng.G);
  437. Frontier.erase(FromN);
  438. if (!IsNew)
  439. return 0;
  440. if (!MarkAsSink)
  441. Frontier.Add(N);
  442. return N;
  443. }
  444. void NodeBuilderWithSinks::anchor() { }
  445. StmtNodeBuilder::~StmtNodeBuilder() {
  446. if (EnclosingBldr)
  447. for (ExplodedNodeSet::iterator I = Frontier.begin(),
  448. E = Frontier.end(); I != E; ++I )
  449. EnclosingBldr->addNodes(*I);
  450. }
  451. void BranchNodeBuilder::anchor() { }
  452. ExplodedNode *BranchNodeBuilder::generateNode(ProgramStateRef State,
  453. bool branch,
  454. ExplodedNode *NodePred) {
  455. // If the branch has been marked infeasible we should not generate a node.
  456. if (!isFeasible(branch))
  457. return NULL;
  458. ProgramPoint Loc = BlockEdge(C.Block, branch ? DstT:DstF,
  459. NodePred->getLocationContext());
  460. ExplodedNode *Succ = generateNodeImpl(Loc, State, NodePred);
  461. return Succ;
  462. }
  463. ExplodedNode*
  464. IndirectGotoNodeBuilder::generateNode(const iterator &I,
  465. ProgramStateRef St,
  466. bool IsSink) {
  467. bool IsNew;
  468. ExplodedNode *Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
  469. Pred->getLocationContext()), St,
  470. IsSink, &IsNew);
  471. Succ->addPredecessor(Pred, *Eng.G);
  472. if (!IsNew)
  473. return 0;
  474. if (!IsSink)
  475. Eng.WList->enqueue(Succ);
  476. return Succ;
  477. }
  478. ExplodedNode*
  479. SwitchNodeBuilder::generateCaseStmtNode(const iterator &I,
  480. ProgramStateRef St) {
  481. bool IsNew;
  482. ExplodedNode *Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
  483. Pred->getLocationContext()), St,
  484. false, &IsNew);
  485. Succ->addPredecessor(Pred, *Eng.G);
  486. if (!IsNew)
  487. return 0;
  488. Eng.WList->enqueue(Succ);
  489. return Succ;
  490. }
  491. ExplodedNode*
  492. SwitchNodeBuilder::generateDefaultCaseNode(ProgramStateRef St,
  493. bool IsSink) {
  494. // Get the block for the default case.
  495. assert(Src->succ_rbegin() != Src->succ_rend());
  496. CFGBlock *DefaultBlock = *Src->succ_rbegin();
  497. // Sanity check for default blocks that are unreachable and not caught
  498. // by earlier stages.
  499. if (!DefaultBlock)
  500. return NULL;
  501. bool IsNew;
  502. ExplodedNode *Succ = Eng.G->getNode(BlockEdge(Src, DefaultBlock,
  503. Pred->getLocationContext()), St,
  504. IsSink, &IsNew);
  505. Succ->addPredecessor(Pred, *Eng.G);
  506. if (!IsNew)
  507. return 0;
  508. if (!IsSink)
  509. Eng.WList->enqueue(Succ);
  510. return Succ;
  511. }