//===------ TargetProcessControl.cpp -- Target 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/TargetProcessControl.h" #include "llvm/ExecutionEngine/Orc/Core.h" #include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h" #include "llvm/Support/Host.h" #include "llvm/Support/Process.h" #include namespace llvm { namespace orc { TargetProcessControl::MemoryAccess::~MemoryAccess() {} TargetProcessControl::~TargetProcessControl() {} SelfTargetProcessControl::SelfTargetProcessControl( std::shared_ptr SSP, Triple TargetTriple, unsigned PageSize, std::unique_ptr MemMgr) : TargetProcessControl(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; if (this->TargetTriple.isOSBinFormatMachO()) GlobalManglingPrefix = '_'; } Expected> SelfTargetProcessControl::Create( std::shared_ptr SSP, std::unique_ptr MemMgr) { 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 SelfTargetProcessControl::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> SelfTargetProcessControl::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 SelfTargetProcessControl::runAsMain(JITTargetAddress MainFnAddr, ArrayRef Args) { using MainTy = int (*)(int, char *[]); return orc::runAsMain(jitTargetAddressToFunction(MainFnAddr), Args); } Expected SelfTargetProcessControl::runWrapper(JITTargetAddress WrapperFnAddr, ArrayRef ArgBuffer) { using WrapperFnTy = tpctypes::CWrapperFunctionResult (*)(const uint8_t *Data, uint64_t Size); auto *WrapperFn = jitTargetAddressToFunction(WrapperFnAddr); return WrapperFn(ArgBuffer.data(), ArgBuffer.size()); } Error SelfTargetProcessControl::disconnect() { return Error::success(); } void SelfTargetProcessControl::writeUInt8s(ArrayRef Ws, WriteResultFn OnWriteComplete) { for (auto &W : Ws) *jitTargetAddressToPointer(W.Address) = W.Value; OnWriteComplete(Error::success()); } void SelfTargetProcessControl::writeUInt16s(ArrayRef Ws, WriteResultFn OnWriteComplete) { for (auto &W : Ws) *jitTargetAddressToPointer(W.Address) = W.Value; OnWriteComplete(Error::success()); } void SelfTargetProcessControl::writeUInt32s(ArrayRef Ws, WriteResultFn OnWriteComplete) { for (auto &W : Ws) *jitTargetAddressToPointer(W.Address) = W.Value; OnWriteComplete(Error::success()); } void SelfTargetProcessControl::writeUInt64s(ArrayRef Ws, WriteResultFn OnWriteComplete) { for (auto &W : Ws) *jitTargetAddressToPointer(W.Address) = W.Value; OnWriteComplete(Error::success()); } void SelfTargetProcessControl::writeBuffers(ArrayRef Ws, WriteResultFn OnWriteComplete) { for (auto &W : Ws) memcpy(jitTargetAddressToPointer(W.Address), W.Buffer.data(), W.Buffer.size()); OnWriteComplete(Error::success()); } } // end namespace orc } // end namespace llvm