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