LLVM 20.0.0git
ExecutorSharedMemoryMapperService.cpp
Go to the documentation of this file.
1//===---------- ExecutorSharedMemoryMapperService.cpp -----------*- 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
10#include "llvm/Config/llvm-config.h" // for LLVM_ON_UNIX
14#include <sstream>
15
16#if defined(LLVM_ON_UNIX)
17#include <errno.h>
18#include <fcntl.h>
19#include <sys/mman.h>
20#if defined(__MVS__)
21#include "llvm/Support/BLAKE3.h"
22#include <sys/shm.h>
23#endif
24#include <unistd.h>
25#endif
26
27namespace llvm {
28namespace orc {
29namespace rt_bootstrap {
30
31#if defined(_WIN32)
32static DWORD getWindowsProtectionFlags(MemProt MP) {
33 if (MP == MemProt::Read)
34 return PAGE_READONLY;
35 if (MP == MemProt::Write ||
36 MP == (MemProt::Write | MemProt::Read)) {
37 // Note: PAGE_WRITE is not supported by VirtualProtect
38 return PAGE_READWRITE;
39 }
40 if (MP == (MemProt::Read | MemProt::Exec))
41 return PAGE_EXECUTE_READ;
43 return PAGE_EXECUTE_READWRITE;
44 if (MP == MemProt::Exec)
45 return PAGE_EXECUTE;
46
47 return PAGE_NOACCESS;
48}
49#endif
50
51Expected<std::pair<ExecutorAddr, std::string>>
53#if (defined(LLVM_ON_UNIX) && !defined(__ANDROID__)) || defined(_WIN32)
54
55#if defined(LLVM_ON_UNIX)
56
57 std::string SharedMemoryName;
58 {
59 std::stringstream SharedMemoryNameStream;
60 SharedMemoryNameStream << "/jitlink_" << sys::Process::getProcessId() << '_'
61 << (++SharedMemoryCount);
62 SharedMemoryName = SharedMemoryNameStream.str();
63 }
64
65#if defined(__MVS__)
67 reinterpret_cast<const uint8_t *>(SharedMemoryName.c_str()),
68 SharedMemoryName.size());
69 auto HashedName = BLAKE3::hash<sizeof(key_t)>(Data);
70 key_t Key = *reinterpret_cast<key_t *>(HashedName.data());
71 int SharedMemoryId =
72 shmget(Key, Size, IPC_CREAT | IPC_EXCL | __IPC_SHAREAS | 0700);
73 if (SharedMemoryId < 0)
75
76 void *Addr = shmat(SharedMemoryId, nullptr, 0);
77 if (Addr == reinterpret_cast<void *>(-1))
79#else
80 int SharedMemoryFile =
81 shm_open(SharedMemoryName.c_str(), O_RDWR | O_CREAT | O_EXCL, 0700);
82 if (SharedMemoryFile < 0)
84
85 // by default size is 0
86 if (ftruncate(SharedMemoryFile, Size) < 0)
88
89 void *Addr = mmap(nullptr, Size, PROT_NONE, MAP_SHARED, SharedMemoryFile, 0);
90 if (Addr == MAP_FAILED)
92
93 close(SharedMemoryFile);
94#endif
95
96#elif defined(_WIN32)
97
98 std::string SharedMemoryName;
99 {
100 std::stringstream SharedMemoryNameStream;
101 SharedMemoryNameStream << "jitlink_" << sys::Process::getProcessId() << '_'
102 << (++SharedMemoryCount);
103 SharedMemoryName = SharedMemoryNameStream.str();
104 }
105
106 std::wstring WideSharedMemoryName(SharedMemoryName.begin(),
107 SharedMemoryName.end());
108 HANDLE SharedMemoryFile = CreateFileMappingW(
109 INVALID_HANDLE_VALUE, NULL, PAGE_EXECUTE_READWRITE, Size >> 32,
110 Size & 0xffffffff, WideSharedMemoryName.c_str());
111 if (!SharedMemoryFile)
112 return errorCodeToError(mapWindowsError(GetLastError()));
113
114 void *Addr = MapViewOfFile(SharedMemoryFile,
115 FILE_MAP_ALL_ACCESS | FILE_MAP_EXECUTE, 0, 0, 0);
116 if (!Addr) {
117 CloseHandle(SharedMemoryFile);
118 return errorCodeToError(mapWindowsError(GetLastError()));
119 }
120
121#endif
122
123 {
124 std::lock_guard<std::mutex> Lock(Mutex);
125 Reservations[Addr].Size = Size;
126#if defined(_WIN32)
127 Reservations[Addr].SharedMemoryFile = SharedMemoryFile;
128#endif
129 }
130
131 return std::make_pair(ExecutorAddr::fromPtr(Addr),
132 std::move(SharedMemoryName));
133#else
134 return make_error<StringError>(
135 "SharedMemoryMapper is not supported on this platform yet",
137#endif
138}
139
142#if (defined(LLVM_ON_UNIX) && !defined(__ANDROID__)) || defined(_WIN32)
143
144 ExecutorAddr MinAddr(~0ULL);
145
146 // Contents are already in place
147 for (auto &Segment : FR.Segments) {
148 if (Segment.Addr < MinAddr)
149 MinAddr = Segment.Addr;
150
151#if defined(LLVM_ON_UNIX)
152
153#if defined(__MVS__)
154 // TODO Is it possible to change the protection level?
155#else
156 int NativeProt = 0;
157 if ((Segment.RAG.Prot & MemProt::Read) == MemProt::Read)
158 NativeProt |= PROT_READ;
159 if ((Segment.RAG.Prot & MemProt::Write) == MemProt::Write)
160 NativeProt |= PROT_WRITE;
161 if ((Segment.RAG.Prot & MemProt::Exec) == MemProt::Exec)
162 NativeProt |= PROT_EXEC;
163
164 if (mprotect(Segment.Addr.toPtr<void *>(), Segment.Size, NativeProt))
166#endif
167
168#elif defined(_WIN32)
169
170 DWORD NativeProt = getWindowsProtectionFlags(Segment.RAG.Prot);
171
172 if (!VirtualProtect(Segment.Addr.toPtr<void *>(), Segment.Size, NativeProt,
173 &NativeProt))
174 return errorCodeToError(mapWindowsError(GetLastError()));
175
176#endif
177
178 if ((Segment.RAG.Prot & MemProt::Exec) == MemProt::Exec)
179 sys::Memory::InvalidateInstructionCache(Segment.Addr.toPtr<void *>(),
180 Segment.Size);
181 }
182
183 // Run finalization actions and get deinitlization action list.
184 auto DeinitializeActions = shared::runFinalizeActions(FR.Actions);
185 if (!DeinitializeActions) {
186 return DeinitializeActions.takeError();
187 }
188
189 {
190 std::lock_guard<std::mutex> Lock(Mutex);
191 Allocations[MinAddr].DeinitializationActions =
192 std::move(*DeinitializeActions);
193 Reservations[Reservation.toPtr<void *>()].Allocations.push_back(MinAddr);
194 }
195
196 return MinAddr;
197
198#else
199 return make_error<StringError>(
200 "SharedMemoryMapper is not supported on this platform yet",
202#endif
203}
204
206 const std::vector<ExecutorAddr> &Bases) {
207 Error AllErr = Error::success();
208
209 {
210 std::lock_guard<std::mutex> Lock(Mutex);
211
212 for (auto Base : llvm::reverse(Bases)) {
214 Allocations[Base].DeinitializationActions)) {
215 AllErr = joinErrors(std::move(AllErr), std::move(Err));
216 }
217
218 // Remove the allocation from the allocation list of its reservation
219 for (auto &Reservation : Reservations) {
220 auto AllocationIt = llvm::find(Reservation.second.Allocations, Base);
221 if (AllocationIt != Reservation.second.Allocations.end()) {
222 Reservation.second.Allocations.erase(AllocationIt);
223 break;
224 }
225 }
226
227 Allocations.erase(Base);
228 }
229 }
230
231 return AllErr;
232}
233
235 const std::vector<ExecutorAddr> &Bases) {
236#if (defined(LLVM_ON_UNIX) && !defined(__ANDROID__)) || defined(_WIN32)
237 Error Err = Error::success();
238
239 for (auto Base : Bases) {
240 std::vector<ExecutorAddr> AllocAddrs;
241 size_t Size;
242
243#if defined(_WIN32)
244 HANDLE SharedMemoryFile;
245#endif
246
247 {
248 std::lock_guard<std::mutex> Lock(Mutex);
249 auto &R = Reservations[Base.toPtr<void *>()];
250 Size = R.Size;
251
252#if defined(_WIN32)
253 SharedMemoryFile = R.SharedMemoryFile;
254#endif
255
256 AllocAddrs.swap(R.Allocations);
257 }
258
259 // deinitialize sub allocations
260 if (Error E = deinitialize(AllocAddrs))
261 Err = joinErrors(std::move(Err), std::move(E));
262
263#if defined(LLVM_ON_UNIX)
264
265#if defined(__MVS__)
266 (void)Size;
267
268 if (shmdt(Base.toPtr<void *>()) < 0)
269 Err = joinErrors(std::move(Err), errorCodeToError(errnoAsErrorCode()));
270#else
271 if (munmap(Base.toPtr<void *>(), Size) != 0)
272 Err = joinErrors(std::move(Err), errorCodeToError(errnoAsErrorCode()));
273#endif
274
275#elif defined(_WIN32)
276 (void)Size;
277
278 if (!UnmapViewOfFile(Base.toPtr<void *>()))
279 Err = joinErrors(std::move(Err),
280 errorCodeToError(mapWindowsError(GetLastError())));
281
282 CloseHandle(SharedMemoryFile);
283
284#endif
285
286 std::lock_guard<std::mutex> Lock(Mutex);
287 Reservations.erase(Base.toPtr<void *>());
288 }
289
290 return Err;
291#else
292 return make_error<StringError>(
293 "SharedMemoryMapper is not supported on this platform yet",
295#endif
296}
297
299 if (Reservations.empty())
300 return Error::success();
301
302 std::vector<ExecutorAddr> ReservationAddrs;
303 ReservationAddrs.reserve(Reservations.size());
304 for (const auto &R : Reservations)
305 ReservationAddrs.push_back(ExecutorAddr::fromPtr(R.getFirst()));
306
307 return release(std::move(ReservationAddrs));
308}
309
315 ExecutorAddr::fromPtr(&reserveWrapper);
317 ExecutorAddr::fromPtr(&initializeWrapper);
319 ExecutorAddr::fromPtr(&deinitializeWrapper);
321 ExecutorAddr::fromPtr(&releaseWrapper);
322}
323
325ExecutorSharedMemoryMapperService::reserveWrapper(const char *ArgData,
326 size_t ArgSize) {
329 handle(ArgData, ArgSize,
332 .release();
333}
334
336ExecutorSharedMemoryMapperService::initializeWrapper(const char *ArgData,
337 size_t ArgSize) {
340 handle(ArgData, ArgSize,
343 .release();
344}
345
347ExecutorSharedMemoryMapperService::deinitializeWrapper(const char *ArgData,
348 size_t ArgSize) {
349 return shared::WrapperFunction<
351 handle(ArgData, ArgSize,
354 .release();
355}
356
358ExecutorSharedMemoryMapperService::releaseWrapper(const char *ArgData,
359 size_t ArgSize) {
360 return shared::WrapperFunction<
362 handle(ArgData, ArgSize,
365 .release();
366}
367
368} // namespace rt_bootstrap
369} // end namespace orc
370} // end namespace llvm
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
uint64_t Addr
uint64_t Size
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
bool erase(const KeyT &Val)
Definition: DenseMap.h:321
unsigned size() const
Definition: DenseMap.h:99
bool empty() const
Definition: DenseMap.h:98
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
static ErrorSuccess success()
Create a success value.
Definition: Error.h:337
Tagged union holding either a T or a Error.
Definition: Error.h:481
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition: StringMap.h:128
Represents an address in the executor process.
static ExecutorAddr fromPtr(T *Ptr, UnwrapFn &&Unwrap=UnwrapFn())
Create an ExecutorAddr from the given pointer.
std::enable_if_t< std::is_pointer< T >::value, T > toPtr(WrapFn &&Wrap=WrapFn()) const
Cast this ExecutorAddr to a pointer of the given type.
Expected< std::pair< ExecutorAddr, std::string > > reserve(uint64_t Size)
Expected< ExecutorAddr > initialize(ExecutorAddr Reservation, tpctypes::SharedMemoryFinalizeRequest &FR)
static void InvalidateInstructionCache(const void *Addr, size_t Len)
InvalidateInstructionCache - Before the JIT can run a block of code that has been emitted it must inv...
static Pid getProcessId()
Get the process's identifier.
shared::SPSExpected< shared::SPSExecutorAddr >(shared::SPSExecutorAddr, shared::SPSExecutorAddr, shared::SPSSharedMemoryFinalizeRequest) SPSExecutorSharedMemoryMapperServiceInitializeSignature
Definition: OrcRTBridge.h:79
const char * ExecutorSharedMemoryMapperServiceInstanceName
Definition: OrcRTBridge.cpp:31
shared::SPSError(shared::SPSExecutorAddr, shared::SPSSequence< shared::SPSExecutorAddr >) SPSExecutorSharedMemoryMapperServiceReleaseSignature
Definition: OrcRTBridge.h:84
const char * ExecutorSharedMemoryMapperServiceReserveWrapperName
Definition: OrcRTBridge.cpp:33
shared::SPSExpected< shared::SPSTuple< shared::SPSExecutorAddr, shared::SPSString > >(shared::SPSExecutorAddr, uint64_t) SPSExecutorSharedMemoryMapperServiceReserveSignature
Definition: OrcRTBridge.h:75
const char * ExecutorSharedMemoryMapperServiceDeinitializeWrapperName
Definition: OrcRTBridge.cpp:37
const char * ExecutorSharedMemoryMapperServiceReleaseWrapperName
Definition: OrcRTBridge.cpp:39
shared::SPSError(shared::SPSExecutorAddr, shared::SPSSequence< shared::SPSExecutorAddr >) SPSExecutorSharedMemoryMapperServiceDeinitializeSignature
Definition: OrcRTBridge.h:82
const char * ExecutorSharedMemoryMapperServiceInitializeWrapperName
Definition: OrcRTBridge.cpp:35
MethodWrapperHandler< RetT, ClassT, ArgTs... > makeMethodWrapperHandler(RetT(ClassT::*Method)(ArgTs...))
Create a MethodWrapperHandler object from the given method pointer.
Error runDeallocActions(ArrayRef< WrapperFunctionCall > DAs)
Run deallocation actions.
Expected< std::vector< WrapperFunctionCall > > runFinalizeActions(AllocActions &AAs)
Run finalize actions.
MemProt
Describes Read/Write/Exec permissions for memory.
Definition: MemoryFlags.h:27
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1759
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:98
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:420
Error joinErrors(Error E1, Error E2)
Concatenate errors.
Definition: Error.h:438
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:111
std::error_code errnoAsErrorCode()
Helper to get errno as an std::error_code.
Definition: Error.h:1226
std::error_code mapWindowsError(unsigned EV)
std::vector< SharedMemorySegFinalizeRequest > Segments