clang 24.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
12using namespace clang;
13using namespace clang::interp;
14
16
18 // Invoke destructors of all the blocks and as a last restort,
19 // reset all the pointers pointing to them to null pointees.
20 // This should never show up in diagnostics, but it's necessary
21 // for us to not cause use-after-free problems.
22 for (auto &Iter : AllocationSites) {
23 auto &AllocSite = Iter.second;
24 for (auto &Alloc : AllocSite.Allocations) {
25 Block *B = Alloc.block();
26 assert(!B->isDead());
27 assert(B->isInitialized());
28 B->invokeDtor();
29 B->removePointers();
30 }
31 }
32
33 AllocationSites.clear();
34}
35
37 size_t NumElements, unsigned EvalID,
38 Form AllocForm) {
39 // Create a new descriptor for an array of the specified size and
40 // element type.
41 const Descriptor *D =
42 allocateDescriptor(Source, nullptr, T, NumElements, /*IsConst=*/false,
43 /*IsTemporary=*/false, /*IsMutable=*/false,
44 /*IsVolatile=*/false);
45
46 return allocate(D, EvalID, AllocForm);
47}
48
50 size_t NumElements, unsigned EvalID,
51 Form AllocForm) {
52 // Create a new descriptor for an array of the specified size and
53 // element type.
54 // FIXME: Pass proper element type.
55 const Descriptor *D = allocateDescriptor(
56 ElementDesc->asExpr(), nullptr, ElementDesc, NumElements,
57 /*IsConst=*/false, /*IsTemporary=*/false, /*IsMutable=*/false);
58 return allocate(D, EvalID, AllocForm);
59}
60
61Block *DynamicAllocator::allocate(const Descriptor *D, unsigned EvalID,
62 Form AllocForm) {
63 assert(D);
64 assert(D->asExpr());
65
66 // Garbage collection. Remove all dead allocations that don't have pointers to
67 // them anymore.
68 llvm::erase_if(DeadAllocations, [](Allocation &Alloc) -> bool {
69 return !Alloc.block()->hasPointers();
70 });
71
72 auto Memory = std::make_unique<std::byte[]>(
73 sizeof(Block) + D->getAllocSize() + Block::InlineDescMD);
74 auto *B = new (Memory.get()) Block(EvalID, D, Block::InlineDescMD,
75 /*isStatic=*/false);
76 B->invokeCtorNoMemset();
77 InlineDescriptor *ID = reinterpret_cast<InlineDescriptor *>(B->rawData());
78 ID->Desc = D;
79 ID->IsActive = true;
80 ID->Offset = sizeof(InlineDescriptor);
81 ID->IsBase = false;
82 ID->IsFieldMutable = false;
83 ID->IsConst = false;
84 ID->IsInitialized = false;
85 ID->IsVolatile = false;
86
87 if (D->isCompositeArray())
88 ID->LifeState = Lifetime::Started;
89 else
90 ID->LifeState =
92
93 if (auto It = AllocationSites.find(D->asExpr());
94 It != AllocationSites.end()) {
95 It->second.Allocations.emplace_back(std::move(Memory));
96 B->setDynAllocId(It->second.NumAllocs);
97 ++It->second.NumAllocs;
98 } else {
99 AllocationSites.insert(
100 {D->asExpr(), AllocationSite(std::move(Memory), AllocForm)});
101 B->setDynAllocId(0);
102 }
103 assert(B->isDynamic());
104 return B;
105}
106
108 const Block *BlockToDelete) {
109 auto It = AllocationSites.find(Source);
110 if (It == AllocationSites.end())
111 return false;
112
113 auto &Site = It->second;
114 assert(!Site.empty());
115
116 // Find the Block to delete.
117 auto *AllocIt = llvm::find_if(Site.Allocations, [&](const Allocation &A) {
118 return BlockToDelete == A.block();
119 });
120
121 // The allocation site it fine, but this block doesn't belong to it. Must've
122 // already been deleted.
123 if (AllocIt == Site.Allocations.end())
124 return false;
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:113
A memory block, either on the stack or in the heap.
Definition InterpBlock.h:43
void invokeDtor()
Invokes the Destructor.
static constexpr uint8_t InlineDescMD
Definition InterpBlock.h:51
bool isDead() const
Definition InterpBlock.h:89
bool isInitialized() const
Returns whether the data of this block has been initialized via invoking the Ctor func.
Definition InterpBlock.h:98
bool hasPointers() const
Checks if the block has any live pointers.
Definition InterpBlock.h:79
void removePointers()
Make all pointers that currently point to this block point to nullptr.
Block * allocate(const Descriptor *D, unsigned EvalID, Form AllocForm)
Allocate ONE element of the given descriptor.
bool deallocate(const Expr *Source, const Block *BlockToDelete)
Deallocate the given source+block combination.
PrimType
Enumeration of the primitive types of the VM.
Definition PrimType.h:34
bool Alloc(InterpState &S, CodePtr OpPC, const Descriptor *Desc)
Definition Interp.h:3859
Top level wrappers for InstallAPI frontend operations.
const FunctionProtoType * T
Describes a memory block created by an allocation site.
Definition Descriptor.h:122
const bool IsConst
Flag indicating if the block is mutable.
Definition Descriptor.h:154
unsigned getAllocSize() const
Returns the allocated size, including metadata.
Definition Descriptor.h:237
bool isCompositeArray() const
Checks if the descriptor is of an array of composites.
Definition Descriptor.h:253
const Expr * asExpr() const
Definition Descriptor.h:202
Inline descriptor embedded in structures and arrays.
Definition Descriptor.h:67