14 #ifndef LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETSERVER_H 15 #define LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETSERVER_H 35 #include <system_error> 37 #include <type_traits> 40 #define DEBUG_TYPE "orc-remote" 46 template <
typename ChannelT,
typename TargetT>
51 std::function<JITTargetAddress(const std::string &Name)>;
54 std::function<void(uint8_t *Addr, uint32_t Size)>;
60 SymbolLookup(
std::move(SymbolLookup)),
61 EHFramesRegister(
std::move(EHFramesRegister)),
62 EHFramesDeregister(
std::move(EHFramesDeregister)) {
63 using ThisT =
typename std::remove_reference<decltype(*this)>::type;
64 addHandler<exec::CallIntVoid>(*
this, &ThisT::handleCallIntVoid);
65 addHandler<exec::CallMain>(*
this, &ThisT::handleCallMain);
66 addHandler<exec::CallVoidVoid>(*
this, &ThisT::handleCallVoidVoid);
67 addHandler<mem::CreateRemoteAllocator>(*
this,
68 &ThisT::handleCreateRemoteAllocator);
69 addHandler<mem::DestroyRemoteAllocator>(
70 *
this, &ThisT::handleDestroyRemoteAllocator);
71 addHandler<mem::ReadMem>(*
this, &ThisT::handleReadMem);
72 addHandler<mem::ReserveMem>(*
this, &ThisT::handleReserveMem);
73 addHandler<mem::SetProtections>(*
this, &ThisT::handleSetProtections);
74 addHandler<mem::WriteMem>(*
this, &ThisT::handleWriteMem);
75 addHandler<mem::WritePtr>(*
this, &ThisT::handleWritePtr);
76 addHandler<eh::RegisterEHFrames>(*
this, &ThisT::handleRegisterEHFrames);
77 addHandler<eh::DeregisterEHFrames>(*
this, &ThisT::handleDeregisterEHFrames);
78 addHandler<stubs::CreateIndirectStubsOwner>(
79 *
this, &ThisT::handleCreateIndirectStubsOwner);
80 addHandler<stubs::DestroyIndirectStubsOwner>(
81 *
this, &ThisT::handleDestroyIndirectStubsOwner);
82 addHandler<stubs::EmitIndirectStubs>(*
this,
83 &ThisT::handleEmitIndirectStubs);
84 addHandler<stubs::EmitResolverBlock>(*
this,
85 &ThisT::handleEmitResolverBlock);
86 addHandler<stubs::EmitTrampolineBlock>(*
this,
87 &ThisT::handleEmitTrampolineBlock);
88 addHandler<utils::GetSymbolAddress>(*
this, &ThisT::handleGetSymbolAddress);
89 addHandler<utils::GetRemoteInfo>(*
this, &ThisT::handleGetRemoteInfo);
90 addHandler<utils::TerminateSession>(*
this, &ThisT::handleTerminateSession);
101 return callB<utils::RequestCompile>(TrampolineAddr);
112 Allocs = std::move(
Other.Allocs);
117 for (
auto &Alloc : Allocs)
129 assert(Allocs.find(MB.
base()) == Allocs.end() &&
"Duplicate alloc");
130 Allocs[MB.
base()] = std::move(MB);
134 Error setProtections(
void *block,
unsigned Flags) {
135 auto I = Allocs.find(block);
136 if (
I == Allocs.end())
143 std::map<void *, sys::MemoryBlock> Allocs;
148 static JITTargetAddress reenter(
void *JITTargetAddr,
void *TrampolineAddr) {
150 auto AddrOrErr =
T->requestCompile(static_cast<JITTargetAddress>(
151 reinterpret_cast<uintptr_t>(TrampolineAddr)));
153 assert(AddrOrErr &&
"Compile request failed");
158 using IntVoidFnTy = int (*)();
161 reinterpret_cast<IntVoidFnTy
>(
static_cast<uintptr_t
>(Addr));
171 std::vector<std::string>
Args) {
172 using MainFnTy = int (*)(int,
const char *[]);
174 MainFnTy Fn =
reinterpret_cast<MainFnTy
>(
static_cast<uintptr_t
>(Addr));
175 int ArgC = Args.size() + 1;
177 std::unique_ptr<const char *[]> ArgV(
new const char *[ArgC + 1]);
178 ArgV[0] =
"<jit process>";
179 for (
auto &
Arg : Args)
180 ArgV[Idx++] =
Arg.c_str();
182 LLVM_DEBUG(
for (
int Idx = 0; Idx < ArgC; ++Idx) {
183 llvm::dbgs() <<
"Arg " << Idx <<
": " << ArgV[Idx] <<
"\n";
187 int Result = Fn(ArgC, ArgV.get());
194 using VoidVoidFnTy = void (*)();
197 reinterpret_cast<VoidVoidFnTy
>(
static_cast<uintptr_t
>(Addr));
207 auto I = Allocators.find(Id);
208 if (
I != Allocators.end())
217 auto I = IndirectStubsOwners.find(Id);
218 if (
I != IndirectStubsOwners.end())
221 LLVM_DEBUG(
dbgs() <<
" Create indirect stubs owner " << Id <<
"\n");
222 IndirectStubsOwners[
Id] = ISBlockOwnerList();
227 uint8_t *Addr =
reinterpret_cast<uint8_t *
>(
static_cast<uintptr_t
>(TAddr));
229 <<
format(
"0x%016x", TAddr) <<
", Size = " << Size
231 EHFramesDeregister(Addr, Size);
236 auto I = Allocators.find(Id);
237 if (
I == Allocators.end())
246 auto I = IndirectStubsOwners.find(Id);
247 if (
I == IndirectStubsOwners.end())
250 IndirectStubsOwners.erase(
I);
257 LLVM_DEBUG(
dbgs() <<
" ISMgr " << Id <<
" request " << NumStubsRequired
260 auto StubOwnerItr = IndirectStubsOwners.find(Id);
261 if (StubOwnerItr == IndirectStubsOwners.end())
265 typename TargetT::IndirectStubsInfo IS;
267 TargetT::emitIndirectStubsBlock(IS, NumStubsRequired,
nullptr))
268 return std::move(Err);
271 reinterpret_cast<uintptr_t
>(IS.getStub(0)));
273 reinterpret_cast<uintptr_t
>(IS.getPtr(0)));
274 uint32_t NumStubsEmitted = IS.getNumStubs();
276 auto &BlockList = StubOwnerItr->second;
277 BlockList.push_back(std::move(IS));
279 return std::make_tuple(StubsBase, PtrsBase, NumStubsEmitted);
282 Error handleEmitResolverBlock() {
285 TargetT::ResolverCodeSize,
nullptr,
290 TargetT::writeResolverCode(static_cast<uint8_t *>(ResolverBlock.base()),
294 ResolverBlock.getMemoryBlock(),
300 auto TrampolineBlock =
309 TargetT::TrampolineSize;
311 uint8_t *TrampolineMem =
static_cast<uint8_t *
>(TrampolineBlock.base());
312 TargetT::writeTrampolines(TrampolineMem, ResolverBlock.base(),
319 TrampolineBlocks.push_back(std::move(TrampolineBlock));
322 reinterpret_cast<uintptr_t
>(TrampolineMem));
324 return std::make_tuple(TrampolineBaseAddr, NumTrampolines);
330 <<
"' = " <<
format(
"0x%016x", Addr) <<
"\n");
335 handleGetRemoteInfo() {
337 uint32_t PointerSize = TargetT::PointerSize;
339 uint32_t TrampolineSize = TargetT::TrampolineSize;
340 uint32_t IndirectStubSize = TargetT::IndirectStubsInfo::StubSize;
342 <<
" triple = '" << ProcessTriple <<
"'\n" 343 <<
" pointer size = " << PointerSize <<
"\n" 344 <<
" page size = " << PageSize <<
"\n" 345 <<
" trampoline size = " << TrampolineSize <<
"\n" 346 <<
" indirect stub size = " << IndirectStubSize
348 return std::make_tuple(ProcessTriple, PointerSize, PageSize, TrampolineSize,
354 uint8_t *Src =
reinterpret_cast<uint8_t *
>(
static_cast<uintptr_t
>(RSrc));
357 <<
format(
"0x%016x", RSrc) <<
"\n");
359 std::vector<uint8_t> Buffer;
361 for (uint8_t *
P = Src; Size != 0; --
Size)
362 Buffer.push_back(*
P++);
368 uint8_t *Addr =
reinterpret_cast<uint8_t *
>(
static_cast<uintptr_t
>(TAddr));
370 <<
format(
"0x%016x", TAddr) <<
", Size = " << Size
372 EHFramesRegister(Addr, Size);
378 auto I = Allocators.find(Id);
379 if (
I == Allocators.end())
383 void *LocalAllocAddr =
nullptr;
384 if (
auto Err =
Allocator.allocate(LocalAllocAddr, Size, Align))
385 return std::move(Err);
387 LLVM_DEBUG(
dbgs() <<
" Allocator " << Id <<
" reserved " << LocalAllocAddr
388 <<
" (" << Size <<
" bytes, alignment " << Align
392 reinterpret_cast<uintptr_t
>(LocalAllocAddr));
399 auto I = Allocators.find(Id);
400 if (
I == Allocators.end())
404 void *LocalAddr =
reinterpret_cast<void *
>(
static_cast<uintptr_t
>(Addr));
405 LLVM_DEBUG(
dbgs() <<
" Allocator " << Id <<
" set permissions on " 406 << LocalAddr <<
" to " 410 return Allocator.setProtections(LocalAddr, Flags);
413 Error handleTerminateSession() {
414 TerminateFlag =
true;
426 <<
" = " <<
format(
"0x%016x", PtrVal) <<
"\n");
428 reinterpret_cast<uintptr_t *
>(
static_cast<uintptr_t
>(Addr));
429 *Ptr =
static_cast<uintptr_t
>(PtrVal);
435 std::map<ResourceIdMgr::ResourceId, Allocator> Allocators;
436 using ISBlockOwnerList = std::vector<typename TargetT::IndirectStubsInfo>;
437 std::map<ResourceIdMgr::ResourceId, ISBlockOwnerList> IndirectStubsOwners;
439 std::vector<sys::OwningMemoryBlock> TrampolineBlocks;
440 bool TerminateFlag =
false;
449 #endif // LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETSERVER_H std::function< JITTargetAddress(const std::string &Name)> SymbolLookupFtor
This class represents lattice values for constants.
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
OrcRemoteTargetServer & operator=(const OrcRemoteTargetServer &)=delete
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
Optional< std::vector< StOtherPiece > > Other
static std::error_code releaseMappedMemory(MemoryBlock &Block)
This method releases a block of memory that was allocated with the allocateMappedMemory method...
block Block Frequency true
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)
bool receivedTerminate() const
Tagged union holding either a T or a Error.
static unsigned getPageSizeEstimate()
Get the process's estimated page size.
std::function< void(uint8_t *Addr, uint32_t Size)> EHFrameRegistrationFtor
uint64_t JITTargetAddress
Represents an address in the target process's address space.
JITTargetAddress getDst() const
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
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...
This struct is a compact representation of a valid (non-zero power of two) alignment.
static ErrorSuccess success()
Create a success value.
This class encapsulates the notion of a memory block which has an address and a size.
Owning version of MemoryBlock.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Provides a library for accessing information about this process and other processes on the operating ...
static cl::opt< int > PageSize("imp-null-check-page-size", cl::desc("The page size of the target in bytes"), cl::init(4096), cl::Hidden)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
Expected< JITTargetAddress > requestCompile(JITTargetAddress TrampolineAddr)
SingleThreadedRPCEndpoint(rpc::RawByteChannel &C, bool LazyAutoNegotiation)
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
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...