a69510d3ff1c596638ba04c44e6f553a85382ee9
[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(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)) {}
51
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);
57       return *this;
58     }
59
60     ~RCMemoryManager() {
61       Client.destroyRemoteAllocator(Id);
62       DEBUG(dbgs() << "Destroyed remote allocator " << Id << "\n");
63     }
64
65     uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
66                                  unsigned SectionID,
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");
74       return Alloc;
75     }
76
77     uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
78                                  unsigned SectionID, StringRef SectionName,
79                                  bool IsReadOnly) override {
80       if (IsReadOnly) {
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");
87         return Alloc;
88       } // else...
89
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");
96       return Alloc;
97     }
98
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());
104
105       DEBUG(dbgs() << "Allocator " << Id << " reserved:\n");
106
107       if (CodeSize != 0) {
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.");
112         (void)EC;
113         DEBUG(dbgs() << "  code: "
114                      << format("0x%016x", Unmapped.back().RemoteCodeAddr)
115                      << " (" << CodeSize << " bytes, alignment " << CodeAlign
116                      << ")\n");
117       }
118
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.");
124         (void)EC;
125         DEBUG(dbgs() << "  ro-data: "
126                      << format("0x%016x", Unmapped.back().RemoteRODataAddr)
127                      << " (" << RODataSize << " bytes, alignment "
128                      << RODataAlign << ")\n");
129       }
130
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.");
136         (void)EC;
137         DEBUG(dbgs() << "  rw-data: "
138                      << format("0x%016x", Unmapped.back().RemoteRWDataAddr)
139                      << " (" << RWDataSize << " bytes, alignment "
140                      << RWDataAlign << ")\n");
141       }
142     }
143
144     bool needsToReserveAllocationSpace() override { return true; }
145
146     void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
147                           size_t Size) override {}
148
149     void deregisterEHFrames(uint8_t *addr, uint64_t LoadAddr,
150                             size_t Size) override {}
151
152     void notifyObjectLoaded(RuntimeDyld &Dyld,
153                             const object::ObjectFile &Obj) override {
154       DEBUG(dbgs() << "Allocator " << Id << " applied mappings:\n");
155       for (auto &ObjAllocs : Unmapped) {
156         {
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();
166           }
167         }
168         {
169           TargetAddress NextRODataAddr = ObjAllocs.RemoteRODataAddr;
170           for (auto &Alloc : ObjAllocs.RODataAllocs) {
171             NextRODataAddr =
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)
177                          << "\n");
178             Alloc.setRemoteAddress(NextRODataAddr);
179             NextRODataAddr += Alloc.getSize();
180           }
181         }
182         {
183           TargetAddress NextRWDataAddr = ObjAllocs.RemoteRWDataAddr;
184           for (auto &Alloc : ObjAllocs.RWDataAllocs) {
185             NextRWDataAddr =
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)
191                          << "\n");
192             Alloc.setRemoteAddress(NextRWDataAddr);
193             NextRWDataAddr += Alloc.getSize();
194           }
195         }
196         Unfinalized.push_back(std::move(ObjAllocs));
197       }
198       Unmapped.clear();
199     }
200
201     bool finalizeMemory(std::string *ErrMsg = nullptr) override {
202       DEBUG(dbgs() << "Allocator " << Id << " finalizing:\n");
203
204       for (auto &ObjAllocs : Unfinalized) {
205
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(),
212                           Alloc.getSize());
213         }
214
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);
220         }
221
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(),
228                           Alloc.getSize());
229         }
230
231         if (ObjAllocs.RemoteRODataAddr) {
232           DEBUG(dbgs() << "  setting R-- permissions on ro-data block: "
233                        << format("0x%016x", ObjAllocs.RemoteRODataAddr)
234                        << "\n");
235           Client.setProtections(Id, ObjAllocs.RemoteRODataAddr,
236                                 sys::Memory::MF_READ);
237         }
238
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(),
245                           Alloc.getSize());
246         }
247
248         if (ObjAllocs.RemoteRWDataAddr) {
249           DEBUG(dbgs() << "  setting RW- permissions on rw-data block: "
250                        << format("0x%016x", ObjAllocs.RemoteRWDataAddr)
251                        << "\n");
252           Client.setProtections(Id, ObjAllocs.RemoteRWDataAddr,
253                                 sys::Memory::MF_READ | sys::Memory::MF_WRITE);
254         }
255       }
256       Unfinalized.clear();
257
258       return false;
259     }
260
261   private:
262     class Alloc {
263     public:
264       Alloc(uint64_t Size, unsigned Align)
265           : Size(Size), Align(Align), Contents(new char[Size + Align - 1]),
266             RemoteAddr(0) {}
267
268       Alloc(Alloc &&Other)
269           : Size(std::move(Other.Size)), Align(std::move(Other.Align)),
270             Contents(std::move(Other.Contents)),
271             RemoteAddr(std::move(Other.RemoteAddr)) {}
272
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);
278         return *this;
279       }
280
281       uint64_t getSize() const { return Size; }
282
283       unsigned getAlign() const { return Align; }
284
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);
289       }
290
291       void setRemoteAddress(TargetAddress RemoteAddr) {
292         this->RemoteAddr = RemoteAddr;
293       }
294
295       TargetAddress getRemoteAddress() const { return RemoteAddr; }
296
297     private:
298       uint64_t Size;
299       unsigned Align;
300       std::unique_ptr<char[]> Contents;
301       TargetAddress RemoteAddr;
302     };
303
304     struct ObjectAllocs {
305       ObjectAllocs()
306           : RemoteCodeAddr(0), RemoteRODataAddr(0), RemoteRWDataAddr(0) {}
307
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)) {}
315
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);
323         return *this;
324       }
325
326       TargetAddress RemoteCodeAddr;
327       TargetAddress RemoteRODataAddr;
328       TargetAddress RemoteRWDataAddr;
329       std::vector<Alloc> CodeAllocs, RODataAllocs, RWDataAllocs;
330     };
331
332     OrcRemoteTargetClient &Client;
333     ResourceIdMgr::ResourceId Id;
334     std::vector<ObjectAllocs> Unmapped;
335     std::vector<ObjectAllocs> Unfinalized;
336   };
337
338   /// Remote indirect stubs manager.
339   class RCIndirectStubsManager : public IndirectStubsManager {
340   public:
341     RCIndirectStubsManager(OrcRemoteTargetClient &Remote,
342                            ResourceIdMgr::ResourceId Id)
343         : Remote(Remote), Id(Id) {}
344
345     ~RCIndirectStubsManager() { Remote.destroyIndirectStubsManager(Id); }
346
347     std::error_code createStub(StringRef StubName, TargetAddress StubAddr,
348                                JITSymbolFlags StubFlags) override {
349       if (auto EC = reserveStubs(1))
350         return EC;
351
352       return createStubInternal(StubName, StubAddr, StubFlags);
353     }
354
355     std::error_code createStubs(const StubInitsMap &StubInits) override {
356       if (auto EC = reserveStubs(StubInits.size()))
357         return EC;
358
359       for (auto &Entry : StubInits)
360         if (auto EC = createStubInternal(Entry.first(), Entry.second.first,
361                                          Entry.second.second))
362           return EC;
363
364       return std::error_code();
365     }
366
367     JITSymbol findStub(StringRef Name, bool ExportedStubsOnly) override {
368       auto I = StubIndexes.find(Name);
369       if (I == StubIndexes.end())
370         return nullptr;
371       auto Key = I->second.first;
372       auto Flags = I->second.second;
373       auto StubSymbol = JITSymbol(getStubAddr(Key), Flags);
374       if (ExportedStubsOnly && !StubSymbol.isExported())
375         return nullptr;
376       return StubSymbol;
377     }
378
379     JITSymbol findPointer(StringRef Name) override {
380       auto I = StubIndexes.find(Name);
381       if (I == StubIndexes.end())
382         return nullptr;
383       auto Key = I->second.first;
384       auto Flags = I->second.second;
385       return JITSymbol(getPtrAddr(Key), Flags);
386     }
387
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);
394     }
395
396   private:
397     struct RemoteIndirectStubsInfo {
398       RemoteIndirectStubsInfo(TargetAddress StubBase, TargetAddress PtrBase,
399                               unsigned NumStubs)
400           : StubBase(StubBase), PtrBase(PtrBase), NumStubs(NumStubs) {}
401       TargetAddress StubBase;
402       TargetAddress PtrBase;
403       unsigned NumStubs;
404     };
405
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;
412
413     std::error_code reserveStubs(unsigned NumStubs) {
414       if (NumStubs <= FreeStubs.size())
415         return std::error_code();
416
417       unsigned NewStubsRequired = NumStubs - FreeStubs.size();
418       TargetAddress StubBase;
419       TargetAddress PtrBase;
420       unsigned NumStubsEmitted;
421
422       Remote.emitIndirectStubs(StubBase, PtrBase, NumStubsEmitted, Id,
423                                NewStubsRequired);
424
425       unsigned NewBlockId = RemoteIndirectStubsInfos.size();
426       RemoteIndirectStubsInfos.push_back(
427           RemoteIndirectStubsInfo(StubBase, PtrBase, NumStubsEmitted));
428
429       for (unsigned I = 0; I < NumStubsEmitted; ++I)
430         FreeStubs.push_back(std::make_pair(NewBlockId, I));
431
432       return std::error_code();
433     }
434
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);
442     }
443
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();
449     }
450
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();
456     }
457   };
458
459   /// Remote compile callback manager.
460   class RCCompileCallbackManager : public JITCompileCallbackManager {
461   public:
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);
468       };
469       Remote.emitResolverBlock();
470     }
471
472   private:
473     void grow() {
474       TargetAddress BlockAddr = 0;
475       uint32_t NumTrampolines = 0;
476       auto EC = Remote.emitTrampolineBlock(BlockAddr, NumTrampolines);
477       assert(!EC && "Failed to create trampolines");
478
479       uint32_t TrampolineSize = Remote.getTrampolineSize();
480       for (unsigned I = 0; I < NumTrampolines; ++I)
481         this->AvailableTrampolines.push_back(BlockAddr + (I * TrampolineSize));
482     }
483
484     OrcRemoteTargetClient &Remote;
485   };
486
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) {
491     std::error_code EC;
492     OrcRemoteTargetClient H(Channel, EC);
493     if (EC)
494       return EC;
495     return H;
496   }
497
498   /// Call the int(void) function at the given address in the target and return
499   /// its result.
500   std::error_code callIntVoid(int &Result, TargetAddress Addr) {
501     DEBUG(dbgs() << "Calling int(*)(void) " << format("0x%016x", Addr) << "\n");
502
503     if (auto EC = call<CallIntVoid>(Channel, Addr))
504       return EC;
505
506     unsigned NextProcId;
507     if (auto EC = listenForCompileRequests(NextProcId))
508       return EC;
509
510     if (NextProcId != CallIntVoidResponseId)
511       return orcError(OrcErrorCode::UnexpectedRPCCall);
512
513     return handle<CallIntVoidResponse>(Channel, [&](int R) {
514       Result = R;
515       DEBUG(dbgs() << "Result: " << R << "\n");
516       return std::error_code();
517     });
518   }
519
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)
525                  << "\n");
526
527     if (auto EC = call<CallMain>(Channel, Addr, Args))
528       return EC;
529
530     unsigned NextProcId;
531     if (auto EC = listenForCompileRequests(NextProcId))
532       return EC;
533
534     if (NextProcId != CallMainResponseId)
535       return orcError(OrcErrorCode::UnexpectedRPCCall);
536
537     return handle<CallMainResponse>(Channel, [&](int R) {
538       Result = R;
539       DEBUG(dbgs() << "Result: " << R << "\n");
540       return std::error_code();
541     });
542   }
543
544   /// Call the void() function at the given address in the target and wait for
545   /// it to finish.
546   std::error_code callVoidVoid(TargetAddress Addr) {
547     DEBUG(dbgs() << "Calling void(*)(void) " << format("0x%016x", Addr)
548                  << "\n");
549
550     if (auto EC = call<CallVoidVoid>(Channel, Addr))
551       return EC;
552
553     unsigned NextProcId;
554     if (auto EC = listenForCompileRequests(NextProcId))
555       return EC;
556
557     if (NextProcId != CallVoidVoidResponseId)
558       return orcError(OrcErrorCode::UnexpectedRPCCall);
559
560     return handle<CallVoidVoidResponse>(Channel, doNothing);
561   }
562
563   /// Create an RCMemoryManager which will allocate its memory on the remote
564   /// target.
565   std::error_code
566   createRemoteMemoryManager(std::unique_ptr<RCMemoryManager> &MM) {
567     assert(!MM && "MemoryManager should be null before creation.");
568
569     auto Id = AllocatorIds.getNext();
570     if (auto EC = call<CreateRemoteAllocator>(Channel, Id))
571       return EC;
572     MM = llvm::make_unique<RCMemoryManager>(*this, Id);
573     return std::error_code();
574   }
575
576   /// Create an RCIndirectStubsManager that will allocate stubs on the remote
577   /// target.
578   std::error_code
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))
583       return EC;
584     I = llvm::make_unique<RCIndirectStubsManager>(*this, Id);
585     return std::error_code();
586   }
587
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
590   /// JIT stack.
591   std::error_code getSymbolAddress(TargetAddress &Addr, StringRef Name) {
592     // Check for an 'out-of-band' error, e.g. from an MM destructor.
593     if (ExistingError)
594       return ExistingError;
595
596     // Request remote symbol address.
597     if (auto EC = call<GetSymbolAddress>(Channel, Name))
598       return EC;
599
600     return expect<GetSymbolAddressResponse>(Channel, [&](TargetAddress &A) {
601       Addr = A;
602       DEBUG(dbgs() << "Remote address lookup " << Name << " = "
603                    << format("0x%016x", Addr) << "\n");
604       return std::error_code();
605     });
606   }
607
608   /// Get the triple for the remote target.
609   const std::string &getTargetTriple() const { return RemoteTargetTriple; }
610
611   std::error_code terminateSession() { return call<TerminateSession>(Channel); }
612
613 private:
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)))
618       return;
619
620     EC = expect<GetRemoteInfoResponse>(
621         Channel, readArgs(RemoteTargetTriple, RemotePointerSize, RemotePageSize,
622                           RemoteTrampolineSize, RemoteIndirectStubSize));
623   }
624
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);
631     }
632   }
633
634   std::error_code destroyIndirectStubsManager(ResourceIdMgr::ResourceId Id) {
635     IndirectStubOwnerIds.release(Id);
636     return call<DestroyIndirectStubsOwner>(Channel, Id);
637   }
638
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))
645       return EC;
646
647     return expect<EmitIndirectStubsResponse>(
648         Channel, readArgs(StubBase, PtrBase, NumStubsEmitted));
649   }
650
651   std::error_code emitResolverBlock() {
652     // Check for an 'out-of-band' error, e.g. from an MM destructor.
653     if (ExistingError)
654       return ExistingError;
655
656     return call<EmitResolverBlock>(Channel);
657   }
658
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.
662     if (ExistingError)
663       return ExistingError;
664
665     if (auto EC = call<EmitTrampolineBlock>(Channel))
666       return EC;
667
668     return expect<EmitTrampolineBlockResponse>(
669         Channel, [&](TargetAddress BAddr, uint32_t NTrampolines) {
670           BlockAddr = BAddr;
671           NumTrampolines = NTrampolines;
672           return std::error_code();
673         });
674   }
675
676   uint32_t getIndirectStubSize() const { return RemoteIndirectStubSize; }
677   uint32_t getPageSize() const { return RemotePageSize; }
678   uint32_t getPointerSize() const { return RemotePointerSize; }
679
680   uint32_t getTrampolineSize() const { return RemoteTrampolineSize; }
681
682   std::error_code listenForCompileRequests(uint32_t &NextId) {
683     // Check for an 'out-of-band' error, e.g. from an MM destructor.
684     if (ExistingError)
685       return ExistingError;
686
687     if (auto EC = getNextProcId(Channel, NextId))
688       return EC;
689
690     while (NextId == RequestCompileId) {
691       TargetAddress TrampolineAddr = 0;
692       if (auto EC = handle<RequestCompile>(Channel, readArgs(TrampolineAddr)))
693         return EC;
694
695       TargetAddress ImplAddr = CompileCallback(TrampolineAddr);
696       if (auto EC = call<RequestCompileResponse>(Channel, ImplAddr))
697         return EC;
698
699       if (auto EC = getNextProcId(Channel, NextId))
700         return EC;
701     }
702
703     return std::error_code();
704   }
705
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.
708     if (ExistingError)
709       return ExistingError;
710
711     if (auto EC = call<ReadMem>(Channel, Src, Size))
712       return EC;
713
714     if (auto EC = expect<ReadMemResponse>(
715             Channel, [&]() { return Channel.readBytes(Dst, Size); }))
716       return EC;
717
718     return std::error_code();
719   }
720
721   std::error_code reserveMem(TargetAddress &RemoteAddr,
722                              ResourceIdMgr::ResourceId Id, uint64_t Size,
723                              uint32_t Align) {
724
725     // Check for an 'out-of-band' error, e.g. from an MM destructor.
726     if (ExistingError)
727       return ExistingError;
728
729     if (auto EC = call<ReserveMem>(Channel, Id, Size, Align))
730       return EC;
731
732     if (std::error_code EC =
733             expect<ReserveMemResponse>(Channel, [&](TargetAddress Addr) {
734               RemoteAddr = Addr;
735               return std::error_code();
736             }))
737       return EC;
738
739     return std::error_code();
740   }
741
742   std::error_code setProtections(ResourceIdMgr::ResourceId Id,
743                                  TargetAddress RemoteSegAddr,
744                                  unsigned ProtFlags) {
745     return call<SetProtections>(Channel, Id, RemoteSegAddr, ProtFlags);
746   }
747
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.
750     if (ExistingError)
751       return ExistingError;
752
753     // Make the send call.
754     if (auto EC = call<WriteMem>(Channel, Addr, Size))
755       return EC;
756
757     // Follow this up with the section contents.
758     if (auto EC = Channel.appendBytes(Src, Size))
759       return EC;
760
761     return Channel.send();
762   }
763
764   std::error_code writePointer(TargetAddress Addr, TargetAddress PtrVal) {
765     // Check for an 'out-of-band' error, e.g. from an MM destructor.
766     if (ExistingError)
767       return ExistingError;
768
769     return call<WritePtr>(Channel, Addr, PtrVal);
770   }
771
772   static std::error_code doNothing() { return std::error_code(); }
773
774   ChannelT &Channel;
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;
783 };
784
785 } // end namespace remote
786 } // end namespace orc
787 } // end namespace llvm
788
789 #undef DEBUG_TYPE
790
791 #endif