xref: /freebsd/contrib/llvm-project/llvm/lib/ExecutionEngine/Orc/ExecutorProcessControl.cpp (revision 9bc300465e48e19d794d88d0c158a2adb92c7197)
1 //===---- ExecutorProcessControl.cpp -- Executor process control APIs -----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/ExecutionEngine/Orc/ExecutorProcessControl.h"
10 
11 #include "llvm/ExecutionEngine/Orc/Core.h"
12 #include "llvm/ExecutionEngine/Orc/Shared/OrcRTBridge.h"
13 #include "llvm/ExecutionEngine/Orc/TargetProcess/RegisterEHFrames.h"
14 #include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h"
15 #include "llvm/Support/FormatVariadic.h"
16 #include "llvm/Support/Process.h"
17 #include "llvm/TargetParser/Host.h"
18 
19 #define DEBUG_TYPE "orc"
20 
21 namespace llvm {
22 namespace orc {
23 
24 ExecutorProcessControl::MemoryAccess::~MemoryAccess() = default;
25 
26 ExecutorProcessControl::~ExecutorProcessControl() = default;
27 
28 SelfExecutorProcessControl::SelfExecutorProcessControl(
29     std::shared_ptr<SymbolStringPool> SSP, std::unique_ptr<TaskDispatcher> D,
30     Triple TargetTriple, unsigned PageSize,
31     std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr)
32     : ExecutorProcessControl(std::move(SSP), std::move(D)),
33       InProcessMemoryAccess(TargetTriple.isArch64Bit()) {
34 
35   OwnedMemMgr = std::move(MemMgr);
36   if (!OwnedMemMgr)
37     OwnedMemMgr = std::make_unique<jitlink::InProcessMemoryManager>(
38         sys::Process::getPageSizeEstimate());
39 
40   this->TargetTriple = std::move(TargetTriple);
41   this->PageSize = PageSize;
42   this->MemMgr = OwnedMemMgr.get();
43   this->MemAccess = this;
44   this->JDI = {ExecutorAddr::fromPtr(jitDispatchViaWrapperFunctionManager),
45                ExecutorAddr::fromPtr(this)};
46   if (this->TargetTriple.isOSBinFormatMachO())
47     GlobalManglingPrefix = '_';
48 
49   this->BootstrapSymbols[rt::RegisterEHFrameSectionWrapperName] =
50       ExecutorAddr::fromPtr(&llvm_orc_registerEHFrameSectionWrapper);
51   this->BootstrapSymbols[rt::DeregisterEHFrameSectionWrapperName] =
52       ExecutorAddr::fromPtr(&llvm_orc_deregisterEHFrameSectionWrapper);
53 }
54 
55 Expected<std::unique_ptr<SelfExecutorProcessControl>>
56 SelfExecutorProcessControl::Create(
57     std::shared_ptr<SymbolStringPool> SSP,
58     std::unique_ptr<TaskDispatcher> D,
59     std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr) {
60 
61   if (!SSP)
62     SSP = std::make_shared<SymbolStringPool>();
63 
64   if (!D) {
65 #if LLVM_ENABLE_THREADS
66     D = std::make_unique<DynamicThreadPoolTaskDispatcher>();
67 #else
68     D = std::make_unique<InPlaceTaskDispatcher>();
69 #endif
70   }
71 
72   auto PageSize = sys::Process::getPageSize();
73   if (!PageSize)
74     return PageSize.takeError();
75 
76   Triple TT(sys::getProcessTriple());
77 
78   return std::make_unique<SelfExecutorProcessControl>(
79       std::move(SSP), std::move(D), std::move(TT), *PageSize,
80       std::move(MemMgr));
81 }
82 
83 Expected<tpctypes::DylibHandle>
84 SelfExecutorProcessControl::loadDylib(const char *DylibPath) {
85   std::string ErrMsg;
86   auto Dylib = sys::DynamicLibrary::getPermanentLibrary(DylibPath, &ErrMsg);
87   if (!Dylib.isValid())
88     return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
89   return ExecutorAddr::fromPtr(Dylib.getOSSpecificHandle());
90 }
91 
92 Expected<std::vector<tpctypes::LookupResult>>
93 SelfExecutorProcessControl::lookupSymbols(ArrayRef<LookupRequest> Request) {
94   std::vector<tpctypes::LookupResult> R;
95 
96   for (auto &Elem : Request) {
97     sys::DynamicLibrary Dylib(Elem.Handle.toPtr<void *>());
98     R.push_back(std::vector<ExecutorSymbolDef>());
99     for (auto &KV : Elem.Symbols) {
100       auto &Sym = KV.first;
101       std::string Tmp((*Sym).data() + !!GlobalManglingPrefix,
102                       (*Sym).size() - !!GlobalManglingPrefix);
103       void *Addr = Dylib.getAddressOfSymbol(Tmp.c_str());
104       if (!Addr && KV.second == SymbolLookupFlags::RequiredSymbol) {
105         // FIXME: Collect all failing symbols before erroring out.
106         SymbolNameVector MissingSymbols;
107         MissingSymbols.push_back(Sym);
108         return make_error<SymbolsNotFound>(SSP, std::move(MissingSymbols));
109       }
110       // FIXME: determine accurate JITSymbolFlags.
111       R.back().push_back(
112           {ExecutorAddr::fromPtr(Addr), JITSymbolFlags::Exported});
113     }
114   }
115 
116   return R;
117 }
118 
119 Expected<int32_t>
120 SelfExecutorProcessControl::runAsMain(ExecutorAddr MainFnAddr,
121                                       ArrayRef<std::string> Args) {
122   using MainTy = int (*)(int, char *[]);
123   return orc::runAsMain(MainFnAddr.toPtr<MainTy>(), Args);
124 }
125 
126 Expected<int32_t>
127 SelfExecutorProcessControl::runAsVoidFunction(ExecutorAddr VoidFnAddr) {
128   using VoidTy = int (*)();
129   return orc::runAsVoidFunction(VoidFnAddr.toPtr<VoidTy>());
130 }
131 
132 Expected<int32_t>
133 SelfExecutorProcessControl::runAsIntFunction(ExecutorAddr IntFnAddr, int Arg) {
134   using IntTy = int (*)(int);
135   return orc::runAsIntFunction(IntFnAddr.toPtr<IntTy>(), Arg);
136 }
137 
138 void SelfExecutorProcessControl::callWrapperAsync(ExecutorAddr WrapperFnAddr,
139                                                   IncomingWFRHandler SendResult,
140                                                   ArrayRef<char> ArgBuffer) {
141   using WrapperFnTy =
142       shared::CWrapperFunctionResult (*)(const char *Data, size_t Size);
143   auto *WrapperFn = WrapperFnAddr.toPtr<WrapperFnTy>();
144   SendResult(WrapperFn(ArgBuffer.data(), ArgBuffer.size()));
145 }
146 
147 Error SelfExecutorProcessControl::disconnect() {
148   D->shutdown();
149   return Error::success();
150 }
151 
152 void InProcessMemoryAccess::writeUInt8sAsync(ArrayRef<tpctypes::UInt8Write> Ws,
153                                              WriteResultFn OnWriteComplete) {
154   for (auto &W : Ws)
155     *W.Addr.toPtr<uint8_t *>() = W.Value;
156   OnWriteComplete(Error::success());
157 }
158 
159 void InProcessMemoryAccess::writeUInt16sAsync(
160     ArrayRef<tpctypes::UInt16Write> Ws, WriteResultFn OnWriteComplete) {
161   for (auto &W : Ws)
162     *W.Addr.toPtr<uint16_t *>() = W.Value;
163   OnWriteComplete(Error::success());
164 }
165 
166 void InProcessMemoryAccess::writeUInt32sAsync(
167     ArrayRef<tpctypes::UInt32Write> Ws, WriteResultFn OnWriteComplete) {
168   for (auto &W : Ws)
169     *W.Addr.toPtr<uint32_t *>() = W.Value;
170   OnWriteComplete(Error::success());
171 }
172 
173 void InProcessMemoryAccess::writeUInt64sAsync(
174     ArrayRef<tpctypes::UInt64Write> Ws, WriteResultFn OnWriteComplete) {
175   for (auto &W : Ws)
176     *W.Addr.toPtr<uint64_t *>() = W.Value;
177   OnWriteComplete(Error::success());
178 }
179 
180 void InProcessMemoryAccess::writeBuffersAsync(
181     ArrayRef<tpctypes::BufferWrite> Ws, WriteResultFn OnWriteComplete) {
182   for (auto &W : Ws)
183     memcpy(W.Addr.toPtr<char *>(), W.Buffer.data(), W.Buffer.size());
184   OnWriteComplete(Error::success());
185 }
186 
187 void InProcessMemoryAccess::writePointersAsync(
188     ArrayRef<tpctypes::PointerWrite> Ws, WriteResultFn OnWriteComplete) {
189   if (IsArch64Bit) {
190     for (auto &W : Ws)
191       *W.Addr.toPtr<uint64_t *>() = W.Value.getValue();
192   } else {
193     for (auto &W : Ws)
194       *W.Addr.toPtr<uint32_t *>() = static_cast<uint32_t>(W.Value.getValue());
195   }
196 
197   OnWriteComplete(Error::success());
198 }
199 
200 shared::CWrapperFunctionResult
201 SelfExecutorProcessControl::jitDispatchViaWrapperFunctionManager(
202     void *Ctx, const void *FnTag, const char *Data, size_t Size) {
203 
204   LLVM_DEBUG({
205     dbgs() << "jit-dispatch call with tag " << FnTag << " and " << Size
206            << " byte payload.\n";
207   });
208 
209   std::promise<shared::WrapperFunctionResult> ResultP;
210   auto ResultF = ResultP.get_future();
211   static_cast<SelfExecutorProcessControl *>(Ctx)
212       ->getExecutionSession()
213       .runJITDispatchHandler(
214           [ResultP = std::move(ResultP)](
215               shared::WrapperFunctionResult Result) mutable {
216             ResultP.set_value(std::move(Result));
217           },
218           ExecutorAddr::fromPtr(FnTag), {Data, Size});
219 
220   return ResultF.get().release();
221 }
222 
223 } // end namespace orc
224 } // end namespace llvm
225