-
Notifications
You must be signed in to change notification settings - Fork 12.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reapply "[clang][Interp] Implement dynamic memory allocation handling (…
- Loading branch information
Showing
19 changed files
with
1,213 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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 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 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); | ||
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; | ||
|
||
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) | ||
AllocationSites.erase(It); | ||
|
||
return true; | ||
} |
This file contains 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,102 @@ | ||
//==--------- 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 allocate 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; | ||
} | ||
|
||
/// Allocation site iterator. | ||
using const_virtual_iter = | ||
llvm::DenseMap<const Expr *, AllocationSite>::const_iterator; | ||
llvm::iterator_range<const_virtual_iter> allocation_sites() const { | ||
return llvm::make_range(AllocationSites.begin(), AllocationSites.end()); | ||
} | ||
|
||
private: | ||
llvm::DenseMap<const Expr *, AllocationSite> AllocationSites; | ||
|
||
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.