LCOV - code coverage report
Current view: top level - include/llvm/ExecutionEngine/Orc - OrcRemoteTargetClient.h (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 101 176 57.4 %
Date: 2018-07-13 00:08:38 Functions: 23 37 62.2 %
Legend: Lines: hit not hit

          Line data    Source code
       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 RawByteChannel with an
      12             : // OrcRemoteTargetServer 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 "llvm/ADT/Optional.h"
      20             : #include "llvm/ADT/STLExtras.h"
      21             : #include "llvm/ADT/StringMap.h"
      22             : #include "llvm/ADT/StringRef.h"
      23             : #include "llvm/ExecutionEngine/JITSymbol.h"
      24             : #include "llvm/ExecutionEngine/Orc/IndirectionUtils.h"
      25             : #include "llvm/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h"
      26             : #include "llvm/ExecutionEngine/RuntimeDyld.h"
      27             : #include "llvm/Support/Debug.h"
      28             : #include "llvm/Support/Error.h"
      29             : #include "llvm/Support/ErrorHandling.h"
      30             : #include "llvm/Support/Format.h"
      31             : #include "llvm/Support/MathExtras.h"
      32             : #include "llvm/Support/Memory.h"
      33             : #include "llvm/Support/raw_ostream.h"
      34             : #include <algorithm>
      35             : #include <cassert>
      36             : #include <cstdint>
      37             : #include <memory>
      38             : #include <string>
      39             : #include <tuple>
      40             : #include <utility>
      41             : #include <vector>
      42             : 
      43             : #define DEBUG_TYPE "orc-remote"
      44             : 
      45             : namespace llvm {
      46             : namespace orc {
      47             : namespace remote {
      48             : 
      49             : /// This class provides utilities (including memory manager, indirect stubs
      50             : /// manager, and compile callback manager types) that support remote JITing
      51             : /// in ORC.
      52             : ///
      53             : /// Each of the utility classes talks to a JIT server (an instance of the
      54             : /// OrcRemoteTargetServer class) via an RPC system (see RPCUtils.h) to carry out
      55             : /// its actions.
      56          48 : class OrcRemoteTargetClient
      57             :     : public rpc::SingleThreadedRPCEndpoint<rpc::RawByteChannel> {
      58             : public:
      59             :   /// Remote-mapped RuntimeDyld-compatible memory manager.
      60             :   class RemoteRTDyldMemoryManager : public RuntimeDyld::MemoryManager {
      61             :     friend class OrcRemoteTargetClient;
      62             : 
      63             :   public:
      64          72 :     ~RemoteRTDyldMemoryManager() {
      65          24 :       Client.destroyRemoteAllocator(Id);
      66             :       LLVM_DEBUG(dbgs() << "Destroyed remote allocator " << Id << "\n");
      67          48 :     }
      68             : 
      69             :     RemoteRTDyldMemoryManager(const RemoteRTDyldMemoryManager &) = delete;
      70             :     RemoteRTDyldMemoryManager &
      71             :     operator=(const RemoteRTDyldMemoryManager &) = delete;
      72             :     RemoteRTDyldMemoryManager(RemoteRTDyldMemoryManager &&) = default;
      73             :     RemoteRTDyldMemoryManager &
      74             :     operator=(RemoteRTDyldMemoryManager &&) = default;
      75             : 
      76          30 :     uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
      77             :                                  unsigned SectionID,
      78             :                                  StringRef SectionName) override {
      79          30 :       Unmapped.back().CodeAllocs.emplace_back(Size, Alignment);
      80             :       uint8_t *Alloc = reinterpret_cast<uint8_t *>(
      81             :           Unmapped.back().CodeAllocs.back().getLocalAddress());
      82             :       LLVM_DEBUG(dbgs() << "Allocator " << Id << " allocated code for "
      83             :                         << SectionName << ": " << Alloc << " (" << Size
      84             :                         << " bytes, alignment " << Alignment << ")\n");
      85          30 :       return Alloc;
      86             :     }
      87             : 
      88          34 :     uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
      89             :                                  unsigned SectionID, StringRef SectionName,
      90             :                                  bool IsReadOnly) override {
      91          34 :       if (IsReadOnly) {
      92          16 :         Unmapped.back().RODataAllocs.emplace_back(Size, Alignment);
      93             :         uint8_t *Alloc = reinterpret_cast<uint8_t *>(
      94             :             Unmapped.back().RODataAllocs.back().getLocalAddress());
      95             :         LLVM_DEBUG(dbgs() << "Allocator " << Id << " allocated ro-data for "
      96             :                           << SectionName << ": " << Alloc << " (" << Size
      97             :                           << " bytes, alignment " << Alignment << ")\n");
      98          16 :         return Alloc;
      99             :       } // else...
     100             : 
     101          18 :       Unmapped.back().RWDataAllocs.emplace_back(Size, Alignment);
     102             :       uint8_t *Alloc = reinterpret_cast<uint8_t *>(
     103             :           Unmapped.back().RWDataAllocs.back().getLocalAddress());
     104             :       LLVM_DEBUG(dbgs() << "Allocator " << Id << " allocated rw-data for "
     105             :                         << SectionName << ": " << Alloc << " (" << Size
     106             :                         << " bytes, alignment " << Alignment << ")\n");
     107          18 :       return Alloc;
     108             :     }
     109             : 
     110          30 :     void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign,
     111             :                                 uintptr_t RODataSize, uint32_t RODataAlign,
     112             :                                 uintptr_t RWDataSize,
     113             :                                 uint32_t RWDataAlign) override {
     114          60 :       Unmapped.push_back(ObjectAllocs());
     115             : 
     116             :       LLVM_DEBUG(dbgs() << "Allocator " << Id << " reserved:\n");
     117             : 
     118          30 :       if (CodeSize != 0) {
     119          30 :         Unmapped.back().RemoteCodeAddr =
     120          60 :             Client.reserveMem(Id, CodeSize, CodeAlign);
     121             : 
     122             :         LLVM_DEBUG(dbgs() << "  code: "
     123             :                           << format("0x%016x", Unmapped.back().RemoteCodeAddr)
     124             :                           << " (" << CodeSize << " bytes, alignment "
     125             :                           << CodeAlign << ")\n");
     126             :       }
     127             : 
     128          30 :       if (RODataSize != 0) {
     129          14 :         Unmapped.back().RemoteRODataAddr =
     130          28 :             Client.reserveMem(Id, RODataSize, RODataAlign);
     131             : 
     132             :         LLVM_DEBUG(dbgs() << "  ro-data: "
     133             :                           << format("0x%016x", Unmapped.back().RemoteRODataAddr)
     134             :                           << " (" << RODataSize << " bytes, alignment "
     135             :                           << RODataAlign << ")\n");
     136             :       }
     137             : 
     138          30 :       if (RWDataSize != 0) {
     139          14 :         Unmapped.back().RemoteRWDataAddr =
     140          28 :             Client.reserveMem(Id, RWDataSize, RWDataAlign);
     141             : 
     142             :         LLVM_DEBUG(dbgs() << "  rw-data: "
     143             :                           << format("0x%016x", Unmapped.back().RemoteRWDataAddr)
     144             :                           << " (" << RWDataSize << " bytes, alignment "
     145             :                           << RWDataAlign << ")\n");
     146             :       }
     147          30 :     }
     148             : 
     149          30 :     bool needsToReserveAllocationSpace() override { return true; }
     150             : 
     151           4 :     void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
     152             :                           size_t Size) override {
     153           8 :       UnfinalizedEHFrames.push_back({LoadAddr, Size});
     154           4 :     }
     155             : 
     156          27 :     void deregisterEHFrames() override {
     157          28 :       for (auto &Frame : RegisteredEHFrames) {
     158             :         // FIXME: Add error poll.
     159           1 :         Client.deregisterEHFrames(Frame.Addr, Frame.Size);
     160             :       }
     161          27 :     }
     162             : 
     163          30 :     void notifyObjectLoaded(RuntimeDyld &Dyld,
     164             :                             const object::ObjectFile &Obj) override {
     165             :       LLVM_DEBUG(dbgs() << "Allocator " << Id << " applied mappings:\n");
     166          60 :       for (auto &ObjAllocs : Unmapped) {
     167          30 :         mapAllocsToRemoteAddrs(Dyld, ObjAllocs.CodeAllocs,
     168             :                                ObjAllocs.RemoteCodeAddr);
     169          30 :         mapAllocsToRemoteAddrs(Dyld, ObjAllocs.RODataAllocs,
     170             :                                ObjAllocs.RemoteRODataAddr);
     171          30 :         mapAllocsToRemoteAddrs(Dyld, ObjAllocs.RWDataAllocs,
     172             :                                ObjAllocs.RemoteRWDataAddr);
     173          30 :         Unfinalized.push_back(std::move(ObjAllocs));
     174             :       }
     175             :       Unmapped.clear();
     176          30 :     }
     177             : 
     178          37 :     bool finalizeMemory(std::string *ErrMsg = nullptr) override {
     179             :       LLVM_DEBUG(dbgs() << "Allocator " << Id << " finalizing:\n");
     180             : 
     181          67 :       for (auto &ObjAllocs : Unfinalized) {
     182          30 :         if (copyAndProtect(ObjAllocs.CodeAllocs, ObjAllocs.RemoteCodeAddr,
     183             :                            sys::Memory::MF_READ | sys::Memory::MF_EXEC))
     184             :           return true;
     185             : 
     186          30 :         if (copyAndProtect(ObjAllocs.RODataAllocs, ObjAllocs.RemoteRODataAddr,
     187             :                            sys::Memory::MF_READ))
     188             :           return true;
     189             : 
     190          30 :         if (copyAndProtect(ObjAllocs.RWDataAllocs, ObjAllocs.RemoteRWDataAddr,
     191             :                            sys::Memory::MF_READ | sys::Memory::MF_WRITE))
     192             :           return true;
     193             :       }
     194             :       Unfinalized.clear();
     195             : 
     196          41 :       for (auto &EHFrame : UnfinalizedEHFrames) {
     197           8 :         if (auto Err = Client.registerEHFrames(EHFrame.Addr, EHFrame.Size)) {
     198             :           // FIXME: Replace this once finalizeMemory can return an Error.
     199           0 :           handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
     200           0 :             if (ErrMsg) {
     201           0 :               raw_string_ostream ErrOut(*ErrMsg);
     202           0 :               EIB.log(ErrOut);
     203             :             }
     204           0 :           });
     205             :           return false;
     206             :         }
     207             :       }
     208          37 :       RegisteredEHFrames = std::move(UnfinalizedEHFrames);
     209             :       UnfinalizedEHFrames = {};
     210             : 
     211          37 :       return false;
     212             :     }
     213             : 
     214             :   private:
     215          70 :     class Alloc {
     216             :     public:
     217             :       Alloc(uint64_t Size, unsigned Align)
     218         128 :           : Size(Size), Align(Align), Contents(new char[Size + Align - 1]) {}
     219             : 
     220             :       Alloc(const Alloc &) = delete;
     221             :       Alloc &operator=(const Alloc &) = delete;
     222          12 :       Alloc(Alloc &&) = default;
     223             :       Alloc &operator=(Alloc &&) = default;
     224             : 
     225             :       uint64_t getSize() const { return Size; }
     226             : 
     227             :       unsigned getAlign() const { return Align; }
     228             : 
     229             :       char *getLocalAddress() const {
     230         192 :         uintptr_t LocalAddr = reinterpret_cast<uintptr_t>(Contents.get());
     231         128 :         LocalAddr = alignTo(LocalAddr, Align);
     232         192 :         return reinterpret_cast<char *>(LocalAddr);
     233             :       }
     234             : 
     235             :       void setRemoteAddress(JITTargetAddress RemoteAddr) {
     236          64 :         this->RemoteAddr = RemoteAddr;
     237             :       }
     238             : 
     239             :       JITTargetAddress getRemoteAddress() const { return RemoteAddr; }
     240             : 
     241             :     private:
     242             :       uint64_t Size;
     243             :       unsigned Align;
     244             :       std::unique_ptr<char[]> Contents;
     245             :       JITTargetAddress RemoteAddr = 0;
     246             :     };
     247             : 
     248          98 :     struct ObjectAllocs {
     249             :       ObjectAllocs() = default;
     250             :       ObjectAllocs(const ObjectAllocs &) = delete;
     251             :       ObjectAllocs &operator=(const ObjectAllocs &) = delete;
     252         136 :       ObjectAllocs(ObjectAllocs &&) = default;
     253             :       ObjectAllocs &operator=(ObjectAllocs &&) = default;
     254             : 
     255             :       JITTargetAddress RemoteCodeAddr = 0;
     256             :       JITTargetAddress RemoteRODataAddr = 0;
     257             :       JITTargetAddress RemoteRWDataAddr = 0;
     258             :       std::vector<Alloc> CodeAllocs, RODataAllocs, RWDataAllocs;
     259             :     };
     260             : 
     261             :     RemoteRTDyldMemoryManager(OrcRemoteTargetClient &Client,
     262             :                               ResourceIdMgr::ResourceId Id)
     263          48 :         : Client(Client), Id(Id) {
     264             :       LLVM_DEBUG(dbgs() << "Created remote allocator " << Id << "\n");
     265             :     }
     266             : 
     267             :     // Maps all allocations in Allocs to aligned blocks
     268          90 :     void mapAllocsToRemoteAddrs(RuntimeDyld &Dyld, std::vector<Alloc> &Allocs,
     269             :                                 JITTargetAddress NextAddr) {
     270         154 :       for (auto &Alloc : Allocs) {
     271          64 :         NextAddr = alignTo(NextAddr, Alloc.getAlign());
     272          64 :         Dyld.mapSectionAddress(Alloc.getLocalAddress(), NextAddr);
     273             :         LLVM_DEBUG(dbgs() << "     "
     274             :                           << static_cast<void *>(Alloc.getLocalAddress())
     275             :                           << " -> " << format("0x%016x", NextAddr) << "\n");
     276             :         Alloc.setRemoteAddress(NextAddr);
     277             : 
     278             :         // Only advance NextAddr if it was non-null to begin with,
     279             :         // otherwise leave it as null.
     280          64 :         if (NextAddr)
     281          64 :           NextAddr += Alloc.getSize();
     282             :       }
     283          90 :     }
     284             : 
     285             :     // Copies data for each alloc in the list, then set permissions on the
     286             :     // segment.
     287          90 :     bool copyAndProtect(const std::vector<Alloc> &Allocs,
     288             :                         JITTargetAddress RemoteSegmentAddr,
     289             :                         unsigned Permissions) {
     290          90 :       if (RemoteSegmentAddr) {
     291             :         assert(!Allocs.empty() && "No sections in allocated segment");
     292             : 
     293          58 :         for (auto &Alloc : Allocs) {
     294             :           LLVM_DEBUG(dbgs() << "  copying section: "
     295             :                             << static_cast<void *>(Alloc.getLocalAddress())
     296             :                             << " -> "
     297             :                             << format("0x%016x", Alloc.getRemoteAddress())
     298             :                             << " (" << Alloc.getSize() << " bytes)\n";);
     299             : 
     300         128 :           if (Client.writeMem(Alloc.getRemoteAddress(), Alloc.getLocalAddress(),
     301             :                               Alloc.getSize()))
     302             :             return true;
     303             :         }
     304             : 
     305             :         LLVM_DEBUG(dbgs() << "  setting "
     306             :                           << (Permissions & sys::Memory::MF_READ ? 'R' : '-')
     307             :                           << (Permissions & sys::Memory::MF_WRITE ? 'W' : '-')
     308             :                           << (Permissions & sys::Memory::MF_EXEC ? 'X' : '-')
     309             :                           << " permissions on block: "
     310             :                           << format("0x%016x", RemoteSegmentAddr) << "\n");
     311          58 :         if (Client.setProtections(Id, RemoteSegmentAddr, Permissions))
     312             :           return true;
     313             :       }
     314             :       return false;
     315             :     }
     316             : 
     317             :     OrcRemoteTargetClient &Client;
     318             :     ResourceIdMgr::ResourceId Id;
     319             :     std::vector<ObjectAllocs> Unmapped;
     320             :     std::vector<ObjectAllocs> Unfinalized;
     321             : 
     322             :     struct EHFrame {
     323             :       JITTargetAddress Addr;
     324             :       uint64_t Size;
     325             :     };
     326             :     std::vector<EHFrame> UnfinalizedEHFrames;
     327             :     std::vector<EHFrame> RegisteredEHFrames;
     328             :   };
     329             : 
     330             :   /// Remote indirect stubs manager.
     331             :   class RemoteIndirectStubsManager : public IndirectStubsManager {
     332             :   public:
     333             :     RemoteIndirectStubsManager(OrcRemoteTargetClient &Client,
     334             :                                ResourceIdMgr::ResourceId Id)
     335             :         : Client(Client), Id(Id) {}
     336             : 
     337           0 :     ~RemoteIndirectStubsManager() override {
     338           0 :       Client.destroyIndirectStubsManager(Id);
     339           0 :     }
     340             : 
     341           0 :     Error createStub(StringRef StubName, JITTargetAddress StubAddr,
     342             :                      JITSymbolFlags StubFlags) override {
     343           0 :       if (auto Err = reserveStubs(1))
     344             :         return Err;
     345             : 
     346           0 :       return createStubInternal(StubName, StubAddr, StubFlags);
     347             :     }
     348             : 
     349           0 :     Error createStubs(const StubInitsMap &StubInits) override {
     350           0 :       if (auto Err = reserveStubs(StubInits.size()))
     351             :         return Err;
     352             : 
     353           0 :       for (auto &Entry : StubInits)
     354           0 :         if (auto Err = createStubInternal(Entry.first(), Entry.second.first,
     355           0 :                                           Entry.second.second))
     356             :           return Err;
     357             : 
     358             :       return Error::success();
     359             :     }
     360             : 
     361           0 :     JITEvaluatedSymbol findStub(StringRef Name, bool ExportedStubsOnly) override {
     362           0 :       auto I = StubIndexes.find(Name);
     363           0 :       if (I == StubIndexes.end())
     364             :         return nullptr;
     365           0 :       auto Key = I->second.first;
     366           0 :       auto Flags = I->second.second;
     367             :       auto StubSymbol = JITEvaluatedSymbol(getStubAddr(Key), Flags);
     368           0 :       if (ExportedStubsOnly && !StubSymbol.getFlags().isExported())
     369             :         return nullptr;
     370           0 :       return StubSymbol;
     371             :     }
     372             : 
     373           0 :     JITEvaluatedSymbol findPointer(StringRef Name) override {
     374           0 :       auto I = StubIndexes.find(Name);
     375           0 :       if (I == StubIndexes.end())
     376             :         return nullptr;
     377           0 :       auto Key = I->second.first;
     378           0 :       auto Flags = I->second.second;
     379             :       return JITEvaluatedSymbol(getPtrAddr(Key), Flags);
     380             :     }
     381             : 
     382           0 :     Error updatePointer(StringRef Name, JITTargetAddress NewAddr) override {
     383           0 :       auto I = StubIndexes.find(Name);
     384             :       assert(I != StubIndexes.end() && "No stub pointer for symbol");
     385           0 :       auto Key = I->second.first;
     386           0 :       return Client.writePointer(getPtrAddr(Key), NewAddr);
     387             :     }
     388             : 
     389             :   private:
     390             :     struct RemoteIndirectStubsInfo {
     391             :       JITTargetAddress StubBase;
     392             :       JITTargetAddress PtrBase;
     393             :       unsigned NumStubs;
     394             :     };
     395             : 
     396             :     using StubKey = std::pair<uint16_t, uint16_t>;
     397             : 
     398           0 :     Error reserveStubs(unsigned NumStubs) {
     399           0 :       if (NumStubs <= FreeStubs.size())
     400             :         return Error::success();
     401             : 
     402           0 :       unsigned NewStubsRequired = NumStubs - FreeStubs.size();
     403             :       JITTargetAddress StubBase;
     404             :       JITTargetAddress PtrBase;
     405             :       unsigned NumStubsEmitted;
     406             : 
     407           0 :       if (auto StubInfoOrErr = Client.emitIndirectStubs(Id, NewStubsRequired))
     408             :         std::tie(StubBase, PtrBase, NumStubsEmitted) = *StubInfoOrErr;
     409             :       else
     410             :         return StubInfoOrErr.takeError();
     411             : 
     412           0 :       unsigned NewBlockId = RemoteIndirectStubsInfos.size();
     413           0 :       RemoteIndirectStubsInfos.push_back({StubBase, PtrBase, NumStubsEmitted});
     414             : 
     415           0 :       for (unsigned I = 0; I < NumStubsEmitted; ++I)
     416           0 :         FreeStubs.push_back(std::make_pair(NewBlockId, I));
     417             : 
     418             :       return Error::success();
     419             :     }
     420             : 
     421           0 :     Error createStubInternal(StringRef StubName, JITTargetAddress InitAddr,
     422             :                              JITSymbolFlags StubFlags) {
     423           0 :       auto Key = FreeStubs.back();
     424             :       FreeStubs.pop_back();
     425           0 :       StubIndexes[StubName] = std::make_pair(Key, StubFlags);
     426           0 :       return Client.writePointer(getPtrAddr(Key), InitAddr);
     427             :     }
     428             : 
     429             :     JITTargetAddress getStubAddr(StubKey K) {
     430             :       assert(RemoteIndirectStubsInfos[K.first].StubBase != 0 &&
     431             :              "Missing stub address");
     432           0 :       return RemoteIndirectStubsInfos[K.first].StubBase +
     433           0 :              K.second * Client.getIndirectStubSize();
     434             :     }
     435             : 
     436             :     JITTargetAddress getPtrAddr(StubKey K) {
     437             :       assert(RemoteIndirectStubsInfos[K.first].PtrBase != 0 &&
     438             :              "Missing pointer address");
     439           0 :       return RemoteIndirectStubsInfos[K.first].PtrBase +
     440           0 :              K.second * Client.getPointerSize();
     441             :     }
     442             : 
     443             :     OrcRemoteTargetClient &Client;
     444             :     ResourceIdMgr::ResourceId Id;
     445             :     std::vector<RemoteIndirectStubsInfo> RemoteIndirectStubsInfos;
     446             :     std::vector<StubKey> FreeStubs;
     447             :     StringMap<std::pair<StubKey, JITSymbolFlags>> StubIndexes;
     448             :   };
     449             : 
     450             :   /// Remote compile callback manager.
     451           0 :   class RemoteCompileCallbackManager : public JITCompileCallbackManager {
     452             :   public:
     453             :     RemoteCompileCallbackManager(OrcRemoteTargetClient &Client,
     454             :                                  ExecutionSession &ES,
     455             :                                  JITTargetAddress ErrorHandlerAddress)
     456             :         : JITCompileCallbackManager(ES, ErrorHandlerAddress), Client(Client) {}
     457             : 
     458             :   private:
     459           0 :     Error grow() override {
     460           0 :       JITTargetAddress BlockAddr = 0;
     461           0 :       uint32_t NumTrampolines = 0;
     462           0 :       if (auto TrampolineInfoOrErr = Client.emitTrampolineBlock())
     463             :         std::tie(BlockAddr, NumTrampolines) = *TrampolineInfoOrErr;
     464             :       else
     465             :         return TrampolineInfoOrErr.takeError();
     466             : 
     467           0 :       uint32_t TrampolineSize = Client.getTrampolineSize();
     468           0 :       for (unsigned I = 0; I < NumTrampolines; ++I)
     469           0 :         this->AvailableTrampolines.push_back(BlockAddr + (I * TrampolineSize));
     470             : 
     471             :       return Error::success();
     472             :     }
     473             : 
     474             :     OrcRemoteTargetClient &Client;
     475             :   };
     476             : 
     477             :   /// Create an OrcRemoteTargetClient.
     478             :   /// Channel is the ChannelT instance to communicate on. It is assumed that
     479             :   /// the channel is ready to be read from and written to.
     480             :   static Expected<std::unique_ptr<OrcRemoteTargetClient>>
     481          24 :   Create(rpc::RawByteChannel &Channel, ExecutionSession &ES) {
     482             :     Error Err = Error::success();
     483             :     auto Client = std::unique_ptr<OrcRemoteTargetClient>(
     484          48 :         new OrcRemoteTargetClient(Channel, ES, Err));
     485          24 :     if (Err)
     486             :       return std::move(Err);
     487             :     return std::move(Client);
     488             :   }
     489             : 
     490             :   /// Call the int(void) function at the given address in the target and return
     491             :   /// its result.
     492             :   Expected<int> callIntVoid(JITTargetAddress Addr) {
     493             :     LLVM_DEBUG(dbgs() << "Calling int(*)(void) " << format("0x%016x", Addr)
     494             :                       << "\n");
     495          24 :     return callB<exec::CallIntVoid>(Addr);
     496             :   }
     497             : 
     498             :   /// Call the int(int, char*[]) function at the given address in the target and
     499             :   /// return its result.
     500             :   Expected<int> callMain(JITTargetAddress Addr,
     501             :                          const std::vector<std::string> &Args) {
     502             :     LLVM_DEBUG(dbgs() << "Calling int(*)(int, char*[]) "
     503             :                       << format("0x%016x", Addr) << "\n");
     504             :     return callB<exec::CallMain>(Addr, Args);
     505             :   }
     506             : 
     507             :   /// Call the void() function at the given address in the target and wait for
     508             :   /// it to finish.
     509             :   Error callVoidVoid(JITTargetAddress Addr) {
     510             :     LLVM_DEBUG(dbgs() << "Calling void(*)(void) " << format("0x%016x", Addr)
     511             :                       << "\n");
     512             :     return callB<exec::CallVoidVoid>(Addr);
     513             :   }
     514             : 
     515             :   /// Create an RCMemoryManager which will allocate its memory on the remote
     516             :   /// target.
     517             :   Expected<std::unique_ptr<RemoteRTDyldMemoryManager>>
     518          24 :   createRemoteMemoryManager() {
     519          24 :     auto Id = AllocatorIds.getNext();
     520          48 :     if (auto Err = callB<mem::CreateRemoteAllocator>(Id))
     521             :       return std::move(Err);
     522             :     return std::unique_ptr<RemoteRTDyldMemoryManager>(
     523          24 :         new RemoteRTDyldMemoryManager(*this, Id));
     524             :   }
     525             : 
     526             :   /// Create an RCIndirectStubsManager that will allocate stubs on the remote
     527             :   /// target.
     528             :   Expected<std::unique_ptr<RemoteIndirectStubsManager>>
     529             :   createIndirectStubsManager() {
     530             :     auto Id = IndirectStubOwnerIds.getNext();
     531             :     if (auto Err = callB<stubs::CreateIndirectStubsOwner>(Id))
     532             :       return std::move(Err);
     533             :     return llvm::make_unique<RemoteIndirectStubsManager>(*this, Id);
     534             :   }
     535             : 
     536             :   Expected<RemoteCompileCallbackManager &>
     537             :   enableCompileCallbacks(JITTargetAddress ErrorHandlerAddress) {
     538             :     assert(!CallbackManager && "CallbackManager already obtained");
     539             : 
     540             :     // Emit the resolver block on the JIT server.
     541             :     if (auto Err = callB<stubs::EmitResolverBlock>())
     542             :       return std::move(Err);
     543             : 
     544             :     // Create the callback manager.
     545             :     CallbackManager.emplace(*this, ES, ErrorHandlerAddress);
     546             :     RemoteCompileCallbackManager &Mgr = *CallbackManager;
     547             :     return Mgr;
     548             :   }
     549             : 
     550             :   /// Search for symbols in the remote process. Note: This should be used by
     551             :   /// symbol resolvers *after* they've searched the local symbol table in the
     552             :   /// JIT stack.
     553             :   Expected<JITTargetAddress> getSymbolAddress(StringRef Name) {
     554          20 :     return callB<utils::GetSymbolAddress>(Name);
     555             :   }
     556             : 
     557             :   /// Get the triple for the remote target.
     558             :   const std::string &getTargetTriple() const { return RemoteTargetTriple; }
     559             : 
     560          24 :   Error terminateSession() { return callB<utils::TerminateSession>(); }
     561             : 
     562             : private:
     563          24 :   OrcRemoteTargetClient(rpc::RawByteChannel &Channel, ExecutionSession &ES,
     564             :                         Error &Err)
     565          24 :       : rpc::SingleThreadedRPCEndpoint<rpc::RawByteChannel>(Channel, true),
     566          96 :         ES(ES) {
     567             :     ErrorAsOutParameter EAO(&Err);
     568             : 
     569             :     addHandler<utils::RequestCompile>(
     570           0 :         [this](JITTargetAddress Addr) -> JITTargetAddress {
     571           0 :           if (CallbackManager)
     572           0 :             return CallbackManager->executeCompileCallback(Addr);
     573             :           return 0;
     574             :         });
     575             : 
     576          72 :     if (auto RIOrErr = callB<utils::GetRemoteInfo>()) {
     577          48 :       std::tie(RemoteTargetTriple, RemotePointerSize, RemotePageSize,
     578             :                RemoteTrampolineSize, RemoteIndirectStubSize) = *RIOrErr;
     579             :       Err = Error::success();
     580             :     } else
     581             :       Err = RIOrErr.takeError();
     582          24 :   }
     583             : 
     584           1 :   void deregisterEHFrames(JITTargetAddress Addr, uint32_t Size) {
     585           2 :     if (auto Err = callB<eh::RegisterEHFrames>(Addr, Size))
     586           0 :       ES.reportError(std::move(Err));
     587           1 :   }
     588             : 
     589          24 :   void destroyRemoteAllocator(ResourceIdMgr::ResourceId Id) {
     590          48 :     if (auto Err = callB<mem::DestroyRemoteAllocator>(Id)) {
     591             :       // FIXME: This will be triggered by a removeModuleSet call: Propagate
     592             :       //        error return up through that.
     593           0 :       llvm_unreachable("Failed to destroy remote allocator.");
     594             :       AllocatorIds.release(Id);
     595             :     }
     596          24 :   }
     597             : 
     598           0 :   void destroyIndirectStubsManager(ResourceIdMgr::ResourceId Id) {
     599           0 :     IndirectStubOwnerIds.release(Id);
     600           0 :     if (auto Err = callB<stubs::DestroyIndirectStubsOwner>(Id))
     601           0 :       ES.reportError(std::move(Err));
     602           0 :   }
     603             : 
     604             :   Expected<std::tuple<JITTargetAddress, JITTargetAddress, uint32_t>>
     605             :   emitIndirectStubs(ResourceIdMgr::ResourceId Id, uint32_t NumStubsRequired) {
     606           0 :     return callB<stubs::EmitIndirectStubs>(Id, NumStubsRequired);
     607             :   }
     608             : 
     609             :   Expected<std::tuple<JITTargetAddress, uint32_t>> emitTrampolineBlock() {
     610           0 :     return callB<stubs::EmitTrampolineBlock>();
     611             :   }
     612             : 
     613             :   uint32_t getIndirectStubSize() const { return RemoteIndirectStubSize; }
     614             :   uint32_t getPageSize() const { return RemotePageSize; }
     615             :   uint32_t getPointerSize() const { return RemotePointerSize; }
     616             : 
     617             :   uint32_t getTrampolineSize() const { return RemoteTrampolineSize; }
     618             : 
     619             :   Expected<std::vector<uint8_t>> readMem(char *Dst, JITTargetAddress Src,
     620             :                                          uint64_t Size) {
     621             :     return callB<mem::ReadMem>(Src, Size);
     622             :   }
     623             : 
     624             :   Error registerEHFrames(JITTargetAddress &RAddr, uint32_t Size) {
     625             :     // FIXME: Duplicate error and report it via ReportError too?
     626           4 :     return callB<eh::RegisterEHFrames>(RAddr, Size);
     627             :   }
     628             : 
     629          58 :   JITTargetAddress reserveMem(ResourceIdMgr::ResourceId Id, uint64_t Size,
     630             :                               uint32_t Align) {
     631         116 :     if (auto AddrOrErr = callB<mem::ReserveMem>(Id, Size, Align))
     632          58 :       return *AddrOrErr;
     633             :     else {
     634           0 :       ES.reportError(AddrOrErr.takeError());
     635           0 :       return 0;
     636             :     }
     637             :   }
     638             : 
     639          58 :   bool setProtections(ResourceIdMgr::ResourceId Id,
     640             :                       JITTargetAddress RemoteSegAddr, unsigned ProtFlags) {
     641         116 :     if (auto Err = callB<mem::SetProtections>(Id, RemoteSegAddr, ProtFlags)) {
     642           0 :       ES.reportError(std::move(Err));
     643           0 :       return true;
     644             :     } else
     645             :       return false;
     646             :   }
     647             : 
     648          64 :   bool writeMem(JITTargetAddress Addr, const char *Src, uint64_t Size) {
     649         192 :     if (auto Err = callB<mem::WriteMem>(DirectBufferWriter(Src, Addr, Size))) {
     650           0 :       ES.reportError(std::move(Err));
     651           0 :       return true;
     652             :     } else
     653             :       return false;
     654             :   }
     655             : 
     656             :   Error writePointer(JITTargetAddress Addr, JITTargetAddress PtrVal) {
     657           0 :     return callB<mem::WritePtr>(Addr, PtrVal);
     658             :   }
     659             : 
     660             :   static Error doNothing() { return Error::success(); }
     661             : 
     662             :   ExecutionSession &ES;
     663             :   std::function<void(Error)> ReportError;
     664             :   std::string RemoteTargetTriple;
     665             :   uint32_t RemotePointerSize = 0;
     666             :   uint32_t RemotePageSize = 0;
     667             :   uint32_t RemoteTrampolineSize = 0;
     668             :   uint32_t RemoteIndirectStubSize = 0;
     669             :   ResourceIdMgr AllocatorIds, IndirectStubOwnerIds;
     670             :   Optional<RemoteCompileCallbackManager> CallbackManager;
     671             : };
     672             : 
     673             : } // end namespace remote
     674             : } // end namespace orc
     675             : } // end namespace llvm
     676             : 
     677             : #undef DEBUG_TYPE
     678             : 
     679             : #endif // LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETCLIENT_H

Generated by: LCOV version 1.13