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