1 //===---- OrcRemoteTargetClient.h - Orc Remote-target Client ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the OrcRemoteTargetClient class and helpers. This class
11 // can be used to communicate over an RPCChannel with an OrcRemoteTargetServer
12 // instance to support remote-JITing.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETCLIENT_H
17 #define LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETCLIENT_H
19 #include "IndirectionUtils.h"
20 #include "OrcRemoteTargetRPCAPI.h"
21 #include <system_error>
23 #define DEBUG_TYPE "orc-remote"
29 /// This class provides utilities (including memory manager, indirect stubs
30 /// manager, and compile callback manager types) that support remote JITing
33 /// Each of the utility classes talks to a JIT server (an instance of the
34 /// OrcRemoteTargetServer class) via an RPC system (see RPCUtils.h) to carry out
36 template <typename ChannelT>
37 class OrcRemoteTargetClient : public OrcRemoteTargetRPCAPI {
39 /// Remote memory manager.
40 class RCMemoryManager : public RuntimeDyld::MemoryManager {
42 RCMemoryManager(OrcRemoteTargetClient &Client, ResourceIdMgr::ResourceId Id)
43 : Client(Client), Id(Id) {
44 DEBUG(dbgs() << "Created remote allocator " << Id << "\n");
47 RCMemoryManager(RCMemoryManager &&Other)
48 : Client(std::move(Other.Client)), Id(std::move(Other.Id)),
49 Unmapped(std::move(Other.Unmapped)),
50 Unfinalized(std::move(Other.Unfinalized)) {}
52 RCMemoryManager operator=(RCMemoryManager &&Other) {
53 Client = std::move(Other.Client);
54 Id = std::move(Other.Id);
55 Unmapped = std::move(Other.Unmapped);
56 Unfinalized = std::move(Other.Unfinalized);
61 Client.destroyRemoteAllocator(Id);
62 DEBUG(dbgs() << "Destroyed remote allocator " << Id << "\n");
65 uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
67 StringRef SectionName) override {
68 Unmapped.back().CodeAllocs.emplace_back(Size, Alignment);
69 uint8_t *Alloc = reinterpret_cast<uint8_t *>(
70 Unmapped.back().CodeAllocs.back().getLocalAddress());
71 DEBUG(dbgs() << "Allocator " << Id << " allocated code for "
72 << SectionName << ": " << Alloc << " (" << Size
73 << " bytes, alignment " << Alignment << ")\n");
77 uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
78 unsigned SectionID, StringRef SectionName,
79 bool IsReadOnly) override {
81 Unmapped.back().RODataAllocs.emplace_back(Size, Alignment);
82 uint8_t *Alloc = reinterpret_cast<uint8_t *>(
83 Unmapped.back().RODataAllocs.back().getLocalAddress());
84 DEBUG(dbgs() << "Allocator " << Id << " allocated ro-data for "
85 << SectionName << ": " << Alloc << " (" << Size
86 << " bytes, alignment " << Alignment << ")\n");
90 Unmapped.back().RWDataAllocs.emplace_back(Size, Alignment);
91 uint8_t *Alloc = reinterpret_cast<uint8_t *>(
92 Unmapped.back().RWDataAllocs.back().getLocalAddress());
93 DEBUG(dbgs() << "Allocator " << Id << " allocated rw-data for "
94 << SectionName << ": " << Alloc << " (" << Size
95 << " bytes, alignment " << Alignment << "\n");
99 void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign,
100 uintptr_t RODataSize, uint32_t RODataAlign,
101 uintptr_t RWDataSize,
102 uint32_t RWDataAlign) override {
103 Unmapped.push_back(ObjectAllocs());
105 DEBUG(dbgs() << "Allocator " << Id << " reserved:\n");
108 std::error_code EC = Client.reserveMem(Unmapped.back().RemoteCodeAddr,
109 Id, CodeSize, CodeAlign);
110 // FIXME; Add error to poll.
111 assert(!EC && "Failed reserving remote memory.");
113 DEBUG(dbgs() << " code: "
114 << format("0x%016x", Unmapped.back().RemoteCodeAddr)
115 << " (" << CodeSize << " bytes, alignment " << CodeAlign
119 if (RODataSize != 0) {
120 std::error_code EC = Client.reserveMem(Unmapped.back().RemoteRODataAddr,
121 Id, RODataSize, RODataAlign);
122 // FIXME; Add error to poll.
123 assert(!EC && "Failed reserving remote memory.");
125 DEBUG(dbgs() << " ro-data: "
126 << format("0x%016x", Unmapped.back().RemoteRODataAddr)
127 << " (" << RODataSize << " bytes, alignment "
128 << RODataAlign << ")\n");
131 if (RWDataSize != 0) {
132 std::error_code EC = Client.reserveMem(Unmapped.back().RemoteRWDataAddr,
133 Id, RWDataSize, RWDataAlign);
134 // FIXME; Add error to poll.
135 assert(!EC && "Failed reserving remote memory.");
137 DEBUG(dbgs() << " rw-data: "
138 << format("0x%016x", Unmapped.back().RemoteRWDataAddr)
139 << " (" << RWDataSize << " bytes, alignment "
140 << RWDataAlign << ")\n");
144 bool needsToReserveAllocationSpace() override { return true; }
146 void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
147 size_t Size) override {}
149 void deregisterEHFrames(uint8_t *addr, uint64_t LoadAddr,
150 size_t Size) override {}
152 void notifyObjectLoaded(RuntimeDyld &Dyld,
153 const object::ObjectFile &Obj) override {
154 DEBUG(dbgs() << "Allocator " << Id << " applied mappings:\n");
155 for (auto &ObjAllocs : Unmapped) {
157 TargetAddress NextCodeAddr = ObjAllocs.RemoteCodeAddr;
158 for (auto &Alloc : ObjAllocs.CodeAllocs) {
159 NextCodeAddr = RoundUpToAlignment(NextCodeAddr, Alloc.getAlign());
160 Dyld.mapSectionAddress(Alloc.getLocalAddress(), NextCodeAddr);
161 DEBUG(dbgs() << " code: "
162 << static_cast<void *>(Alloc.getLocalAddress())
163 << " -> " << format("0x%016x", NextCodeAddr) << "\n");
164 Alloc.setRemoteAddress(NextCodeAddr);
165 NextCodeAddr += Alloc.getSize();
169 TargetAddress NextRODataAddr = ObjAllocs.RemoteRODataAddr;
170 for (auto &Alloc : ObjAllocs.RODataAllocs) {
172 RoundUpToAlignment(NextRODataAddr, Alloc.getAlign());
173 Dyld.mapSectionAddress(Alloc.getLocalAddress(), NextRODataAddr);
174 DEBUG(dbgs() << " ro-data: "
175 << static_cast<void *>(Alloc.getLocalAddress())
176 << " -> " << format("0x%016x", NextRODataAddr)
178 Alloc.setRemoteAddress(NextRODataAddr);
179 NextRODataAddr += Alloc.getSize();
183 TargetAddress NextRWDataAddr = ObjAllocs.RemoteRWDataAddr;
184 for (auto &Alloc : ObjAllocs.RWDataAllocs) {
186 RoundUpToAlignment(NextRWDataAddr, Alloc.getAlign());
187 Dyld.mapSectionAddress(Alloc.getLocalAddress(), NextRWDataAddr);
188 DEBUG(dbgs() << " rw-data: "
189 << static_cast<void *>(Alloc.getLocalAddress())
190 << " -> " << format("0x%016x", NextRWDataAddr)
192 Alloc.setRemoteAddress(NextRWDataAddr);
193 NextRWDataAddr += Alloc.getSize();
196 Unfinalized.push_back(std::move(ObjAllocs));
201 bool finalizeMemory(std::string *ErrMsg = nullptr) override {
202 DEBUG(dbgs() << "Allocator " << Id << " finalizing:\n");
204 for (auto &ObjAllocs : Unfinalized) {
206 for (auto &Alloc : ObjAllocs.CodeAllocs) {
207 DEBUG(dbgs() << " copying code: "
208 << static_cast<void *>(Alloc.getLocalAddress()) << " -> "
209 << format("0x%016x", Alloc.getRemoteAddress()) << " ("
210 << Alloc.getSize() << " bytes)\n");
211 Client.writeMem(Alloc.getRemoteAddress(), Alloc.getLocalAddress(),
215 if (ObjAllocs.RemoteCodeAddr) {
216 DEBUG(dbgs() << " setting R-X permissions on code block: "
217 << format("0x%016x", ObjAllocs.RemoteCodeAddr) << "\n");
218 Client.setProtections(Id, ObjAllocs.RemoteCodeAddr,
219 sys::Memory::MF_READ | sys::Memory::MF_EXEC);
222 for (auto &Alloc : ObjAllocs.RODataAllocs) {
223 DEBUG(dbgs() << " copying ro-data: "
224 << static_cast<void *>(Alloc.getLocalAddress()) << " -> "
225 << format("0x%016x", Alloc.getRemoteAddress()) << " ("
226 << Alloc.getSize() << " bytes)\n");
227 Client.writeMem(Alloc.getRemoteAddress(), Alloc.getLocalAddress(),
231 if (ObjAllocs.RemoteRODataAddr) {
232 DEBUG(dbgs() << " setting R-- permissions on ro-data block: "
233 << format("0x%016x", ObjAllocs.RemoteRODataAddr)
235 Client.setProtections(Id, ObjAllocs.RemoteRODataAddr,
236 sys::Memory::MF_READ);
239 for (auto &Alloc : ObjAllocs.RWDataAllocs) {
240 DEBUG(dbgs() << " copying rw-data: "
241 << static_cast<void *>(Alloc.getLocalAddress()) << " -> "
242 << format("0x%016x", Alloc.getRemoteAddress()) << " ("
243 << Alloc.getSize() << " bytes)\n");
244 Client.writeMem(Alloc.getRemoteAddress(), Alloc.getLocalAddress(),
248 if (ObjAllocs.RemoteRWDataAddr) {
249 DEBUG(dbgs() << " setting RW- permissions on rw-data block: "
250 << format("0x%016x", ObjAllocs.RemoteRWDataAddr)
252 Client.setProtections(Id, ObjAllocs.RemoteRWDataAddr,
253 sys::Memory::MF_READ | sys::Memory::MF_WRITE);
264 Alloc(uint64_t Size, unsigned Align)
265 : Size(Size), Align(Align), Contents(new char[Size + Align - 1]),
269 : Size(std::move(Other.Size)), Align(std::move(Other.Align)),
270 Contents(std::move(Other.Contents)),
271 RemoteAddr(std::move(Other.RemoteAddr)) {}
273 Alloc &operator=(Alloc &&Other) {
274 Size = std::move(Other.Size);
275 Align = std::move(Other.Align);
276 Contents = std::move(Other.Contents);
277 RemoteAddr = std::move(Other.RemoteAddr);
281 uint64_t getSize() const { return Size; }
283 unsigned getAlign() const { return Align; }
285 char *getLocalAddress() const {
286 uintptr_t LocalAddr = reinterpret_cast<uintptr_t>(Contents.get());
287 LocalAddr = RoundUpToAlignment(LocalAddr, Align);
288 return reinterpret_cast<char *>(LocalAddr);
291 void setRemoteAddress(TargetAddress RemoteAddr) {
292 this->RemoteAddr = RemoteAddr;
295 TargetAddress getRemoteAddress() const { return RemoteAddr; }
300 std::unique_ptr<char[]> Contents;
301 TargetAddress RemoteAddr;
304 struct ObjectAllocs {
306 : RemoteCodeAddr(0), RemoteRODataAddr(0), RemoteRWDataAddr(0) {}
308 ObjectAllocs(ObjectAllocs &&Other)
309 : RemoteCodeAddr(std::move(Other.RemoteCodeAddr)),
310 RemoteRODataAddr(std::move(Other.RemoteRODataAddr)),
311 RemoteRWDataAddr(std::move(Other.RemoteRWDataAddr)),
312 CodeAllocs(std::move(Other.CodeAllocs)),
313 RODataAllocs(std::move(Other.RODataAllocs)),
314 RWDataAllocs(std::move(Other.RWDataAllocs)) {}
316 ObjectAllocs &operator=(ObjectAllocs &&Other) {
317 RemoteCodeAddr = std::move(Other.RemoteCodeAddr);
318 RemoteRODataAddr = std::move(Other.RemoteRODataAddr);
319 RemoteRWDataAddr = std::move(Other.RemoteRWDataAddr);
320 CodeAllocs = std::move(Other.CodeAllocs);
321 RODataAllocs = std::move(Other.RODataAllocs);
322 RWDataAllocs = std::move(Other.RWDataAllocs);
326 TargetAddress RemoteCodeAddr;
327 TargetAddress RemoteRODataAddr;
328 TargetAddress RemoteRWDataAddr;
329 std::vector<Alloc> CodeAllocs, RODataAllocs, RWDataAllocs;
332 OrcRemoteTargetClient &Client;
333 ResourceIdMgr::ResourceId Id;
334 std::vector<ObjectAllocs> Unmapped;
335 std::vector<ObjectAllocs> Unfinalized;
338 /// Remote indirect stubs manager.
339 class RCIndirectStubsManager : public IndirectStubsManager {
341 RCIndirectStubsManager(OrcRemoteTargetClient &Remote,
342 ResourceIdMgr::ResourceId Id)
343 : Remote(Remote), Id(Id) {}
345 ~RCIndirectStubsManager() { Remote.destroyIndirectStubsManager(Id); }
347 std::error_code createStub(StringRef StubName, TargetAddress StubAddr,
348 JITSymbolFlags StubFlags) override {
349 if (auto EC = reserveStubs(1))
352 return createStubInternal(StubName, StubAddr, StubFlags);
355 std::error_code createStubs(const StubInitsMap &StubInits) override {
356 if (auto EC = reserveStubs(StubInits.size()))
359 for (auto &Entry : StubInits)
360 if (auto EC = createStubInternal(Entry.first(), Entry.second.first,
361 Entry.second.second))
364 return std::error_code();
367 JITSymbol findStub(StringRef Name, bool ExportedStubsOnly) override {
368 auto I = StubIndexes.find(Name);
369 if (I == StubIndexes.end())
371 auto Key = I->second.first;
372 auto Flags = I->second.second;
373 auto StubSymbol = JITSymbol(getStubAddr(Key), Flags);
374 if (ExportedStubsOnly && !StubSymbol.isExported())
379 JITSymbol findPointer(StringRef Name) override {
380 auto I = StubIndexes.find(Name);
381 if (I == StubIndexes.end())
383 auto Key = I->second.first;
384 auto Flags = I->second.second;
385 return JITSymbol(getPtrAddr(Key), Flags);
388 std::error_code updatePointer(StringRef Name,
389 TargetAddress NewAddr) override {
390 auto I = StubIndexes.find(Name);
391 assert(I != StubIndexes.end() && "No stub pointer for symbol");
392 auto Key = I->second.first;
393 return Remote.writePointer(getPtrAddr(Key), NewAddr);
397 struct RemoteIndirectStubsInfo {
398 RemoteIndirectStubsInfo(TargetAddress StubBase, TargetAddress PtrBase,
400 : StubBase(StubBase), PtrBase(PtrBase), NumStubs(NumStubs) {}
401 TargetAddress StubBase;
402 TargetAddress PtrBase;
406 OrcRemoteTargetClient &Remote;
407 ResourceIdMgr::ResourceId Id;
408 std::vector<RemoteIndirectStubsInfo> RemoteIndirectStubsInfos;
409 typedef std::pair<uint16_t, uint16_t> StubKey;
410 std::vector<StubKey> FreeStubs;
411 StringMap<std::pair<StubKey, JITSymbolFlags>> StubIndexes;
413 std::error_code reserveStubs(unsigned NumStubs) {
414 if (NumStubs <= FreeStubs.size())
415 return std::error_code();
417 unsigned NewStubsRequired = NumStubs - FreeStubs.size();
418 TargetAddress StubBase;
419 TargetAddress PtrBase;
420 unsigned NumStubsEmitted;
422 Remote.emitIndirectStubs(StubBase, PtrBase, NumStubsEmitted, Id,
425 unsigned NewBlockId = RemoteIndirectStubsInfos.size();
426 RemoteIndirectStubsInfos.push_back(
427 RemoteIndirectStubsInfo(StubBase, PtrBase, NumStubsEmitted));
429 for (unsigned I = 0; I < NumStubsEmitted; ++I)
430 FreeStubs.push_back(std::make_pair(NewBlockId, I));
432 return std::error_code();
435 std::error_code createStubInternal(StringRef StubName,
436 TargetAddress InitAddr,
437 JITSymbolFlags StubFlags) {
438 auto Key = FreeStubs.back();
439 FreeStubs.pop_back();
440 StubIndexes[StubName] = std::make_pair(Key, StubFlags);
441 return Remote.writePointer(getPtrAddr(Key), InitAddr);
444 TargetAddress getStubAddr(StubKey K) {
445 assert(RemoteIndirectStubsInfos[K.first].StubBase != 0 &&
446 "Missing stub address");
447 return RemoteIndirectStubsInfos[K.first].StubBase +
448 K.second * Remote.getIndirectStubSize();
451 TargetAddress getPtrAddr(StubKey K) {
452 assert(RemoteIndirectStubsInfos[K.first].PtrBase != 0 &&
453 "Missing pointer address");
454 return RemoteIndirectStubsInfos[K.first].PtrBase +
455 K.second * Remote.getPointerSize();
459 /// Remote compile callback manager.
460 class RCCompileCallbackManager : public JITCompileCallbackManager {
462 RCCompileCallbackManager(TargetAddress ErrorHandlerAddress,
463 OrcRemoteTargetClient &Remote)
464 : JITCompileCallbackManager(ErrorHandlerAddress), Remote(Remote) {
465 assert(!Remote.CompileCallback && "Compile callback already set");
466 Remote.CompileCallback = [this](TargetAddress TrampolineAddr) {
467 return executeCompileCallback(TrampolineAddr);
469 Remote.emitResolverBlock();
474 TargetAddress BlockAddr = 0;
475 uint32_t NumTrampolines = 0;
476 auto EC = Remote.emitTrampolineBlock(BlockAddr, NumTrampolines);
477 assert(!EC && "Failed to create trampolines");
479 uint32_t TrampolineSize = Remote.getTrampolineSize();
480 for (unsigned I = 0; I < NumTrampolines; ++I)
481 this->AvailableTrampolines.push_back(BlockAddr + (I * TrampolineSize));
484 OrcRemoteTargetClient &Remote;
487 /// Create an OrcRemoteTargetClient.
488 /// Channel is the ChannelT instance to communicate on. It is assumed that
489 /// the channel is ready to be read from and written to.
490 static ErrorOr<OrcRemoteTargetClient> Create(ChannelT &Channel) {
492 OrcRemoteTargetClient H(Channel, EC);
498 /// Call the int(void) function at the given address in the target and return
500 std::error_code callIntVoid(int &Result, TargetAddress Addr) {
501 DEBUG(dbgs() << "Calling int(*)(void) " << format("0x%016x", Addr) << "\n");
503 if (auto EC = call<CallIntVoid>(Channel, Addr))
507 if (auto EC = listenForCompileRequests(NextProcId))
510 if (NextProcId != CallIntVoidResponseId)
511 return orcError(OrcErrorCode::UnexpectedRPCCall);
513 return handle<CallIntVoidResponse>(Channel, [&](int R) {
515 DEBUG(dbgs() << "Result: " << R << "\n");
516 return std::error_code();
520 /// Call the int(int, char*[]) function at the given address in the target and
521 /// return its result.
522 std::error_code callMain(int &Result, TargetAddress Addr,
523 const std::vector<std::string> &Args) {
524 DEBUG(dbgs() << "Calling int(*)(int, char*[]) " << format("0x%016x", Addr)
527 if (auto EC = call<CallMain>(Channel, Addr, Args))
531 if (auto EC = listenForCompileRequests(NextProcId))
534 if (NextProcId != CallMainResponseId)
535 return orcError(OrcErrorCode::UnexpectedRPCCall);
537 return handle<CallMainResponse>(Channel, [&](int R) {
539 DEBUG(dbgs() << "Result: " << R << "\n");
540 return std::error_code();
544 /// Call the void() function at the given address in the target and wait for
546 std::error_code callVoidVoid(TargetAddress Addr) {
547 DEBUG(dbgs() << "Calling void(*)(void) " << format("0x%016x", Addr)
550 if (auto EC = call<CallVoidVoid>(Channel, Addr))
554 if (auto EC = listenForCompileRequests(NextProcId))
557 if (NextProcId != CallVoidVoidResponseId)
558 return orcError(OrcErrorCode::UnexpectedRPCCall);
560 return handle<CallVoidVoidResponse>(Channel, doNothing);
563 /// Create an RCMemoryManager which will allocate its memory on the remote
566 createRemoteMemoryManager(std::unique_ptr<RCMemoryManager> &MM) {
567 assert(!MM && "MemoryManager should be null before creation.");
569 auto Id = AllocatorIds.getNext();
570 if (auto EC = call<CreateRemoteAllocator>(Channel, Id))
572 MM = llvm::make_unique<RCMemoryManager>(*this, Id);
573 return std::error_code();
576 /// Create an RCIndirectStubsManager that will allocate stubs on the remote
579 createIndirectStubsManager(std::unique_ptr<RCIndirectStubsManager> &I) {
580 assert(!I && "Indirect stubs manager should be null before creation.");
581 auto Id = IndirectStubOwnerIds.getNext();
582 if (auto EC = call<CreateIndirectStubsOwner>(Channel, Id))
584 I = llvm::make_unique<RCIndirectStubsManager>(*this, Id);
585 return std::error_code();
588 /// Search for symbols in the remote process. Note: This should be used by
589 /// symbol resolvers *after* they've searched the local symbol table in the
591 std::error_code getSymbolAddress(TargetAddress &Addr, StringRef Name) {
592 // Check for an 'out-of-band' error, e.g. from an MM destructor.
594 return ExistingError;
596 // Request remote symbol address.
597 if (auto EC = call<GetSymbolAddress>(Channel, Name))
600 return expect<GetSymbolAddressResponse>(Channel, [&](TargetAddress &A) {
602 DEBUG(dbgs() << "Remote address lookup " << Name << " = "
603 << format("0x%016x", Addr) << "\n");
604 return std::error_code();
608 /// Get the triple for the remote target.
609 const std::string &getTargetTriple() const { return RemoteTargetTriple; }
611 std::error_code terminateSession() { return call<TerminateSession>(Channel); }
614 OrcRemoteTargetClient(ChannelT &Channel, std::error_code &EC)
615 : Channel(Channel), RemotePointerSize(0), RemotePageSize(0),
616 RemoteTrampolineSize(0), RemoteIndirectStubSize(0) {
617 if ((EC = call<GetRemoteInfo>(Channel)))
620 EC = expect<GetRemoteInfoResponse>(
621 Channel, readArgs(RemoteTargetTriple, RemotePointerSize, RemotePageSize,
622 RemoteTrampolineSize, RemoteIndirectStubSize));
625 void destroyRemoteAllocator(ResourceIdMgr::ResourceId Id) {
626 if (auto EC = call<DestroyRemoteAllocator>(Channel, Id)) {
627 // FIXME: This will be triggered by a removeModuleSet call: Propagate
628 // error return up through that.
629 llvm_unreachable("Failed to destroy remote allocator.");
630 AllocatorIds.release(Id);
634 std::error_code destroyIndirectStubsManager(ResourceIdMgr::ResourceId Id) {
635 IndirectStubOwnerIds.release(Id);
636 return call<DestroyIndirectStubsOwner>(Channel, Id);
639 std::error_code emitIndirectStubs(TargetAddress &StubBase,
640 TargetAddress &PtrBase,
641 uint32_t &NumStubsEmitted,
642 ResourceIdMgr::ResourceId Id,
643 uint32_t NumStubsRequired) {
644 if (auto EC = call<EmitIndirectStubs>(Channel, Id, NumStubsRequired))
647 return expect<EmitIndirectStubsResponse>(
648 Channel, readArgs(StubBase, PtrBase, NumStubsEmitted));
651 std::error_code emitResolverBlock() {
652 // Check for an 'out-of-band' error, e.g. from an MM destructor.
654 return ExistingError;
656 return call<EmitResolverBlock>(Channel);
659 std::error_code emitTrampolineBlock(TargetAddress &BlockAddr,
660 uint32_t &NumTrampolines) {
661 // Check for an 'out-of-band' error, e.g. from an MM destructor.
663 return ExistingError;
665 if (auto EC = call<EmitTrampolineBlock>(Channel))
668 return expect<EmitTrampolineBlockResponse>(
669 Channel, [&](TargetAddress BAddr, uint32_t NTrampolines) {
671 NumTrampolines = NTrampolines;
672 return std::error_code();
676 uint32_t getIndirectStubSize() const { return RemoteIndirectStubSize; }
677 uint32_t getPageSize() const { return RemotePageSize; }
678 uint32_t getPointerSize() const { return RemotePointerSize; }
680 uint32_t getTrampolineSize() const { return RemoteTrampolineSize; }
682 std::error_code listenForCompileRequests(uint32_t &NextId) {
683 // Check for an 'out-of-band' error, e.g. from an MM destructor.
685 return ExistingError;
687 if (auto EC = getNextProcId(Channel, NextId))
690 while (NextId == RequestCompileId) {
691 TargetAddress TrampolineAddr = 0;
692 if (auto EC = handle<RequestCompile>(Channel, readArgs(TrampolineAddr)))
695 TargetAddress ImplAddr = CompileCallback(TrampolineAddr);
696 if (auto EC = call<RequestCompileResponse>(Channel, ImplAddr))
699 if (auto EC = getNextProcId(Channel, NextId))
703 return std::error_code();
706 std::error_code readMem(char *Dst, TargetAddress Src, uint64_t Size) {
707 // Check for an 'out-of-band' error, e.g. from an MM destructor.
709 return ExistingError;
711 if (auto EC = call<ReadMem>(Channel, Src, Size))
714 if (auto EC = expect<ReadMemResponse>(
715 Channel, [&]() { return Channel.readBytes(Dst, Size); }))
718 return std::error_code();
721 std::error_code reserveMem(TargetAddress &RemoteAddr,
722 ResourceIdMgr::ResourceId Id, uint64_t Size,
725 // Check for an 'out-of-band' error, e.g. from an MM destructor.
727 return ExistingError;
729 if (auto EC = call<ReserveMem>(Channel, Id, Size, Align))
732 if (std::error_code EC =
733 expect<ReserveMemResponse>(Channel, [&](TargetAddress Addr) {
735 return std::error_code();
739 return std::error_code();
742 std::error_code setProtections(ResourceIdMgr::ResourceId Id,
743 TargetAddress RemoteSegAddr,
744 unsigned ProtFlags) {
745 return call<SetProtections>(Channel, Id, RemoteSegAddr, ProtFlags);
748 std::error_code writeMem(TargetAddress Addr, const char *Src, uint64_t Size) {
749 // Check for an 'out-of-band' error, e.g. from an MM destructor.
751 return ExistingError;
753 // Make the send call.
754 if (auto EC = call<WriteMem>(Channel, Addr, Size))
757 // Follow this up with the section contents.
758 if (auto EC = Channel.appendBytes(Src, Size))
761 return Channel.send();
764 std::error_code writePointer(TargetAddress Addr, TargetAddress PtrVal) {
765 // Check for an 'out-of-band' error, e.g. from an MM destructor.
767 return ExistingError;
769 return call<WritePtr>(Channel, Addr, PtrVal);
772 static std::error_code doNothing() { return std::error_code(); }
775 std::error_code ExistingError;
776 std::string RemoteTargetTriple;
777 uint32_t RemotePointerSize;
778 uint32_t RemotePageSize;
779 uint32_t RemoteTrampolineSize;
780 uint32_t RemoteIndirectStubSize;
781 ResourceIdMgr AllocatorIds, IndirectStubOwnerIds;
782 std::function<TargetAddress(TargetAddress)> CompileCallback;
785 } // end namespace remote
786 } // end namespace orc
787 } // end namespace llvm