LLVM 22.0.0git
SectionMemoryManager.h
Go to the documentation of this file.
1//===- SectionMemoryManager.h - Memory manager for MCJIT/RtDyld -*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the declaration of a section-based memory manager used by
10// the MCJIT execution engine and RuntimeDyld.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_EXECUTIONENGINE_SECTIONMEMORYMANAGER_H
15#define LLVM_EXECUTIONENGINE_SECTIONMEMORYMANAGER_H
16
20#include "llvm/Support/Memory.h"
21#include <cstdint>
22#include <string>
23#include <system_error>
24
25namespace llvm {
26
27/// This is a simple memory manager which implements the methods called by
28/// the RuntimeDyld class to allocate memory for section-based loading of
29/// objects, usually those generated by the MCJIT execution engine.
30///
31/// This memory manager allocates all section memory as read-write. The
32/// RuntimeDyld will copy JITed section memory into these allocated blocks
33/// and perform any necessary linking and relocations.
34///
35/// Any client using this memory manager MUST ensure that section-specific
36/// page permissions have been applied before attempting to execute functions
37/// in the JITed object. Permissions can be applied either by calling
38/// MCJIT::finalizeObject or by calling SectionMemoryManager::finalizeMemory
39/// directly. Clients of MCJIT should call MCJIT::finalizeObject.
41public:
42 /// This enum describes the various reasons to allocate pages from
43 /// allocateMappedMemory.
49
50 /// Implementations of this interface are used by SectionMemoryManager to
51 /// request pages from the operating system.
53 public:
54 /// This method attempts to allocate \p NumBytes bytes of virtual memory for
55 /// \p Purpose. \p NearBlock may point to an existing allocation, in which
56 /// case an attempt is made to allocate more memory near the existing block.
57 /// The actual allocated address is not guaranteed to be near the requested
58 /// address. \p Flags is used to set the initial protection flags for the
59 /// block of the memory. \p EC [out] returns an object describing any error
60 /// that occurs.
61 ///
62 /// This method may allocate more than the number of bytes requested. The
63 /// actual number of bytes allocated is indicated in the returned
64 /// MemoryBlock.
65 ///
66 /// The start of the allocated block must be aligned with the system
67 /// allocation granularity (64K on Windows, page size on Linux). If the
68 /// address following \p NearBlock is not so aligned, it will be rounded up
69 /// to the next allocation granularity boundary.
70 ///
71 /// \r a non-null MemoryBlock if the function was successful, otherwise a
72 /// null MemoryBlock with \p EC describing the error.
73 virtual sys::MemoryBlock
74 allocateMappedMemory(AllocationPurpose Purpose, size_t NumBytes,
75 const sys::MemoryBlock *const NearBlock,
76 unsigned Flags, std::error_code &EC) = 0;
77
78 /// This method sets the protection flags for a block of memory to the state
79 /// specified by \p Flags. The behavior is not specified if the memory was
80 /// not allocated using the allocateMappedMemory method.
81 /// \p Block describes the memory block to be protected.
82 /// \p Flags specifies the new protection state to be assigned to the block.
83 ///
84 /// If \p Flags is MF_WRITE, the actual behavior varies with the operating
85 /// system (i.e. MF_READ | MF_WRITE on Windows) and the target architecture
86 /// (i.e. MF_WRITE -> MF_READ | MF_WRITE on i386).
87 ///
88 /// \r error_success if the function was successful, or an error_code
89 /// describing the failure if an error occurred.
90 virtual std::error_code protectMappedMemory(const sys::MemoryBlock &Block,
91 unsigned Flags) = 0;
92
93 /// This method releases a block of memory that was allocated with the
94 /// allocateMappedMemory method. It should not be used to release any memory
95 /// block allocated any other way.
96 /// \p Block describes the memory to be released.
97 ///
98 /// \r error_success if the function was successful, or an error_code
99 /// describing the failure if an error occurred.
100 virtual std::error_code releaseMappedMemory(sys::MemoryBlock &M) = 0;
101
102 virtual ~MemoryMapper();
103 };
104
105 /// Creates a SectionMemoryManager instance with \p MM as the associated
106 /// memory mapper. If \p MM is nullptr then a default memory mapper is used
107 /// that directly calls into the operating system.
108 ///
109 /// If \p ReserveAlloc is true all memory will be pre-allocated, and any
110 /// attempts to allocate beyond pre-allocated memory will fail.
111 SectionMemoryManager(MemoryMapper *MM = nullptr, bool ReserveAlloc = false);
113 void operator=(const SectionMemoryManager &) = delete;
114 ~SectionMemoryManager() override;
115
116 /// Enable reserveAllocationSpace when requested.
117 bool needsToReserveAllocationSpace() override { return ReserveAllocation; }
118
119 /// Implements allocating all memory in a single block. This is required to
120 /// limit memory offsets to fit the ARM ABI; large memory systems may
121 /// otherwise allocate separate sections too far apart.
122 void reserveAllocationSpace(uintptr_t CodeSize, Align CodeAlign,
123 uintptr_t RODataSize, Align RODataAlign,
124 uintptr_t RWDataSize, Align RWDataAlign) override;
125
126 /// Allocates a memory block of (at least) the given size suitable for
127 /// executable code.
128 ///
129 /// The value of \p Alignment must be a power of two. If \p Alignment is zero
130 /// a default alignment of 16 will be used.
131 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
132 unsigned SectionID,
133 StringRef SectionName) override;
134
135 /// Allocates a memory block of (at least) the given size suitable for
136 /// executable code.
137 ///
138 /// The value of \p Alignment must be a power of two. If \p Alignment is zero
139 /// a default alignment of 16 will be used.
140 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
141 unsigned SectionID, StringRef SectionName,
142 bool isReadOnly) override;
143
144 /// Update section-specific memory permissions and other attributes.
145 ///
146 /// This method is called when object loading is complete and section page
147 /// permissions can be applied. It is up to the memory manager implementation
148 /// to decide whether or not to act on this method. The memory manager will
149 /// typically allocate all sections as read-write and then apply specific
150 /// permissions when this method is called. Code sections cannot be executed
151 /// until this function has been called. In addition, any cache coherency
152 /// operations needed to reliably use the memory are also performed.
153 ///
154 /// \returns true if an error occurred, false otherwise.
155 bool finalizeMemory(std::string *ErrMsg = nullptr) override;
156
157 /// Invalidate instruction cache for code sections.
158 ///
159 /// Some platforms with separate data cache and instruction cache require
160 /// explicit cache flush, otherwise JIT code manipulations (like resolved
161 /// relocations) will get to the data cache but not to the instruction cache.
162 ///
163 /// This method is called from finalizeMemory.
164 virtual void invalidateInstructionCache();
165
166private:
167 struct FreeMemBlock {
168 // The actual block of free memory
170 // If there is a pending allocation from the same reservation right before
171 // this block, store it's index in PendingMem, to be able to update the
172 // pending region if part of this block is allocated, rather than having to
173 // create a new one
174 unsigned PendingPrefixIndex;
175 };
176
177 struct MemoryGroup {
178 // PendingMem contains all blocks of memory (subblocks of AllocatedMem)
179 // which have not yet had their permissions applied, but have been given
180 // out to the user. FreeMem contains all block of memory, which have
181 // neither had their permissions applied, nor been given out to the user.
182 SmallVector<sys::MemoryBlock, 16> PendingMem;
183 SmallVector<FreeMemBlock, 16> FreeMem;
184
185 // All memory blocks that have been requested from the system
186 SmallVector<sys::MemoryBlock, 16> AllocatedMem;
187
188 sys::MemoryBlock Near;
189 };
190
191 uint8_t *allocateSection(AllocationPurpose Purpose, uintptr_t Size,
192 unsigned Alignment);
193
194 std::error_code applyMemoryGroupPermissions(MemoryGroup &MemGroup,
195 unsigned Permissions);
196
197 bool hasSpace(const MemoryGroup &MemGroup, uintptr_t Size) const;
198
199 void anchor() override;
200
201 MemoryGroup CodeMem;
202 MemoryGroup RWDataMem;
203 MemoryGroup RODataMem;
204 MemoryMapper *MMapper;
205 std::unique_ptr<MemoryMapper> OwnedMMapper;
206 bool ReserveAllocation;
207};
208
209} // end namespace llvm
210
211#endif // LLVM_EXECUTIONENGINE_SECTIONMEMORYMANAGER_H
#define LLVM_ABI
Definition Compiler.h:213
This file defines the SmallVector class.
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.
void operator=(const SectionMemoryManager &)=delete
SectionMemoryManager(MemoryMapper *MM=nullptr, bool ReserveAlloc=false)
Creates a SectionMemoryManager instance with MM as the associated memory mapper.
SectionMemoryManager(const SectionMemoryManager &)=delete
bool needsToReserveAllocationSpace() override
Enable reserveAllocationSpace when requested.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
This class encapsulates the notion of a memory block which has an address and a size.
Definition Memory.h:33
This is an optimization pass for GlobalISel generic memory operations.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39