8b391acf0be6246f3295eac5ba771ff85a683af0
[oota-llvm.git] / include / llvm / ExecutionEngine / Orc / OrcRemoteTargetClient.h
1 //===---- OrcRemoteTargetClient.h - Orc Remote-target Client ----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETCLIENT_H
17 #define LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETCLIENT_H
18
19 #include "IndirectionUtils.h"
20 #include "OrcRemoteTargetRPCAPI.h"
21 #include <system_error>
22
23 #define DEBUG_TYPE "orc-remote"
24
25 namespace llvm {
26 namespace orc {
27 namespace remote {
28
29 /// This class provides utilities (including memory manager, indirect stubs
30 /// manager, and compile callback manager types) that support remote JITing
31 /// in ORC.
32 ///
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
35 /// its actions.
36 template <typename ChannelT>
37 class OrcRemoteTargetClient : public OrcRemoteTargetRPCAPI {
38 public:
39   /// Remote memory manager.
40   class RCMemoryManager : public RuntimeDyld::MemoryManager {
41   public:
42     RCMemoryManager(OrcRemoteTargetClient &Client, ResourceIdMgr::ResourceId Id)
43         : Client(Client), Id(Id) {
44       DEBUG(dbgs() << "Created remote allocator " << Id << "\n");
45     }
46
47     ~RCMemoryManager() {
48       Client.destroyRemoteAllocator(Id);
49       DEBUG(dbgs() << "Destroyed remote allocator " << Id << "\n");
50     }
51
52     uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
53                                  unsigned SectionID,
54                                  StringRef SectionName) override {
55       Unmapped.back().CodeAllocs.emplace_back(Size, Alignment);
56       uint8_t *Alloc = reinterpret_cast<uint8_t *>(
57           Unmapped.back().CodeAllocs.back().getLocalAddress());
58       DEBUG(dbgs() << "Allocator " << Id << " allocated code for "
59                    << SectionName << ": " << Alloc << " (" << Size
60                    << " bytes, alignment " << Alignment << ")\n");
61       return Alloc;
62     }
63
64     uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
65                                  unsigned SectionID, StringRef SectionName,
66                                  bool IsReadOnly) override {
67       if (IsReadOnly) {
68         Unmapped.back().RODataAllocs.emplace_back(Size, Alignment);
69         uint8_t *Alloc = reinterpret_cast<uint8_t *>(
70             Unmapped.back().RODataAllocs.back().getLocalAddress());
71         DEBUG(dbgs() << "Allocator " << Id << " allocated ro-data for "
72                      << SectionName << ": " << Alloc << " (" << Size
73                      << " bytes, alignment " << Alignment << ")\n");
74         return Alloc;
75       } // else...
76
77       Unmapped.back().RWDataAllocs.emplace_back(Size, Alignment);
78       uint8_t *Alloc = reinterpret_cast<uint8_t *>(
79           Unmapped.back().RWDataAllocs.back().getLocalAddress());
80       DEBUG(dbgs() << "Allocator " << Id << " allocated rw-data for "
81                    << SectionName << ": " << Alloc << " (" << Size
82                    << " bytes, alignment " << Alignment << "\n");
83       return Alloc;
84     }
85
86     void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign,
87                                 uintptr_t RODataSize, uint32_t RODataAlign,
88                                 uintptr_t RWDataSize,
89                                 uint32_t RWDataAlign) override {
90       Unmapped.push_back(ObjectAllocs());
91
92       DEBUG(dbgs() << "Allocator " << Id << " reserved:\n");
93
94       if (CodeSize != 0) {
95         if (std::error_code EC = Client.reserveMem(
96                 Unmapped.back().RemoteCodeAddr, Id, CodeSize, CodeAlign)) {
97           (void)EC;
98           // FIXME; Add error to poll.
99           llvm_unreachable("Failed reserving remote memory.");
100         }
101         DEBUG(dbgs() << "  code: "
102                      << format("0x%016x", Unmapped.back().RemoteCodeAddr)
103                      << " (" << CodeSize << " bytes, alignment " << CodeAlign
104                      << ")\n");
105       }
106
107       if (RODataSize != 0) {
108         if (auto EC = Client.reserveMem(Unmapped.back().RemoteRODataAddr, Id,
109                                         RODataSize, RODataAlign)) {
110           // FIXME; Add error to poll.
111           llvm_unreachable("Failed reserving remote memory.");
112         }
113         DEBUG(dbgs() << "  ro-data: "
114                      << format("0x%016x", Unmapped.back().RemoteRODataAddr)
115                      << " (" << RODataSize << " bytes, alignment "
116                      << RODataAlign << ")\n");
117       }
118
119       if (RWDataSize != 0) {
120         if (auto EC = Client.reserveMem(Unmapped.back().RemoteRWDataAddr, Id,
121                                         RWDataSize, RWDataAlign)) {
122           // FIXME; Add error to poll.
123           llvm_unreachable("Failed reserving remote memory.");
124         }
125         DEBUG(dbgs() << "  rw-data: "
126                      << format("0x%016x", Unmapped.back().RemoteRWDataAddr)
127                      << " (" << RWDataSize << " bytes, alignment "
128                      << RWDataAlign << ")\n");
129       }
130     }
131
132     bool needsToReserveAllocationSpace() override { return true; }
133
134     void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
135                           size_t Size) override {}
136
137     void deregisterEHFrames(uint8_t *addr, uint64_t LoadAddr,
138                             size_t Size) override {}
139
140     void notifyObjectLoaded(RuntimeDyld &Dyld,
141                             const object::ObjectFile &Obj) override {
142       DEBUG(dbgs() << "Allocator " << Id << " applied mappings:\n");
143       for (auto &ObjAllocs : Unmapped) {
144         {
145           TargetAddress NextCodeAddr = ObjAllocs.RemoteCodeAddr;
146           for (auto &Alloc : ObjAllocs.CodeAllocs) {
147             NextCodeAddr = RoundUpToAlignment(NextCodeAddr, Alloc.getAlign());
148             Dyld.mapSectionAddress(Alloc.getLocalAddress(), NextCodeAddr);
149             DEBUG(dbgs() << "     code: "
150                          << static_cast<void *>(Alloc.getLocalAddress())
151                          << " -> " << format("0x%016x", NextCodeAddr) << "\n");
152             Alloc.setRemoteAddress(NextCodeAddr);
153             NextCodeAddr += Alloc.getSize();
154           }
155         }
156         {
157           TargetAddress NextRODataAddr = ObjAllocs.RemoteRODataAddr;
158           for (auto &Alloc : ObjAllocs.RODataAllocs) {
159             NextRODataAddr =
160                 RoundUpToAlignment(NextRODataAddr, Alloc.getAlign());
161             Dyld.mapSectionAddress(Alloc.getLocalAddress(), NextRODataAddr);
162             DEBUG(dbgs() << "  ro-data: "
163                          << static_cast<void *>(Alloc.getLocalAddress())
164                          << " -> " << format("0x%016x", NextRODataAddr)
165                          << "\n");
166             Alloc.setRemoteAddress(NextRODataAddr);
167             NextRODataAddr += Alloc.getSize();
168           }
169         }
170         {
171           TargetAddress NextRWDataAddr = ObjAllocs.RemoteRWDataAddr;
172           for (auto &Alloc : ObjAllocs.RWDataAllocs) {
173             NextRWDataAddr =
174                 RoundUpToAlignment(NextRWDataAddr, Alloc.getAlign());
175             Dyld.mapSectionAddress(Alloc.getLocalAddress(), NextRWDataAddr);
176             DEBUG(dbgs() << "  rw-data: "
177                          << static_cast<void *>(Alloc.getLocalAddress())
178                          << " -> " << format("0x%016x", NextRWDataAddr)
179                          << "\n");
180             Alloc.setRemoteAddress(NextRWDataAddr);
181             NextRWDataAddr += Alloc.getSize();
182           }
183         }
184         Unfinalized.push_back(std::move(ObjAllocs));
185       }
186       Unmapped.clear();
187     }
188
189     bool finalizeMemory(std::string *ErrMsg = nullptr) override {
190       DEBUG(dbgs() << "Allocator " << Id << " finalizing:\n");
191
192       for (auto &ObjAllocs : Unfinalized) {
193
194         for (auto &Alloc : ObjAllocs.CodeAllocs) {
195           DEBUG(dbgs() << "  copying code: "
196                        << static_cast<void *>(Alloc.getLocalAddress()) << " -> "
197                        << format("0x%016x", Alloc.getRemoteAddress()) << " ("
198                        << Alloc.getSize() << " bytes)\n");
199           Client.writeMem(Alloc.getRemoteAddress(), Alloc.getLocalAddress(),
200                           Alloc.getSize());
201         }
202
203         if (ObjAllocs.RemoteCodeAddr) {
204           DEBUG(dbgs() << "  setting R-X permissions on code block: "
205                        << format("0x%016x", ObjAllocs.RemoteCodeAddr) << "\n");
206           Client.setProtections(Id, ObjAllocs.RemoteCodeAddr,
207                                 sys::Memory::MF_READ | sys::Memory::MF_EXEC);
208         }
209
210         for (auto &Alloc : ObjAllocs.RODataAllocs) {
211           DEBUG(dbgs() << "  copying ro-data: "
212                        << static_cast<void *>(Alloc.getLocalAddress()) << " -> "
213                        << format("0x%016x", Alloc.getRemoteAddress()) << " ("
214                        << Alloc.getSize() << " bytes)\n");
215           Client.writeMem(Alloc.getRemoteAddress(), Alloc.getLocalAddress(),
216                           Alloc.getSize());
217         }
218
219         if (ObjAllocs.RemoteRODataAddr) {
220           DEBUG(dbgs() << "  setting R-- permissions on ro-data block: "
221                        << format("0x%016x", ObjAllocs.RemoteRODataAddr)
222                        << "\n");
223           Client.setProtections(Id, ObjAllocs.RemoteRODataAddr,
224                                 sys::Memory::MF_READ);
225         }
226
227         for (auto &Alloc : ObjAllocs.RWDataAllocs) {
228           DEBUG(dbgs() << "  copying rw-data: "
229                        << static_cast<void *>(Alloc.getLocalAddress()) << " -> "
230                        << format("0x%016x", Alloc.getRemoteAddress()) << " ("
231                        << Alloc.getSize() << " bytes)\n");
232           Client.writeMem(Alloc.getRemoteAddress(), Alloc.getLocalAddress(),
233                           Alloc.getSize());
234         }
235
236         if (ObjAllocs.RemoteRWDataAddr) {
237           DEBUG(dbgs() << "  setting RW- permissions on rw-data block: "
238                        << format("0x%016x", ObjAllocs.RemoteRWDataAddr)
239                        << "\n");
240           Client.setProtections(Id, ObjAllocs.RemoteRWDataAddr,
241                                 sys::Memory::MF_READ | sys::Memory::MF_WRITE);
242         }
243       }
244       Unfinalized.clear();
245
246       return false;
247     }
248
249   private:
250     class Alloc {
251     public:
252       Alloc(uint64_t Size, unsigned Align)
253           : Size(Size), Align(Align), Contents(new char[Size + Align - 1]),
254             RemoteAddr(0) {}
255
256       Alloc(const Alloc&) = delete;
257       Alloc& operator=(const Alloc&) = delete;
258
259       Alloc(Alloc &&Other)
260           : Size(std::move(Other.Size)), Align(std::move(Other.Align)),
261             Contents(std::move(Other.Contents)),
262             RemoteAddr(std::move(Other.RemoteAddr)) {}
263
264       Alloc &operator=(Alloc &&Other) {
265         Size = std::move(Other.Size);
266         Align = std::move(Other.Align);
267         Contents = std::move(Other.Contents);
268         RemoteAddr = std::move(Other.RemoteAddr);
269         return *this;
270       }
271
272       uint64_t getSize() const { return Size; }
273
274       unsigned getAlign() const { return Align; }
275
276       char *getLocalAddress() const {
277         uintptr_t LocalAddr = reinterpret_cast<uintptr_t>(Contents.get());
278         LocalAddr = RoundUpToAlignment(LocalAddr, Align);
279         return reinterpret_cast<char *>(LocalAddr);
280       }
281
282       void setRemoteAddress(TargetAddress RemoteAddr) {
283         this->RemoteAddr = RemoteAddr;
284       }
285
286       TargetAddress getRemoteAddress() const { return RemoteAddr; }
287
288     private:
289       uint64_t Size;
290       unsigned Align;
291       std::unique_ptr<char[]> Contents;
292       TargetAddress RemoteAddr;
293     };
294
295     struct ObjectAllocs {
296       ObjectAllocs()
297           : RemoteCodeAddr(0), RemoteRODataAddr(0), RemoteRWDataAddr(0) {}
298       TargetAddress RemoteCodeAddr;
299       TargetAddress RemoteRODataAddr;
300       TargetAddress RemoteRWDataAddr;
301       std::vector<Alloc> CodeAllocs, RODataAllocs, RWDataAllocs;
302     };
303
304     OrcRemoteTargetClient &Client;
305     ResourceIdMgr::ResourceId Id;
306     std::vector<ObjectAllocs> Unmapped;
307     std::vector<ObjectAllocs> Unfinalized;
308   };
309
310   /// Remote indirect stubs manager.
311   class RCIndirectStubsManager : public IndirectStubsManager {
312   public:
313     RCIndirectStubsManager(OrcRemoteTargetClient &Remote,
314                            ResourceIdMgr::ResourceId Id)
315         : Remote(Remote), Id(Id) {}
316
317     ~RCIndirectStubsManager() { Remote.destroyIndirectStubsManager(Id); }
318
319     std::error_code createStub(StringRef StubName, TargetAddress StubAddr,
320                                JITSymbolFlags StubFlags) override {
321       if (auto EC = reserveStubs(1))
322         return EC;
323
324       return createStubInternal(StubName, StubAddr, StubFlags);
325     }
326
327     std::error_code createStubs(const StubInitsMap &StubInits) override {
328       if (auto EC = reserveStubs(StubInits.size()))
329         return EC;
330
331       for (auto &Entry : StubInits)
332         if (auto EC = createStubInternal(Entry.first(), Entry.second.first,
333                                          Entry.second.second))
334           return EC;
335
336       return std::error_code();
337     }
338
339     JITSymbol findStub(StringRef Name, bool ExportedStubsOnly) override {
340       auto I = StubIndexes.find(Name);
341       if (I == StubIndexes.end())
342         return nullptr;
343       auto Key = I->second.first;
344       auto Flags = I->second.second;
345       auto StubSymbol = JITSymbol(getStubAddr(Key), Flags);
346       if (ExportedStubsOnly && !StubSymbol.isExported())
347         return nullptr;
348       return StubSymbol;
349     }
350
351     JITSymbol findPointer(StringRef Name) override {
352       auto I = StubIndexes.find(Name);
353       if (I == StubIndexes.end())
354         return nullptr;
355       auto Key = I->second.first;
356       auto Flags = I->second.second;
357       return JITSymbol(getPtrAddr(Key), Flags);
358     }
359
360     std::error_code updatePointer(StringRef Name,
361                                   TargetAddress NewAddr) override {
362       auto I = StubIndexes.find(Name);
363       assert(I != StubIndexes.end() && "No stub pointer for symbol");
364       auto Key = I->second.first;
365       return Remote.writePointer(getPtrAddr(Key), NewAddr);
366     }
367
368   private:
369     struct RemoteIndirectStubsInfo {
370       RemoteIndirectStubsInfo(TargetAddress StubBase, TargetAddress PtrBase,
371                               unsigned NumStubs)
372           : StubBase(StubBase), PtrBase(PtrBase), NumStubs(NumStubs) {}
373       TargetAddress StubBase;
374       TargetAddress PtrBase;
375       unsigned NumStubs;
376     };
377
378     OrcRemoteTargetClient &Remote;
379     ResourceIdMgr::ResourceId Id;
380     std::vector<RemoteIndirectStubsInfo> RemoteIndirectStubsInfos;
381     typedef std::pair<uint16_t, uint16_t> StubKey;
382     std::vector<StubKey> FreeStubs;
383     StringMap<std::pair<StubKey, JITSymbolFlags>> StubIndexes;
384
385     std::error_code reserveStubs(unsigned NumStubs) {
386       if (NumStubs <= FreeStubs.size())
387         return std::error_code();
388
389       unsigned NewStubsRequired = NumStubs - FreeStubs.size();
390       TargetAddress StubBase;
391       TargetAddress PtrBase;
392       unsigned NumStubsEmitted;
393
394       Remote.emitIndirectStubs(StubBase, PtrBase, NumStubsEmitted, Id,
395                                NewStubsRequired);
396
397       unsigned NewBlockId = RemoteIndirectStubsInfos.size();
398       RemoteIndirectStubsInfos.push_back(
399           RemoteIndirectStubsInfo(StubBase, PtrBase, NumStubsEmitted));
400
401       for (unsigned I = 0; I < NumStubsEmitted; ++I)
402         FreeStubs.push_back(std::make_pair(NewBlockId, I));
403
404       return std::error_code();
405     }
406
407     std::error_code createStubInternal(StringRef StubName,
408                                        TargetAddress InitAddr,
409                                        JITSymbolFlags StubFlags) {
410       auto Key = FreeStubs.back();
411       FreeStubs.pop_back();
412       StubIndexes[StubName] = std::make_pair(Key, StubFlags);
413       return Remote.writePointer(getPtrAddr(Key), InitAddr);
414     }
415
416     TargetAddress getStubAddr(StubKey K) {
417       assert(RemoteIndirectStubsInfos[K.first].StubBase != 0 &&
418              "Missing stub address");
419       return RemoteIndirectStubsInfos[K.first].StubBase +
420              K.second * Remote.getIndirectStubSize();
421     }
422
423     TargetAddress getPtrAddr(StubKey K) {
424       assert(RemoteIndirectStubsInfos[K.first].PtrBase != 0 &&
425              "Missing pointer address");
426       return RemoteIndirectStubsInfos[K.first].PtrBase +
427              K.second * Remote.getPointerSize();
428     }
429   };
430
431   /// Remote compile callback manager.
432   class RCCompileCallbackManager : public JITCompileCallbackManager {
433   public:
434     RCCompileCallbackManager(TargetAddress ErrorHandlerAddress,
435                              OrcRemoteTargetClient &Remote)
436         : JITCompileCallbackManager(ErrorHandlerAddress), Remote(Remote) {
437       assert(!Remote.CompileCallback && "Compile callback already set");
438       Remote.CompileCallback = [this](TargetAddress TrampolineAddr) {
439         return executeCompileCallback(TrampolineAddr);
440       };
441       Remote.emitResolverBlock();
442     }
443
444   private:
445     void grow() {
446       TargetAddress BlockAddr = 0;
447       uint32_t NumTrampolines = 0;
448       auto EC = Remote.emitTrampolineBlock(BlockAddr, NumTrampolines);
449       assert(!EC && "Failed to create trampolines");
450
451       uint32_t TrampolineSize = Remote.getTrampolineSize();
452       for (unsigned I = 0; I < NumTrampolines; ++I)
453         this->AvailableTrampolines.push_back(BlockAddr + (I * TrampolineSize));
454     }
455
456     OrcRemoteTargetClient &Remote;
457   };
458
459   /// Create an OrcRemoteTargetClient.
460   /// Channel is the ChannelT instance to communicate on. It is assumed that
461   /// the channel is ready to be read from and written to.
462   static ErrorOr<OrcRemoteTargetClient> Create(ChannelT &Channel) {
463     std::error_code EC;
464     OrcRemoteTargetClient H(Channel, EC);
465     if (EC)
466       return EC;
467     return H;
468   }
469
470   /// Call the int(void) function at the given address in the target and return
471   /// its result.
472   std::error_code callIntVoid(int &Result, TargetAddress Addr) {
473     DEBUG(dbgs() << "Calling int(*)(void) " << format("0x%016x", Addr) << "\n");
474
475     if (auto EC = call<CallIntVoid>(Channel, Addr))
476       return EC;
477
478     unsigned NextProcId;
479     if (auto EC = listenForCompileRequests(NextProcId))
480       return EC;
481
482     if (NextProcId != CallIntVoidResponseId)
483       return orcError(OrcErrorCode::UnexpectedRPCCall);
484
485     return handle<CallIntVoidResponse>(Channel, [&](int R) {
486       Result = R;
487       DEBUG(dbgs() << "Result: " << R << "\n");
488       return std::error_code();
489     });
490   }
491
492   /// Call the int(int, char*[]) function at the given address in the target and
493   /// return its result.
494   std::error_code callMain(int &Result, TargetAddress Addr,
495                            const std::vector<std::string> &Args) {
496     DEBUG(dbgs() << "Calling int(*)(int, char*[]) " << format("0x%016x", Addr)
497                  << "\n");
498
499     if (auto EC = call<CallMain>(Channel, Addr, Args))
500       return EC;
501
502     unsigned NextProcId;
503     if (auto EC = listenForCompileRequests(NextProcId))
504       return EC;
505
506     if (NextProcId != CallMainResponseId)
507       return orcError(OrcErrorCode::UnexpectedRPCCall);
508
509     return handle<CallMainResponse>(Channel, [&](int R) {
510       Result = R;
511       DEBUG(dbgs() << "Result: " << R << "\n");
512       return std::error_code();
513     });
514   }
515
516   /// Call the void() function at the given address in the target and wait for
517   /// it to finish.
518   std::error_code callVoidVoid(TargetAddress Addr) {
519     DEBUG(dbgs() << "Calling void(*)(void) " << format("0x%016x", Addr)
520                  << "\n");
521
522     if (auto EC = call<CallVoidVoid>(Channel, Addr))
523       return EC;
524
525     unsigned NextProcId;
526     if (auto EC = listenForCompileRequests(NextProcId))
527       return EC;
528
529     if (NextProcId != CallVoidVoidResponseId)
530       return orcError(OrcErrorCode::UnexpectedRPCCall);
531
532     return handle<CallVoidVoidResponse>(Channel, doNothing);
533   }
534
535   /// Create an RCMemoryManager which will allocate its memory on the remote
536   /// target.
537   std::error_code
538   createRemoteMemoryManager(std::unique_ptr<RCMemoryManager> &MM) {
539     assert(!MM && "MemoryManager should be null before creation.");
540
541     auto Id = AllocatorIds.getNext();
542     if (auto EC = call<CreateRemoteAllocator>(Channel, Id))
543       return EC;
544     MM = llvm::make_unique<RCMemoryManager>(*this, Id);
545     return std::error_code();
546   }
547
548   /// Create an RCIndirectStubsManager that will allocate stubs on the remote
549   /// target.
550   std::error_code
551   createIndirectStubsManager(std::unique_ptr<RCIndirectStubsManager> &I) {
552     assert(!I && "Indirect stubs manager should be null before creation.");
553     auto Id = IndirectStubOwnerIds.getNext();
554     if (auto EC = call<CreateIndirectStubsOwner>(Channel, Id))
555       return EC;
556     I = llvm::make_unique<RCIndirectStubsManager>(*this, Id);
557     return std::error_code();
558   }
559
560   /// Search for symbols in the remote process. Note: This should be used by
561   /// symbol resolvers *after* they've searched the local symbol table in the
562   /// JIT stack.
563   std::error_code getSymbolAddress(TargetAddress &Addr, StringRef Name) {
564     // Check for an 'out-of-band' error, e.g. from an MM destructor.
565     if (ExistingError)
566       return ExistingError;
567
568     // Request remote symbol address.
569     if (auto EC = call<GetSymbolAddress>(Channel, Name))
570       return EC;
571
572     return expect<GetSymbolAddressResponse>(Channel, [&](TargetAddress &A) {
573       Addr = A;
574       DEBUG(dbgs() << "Remote address lookup " << Name << " = "
575                    << format("0x%016x", Addr) << "\n");
576       return std::error_code();
577     });
578   }
579
580   /// Get the triple for the remote target.
581   const std::string &getTargetTriple() const { return RemoteTargetTriple; }
582
583   std::error_code terminateSession() { return call<TerminateSession>(Channel); }
584
585 private:
586   OrcRemoteTargetClient(ChannelT &Channel, std::error_code &EC)
587       : Channel(Channel), RemotePointerSize(0), RemotePageSize(0),
588         RemoteTrampolineSize(0), RemoteIndirectStubSize(0) {
589     if ((EC = call<GetRemoteInfo>(Channel)))
590       return;
591
592     EC = expect<GetRemoteInfoResponse>(
593         Channel, readArgs(RemoteTargetTriple, RemotePointerSize, RemotePageSize,
594                           RemoteTrampolineSize, RemoteIndirectStubSize));
595   }
596
597   void destroyRemoteAllocator(ResourceIdMgr::ResourceId Id) {
598     if (auto EC = call<DestroyRemoteAllocator>(Channel, Id)) {
599       // FIXME: This will be triggered by a removeModuleSet call: Propagate
600       //        error return up through that.
601       llvm_unreachable("Failed to destroy remote allocator.");
602       AllocatorIds.release(Id);
603     }
604   }
605
606   std::error_code destroyIndirectStubsManager(ResourceIdMgr::ResourceId Id) {
607     IndirectStubOwnerIds.release(Id);
608     return call<DestroyIndirectStubsOwner>(Channel, Id);
609   }
610
611   std::error_code emitIndirectStubs(TargetAddress &StubBase,
612                                     TargetAddress &PtrBase,
613                                     uint32_t &NumStubsEmitted,
614                                     ResourceIdMgr::ResourceId Id,
615                                     uint32_t NumStubsRequired) {
616     if (auto EC = call<EmitIndirectStubs>(Channel, Id, NumStubsRequired))
617       return EC;
618
619     return expect<EmitIndirectStubsResponse>(
620         Channel, readArgs(StubBase, PtrBase, NumStubsEmitted));
621   }
622
623   std::error_code emitResolverBlock() {
624     // Check for an 'out-of-band' error, e.g. from an MM destructor.
625     if (ExistingError)
626       return ExistingError;
627
628     return call<EmitResolverBlock>(Channel);
629   }
630
631   std::error_code emitTrampolineBlock(TargetAddress &BlockAddr,
632                                       uint32_t &NumTrampolines) {
633     // Check for an 'out-of-band' error, e.g. from an MM destructor.
634     if (ExistingError)
635       return ExistingError;
636
637     if (auto EC = call<EmitTrampolineBlock>(Channel))
638       return EC;
639
640     return expect<EmitTrampolineBlockResponse>(
641         Channel, [&](TargetAddress BAddr, uint32_t NTrampolines) {
642           BlockAddr = BAddr;
643           NumTrampolines = NTrampolines;
644           return std::error_code();
645         });
646   }
647
648   uint32_t getIndirectStubSize() const { return RemoteIndirectStubSize; }
649   uint32_t getPageSize() const { return RemotePageSize; }
650   uint32_t getPointerSize() const { return RemotePointerSize; }
651
652   uint32_t getTrampolineSize() const { return RemoteTrampolineSize; }
653
654   std::error_code listenForCompileRequests(uint32_t &NextId) {
655     // Check for an 'out-of-band' error, e.g. from an MM destructor.
656     if (ExistingError)
657       return ExistingError;
658
659     if (auto EC = getNextProcId(Channel, NextId))
660       return EC;
661
662     while (NextId == RequestCompileId) {
663       TargetAddress TrampolineAddr = 0;
664       if (auto EC = handle<RequestCompile>(Channel, readArgs(TrampolineAddr)))
665         return EC;
666
667       TargetAddress ImplAddr = CompileCallback(TrampolineAddr);
668       if (auto EC = call<RequestCompileResponse>(Channel, ImplAddr))
669         return EC;
670
671       if (auto EC = getNextProcId(Channel, NextId))
672         return EC;
673     }
674
675     return std::error_code();
676   }
677
678   std::error_code readMem(char *Dst, TargetAddress Src, uint64_t Size) {
679     // Check for an 'out-of-band' error, e.g. from an MM destructor.
680     if (ExistingError)
681       return ExistingError;
682
683     if (auto EC = call<ReadMem>(Channel, Src, Size))
684       return EC;
685
686     if (auto EC = expect<ReadMemResponse>(
687             Channel, [&]() { return Channel.readBytes(Dst, Size); }))
688       return EC;
689
690     return std::error_code();
691   }
692
693   std::error_code reserveMem(TargetAddress &RemoteAddr,
694                              ResourceIdMgr::ResourceId Id, uint64_t Size,
695                              uint32_t Align) {
696
697     // Check for an 'out-of-band' error, e.g. from an MM destructor.
698     if (ExistingError)
699       return ExistingError;
700
701     if (auto EC = call<ReserveMem>(Channel, Id, Size, Align))
702       return EC;
703
704     if (auto EC = expect<ReserveMemResponse>(Channel, [&](TargetAddress Addr) {
705           RemoteAddr = Addr;
706           return std::error_code();
707         }))
708       return EC;
709
710     return std::error_code();
711   }
712
713   std::error_code setProtections(ResourceIdMgr::ResourceId Id,
714                                  TargetAddress RemoteSegAddr,
715                                  unsigned ProtFlags) {
716     return call<SetProtections>(Channel, Id, RemoteSegAddr, ProtFlags);
717   }
718
719   std::error_code writeMem(TargetAddress Addr, const char *Src, uint64_t Size) {
720     // Check for an 'out-of-band' error, e.g. from an MM destructor.
721     if (ExistingError)
722       return ExistingError;
723
724     // Make the send call.
725     if (auto EC = call<WriteMem>(Channel, Addr, Size))
726       return EC;
727
728     // Follow this up with the section contents.
729     if (auto EC = Channel.appendBytes(Src, Size))
730       return EC;
731
732     return Channel.send();
733   }
734
735   std::error_code writePointer(TargetAddress Addr, TargetAddress PtrVal) {
736     // Check for an 'out-of-band' error, e.g. from an MM destructor.
737     if (ExistingError)
738       return ExistingError;
739
740     return call<WritePtr>(Channel, Addr, PtrVal);
741   }
742
743   static std::error_code doNothing() { return std::error_code(); }
744
745   ChannelT &Channel;
746   std::error_code ExistingError;
747   std::string RemoteTargetTriple;
748   uint32_t RemotePointerSize;
749   uint32_t RemotePageSize;
750   uint32_t RemoteTrampolineSize;
751   uint32_t RemoteIndirectStubSize;
752   ResourceIdMgr AllocatorIds, IndirectStubOwnerIds;
753   std::function<TargetAddress(TargetAddress)> CompileCallback;
754 };
755
756 } // end namespace remote
757 } // end namespace orc
758 } // end namespace llvm
759
760 #undef DEBUG_TYPE
761
762 #endif