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/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h" 16 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 17 #include "clang/AST/Stmt.h" 18 #include "clang/AST/ParentMap.h" 19 #include "llvm/ADT/DenseSet.h" 20 #include "llvm/ADT/DenseMap.h" 21 #include "llvm/ADT/SmallVector.h" 22 #include <vector> 23 24 using namespace clang; 25 using namespace ento; 26 27 //===----------------------------------------------------------------------===// 28 // Node auditing. 29 //===----------------------------------------------------------------------===// 30 31 // An out of line virtual method to provide a home for the class vtable. 32 ExplodedNode::Auditor::~Auditor() {} 33 34 #ifndef NDEBUG 35 static ExplodedNode::Auditor* NodeAuditor = 0; 36 #endif 37 38 void ExplodedNode::SetAuditor(ExplodedNode::Auditor* A) { 39 #ifndef NDEBUG 40 NodeAuditor = A; 41 #endif 42 } 43 44 //===----------------------------------------------------------------------===// 45 // Cleanup. 46 //===----------------------------------------------------------------------===// 47 48 static const unsigned CounterTop = 1000; 49 50 ExplodedGraph::ExplodedGraph() 51 : NumNodes(0), reclaimNodes(false), reclaimCounter(CounterTop) {} 52 53 ExplodedGraph::~ExplodedGraph() {} 54 55 //===----------------------------------------------------------------------===// 56 // Node reclamation. 57 //===----------------------------------------------------------------------===// 58 59 bool ExplodedGraph::shouldCollect(const ExplodedNode *node) { 60 // Reclaim all nodes that match *all* the following criteria: 61 // 62 // (1) 1 predecessor (that has one successor) 63 // (2) 1 successor (that has one predecessor) 64 // (3) The ProgramPoint is for a PostStmt. 65 // (4) There is no 'tag' for the ProgramPoint. 66 // (5) The 'store' is the same as the predecessor. 67 // (6) The 'GDM' is the same as the predecessor. 68 // (7) The LocationContext is the same as the predecessor. 69 // (8) The PostStmt is for a non-consumed Stmt or Expr. 70 71 // Conditions 1 and 2. 72 if (node->pred_size() != 1 || node->succ_size() != 1) 73 return false; 74 75 const ExplodedNode *pred = *(node->pred_begin()); 76 if (pred->succ_size() != 1) 77 return false; 78 79 const ExplodedNode *succ = *(node->succ_begin()); 80 if (succ->pred_size() != 1) 81 return false; 82 83 // Condition 3. 84 ProgramPoint progPoint = node->getLocation(); 85 if (!isa<PostStmt>(progPoint) || 86 (isa<CallEnter>(progPoint) || 87 isa<CallExitBegin>(progPoint) || isa<CallExitEnd>(progPoint))) 88 return false; 89 90 // Condition 4. 91 PostStmt ps = cast<PostStmt>(progPoint); 92 if (ps.getTag()) 93 return false; 94 95 if (isa<BinaryOperator>(ps.getStmt())) 96 return false; 97 98 // Conditions 5, 6, and 7. 99 ProgramStateRef state = node->getState(); 100 ProgramStateRef pred_state = pred->getState(); 101 if (state->store != pred_state->store || state->GDM != pred_state->GDM || 102 progPoint.getLocationContext() != pred->getLocationContext()) 103 return false; 104 105 // Condition 8. 106 if (const Expr *Ex = dyn_cast<Expr>(ps.getStmt())) { 107 ParentMap &PM = progPoint.getLocationContext()->getParentMap(); 108 if (!PM.isConsumedExpr(Ex)) 109 return false; 110 } 111 112 return true; 113 } 114 115 void ExplodedGraph::collectNode(ExplodedNode *node) { 116 // Removing a node means: 117 // (a) changing the predecessors successor to the successor of this node 118 // (b) changing the successors predecessor to the predecessor of this node 119 // (c) Putting 'node' onto freeNodes. 120 assert(node->pred_size() == 1 || node->succ_size() == 1); 121 ExplodedNode *pred = *(node->pred_begin()); 122 ExplodedNode *succ = *(node->succ_begin()); 123 pred->replaceSuccessor(succ); 124 succ->replacePredecessor(pred); 125 FreeNodes.push_back(node); 126 Nodes.RemoveNode(node); 127 --NumNodes; 128 node->~ExplodedNode(); 129 } 130 131 void ExplodedGraph::reclaimRecentlyAllocatedNodes() { 132 if (ChangedNodes.empty()) 133 return; 134 135 // Only periodically relcaim nodes so that we can build up a set of 136 // nodes that meet the reclamation criteria. Freshly created nodes 137 // by definition have no successor, and thus cannot be reclaimed (see below). 138 assert(reclaimCounter > 0); 139 if (--reclaimCounter != 0) 140 return; 141 reclaimCounter = CounterTop; 142 143 for (NodeVector::iterator it = ChangedNodes.begin(), et = ChangedNodes.end(); 144 it != et; ++it) { 145 ExplodedNode *node = *it; 146 if (shouldCollect(node)) 147 collectNode(node); 148 } 149 ChangedNodes.clear(); 150 } 151 152 //===----------------------------------------------------------------------===// 153 // ExplodedNode. 154 //===----------------------------------------------------------------------===// 155 156 static inline BumpVector<ExplodedNode*>& getVector(void *P) { 157 return *reinterpret_cast<BumpVector<ExplodedNode*>*>(P); 158 } 159 160 void ExplodedNode::addPredecessor(ExplodedNode *V, ExplodedGraph &G) { 161 assert (!V->isSink()); 162 Preds.addNode(V, G); 163 V->Succs.addNode(this, G); 164 #ifndef NDEBUG 165 if (NodeAuditor) NodeAuditor->AddEdge(V, this); 166 #endif 167 } 168 169 void ExplodedNode::NodeGroup::replaceNode(ExplodedNode *node) { 170 assert(getKind() == Size1); 171 P = reinterpret_cast<uintptr_t>(node); 172 assert(getKind() == Size1); 173 } 174 175 void ExplodedNode::NodeGroup::addNode(ExplodedNode *N, ExplodedGraph &G) { 176 assert((reinterpret_cast<uintptr_t>(N) & Mask) == 0x0); 177 assert(!getFlag()); 178 179 if (getKind() == Size1) { 180 if (ExplodedNode *NOld = getNode()) { 181 BumpVectorContext &Ctx = G.getNodeAllocator(); 182 BumpVector<ExplodedNode*> *V = 183 G.getAllocator().Allocate<BumpVector<ExplodedNode*> >(); 184 new (V) BumpVector<ExplodedNode*>(Ctx, 4); 185 186 assert((reinterpret_cast<uintptr_t>(V) & Mask) == 0x0); 187 V->push_back(NOld, Ctx); 188 V->push_back(N, Ctx); 189 P = reinterpret_cast<uintptr_t>(V) | SizeOther; 190 assert(getPtr() == (void*) V); 191 assert(getKind() == SizeOther); 192 } 193 else { 194 P = reinterpret_cast<uintptr_t>(N); 195 assert(getKind() == Size1); 196 } 197 } 198 else { 199 assert(getKind() == SizeOther); 200 getVector(getPtr()).push_back(N, G.getNodeAllocator()); 201 } 202 } 203 204 unsigned ExplodedNode::NodeGroup::size() const { 205 if (getFlag()) 206 return 0; 207 208 if (getKind() == Size1) 209 return getNode() ? 1 : 0; 210 else 211 return getVector(getPtr()).size(); 212 } 213 214 ExplodedNode **ExplodedNode::NodeGroup::begin() const { 215 if (getFlag()) 216 return NULL; 217 218 if (getKind() == Size1) 219 return (ExplodedNode**) (getPtr() ? &P : NULL); 220 else 221 return const_cast<ExplodedNode**>(&*(getVector(getPtr()).begin())); 222 } 223 224 ExplodedNode** ExplodedNode::NodeGroup::end() const { 225 if (getFlag()) 226 return NULL; 227 228 if (getKind() == Size1) 229 return (ExplodedNode**) (getPtr() ? &P+1 : NULL); 230 else { 231 // Dereferencing end() is undefined behaviour. The vector is not empty, so 232 // we can dereference the last elem and then add 1 to the result. 233 return const_cast<ExplodedNode**>(getVector(getPtr()).end()); 234 } 235 } 236 237 ExplodedNode *ExplodedGraph::getNode(const ProgramPoint &L, 238 ProgramStateRef State, 239 bool IsSink, 240 bool* IsNew) { 241 // Profile 'State' to determine if we already have an existing node. 242 llvm::FoldingSetNodeID profile; 243 void *InsertPos = 0; 244 245 NodeTy::Profile(profile, L, State, IsSink); 246 NodeTy* V = Nodes.FindNodeOrInsertPos(profile, InsertPos); 247 248 if (!V) { 249 if (!FreeNodes.empty()) { 250 V = FreeNodes.back(); 251 FreeNodes.pop_back(); 252 } 253 else { 254 // Allocate a new node. 255 V = (NodeTy*) getAllocator().Allocate<NodeTy>(); 256 } 257 258 new (V) NodeTy(L, State, IsSink); 259 260 if (reclaimNodes) 261 ChangedNodes.push_back(V); 262 263 // Insert the node into the node set and return it. 264 Nodes.InsertNode(V, InsertPos); 265 ++NumNodes; 266 267 if (IsNew) *IsNew = true; 268 } 269 else 270 if (IsNew) *IsNew = false; 271 272 return V; 273 } 274 275 std::pair<ExplodedGraph*, InterExplodedGraphMap*> 276 ExplodedGraph::Trim(const NodeTy* const* NBeg, const NodeTy* const* NEnd, 277 llvm::DenseMap<const void*, const void*> *InverseMap) const { 278 279 if (NBeg == NEnd) 280 return std::make_pair((ExplodedGraph*) 0, 281 (InterExplodedGraphMap*) 0); 282 283 assert (NBeg < NEnd); 284 285 OwningPtr<InterExplodedGraphMap> M(new InterExplodedGraphMap()); 286 287 ExplodedGraph* G = TrimInternal(NBeg, NEnd, M.get(), InverseMap); 288 289 return std::make_pair(static_cast<ExplodedGraph*>(G), M.take()); 290 } 291 292 ExplodedGraph* 293 ExplodedGraph::TrimInternal(const ExplodedNode* const* BeginSources, 294 const ExplodedNode* const* EndSources, 295 InterExplodedGraphMap* M, 296 llvm::DenseMap<const void*, const void*> *InverseMap) const { 297 298 typedef llvm::DenseSet<const ExplodedNode*> Pass1Ty; 299 Pass1Ty Pass1; 300 301 typedef llvm::DenseMap<const ExplodedNode*, ExplodedNode*> Pass2Ty; 302 Pass2Ty& Pass2 = M->M; 303 304 SmallVector<const ExplodedNode*, 10> WL1, WL2; 305 306 // ===- Pass 1 (reverse DFS) -=== 307 for (const ExplodedNode* const* I = BeginSources; I != EndSources; ++I) { 308 assert(*I); 309 WL1.push_back(*I); 310 } 311 312 // Process the first worklist until it is empty. Because it is a std::list 313 // it acts like a FIFO queue. 314 while (!WL1.empty()) { 315 const ExplodedNode *N = WL1.back(); 316 WL1.pop_back(); 317 318 // Have we already visited this node? If so, continue to the next one. 319 if (Pass1.count(N)) 320 continue; 321 322 // Otherwise, mark this node as visited. 323 Pass1.insert(N); 324 325 // If this is a root enqueue it to the second worklist. 326 if (N->Preds.empty()) { 327 WL2.push_back(N); 328 continue; 329 } 330 331 // Visit our predecessors and enqueue them. 332 for (ExplodedNode** I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) 333 WL1.push_back(*I); 334 } 335 336 // We didn't hit a root? Return with a null pointer for the new graph. 337 if (WL2.empty()) 338 return 0; 339 340 // Create an empty graph. 341 ExplodedGraph* G = MakeEmptyGraph(); 342 343 // ===- Pass 2 (forward DFS to construct the new graph) -=== 344 while (!WL2.empty()) { 345 const ExplodedNode *N = WL2.back(); 346 WL2.pop_back(); 347 348 // Skip this node if we have already processed it. 349 if (Pass2.find(N) != Pass2.end()) 350 continue; 351 352 // Create the corresponding node in the new graph and record the mapping 353 // from the old node to the new node. 354 ExplodedNode *NewN = G->getNode(N->getLocation(), N->State, N->isSink(), 0); 355 Pass2[N] = NewN; 356 357 // Also record the reverse mapping from the new node to the old node. 358 if (InverseMap) (*InverseMap)[NewN] = N; 359 360 // If this node is a root, designate it as such in the graph. 361 if (N->Preds.empty()) 362 G->addRoot(NewN); 363 364 // In the case that some of the intended predecessors of NewN have already 365 // been created, we should hook them up as predecessors. 366 367 // Walk through the predecessors of 'N' and hook up their corresponding 368 // nodes in the new graph (if any) to the freshly created node. 369 for (ExplodedNode **I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) { 370 Pass2Ty::iterator PI = Pass2.find(*I); 371 if (PI == Pass2.end()) 372 continue; 373 374 NewN->addPredecessor(PI->second, *G); 375 } 376 377 // In the case that some of the intended successors of NewN have already 378 // been created, we should hook them up as successors. Otherwise, enqueue 379 // the new nodes from the original graph that should have nodes created 380 // in the new graph. 381 for (ExplodedNode **I=N->Succs.begin(), **E=N->Succs.end(); I!=E; ++I) { 382 Pass2Ty::iterator PI = Pass2.find(*I); 383 if (PI != Pass2.end()) { 384 PI->second->addPredecessor(NewN, *G); 385 continue; 386 } 387 388 // Enqueue nodes to the worklist that were marked during pass 1. 389 if (Pass1.count(*I)) 390 WL2.push_back(*I); 391 } 392 } 393 394 return G; 395 } 396 397 void InterExplodedGraphMap::anchor() { } 398 399 ExplodedNode* 400 InterExplodedGraphMap::getMappedNode(const ExplodedNode *N) const { 401 llvm::DenseMap<const ExplodedNode*, ExplodedNode*>::const_iterator I = 402 M.find(N); 403 404 return I == M.end() ? 0 : I->second; 405 } 406 407