LLVM 23.0.0git
SelfExecutorProcessControl.cpp
Go to the documentation of this file.
1//===------ SelfExecutorProcessControl.cpp -- EPC for in-process JITs -----===//
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
10
17
18#define DEBUG_TYPE "orc"
19
20namespace llvm::orc {
21
23 std::shared_ptr<SymbolStringPool> SSP, std::unique_ptr<TaskDispatcher> D,
25 std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr)
27 IPMA(TargetTriple.isArch64Bit()) {
28
29 OwnedMemMgr = std::move(MemMgr);
30 if (!OwnedMemMgr)
31 OwnedMemMgr = std::make_unique<jitlink::InProcessMemoryManager>(
33
34 this->TargetTriple = std::move(TargetTriple);
35 this->PageSize = PageSize;
36 this->MemMgr = OwnedMemMgr.get();
37 this->MemAccess = &IPMA;
38 this->JDI = {ExecutorAddr::fromPtr(jitDispatchViaWrapperFunctionManager),
40
42
43#ifdef __APPLE__
44 // FIXME: Don't add an UnwindInfoManager by default -- it's redundant when
45 // the ORC runtime is loaded. We'll need a way to document this and
46 // allow clients to choose.
49#endif // __APPLE__
50}
51
54 std::shared_ptr<SymbolStringPool> SSP, std::unique_ptr<TaskDispatcher> D,
55 std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr) {
56
57 if (!SSP)
58 SSP = std::make_shared<SymbolStringPool>();
59
60 if (!D)
61 D = std::make_unique<InPlaceTaskDispatcher>();
62
64 if (!PageSize)
65 return PageSize.takeError();
66
68
69 return std::make_unique<SelfExecutorProcessControl>(
70 std::move(SSP), std::move(D), std::move(TT), *PageSize,
71 std::move(MemMgr));
72}
73
77 using MainTy = int (*)(int, char *[]);
78 return orc::runAsMain(MainFnAddr.toPtr<MainTy>(), Args);
79}
80
83 using VoidTy = int (*)();
84 return orc::runAsVoidFunction(VoidFnAddr.toPtr<VoidTy>());
85}
86
89 using IntTy = int (*)(int);
90 return orc::runAsIntFunction(IntFnAddr.toPtr<IntTy>(), Arg);
91}
92
94 IncomingWFRHandler SendResult,
95 ArrayRef<char> ArgBuffer) {
96 using WrapperFnTy =
97 shared::CWrapperFunctionBuffer (*)(const char *Data, size_t Size);
98 auto *WrapperFn = WrapperFnAddr.toPtr<WrapperFnTy>();
100 WrapperFn(ArgBuffer.data(), ArgBuffer.size())));
101}
102
104 D->shutdown();
105 return Error::success();
106}
107
110 char Prefix = TargetTriple.isOSBinFormatMachO() ? '_' : '\0';
111 return std::make_unique<InProcessDylibManager>(Prefix);
112}
113
115SelfExecutorProcessControl::jitDispatchViaWrapperFunctionManager(
116 void *Ctx, const void *FnTag, const char *Data, size_t Size) {
117
118 LLVM_DEBUG({
119 dbgs() << "jit-dispatch call with tag " << FnTag << " and " << Size
120 << " byte payload.\n";
121 });
122
123 std::promise<shared::WrapperFunctionBuffer> ResultP;
124 auto ResultF = ResultP.get_future();
125 static_cast<SelfExecutorProcessControl *>(Ctx)
128 [ResultP = std::move(ResultP)](
129 shared::WrapperFunctionBuffer Result) mutable {
130 ResultP.set_value(std::move(Result));
131 },
134
135 return ResultF.get().release();
136}
137
138SelfExecutorProcessControl::InProcessDylibManager::InProcessDylibManager(
139 char GlobalManglingPrefix)
140 : GlobalManglingPrefix(GlobalManglingPrefix) {}
141
142Expected<tpctypes::DylibHandle>
143SelfExecutorProcessControl::InProcessDylibManager::loadDylib(
144 const char *DylibPath) {
145 std::string ErrMsg;
146 auto Dylib = sys::DynamicLibrary::getPermanentLibrary(DylibPath, &ErrMsg);
147 if (!Dylib.isValid())
148 return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
149 return ExecutorAddr::fromPtr(Dylib.getOSSpecificHandle());
150}
151
152void SelfExecutorProcessControl::InProcessDylibManager::lookupSymbolsAsync(
155 std::vector<tpctypes::LookupResult> R;
156
157 for (auto &Elem : Request) {
158 sys::DynamicLibrary Dylib(Elem.Handle.toPtr<void *>());
159 R.push_back(tpctypes::LookupResult());
160 for (auto &KV : Elem.Symbols) {
161 auto &Sym = KV.first;
162 std::string Tmp((*Sym).data() + !!GlobalManglingPrefix,
163 (*Sym).size() - !!GlobalManglingPrefix);
164 void *Addr = Dylib.getAddressOfSymbol(Tmp.c_str());
165 if (!Addr && KV.second == SymbolLookupFlags::RequiredSymbol)
166 R.back().emplace_back();
167 else
168 // FIXME: determine accurate JITSymbolFlags.
169 R.back().emplace_back(ExecutorSymbolDef(ExecutorAddr::fromPtr(Addr),
171 }
172 }
173 Complete(std::move(R));
174}
175
176} // namespace llvm::orc
Provides a library for accessing information about this process and other processes on the operating ...
#define LLVM_DEBUG(...)
Definition Debug.h:114
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
const T * data() const
Definition ArrayRef.h:139
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
Tagged union holding either a T or a Error.
Definition Error.h:485
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
unique_function< void(Expected< std::vector< tpctypes::LookupResult > >)> SymbolLookupCompleteFn
LLVM_ABI void runJITDispatchHandler(SendResultFunction SendResult, ExecutorAddr HandlerFnTagAddr, shared::WrapperFunctionBuffer ArgBytes)
Run a registered jit-side wrapper function.
Definition Core.cpp:1924
Represents an address in the executor process.
static ExecutorAddr fromPtr(T *Ptr, UnwrapFn &&Unwrap=UnwrapFn())
Create an ExecutorAddr from the given pointer.
std::enable_if_t< std::is_pointer< T >::value, T > toPtr(WrapFn &&Wrap=WrapFn()) const
Cast this ExecutorAddr to a pointer of the given type.
A handler or incoming WrapperFunctionBuffers – either return values from callWrapper* calls,...
std::unique_ptr< TaskDispatcher > D
std::shared_ptr< SymbolStringPool > SSP
StringMap< ExecutorAddr > BootstrapSymbols
StringMap< std::vector< char > > BootstrapMap
jitlink::JITLinkMemoryManager * MemMgr
ExecutionSession & getExecutionSession()
Return the ExecutionSession associated with this instance.
ExecutorProcessControl(std::shared_ptr< SymbolStringPool > SSP, std::unique_ptr< TaskDispatcher > D)
SelfExecutorProcessControl(std::shared_ptr< SymbolStringPool > SSP, std::unique_ptr< TaskDispatcher > D, Triple TargetTriple, unsigned PageSize, std::unique_ptr< jitlink::JITLinkMemoryManager > MemMgr)
Error disconnect() override
Disconnect from the target process.
Expected< int32_t > runAsVoidFunction(ExecutorAddr VoidFnAddr) override
Run function with a int (*)(void) signature.
Expected< int32_t > runAsMain(ExecutorAddr MainFnAddr, ArrayRef< std::string > Args) override
Run function with a main-like signature.
void callWrapperAsync(ExecutorAddr WrapperFnAddr, IncomingWFRHandler OnComplete, ArrayRef< char > ArgBuffer) override
Run a wrapper function in the executor.
Expected< std::unique_ptr< DylibManager > > createDefaultDylibMgr() override
Create a default DylibManager for the target process.
static Expected< std::unique_ptr< SelfExecutorProcessControl > > Create(std::shared_ptr< SymbolStringPool > SSP=nullptr, std::unique_ptr< TaskDispatcher > D=nullptr, std::unique_ptr< jitlink::JITLinkMemoryManager > MemMgr=nullptr)
Create a SelfExecutorProcessControl with the given symbol string pool and memory manager.
Expected< int32_t > runAsIntFunction(ExecutorAddr IntFnAddr, int Arg) override
Run function with a int (*)(int) signature.
static LLVM_ABI bool TryEnable()
If the libunwind find-dynamic-unwind-info callback registration APIs are available then this method w...
static LLVM_ABI void addBootstrapSymbols(StringMap< ExecutorAddr > &M)
C++ wrapper function buffer: Same as CWrapperFunctionBuffer but auto-releases memory.
static WrapperFunctionBuffer copyFrom(const char *Source, size_t Size)
Copy from the given char range.
static LLVM_ABI DynamicLibrary getPermanentLibrary(const char *filename, std::string *errMsg=nullptr)
This function permanently loads the dynamic library at the given path using the library load operatio...
static LLVM_ABI Expected< unsigned > getPageSize()
Get the process's page size.
static unsigned getPageSizeEstimate()
Get the process's estimated page size.
Definition Process.h:62
std::vector< std::optional< ExecutorSymbolDef > > LookupResult
LLVM_ABI void addDefaultBootstrapValuesForHostProcess(StringMap< std::vector< char > > &BootstrapMap, StringMap< ExecutorAddr > &BootstrapSymbols)
LLVM_ABI int runAsVoidFunction(int(*Func)(void))
LLVM_ABI int runAsIntFunction(int(*Func)(int), int Arg)
LLVM_ABI int runAsMain(int(*Main)(int, char *[]), ArrayRef< std::string > Args, std::optional< StringRef > ProgramName=std::nullopt)
Run a main function, returning the result.
LLVM_ABI std::string getProcessTriple()
getProcessTriple() - Return an appropriate target triple for generating code to be loaded into the cu...
Definition Host.cpp:2576
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:94
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:221
ArrayRef(const T &OneElt) -> ArrayRef< T >
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1917
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:870