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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
//===---------- ExecutorSharedMemoryMapperService.cpp -----------*- 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
//
//===----------------------------------------------------------------------===//
#include "llvm/ExecutionEngine/Orc/TargetProcess/ExecutorSharedMemoryMapperService.h"
#include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/WindowsError.h"
#include <sstream>
#if defined(LLVM_ON_UNIX)
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#endif
namespace llvm {
namespace orc {
namespace rt_bootstrap {
#if defined(_WIN32)
static DWORD getWindowsProtectionFlags(MemProt MP) {
if (MP == MemProt::Read)
return PAGE_READONLY;
if (MP == MemProt::Write ||
MP == (MemProt::Write | MemProt::Read)) {
// Note: PAGE_WRITE is not supported by VirtualProtect
return PAGE_READWRITE;
}
if (MP == (MemProt::Read | MemProt::Exec))
return PAGE_EXECUTE_READ;
if (MP == (MemProt::Read | MemProt::Write | MemProt::Exec))
return PAGE_EXECUTE_READWRITE;
if (MP == MemProt::Exec)
return PAGE_EXECUTE;
return PAGE_NOACCESS;
}
#endif
Expected<std::pair<ExecutorAddr, std::string>>
ExecutorSharedMemoryMapperService::reserve(uint64_t Size) {
#if (defined(LLVM_ON_UNIX) && !defined(__ANDROID__)) || defined(_WIN32)
#if defined(LLVM_ON_UNIX)
std::string SharedMemoryName;
{
std::stringstream SharedMemoryNameStream;
SharedMemoryNameStream << "/jitlink_" << sys::Process::getProcessId() << '_'
<< (++SharedMemoryCount);
SharedMemoryName = SharedMemoryNameStream.str();
}
int SharedMemoryFile =
shm_open(SharedMemoryName.c_str(), O_RDWR | O_CREAT | O_EXCL, 0700);
if (SharedMemoryFile < 0)
return errorCodeToError(std::error_code(errno, std::generic_category()));
// by default size is 0
if (ftruncate(SharedMemoryFile, Size) < 0)
return errorCodeToError(std::error_code(errno, std::generic_category()));
void *Addr = mmap(nullptr, Size, PROT_NONE, MAP_SHARED, SharedMemoryFile, 0);
if (Addr == MAP_FAILED)
return errorCodeToError(std::error_code(errno, std::generic_category()));
close(SharedMemoryFile);
#elif defined(_WIN32)
std::string SharedMemoryName;
{
std::stringstream SharedMemoryNameStream;
SharedMemoryNameStream << "jitlink_" << sys::Process::getProcessId() << '_'
<< (++SharedMemoryCount);
SharedMemoryName = SharedMemoryNameStream.str();
}
std::wstring WideSharedMemoryName(SharedMemoryName.begin(),
SharedMemoryName.end());
HANDLE SharedMemoryFile = CreateFileMappingW(
INVALID_HANDLE_VALUE, NULL, PAGE_EXECUTE_READWRITE, Size >> 32,
Size & 0xffffffff, WideSharedMemoryName.c_str());
if (!SharedMemoryFile)
return errorCodeToError(mapWindowsError(GetLastError()));
void *Addr = MapViewOfFile(SharedMemoryFile,
FILE_MAP_ALL_ACCESS | FILE_MAP_EXECUTE, 0, 0, 0);
if (!Addr) {
CloseHandle(SharedMemoryFile);
return errorCodeToError(mapWindowsError(GetLastError()));
}
#endif
{
std::lock_guard<std::mutex> Lock(Mutex);
Reservations[Addr].Size = Size;
#if defined(_WIN32)
Reservations[Addr].SharedMemoryFile = SharedMemoryFile;
#endif
}
return std::make_pair(ExecutorAddr::fromPtr(Addr),
std::move(SharedMemoryName));
#else
return make_error<StringError>(
"SharedMemoryMapper is not supported on this platform yet",
inconvertibleErrorCode());
#endif
}
Expected<ExecutorAddr> ExecutorSharedMemoryMapperService::initialize(
ExecutorAddr Reservation, tpctypes::SharedMemoryFinalizeRequest &FR) {
#if (defined(LLVM_ON_UNIX) && !defined(__ANDROID__)) || defined(_WIN32)
ExecutorAddr MinAddr(~0ULL);
// Contents are already in place
for (auto &Segment : FR.Segments) {
if (Segment.Addr < MinAddr)
MinAddr = Segment.Addr;
#if defined(LLVM_ON_UNIX)
int NativeProt = 0;
if ((Segment.AG.getMemProt() & MemProt::Read) == MemProt::Read)
NativeProt |= PROT_READ;
if ((Segment.AG.getMemProt() & MemProt::Write) == MemProt::Write)
NativeProt |= PROT_WRITE;
if ((Segment.AG.getMemProt() & MemProt::Exec) == MemProt::Exec)
NativeProt |= PROT_EXEC;
if (mprotect(Segment.Addr.toPtr<void *>(), Segment.Size, NativeProt))
return errorCodeToError(std::error_code(errno, std::generic_category()));
#elif defined(_WIN32)
DWORD NativeProt =
getWindowsProtectionFlags(Segment.AG.getMemProt());
if (!VirtualProtect(Segment.Addr.toPtr<void *>(), Segment.Size, NativeProt,
&NativeProt))
return errorCodeToError(mapWindowsError(GetLastError()));
#endif
if ((Segment.AG.getMemProt() & MemProt::Exec) == MemProt::Exec)
sys::Memory::InvalidateInstructionCache(Segment.Addr.toPtr<void *>(),
Segment.Size);
}
// Run finalization actions and get deinitlization action list.
auto DeinitializeActions = shared::runFinalizeActions(FR.Actions);
if (!DeinitializeActions) {
return DeinitializeActions.takeError();
}
{
std::lock_guard<std::mutex> Lock(Mutex);
Allocations[MinAddr].DeinitializationActions =
std::move(*DeinitializeActions);
Reservations[Reservation.toPtr<void *>()].Allocations.push_back(MinAddr);
}
return MinAddr;
#else
return make_error<StringError>(
"SharedMemoryMapper is not supported on this platform yet",
inconvertibleErrorCode());
#endif
}
Error ExecutorSharedMemoryMapperService::deinitialize(
const std::vector<ExecutorAddr> &Bases) {
Error AllErr = Error::success();
{
std::lock_guard<std::mutex> Lock(Mutex);
for (auto Base : llvm::reverse(Bases)) {
if (Error Err = shared::runDeallocActions(
Allocations[Base].DeinitializationActions)) {
AllErr = joinErrors(std::move(AllErr), std::move(Err));
}
// Remove the allocation from the allocation list of its reservation
for (auto &Reservation : Reservations) {
auto AllocationIt =
std::find(Reservation.second.Allocations.begin(),
Reservation.second.Allocations.end(), Base);
if (AllocationIt != Reservation.second.Allocations.end()) {
Reservation.second.Allocations.erase(AllocationIt);
break;
}
}
Allocations.erase(Base);
}
}
return AllErr;
}
Error ExecutorSharedMemoryMapperService::release(
const std::vector<ExecutorAddr> &Bases) {
#if (defined(LLVM_ON_UNIX) && !defined(__ANDROID__)) || defined(_WIN32)
Error Err = Error::success();
for (auto Base : Bases) {
std::vector<ExecutorAddr> AllocAddrs;
size_t Size;
#if defined(_WIN32)
HANDLE SharedMemoryFile;
#endif
{
std::lock_guard<std::mutex> Lock(Mutex);
auto &R = Reservations[Base.toPtr<void *>()];
Size = R.Size;
#if defined(_WIN32)
SharedMemoryFile = R.SharedMemoryFile;
#endif
AllocAddrs.swap(R.Allocations);
}
// deinitialize sub allocations
if (Error E = deinitialize(AllocAddrs))
Err = joinErrors(std::move(Err), std::move(E));
#if defined(LLVM_ON_UNIX)
if (munmap(Base.toPtr<void *>(), Size) != 0)
Err = joinErrors(std::move(Err), errorCodeToError(std::error_code(
errno, std::generic_category())));
#elif defined(_WIN32)
(void)Size;
if (!UnmapViewOfFile(Base.toPtr<void *>()))
Err = joinErrors(std::move(Err),
errorCodeToError(mapWindowsError(GetLastError())));
CloseHandle(SharedMemoryFile);
#endif
std::lock_guard<std::mutex> Lock(Mutex);
Reservations.erase(Base.toPtr<void *>());
}
return Err;
#else
return make_error<StringError>(
"SharedMemoryMapper is not supported on this platform yet",
inconvertibleErrorCode());
#endif
}
Error ExecutorSharedMemoryMapperService::shutdown() {
if (Reservations.empty())
return Error::success();
std::vector<ExecutorAddr> ReservationAddrs;
ReservationAddrs.reserve(Reservations.size());
for (const auto &R : Reservations)
ReservationAddrs.push_back(ExecutorAddr::fromPtr(R.getFirst()));
return release(std::move(ReservationAddrs));
}
void ExecutorSharedMemoryMapperService::addBootstrapSymbols(
StringMap<ExecutorAddr> &M) {
M[rt::ExecutorSharedMemoryMapperServiceInstanceName] =
ExecutorAddr::fromPtr(this);
M[rt::ExecutorSharedMemoryMapperServiceReserveWrapperName] =
ExecutorAddr::fromPtr(&reserveWrapper);
M[rt::ExecutorSharedMemoryMapperServiceInitializeWrapperName] =
ExecutorAddr::fromPtr(&initializeWrapper);
M[rt::ExecutorSharedMemoryMapperServiceDeinitializeWrapperName] =
ExecutorAddr::fromPtr(&deinitializeWrapper);
M[rt::ExecutorSharedMemoryMapperServiceReleaseWrapperName] =
ExecutorAddr::fromPtr(&releaseWrapper);
}
llvm::orc::shared::CWrapperFunctionResult
ExecutorSharedMemoryMapperService::reserveWrapper(const char *ArgData,
size_t ArgSize) {
return shared::WrapperFunction<
rt::SPSExecutorSharedMemoryMapperServiceReserveSignature>::
handle(ArgData, ArgSize,
shared::makeMethodWrapperHandler(
&ExecutorSharedMemoryMapperService::reserve))
.release();
}
llvm::orc::shared::CWrapperFunctionResult
ExecutorSharedMemoryMapperService::initializeWrapper(const char *ArgData,
size_t ArgSize) {
return shared::WrapperFunction<
rt::SPSExecutorSharedMemoryMapperServiceInitializeSignature>::
handle(ArgData, ArgSize,
shared::makeMethodWrapperHandler(
&ExecutorSharedMemoryMapperService::initialize))
.release();
}
llvm::orc::shared::CWrapperFunctionResult
ExecutorSharedMemoryMapperService::deinitializeWrapper(const char *ArgData,
size_t ArgSize) {
return shared::WrapperFunction<
rt::SPSExecutorSharedMemoryMapperServiceDeinitializeSignature>::
handle(ArgData, ArgSize,
shared::makeMethodWrapperHandler(
&ExecutorSharedMemoryMapperService::deinitialize))
.release();
}
llvm::orc::shared::CWrapperFunctionResult
ExecutorSharedMemoryMapperService::releaseWrapper(const char *ArgData,
size_t ArgSize) {
return shared::WrapperFunction<
rt::SPSExecutorSharedMemoryMapperServiceReleaseSignature>::
handle(ArgData, ArgSize,
shared::makeMethodWrapperHandler(
&ExecutorSharedMemoryMapperService::release))
.release();
}
} // namespace rt_bootstrap
} // end namespace orc
} // end namespace llvm
|