15 #include "llvm/Config/config.h" 41 uint8_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) {
79 FreeMB.PendingPrefixIndex = MemGroup.PendingMem.size() - 1;
82 MemGroup.PendingMem[FreeMB.PendingPrefixIndex];
84 Addr + Size - (uintptr_t)PendingMB.
base());
90 return (uint8_t *)Addr;
105 Purpose, RequiredSize, &MemGroup.Near,
116 MemGroup.AllocatedMem.push_back(MB);
117 Addr = (uintptr_t)MB.
base();
121 Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
128 unsigned FreeSize = EndOfBlock - Addr -
Size;
132 FreeMB.PendingPrefixIndex = (
unsigned)-1;
133 MemGroup.FreeMem.push_back(FreeMB);
137 return (uint8_t *)Addr;
145 ec = applyMemoryGroupPermissions(CodeMem,
149 *ErrMsg = ec.message();
155 ec = applyMemoryGroupPermissions(RODataMem,
159 *ErrMsg = ec.message();
177 size_t StartOverlap =
181 TrimmedSize -= StartOverlap;
182 TrimmedSize -= TrimmedSize %
PageSize;
196 SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
197 unsigned Permissions) {
202 MemGroup.PendingMem.clear();
206 for (FreeMemBlock &FreeMB : MemGroup.FreeMem) {
209 FreeMB.PendingPrefixIndex = (
unsigned)-1;
213 MemGroup.FreeMem.erase(
remove_if(MemGroup.FreeMem,
214 [](FreeMemBlock &FreeMB) {
215 return FreeMB.Free.allocatedSize() == 0;
217 MemGroup.FreeMem.end());
219 return std::error_code();
229 for (MemoryGroup *Group : {&CodeMem, &RWDataMem, &RODataMem}) {
237 void SectionMemoryManager::anchor() {}
247 unsigned Flags, std::error_code &EC)
override {
252 unsigned Flags)
override {
261 DefaultMMapper DefaultMMapperInstance;
265 : MMapper(MM ? *MM : DefaultMMapperInstance) {}
virtual std::error_code releaseMappedMemory(sys::MemoryBlock &M)=0
This method releases a block of memory that was allocated with the allocateMappedMemory method...
This class represents lattice values for constants.
size_t allocatedSize() const
The size as it was allocated.
SectionMemoryManager(MemoryMapper *MM=nullptr)
Creates a SectionMemoryManager instance with MM as the associated memory mapper.
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...
Implementations of this interface are used by SectionMemoryManager to request pages from the operatin...
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...
~SectionMemoryManager() override
AllocationPurpose
This enum describes the various reasons to allocate pages from allocateMappedMemory.
virtual void invalidateInstructionCache()
Invalidate instruction cache for code sections.
static unsigned getPageSizeEstimate()
Get the process's estimated page size.
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.
auto remove_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::remove_if which take ranges instead of having to pass begin/end explicitly...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
static sys::MemoryBlock trimBlockToPageSize(sys::MemoryBlock M)
This class encapsulates the notion of a memory block which has an address and a size.
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.
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.
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...
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)
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...