//===---- ExecutorProcessControl.cpp -- Executor process control APIs -----===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h" #include "llvm/ExecutionEngine/Orc/Core.h" #include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h" #include "llvm/Support/FormatVariadic.h" #include "llvm/Support/Host.h" #include "llvm/Support/Process.h" #define DEBUG_TYPE "orc" namespace llvm { namespace orc { ExecutorProcessControl::MemoryAccess::~MemoryAccess() {} ExecutorProcessControl::~ExecutorProcessControl() {} SelfExecutorProcessControl::SelfExecutorProcessControl( std::shared_ptr SSP, Triple TargetTriple, unsigned PageSize, std::unique_ptr MemMgr) : ExecutorProcessControl(std::move(SSP)) { OwnedMemMgr = std::move(MemMgr); if (!OwnedMemMgr) OwnedMemMgr = std::make_unique(); this->TargetTriple = std::move(TargetTriple); this->PageSize = PageSize; this->MemMgr = OwnedMemMgr.get(); this->MemAccess = this; this->JDI = {ExecutorAddress::fromPtr(jitDispatchViaWrapperFunctionManager), ExecutorAddress::fromPtr(this)}; if (this->TargetTriple.isOSBinFormatMachO()) GlobalManglingPrefix = '_'; } Expected> SelfExecutorProcessControl::Create( std::shared_ptr SSP, std::unique_ptr MemMgr) { if (!SSP) SSP = std::make_shared(); auto PageSize = sys::Process::getPageSize(); if (!PageSize) return PageSize.takeError(); Triple TT(sys::getProcessTriple()); return std::make_unique( std::move(SSP), std::move(TT), *PageSize, std::move(MemMgr)); } Expected SelfExecutorProcessControl::loadDylib(const char *DylibPath) { std::string ErrMsg; auto Dylib = std::make_unique( sys::DynamicLibrary::getPermanentLibrary(DylibPath, &ErrMsg)); if (!Dylib->isValid()) return make_error(std::move(ErrMsg), inconvertibleErrorCode()); DynamicLibraries.push_back(std::move(Dylib)); return pointerToJITTargetAddress(DynamicLibraries.back().get()); } Expected> SelfExecutorProcessControl::lookupSymbols(ArrayRef Request) { std::vector R; for (auto &Elem : Request) { auto *Dylib = jitTargetAddressToPointer(Elem.Handle); assert(llvm::any_of(DynamicLibraries, [=](const std::unique_ptr &DL) { return DL.get() == Dylib; }) && "Invalid handle"); R.push_back(std::vector()); for (auto &KV : Elem.Symbols) { auto &Sym = KV.first; std::string Tmp((*Sym).data() + !!GlobalManglingPrefix, (*Sym).size() - !!GlobalManglingPrefix); void *Addr = Dylib->getAddressOfSymbol(Tmp.c_str()); if (!Addr && KV.second == SymbolLookupFlags::RequiredSymbol) { // FIXME: Collect all failing symbols before erroring out. SymbolNameVector MissingSymbols; MissingSymbols.push_back(Sym); return make_error(std::move(MissingSymbols)); } R.back().push_back(pointerToJITTargetAddress(Addr)); } } return R; } Expected SelfExecutorProcessControl::runAsMain(JITTargetAddress MainFnAddr, ArrayRef Args) { using MainTy = int (*)(int, char *[]); return orc::runAsMain(jitTargetAddressToFunction(MainFnAddr), Args); } void SelfExecutorProcessControl::callWrapperAsync( SendResultFunction SendResult, JITTargetAddress WrapperFnAddr, ArrayRef ArgBuffer) { using WrapperFnTy = shared::detail::CWrapperFunctionResult (*)(const char *Data, size_t Size); auto *WrapperFn = jitTargetAddressToFunction(WrapperFnAddr); SendResult(WrapperFn(ArgBuffer.data(), ArgBuffer.size())); } Error SelfExecutorProcessControl::disconnect() { return Error::success(); } void SelfExecutorProcessControl::writeUInt8s(ArrayRef Ws, WriteResultFn OnWriteComplete) { for (auto &W : Ws) *jitTargetAddressToPointer(W.Address) = W.Value; OnWriteComplete(Error::success()); } void SelfExecutorProcessControl::writeUInt16s( ArrayRef Ws, WriteResultFn OnWriteComplete) { for (auto &W : Ws) *jitTargetAddressToPointer(W.Address) = W.Value; OnWriteComplete(Error::success()); } void SelfExecutorProcessControl::writeUInt32s( ArrayRef Ws, WriteResultFn OnWriteComplete) { for (auto &W : Ws) *jitTargetAddressToPointer(W.Address) = W.Value; OnWriteComplete(Error::success()); } void SelfExecutorProcessControl::writeUInt64s( ArrayRef Ws, WriteResultFn OnWriteComplete) { for (auto &W : Ws) *jitTargetAddressToPointer(W.Address) = W.Value; OnWriteComplete(Error::success()); } void SelfExecutorProcessControl::writeBuffers( ArrayRef Ws, WriteResultFn OnWriteComplete) { for (auto &W : Ws) memcpy(jitTargetAddressToPointer(W.Address), W.Buffer.data(), W.Buffer.size()); OnWriteComplete(Error::success()); } shared::detail::CWrapperFunctionResult SelfExecutorProcessControl::jitDispatchViaWrapperFunctionManager( void *Ctx, const void *FnTag, const char *Data, size_t Size) { LLVM_DEBUG({ dbgs() << "jit-dispatch call with tag " << FnTag << " and " << Size << " byte payload.\n"; }); std::promise ResultP; auto ResultF = ResultP.get_future(); static_cast(Ctx) ->getExecutionSession() .runJITDispatchHandler( [ResultP = std::move(ResultP)]( shared::WrapperFunctionResult Result) mutable { ResultP.set_value(std::move(Result)); }, pointerToJITTargetAddress(FnTag), {Data, Size}); return ResultF.get().release(); } } // end namespace orc } // end namespace llvm