15#include "llvm/Config/config.h"
20bool SectionMemoryManager::hasSpace(
const MemoryGroup &MemGroup,
21 uintptr_t
Size)
const {
22 for (
const FreeMemBlock &FreeMB : MemGroup.FreeMem) {
23 if (FreeMB.Free.allocatedSize() >=
Size)
31 Align RODataAlign, uintptr_t RWDataSize,
Align RWDataAlign) {
32 if (
CodeSize == 0 && RODataSize == 0 && RWDataSize == 0)
41 CodeAlign =
Align(std::max(CodeAlign.
value(), StubAlign));
42 RODataAlign =
Align(std::max(RODataAlign.
value(), StubAlign));
43 RWDataAlign =
Align(std::max(RWDataAlign.
value(), StubAlign));
53 if (hasSpace(CodeMem, RequiredCodeSize) &&
54 hasSpace(RODataMem, RequiredRODataSize) &&
55 hasSpace(RWDataMem, RequiredRWDataSize)) {
66 CodeMem.FreeMem.clear();
67 RODataMem.FreeMem.clear();
68 RWDataMem.FreeMem.clear();
75 RequiredCodeSize + RequiredRODataSize + RequiredRWDataSize;
85 CodeMem.AllocatedMem.push_back(MB);
86 uintptr_t Addr = (uintptr_t)MB.
base();
88 FreeMB.PendingPrefixIndex = (
unsigned)-1;
93 CodeMem.FreeMem.push_back(FreeMB);
94 Addr += RequiredCodeSize;
100 RODataMem.FreeMem.push_back(FreeMB);
101 Addr += RequiredRODataSize;
104 if (RWDataSize > 0) {
107 RWDataMem.FreeMem.push_back(FreeMB);
131uint8_t *SectionMemoryManager::allocateSection(
133 unsigned Alignment) {
137 assert(!(Alignment & (Alignment - 1)) &&
"Alignment must be a power of two.");
139 uintptr_t RequiredSize = Alignment * ((
Size + Alignment - 1) / Alignment + 1);
142 MemoryGroup &MemGroup = [&]() -> MemoryGroup & {
156 for (FreeMemBlock &FreeMB : MemGroup.FreeMem) {
157 if (FreeMB.Free.allocatedSize() >= RequiredSize) {
158 Addr = (uintptr_t)FreeMB.Free.base();
159 uintptr_t EndOfBlock = Addr + FreeMB.Free.allocatedSize();
161 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
163 if (FreeMB.PendingPrefixIndex == (
unsigned)-1) {
165 MemGroup.PendingMem.push_back(sys::MemoryBlock((
void *)Addr,
Size));
169 FreeMB.PendingPrefixIndex = MemGroup.PendingMem.size() - 1;
171 sys::MemoryBlock &PendingMB =
172 MemGroup.PendingMem[FreeMB.PendingPrefixIndex];
173 PendingMB = sys::MemoryBlock(PendingMB.base(),
174 Addr +
Size - (uintptr_t)PendingMB.base());
179 sys::MemoryBlock((
void *)(Addr +
Size), EndOfBlock - Addr -
Size);
180 return (uint8_t *)Addr;
194 sys::MemoryBlock MB = MMapper->allocateMappedMemory(
195 Purpose, RequiredSize, &MemGroup.Near,
207 if (CodeMem.Near.base() ==
nullptr)
209 if (RODataMem.Near.base() ==
nullptr)
211 if (RWDataMem.Near.base() ==
nullptr)
215 MemGroup.AllocatedMem.push_back(MB);
216 Addr = (uintptr_t)MB.base();
217 uintptr_t EndOfBlock = Addr + MB.allocatedSize();
220 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
223 MemGroup.PendingMem.push_back(sys::MemoryBlock((
void *)Addr,
Size));
227 unsigned FreeSize = EndOfBlock - Addr -
Size;
230 FreeMB.Free = sys::MemoryBlock((
void *)(Addr +
Size), FreeSize);
231 FreeMB.PendingPrefixIndex = (unsigned)-1;
232 MemGroup.FreeMem.push_back(FreeMB);
236 return (uint8_t *)Addr;
244 ec = applyMemoryGroupPermissions(CodeMem,
248 *ErrMsg = ec.message();
257 *ErrMsg = ec.message();
275 size_t StartOverlap =
278 size_t TrimmedSize = M.allocatedSize();
279 TrimmedSize -= StartOverlap;
280 TrimmedSize -= TrimmedSize %
PageSize;
294SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
295 unsigned Permissions) {
296 for (sys::MemoryBlock &MB : MemGroup.PendingMem)
300 MemGroup.PendingMem.clear();
304 for (FreeMemBlock &FreeMB : MemGroup.FreeMem) {
307 FreeMB.PendingPrefixIndex = (
unsigned)-1;
311 erase_if(MemGroup.FreeMem, [](FreeMemBlock &FreeMB) {
312 return FreeMB.Free.allocatedSize() == 0;
315 return std::error_code();
321 Block.allocatedSize());
325 for (MemoryGroup *Group : {&CodeMem, &RWDataMem, &RODataMem}) {
327 MMapper->releaseMappedMemory(
Block);
333void SectionMemoryManager::anchor() {}
338class DefaultMMapper final :
public SectionMemoryManager::MemoryMapper {
342 size_t NumBytes,
const sys::MemoryBlock *
const NearBlock,
343 unsigned Flags, std::error_code &EC)
override {
347 std::error_code protectMappedMemory(
const sys::MemoryBlock &
Block,
348 unsigned Flags)
override {
352 std::error_code releaseMappedMemory(sys::MemoryBlock &M)
override {
360 : MMapper(UnownedMM), OwnedMMapper(nullptr),
361 ReserveAllocation(ReserveAlloc) {
363 OwnedMMapper = std::make_unique<DefaultMMapper>();
364 MMapper = OwnedMMapper.get();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static cl::opt< int > PageSize("imp-null-check-page-size", cl::desc("The page size of the target in bytes"), cl::init(4096), cl::Hidden)
Provides a library for accessing information about this process and other processes on the operating ...
Implementations of this interface are used by SectionMemoryManager to request pages from the operatin...
virtual std::error_code protectMappedMemory(const sys::MemoryBlock &Block, unsigned Flags)=0
This method sets the protection flags for a block of memory to the state specified by Flags.
AllocationPurpose
This enum describes the various reasons to allocate pages from allocateMappedMemory.
void reserveAllocationSpace(uintptr_t CodeSize, Align CodeAlign, uintptr_t RODataSize, Align RODataAlign, uintptr_t RWDataSize, Align RWDataAlign) override
Implements allocating all memory in a single block.
virtual void invalidateInstructionCache()
Invalidate instruction cache for code sections.
~SectionMemoryManager() override
uint8_t * allocateDataSection(uintptr_t Size, unsigned Alignment, unsigned SectionID, StringRef SectionName, bool isReadOnly) override
Allocates a memory block of (at least) the given size suitable for executable code.
SectionMemoryManager(MemoryMapper *MM=nullptr, bool ReserveAlloc=false)
Creates a SectionMemoryManager instance with MM as the associated memory mapper.
uint8_t * allocateCodeSection(uintptr_t Size, unsigned Alignment, unsigned SectionID, StringRef SectionName) override
Allocates a memory block of (at least) the given size suitable for executable code.
bool finalizeMemory(std::string *ErrMsg=nullptr) override
Update section-specific memory permissions and other attributes.
StringRef - Represent a constant reference to a string, i.e.
This class encapsulates the notion of a memory block which has an address and a size.
size_t allocatedSize() const
The size as it was allocated.
static LLVM_ABI std::error_code protectMappedMemory(const MemoryBlock &Block, unsigned Flags)
This method sets the protection flags for a block of memory to the state specified by /p Flags.
static LLVM_ABI std::error_code releaseMappedMemory(MemoryBlock &Block)
This method releases a block of memory that was allocated with the allocateMappedMemory method.
static LLVM_ABI void InvalidateInstructionCache(const void *Addr, size_t Len)
InvalidateInstructionCache - Before the JIT can run a block of code that has been emitted it must inv...
static LLVM_ABI MemoryBlock allocateMappedMemory(size_t NumBytes, const MemoryBlock *const NearBlock, unsigned Flags, std::error_code &EC)
This method allocates a block of memory that is suitable for loading dynamically generated code (e....
static unsigned getPageSizeEstimate()
Get the process's estimated page size.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
static sys::MemoryBlock trimBlockToPageSize(sys::MemoryBlock M)
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
bool isAddrAligned(Align Lhs, const void *Addr)
Checks that Addr is a multiple of the alignment.
This struct is a compact representation of a valid (non-zero power of two) alignment.
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.