LLVM 17.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
19
20#include <mutex>
21
22namespace llvm {
23namespace orc {
24
25/// Manages mapping, content transfer and protections for JIT memory
27public:
28 /// Represents a single allocation containing multiple segments and
29 /// initialization and deinitialization actions
30 struct AllocInfo {
31 struct SegInfo {
33 const char *WorkingMem;
37 };
38
40 std::vector<SegInfo> Segments;
42 };
43
45
46 // Page size of the target process
47 virtual unsigned int getPageSize() = 0;
48
49 /// Reserves address space in executor process
50 virtual void reserve(size_t NumBytes, OnReservedFunction OnReserved) = 0;
51
52 /// Provides working memory
53 virtual char *prepare(ExecutorAddr Addr, size_t ContentSize) = 0;
54
56
57 /// Ensures executor memory is synchronized with working copy memory, sends
58 /// functions to be called after initilization and before deinitialization and
59 /// applies memory protections
60 /// Returns a unique address identifying the allocation. This address should
61 /// be passed to deinitialize to run deallocation actions (and reset
62 /// permissions where possible).
63 virtual void initialize(AllocInfo &AI,
64 OnInitializedFunction OnInitialized) = 0;
65
67
68 /// Runs previously specified deinitialization actions
69 /// Executor addresses returned by initialize should be passed
70 virtual void deinitialize(ArrayRef<ExecutorAddr> Allocations,
71 OnDeinitializedFunction OnDeInitialized) = 0;
72
74
75 /// Release address space acquired through reserve()
76 virtual void release(ArrayRef<ExecutorAddr> Reservations,
77 OnReleasedFunction OnRelease) = 0;
78
79 virtual ~MemoryMapper();
80};
81
83public:
84 InProcessMemoryMapper(size_t PageSize);
85
87
88 unsigned int getPageSize() override { return PageSize; }
89
90 void reserve(size_t NumBytes, OnReservedFunction OnReserved) override;
91
92 void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override;
93
94 char *prepare(ExecutorAddr Addr, size_t ContentSize) override;
95
96 void deinitialize(ArrayRef<ExecutorAddr> Allocations,
97 OnDeinitializedFunction OnDeInitialized) override;
98
99 void release(ArrayRef<ExecutorAddr> Reservations,
100 OnReleasedFunction OnRelease) override;
101
102 ~InProcessMemoryMapper() override;
103
104private:
105 struct Allocation {
106 size_t Size;
107 std::vector<shared::WrapperFunctionCall> DeinitializationActions;
108 };
109 using AllocationMap = DenseMap<ExecutorAddr, Allocation>;
110
111 struct Reservation {
112 size_t Size;
113 std::vector<ExecutorAddr> Allocations;
114 };
115 using ReservationMap = DenseMap<void *, Reservation>;
116
117 std::mutex Mutex;
118 ReservationMap Reservations;
119 AllocationMap Allocations;
120
121 size_t PageSize;
122};
123
124class SharedMemoryMapper final : public MemoryMapper {
125public:
126 struct SymbolAddrs {
132 };
133
135 size_t PageSize);
136
139
140 unsigned int getPageSize() override { return PageSize; }
141
142 void reserve(size_t NumBytes, OnReservedFunction OnReserved) override;
143
144 char *prepare(ExecutorAddr Addr, size_t ContentSize) override;
145
146 void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override;
147
148 void deinitialize(ArrayRef<ExecutorAddr> Allocations,
149 OnDeinitializedFunction OnDeInitialized) override;
150
151 void release(ArrayRef<ExecutorAddr> Reservations,
152 OnReleasedFunction OnRelease) override;
153
154 ~SharedMemoryMapper() override;
155
156private:
157 struct Reservation {
158 void *LocalAddr;
159 size_t Size;
160 };
161
162 ExecutorProcessControl &EPC;
163 SymbolAddrs SAs;
164
165 std::mutex Mutex;
166
167 std::map<ExecutorAddr, Reservation> Reservations;
168
169 size_t PageSize;
170};
171
172} // namespace orc
173} // end namespace llvm
174
175#endif // LLVM_EXECUTIONENGINE_ORC_MEMORYMAPPER_H
uint64_t Addr
Provides a library for accessing information about this process and other processes on the operating ...
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:156
Tagged union holding either a T or a Error.
Definition: Error.h:470
A pair of memory protections and allocation policies.
Definition: MemoryFlags.h:93
Represents an address in the executor process.
ExecutorProcessControl supports interaction with a JIT target process.
void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override
Ensures executor memory is synchronized with working copy memory, sends functions to be called after ...
void reserve(size_t NumBytes, OnReservedFunction OnReserved) override
Reserves address space in executor process.
void deinitialize(ArrayRef< ExecutorAddr > Allocations, OnDeinitializedFunction OnDeInitialized) override
Runs previously specified deinitialization actions Executor addresses returned by initialize should b...
static Expected< std::unique_ptr< InProcessMemoryMapper > > Create()
char * prepare(ExecutorAddr Addr, size_t ContentSize) override
Provides working memory.
unsigned int getPageSize() override
Definition: MemoryMapper.h:88
Manages mapping, content transfer and protections for JIT memory.
Definition: MemoryMapper.h:26
unique_function< void(Error)> OnReleasedFunction
Definition: MemoryMapper.h:73
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.
unique_function< void(Expected< ExecutorAddr >)> OnInitializedFunction
Definition: MemoryMapper.h:55
unique_function< void(Expected< ExecutorAddrRange >)> OnReservedFunction
Definition: MemoryMapper.h:44
unique_function< void(Error)> OnDeinitializedFunction
Definition: MemoryMapper.h:66
static Expected< std::unique_ptr< SharedMemoryMapper > > Create(ExecutorProcessControl &EPC, SymbolAddrs SAs)
void reserve(size_t NumBytes, OnReservedFunction OnReserved) override
Reserves address space in executor process.
void deinitialize(ArrayRef< ExecutorAddr > Allocations, OnDeinitializedFunction OnDeInitialized) override
Runs previously specified deinitialization actions Executor addresses returned by initialize should b...
void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override
Ensures executor memory is synchronized with working copy memory, sends functions to be called after ...
char * prepare(ExecutorAddr Addr, size_t ContentSize) override
Provides working memory.
unsigned int getPageSize() override
Definition: MemoryMapper.h:140
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:30
std::vector< SegInfo > Segments
Definition: MemoryMapper.h:40
shared::AllocActions Actions
Definition: MemoryMapper.h:41