#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/Config/config.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/Process.h"
namespace llvm {
uint8_t *SectionMemoryManager::allocateDataSection(uintptr_t Size,
unsigned Alignment,
unsigned SectionID,
StringRef SectionName,
bool IsReadOnly) {
if (IsReadOnly)
return allocateSection(SectionMemoryManager::AllocationPurpose::ROData,
Size, Alignment);
return allocateSection(SectionMemoryManager::AllocationPurpose::RWData, Size,
Alignment);
}
uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_t Size,
unsigned Alignment,
unsigned SectionID,
StringRef SectionName) {
return allocateSection(SectionMemoryManager::AllocationPurpose::Code, Size,
Alignment);
}
uint8_t *SectionMemoryManager::allocateSection(
SectionMemoryManager::AllocationPurpose Purpose, uintptr_t Size,
unsigned Alignment) {
if (!Alignment)
Alignment = 16;
assert(!(Alignment & (Alignment - 1)) && "Alignment must be a power of two.");
uintptr_t RequiredSize = Alignment * ((Size + Alignment - 1) / Alignment + 1);
uintptr_t Addr = 0;
MemoryGroup &MemGroup = [&]() -> MemoryGroup & {
switch (Purpose) {
case AllocationPurpose::Code:
return CodeMem;
case AllocationPurpose::ROData:
return RODataMem;
case AllocationPurpose::RWData:
return RWDataMem;
}
llvm_unreachable("Unknown SectionMemoryManager::AllocationPurpose");
}();
for (FreeMemBlock &FreeMB : MemGroup.FreeMem) {
if (FreeMB.Free.allocatedSize() >= RequiredSize) {
Addr = (uintptr_t)FreeMB.Free.base();
uintptr_t EndOfBlock = Addr + FreeMB.Free.allocatedSize();
Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
if (FreeMB.PendingPrefixIndex == (unsigned)-1) {
MemGroup.PendingMem.push_back(sys::MemoryBlock((void *)Addr, Size));
FreeMB.PendingPrefixIndex = MemGroup.PendingMem.size() - 1;
} else {
sys::MemoryBlock &PendingMB =
MemGroup.PendingMem[FreeMB.PendingPrefixIndex];
PendingMB = sys::MemoryBlock(PendingMB.base(),
Addr + Size - (uintptr_t)PendingMB.base());
}
FreeMB.Free =
sys::MemoryBlock((void *)(Addr + Size), EndOfBlock - Addr - Size);
return (uint8_t *)Addr;
}
}
std::error_code ec;
sys::MemoryBlock MB = MMapper.allocateMappedMemory(
Purpose, RequiredSize, &MemGroup.Near,
sys::Memory::MF_READ | sys::Memory::MF_WRITE, ec);
if (ec) {
return nullptr;
}
MemGroup.Near = MB;
if (CodeMem.Near.base() == nullptr)
CodeMem.Near = MB;
if (RODataMem.Near.base() == nullptr)
RODataMem.Near = MB;
if (RWDataMem.Near.base() == nullptr)
RWDataMem.Near = MB;
MemGroup.AllocatedMem.push_back(MB);
Addr = (uintptr_t)MB.base();
uintptr_t EndOfBlock = Addr + MB.allocatedSize();
Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
MemGroup.PendingMem.push_back(sys::MemoryBlock((void *)Addr, Size));
unsigned FreeSize = EndOfBlock - Addr - Size;
if (FreeSize > 16) {
FreeMemBlock FreeMB;
FreeMB.Free = sys::MemoryBlock((void *)(Addr + Size), FreeSize);
FreeMB.PendingPrefixIndex = (unsigned)-1;
MemGroup.FreeMem.push_back(FreeMB);
}
return (uint8_t *)Addr;
}
bool SectionMemoryManager::finalizeMemory(std::string *ErrMsg) {
std::error_code ec;
ec = applyMemoryGroupPermissions(CodeMem,
sys::Memory::MF_READ | sys::Memory::MF_EXEC);
if (ec) {
if (ErrMsg) {
*ErrMsg = ec.message();
}
return true;
}
ec = applyMemoryGroupPermissions(RODataMem, sys::Memory::MF_READ);
if (ec) {
if (ErrMsg) {
*ErrMsg = ec.message();
}
return true;
}
invalidateInstructionCache();
return false;
}
static sys::MemoryBlock trimBlockToPageSize(sys::MemoryBlock M) {
static const size_t PageSize = sys::Process::getPageSizeEstimate();
size_t StartOverlap =
(PageSize - ((uintptr_t)M.base() % PageSize)) % PageSize;
size_t TrimmedSize = M.allocatedSize();
TrimmedSize -= StartOverlap;
TrimmedSize -= TrimmedSize % PageSize;
sys::MemoryBlock Trimmed((void *)((uintptr_t)M.base() + StartOverlap),
TrimmedSize);
assert(((uintptr_t)Trimmed.base() % PageSize) == 0);
assert((Trimmed.allocatedSize() % PageSize) == 0);
assert(M.base() <= Trimmed.base() &&
Trimmed.allocatedSize() <= M.allocatedSize());
return Trimmed;
}
std::error_code
SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
unsigned Permissions) {
for (sys::MemoryBlock &MB : MemGroup.PendingMem)
if (std::error_code EC = MMapper.protectMappedMemory(MB, Permissions))
return EC;
MemGroup.PendingMem.clear();
for (FreeMemBlock &FreeMB : MemGroup.FreeMem) {
FreeMB.Free = trimBlockToPageSize(FreeMB.Free);
FreeMB.PendingPrefixIndex = (unsigned)-1;
}
erase_if(MemGroup.FreeMem, [](FreeMemBlock &FreeMB) {
return FreeMB.Free.allocatedSize() == 0;
});
return std::error_code();
}
void SectionMemoryManager::invalidateInstructionCache() {
for (sys::MemoryBlock &Block : CodeMem.PendingMem)
sys::Memory::InvalidateInstructionCache(Block.base(),
Block.allocatedSize());
}
SectionMemoryManager::~SectionMemoryManager() {
for (MemoryGroup *Group : {&CodeMem, &RWDataMem, &RODataMem}) {
for (sys::MemoryBlock &Block : Group->AllocatedMem)
MMapper.releaseMappedMemory(Block);
}
}
SectionMemoryManager::MemoryMapper::~MemoryMapper() = default;
void SectionMemoryManager::anchor() {}
namespace {
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 {
return sys::Memory::allocateMappedMemory(NumBytes, NearBlock, Flags, EC);
}
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;
}
SectionMemoryManager::SectionMemoryManager(MemoryMapper *MM)
: MMapper(MM ? *MM : DefaultMMapperInstance) {}
}