2019-06-14 21:41:21 +02:00
|
|
|
//===--- JITLinkMemoryManager.cpp - JITLinkMemoryManager implementation ---===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
|
|
|
|
#include "llvm/Support/Process.h"
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace jitlink {
|
|
|
|
|
|
|
|
JITLinkMemoryManager::~JITLinkMemoryManager() = default;
|
|
|
|
JITLinkMemoryManager::Allocation::~Allocation() = default;
|
|
|
|
|
|
|
|
Expected<std::unique_ptr<JITLinkMemoryManager::Allocation>>
|
2020-12-14 03:38:00 +01:00
|
|
|
InProcessMemoryManager::allocate(const JITLinkDylib *JD,
|
|
|
|
const SegmentsRequestMap &Request) {
|
2019-06-14 21:41:21 +02:00
|
|
|
|
|
|
|
using AllocationMap = DenseMap<unsigned, sys::MemoryBlock>;
|
|
|
|
|
|
|
|
// Local class for allocation.
|
|
|
|
class IPMMAlloc : public Allocation {
|
|
|
|
public:
|
|
|
|
IPMMAlloc(AllocationMap SegBlocks) : SegBlocks(std::move(SegBlocks)) {}
|
|
|
|
MutableArrayRef<char> getWorkingMemory(ProtectionFlags Seg) override {
|
|
|
|
assert(SegBlocks.count(Seg) && "No allocation for segment");
|
|
|
|
return {static_cast<char *>(SegBlocks[Seg].base()),
|
|
|
|
SegBlocks[Seg].allocatedSize()};
|
|
|
|
}
|
|
|
|
JITTargetAddress getTargetMemory(ProtectionFlags Seg) override {
|
|
|
|
assert(SegBlocks.count(Seg) && "No allocation for segment");
|
2020-03-03 22:51:05 +01:00
|
|
|
return pointerToJITTargetAddress(SegBlocks[Seg].base());
|
2019-06-14 21:41:21 +02:00
|
|
|
}
|
|
|
|
void finalizeAsync(FinalizeContinuation OnFinalize) override {
|
|
|
|
OnFinalize(applyProtections());
|
|
|
|
}
|
|
|
|
Error deallocate() override {
|
2019-10-15 23:06:57 +02:00
|
|
|
if (SegBlocks.empty())
|
|
|
|
return Error::success();
|
|
|
|
void *SlabStart = SegBlocks.begin()->second.base();
|
|
|
|
char *SlabEnd = (char *)SlabStart;
|
|
|
|
for (auto &KV : SegBlocks) {
|
|
|
|
SlabStart = std::min(SlabStart, KV.second.base());
|
|
|
|
SlabEnd = std::max(SlabEnd, (char *)(KV.second.base()) +
|
|
|
|
KV.second.allocatedSize());
|
|
|
|
}
|
|
|
|
size_t SlabSize = SlabEnd - (char *)SlabStart;
|
|
|
|
assert((SlabSize % sys::Process::getPageSizeEstimate()) == 0 &&
|
|
|
|
"Slab size is not a multiple of page size");
|
|
|
|
sys::MemoryBlock Slab(SlabStart, SlabSize);
|
|
|
|
if (auto EC = sys::Memory::releaseMappedMemory(Slab))
|
|
|
|
return errorCodeToError(EC);
|
2019-06-14 21:41:21 +02:00
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Error applyProtections() {
|
|
|
|
for (auto &KV : SegBlocks) {
|
|
|
|
auto &Prot = KV.first;
|
|
|
|
auto &Block = KV.second;
|
|
|
|
if (auto EC = sys::Memory::protectMappedMemory(Block, Prot))
|
|
|
|
return errorCodeToError(EC);
|
|
|
|
if (Prot & sys::Memory::MF_EXEC)
|
|
|
|
sys::Memory::InvalidateInstructionCache(Block.base(),
|
|
|
|
Block.allocatedSize());
|
|
|
|
}
|
|
|
|
return Error::success();
|
|
|
|
}
|
|
|
|
|
|
|
|
AllocationMap SegBlocks;
|
|
|
|
};
|
|
|
|
|
2019-10-04 05:55:26 +02:00
|
|
|
if (!isPowerOf2_64((uint64_t)sys::Process::getPageSizeEstimate()))
|
|
|
|
return make_error<StringError>("Page size is not a power of 2",
|
|
|
|
inconvertibleErrorCode());
|
|
|
|
|
2019-06-14 21:41:21 +02:00
|
|
|
AllocationMap Blocks;
|
|
|
|
const sys::Memory::ProtectionFlags ReadWrite =
|
|
|
|
static_cast<sys::Memory::ProtectionFlags>(sys::Memory::MF_READ |
|
|
|
|
sys::Memory::MF_WRITE);
|
|
|
|
|
2019-10-15 23:06:57 +02:00
|
|
|
// Compute the total number of pages to allocate.
|
|
|
|
size_t TotalSize = 0;
|
2019-06-14 21:41:21 +02:00
|
|
|
for (auto &KV : Request) {
|
2019-10-15 23:06:57 +02:00
|
|
|
const auto &Seg = KV.second;
|
2019-06-14 21:41:21 +02:00
|
|
|
|
2019-10-04 05:55:26 +02:00
|
|
|
if (Seg.getAlignment() > sys::Process::getPageSizeEstimate())
|
2019-06-14 21:41:21 +02:00
|
|
|
return make_error<StringError>("Cannot request higher than page "
|
|
|
|
"alignment",
|
|
|
|
inconvertibleErrorCode());
|
|
|
|
|
2019-10-15 23:06:57 +02:00
|
|
|
TotalSize = alignTo(TotalSize, sys::Process::getPageSizeEstimate());
|
|
|
|
TotalSize += Seg.getContentSize();
|
|
|
|
TotalSize += Seg.getZeroFillSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate one slab to cover all the segments.
|
|
|
|
std::error_code EC;
|
|
|
|
auto SlabRemaining =
|
|
|
|
sys::Memory::allocateMappedMemory(TotalSize, nullptr, ReadWrite, EC);
|
|
|
|
|
|
|
|
if (EC)
|
|
|
|
return errorCodeToError(EC);
|
|
|
|
|
|
|
|
// Allocate segment memory from the slab.
|
|
|
|
for (auto &KV : Request) {
|
|
|
|
|
|
|
|
const auto &Seg = KV.second;
|
2019-06-14 21:41:21 +02:00
|
|
|
|
2019-10-15 23:06:57 +02:00
|
|
|
uint64_t SegmentSize = alignTo(Seg.getContentSize() + Seg.getZeroFillSize(),
|
|
|
|
sys::Process::getPageSizeEstimate());
|
2021-03-08 05:55:13 +01:00
|
|
|
assert(SlabRemaining.allocatedSize() >= SegmentSize &&
|
|
|
|
"Mapping exceeds allocation");
|
2019-06-14 21:41:21 +02:00
|
|
|
|
2019-10-15 23:06:57 +02:00
|
|
|
sys::MemoryBlock SegMem(SlabRemaining.base(), SegmentSize);
|
|
|
|
SlabRemaining = sys::MemoryBlock((char *)SlabRemaining.base() + SegmentSize,
|
2021-02-12 14:03:41 +01:00
|
|
|
SlabRemaining.allocatedSize() - SegmentSize);
|
2019-06-14 21:41:21 +02:00
|
|
|
|
|
|
|
// Zero out the zero-fill memory.
|
2019-10-04 05:55:26 +02:00
|
|
|
memset(static_cast<char *>(SegMem.base()) + Seg.getContentSize(), 0,
|
2019-06-14 21:41:21 +02:00
|
|
|
Seg.getZeroFillSize());
|
|
|
|
|
|
|
|
// Record the block for this segment.
|
|
|
|
Blocks[KV.first] = std::move(SegMem);
|
|
|
|
}
|
2021-02-12 14:03:41 +01:00
|
|
|
|
2019-06-14 21:41:21 +02:00
|
|
|
return std::unique_ptr<InProcessMemoryManager::Allocation>(
|
|
|
|
new IPMMAlloc(std::move(Blocks)));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace jitlink
|
|
|
|
} // end namespace llvm
|