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/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 #include <vector> 36 37 namespace clang { 38 39 class CFG; 40 41 namespace ento { 42 43 class 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 54 class 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: P(Flag)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 empty()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. getFlag()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 123 public: 124 ExplodedNode(const ProgramPoint & loc,ProgramStateRef state,bool IsSink)125 explicit ExplodedNode(const ProgramPoint &loc, ProgramStateRef state, 126 bool IsSink) 127 : Location(loc), State(state), Succs(IsSink) { 128 assert(isSink() == IsSink); 129 } 130 ~ExplodedNode()131 ~ExplodedNode() {} 132 133 /// getLocation - Returns the edge associated with the given node. getLocation()134 ProgramPoint getLocation() const { return Location; } 135 getLocationContext()136 const LocationContext *getLocationContext() const { 137 return getLocation().getLocationContext(); 138 } 139 getStackFrame()140 const StackFrameContext *getStackFrame() const { 141 return getLocationContext()->getCurrentStackFrame(); 142 } 143 getCodeDecl()144 const Decl &getCodeDecl() const { return *getLocationContext()->getDecl(); } 145 getCFG()146 CFG &getCFG() const { return *getLocationContext()->getCFG(); } 147 getParentMap()148 ParentMap &getParentMap() const {return getLocationContext()->getParentMap();} 149 150 template <typename T> getAnalysis()151 T &getAnalysis() const { 152 return *getLocationContext()->getAnalysis<T>(); 153 } 154 getState()155 ProgramStateRef getState() const { return State; } 156 157 template <typename T> getLocationAs()158 const T* getLocationAs() const { return llvm::dyn_cast<T>(&Location); } 159 Profile(llvm::FoldingSetNodeID & ID,const ProgramPoint & Loc,const ProgramStateRef & state,bool IsSink)160 static void Profile(llvm::FoldingSetNodeID &ID, 161 const ProgramPoint &Loc, 162 const ProgramStateRef &state, 163 bool IsSink) { 164 ID.Add(Loc); 165 ID.AddPointer(state.getPtr()); 166 ID.AddBoolean(IsSink); 167 } 168 Profile(llvm::FoldingSetNodeID & ID)169 void Profile(llvm::FoldingSetNodeID& ID) const { 170 Profile(ID, getLocation(), getState(), isSink()); 171 } 172 173 /// addPredeccessor - Adds a predecessor to the current node, and 174 /// in tandem add this node as a successor of the other node. 175 void addPredecessor(ExplodedNode *V, ExplodedGraph &G); 176 succ_size()177 unsigned succ_size() const { return Succs.size(); } pred_size()178 unsigned pred_size() const { return Preds.size(); } succ_empty()179 bool succ_empty() const { return Succs.empty(); } pred_empty()180 bool pred_empty() const { return Preds.empty(); } 181 isSink()182 bool isSink() const { return Succs.getFlag(); } 183 hasSinglePred()184 bool hasSinglePred() const { 185 return (pred_size() == 1); 186 } 187 getFirstPred()188 ExplodedNode *getFirstPred() { 189 return pred_empty() ? NULL : *(pred_begin()); 190 } 191 getFirstPred()192 const ExplodedNode *getFirstPred() const { 193 return const_cast<ExplodedNode*>(this)->getFirstPred(); 194 } 195 196 // Iterators over successor and predecessor vertices. 197 typedef ExplodedNode* const * succ_iterator; 198 typedef const ExplodedNode* const * const_succ_iterator; 199 typedef ExplodedNode* const * pred_iterator; 200 typedef const ExplodedNode* const * const_pred_iterator; 201 pred_begin()202 pred_iterator pred_begin() { return Preds.begin(); } pred_end()203 pred_iterator pred_end() { return Preds.end(); } 204 pred_begin()205 const_pred_iterator pred_begin() const { 206 return const_cast<ExplodedNode*>(this)->pred_begin(); 207 } pred_end()208 const_pred_iterator pred_end() const { 209 return const_cast<ExplodedNode*>(this)->pred_end(); 210 } 211 succ_begin()212 succ_iterator succ_begin() { return Succs.begin(); } succ_end()213 succ_iterator succ_end() { return Succs.end(); } 214 succ_begin()215 const_succ_iterator succ_begin() const { 216 return const_cast<ExplodedNode*>(this)->succ_begin(); 217 } succ_end()218 const_succ_iterator succ_end() const { 219 return const_cast<ExplodedNode*>(this)->succ_end(); 220 } 221 222 // For debugging. 223 224 public: 225 226 class Auditor { 227 public: 228 virtual ~Auditor(); 229 virtual void AddEdge(ExplodedNode *Src, ExplodedNode *Dst) = 0; 230 }; 231 232 static void SetAuditor(Auditor* A); 233 234 private: replaceSuccessor(ExplodedNode * node)235 void replaceSuccessor(ExplodedNode *node) { Succs.replaceNode(node); } replacePredecessor(ExplodedNode * node)236 void replacePredecessor(ExplodedNode *node) { Preds.replaceNode(node); } 237 }; 238 239 // FIXME: Is this class necessary? 240 class InterExplodedGraphMap { 241 virtual void anchor(); 242 llvm::DenseMap<const ExplodedNode*, ExplodedNode*> M; 243 friend class ExplodedGraph; 244 245 public: 246 ExplodedNode *getMappedNode(const ExplodedNode *N) const; 247 InterExplodedGraphMap()248 InterExplodedGraphMap() {} ~InterExplodedGraphMap()249 virtual ~InterExplodedGraphMap() {} 250 }; 251 252 class ExplodedGraph { 253 protected: 254 friend class CoreEngine; 255 256 // Type definitions. 257 typedef std::vector<ExplodedNode *> NodeVector; 258 259 /// The roots of the simulation graph. Usually there will be only 260 /// one, but clients are free to establish multiple subgraphs within a single 261 /// SimulGraph. Moreover, these subgraphs can often merge when paths from 262 /// different roots reach the same state at the same program location. 263 NodeVector Roots; 264 265 /// The nodes in the simulation graph which have been 266 /// specially marked as the endpoint of an abstract simulation path. 267 NodeVector EndNodes; 268 269 /// Nodes - The nodes in the graph. 270 llvm::FoldingSet<ExplodedNode> Nodes; 271 272 /// BVC - Allocator and context for allocating nodes and their predecessor 273 /// and successor groups. 274 BumpVectorContext BVC; 275 276 /// NumNodes - The number of nodes in the graph. 277 unsigned NumNodes; 278 279 /// A list of recently allocated nodes that can potentially be recycled. 280 NodeVector ChangedNodes; 281 282 /// A list of nodes that can be reused. 283 NodeVector FreeNodes; 284 285 /// A flag that indicates whether nodes should be recycled. 286 bool reclaimNodes; 287 288 /// Counter to determine when to reclaim nodes. 289 unsigned reclaimCounter; 290 291 public: 292 293 /// \brief Retrieve the node associated with a (Location,State) pair, 294 /// where the 'Location' is a ProgramPoint in the CFG. If no node for 295 /// this pair exists, it is created. IsNew is set to true if 296 /// the node was freshly created. 297 ExplodedNode *getNode(const ProgramPoint &L, ProgramStateRef State, 298 bool IsSink = false, 299 bool* IsNew = 0); 300 MakeEmptyGraph()301 ExplodedGraph* MakeEmptyGraph() const { 302 return new ExplodedGraph(); 303 } 304 305 /// addRoot - Add an untyped node to the set of roots. addRoot(ExplodedNode * V)306 ExplodedNode *addRoot(ExplodedNode *V) { 307 Roots.push_back(V); 308 return V; 309 } 310 311 /// addEndOfPath - Add an untyped node to the set of EOP nodes. addEndOfPath(ExplodedNode * V)312 ExplodedNode *addEndOfPath(ExplodedNode *V) { 313 EndNodes.push_back(V); 314 return V; 315 } 316 317 ExplodedGraph(); 318 319 ~ExplodedGraph(); 320 num_roots()321 unsigned num_roots() const { return Roots.size(); } num_eops()322 unsigned num_eops() const { return EndNodes.size(); } 323 empty()324 bool empty() const { return NumNodes == 0; } size()325 unsigned size() const { return NumNodes; } 326 327 // Iterators. 328 typedef ExplodedNode NodeTy; 329 typedef llvm::FoldingSet<ExplodedNode> AllNodesTy; 330 typedef NodeVector::iterator roots_iterator; 331 typedef NodeVector::const_iterator const_roots_iterator; 332 typedef NodeVector::iterator eop_iterator; 333 typedef NodeVector::const_iterator const_eop_iterator; 334 typedef AllNodesTy::iterator node_iterator; 335 typedef AllNodesTy::const_iterator const_node_iterator; 336 nodes_begin()337 node_iterator nodes_begin() { return Nodes.begin(); } 338 nodes_end()339 node_iterator nodes_end() { return Nodes.end(); } 340 nodes_begin()341 const_node_iterator nodes_begin() const { return Nodes.begin(); } 342 nodes_end()343 const_node_iterator nodes_end() const { return Nodes.end(); } 344 roots_begin()345 roots_iterator roots_begin() { return Roots.begin(); } 346 roots_end()347 roots_iterator roots_end() { return Roots.end(); } 348 roots_begin()349 const_roots_iterator roots_begin() const { return Roots.begin(); } 350 roots_end()351 const_roots_iterator roots_end() const { return Roots.end(); } 352 eop_begin()353 eop_iterator eop_begin() { return EndNodes.begin(); } 354 eop_end()355 eop_iterator eop_end() { return EndNodes.end(); } 356 eop_begin()357 const_eop_iterator eop_begin() const { return EndNodes.begin(); } 358 eop_end()359 const_eop_iterator eop_end() const { return EndNodes.end(); } 360 getAllocator()361 llvm::BumpPtrAllocator & getAllocator() { return BVC.getAllocator(); } getNodeAllocator()362 BumpVectorContext &getNodeAllocator() { return BVC; } 363 364 typedef llvm::DenseMap<const ExplodedNode*, ExplodedNode*> NodeMap; 365 366 std::pair<ExplodedGraph*, InterExplodedGraphMap*> 367 Trim(const NodeTy* const* NBeg, const NodeTy* const* NEnd, 368 llvm::DenseMap<const void*, const void*> *InverseMap = 0) const; 369 370 ExplodedGraph* TrimInternal(const ExplodedNode* const * NBeg, 371 const ExplodedNode* const * NEnd, 372 InterExplodedGraphMap *M, 373 llvm::DenseMap<const void*, const void*> *InverseMap) const; 374 375 /// Enable tracking of recently allocated nodes for potential reclamation 376 /// when calling reclaimRecentlyAllocatedNodes(). enableNodeReclamation()377 void enableNodeReclamation() { reclaimNodes = true; } 378 379 /// Reclaim "uninteresting" nodes created since the last time this method 380 /// was called. 381 void reclaimRecentlyAllocatedNodes(); 382 383 private: 384 bool shouldCollect(const ExplodedNode *node); 385 void collectNode(ExplodedNode *node); 386 }; 387 388 class ExplodedNodeSet { 389 typedef llvm::SmallPtrSet<ExplodedNode*,5> ImplTy; 390 ImplTy Impl; 391 392 public: ExplodedNodeSet(ExplodedNode * N)393 ExplodedNodeSet(ExplodedNode *N) { 394 assert (N && !static_cast<ExplodedNode*>(N)->isSink()); 395 Impl.insert(N); 396 } 397 ExplodedNodeSet()398 ExplodedNodeSet() {} 399 Add(ExplodedNode * N)400 inline void Add(ExplodedNode *N) { 401 if (N && !static_cast<ExplodedNode*>(N)->isSink()) Impl.insert(N); 402 } 403 404 typedef ImplTy::iterator iterator; 405 typedef ImplTy::const_iterator const_iterator; 406 size()407 unsigned size() const { return Impl.size(); } empty()408 bool empty() const { return Impl.empty(); } erase(ExplodedNode * N)409 bool erase(ExplodedNode *N) { return Impl.erase(N); } 410 clear()411 void clear() { Impl.clear(); } insert(const ExplodedNodeSet & S)412 void insert(const ExplodedNodeSet &S) { 413 assert(&S != this); 414 if (empty()) 415 Impl = S.Impl; 416 else 417 Impl.insert(S.begin(), S.end()); 418 } 419 begin()420 inline iterator begin() { return Impl.begin(); } end()421 inline iterator end() { return Impl.end(); } 422 begin()423 inline const_iterator begin() const { return Impl.begin(); } end()424 inline const_iterator end() const { return Impl.end(); } 425 }; 426 427 } // end GR namespace 428 429 } // end clang namespace 430 431 // GraphTraits 432 433 namespace llvm { 434 template<> struct GraphTraits<clang::ento::ExplodedNode*> { 435 typedef clang::ento::ExplodedNode NodeType; 436 typedef NodeType::succ_iterator ChildIteratorType; 437 typedef llvm::df_iterator<NodeType*> nodes_iterator; 438 439 static inline NodeType* getEntryNode(NodeType* N) { 440 return N; 441 } 442 443 static inline ChildIteratorType child_begin(NodeType* N) { 444 return N->succ_begin(); 445 } 446 447 static inline ChildIteratorType child_end(NodeType* N) { 448 return N->succ_end(); 449 } 450 451 static inline nodes_iterator nodes_begin(NodeType* N) { 452 return df_begin(N); 453 } 454 455 static inline nodes_iterator nodes_end(NodeType* N) { 456 return df_end(N); 457 } 458 }; 459 460 template<> struct GraphTraits<const clang::ento::ExplodedNode*> { 461 typedef const clang::ento::ExplodedNode NodeType; 462 typedef NodeType::const_succ_iterator ChildIteratorType; 463 typedef llvm::df_iterator<NodeType*> nodes_iterator; 464 465 static inline NodeType* getEntryNode(NodeType* N) { 466 return N; 467 } 468 469 static inline ChildIteratorType child_begin(NodeType* N) { 470 return N->succ_begin(); 471 } 472 473 static inline ChildIteratorType child_end(NodeType* N) { 474 return N->succ_end(); 475 } 476 477 static inline nodes_iterator nodes_begin(NodeType* N) { 478 return df_begin(N); 479 } 480 481 static inline nodes_iterator nodes_end(NodeType* N) { 482 return df_end(N); 483 } 484 }; 485 486 } // end llvm namespace 487 488 #endif 489