1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
#pragma once
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif
//===- MemoryMapper.h - Cross-process memory mapper -------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Cross-process (and in-process) memory mapping and transfer
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_EXECUTIONENGINE_ORC_MEMORYMAPPER_H
#define LLVM_EXECUTIONENGINE_ORC_MEMORYMAPPER_H
#include "llvm/ExecutionEngine/Orc/Core.h"
#include "llvm/ExecutionEngine/Orc/Shared/MemoryFlags.h"
#include "llvm/Support/Process.h"
#include <mutex>
namespace llvm {
namespace orc {
/// Manages mapping, content transfer and protections for JIT memory
class MemoryMapper {
public:
/// Represents a single allocation containing multiple segments and
/// initialization and deinitialization actions
struct AllocInfo {
struct SegInfo {
ExecutorAddrDiff Offset;
const char *WorkingMem;
size_t ContentSize;
size_t ZeroFillSize;
AllocGroup AG;
};
ExecutorAddr MappingBase;
std::vector<SegInfo> Segments;
shared::AllocActions Actions;
};
using OnReservedFunction = unique_function<void(Expected<ExecutorAddrRange>)>;
// Page size of the target process
virtual unsigned int getPageSize() = 0;
/// Reserves address space in executor process
virtual void reserve(size_t NumBytes, OnReservedFunction OnReserved) = 0;
/// Provides working memory
virtual char *prepare(ExecutorAddr Addr, size_t ContentSize) = 0;
using OnInitializedFunction = unique_function<void(Expected<ExecutorAddr>)>;
/// Ensures executor memory is synchronized with working copy memory, sends
/// functions to be called after initilization and before deinitialization and
/// applies memory protections
/// Returns a unique address identifying the allocation. This address should
/// be passed to deinitialize to run deallocation actions (and reset
/// permissions where possible).
virtual void initialize(AllocInfo &AI,
OnInitializedFunction OnInitialized) = 0;
using OnDeinitializedFunction = unique_function<void(Error)>;
/// Runs previously specified deinitialization actions
/// Executor addresses returned by initialize should be passed
virtual void deinitialize(ArrayRef<ExecutorAddr> Allocations,
OnDeinitializedFunction OnDeInitialized) = 0;
using OnReleasedFunction = unique_function<void(Error)>;
/// Release address space acquired through reserve()
virtual void release(ArrayRef<ExecutorAddr> Reservations,
OnReleasedFunction OnRelease) = 0;
virtual ~MemoryMapper();
};
class InProcessMemoryMapper : public MemoryMapper {
public:
InProcessMemoryMapper(size_t PageSize);
static Expected<std::unique_ptr<InProcessMemoryMapper>> Create();
unsigned int getPageSize() override { return PageSize; }
void reserve(size_t NumBytes, OnReservedFunction OnReserved) override;
void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override;
char *prepare(ExecutorAddr Addr, size_t ContentSize) override;
void deinitialize(ArrayRef<ExecutorAddr> Allocations,
OnDeinitializedFunction OnDeInitialized) override;
void release(ArrayRef<ExecutorAddr> Reservations,
OnReleasedFunction OnRelease) override;
~InProcessMemoryMapper() override;
private:
struct Allocation {
size_t Size;
std::vector<shared::WrapperFunctionCall> DeinitializationActions;
};
using AllocationMap = DenseMap<ExecutorAddr, Allocation>;
struct Reservation {
size_t Size;
std::vector<ExecutorAddr> Allocations;
};
using ReservationMap = DenseMap<void *, Reservation>;
std::mutex Mutex;
ReservationMap Reservations;
AllocationMap Allocations;
size_t PageSize;
};
class SharedMemoryMapper final : public MemoryMapper {
public:
struct SymbolAddrs {
ExecutorAddr Instance;
ExecutorAddr Reserve;
ExecutorAddr Initialize;
ExecutorAddr Deinitialize;
ExecutorAddr Release;
};
SharedMemoryMapper(ExecutorProcessControl &EPC, SymbolAddrs SAs,
size_t PageSize);
static Expected<std::unique_ptr<SharedMemoryMapper>>
Create(ExecutorProcessControl &EPC, SymbolAddrs SAs);
unsigned int getPageSize() override { return PageSize; }
void reserve(size_t NumBytes, OnReservedFunction OnReserved) override;
char *prepare(ExecutorAddr Addr, size_t ContentSize) override;
void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override;
void deinitialize(ArrayRef<ExecutorAddr> Allocations,
OnDeinitializedFunction OnDeInitialized) override;
void release(ArrayRef<ExecutorAddr> Reservations,
OnReleasedFunction OnRelease) override;
~SharedMemoryMapper() override;
private:
struct Reservation {
void *LocalAddr;
size_t Size;
};
ExecutorProcessControl &EPC;
SymbolAddrs SAs;
std::mutex Mutex;
std::map<ExecutorAddr, Reservation> Reservations;
size_t PageSize;
};
} // namespace orc
} // end namespace llvm
#endif // LLVM_EXECUTIONENGINE_ORC_MEMORYMAPPER_H
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
|