blob: 4c4612fab5e760793e916dc26dbf1fd7e6f2e90a [file] [log] [blame]
Ted Kremenek9eb49a42008-01-13 04:56:13 +00001//=-- 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
Ted Kremenek21142582010-12-23 19:38:26 +000015#include "clang/StaticAnalyzer/PathSensitive/ExplodedGraph.h"
16#include "clang/StaticAnalyzer/PathSensitive/GRState.h"
Ted Kremenek33c63692008-04-16 15:51:26 +000017#include "clang/AST/Stmt.h"
Ted Kremenekffe0f432008-03-07 22:58:01 +000018#include "llvm/ADT/DenseSet.h"
19#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/SmallVector.h"
Ted Kremenek9eb49a42008-01-13 04:56:13 +000021#include <vector>
22
23using namespace clang;
Ted Kremenek9ef65372010-12-23 07:20:52 +000024using namespace ento;
Ted Kremenek9eb49a42008-01-13 04:56:13 +000025
Ted Kremenek2e287542008-08-27 01:56:11 +000026//===----------------------------------------------------------------------===//
27// Node auditing.
28//===----------------------------------------------------------------------===//
29
30// An out of line virtual method to provide a home for the class vtable.
Zhongxing Xuc5619d92009-08-06 01:32:16 +000031ExplodedNode::Auditor::~Auditor() {}
Ted Kremenek2e287542008-08-27 01:56:11 +000032
33#ifndef NDEBUG
Zhongxing Xuc5619d92009-08-06 01:32:16 +000034static ExplodedNode::Auditor* NodeAuditor = 0;
Ted Kremenek2e287542008-08-27 01:56:11 +000035#endif
36
Zhongxing Xuc5619d92009-08-06 01:32:16 +000037void ExplodedNode::SetAuditor(ExplodedNode::Auditor* A) {
Ted Kremenek2e287542008-08-27 01:56:11 +000038#ifndef NDEBUG
39 NodeAuditor = A;
40#endif
41}
42
43//===----------------------------------------------------------------------===//
Zhongxing Xuc5619d92009-08-06 01:32:16 +000044// ExplodedNode.
Ted Kremenek2e287542008-08-27 01:56:11 +000045//===----------------------------------------------------------------------===//
Ted Kremenek9eb49a42008-01-13 04:56:13 +000046
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +000047static inline BumpVector<ExplodedNode*>& getVector(void* P) {
48 return *reinterpret_cast<BumpVector<ExplodedNode*>*>(P);
Ted Kremenek9eb49a42008-01-13 04:56:13 +000049}
50
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +000051void ExplodedNode::addPredecessor(ExplodedNode* V, ExplodedGraph &G) {
Ted Kremenek45b87892008-08-27 01:27:52 +000052 assert (!V->isSink());
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +000053 Preds.addNode(V, G);
54 V->Succs.addNode(this, G);
Ted Kremenek2e287542008-08-27 01:56:11 +000055#ifndef NDEBUG
56 if (NodeAuditor) NodeAuditor->AddEdge(V, this);
57#endif
Ted Kremenek45b87892008-08-27 01:27:52 +000058}
59
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +000060void ExplodedNode::NodeGroup::addNode(ExplodedNode* N, ExplodedGraph &G) {
61 assert((reinterpret_cast<uintptr_t>(N) & Mask) == 0x0);
62 assert(!getFlag());
Mike Stump1eb44332009-09-09 15:08:12 +000063
Ted Kremenek9eb49a42008-01-13 04:56:13 +000064 if (getKind() == Size1) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +000065 if (ExplodedNode* NOld = getNode()) {
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +000066 BumpVectorContext &Ctx = G.getNodeAllocator();
67 BumpVector<ExplodedNode*> *V =
68 G.getAllocator().Allocate<BumpVector<ExplodedNode*> >();
69 new (V) BumpVector<ExplodedNode*>(Ctx, 4);
70
71 assert((reinterpret_cast<uintptr_t>(V) & Mask) == 0x0);
72 V->push_back(NOld, Ctx);
73 V->push_back(N, Ctx);
Ted Kremenek45c63bd2008-01-29 23:31:09 +000074 P = reinterpret_cast<uintptr_t>(V) | SizeOther;
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +000075 assert(getPtr() == (void*) V);
76 assert(getKind() == SizeOther);
Ted Kremenek9eb49a42008-01-13 04:56:13 +000077 }
Ted Kremenek596f0a12008-03-05 19:08:55 +000078 else {
Ted Kremenek9eb49a42008-01-13 04:56:13 +000079 P = reinterpret_cast<uintptr_t>(N);
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +000080 assert(getKind() == Size1);
Ted Kremenek596f0a12008-03-05 19:08:55 +000081 }
Ted Kremenek9eb49a42008-01-13 04:56:13 +000082 }
Ted Kremenek596f0a12008-03-05 19:08:55 +000083 else {
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +000084 assert(getKind() == SizeOther);
85 getVector(getPtr()).push_back(N, G.getNodeAllocator());
Ted Kremenek596f0a12008-03-05 19:08:55 +000086 }
Ted Kremenek9eb49a42008-01-13 04:56:13 +000087}
88
Zhongxing Xuc5619d92009-08-06 01:32:16 +000089unsigned ExplodedNode::NodeGroup::size() const {
Ted Kremenekffe0f432008-03-07 22:58:01 +000090 if (getFlag())
91 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +000092
Ted Kremenek9eb49a42008-01-13 04:56:13 +000093 if (getKind() == Size1)
94 return getNode() ? 1 : 0;
95 else
96 return getVector(getPtr()).size();
97}
98
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +000099ExplodedNode **ExplodedNode::NodeGroup::begin() const {
Ted Kremenekffe0f432008-03-07 22:58:01 +0000100 if (getFlag())
101 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremenek9eb49a42008-01-13 04:56:13 +0000103 if (getKind() == Size1)
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000104 return (ExplodedNode**) (getPtr() ? &P : NULL);
Ted Kremenek9eb49a42008-01-13 04:56:13 +0000105 else
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000106 return const_cast<ExplodedNode**>(&*(getVector(getPtr()).begin()));
Ted Kremenek9eb49a42008-01-13 04:56:13 +0000107}
108
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000109ExplodedNode** ExplodedNode::NodeGroup::end() const {
Ted Kremenekffe0f432008-03-07 22:58:01 +0000110 if (getFlag())
111 return NULL;
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Ted Kremenek9eb49a42008-01-13 04:56:13 +0000113 if (getKind() == Size1)
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000114 return (ExplodedNode**) (getPtr() ? &P+1 : NULL);
Ted Kremenek7b989572008-04-20 23:54:24 +0000115 else {
116 // Dereferencing end() is undefined behaviour. The vector is not empty, so
Argyrios Kyrtzidis5fc073f2008-04-22 07:37:18 +0000117 // we can dereference the last elem and then add 1 to the result.
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +0000118 return const_cast<ExplodedNode**>(getVector(getPtr()).end());
Ted Kremenek7b989572008-04-20 23:54:24 +0000119 }
Ted Kremenek9eb49a42008-01-13 04:56:13 +0000120}
121
Mike Stump1eb44332009-09-09 15:08:12 +0000122ExplodedNode *ExplodedGraph::getNode(const ProgramPoint& L,
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000123 const GRState* State, bool* IsNew) {
124 // Profile 'State' to determine if we already have an existing node.
Mike Stump1eb44332009-09-09 15:08:12 +0000125 llvm::FoldingSetNodeID profile;
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000126 void* InsertPos = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000127
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000128 NodeTy::Profile(profile, L, State);
129 NodeTy* V = Nodes.FindNodeOrInsertPos(profile, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000130
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000131 if (!V) {
132 // Allocate a new node.
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +0000133 V = (NodeTy*) getAllocator().Allocate<NodeTy>();
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000134 new (V) NodeTy(L, State);
Mike Stump1eb44332009-09-09 15:08:12 +0000135
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000136 // Insert the node into the node set and return it.
137 Nodes.InsertNode(V, InsertPos);
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000139 ++NumNodes;
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000141 if (IsNew) *IsNew = true;
142 }
143 else
144 if (IsNew) *IsNew = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000146 return V;
147}
148
149std::pair<ExplodedGraph*, InterExplodedGraphMap*>
150ExplodedGraph::Trim(const NodeTy* const* NBeg, const NodeTy* const* NEnd,
151 llvm::DenseMap<const void*, const void*> *InverseMap) const {
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000153 if (NBeg == NEnd)
154 return std::make_pair((ExplodedGraph*) 0,
155 (InterExplodedGraphMap*) 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000157 assert (NBeg < NEnd);
158
159 llvm::OwningPtr<InterExplodedGraphMap> M(new InterExplodedGraphMap());
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000161 ExplodedGraph* G = TrimInternal(NBeg, NEnd, M.get(), InverseMap);
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000163 return std::make_pair(static_cast<ExplodedGraph*>(G), M.take());
164}
165
166ExplodedGraph*
167ExplodedGraph::TrimInternal(const ExplodedNode* const* BeginSources,
168 const ExplodedNode* const* EndSources,
169 InterExplodedGraphMap* M,
170 llvm::DenseMap<const void*, const void*> *InverseMap) const {
171
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000172 typedef llvm::DenseSet<const ExplodedNode*> Pass1Ty;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000173 Pass1Ty Pass1;
Mike Stump1eb44332009-09-09 15:08:12 +0000174
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000175 typedef llvm::DenseMap<const ExplodedNode*, ExplodedNode*> Pass2Ty;
Ted Kremenekcf118d42009-02-04 23:49:09 +0000176 Pass2Ty& Pass2 = M->M;
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000178 llvm::SmallVector<const ExplodedNode*, 10> WL1, WL2;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000179
Ted Kremenek10aa5542009-03-12 23:41:59 +0000180 // ===- Pass 1 (reverse DFS) -===
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000181 for (const ExplodedNode* const* I = BeginSources; I != EndSources; ++I) {
Ted Kremenekdf969292009-02-20 21:10:26 +0000182 assert(*I);
183 WL1.push_back(*I);
184 }
Mike Stump1eb44332009-09-09 15:08:12 +0000185
Ted Kremenekdf969292009-02-20 21:10:26 +0000186 // Process the first worklist until it is empty. Because it is a std::list
187 // it acts like a FIFO queue.
188 while (!WL1.empty()) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000189 const ExplodedNode *N = WL1.back();
Ted Kremenekdf969292009-02-20 21:10:26 +0000190 WL1.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000191
Ted Kremenekdf969292009-02-20 21:10:26 +0000192 // Have we already visited this node? If so, continue to the next one.
193 if (Pass1.count(N))
194 continue;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000195
Ted Kremenekdf969292009-02-20 21:10:26 +0000196 // Otherwise, mark this node as visited.
197 Pass1.insert(N);
Mike Stump1eb44332009-09-09 15:08:12 +0000198
Ted Kremenekdf969292009-02-20 21:10:26 +0000199 // If this is a root enqueue it to the second worklist.
200 if (N->Preds.empty()) {
201 WL2.push_back(N);
202 continue;
Ted Kremenekffe0f432008-03-07 22:58:01 +0000203 }
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Ted Kremenekdf969292009-02-20 21:10:26 +0000205 // Visit our predecessors and enqueue them.
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000206 for (ExplodedNode** I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I)
Ted Kremenek10aa5542009-03-12 23:41:59 +0000207 WL1.push_back(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000208 }
Mike Stump1eb44332009-09-09 15:08:12 +0000209
Ted Kremenekdf969292009-02-20 21:10:26 +0000210 // We didn't hit a root? Return with a null pointer for the new graph.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000211 if (WL2.empty())
Ted Kremenekcf118d42009-02-04 23:49:09 +0000212 return 0;
213
Ted Kremenekdf969292009-02-20 21:10:26 +0000214 // Create an empty graph.
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000215 ExplodedGraph* G = MakeEmptyGraph();
Mike Stump1eb44332009-09-09 15:08:12 +0000216
217 // ===- Pass 2 (forward DFS to construct the new graph) -===
Ted Kremenekffe0f432008-03-07 22:58:01 +0000218 while (!WL2.empty()) {
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000219 const ExplodedNode* N = WL2.back();
Ted Kremenekffe0f432008-03-07 22:58:01 +0000220 WL2.pop_back();
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Ted Kremenekffe0f432008-03-07 22:58:01 +0000222 // Skip this node if we have already processed it.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000223 if (Pass2.find(N) != Pass2.end())
224 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Ted Kremenekdf969292009-02-20 21:10:26 +0000226 // Create the corresponding node in the new graph and record the mapping
227 // from the old node to the new node.
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000228 ExplodedNode* NewN = G->getNode(N->getLocation(), N->State, NULL);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000229 Pass2[N] = NewN;
Mike Stump1eb44332009-09-09 15:08:12 +0000230
Ted Kremenekdf969292009-02-20 21:10:26 +0000231 // Also record the reverse mapping from the new node to the old node.
Ted Kremenekfe9e5432009-02-18 03:48:14 +0000232 if (InverseMap) (*InverseMap)[NewN] = N;
Mike Stump1eb44332009-09-09 15:08:12 +0000233
Ted Kremenekdf969292009-02-20 21:10:26 +0000234 // If this node is a root, designate it as such in the graph.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000235 if (N->Preds.empty())
236 G->addRoot(NewN);
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Ted Kremenekffe0f432008-03-07 22:58:01 +0000238 // In the case that some of the intended predecessors of NewN have already
239 // been created, we should hook them up as predecessors.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000240
Ted Kremenekdf969292009-02-20 21:10:26 +0000241 // Walk through the predecessors of 'N' and hook up their corresponding
242 // nodes in the new graph (if any) to the freshly created node.
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000243 for (ExplodedNode **I=N->Preds.begin(), **E=N->Preds.end(); I!=E; ++I) {
Ted Kremenekdf969292009-02-20 21:10:26 +0000244 Pass2Ty::iterator PI = Pass2.find(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000245 if (PI == Pass2.end())
246 continue;
Mike Stump1eb44332009-09-09 15:08:12 +0000247
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +0000248 NewN->addPredecessor(PI->second, *G);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000249 }
250
251 // In the case that some of the intended successors of NewN have already
252 // been created, we should hook them up as successors. Otherwise, enqueue
253 // the new nodes from the original graph that should have nodes created
254 // in the new graph.
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000255 for (ExplodedNode **I=N->Succs.begin(), **E=N->Succs.end(); I!=E; ++I) {
Mike Stump1eb44332009-09-09 15:08:12 +0000256 Pass2Ty::iterator PI = Pass2.find(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000257 if (PI != Pass2.end()) {
Ted Kremenek5fe4d9d2009-10-07 00:42:52 +0000258 PI->second->addPredecessor(NewN, *G);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000259 continue;
260 }
261
262 // Enqueue nodes to the worklist that were marked during pass 1.
Ted Kremenekdf969292009-02-20 21:10:26 +0000263 if (Pass1.count(*I))
264 WL2.push_back(*I);
Ted Kremenekffe0f432008-03-07 22:58:01 +0000265 }
Mike Stump1eb44332009-09-09 15:08:12 +0000266
Ted Kremenekdf969292009-02-20 21:10:26 +0000267 // Finally, explictly mark all nodes without any successors as sinks.
Ted Kremenekffe0f432008-03-07 22:58:01 +0000268 if (N->isSink())
269 NewN->markAsSink();
270 }
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Ted Kremenekffe0f432008-03-07 22:58:01 +0000272 return G;
273}
Ted Kremenekcf118d42009-02-04 23:49:09 +0000274
Zhongxing Xuc5619d92009-08-06 01:32:16 +0000275ExplodedNode*
Zhongxing Xu38b02b92009-08-06 06:28:40 +0000276InterExplodedGraphMap::getMappedNode(const ExplodedNode* N) const {
Jeffrey Yasskin3958b502009-11-10 01:17:45 +0000277 llvm::DenseMap<const ExplodedNode*, ExplodedNode*>::const_iterator I =
Ted Kremenekcf118d42009-02-04 23:49:09 +0000278 M.find(N);
279
280 return I == M.end() ? 0 : I->second;
281}
282