LLVM 22.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 orc {
25
26/// Manages mapping, content transfer and protections for JIT memory
28public:
29 /// Represents a single allocation containing multiple segments and
30 /// initialization and deinitialization actions
44
46
47 // Page size of the target process
48 virtual unsigned int getPageSize() = 0;
49
50 /// Reserves address space in executor process
51 virtual void reserve(size_t NumBytes, OnReservedFunction OnReserved) = 0;
52
53 /// Provides working memory
54 /// The LinkGraph parameter is included to allow implementations to allocate
55 /// working memory from the LinkGraph's allocator, in which case it will be
56 /// deallocated when the LinkGraph is destroyed.
58 size_t ContentSize) = 0;
59
61
62 /// Ensures executor memory is synchronized with working copy memory, sends
63 /// functions to be called after initilization and before deinitialization and
64 /// applies memory protections
65 /// Returns a unique address identifying the allocation. This address should
66 /// be passed to deinitialize to run deallocation actions (and reset
67 /// permissions where possible).
68 virtual void initialize(AllocInfo &AI,
69 OnInitializedFunction OnInitialized) = 0;
70
72
73 /// Runs previously specified deinitialization actions
74 /// Executor addresses returned by initialize should be passed
75 virtual void deinitialize(ArrayRef<ExecutorAddr> Allocations,
76 OnDeinitializedFunction OnDeInitialized) = 0;
77
79
80 /// Release address space acquired through reserve()
81 virtual void release(ArrayRef<ExecutorAddr> Reservations,
82 OnReleasedFunction OnRelease) = 0;
83
84 virtual ~MemoryMapper();
85};
86
88public:
89 InProcessMemoryMapper(size_t PageSize);
90
92
93 unsigned int getPageSize() override { return PageSize; }
94
95 void reserve(size_t NumBytes, OnReservedFunction OnReserved) override;
96
97 void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override;
98
100 size_t ContentSize) override;
101
102 void deinitialize(ArrayRef<ExecutorAddr> Allocations,
103 OnDeinitializedFunction OnDeInitialized) override;
104
105 void release(ArrayRef<ExecutorAddr> Reservations,
106 OnReleasedFunction OnRelease) override;
107
108 ~InProcessMemoryMapper() override;
109
110private:
111 struct Allocation {
112 size_t Size;
113 std::vector<shared::WrapperFunctionCall> DeinitializationActions;
114 };
115 using AllocationMap = DenseMap<ExecutorAddr, Allocation>;
116
117 struct Reservation {
118 size_t Size;
119 std::vector<ExecutorAddr> Allocations;
120 };
121 using ReservationMap = DenseMap<void *, Reservation>;
122
123 std::mutex Mutex;
124 ReservationMap Reservations;
125 AllocationMap Allocations;
126
127 size_t PageSize;
128};
129
131public:
139
141 size_t PageSize);
142
145
146 unsigned int getPageSize() override { return PageSize; }
147
148 void reserve(size_t NumBytes, OnReservedFunction OnReserved) override;
149
151 size_t ContentSize) override;
152
153 void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override;
154
155 void deinitialize(ArrayRef<ExecutorAddr> Allocations,
156 OnDeinitializedFunction OnDeInitialized) override;
157
158 void release(ArrayRef<ExecutorAddr> Reservations,
159 OnReleasedFunction OnRelease) override;
160
161 ~SharedMemoryMapper() override;
162
163private:
164 struct Reservation {
165 void *LocalAddr;
166 size_t Size;
167 int SharedMemoryId;
168 };
169
170 ExecutorProcessControl &EPC;
171 SymbolAddrs SAs;
172
173 std::mutex Mutex;
174
175 std::map<ExecutorAddr, Reservation> Reservations;
176
177 size_t PageSize;
178};
179
180} // namespace orc
181} // end namespace llvm
182
183#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:56
Provides a library for accessing information about this process and other processes on the operating ...
static void initialize(TargetLibraryInfoImpl &TLI, const Triple &T, ArrayRef< StringLiteral > StandardNames)
Initialize the set of available library functions based on the specified target triple.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
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