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