blob: 36f330303c372758cd1c3243cd0e3cfeade84e12 [file] [log] [blame]
Ted Kremenek17810802008-11-12 19:24:17 +00001//==- GRCoreEngine.cpp - Path-Sensitive Dataflow Engine ------------*- C++ -*-//
Mike Stump11289f42009-09-09 15:08:12 +00002//
Ted Kremenek3e743662008-01-14 23:24:37 +00003// 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
Ted Kremenekd6b87082010-01-25 04:41:41 +000015#include "clang/Checker/PathSensitive/GRCoreEngine.h"
16#include "clang/Checker/PathSensitive/GRExprEngine.h"
Ted Kremenek3e743662008-01-14 23:24:37 +000017#include "clang/AST/Expr.h"
Ted Kremenek3e743662008-01-14 23:24:37 +000018#include "llvm/Support/Casting.h"
19#include "llvm/ADT/DenseMap.h"
20#include <vector>
Ted Kremenekd9de9f12008-12-16 22:13:33 +000021#include <queue>
Ted Kremenek3e743662008-01-14 23:24:37 +000022
23using llvm::cast;
24using llvm::isa;
25using namespace clang;
26
Ted Kremenekd9de9f12008-12-16 22:13:33 +000027//===----------------------------------------------------------------------===//
28// Worklist classes for exploration of reachable states.
29//===----------------------------------------------------------------------===//
30
Ted Kremenek3e743662008-01-14 23:24:37 +000031namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000032class DFS : public GRWorkList {
Ted Kremenek3e743662008-01-14 23:24:37 +000033 llvm::SmallVector<GRWorkListUnit,20> Stack;
34public:
35 virtual bool hasWork() const {
36 return !Stack.empty();
37 }
38
39 virtual void Enqueue(const GRWorkListUnit& U) {
40 Stack.push_back(U);
41 }
42
43 virtual GRWorkListUnit Dequeue() {
44 assert (!Stack.empty());
45 const GRWorkListUnit& U = Stack.back();
46 Stack.pop_back(); // This technically "invalidates" U, but we are fine.
47 return U;
48 }
49};
Mike Stump11289f42009-09-09 15:08:12 +000050
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000051class BFS : public GRWorkList {
Ted Kremenek2c327732009-05-01 22:18:46 +000052 std::queue<GRWorkListUnit> Queue;
53public:
54 virtual bool hasWork() const {
55 return !Queue.empty();
56 }
Mike Stump11289f42009-09-09 15:08:12 +000057
Ted Kremenek2c327732009-05-01 22:18:46 +000058 virtual void Enqueue(const GRWorkListUnit& U) {
59 Queue.push(U);
60 }
Mike Stump11289f42009-09-09 15:08:12 +000061
Ted Kremenek2c327732009-05-01 22:18:46 +000062 virtual GRWorkListUnit Dequeue() {
63 // Don't use const reference. The subsequent pop_back() might make it
64 // unsafe.
Mike Stump11289f42009-09-09 15:08:12 +000065 GRWorkListUnit U = Queue.front();
Ted Kremenek2c327732009-05-01 22:18:46 +000066 Queue.pop();
67 return U;
68 }
69};
Mike Stump11289f42009-09-09 15:08:12 +000070
Ted Kremenek3e743662008-01-14 23:24:37 +000071} // end anonymous namespace
72
Ted Kremenek2e12c2e2008-01-16 18:18:48 +000073// Place the dstor for GRWorkList here because it contains virtual member
74// functions, and we the code for the dstor generated in one compilation unit.
75GRWorkList::~GRWorkList() {}
76
Ted Kremenek2c327732009-05-01 22:18:46 +000077GRWorkList *GRWorkList::MakeDFS() { return new DFS(); }
78GRWorkList *GRWorkList::MakeBFS() { return new BFS(); }
Ted Kremenek3e743662008-01-14 23:24:37 +000079
Ted Kremenekd9de9f12008-12-16 22:13:33 +000080namespace {
Kovarththanan Rajaratnam65c65662009-11-28 06:07:30 +000081 class BFSBlockDFSContents : public GRWorkList {
Ted Kremenekd9de9f12008-12-16 22:13:33 +000082 std::queue<GRWorkListUnit> Queue;
83 llvm::SmallVector<GRWorkListUnit,20> Stack;
84 public:
85 virtual bool hasWork() const {
86 return !Queue.empty() || !Stack.empty();
87 }
Mike Stump11289f42009-09-09 15:08:12 +000088
Ted Kremenekd9de9f12008-12-16 22:13:33 +000089 virtual void Enqueue(const GRWorkListUnit& U) {
90 if (isa<BlockEntrance>(U.getNode()->getLocation()))
91 Queue.push(U);
92 else
93 Stack.push_back(U);
94 }
Mike Stump11289f42009-09-09 15:08:12 +000095
Ted Kremenekd9de9f12008-12-16 22:13:33 +000096 virtual GRWorkListUnit Dequeue() {
97 // Process all basic blocks to completion.
98 if (!Stack.empty()) {
99 const GRWorkListUnit& U = Stack.back();
100 Stack.pop_back(); // This technically "invalidates" U, but we are fine.
101 return U;
102 }
Mike Stump11289f42009-09-09 15:08:12 +0000103
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000104 assert(!Queue.empty());
105 // Don't use const reference. The subsequent pop_back() might make it
106 // unsafe.
Mike Stump11289f42009-09-09 15:08:12 +0000107 GRWorkListUnit U = Queue.front();
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000108 Queue.pop();
Mike Stump11289f42009-09-09 15:08:12 +0000109 return U;
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000110 }
111 };
112} // end anonymous namespace
113
114GRWorkList* GRWorkList::MakeBFSBlockDFSContents() {
115 return new BFSBlockDFSContents();
116}
117
118//===----------------------------------------------------------------------===//
119// Core analysis engine.
120//===----------------------------------------------------------------------===//
Zhongxing Xu107f7592009-08-06 12:48:26 +0000121void GRCoreEngine::ProcessEndPath(GREndPathNodeBuilder& Builder) {
Zhongxing Xu82003da2009-08-06 10:00:15 +0000122 SubEngine.ProcessEndPath(Builder);
123}
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000124
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000125void GRCoreEngine::ProcessStmt(CFGElement E, GRStmtNodeBuilder& Builder) {
126 SubEngine.ProcessStmt(E, Builder);
Zhongxing Xu82003da2009-08-06 10:00:15 +0000127}
128
Zhongxing Xu3c0c81a2010-03-23 05:05:02 +0000129bool GRCoreEngine::ProcessBlockEntrance(CFGBlock* Blk, const ExplodedNode *Pred,
Mike Stump11289f42009-09-09 15:08:12 +0000130 GRBlockCounter BC) {
Zhongxing Xu3c0c81a2010-03-23 05:05:02 +0000131 return SubEngine.ProcessBlockEntrance(Blk, Pred, BC);
Zhongxing Xu82003da2009-08-06 10:00:15 +0000132}
133
134void GRCoreEngine::ProcessBranch(Stmt* Condition, Stmt* Terminator,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000135 GRBranchNodeBuilder& Builder) {
Mike Stump11289f42009-09-09 15:08:12 +0000136 SubEngine.ProcessBranch(Condition, Terminator, Builder);
Zhongxing Xu82003da2009-08-06 10:00:15 +0000137}
138
Zhongxing Xu107f7592009-08-06 12:48:26 +0000139void GRCoreEngine::ProcessIndirectGoto(GRIndirectGotoNodeBuilder& Builder) {
Zhongxing Xu82003da2009-08-06 10:00:15 +0000140 SubEngine.ProcessIndirectGoto(Builder);
141}
142
Zhongxing Xu107f7592009-08-06 12:48:26 +0000143void GRCoreEngine::ProcessSwitch(GRSwitchNodeBuilder& Builder) {
Zhongxing Xu82003da2009-08-06 10:00:15 +0000144 SubEngine.ProcessSwitch(Builder);
145}
Zhongxing Xu107f7592009-08-06 12:48:26 +0000146
Douglas Gregora2fbc942010-02-25 19:01:53 +0000147void GRCoreEngine::ProcessCallEnter(GRCallEnterNodeBuilder &Builder) {
148 SubEngine.ProcessCallEnter(Builder);
149}
150
151void GRCoreEngine::ProcessCallExit(GRCallExitNodeBuilder &Builder) {
152 SubEngine.ProcessCallExit(Builder);
153}
154
Ted Kremenek3e743662008-01-14 23:24:37 +0000155/// ExecuteWorkList - Run the worklist algorithm for a maximum number of steps.
Zhongxing Xue1190f72009-08-15 03:17:38 +0000156bool GRCoreEngine::ExecuteWorkList(const LocationContext *L, unsigned Steps) {
Mike Stump11289f42009-09-09 15:08:12 +0000157
Ted Kremenek3e743662008-01-14 23:24:37 +0000158 if (G->num_roots() == 0) { // Initialize the analysis by constructing
159 // the root if none exists.
Mike Stump11289f42009-09-09 15:08:12 +0000160
Zhongxing Xu94ec6492009-08-25 03:33:41 +0000161 CFGBlock* Entry = &(L->getCFG()->getEntry());
Mike Stump11289f42009-09-09 15:08:12 +0000162
163 assert (Entry->empty() &&
Ted Kremenek3e743662008-01-14 23:24:37 +0000164 "Entry block must be empty.");
Mike Stump11289f42009-09-09 15:08:12 +0000165
Ted Kremenek3e743662008-01-14 23:24:37 +0000166 assert (Entry->succ_size() == 1 &&
167 "Entry block must have 1 successor.");
Mike Stump11289f42009-09-09 15:08:12 +0000168
Ted Kremenek3e743662008-01-14 23:24:37 +0000169 // Get the solitary successor.
Mike Stump11289f42009-09-09 15:08:12 +0000170 CFGBlock* Succ = *(Entry->succ_begin());
171
Ted Kremenek3e743662008-01-14 23:24:37 +0000172 // Construct an edge representing the
173 // starting location in the function.
Zhongxing Xue1190f72009-08-15 03:17:38 +0000174 BlockEdge StartLoc(Entry, Succ, L);
Mike Stump11289f42009-09-09 15:08:12 +0000175
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000176 // Set the current block counter to being empty.
177 WList->setBlockCounter(BCounterFactory.GetEmptyCounter());
Mike Stump11289f42009-09-09 15:08:12 +0000178
Ted Kremenek3e743662008-01-14 23:24:37 +0000179 // Generate the root.
Zhongxing Xu5f078cb2009-08-17 06:19:58 +0000180 GenerateNode(StartLoc, getInitialState(L), 0);
Ted Kremenek3e743662008-01-14 23:24:37 +0000181 }
Mike Stump11289f42009-09-09 15:08:12 +0000182
Ted Kremenek3e743662008-01-14 23:24:37 +0000183 while (Steps && WList->hasWork()) {
184 --Steps;
185 const GRWorkListUnit& WU = WList->Dequeue();
Mike Stump11289f42009-09-09 15:08:12 +0000186
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000187 // Set the current block counter.
188 WList->setBlockCounter(WU.getBlockCounter());
189
190 // Retrieve the node.
Zhongxing Xu20227f72009-08-06 01:32:16 +0000191 ExplodedNode* Node = WU.getNode();
Mike Stump11289f42009-09-09 15:08:12 +0000192
Ted Kremenek3e743662008-01-14 23:24:37 +0000193 // Dispatch on the location type.
194 switch (Node->getLocation().getKind()) {
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000195 case ProgramPoint::BlockEdgeKind:
Ted Kremenek3e743662008-01-14 23:24:37 +0000196 HandleBlockEdge(cast<BlockEdge>(Node->getLocation()), Node);
197 break;
Mike Stump11289f42009-09-09 15:08:12 +0000198
Ted Kremenek3e743662008-01-14 23:24:37 +0000199 case ProgramPoint::BlockEntranceKind:
200 HandleBlockEntrance(cast<BlockEntrance>(Node->getLocation()), Node);
201 break;
Mike Stump11289f42009-09-09 15:08:12 +0000202
Ted Kremenek3e743662008-01-14 23:24:37 +0000203 case ProgramPoint::BlockExitKind:
Ted Kremeneke5843592008-01-15 00:24:08 +0000204 assert (false && "BlockExit location never occur in forward analysis.");
Ted Kremenek3e743662008-01-14 23:24:37 +0000205 break;
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000206
Douglas Gregora2fbc942010-02-25 19:01:53 +0000207 case ProgramPoint::CallEnterKind:
208 HandleCallEnter(cast<CallEnter>(Node->getLocation()), WU.getBlock(),
209 WU.getIndex(), Node);
210 break;
211
212 case ProgramPoint::CallExitKind:
213 HandleCallExit(cast<CallExit>(Node->getLocation()), Node);
214 break;
215
Ted Kremenekd9de9f12008-12-16 22:13:33 +0000216 default:
217 assert(isa<PostStmt>(Node->getLocation()));
Ted Kremenek3e743662008-01-14 23:24:37 +0000218 HandlePostStmt(cast<PostStmt>(Node->getLocation()), WU.getBlock(),
219 WU.getIndex(), Node);
Mike Stump11289f42009-09-09 15:08:12 +0000220 break;
Ted Kremenek3e743662008-01-14 23:24:37 +0000221 }
222 }
Mike Stump11289f42009-09-09 15:08:12 +0000223
Ted Kremenek574f3042010-06-23 22:08:00 +0000224 SubEngine.ProcessEndWorklist(WList->hasWork());
Ted Kremenek3e743662008-01-14 23:24:37 +0000225 return WList->hasWork();
226}
227
Douglas Gregora2fbc942010-02-25 19:01:53 +0000228void GRCoreEngine::HandleCallEnter(const CallEnter &L, const CFGBlock *Block,
229 unsigned Index, ExplodedNode *Pred) {
230 GRCallEnterNodeBuilder Builder(*this, Pred, L.getCallExpr(), L.getCallee(),
231 Block, Index);
232 ProcessCallEnter(Builder);
233}
234
235void GRCoreEngine::HandleCallExit(const CallExit &L, ExplodedNode *Pred) {
236 GRCallExitNodeBuilder Builder(*this, Pred);
237 ProcessCallExit(Builder);
238}
Zhongxing Xu82003da2009-08-06 10:00:15 +0000239
240void GRCoreEngine::HandleBlockEdge(const BlockEdge& L, ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000241
Ted Kremenek3e743662008-01-14 23:24:37 +0000242 CFGBlock* Blk = L.getDst();
Mike Stump11289f42009-09-09 15:08:12 +0000243
244 // Check if we are entering the EXIT block.
Zhongxing Xud2ab38e2009-12-23 08:54:57 +0000245 if (Blk == &(L.getLocationContext()->getCFG()->getExit())) {
Mike Stump11289f42009-09-09 15:08:12 +0000246
Zhongxing Xud2ab38e2009-12-23 08:54:57 +0000247 assert (L.getLocationContext()->getCFG()->getExit().size() == 0
Ted Kremenek997d8722008-01-29 00:33:40 +0000248 && "EXIT block cannot contain Stmts.");
Ted Kremenek3e743662008-01-14 23:24:37 +0000249
Ted Kremenek811c2b42008-04-11 22:03:04 +0000250 // Process the final state transition.
Zhongxing Xu107f7592009-08-06 12:48:26 +0000251 GREndPathNodeBuilder Builder(Blk, Pred, this);
Ted Kremenek811c2b42008-04-11 22:03:04 +0000252 ProcessEndPath(Builder);
Ted Kremenek3e743662008-01-14 23:24:37 +0000253
Ted Kremenek3e743662008-01-14 23:24:37 +0000254 // This path is done. Don't enqueue any more nodes.
255 return;
256 }
Ted Kremenek17f4dbd2008-02-29 20:27:50 +0000257
258 // FIXME: Should we allow ProcessBlockEntrance to also manipulate state?
Mike Stump11289f42009-09-09 15:08:12 +0000259
Zhongxing Xu3c0c81a2010-03-23 05:05:02 +0000260 if (ProcessBlockEntrance(Blk, Pred, WList->getBlockCounter()))
Zhongxing Xue1190f72009-08-15 03:17:38 +0000261 GenerateNode(BlockEntrance(Blk, Pred->getLocationContext()), Pred->State, Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000262}
263
Zhongxing Xu82003da2009-08-06 10:00:15 +0000264void GRCoreEngine::HandleBlockEntrance(const BlockEntrance& L,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000265 ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000266
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000267 // Increment the block counter.
268 GRBlockCounter Counter = WList->getBlockCounter();
Zhongxing Xu3c0c81a2010-03-23 05:05:02 +0000269 Counter = BCounterFactory.IncrementCount(Counter,
270 Pred->getLocationContext()->getCurrentStackFrame(),
271 L.getBlock()->getBlockID());
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000272 WList->setBlockCounter(Counter);
Mike Stump11289f42009-09-09 15:08:12 +0000273
274 // Process the entrance of the block.
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000275 if (CFGElement E = L.getFirstElement()) {
Mike Stump11289f42009-09-09 15:08:12 +0000276 GRStmtNodeBuilder Builder(L.getBlock(), 0, Pred, this,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000277 SubEngine.getStateManager());
Ted Kremenek4cad5fc2009-12-16 03:18:58 +0000278 ProcessStmt(E, Builder);
Ted Kremenek3e743662008-01-14 23:24:37 +0000279 }
Mike Stump11289f42009-09-09 15:08:12 +0000280 else
Ted Kremeneke5843592008-01-15 00:24:08 +0000281 HandleBlockExit(L.getBlock(), Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000282}
283
Zhongxing Xu82003da2009-08-06 10:00:15 +0000284void GRCoreEngine::HandleBlockExit(CFGBlock * B, ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000285
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000286 if (Stmt* Term = B->getTerminator()) {
287 switch (Term->getStmtClass()) {
288 default:
289 assert(false && "Analysis for this terminator not implemented.");
290 break;
Mike Stump11289f42009-09-09 15:08:12 +0000291
Ted Kremenek822f7372008-02-12 21:51:20 +0000292 case Stmt::BinaryOperatorClass: // '&&' and '||'
293 HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred);
294 return;
Mike Stump11289f42009-09-09 15:08:12 +0000295
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000296 case Stmt::ConditionalOperatorClass:
297 HandleBranch(cast<ConditionalOperator>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000298 return;
Mike Stump11289f42009-09-09 15:08:12 +0000299
Ted Kremenek822f7372008-02-12 21:51:20 +0000300 // FIXME: Use constant-folding in CFG construction to simplify this
301 // case.
Mike Stump11289f42009-09-09 15:08:12 +0000302
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000303 case Stmt::ChooseExprClass:
304 HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000305 return;
Mike Stump11289f42009-09-09 15:08:12 +0000306
Ted Kremenek822f7372008-02-12 21:51:20 +0000307 case Stmt::DoStmtClass:
308 HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred);
309 return;
Mike Stump11289f42009-09-09 15:08:12 +0000310
Ted Kremenek822f7372008-02-12 21:51:20 +0000311 case Stmt::ForStmtClass:
312 HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred);
313 return;
Mike Stump11289f42009-09-09 15:08:12 +0000314
Ted Kremenek632bcb82008-02-13 16:56:51 +0000315 case Stmt::ContinueStmtClass:
316 case Stmt::BreakStmtClass:
Mike Stump11289f42009-09-09 15:08:12 +0000317 case Stmt::GotoStmtClass:
Ted Kremenek3f2f1ad2008-02-05 00:26:40 +0000318 break;
Mike Stump11289f42009-09-09 15:08:12 +0000319
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000320 case Stmt::IfStmtClass:
321 HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000322 return;
Mike Stump11289f42009-09-09 15:08:12 +0000323
Ted Kremenek7022efb2008-02-13 00:24:44 +0000324 case Stmt::IndirectGotoStmtClass: {
325 // Only 1 successor: the indirect goto dispatch block.
326 assert (B->succ_size() == 1);
Mike Stump11289f42009-09-09 15:08:12 +0000327
Zhongxing Xu107f7592009-08-06 12:48:26 +0000328 GRIndirectGotoNodeBuilder
Ted Kremenek7022efb2008-02-13 00:24:44 +0000329 builder(Pred, B, cast<IndirectGotoStmt>(Term)->getTarget(),
330 *(B->succ_begin()), this);
Mike Stump11289f42009-09-09 15:08:12 +0000331
Ted Kremenek7022efb2008-02-13 00:24:44 +0000332 ProcessIndirectGoto(builder);
333 return;
334 }
Mike Stump11289f42009-09-09 15:08:12 +0000335
Ted Kremenek17810802008-11-12 19:24:17 +0000336 case Stmt::ObjCForCollectionStmtClass: {
337 // In the case of ObjCForCollectionStmt, it appears twice in a CFG:
338 //
339 // (1) inside a basic block, which represents the binding of the
340 // 'element' variable to a value.
341 // (2) in a terminator, which represents the branch.
342 //
343 // For (1), subengines will bind a value (i.e., 0 or 1) indicating
344 // whether or not collection contains any more elements. We cannot
345 // just test to see if the element is nil because a container can
346 // contain nil elements.
347 HandleBranch(Term, Term, B, Pred);
348 return;
349 }
Mike Stump11289f42009-09-09 15:08:12 +0000350
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000351 case Stmt::SwitchStmtClass: {
Zhongxing Xu107f7592009-08-06 12:48:26 +0000352 GRSwitchNodeBuilder builder(Pred, B, cast<SwitchStmt>(Term)->getCond(),
353 this);
Mike Stump11289f42009-09-09 15:08:12 +0000354
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000355 ProcessSwitch(builder);
356 return;
357 }
Mike Stump11289f42009-09-09 15:08:12 +0000358
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000359 case Stmt::WhileStmtClass:
360 HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred);
Ted Kremenek822f7372008-02-12 21:51:20 +0000361 return;
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000362 }
363 }
Ted Kremenek822f7372008-02-12 21:51:20 +0000364
365 assert (B->succ_size() == 1 &&
366 "Blocks with no terminator should have at most 1 successor.");
Mike Stump11289f42009-09-09 15:08:12 +0000367
368 GenerateNode(BlockEdge(B, *(B->succ_begin()), Pred->getLocationContext()),
Zhongxing Xue1190f72009-08-15 03:17:38 +0000369 Pred->State, Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000370}
371
Zhongxing Xu82003da2009-08-06 10:00:15 +0000372void GRCoreEngine::HandleBranch(Stmt* Cond, Stmt* Term, CFGBlock * B,
373 ExplodedNode* Pred) {
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000374 assert (B->succ_size() == 2);
375
Zhongxing Xu107f7592009-08-06 12:48:26 +0000376 GRBranchNodeBuilder Builder(B, *(B->succ_begin()), *(B->succ_begin()+1),
377 Pred, this);
Mike Stump11289f42009-09-09 15:08:12 +0000378
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000379 ProcessBranch(Cond, Term, Builder);
380}
381
Zhongxing Xu82003da2009-08-06 10:00:15 +0000382void GRCoreEngine::HandlePostStmt(const PostStmt& L, CFGBlock* B,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000383 unsigned StmtIdx, ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000384
Ted Kremenek3e743662008-01-14 23:24:37 +0000385 assert (!B->empty());
386
Ted Kremeneke5843592008-01-15 00:24:08 +0000387 if (StmtIdx == B->size())
388 HandleBlockExit(B, Pred);
Ted Kremenek3e743662008-01-14 23:24:37 +0000389 else {
Mike Stump11289f42009-09-09 15:08:12 +0000390 GRStmtNodeBuilder Builder(B, StmtIdx, Pred, this,
Zhongxing Xu107f7592009-08-06 12:48:26 +0000391 SubEngine.getStateManager());
Ted Kremeneke914bb82008-01-16 22:13:19 +0000392 ProcessStmt((*B)[StmtIdx], Builder);
Ted Kremenek3e743662008-01-14 23:24:37 +0000393 }
394}
395
Ted Kremenek3e743662008-01-14 23:24:37 +0000396/// GenerateNode - Utility method to generate nodes, hook up successors,
397/// and add nodes to the worklist.
Mike Stump11289f42009-09-09 15:08:12 +0000398void GRCoreEngine::GenerateNode(const ProgramPoint& Loc,
Zhongxing Xu82003da2009-08-06 10:00:15 +0000399 const GRState* State, ExplodedNode* Pred) {
Mike Stump11289f42009-09-09 15:08:12 +0000400
Ted Kremenek3e743662008-01-14 23:24:37 +0000401 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000402 ExplodedNode* Node = G->getNode(Loc, State, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000403
404 if (Pred)
Ted Kremenekc3661de2009-10-07 00:42:52 +0000405 Node->addPredecessor(Pred, *G); // Link 'Node' with its predecessor.
Ted Kremenek3e743662008-01-14 23:24:37 +0000406 else {
407 assert (IsNew);
408 G->addRoot(Node); // 'Node' has no predecessor. Make it a root.
409 }
Mike Stump11289f42009-09-09 15:08:12 +0000410
Ted Kremenek3e743662008-01-14 23:24:37 +0000411 // Only add 'Node' to the worklist if it was freshly generated.
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000412 if (IsNew) WList->Enqueue(Node);
Ted Kremenek3e743662008-01-14 23:24:37 +0000413}
414
Zhongxing Xu107f7592009-08-06 12:48:26 +0000415GRStmtNodeBuilder::GRStmtNodeBuilder(CFGBlock* b, unsigned idx,
416 ExplodedNode* N, GRCoreEngine* e,
417 GRStateManager &mgr)
Zhongxing Xud041bc62010-02-26 02:38:09 +0000418 : Eng(*e), B(*b), Idx(idx), Pred(N), Mgr(mgr), Auditor(0),
Zhongxing Xu107f7592009-08-06 12:48:26 +0000419 PurgingDeadSymbols(false), BuildSinks(false), HasGeneratedNode(false),
420 PointKind(ProgramPoint::PostStmtKind), Tag(0) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000421 Deferred.insert(N);
Zhongxing Xud041bc62010-02-26 02:38:09 +0000422 CleanedState = Pred->getState();
Ted Kremenek3e743662008-01-14 23:24:37 +0000423}
424
Zhongxing Xu107f7592009-08-06 12:48:26 +0000425GRStmtNodeBuilder::~GRStmtNodeBuilder() {
Ted Kremenek3e743662008-01-14 23:24:37 +0000426 for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
Ted Kremeneka50d9852008-01-30 23:03:39 +0000427 if (!(*I)->isSink())
Ted Kremenek3e743662008-01-14 23:24:37 +0000428 GenerateAutoTransition(*I);
429}
430
Zhongxing Xu107f7592009-08-06 12:48:26 +0000431void GRStmtNodeBuilder::GenerateAutoTransition(ExplodedNode* N) {
Ted Kremeneka50d9852008-01-30 23:03:39 +0000432 assert (!N->isSink());
Mike Stump11289f42009-09-09 15:08:12 +0000433
Douglas Gregora2fbc942010-02-25 19:01:53 +0000434 // Check if this node entered a callee.
435 if (isa<CallEnter>(N->getLocation())) {
436 // Still use the index of the CallExpr. It's needed to create the callee
437 // StackFrameContext.
438 Eng.WList->Enqueue(N, B, Idx);
439 return;
440 }
441
Zhongxing Xue1190f72009-08-15 03:17:38 +0000442 PostStmt Loc(getStmt(), N->getLocationContext());
Mike Stump11289f42009-09-09 15:08:12 +0000443
Ted Kremenek3e743662008-01-14 23:24:37 +0000444 if (Loc == N->getLocation()) {
445 // Note: 'N' should be a fresh node because otherwise it shouldn't be
446 // a member of Deferred.
447 Eng.WList->Enqueue(N, B, Idx+1);
448 return;
449 }
Mike Stump11289f42009-09-09 15:08:12 +0000450
Ted Kremenek3e743662008-01-14 23:24:37 +0000451 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000452 ExplodedNode* Succ = Eng.G->getNode(Loc, N->State, &IsNew);
Ted Kremenekc3661de2009-10-07 00:42:52 +0000453 Succ->addPredecessor(N, *Eng.G);
Ted Kremenek3e743662008-01-14 23:24:37 +0000454
455 if (IsNew)
456 Eng.WList->Enqueue(Succ, B, Idx+1);
457}
458
Zhongxing Xu5eb08f72010-04-14 06:35:09 +0000459ExplodedNode* GRStmtNodeBuilder::MakeNode(ExplodedNodeSet& Dst, Stmt* S,
460 ExplodedNode* Pred, const GRState* St,
461 ProgramPoint::Kind K) {
462 const GRState* PredState = GetState(Pred);
463
464 // If the state hasn't changed, don't generate a new node.
465 if (!BuildSinks && St == PredState && Auditor == 0) {
466 Dst.Add(Pred);
467 return NULL;
468 }
469
470 ExplodedNode* N = generateNode(S, St, Pred, K);
471
472 if (N) {
473 if (BuildSinks)
474 N->markAsSink();
475 else {
476 if (Auditor && Auditor->Audit(N, Mgr))
477 N->markAsSink();
478
479 Dst.Add(N);
480 }
481 }
482
483 return N;
484}
485
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000486static ProgramPoint GetProgramPoint(const Stmt *S, ProgramPoint::Kind K,
487 const LocationContext *LC, const void *tag){
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000488 switch (K) {
489 default:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000490 assert(false && "Unhandled ProgramPoint kind");
491 case ProgramPoint::PreStmtKind:
492 return PreStmt(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000493 case ProgramPoint::PostStmtKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000494 return PostStmt(S, LC, tag);
495 case ProgramPoint::PreLoadKind:
496 return PreLoad(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000497 case ProgramPoint::PostLoadKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000498 return PostLoad(S, LC, tag);
499 case ProgramPoint::PreStoreKind:
500 return PreStore(S, LC, tag);
Ted Kremeneka1966182008-10-17 20:49:23 +0000501 case ProgramPoint::PostStoreKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000502 return PostStore(S, LC, tag);
Ted Kremeneka6e08322009-05-07 18:27:16 +0000503 case ProgramPoint::PostLValueKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000504 return PostLValue(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000505 case ProgramPoint::PostPurgeDeadSymbolsKind:
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000506 return PostPurgeDeadSymbols(S, LC, tag);
Ted Kremenek9a935fb2008-06-18 05:34:07 +0000507 }
508}
509
Zhongxing Xu20227f72009-08-06 01:32:16 +0000510ExplodedNode*
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000511GRStmtNodeBuilder::generateNodeInternal(const Stmt* S, const GRState* state,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000512 ExplodedNode* Pred,
Ted Kremenekdf240002009-04-11 00:11:10 +0000513 ProgramPoint::Kind K,
514 const void *tag) {
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000515
Zhongxing Xud2ab38e2009-12-23 08:54:57 +0000516 const ProgramPoint &L = GetProgramPoint(S, K, Pred->getLocationContext(),tag);
Ted Kremenek5e1f78a2009-11-11 03:26:34 +0000517 return generateNodeInternal(L, state, Pred);
Ted Kremenek513f0b12009-02-19 23:45:28 +0000518}
519
Zhongxing Xu20227f72009-08-06 01:32:16 +0000520ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000521GRStmtNodeBuilder::generateNodeInternal(const ProgramPoint &Loc,
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000522 const GRState* State,
Zhongxing Xu20227f72009-08-06 01:32:16 +0000523 ExplodedNode* Pred) {
Ted Kremenek3e743662008-01-14 23:24:37 +0000524 bool IsNew;
Zhongxing Xuc90a0c22009-08-06 06:28:40 +0000525 ExplodedNode* N = Eng.G->getNode(Loc, State, &IsNew);
Ted Kremenekc3661de2009-10-07 00:42:52 +0000526 N->addPredecessor(Pred, *Eng.G);
Ted Kremenek3e743662008-01-14 23:24:37 +0000527 Deferred.erase(Pred);
Mike Stump11289f42009-09-09 15:08:12 +0000528
Ted Kremenek3e743662008-01-14 23:24:37 +0000529 if (IsNew) {
530 Deferred.insert(N);
Ted Kremenek3e743662008-01-14 23:24:37 +0000531 return N;
532 }
Mike Stump11289f42009-09-09 15:08:12 +0000533
Mike Stump11289f42009-09-09 15:08:12 +0000534 return NULL;
Ted Kremenek3e743662008-01-14 23:24:37 +0000535}
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000536
Zhongxing Xu107f7592009-08-06 12:48:26 +0000537ExplodedNode* GRBranchNodeBuilder::generateNode(const GRState* State,
538 bool branch) {
Mike Stump11289f42009-09-09 15:08:12 +0000539
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000540 // If the branch has been marked infeasible we should not generate a node.
541 if (!isFeasible(branch))
542 return NULL;
Mike Stump11289f42009-09-09 15:08:12 +0000543
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000544 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000545
Zhongxing Xu20227f72009-08-06 01:32:16 +0000546 ExplodedNode* Succ =
Zhongxing Xue1190f72009-08-15 03:17:38 +0000547 Eng.G->getNode(BlockEdge(Src,branch ? DstT:DstF,Pred->getLocationContext()),
548 State, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000549
Ted Kremenekc3661de2009-10-07 00:42:52 +0000550 Succ->addPredecessor(Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000551
Ted Kremenekaf9f3622009-07-20 18:44:36 +0000552 if (branch)
553 GeneratedTrue = true;
554 else
Mike Stump11289f42009-09-09 15:08:12 +0000555 GeneratedFalse = true;
556
Ted Kremeneka50d9852008-01-30 23:03:39 +0000557 if (IsNew) {
Ted Kremenek2531fce2008-01-30 23:24:39 +0000558 Deferred.push_back(Succ);
Ted Kremeneka50d9852008-01-30 23:03:39 +0000559 return Succ;
560 }
Mike Stump11289f42009-09-09 15:08:12 +0000561
Ted Kremeneka50d9852008-01-30 23:03:39 +0000562 return NULL;
Ted Kremenek9b4211d2008-01-29 22:56:11 +0000563}
Ted Kremenek7ff18932008-01-29 23:32:35 +0000564
Zhongxing Xu107f7592009-08-06 12:48:26 +0000565GRBranchNodeBuilder::~GRBranchNodeBuilder() {
566 if (!GeneratedTrue) generateNode(Pred->State, true);
567 if (!GeneratedFalse) generateNode(Pred->State, false);
Mike Stump11289f42009-09-09 15:08:12 +0000568
Ted Kremenek2531fce2008-01-30 23:24:39 +0000569 for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I)
Ted Kremenek90ae68f2008-02-12 18:08:17 +0000570 if (!(*I)->isSink()) Eng.WList->Enqueue(*I);
Ted Kremenek7ff18932008-01-29 23:32:35 +0000571}
Ted Kremenek7022efb2008-02-13 00:24:44 +0000572
Ted Kremenek7022efb2008-02-13 00:24:44 +0000573
Zhongxing Xu20227f72009-08-06 01:32:16 +0000574ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000575GRIndirectGotoNodeBuilder::generateNode(const iterator& I, const GRState* St,
576 bool isSink) {
Ted Kremenek7022efb2008-02-13 00:24:44 +0000577 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000578
579 ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
Zhongxing Xue1190f72009-08-15 03:17:38 +0000580 Pred->getLocationContext()), St, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000581
Ted Kremenekc3661de2009-10-07 00:42:52 +0000582 Succ->addPredecessor(Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000583
Ted Kremenek7022efb2008-02-13 00:24:44 +0000584 if (IsNew) {
Mike Stump11289f42009-09-09 15:08:12 +0000585
Ted Kremenek7022efb2008-02-13 00:24:44 +0000586 if (isSink)
587 Succ->markAsSink();
588 else
589 Eng.WList->Enqueue(Succ);
Mike Stump11289f42009-09-09 15:08:12 +0000590
Ted Kremenek7022efb2008-02-13 00:24:44 +0000591 return Succ;
592 }
Mike Stump11289f42009-09-09 15:08:12 +0000593
Ted Kremenek7022efb2008-02-13 00:24:44 +0000594 return NULL;
595}
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000596
597
Zhongxing Xu20227f72009-08-06 01:32:16 +0000598ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000599GRSwitchNodeBuilder::generateCaseStmtNode(const iterator& I, const GRState* St){
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000600
601 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000602
Zhongxing Xue1190f72009-08-15 03:17:38 +0000603 ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, I.getBlock(),
604 Pred->getLocationContext()), St, &IsNew);
Ted Kremenekc3661de2009-10-07 00:42:52 +0000605 Succ->addPredecessor(Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000606
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000607 if (IsNew) {
608 Eng.WList->Enqueue(Succ);
609 return Succ;
610 }
Mike Stump11289f42009-09-09 15:08:12 +0000611
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000612 return NULL;
613}
614
615
Zhongxing Xu20227f72009-08-06 01:32:16 +0000616ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000617GRSwitchNodeBuilder::generateDefaultCaseNode(const GRState* St, bool isSink) {
Mike Stump11289f42009-09-09 15:08:12 +0000618
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000619 // Get the block for the default case.
620 assert (Src->succ_rbegin() != Src->succ_rend());
621 CFGBlock* DefaultBlock = *Src->succ_rbegin();
Mike Stump11289f42009-09-09 15:08:12 +0000622
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000623 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000624
Zhongxing Xue1190f72009-08-15 03:17:38 +0000625 ExplodedNode* Succ = Eng.G->getNode(BlockEdge(Src, DefaultBlock,
626 Pred->getLocationContext()), St, &IsNew);
Ted Kremenekc3661de2009-10-07 00:42:52 +0000627 Succ->addPredecessor(Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000628
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000629 if (IsNew) {
630 if (isSink)
631 Succ->markAsSink();
632 else
633 Eng.WList->Enqueue(Succ);
Mike Stump11289f42009-09-09 15:08:12 +0000634
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000635 return Succ;
636 }
Mike Stump11289f42009-09-09 15:08:12 +0000637
Ted Kremenek80ebc1d2008-02-13 23:08:21 +0000638 return NULL;
639}
Ted Kremenek811c2b42008-04-11 22:03:04 +0000640
Zhongxing Xu107f7592009-08-06 12:48:26 +0000641GREndPathNodeBuilder::~GREndPathNodeBuilder() {
Ted Kremenek811c2b42008-04-11 22:03:04 +0000642 // Auto-generate an EOP node if one has not been generated.
Douglas Gregora2fbc942010-02-25 19:01:53 +0000643 if (!HasGeneratedNode) {
644 // If we are in an inlined call, generate CallExit node.
645 if (Pred->getLocationContext()->getParent())
646 GenerateCallExitNode(Pred->State);
647 else
648 generateNode(Pred->State);
649 }
Ted Kremenek811c2b42008-04-11 22:03:04 +0000650}
651
Zhongxing Xu20227f72009-08-06 01:32:16 +0000652ExplodedNode*
Zhongxing Xu107f7592009-08-06 12:48:26 +0000653GREndPathNodeBuilder::generateNode(const GRState* State, const void *tag,
654 ExplodedNode* P) {
Mike Stump11289f42009-09-09 15:08:12 +0000655 HasGeneratedNode = true;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000656 bool IsNew;
Mike Stump11289f42009-09-09 15:08:12 +0000657
658 ExplodedNode* Node = Eng.G->getNode(BlockEntrance(&B,
Zhongxing Xue1190f72009-08-15 03:17:38 +0000659 Pred->getLocationContext(), tag), State, &IsNew);
Mike Stump11289f42009-09-09 15:08:12 +0000660
Ted Kremenekc3661de2009-10-07 00:42:52 +0000661 Node->addPredecessor(P ? P : Pred, *Eng.G);
Mike Stump11289f42009-09-09 15:08:12 +0000662
Ted Kremenek811c2b42008-04-11 22:03:04 +0000663 if (IsNew) {
Ted Kremenek811c2b42008-04-11 22:03:04 +0000664 Eng.G->addEndOfPath(Node);
Ted Kremenekd004c412008-04-18 16:30:14 +0000665 return Node;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000666 }
Mike Stump11289f42009-09-09 15:08:12 +0000667
Ted Kremenekd004c412008-04-18 16:30:14 +0000668 return NULL;
Ted Kremenek811c2b42008-04-11 22:03:04 +0000669}
Douglas Gregora2fbc942010-02-25 19:01:53 +0000670
671void GREndPathNodeBuilder::GenerateCallExitNode(const GRState *state) {
672 HasGeneratedNode = true;
673 // Create a CallExit node and enqueue it.
674 const StackFrameContext *LocCtx
675 = cast<StackFrameContext>(Pred->getLocationContext());
676 const Stmt *CE = LocCtx->getCallSite();
677
678 // Use the the callee location context.
679 CallExit Loc(CE, LocCtx);
680
681 bool isNew;
682 ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew);
683 Node->addPredecessor(Pred, *Eng.G);
684
685 if (isNew)
686 Eng.WList->Enqueue(Node);
687}
688
689
690void GRCallEnterNodeBuilder::GenerateNode(const GRState *state,
691 const LocationContext *LocCtx) {
692 // Get the callee entry block.
693 const CFGBlock *Entry = &(LocCtx->getCFG()->getEntry());
694 assert(Entry->empty());
695 assert(Entry->succ_size() == 1);
696
697 // Get the solitary successor.
698 const CFGBlock *SuccB = *(Entry->succ_begin());
699
700 // Construct an edge representing the starting location in the callee.
701 BlockEdge Loc(Entry, SuccB, LocCtx);
702
703 bool isNew;
704 ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew);
705 Node->addPredecessor(const_cast<ExplodedNode*>(Pred), *Eng.G);
706
707 if (isNew)
708 Eng.WList->Enqueue(Node);
709}
710
711void GRCallExitNodeBuilder::GenerateNode(const GRState *state) {
712 // Get the callee's location context.
713 const StackFrameContext *LocCtx
714 = cast<StackFrameContext>(Pred->getLocationContext());
715
716 PostStmt Loc(LocCtx->getCallSite(), LocCtx->getParent());
717 bool isNew;
718 ExplodedNode *Node = Eng.G->getNode(Loc, state, &isNew);
719 Node->addPredecessor(const_cast<ExplodedNode*>(Pred), *Eng.G);
720 if (isNew)
721 Eng.WList->Enqueue(Node, *const_cast<CFGBlock*>(LocCtx->getCallSiteBlock()),
722 LocCtx->getIndex() + 1);
723}