-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[clang][Interp] Implement dynamic memory allocation handling #70306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
//==-------- DynamicAllocator.cpp - Dynamic allocations ----------*- C++ -*-==// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "DynamicAllocator.h" | ||
#include "InterpBlock.h" | ||
#include "InterpState.h" | ||
|
||
using namespace clang; | ||
using namespace clang::interp; | ||
|
||
DynamicAllocator::~DynamicAllocator() { cleanup(); } | ||
|
||
void DynamicAllocator::cleanup() { | ||
// Invoke destructors of all the blocks and as a last restort, | ||
// reset all the pointers pointing to them to null pointees. | ||
// This should never show up in diagnostics, but it's necessary | ||
// for us to not cause use-after-free problems. | ||
for (auto &Iter : AllocationSites) { | ||
auto &AllocSite = Iter.second; | ||
for (auto &Alloc : AllocSite.Allocations) { | ||
Block *B = reinterpret_cast<Block *>(Alloc.Memory.get()); | ||
B->invokeDtor(); | ||
if (B->hasPointers()) { | ||
while (B->Pointers) { | ||
Pointer *Next = B->Pointers->Next; | ||
B->Pointers->PointeeStorage.BS.Pointee = nullptr; | ||
B->Pointers = Next; | ||
} | ||
B->Pointers = nullptr; | ||
} | ||
} | ||
} | ||
|
||
AllocationSites.clear(); | ||
} | ||
|
||
Block *DynamicAllocator::allocate(const Expr *Source, PrimType T, | ||
size_t NumElements, unsigned EvalID) { | ||
// Create a new descriptor for an array of the specified size and | ||
// element type. | ||
const Descriptor *D = allocateDescriptor( | ||
Source, T, Descriptor::InlineDescMD, NumElements, /*IsConst=*/false, | ||
/*IsTemporary=*/false, /*IsMutable=*/false); | ||
|
||
return allocate(D, EvalID); | ||
} | ||
|
||
Block *DynamicAllocator::allocate(const Descriptor *ElementDesc, | ||
size_t NumElements, unsigned EvalID) { | ||
// Create a new descriptor for an array of the specified size and | ||
// element type. | ||
const Descriptor *D = allocateDescriptor( | ||
ElementDesc->asExpr(), ElementDesc, Descriptor::InlineDescMD, NumElements, | ||
/*IsConst=*/false, /*IsTemporary=*/false, /*IsMutable=*/false); | ||
return allocate(D, EvalID); | ||
} | ||
|
||
Block *DynamicAllocator::allocate(const Descriptor *D, unsigned EvalID) { | ||
assert(D->asExpr()); | ||
|
||
auto Memory = | ||
std::make_unique<std::byte[]>(sizeof(Block) + D->getAllocSize()); | ||
auto *B = new (Memory.get()) Block(EvalID, D, /*isStatic=*/false); | ||
B->invokeCtor(); | ||
|
||
InlineDescriptor *ID = reinterpret_cast<InlineDescriptor *>(B->rawData()); | ||
ID->Desc = D; | ||
ID->IsActive = true; | ||
ID->Offset = sizeof(InlineDescriptor); | ||
ID->IsBase = false; | ||
ID->IsFieldMutable = false; | ||
ID->IsConst = false; | ||
ID->IsInitialized = false; | ||
assert(ID->Desc); | ||
|
||
B->IsDynamic = true; | ||
|
||
if (auto It = AllocationSites.find(D->asExpr()); It != AllocationSites.end()) | ||
It->second.Allocations.emplace_back(std::move(Memory)); | ||
else | ||
AllocationSites.insert( | ||
{D->asExpr(), AllocationSite(std::move(Memory), D->isArray())}); | ||
return B; | ||
} | ||
|
||
bool DynamicAllocator::deallocate(const Expr *Source, | ||
const Block *BlockToDelete, InterpState &S) { | ||
auto It = AllocationSites.find(Source); | ||
if (It == AllocationSites.end()) | ||
return false; | ||
|
||
auto &Site = It->second; | ||
assert(Site.size() > 0); | ||
|
||
// Find the Block to delete. | ||
auto AllocIt = llvm::find_if(Site.Allocations, [&](const Allocation &A) { | ||
const Block *B = reinterpret_cast<const Block *>(A.Memory.get()); | ||
return BlockToDelete == B; | ||
}); | ||
|
||
assert(AllocIt != Site.Allocations.end()); | ||
|
||
Block *B = reinterpret_cast<Block *>(AllocIt->Memory.get()); | ||
B->invokeDtor(); | ||
|
||
S.deallocate(B); | ||
Site.Allocations.erase(AllocIt); | ||
|
||
if (Site.size() == 0) | ||
AaronBallman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
AllocationSites.erase(It); | ||
|
||
return true; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
//==--------- DynamicAllocator.h - Dynamic allocations ------------*- C++ -*-=// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_AST_INTERP_DYNAMIC_ALLOCATOR_H | ||
#define LLVM_CLANG_AST_INTERP_DYNAMIC_ALLOCATOR_H | ||
|
||
#include "Descriptor.h" | ||
#include "InterpBlock.h" | ||
#include "llvm/ADT/SmallVector.h" | ||
#include "llvm/ADT/iterator_range.h" | ||
#include "llvm/Support/Allocator.h" | ||
|
||
namespace clang { | ||
class Expr; | ||
namespace interp { | ||
class Block; | ||
class InterpState; | ||
|
||
/// Manages dynamic memory allocations done during bytecode interpretation. | ||
/// | ||
/// We manage allocations as a map from their new-expression to a list | ||
/// of allocations. This is called an AllocationSite. For each site, we | ||
/// record whether it was allocated using new or new[], the | ||
/// IsArrayAllocation flag. | ||
/// | ||
/// For all array allocations, we need to allocat new Descriptor instances, | ||
/// so the DynamicAllocator has a llvm::BumpPtrAllocator similar to Program. | ||
class DynamicAllocator final { | ||
struct Allocation { | ||
std::unique_ptr<std::byte[]> Memory; | ||
Allocation(std::unique_ptr<std::byte[]> Memory) | ||
: Memory(std::move(Memory)) {} | ||
}; | ||
|
||
struct AllocationSite { | ||
llvm::SmallVector<Allocation> Allocations; | ||
bool IsArrayAllocation = false; | ||
|
||
AllocationSite(std::unique_ptr<std::byte[]> Memory, bool Array) | ||
: IsArrayAllocation(Array) { | ||
Allocations.push_back({std::move(Memory)}); | ||
} | ||
|
||
size_t size() const { return Allocations.size(); } | ||
}; | ||
|
||
public: | ||
DynamicAllocator() = default; | ||
~DynamicAllocator(); | ||
|
||
void cleanup(); | ||
|
||
unsigned getNumAllocations() const { return AllocationSites.size(); } | ||
|
||
/// Allocate ONE element of the given descriptor. | ||
Block *allocate(const Descriptor *D, unsigned EvalID); | ||
/// Allocate \p NumElements primitive elements of the given type. | ||
Block *allocate(const Expr *Source, PrimType T, size_t NumElements, | ||
unsigned EvalID); | ||
/// Allocate \p NumElements elements of the given descriptor. | ||
Block *allocate(const Descriptor *D, size_t NumElements, unsigned EvalID); | ||
|
||
/// Deallocate the given source+block combination. | ||
/// Returns \c true if anything has been deallocatd, \c false otherwise. | ||
bool deallocate(const Expr *Source, const Block *BlockToDelete, | ||
InterpState &S); | ||
|
||
/// Checks whether the allocation done at the given source is an array | ||
/// allocation. | ||
bool isArrayAllocation(const Expr *Source) const { | ||
if (auto It = AllocationSites.find(Source); It != AllocationSites.end()) | ||
return It->second.IsArrayAllocation; | ||
return false; | ||
} | ||
|
||
// FIXME: Public because I'm not sure how to expose an iterator to it. | ||
llvm::DenseMap<const Expr *, AllocationSite> AllocationSites; | ||
|
||
private: | ||
using PoolAllocTy = llvm::BumpPtrAllocatorImpl<llvm::MallocAllocator>; | ||
PoolAllocTy DescAllocator; | ||
|
||
/// Allocates a new descriptor. | ||
template <typename... Ts> Descriptor *allocateDescriptor(Ts &&...Args) { | ||
return new (DescAllocator) Descriptor(std::forward<Ts>(Args)...); | ||
} | ||
}; | ||
|
||
} // namespace interp | ||
} // namespace clang | ||
#endif |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW: https://wg21.link/p2747r2 was just adopted for C++26 (doesn't need to be handled in this PR, just a comment on "any other form")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, I saw. I'll wait for @cor3ntin to come up with the test cases though :)