Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/StaticAnalyzer/Core/ExplodedGraph.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- ExplodedGraph.cpp - Local, Path-Sens. "Exploded Graph" -------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
//
9
//  This file defines the template classes ExplodedNode and ExplodedGraph,
10
//  which represent a path-sensitive, intra-procedural "exploded graph."
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
// Cleanup.
39
//===----------------------------------------------------------------------===//
40
41
30.9k
ExplodedGraph::ExplodedGraph() = default;
42
43
30.9k
ExplodedGraph::~ExplodedGraph() = default;
44
45
//===----------------------------------------------------------------------===//
46
// Node reclamation.
47
//===----------------------------------------------------------------------===//
48
49
682k
bool ExplodedGraph::isInterestingLValueExpr(const Expr *Ex) {
50
682k
  if (!Ex->isLValue())
51
355k
    return false;
52
326k
  return isa<DeclRefExpr>(Ex) ||
53
326k
         
isa<MemberExpr>(Ex)102k
||
54
326k
         
isa<ObjCIvarRefExpr>(Ex)43.0k
;
55
326k
}
56
57
1.27M
bool ExplodedGraph::shouldCollect(const ExplodedNode *node) {
58
1.27M
  // First, we only consider nodes for reclamation of the following
59
1.27M
  // conditions apply:
60
1.27M
  //
61
1.27M
  // (1) 1 predecessor (that has one successor)
62
1.27M
  // (2) 1 successor (that has one predecessor)
63
1.27M
  //
64
1.27M
  // If a node has no successor it is on the "frontier", while a node
65
1.27M
  // with no predecessor is a root.
66
1.27M
  //
67
1.27M
  // After these prerequisites, we discard all "filler" nodes that
68
1.27M
  // are used only for intermediate processing, and are not essential
69
1.27M
  // for analyzer history:
70
1.27M
  //
71
1.27M
  // (a) PreStmtPurgeDeadSymbols
72
1.27M
  //
73
1.27M
  // We then discard all other nodes where *all* of the following conditions
74
1.27M
  // apply:
75
1.27M
  //
76
1.27M
  // (3) The ProgramPoint is for a PostStmt, but not a PostStore.
77
1.27M
  // (4) There is no 'tag' for the ProgramPoint.
78
1.27M
  // (5) The 'store' is the same as the predecessor.
79
1.27M
  // (6) The 'GDM' is the same as the predecessor.
80
1.27M
  // (7) The LocationContext is the same as the predecessor.
81
1.27M
  // (8) Expressions that are *not* lvalue expressions.
82
1.27M
  // (9) The PostStmt isn't for a non-consumed Stmt or Expr.
83
1.27M
  // (10) The successor is neither a CallExpr StmtPoint nor a CallEnter or
84
1.27M
  //      PreImplicitCall (so that we would be able to find it when retrying a
85
1.27M
  //      call with no inlining).
86
1.27M
  // FIXME: It may be safe to reclaim PreCall and PostCall nodes as well.
87
1.27M
88
1.27M
  // Conditions 1 and 2.
89
1.27M
  if (node->pred_size() != 1 || 
node->succ_size() != 11.27M
)
90
70.6k
    return false;
91
1.20M
92
1.20M
  const ExplodedNode *pred = *(node->pred_begin());
93
1.20M
  if (pred->succ_size() != 1)
94
53.5k
    return false;
95
1.15M
96
1.15M
  const ExplodedNode *succ = *(node->succ_begin());
97
1.15M
  if (succ->pred_size() != 1)
98
4.62k
    return false;
99
1.14M
100
1.14M
  // Now reclaim any nodes that are (by definition) not essential to
101
1.14M
  // analysis history and are not consulted by any client code.
102
1.14M
  ProgramPoint progPoint = node->getLocation();
103
1.14M
  if (progPoint.getAs<PreStmtPurgeDeadSymbols>())
104
134k
    return !progPoint.getTag();
105
1.01M
106
1.01M
  // Condition 3.
107
1.01M
  if (!progPoint.getAs<PostStmt>() || 
progPoint.getAs<PostStore>()799k
)
108
252k
    return false;
109
758k
110
758k
  // Condition 4.
111
758k
  if (progPoint.getTag())
112
43.5k
    return false;
113
714k
114
714k
  // Conditions 5, 6, and 7.
115
714k
  ProgramStateRef state = node->getState();
116
714k
  ProgramStateRef pred_state = pred->getState();
117
714k
  if (state->store != pred_state->store || 
state->GDM != pred_state->GDM702k
||
118
714k
      
progPoint.getLocationContext() != pred->getLocationContext()699k
)
119
15.1k
    return false;
120
699k
121
699k
  // All further checks require expressions. As per #3, we know that we have
122
699k
  // a PostStmt.
123
699k
  const Expr *Ex = dyn_cast<Expr>(progPoint.castAs<PostStmt>().getStmt());
124
699k
  if (!Ex)
125
20.2k
    return false;
126
679k
127
679k
  // Condition 8.
128
679k
  // Do not collect nodes for "interesting" lvalue expressions since they are
129
679k
  // used extensively for generating path diagnostics.
130
679k
  if (isInterestingLValueExpr(Ex))
131
282k
    return false;
132
396k
133
396k
  // Condition 9.
134
396k
  // Do not collect nodes for non-consumed Stmt or Expr to ensure precise
135
396k
  // diagnostic generation; specifically, so that we could anchor arrows
136
396k
  // pointing to the beginning of statements (as written in code).
137
396k
  ParentMap &PM = progPoint.getLocationContext()->getParentMap();
138
396k
  if (!PM.isConsumedExpr(Ex))
139
23.2k
    return false;
140
373k
141
373k
  // Condition 10.
142
373k
  const ProgramPoint SuccLoc = succ->getLocation();
143
373k
  if (Optional<StmtPoint> SP = SuccLoc.getAs<StmtPoint>())
144
370k
    if (CallEvent::isCallStmt(SP->getStmt()))
145
30.4k
      return false;
146
343k
147
343k
  // Condition 10, continuation.
148
343k
  if (SuccLoc.getAs<CallEnter>() || 
SuccLoc.getAs<PreImplicitCall>()342k
)
149
233
    return false;
150
342k
151
342k
  return true;
152
342k
}
153
154
342k
void ExplodedGraph::collectNode(ExplodedNode *node) {
155
342k
  // Removing a node means:
156
342k
  // (a) changing the predecessors successor to the successor of this node
157
342k
  // (b) changing the successors predecessor to the predecessor of this node
158
342k
  // (c) Putting 'node' onto freeNodes.
159
342k
  assert(node->pred_size() == 1 || node->succ_size() == 1);
160
342k
  ExplodedNode *pred = *(node->pred_begin());
161
342k
  ExplodedNode *succ = *(node->succ_begin());
162
342k
  pred->replaceSuccessor(succ);
163
342k
  succ->replacePredecessor(pred);
164
342k
  FreeNodes.push_back(node);
165
342k
  Nodes.RemoveNode(node);
166
342k
  --NumNodes;
167
342k
  node->~ExplodedNode();
168
342k
}
169
170
876k
void ExplodedGraph::reclaimRecentlyAllocatedNodes() {
171
876k
  if (ChangedNodes.empty())
172
2
    return;
173
876k
174
876k
  // Only periodically reclaim nodes so that we can build up a set of
175
876k
  // nodes that meet the reclamation criteria.  Freshly created nodes
176
876k
  // by definition have no successor, and thus cannot be reclaimed (see below).
177
876k
  assert(ReclaimCounter > 0);
178
876k
  if (--ReclaimCounter != 0)
179
875k
    return;
180
548
  ReclaimCounter = ReclaimNodeInterval;
181
548
182
548
  for (const auto node : ChangedNodes)
183
1.27M
    if (shouldCollect(node))
184
342k
      collectNode(node);
185
548
  ChangedNodes.clear();
186
548
}
187
188
//===----------------------------------------------------------------------===//
189
// ExplodedNode.
190
//===----------------------------------------------------------------------===//
191
192
// An NodeGroup's storage type is actually very much like a TinyPtrVector:
193
// it can be either a pointer to a single ExplodedNode, or a pointer to a
194
// BumpVector allocated with the ExplodedGraph's allocator. This allows the
195
// common case of single-node NodeGroups to be implemented with no extra memory.
196
//
197
// Consequently, each of the NodeGroup methods have up to four cases to handle:
198
// 1. The flag is set and this group does not actually contain any nodes.
199
// 2. The group is empty, in which case the storage value is null.
200
// 3. The group contains a single node.
201
// 4. The group contains more than one node.
202
using ExplodedNodeVector = BumpVector<ExplodedNode *>;
203
using GroupStorage = llvm::PointerUnion<ExplodedNode *, ExplodedNodeVector *>;
204
205
4.32M
void ExplodedNode::addPredecessor(ExplodedNode *V, ExplodedGraph &G) {
206
4.32M
  assert(!V->isSink());
207
4.32M
  Preds.addNode(V, G);
208
4.32M
  V->Succs.addNode(this, G);
209
4.32M
}
210
211
685k
void ExplodedNode::NodeGroup::replaceNode(ExplodedNode *node) {
212
685k
  assert(!getFlag());
213
685k
214
685k
  GroupStorage &Storage = reinterpret_cast<GroupStorage&>(P);
215
685k
  assert(Storage.is<ExplodedNode *>());
216
685k
  Storage = node;
217
685k
  assert(Storage.is<ExplodedNode *>());
218
685k
}
219
220
8.64M
void ExplodedNode::NodeGroup::addNode(ExplodedNode *N, ExplodedGraph &G) {
221
8.64M
  assert(!getFlag());
222
8.64M
223
8.64M
  GroupStorage &Storage = reinterpret_cast<GroupStorage&>(P);
224
8.64M
  if (Storage.isNull()) {
225
8.60M
    Storage = N;
226
8.60M
    assert(Storage.is<ExplodedNode *>());
227
8.60M
    return;
228
8.60M
  }
229
41.4k
230
41.4k
  ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>();
231
41.4k
232
41.4k
  if (!V) {
233
37.8k
    // Switch from single-node to multi-node representation.
234
37.8k
    ExplodedNode *Old = Storage.get<ExplodedNode *>();
235
37.8k
236
37.8k
    BumpVectorContext &Ctx = G.getNodeAllocator();
237
37.8k
    V = G.getAllocator().Allocate<ExplodedNodeVector>();
238
37.8k
    new (V) ExplodedNodeVector(Ctx, 4);
239
37.8k
    V->push_back(Old, Ctx);
240
37.8k
241
37.8k
    Storage = V;
242
37.8k
    assert(!getFlag());
243
37.8k
    assert(Storage.is<ExplodedNodeVector *>());
244
37.8k
  }
245
41.4k
246
41.4k
  V->push_back(N, G.getNodeAllocator());
247
41.4k
}
248
249
4.96M
unsigned ExplodedNode::NodeGroup::size() const {
250
4.96M
  if (getFlag())
251
2.66k
    return 0;
252
4.95M
253
4.95M
  const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P);
254
4.95M
  if (Storage.isNull())
255
40.4k
    return 0;
256
4.91M
  if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>())
257
87.1k
    return V->size();
258
4.83M
  return 1;
259
4.83M
}
260
261
13.0M
ExplodedNode * const *ExplodedNode::NodeGroup::begin() const {
262
13.0M
  if (getFlag())
263
2.76k
    return nullptr;
264
13.0M
265
13.0M
  const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P);
266
13.0M
  if (Storage.isNull())
267
10.2k
    return nullptr;
268
13.0M
  if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>())
269
8.48k
    return V->begin();
270
12.9M
  return Storage.getAddrOfPtr1();
271
12.9M
}
272
273
5.48M
ExplodedNode * const *ExplodedNode::NodeGroup::end() const {
274
5.48M
  if (getFlag())
275
2.76k
    return nullptr;
276
5.47M
277
5.47M
  const GroupStorage &Storage = reinterpret_cast<const GroupStorage &>(P);
278
5.47M
  if (Storage.isNull())
279
10.2k
    return nullptr;
280
5.46M
  if (ExplodedNodeVector *V = Storage.dyn_cast<ExplodedNodeVector *>())
281
7.75k
    return V->end();
282
5.46M
  return Storage.getAddrOfPtr1() + 1;
283
5.46M
}
284
285
0
int64_t ExplodedNode::getID(ExplodedGraph *G) const {
286
0
  return G->getAllocator().identifyKnownAlignedObject<ExplodedNode>(this);
287
0
}
288
289
0
bool ExplodedNode::isTrivial() const {
290
0
  return pred_size() == 1 && succ_size() == 1 &&
291
0
         getFirstPred()->getState()->getID() == getState()->getID() &&
292
0
         getFirstPred()->succ_size() == 1;
293
0
}
294
295
ExplodedNode *ExplodedGraph::getNode(const ProgramPoint &L,
296
                                     ProgramStateRef State,
297
                                     bool IsSink,
298
2.21M
                                     bool* IsNew) {
299
2.21M
  // Profile 'State' to determine if we already have an existing node.
300
2.21M
  llvm::FoldingSetNodeID profile;
301
2.21M
  void *InsertPos = nullptr;
302
2.21M
303
2.21M
  NodeTy::Profile(profile, L, State, IsSink);
304
2.21M
  NodeTy* V = Nodes.FindNodeOrInsertPos(profile, InsertPos);
305
2.21M
306
2.21M
  if (!V) {
307
2.21M
    if (!FreeNodes.empty()) {
308
340k
      V = FreeNodes.back();
309
340k
      FreeNodes.pop_back();
310
340k
    }
311
1.87M
    else {
312
1.87M
      // Allocate a new node.
313
1.87M
      V = (NodeTy*) getAllocator().Allocate<NodeTy>();
314
1.87M
    }
315
2.21M
316
2.21M
    new (V) NodeTy(L, State, IsSink);
317
2.21M
318
2.21M
    if (ReclaimNodeInterval)
319
2.21M
      ChangedNodes.push_back(V);
320
2.21M
321
2.21M
    // Insert the node into the node set and return it.
322
2.21M
    Nodes.InsertNode(V, InsertPos);
323
2.21M
    ++NumNodes;
324
2.21M
325
2.21M
    if (IsNew) *IsNew = true;
326
2.21M
  }
327
6.00k
  else
328
6.00k
    if (IsNew) *IsNew = false;
329
2.21M
330
2.21M
  return V;
331
2.21M
}
332
333
ExplodedNode *ExplodedGraph::createUncachedNode(const ProgramPoint &L,
334
                                                ProgramStateRef State,
335
2.13M
                                                bool IsSink) {
336
2.13M
  NodeTy *V = (NodeTy *) getAllocator().Allocate<NodeTy>();
337
2.13M
  new (V) NodeTy(L, State, IsSink);
338
2.13M
  return V;
339
2.13M
}
340
341
std::unique_ptr<ExplodedGraph>
342
ExplodedGraph::trim(ArrayRef<const NodeTy *> Sinks,
343
                    InterExplodedGraphMap *ForwardMap,
344
10.0k
                    InterExplodedGraphMap *InverseMap) const {
345
10.0k
  if (Nodes.empty())
346
0
    return nullptr;
347
10.0k
348
10.0k
  using Pass1Ty = llvm::DenseSet<const ExplodedNode *>;
349
10.0k
  Pass1Ty Pass1;
350
10.0k
351
10.0k
  using Pass2Ty = InterExplodedGraphMap;
352
10.0k
  InterExplodedGraphMap Pass2Scratch;
353
10.0k
  Pass2Ty &Pass2 = ForwardMap ? *ForwardMap : 
Pass2Scratch0
;
354
10.0k
355
10.0k
  SmallVector<const ExplodedNode*, 10> WL1, WL2;
356
10.0k
357
10.0k
  // ===- Pass 1 (reverse DFS) -===
358
10.0k
  for (const auto Sink : Sinks)
359
10.5k
    if (Sink)
360
10.5k
      WL1.push_back(Sink);
361
10.0k
362
10.0k
  // Process the first worklist until it is empty.
363
1.12M
  while (!WL1.empty()) {
364
1.11M
    const ExplodedNode *N = WL1.pop_back_val();
365
1.11M
366
1.11M
    // Have we already visited this node?  If so, continue to the next one.
367
1.11M
    if (!Pass1.insert(N).second)
368
1.16k
      continue;
369
1.11M
370
1.11M
    // If this is a root enqueue it to the second worklist.
371
1.11M
    if (N->Preds.empty()) {
372
10.0k
      WL2.push_back(N);
373
10.0k
      continue;
374
10.0k
    }
375
1.10M
376
1.10M
    // Visit our predecessors and enqueue them.
377
1.10M
    WL1.append(N->Preds.begin(), N->Preds.end());
378
1.10M
  }
379
10.0k
380
10.0k
  // We didn't hit a root? Return with a null pointer for the new graph.
381
10.0k
  if (WL2.empty())
382
0
    return nullptr;
383
10.0k
384
10.0k
  // Create an empty graph.
385
10.0k
  std::unique_ptr<ExplodedGraph> G = MakeEmptyGraph();
386
10.0k
387
10.0k
  // ===- Pass 2 (forward DFS to construct the new graph) -===
388
1.12M
  while (!WL2.empty()) {
389
1.11M
    const ExplodedNode *N = WL2.pop_back_val();
390
1.11M
391
1.11M
    // Skip this node if we have already processed it.
392
1.11M
    if (Pass2.find(N) != Pass2.end())
393
9
      continue;
394
1.11M
395
1.11M
    // Create the corresponding node in the new graph and record the mapping
396
1.11M
    // from the old node to the new node.
397
1.11M
    ExplodedNode *NewN = G->createUncachedNode(N->getLocation(), N->State, N->isSink());
398
1.11M
    Pass2[N] = NewN;
399
1.11M
400
1.11M
    // Also record the reverse mapping from the new node to the old node.
401
1.11M
    if (InverseMap) (*InverseMap)[NewN] = N;
402
1.11M
403
1.11M
    // If this node is a root, designate it as such in the graph.
404
1.11M
    if (N->Preds.empty())
405
10.0k
      G->addRoot(NewN);
406
1.11M
407
1.11M
    // In the case that some of the intended predecessors of NewN have already
408
1.11M
    // been created, we should hook them up as predecessors.
409
1.11M
410
1.11M
    // Walk through the predecessors of 'N' and hook up their corresponding
411
1.11M
    // nodes in the new graph (if any) to the freshly created node.
412
1.11M
    for (ExplodedNode::pred_iterator I = N->Preds.begin(), E = N->Preds.end();
413
2.22M
         I != E; 
++I1.10M
) {
414
1.10M
      Pass2Ty::iterator PI = Pass2.find(*I);
415
1.10M
      if (PI == Pass2.end())
416
630
        continue;
417
1.10M
418
1.10M
      NewN->addPredecessor(const_cast<ExplodedNode *>(PI->second), *G);
419
1.10M
    }
420
1.11M
421
1.11M
    // In the case that some of the intended successors of NewN have already
422
1.11M
    // been created, we should hook them up as successors.  Otherwise, enqueue
423
1.11M
    // the new nodes from the original graph that should have nodes created
424
1.11M
    // in the new graph.
425
1.11M
    for (ExplodedNode::succ_iterator I = N->Succs.begin(), E = N->Succs.end();
426
2.24M
         I != E; 
++I1.12M
) {
427
1.12M
      Pass2Ty::iterator PI = Pass2.find(*I);
428
1.12M
      if (PI != Pass2.end()) {
429
630
        const_cast<ExplodedNode *>(PI->second)->addPredecessor(NewN, *G);
430
630
        continue;
431
630
      }
432
1.12M
433
1.12M
      // Enqueue nodes to the worklist that were marked during pass 1.
434
1.12M
      if (Pass1.count(*I))
435
1.10M
        WL2.push_back(*I);
436
1.12M
    }
437
1.11M
  }
438
10.0k
439
10.0k
  return G;
440
10.0k
}