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