Coverage Report

Created: 2020-02-18 08:44

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/StaticAnalyzer/Core/BasicValueFactory.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- BasicValueFactory.cpp - Basic values for Path Sens analysis --------===//
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 BasicValueFactory, a class that manages the lifetime
10
//  of APSInt objects and symbolic constraints used by ExprEngine
11
//  and related classes.
12
//
13
//===----------------------------------------------------------------------===//
14
15
#include "clang/StaticAnalyzer/Core/PathSensitive/BasicValueFactory.h"
16
#include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h"
17
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
18
#include "clang/StaticAnalyzer/Core/PathSensitive/Store.h"
19
#include "clang/StaticAnalyzer/Core/PathSensitive/StoreRef.h"
20
#include "llvm/ADT/APSInt.h"
21
#include "llvm/ADT/FoldingSet.h"
22
#include "llvm/ADT/ImmutableList.h"
23
#include "llvm/ADT/STLExtras.h"
24
#include <cassert>
25
#include <cstdint>
26
#include <utility>
27
28
using namespace clang;
29
using namespace ento;
30
31
void CompoundValData::Profile(llvm::FoldingSetNodeID& ID, QualType T,
32
1.36k
                              llvm::ImmutableList<SVal> L) {
33
1.36k
  T.Profile(ID);
34
1.36k
  ID.AddPointer(L.getInternalPointer());
35
1.36k
}
36
37
void LazyCompoundValData::Profile(llvm::FoldingSetNodeID& ID,
38
                                  const StoreRef &store,
39
138k
                                  const TypedValueRegion *region) {
40
138k
  ID.AddPointer(store.getStore());
41
138k
  ID.AddPointer(region);
42
138k
}
43
44
void PointerToMemberData::Profile(
45
    llvm::FoldingSetNodeID& ID, const DeclaratorDecl *D,
46
29
    llvm::ImmutableList<const CXXBaseSpecifier *> L) {
47
29
  ID.AddPointer(D);
48
29
  ID.AddPointer(L.getInternalPointer());
49
29
}
50
51
using SValData = std::pair<SVal, uintptr_t>;
52
using SValPair = std::pair<SVal, SVal>;
53
54
namespace llvm {
55
56
template<> struct FoldingSetTrait<SValData> {
57
14
  static inline void Profile(const SValData& X, llvm::FoldingSetNodeID& ID) {
58
14
    X.first.Profile(ID);
59
14
    ID.AddPointer( (void*) X.second);
60
14
  }
61
};
62
63
template<> struct FoldingSetTrait<SValPair> {
64
0
  static inline void Profile(const SValPair& X, llvm::FoldingSetNodeID& ID) {
65
0
    X.first.Profile(ID);
66
0
    X.second.Profile(ID);
67
0
  }
68
};
69
70
} // namespace llvm
71
72
using PersistentSValsTy =
73
    llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValData>>;
74
75
using PersistentSValPairsTy =
76
    llvm::FoldingSet<llvm::FoldingSetNodeWrapper<SValPair>>;
77
78
12.6k
BasicValueFactory::~BasicValueFactory() {
79
12.6k
  // Note that the dstor for the contents of APSIntSet will never be called,
80
12.6k
  // so we iterate over the set and invoke the dstor for each APSInt.  This
81
12.6k
  // frees an aux. memory allocated to represent very large constants.
82
12.6k
  for (const auto &I : APSIntSet)
83
88.1k
    I.getValue().~APSInt();
84
12.6k
85
12.6k
  delete (PersistentSValsTy*) PersistentSVals;
86
12.6k
  delete (PersistentSValPairsTy*) PersistentSValPairs;
87
12.6k
}
88
89
1.79M
const llvm::APSInt& BasicValueFactory::getValue(const llvm::APSInt& X) {
90
1.79M
  llvm::FoldingSetNodeID ID;
91
1.79M
  void *InsertPos;
92
1.79M
93
1.79M
  using FoldNodeTy = llvm::FoldingSetNodeWrapper<llvm::APSInt>;
94
1.79M
95
1.79M
  X.Profile(ID);
96
1.79M
  FoldNodeTy* P = APSIntSet.FindNodeOrInsertPos(ID, InsertPos);
97
1.79M
98
1.79M
  if (!P) {
99
88.1k
    P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
100
88.1k
    new (P) FoldNodeTy(X);
101
88.1k
    APSIntSet.InsertNode(P, InsertPos);
102
88.1k
  }
103
1.79M
104
1.79M
  return *P;
105
1.79M
}
106
107
const llvm::APSInt& BasicValueFactory::getValue(const llvm::APInt& X,
108
66.4k
                                                bool isUnsigned) {
109
66.4k
  llvm::APSInt V(X, isUnsigned);
110
66.4k
  return getValue(V);
111
66.4k
}
112
113
const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, unsigned BitWidth,
114
713k
                                           bool isUnsigned) {
115
713k
  llvm::APSInt V(BitWidth, isUnsigned);
116
713k
  V = X;
117
713k
  return getValue(V);
118
713k
}
119
120
242k
const llvm::APSInt& BasicValueFactory::getValue(uint64_t X, QualType T) {
121
242k
  return getValue(getAPSIntType(T).getValue(X));
122
242k
}
123
124
const CompoundValData*
125
BasicValueFactory::getCompoundValData(QualType T,
126
1.11k
                                      llvm::ImmutableList<SVal> Vals) {
127
1.11k
  llvm::FoldingSetNodeID ID;
128
1.11k
  CompoundValData::Profile(ID, T, Vals);
129
1.11k
  void *InsertPos;
130
1.11k
131
1.11k
  CompoundValData* D = CompoundValDataSet.FindNodeOrInsertPos(ID, InsertPos);
132
1.11k
133
1.11k
  if (!D) {
134
881
    D = (CompoundValData*) BPAlloc.Allocate<CompoundValData>();
135
881
    new (D) CompoundValData(T, Vals);
136
881
    CompoundValDataSet.InsertNode(D, InsertPos);
137
881
  }
138
1.11k
139
1.11k
  return D;
140
1.11k
}
141
142
const LazyCompoundValData*
143
BasicValueFactory::getLazyCompoundValData(const StoreRef &store,
144
66.7k
                                          const TypedValueRegion *region) {
145
66.7k
  llvm::FoldingSetNodeID ID;
146
66.7k
  LazyCompoundValData::Profile(ID, store, region);
147
66.7k
  void *InsertPos;
148
66.7k
149
66.7k
  LazyCompoundValData *D =
150
66.7k
    LazyCompoundValDataSet.FindNodeOrInsertPos(ID, InsertPos);
151
66.7k
152
66.7k
  if (!D) {
153
14.3k
    D = (LazyCompoundValData*) BPAlloc.Allocate<LazyCompoundValData>();
154
14.3k
    new (D) LazyCompoundValData(store, region);
155
14.3k
    LazyCompoundValDataSet.InsertNode(D, InsertPos);
156
14.3k
  }
157
66.7k
158
66.7k
  return D;
159
66.7k
}
160
161
const PointerToMemberData *BasicValueFactory::getPointerToMemberData(
162
24
    const DeclaratorDecl *DD, llvm::ImmutableList<const CXXBaseSpecifier *> L) {
163
24
  llvm::FoldingSetNodeID ID;
164
24
  PointerToMemberData::Profile(ID, DD, L);
165
24
  void *InsertPos;
166
24
167
24
  PointerToMemberData *D =
168
24
      PointerToMemberDataSet.FindNodeOrInsertPos(ID, InsertPos);
169
24
170
24
  if (!D) {
171
20
    D = (PointerToMemberData*) BPAlloc.Allocate<PointerToMemberData>();
172
20
    new (D) PointerToMemberData(DD, L);
173
20
    PointerToMemberDataSet.InsertNode(D, InsertPos);
174
20
  }
175
24
176
24
  return D;
177
24
}
178
179
const PointerToMemberData *BasicValueFactory::accumCXXBase(
180
    llvm::iterator_range<CastExpr::path_const_iterator> PathRange,
181
24
    const nonloc::PointerToMember &PTM) {
182
24
  nonloc::PointerToMember::PTMDataType PTMDT = PTM.getPTMData();
183
24
  const DeclaratorDecl *DD = nullptr;
184
24
  llvm::ImmutableList<const CXXBaseSpecifier *> PathList;
185
24
186
24
  if (PTMDT.isNull() || PTMDT.is<const DeclaratorDecl *>()) {
187
12
    if (PTMDT.is<const DeclaratorDecl *>())
188
12
      DD = PTMDT.get<const DeclaratorDecl *>();
189
12
190
12
    PathList = CXXBaseListFactory.getEmptyList();
191
12
  } else { // const PointerToMemberData *
192
12
    const PointerToMemberData *PTMD =
193
12
        PTMDT.get<const PointerToMemberData *>();
194
12
    DD = PTMD->getDeclaratorDecl();
195
12
196
12
    PathList = PTMD->getCXXBaseList();
197
12
  }
198
24
199
24
  for (const auto &I : llvm::reverse(PathRange))
200
28
    PathList = prependCXXBase(I, PathList);
201
24
  return getPointerToMemberData(DD, PathList);
202
24
}
203
204
const llvm::APSInt*
205
BasicValueFactory::evalAPSInt(BinaryOperator::Opcode Op,
206
33.2k
                             const llvm::APSInt& V1, const llvm::APSInt& V2) {
207
33.2k
  switch (Op) {
208
0
    default:
209
0
      llvm_unreachable("Invalid Opcode.");
210
0
211
1.09k
    case BO_Mul:
212
1.09k
      return &getValue( V1 * V2 );
213
0
214
1.34k
    case BO_Div:
215
1.34k
      if (V2 == 0) // Avoid division by zero
216
1
        return nullptr;
217
1.34k
      return &getValue( V1 / V2 );
218
1.34k
219
1.34k
    case BO_Rem:
220
19
      if (V2 == 0) // Avoid division by zero
221
0
        return nullptr;
222
19
      return &getValue( V1 % V2 );
223
19
224
15.7k
    case BO_Add:
225
15.7k
      return &getValue( V1 + V2 );
226
19
227
1.42k
    case BO_Sub:
228
1.42k
      return &getValue( V1 - V2 );
229
19
230
341
    case BO_Shl: {
231
341
      // FIXME: This logic should probably go higher up, where we can
232
341
      // test these conditions symbolically.
233
341
234
341
      if (V2.isSigned() && 
V2.isNegative()329
)
235
3
        return nullptr;
236
338
237
338
      uint64_t Amt = V2.getZExtValue();
238
338
239
338
      if (Amt >= V1.getBitWidth())
240
6
        return nullptr;
241
332
242
332
      if (!Ctx.getLangOpts().CPlusPlus2a) {
243
330
        if (V1.isSigned() && 
V1.isNegative()174
)
244
2
          return nullptr;
245
328
246
328
        if (V1.isSigned() && 
Amt > V1.countLeadingZeros()172
)
247
2
          return nullptr;
248
328
      }
249
328
250
328
      return &getValue( V1.operator<<( (unsigned) Amt ));
251
328
    }
252
328
253
398
    case BO_Shr: {
254
398
      // FIXME: This logic should probably go higher up, where we can
255
398
      // test these conditions symbolically.
256
398
257
398
      if (V2.isSigned() && V2.isNegative())
258
0
        return nullptr;
259
398
260
398
      uint64_t Amt = V2.getZExtValue();
261
398
262
398
      if (Amt >= V1.getBitWidth())
263
0
        return nullptr;
264
398
265
398
      return &getValue( V1.operator>>( (unsigned) Amt ));
266
398
    }
267
398
268
4.96k
    case BO_LT:
269
4.96k
      return &getTruthValue( V1 < V2 );
270
398
271
398
    case BO_GT:
272
329
      return &getTruthValue( V1 > V2 );
273
398
274
398
    case BO_LE:
275
89
      return &getTruthValue( V1 <= V2 );
276
398
277
1.14k
    case BO_GE:
278
1.14k
      return &getTruthValue( V1 >= V2 );
279
398
280
5.34k
    case BO_EQ:
281
5.34k
      return &getTruthValue( V1 == V2 );
282
398
283
398
    case BO_NE:
284
307
      return &getTruthValue( V1 != V2 );
285
398
286
398
      // Note: LAnd, LOr, Comma are handled specially by higher-level logic.
287
398
288
410
    case BO_And:
289
410
      return &getValue( V1 & V2 );
290
398
291
398
    case BO_Or:
292
304
      return &getValue( V1 | V2 );
293
398
294
398
    case BO_Xor:
295
7
      return &getValue( V1 ^ V2 );
296
33.2k
  }
297
33.2k
}
298
299
const std::pair<SVal, uintptr_t>&
300
132
BasicValueFactory::getPersistentSValWithData(const SVal& V, uintptr_t Data) {
301
132
  // Lazily create the folding set.
302
132
  if (!PersistentSVals) 
PersistentSVals = new PersistentSValsTy()102
;
303
132
304
132
  llvm::FoldingSetNodeID ID;
305
132
  void *InsertPos;
306
132
  V.Profile(ID);
307
132
  ID.AddPointer((void*) Data);
308
132
309
132
  PersistentSValsTy& Map = *((PersistentSValsTy*) PersistentSVals);
310
132
311
132
  using FoldNodeTy = llvm::FoldingSetNodeWrapper<SValData>;
312
132
313
132
  FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos);
314
132
315
132
  if (!P) {
316
118
    P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
317
118
    new (P) FoldNodeTy(std::make_pair(V, Data));
318
118
    Map.InsertNode(P, InsertPos);
319
118
  }
320
132
321
132
  return P->getValue();
322
132
}
323
324
const std::pair<SVal, SVal>&
325
0
BasicValueFactory::getPersistentSValPair(const SVal& V1, const SVal& V2) {
326
0
  // Lazily create the folding set.
327
0
  if (!PersistentSValPairs) PersistentSValPairs = new PersistentSValPairsTy();
328
0
329
0
  llvm::FoldingSetNodeID ID;
330
0
  void *InsertPos;
331
0
  V1.Profile(ID);
332
0
  V2.Profile(ID);
333
0
334
0
  PersistentSValPairsTy& Map = *((PersistentSValPairsTy*) PersistentSValPairs);
335
0
336
0
  using FoldNodeTy = llvm::FoldingSetNodeWrapper<SValPair>;
337
0
338
0
  FoldNodeTy* P = Map.FindNodeOrInsertPos(ID, InsertPos);
339
0
340
0
  if (!P) {
341
0
    P = (FoldNodeTy*) BPAlloc.Allocate<FoldNodeTy>();
342
0
    new (P) FoldNodeTy(std::make_pair(V1, V2));
343
0
    Map.InsertNode(P, InsertPos);
344
0
  }
345
0
346
0
  return P->getValue();
347
0
}
348
349
0
const SVal* BasicValueFactory::getPersistentSVal(SVal X) {
350
0
  return &getPersistentSValWithData(X, 0).first;
351
0
}