1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | #include "clang/StaticAnalyzer/Core/PathSensitive/ExplodedGraph.h" |
15 | #include "clang/AST/Expr.h" |
16 | #include "clang/AST/ExprObjC.h" |
17 | #include "clang/AST/ParentMap.h" |
18 | #include "clang/AST/Stmt.h" |
19 | #include "clang/Analysis/ProgramPoint.h" |
20 | #include "clang/Analysis/Support/BumpVector.h" |
21 | #include "clang/Basic/LLVM.h" |
22 | #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" |
23 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState.h" |
24 | #include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h" |
25 | #include "llvm/ADT/DenseSet.h" |
26 | #include "llvm/ADT/FoldingSet.h" |
27 | #include "llvm/ADT/Optional.h" |
28 | #include "llvm/ADT/PointerUnion.h" |
29 | #include "llvm/ADT/SmallVector.h" |
30 | #include "llvm/Support/Casting.h" |
31 | #include <cassert> |
32 | #include <memory> |
33 | |
34 | using namespace clang; |
35 | using namespace ento; |
36 | |
37 | |
38 | |
39 | |
40 | |
41 | ExplodedGraph::ExplodedGraph() = default; |
42 | |
43 | ExplodedGraph::~ExplodedGraph() = default; |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | bool ExplodedGraph::isInterestingLValueExpr(const Expr *Ex) { |
50 | if (!Ex->isLValue()) |
51 | return false; |
52 | return isa<DeclRefExpr>(Ex) || |
53 | isa<MemberExpr>(Ex) || |
54 | isa<ObjCIvarRefExpr>(Ex); |
55 | } |
56 | |
57 | bool ExplodedGraph::shouldCollect(const ExplodedNode *node) { |
58 | |
59 | |
60 | |
61 | |
62 | |
63 | |
64 | |
65 | |
66 | |
67 | |
68 | |
69 | |
70 | |
71 | |
72 | |
73 | |
74 | |
75 | |
76 | |
77 | |
78 | |
79 | |
80 | |
81 | |
82 | |
83 | |
84 | |
85 | |
86 | |
87 | |
88 | |
89 | if (node->pred_size() != 1 || node->succ_size() != 1) |
90 | return false; |
91 | |
92 | const ExplodedNode *pred = *(node->pred_begin()); |
93 | if (pred->succ_size() != 1) |
94 | return false; |
95 | |
96 | const ExplodedNode *succ = *(node->succ_begin()); |
97 | if (succ->pred_size() != 1) |
98 | return false; |
99 | |
100 | |
101 | |
102 | ProgramPoint progPoint = node->getLocation(); |
103 | if (progPoint.getAs<PreStmtPurgeDeadSymbols>()) |
104 | return !progPoint.getTag(); |
105 | |
106 | |
107 | if (!progPoint.getAs<PostStmt>() || progPoint.getAs<PostStore>()) |
108 | return false; |
109 | |
110 | |
111 | if (progPoint.getTag()) |
112 | return false; |
113 | |
114 | |
115 | ProgramStateRef state = node->getState(); |
116 | ProgramStateRef pred_state = pred->getState(); |
117 | if (state->store != pred_state->store || state->GDM != pred_state->GDM || |
118 | progPoint.getLocationContext() != pred->getLocationContext()) |
119 | return false; |
120 | |
121 | |
122 | |
123 | const Expr *Ex = dyn_cast<Expr>(progPoint.castAs<PostStmt>().getStmt()); |
124 | if (!Ex) |
125 | return false; |
126 | |
127 | |
128 | |
129 | |
130 | if (isInterestingLValueExpr(Ex)) |
131 | return false; |
132 | |
133 | |
134 | |
135 | |
136 | |
137 | ParentMap &PM = progPoint.getLocationContext()->getParentMap(); |
138 | if (!PM.isConsumedExpr(Ex)) |
139 | return false; |
140 | |
141 | |
142 | const ProgramPoint SuccLoc = succ->getLocation(); |
143 | if (Optional<StmtPoint> SP = SuccLoc.getAs<StmtPoint>()) |
144 | if (CallEvent::isCallStmt(SP->getStmt())) |
145 | return false; |
146 | |
147 | |
148 | if (SuccLoc.getAs<CallEnter>() || SuccLoc.getAs<PreImplicitCall>()) |
149 | return false; |
150 | |
151 | return true; |
152 | } |
153 | |
154 | void ExplodedGraph::collectNode(ExplodedNode *node) { |
155 | |
156 | |
157 | |
158 | |
159 | pred_size() == 1 || node->succ_size() == 1", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp", 159, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(node->pred_size() == 1 || node->succ_size() == 1); |
160 | ExplodedNode *pred = *(node->pred_begin()); |
161 | ExplodedNode *succ = *(node->succ_begin()); |
162 | pred->replaceSuccessor(succ); |
163 | succ->replacePredecessor(pred); |
164 | FreeNodes.push_back(node); |
165 | Nodes.RemoveNode(node); |
166 | --NumNodes; |
167 | node->~ExplodedNode(); |
168 | } |
169 | |
170 | void ExplodedGraph::reclaimRecentlyAllocatedNodes() { |
171 | if (ChangedNodes.empty()) |
172 | return; |
173 | |
174 | |
175 | |
176 | |
177 | 0", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp", 177, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(ReclaimCounter > 0); |
178 | if (--ReclaimCounter != 0) |
179 | return; |
180 | ReclaimCounter = ReclaimNodeInterval; |
181 | |
182 | for (const auto node : ChangedNodes) |
183 | if (shouldCollect(node)) |
184 | collectNode(node); |
185 | ChangedNodes.clear(); |
186 | } |
187 | |
188 | |
189 | |
190 | |
191 | |
192 | |
193 | |
194 | |
195 | |
196 | |
197 | |
198 | |
199 | |
200 | |
201 | |
202 | using ExplodedNodeVector = BumpVector<ExplodedNode *>; |
203 | using GroupStorage = llvm::PointerUnion<ExplodedNode *, ExplodedNodeVector *>; |
204 | |
205 | void ExplodedNode::addPredecessor(ExplodedNode *V, ExplodedGraph &G) { |
206 | isSink()", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp", 206, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(!V->isSink()); |
207 | Preds.addNode(V, G); |
208 | V->Succs.addNode(this, G); |
209 | } |
210 | |
211 | void ExplodedNode::NodeGroup::replaceNode(ExplodedNode *node) { |
212 | assert(!getFlag()); |
213 | |
214 | GroupStorage &Storage = reinterpret_cast<GroupStorage&>(P); |
215 | ()", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp", 215, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(Storage.is<ExplodedNode *>()); |
216 | Storage = node; |
217 | ()", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp", 217, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(Storage.is<ExplodedNode *>()); |
218 | } |
219 | |
220 | void ExplodedNode::NodeGroup::addNode(ExplodedNode *N, ExplodedGraph &G) { |
221 | assert(!getFlag()); |
222 | |
223 | GroupStorage &Storage = reinterpret_cast<GroupStorage&>(P); |
224 | if (Storage.isNull()) { |
225 | Storage = N; |
226 | ()", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp", 226, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(Storage.is<ExplodedNode *>()); |
227 | return; |
228 | } |
229 | |
230 | ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>(); |
231 | |
232 | if (!V) { |
233 | |
234 | ExplodedNode *Old = Storage.get<ExplodedNode *>(); |
235 | |
236 | BumpVectorContext &Ctx = G.getNodeAllocator(); |
237 | V = G.getAllocator().Allocate<ExplodedNodeVector>(); |
238 | new (V) ExplodedNodeVector(Ctx, 4); |
239 | V->push_back(Old, Ctx); |
240 | |
241 | Storage = V; |
242 | assert(!getFlag()); |
243 | ()", "/home/seafit/code_projects/clang_source/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp", 243, __PRETTY_FUNCTION__))" file_link="../../../../include/assert.h.html#88" macro="true">assert(Storage.is<ExplodedNodeVector *>()); |
244 | } |
245 | |
246 | V->push_back(N, G.getNodeAllocator()); |
247 | } |
248 | |
249 | unsigned ExplodedNode::NodeGroup::size() const { |
250 | if (getFlag()) |
251 | return 0; |
252 | |
253 | const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P); |
254 | if (Storage.isNull()) |
255 | return 0; |
256 | if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>()) |
257 | return V->size(); |
258 | return 1; |
259 | } |
260 | |
261 | ExplodedNode * const *ExplodedNode::NodeGroup::begin() const { |
262 | if (getFlag()) |
263 | return nullptr; |
264 | |
265 | const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P); |
266 | if (Storage.isNull()) |
267 | return nullptr; |
268 | if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>()) |
269 | return V->begin(); |
270 | return Storage.getAddrOfPtr1(); |
271 | } |
272 | |
273 | ExplodedNode * const *ExplodedNode::NodeGroup::end() const { |
274 | if (getFlag()) |
275 | return nullptr; |
276 | |
277 | const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P); |
278 | if (Storage.isNull()) |
279 | return nullptr; |
280 | if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>()) |
281 | return V->end(); |
282 | return Storage.getAddrOfPtr1() + 1; |
283 | } |
284 | |
285 | int64_t ExplodedNode::getID(ExplodedGraph *G) const { |
286 | return G->getAllocator().identifyKnownAlignedObject<ExplodedNode>(this); |
287 | } |
288 | |
289 | bool ExplodedNode::isTrivial() const { |
290 | return pred_size() == 1 && succ_size() == 1 && |
291 | getFirstPred()->getState()->getID() == getState()->getID() && |
292 | getFirstPred()->succ_size() == 1; |
293 | } |
294 | |
295 | ExplodedNode *ExplodedGraph::getNode(const ProgramPoint &L, |
296 | ProgramStateRef State, |
297 | bool IsSink, |
298 | bool* IsNew) { |
299 | |
300 | llvm::FoldingSetNodeID profile; |
301 | void *InsertPos = nullptr; |
302 | |
303 | NodeTy::Profile(profile, L, State, IsSink); |
304 | NodeTy* V = Nodes.FindNodeOrInsertPos(profile, InsertPos); |
305 | |
306 | if (!V) { |
307 | if (!FreeNodes.empty()) { |
308 | V = FreeNodes.back(); |
309 | FreeNodes.pop_back(); |
310 | } |
311 | else { |
312 | |
313 | V = (NodeTy*) getAllocator().Allocate<NodeTy>(); |
314 | } |
315 | |
316 | new (V) NodeTy(L, State, IsSink); |
317 | |
318 | if (ReclaimNodeInterval) |
319 | ChangedNodes.push_back(V); |
320 | |
321 | |
322 | Nodes.InsertNode(V, InsertPos); |
323 | ++NumNodes; |
324 | |
325 | if (IsNew) *IsNew = true; |
326 | } |
327 | else |
328 | if (IsNew) *IsNew = false; |
329 | |
330 | return V; |
331 | } |
332 | |
333 | ExplodedNode *ExplodedGraph::createUncachedNode(const ProgramPoint &L, |
334 | ProgramStateRef State, |
335 | bool IsSink) { |
336 | NodeTy *V = (NodeTy *) getAllocator().Allocate<NodeTy>(); |
337 | new (V) NodeTy(L, State, IsSink); |
338 | return V; |
339 | } |
340 | |
341 | std::unique_ptr<ExplodedGraph> |
342 | ExplodedGraph::trim(ArrayRef<const NodeTy *> Sinks, |
343 | InterExplodedGraphMap *ForwardMap, |
344 | InterExplodedGraphMap *InverseMap) const { |
345 | if (Nodes.empty()) |
346 | return nullptr; |
347 | |
348 | using Pass1Ty = llvm::DenseSet<const ExplodedNode *>; |
349 | Pass1Ty Pass1; |
350 | |
351 | using Pass2Ty = InterExplodedGraphMap; |
352 | InterExplodedGraphMap Pass2Scratch; |
353 | Pass2Ty &Pass2 = ForwardMap ? *ForwardMap : Pass2Scratch; |
354 | |
355 | SmallVector<const ExplodedNode*, 10> WL1, WL2; |
356 | |
357 | |
358 | for (const auto Sink : Sinks) |
359 | if (Sink) |
360 | WL1.push_back(Sink); |
361 | |
362 | |
363 | while (!WL1.empty()) { |
364 | const ExplodedNode *N = WL1.pop_back_val(); |
365 | |
366 | |
367 | if (!Pass1.insert(N).second) |
368 | continue; |
369 | |
370 | |
371 | if (N->Preds.empty()) { |
372 | WL2.push_back(N); |
373 | continue; |
374 | } |
375 | |
376 | |
377 | WL1.append(N->Preds.begin(), N->Preds.end()); |
378 | } |
379 | |
380 | |
381 | if (WL2.empty()) |
382 | return nullptr; |
383 | |
384 | |
385 | std::unique_ptr<ExplodedGraph> G = MakeEmptyGraph(); |
386 | |
387 | |
388 | while (!WL2.empty()) { |
389 | const ExplodedNode *N = WL2.pop_back_val(); |
390 | |
391 | |
392 | if (Pass2.find(N) != Pass2.end()) |
393 | continue; |
394 | |
395 | |
396 | |
397 | ExplodedNode *NewN = G->createUncachedNode(N->getLocation(), N->State, N->isSink()); |
398 | Pass2[N] = NewN; |
399 | |
400 | |
401 | if (InverseMap) (*InverseMap)[NewN] = N; |
402 | |
403 | |
404 | if (N->Preds.empty()) |
405 | G->addRoot(NewN); |
406 | |
407 | |
408 | |
409 | |
410 | |
411 | |
412 | for (ExplodedNode::pred_iterator I = N->Preds.begin(), E = N->Preds.end(); |
413 | I != E; ++I) { |
414 | Pass2Ty::iterator PI = Pass2.find(*I); |
415 | if (PI == Pass2.end()) |
416 | continue; |
417 | |
418 | NewN->addPredecessor(const_cast<ExplodedNode *>(PI->second), *G); |
419 | } |
420 | |
421 | |
422 | |
423 | |
424 | |
425 | for (ExplodedNode::succ_iterator I = N->Succs.begin(), E = N->Succs.end(); |
426 | I != E; ++I) { |
427 | Pass2Ty::iterator PI = Pass2.find(*I); |
428 | if (PI != Pass2.end()) { |
429 | const_cast<ExplodedNode *>(PI->second)->addPredecessor(NewN, *G); |
430 | continue; |
431 | } |
432 | |
433 | |
434 | if (Pass1.count(*I)) |
435 | WL2.push_back(*I); |
436 | } |
437 | } |
438 | |
439 | return G; |
440 | } |
441 | |