1 //=-- ExplodedGraph.cpp - 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 // 13 //===----------------------------------------------------------------------===// 14 15 #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h" 16 #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" 17 #include "clang/AST/Stmt.h" 18 #include "llvm/ADT/DenseSet.h" 19 #include "llvm/ADT/DenseMap.h" 20 #include "llvm/ADT/SmallVector.h" 21 #include <vector> 22 23 using namespace clang; 24 using namespace ento; 25 26 //===----------------------------------------------------------------------===// 27 // Node auditing. 28 //===----------------------------------------------------------------------===// 29 30 // An out of line virtual method to provide a home for the class vtable. 31 ExplodedNode::Auditor::~Auditor() {} 32 33 #ifndef NDEBUG 34 static ExplodedNode::Auditor* NodeAuditor = 0; 35 #endif 36 37 void ExplodedNode::SetAuditor(ExplodedNode::Auditor* A) { 38 #ifndef NDEBUG 39 NodeAuditor = A; 40 #endif 41 } 42 43 //===----------------------------------------------------------------------===// 44 // Cleanup. 45 //===----------------------------------------------------------------------===// 46 47 typedef std::vector<ExplodedNode*> NodeList; 48 static inline NodeList*& getNodeList(void *&p) { return (NodeList*&) p; } 49 50 ExplodedGraph::~ExplodedGraph() { 51 if (reclaimNodes) { 52 delete getNodeList(recentlyAllocatedNodes); 53 delete getNodeList(freeNodes); 54 } 55 } 56 57 //===----------------------------------------------------------------------===// 58 // Node reclamation. 59 //===----------------------------------------------------------------------===// 60 61 void ExplodedGraph::reclaimRecentlyAllocatedNodes() { 62 if (!recentlyAllocatedNodes) 63 return; 64 NodeList &nl = *getNodeList(recentlyAllocatedNodes); 65 66 // Reclaimn all nodes that match *all* the following criteria: 67 // 68 // (1) 1 predecessor (that has one successor) 69 // (2) 1 successor (that has one predecessor) 70 // (3) The ProgramPoint is for a PostStmt. 71 // (4) There is no 'tag' for the ProgramPoint. 72 // (5) The 'store' is the same as the predecessor. 73 // (6) The 'GDM' is the same as the predecessor. 74 // (7) The LocationContext is the same as the predecessor. 75 // (8) The PostStmt is for a non-CFGElement expression. 76 77 for (NodeList::iterator i = nl.begin(), e = nl.end() ; i != e; ++i) { 78 ExplodedNode *node = *i; 79 80 // Conditions 1 and 2. 81 if (node->pred_size() != 1 || node->succ_size() != 1) 82 continue; 83 84 ExplodedNode *pred = *(node->pred_begin()); 85 if (pred->succ_size() != 1) 86 continue; 87 88 ExplodedNode *succ = *(node->succ_begin()); 89 if (succ->pred_size() != 1) 90 continue; 91 92 // Condition 3. 93 ProgramPoint progPoint = node->getLocation(); 94 if (!isa<PostStmt>(progPoint)) 95 continue; 96 // Condition 4. 97 PostStmt ps = cast<PostStmt>(progPoint); 98 if (ps.getTag()) 99 continue; 100 101 if (isa<BinaryOperator>(ps.getStmt())) 102 continue; 103 104 // Conditions 5, 6, and 7. 105 const ProgramState *state = node->getState(); 106 const ProgramState *pred_state = pred->getState(); 107 if (state->store != pred_state->store || state->GDM != pred_state->GDM || 108 progPoint.getLocationContext() != pred->getLocationContext()) 109 continue; 110 111 // Condition 8. 112 if (node->getCFG().isBlkExpr(ps.getStmt())) 113 continue; 114 115 // If we reach here, we can remove the node. This means: 116 // (a) changing the predecessors successor to the successor of this node 117 // (b) changing the successors predecessor to the predecessor of this node 118 // (c) Putting 'node' onto freeNodes. 119 pred->replaceSuccessor(succ); 120 succ->replacePredecessor(pred); 121 if (!freeNodes) 122 freeNodes = new NodeList(); 123 getNodeList(freeNodes)->push_back(node); 124 Nodes.RemoveNode(node); 125 --NumNodes; 126 node->~ExplodedNode(); 127 } 128 129 nl.clear(); 130 } 131 132 //===----------------------------------------------------------------------===// 133 // ExplodedNode. 134 //===----------------------------------------------------------------------===// 135 136 static inline BumpVector<ExplodedNode*>& getVector(void *P) { 137 return *reinterpret_cast<BumpVector<ExplodedNode*>*>(P); 138 } 139 140 void ExplodedNode::addPredecessor(ExplodedNode *V, ExplodedGraph &G) { 141 assert (!V->isSink()); 142 Preds.addNode(V, G); 143 V->Succs.addNode(this, G); 144 #ifndef NDEBUG 145 if (NodeAuditor) NodeAuditor->AddEdge(V, this); 146 #endif 147 } 148 149 void ExplodedNode::NodeGroup::replaceNode(ExplodedNode *node) { 150 assert(getKind() == Size1); 151 P = reinterpret_cast<uintptr_t>(node); 152 assert(getKind() == Size1); 153 } 154 155 void ExplodedNode::NodeGroup::addNode(ExplodedNode *N, ExplodedGraph &G) { 156 assert((reinterpret_cast<uintptr_t>(N) & Mask) == 0x0); 157 assert(!getFlag()); 158 159 if (getKind() == Size1) { 160 if (ExplodedNode *NOld = getNode()) { 161 BumpVectorContext &Ctx = G.getNodeAllocator(); 162 BumpVector<ExplodedNode*> *V = 163 G.getAllocator().Allocate<BumpVector<ExplodedNode*> >(); 164 new (V) BumpVector<ExplodedNode*>(Ctx, 4); 165 166 assert((reinterpret_cast<uintptr_t>(V) & Mask) == 0x0); 167 V->push_back(NOld, Ctx); 168 V->push_back(N, Ctx); 169 P = reinterpret_cast<uintptr_t>(V) | SizeOther; 170 assert(getPtr() == (void*) V); 171 assert(getKind() == SizeOther); 172 } 173 else { 174 P = reinterpret_cast<uintptr_t>(N); 175 assert(getKind() == Size1); 176 } 177 } 178 else { 179 assert(getKind() == SizeOther); 180 getVector(getPtr()).push_back(N, G.getNodeAllocator()); 181 } 182 } 183 184 unsigned ExplodedNode::NodeGroup::size() const { 185 if (getFlag()) 186 return 0; 187 188 if (getKind() == Size1) 189 return getNode() ? 1 : 0; 190 else 191 return getVector(getPtr()).size(); 192 } 193 194 ExplodedNode **ExplodedNode::NodeGroup::begin() const { 195 if (getFlag()) 196 return NULL; 197 198 if (getKind() == Size1) 199 return (ExplodedNode**) (getPtr() ? &P : NULL); 200 else 201 return const_cast<ExplodedNode**>(&*(getVector(getPtr()).begin())); 202 } 203 204 ExplodedNode** ExplodedNode::NodeGroup::end() const { 205 if (getFlag()) 206 return NULL; 207 208 if (getKind() == Size1) 209 return (ExplodedNode**) (getPtr() ? &P+1 : NULL); 210 else { 211 // Dereferencing end() is undefined behaviour. The vector is not empty, so 212 // we can dereference the last elem and then add 1 to the result. 213 return const_cast<ExplodedNode**>(getVector(getPtr()).end()); 214 } 215 } 216 217 ExplodedNode *ExplodedGraph::getNode(const ProgramPoint &L, 218 const ProgramState *State, 219 bool IsSink, 220 bool* IsNew) { 221 // Profile 'State' to determine if we already have an existing node. 222 llvm::FoldingSetNodeID profile; 223 void *InsertPos = 0; 224 225 NodeTy::Profile(profile, L, State, IsSink); 226 NodeTy* V = Nodes.FindNodeOrInsertPos(profile, InsertPos); 227 228 if (!V) { 229 if (freeNodes && !getNodeList(freeNodes)->empty()) { 230 NodeList *nl = getNodeList(freeNodes); 231 V = nl->back(); 232 nl->pop_back(); 233 } 234 else { 235 // Allocate a new node. 236 V = (NodeTy*) getAllocator().Allocate<NodeTy>(); 237 } 238 239 new (V) NodeTy(L, State, IsSink); 240 241 if (reclaimNodes) { 242 if (!recentlyAllocatedNodes) 243 recentlyAllocatedNodes = new NodeList(); 244 getNodeList(recentlyAllocatedNodes)->push_back(V); 245 } 246 247 // Insert the node into the node set and return it. 248 Nodes.InsertNode(V, InsertPos); 249 250 ++NumNodes; 251 252 if (IsNew) *IsNew = true; 253 } 254 else 255 if (IsNew) *IsNew = false; 256 257 return V; 258 } 259 260 std::pair<ExplodedGraph*, InterExplodedGraphMap*> 261 ExplodedGraph::Trim(const NodeTy* const* NBeg, const NodeTy* const* NEnd, 262 llvm::DenseMap<const void*, const void*> *InverseMap) const { 263 264 if (NBeg == NEnd) 265 return std::make_pair((ExplodedGraph*) 0, 266 (InterExplodedGraphMap*) 0); 267 268 assert (NBeg < NEnd); 269 270 llvm::OwningPtr<InterExplodedGraphMap> M(new InterExplodedGraphMap()); 271 272 ExplodedGraph* G = TrimInternal(NBeg, NEnd, M.get(), InverseMap); 273 274 return std::make_pair(static_cast<ExplodedGraph*>(G), M.take()); 275 } 276 277 ExplodedGraph* 278 ExplodedGraph::TrimInternal(const ExplodedNode* const* BeginSources, 279 const ExplodedNode* const* EndSources, 280 InterExplodedGraphMap* M, 281 llvm::DenseMap<const void*, const void*> *InverseMap) const { 282 283 typedef llvm::DenseSet<const ExplodedNode*> Pass1Ty; 284 Pass1Ty Pass1; 285 286 typedef llvm::DenseMap<const ExplodedNode*, ExplodedNode*> Pass2Ty; 287 Pass2Ty& Pass2 = M->M; 288 289 SmallVector<const ExplodedNode*, 10> WL1, WL2; 290 291 // ===- Pass 1 (reverse DFS) -=== 292 for (const ExplodedNode* const* I = BeginSources; I != EndSources; ++I) { 293 assert(*I); 294 WL1.push_back(*I); 295 } 296 297 // Process the first worklist until it is empty. Because it is a std::list 298 // it acts like a FIFO queue. 299 while (!WL1.empty()) { 300 const ExplodedNode *N = WL1.back(); 301 WL1.pop_back(); 302 303 // Have we already visited this node? If so, continue to the next one. 304 if (Pass1.count(N)) 305 continue; 306 307 // Otherwise, mark this node as visited. 308 Pass1.insert(N); 309 310 // If this is a root enqueue it to the second worklist. 311 if (N->Preds.empty()) { 312 WL2.push_back(N); 313 continue; 314 } 315 316 // Visit our predecessors and enqueue them. 317 for (ExplodedNode** I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) 318 WL1.push_back(*I); 319 } 320 321 // We didn't hit a root? Return with a null pointer for the new graph. 322 if (WL2.empty()) 323 return 0; 324 325 // Create an empty graph. 326 ExplodedGraph* G = MakeEmptyGraph(); 327 328 // ===- Pass 2 (forward DFS to construct the new graph) -=== 329 while (!WL2.empty()) { 330 const ExplodedNode *N = WL2.back(); 331 WL2.pop_back(); 332 333 // Skip this node if we have already processed it. 334 if (Pass2.find(N) != Pass2.end()) 335 continue; 336 337 // Create the corresponding node in the new graph and record the mapping 338 // from the old node to the new node. 339 ExplodedNode *NewN = G->getNode(N->getLocation(), N->State, N->isSink(), 0); 340 Pass2[N] = NewN; 341 342 // Also record the reverse mapping from the new node to the old node. 343 if (InverseMap) (*InverseMap)[NewN] = N; 344 345 // If this node is a root, designate it as such in the graph. 346 if (N->Preds.empty()) 347 G->addRoot(NewN); 348 349 // In the case that some of the intended predecessors of NewN have already 350 // been created, we should hook them up as predecessors. 351 352 // Walk through the predecessors of 'N' and hook up their corresponding 353 // nodes in the new graph (if any) to the freshly created node. 354 for (ExplodedNode **I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) { 355 Pass2Ty::iterator PI = Pass2.find(*I); 356 if (PI == Pass2.end()) 357 continue; 358 359 NewN->addPredecessor(PI->second, *G); 360 } 361 362 // In the case that some of the intended successors of NewN have already 363 // been created, we should hook them up as successors. Otherwise, enqueue 364 // the new nodes from the original graph that should have nodes created 365 // in the new graph. 366 for (ExplodedNode **I=N->Succs.begin(), **E=N->Succs.end(); I!=E; ++I) { 367 Pass2Ty::iterator PI = Pass2.find(*I); 368 if (PI != Pass2.end()) { 369 PI->second->addPredecessor(NewN, *G); 370 continue; 371 } 372 373 // Enqueue nodes to the worklist that were marked during pass 1. 374 if (Pass1.count(*I)) 375 WL2.push_back(*I); 376 } 377 } 378 379 return G; 380 } 381 382 ExplodedNode* 383 InterExplodedGraphMap::getMappedNode(const ExplodedNode *N) const { 384 llvm::DenseMap<const ExplodedNode*, ExplodedNode*>::const_iterator I = 385 M.find(N); 386 387 return I == M.end() ? 0 : I->second; 388 } 389 390