1//===--- SimpleExecutorDylibManager.cpp - Executor-side dylib management --===//
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/SimpleExecutorDylibManager.h"
10
11#include "llvm/ExecutionEngine/Orc/Shared/SPSCI/NativeDylibManagerSPSCI.h"
12
13#include "llvm/Support/MSVCErrorWorkarounds.h"
14
15#include <future>
16
17#define DEBUG_TYPE "orc"
18
19namespace llvm {
20namespace orc {
21namespace rt_bootstrap {
22
23SimpleExecutorDylibManager::~SimpleExecutorDylibManager() {
24 assert(Dylibs.empty() && "shutdown not called?");
25}
26
27Expected<tpctypes::DylibHandle>
28SimpleExecutorDylibManager::open(const std::string &Path, uint64_t Mode) {
29 if (Mode != 0)
30 return make_error<StringError>(Args: "open: non-zero mode bits not yet supported",
31 Args: inconvertibleErrorCode());
32
33 const char *PathCStr = Path.empty() ? nullptr : Path.c_str();
34 std::string ErrMsg;
35
36 auto DL = sys::DynamicLibrary::getPermanentLibrary(filename: PathCStr, errMsg: &ErrMsg);
37 if (!DL.isValid())
38 return make_error<StringError>(Args: std::move(ErrMsg), Args: inconvertibleErrorCode());
39
40 std::lock_guard<std::mutex> Lock(M);
41 auto H = ExecutorAddr::fromPtr(Ptr: DL.getOSSpecificHandle());
42 Resolvers.push_back(x: std::make_unique<DylibSymbolResolver>(args&: H));
43 Dylibs.insert(V: DL.getOSSpecificHandle());
44 return ExecutorAddr::fromPtr(Ptr: Resolvers.back().get());
45}
46
47ExecutorResolver::ResolveResult
48SimpleExecutorDylibManager::resolve(ExecutorAddr Resolver,
49 RemoteSymbolLookupSet Lookup) {
50 using TmpResult = MSVCPExpected<std::vector<std::optional<ExecutorAddr>>>;
51 std::promise<TmpResult> P;
52 auto F = P.get_future();
53 Resolver.toPtr<ExecutorResolver *>()->resolveAsync(
54 L: std::move(Lookup), OnResolve: [&](TmpResult R) { P.set_value(std::move(R)); });
55 return F.get();
56}
57
58Error SimpleExecutorDylibManager::shutdown() {
59
60 DylibSet DS;
61 {
62 std::lock_guard<std::mutex> Lock(M);
63 std::swap(a&: DS, b&: Dylibs);
64 }
65
66 // There is no removal of dylibs at the moment, so nothing to do here.
67 return Error::success();
68}
69
70void SimpleExecutorDylibManager::addBootstrapSymbols(
71 StringMap<ExecutorAddr> &M) {
72 // SimpleExecutorDylibManager is the LLVM-side implementation of the runtime's
73 // NativeDylibManager controller interface, so it publishes its bootstrap
74 // symbols under the NativeDylibManager_* names. The class itself will be
75 // renamed to NativeDylibManager to match in a future cleanup.
76 M[rt::sps_ci::NativeDylibManagerInstanceName] = ExecutorAddr::fromPtr(Ptr: this);
77 M[rt::sps_ci::DylibMgrOpen::Name] = ExecutorAddr::fromPtr(Ptr: &openWrapper);
78 M[rt::sps_ci::DylibMgrResolve::Name] = ExecutorAddr::fromPtr(Ptr: &resolveWrapper);
79}
80
81llvm::orc::shared::CWrapperFunctionBuffer
82SimpleExecutorDylibManager::openWrapper(const char *ArgData, size_t ArgSize) {
83 return shared::WrapperFunction<rt::sps_ci::DylibMgrOpen::SPSSig>::handle(
84 ArgData, ArgSize,
85 Handler: shared::makeMethodWrapperHandler(
86 Method: &SimpleExecutorDylibManager::open))
87 .release();
88}
89
90llvm::orc::shared::CWrapperFunctionBuffer
91SimpleExecutorDylibManager::resolveWrapper(const char *ArgData,
92 size_t ArgSize) {
93 return shared::WrapperFunction<rt::sps_ci::DylibMgrResolve::SPSSig>::handle(
94 ArgData, ArgSize,
95 Handler: shared::makeMethodWrapperHandler(
96 Method: &SimpleExecutorDylibManager::resolve))
97 .release();
98}
99
100} // namespace rt_bootstrap
101} // end namespace orc
102} // end namespace llvm
103