LLVM 23.0.0git
MemoryMapper.h
Go to the documentation of this file.
1//===- MemoryMapper.h - Cross-process memory mapper -------------*- 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// Cross-process (and in-process) memory mapping and transfer
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_EXECUTIONENGINE_ORC_MEMORYMAPPER_H
14#define LLVM_EXECUTIONENGINE_ORC_MEMORYMAPPER_H
15
20
21#include <mutex>
22
23namespace llvm {
24namespace jitlink {
25class LinkGraph;
26} // namespace jitlink
27
28namespace orc {
29
30/// Manages mapping, content transfer and protections for JIT memory
32public:
33 /// Represents a single allocation containing multiple segments and
34 /// initialization and deinitialization actions
48
50
51 // Page size of the target process
52 virtual unsigned int getPageSize() = 0;
53
54 /// Reserves address space in executor process
55 virtual void reserve(size_t NumBytes, OnReservedFunction OnReserved) = 0;
56
57 /// Provides working memory
58 /// The LinkGraph parameter is included to allow implementations to allocate
59 /// working memory from the LinkGraph's allocator, in which case it will be
60 /// deallocated when the LinkGraph is destroyed.
62 size_t ContentSize) = 0;
63
65
66 /// Ensures executor memory is synchronized with working copy memory, sends
67 /// functions to be called after initilization and before deinitialization and
68 /// applies memory protections
69 /// Returns a unique address identifying the allocation. This address should
70 /// be passed to deinitialize to run deallocation actions (and reset
71 /// permissions where possible).
72 virtual void initialize(AllocInfo &AI,
73 OnInitializedFunction OnInitialized) = 0;
74
76
77 /// Runs previously specified deinitialization actions
78 /// Executor addresses returned by initialize should be passed
79 virtual void deinitialize(ArrayRef<ExecutorAddr> Allocations,
80 OnDeinitializedFunction OnDeInitialized) = 0;
81
83
84 /// Release address space acquired through reserve()
85 virtual void release(ArrayRef<ExecutorAddr> Reservations,
86 OnReleasedFunction OnRelease) = 0;
87
88 virtual ~MemoryMapper();
89};
90
92public:
93 InProcessMemoryMapper(size_t PageSize);
94
96
97 unsigned int getPageSize() override { return PageSize; }
98
99 void reserve(size_t NumBytes, OnReservedFunction OnReserved) override;
100
101 void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override;
102
104 size_t ContentSize) override;
105
106 void deinitialize(ArrayRef<ExecutorAddr> Allocations,
107 OnDeinitializedFunction OnDeInitialized) override;
108
109 void release(ArrayRef<ExecutorAddr> Reservations,
110 OnReleasedFunction OnRelease) override;
111
112 ~InProcessMemoryMapper() override;
113
114private:
115 struct Allocation {
116 size_t Size;
117 std::vector<shared::WrapperFunctionCall> DeinitializationActions;
118 };
119 using AllocationMap = DenseMap<ExecutorAddr, Allocation>;
120
121 struct Reservation {
122 size_t Size;
123 std::vector<ExecutorAddr> Allocations;
124 };
125 using ReservationMap = DenseMap<void *, Reservation>;
126
127 std::mutex Mutex;
128 ReservationMap Reservations;
129 AllocationMap Allocations;
130
131 size_t PageSize;
132};
133
135public:
143
145 size_t PageSize);
146
149
150 unsigned int getPageSize() override { return PageSize; }
151
152 void reserve(size_t NumBytes, OnReservedFunction OnReserved) override;
153
155 size_t ContentSize) override;
156
157 void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override;
158
159 void deinitialize(ArrayRef<ExecutorAddr> Allocations,
160 OnDeinitializedFunction OnDeInitialized) override;
161
162 void release(ArrayRef<ExecutorAddr> Reservations,
163 OnReleasedFunction OnRelease) override;
164
165 ~SharedMemoryMapper() override;
166
167private:
168 struct Reservation {
169 void *LocalAddr;
170 size_t Size;
171 int SharedMemoryId;
172 };
173
174 ExecutorProcessControl &EPC;
175 SymbolAddrs SAs;
176
177 std::mutex Mutex;
178
179 std::map<ExecutorAddr, Reservation> Reservations;
180
181 size_t PageSize;
182};
183
184} // namespace orc
185} // end namespace llvm
186
187#endif // LLVM_EXECUTIONENGINE_ORC_MEMORYMAPPER_H
bbsections prepare
#define LLVM_ABI
Definition Compiler.h:213
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)
#define G(x, y, z)
Definition MD5.cpp:55
Provides a library for accessing information about this process and other processes on the operating ...
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, const llvm::StringTable &StandardNames, VectorLibrary VecLib)
Initialize the set of available library functions based on the specified target triple.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
A pair of memory protections and allocation policies.
Represents an address in the executor process.
ExecutorProcessControl supports interaction with a JIT target process.
static Expected< std::unique_ptr< InProcessMemoryMapper > > Create()
unsigned int getPageSize() override
Manages mapping, content transfer and protections for JIT memory.
unique_function< void(Error)> OnReleasedFunction
virtual char * prepare(jitlink::LinkGraph &G, ExecutorAddr Addr, size_t ContentSize)=0
Provides working memory The LinkGraph parameter is included to allow implementations to allocate work...
virtual unsigned int getPageSize()=0
virtual void release(ArrayRef< ExecutorAddr > Reservations, OnReleasedFunction OnRelease)=0
Release address space acquired through reserve()
virtual void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized)=0
Ensures executor memory is synchronized with working copy memory, sends functions to be called after ...
virtual void deinitialize(ArrayRef< ExecutorAddr > Allocations, OnDeinitializedFunction OnDeInitialized)=0
Runs previously specified deinitialization actions Executor addresses returned by initialize should b...
virtual void reserve(size_t NumBytes, OnReservedFunction OnReserved)=0
Reserves address space in executor process.
unique_function< void(Expected< ExecutorAddr >)> OnInitializedFunction
unique_function< void(Expected< ExecutorAddrRange >)> OnReservedFunction
unique_function< void(Error)> OnDeinitializedFunction
static Expected< std::unique_ptr< SharedMemoryMapper > > Create(ExecutorProcessControl &EPC, SymbolAddrs SAs)
SharedMemoryMapper(ExecutorProcessControl &EPC, SymbolAddrs SAs, size_t PageSize)
unsigned int getPageSize() override
unique_function is a type-erasing functor similar to std::function.
std::vector< AllocActionCallPair > AllocActions
A vector of allocation actions to be run for this allocation.
uint64_t ExecutorAddrDiff
SmartMutex< false > Mutex
Mutex - A standard, always enforced mutex.
Definition Mutex.h:66
This is an optimization pass for GlobalISel generic memory operations.
Summary of memprof metadata on allocations.
Represents a single allocation containing multiple segments and initialization and deinitialization a...
std::vector< SegInfo > Segments