Coverage Report

Created: 2024-01-17 10:31

/src/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 Pointer;
30
class Context;
31
enum PrimType : unsigned;
32
33
class Pointer;
34
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Pointer &P);
35
36
/// A pointer to a memory block, live or dead.
37
///
38
/// This object can be allocated into interpreter stack frames. If pointing to
39
/// a live block, it is a link in the chain of pointers pointing to the block.
40
///
41
/// In the simplest form, a Pointer has a Block* (the pointee) and both Base
42
/// and Offset are 0, which means it will point to raw data.
43
///
44
/// The Base field is used to access metadata about the data. For primitive
45
/// arrays, the Base is followed by an InitMap. In a variety of cases, the
46
/// Base is preceded by an InlineDescriptor, which is used to track the
47
/// initialization state, among other things.
48
///
49
/// The Offset field is used to access the actual data. In other words, the
50
/// data the pointer decribes can be found at
51
/// Pointee->rawData() + Pointer.Offset.
52
///
53
///
54
/// Pointee                      Offset
55
/// │                              │
56
/// │                              │
57
/// ▼                              ▼
58
/// ┌───────┬────────────┬─────────┬────────────────────────────┐
59
/// │ Block │ InlineDesc │ InitMap │ Actual Data                │
60
/// └───────┴────────────┴─────────┴────────────────────────────┘
61
///                      ▲
62
///                      │
63
///                      │
64
///                     Base
65
class Pointer {
66
private:
67
  static constexpr unsigned PastEndMark = ~0u;
68
  static constexpr unsigned RootPtrMark = ~0u;
69
70
public:
71
0
  Pointer() {}
72
  Pointer(Block *B);
73
  Pointer(Block *B, unsigned BaseAndOffset);
74
  Pointer(const Pointer &P);
75
  Pointer(Pointer &&P);
76
  ~Pointer();
77
78
  void operator=(const Pointer &P);
79
  void operator=(Pointer &&P);
80
81
  /// Equality operators are just for tests.
82
0
  bool operator==(const Pointer &P) const {
83
0
    return Pointee == P.Pointee && Base == P.Base && Offset == P.Offset;
84
0
  }
85
86
0
  bool operator!=(const Pointer &P) const {
87
0
    return Pointee != P.Pointee || Base != P.Base || Offset != P.Offset;
88
0
  }
89
90
  /// Converts the pointer to an APValue.
91
  APValue toAPValue() const;
92
93
  /// Converts the pointer to a string usable in diagnostics.
94
  std::string toDiagnosticString(const ASTContext &Ctx) const;
95
96
0
  unsigned getIntegerRepresentation() const {
97
0
    return reinterpret_cast<uintptr_t>(Pointee) + Offset;
98
0
  }
99
100
  /// Converts the pointer to an APValue that is an rvalue.
101
  APValue toRValue(const Context &Ctx) const;
102
103
  /// Offsets a pointer inside an array.
104
0
  [[nodiscard]] Pointer atIndex(unsigned Idx) const {
105
0
    if (Base == RootPtrMark)
106
0
      return Pointer(Pointee, RootPtrMark, getDeclDesc()->getSize());
107
0
    unsigned Off = Idx * elemSize();
108
0
    if (getFieldDesc()->ElemDesc)
109
0
      Off += sizeof(InlineDescriptor);
110
0
    else
111
0
      Off += sizeof(InitMapPtr);
112
0
    return Pointer(Pointee, Base, Base + Off);
113
0
  }
114
115
  /// Creates a pointer to a field.
116
0
  [[nodiscard]] Pointer atField(unsigned Off) const {
117
0
    unsigned Field = Offset + Off;
118
0
    return Pointer(Pointee, Field, Field);
119
0
  }
120
121
  /// Subtract the given offset from the current Base and Offset
122
  /// of the pointer.
123
0
  [[nodiscard]]  Pointer atFieldSub(unsigned Off) const {
124
0
    assert(Offset >= Off);
125
0
    unsigned O = Offset - Off;
126
0
    return Pointer(Pointee, O, O);
127
0
  }
128
129
  /// Restricts the scope of an array element pointer.
130
0
  [[nodiscard]] Pointer narrow() const {
131
    // Null pointers cannot be narrowed.
132
0
    if (isZero() || isUnknownSizeArray())
133
0
      return *this;
134
135
    // Pointer to an array of base types - enter block.
136
0
    if (Base == RootPtrMark)
137
0
      return Pointer(Pointee, 0, Offset == 0 ? Offset : PastEndMark);
138
139
    // Pointer is one past end - magic offset marks that.
140
0
    if (isOnePastEnd())
141
0
      return Pointer(Pointee, Base, PastEndMark);
142
143
    // Primitive arrays are a bit special since they do not have inline
144
    // descriptors. If Offset != Base, then the pointer already points to
145
    // an element and there is nothing to do. Otherwise, the pointer is
146
    // adjusted to the first element of the array.
147
0
    if (inPrimitiveArray()) {
148
0
      if (Offset != Base)
149
0
        return *this;
150
0
      return Pointer(Pointee, Base, Offset + sizeof(InitMapPtr));
151
0
    }
152
153
    // Pointer is to a field or array element - enter it.
154
0
    if (Offset != Base)
155
0
      return Pointer(Pointee, Offset, Offset);
156
157
    // Enter the first element of an array.
158
0
    if (!getFieldDesc()->isArray())
159
0
      return *this;
160
161
0
    const unsigned NewBase = Base + sizeof(InlineDescriptor);
162
0
    return Pointer(Pointee, NewBase, NewBase);
163
0
  }
164
165
  /// Expands a pointer to the containing array, undoing narrowing.
166
0
  [[nodiscard]] Pointer expand() const {
167
0
    if (isElementPastEnd()) {
168
      // Revert to an outer one-past-end pointer.
169
0
      unsigned Adjust;
170
0
      if (inPrimitiveArray())
171
0
        Adjust = sizeof(InitMapPtr);
172
0
      else
173
0
        Adjust = sizeof(InlineDescriptor);
174
0
      return Pointer(Pointee, Base, Base + getSize() + Adjust);
175
0
    }
176
177
    // Do not step out of array elements.
178
0
    if (Base != Offset)
179
0
      return *this;
180
181
    // If at base, point to an array of base types.
182
0
    if (Base == 0)
183
0
      return Pointer(Pointee, RootPtrMark, 0);
184
185
    // Step into the containing array, if inside one.
186
0
    unsigned Next = Base - getInlineDesc()->Offset;
187
0
    const Descriptor *Desc =
188
0
        Next == 0 ? getDeclDesc() : getDescriptor(Next)->Desc;
189
0
    if (!Desc->IsArray)
190
0
      return *this;
191
0
    return Pointer(Pointee, Next, Offset);
192
0
  }
193
194
  /// Checks if the pointer is null.
195
0
  bool isZero() const { return Pointee == nullptr; }
196
  /// Checks if the pointer is live.
197
0
  bool isLive() const { return Pointee && !Pointee->IsDead; }
198
  /// Checks if the item is a field in an object.
199
0
  bool isField() const { return Base != 0 && Base != RootPtrMark; }
200
201
  /// Accessor for information about the declaration site.
202
0
  const Descriptor *getDeclDesc() const {
203
0
    assert(Pointee);
204
0
    return Pointee->Desc;
205
0
  }
206
0
  SourceLocation getDeclLoc() const { return getDeclDesc()->getLocation(); }
207
208
  /// Returns a pointer to the object of which this pointer is a field.
209
0
  [[nodiscard]] Pointer getBase() const {
210
0
    if (Base == RootPtrMark) {
211
0
      assert(Offset == PastEndMark && "cannot get base of a block");
212
0
      return Pointer(Pointee, Base, 0);
213
0
    }
214
0
    assert(Offset == Base && "not an inner field");
215
0
    unsigned NewBase = Base - getInlineDesc()->Offset;
216
0
    return Pointer(Pointee, NewBase, NewBase);
217
0
  }
218
  /// Returns the parent array.
219
0
  [[nodiscard]] Pointer getArray() const {
220
0
    if (Base == RootPtrMark) {
221
0
      assert(Offset != 0 && Offset != PastEndMark && "not an array element");
222
0
      return Pointer(Pointee, Base, 0);
223
0
    }
224
0
    assert(Offset != Base && "not an array element");
225
0
    return Pointer(Pointee, Base, Base);
226
0
  }
227
228
  /// Accessors for information about the innermost field.
229
0
  const Descriptor *getFieldDesc() const {
230
0
    if (Base == 0 || Base == RootPtrMark)
231
0
      return getDeclDesc();
232
0
    return getInlineDesc()->Desc;
233
0
  }
234
235
  /// Returns the type of the innermost field.
236
0
  QualType getType() const {
237
0
    if (inPrimitiveArray() && Offset != Base)
238
0
      return getFieldDesc()->getType()->getAsArrayTypeUnsafe()->getElementType();
239
0
    return getFieldDesc()->getType();
240
0
  }
241
242
0
  [[nodiscard]] Pointer getDeclPtr() const { return Pointer(Pointee); }
243
244
  /// Returns the element size of the innermost field.
245
0
  size_t elemSize() const {
246
0
    if (Base == RootPtrMark)
247
0
      return getDeclDesc()->getSize();
248
0
    return getFieldDesc()->getElemSize();
249
0
  }
250
  /// Returns the total size of the innermost field.
251
0
  size_t getSize() const { return getFieldDesc()->getSize(); }
252
253
  /// Returns the offset into an array.
254
0
  unsigned getOffset() const {
255
0
    assert(Offset != PastEndMark && "invalid offset");
256
0
    if (Base == RootPtrMark)
257
0
      return Offset;
258
259
0
    unsigned Adjust = 0;
260
0
    if (Offset != Base) {
261
0
      if (getFieldDesc()->ElemDesc)
262
0
        Adjust = sizeof(InlineDescriptor);
263
0
      else
264
0
        Adjust = sizeof(InitMapPtr);
265
0
    }
266
0
    return Offset - Base - Adjust;
267
0
  }
268
269
  /// Whether this array refers to an array, but not
270
  /// to the first element.
271
0
  bool isArrayRoot() const { return inArray() && Offset == Base; }
272
273
  /// Checks if the innermost field is an array.
274
0
  bool inArray() const { return getFieldDesc()->IsArray; }
275
  /// Checks if the structure is a primitive array.
276
0
  bool inPrimitiveArray() const { return getFieldDesc()->isPrimitiveArray(); }
277
  /// Checks if the structure is an array of unknown size.
278
0
  bool isUnknownSizeArray() const {
279
0
    return getFieldDesc()->isUnknownSizeArray();
280
0
  }
281
  /// Checks if the pointer points to an array.
282
0
  bool isArrayElement() const { return inArray() && Base != Offset; }
283
  /// Pointer points directly to a block.
284
0
  bool isRoot() const {
285
0
    return (Base == 0 || Base == RootPtrMark) && Offset == 0;
286
0
  }
287
288
  /// Returns the record descriptor of a class.
289
0
  const Record *getRecord() const { return getFieldDesc()->ElemRecord; }
290
  /// Returns the element record type, if this is a non-primive array.
291
0
  const Record *getElemRecord() const {
292
0
    const Descriptor *ElemDesc = getFieldDesc()->ElemDesc;
293
0
    return ElemDesc ? ElemDesc->ElemRecord : nullptr;
294
0
  }
295
  /// Returns the field information.
296
0
  const FieldDecl *getField() const { return getFieldDesc()->asFieldDecl(); }
297
298
  /// Checks if the object is a union.
299
  bool isUnion() const;
300
301
  /// Checks if the storage is extern.
302
0
  bool isExtern() const { return Pointee && Pointee->isExtern(); }
303
  /// Checks if the storage is static.
304
0
  bool isStatic() const {
305
0
    assert(Pointee);
306
0
    return Pointee->isStatic();
307
0
  }
308
  /// Checks if the storage is temporary.
309
0
  bool isTemporary() const {
310
0
    assert(Pointee);
311
0
    return Pointee->isTemporary();
312
0
  }
313
  /// Checks if the storage is a static temporary.
314
0
  bool isStaticTemporary() const { return isStatic() && isTemporary(); }
315
316
  /// Checks if the field is mutable.
317
0
  bool isMutable() const {
318
0
    return Base != 0 && getInlineDesc()->IsFieldMutable;
319
0
  }
320
  /// Checks if an object was initialized.
321
  bool isInitialized() const;
322
  /// Checks if the object is active.
323
0
  bool isActive() const { return Base == 0 || getInlineDesc()->IsActive; }
324
  /// Checks if a structure is a base class.
325
0
  bool isBaseClass() const { return isField() && getInlineDesc()->IsBase; }
326
  /// Checks if the pointer pointers to a dummy value.
327
0
  bool isDummy() const { return getDeclDesc()->isDummy(); }
328
329
  /// Checks if an object or a subfield is mutable.
330
0
  bool isConst() const {
331
0
    return Base == 0 ? getDeclDesc()->IsConst : getInlineDesc()->IsConst;
332
0
  }
333
334
  /// Returns the declaration ID.
335
0
  std::optional<unsigned> getDeclID() const {
336
0
    assert(Pointee);
337
0
    return Pointee->getDeclID();
338
0
  }
339
340
  /// Returns the byte offset from the start.
341
0
  unsigned getByteOffset() const {
342
0
    return Offset;
343
0
  }
344
345
  /// Returns the number of elements.
346
0
  unsigned getNumElems() const { return getSize() / elemSize(); }
347
348
0
  const Block *block() const { return Pointee; }
349
350
  /// Returns the index into an array.
351
0
  int64_t getIndex() const {
352
0
    if (isElementPastEnd())
353
0
      return 1;
354
355
    // narrow()ed element in a composite array.
356
0
    if (Base > 0 && Base == Offset)
357
0
      return 0;
358
359
0
    if (auto ElemSize = elemSize())
360
0
      return getOffset() / ElemSize;
361
0
    return 0;
362
0
  }
363
364
  /// Checks if the index is one past end.
365
0
  bool isOnePastEnd() const {
366
0
    if (!Pointee)
367
0
      return false;
368
0
    return isElementPastEnd() || getSize() == getOffset();
369
0
  }
370
371
  /// Checks if the pointer is an out-of-bounds element pointer.
372
0
  bool isElementPastEnd() const { return Offset == PastEndMark; }
373
374
  /// Dereferences the pointer, if it's live.
375
0
  template <typename T> T &deref() const {
376
0
    assert(isLive() && "Invalid pointer");
377
0
    assert(Pointee);
378
0
    if (isArrayRoot())
379
0
      return *reinterpret_cast<T *>(Pointee->rawData() + Base +
380
0
                                    sizeof(InitMapPtr));
381
382
0
    return *reinterpret_cast<T *>(Pointee->rawData() + Offset);
383
0
  }
Unexecuted instantiation: clang::interp::Floating& clang::interp::Pointer::deref<clang::interp::Floating>() const
Unexecuted instantiation: clang::interp::Pointer& clang::interp::Pointer::deref<clang::interp::Pointer>() const
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::IntegralAP<false>& clang::interp::Pointer::deref<clang::interp::IntegralAP<false> >() const
Unexecuted instantiation: clang::interp::IntegralAP<true>& clang::interp::Pointer::deref<clang::interp::IntegralAP<true> >() const
Unexecuted instantiation: clang::interp::Boolean& clang::interp::Pointer::deref<clang::interp::Boolean>() const
Unexecuted instantiation: clang::interp::FunctionPointer& clang::interp::Pointer::deref<clang::interp::FunctionPointer>() const
Unexecuted instantiation: unsigned char& clang::interp::Pointer::deref<unsigned char>() const
Unexecuted instantiation: signed char& clang::interp::Pointer::deref<signed char>() const
Unexecuted instantiation: char& clang::interp::Pointer::deref<char>() const
384
385
  /// Dereferences a primitive element.
386
  template <typename T> T &elem(unsigned I) const {
387
    assert(I < getNumElems());
388
    assert(Pointee);
389
    return reinterpret_cast<T *>(Pointee->data() + sizeof(InitMapPtr))[I];
390
  }
391
392
  /// Initializes a field.
393
  void initialize() const;
394
  /// Activats a field.
395
  void activate() const;
396
  /// Deactivates an entire strurcutre.
397
  void deactivate() const;
398
399
  /// Compare two pointers.
400
0
  ComparisonCategoryResult compare(const Pointer &Other) const {
401
0
    if (!hasSameBase(*this, Other))
402
0
      return ComparisonCategoryResult::Unordered;
403
404
0
    if (Offset < Other.Offset)
405
0
      return ComparisonCategoryResult::Less;
406
0
    else if (Offset > Other.Offset)
407
0
      return ComparisonCategoryResult::Greater;
408
409
0
    return ComparisonCategoryResult::Equal;
410
0
  }
411
412
  /// Checks if two pointers are comparable.
413
  static bool hasSameBase(const Pointer &A, const Pointer &B);
414
  /// Checks if two pointers can be subtracted.
415
  static bool hasSameArray(const Pointer &A, const Pointer &B);
416
417
  /// Prints the pointer.
418
0
  void print(llvm::raw_ostream &OS) const {
419
0
    OS << Pointee << " {";
420
0
    if (Base == RootPtrMark)
421
0
      OS << "rootptr, ";
422
0
    else
423
0
      OS << Base << ", ";
424
425
0
    if (Offset == PastEndMark)
426
0
      OS << "pastend, ";
427
0
    else
428
0
      OS << Offset << ", ";
429
430
0
    if (Pointee)
431
0
      OS << Pointee->getSize();
432
0
    else
433
0
      OS << "nullptr";
434
0
    OS << "}";
435
0
  }
436
437
private:
438
  friend class Block;
439
  friend class DeadBlock;
440
  friend struct InitMap;
441
442
  Pointer(Block *Pointee, unsigned Base, unsigned Offset);
443
444
  /// Returns the embedded descriptor preceding a field.
445
0
  InlineDescriptor *getInlineDesc() const { return getDescriptor(Base); }
446
447
  /// Returns a descriptor at a given offset.
448
0
  InlineDescriptor *getDescriptor(unsigned Offset) const {
449
0
    assert(Offset != 0 && "Not a nested pointer");
450
0
    assert(Pointee);
451
0
    return reinterpret_cast<InlineDescriptor *>(Pointee->rawData() + Offset) -
452
0
           1;
453
0
  }
454
455
  /// Returns a reference to the InitMapPtr which stores the initialization map.
456
0
  InitMapPtr &getInitMap() const {
457
0
    assert(Pointee);
458
0
    return *reinterpret_cast<InitMapPtr *>(Pointee->rawData() + Base);
459
0
  }
460
461
  /// The block the pointer is pointing to.
462
  Block *Pointee = nullptr;
463
  /// Start of the current subfield.
464
  unsigned Base = 0;
465
  /// Offset into the block.
466
  unsigned Offset = 0;
467
468
  /// Previous link in the pointer chain.
469
  Pointer *Prev = nullptr;
470
  /// Next link in the pointer chain.
471
  Pointer *Next = nullptr;
472
};
473
474
0
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Pointer &P) {
475
0
  P.print(OS);
476
0
  return OS;
477
0
}
478
479
} // namespace interp
480
} // namespace clang
481
482
#endif