Ted Kremenek | f24af5b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 1 | //==- GREngine.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 | |
| 15 | #include "clang/Analysis/PathSensitive/GREngine.h" |
| 16 | #include "clang/AST/Expr.h" |
| 17 | #include "llvm/Support/Compiler.h" |
| 18 | #include "llvm/Support/Casting.h" |
| 19 | #include "llvm/ADT/DenseMap.h" |
| 20 | #include <vector> |
| 21 | |
| 22 | using llvm::cast; |
| 23 | using llvm::isa; |
| 24 | using namespace clang; |
| 25 | |
| 26 | namespace { |
| 27 | class VISIBILITY_HIDDEN DFS : public GRWorkList { |
| 28 | llvm::SmallVector<GRWorkListUnit,20> Stack; |
| 29 | public: |
| 30 | virtual bool hasWork() const { |
| 31 | return !Stack.empty(); |
| 32 | } |
| 33 | |
| 34 | virtual void Enqueue(const GRWorkListUnit& U) { |
| 35 | Stack.push_back(U); |
| 36 | } |
| 37 | |
| 38 | virtual GRWorkListUnit Dequeue() { |
| 39 | assert (!Stack.empty()); |
| 40 | const GRWorkListUnit& U = Stack.back(); |
| 41 | Stack.pop_back(); // This technically "invalidates" U, but we are fine. |
| 42 | return U; |
| 43 | } |
| 44 | }; |
| 45 | } // end anonymous namespace |
| 46 | |
Ted Kremenek | ee98546 | 2008-01-16 18:18:48 +0000 | [diff] [blame] | 47 | // Place the dstor for GRWorkList here because it contains virtual member |
| 48 | // functions, and we the code for the dstor generated in one compilation unit. |
| 49 | GRWorkList::~GRWorkList() {} |
| 50 | |
Ted Kremenek | f24af5b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 51 | GRWorkList* GRWorkList::MakeDFS() { return new DFS(); } |
| 52 | |
| 53 | /// ExecuteWorkList - Run the worklist algorithm for a maximum number of steps. |
| 54 | bool GREngineImpl::ExecuteWorkList(unsigned Steps) { |
| 55 | |
| 56 | if (G->num_roots() == 0) { // Initialize the analysis by constructing |
| 57 | // the root if none exists. |
| 58 | |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 59 | CFGBlock* Entry = &getCFG().getEntry(); |
Ted Kremenek | f24af5b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 60 | |
| 61 | assert (Entry->empty() && |
| 62 | "Entry block must be empty."); |
| 63 | |
| 64 | assert (Entry->succ_size() == 1 && |
| 65 | "Entry block must have 1 successor."); |
| 66 | |
| 67 | // Get the solitary successor. |
| 68 | CFGBlock* Succ = *(Entry->succ_begin()); |
| 69 | |
| 70 | // Construct an edge representing the |
| 71 | // starting location in the function. |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 72 | BlockEdge StartLoc(getCFG(), Entry, Succ); |
Ted Kremenek | f24af5b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 73 | |
| 74 | // Generate the root. |
| 75 | GenerateNode(StartLoc, getInitialState()); |
| 76 | } |
| 77 | |
| 78 | while (Steps && WList->hasWork()) { |
| 79 | --Steps; |
| 80 | const GRWorkListUnit& WU = WList->Dequeue(); |
| 81 | ExplodedNodeImpl* Node = WU.getNode(); |
| 82 | |
| 83 | // Dispatch on the location type. |
| 84 | switch (Node->getLocation().getKind()) { |
| 85 | default: |
| 86 | assert (isa<BlockEdge>(Node->getLocation())); |
| 87 | HandleBlockEdge(cast<BlockEdge>(Node->getLocation()), Node); |
| 88 | break; |
| 89 | |
| 90 | case ProgramPoint::BlockEntranceKind: |
| 91 | HandleBlockEntrance(cast<BlockEntrance>(Node->getLocation()), Node); |
| 92 | break; |
| 93 | |
| 94 | case ProgramPoint::BlockExitKind: |
Ted Kremenek | 425c08c | 2008-01-15 00:24:08 +0000 | [diff] [blame] | 95 | assert (false && "BlockExit location never occur in forward analysis."); |
Ted Kremenek | f24af5b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 96 | break; |
| 97 | |
| 98 | case ProgramPoint::PostStmtKind: |
| 99 | HandlePostStmt(cast<PostStmt>(Node->getLocation()), WU.getBlock(), |
| 100 | WU.getIndex(), Node); |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return WList->hasWork(); |
| 106 | } |
| 107 | |
| 108 | void GREngineImpl::HandleBlockEdge(const BlockEdge& L, ExplodedNodeImpl* Pred) { |
| 109 | |
| 110 | CFGBlock* Blk = L.getDst(); |
| 111 | |
| 112 | // Check if we are entering the EXIT block. |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 113 | if (Blk == &getCFG().getExit()) { |
Ted Kremenek | f24af5b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 114 | |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 115 | assert (getCFG().getExit().size() == 0 |
| 116 | && "EXIT block cannot contain Stmts."); |
Ted Kremenek | f24af5b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 117 | |
| 118 | // Process the final state transition. |
| 119 | void* State = ProcessEOP(Blk, Pred->State); |
| 120 | |
| 121 | bool IsNew; |
| 122 | ExplodedNodeImpl* Node = G->getNodeImpl(BlockEntrance(Blk), State, &IsNew); |
| 123 | Node->addPredecessor(Pred); |
| 124 | |
| 125 | // If the node was freshly created, mark it as an "End-Of-Path" node. |
| 126 | if (IsNew) G->addEndOfPath(Node); |
| 127 | |
| 128 | // This path is done. Don't enqueue any more nodes. |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | // FIXME: we will dispatch to a function that |
| 133 | // manipulates the state at the entrance to a block. |
| 134 | |
Ted Kremenek | 425c08c | 2008-01-15 00:24:08 +0000 | [diff] [blame] | 135 | GenerateNode(BlockEntrance(Blk), Pred->State, Pred); |
Ted Kremenek | f24af5b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void GREngineImpl::HandleBlockEntrance(const BlockEntrance& L, |
| 139 | ExplodedNodeImpl* Pred) { |
| 140 | |
| 141 | if (Stmt* S = L.getFirstStmt()) { |
Ted Kremenek | f4b7a69 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 142 | GRStmtNodeBuilderImpl Builder(L.getBlock(), 0, Pred, this); |
Ted Kremenek | f24af5b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 143 | ProcessStmt(S, Builder); |
| 144 | } |
Ted Kremenek | 425c08c | 2008-01-15 00:24:08 +0000 | [diff] [blame] | 145 | else |
| 146 | HandleBlockExit(L.getBlock(), Pred); |
Ted Kremenek | f24af5b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | |
Ted Kremenek | 425c08c | 2008-01-15 00:24:08 +0000 | [diff] [blame] | 150 | void GREngineImpl::HandleBlockExit(CFGBlock * B, ExplodedNodeImpl* Pred) { |
Ted Kremenek | f24af5b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 151 | |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 152 | if (Stmt* Term = B->getTerminator()) { |
| 153 | switch (Term->getStmtClass()) { |
| 154 | default: |
| 155 | assert(false && "Analysis for this terminator not implemented."); |
| 156 | break; |
| 157 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 158 | case Stmt::ConditionalOperatorClass: |
| 159 | HandleBranch(cast<ConditionalOperator>(Term)->getCond(), Term, B, Pred); |
| 160 | break; |
| 161 | |
| 162 | case Stmt::ChooseExprClass: |
| 163 | HandleBranch(cast<ChooseExpr>(Term)->getCond(), Term, B, Pred); |
| 164 | break; |
| 165 | |
| 166 | case Stmt::BinaryOperatorClass: // '&&' and '||' |
| 167 | HandleBranch(cast<BinaryOperator>(Term)->getLHS(), Term, B, Pred); |
| 168 | break; |
| 169 | |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 170 | case Stmt::IfStmtClass: |
| 171 | HandleBranch(cast<IfStmt>(Term)->getCond(), Term, B, Pred); |
| 172 | break; |
| 173 | |
| 174 | case Stmt::ForStmtClass: |
| 175 | HandleBranch(cast<ForStmt>(Term)->getCond(), Term, B, Pred); |
| 176 | break; |
| 177 | |
| 178 | case Stmt::WhileStmtClass: |
| 179 | HandleBranch(cast<WhileStmt>(Term)->getCond(), Term, B, Pred); |
| 180 | break; |
| 181 | |
| 182 | case Stmt::DoStmtClass: |
| 183 | HandleBranch(cast<DoStmt>(Term)->getCond(), Term, B, Pred); |
| 184 | break; |
| 185 | } |
| 186 | } |
Ted Kremenek | f24af5b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 187 | else { |
| 188 | assert (B->succ_size() == 1 && |
| 189 | "Blocks with no terminator should have at most 1 successor."); |
| 190 | |
Ted Kremenek | cb48b9c | 2008-01-29 00:33:40 +0000 | [diff] [blame] | 191 | GenerateNode(BlockEdge(getCFG(),B,*(B->succ_begin())), Pred->State, Pred); |
Ted Kremenek | f24af5b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Ted Kremenek | f233d48 | 2008-02-05 00:26:40 +0000 | [diff] [blame] | 195 | void GREngineImpl::HandleBranch(Expr* Cond, Stmt* Term, CFGBlock * B, |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 196 | ExplodedNodeImpl* Pred) { |
| 197 | assert (B->succ_size() == 2); |
| 198 | |
| 199 | GRBranchNodeBuilderImpl Builder(B, *(B->succ_begin()), *(B->succ_begin()+1), |
| 200 | Pred, this); |
| 201 | |
| 202 | ProcessBranch(Cond, Term, Builder); |
| 203 | } |
| 204 | |
Ted Kremenek | f24af5b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 205 | void GREngineImpl::HandlePostStmt(const PostStmt& L, CFGBlock* B, |
| 206 | unsigned StmtIdx, ExplodedNodeImpl* Pred) { |
| 207 | |
| 208 | assert (!B->empty()); |
| 209 | |
Ted Kremenek | 425c08c | 2008-01-15 00:24:08 +0000 | [diff] [blame] | 210 | if (StmtIdx == B->size()) |
| 211 | HandleBlockExit(B, Pred); |
Ted Kremenek | f24af5b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 212 | else { |
Ted Kremenek | f4b7a69 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 213 | GRStmtNodeBuilderImpl Builder(B, StmtIdx, Pred, this); |
Ted Kremenek | 160760e | 2008-01-16 22:13:19 +0000 | [diff] [blame] | 214 | ProcessStmt((*B)[StmtIdx], Builder); |
Ted Kremenek | f24af5b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
| 218 | typedef llvm::DenseMap<Stmt*,Stmt*> ParentMapTy; |
| 219 | /// PopulateParentMap - Recurse the AST starting at 'Parent' and add the |
| 220 | /// mappings between child and parent to ParentMap. |
| 221 | static void PopulateParentMap(Stmt* Parent, ParentMapTy& M) { |
| 222 | for (Stmt::child_iterator I=Parent->child_begin(), |
| 223 | E=Parent->child_end(); I!=E; ++I) { |
| 224 | |
| 225 | assert (M.find(*I) == M.end()); |
| 226 | M[*I] = Parent; |
| 227 | PopulateParentMap(*I, M); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /// GenerateNode - Utility method to generate nodes, hook up successors, |
| 232 | /// and add nodes to the worklist. |
| 233 | void GREngineImpl::GenerateNode(const ProgramPoint& Loc, void* State, |
| 234 | ExplodedNodeImpl* Pred) { |
| 235 | |
| 236 | bool IsNew; |
| 237 | ExplodedNodeImpl* Node = G->getNodeImpl(Loc, State, &IsNew); |
| 238 | |
| 239 | if (Pred) |
| 240 | Node->addPredecessor(Pred); // Link 'Node' with its predecessor. |
| 241 | else { |
| 242 | assert (IsNew); |
| 243 | G->addRoot(Node); // 'Node' has no predecessor. Make it a root. |
| 244 | } |
| 245 | |
| 246 | // Only add 'Node' to the worklist if it was freshly generated. |
| 247 | if (IsNew) WList->Enqueue(GRWorkListUnit(Node)); |
| 248 | } |
| 249 | |
Ted Kremenek | f4b7a69 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 250 | GRStmtNodeBuilderImpl::GRStmtNodeBuilderImpl(CFGBlock* b, unsigned idx, |
Ted Kremenek | f24af5b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 251 | ExplodedNodeImpl* N, GREngineImpl* e) |
| 252 | : Eng(*e), B(*b), Idx(idx), LastNode(N), Populated(false) { |
| 253 | Deferred.insert(N); |
| 254 | } |
| 255 | |
Ted Kremenek | f4b7a69 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 256 | GRStmtNodeBuilderImpl::~GRStmtNodeBuilderImpl() { |
Ted Kremenek | f24af5b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 257 | for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I) |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 258 | if (!(*I)->isSink()) |
Ted Kremenek | f24af5b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 259 | GenerateAutoTransition(*I); |
| 260 | } |
| 261 | |
Ted Kremenek | f4b7a69 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 262 | void GRStmtNodeBuilderImpl::GenerateAutoTransition(ExplodedNodeImpl* N) { |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 263 | assert (!N->isSink()); |
Ted Kremenek | f24af5b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 264 | |
| 265 | PostStmt Loc(getStmt()); |
| 266 | |
| 267 | if (Loc == N->getLocation()) { |
| 268 | // Note: 'N' should be a fresh node because otherwise it shouldn't be |
| 269 | // a member of Deferred. |
| 270 | Eng.WList->Enqueue(N, B, Idx+1); |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | bool IsNew; |
| 275 | ExplodedNodeImpl* Succ = Eng.G->getNodeImpl(Loc, N->State, &IsNew); |
| 276 | Succ->addPredecessor(N); |
| 277 | |
| 278 | if (IsNew) |
| 279 | Eng.WList->Enqueue(Succ, B, Idx+1); |
| 280 | } |
| 281 | |
Ted Kremenek | f4b7a69 | 2008-01-29 22:11:49 +0000 | [diff] [blame] | 282 | ExplodedNodeImpl* GRStmtNodeBuilderImpl::generateNodeImpl(Stmt* S, void* State, |
Ted Kremenek | f24af5b | 2008-01-14 23:24:37 +0000 | [diff] [blame] | 283 | ExplodedNodeImpl* Pred) { |
| 284 | |
| 285 | bool IsNew; |
| 286 | ExplodedNodeImpl* N = Eng.G->getNodeImpl(PostStmt(S), State, &IsNew); |
| 287 | N->addPredecessor(Pred); |
| 288 | Deferred.erase(Pred); |
| 289 | |
| 290 | HasGeneratedNode = true; |
| 291 | |
| 292 | if (IsNew) { |
| 293 | Deferred.insert(N); |
| 294 | LastNode = N; |
| 295 | return N; |
| 296 | } |
| 297 | |
| 298 | LastNode = NULL; |
| 299 | return NULL; |
| 300 | } |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 301 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 302 | ExplodedNodeImpl* GRBranchNodeBuilderImpl::generateNodeImpl(void* State, |
| 303 | bool branch) { |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 304 | bool IsNew; |
| 305 | |
| 306 | ExplodedNodeImpl* Succ = |
| 307 | Eng.G->getNodeImpl(BlockEdge(Eng.getCFG(), Src, branch ? DstT : DstF), |
| 308 | State, &IsNew); |
| 309 | |
| 310 | Succ->addPredecessor(Pred); |
| 311 | |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 312 | if (branch) GeneratedTrue = true; |
| 313 | else GeneratedFalse = true; |
| 314 | |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 315 | if (IsNew) { |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 316 | Deferred.push_back(Succ); |
Ted Kremenek | b38911f | 2008-01-30 23:03:39 +0000 | [diff] [blame] | 317 | return Succ; |
| 318 | } |
| 319 | |
| 320 | return NULL; |
Ted Kremenek | 7d7fe6d | 2008-01-29 22:56:11 +0000 | [diff] [blame] | 321 | } |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 322 | |
| 323 | GRBranchNodeBuilderImpl::~GRBranchNodeBuilderImpl() { |
| 324 | if (!GeneratedTrue) generateNodeImpl(Pred->State, true); |
| 325 | if (!GeneratedFalse) generateNodeImpl(Pred->State, false); |
Ted Kremenek | 3b4f670 | 2008-01-30 23:24:39 +0000 | [diff] [blame] | 326 | |
| 327 | for (DeferredTy::iterator I=Deferred.begin(), E=Deferred.end(); I!=E; ++I) |
| 328 | if (!(*I)->isSink()) Eng.WList->Enqueue(GRWorkListUnit(*I)); |
Ted Kremenek | 71c29bd | 2008-01-29 23:32:35 +0000 | [diff] [blame] | 329 | } |