3f1f74d65faecd2e32447090d40cb43e0578e2ef
[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(Alloc &&Other)
257           : Size(std::move(Other.Size)), Align(std::move(Other.Align)),
258             Contents(std::move(Other.Contents)),
259             RemoteAddr(std::move(Other.RemoteAddr)) {}
260
261       Alloc &operator=(Alloc &&Other) {
262         Size = std::move(Other.Size);
263         Align = std::move(Other.Align);
264         Contents = std::move(Other.Contents);
265         RemoteAddr = std::move(Other.RemoteAddr);
266         return *this;
267       }
268
269       uint64_t getSize() const { return Size; }
270
271       unsigned getAlign() const { return Align; }
272
273       char *getLocalAddress() const {
274         uintptr_t LocalAddr = reinterpret_cast<uintptr_t>(Contents.get());
275         LocalAddr = RoundUpToAlignment(LocalAddr, Align);
276         return reinterpret_cast<char *>(LocalAddr);
277       }
278
279       void setRemoteAddress(TargetAddress RemoteAddr) {
280         this->RemoteAddr = RemoteAddr;
281       }
282
283       TargetAddress getRemoteAddress() const { return RemoteAddr; }
284
285     private:
286       uint64_t Size;
287       unsigned Align;
288       std::unique_ptr<char[]> Contents;
289       TargetAddress RemoteAddr;
290     };
291
292     struct ObjectAllocs {
293       ObjectAllocs()
294           : RemoteCodeAddr(0), RemoteRODataAddr(0), RemoteRWDataAddr(0) {}
295       TargetAddress RemoteCodeAddr;
296       TargetAddress RemoteRODataAddr;
297       TargetAddress RemoteRWDataAddr;
298       std::vector<Alloc> CodeAllocs, RODataAllocs, RWDataAllocs;
299     };
300
301     OrcRemoteTargetClient &Client;
302     ResourceIdMgr::ResourceId Id;
303     std::vector<ObjectAllocs> Unmapped;
304     std::vector<ObjectAllocs> Unfinalized;
305   };
306
307   /// Remote indirect stubs manager.
308   class RCIndirectStubsManager : public IndirectStubsManager {
309   public:
310     RCIndirectStubsManager(OrcRemoteTargetClient &Remote,
311                            ResourceIdMgr::ResourceId Id)
312         : Remote(Remote), Id(Id) {}
313
314     ~RCIndirectStubsManager() { Remote.destroyIndirectStubsManager(Id); }
315
316     std::error_code createStub(StringRef StubName, TargetAddress StubAddr,
317                                JITSymbolFlags StubFlags) override {
318       if (auto EC = reserveStubs(1))
319         return EC;
320
321       return createStubInternal(StubName, StubAddr, StubFlags);
322     }
323
324     std::error_code createStubs(const StubInitsMap &StubInits) override {
325       if (auto EC = reserveStubs(StubInits.size()))
326         return EC;
327
328       for (auto &Entry : StubInits)
329         if (auto EC = createStubInternal(Entry.first(), Entry.second.first,
330                                          Entry.second.second))
331           return EC;
332
333       return std::error_code();
334     }
335
336     JITSymbol findStub(StringRef Name, bool ExportedStubsOnly) override {
337       auto I = StubIndexes.find(Name);
338       if (I == StubIndexes.end())
339         return nullptr;
340       auto Key = I->second.first;
341       auto Flags = I->second.second;
342       auto StubSymbol = JITSymbol(getStubAddr(Key), Flags);
343       if (ExportedStubsOnly && !StubSymbol.isExported())
344         return nullptr;
345       return StubSymbol;
346     }
347
348     JITSymbol findPointer(StringRef Name) override {
349       auto I = StubIndexes.find(Name);
350       if (I == StubIndexes.end())
351         return nullptr;
352       auto Key = I->second.first;
353       auto Flags = I->second.second;
354       return JITSymbol(getPtrAddr(Key), Flags);
355     }
356
357     std::error_code updatePointer(StringRef Name,
358                                   TargetAddress NewAddr) override {
359       auto I = StubIndexes.find(Name);
360       assert(I != StubIndexes.end() && "No stub pointer for symbol");
361       auto Key = I->second.first;
362       return Remote.writePointer(getPtrAddr(Key), NewAddr);
363     }
364
365   private:
366     struct RemoteIndirectStubsInfo {
367       RemoteIndirectStubsInfo(TargetAddress StubBase, TargetAddress PtrBase,
368                               unsigned NumStubs)
369           : StubBase(StubBase), PtrBase(PtrBase), NumStubs(NumStubs) {}
370       TargetAddress StubBase;
371       TargetAddress PtrBase;
372       unsigned NumStubs;
373     };
374
375     OrcRemoteTargetClient &Remote;
376     ResourceIdMgr::ResourceId Id;
377     std::vector<RemoteIndirectStubsInfo> RemoteIndirectStubsInfos;
378     typedef std::pair<uint16_t, uint16_t> StubKey;
379     std::vector<StubKey> FreeStubs;
380     StringMap<std::pair<StubKey, JITSymbolFlags>> StubIndexes;
381
382     std::error_code reserveStubs(unsigned NumStubs) {
383       if (NumStubs <= FreeStubs.size())
384         return std::error_code();
385
386       unsigned NewStubsRequired = NumStubs - FreeStubs.size();
387       TargetAddress StubBase;
388       TargetAddress PtrBase;
389       unsigned NumStubsEmitted;
390
391       Remote.emitIndirectStubs(StubBase, PtrBase, NumStubsEmitted, Id,
392                                NewStubsRequired);
393
394       unsigned NewBlockId = RemoteIndirectStubsInfos.size();
395       RemoteIndirectStubsInfos.push_back(
396           RemoteIndirectStubsInfo(StubBase, PtrBase, NumStubsEmitted));
397
398       for (unsigned I = 0; I < NumStubsEmitted; ++I)
399         FreeStubs.push_back(std::make_pair(NewBlockId, I));
400
401       return std::error_code();
402     }
403
404     std::error_code createStubInternal(StringRef StubName,
405                                        TargetAddress InitAddr,
406                                        JITSymbolFlags StubFlags) {
407       auto Key = FreeStubs.back();
408       FreeStubs.pop_back();
409       StubIndexes[StubName] = std::make_pair(Key, StubFlags);
410       return Remote.writePointer(getPtrAddr(Key), InitAddr);
411     }
412
413     TargetAddress getStubAddr(StubKey K) {
414       assert(RemoteIndirectStubsInfos[K.first].StubBase != 0 &&
415              "Missing stub address");
416       return RemoteIndirectStubsInfos[K.first].StubBase +
417              K.second * Remote.getIndirectStubSize();
418     }
419
420     TargetAddress getPtrAddr(StubKey K) {
421       assert(RemoteIndirectStubsInfos[K.first].PtrBase != 0 &&
422              "Missing pointer address");
423       return RemoteIndirectStubsInfos[K.first].PtrBase +
424              K.second * Remote.getPointerSize();
425     }
426   };
427
428   /// Remote compile callback manager.
429   class RCCompileCallbackManager : public JITCompileCallbackManager {
430   public:
431     RCCompileCallbackManager(TargetAddress ErrorHandlerAddress,
432                              OrcRemoteTargetClient &Remote)
433         : JITCompileCallbackManager(ErrorHandlerAddress), Remote(Remote) {
434       assert(!Remote.CompileCallback && "Compile callback already set");
435       Remote.CompileCallback = [this](TargetAddress TrampolineAddr) {
436         return executeCompileCallback(TrampolineAddr);
437       };
438       Remote.emitResolverBlock();
439     }
440
441   private:
442     void grow() {
443       TargetAddress BlockAddr = 0;
444       uint32_t NumTrampolines = 0;
445       auto EC = Remote.emitTrampolineBlock(BlockAddr, NumTrampolines);
446       assert(!EC && "Failed to create trampolines");
447
448       uint32_t TrampolineSize = Remote.getTrampolineSize();
449       for (unsigned I = 0; I < NumTrampolines; ++I)
450         this->AvailableTrampolines.push_back(BlockAddr + (I * TrampolineSize));
451     }
452
453     OrcRemoteTargetClient &Remote;
454   };
455
456   /// Create an OrcRemoteTargetClient.
457   /// Channel is the ChannelT instance to communicate on. It is assumed that
458   /// the channel is ready to be read from and written to.
459   static ErrorOr<OrcRemoteTargetClient> Create(ChannelT &Channel) {
460     std::error_code EC;
461     OrcRemoteTargetClient H(Channel, EC);
462     if (EC)
463       return EC;
464     return H;
465   }
466
467   /// Call the int(void) function at the given address in the target and return
468   /// its result.
469   std::error_code callIntVoid(int &Result, TargetAddress Addr) {
470     DEBUG(dbgs() << "Calling int(*)(void) " << format("0x%016x", Addr) << "\n");
471
472     if (auto EC = call<CallIntVoid>(Channel, Addr))
473       return EC;
474
475     unsigned NextProcId;
476     if (auto EC = listenForCompileRequests(NextProcId))
477       return EC;
478
479     if (NextProcId != CallIntVoidResponseId)
480       return orcError(OrcErrorCode::UnexpectedRPCCall);
481
482     return handle<CallIntVoidResponse>(Channel, [&](int R) {
483       Result = R;
484       DEBUG(dbgs() << "Result: " << R << "\n");
485       return std::error_code();
486     });
487   }
488
489   /// Call the int(int, char*[]) function at the given address in the target and
490   /// return its result.
491   std::error_code callMain(int &Result, TargetAddress Addr,
492                            const std::vector<std::string> &Args) {
493     DEBUG(dbgs() << "Calling int(*)(int, char*[]) " << format("0x%016x", Addr)
494                  << "\n");
495
496     if (auto EC = call<CallMain>(Channel, Addr, Args))
497       return EC;
498
499     unsigned NextProcId;
500     if (auto EC = listenForCompileRequests(NextProcId))
501       return EC;
502
503     if (NextProcId != CallMainResponseId)
504       return orcError(OrcErrorCode::UnexpectedRPCCall);
505
506     return handle<CallMainResponse>(Channel, [&](int R) {
507       Result = R;
508       DEBUG(dbgs() << "Result: " << R << "\n");
509       return std::error_code();
510     });
511   }
512
513   /// Call the void() function at the given address in the target and wait for
514   /// it to finish.
515   std::error_code callVoidVoid(TargetAddress Addr) {
516     DEBUG(dbgs() << "Calling void(*)(void) " << format("0x%016x", Addr)
517                  << "\n");
518
519     if (auto EC = call<CallVoidVoid>(Channel, Addr))
520       return EC;
521
522     unsigned NextProcId;
523     if (auto EC = listenForCompileRequests(NextProcId))
524       return EC;
525
526     if (NextProcId != CallVoidVoidResponseId)
527       return orcError(OrcErrorCode::UnexpectedRPCCall);
528
529     return handle<CallVoidVoidResponse>(Channel, doNothing);
530   }
531
532   /// Create an RCMemoryManager which will allocate its memory on the remote
533   /// target.
534   std::error_code
535   createRemoteMemoryManager(std::unique_ptr<RCMemoryManager> &MM) {
536     assert(!MM && "MemoryManager should be null before creation.");
537
538     auto Id = AllocatorIds.getNext();
539     if (auto EC = call<CreateRemoteAllocator>(Channel, Id))
540       return EC;
541     MM = llvm::make_unique<RCMemoryManager>(*this, Id);
542     return std::error_code();
543   }
544
545   /// Create an RCIndirectStubsManager that will allocate stubs on the remote
546   /// target.
547   std::error_code
548   createIndirectStubsManager(std::unique_ptr<RCIndirectStubsManager> &I) {
549     assert(!I && "Indirect stubs manager should be null before creation.");
550     auto Id = IndirectStubOwnerIds.getNext();
551     if (auto EC = call<CreateIndirectStubsOwner>(Channel, Id))
552       return EC;
553     I = llvm::make_unique<RCIndirectStubsManager>(*this, Id);
554     return std::error_code();
555   }
556
557   /// Search for symbols in the remote process. Note: This should be used by
558   /// symbol resolvers *after* they've searched the local symbol table in the
559   /// JIT stack.
560   std::error_code getSymbolAddress(TargetAddress &Addr, StringRef Name) {
561     // Check for an 'out-of-band' error, e.g. from an MM destructor.
562     if (ExistingError)
563       return ExistingError;
564
565     // Request remote symbol address.
566     if (auto EC = call<GetSymbolAddress>(Channel, Name))
567       return EC;
568
569     return expect<GetSymbolAddressResponse>(Channel, [&](TargetAddress &A) {
570       Addr = A;
571       DEBUG(dbgs() << "Remote address lookup " << Name << " = "
572                    << format("0x%016x", Addr) << "\n");
573       return std::error_code();
574     });
575   }
576
577   /// Get the triple for the remote target.
578   const std::string &getTargetTriple() const { return RemoteTargetTriple; }
579
580   std::error_code terminateSession() { return call<TerminateSession>(Channel); }
581
582 private:
583   OrcRemoteTargetClient(ChannelT &Channel, std::error_code &EC)
584       : Channel(Channel), RemotePointerSize(0), RemotePageSize(0),
585         RemoteTrampolineSize(0), RemoteIndirectStubSize(0) {
586     if ((EC = call<GetRemoteInfo>(Channel)))
587       return;
588
589     EC = expect<GetRemoteInfoResponse>(
590         Channel, readArgs(RemoteTargetTriple, RemotePointerSize, RemotePageSize,
591                           RemoteTrampolineSize, RemoteIndirectStubSize));
592   }
593
594   void destroyRemoteAllocator(ResourceIdMgr::ResourceId Id) {
595     if (auto EC = call<DestroyRemoteAllocator>(Channel, Id)) {
596       // FIXME: This will be triggered by a removeModuleSet call: Propagate
597       //        error return up through that.
598       llvm_unreachable("Failed to destroy remote allocator.");
599       AllocatorIds.release(Id);
600     }
601   }
602
603   std::error_code destroyIndirectStubsManager(ResourceIdMgr::ResourceId Id) {
604     IndirectStubOwnerIds.release(Id);
605     return call<DestroyIndirectStubsOwner>(Channel, Id);
606   }
607
608   std::error_code emitIndirectStubs(TargetAddress &StubBase,
609                                     TargetAddress &PtrBase,
610                                     uint32_t &NumStubsEmitted,
611                                     ResourceIdMgr::ResourceId Id,
612                                     uint32_t NumStubsRequired) {
613     if (auto EC = call<EmitIndirectStubs>(Channel, Id, NumStubsRequired))
614       return EC;
615
616     return expect<EmitIndirectStubsResponse>(
617         Channel, readArgs(StubBase, PtrBase, NumStubsEmitted));
618   }
619
620   std::error_code emitResolverBlock() {
621     // Check for an 'out-of-band' error, e.g. from an MM destructor.
622     if (ExistingError)
623       return ExistingError;
624
625     return call<EmitResolverBlock>(Channel);
626   }
627
628   std::error_code emitTrampolineBlock(TargetAddress &BlockAddr,
629                                       uint32_t &NumTrampolines) {
630     // Check for an 'out-of-band' error, e.g. from an MM destructor.
631     if (ExistingError)
632       return ExistingError;
633
634     if (auto EC = call<EmitTrampolineBlock>(Channel))
635       return EC;
636
637     return expect<EmitTrampolineBlockResponse>(
638         Channel, [&](TargetAddress BAddr, uint32_t NTrampolines) {
639           BlockAddr = BAddr;
640           NumTrampolines = NTrampolines;
641           return std::error_code();
642         });
643   }
644
645   uint32_t getIndirectStubSize() const { return RemoteIndirectStubSize; }
646   uint32_t getPageSize() const { return RemotePageSize; }
647   uint32_t getPointerSize() const { return RemotePointerSize; }
648
649   uint32_t getTrampolineSize() const { return RemoteTrampolineSize; }
650
651   std::error_code listenForCompileRequests(uint32_t &NextId) {
652     // Check for an 'out-of-band' error, e.g. from an MM destructor.
653     if (ExistingError)
654       return ExistingError;
655
656     if (auto EC = getNextProcId(Channel, NextId))
657       return EC;
658
659     while (NextId == RequestCompileId) {
660       TargetAddress TrampolineAddr = 0;
661       if (auto EC = handle<RequestCompile>(Channel, readArgs(TrampolineAddr)))
662         return EC;
663
664       TargetAddress ImplAddr = CompileCallback(TrampolineAddr);
665       if (auto EC = call<RequestCompileResponse>(Channel, ImplAddr))
666         return EC;
667
668       if (auto EC = getNextProcId(Channel, NextId))
669         return EC;
670     }
671
672     return std::error_code();
673   }
674
675   std::error_code readMem(char *Dst, TargetAddress Src, uint64_t Size) {
676     // Check for an 'out-of-band' error, e.g. from an MM destructor.
677     if (ExistingError)
678       return ExistingError;
679
680     if (auto EC = call<ReadMem>(Channel, Src, Size))
681       return EC;
682
683     if (auto EC = expect<ReadMemResponse>(
684             Channel, [&]() { return Channel.readBytes(Dst, Size); }))
685       return EC;
686
687     return std::error_code();
688   }
689
690   std::error_code reserveMem(TargetAddress &RemoteAddr,
691                              ResourceIdMgr::ResourceId Id, uint64_t Size,
692                              uint32_t Align) {
693
694     // Check for an 'out-of-band' error, e.g. from an MM destructor.
695     if (ExistingError)
696       return ExistingError;
697
698     if (auto EC = call<ReserveMem>(Channel, Id, Size, Align))
699       return EC;
700
701     if (auto EC = expect<ReserveMemResponse>(Channel, [&](TargetAddress Addr) {
702           RemoteAddr = Addr;
703           return std::error_code();
704         }))
705       return EC;
706
707     return std::error_code();
708   }
709
710   std::error_code setProtections(ResourceIdMgr::ResourceId Id,
711                                  TargetAddress RemoteSegAddr,
712                                  unsigned ProtFlags) {
713     return call<SetProtections>(Channel, Id, RemoteSegAddr, ProtFlags);
714   }
715
716   std::error_code writeMem(TargetAddress Addr, const char *Src, uint64_t Size) {
717     // Check for an 'out-of-band' error, e.g. from an MM destructor.
718     if (ExistingError)
719       return ExistingError;
720
721     // Make the send call.
722     if (auto EC = call<WriteMem>(Channel, Addr, Size))
723       return EC;
724
725     // Follow this up with the section contents.
726     if (auto EC = Channel.appendBytes(Src, Size))
727       return EC;
728
729     return Channel.send();
730   }
731
732   std::error_code writePointer(TargetAddress Addr, TargetAddress PtrVal) {
733     // Check for an 'out-of-band' error, e.g. from an MM destructor.
734     if (ExistingError)
735       return ExistingError;
736
737     return call<WritePtr>(Channel, Addr, PtrVal);
738   }
739
740   static std::error_code doNothing() { return std::error_code(); }
741
742   ChannelT &Channel;
743   std::error_code ExistingError;
744   std::string RemoteTargetTriple;
745   uint32_t RemotePointerSize;
746   uint32_t RemotePageSize;
747   uint32_t RemoteTrampolineSize;
748   uint32_t RemoteIndirectStubSize;
749   ResourceIdMgr AllocatorIds, IndirectStubOwnerIds;
750   std::function<TargetAddress(TargetAddress)> CompileCallback;
751 };
752
753 } // end namespace remote
754 } // end namespace orc
755 } // end namespace llvm
756
757 #undef DEBUG_TYPE
758
759 #endif