1*700637cbSDimitry Andric //===------ SelfExecutorProcessControl.cpp -- EPC for in-process JITs -----===//
2*700637cbSDimitry Andric //
3*700637cbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*700637cbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*700637cbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*700637cbSDimitry Andric //
7*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
8*700637cbSDimitry Andric
9*700637cbSDimitry Andric #include "llvm/ExecutionEngine/Orc/SelfExecutorProcessControl.h"
10*700637cbSDimitry Andric
11*700637cbSDimitry Andric #include "llvm/ExecutionEngine/Orc/Core.h"
12*700637cbSDimitry Andric #include "llvm/ExecutionEngine/Orc/TargetProcess/DefaultHostBootstrapValues.h"
13*700637cbSDimitry Andric #include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h"
14*700637cbSDimitry Andric #include "llvm/Support/DynamicLibrary.h"
15*700637cbSDimitry Andric #include "llvm/Support/Process.h"
16*700637cbSDimitry Andric #include "llvm/TargetParser/Host.h"
17*700637cbSDimitry Andric
18*700637cbSDimitry Andric #define DEBUG_TYPE "orc"
19*700637cbSDimitry Andric
20*700637cbSDimitry Andric namespace llvm::orc {
21*700637cbSDimitry Andric
SelfExecutorProcessControl(std::shared_ptr<SymbolStringPool> SSP,std::unique_ptr<TaskDispatcher> D,Triple TargetTriple,unsigned PageSize,std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr)22*700637cbSDimitry Andric SelfExecutorProcessControl::SelfExecutorProcessControl(
23*700637cbSDimitry Andric std::shared_ptr<SymbolStringPool> SSP, std::unique_ptr<TaskDispatcher> D,
24*700637cbSDimitry Andric Triple TargetTriple, unsigned PageSize,
25*700637cbSDimitry Andric std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr)
26*700637cbSDimitry Andric : ExecutorProcessControl(std::move(SSP), std::move(D)),
27*700637cbSDimitry Andric IPMA(TargetTriple.isArch64Bit()) {
28*700637cbSDimitry Andric
29*700637cbSDimitry Andric OwnedMemMgr = std::move(MemMgr);
30*700637cbSDimitry Andric if (!OwnedMemMgr)
31*700637cbSDimitry Andric OwnedMemMgr = std::make_unique<jitlink::InProcessMemoryManager>(
32*700637cbSDimitry Andric sys::Process::getPageSizeEstimate());
33*700637cbSDimitry Andric
34*700637cbSDimitry Andric this->TargetTriple = std::move(TargetTriple);
35*700637cbSDimitry Andric this->PageSize = PageSize;
36*700637cbSDimitry Andric this->MemMgr = OwnedMemMgr.get();
37*700637cbSDimitry Andric this->MemAccess = &IPMA;
38*700637cbSDimitry Andric this->DylibMgr = this;
39*700637cbSDimitry Andric this->JDI = {ExecutorAddr::fromPtr(jitDispatchViaWrapperFunctionManager),
40*700637cbSDimitry Andric ExecutorAddr::fromPtr(this)};
41*700637cbSDimitry Andric
42*700637cbSDimitry Andric if (this->TargetTriple.isOSBinFormatMachO())
43*700637cbSDimitry Andric GlobalManglingPrefix = '_';
44*700637cbSDimitry Andric
45*700637cbSDimitry Andric addDefaultBootstrapValuesForHostProcess(BootstrapMap, BootstrapSymbols);
46*700637cbSDimitry Andric
47*700637cbSDimitry Andric #ifdef __APPLE__
48*700637cbSDimitry Andric // FIXME: Don't add an UnwindInfoManager by default -- it's redundant when
49*700637cbSDimitry Andric // the ORC runtime is loaded. We'll need a way to document this and
50*700637cbSDimitry Andric // allow clients to choose.
51*700637cbSDimitry Andric if (UnwindInfoManager::TryEnable())
52*700637cbSDimitry Andric UnwindInfoManager::addBootstrapSymbols(this->BootstrapSymbols);
53*700637cbSDimitry Andric #endif // __APPLE__
54*700637cbSDimitry Andric }
55*700637cbSDimitry Andric
56*700637cbSDimitry Andric Expected<std::unique_ptr<SelfExecutorProcessControl>>
Create(std::shared_ptr<SymbolStringPool> SSP,std::unique_ptr<TaskDispatcher> D,std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr)57*700637cbSDimitry Andric SelfExecutorProcessControl::Create(
58*700637cbSDimitry Andric std::shared_ptr<SymbolStringPool> SSP, std::unique_ptr<TaskDispatcher> D,
59*700637cbSDimitry Andric std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr) {
60*700637cbSDimitry Andric
61*700637cbSDimitry Andric if (!SSP)
62*700637cbSDimitry Andric SSP = std::make_shared<SymbolStringPool>();
63*700637cbSDimitry Andric
64*700637cbSDimitry Andric if (!D)
65*700637cbSDimitry Andric D = std::make_unique<InPlaceTaskDispatcher>();
66*700637cbSDimitry Andric
67*700637cbSDimitry Andric auto PageSize = sys::Process::getPageSize();
68*700637cbSDimitry Andric if (!PageSize)
69*700637cbSDimitry Andric return PageSize.takeError();
70*700637cbSDimitry Andric
71*700637cbSDimitry Andric Triple TT(sys::getProcessTriple());
72*700637cbSDimitry Andric
73*700637cbSDimitry Andric return std::make_unique<SelfExecutorProcessControl>(
74*700637cbSDimitry Andric std::move(SSP), std::move(D), std::move(TT), *PageSize,
75*700637cbSDimitry Andric std::move(MemMgr));
76*700637cbSDimitry Andric }
77*700637cbSDimitry Andric
78*700637cbSDimitry Andric Expected<tpctypes::DylibHandle>
loadDylib(const char * DylibPath)79*700637cbSDimitry Andric SelfExecutorProcessControl::loadDylib(const char *DylibPath) {
80*700637cbSDimitry Andric std::string ErrMsg;
81*700637cbSDimitry Andric auto Dylib = sys::DynamicLibrary::getPermanentLibrary(DylibPath, &ErrMsg);
82*700637cbSDimitry Andric if (!Dylib.isValid())
83*700637cbSDimitry Andric return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
84*700637cbSDimitry Andric return ExecutorAddr::fromPtr(Dylib.getOSSpecificHandle());
85*700637cbSDimitry Andric }
86*700637cbSDimitry Andric
lookupSymbolsAsync(ArrayRef<LookupRequest> Request,DylibManager::SymbolLookupCompleteFn Complete)87*700637cbSDimitry Andric void SelfExecutorProcessControl::lookupSymbolsAsync(
88*700637cbSDimitry Andric ArrayRef<LookupRequest> Request,
89*700637cbSDimitry Andric DylibManager::SymbolLookupCompleteFn Complete) {
90*700637cbSDimitry Andric std::vector<tpctypes::LookupResult> R;
91*700637cbSDimitry Andric
92*700637cbSDimitry Andric for (auto &Elem : Request) {
93*700637cbSDimitry Andric sys::DynamicLibrary Dylib(Elem.Handle.toPtr<void *>());
94*700637cbSDimitry Andric R.push_back(std::vector<ExecutorSymbolDef>());
95*700637cbSDimitry Andric for (auto &KV : Elem.Symbols) {
96*700637cbSDimitry Andric auto &Sym = KV.first;
97*700637cbSDimitry Andric std::string Tmp((*Sym).data() + !!GlobalManglingPrefix,
98*700637cbSDimitry Andric (*Sym).size() - !!GlobalManglingPrefix);
99*700637cbSDimitry Andric void *Addr = Dylib.getAddressOfSymbol(Tmp.c_str());
100*700637cbSDimitry Andric if (!Addr && KV.second == SymbolLookupFlags::RequiredSymbol) {
101*700637cbSDimitry Andric // FIXME: Collect all failing symbols before erroring out.
102*700637cbSDimitry Andric SymbolNameVector MissingSymbols;
103*700637cbSDimitry Andric MissingSymbols.push_back(Sym);
104*700637cbSDimitry Andric return Complete(
105*700637cbSDimitry Andric make_error<SymbolsNotFound>(SSP, std::move(MissingSymbols)));
106*700637cbSDimitry Andric }
107*700637cbSDimitry Andric // FIXME: determine accurate JITSymbolFlags.
108*700637cbSDimitry Andric R.back().push_back(
109*700637cbSDimitry Andric {ExecutorAddr::fromPtr(Addr), JITSymbolFlags::Exported});
110*700637cbSDimitry Andric }
111*700637cbSDimitry Andric }
112*700637cbSDimitry Andric
113*700637cbSDimitry Andric Complete(std::move(R));
114*700637cbSDimitry Andric }
115*700637cbSDimitry Andric
116*700637cbSDimitry Andric Expected<int32_t>
runAsMain(ExecutorAddr MainFnAddr,ArrayRef<std::string> Args)117*700637cbSDimitry Andric SelfExecutorProcessControl::runAsMain(ExecutorAddr MainFnAddr,
118*700637cbSDimitry Andric ArrayRef<std::string> Args) {
119*700637cbSDimitry Andric using MainTy = int (*)(int, char *[]);
120*700637cbSDimitry Andric return orc::runAsMain(MainFnAddr.toPtr<MainTy>(), Args);
121*700637cbSDimitry Andric }
122*700637cbSDimitry Andric
123*700637cbSDimitry Andric Expected<int32_t>
runAsVoidFunction(ExecutorAddr VoidFnAddr)124*700637cbSDimitry Andric SelfExecutorProcessControl::runAsVoidFunction(ExecutorAddr VoidFnAddr) {
125*700637cbSDimitry Andric using VoidTy = int (*)();
126*700637cbSDimitry Andric return orc::runAsVoidFunction(VoidFnAddr.toPtr<VoidTy>());
127*700637cbSDimitry Andric }
128*700637cbSDimitry Andric
129*700637cbSDimitry Andric Expected<int32_t>
runAsIntFunction(ExecutorAddr IntFnAddr,int Arg)130*700637cbSDimitry Andric SelfExecutorProcessControl::runAsIntFunction(ExecutorAddr IntFnAddr, int Arg) {
131*700637cbSDimitry Andric using IntTy = int (*)(int);
132*700637cbSDimitry Andric return orc::runAsIntFunction(IntFnAddr.toPtr<IntTy>(), Arg);
133*700637cbSDimitry Andric }
134*700637cbSDimitry Andric
callWrapperAsync(ExecutorAddr WrapperFnAddr,IncomingWFRHandler SendResult,ArrayRef<char> ArgBuffer)135*700637cbSDimitry Andric void SelfExecutorProcessControl::callWrapperAsync(ExecutorAddr WrapperFnAddr,
136*700637cbSDimitry Andric IncomingWFRHandler SendResult,
137*700637cbSDimitry Andric ArrayRef<char> ArgBuffer) {
138*700637cbSDimitry Andric using WrapperFnTy =
139*700637cbSDimitry Andric shared::CWrapperFunctionResult (*)(const char *Data, size_t Size);
140*700637cbSDimitry Andric auto *WrapperFn = WrapperFnAddr.toPtr<WrapperFnTy>();
141*700637cbSDimitry Andric SendResult(WrapperFn(ArgBuffer.data(), ArgBuffer.size()));
142*700637cbSDimitry Andric }
143*700637cbSDimitry Andric
disconnect()144*700637cbSDimitry Andric Error SelfExecutorProcessControl::disconnect() {
145*700637cbSDimitry Andric D->shutdown();
146*700637cbSDimitry Andric return Error::success();
147*700637cbSDimitry Andric }
148*700637cbSDimitry Andric
149*700637cbSDimitry Andric shared::CWrapperFunctionResult
jitDispatchViaWrapperFunctionManager(void * Ctx,const void * FnTag,const char * Data,size_t Size)150*700637cbSDimitry Andric SelfExecutorProcessControl::jitDispatchViaWrapperFunctionManager(
151*700637cbSDimitry Andric void *Ctx, const void *FnTag, const char *Data, size_t Size) {
152*700637cbSDimitry Andric
153*700637cbSDimitry Andric LLVM_DEBUG({
154*700637cbSDimitry Andric dbgs() << "jit-dispatch call with tag " << FnTag << " and " << Size
155*700637cbSDimitry Andric << " byte payload.\n";
156*700637cbSDimitry Andric });
157*700637cbSDimitry Andric
158*700637cbSDimitry Andric std::promise<shared::WrapperFunctionResult> ResultP;
159*700637cbSDimitry Andric auto ResultF = ResultP.get_future();
160*700637cbSDimitry Andric static_cast<SelfExecutorProcessControl *>(Ctx)
161*700637cbSDimitry Andric ->getExecutionSession()
162*700637cbSDimitry Andric .runJITDispatchHandler(
163*700637cbSDimitry Andric [ResultP = std::move(ResultP)](
164*700637cbSDimitry Andric shared::WrapperFunctionResult Result) mutable {
165*700637cbSDimitry Andric ResultP.set_value(std::move(Result));
166*700637cbSDimitry Andric },
167*700637cbSDimitry Andric ExecutorAddr::fromPtr(FnTag), {Data, Size});
168*700637cbSDimitry Andric
169*700637cbSDimitry Andric return ResultF.get().release();
170*700637cbSDimitry Andric }
171*700637cbSDimitry Andric
172*700637cbSDimitry Andric } // namespace llvm::orc
173