clang 23.0.0git
DynamicAllocator.cpp
Go to the documentation of this file.
1//==-------- DynamicAllocator.cpp - Dynamic allocations ----------*- 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#include "DynamicAllocator.h"
10#include "InterpBlock.h"
11#include "InterpState.h"
12
13using namespace clang;
14using namespace clang::interp;
15
17
19 // Invoke destructors of all the blocks and as a last restort,
20 // reset all the pointers pointing to them to null pointees.
21 // This should never show up in diagnostics, but it's necessary
22 // for us to not cause use-after-free problems.
23 for (auto &Iter : AllocationSites) {
24 auto &AllocSite = Iter.second;
25 for (auto &Alloc : AllocSite.Allocations) {
26 Block *B = Alloc.block();
27 assert(!B->isDead());
28 assert(B->isInitialized());
29 B->invokeDtor();
30 B->removePointers();
31 }
32 }
33
34 AllocationSites.clear();
35}
36
38 size_t NumElements, unsigned EvalID,
39 Form AllocForm) {
40 // Create a new descriptor for an array of the specified size and
41 // element type.
42 const Descriptor *D = allocateDescriptor(
43 Source, T, Descriptor::InlineDescMD, NumElements, /*IsConst=*/false,
44 /*IsTemporary=*/false, /*IsMutable=*/false);
45
46 return allocate(D, EvalID, AllocForm);
47}
48
50 size_t NumElements, unsigned EvalID,
51 Form AllocForm) {
52 assert(ElementDesc->getMetadataSize() == 0);
53 // Create a new descriptor for an array of the specified size and
54 // element type.
55 // FIXME: Pass proper element type.
56 const Descriptor *D = allocateDescriptor(
57 ElementDesc->asExpr(), nullptr, ElementDesc, Descriptor::InlineDescMD,
58 NumElements,
59 /*IsConst=*/false, /*IsTemporary=*/false, /*IsMutable=*/false);
60 return allocate(D, EvalID, AllocForm);
61}
62
63Block *DynamicAllocator::allocate(const Descriptor *D, unsigned EvalID,
64 Form AllocForm) {
65 assert(D);
66 assert(D->asExpr());
67
68 // Garbage collection. Remove all dead allocations that don't have pointers to
69 // them anymore.
70 llvm::erase_if(DeadAllocations, [](Allocation &Alloc) -> bool {
71 return !Alloc.block()->hasPointers();
72 });
73
74 auto Memory =
75 std::make_unique<std::byte[]>(sizeof(Block) + D->getAllocSize());
76 auto *B = new (Memory.get()) Block(EvalID, D, /*isStatic=*/false);
77 B->invokeCtor();
78
79 assert(D->getMetadataSize() == sizeof(InlineDescriptor));
80 InlineDescriptor *ID = reinterpret_cast<InlineDescriptor *>(B->rawData());
81 ID->Desc = D;
82 ID->IsActive = true;
83 ID->Offset = sizeof(InlineDescriptor);
84 ID->IsBase = false;
85 ID->IsFieldMutable = false;
86 ID->IsConst = false;
87 ID->IsInitialized = false;
88 ID->IsVolatile = false;
89
90 if (D->isCompositeArray())
91 ID->LifeState = Lifetime::Started;
92 else
93 ID->LifeState =
95
96 if (auto It = AllocationSites.find(D->asExpr());
97 It != AllocationSites.end()) {
98 It->second.Allocations.emplace_back(std::move(Memory));
99 B->setDynAllocId(It->second.NumAllocs);
100 ++It->second.NumAllocs;
101 } else {
102 AllocationSites.insert(
103 {D->asExpr(), AllocationSite(std::move(Memory), AllocForm)});
104 B->setDynAllocId(0);
105 }
106 assert(B->isDynamic());
107 return B;
108}
109
111 const Block *BlockToDelete, InterpState &S) {
112 auto It = AllocationSites.find(Source);
113 if (It == AllocationSites.end())
114 return false;
115
116 auto &Site = It->second;
117 assert(!Site.empty());
118
119 // Find the Block to delete.
120 auto *AllocIt = llvm::find_if(Site.Allocations, [&](const Allocation &A) {
121 return BlockToDelete == A.block();
122 });
123
124 assert(AllocIt != Site.Allocations.end());
125
126 Block *B = AllocIt->block();
127 assert(B->isInitialized());
128 assert(!B->isDead());
129 B->invokeDtor();
130
131 // Almost all our dynamic allocations have a pointer pointing to them
132 // when we deallocate them, since otherwise we can't call delete() at all.
133 // This means that we would usually need to create DeadBlocks for all of them.
134 // To work around that, we instead mark them as dead without moving the data
135 // over to a DeadBlock and simply keep the block in a separate DeadAllocations
136 // list.
137 if (B->hasPointers()) {
138 B->AccessFlags |= Block::DeadFlag;
139 DeadAllocations.push_back(std::move(*AllocIt));
140 Site.Allocations.erase(AllocIt);
141
142 if (Site.size() == 0)
143 AllocationSites.erase(It);
144 return true;
145 }
146
147 // Get rid of the allocation altogether.
148 Site.Allocations.erase(AllocIt);
149 if (Site.empty())
150 AllocationSites.erase(It);
151
152 return true;
153}
This represents one expression.
Definition Expr.h:112
A memory block, either on the stack or in the heap.
Definition InterpBlock.h:44
void invokeDtor()
Invokes the Destructor.
bool isDead() const
Definition InterpBlock.h:85
bool isInitialized() const
Returns whether the data of this block has been initialized via invoking the Ctor func.
Definition InterpBlock.h:92
bool hasPointers() const
Checks if the block has any live pointers.
Definition InterpBlock.h:75
void removePointers()
Make all pointers that currently point to this block point to nullptr.
bool deallocate(const Expr *Source, const Block *BlockToDelete, InterpState &S)
Deallocate the given source+block combination.
Block * allocate(const Descriptor *D, unsigned EvalID, Form AllocForm)
Allocate ONE element of the given descriptor.
Interpreter context.
Definition InterpState.h:36
PrimType
Enumeration of the primitive types of the VM.
Definition PrimType.h:33
bool Alloc(InterpState &S, CodePtr OpPC, const Descriptor *Desc)
Definition Interp.h:3521
The JSON file list parser is used to communicate input to InstallAPI.
Describes a memory block created by an allocation site.
Definition Descriptor.h:121
const bool IsConst
Flag indicating if the block is mutable.
Definition Descriptor.h:160
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Definition Descriptor.h:246
bool isCompositeArray() const
Checks if the descriptor is of an array of composites.
Definition Descriptor.h:260
unsigned getMetadataSize() const
Returns the size of the metadata.
Definition Descriptor.h:250
static constexpr MetadataSize InlineDescMD
Definition Descriptor.h:143
const Expr * asExpr() const
Definition Descriptor.h:211
Inline descriptor embedded in structures and arrays.
Definition Descriptor.h:66