summaryrefslogtreecommitdiffstats
path: root/include/llvm/ExecutionEngine/Orc
diff options
context:
space:
mode:
Diffstat (limited to 'include/llvm/ExecutionEngine/Orc')
-rw-r--r--include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h106
-rw-r--r--include/llvm/ExecutionEngine/Orc/IndirectionUtils.h10
-rw-r--r--include/llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h4
-rw-r--r--include/llvm/ExecutionEngine/Orc/OrcArchitectureSupport.h (renamed from include/llvm/ExecutionEngine/Orc/OrcTargetSupport.h)77
-rw-r--r--include/llvm/ExecutionEngine/Orc/OrcError.h37
-rw-r--r--include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h784
-rw-r--r--include/llvm/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h185
-rw-r--r--include/llvm/ExecutionEngine/Orc/OrcRemoteTargetServer.h432
-rw-r--r--include/llvm/ExecutionEngine/Orc/RPCChannel.h179
-rw-r--r--include/llvm/ExecutionEngine/Orc/RPCUtils.h266
10 files changed, 2020 insertions, 60 deletions
diff --git a/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h b/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h
index 7dab5d1..84af472 100644
--- a/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h
+++ b/include/llvm/ExecutionEngine/Orc/CompileOnDemandLayer.h
@@ -19,7 +19,6 @@
#include "LambdaResolver.h"
#include "LogicalDylib.h"
#include "llvm/ADT/STLExtras.h"
-#include "llvm/ExecutionEngine/SectionMemoryManager.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include <list>
#include <memory>
@@ -61,31 +60,36 @@ private:
typedef typename BaseLayerT::ModuleSetHandleT BaseLayerModuleSetHandleT;
- class ModuleOwner {
+ // Provide type-erasure for the Modules and MemoryManagers.
+ template <typename ResourceT>
+ class ResourceOwner {
public:
- ModuleOwner() = default;
- ModuleOwner(const ModuleOwner&) = delete;
- ModuleOwner& operator=(const ModuleOwner&) = delete;
- virtual ~ModuleOwner() { }
- virtual Module& getModule() const = 0;
+ ResourceOwner() = default;
+ ResourceOwner(const ResourceOwner&) = delete;
+ ResourceOwner& operator=(const ResourceOwner&) = delete;
+ virtual ~ResourceOwner() { }
+ virtual ResourceT& getResource() const = 0;
};
- template <typename ModulePtrT>
- class ModuleOwnerImpl : public ModuleOwner {
+ template <typename ResourceT, typename ResourcePtrT>
+ class ResourceOwnerImpl : public ResourceOwner<ResourceT> {
public:
- ModuleOwnerImpl(ModulePtrT ModulePtr) : ModulePtr(std::move(ModulePtr)) {}
- Module& getModule() const override { return *ModulePtr; }
+ ResourceOwnerImpl(ResourcePtrT ResourcePtr)
+ : ResourcePtr(std::move(ResourcePtr)) {}
+ ResourceT& getResource() const override { return *ResourcePtr; }
private:
- ModulePtrT ModulePtr;
+ ResourcePtrT ResourcePtr;
};
- template <typename ModulePtrT>
- std::unique_ptr<ModuleOwner> wrapOwnership(ModulePtrT ModulePtr) {
- return llvm::make_unique<ModuleOwnerImpl<ModulePtrT>>(std::move(ModulePtr));
+ template <typename ResourceT, typename ResourcePtrT>
+ std::unique_ptr<ResourceOwner<ResourceT>>
+ wrapOwnership(ResourcePtrT ResourcePtr) {
+ typedef ResourceOwnerImpl<ResourceT, ResourcePtrT> RO;
+ return llvm::make_unique<RO>(std::move(ResourcePtr));
}
struct LogicalModuleResources {
- std::unique_ptr<ModuleOwner> SourceModuleOwner;
+ std::unique_ptr<ResourceOwner<Module>> SourceModule;
std::set<const Function*> StubsToClone;
std::unique_ptr<IndirectStubsMgrT> StubsMgr;
@@ -93,15 +97,16 @@ private:
// Explicit move constructor to make MSVC happy.
LogicalModuleResources(LogicalModuleResources &&Other)
- : SourceModuleOwner(std::move(Other.SourceModuleOwner)),
+ : SourceModule(std::move(Other.SourceModule)),
StubsToClone(std::move(Other.StubsToClone)),
StubsMgr(std::move(Other.StubsMgr)) {}
// Explicit move assignment to make MSVC happy.
LogicalModuleResources& operator=(LogicalModuleResources &&Other) {
- SourceModuleOwner = std::move(Other.SourceModuleOwner);
+ SourceModule = std::move(Other.SourceModule);
StubsToClone = std::move(Other.StubsToClone);
StubsMgr = std::move(Other.StubsMgr);
+ return *this;
}
JITSymbol findSymbol(StringRef Name, bool ExportedSymbolsOnly) {
@@ -114,12 +119,35 @@ private:
};
-
-
struct LogicalDylibResources {
typedef std::function<RuntimeDyld::SymbolInfo(const std::string&)>
SymbolResolverFtor;
+
+ typedef std::function<typename BaseLayerT::ModuleSetHandleT(
+ BaseLayerT&,
+ std::unique_ptr<Module>,
+ std::unique_ptr<RuntimeDyld::SymbolResolver>)>
+ ModuleAdderFtor;
+
+ LogicalDylibResources() = default;
+
+ // Explicit move constructor to make MSVC happy.
+ LogicalDylibResources(LogicalDylibResources &&Other)
+ : ExternalSymbolResolver(std::move(Other.ExternalSymbolResolver)),
+ MemMgr(std::move(Other.MemMgr)),
+ ModuleAdder(std::move(Other.ModuleAdder)) {}
+
+ // Explicit move assignment operator to make MSVC happy.
+ LogicalDylibResources& operator=(LogicalDylibResources &&Other) {
+ ExternalSymbolResolver = std::move(Other.ExternalSymbolResolver);
+ MemMgr = std::move(Other.MemMgr);
+ ModuleAdder = std::move(Other.ModuleAdder);
+ return *this;
+ }
+
SymbolResolverFtor ExternalSymbolResolver;
+ std::unique_ptr<ResourceOwner<RuntimeDyld::MemoryManager>> MemMgr;
+ ModuleAdderFtor ModuleAdder;
};
typedef LogicalDylib<BaseLayerT, LogicalModuleResources,
@@ -157,9 +185,6 @@ public:
MemoryManagerPtrT MemMgr,
SymbolResolverPtrT Resolver) {
- assert(MemMgr == nullptr &&
- "User supplied memory managers not supported with COD yet.");
-
LogicalDylibs.push_back(CODLogicalDylib(BaseLayer));
auto &LDResources = LogicalDylibs.back().getDylibResources();
@@ -168,6 +193,18 @@ public:
return Resolver->findSymbol(Name);
};
+ auto &MemMgrRef = *MemMgr;
+ LDResources.MemMgr =
+ wrapOwnership<RuntimeDyld::MemoryManager>(std::move(MemMgr));
+
+ LDResources.ModuleAdder =
+ [&MemMgrRef](BaseLayerT &B, std::unique_ptr<Module> M,
+ std::unique_ptr<RuntimeDyld::SymbolResolver> R) {
+ std::vector<std::unique_ptr<Module>> Ms;
+ Ms.push_back(std::move(M));
+ return B.addModuleSet(std::move(Ms), &MemMgrRef, std::move(R));
+ };
+
// Process each of the modules in this module set.
for (auto &M : Ms)
addLogicalModule(LogicalDylibs.back(), std::move(M));
@@ -215,9 +252,9 @@ private:
auto LMH = LD.createLogicalModule();
auto &LMResources = LD.getLogicalModuleResources(LMH);
- LMResources.SourceModuleOwner = wrapOwnership(std::move(SrcMPtr));
+ LMResources.SourceModule = wrapOwnership<Module>(std::move(SrcMPtr));
- Module &SrcM = LMResources.SourceModuleOwner->getModule();
+ Module &SrcM = LMResources.SourceModule->getResource();
// Create the GlobalValues module.
const DataLayout &DL = SrcM.getDataLayout();
@@ -326,12 +363,9 @@ private:
return RuntimeDyld::SymbolInfo(nullptr);
});
- std::vector<std::unique_ptr<Module>> GVsMSet;
- GVsMSet.push_back(std::move(GVsM));
auto GVsH =
- BaseLayer.addModuleSet(std::move(GVsMSet),
- llvm::make_unique<SectionMemoryManager>(),
- std::move(GVsResolver));
+ LD.getDylibResources().ModuleAdder(BaseLayer, std::move(GVsM),
+ std::move(GVsResolver));
LD.addToLogicalModule(LMH, GVsH);
}
@@ -348,7 +382,7 @@ private:
LogicalModuleHandle LMH,
Function &F) {
auto &LMResources = LD.getLogicalModuleResources(LMH);
- Module &SrcM = LMResources.SourceModuleOwner->getModule();
+ Module &SrcM = LMResources.SourceModule->getResource();
// If F is a declaration we must already have compiled it.
if (F.isDeclaration())
@@ -386,7 +420,7 @@ private:
LogicalModuleHandle LMH,
const PartitionT &Part) {
auto &LMResources = LD.getLogicalModuleResources(LMH);
- Module &SrcM = LMResources.SourceModuleOwner->getModule();
+ Module &SrcM = LMResources.SourceModule->getResource();
// Create the module.
std::string NewName = SrcM.getName();
@@ -445,7 +479,6 @@ private:
moveFunctionBody(*F, VMap, &Materializer);
// Create memory manager and symbol resolver.
- auto MemMgr = llvm::make_unique<SectionMemoryManager>();
auto Resolver = createLambdaResolver(
[this, &LD, LMH](const std::string &Name) {
if (auto Symbol = LD.findSymbolInternally(LMH, Name))
@@ -459,10 +492,9 @@ private:
Symbol.getFlags());
return RuntimeDyld::SymbolInfo(nullptr);
});
- std::vector<std::unique_ptr<Module>> PartMSet;
- PartMSet.push_back(std::move(M));
- return BaseLayer.addModuleSet(std::move(PartMSet), std::move(MemMgr),
- std::move(Resolver));
+
+ return LD.getDylibResources().ModuleAdder(BaseLayer, std::move(M),
+ std::move(Resolver));
}
BaseLayerT &BaseLayer;
diff --git a/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h b/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h
index d6ee3a8..e17630f 100644
--- a/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h
+++ b/include/llvm/ExecutionEngine/Orc/IndirectionUtils.h
@@ -22,6 +22,7 @@
#include "llvm/IR/Mangler.h"
#include "llvm/IR/Module.h"
#include "llvm/Transforms/Utils/ValueMapper.h"
+#include "llvm/Support/Process.h"
#include <sstream>
namespace llvm {
@@ -179,14 +180,15 @@ private:
std::error_code EC;
auto TrampolineBlock =
sys::OwningMemoryBlock(
- sys::Memory::allocateMappedMemory(TargetT::PageSize, nullptr,
+ sys::Memory::allocateMappedMemory(sys::Process::getPageSize(), nullptr,
sys::Memory::MF_READ |
sys::Memory::MF_WRITE, EC));
assert(!EC && "Failed to allocate trampoline block");
unsigned NumTrampolines =
- (TargetT::PageSize - TargetT::PointerSize) / TargetT::TrampolineSize;
+ (sys::Process::getPageSize() - TargetT::PointerSize) /
+ TargetT::TrampolineSize;
uint8_t *TrampolineMem = static_cast<uint8_t*>(TrampolineBlock.base());
TargetT::writeTrampolines(TrampolineMem, ResolverBlock.base(),
@@ -240,8 +242,8 @@ private:
virtual void anchor();
};
-/// @brief IndirectStubsManager implementation for a concrete target, e.g.
-/// OrcX86_64. (See OrcTargetSupport.h).
+/// @brief IndirectStubsManager implementation for the host architecture, e.g.
+/// OrcX86_64. (See OrcArchitectureSupport.h).
template <typename TargetT>
class LocalIndirectStubsManager : public IndirectStubsManager {
public:
diff --git a/include/llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h b/include/llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h
index 2acfecf..4dc48f1 100644
--- a/include/llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h
+++ b/include/llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h
@@ -108,9 +108,7 @@ private:
void Finalize() override {
State = Finalizing;
- RTDyld->resolveRelocations();
- RTDyld->registerEHFrames();
- MemMgr->finalizeMemory();
+ RTDyld->finalizeWithMemoryManagerLocking();
State = Finalized;
}
diff --git a/include/llvm/ExecutionEngine/Orc/OrcTargetSupport.h b/include/llvm/ExecutionEngine/Orc/OrcArchitectureSupport.h
index 246d3e0..1b0488b 100644
--- a/include/llvm/ExecutionEngine/Orc/OrcTargetSupport.h
+++ b/include/llvm/ExecutionEngine/Orc/OrcArchitectureSupport.h
@@ -1,4 +1,4 @@
-//===-- OrcTargetSupport.h - Code to support specific targets --*- C++ -*-===//
+//===-- OrcArchitectureSupport.h - Architecture support code ---*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
@@ -7,32 +7,76 @@
//
//===----------------------------------------------------------------------===//
//
-// Target specific code for Orc, e.g. callback assembly.
+// Architecture specific code for Orc, e.g. callback assembly.
//
-// Target classes should be part of the JIT *target* process, not the host
+// Architecture classes should be part of the JIT *target* process, not the host
// process (except where you're doing hosted JITing and the two are one and the
// same).
//
//===----------------------------------------------------------------------===//
-#ifndef LLVM_EXECUTIONENGINE_ORC_ORCTARGETSUPPORT_H
-#define LLVM_EXECUTIONENGINE_ORC_ORCTARGETSUPPORT_H
+#ifndef LLVM_EXECUTIONENGINE_ORC_ORCARCHITECTURESUPPORT_H
+#define LLVM_EXECUTIONENGINE_ORC_ORCARCHITECTURESUPPORT_H
#include "IndirectionUtils.h"
#include "llvm/Support/Memory.h"
+#include "llvm/Support/Process.h"
namespace llvm {
namespace orc {
+/// Generic ORC Architecture support.
+///
+/// This class can be substituted as the target architecure support class for
+/// ORC templates that require one (e.g. IndirectStubsManagers). It does not
+/// support lazy JITing however, and any attempt to use that functionality
+/// will result in execution of an llvm_unreachable.
+class OrcGenericArchitecture {
+public:
+ static const unsigned PointerSize = sizeof(uintptr_t);
+ static const unsigned TrampolineSize = 1;
+ static const unsigned ResolverCodeSize = 1;
+
+ typedef TargetAddress (*JITReentryFn)(void *CallbackMgr, void *TrampolineId);
+
+ static void writeResolverCode(uint8_t *ResolveMem, JITReentryFn Reentry,
+ void *CallbackMgr) {
+ llvm_unreachable("writeResolverCode is not supported by the generic host "
+ "support class");
+ }
+
+ static void writeTrampolines(uint8_t *TrampolineMem, void *ResolverAddr,
+ unsigned NumTrampolines) {
+ llvm_unreachable("writeTrampolines is not supported by the generic host "
+ "support class");
+ }
+
+ class IndirectStubsInfo {
+ public:
+ const static unsigned StubSize = 1;
+ unsigned getNumStubs() const { llvm_unreachable("Not supported"); }
+ void *getStub(unsigned Idx) const { llvm_unreachable("Not supported"); }
+ void **getPtr(unsigned Idx) const { llvm_unreachable("Not supported"); }
+ };
+
+ static std::error_code emitIndirectStubsBlock(IndirectStubsInfo &StubsInfo,
+ unsigned MinStubs,
+ void *InitialPtrVal) {
+ llvm_unreachable("emitIndirectStubsBlock is not supported by the generic "
+ "host support class");
+ }
+};
+
+/// @brief X86_64 support.
+///
+/// X86_64 supports lazy JITing.
class OrcX86_64 {
public:
- static const unsigned PageSize = 4096;
static const unsigned PointerSize = 8;
static const unsigned TrampolineSize = 8;
static const unsigned ResolverCodeSize = 0x78;
- typedef TargetAddress (*JITReentryFn)(void *CallbackMgr,
- void *TrampolineId);
+ typedef TargetAddress (*JITReentryFn)(void *CallbackMgr, void *TrampolineId);
/// @brief Write the resolver code into the given memory. The user is be
/// responsible for allocating the memory and setting permissions.
@@ -49,16 +93,16 @@ public:
/// makeIndirectStubsBlock function.
class IndirectStubsInfo {
friend class OrcX86_64;
+
public:
const static unsigned StubSize = 8;
- const static unsigned PtrSize = 8;
IndirectStubsInfo() : NumStubs(0) {}
IndirectStubsInfo(IndirectStubsInfo &&Other)
: NumStubs(Other.NumStubs), StubsMem(std::move(Other.StubsMem)) {
Other.NumStubs = 0;
}
- IndirectStubsInfo& operator=(IndirectStubsInfo &&Other) {
+ IndirectStubsInfo &operator=(IndirectStubsInfo &&Other) {
NumStubs = Other.NumStubs;
Other.NumStubs = 0;
StubsMem = std::move(Other.StubsMem);
@@ -70,17 +114,18 @@ public:
/// @brief Get a pointer to the stub at the given index, which must be in
/// the range 0 .. getNumStubs() - 1.
- void* getStub(unsigned Idx) const {
- return static_cast<uint64_t*>(StubsMem.base()) + Idx;
+ void *getStub(unsigned Idx) const {
+ return static_cast<uint64_t *>(StubsMem.base()) + Idx;
}
/// @brief Get a pointer to the implementation-pointer at the given index,
/// which must be in the range 0 .. getNumStubs() - 1.
- void** getPtr(unsigned Idx) const {
+ void **getPtr(unsigned Idx) const {
char *PtrsBase =
- static_cast<char*>(StubsMem.base()) + NumStubs * StubSize;
- return reinterpret_cast<void**>(PtrsBase) + Idx;
+ static_cast<char *>(StubsMem.base()) + NumStubs * StubSize;
+ return reinterpret_cast<void **>(PtrsBase) + Idx;
}
+
private:
unsigned NumStubs;
sys::OwningMemoryBlock StubsMem;
@@ -100,4 +145,4 @@ public:
} // End namespace orc.
} // End namespace llvm.
-#endif // LLVM_EXECUTIONENGINE_ORC_ORCTARGETSUPPORT_H
+#endif // LLVM_EXECUTIONENGINE_ORC_ORCARCHITECTURESUPPORT_H
diff --git a/include/llvm/ExecutionEngine/Orc/OrcError.h b/include/llvm/ExecutionEngine/Orc/OrcError.h
new file mode 100644
index 0000000..48f35d6
--- /dev/null
+++ b/include/llvm/ExecutionEngine/Orc/OrcError.h
@@ -0,0 +1,37 @@
+//===------ OrcError.h - Reject symbol lookup requests ------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Define an error category, error codes, and helper utilities for Orc.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_EXECUTIONENGINE_ORC_ORCERROR_H
+#define LLVM_EXECUTIONENGINE_ORC_ORCERROR_H
+
+#include <system_error>
+
+namespace llvm {
+namespace orc {
+
+enum class OrcErrorCode : int {
+ // RPC Errors
+ RemoteAllocatorDoesNotExist = 1,
+ RemoteAllocatorIdAlreadyInUse,
+ RemoteMProtectAddrUnrecognized,
+ RemoteIndirectStubsOwnerDoesNotExist,
+ RemoteIndirectStubsOwnerIdAlreadyInUse,
+ UnexpectedRPCCall
+};
+
+std::error_code orcError(OrcErrorCode ErrCode);
+
+} // End namespace orc.
+} // End namespace llvm.
+
+#endif // LLVM_EXECUTIONENGINE_ORC_ORCERROR_H
diff --git a/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h b/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h
new file mode 100644
index 0000000..d7640b8
--- /dev/null
+++ b/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetClient.h
@@ -0,0 +1,784 @@
+//===---- OrcRemoteTargetClient.h - Orc Remote-target Client ----*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines the OrcRemoteTargetClient class and helpers. This class
+// can be used to communicate over an RPCChannel with an OrcRemoteTargetServer
+// instance to support remote-JITing.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETCLIENT_H
+#define LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETCLIENT_H
+
+#include "IndirectionUtils.h"
+#include "OrcRemoteTargetRPCAPI.h"
+#include <system_error>
+
+#define DEBUG_TYPE "orc-remote"
+
+namespace llvm {
+namespace orc {
+namespace remote {
+
+/// This class provides utilities (including memory manager, indirect stubs
+/// manager, and compile callback manager types) that support remote JITing
+/// in ORC.
+///
+/// Each of the utility classes talks to a JIT server (an instance of the
+/// OrcRemoteTargetServer class) via an RPC system (see RPCUtils.h) to carry out
+/// its actions.
+template <typename ChannelT>
+class OrcRemoteTargetClient : public OrcRemoteTargetRPCAPI {
+public:
+ /// Remote memory manager.
+ class RCMemoryManager : public RuntimeDyld::MemoryManager {
+ public:
+ RCMemoryManager(OrcRemoteTargetClient &Client, ResourceIdMgr::ResourceId Id)
+ : Client(Client), Id(Id) {
+ DEBUG(dbgs() << "Created remote allocator " << Id << "\n");
+ }
+
+ RCMemoryManager(RCMemoryManager &&Other)
+ : Client(std::move(Other.Client)), Id(std::move(Other.Id)),
+ Unmapped(std::move(Other.Unmapped)),
+ Unfinalized(std::move(Other.Unfinalized)) {}
+
+ RCMemoryManager operator=(RCMemoryManager &&Other) {
+ Client = std::move(Other.Client);
+ Id = std::move(Other.Id);
+ Unmapped = std::move(Other.Unmapped);
+ Unfinalized = std::move(Other.Unfinalized);
+ return *this;
+ }
+
+ ~RCMemoryManager() {
+ Client.destroyRemoteAllocator(Id);
+ DEBUG(dbgs() << "Destroyed remote allocator " << Id << "\n");
+ }
+
+ uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
+ unsigned SectionID,
+ StringRef SectionName) override {
+ Unmapped.back().CodeAllocs.emplace_back(Size, Alignment);
+ uint8_t *Alloc = reinterpret_cast<uint8_t *>(
+ Unmapped.back().CodeAllocs.back().getLocalAddress());
+ DEBUG(dbgs() << "Allocator " << Id << " allocated code for "
+ << SectionName << ": " << Alloc << " (" << Size
+ << " bytes, alignment " << Alignment << ")\n");
+ return Alloc;
+ }
+
+ uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
+ unsigned SectionID, StringRef SectionName,
+ bool IsReadOnly) override {
+ if (IsReadOnly) {
+ Unmapped.back().RODataAllocs.emplace_back(Size, Alignment);
+ uint8_t *Alloc = reinterpret_cast<uint8_t *>(
+ Unmapped.back().RODataAllocs.back().getLocalAddress());
+ DEBUG(dbgs() << "Allocator " << Id << " allocated ro-data for "
+ << SectionName << ": " << Alloc << " (" << Size
+ << " bytes, alignment " << Alignment << ")\n");
+ return Alloc;
+ } // else...
+
+ Unmapped.back().RWDataAllocs.emplace_back(Size, Alignment);
+ uint8_t *Alloc = reinterpret_cast<uint8_t *>(
+ Unmapped.back().RWDataAllocs.back().getLocalAddress());
+ DEBUG(dbgs() << "Allocator " << Id << " allocated rw-data for "
+ << SectionName << ": " << Alloc << " (" << Size
+ << " bytes, alignment " << Alignment << ")\n");
+ return Alloc;
+ }
+
+ void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign,
+ uintptr_t RODataSize, uint32_t RODataAlign,
+ uintptr_t RWDataSize,
+ uint32_t RWDataAlign) override {
+ Unmapped.push_back(ObjectAllocs());
+
+ DEBUG(dbgs() << "Allocator " << Id << " reserved:\n");
+
+ if (CodeSize != 0) {
+ std::error_code EC = Client.reserveMem(Unmapped.back().RemoteCodeAddr,
+ Id, CodeSize, CodeAlign);
+ // FIXME; Add error to poll.
+ assert(!EC && "Failed reserving remote memory.");
+ (void)EC;
+ DEBUG(dbgs() << " code: "
+ << format("0x%016x", Unmapped.back().RemoteCodeAddr)
+ << " (" << CodeSize << " bytes, alignment " << CodeAlign
+ << ")\n");
+ }
+
+ if (RODataSize != 0) {
+ std::error_code EC = Client.reserveMem(Unmapped.back().RemoteRODataAddr,
+ Id, RODataSize, RODataAlign);
+ // FIXME; Add error to poll.
+ assert(!EC && "Failed reserving remote memory.");
+ (void)EC;
+ DEBUG(dbgs() << " ro-data: "
+ << format("0x%016x", Unmapped.back().RemoteRODataAddr)
+ << " (" << RODataSize << " bytes, alignment "
+ << RODataAlign << ")\n");
+ }
+
+ if (RWDataSize != 0) {
+ std::error_code EC = Client.reserveMem(Unmapped.back().RemoteRWDataAddr,
+ Id, RWDataSize, RWDataAlign);
+ // FIXME; Add error to poll.
+ assert(!EC && "Failed reserving remote memory.");
+ (void)EC;
+ DEBUG(dbgs() << " rw-data: "
+ << format("0x%016x", Unmapped.back().RemoteRWDataAddr)
+ << " (" << RWDataSize << " bytes, alignment "
+ << RWDataAlign << ")\n");
+ }
+ }
+
+ bool needsToReserveAllocationSpace() override { return true; }
+
+ void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
+ size_t Size) override {}
+
+ void deregisterEHFrames(uint8_t *addr, uint64_t LoadAddr,
+ size_t Size) override {}
+
+ void notifyObjectLoaded(RuntimeDyld &Dyld,
+ const object::ObjectFile &Obj) override {
+ DEBUG(dbgs() << "Allocator " << Id << " applied mappings:\n");
+ for (auto &ObjAllocs : Unmapped) {
+ {
+ TargetAddress NextCodeAddr = ObjAllocs.RemoteCodeAddr;
+ for (auto &Alloc : ObjAllocs.CodeAllocs) {
+ NextCodeAddr = RoundUpToAlignment(NextCodeAddr, Alloc.getAlign());
+ Dyld.mapSectionAddress(Alloc.getLocalAddress(), NextCodeAddr);
+ DEBUG(dbgs() << " code: "
+ << static_cast<void *>(Alloc.getLocalAddress())
+ << " -> " << format("0x%016x", NextCodeAddr) << "\n");
+ Alloc.setRemoteAddress(NextCodeAddr);
+ NextCodeAddr += Alloc.getSize();
+ }
+ }
+ {
+ TargetAddress NextRODataAddr = ObjAllocs.RemoteRODataAddr;
+ for (auto &Alloc : ObjAllocs.RODataAllocs) {
+ NextRODataAddr =
+ RoundUpToAlignment(NextRODataAddr, Alloc.getAlign());
+ Dyld.mapSectionAddress(Alloc.getLocalAddress(), NextRODataAddr);
+ DEBUG(dbgs() << " ro-data: "
+ << static_cast<void *>(Alloc.getLocalAddress())
+ << " -> " << format("0x%016x", NextRODataAddr)
+ << "\n");
+ Alloc.setRemoteAddress(NextRODataAddr);
+ NextRODataAddr += Alloc.getSize();
+ }
+ }
+ {
+ TargetAddress NextRWDataAddr = ObjAllocs.RemoteRWDataAddr;
+ for (auto &Alloc : ObjAllocs.RWDataAllocs) {
+ NextRWDataAddr =
+ RoundUpToAlignment(NextRWDataAddr, Alloc.getAlign());
+ Dyld.mapSectionAddress(Alloc.getLocalAddress(), NextRWDataAddr);
+ DEBUG(dbgs() << " rw-data: "
+ << static_cast<void *>(Alloc.getLocalAddress())
+ << " -> " << format("0x%016x", NextRWDataAddr)
+ << "\n");
+ Alloc.setRemoteAddress(NextRWDataAddr);
+ NextRWDataAddr += Alloc.getSize();
+ }
+ }
+ Unfinalized.push_back(std::move(ObjAllocs));
+ }
+ Unmapped.clear();
+ }
+
+ bool finalizeMemory(std::string *ErrMsg = nullptr) override {
+ DEBUG(dbgs() << "Allocator " << Id << " finalizing:\n");
+
+ for (auto &ObjAllocs : Unfinalized) {
+
+ for (auto &Alloc : ObjAllocs.CodeAllocs) {
+ DEBUG(dbgs() << " copying code: "
+ << static_cast<void *>(Alloc.getLocalAddress()) << " -> "
+ << format("0x%016x", Alloc.getRemoteAddress()) << " ("
+ << Alloc.getSize() << " bytes)\n");
+ Client.writeMem(Alloc.getRemoteAddress(), Alloc.getLocalAddress(),
+ Alloc.getSize());
+ }
+
+ if (ObjAllocs.RemoteCodeAddr) {
+ DEBUG(dbgs() << " setting R-X permissions on code block: "
+ << format("0x%016x", ObjAllocs.RemoteCodeAddr) << "\n");
+ Client.setProtections(Id, ObjAllocs.RemoteCodeAddr,
+ sys::Memory::MF_READ | sys::Memory::MF_EXEC);
+ }
+
+ for (auto &Alloc : ObjAllocs.RODataAllocs) {
+ DEBUG(dbgs() << " copying ro-data: "
+ << static_cast<void *>(Alloc.getLocalAddress()) << " -> "
+ << format("0x%016x", Alloc.getRemoteAddress()) << " ("
+ << Alloc.getSize() << " bytes)\n");
+ Client.writeMem(Alloc.getRemoteAddress(), Alloc.getLocalAddress(),
+ Alloc.getSize());
+ }
+
+ if (ObjAllocs.RemoteRODataAddr) {
+ DEBUG(dbgs() << " setting R-- permissions on ro-data block: "
+ << format("0x%016x", ObjAllocs.RemoteRODataAddr)
+ << "\n");
+ Client.setProtections(Id, ObjAllocs.RemoteRODataAddr,
+ sys::Memory::MF_READ);
+ }
+
+ for (auto &Alloc : ObjAllocs.RWDataAllocs) {
+ DEBUG(dbgs() << " copying rw-data: "
+ << static_cast<void *>(Alloc.getLocalAddress()) << " -> "
+ << format("0x%016x", Alloc.getRemoteAddress()) << " ("
+ << Alloc.getSize() << " bytes)\n");
+ Client.writeMem(Alloc.getRemoteAddress(), Alloc.getLocalAddress(),
+ Alloc.getSize());
+ }
+
+ if (ObjAllocs.RemoteRWDataAddr) {
+ DEBUG(dbgs() << " setting RW- permissions on rw-data block: "
+ << format("0x%016x", ObjAllocs.RemoteRWDataAddr)
+ << "\n");
+ Client.setProtections(Id, ObjAllocs.RemoteRWDataAddr,
+ sys::Memory::MF_READ | sys::Memory::MF_WRITE);
+ }
+ }
+ Unfinalized.clear();
+
+ return false;
+ }
+
+ private:
+ class Alloc {
+ public:
+ Alloc(uint64_t Size, unsigned Align)
+ : Size(Size), Align(Align), Contents(new char[Size + Align - 1]),
+ RemoteAddr(0) {}
+
+ Alloc(Alloc &&Other)
+ : Size(std::move(Other.Size)), Align(std::move(Other.Align)),
+ Contents(std::move(Other.Contents)),
+ RemoteAddr(std::move(Other.RemoteAddr)) {}
+
+ Alloc &operator=(Alloc &&Other) {
+ Size = std::move(Other.Size);
+ Align = std::move(Other.Align);
+ Contents = std::move(Other.Contents);
+ RemoteAddr = std::move(Other.RemoteAddr);
+ return *this;
+ }
+
+ uint64_t getSize() const { return Size; }
+
+ unsigned getAlign() const { return Align; }
+
+ char *getLocalAddress() const {
+ uintptr_t LocalAddr = reinterpret_cast<uintptr_t>(Contents.get());
+ LocalAddr = RoundUpToAlignment(LocalAddr, Align);
+ return reinterpret_cast<char *>(LocalAddr);
+ }
+
+ void setRemoteAddress(TargetAddress RemoteAddr) {
+ this->RemoteAddr = RemoteAddr;
+ }
+
+ TargetAddress getRemoteAddress() const { return RemoteAddr; }
+
+ private:
+ uint64_t Size;
+ unsigned Align;
+ std::unique_ptr<char[]> Contents;
+ TargetAddress RemoteAddr;
+ };
+
+ struct ObjectAllocs {
+ ObjectAllocs()
+ : RemoteCodeAddr(0), RemoteRODataAddr(0), RemoteRWDataAddr(0) {}
+
+ ObjectAllocs(ObjectAllocs &&Other)
+ : RemoteCodeAddr(std::move(Other.RemoteCodeAddr)),
+ RemoteRODataAddr(std::move(Other.RemoteRODataAddr)),
+ RemoteRWDataAddr(std::move(Other.RemoteRWDataAddr)),
+ CodeAllocs(std::move(Other.CodeAllocs)),
+ RODataAllocs(std::move(Other.RODataAllocs)),
+ RWDataAllocs(std::move(Other.RWDataAllocs)) {}
+
+ ObjectAllocs &operator=(ObjectAllocs &&Other) {
+ RemoteCodeAddr = std::move(Other.RemoteCodeAddr);
+ RemoteRODataAddr = std::move(Other.RemoteRODataAddr);
+ RemoteRWDataAddr = std::move(Other.RemoteRWDataAddr);
+ CodeAllocs = std::move(Other.CodeAllocs);
+ RODataAllocs = std::move(Other.RODataAllocs);
+ RWDataAllocs = std::move(Other.RWDataAllocs);
+ return *this;
+ }
+
+ TargetAddress RemoteCodeAddr;
+ TargetAddress RemoteRODataAddr;
+ TargetAddress RemoteRWDataAddr;
+ std::vector<Alloc> CodeAllocs, RODataAllocs, RWDataAllocs;
+ };
+
+ OrcRemoteTargetClient &Client;
+ ResourceIdMgr::ResourceId Id;
+ std::vector<ObjectAllocs> Unmapped;
+ std::vector<ObjectAllocs> Unfinalized;
+ };
+
+ /// Remote indirect stubs manager.
+ class RCIndirectStubsManager : public IndirectStubsManager {
+ public:
+ RCIndirectStubsManager(OrcRemoteTargetClient &Remote,
+ ResourceIdMgr::ResourceId Id)
+ : Remote(Remote), Id(Id) {}
+
+ ~RCIndirectStubsManager() { Remote.destroyIndirectStubsManager(Id); }
+
+ std::error_code createStub(StringRef StubName, TargetAddress StubAddr,
+ JITSymbolFlags StubFlags) override {
+ if (auto EC = reserveStubs(1))
+ return EC;
+
+ return createStubInternal(StubName, StubAddr, StubFlags);
+ }
+
+ std::error_code createStubs(const StubInitsMap &StubInits) override {
+ if (auto EC = reserveStubs(StubInits.size()))
+ return EC;
+
+ for (auto &Entry : StubInits)
+ if (auto EC = createStubInternal(Entry.first(), Entry.second.first,
+ Entry.second.second))
+ return EC;
+
+ return std::error_code();
+ }
+
+ JITSymbol findStub(StringRef Name, bool ExportedStubsOnly) override {
+ auto I = StubIndexes.find(Name);
+ if (I == StubIndexes.end())
+ return nullptr;
+ auto Key = I->second.first;
+ auto Flags = I->second.second;
+ auto StubSymbol = JITSymbol(getStubAddr(Key), Flags);
+ if (ExportedStubsOnly && !StubSymbol.isExported())
+ return nullptr;
+ return StubSymbol;
+ }
+
+ JITSymbol findPointer(StringRef Name) override {
+ auto I = StubIndexes.find(Name);
+ if (I == StubIndexes.end())
+ return nullptr;
+ auto Key = I->second.first;
+ auto Flags = I->second.second;
+ return JITSymbol(getPtrAddr(Key), Flags);
+ }
+
+ std::error_code updatePointer(StringRef Name,
+ TargetAddress NewAddr) override {
+ auto I = StubIndexes.find(Name);
+ assert(I != StubIndexes.end() && "No stub pointer for symbol");
+ auto Key = I->second.first;
+ return Remote.writePointer(getPtrAddr(Key), NewAddr);
+ }
+
+ private:
+ struct RemoteIndirectStubsInfo {
+ RemoteIndirectStubsInfo(TargetAddress StubBase, TargetAddress PtrBase,
+ unsigned NumStubs)
+ : StubBase(StubBase), PtrBase(PtrBase), NumStubs(NumStubs) {}
+ TargetAddress StubBase;
+ TargetAddress PtrBase;
+ unsigned NumStubs;
+ };
+
+ OrcRemoteTargetClient &Remote;
+ ResourceIdMgr::ResourceId Id;
+ std::vector<RemoteIndirectStubsInfo> RemoteIndirectStubsInfos;
+ typedef std::pair<uint16_t, uint16_t> StubKey;
+ std::vector<StubKey> FreeStubs;
+ StringMap<std::pair<StubKey, JITSymbolFlags>> StubIndexes;
+
+ std::error_code reserveStubs(unsigned NumStubs) {
+ if (NumStubs <= FreeStubs.size())
+ return std::error_code();
+
+ unsigned NewStubsRequired = NumStubs - FreeStubs.size();
+ TargetAddress StubBase;
+ TargetAddress PtrBase;
+ unsigned NumStubsEmitted;
+
+ Remote.emitIndirectStubs(StubBase, PtrBase, NumStubsEmitted, Id,
+ NewStubsRequired);
+
+ unsigned NewBlockId = RemoteIndirectStubsInfos.size();
+ RemoteIndirectStubsInfos.push_back(
+ RemoteIndirectStubsInfo(StubBase, PtrBase, NumStubsEmitted));
+
+ for (unsigned I = 0; I < NumStubsEmitted; ++I)
+ FreeStubs.push_back(std::make_pair(NewBlockId, I));
+
+ return std::error_code();
+ }
+
+ std::error_code createStubInternal(StringRef StubName,
+ TargetAddress InitAddr,
+ JITSymbolFlags StubFlags) {
+ auto Key = FreeStubs.back();
+ FreeStubs.pop_back();
+ StubIndexes[StubName] = std::make_pair(Key, StubFlags);
+ return Remote.writePointer(getPtrAddr(Key), InitAddr);
+ }
+
+ TargetAddress getStubAddr(StubKey K) {
+ assert(RemoteIndirectStubsInfos[K.first].StubBase != 0 &&
+ "Missing stub address");
+ return RemoteIndirectStubsInfos[K.first].StubBase +
+ K.second * Remote.getIndirectStubSize();
+ }
+
+ TargetAddress getPtrAddr(StubKey K) {
+ assert(RemoteIndirectStubsInfos[K.first].PtrBase != 0 &&
+ "Missing pointer address");
+ return RemoteIndirectStubsInfos[K.first].PtrBase +
+ K.second * Remote.getPointerSize();
+ }
+ };
+
+ /// Remote compile callback manager.
+ class RCCompileCallbackManager : public JITCompileCallbackManager {
+ public:
+ RCCompileCallbackManager(TargetAddress ErrorHandlerAddress,
+ OrcRemoteTargetClient &Remote)
+ : JITCompileCallbackManager(ErrorHandlerAddress), Remote(Remote) {
+ assert(!Remote.CompileCallback && "Compile callback already set");
+ Remote.CompileCallback = [this](TargetAddress TrampolineAddr) {
+ return executeCompileCallback(TrampolineAddr);
+ };
+ Remote.emitResolverBlock();
+ }
+
+ private:
+ void grow() {
+ TargetAddress BlockAddr = 0;
+ uint32_t NumTrampolines = 0;
+ auto EC = Remote.emitTrampolineBlock(BlockAddr, NumTrampolines);
+ assert(!EC && "Failed to create trampolines");
+
+ uint32_t TrampolineSize = Remote.getTrampolineSize();
+ for (unsigned I = 0; I < NumTrampolines; ++I)
+ this->AvailableTrampolines.push_back(BlockAddr + (I * TrampolineSize));
+ }
+
+ OrcRemoteTargetClient &Remote;
+ };
+
+ /// Create an OrcRemoteTargetClient.
+ /// Channel is the ChannelT instance to communicate on. It is assumed that
+ /// the channel is ready to be read from and written to.
+ static ErrorOr<OrcRemoteTargetClient> Create(ChannelT &Channel) {
+ std::error_code EC;
+ OrcRemoteTargetClient H(Channel, EC);
+ if (EC)
+ return EC;
+ return H;
+ }
+
+ /// Call the int(void) function at the given address in the target and return
+ /// its result.
+ std::error_code callIntVoid(int &Result, TargetAddress Addr) {
+ DEBUG(dbgs() << "Calling int(*)(void) " << format("0x%016x", Addr) << "\n");
+
+ if (auto EC = call<CallIntVoid>(Channel, Addr))
+ return EC;
+
+ unsigned NextProcId;
+ if (auto EC = listenForCompileRequests(NextProcId))
+ return EC;
+
+ if (NextProcId != CallIntVoidResponseId)
+ return orcError(OrcErrorCode::UnexpectedRPCCall);
+
+ return handle<CallIntVoidResponse>(Channel, [&](int R) {
+ Result = R;
+ DEBUG(dbgs() << "Result: " << R << "\n");
+ return std::error_code();
+ });
+ }
+
+ /// Call the int(int, char*[]) function at the given address in the target and
+ /// return its result.
+ std::error_code callMain(int &Result, TargetAddress Addr,
+ const std::vector<std::string> &Args) {
+ DEBUG(dbgs() << "Calling int(*)(int, char*[]) " << format("0x%016x", Addr)
+ << "\n");
+
+ if (auto EC = call<CallMain>(Channel, Addr, Args))
+ return EC;
+
+ unsigned NextProcId;
+ if (auto EC = listenForCompileRequests(NextProcId))
+ return EC;
+
+ if (NextProcId != CallMainResponseId)
+ return orcError(OrcErrorCode::UnexpectedRPCCall);
+
+ return handle<CallMainResponse>(Channel, [&](int R) {
+ Result = R;
+ DEBUG(dbgs() << "Result: " << R << "\n");
+ return std::error_code();
+ });
+ }
+
+ /// Call the void() function at the given address in the target and wait for
+ /// it to finish.
+ std::error_code callVoidVoid(TargetAddress Addr) {
+ DEBUG(dbgs() << "Calling void(*)(void) " << format("0x%016x", Addr)
+ << "\n");
+
+ if (auto EC = call<CallVoidVoid>(Channel, Addr))
+ return EC;
+
+ unsigned NextProcId;
+ if (auto EC = listenForCompileRequests(NextProcId))
+ return EC;
+
+ if (NextProcId != CallVoidVoidResponseId)
+ return orcError(OrcErrorCode::UnexpectedRPCCall);
+
+ return handle<CallVoidVoidResponse>(Channel, doNothing);
+ }
+
+ /// Create an RCMemoryManager which will allocate its memory on the remote
+ /// target.
+ std::error_code
+ createRemoteMemoryManager(std::unique_ptr<RCMemoryManager> &MM) {
+ assert(!MM && "MemoryManager should be null before creation.");
+
+ auto Id = AllocatorIds.getNext();
+ if (auto EC = call<CreateRemoteAllocator>(Channel, Id))
+ return EC;
+ MM = llvm::make_unique<RCMemoryManager>(*this, Id);
+ return std::error_code();
+ }
+
+ /// Create an RCIndirectStubsManager that will allocate stubs on the remote
+ /// target.
+ std::error_code
+ createIndirectStubsManager(std::unique_ptr<RCIndirectStubsManager> &I) {
+ assert(!I && "Indirect stubs manager should be null before creation.");
+ auto Id = IndirectStubOwnerIds.getNext();
+ if (auto EC = call<CreateIndirectStubsOwner>(Channel, Id))
+ return EC;
+ I = llvm::make_unique<RCIndirectStubsManager>(*this, Id);
+ return std::error_code();
+ }
+
+ /// Search for symbols in the remote process. Note: This should be used by
+ /// symbol resolvers *after* they've searched the local symbol table in the
+ /// JIT stack.
+ std::error_code getSymbolAddress(TargetAddress &Addr, StringRef Name) {
+ // Check for an 'out-of-band' error, e.g. from an MM destructor.
+ if (ExistingError)
+ return ExistingError;
+
+ // Request remote symbol address.
+ if (auto EC = call<GetSymbolAddress>(Channel, Name))
+ return EC;
+
+ return expect<GetSymbolAddressResponse>(Channel, [&](TargetAddress &A) {
+ Addr = A;
+ DEBUG(dbgs() << "Remote address lookup " << Name << " = "
+ << format("0x%016x", Addr) << "\n");
+ return std::error_code();
+ });
+ }
+
+ /// Get the triple for the remote target.
+ const std::string &getTargetTriple() const { return RemoteTargetTriple; }
+
+ std::error_code terminateSession() { return call<TerminateSession>(Channel); }
+
+private:
+ OrcRemoteTargetClient(ChannelT &Channel, std::error_code &EC)
+ : Channel(Channel), RemotePointerSize(0), RemotePageSize(0),
+ RemoteTrampolineSize(0), RemoteIndirectStubSize(0) {
+ if ((EC = call<GetRemoteInfo>(Channel)))
+ return;
+
+ EC = expect<GetRemoteInfoResponse>(
+ Channel, readArgs(RemoteTargetTriple, RemotePointerSize, RemotePageSize,
+ RemoteTrampolineSize, RemoteIndirectStubSize));
+ }
+
+ void destroyRemoteAllocator(ResourceIdMgr::ResourceId Id) {
+ if (auto EC = call<DestroyRemoteAllocator>(Channel, Id)) {
+ // FIXME: This will be triggered by a removeModuleSet call: Propagate
+ // error return up through that.
+ llvm_unreachable("Failed to destroy remote allocator.");
+ AllocatorIds.release(Id);
+ }
+ }
+
+ std::error_code destroyIndirectStubsManager(ResourceIdMgr::ResourceId Id) {
+ IndirectStubOwnerIds.release(Id);
+ return call<DestroyIndirectStubsOwner>(Channel, Id);
+ }
+
+ std::error_code emitIndirectStubs(TargetAddress &StubBase,
+ TargetAddress &PtrBase,
+ uint32_t &NumStubsEmitted,
+ ResourceIdMgr::ResourceId Id,
+ uint32_t NumStubsRequired) {
+ if (auto EC = call<EmitIndirectStubs>(Channel, Id, NumStubsRequired))
+ return EC;
+
+ return expect<EmitIndirectStubsResponse>(
+ Channel, readArgs(StubBase, PtrBase, NumStubsEmitted));
+ }
+
+ std::error_code emitResolverBlock() {
+ // Check for an 'out-of-band' error, e.g. from an MM destructor.
+ if (ExistingError)
+ return ExistingError;
+
+ return call<EmitResolverBlock>(Channel);
+ }
+
+ std::error_code emitTrampolineBlock(TargetAddress &BlockAddr,
+ uint32_t &NumTrampolines) {
+ // Check for an 'out-of-band' error, e.g. from an MM destructor.
+ if (ExistingError)
+ return ExistingError;
+
+ if (auto EC = call<EmitTrampolineBlock>(Channel))
+ return EC;
+
+ return expect<EmitTrampolineBlockResponse>(
+ Channel, [&](TargetAddress BAddr, uint32_t NTrampolines) {
+ BlockAddr = BAddr;
+ NumTrampolines = NTrampolines;
+ return std::error_code();
+ });
+ }
+
+ uint32_t getIndirectStubSize() const { return RemoteIndirectStubSize; }
+ uint32_t getPageSize() const { return RemotePageSize; }
+ uint32_t getPointerSize() const { return RemotePointerSize; }
+
+ uint32_t getTrampolineSize() const { return RemoteTrampolineSize; }
+
+ std::error_code listenForCompileRequests(uint32_t &NextId) {
+ // Check for an 'out-of-band' error, e.g. from an MM destructor.
+ if (ExistingError)
+ return ExistingError;
+
+ if (auto EC = getNextProcId(Channel, NextId))
+ return EC;
+
+ while (NextId == RequestCompileId) {
+ TargetAddress TrampolineAddr = 0;
+ if (auto EC = handle<RequestCompile>(Channel, readArgs(TrampolineAddr)))
+ return EC;
+
+ TargetAddress ImplAddr = CompileCallback(TrampolineAddr);
+ if (auto EC = call<RequestCompileResponse>(Channel, ImplAddr))
+ return EC;
+
+ if (auto EC = getNextProcId(Channel, NextId))
+ return EC;
+ }
+
+ return std::error_code();
+ }
+
+ std::error_code readMem(char *Dst, TargetAddress Src, uint64_t Size) {
+ // Check for an 'out-of-band' error, e.g. from an MM destructor.
+ if (ExistingError)
+ return ExistingError;
+
+ if (auto EC = call<ReadMem>(Channel, Src, Size))
+ return EC;
+
+ if (auto EC = expect<ReadMemResponse>(
+ Channel, [&]() { return Channel.readBytes(Dst, Size); }))
+ return EC;
+
+ return std::error_code();
+ }
+
+ std::error_code reserveMem(TargetAddress &RemoteAddr,
+ ResourceIdMgr::ResourceId Id, uint64_t Size,
+ uint32_t Align) {
+
+ // Check for an 'out-of-band' error, e.g. from an MM destructor.
+ if (ExistingError)
+ return ExistingError;
+
+ if (std::error_code EC = call<ReserveMem>(Channel, Id, Size, Align))
+ return EC;
+
+ return expect<ReserveMemResponse>(Channel, readArgs(RemoteAddr));
+ }
+
+ std::error_code setProtections(ResourceIdMgr::ResourceId Id,
+ TargetAddress RemoteSegAddr,
+ unsigned ProtFlags) {
+ return call<SetProtections>(Channel, Id, RemoteSegAddr, ProtFlags);
+ }
+
+ std::error_code writeMem(TargetAddress Addr, const char *Src, uint64_t Size) {
+ // Check for an 'out-of-band' error, e.g. from an MM destructor.
+ if (ExistingError)
+ return ExistingError;
+
+ // Make the send call.
+ if (auto EC = call<WriteMem>(Channel, Addr, Size))
+ return EC;
+
+ // Follow this up with the section contents.
+ if (auto EC = Channel.appendBytes(Src, Size))
+ return EC;
+
+ return Channel.send();
+ }
+
+ std::error_code writePointer(TargetAddress Addr, TargetAddress PtrVal) {
+ // Check for an 'out-of-band' error, e.g. from an MM destructor.
+ if (ExistingError)
+ return ExistingError;
+
+ return call<WritePtr>(Channel, Addr, PtrVal);
+ }
+
+ static std::error_code doNothing() { return std::error_code(); }
+
+ ChannelT &Channel;
+ std::error_code ExistingError;
+ std::string RemoteTargetTriple;
+ uint32_t RemotePointerSize;
+ uint32_t RemotePageSize;
+ uint32_t RemoteTrampolineSize;
+ uint32_t RemoteIndirectStubSize;
+ ResourceIdMgr AllocatorIds, IndirectStubOwnerIds;
+ std::function<TargetAddress(TargetAddress)> CompileCallback;
+};
+
+} // end namespace remote
+} // end namespace orc
+} // end namespace llvm
+
+#undef DEBUG_TYPE
+
+#endif
diff --git a/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h b/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h
new file mode 100644
index 0000000..96dc242
--- /dev/null
+++ b/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetRPCAPI.h
@@ -0,0 +1,185 @@
+//===--- OrcRemoteTargetRPCAPI.h - Orc Remote-target RPC API ----*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines the Orc remote-target RPC API. It should not be used
+// directly, but is used by the RemoteTargetClient and RemoteTargetServer
+// classes.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETRPCAPI_H
+#define LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETRPCAPI_H
+
+#include "JITSymbol.h"
+#include "RPCChannel.h"
+#include "RPCUtils.h"
+
+namespace llvm {
+namespace orc {
+namespace remote {
+
+class OrcRemoteTargetRPCAPI : public RPC<RPCChannel> {
+protected:
+ class ResourceIdMgr {
+ public:
+ typedef uint64_t ResourceId;
+ ResourceIdMgr() : NextId(0) {}
+ ResourceId getNext() {
+ if (!FreeIds.empty()) {
+ ResourceId I = FreeIds.back();
+ FreeIds.pop_back();
+ return I;
+ }
+ return NextId++;
+ }
+ void release(ResourceId I) { FreeIds.push_back(I); }
+
+ private:
+ ResourceId NextId;
+ std::vector<ResourceId> FreeIds;
+ };
+
+public:
+ enum JITProcId : uint32_t {
+ InvalidId = 0,
+ CallIntVoidId,
+ CallIntVoidResponseId,
+ CallMainId,
+ CallMainResponseId,
+ CallVoidVoidId,
+ CallVoidVoidResponseId,
+ CreateRemoteAllocatorId,
+ CreateIndirectStubsOwnerId,
+ DestroyRemoteAllocatorId,
+ DestroyIndirectStubsOwnerId,
+ EmitIndirectStubsId,
+ EmitIndirectStubsResponseId,
+ EmitResolverBlockId,
+ EmitTrampolineBlockId,
+ EmitTrampolineBlockResponseId,
+ GetSymbolAddressId,
+ GetSymbolAddressResponseId,
+ GetRemoteInfoId,
+ GetRemoteInfoResponseId,
+ ReadMemId,
+ ReadMemResponseId,
+ ReserveMemId,
+ ReserveMemResponseId,
+ RequestCompileId,
+ RequestCompileResponseId,
+ SetProtectionsId,
+ TerminateSessionId,
+ WriteMemId,
+ WritePtrId
+ };
+
+ static const char *getJITProcIdName(JITProcId Id);
+
+ typedef Procedure<CallIntVoidId, TargetAddress /* FnAddr */> CallIntVoid;
+
+ typedef Procedure<CallIntVoidResponseId, int /* Result */>
+ CallIntVoidResponse;
+
+ typedef Procedure<CallMainId, TargetAddress /* FnAddr */,
+ std::vector<std::string> /* Args */>
+ CallMain;
+
+ typedef Procedure<CallMainResponseId, int /* Result */> CallMainResponse;
+
+ typedef Procedure<CallVoidVoidId, TargetAddress /* FnAddr */> CallVoidVoid;
+
+ typedef Procedure<CallVoidVoidResponseId> CallVoidVoidResponse;
+
+ typedef Procedure<CreateRemoteAllocatorId,
+ ResourceIdMgr::ResourceId /* Allocator ID */>
+ CreateRemoteAllocator;
+
+ typedef Procedure<CreateIndirectStubsOwnerId,
+ ResourceIdMgr::ResourceId /* StubsOwner ID */>
+ CreateIndirectStubsOwner;
+
+ typedef Procedure<DestroyRemoteAllocatorId,
+ ResourceIdMgr::ResourceId /* Allocator ID */>
+ DestroyRemoteAllocator;
+
+ typedef Procedure<DestroyIndirectStubsOwnerId,
+ ResourceIdMgr::ResourceId /* StubsOwner ID */>
+ DestroyIndirectStubsOwner;
+
+ typedef Procedure<EmitIndirectStubsId,
+ ResourceIdMgr::ResourceId /* StubsOwner ID */,
+ uint32_t /* NumStubsRequired */>
+ EmitIndirectStubs;
+
+ typedef Procedure<
+ EmitIndirectStubsResponseId, TargetAddress /* StubsBaseAddr */,
+ TargetAddress /* PtrsBaseAddr */, uint32_t /* NumStubsEmitted */>
+ EmitIndirectStubsResponse;
+
+ typedef Procedure<EmitResolverBlockId> EmitResolverBlock;
+
+ typedef Procedure<EmitTrampolineBlockId> EmitTrampolineBlock;
+
+ typedef Procedure<EmitTrampolineBlockResponseId,
+ TargetAddress /* BlockAddr */,
+ uint32_t /* NumTrampolines */>
+ EmitTrampolineBlockResponse;
+
+ typedef Procedure<GetSymbolAddressId, std::string /*SymbolName*/>
+ GetSymbolAddress;
+
+ typedef Procedure<GetSymbolAddressResponseId, uint64_t /* SymbolAddr */>
+ GetSymbolAddressResponse;
+
+ typedef Procedure<GetRemoteInfoId> GetRemoteInfo;
+
+ typedef Procedure<GetRemoteInfoResponseId, std::string /* Triple */,
+ uint32_t /* PointerSize */, uint32_t /* PageSize */,
+ uint32_t /* TrampolineSize */,
+ uint32_t /* IndirectStubSize */>
+ GetRemoteInfoResponse;
+
+ typedef Procedure<ReadMemId, TargetAddress /* Src */, uint64_t /* Size */>
+ ReadMem;
+
+ typedef Procedure<ReadMemResponseId> ReadMemResponse;
+
+ typedef Procedure<ReserveMemId, ResourceIdMgr::ResourceId /* Id */,
+ uint64_t /* Size */, uint32_t /* Align */>
+ ReserveMem;
+
+ typedef Procedure<ReserveMemResponseId, TargetAddress /* Addr */>
+ ReserveMemResponse;
+
+ typedef Procedure<RequestCompileId, TargetAddress /* TrampolineAddr */>
+ RequestCompile;
+
+ typedef Procedure<RequestCompileResponseId, TargetAddress /* ImplAddr */>
+ RequestCompileResponse;
+
+ typedef Procedure<SetProtectionsId, ResourceIdMgr::ResourceId /* Id */,
+ TargetAddress /* Dst */, uint32_t /* ProtFlags */>
+ SetProtections;
+
+ typedef Procedure<TerminateSessionId> TerminateSession;
+
+ typedef Procedure<WriteMemId, TargetAddress /* Dst */, uint64_t /* Size */
+ /* Data should follow */>
+ WriteMem;
+
+ typedef Procedure<WritePtrId, TargetAddress /* Dst */,
+ TargetAddress /* Val */>
+ WritePtr;
+};
+
+} // end namespace remote
+} // end namespace orc
+} // end namespace llvm
+
+#endif
diff --git a/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetServer.h b/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetServer.h
new file mode 100644
index 0000000..5247661
--- /dev/null
+++ b/include/llvm/ExecutionEngine/Orc/OrcRemoteTargetServer.h
@@ -0,0 +1,432 @@
+//===---- OrcRemoteTargetServer.h - Orc Remote-target Server ----*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines the OrcRemoteTargetServer class. It can be used to build a
+// JIT server that can execute code sent from an OrcRemoteTargetClient.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETSERVER_H
+#define LLVM_EXECUTIONENGINE_ORC_ORCREMOTETARGETSERVER_H
+
+#include "OrcRemoteTargetRPCAPI.h"
+#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
+#include "llvm/Support/Debug.h"
+#include "llvm/Support/Format.h"
+#include "llvm/Support/Process.h"
+#include "llvm/Support/raw_ostream.h"
+#include <map>
+
+#define DEBUG_TYPE "orc-remote"
+
+namespace llvm {
+namespace orc {
+namespace remote {
+
+template <typename ChannelT, typename TargetT>
+class OrcRemoteTargetServer : public OrcRemoteTargetRPCAPI {
+public:
+ typedef std::function<TargetAddress(const std::string &Name)>
+ SymbolLookupFtor;
+
+ OrcRemoteTargetServer(ChannelT &Channel, SymbolLookupFtor SymbolLookup)
+ : Channel(Channel), SymbolLookup(std::move(SymbolLookup)) {}
+
+ std::error_code getNextProcId(JITProcId &Id) {
+ return deserialize(Channel, Id);
+ }
+
+ std::error_code handleKnownProcedure(JITProcId Id) {
+ typedef OrcRemoteTargetServer ThisT;
+
+ DEBUG(dbgs() << "Handling known proc: " << getJITProcIdName(Id) << "\n");
+
+ switch (Id) {
+ case CallIntVoidId:
+ return handle<CallIntVoid>(Channel, *this, &ThisT::handleCallIntVoid);
+ case CallMainId:
+ return handle<CallMain>(Channel, *this, &ThisT::handleCallMain);
+ case CallVoidVoidId:
+ return handle<CallVoidVoid>(Channel, *this, &ThisT::handleCallVoidVoid);
+ case CreateRemoteAllocatorId:
+ return handle<CreateRemoteAllocator>(Channel, *this,
+ &ThisT::handleCreateRemoteAllocator);
+ case CreateIndirectStubsOwnerId:
+ return handle<CreateIndirectStubsOwner>(
+ Channel, *this, &ThisT::handleCreateIndirectStubsOwner);
+ case DestroyRemoteAllocatorId:
+ return handle<DestroyRemoteAllocator>(
+ Channel, *this, &ThisT::handleDestroyRemoteAllocator);
+ case DestroyIndirectStubsOwnerId:
+ return handle<DestroyIndirectStubsOwner>(
+ Channel, *this, &ThisT::handleDestroyIndirectStubsOwner);
+ case EmitIndirectStubsId:
+ return handle<EmitIndirectStubs>(Channel, *this,
+ &ThisT::handleEmitIndirectStubs);
+ case EmitResolverBlockId:
+ return handle<EmitResolverBlock>(Channel, *this,
+ &ThisT::handleEmitResolverBlock);
+ case EmitTrampolineBlockId:
+ return handle<EmitTrampolineBlock>(Channel, *this,
+ &ThisT::handleEmitTrampolineBlock);
+ case GetSymbolAddressId:
+ return handle<GetSymbolAddress>(Channel, *this,
+ &ThisT::handleGetSymbolAddress);
+ case GetRemoteInfoId:
+ return handle<GetRemoteInfo>(Channel, *this, &ThisT::handleGetRemoteInfo);
+ case ReadMemId:
+ return handle<ReadMem>(Channel, *this, &ThisT::handleReadMem);
+ case ReserveMemId:
+ return handle<ReserveMem>(Channel, *this, &ThisT::handleReserveMem);
+ case SetProtectionsId:
+ return handle<SetProtections>(Channel, *this,
+ &ThisT::handleSetProtections);
+ case WriteMemId:
+ return handle<WriteMem>(Channel, *this, &ThisT::handleWriteMem);
+ case WritePtrId:
+ return handle<WritePtr>(Channel, *this, &ThisT::handleWritePtr);
+ default:
+ return orcError(OrcErrorCode::UnexpectedRPCCall);
+ }
+
+ llvm_unreachable("Unhandled JIT RPC procedure Id.");
+ }
+
+ std::error_code requestCompile(TargetAddress &CompiledFnAddr,
+ TargetAddress TrampolineAddr) {
+ if (auto EC = call<RequestCompile>(Channel, TrampolineAddr))
+ return EC;
+
+ while (1) {
+ JITProcId Id = InvalidId;
+ if (auto EC = getNextProcId(Id))
+ return EC;
+
+ switch (Id) {
+ case RequestCompileResponseId:
+ return handle<RequestCompileResponse>(Channel,
+ readArgs(CompiledFnAddr));
+ default:
+ if (auto EC = handleKnownProcedure(Id))
+ return EC;
+ }
+ }
+
+ llvm_unreachable("Fell through request-compile command loop.");
+ }
+
+private:
+ struct Allocator {
+ Allocator() = default;
+ Allocator(Allocator &&Other) : Allocs(std::move(Other.Allocs)) {}
+ Allocator &operator=(Allocator &&Other) {
+ Allocs = std::move(Other.Allocs);
+ return *this;
+ }
+
+ ~Allocator() {
+ for (auto &Alloc : Allocs)
+ sys::Memory::releaseMappedMemory(Alloc.second);
+ }
+
+ std::error_code allocate(void *&Addr, size_t Size, uint32_t Align) {
+ std::error_code EC;
+ sys::MemoryBlock MB = sys::Memory::allocateMappedMemory(
+ Size, nullptr, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
+ if (EC)
+ return EC;
+
+ Addr = MB.base();
+ assert(Allocs.find(MB.base()) == Allocs.end() && "Duplicate alloc");
+ Allocs[MB.base()] = std::move(MB);
+ return std::error_code();
+ }
+
+ std::error_code setProtections(void *block, unsigned Flags) {
+ auto I = Allocs.find(block);
+ if (I == Allocs.end())
+ return orcError(OrcErrorCode::RemoteMProtectAddrUnrecognized);
+ return sys::Memory::protectMappedMemory(I->second, Flags);
+ }
+
+ private:
+ std::map<void *, sys::MemoryBlock> Allocs;
+ };
+
+ static std::error_code doNothing() { return std::error_code(); }
+
+ static TargetAddress reenter(void *JITTargetAddr, void *TrampolineAddr) {
+ TargetAddress CompiledFnAddr = 0;
+
+ auto T = static_cast<OrcRemoteTargetServer *>(JITTargetAddr);
+ auto EC = T->requestCompile(
+ CompiledFnAddr, static_cast<TargetAddress>(
+ reinterpret_cast<uintptr_t>(TrampolineAddr)));
+ assert(!EC && "Compile request failed");
+ (void)EC;
+ return CompiledFnAddr;
+ }
+
+ std::error_code handleCallIntVoid(TargetAddress Addr) {
+ typedef int (*IntVoidFnTy)();
+ IntVoidFnTy Fn =
+ reinterpret_cast<IntVoidFnTy>(static_cast<uintptr_t>(Addr));
+
+ DEBUG(dbgs() << " Calling "
+ << reinterpret_cast<void *>(reinterpret_cast<intptr_t>(Fn))
+ << "\n");
+ int Result = Fn();
+ DEBUG(dbgs() << " Result = " << Result << "\n");
+
+ return call<CallIntVoidResponse>(Channel, Result);
+ }
+
+ std::error_code handleCallMain(TargetAddress Addr,
+ std::vector<std::string> Args) {
+ typedef int (*MainFnTy)(int, const char *[]);
+
+ MainFnTy Fn = reinterpret_cast<MainFnTy>(static_cast<uintptr_t>(Addr));
+ int ArgC = Args.size() + 1;
+ int Idx = 1;
+ std::unique_ptr<const char *[]> ArgV(new const char *[ArgC + 1]);
+ ArgV[0] = "<jit process>";
+ for (auto &Arg : Args)
+ ArgV[Idx++] = Arg.c_str();
+
+ DEBUG(dbgs() << " Calling " << reinterpret_cast<void *>(Fn) << "\n");
+ int Result = Fn(ArgC, ArgV.get());
+ DEBUG(dbgs() << " Result = " << Result << "\n");
+
+ return call<CallMainResponse>(Channel, Result);
+ }
+
+ std::error_code handleCallVoidVoid(TargetAddress Addr) {
+ typedef void (*VoidVoidFnTy)();
+ VoidVoidFnTy Fn =
+ reinterpret_cast<VoidVoidFnTy>(static_cast<uintptr_t>(Addr));
+
+ DEBUG(dbgs() << " Calling " << reinterpret_cast<void *>(Fn) << "\n");
+ Fn();
+ DEBUG(dbgs() << " Complete.\n");
+
+ return call<CallVoidVoidResponse>(Channel);
+ }
+
+ std::error_code handleCreateRemoteAllocator(ResourceIdMgr::ResourceId Id) {
+ auto I = Allocators.find(Id);
+ if (I != Allocators.end())
+ return orcError(OrcErrorCode::RemoteAllocatorIdAlreadyInUse);
+ DEBUG(dbgs() << " Created allocator " << Id << "\n");
+ Allocators[Id] = Allocator();
+ return std::error_code();
+ }
+
+ std::error_code handleCreateIndirectStubsOwner(ResourceIdMgr::ResourceId Id) {
+ auto I = IndirectStubsOwners.find(Id);
+ if (I != IndirectStubsOwners.end())
+ return orcError(OrcErrorCode::RemoteIndirectStubsOwnerIdAlreadyInUse);
+ DEBUG(dbgs() << " Create indirect stubs owner " << Id << "\n");
+ IndirectStubsOwners[Id] = ISBlockOwnerList();
+ return std::error_code();
+ }
+
+ std::error_code handleDestroyRemoteAllocator(ResourceIdMgr::ResourceId Id) {
+ auto I = Allocators.find(Id);
+ if (I == Allocators.end())
+ return orcError(OrcErrorCode::RemoteAllocatorDoesNotExist);
+ Allocators.erase(I);
+ DEBUG(dbgs() << " Destroyed allocator " << Id << "\n");
+ return std::error_code();
+ }
+
+ std::error_code
+ handleDestroyIndirectStubsOwner(ResourceIdMgr::ResourceId Id) {
+ auto I = IndirectStubsOwners.find(Id);
+ if (I == IndirectStubsOwners.end())
+ return orcError(OrcErrorCode::RemoteIndirectStubsOwnerDoesNotExist);
+ IndirectStubsOwners.erase(I);
+ return std::error_code();
+ }
+
+ std::error_code handleEmitIndirectStubs(ResourceIdMgr::ResourceId Id,
+ uint32_t NumStubsRequired) {
+ DEBUG(dbgs() << " ISMgr " << Id << " request " << NumStubsRequired
+ << " stubs.\n");
+
+ auto StubOwnerItr = IndirectStubsOwners.find(Id);
+ if (StubOwnerItr == IndirectStubsOwners.end())
+ return orcError(OrcErrorCode::RemoteIndirectStubsOwnerDoesNotExist);
+
+ typename TargetT::IndirectStubsInfo IS;
+ if (auto EC =
+ TargetT::emitIndirectStubsBlock(IS, NumStubsRequired, nullptr))
+ return EC;
+
+ TargetAddress StubsBase =
+ static_cast<TargetAddress>(reinterpret_cast<uintptr_t>(IS.getStub(0)));
+ TargetAddress PtrsBase =
+ static_cast<TargetAddress>(reinterpret_cast<uintptr_t>(IS.getPtr(0)));
+ uint32_t NumStubsEmitted = IS.getNumStubs();
+
+ auto &BlockList = StubOwnerItr->second;
+ BlockList.push_back(std::move(IS));
+
+ return call<EmitIndirectStubsResponse>(Channel, StubsBase, PtrsBase,
+ NumStubsEmitted);
+ }
+
+ std::error_code handleEmitResolverBlock() {
+ std::error_code EC;
+ ResolverBlock = sys::OwningMemoryBlock(sys::Memory::allocateMappedMemory(
+ TargetT::ResolverCodeSize, nullptr,
+ sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC));
+ if (EC)
+ return EC;
+
+ TargetT::writeResolverCode(static_cast<uint8_t *>(ResolverBlock.base()),
+ &reenter, this);
+
+ return sys::Memory::protectMappedMemory(ResolverBlock.getMemoryBlock(),
+ sys::Memory::MF_READ |
+ sys::Memory::MF_EXEC);
+ }
+
+ std::error_code handleEmitTrampolineBlock() {
+ std::error_code EC;
+ auto TrampolineBlock =
+ sys::OwningMemoryBlock(sys::Memory::allocateMappedMemory(
+ sys::Process::getPageSize(), nullptr,
+ sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC));
+ if (EC)
+ return EC;
+
+ unsigned NumTrampolines =
+ (sys::Process::getPageSize() - TargetT::PointerSize) /
+ TargetT::TrampolineSize;
+
+ uint8_t *TrampolineMem = static_cast<uint8_t *>(TrampolineBlock.base());
+ TargetT::writeTrampolines(TrampolineMem, ResolverBlock.base(),
+ NumTrampolines);
+
+ EC = sys::Memory::protectMappedMemory(TrampolineBlock.getMemoryBlock(),
+ sys::Memory::MF_READ |
+ sys::Memory::MF_EXEC);
+
+ TrampolineBlocks.push_back(std::move(TrampolineBlock));
+
+ return call<EmitTrampolineBlockResponse>(
+ Channel,
+ static_cast<TargetAddress>(reinterpret_cast<uintptr_t>(TrampolineMem)),
+ NumTrampolines);
+ }
+
+ std::error_code handleGetSymbolAddress(const std::string &Name) {
+ TargetAddress Addr = SymbolLookup(Name);
+ DEBUG(dbgs() << " Symbol '" << Name << "' = " << format("0x%016x", Addr)
+ << "\n");
+ return call<GetSymbolAddressResponse>(Channel, Addr);
+ }
+
+ std::error_code handleGetRemoteInfo() {
+ std::string ProcessTriple = sys::getProcessTriple();
+ uint32_t PointerSize = TargetT::PointerSize;
+ uint32_t PageSize = sys::Process::getPageSize();
+ uint32_t TrampolineSize = TargetT::TrampolineSize;
+ uint32_t IndirectStubSize = TargetT::IndirectStubsInfo::StubSize;
+ DEBUG(dbgs() << " Remote info:\n"
+ << " triple = '" << ProcessTriple << "'\n"
+ << " pointer size = " << PointerSize << "\n"
+ << " page size = " << PageSize << "\n"
+ << " trampoline size = " << TrampolineSize << "\n"
+ << " indirect stub size = " << IndirectStubSize << "\n");
+ return call<GetRemoteInfoResponse>(Channel, ProcessTriple, PointerSize,
+ PageSize, TrampolineSize,
+ IndirectStubSize);
+ }
+
+ std::error_code handleReadMem(TargetAddress RSrc, uint64_t Size) {
+ char *Src = reinterpret_cast<char *>(static_cast<uintptr_t>(RSrc));
+
+ DEBUG(dbgs() << " Reading " << Size << " bytes from "
+ << static_cast<void *>(Src) << "\n");
+
+ if (auto EC = call<ReadMemResponse>(Channel))
+ return EC;
+
+ if (auto EC = Channel.appendBytes(Src, Size))
+ return EC;
+
+ return Channel.send();
+ }
+
+ std::error_code handleReserveMem(ResourceIdMgr::ResourceId Id, uint64_t Size,
+ uint32_t Align) {
+ auto I = Allocators.find(Id);
+ if (I == Allocators.end())
+ return orcError(OrcErrorCode::RemoteAllocatorDoesNotExist);
+ auto &Allocator = I->second;
+ void *LocalAllocAddr = nullptr;
+ if (auto EC = Allocator.allocate(LocalAllocAddr, Size, Align))
+ return EC;
+
+ DEBUG(dbgs() << " Allocator " << Id << " reserved " << LocalAllocAddr
+ << " (" << Size << " bytes, alignment " << Align << ")\n");
+
+ TargetAddress AllocAddr =
+ static_cast<TargetAddress>(reinterpret_cast<uintptr_t>(LocalAllocAddr));
+
+ return call<ReserveMemResponse>(Channel, AllocAddr);
+ }
+
+ std::error_code handleSetProtections(ResourceIdMgr::ResourceId Id,
+ TargetAddress Addr, uint32_t Flags) {
+ auto I = Allocators.find(Id);
+ if (I == Allocators.end())
+ return orcError(OrcErrorCode::RemoteAllocatorDoesNotExist);
+ auto &Allocator = I->second;
+ void *LocalAddr = reinterpret_cast<void *>(static_cast<uintptr_t>(Addr));
+ DEBUG(dbgs() << " Allocator " << Id << " set permissions on " << LocalAddr
+ << " to " << (Flags & sys::Memory::MF_READ ? 'R' : '-')
+ << (Flags & sys::Memory::MF_WRITE ? 'W' : '-')
+ << (Flags & sys::Memory::MF_EXEC ? 'X' : '-') << "\n");
+ return Allocator.setProtections(LocalAddr, Flags);
+ }
+
+ std::error_code handleWriteMem(TargetAddress RDst, uint64_t Size) {
+ char *Dst = reinterpret_cast<char *>(static_cast<uintptr_t>(RDst));
+ DEBUG(dbgs() << " Writing " << Size << " bytes to "
+ << format("0x%016x", RDst) << "\n");
+ return Channel.readBytes(Dst, Size);
+ }
+
+ std::error_code handleWritePtr(TargetAddress Addr, TargetAddress PtrVal) {
+ DEBUG(dbgs() << " Writing pointer *" << format("0x%016x", Addr) << " = "
+ << format("0x%016x", PtrVal) << "\n");
+ uintptr_t *Ptr =
+ reinterpret_cast<uintptr_t *>(static_cast<uintptr_t>(Addr));
+ *Ptr = static_cast<uintptr_t>(PtrVal);
+ return std::error_code();
+ }
+
+ ChannelT &Channel;
+ SymbolLookupFtor SymbolLookup;
+ std::map<ResourceIdMgr::ResourceId, Allocator> Allocators;
+ typedef std::vector<typename TargetT::IndirectStubsInfo> ISBlockOwnerList;
+ std::map<ResourceIdMgr::ResourceId, ISBlockOwnerList> IndirectStubsOwners;
+ sys::OwningMemoryBlock ResolverBlock;
+ std::vector<sys::OwningMemoryBlock> TrampolineBlocks;
+};
+
+} // end namespace remote
+} // end namespace orc
+} // end namespace llvm
+
+#undef DEBUG_TYPE
+
+#endif
diff --git a/include/llvm/ExecutionEngine/Orc/RPCChannel.h b/include/llvm/ExecutionEngine/Orc/RPCChannel.h
new file mode 100644
index 0000000..b97b6da
--- /dev/null
+++ b/include/llvm/ExecutionEngine/Orc/RPCChannel.h
@@ -0,0 +1,179 @@
+// -*- c++ -*-
+
+#ifndef LLVM_EXECUTIONENGINE_ORC_RPCCHANNEL_H
+#define LLVM_EXECUTIONENGINE_ORC_RPCCHANNEL_H
+
+#include "OrcError.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/Support/Endian.h"
+
+#include <system_error>
+
+namespace llvm {
+namespace orc {
+namespace remote {
+
+/// Interface for byte-streams to be used with RPC.
+class RPCChannel {
+public:
+ virtual ~RPCChannel() {}
+
+ /// Read Size bytes from the stream into *Dst.
+ virtual std::error_code readBytes(char *Dst, unsigned Size) = 0;
+
+ /// Read size bytes from *Src and append them to the stream.
+ virtual std::error_code appendBytes(const char *Src, unsigned Size) = 0;
+
+ /// Flush the stream if possible.
+ virtual std::error_code send() = 0;
+};
+
+/// RPC channel serialization for a variadic list of arguments.
+template <typename T, typename... Ts>
+std::error_code serialize_seq(RPCChannel &C, const T &Arg, const Ts &... Args) {
+ if (auto EC = serialize(C, Arg))
+ return EC;
+ return serialize_seq(C, Args...);
+}
+
+/// RPC channel serialization for an (empty) variadic list of arguments.
+inline std::error_code serialize_seq(RPCChannel &C) {
+ return std::error_code();
+}
+
+/// RPC channel deserialization for a variadic list of arguments.
+template <typename T, typename... Ts>
+std::error_code deserialize_seq(RPCChannel &C, T &Arg, Ts &... Args) {
+ if (auto EC = deserialize(C, Arg))
+ return EC;
+ return deserialize_seq(C, Args...);
+}
+
+/// RPC channel serialization for an (empty) variadic list of arguments.
+inline std::error_code deserialize_seq(RPCChannel &C) {
+ return std::error_code();
+}
+
+/// RPC channel serialization for integer primitives.
+template <typename T>
+typename std::enable_if<
+ std::is_same<T, uint64_t>::value || std::is_same<T, int64_t>::value ||
+ std::is_same<T, uint32_t>::value || std::is_same<T, int32_t>::value ||
+ std::is_same<T, uint16_t>::value || std::is_same<T, int16_t>::value ||
+ std::is_same<T, uint8_t>::value || std::is_same<T, int8_t>::value,
+ std::error_code>::type
+serialize(RPCChannel &C, T V) {
+ support::endian::byte_swap<T, support::big>(V);
+ return C.appendBytes(reinterpret_cast<const char *>(&V), sizeof(T));
+}
+
+/// RPC channel deserialization for integer primitives.
+template <typename T>
+typename std::enable_if<
+ std::is_same<T, uint64_t>::value || std::is_same<T, int64_t>::value ||
+ std::is_same<T, uint32_t>::value || std::is_same<T, int32_t>::value ||
+ std::is_same<T, uint16_t>::value || std::is_same<T, int16_t>::value ||
+ std::is_same<T, uint8_t>::value || std::is_same<T, int8_t>::value,
+ std::error_code>::type
+deserialize(RPCChannel &C, T &V) {
+ if (auto EC = C.readBytes(reinterpret_cast<char *>(&V), sizeof(T)))
+ return EC;
+ support::endian::byte_swap<T, support::big>(V);
+ return std::error_code();
+}
+
+/// RPC channel serialization for enums.
+template <typename T>
+typename std::enable_if<std::is_enum<T>::value, std::error_code>::type
+serialize(RPCChannel &C, T V) {
+ return serialize(C, static_cast<typename std::underlying_type<T>::type>(V));
+}
+
+/// RPC channel deserialization for enums.
+template <typename T>
+typename std::enable_if<std::is_enum<T>::value, std::error_code>::type
+deserialize(RPCChannel &C, T &V) {
+ typename std::underlying_type<T>::type Tmp;
+ std::error_code EC = deserialize(C, Tmp);
+ V = static_cast<T>(Tmp);
+ return EC;
+}
+
+/// RPC channel serialization for bools.
+inline std::error_code serialize(RPCChannel &C, bool V) {
+ uint8_t VN = V ? 1 : 0;
+ return C.appendBytes(reinterpret_cast<const char *>(&VN), 1);
+}
+
+/// RPC channel deserialization for bools.
+inline std::error_code deserialize(RPCChannel &C, bool &V) {
+ uint8_t VN = 0;
+ if (auto EC = C.readBytes(reinterpret_cast<char *>(&VN), 1))
+ return EC;
+
+ V = (VN != 0) ? true : false;
+ return std::error_code();
+}
+
+/// RPC channel serialization for StringRefs.
+/// Note: There is no corresponding deseralization for this, as StringRef
+/// doesn't own its memory and so can't hold the deserialized data.
+inline std::error_code serialize(RPCChannel &C, StringRef S) {
+ if (auto EC = serialize(C, static_cast<uint64_t>(S.size())))
+ return EC;
+ return C.appendBytes((const char *)S.bytes_begin(), S.size());
+}
+
+/// RPC channel serialization for std::strings.
+inline std::error_code serialize(RPCChannel &C, const std::string &S) {
+ return serialize(C, StringRef(S));
+}
+
+/// RPC channel deserialization for std::strings.
+inline std::error_code deserialize(RPCChannel &C, std::string &S) {
+ uint64_t Count;
+ if (auto EC = deserialize(C, Count))
+ return EC;
+ S.resize(Count);
+ return C.readBytes(&S[0], Count);
+}
+
+/// RPC channel serialization for ArrayRef<T>.
+template <typename T>
+std::error_code serialize(RPCChannel &C, const ArrayRef<T> &A) {
+ if (auto EC = serialize(C, static_cast<uint64_t>(A.size())))
+ return EC;
+
+ for (const auto &E : A)
+ if (auto EC = serialize(C, E))
+ return EC;
+
+ return std::error_code();
+}
+
+/// RPC channel serialization for std::array<T>.
+template <typename T>
+std::error_code serialize(RPCChannel &C, const std::vector<T> &V) {
+ return serialize(C, ArrayRef<T>(V));
+}
+
+/// RPC channel deserialization for std::array<T>.
+template <typename T>
+std::error_code deserialize(RPCChannel &C, std::vector<T> &V) {
+ uint64_t Count = 0;
+ if (auto EC = deserialize(C, Count))
+ return EC;
+
+ V.resize(Count);
+ for (auto &E : V)
+ if (auto EC = deserialize(C, E))
+ return EC;
+
+ return std::error_code();
+}
+
+} // end namespace remote
+} // end namespace orc
+} // end namespace llvm
+
+#endif
diff --git a/include/llvm/ExecutionEngine/Orc/RPCUtils.h b/include/llvm/ExecutionEngine/Orc/RPCUtils.h
new file mode 100644
index 0000000..0bd5cbc
--- /dev/null
+++ b/include/llvm/ExecutionEngine/Orc/RPCUtils.h
@@ -0,0 +1,266 @@
+//===----- RPCUTils.h - Basic tilities for building RPC APIs ----*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Basic utilities for building RPC APIs.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_EXECUTIONENGINE_ORC_RPCUTILS_H
+#define LLVM_EXECUTIONENGINE_ORC_RPCUTILS_H
+
+#include "llvm/ADT/STLExtras.h"
+
+namespace llvm {
+namespace orc {
+namespace remote {
+
+// Base class containing utilities that require partial specialization.
+// These cannot be included in RPC, as template class members cannot be
+// partially specialized.
+class RPCBase {
+protected:
+ template <typename ProcedureIdT, ProcedureIdT ProcId, typename... Ts>
+ class ProcedureHelper {
+ public:
+ static const ProcedureIdT Id = ProcId;
+ };
+
+ template <typename ChannelT, typename Proc> class CallHelper;
+
+ template <typename ChannelT, typename ProcedureIdT, ProcedureIdT ProcId,
+ typename... ArgTs>
+ class CallHelper<ChannelT, ProcedureHelper<ProcedureIdT, ProcId, ArgTs...>> {
+ public:
+ static std::error_code call(ChannelT &C, const ArgTs &... Args) {
+ if (auto EC = serialize(C, ProcId))
+ return EC;
+ // If you see a compile-error on this line you're probably calling a
+ // function with the wrong signature.
+ return serialize_seq(C, Args...);
+ }
+ };
+
+ template <typename ChannelT, typename Proc> class HandlerHelper;
+
+ template <typename ChannelT, typename ProcedureIdT, ProcedureIdT ProcId,
+ typename... ArgTs>
+ class HandlerHelper<ChannelT,
+ ProcedureHelper<ProcedureIdT, ProcId, ArgTs...>> {
+ public:
+ template <typename HandlerT>
+ static std::error_code handle(ChannelT &C, HandlerT Handler) {
+ return readAndHandle(C, Handler, llvm::index_sequence_for<ArgTs...>());
+ }
+
+ private:
+ template <typename HandlerT, size_t... Is>
+ static std::error_code readAndHandle(ChannelT &C, HandlerT Handler,
+ llvm::index_sequence<Is...> _) {
+ std::tuple<ArgTs...> RPCArgs;
+ if (auto EC = deserialize_seq(C, std::get<Is>(RPCArgs)...))
+ return EC;
+ return Handler(std::get<Is>(RPCArgs)...);
+ }
+ };
+
+ template <typename ClassT, typename... ArgTs> class MemberFnWrapper {
+ public:
+ typedef std::error_code (ClassT::*MethodT)(ArgTs...);
+ MemberFnWrapper(ClassT &Instance, MethodT Method)
+ : Instance(Instance), Method(Method) {}
+ std::error_code operator()(ArgTs &... Args) {
+ return (Instance.*Method)(Args...);
+ }
+
+ private:
+ ClassT &Instance;
+ MethodT Method;
+ };
+
+ template <typename... ArgTs> class ReadArgs {
+ public:
+ std::error_code operator()() { return std::error_code(); }
+ };
+
+ template <typename ArgT, typename... ArgTs>
+ class ReadArgs<ArgT, ArgTs...> : public ReadArgs<ArgTs...> {
+ public:
+ ReadArgs(ArgT &Arg, ArgTs &... Args)
+ : ReadArgs<ArgTs...>(Args...), Arg(Arg) {}
+
+ std::error_code operator()(ArgT &ArgVal, ArgTs &... ArgVals) {
+ this->Arg = std::move(ArgVal);
+ return ReadArgs<ArgTs...>::operator()(ArgVals...);
+ }
+
+ private:
+ ArgT &Arg;
+ };
+};
+
+/// Contains primitive utilities for defining, calling and handling calls to
+/// remote procedures. ChannelT is a bidirectional stream conforming to the
+/// RPCChannel interface (see RPCChannel.h), and ProcedureIdT is a procedure
+/// identifier type that must be serializable on ChannelT.
+///
+/// These utilities support the construction of very primitive RPC utilities.
+/// Their intent is to ensure correct serialization and deserialization of
+/// procedure arguments, and to keep the client and server's view of the API in
+/// sync.
+///
+/// These utilities do not support return values. These can be handled by
+/// declaring a corresponding '.*Response' procedure and expecting it after a
+/// call). They also do not support versioning: the client and server *must* be
+/// compiled with the same procedure definitions.
+///
+///
+///
+/// Overview (see comments individual types/methods for details):
+///
+/// Procedure<Id, Args...> :
+///
+/// associates a unique serializable id with an argument list.
+///
+///
+/// call<Proc>(Channel, Args...) :
+///
+/// Calls the remote procedure 'Proc' by serializing Proc's id followed by its
+/// arguments and sending the resulting bytes to 'Channel'.
+///
+///
+/// handle<Proc>(Channel, <functor matching std::error_code(Args...)> :
+///
+/// Handles a call to 'Proc' by deserializing its arguments and calling the
+/// given functor. This assumes that the id for 'Proc' has already been
+/// deserialized.
+///
+/// expect<Proc>(Channel, <functor matching std::error_code(Args...)> :
+///
+/// The same as 'handle', except that the procedure id should not have been
+/// read yet. Expect will deserialize the id and assert that it matches Proc's
+/// id. If it does not, and unexpected RPC call error is returned.
+
+template <typename ChannelT, typename ProcedureIdT = uint32_t>
+class RPC : public RPCBase {
+public:
+ /// Utility class for defining/referring to RPC procedures.
+ ///
+ /// Typedefs of this utility are used when calling/handling remote procedures.
+ ///
+ /// ProcId should be a unique value of ProcedureIdT (i.e. not used with any
+ /// other Procedure typedef in the RPC API being defined.
+ ///
+ /// the template argument Ts... gives the argument list for the remote
+ /// procedure.
+ ///
+ /// E.g.
+ ///
+ /// typedef Procedure<0, bool> Proc1;
+ /// typedef Procedure<1, std::string, std::vector<int>> Proc2;
+ ///
+ /// if (auto EC = call<Proc1>(Channel, true))
+ /// /* handle EC */;
+ ///
+ /// if (auto EC = expect<Proc2>(Channel,
+ /// [](std::string &S, std::vector<int> &V) {
+ /// // Stuff.
+ /// return std::error_code();
+ /// })
+ /// /* handle EC */;
+ ///
+ template <ProcedureIdT ProcId, typename... Ts>
+ using Procedure = ProcedureHelper<ProcedureIdT, ProcId, Ts...>;
+
+ /// Serialize Args... to channel C, but do not call C.send().
+ ///
+ /// For buffered channels, this can be used to queue up several calls before
+ /// flushing the channel.
+ template <typename Proc, typename... ArgTs>
+ static std::error_code appendCall(ChannelT &C, const ArgTs &... Args) {
+ return CallHelper<ChannelT, Proc>::call(C, Args...);
+ }
+
+ /// Serialize Args... to channel C and call C.send().
+ template <typename Proc, typename... ArgTs>
+ static std::error_code call(ChannelT &C, const ArgTs &... Args) {
+ if (auto EC = appendCall<Proc>(C, Args...))
+ return EC;
+ return C.send();
+ }
+
+ /// Deserialize and return an enum whose underlying type is ProcedureIdT.
+ static std::error_code getNextProcId(ChannelT &C, ProcedureIdT &Id) {
+ return deserialize(C, Id);
+ }
+
+ /// Deserialize args for Proc from C and call Handler. The signature of
+ /// handler must conform to 'std::error_code(Args...)' where Args... matches
+ /// the arguments used in the Proc typedef.
+ template <typename Proc, typename HandlerT>
+ static std::error_code handle(ChannelT &C, HandlerT Handler) {
+ return HandlerHelper<ChannelT, Proc>::handle(C, Handler);
+ }
+
+ /// Helper version of 'handle' for calling member functions.
+ template <typename Proc, typename ClassT, typename... ArgTs>
+ static std::error_code
+ handle(ChannelT &C, ClassT &Instance,
+ std::error_code (ClassT::*HandlerMethod)(ArgTs...)) {
+ return handle<Proc>(
+ C, MemberFnWrapper<ClassT, ArgTs...>(Instance, HandlerMethod));
+ }
+
+ /// Deserialize a ProcedureIdT from C and verify it matches the id for Proc.
+ /// If the id does match, deserialize the arguments and call the handler
+ /// (similarly to handle).
+ /// If the id does not match, return an unexpect RPC call error and do not
+ /// deserialize any further bytes.
+ template <typename Proc, typename HandlerT>
+ static std::error_code expect(ChannelT &C, HandlerT Handler) {
+ ProcedureIdT ProcId;
+ if (auto EC = getNextProcId(C, ProcId))
+ return EC;
+ if (ProcId != Proc::Id)
+ return orcError(OrcErrorCode::UnexpectedRPCCall);
+ return handle<Proc>(C, Handler);
+ }
+
+ /// Helper version of expect for calling member functions.
+ template <typename Proc, typename ClassT, typename... ArgTs>
+ static std::error_code
+ expect(ChannelT &C, ClassT &Instance,
+ std::error_code (ClassT::*HandlerMethod)(ArgTs...)) {
+ return expect<Proc>(
+ C, MemberFnWrapper<ClassT, ArgTs...>(Instance, HandlerMethod));
+ }
+
+ /// Helper for handling setter procedures - this method returns a functor that
+ /// sets the variables referred to by Args... to values deserialized from the
+ /// channel.
+ /// E.g.
+ ///
+ /// typedef Procedure<0, bool, int> Proc1;
+ ///
+ /// ...
+ /// bool B;
+ /// int I;
+ /// if (auto EC = expect<Proc1>(Channel, readArgs(B, I)))
+ /// /* Handle Args */ ;
+ ///
+ template <typename... ArgTs>
+ static ReadArgs<ArgTs...> readArgs(ArgTs &... Args) {
+ return ReadArgs<ArgTs...>(Args...);
+ }
+};
+
+} // end namespace remote
+} // end namespace orc
+} // end namespace llvm
+
+#endif
OpenPOWER on IntegriCloud