1 //===------- SimpleEPCServer.cpp - EPC over simple abstract channel -------===//
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/TargetProcess/SimpleRemoteEPCServer.h"
10 
11 #include "llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h"
12 #include "llvm/ExecutionEngine/Orc/TargetProcess/TargetExecutionUtils.h"
13 #include "llvm/Support/FormatVariadic.h"
14 #include "llvm/Support/Host.h"
15 #include "llvm/Support/Process.h"
16 
17 #define DEBUG_TYPE "orc"
18 
19 using namespace llvm::orc::shared;
20 
21 namespace llvm {
22 namespace orc {
23 
24 static llvm::orc::shared::detail::CWrapperFunctionResult
25 reserveWrapper(const char *ArgData, size_t ArgSize) {
26   return WrapperFunction<SPSOrcTargetProcessAllocate>::handle(
27              ArgData, ArgSize,
28              [](uint64_t Size) -> Expected<ExecutorAddress> {
29                std::error_code EC;
30                auto MB = sys::Memory::allocateMappedMemory(
31                    Size, 0, sys::Memory::MF_READ | sys::Memory::MF_WRITE, EC);
32                if (EC)
33                  return errorCodeToError(EC);
34                return ExecutorAddress::fromPtr(MB.base());
35              })
36       .release();
37 }
38 
39 static llvm::orc::shared::detail::CWrapperFunctionResult
40 finalizeWrapper(const char *ArgData, size_t ArgSize) {
41   return WrapperFunction<SPSOrcTargetProcessFinalize>::handle(
42              ArgData, ArgSize,
43              [](const tpctypes::FinalizeRequest &FR) -> Error {
44                for (auto &Seg : FR) {
45                  char *Mem = Seg.Addr.toPtr<char *>();
46                  memcpy(Mem, Seg.Content.data(), Seg.Content.size());
47                  memset(Mem + Seg.Content.size(), 0,
48                         Seg.Size - Seg.Content.size());
49                  assert(Seg.Size <= std::numeric_limits<size_t>::max());
50                  if (auto EC = sys::Memory::protectMappedMemory(
51                          {Mem, static_cast<size_t>(Seg.Size)},
52                          tpctypes::fromWireProtectionFlags(Seg.Prot)))
53                    return errorCodeToError(EC);
54                  if (Seg.Prot & tpctypes::WPF_Exec)
55                    sys::Memory::InvalidateInstructionCache(Mem, Seg.Size);
56                }
57                return Error::success();
58              })
59       .release();
60 }
61 
62 static llvm::orc::shared::detail::CWrapperFunctionResult
63 deallocateWrapper(const char *ArgData, size_t ArgSize) {
64   return WrapperFunction<SPSOrcTargetProcessDeallocate>::handle(
65              ArgData, ArgSize,
66              [](ExecutorAddress Base, uint64_t Size) -> Error {
67                sys::MemoryBlock MB(Base.toPtr<void *>(), Size);
68                if (auto EC = sys::Memory::releaseMappedMemory(MB))
69                  return errorCodeToError(EC);
70                return Error::success();
71              })
72       .release();
73 }
74 
75 template <typename WriteT, typename SPSWriteT>
76 static llvm::orc::shared::detail::CWrapperFunctionResult
77 writeUIntsWrapper(const char *ArgData, size_t ArgSize) {
78   return WrapperFunction<void(SPSSequence<SPSWriteT>)>::handle(
79              ArgData, ArgSize,
80              [](std::vector<WriteT> Ws) {
81                for (auto &W : Ws)
82                  *jitTargetAddressToPointer<decltype(W.Value) *>(W.Address) =
83                      W.Value;
84              })
85       .release();
86 }
87 
88 static llvm::orc::shared::detail::CWrapperFunctionResult
89 writeBuffersWrapper(const char *ArgData, size_t ArgSize) {
90   return WrapperFunction<void(SPSSequence<SPSMemoryAccessBufferWrite>)>::handle(
91              ArgData, ArgSize,
92              [](std::vector<tpctypes::BufferWrite> Ws) {
93                for (auto &W : Ws)
94                  memcpy(jitTargetAddressToPointer<char *>(W.Address),
95                         W.Buffer.data(), W.Buffer.size());
96              })
97       .release();
98 }
99 
100 static llvm::orc::shared::detail::CWrapperFunctionResult
101 runAsMainWrapper(const char *ArgData, size_t ArgSize) {
102   return WrapperFunction<SPSRunAsMainSignature>::handle(
103              ArgData, ArgSize,
104              [](ExecutorAddress MainAddr,
105                 std::vector<std::string> Args) -> int64_t {
106                return runAsMain(MainAddr.toPtr<int (*)(int, char *[])>(), Args);
107              })
108       .release();
109 }
110 
111 SimpleRemoteEPCServer::Dispatcher::~Dispatcher() {}
112 
113 #if LLVM_ENABLE_THREADS
114 void SimpleRemoteEPCServer::ThreadDispatcher::dispatch(
115     unique_function<void()> Work) {
116   {
117     std::lock_guard<std::mutex> Lock(DispatchMutex);
118     if (!Running)
119       return;
120     ++Outstanding;
121   }
122 
123   std::thread([this, Work = std::move(Work)]() mutable {
124     Work();
125     std::lock_guard<std::mutex> Lock(DispatchMutex);
126     --Outstanding;
127     OutstandingCV.notify_all();
128   }).detach();
129 }
130 
131 void SimpleRemoteEPCServer::ThreadDispatcher::shutdown() {
132   std::unique_lock<std::mutex> Lock(DispatchMutex);
133   Running = false;
134   OutstandingCV.wait(Lock, [this]() { return Outstanding == 0; });
135 }
136 #endif
137 
138 StringMap<ExecutorAddress> SimpleRemoteEPCServer::defaultBootstrapSymbols() {
139   StringMap<ExecutorAddress> DBS;
140 
141   DBS["__llvm_orc_memory_reserve"] = ExecutorAddress::fromPtr(&reserveWrapper);
142   DBS["__llvm_orc_memory_finalize"] =
143       ExecutorAddress::fromPtr(&finalizeWrapper);
144   DBS["__llvm_orc_memory_deallocate"] =
145       ExecutorAddress::fromPtr(&deallocateWrapper);
146   DBS["__llvm_orc_memory_write_uint8s"] = ExecutorAddress::fromPtr(
147       &writeUIntsWrapper<tpctypes::UInt8Write,
148                          shared::SPSMemoryAccessUInt8Write>);
149   DBS["__llvm_orc_memory_write_uint16s"] = ExecutorAddress::fromPtr(
150       &writeUIntsWrapper<tpctypes::UInt16Write,
151                          shared::SPSMemoryAccessUInt16Write>);
152   DBS["__llvm_orc_memory_write_uint32s"] = ExecutorAddress::fromPtr(
153       &writeUIntsWrapper<tpctypes::UInt32Write,
154                          shared::SPSMemoryAccessUInt32Write>);
155   DBS["__llvm_orc_memory_write_uint64s"] = ExecutorAddress::fromPtr(
156       &writeUIntsWrapper<tpctypes::UInt64Write,
157                          shared::SPSMemoryAccessUInt64Write>);
158   DBS["__llvm_orc_memory_write_buffers"] =
159       ExecutorAddress::fromPtr(&writeBuffersWrapper);
160   DBS["__llvm_orc_run_as_main"] = ExecutorAddress::fromPtr(&runAsMainWrapper);
161   DBS["__llvm_orc_load_dylib"] = ExecutorAddress::fromPtr(&loadDylibWrapper);
162   DBS["__llvm_orc_lookup_symbols"] =
163       ExecutorAddress::fromPtr(&lookupSymbolsWrapper);
164   return DBS;
165 }
166 
167 Expected<SimpleRemoteEPCTransportClient::HandleMessageAction>
168 SimpleRemoteEPCServer::handleMessage(SimpleRemoteEPCOpcode OpC, uint64_t SeqNo,
169                                      ExecutorAddress TagAddr,
170                                      SimpleRemoteEPCArgBytesVector ArgBytes) {
171   using UT = std::underlying_type_t<SimpleRemoteEPCOpcode>;
172   if (static_cast<UT>(OpC) > static_cast<UT>(SimpleRemoteEPCOpcode::LastOpC))
173     return make_error<StringError>("Unexpected opcode",
174                                    inconvertibleErrorCode());
175 
176   // TODO: Clean detach message?
177   switch (OpC) {
178   case SimpleRemoteEPCOpcode::Setup:
179     return make_error<StringError>("Unexpected Setup opcode",
180                                    inconvertibleErrorCode());
181   case SimpleRemoteEPCOpcode::Hangup:
182     return SimpleRemoteEPCTransportClient::EndSession;
183   case SimpleRemoteEPCOpcode::Result:
184     if (auto Err = handleResult(SeqNo, TagAddr, std::move(ArgBytes)))
185       return std::move(Err);
186     break;
187   case SimpleRemoteEPCOpcode::CallWrapper:
188     handleCallWrapper(SeqNo, TagAddr, std::move(ArgBytes));
189     break;
190   }
191   return ContinueSession;
192 }
193 
194 Error SimpleRemoteEPCServer::waitForDisconnect() {
195   std::unique_lock<std::mutex> Lock(ServerStateMutex);
196   ShutdownCV.wait(Lock, [this]() { return RunState == ServerShutDown; });
197   return std::move(ShutdownErr);
198 }
199 
200 void SimpleRemoteEPCServer::handleDisconnect(Error Err) {
201   PendingJITDispatchResultsMap TmpPending;
202 
203   {
204     std::lock_guard<std::mutex> Lock(ServerStateMutex);
205     std::swap(TmpPending, PendingJITDispatchResults);
206     RunState = ServerShuttingDown;
207   }
208 
209   // Send out-of-band errors to any waiting threads.
210   for (auto &KV : TmpPending)
211     KV.second->set_value(
212         shared::WrapperFunctionResult::createOutOfBandError("disconnecting"));
213 
214   // TODO: Free attached resources.
215   // 1. Close libraries in DylibHandles.
216 
217   // Wait for dispatcher to clear.
218   D->shutdown();
219 
220   std::lock_guard<std::mutex> Lock(ServerStateMutex);
221   ShutdownErr = joinErrors(std::move(ShutdownErr), std::move(Err));
222   RunState = ServerShutDown;
223   ShutdownCV.notify_all();
224 }
225 
226 Error SimpleRemoteEPCServer::sendSetupMessage(
227     StringMap<ExecutorAddress> BootstrapSymbols) {
228 
229   using namespace SimpleRemoteEPCDefaultBootstrapSymbolNames;
230 
231   std::vector<char> SetupPacket;
232   SimpleRemoteEPCExecutorInfo EI;
233   EI.TargetTriple = sys::getProcessTriple();
234   if (auto PageSize = sys::Process::getPageSize())
235     EI.PageSize = *PageSize;
236   else
237     return PageSize.takeError();
238   EI.BootstrapSymbols = std::move(BootstrapSymbols);
239 
240   assert(!EI.BootstrapSymbols.count(ExecutorSessionObjectName) &&
241          "Dispatch context name should not be set");
242   assert(!EI.BootstrapSymbols.count(DispatchFnName) &&
243          "Dispatch function name should not be set");
244   EI.BootstrapSymbols[ExecutorSessionObjectName] =
245       ExecutorAddress::fromPtr(this);
246   EI.BootstrapSymbols[DispatchFnName] =
247       ExecutorAddress::fromPtr(jitDispatchEntry);
248 
249   using SPSSerialize =
250       shared::SPSArgList<shared::SPSSimpleRemoteEPCExecutorInfo>;
251   auto SetupPacketBytes =
252       shared::WrapperFunctionResult::allocate(SPSSerialize::size(EI));
253   shared::SPSOutputBuffer OB(SetupPacketBytes.data(), SetupPacketBytes.size());
254   if (!SPSSerialize::serialize(OB, EI))
255     return make_error<StringError>("Could not send setup packet",
256                                    inconvertibleErrorCode());
257 
258   return T->sendMessage(SimpleRemoteEPCOpcode::Setup, 0, ExecutorAddress(),
259                         {SetupPacketBytes.data(), SetupPacketBytes.size()});
260 }
261 
262 Error SimpleRemoteEPCServer::handleResult(
263     uint64_t SeqNo, ExecutorAddress TagAddr,
264     SimpleRemoteEPCArgBytesVector ArgBytes) {
265   std::promise<shared::WrapperFunctionResult> *P = nullptr;
266   {
267     std::lock_guard<std::mutex> Lock(ServerStateMutex);
268     auto I = PendingJITDispatchResults.find(SeqNo);
269     if (I == PendingJITDispatchResults.end())
270       return make_error<StringError>("No call for sequence number " +
271                                          Twine(SeqNo),
272                                      inconvertibleErrorCode());
273     P = I->second;
274     PendingJITDispatchResults.erase(I);
275     releaseSeqNo(SeqNo);
276   }
277   auto R = shared::WrapperFunctionResult::allocate(ArgBytes.size());
278   memcpy(R.data(), ArgBytes.data(), ArgBytes.size());
279   P->set_value(std::move(R));
280   return Error::success();
281 }
282 
283 void SimpleRemoteEPCServer::handleCallWrapper(
284     uint64_t RemoteSeqNo, ExecutorAddress TagAddr,
285     SimpleRemoteEPCArgBytesVector ArgBytes) {
286   D->dispatch([this, RemoteSeqNo, TagAddr, ArgBytes = std::move(ArgBytes)]() {
287     using WrapperFnTy =
288         shared::detail::CWrapperFunctionResult (*)(const char *, size_t);
289     auto *Fn = TagAddr.toPtr<WrapperFnTy>();
290     shared::WrapperFunctionResult ResultBytes(
291         Fn(ArgBytes.data(), ArgBytes.size()));
292     if (auto Err = T->sendMessage(SimpleRemoteEPCOpcode::Result, RemoteSeqNo,
293                                   ExecutorAddress(),
294                                   {ResultBytes.data(), ResultBytes.size()}))
295       ReportError(std::move(Err));
296   });
297 }
298 
299 shared::detail::CWrapperFunctionResult
300 SimpleRemoteEPCServer::loadDylibWrapper(const char *ArgData, size_t ArgSize) {
301   return shared::WrapperFunction<shared::SPSLoadDylibSignature>::handle(
302              ArgData, ArgSize,
303              [](ExecutorAddress ExecutorSessionObj, std::string Path,
304                 uint64_t Flags) -> Expected<uint64_t> {
305                return ExecutorSessionObj.toPtr<SimpleRemoteEPCServer *>()
306                    ->loadDylib(Path, Flags);
307              })
308       .release();
309 }
310 
311 shared::detail::CWrapperFunctionResult
312 SimpleRemoteEPCServer::lookupSymbolsWrapper(const char *ArgData,
313                                             size_t ArgSize) {
314   return shared::WrapperFunction<shared::SPSLookupSymbolsSignature>::handle(
315              ArgData, ArgSize,
316              [](ExecutorAddress ExecutorSessionObj,
317                 std::vector<RemoteSymbolLookup> Lookup) {
318                return ExecutorSessionObj.toPtr<SimpleRemoteEPCServer *>()
319                    ->lookupSymbols(Lookup);
320              })
321       .release();
322 }
323 
324 Expected<tpctypes::DylibHandle>
325 SimpleRemoteEPCServer::loadDylib(const std::string &Path, uint64_t Mode) {
326   std::string ErrMsg;
327   const char *P = Path.empty() ? nullptr : Path.c_str();
328   auto DL = sys::DynamicLibrary::getPermanentLibrary(P, &ErrMsg);
329   if (!DL.isValid())
330     return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
331   std::lock_guard<std::mutex> Lock(ServerStateMutex);
332   uint64_t Id = Dylibs.size();
333   Dylibs.push_back(std::move(DL));
334   return Id;
335 }
336 
337 Expected<std::vector<std::vector<ExecutorAddress>>>
338 SimpleRemoteEPCServer::lookupSymbols(const std::vector<RemoteSymbolLookup> &L) {
339   std::vector<std::vector<ExecutorAddress>> Result;
340 
341   for (const auto &E : L) {
342     if (E.H >= Dylibs.size())
343       return make_error<StringError>("Unrecognized handle",
344                                      inconvertibleErrorCode());
345     auto &DL = Dylibs[E.H];
346     Result.push_back({});
347 
348     for (const auto &Sym : E.Symbols) {
349 
350       const char *DemangledSymName = Sym.Name.c_str();
351 #ifdef __APPLE__
352       if (*DemangledSymName == '_')
353         ++DemangledSymName;
354 #endif
355 
356       void *Addr = DL.getAddressOfSymbol(DemangledSymName);
357       if (!Addr && Sym.Required)
358         return make_error<StringError>(Twine("Missing definition for ") +
359                                            DemangledSymName,
360                                        inconvertibleErrorCode());
361 
362       Result.back().push_back(ExecutorAddress::fromPtr(Addr));
363     }
364   }
365 
366   return std::move(Result);
367 }
368 
369 shared::WrapperFunctionResult
370 SimpleRemoteEPCServer::doJITDispatch(const void *FnTag, const char *ArgData,
371                                      size_t ArgSize) {
372   uint64_t SeqNo;
373   std::promise<shared::WrapperFunctionResult> ResultP;
374   auto ResultF = ResultP.get_future();
375   {
376     std::lock_guard<std::mutex> Lock(ServerStateMutex);
377     if (RunState != ServerRunning)
378       return shared::WrapperFunctionResult::createOutOfBandError(
379           "jit_dispatch not available (EPC server shut down)");
380 
381     SeqNo = getNextSeqNo();
382     assert(!PendingJITDispatchResults.count(SeqNo) && "SeqNo already in use");
383     PendingJITDispatchResults[SeqNo] = &ResultP;
384   }
385 
386   if (auto Err =
387           T->sendMessage(SimpleRemoteEPCOpcode::CallWrapper, SeqNo,
388                          ExecutorAddress::fromPtr(FnTag), {ArgData, ArgSize}))
389     ReportError(std::move(Err));
390 
391   return ResultF.get();
392 }
393 
394 shared::detail::CWrapperFunctionResult
395 SimpleRemoteEPCServer::jitDispatchEntry(void *DispatchCtx, const void *FnTag,
396                                         const char *ArgData, size_t ArgSize) {
397   return reinterpret_cast<SimpleRemoteEPCServer *>(DispatchCtx)
398       ->doJITDispatch(FnTag, ArgData, ArgSize)
399       .release();
400 }
401 
402 } // end namespace orc
403 } // end namespace llvm
404