11#include "llvm/Config/llvm-config.h"
15#if defined(LLVM_ON_UNIX) && !defined(__ANDROID__)
33 : PageSize(PageSize) {}
39 return PageSize.takeError();
40 return std::make_unique<InProcessMemoryMapper>(*PageSize);
53 std::lock_guard<std::mutex> Lock(Mutex);
54 Reservations[MB.base()].Size = MB.allocatedSize();
63 return Addr.
toPtr<
char *>();
74 auto Size = Segment.ContentSize + Segment.ZeroFillSize;
82 std::memset((
Base + Segment.ContentSize).toPtr<
void *>(), 0,
83 Segment.ZeroFillSize);
95 if (!DeinitializeActions)
96 return OnInitialized(DeinitializeActions.takeError());
99 std::lock_guard<std::mutex> Lock(Mutex);
102 auto &
Alloc = Allocations[MinAddr];
103 Alloc.Size = MaxAddr - MinAddr;
104 Alloc.DeinitializationActions = std::move(*DeinitializeActions);
105 Reservations[AI.
MappingBase.
toPtr<
void *>()].Allocations.push_back(MinAddr);
108 OnInitialized(MinAddr);
117 std::lock_guard<std::mutex> Lock(Mutex);
122 Allocations[
Base].DeinitializationActions)) {
123 AllErr =
joinErrors(std::move(AllErr), std::move(Err));
134 Allocations.erase(
Base);
138 OnDeinitialized(std::move(AllErr));
145 for (
auto Base : Bases) {
146 std::vector<ExecutorAddr> AllocAddrs;
149 std::lock_guard<std::mutex> Lock(Mutex);
150 auto &R = Reservations[
Base.toPtr<
void *>()];
152 AllocAddrs.swap(R.Allocations);
156 std::promise<MSVCPError>
P;
157 auto F =
P.get_future();
171 std::lock_guard<std::mutex> Lock(Mutex);
172 Reservations.erase(
Base.toPtr<
void *>());
175 OnReleased(std::move(Err));
179 std::vector<ExecutorAddr> ReservationAddrs;
181 std::lock_guard<std::mutex> Lock(Mutex);
183 ReservationAddrs.reserve(Reservations.size());
184 for (
const auto &R : Reservations) {
189 std::promise<MSVCPError>
P;
190 auto F =
P.get_future();
191 release(ReservationAddrs, [&](
Error Err) {
P.set_value(std::move(Err)); });
200 : ES(ES), B(
std::
move(B)), PageSize(PageSize) {
201#if (!defined(LLVM_ON_UNIX) || defined(__ANDROID__)) && !defined(_WIN32)
202 llvm_unreachable(
"SharedMemoryMapper is not supported on this platform yet");
208#if (defined(LLVM_ON_UNIX) && !defined(__ANDROID__)) || defined(_WIN32)
211 return PageSize.takeError();
213 return std::make_unique<SharedMemoryMapper>(ES, std::move(B), *PageSize);
216 "SharedMemoryMapper is not supported on this platform yet",
223#if (defined(LLVM_ON_UNIX) && !defined(__ANDROID__)) || defined(_WIN32)
225 int SharedMemoryId = -1;
227 [
this, NumBytes, OnReserved = std::move(OnReserved), SharedMemoryId](
230 return OnReserved(
Result.takeError());
233 std::string SharedMemoryName;
234 std::tie(RemoteAddr, SharedMemoryName) = std::move(*
Result);
236 void *LocalAddr =
nullptr;
238#if defined(LLVM_ON_UNIX)
242 reinterpret_cast<const uint8_t *
>(SharedMemoryName.c_str()),
243 SharedMemoryName.size());
245 key_t
Key = *
reinterpret_cast<key_t *
>(HashedName.data());
247 shmget(
Key, NumBytes, IPC_CREAT | __IPC_SHAREAS | 0700);
248 if (SharedMemoryId < 0) {
250 std::error_code(errno, std::generic_category())));
252 LocalAddr = shmat(SharedMemoryId,
nullptr, 0);
253 if (LocalAddr ==
reinterpret_cast<void *
>(-1)) {
255 std::error_code(errno, std::generic_category())));
258 int SharedMemoryFile = shm_open(SharedMemoryName.c_str(), O_RDWR, 0700);
259 if (SharedMemoryFile < 0) {
264 shm_unlink(SharedMemoryName.c_str());
266 LocalAddr = mmap(
nullptr, NumBytes, PROT_READ | PROT_WRITE, MAP_SHARED,
267 SharedMemoryFile, 0);
268 if (LocalAddr == MAP_FAILED) {
272 close(SharedMemoryFile);
277 std::wstring WideSharedMemoryName(SharedMemoryName.begin(),
278 SharedMemoryName.end());
279 HANDLE SharedMemoryFile = OpenFileMappingW(
280 FILE_MAP_ALL_ACCESS, FALSE, WideSharedMemoryName.c_str());
281 if (!SharedMemoryFile)
285 MapViewOfFile(SharedMemoryFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
287 CloseHandle(SharedMemoryFile);
291 CloseHandle(SharedMemoryFile);
295 std::lock_guard<std::mutex> Lock(Mutex);
297 {RemoteAddr, {LocalAddr, NumBytes, SharedMemoryId}});
302 ES, B.Instance,
static_cast<uint64_t>(NumBytes));
306 "SharedMemoryMapper is not supported on this platform yet",
312 size_t ContentSize) {
313 auto R = Reservations.upper_bound(Addr);
314 assert(R != Reservations.begin() &&
"Attempt to prepare unreserved range");
319 return static_cast<char *
>(R->second.LocalAddr) +
Offset;
324 auto Reservation = Reservations.upper_bound(AI.
MappingBase);
325 assert(Reservation != Reservations.begin() &&
"Attempt to initialize unreserved range");
328 auto AllocationOffset = AI.
MappingBase - Reservation->first;
337 char *
Base =
static_cast<char *
>(Reservation->second.LocalAddr) +
338 AllocationOffset + Segment.Offset;
339 std::memset(
Base + Segment.ContentSize, 0, Segment.ZeroFillSize);
342 SegReq.
RAG = {Segment.AG.getMemProt(),
345 SegReq.
Size = Segment.ContentSize + Segment.ZeroFillSize;
350 B.Initialize(std::move(OnInitialized), ES, B.Instance, Reservation->first,
357 B.Deinitialize(std::move(OnDeinitialized), ES, B.Instance, Allocations);
362#if (defined(LLVM_ON_UNIX) && !defined(__ANDROID__)) || defined(_WIN32)
366 std::lock_guard<std::mutex> Lock(Mutex);
368 for (
auto Base : Bases) {
370#if defined(LLVM_ON_UNIX)
373 if (shmdt(Reservations[
Base].LocalAddr) < 0 ||
374 shmctl(Reservations[
Base].SharedMemoryId, IPC_RMID, NULL) < 0)
377 if (munmap(Reservations[
Base].LocalAddr, Reservations[
Base].
Size) != 0)
383 if (!UnmapViewOfFile(Reservations[
Base].LocalAddr))
389 Reservations.erase(
Base);
394 [OnReleased = std::move(OnReleased),
398 ES, B.Instance, Bases);
401 "SharedMemoryMapper is not supported on this platform yet",
407 std::lock_guard<std::mutex> Lock(Mutex);
408 for (
const auto &R : Reservations) {
410#if defined(LLVM_ON_UNIX) && !defined(__ANDROID__)
413 shmdt(R.second.LocalAddr);
415 munmap(R.second.LocalAddr, R.second.Size);
420 UnmapViewOfFile(R.second.LocalAddr);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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.
An ExecutionSession represents a running JIT program.
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.
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.
InProcessMemoryMapper(size_t PageSize)
~InProcessMemoryMapper() override
char * prepare(jitlink::LinkGraph &G, ExecutorAddr Addr, size_t ContentSize) override
Provides working memory The LinkGraph parameter is included to allow implementations to allocate work...
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()
void release(ArrayRef< ExecutorAddr > Reservations, OnReleasedFunction OnRelease) override
Release address space acquired through reserve()
unique_function< void(Error)> OnReleasedFunction
unique_function< void(Expected< ExecutorAddr >)> OnInitializedFunction
unique_function< void(Expected< ExecutorAddrRange >)> OnReservedFunction
unique_function< void(Error)> OnDeinitializedFunction
char * prepare(jitlink::LinkGraph &G, ExecutorAddr Addr, size_t ContentSize) override
Provides working memory The LinkGraph parameter is included to allow implementations to allocate work...
SharedMemoryMapper(ExecutionSession &ES, SharedMemoryMapBindings B, size_t PageSize)
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...
~SharedMemoryMapper() override
void initialize(AllocInfo &AI, OnInitializedFunction OnInitialized) override
Ensures executor memory is synchronized with working copy memory, sends functions to be called after ...
void release(ArrayRef< ExecutorAddr > Reservations, OnReleasedFunction OnRelease) override
Release address space acquired through reserve()
static Expected< std::unique_ptr< SharedMemoryMapper > > Create(ExecutionSession &ES, SharedMemoryMapBindings B)
This class encapsulates the notion of a memory block which has an address and a size.
static LLVM_ABI std::error_code protectMappedMemory(const MemoryBlock &Block, unsigned Flags)
This method sets the protection flags for a block of memory to the state specified by /p Flags.
static LLVM_ABI std::error_code releaseMappedMemory(MemoryBlock &Block)
This method releases a block of memory that was allocated with the allocateMappedMemory method.
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 MemoryBlock allocateMappedMemory(size_t NumBytes, const MemoryBlock *const NearBlock, unsigned Flags, std::error_code &EC)
This method allocates a block of memory that is suitable for loading dynamically generated code (e....
static LLVM_ABI Expected< unsigned > getPageSize()
Get the process's page size.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI Error runDeallocActions(ArrayRef< WrapperFunctionCall > DAs)
Run deallocation actions.
LLVM_ABI Expected< std::vector< WrapperFunctionCall > > runFinalizeActions(AllocActions &AAs)
Run finalize actions.
uint64_t ExecutorAddrDiff
@ Finalize
Finalize memory should be allocated by the allocator, and then be overwritten and deallocated after a...
sys::Memory::ProtectionFlags toSysMemoryProtectionFlags(MemProt MP)
Convert a MemProt value to a corresponding sys::Memory::ProtectionFlags value.
This is an optimization pass for GlobalISel generic memory operations.
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.
void cantFail(Error Err, const char *Msg=nullptr)
Report a fatal error if Err is a failure value.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
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.
Implement std::hash so that hash_code can be used in STL containers.
Represents an address range in the exceutor process.
Represents a single allocation containing multiple segments and initialization and deinitialization a...
std::vector< SegInfo > Segments
shared::AllocActions Actions
The resolved controller-side handle to an executor-side shared-memory mapper: the address of the mapp...
std::vector< SharedMemorySegFinalizeRequest > Segments
shared::AllocActions Actions