15 #ifndef LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETSERVER_H
16 #define LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETSERVER_H
36 #include <system_error>
38 #include <type_traits>
41 #define DEBUG_TYPE "orc-remote"
47 template <
typename ChannelT,
typename TargetT>
51 std::function<JITTargetAddress(const std::string &Name)>;
54 std::function<void(uint8_t *Addr, uint32_t Size)>;
60 EHFramesRegister(std::move(EHFramesRegister)),
61 EHFramesDeregister(std::move(EHFramesDeregister)) {
62 using ThisT =
typename std::remove_reference<decltype(*this)>::type;
63 addHandler<CallIntVoid>(*
this, &ThisT::handleCallIntVoid);
64 addHandler<CallMain>(*
this, &ThisT::handleCallMain);
65 addHandler<CallVoidVoid>(*
this, &ThisT::handleCallVoidVoid);
66 addHandler<CreateRemoteAllocator>(*
this,
67 &ThisT::handleCreateRemoteAllocator);
68 addHandler<CreateIndirectStubsOwner>(
69 *
this, &ThisT::handleCreateIndirectStubsOwner);
70 addHandler<DeregisterEHFrames>(*
this, &ThisT::handleDeregisterEHFrames);
71 addHandler<DestroyRemoteAllocator>(*
this,
72 &ThisT::handleDestroyRemoteAllocator);
73 addHandler<DestroyIndirectStubsOwner>(
74 *
this, &ThisT::handleDestroyIndirectStubsOwner);
75 addHandler<EmitIndirectStubs>(*
this, &ThisT::handleEmitIndirectStubs);
76 addHandler<EmitResolverBlock>(*
this, &ThisT::handleEmitResolverBlock);
77 addHandler<EmitTrampolineBlock>(*
this, &ThisT::handleEmitTrampolineBlock);
78 addHandler<GetSymbolAddress>(*
this, &ThisT::handleGetSymbolAddress);
79 addHandler<GetRemoteInfo>(*
this, &ThisT::handleGetRemoteInfo);
80 addHandler<ReadMem>(*
this, &ThisT::handleReadMem);
81 addHandler<RegisterEHFrames>(*
this, &ThisT::handleRegisterEHFrames);
82 addHandler<ReserveMem>(*
this, &ThisT::handleReserveMem);
83 addHandler<SetProtections>(*
this, &ThisT::handleSetProtections);
84 addHandler<TerminateSession>(*
this, &ThisT::handleTerminateSession);
85 addHandler<WriteMem>(*
this, &ThisT::handleWriteMem);
86 addHandler<WritePtr>(*
this, &ThisT::handleWritePtr);
97 return callB<RequestCompile>(TrampolineAddr);
108 Allocs = std::move(
Other.Allocs);
113 for (
auto &
Alloc : Allocs)
125 assert(Allocs.find(MB.base()) == Allocs.end() &&
"Duplicate alloc");
126 Allocs[MB.base()] = std::move(MB);
130 Error setProtections(
void *block,
unsigned Flags) {
131 auto I = Allocs.find(block);
132 if (
I == Allocs.end())
139 std::map<void *, sys::MemoryBlock> Allocs;
144 static JITTargetAddress reenter(
void *JITTargetAddr,
void *TrampolineAddr) {
146 auto AddrOrErr =
T->requestCompile(static_cast<JITTargetAddress>(
147 reinterpret_cast<uintptr_t>(TrampolineAddr)));
149 assert(AddrOrErr &&
"Compile request failed");
154 using IntVoidFnTy = int (*)();
157 reinterpret_cast<IntVoidFnTy
>(
static_cast<uintptr_t
>(Addr));
161 DEBUG(
dbgs() <<
" Result = " << Result <<
"\n");
167 std::vector<std::string>
Args) {
168 using MainFnTy = int (*)(int,
const char *[]);
170 MainFnTy Fn =
reinterpret_cast<MainFnTy
>(
static_cast<uintptr_t
>(Addr));
171 int ArgC = Args.size() + 1;
173 std::unique_ptr<const char *[]> ArgV(
new const char *[ArgC + 1]);
174 ArgV[0] =
"<jit process>";
175 for (
auto &Arg : Args)
176 ArgV[Idx++] = Arg.c_str();
179 for (
int Idx = 0; Idx < ArgC; ++Idx) {
180 llvm::dbgs() <<
"Arg " << Idx <<
": " << ArgV[Idx] <<
"\n";
185 int Result = Fn(ArgC, ArgV.get());
186 DEBUG(
dbgs() <<
" Result = " << Result <<
"\n");
192 using VoidVoidFnTy = void (*)();
195 reinterpret_cast<VoidVoidFnTy
>(
static_cast<uintptr_t
>(Addr));
205 auto I = Allocators.find(Id);
206 if (
I != Allocators.end())
209 DEBUG(
dbgs() <<
" Created allocator " << Id <<
"\n");
215 auto I = IndirectStubsOwners.find(Id);
216 if (
I != IndirectStubsOwners.end())
219 DEBUG(
dbgs() <<
" Create indirect stubs owner " << Id <<
"\n");
220 IndirectStubsOwners[
Id] = ISBlockOwnerList();
225 uint8_t *Addr =
reinterpret_cast<uint8_t *
>(
static_cast<uintptr_t
>(TAddr));
226 DEBUG(
dbgs() <<
" Registering EH frames at " <<
format(
"0x%016x", TAddr)
227 <<
", Size = " << Size <<
" bytes\n");
228 EHFramesDeregister(Addr, Size);
233 auto I = Allocators.find(Id);
234 if (
I == Allocators.end())
238 DEBUG(
dbgs() <<
" Destroyed allocator " << Id <<
"\n");
243 auto I = IndirectStubsOwners.find(Id);
244 if (
I == IndirectStubsOwners.end())
247 IndirectStubsOwners.erase(
I);
251 Expected<std::tuple<JITTargetAddress, JITTargetAddress, uint32_t>>
254 DEBUG(
dbgs() <<
" ISMgr " << Id <<
" request " << NumStubsRequired
257 auto StubOwnerItr = IndirectStubsOwners.find(Id);
258 if (StubOwnerItr == IndirectStubsOwners.end())
262 typename TargetT::IndirectStubsInfo IS;
264 TargetT::emitIndirectStubsBlock(IS, NumStubsRequired,
nullptr))
265 return std::move(Err);
268 reinterpret_cast<uintptr_t
>(IS.getStub(0)));
270 reinterpret_cast<uintptr_t
>(IS.getPtr(0)));
271 uint32_t NumStubsEmitted = IS.getNumStubs();
273 auto &BlockList = StubOwnerItr->second;
274 BlockList.push_back(std::move(IS));
276 return std::make_tuple(StubsBase, PtrsBase, NumStubsEmitted);
279 Error handleEmitResolverBlock() {
282 TargetT::ResolverCodeSize,
nullptr,
287 TargetT::writeResolverCode(static_cast<uint8_t *>(ResolverBlock.
base()),
295 Expected<std::tuple<JITTargetAddress, uint32_t>> handleEmitTrampolineBlock() {
297 auto TrampolineBlock =
306 TargetT::TrampolineSize;
308 uint8_t *TrampolineMem =
static_cast<uint8_t *
>(TrampolineBlock.base());
309 TargetT::writeTrampolines(TrampolineMem, ResolverBlock.
base(),
316 TrampolineBlocks.push_back(std::move(TrampolineBlock));
319 reinterpret_cast<uintptr_t
>(TrampolineMem));
321 return std::make_tuple(TrampolineBaseAddr, NumTrampolines);
324 Expected<JITTargetAddress> handleGetSymbolAddress(
const std::string &
Name) {
326 DEBUG(
dbgs() <<
" Symbol '" << Name <<
"' = " <<
format(
"0x%016x", Addr)
331 Expected<std::tuple<std::string, uint32_t, uint32_t, uint32_t, uint32_t>>
332 handleGetRemoteInfo() {
334 uint32_t PointerSize = TargetT::PointerSize;
336 uint32_t TrampolineSize = TargetT::TrampolineSize;
337 uint32_t IndirectStubSize = TargetT::IndirectStubsInfo::StubSize;
339 <<
" triple = '" << ProcessTriple <<
"'\n"
340 <<
" pointer size = " << PointerSize <<
"\n"
341 <<
" page size = " << PageSize <<
"\n"
342 <<
" trampoline size = " << TrampolineSize <<
"\n"
343 <<
" indirect stub size = " << IndirectStubSize <<
"\n");
344 return std::make_tuple(ProcessTriple, PointerSize, PageSize, TrampolineSize,
350 uint8_t *Src =
reinterpret_cast<uint8_t *
>(
static_cast<uintptr_t
>(RSrc));
352 DEBUG(
dbgs() <<
" Reading " << Size <<
" bytes from "
353 <<
format(
"0x%016x", RSrc) <<
"\n");
355 std::vector<uint8_t> Buffer;
357 for (uint8_t *
P = Src; Size != 0; --
Size)
358 Buffer.push_back(*
P++);
364 uint8_t *Addr =
reinterpret_cast<uint8_t *
>(
static_cast<uintptr_t
>(TAddr));
365 DEBUG(
dbgs() <<
" Registering EH frames at " <<
format(
"0x%016x", TAddr)
366 <<
", Size = " << Size <<
" bytes\n");
367 EHFramesRegister(Addr, Size);
373 auto I = Allocators.find(Id);
374 if (
I == Allocators.end())
378 void *LocalAllocAddr =
nullptr;
379 if (
auto Err =
Allocator.allocate(LocalAllocAddr, Size, Align))
380 return std::move(Err);
382 DEBUG(
dbgs() <<
" Allocator " << Id <<
" reserved " << LocalAllocAddr
383 <<
" (" << Size <<
" bytes, alignment " << Align <<
")\n");
386 reinterpret_cast<uintptr_t
>(LocalAllocAddr));
393 auto I = Allocators.find(Id);
394 if (
I == Allocators.end())
398 void *LocalAddr =
reinterpret_cast<void *
>(
static_cast<uintptr_t
>(Addr));
399 DEBUG(
dbgs() <<
" Allocator " << Id <<
" set permissions on " << LocalAddr
403 return Allocator.setProtections(LocalAddr, Flags);
406 Error handleTerminateSession() {
407 TerminateFlag =
true;
411 Error handleWriteMem(DirectBufferWriter DBW) {
412 DEBUG(
dbgs() <<
" Writing " << DBW.getSize() <<
" bytes to "
413 <<
format(
"0x%016x", DBW.getDst()) <<
"\n");
418 DEBUG(
dbgs() <<
" Writing pointer *" <<
format(
"0x%016x", Addr) <<
" = "
419 <<
format(
"0x%016x", PtrVal) <<
"\n");
421 reinterpret_cast<uintptr_t *
>(
static_cast<uintptr_t
>(Addr));
422 *Ptr =
static_cast<uintptr_t
>(PtrVal);
428 std::map<ResourceIdMgr::ResourceId, Allocator> Allocators;
429 using ISBlockOwnerList = std::vector<typename TargetT::IndirectStubsInfo>;
430 std::map<ResourceIdMgr::ResourceId, ISBlockOwnerList> IndirectStubsOwners;
431 sys::OwningMemoryBlock ResolverBlock;
432 std::vector<sys::OwningMemoryBlock> TrampolineBlocks;
433 bool TerminateFlag =
false;
442 #endif // LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETSERVER_H
std::function< JITTargetAddress(const std::string &Name)> SymbolLookupFtor
OrcRemoteTargetServer & operator=(const OrcRemoteTargetServer &)=delete
static std::error_code releaseMappedMemory(MemoryBlock &Block)
This method releases a block of memory that was allocated with the allocateMappedMemory method...
static 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...
std::error_code orcError(OrcErrorCode ErrCode)
Tagged union holding either a T or a Error.
bool receivedTerminate() const
static cl::opt< int > PageSize("imp-null-check-page-size", cl::desc("The page size of the target in bytes"), cl::init(4096))
std::function< void(uint8_t *Addr, uint32_t Size)> EHFrameRegistrationFtor
uint64_t JITTargetAddress
Represents an address in the target process's address space.
MemoryBlock getMemoryBlock() const
format_object< Ts...> format(const char *Fmt, const Ts &...Vals)
These are helper functions used to produce formatted output.
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
OrcRemoteTargetServer(ChannelT &Channel, SymbolLookupFtor SymbolLookup, EHFrameRegistrationFtor EHFramesRegister, EHFrameRegistrationFtor EHFramesDeregister)
std::string getProcessTriple()
getProcessTriple() - Return an appropriate target triple for generating code to be loaded into the cu...
static unsigned getPageSize()
static ErrorSuccess success()
Create a success value.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
static std::vector< std::string > Flags
Provides a library for accessing information about this process and other processes on the operating ...
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Expected< JITTargetAddress > requestCompile(JITTargetAddress TrampolineAddr)
static 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...