Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 1 | //=-- ExplodedGraph.cpp - Local, Path-Sens. "Exploded Graph" -*- 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 the template classes ExplodedNode and ExplodedGraph, |
| 11 | // which represent a path-sensitive, intra-procedural "exploded graph." |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Analysis/PathSensitive/ExplodedGraph.h" |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 16 | #include "clang/Analysis/PathSensitive/GRState.h" |
Ted Kremenek | 33c6369 | 2008-04-16 15:51:26 +0000 | [diff] [blame] | 17 | #include "clang/AST/Stmt.h" |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/DenseSet.h" |
| 19 | #include "llvm/ADT/DenseMap.h" |
| 20 | #include "llvm/ADT/SmallVector.h" |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 21 | #include <vector> |
| 22 | |
| 23 | using namespace clang; |
| 24 | |
Ted Kremenek | 2e28754 | 2008-08-27 01:56:11 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
| 26 | // Node auditing. |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | // An out of line virtual method to provide a home for the class vtable. |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 30 | ExplodedNode::Auditor::~Auditor() {} |
Ted Kremenek | 2e28754 | 2008-08-27 01:56:11 +0000 | [diff] [blame] | 31 | |
| 32 | #ifndef NDEBUG |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 33 | static ExplodedNode::Auditor* NodeAuditor = 0; |
Ted Kremenek | 2e28754 | 2008-08-27 01:56:11 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 36 | void ExplodedNode::SetAuditor(ExplodedNode::Auditor* A) { |
Ted Kremenek | 2e28754 | 2008-08-27 01:56:11 +0000 | [diff] [blame] | 37 | #ifndef NDEBUG |
| 38 | NodeAuditor = A; |
| 39 | #endif |
| 40 | } |
| 41 | |
| 42 | //===----------------------------------------------------------------------===// |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 43 | // ExplodedNode. |
Ted Kremenek | 2e28754 | 2008-08-27 01:56:11 +0000 | [diff] [blame] | 44 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 45 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 46 | static inline std::vector<ExplodedNode*>& getVector(void* P) { |
| 47 | return *reinterpret_cast<std::vector<ExplodedNode*>*>(P); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 50 | void ExplodedNode::Profile(llvm::FoldingSetNodeID& ID, |
| 51 | const ProgramPoint& Loc, |
| 52 | const GRState* state) { |
| 53 | ID.Add(Loc); |
| 54 | state->Profile(ID); |
| 55 | } |
| 56 | |
| 57 | void ExplodedNode::addPredecessor(ExplodedNode* V) { |
Ted Kremenek | 45b8789 | 2008-08-27 01:27:52 +0000 | [diff] [blame] | 58 | assert (!V->isSink()); |
| 59 | Preds.addNode(V); |
| 60 | V->Succs.addNode(this); |
Ted Kremenek | 2e28754 | 2008-08-27 01:56:11 +0000 | [diff] [blame] | 61 | #ifndef NDEBUG |
| 62 | if (NodeAuditor) NodeAuditor->AddEdge(V, this); |
| 63 | #endif |
Ted Kremenek | 45b8789 | 2008-08-27 01:27:52 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 66 | void ExplodedNode::NodeGroup::addNode(ExplodedNode* N) { |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 67 | |
| 68 | assert ((reinterpret_cast<uintptr_t>(N) & Mask) == 0x0); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 69 | assert (!getFlag()); |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 70 | |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 71 | if (getKind() == Size1) { |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 72 | if (ExplodedNode* NOld = getNode()) { |
| 73 | std::vector<ExplodedNode*>* V = new std::vector<ExplodedNode*>(); |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 74 | assert ((reinterpret_cast<uintptr_t>(V) & Mask) == 0x0); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 75 | V->push_back(NOld); |
| 76 | V->push_back(N); |
Ted Kremenek | 45c63bd | 2008-01-29 23:31:09 +0000 | [diff] [blame] | 77 | P = reinterpret_cast<uintptr_t>(V) | SizeOther; |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 78 | assert (getPtr() == (void*) V); |
| 79 | assert (getKind() == SizeOther); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 80 | } |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 81 | else { |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 82 | P = reinterpret_cast<uintptr_t>(N); |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 83 | assert (getKind() == Size1); |
| 84 | } |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 85 | } |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 86 | else { |
| 87 | assert (getKind() == SizeOther); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 88 | getVector(getPtr()).push_back(N); |
Ted Kremenek | 596f0a1 | 2008-03-05 19:08:55 +0000 | [diff] [blame] | 89 | } |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 92 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 93 | unsigned ExplodedNode::NodeGroup::size() const { |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 94 | if (getFlag()) |
| 95 | return 0; |
| 96 | |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 97 | if (getKind() == Size1) |
| 98 | return getNode() ? 1 : 0; |
| 99 | else |
| 100 | return getVector(getPtr()).size(); |
| 101 | } |
| 102 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 103 | ExplodedNode** ExplodedNode::NodeGroup::begin() const { |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 104 | if (getFlag()) |
| 105 | return NULL; |
| 106 | |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 107 | if (getKind() == Size1) |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 108 | return (ExplodedNode**) (getPtr() ? &P : NULL); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 109 | else |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 110 | return const_cast<ExplodedNode**>(&*(getVector(getPtr()).begin())); |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 113 | ExplodedNode** ExplodedNode::NodeGroup::end() const { |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 114 | if (getFlag()) |
| 115 | return NULL; |
| 116 | |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 117 | if (getKind() == Size1) |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 118 | return (ExplodedNode**) (getPtr() ? &P+1 : NULL); |
Ted Kremenek | 7b98957 | 2008-04-20 23:54:24 +0000 | [diff] [blame] | 119 | else { |
| 120 | // Dereferencing end() is undefined behaviour. The vector is not empty, so |
Argyrios Kyrtzidis | 5fc073f | 2008-04-22 07:37:18 +0000 | [diff] [blame] | 121 | // we can dereference the last elem and then add 1 to the result. |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 122 | return const_cast<ExplodedNode**>(&getVector(getPtr()).back()) + 1; |
Ted Kremenek | 7b98957 | 2008-04-20 23:54:24 +0000 | [diff] [blame] | 123 | } |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 126 | ExplodedNode::NodeGroup::~NodeGroup() { |
Ted Kremenek | 9eb49a4 | 2008-01-13 04:56:13 +0000 | [diff] [blame] | 127 | if (getKind() == SizeOther) delete &getVector(getPtr()); |
| 128 | } |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 129 | |
Ted Kremenek | 3148eb4 | 2009-01-24 00:55:43 +0000 | [diff] [blame] | 130 | ExplodedGraphImpl* |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 131 | ExplodedGraphImpl::Trim(const ExplodedNode* const* BeginSources, |
| 132 | const ExplodedNode* const* EndSources, |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 133 | InterExplodedGraphMapImpl* M, |
| 134 | llvm::DenseMap<const void*, const void*> *InverseMap) |
| 135 | const { |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 136 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 137 | typedef llvm::DenseSet<const ExplodedNode*> Pass1Ty; |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 138 | Pass1Ty Pass1; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 139 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 140 | typedef llvm::DenseMap<const ExplodedNode*, ExplodedNode*> Pass2Ty; |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 141 | Pass2Ty& Pass2 = M->M; |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 142 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 143 | llvm::SmallVector<const ExplodedNode*, 10> WL1, WL2; |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 144 | |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 145 | // ===- Pass 1 (reverse DFS) -=== |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 146 | for (const ExplodedNode* const* I = BeginSources; I != EndSources; ++I) { |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 147 | assert(*I); |
| 148 | WL1.push_back(*I); |
| 149 | } |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 150 | |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 151 | // Process the first worklist until it is empty. Because it is a std::list |
| 152 | // it acts like a FIFO queue. |
| 153 | while (!WL1.empty()) { |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 154 | const ExplodedNode *N = WL1.back(); |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 155 | WL1.pop_back(); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 156 | |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 157 | // Have we already visited this node? If so, continue to the next one. |
| 158 | if (Pass1.count(N)) |
| 159 | continue; |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 160 | |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 161 | // Otherwise, mark this node as visited. |
| 162 | Pass1.insert(N); |
| 163 | |
| 164 | // If this is a root enqueue it to the second worklist. |
| 165 | if (N->Preds.empty()) { |
| 166 | WL2.push_back(N); |
| 167 | continue; |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 168 | } |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 169 | |
| 170 | // Visit our predecessors and enqueue them. |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 171 | for (ExplodedNode** I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) |
Ted Kremenek | 10aa554 | 2009-03-12 23:41:59 +0000 | [diff] [blame] | 172 | WL1.push_back(*I); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 173 | } |
| 174 | |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 175 | // We didn't hit a root? Return with a null pointer for the new graph. |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 176 | if (WL2.empty()) |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 177 | return 0; |
| 178 | |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 179 | // Create an empty graph. |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 180 | ExplodedGraphImpl* G = MakeEmptyGraph(); |
| 181 | |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 182 | // ===- Pass 2 (forward DFS to construct the new graph) -=== |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 183 | while (!WL2.empty()) { |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 184 | const ExplodedNode* N = WL2.back(); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 185 | WL2.pop_back(); |
| 186 | |
| 187 | // Skip this node if we have already processed it. |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 188 | if (Pass2.find(N) != Pass2.end()) |
| 189 | continue; |
| 190 | |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 191 | // Create the corresponding node in the new graph and record the mapping |
| 192 | // from the old node to the new node. |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 193 | ExplodedNode* NewN = G->getNodeImpl(N->getLocation(), N->State, NULL); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 194 | Pass2[N] = NewN; |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 195 | |
| 196 | // Also record the reverse mapping from the new node to the old node. |
Ted Kremenek | fe9e543 | 2009-02-18 03:48:14 +0000 | [diff] [blame] | 197 | if (InverseMap) (*InverseMap)[NewN] = N; |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 198 | |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 199 | // If this node is a root, designate it as such in the graph. |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 200 | if (N->Preds.empty()) |
| 201 | G->addRoot(NewN); |
| 202 | |
| 203 | // In the case that some of the intended predecessors of NewN have already |
| 204 | // been created, we should hook them up as predecessors. |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 205 | |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 206 | // Walk through the predecessors of 'N' and hook up their corresponding |
| 207 | // nodes in the new graph (if any) to the freshly created node. |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 208 | for (ExplodedNode **I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) { |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 209 | Pass2Ty::iterator PI = Pass2.find(*I); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 210 | if (PI == Pass2.end()) |
| 211 | continue; |
| 212 | |
| 213 | NewN->addPredecessor(PI->second); |
| 214 | } |
| 215 | |
| 216 | // In the case that some of the intended successors of NewN have already |
| 217 | // been created, we should hook them up as successors. Otherwise, enqueue |
| 218 | // the new nodes from the original graph that should have nodes created |
| 219 | // in the new graph. |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 220 | for (ExplodedNode **I=N->Succs.begin(), **E=N->Succs.end(); I!=E; ++I) { |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 221 | Pass2Ty::iterator PI = Pass2.find(*I); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 222 | if (PI != Pass2.end()) { |
| 223 | PI->second->addPredecessor(NewN); |
| 224 | continue; |
| 225 | } |
| 226 | |
| 227 | // Enqueue nodes to the worklist that were marked during pass 1. |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 228 | if (Pass1.count(*I)) |
| 229 | WL2.push_back(*I); |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Ted Kremenek | df96929 | 2009-02-20 21:10:26 +0000 | [diff] [blame] | 232 | // Finally, explictly mark all nodes without any successors as sinks. |
Ted Kremenek | ffe0f43 | 2008-03-07 22:58:01 +0000 | [diff] [blame] | 233 | if (N->isSink()) |
| 234 | NewN->markAsSink(); |
| 235 | } |
| 236 | |
| 237 | return G; |
| 238 | } |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 239 | |
Zhongxing Xu | c5619d9 | 2009-08-06 01:32:16 +0000 | [diff] [blame] | 240 | ExplodedNode* |
| 241 | InterExplodedGraphMapImpl::getMappedImplNode(const ExplodedNode* N) const { |
| 242 | llvm::DenseMap<const ExplodedNode*, ExplodedNode*>::iterator I = |
Ted Kremenek | cf118d4 | 2009-02-04 23:49:09 +0000 | [diff] [blame] | 243 | M.find(N); |
| 244 | |
| 245 | return I == M.end() ? 0 : I->second; |
| 246 | } |
| 247 | |
| 248 | InterExplodedGraphMapImpl::InterExplodedGraphMapImpl() {} |
| 249 | |