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
31 struct AllocInfo {
32 struct SegInfo {
34 const char *WorkingMem;
38 };
39
41 std::vector<SegInfo> Segments;
43 };
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 virtual char *prepare(ExecutorAddr Addr, size_t ContentSize) = 0;
55
57
58 /// Ensures executor memory is synchronized with working copy memory, sends
59 /// functions to be called after initilization and before deinitialization and
60 /// applies memory protections
61 /// Returns a unique address identifying the allocation. This address should
62 /// be passed to deinitialize to run deallocation actions (and reset
63 /// permissions where possible).
64 virtual void initialize(AllocInfo &AI,
65 OnInitializedFunction OnInitialized) = 0;
66
68
69 /// Runs previously specified deinitialization actions
70 /// Executor addresses returned by initialize should be passed
71 virtual void deinitialize(ArrayRef<ExecutorAddr> Allocations,
72 OnDeinitializedFunction OnDeInitialized) = 0;
73
75
76 /// Release address space acquired through reserve()
77 virtual void release(ArrayRef<ExecutorAddr> Reservations,
78 OnReleasedFunction OnRelease) = 0;
79
80 virtual ~MemoryMapper();
81};
82
84public:
86
88
89 unsigned int getPageSize() override { return PageSize; }
90
91 void reserve(size_t NumBytes, OnReservedFunction OnReserved) override;
92
93 void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override;
94
95 char *prepare(ExecutorAddr Addr, size_t ContentSize) override;
96
97 void deinitialize(ArrayRef<ExecutorAddr> Allocations,
98 OnDeinitializedFunction OnDeInitialized) override;
99
100 void release(ArrayRef<ExecutorAddr> Reservations,
101 OnReleasedFunction OnRelease) override;
102
103 ~InProcessMemoryMapper() override;
104
105private:
106 struct Allocation {
107 size_t Size;
108 std::vector<shared::WrapperFunctionCall> DeinitializationActions;
109 };
110 using AllocationMap = DenseMap<ExecutorAddr, Allocation>;
111
112 struct Reservation {
113 size_t Size;
114 std::vector<ExecutorAddr> Allocations;
115 };
116 using ReservationMap = DenseMap<void *, Reservation>;
117
118 std::mutex Mutex;
119 ReservationMap Reservations;
120 AllocationMap Allocations;
121
122 size_t PageSize;
123};
124
126public:
127 struct SymbolAddrs {
133 };
134
136 size_t PageSize);
137
139 Create(ExecutorProcessControl &EPC, SymbolAddrs SAs);
140
141 unsigned int getPageSize() override { return PageSize; }
142
143 void reserve(size_t NumBytes, OnReservedFunction OnReserved) override;
144
145 char *prepare(ExecutorAddr Addr, size_t ContentSize) override;
146
147 void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override;
148
149 void deinitialize(ArrayRef<ExecutorAddr> Allocations,
150 OnDeinitializedFunction OnDeInitialized) override;
151
152 void release(ArrayRef<ExecutorAddr> Reservations,
153 OnReleasedFunction OnRelease) override;
154
155 ~SharedMemoryMapper() override;
156
157private:
158 struct Reservation {
159 void *LocalAddr;
160 size_t Size;
161 int SharedMemoryId;
162 };
163
164 ExecutorProcessControl &EPC;
165 SymbolAddrs SAs;
166
167 std::mutex Mutex;
168
169 std::map<ExecutorAddr, Reservation> Reservations;
170
171 size_t PageSize;
172};
173
174} // namespace orc
175} // end namespace llvm
176
177#endif // LLVM_EXECUTIONENGINE_ORC_MEMORYMAPPER_H
bbsections prepare
#define LLVM_ABI
Definition: Compiler.h:213
uint64_t Addr
uint64_t Size
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)
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.
Definition: MemoryFlags.h:110
Represents an address in the executor process.
ExecutorProcessControl supports interaction with a JIT target process.
unsigned int getPageSize() override
Definition: MemoryMapper.h:89
Manages mapping, content transfer and protections for JIT memory.
Definition: MemoryMapper.h:27
virtual char * prepare(ExecutorAddr Addr, size_t ContentSize)=0
Provides working memory.
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.
unsigned int getPageSize() override
Definition: MemoryMapper.h:141
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.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Summary of memprof metadata on allocations.
Represents a single allocation containing multiple segments and initialization and deinitialization a...
Definition: MemoryMapper.h:31
std::vector< SegInfo > Segments
Definition: MemoryMapper.h:41
shared::AllocActions Actions
Definition: MemoryMapper.h:42