ExplodedGraph.h revision a5888f61be9f8d76e9b48a453dbced50523bd2e0
15c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu//=-- ExplodedGraph.h - Local, Path-Sens. "Exploded Graph" -*- C++ -*-------==// 25c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// 35c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// The LLVM Compiler Infrastructure 45c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu// 51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// This file is distributed under the University of Illinois Open Source 61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// 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// See "Precise interprocedural dataflow analysis via graph reachability" 13// by Reps, Horwitz, and Sagiv 14// (http://portal.acm.org/citation.cfm?id=199462) for the definition of an 15// exploded graph. 16// 17//===----------------------------------------------------------------------===// 18 19#ifndef LLVM_CLANG_GR_EXPLODEDGRAPH 20#define LLVM_CLANG_GR_EXPLODEDGRAPH 21 22#include "clang/Analysis/ProgramPoint.h" 23#include "clang/Analysis/AnalysisContext.h" 24#include "clang/AST/Decl.h" 25#include "llvm/ADT/SmallVector.h" 26#include "llvm/ADT/FoldingSet.h" 27#include "llvm/ADT/SmallPtrSet.h" 28#include "llvm/Support/Allocator.h" 29#include "llvm/ADT/OwningPtr.h" 30#include "llvm/ADT/GraphTraits.h" 31#include "llvm/ADT/DepthFirstIterator.h" 32#include "llvm/Support/Casting.h" 33#include "clang/Analysis/Support/BumpVector.h" 34#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 35 36namespace clang { 37 38class CFG; 39 40namespace ento { 41 42class ExplodedGraph; 43 44//===----------------------------------------------------------------------===// 45// ExplodedGraph "implementation" classes. These classes are not typed to 46// contain a specific kind of state. Typed-specialized versions are defined 47// on top of these classes. 48//===----------------------------------------------------------------------===// 49 50// ExplodedNode is not constified all over the engine because we need to add 51// successors to it at any time after creating it. 52 53class ExplodedNode : public llvm::FoldingSetNode { 54 friend class ExplodedGraph; 55 friend class CoreEngine; 56 friend class NodeBuilder; 57 friend class BranchNodeBuilder; 58 friend class IndirectGotoNodeBuilder; 59 friend class SwitchNodeBuilder; 60 friend class EndOfFunctionNodeBuilder; 61 62 class NodeGroup { 63 enum { Size1 = 0x0, SizeOther = 0x1, AuxFlag = 0x2, Mask = 0x3 }; 64 uintptr_t P; 65 66 unsigned getKind() const { 67 return P & 0x1; 68 } 69 70 void *getPtr() const { 71 assert (!getFlag()); 72 return reinterpret_cast<void*>(P & ~Mask); 73 } 74 75 ExplodedNode *getNode() const { 76 return reinterpret_cast<ExplodedNode*>(getPtr()); 77 } 78 79 public: 80 NodeGroup() : P(0) {} 81 82 ExplodedNode **begin() const; 83 84 ExplodedNode **end() const; 85 86 unsigned size() const; 87 88 bool empty() const { return (P & ~Mask) == 0; } 89 90 void addNode(ExplodedNode *N, ExplodedGraph &G); 91 92 void replaceNode(ExplodedNode *node); 93 94 void setFlag() { 95 assert(P == 0); 96 P = AuxFlag; 97 } 98 99 bool getFlag() const { 100 return P & AuxFlag ? true : false; 101 } 102 }; 103 104 /// Location - The program location (within a function body) associated 105 /// with this node. 106 const ProgramPoint Location; 107 108 /// State - The state associated with this node. 109 ProgramStateRef State; 110 111 /// Preds - The predecessors of this node. 112 NodeGroup Preds; 113 114 /// Succs - The successors of this node. 115 NodeGroup Succs; 116 117public: 118 119 explicit ExplodedNode(const ProgramPoint &loc, ProgramStateRef state, 120 bool IsSink) 121 : Location(loc), State(state) { 122 if (IsSink) 123 Succs.setFlag(); 124 } 125 126 ~ExplodedNode() {} 127 128 /// getLocation - Returns the edge associated with the given node. 129 ProgramPoint getLocation() const { return Location; } 130 131 const LocationContext *getLocationContext() const { 132 return getLocation().getLocationContext(); 133 } 134 135 const Decl &getCodeDecl() const { return *getLocationContext()->getDecl(); } 136 137 CFG &getCFG() const { return *getLocationContext()->getCFG(); } 138 139 ParentMap &getParentMap() const {return getLocationContext()->getParentMap();} 140 141 template <typename T> 142 T &getAnalysis() const { 143 return *getLocationContext()->getAnalysis<T>(); 144 } 145 146 ProgramStateRef getState() const { return State; } 147 148 template <typename T> 149 const T* getLocationAs() const { return llvm::dyn_cast<T>(&Location); } 150 151 static void Profile(llvm::FoldingSetNodeID &ID, 152 const ProgramPoint &Loc, 153 ProgramStateRef state, 154 bool IsSink) { 155 ID.Add(Loc); 156 ID.AddPointer(state.getPtr()); 157 ID.AddBoolean(IsSink); 158 } 159 160 void Profile(llvm::FoldingSetNodeID& ID) const { 161 Profile(ID, getLocation(), getState(), isSink()); 162 } 163 164 /// addPredeccessor - Adds a predecessor to the current node, and 165 /// in tandem add this node as a successor of the other node. 166 void addPredecessor(ExplodedNode *V, ExplodedGraph &G); 167 168 unsigned succ_size() const { return Succs.size(); } 169 unsigned pred_size() const { return Preds.size(); } 170 bool succ_empty() const { return Succs.empty(); } 171 bool pred_empty() const { return Preds.empty(); } 172 173 bool isSink() const { return Succs.getFlag(); } 174 175 ExplodedNode *getFirstPred() { 176 return pred_empty() ? NULL : *(pred_begin()); 177 } 178 179 const ExplodedNode *getFirstPred() const { 180 return const_cast<ExplodedNode*>(this)->getFirstPred(); 181 } 182 183 // Iterators over successor and predecessor vertices. 184 typedef ExplodedNode** succ_iterator; 185 typedef const ExplodedNode* const * const_succ_iterator; 186 typedef ExplodedNode** pred_iterator; 187 typedef const ExplodedNode* const * const_pred_iterator; 188 189 pred_iterator pred_begin() { return Preds.begin(); } 190 pred_iterator pred_end() { return Preds.end(); } 191 192 const_pred_iterator pred_begin() const { 193 return const_cast<ExplodedNode*>(this)->pred_begin(); 194 } 195 const_pred_iterator pred_end() const { 196 return const_cast<ExplodedNode*>(this)->pred_end(); 197 } 198 199 succ_iterator succ_begin() { return Succs.begin(); } 200 succ_iterator succ_end() { return Succs.end(); } 201 202 const_succ_iterator succ_begin() const { 203 return const_cast<ExplodedNode*>(this)->succ_begin(); 204 } 205 const_succ_iterator succ_end() const { 206 return const_cast<ExplodedNode*>(this)->succ_end(); 207 } 208 209 // For debugging. 210 211public: 212 213 class Auditor { 214 public: 215 virtual ~Auditor(); 216 virtual void AddEdge(ExplodedNode *Src, ExplodedNode *Dst) = 0; 217 }; 218 219 static void SetAuditor(Auditor* A); 220 221private: 222 void replaceSuccessor(ExplodedNode *node) { Succs.replaceNode(node); } 223 void replacePredecessor(ExplodedNode *node) { Preds.replaceNode(node); } 224}; 225 226// FIXME: Is this class necessary? 227class InterExplodedGraphMap { 228 virtual void anchor(); 229 llvm::DenseMap<const ExplodedNode*, ExplodedNode*> M; 230 friend class ExplodedGraph; 231 232public: 233 ExplodedNode *getMappedNode(const ExplodedNode *N) const; 234 235 InterExplodedGraphMap() {} 236 virtual ~InterExplodedGraphMap() {} 237}; 238 239class ExplodedGraph { 240protected: 241 friend class CoreEngine; 242 243 // Type definitions. 244 typedef SmallVector<ExplodedNode*,2> RootsTy; 245 typedef SmallVector<ExplodedNode*,10> EndNodesTy; 246 247 /// Roots - The roots of the simulation graph. Usually there will be only 248 /// one, but clients are free to establish multiple subgraphs within a single 249 /// SimulGraph. Moreover, these subgraphs can often merge when paths from 250 /// different roots reach the same state at the same program location. 251 RootsTy Roots; 252 253 /// EndNodes - The nodes in the simulation graph which have been 254 /// specially marked as the endpoint of an abstract simulation path. 255 EndNodesTy EndNodes; 256 257 /// Nodes - The nodes in the graph. 258 llvm::FoldingSet<ExplodedNode> Nodes; 259 260 /// BVC - Allocator and context for allocating nodes and their predecessor 261 /// and successor groups. 262 BumpVectorContext BVC; 263 264 /// NumNodes - The number of nodes in the graph. 265 unsigned NumNodes; 266 267 /// A list of recently allocated nodes that can potentially be recycled. 268 void *recentlyAllocatedNodes; 269 270 /// A list of nodes that can be reused. 271 void *freeNodes; 272 273 /// A flag that indicates whether nodes should be recycled. 274 bool reclaimNodes; 275 276 /// Counter to determine when to reclaim nodes. 277 unsigned reclaimCounter; 278 279public: 280 281 /// \brief Retrieve the node associated with a (Location,State) pair, 282 /// where the 'Location' is a ProgramPoint in the CFG. If no node for 283 /// this pair exists, it is created. IsNew is set to true if 284 /// the node was freshly created. 285 ExplodedNode *getNode(const ProgramPoint &L, ProgramStateRef State, 286 bool IsSink = false, 287 bool* IsNew = 0); 288 289 ExplodedGraph* MakeEmptyGraph() const { 290 return new ExplodedGraph(); 291 } 292 293 /// addRoot - Add an untyped node to the set of roots. 294 ExplodedNode *addRoot(ExplodedNode *V) { 295 Roots.push_back(V); 296 return V; 297 } 298 299 /// addEndOfPath - Add an untyped node to the set of EOP nodes. 300 ExplodedNode *addEndOfPath(ExplodedNode *V) { 301 EndNodes.push_back(V); 302 return V; 303 } 304 305 ExplodedGraph(); 306 307 ~ExplodedGraph(); 308 309 unsigned num_roots() const { return Roots.size(); } 310 unsigned num_eops() const { return EndNodes.size(); } 311 312 bool empty() const { return NumNodes == 0; } 313 unsigned size() const { return NumNodes; } 314 315 // Iterators. 316 typedef ExplodedNode NodeTy; 317 typedef llvm::FoldingSet<ExplodedNode> AllNodesTy; 318 typedef NodeTy** roots_iterator; 319 typedef NodeTy* const * const_roots_iterator; 320 typedef NodeTy** eop_iterator; 321 typedef NodeTy* const * const_eop_iterator; 322 typedef AllNodesTy::iterator node_iterator; 323 typedef AllNodesTy::const_iterator const_node_iterator; 324 325 node_iterator nodes_begin() { return Nodes.begin(); } 326 327 node_iterator nodes_end() { return Nodes.end(); } 328 329 const_node_iterator nodes_begin() const { return Nodes.begin(); } 330 331 const_node_iterator nodes_end() const { return Nodes.end(); } 332 333 roots_iterator roots_begin() { return Roots.begin(); } 334 335 roots_iterator roots_end() { return Roots.end(); } 336 337 const_roots_iterator roots_begin() const { return Roots.begin(); } 338 339 const_roots_iterator roots_end() const { return Roots.end(); } 340 341 eop_iterator eop_begin() { return EndNodes.begin(); } 342 343 eop_iterator eop_end() { return EndNodes.end(); } 344 345 const_eop_iterator eop_begin() const { return EndNodes.begin(); } 346 347 const_eop_iterator eop_end() const { return EndNodes.end(); } 348 349 llvm::BumpPtrAllocator & getAllocator() { return BVC.getAllocator(); } 350 BumpVectorContext &getNodeAllocator() { return BVC; } 351 352 typedef llvm::DenseMap<const ExplodedNode*, ExplodedNode*> NodeMap; 353 354 std::pair<ExplodedGraph*, InterExplodedGraphMap*> 355 Trim(const NodeTy* const* NBeg, const NodeTy* const* NEnd, 356 llvm::DenseMap<const void*, const void*> *InverseMap = 0) const; 357 358 ExplodedGraph* TrimInternal(const ExplodedNode* const * NBeg, 359 const ExplodedNode* const * NEnd, 360 InterExplodedGraphMap *M, 361 llvm::DenseMap<const void*, const void*> *InverseMap) const; 362 363 /// Enable tracking of recently allocated nodes for potential reclamation 364 /// when calling reclaimRecentlyAllocatedNodes(). 365 void enableNodeReclamation() { reclaimNodes = true; } 366 367 /// Reclaim "uninteresting" nodes created since the last time this method 368 /// was called. 369 void reclaimRecentlyAllocatedNodes(); 370 371private: 372 bool shouldCollect(const ExplodedNode *node); 373 void collectNode(ExplodedNode *node); 374}; 375 376class ExplodedNodeSet { 377 typedef llvm::SmallPtrSet<ExplodedNode*,5> ImplTy; 378 ImplTy Impl; 379 380public: 381 ExplodedNodeSet(ExplodedNode *N) { 382 assert (N && !static_cast<ExplodedNode*>(N)->isSink()); 383 Impl.insert(N); 384 } 385 386 ExplodedNodeSet() {} 387 388 inline void Add(ExplodedNode *N) { 389 if (N && !static_cast<ExplodedNode*>(N)->isSink()) Impl.insert(N); 390 } 391 392 typedef ImplTy::iterator iterator; 393 typedef ImplTy::const_iterator const_iterator; 394 395 unsigned size() const { return Impl.size(); } 396 bool empty() const { return Impl.empty(); } 397 bool erase(ExplodedNode *N) { return Impl.erase(N); } 398 399 void clear() { Impl.clear(); } 400 void insert(const ExplodedNodeSet &S) { 401 assert(&S != this); 402 if (empty()) 403 Impl = S.Impl; 404 else 405 Impl.insert(S.begin(), S.end()); 406 } 407 408 inline iterator begin() { return Impl.begin(); } 409 inline iterator end() { return Impl.end(); } 410 411 inline const_iterator begin() const { return Impl.begin(); } 412 inline const_iterator end() const { return Impl.end(); } 413}; 414 415} // end GR namespace 416 417} // end clang namespace 418 419// GraphTraits 420 421namespace llvm { 422 template<> struct GraphTraits<clang::ento::ExplodedNode*> { 423 typedef clang::ento::ExplodedNode NodeType; 424 typedef NodeType::succ_iterator ChildIteratorType; 425 typedef llvm::df_iterator<NodeType*> nodes_iterator; 426 427 static inline NodeType* getEntryNode(NodeType* N) { 428 return N; 429 } 430 431 static inline ChildIteratorType child_begin(NodeType* N) { 432 return N->succ_begin(); 433 } 434 435 static inline ChildIteratorType child_end(NodeType* N) { 436 return N->succ_end(); 437 } 438 439 static inline nodes_iterator nodes_begin(NodeType* N) { 440 return df_begin(N); 441 } 442 443 static inline nodes_iterator nodes_end(NodeType* N) { 444 return df_end(N); 445 } 446 }; 447 448 template<> struct GraphTraits<const clang::ento::ExplodedNode*> { 449 typedef const clang::ento::ExplodedNode NodeType; 450 typedef NodeType::const_succ_iterator ChildIteratorType; 451 typedef llvm::df_iterator<NodeType*> nodes_iterator; 452 453 static inline NodeType* getEntryNode(NodeType* N) { 454 return N; 455 } 456 457 static inline ChildIteratorType child_begin(NodeType* N) { 458 return N->succ_begin(); 459 } 460 461 static inline ChildIteratorType child_end(NodeType* N) { 462 return N->succ_end(); 463 } 464 465 static inline nodes_iterator nodes_begin(NodeType* N) { 466 return df_begin(N); 467 } 468 469 static inline nodes_iterator nodes_end(NodeType* N) { 470 return df_end(N); 471 } 472 }; 473 474} // end llvm namespace 475 476#endif 477