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