Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
SectionMemoryManager.cpp
Go to the documentation of this file.
1//===- SectionMemoryManager.cpp - 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 implements the section-based memory manager used by the MCJIT
10// execution engine and RuntimeDyld
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ExecutionEngine/SectionMemoryManager.h"
15#include "llvm/Config/config.h"
16#include "llvm/Support/Process.h"
17
18namespacellvm {
19
20uint8_t *SectionMemoryManager::allocateDataSection(uintptr_tSize,
21unsigned Alignment,
22unsigned SectionID,
23StringRefSectionName,
24bool IsReadOnly) {
25if (IsReadOnly)
26return allocateSection(SectionMemoryManager::AllocationPurpose::ROData,
27Size, Alignment);
28return allocateSection(SectionMemoryManager::AllocationPurpose::RWData,Size,
29 Alignment);
30}
31
32uint8_t *SectionMemoryManager::allocateCodeSection(uintptr_tSize,
33unsigned Alignment,
34unsigned SectionID,
35StringRefSectionName) {
36return allocateSection(SectionMemoryManager::AllocationPurpose::Code,Size,
37 Alignment);
38}
39
40uint8_t *SectionMemoryManager::allocateSection(
41SectionMemoryManager::AllocationPurpose Purpose, uintptr_tSize,
42unsigned Alignment) {
43if (!Alignment)
44 Alignment = 16;
45
46assert(!(Alignment & (Alignment - 1)) &&"Alignment must be a power of two.");
47
48 uintptr_t RequiredSize = Alignment * ((Size + Alignment - 1) / Alignment + 1);
49 uintptr_tAddr = 0;
50
51 MemoryGroup &MemGroup = [&]() -> MemoryGroup & {
52switch (Purpose) {
53caseAllocationPurpose::Code:
54return CodeMem;
55caseAllocationPurpose::ROData:
56return RODataMem;
57caseAllocationPurpose::RWData:
58return RWDataMem;
59 }
60llvm_unreachable("Unknown SectionMemoryManager::AllocationPurpose");
61 }();
62
63// Look in the list of free memory regions and use a block there if one
64// is available.
65for (FreeMemBlock &FreeMB : MemGroup.FreeMem) {
66if (FreeMB.Free.allocatedSize() >= RequiredSize) {
67Addr = (uintptr_t)FreeMB.Free.base();
68 uintptr_t EndOfBlock =Addr + FreeMB.Free.allocatedSize();
69// Align the address.
70Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
71
72if (FreeMB.PendingPrefixIndex == (unsigned)-1) {
73// The part of the block we're giving out to the user is now pending
74 MemGroup.PendingMem.push_back(sys::MemoryBlock((void *)Addr,Size));
75
76// Remember this pending block, such that future allocations can just
77// modify it rather than creating a new one
78 FreeMB.PendingPrefixIndex = MemGroup.PendingMem.size() - 1;
79 }else {
80 sys::MemoryBlock &PendingMB =
81 MemGroup.PendingMem[FreeMB.PendingPrefixIndex];
82 PendingMB = sys::MemoryBlock(PendingMB.base(),
83Addr +Size - (uintptr_t)PendingMB.base());
84 }
85
86// Remember how much free space is now left in this block
87 FreeMB.Free =
88 sys::MemoryBlock((void *)(Addr +Size), EndOfBlock -Addr -Size);
89return (uint8_t *)Addr;
90 }
91 }
92
93// No pre-allocated free block was large enough. Allocate a new memory region.
94// Note that all sections get allocated as read-write. The permissions will
95// be updated later based on memory group.
96//
97// FIXME: It would be useful to define a default allocation size (or add
98// it as a constructor parameter) to minimize the number of allocations.
99//
100// FIXME: Initialize the Near member for each memory group to avoid
101// interleaving.
102 std::error_code ec;
103 sys::MemoryBlock MB = MMapper->allocateMappedMemory(
104 Purpose, RequiredSize, &MemGroup.Near,
105sys::Memory::MF_READ |sys::Memory::MF_WRITE, ec);
106if (ec) {
107// FIXME: Add error propagation to the interface.
108returnnullptr;
109 }
110
111// Save this address as the basis for our next request
112 MemGroup.Near = MB;
113
114// Copy the address to all the other groups, if they have not
115// been initialized.
116if (CodeMem.Near.base() ==nullptr)
117 CodeMem.Near = MB;
118if (RODataMem.Near.base() ==nullptr)
119 RODataMem.Near = MB;
120if (RWDataMem.Near.base() ==nullptr)
121 RWDataMem.Near = MB;
122
123// Remember that we allocated this memory
124 MemGroup.AllocatedMem.push_back(MB);
125Addr = (uintptr_t)MB.base();
126 uintptr_t EndOfBlock =Addr + MB.allocatedSize();
127
128// Align the address.
129Addr = (Addr + Alignment - 1) & ~(uintptr_t)(Alignment - 1);
130
131// The part of the block we're giving out to the user is now pending
132 MemGroup.PendingMem.push_back(sys::MemoryBlock((void *)Addr,Size));
133
134// The allocateMappedMemory may allocate much more memory than we need. In
135// this case, we store the unused memory as a free memory block.
136unsigned FreeSize = EndOfBlock -Addr -Size;
137if (FreeSize > 16) {
138 FreeMemBlock FreeMB;
139 FreeMB.Free = sys::MemoryBlock((void *)(Addr +Size), FreeSize);
140 FreeMB.PendingPrefixIndex = (unsigned)-1;
141 MemGroup.FreeMem.push_back(FreeMB);
142 }
143
144// Return aligned address
145return (uint8_t *)Addr;
146}
147
148boolSectionMemoryManager::finalizeMemory(std::string *ErrMsg) {
149// FIXME: Should in-progress permissions be reverted if an error occurs?
150 std::error_code ec;
151
152// Make code memory executable.
153 ec = applyMemoryGroupPermissions(CodeMem,
154sys::Memory::MF_READ |sys::Memory::MF_EXEC);
155if (ec) {
156if (ErrMsg) {
157 *ErrMsg = ec.message();
158 }
159returntrue;
160 }
161
162// Make read-only data memory read-only.
163 ec = applyMemoryGroupPermissions(RODataMem,sys::Memory::MF_READ);
164if (ec) {
165if (ErrMsg) {
166 *ErrMsg = ec.message();
167 }
168returntrue;
169 }
170
171// Read-write data memory already has the correct permissions
172
173// Some platforms with separate data cache and instruction cache require
174// explicit cache flush, otherwise JIT code manipulations (like resolved
175// relocations) will get to the data cache but not to the instruction cache.
176invalidateInstructionCache();
177
178returnfalse;
179}
180
181staticsys::MemoryBlocktrimBlockToPageSize(sys::MemoryBlock M) {
182staticconstsize_tPageSize =sys::Process::getPageSizeEstimate();
183
184size_t StartOverlap =
185 (PageSize - ((uintptr_t)M.base() %PageSize)) %PageSize;
186
187size_t TrimmedSize = M.allocatedSize();
188 TrimmedSize -= StartOverlap;
189 TrimmedSize -= TrimmedSize %PageSize;
190
191sys::MemoryBlock Trimmed((void *)((uintptr_t)M.base() + StartOverlap),
192 TrimmedSize);
193
194assert(((uintptr_t)Trimmed.base() %PageSize) == 0);
195assert((Trimmed.allocatedSize() %PageSize) == 0);
196assert(M.base() <= Trimmed.base() &&
197 Trimmed.allocatedSize() <= M.allocatedSize());
198
199return Trimmed;
200}
201
202std::error_code
203SectionMemoryManager::applyMemoryGroupPermissions(MemoryGroup &MemGroup,
204unsigned Permissions) {
205for (sys::MemoryBlock &MB : MemGroup.PendingMem)
206if (std::error_code EC = MMapper->protectMappedMemory(MB, Permissions))
207return EC;
208
209 MemGroup.PendingMem.clear();
210
211// Now go through free blocks and trim any of them that don't span the entire
212// page because one of the pending blocks may have overlapped it.
213for (FreeMemBlock &FreeMB : MemGroup.FreeMem) {
214 FreeMB.Free =trimBlockToPageSize(FreeMB.Free);
215// We cleared the PendingMem list, so all these pointers are now invalid
216 FreeMB.PendingPrefixIndex = (unsigned)-1;
217 }
218
219// Remove all blocks which are now empty
220erase_if(MemGroup.FreeMem, [](FreeMemBlock &FreeMB) {
221 return FreeMB.Free.allocatedSize() == 0;
222 });
223
224return std::error_code();
225}
226
227voidSectionMemoryManager::invalidateInstructionCache() {
228for (sys::MemoryBlock &Block : CodeMem.PendingMem)
229sys::Memory::InvalidateInstructionCache(Block.base(),
230Block.allocatedSize());
231}
232
233SectionMemoryManager::~SectionMemoryManager() {
234for (MemoryGroup *Group : {&CodeMem, &RWDataMem, &RODataMem}) {
235for (sys::MemoryBlock &Block : Group->AllocatedMem)
236 MMapper->releaseMappedMemory(Block);
237 }
238}
239
240SectionMemoryManager::MemoryMapper::~MemoryMapper() =default;
241
242void SectionMemoryManager::anchor() {}
243
244namespace{
245// Trivial implementation of SectionMemoryManager::MemoryMapper that just calls
246// into sys::Memory.
247classDefaultMMapper final :public SectionMemoryManager::MemoryMapper {
248public:
249 sys::MemoryBlock
250 allocateMappedMemory(SectionMemoryManager::AllocationPurpose Purpose,
251size_t NumBytes,const sys::MemoryBlock *const NearBlock,
252unsigned Flags, std::error_code &EC) override{
253returnsys::Memory::allocateMappedMemory(NumBytes, NearBlock, Flags, EC);
254 }
255
256 std::error_code protectMappedMemory(const sys::MemoryBlock &Block,
257unsigned Flags) override{
258returnsys::Memory::protectMappedMemory(Block, Flags);
259 }
260
261 std::error_code releaseMappedMemory(sys::MemoryBlock &M) override{
262returnsys::Memory::releaseMappedMemory(M);
263 }
264};
265}// namespace
266
267SectionMemoryManager::SectionMemoryManager(MemoryMapper *UnownedMM)
268 : MMapper(UnownedMM), OwnedMMapper(nullptr) {
269if (!MMapper) {
270 OwnedMMapper = std::make_unique<DefaultMMapper>();
271 MMapper = OwnedMMapper.get();
272 }
273}
274
275}// namespace llvm
Addr
uint64_t Addr
Definition:ELFObjHandler.cpp:79
Size
uint64_t Size
Definition:ELFObjHandler.cpp:81
PageSize
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)
Process.h
Provides a library for accessing information about this process and other processes on the operating ...
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
SectionMemoryManager.h
llvm::SectionMemoryManager::MemoryMapper
Implementations of this interface are used by SectionMemoryManager to request pages from the operatin...
Definition:SectionMemoryManager.h:51
llvm::SectionMemoryManager::MemoryMapper::protectMappedMemory
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.
llvm::SectionMemoryManager::MemoryMapper::~MemoryMapper
virtual ~MemoryMapper()
llvm::SectionMemoryManager::MemoryMapper::releaseMappedMemory
virtual std::error_code releaseMappedMemory(sys::MemoryBlock &M)=0
This method releases a block of memory that was allocated with the allocateMappedMemory method.
llvm::SectionMemoryManager::MemoryMapper::allocateMappedMemory
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.
llvm::SectionMemoryManager::AllocationPurpose
AllocationPurpose
This enum describes the various reasons to allocate pages from allocateMappedMemory.
Definition:SectionMemoryManager.h:43
llvm::SectionMemoryManager::AllocationPurpose::ROData
@ ROData
llvm::SectionMemoryManager::AllocationPurpose::RWData
@ RWData
llvm::SectionMemoryManager::AllocationPurpose::Code
@ Code
llvm::SectionMemoryManager::SectionMemoryManager
SectionMemoryManager(MemoryMapper *MM=nullptr)
Creates a SectionMemoryManager instance with MM as the associated memory mapper.
Definition:SectionMemoryManager.cpp:267
llvm::SectionMemoryManager::invalidateInstructionCache
virtual void invalidateInstructionCache()
Invalidate instruction cache for code sections.
Definition:SectionMemoryManager.cpp:227
llvm::SectionMemoryManager::~SectionMemoryManager
~SectionMemoryManager() override
Definition:SectionMemoryManager.cpp:233
llvm::SectionMemoryManager::allocateDataSection
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.
Definition:SectionMemoryManager.cpp:20
llvm::SectionMemoryManager::allocateCodeSection
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.
Definition:SectionMemoryManager.cpp:32
llvm::SectionMemoryManager::finalizeMemory
bool finalizeMemory(std::string *ErrMsg=nullptr) override
Update section-specific memory permissions and other attributes.
Definition:SectionMemoryManager.cpp:148
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::sys::MemoryBlock
This class encapsulates the notion of a memory block which has an address and a size.
Definition:Memory.h:32
llvm::sys::MemoryBlock::base
void * base() const
Definition:Memory.h:37
llvm::sys::MemoryBlock::allocatedSize
size_t allocatedSize() const
The size as it was allocated.
Definition:Memory.h:40
llvm::sys::Memory::releaseMappedMemory
static std::error_code releaseMappedMemory(MemoryBlock &Block)
This method releases a block of memory that was allocated with the allocateMappedMemory method.
llvm::sys::Memory::allocateMappedMemory
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....
llvm::sys::Memory::MF_READ
@ MF_READ
Definition:Memory.h:56
llvm::sys::Memory::MF_EXEC
@ MF_EXEC
Definition:Memory.h:58
llvm::sys::Memory::MF_WRITE
@ MF_WRITE
Definition:Memory.h:57
llvm::sys::Memory::InvalidateInstructionCache
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...
llvm::sys::Memory::protectMappedMemory
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.
llvm::sys::Process::getPageSizeEstimate
static unsigned getPageSizeEstimate()
Get the process's estimated page size.
Definition:Process.h:61
uint8_t
unsigned
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition:ErrorHandling.h:143
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::PseudoProbeType::Block
@ Block
llvm::trimBlockToPageSize
static sys::MemoryBlock trimBlockToPageSize(sys::MemoryBlock M)
Definition:SectionMemoryManager.cpp:181
llvm::erase_if
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition:STLExtras.h:2099
llvm::SectionName
Definition:DWARFSection.h:21

Generated on Fri Jul 18 2025 11:32:39 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp