//===---- 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/Shared/OrcRTBridge.h" #include "llvm/ExecutionEngine/Orc/TargetProcess/RegisterEHFrames.h" #include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h" #include "llvm/Support/FormatVariadic.h" #include "llvm/Support/Process.h" #include "llvm/TargetParser/Host.h" #define DEBUG_TYPE "orc" namespace llvm { namespace orc { ExecutorProcessControl::MemoryAccess::~MemoryAccess() = default; ExecutorProcessControl::~ExecutorProcessControl() = default; SelfExecutorProcessControl::SelfExecutorProcessControl( std::shared_ptr SSP, std::unique_ptr D, Triple TargetTriple, unsigned PageSize, std::unique_ptr MemMgr) : ExecutorProcessControl(std::move(SSP), std::move(D)), InProcessMemoryAccess(TargetTriple.isArch64Bit()) { OwnedMemMgr = std::move(MemMgr); if (!OwnedMemMgr) OwnedMemMgr = std::make_unique( sys::Process::getPageSizeEstimate()); this->TargetTriple = std::move(TargetTriple); this->PageSize = PageSize; this->MemMgr = OwnedMemMgr.get(); this->MemAccess = this; this->JDI = {ExecutorAddr::fromPtr(jitDispatchViaWrapperFunctionManager), ExecutorAddr::fromPtr(this)}; if (this->TargetTriple.isOSBinFormatMachO()) GlobalManglingPrefix = '_'; this->BootstrapSymbols[rt::RegisterEHFrameSectionWrapperName] = ExecutorAddr::fromPtr(&llvm_orc_registerEHFrameSectionWrapper); this->BootstrapSymbols[rt::DeregisterEHFrameSectionWrapperName] = ExecutorAddr::fromPtr(&llvm_orc_deregisterEHFrameSectionWrapper); } Expected> SelfExecutorProcessControl::Create( std::shared_ptr SSP, std::unique_ptr D, std::unique_ptr MemMgr) { if (!SSP) SSP = std::make_shared(); if (!D) D = std::make_unique(); auto PageSize = sys::Process::getPageSize(); if (!PageSize) return PageSize.takeError(); Triple TT(sys::getProcessTriple()); return std::make_unique( std::move(SSP), std::move(D), std::move(TT), *PageSize, std::move(MemMgr)); } Expected SelfExecutorProcessControl::loadDylib(const char *DylibPath) { std::string ErrMsg; auto Dylib = sys::DynamicLibrary::getPermanentLibrary(DylibPath, &ErrMsg); if (!Dylib.isValid()) return make_error(std::move(ErrMsg), inconvertibleErrorCode()); return ExecutorAddr::fromPtr(Dylib.getOSSpecificHandle()); } void SelfExecutorProcessControl::lookupSymbolsAsync( ArrayRef Request, ExecutorProcessControl::SymbolLookupCompleteFn Complete) { std::vector R; for (auto &Elem : Request) { sys::DynamicLibrary Dylib(Elem.Handle.toPtr()); 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 Complete( make_error(SSP, std::move(MissingSymbols))); } // FIXME: determine accurate JITSymbolFlags. R.back().push_back( {ExecutorAddr::fromPtr(Addr), JITSymbolFlags::Exported}); } } Complete(std::move(R)); } Expected SelfExecutorProcessControl::runAsMain(ExecutorAddr MainFnAddr, ArrayRef Args) { using MainTy = int (*)(int, char *[]); return orc::runAsMain(MainFnAddr.toPtr(), Args); } Expected SelfExecutorProcessControl::runAsVoidFunction(ExecutorAddr VoidFnAddr) { using VoidTy = int (*)(); return orc::runAsVoidFunction(VoidFnAddr.toPtr()); } Expected SelfExecutorProcessControl::runAsIntFunction(ExecutorAddr IntFnAddr, int Arg) { using IntTy = int (*)(int); return orc::runAsIntFunction(IntFnAddr.toPtr(), Arg); } void SelfExecutorProcessControl::callWrapperAsync(ExecutorAddr WrapperFnAddr, IncomingWFRHandler SendResult, ArrayRef ArgBuffer) { using WrapperFnTy = shared::CWrapperFunctionResult (*)(const char *Data, size_t Size); auto *WrapperFn = WrapperFnAddr.toPtr(); SendResult(WrapperFn(ArgBuffer.data(), ArgBuffer.size())); } Error SelfExecutorProcessControl::disconnect() { D->shutdown(); return Error::success(); } void InProcessMemoryAccess::writeUInt8sAsync(ArrayRef Ws, WriteResultFn OnWriteComplete) { for (auto &W : Ws) *W.Addr.toPtr() = W.Value; OnWriteComplete(Error::success()); } void InProcessMemoryAccess::writeUInt16sAsync( ArrayRef Ws, WriteResultFn OnWriteComplete) { for (auto &W : Ws) *W.Addr.toPtr() = W.Value; OnWriteComplete(Error::success()); } void InProcessMemoryAccess::writeUInt32sAsync( ArrayRef Ws, WriteResultFn OnWriteComplete) { for (auto &W : Ws) *W.Addr.toPtr() = W.Value; OnWriteComplete(Error::success()); } void InProcessMemoryAccess::writeUInt64sAsync( ArrayRef Ws, WriteResultFn OnWriteComplete) { for (auto &W : Ws) *W.Addr.toPtr() = W.Value; OnWriteComplete(Error::success()); } void InProcessMemoryAccess::writeBuffersAsync( ArrayRef Ws, WriteResultFn OnWriteComplete) { for (auto &W : Ws) memcpy(W.Addr.toPtr(), W.Buffer.data(), W.Buffer.size()); OnWriteComplete(Error::success()); } void InProcessMemoryAccess::writePointersAsync( ArrayRef Ws, WriteResultFn OnWriteComplete) { if (IsArch64Bit) { for (auto &W : Ws) *W.Addr.toPtr() = W.Value.getValue(); } else { for (auto &W : Ws) *W.Addr.toPtr() = static_cast(W.Value.getValue()); } OnWriteComplete(Error::success()); } shared::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)); }, ExecutorAddr::fromPtr(FnTag), {Data, Size}); return ResultF.get().release(); } } // end namespace orc } // end namespace llvm