15#include "llvm/Config/config.h"
40uint8_t *SectionMemoryManager::allocateSection(
46 assert(!(Alignment & (Alignment - 1)) &&
"Alignment must be a power of two.");
48 uintptr_t RequiredSize = Alignment * ((
Size + Alignment - 1) / Alignment + 1);
51 MemoryGroup &MemGroup = [&]() -> MemoryGroup & {
65 for (FreeMemBlock &FreeMB : MemGroup.FreeMem) {
66 if (FreeMB.Free.allocatedSize() >= RequiredSize) {
67 Addr = (uintptr_t)FreeMB.Free.base();
68 uintptr_t EndOfBlock =
Addr + FreeMB.Free.allocatedSize();
70 Addr = (
Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
72 if (FreeMB.PendingPrefixIndex == (
unsigned)-1) {
74 MemGroup.PendingMem.push_back(sys::MemoryBlock((
void *)
Addr,
Size));
78 FreeMB.PendingPrefixIndex = MemGroup.PendingMem.size() - 1;
80 sys::MemoryBlock &PendingMB =
81 MemGroup.PendingMem[FreeMB.PendingPrefixIndex];
82 PendingMB = sys::MemoryBlock(PendingMB.base(),
83 Addr +
Size - (uintptr_t)PendingMB.base());
104 Purpose, RequiredSize, &MemGroup.Near,
116 if (CodeMem.Near.base() ==
nullptr)
118 if (RODataMem.Near.base() ==
nullptr)
120 if (RWDataMem.Near.base() ==
nullptr)
124 MemGroup.AllocatedMem.push_back(MB);
125 Addr = (uintptr_t)MB.base();
126 uintptr_t EndOfBlock =
Addr + MB.allocatedSize();
129 Addr = (
Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
132 MemGroup.PendingMem.push_back(sys::MemoryBlock((
void *)
Addr,
Size));
136 unsigned FreeSize = EndOfBlock -
Addr -
Size;
139 FreeMB.Free = sys::MemoryBlock((
void *)(
Addr +
Size), FreeSize);
140 FreeMB.PendingPrefixIndex = (
unsigned)-1;
141 MemGroup.FreeMem.push_back(FreeMB);
153 ec = applyMemoryGroupPermissions(CodeMem,
157 *ErrMsg = ec.message();
166 *ErrMsg = ec.message();
184 size_t StartOverlap =
187 size_t TrimmedSize = M.allocatedSize();
188 TrimmedSize -= StartOverlap;
189 TrimmedSize -= TrimmedSize %
PageSize;
203SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
204 unsigned Permissions) {
205 for (sys::MemoryBlock &MB : MemGroup.PendingMem)
209 MemGroup.PendingMem.clear();
213 for (FreeMemBlock &FreeMB : MemGroup.FreeMem) {
216 FreeMB.PendingPrefixIndex = (
unsigned)-1;
220 erase_if(MemGroup.FreeMem, [](FreeMemBlock &FreeMB) {
221 return FreeMB.Free.allocatedSize() == 0;
224 return std::error_code();
230 Block.allocatedSize());
234 for (MemoryGroup *Group : {&CodeMem, &RWDataMem, &RODataMem}) {
242void SectionMemoryManager::anchor() {}
247class DefaultMMapper final :
public SectionMemoryManager::MemoryMapper {
251 size_t NumBytes,
const sys::MemoryBlock *
const NearBlock,
252 unsigned Flags, std::error_code &EC)
override {
256 std::error_code protectMappedMemory(
const sys::MemoryBlock &
Block,
257 unsigned Flags)
override {
261 std::error_code releaseMappedMemory(sys::MemoryBlock &M)
override {
268 : MMapper(UnownedMM), OwnedMMapper(nullptr) {
270 OwnedMMapper = std::make_unique<DefaultMMapper>();
271 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...