Coverage Report

Created: 2020-02-25 14:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/Interp/Pointer.h
Line
Count
Source (jump to first uncovered line)
1
//===--- Pointer.h - Types for the constexpr VM -----------------*- C++ -*-===//
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
// Defines the classes responsible for pointer tracking.
10
//
11
//===----------------------------------------------------------------------===//
12
13
#ifndef LLVM_CLANG_AST_INTERP_POINTER_H
14
#define LLVM_CLANG_AST_INTERP_POINTER_H
15
16
#include "Descriptor.h"
17
#include "InterpBlock.h"
18
#include "clang/AST/ComparisonCategories.h"
19
#include "clang/AST/Decl.h"
20
#include "clang/AST/DeclCXX.h"
21
#include "clang/AST/Expr.h"
22
#include "llvm/ADT/PointerUnion.h"
23
#include "llvm/Support/raw_ostream.h"
24
25
namespace clang {
26
namespace interp {
27
class Block;
28
class DeadBlock;
29
class Context;
30
class InterpState;
31
class Pointer;
32
class Function;
33
enum PrimType : unsigned;
34
35
/// A pointer to a memory block, live or dead.
36
///
37
/// This object can be allocated into interpreter stack frames. If pointing to
38
/// a live block, it is a link in the chain of pointers pointing to the block.
39
class Pointer {
40
private:
41
  static constexpr unsigned PastEndMark = (unsigned)-1;
42
  static constexpr unsigned RootPtrMark = (unsigned)-1;
43
44
public:
45
8
  Pointer() {}
46
  Pointer(Block *B);
47
  Pointer(const Pointer &P);
48
  Pointer(Pointer &&P);
49
  ~Pointer();
50
51
  void operator=(const Pointer &P);
52
  void operator=(Pointer &&P);
53
54
  /// Converts the pointer to an APValue.
55
  APValue toAPValue() const;
56
57
  /// Offsets a pointer inside an array.
58
0
  Pointer atIndex(unsigned Idx) const {
59
0
    if (Base == RootPtrMark)
60
0
      return Pointer(Pointee, RootPtrMark, getDeclDesc()->getSize());
61
0
    unsigned Off = Idx * elemSize();
62
0
    if (getFieldDesc()->ElemDesc)
63
0
      Off += sizeof(InlineDescriptor);
64
0
    else
65
0
      Off += sizeof(InitMap *);
66
0
    return Pointer(Pointee, Base, Base + Off);
67
0
  }
68
69
  /// Creates a pointer to a field.
70
0
  Pointer atField(unsigned Off) const {
71
0
    unsigned Field = Offset + Off;
72
0
    return Pointer(Pointee, Field, Field);
73
0
  }
74
75
  /// Restricts the scope of an array element pointer.
76
0
  Pointer narrow() const {
77
0
    // Null pointers cannot be narrowed.
78
0
    if (isZero() || isUnknownSizeArray())
79
0
      return *this;
80
0
81
0
    // Pointer to an array of base types - enter block.
82
0
    if (Base == RootPtrMark)
83
0
      return Pointer(Pointee, 0, Offset == 0 ? Offset : PastEndMark);
84
0
85
0
    // Pointer is one past end - magic offset marks that.
86
0
    if (isOnePastEnd())
87
0
      return Pointer(Pointee, Base, PastEndMark);
88
0
89
0
    // Primitive arrays are a bit special since they do not have inline
90
0
    // descriptors. If Offset != Base, then the pointer already points to
91
0
    // an element and there is nothing to do. Otherwise, the pointer is
92
0
    // adjusted to the first element of the array.
93
0
    if (inPrimitiveArray()) {
94
0
      if (Offset != Base)
95
0
        return *this;
96
0
      return Pointer(Pointee, Base, Offset + sizeof(InitMap *));
97
0
    }
98
0
99
0
    // Pointer is to a field or array element - enter it.
100
0
    if (Offset != Base)
101
0
      return Pointer(Pointee, Offset, Offset);
102
0
103
0
    // Enter the first element of an array.
104
0
    if (!getFieldDesc()->isArray())
105
0
      return *this;
106
0
107
0
    const unsigned NewBase = Base + sizeof(InlineDescriptor);
108
0
    return Pointer(Pointee, NewBase, NewBase);
109
0
  }
110
111
  /// Expands a pointer to the containing array, undoing narrowing.
112
0
  Pointer expand() const {
113
0
    if (isElementPastEnd()) {
114
0
      // Revert to an outer one-past-end pointer.
115
0
      unsigned Adjust;
116
0
      if (inPrimitiveArray())
117
0
        Adjust = sizeof(InitMap *);
118
0
      else
119
0
        Adjust = sizeof(InlineDescriptor);
120
0
      return Pointer(Pointee, Base, Base + getSize() + Adjust);
121
0
    }
122
0
123
0
    // Do not step out of array elements.
124
0
    if (Base != Offset)
125
0
      return *this;
126
0
127
0
    // If at base, point to an array of base types.
128
0
    if (Base == 0)
129
0
      return Pointer(Pointee, RootPtrMark, 0);
130
0
131
0
    // Step into the containing array, if inside one.
132
0
    unsigned Next = Base - getInlineDesc()->Offset;
133
0
    Descriptor *Desc = Next == 0 ? getDeclDesc() : getDescriptor(Next)->Desc;
134
0
    if (!Desc->IsArray)
135
0
      return *this;
136
0
    return Pointer(Pointee, Next, Offset);
137
0
  }
138
139
  /// Checks if the pointer is null.
140
0
  bool isZero() const { return Pointee == nullptr; }
141
  /// Checks if the pointer is live.
142
0
  bool isLive() const { return Pointee && !Pointee->IsDead; }
143
  /// Checks if the item is a field in an object.
144
0
  bool isField() const { return Base != 0 && Base != RootPtrMark; }
145
146
  /// Accessor for information about the declaration site.
147
0
  Descriptor *getDeclDesc() const { return Pointee->Desc; }
148
0
  SourceLocation getDeclLoc() const { return getDeclDesc()->getLocation(); }
149
150
  /// Returns a pointer to the object of which this pointer is a field.
151
0
  Pointer getBase() const {
152
0
    if (Base == RootPtrMark) {
153
0
      assert(Offset == PastEndMark && "cannot get base of a block");
154
0
      return Pointer(Pointee, Base, 0);
155
0
    }
156
0
    assert(Offset == Base && "not an inner field");
157
0
    unsigned NewBase = Base - getInlineDesc()->Offset;
158
0
    return Pointer(Pointee, NewBase, NewBase);
159
0
  }
160
  /// Returns the parent array.
161
0
  Pointer getArray() const {
162
0
    if (Base == RootPtrMark) {
163
0
      assert(Offset != 0 && Offset != PastEndMark && "not an array element");
164
0
      return Pointer(Pointee, Base, 0);
165
0
    }
166
0
    assert(Offset != Base && "not an array element");
167
0
    return Pointer(Pointee, Base, Base);
168
0
  }
169
170
  /// Accessors for information about the innermost field.
171
0
  Descriptor *getFieldDesc() const {
172
0
    if (Base == 0 || Base == RootPtrMark)
173
0
      return getDeclDesc();
174
0
    return getInlineDesc()->Desc;
175
0
  }
176
177
  /// Returns the type of the innermost field.
178
0
  QualType getType() const { return getFieldDesc()->getType(); }
179
180
  /// Returns the element size of the innermost field.
181
0
  size_t elemSize() const {
182
0
    if (Base == RootPtrMark)
183
0
      return getDeclDesc()->getSize();
184
0
    return getFieldDesc()->getElemSize();
185
0
  }
186
  /// Returns the total size of the innermost field.
187
0
  size_t getSize() const { return getFieldDesc()->getSize(); }
188
189
  /// Returns the offset into an array.
190
0
  unsigned getOffset() const {
191
0
    assert(Offset != PastEndMark && "invalid offset");
192
0
    if (Base == RootPtrMark)
193
0
      return Offset;
194
0
195
0
    unsigned Adjust = 0;
196
0
    if (Offset != Base) {
197
0
      if (getFieldDesc()->ElemDesc)
198
0
        Adjust = sizeof(InlineDescriptor);
199
0
      else
200
0
        Adjust = sizeof(InitMap *);
201
0
    }
202
0
    return Offset - Base - Adjust;
203
0
  }
204
205
  /// Checks if the innermost field is an array.
206
0
  bool inArray() const { return getFieldDesc()->IsArray; }
207
  /// Checks if the structure is a primitive array.
208
0
  bool inPrimitiveArray() const { return getFieldDesc()->isPrimitiveArray(); }
209
  /// Checks if the structure is an array of unknown size.
210
0
  bool isUnknownSizeArray() const {
211
0
    return getFieldDesc()->isUnknownSizeArray();
212
0
  }
213
  /// Checks if the pointer points to an array.
214
0
  bool isArrayElement() const { return Base != Offset; }
215
  /// Pointer points directly to a block.
216
0
  bool isRoot() const {
217
0
    return (Base == 0 || Base == RootPtrMark) && Offset == 0;
218
0
  }
219
220
  /// Returns the record descriptor of a class.
221
0
  Record *getRecord() const { return getFieldDesc()->ElemRecord; }
222
  /// Returns the field information.
223
0
  const FieldDecl *getField() const { return getFieldDesc()->asFieldDecl(); }
224
225
  /// Checks if the object is a union.
226
  bool isUnion() const;
227
228
  /// Checks if the storage is extern.
229
0
  bool isExtern() const { return Pointee->isExtern(); }
230
  /// Checks if the storage is static.
231
0
  bool isStatic() const { return Pointee->isStatic(); }
232
  /// Checks if the storage is temporary.
233
0
  bool isTemporary() const { return Pointee->isTemporary(); }
234
  /// Checks if the storage is a static temporary.
235
0
  bool isStaticTemporary() const { return isStatic() && isTemporary(); }
236
237
  /// Checks if the field is mutable.
238
0
  bool isMutable() const { return Base != 0 && getInlineDesc()->IsMutable; }
239
  /// Checks if an object was initialized.
240
  bool isInitialized() const;
241
  /// Checks if the object is active.
242
0
  bool isActive() const { return Base == 0 || getInlineDesc()->IsActive; }
243
  /// Checks if a structure is a base class.
244
0
  bool isBaseClass() const { return isField() && getInlineDesc()->IsBase; }
245
246
  /// Checks if an object or a subfield is mutable.
247
0
  bool isConst() const {
248
0
    return Base == 0 ? getDeclDesc()->IsConst : getInlineDesc()->IsConst;
249
0
  }
250
251
  /// Returns the declaration ID.
252
0
  llvm::Optional<unsigned> getDeclID() const { return Pointee->getDeclID(); }
253
254
  /// Returns the byte offset from the start.
255
0
  unsigned getByteOffset() const {
256
0
    return Offset;
257
0
  }
258
259
  /// Returns the number of elements.
260
0
  unsigned getNumElems() const { return getSize() / elemSize(); }
261
262
  /// Returns the index into an array.
263
0
  int64_t getIndex() const {
264
0
    if (isElementPastEnd())
265
0
      return 1;
266
0
    if (auto ElemSize = elemSize())
267
0
      return getOffset() / ElemSize;
268
0
    return 0;
269
0
  }
270
271
  /// Checks if the index is one past end.
272
0
  bool isOnePastEnd() const {
273
0
    return isElementPastEnd() || getSize() == getOffset();
274
0
  }
275
276
  /// Checks if the pointer is an out-of-bounds element pointer.
277
0
  bool isElementPastEnd() const { return Offset == PastEndMark; }
278
279
  /// Dereferences the pointer, if it's live.
280
0
  template <typename T> T &deref() const {
281
0
    assert(isLive() && "Invalid pointer");
282
0
    return *reinterpret_cast<T *>(Pointee->data() + Offset);
283
0
  }
Unexecuted instantiation: clang::interp::Integral<8u, true>& clang::interp::Pointer::deref<clang::interp::Integral<8u, true> >() const
Unexecuted instantiation: clang::interp::Integral<8u, false>& clang::interp::Pointer::deref<clang::interp::Integral<8u, false> >() const
Unexecuted instantiation: clang::interp::Integral<16u, true>& clang::interp::Pointer::deref<clang::interp::Integral<16u, true> >() const
Unexecuted instantiation: clang::interp::Integral<16u, false>& clang::interp::Pointer::deref<clang::interp::Integral<16u, false> >() const
Unexecuted instantiation: clang::interp::Integral<32u, true>& clang::interp::Pointer::deref<clang::interp::Integral<32u, true> >() const
Unexecuted instantiation: clang::interp::Integral<32u, false>& clang::interp::Pointer::deref<clang::interp::Integral<32u, false> >() const
Unexecuted instantiation: clang::interp::Integral<64u, true>& clang::interp::Pointer::deref<clang::interp::Integral<64u, true> >() const
Unexecuted instantiation: clang::interp::Integral<64u, false>& clang::interp::Pointer::deref<clang::interp::Integral<64u, false> >() const
Unexecuted instantiation: clang::interp::Boolean& clang::interp::Pointer::deref<clang::interp::Boolean>() const
Unexecuted instantiation: clang::interp::Pointer& clang::interp::Pointer::deref<clang::interp::Pointer>() const
284
285
  /// Dereferences a primitive element.
286
  template <typename T> T &elem(unsigned I) const {
287
    return reinterpret_cast<T *>(Pointee->data())[I];
288
  }
289
290
  /// Initializes a field.
291
  void initialize() const;
292
  /// Activats a field.
293
  void activate() const;
294
  /// Deactivates an entire strurcutre.
295
  void deactivate() const;
296
297
  /// Checks if two pointers are comparable.
298
  static bool hasSameBase(const Pointer &A, const Pointer &B);
299
  /// Checks if two pointers can be subtracted.
300
  static bool hasSameArray(const Pointer &A, const Pointer &B);
301
302
  /// Prints the pointer.
303
0
  void print(llvm::raw_ostream &OS) const {
304
0
    OS << "{" << Base << ", " << Offset << ", ";
305
0
    if (Pointee)
306
0
      OS << Pointee->getSize();
307
0
    else
308
0
      OS << "nullptr";
309
0
    OS << "}";
310
0
  }
311
312
private:
313
  friend class Block;
314
  friend class DeadBlock;
315
316
  Pointer(Block *Pointee, unsigned Base, unsigned Offset);
317
318
  /// Returns the embedded descriptor preceding a field.
319
0
  InlineDescriptor *getInlineDesc() const { return getDescriptor(Base); }
320
321
  /// Returns a descriptor at a given offset.
322
0
  InlineDescriptor *getDescriptor(unsigned Offset) const {
323
0
    assert(Offset != 0 && "Not a nested pointer");
324
0
    return reinterpret_cast<InlineDescriptor *>(Pointee->data() + Offset) - 1;
325
0
  }
326
327
  /// Returns a reference to the pointer which stores the initialization map.
328
0
  InitMap *&getInitMap() const {
329
0
    return *reinterpret_cast<InitMap **>(Pointee->data() + Base);
330
0
  }
331
332
  /// The block the pointer is pointing to.
333
  Block *Pointee = nullptr;
334
  /// Start of the current subfield.
335
  unsigned Base = 0;
336
  /// Offset into the block.
337
  unsigned Offset = 0;
338
339
  /// Previous link in the pointer chain.
340
  Pointer *Prev = nullptr;
341
  /// Next link in the pointer chain.
342
  Pointer *Next = nullptr;
343
};
344
345
0
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Pointer &P) {
346
0
  P.print(OS);
347
0
  return OS;
348
0
}
349
350
} // namespace interp
351
} // namespace clang
352
353
#endif