15#include "llvm/Config/config.h"
41uint8_t *SectionMemoryManager::allocateSection(
47 assert(!(Alignment & (Alignment - 1)) &&
"Alignment must be a power of two.");
49 uintptr_t RequiredSize = Alignment * ((
Size + Alignment - 1) / Alignment + 1);
52 MemoryGroup &MemGroup = [&]() -> MemoryGroup & {
66 for (FreeMemBlock &FreeMB : MemGroup.FreeMem) {
67 if (FreeMB.Free.allocatedSize() >= RequiredSize) {
68 Addr = (uintptr_t)FreeMB.Free.base();
69 uintptr_t EndOfBlock =
Addr + FreeMB.Free.allocatedSize();
71 Addr = (
Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
73 if (FreeMB.PendingPrefixIndex == (
unsigned)-1) {
75 MemGroup.PendingMem.push_back(sys::MemoryBlock((
void *)
Addr,
Size));
79 FreeMB.PendingPrefixIndex = MemGroup.PendingMem.size() - 1;
81 sys::MemoryBlock &PendingMB =
82 MemGroup.PendingMem[FreeMB.PendingPrefixIndex];
83 PendingMB = sys::MemoryBlock(PendingMB.base(),
84 Addr +
Size - (uintptr_t)PendingMB.base());
90 return (uint8_t *)
Addr;
105 Purpose, RequiredSize, &MemGroup.Near,
117 if (CodeMem.Near.base() ==
nullptr)
119 if (RODataMem.Near.base() ==
nullptr)
121 if (RWDataMem.Near.base() ==
nullptr)
125 MemGroup.AllocatedMem.push_back(MB);
126 Addr = (uintptr_t)MB.base();
127 uintptr_t EndOfBlock =
Addr + MB.allocatedSize();
130 Addr = (
Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
133 MemGroup.PendingMem.push_back(sys::MemoryBlock((
void *)
Addr,
Size));
137 unsigned FreeSize = EndOfBlock -
Addr -
Size;
140 FreeMB.Free = sys::MemoryBlock((
void *)(
Addr +
Size), FreeSize);
141 FreeMB.PendingPrefixIndex = (
unsigned)-1;
142 MemGroup.FreeMem.push_back(FreeMB);
146 return (uint8_t *)
Addr;
154 ec = applyMemoryGroupPermissions(CodeMem,
158 *ErrMsg = ec.message();
167 *ErrMsg = ec.message();
185 size_t StartOverlap =
188 size_t TrimmedSize = M.allocatedSize();
189 TrimmedSize -= StartOverlap;
190 TrimmedSize -= TrimmedSize %
PageSize;
204SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
205 unsigned Permissions) {
206 for (sys::MemoryBlock &MB : MemGroup.PendingMem)
210 MemGroup.PendingMem.clear();
214 for (FreeMemBlock &FreeMB : MemGroup.FreeMem) {
217 FreeMB.PendingPrefixIndex = (
unsigned)-1;
221 erase_if(MemGroup.FreeMem, [](FreeMemBlock &FreeMB) {
222 return FreeMB.Free.allocatedSize() == 0;
225 return std::error_code();
231 Block.allocatedSize());
235 for (MemoryGroup *Group : {&CodeMem, &RWDataMem, &RODataMem}) {
243void SectionMemoryManager::anchor() {}
248class DefaultMMapper final :
public SectionMemoryManager::MemoryMapper {
252 size_t NumBytes,
const sys::MemoryBlock *
const NearBlock,
253 unsigned Flags, std::error_code &EC)
override {
257 std::error_code protectMappedMemory(
const sys::MemoryBlock &
Block,
258 unsigned Flags)
override {
262 std::error_code releaseMappedMemory(sys::MemoryBlock &M)
override {
269 : MMapper(UnownedMM), OwnedMMapper(nullptr) {
271 OwnedMMapper = std::make_unique<DefaultMMapper>();
272 MMapper = OwnedMMapper.get();
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 ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
virtual std::error_code releaseMappedMemory(sys::MemoryBlock &M)=0
This method releases a block of memory that was allocated with the allocateMappedMemory method.
virtual sys::MemoryBlock allocateMappedMemory(AllocationPurpose Purpose, size_t NumBytes, const sys::MemoryBlock *const NearBlock, unsigned Flags, std::error_code &EC)=0
This method attempts to allocate NumBytes bytes of virtual memory for Purpose.
AllocationPurpose
This enum describes the various reasons to allocate pages from allocateMappedMemory.
SectionMemoryManager(MemoryMapper *MM=nullptr)
Creates a SectionMemoryManager instance with MM as the associated memory mapper.
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.
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 std::error_code releaseMappedMemory(MemoryBlock &Block)
This method releases a block of memory that was allocated with the allocateMappedMemory method.
static 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 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 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 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)
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...