15 #include "llvm/Config/config.h"
28 return allocateSection(RODataMem, Size, Alignment);
29 return allocateSection(RWDataMem, Size, Alignment);
36 return allocateSection(CodeMem, Size, Alignment);
39 uint8_t *SectionMemoryManager::allocateSection(MemoryGroup &MemGroup,
45 assert(!(Alignment & (Alignment - 1)) &&
"Alignment must be a power of two.");
47 uintptr_t RequiredSize = Alignment * ((Size + Alignment - 1)/Alignment + 1);
52 for (FreeMemBlock &FreeMB : MemGroup.FreeMem) {
53 if (FreeMB.Free.size() >= RequiredSize) {
54 Addr = (uintptr_t)FreeMB.Free.base();
55 uintptr_t EndOfBlock = Addr + FreeMB.Free.size();
57 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
59 if (FreeMB.PendingPrefixIndex == (
unsigned)-1) {
65 FreeMB.PendingPrefixIndex = MemGroup.PendingMem.size() - 1;
67 sys::MemoryBlock &PendingMB = MemGroup.PendingMem[FreeMB.PendingPrefixIndex];
68 PendingMB = sys::MemoryBlock(PendingMB.base(), Addr + Size - (uintptr_t)PendingMB.base());
72 FreeMB.Free = sys::MemoryBlock((
void *)(Addr + Size), EndOfBlock - Addr - Size);
73 return (uint8_t*)Addr;
101 MemGroup.AllocatedMem.push_back(MB);
102 Addr = (uintptr_t)MB.base();
103 uintptr_t EndOfBlock = Addr + MB.size();
106 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
109 MemGroup.PendingMem.push_back(sys::MemoryBlock((
void *)Addr, Size));
113 unsigned FreeSize = EndOfBlock-Addr-Size;
116 FreeMB.Free = sys::MemoryBlock((
void*)(Addr + Size), FreeSize);
117 FreeMB.PendingPrefixIndex = (
unsigned)-1;
118 MemGroup.FreeMem.push_back(FreeMB);
122 return (uint8_t*)Addr;
131 ec = applyMemoryGroupPermissions(CodeMem,
135 *ErrMsg = ec.message();
141 ec = applyMemoryGroupPermissions(RODataMem,
145 *ErrMsg = ec.message();
163 size_t StartOverlap =
166 size_t TrimmedSize = M.
size();
167 TrimmedSize -= StartOverlap;
168 TrimmedSize -= TrimmedSize %
PageSize;
181 SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
182 unsigned Permissions) {
183 for (sys::MemoryBlock &MB : MemGroup.PendingMem)
187 MemGroup.PendingMem.clear();
191 for (FreeMemBlock &FreeMB : MemGroup.FreeMem) {
194 FreeMB.PendingPrefixIndex = (
unsigned)-1;
198 MemGroup.FreeMem.erase(
200 [](FreeMemBlock &FreeMB) {
return FreeMB.Free.size() == 0; }),
201 MemGroup.FreeMem.end());
203 return std::error_code();
206 void SectionMemoryManager::invalidateInstructionCache() {
208 sys::Memory::InvalidateInstructionCache(Block.
base(), Block.
size());
211 SectionMemoryManager::~SectionMemoryManager() {
212 for (MemoryGroup *Group : {&CodeMem, &RWDataMem, &RODataMem}) {
214 sys::Memory::releaseMappedMemory(Block);
auto remove_if(R &&Range, UnaryPredicate P) -> decltype(std::begin(Range))
Provide wrappers to std::remove_if which take ranges instead of having to pass begin/end explicitly...
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...
virtual void invalidateInstructionCache()
Invalidate instruction cache for code sections.
static cl::opt< int > PageSize("imp-null-check-page-size", cl::desc("The page size of the target in bytes"), cl::init(4096))
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.
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.
static unsigned getPageSize()
static sys::MemoryBlock trimBlockToPageSize(sys::MemoryBlock M)
This class encapsulates the notion of a memory block which has an address and a size.
Provides a library for accessing information about this process and other processes on the operating ...
bool finalizeMemory(std::string *ErrMsg=nullptr) override
Update section-specific memory permissions and other attributes.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
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...