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