2012-11-28 03:00:17 +08:00
|
|
|
//===- SectionMemoryManager.cpp - Memory manager for MCJIT/RtDyld *- C++ -*-==//
|
2012-10-05 04:29:44 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2012-10-05 04:29:44 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-11-28 03:00:17 +08:00
|
|
|
// This file implements the section-based memory manager used by the MCJIT
|
|
|
|
// execution engine and RuntimeDyld
|
2012-10-05 04:29:44 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-11-28 03:42:02 +08:00
|
|
|
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Config/config.h"
|
2012-11-01 01:41:51 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2015-12-16 19:13:23 +08:00
|
|
|
#include "llvm/Support/Process.h"
|
2012-10-05 04:29:44 +08:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size,
|
2013-10-02 08:59:25 +08:00
|
|
|
unsigned Alignment,
|
|
|
|
unsigned SectionID,
|
|
|
|
StringRef SectionName,
|
|
|
|
bool IsReadOnly) {
|
2012-11-28 03:00:17 +08:00
|
|
|
if (IsReadOnly)
|
2017-11-09 14:31:33 +08:00
|
|
|
return allocateSection(SectionMemoryManager::AllocationPurpose::ROData,
|
|
|
|
Size, Alignment);
|
|
|
|
return allocateSection(SectionMemoryManager::AllocationPurpose::RWData, Size,
|
|
|
|
Alignment);
|
2012-10-05 04:29:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
|
2012-11-28 03:00:17 +08:00
|
|
|
unsigned Alignment,
|
2013-10-02 08:59:25 +08:00
|
|
|
unsigned SectionID,
|
|
|
|
StringRef SectionName) {
|
2017-11-09 14:31:33 +08:00
|
|
|
return allocateSection(SectionMemoryManager::AllocationPurpose::Code, Size,
|
|
|
|
Alignment);
|
2012-11-28 03:00:17 +08:00
|
|
|
}
|
|
|
|
|
2017-11-09 14:31:33 +08:00
|
|
|
uint8_t *SectionMemoryManager::allocateSection(
|
|
|
|
SectionMemoryManager::AllocationPurpose Purpose, uintptr_t Size,
|
|
|
|
unsigned Alignment) {
|
2012-10-05 04:29:44 +08:00
|
|
|
if (!Alignment)
|
|
|
|
Alignment = 16;
|
2012-11-28 03:00:17 +08:00
|
|
|
|
|
|
|
assert(!(Alignment & (Alignment - 1)) && "Alignment must be a power of two.");
|
|
|
|
|
2017-11-09 14:31:33 +08:00
|
|
|
uintptr_t RequiredSize = Alignment * ((Size + Alignment - 1) / Alignment + 1);
|
2012-10-05 04:29:44 +08:00
|
|
|
uintptr_t Addr = 0;
|
2012-11-28 03:00:17 +08:00
|
|
|
|
2017-11-09 14:31:33 +08:00
|
|
|
MemoryGroup &MemGroup = [&]() -> MemoryGroup & {
|
|
|
|
switch (Purpose) {
|
|
|
|
case AllocationPurpose::Code:
|
|
|
|
return CodeMem;
|
|
|
|
case AllocationPurpose::ROData:
|
|
|
|
return RODataMem;
|
|
|
|
case AllocationPurpose::RWData:
|
|
|
|
return RWDataMem;
|
|
|
|
}
|
2017-11-09 22:56:17 +08:00
|
|
|
llvm_unreachable("Unknown SectionMemoryManager::AllocationPurpose");
|
2017-11-09 14:31:33 +08:00
|
|
|
}();
|
|
|
|
|
2012-11-28 03:00:17 +08:00
|
|
|
// Look in the list of free memory regions and use a block there if one
|
2012-10-05 04:29:44 +08:00
|
|
|
// is available.
|
2015-12-16 19:13:23 +08:00
|
|
|
for (FreeMemBlock &FreeMB : MemGroup.FreeMem) {
|
2019-05-21 04:53:05 +08:00
|
|
|
if (FreeMB.Free.allocatedSize() >= RequiredSize) {
|
2015-12-16 19:13:23 +08:00
|
|
|
Addr = (uintptr_t)FreeMB.Free.base();
|
2019-05-21 04:53:05 +08:00
|
|
|
uintptr_t EndOfBlock = Addr + FreeMB.Free.allocatedSize();
|
2012-10-05 04:29:44 +08:00
|
|
|
// Align the address.
|
|
|
|
Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
|
2015-12-16 19:13:23 +08:00
|
|
|
|
|
|
|
if (FreeMB.PendingPrefixIndex == (unsigned)-1) {
|
|
|
|
// The part of the block we're giving out to the user is now pending
|
|
|
|
MemGroup.PendingMem.push_back(sys::MemoryBlock((void *)Addr, Size));
|
|
|
|
|
|
|
|
// Remember this pending block, such that future allocations can just
|
|
|
|
// modify it rather than creating a new one
|
|
|
|
FreeMB.PendingPrefixIndex = MemGroup.PendingMem.size() - 1;
|
|
|
|
} else {
|
2017-11-09 14:31:33 +08:00
|
|
|
sys::MemoryBlock &PendingMB =
|
|
|
|
MemGroup.PendingMem[FreeMB.PendingPrefixIndex];
|
|
|
|
PendingMB = sys::MemoryBlock(PendingMB.base(),
|
|
|
|
Addr + Size - (uintptr_t)PendingMB.base());
|
2015-12-16 19:13:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remember how much free space is now left in this block
|
2017-11-09 14:31:33 +08:00
|
|
|
FreeMB.Free =
|
|
|
|
sys::MemoryBlock((void *)(Addr + Size), EndOfBlock - Addr - Size);
|
|
|
|
return (uint8_t *)Addr;
|
2012-10-05 04:29:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// No pre-allocated free block was large enough. Allocate a new memory region.
|
2012-11-28 03:00:17 +08:00
|
|
|
// Note that all sections get allocated as read-write. The permissions will
|
|
|
|
// be updated later based on memory group.
|
|
|
|
//
|
|
|
|
// FIXME: It would be useful to define a default allocation size (or add
|
|
|
|
// it as a constructor parameter) to minimize the number of allocations.
|
2012-11-28 03:42:02 +08:00
|
|
|
//
|
2012-11-28 03:00:17 +08:00
|
|
|
// FIXME: Initialize the Near member for each memory group to avoid
|
|
|
|
// interleaving.
|
2014-06-13 05:46:39 +08:00
|
|
|
std::error_code ec;
|
2017-11-09 14:31:33 +08:00
|
|
|
sys::MemoryBlock MB = MMapper.allocateMappedMemory(
|
|
|
|
Purpose, RequiredSize, &MemGroup.Near,
|
|
|
|
sys::Memory::MF_READ | sys::Memory::MF_WRITE, ec);
|
2012-11-28 03:00:17 +08:00
|
|
|
if (ec) {
|
2014-01-25 01:20:08 +08:00
|
|
|
// FIXME: Add error propagation to the interface.
|
2014-04-24 14:44:33 +08:00
|
|
|
return nullptr;
|
2012-11-28 03:00:17 +08:00
|
|
|
}
|
2012-10-05 04:29:44 +08:00
|
|
|
|
2012-11-28 03:00:17 +08:00
|
|
|
// Save this address as the basis for our next request
|
|
|
|
MemGroup.Near = MB;
|
|
|
|
|
2015-12-16 19:13:23 +08:00
|
|
|
// Remember that we allocated this memory
|
|
|
|
MemGroup.AllocatedMem.push_back(MB);
|
2012-10-05 04:29:44 +08:00
|
|
|
Addr = (uintptr_t)MB.base();
|
2019-05-21 04:53:05 +08:00
|
|
|
uintptr_t EndOfBlock = Addr + MB.allocatedSize();
|
2012-11-28 03:00:17 +08:00
|
|
|
|
2012-10-05 04:29:44 +08:00
|
|
|
// Align the address.
|
|
|
|
Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
|
2012-11-28 03:00:17 +08:00
|
|
|
|
2015-12-16 19:13:23 +08:00
|
|
|
// The part of the block we're giving out to the user is now pending
|
|
|
|
MemGroup.PendingMem.push_back(sys::MemoryBlock((void *)Addr, Size));
|
|
|
|
|
2012-11-28 03:00:17 +08:00
|
|
|
// The allocateMappedMemory may allocate much more memory than we need. In
|
|
|
|
// this case, we store the unused memory as a free memory block.
|
2017-11-09 14:31:33 +08:00
|
|
|
unsigned FreeSize = EndOfBlock - Addr - Size;
|
2015-12-16 19:13:23 +08:00
|
|
|
if (FreeSize > 16) {
|
|
|
|
FreeMemBlock FreeMB;
|
2017-11-09 14:31:33 +08:00
|
|
|
FreeMB.Free = sys::MemoryBlock((void *)(Addr + Size), FreeSize);
|
2015-12-16 19:13:23 +08:00
|
|
|
FreeMB.PendingPrefixIndex = (unsigned)-1;
|
|
|
|
MemGroup.FreeMem.push_back(FreeMB);
|
|
|
|
}
|
2012-10-05 04:29:44 +08:00
|
|
|
|
|
|
|
// Return aligned address
|
2017-11-09 14:31:33 +08:00
|
|
|
return (uint8_t *)Addr;
|
2012-10-05 04:29:44 +08:00
|
|
|
}
|
|
|
|
|
2017-11-09 14:31:33 +08:00
|
|
|
bool SectionMemoryManager::finalizeMemory(std::string *ErrMsg) {
|
2012-11-28 03:00:17 +08:00
|
|
|
// FIXME: Should in-progress permissions be reverted if an error occurs?
|
2014-06-13 05:46:39 +08:00
|
|
|
std::error_code ec;
|
2012-11-28 03:00:17 +08:00
|
|
|
|
|
|
|
// Make code memory executable.
|
|
|
|
ec = applyMemoryGroupPermissions(CodeMem,
|
|
|
|
sys::Memory::MF_READ | sys::Memory::MF_EXEC);
|
|
|
|
if (ec) {
|
|
|
|
if (ErrMsg) {
|
|
|
|
*ErrMsg = ec.message();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make read-only data memory read-only.
|
|
|
|
ec = applyMemoryGroupPermissions(RODataMem,
|
|
|
|
sys::Memory::MF_READ | sys::Memory::MF_EXEC);
|
|
|
|
if (ec) {
|
|
|
|
if (ErrMsg) {
|
|
|
|
*ErrMsg = ec.message();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read-write data memory already has the correct permissions
|
|
|
|
|
2013-04-25 06:39:12 +08:00
|
|
|
// Some platforms with separate data cache and instruction cache require
|
|
|
|
// explicit cache flush, otherwise JIT code manipulations (like resolved
|
|
|
|
// relocations) will get to the data cache but not to the instruction cache.
|
|
|
|
invalidateInstructionCache();
|
|
|
|
|
2015-12-16 19:13:23 +08:00
|
|
|
return false;
|
|
|
|
}
|
2015-10-01 10:45:07 +08:00
|
|
|
|
2015-12-16 19:13:23 +08:00
|
|
|
static sys::MemoryBlock trimBlockToPageSize(sys::MemoryBlock M) {
|
2019-05-08 10:11:07 +08:00
|
|
|
static const size_t PageSize = sys::Process::getPageSizeEstimate();
|
2015-10-01 10:45:07 +08:00
|
|
|
|
2015-12-16 19:13:23 +08:00
|
|
|
size_t StartOverlap =
|
|
|
|
(PageSize - ((uintptr_t)M.base() % PageSize)) % PageSize;
|
|
|
|
|
2019-05-21 04:53:05 +08:00
|
|
|
size_t TrimmedSize = M.allocatedSize();
|
2015-12-16 19:13:23 +08:00
|
|
|
TrimmedSize -= StartOverlap;
|
|
|
|
TrimmedSize -= TrimmedSize % PageSize;
|
|
|
|
|
2017-11-09 14:31:33 +08:00
|
|
|
sys::MemoryBlock Trimmed((void *)((uintptr_t)M.base() + StartOverlap),
|
|
|
|
TrimmedSize);
|
2015-12-16 19:13:23 +08:00
|
|
|
|
|
|
|
assert(((uintptr_t)Trimmed.base() % PageSize) == 0);
|
2019-05-21 04:53:05 +08:00
|
|
|
assert((Trimmed.allocatedSize() % PageSize) == 0);
|
|
|
|
assert(M.base() <= Trimmed.base() &&
|
|
|
|
Trimmed.allocatedSize() <= M.allocatedSize());
|
2015-12-16 19:13:23 +08:00
|
|
|
|
|
|
|
return Trimmed;
|
2012-11-28 03:00:17 +08:00
|
|
|
}
|
|
|
|
|
2014-06-13 05:46:39 +08:00
|
|
|
std::error_code
|
|
|
|
SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
|
|
|
|
unsigned Permissions) {
|
2015-10-01 10:45:07 +08:00
|
|
|
for (sys::MemoryBlock &MB : MemGroup.PendingMem)
|
2017-11-09 14:31:33 +08:00
|
|
|
if (std::error_code EC = MMapper.protectMappedMemory(MB, Permissions))
|
2015-08-31 21:39:14 +08:00
|
|
|
return EC;
|
2012-11-28 03:00:17 +08:00
|
|
|
|
2015-12-16 19:13:23 +08:00
|
|
|
MemGroup.PendingMem.clear();
|
|
|
|
|
|
|
|
// Now go through free blocks and trim any of them that don't span the entire
|
|
|
|
// page because one of the pending blocks may have overlapped it.
|
|
|
|
for (FreeMemBlock &FreeMB : MemGroup.FreeMem) {
|
|
|
|
FreeMB.Free = trimBlockToPageSize(FreeMB.Free);
|
|
|
|
// We cleared the PendingMem list, so all these pointers are now invalid
|
|
|
|
FreeMB.PendingPrefixIndex = (unsigned)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all blocks which are now empty
|
2019-05-21 04:53:05 +08:00
|
|
|
MemGroup.FreeMem.erase(remove_if(MemGroup.FreeMem,
|
|
|
|
[](FreeMemBlock &FreeMB) {
|
|
|
|
return FreeMB.Free.allocatedSize() == 0;
|
|
|
|
}),
|
|
|
|
MemGroup.FreeMem.end());
|
2015-12-16 19:13:23 +08:00
|
|
|
|
2014-06-13 05:46:39 +08:00
|
|
|
return std::error_code();
|
2012-11-28 03:00:17 +08:00
|
|
|
}
|
|
|
|
|
2012-10-05 04:29:44 +08:00
|
|
|
void SectionMemoryManager::invalidateInstructionCache() {
|
2015-10-01 10:45:07 +08:00
|
|
|
for (sys::MemoryBlock &Block : CodeMem.PendingMem)
|
2019-05-21 04:53:05 +08:00
|
|
|
sys::Memory::InvalidateInstructionCache(Block.base(),
|
|
|
|
Block.allocatedSize());
|
2012-11-28 03:00:17 +08:00
|
|
|
}
|
|
|
|
|
2012-10-05 04:29:44 +08:00
|
|
|
SectionMemoryManager::~SectionMemoryManager() {
|
2015-10-01 10:45:07 +08:00
|
|
|
for (MemoryGroup *Group : {&CodeMem, &RWDataMem, &RODataMem}) {
|
2015-08-31 21:39:14 +08:00
|
|
|
for (sys::MemoryBlock &Block : Group->AllocatedMem)
|
2017-11-09 14:31:33 +08:00
|
|
|
MMapper.releaseMappedMemory(Block);
|
2015-10-01 10:45:07 +08:00
|
|
|
}
|
2012-10-05 04:29:44 +08:00
|
|
|
}
|
|
|
|
|
2017-11-09 14:31:33 +08:00
|
|
|
SectionMemoryManager::MemoryMapper::~MemoryMapper() {}
|
|
|
|
|
Add missing vtable anchors
Summary: This patch adds anchor() for MemoryBuffer, raw_fd_ostream, RTDyldMemoryManager, SectionMemoryManager, etc.
Reviewers: jlebar, eli.friedman, dblaikie
Reviewed By: dblaikie
Subscribers: mehdi_amini, mgorny, dblaikie, weimingz, llvm-commits
Differential Revision: https://reviews.llvm.org/D45244
llvm-svn: 329861
2018-04-12 07:09:20 +08:00
|
|
|
void SectionMemoryManager::anchor() {}
|
|
|
|
|
2017-11-09 14:31:33 +08:00
|
|
|
namespace {
|
|
|
|
// Trivial implementation of SectionMemoryManager::MemoryMapper that just calls
|
|
|
|
// into sys::Memory.
|
|
|
|
class DefaultMMapper final : public SectionMemoryManager::MemoryMapper {
|
|
|
|
public:
|
|
|
|
sys::MemoryBlock
|
|
|
|
allocateMappedMemory(SectionMemoryManager::AllocationPurpose Purpose,
|
|
|
|
size_t NumBytes, const sys::MemoryBlock *const NearBlock,
|
|
|
|
unsigned Flags, std::error_code &EC) override {
|
2019-05-21 04:53:05 +08:00
|
|
|
return sys::Memory::allocateMappedMemory(NumBytes, NearBlock, Flags, EC);
|
2017-11-09 14:31:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code protectMappedMemory(const sys::MemoryBlock &Block,
|
|
|
|
unsigned Flags) override {
|
|
|
|
return sys::Memory::protectMappedMemory(Block, Flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code releaseMappedMemory(sys::MemoryBlock &M) override {
|
|
|
|
return sys::Memory::releaseMappedMemory(M);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
DefaultMMapper DefaultMMapperInstance;
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
SectionMemoryManager::SectionMemoryManager(MemoryMapper *MM)
|
|
|
|
: MMapper(MM ? *MM : DefaultMMapperInstance) {}
|
|
|
|
|
2012-10-05 04:29:44 +08:00
|
|
|
} // namespace llvm
|