10#include "llvm/Config/llvm-config.h"
20#if defined(LLVM_ON_UNIX)
42 return PAGE_READWRITE;
45 return PAGE_EXECUTE_READ;
47 return PAGE_EXECUTE_READWRITE;
55Expected<std::pair<ExecutorAddr, std::string>>
57#if (defined(LLVM_ON_UNIX) && !defined(__ANDROID__)) || defined(_WIN32)
59#if defined(LLVM_ON_UNIX)
61 std::string SharedMemoryName;
63 std::stringstream SharedMemoryNameStream;
65 << (++SharedMemoryCount);
66 SharedMemoryName = SharedMemoryNameStream.str();
71 reinterpret_cast<const uint8_t *
>(SharedMemoryName.c_str()),
72 SharedMemoryName.size());
74 key_t
Key = *
reinterpret_cast<key_t *
>(HashedName.data());
76 shmget(
Key,
Size, IPC_CREAT | IPC_EXCL | __IPC_SHAREAS | 0700);
77 if (SharedMemoryId < 0)
80 void *Addr = shmat(SharedMemoryId,
nullptr, 0);
81 if (Addr ==
reinterpret_cast<void *
>(-1))
84 int SharedMemoryFile =
85 shm_open(SharedMemoryName.c_str(), O_RDWR | O_CREAT | O_EXCL, 0700);
86 if (SharedMemoryFile < 0)
90 if (ftruncate(SharedMemoryFile,
Size) < 0)
93 void *Addr = mmap(
nullptr,
Size, PROT_NONE, MAP_SHARED, SharedMemoryFile, 0);
94 if (Addr == MAP_FAILED)
97 close(SharedMemoryFile);
102 std::string SharedMemoryName;
104 std::stringstream SharedMemoryNameStream;
106 << (++SharedMemoryCount);
107 SharedMemoryName = SharedMemoryNameStream.str();
110 std::wstring WideSharedMemoryName(SharedMemoryName.begin(),
111 SharedMemoryName.end());
112 HANDLE SharedMemoryFile = CreateFileMappingW(
113 INVALID_HANDLE_VALUE, NULL, PAGE_EXECUTE_READWRITE,
Size >> 32,
114 Size & 0xffffffff, WideSharedMemoryName.c_str());
115 if (!SharedMemoryFile)
118 void *Addr = MapViewOfFile(SharedMemoryFile,
119 FILE_MAP_ALL_ACCESS | FILE_MAP_EXECUTE, 0, 0, 0);
121 CloseHandle(SharedMemoryFile);
128 std::lock_guard<std::mutex> Lock(Mutex);
129 Reservations[Addr].Size =
Size;
131 Reservations[Addr].SharedMemoryFile = SharedMemoryFile;
136 std::move(SharedMemoryName));
139 "SharedMemoryMapper is not supported on this platform yet",
146#if (defined(LLVM_ON_UNIX) && !defined(__ANDROID__)) || defined(_WIN32)
152 if (Segment.Addr < MinAddr)
153 MinAddr = Segment.Addr;
155#if defined(LLVM_ON_UNIX)
162 NativeProt |= PROT_READ;
164 NativeProt |= PROT_WRITE;
166 NativeProt |= PROT_EXEC;
168 if (mprotect(Segment.Addr.toPtr<
void *>(), Segment.Size, NativeProt))
174 DWORD NativeProt = getWindowsProtectionFlags(Segment.RAG.Prot);
176 if (!VirtualProtect(Segment.Addr.toPtr<
void *>(), Segment.Size, NativeProt,
189 if (!DeinitializeActions) {
190 return DeinitializeActions.takeError();
194 std::lock_guard<std::mutex> Lock(Mutex);
195 Allocations[MinAddr].DeinitializationActions =
196 std::move(*DeinitializeActions);
197 Reservations[Reservation.toPtr<
void *>()].Allocations.push_back(MinAddr);
204 "SharedMemoryMapper is not supported on this platform yet",
210 const std::vector<ExecutorAddr> &Bases) {
214 std::lock_guard<std::mutex> Lock(Mutex);
218 Allocations[
Base].DeinitializationActions)) {
219 AllErr =
joinErrors(std::move(AllErr), std::move(Err));
223 for (
auto &Reservation : Reservations) {
224 auto AllocationIt =
llvm::find(Reservation.second.Allocations,
Base);
225 if (AllocationIt != Reservation.second.Allocations.end()) {
226 Reservation.second.Allocations.erase(AllocationIt);
231 Allocations.erase(
Base);
239 const std::vector<ExecutorAddr> &Bases) {
240#if (defined(LLVM_ON_UNIX) && !defined(__ANDROID__)) || defined(_WIN32)
243 for (
auto Base : Bases) {
244 std::vector<ExecutorAddr> AllocAddrs;
248 HANDLE SharedMemoryFile;
252 std::lock_guard<std::mutex> Lock(Mutex);
253 auto &R = Reservations[
Base.toPtr<
void *>()];
257 SharedMemoryFile = R.SharedMemoryFile;
260 AllocAddrs.swap(R.Allocations);
267#if defined(LLVM_ON_UNIX)
272 if (shmdt(
Base.toPtr<
void *>()) < 0)
275 if (munmap(
Base.toPtr<
void *>(),
Size) != 0)
282 if (!UnmapViewOfFile(
Base.toPtr<
void *>()))
286 CloseHandle(SharedMemoryFile);
290 std::lock_guard<std::mutex> Lock(Mutex);
291 Reservations.erase(
Base.toPtr<
void *>());
297 "SharedMemoryMapper is not supported on this platform yet",
303 if (Reservations.empty())
306 std::vector<ExecutorAddr> ReservationAddrs;
307 ReservationAddrs.reserve(Reservations.size());
308 for (
const auto &R : Reservations)
311 return release(std::move(ReservationAddrs));
330ExecutorSharedMemoryMapperService::reserveWrapper(
const char *ArgData,
333 WrapperFunction<rt::sps_ci::SharedMemoryMapperReserve::SPSSig>::handle(
341ExecutorSharedMemoryMapperService::initializeWrapper(
const char *ArgData,
344 WrapperFunction<rt::sps_ci::SharedMemoryMapperInitialize::SPSSig>::handle(
352ExecutorSharedMemoryMapperService::deinitializeWrapper(
const char *ArgData,
354 return shared::WrapperFunction<
356 handle(ArgData, ArgSize,
363ExecutorSharedMemoryMapperService::releaseWrapper(
const char *ArgData,
366 WrapperFunction<rt::sps_ci::SharedMemoryMapperRelease::SPSSig>::handle(
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Provides a library for accessing information about this process and other processes on the operating ...
Represent a constant reference to an array (0 or more elements consecutively in memory),...
static BLAKE3Result< NumBytes > hash(ArrayRef< uint8_t > Data)
Returns a BLAKE3 hash for the given data.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Triple - Helper class for working with autoconf configuration names.
Represents an address in the executor process.
static ExecutorAddr fromPtr(T *Ptr, UnwrapFn &&Unwrap=UnwrapFn())
Create an ExecutorAddr from the given pointer.
Applies linker name-mangling for a target.
void addBootstrapSymbols(StringMap< ExecutorAddr > &M) override
Error deinitialize(const std::vector< ExecutorAddr > &Bases)
Error release(const std::vector< ExecutorAddr > &Bases)
Expected< std::pair< ExecutorAddr, std::string > > reserve(uint64_t Size)
Error shutdown() override
Expected< ExecutorAddr > initialize(ExecutorAddr Reservation, tpctypes::SharedMemoryFinalizeRequest &FR)
static LLVM_ABI 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 LLVM_ABI Pid getProcessId()
Get the process's identifier.
constexpr SymbolNameSpec SharedMemoryMapperInstanceName
MethodWrapperHandler< RetT, ClassT, ArgTs... > makeMethodWrapperHandler(RetT(ClassT::*Method)(ArgTs...))
Create a MethodWrapperHandler object from the given method pointer.
LLVM_ABI Error runDeallocActions(ArrayRef< WrapperFunctionCall > DAs)
Run deallocation actions.
LLVM_ABI Expected< std::vector< WrapperFunctionCall > > runFinalizeActions(AllocActions &AAs)
Run finalize actions.
MemProt
Describes Read/Write/Exec permissions for memory.
LLVM_ABI std::string getProcessTriple()
getProcessTriple() - Return an appropriate target triple for generating code to be loaded into the cu...
This is an optimization pass for GlobalISel generic memory operations.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
auto reverse(ContainerTy &&C)
Error joinErrors(Error E1, Error E2)
Concatenate errors.
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
LLVM_ABI std::error_code mapWindowsError(unsigned EV)
std::error_code errnoAsErrorCode()
Helper to get errno as an std::error_code.
shared::SPSError(shared::SPSExecutorAddr, shared::SPSSequence< shared::SPSExecutorAddr >) SPSSig
static constexpr SymbolNameSpec Name
static constexpr SymbolNameSpec Name
static constexpr SymbolNameSpec Name
static constexpr SymbolNameSpec Name
std::vector< SharedMemorySegFinalizeRequest > Segments
shared::AllocActions Actions