LLVM 24.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
22
23#define DEBUG_TYPE "orc"
24
25namespace llvm::orc {
26
28public:
29 InProcessDylibManager(char GlobalManglingPrefix);
30 Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) override;
31 void
33 DylibManager::SymbolLookupCompleteFn Complete) override;
34
35private:
36 char GlobalManglingPrefix;
37};
38
40 std::shared_ptr<SymbolStringPool> SSP, std::unique_ptr<TaskDispatcher> D,
43
44 this->TargetTriple = std::move(TargetTriple);
45 this->PageSize = PageSize;
46
49
51 ExecutorAddr::fromPtr(jitDispatchViaWrapperFunctionManager);
53
54#ifdef __APPLE__
55 // FIXME: Don't add an UnwindInfoManager by default -- it's redundant when
56 // the ORC runtime is loaded. We'll need a way to document this and
57 // allow clients to choose.
60#endif // __APPLE__
61}
62
64SelfExecutorProcessControl::Create(std::shared_ptr<SymbolStringPool> SSP,
65 std::unique_ptr<TaskDispatcher> D) {
66
67 if (!SSP)
68 SSP = std::make_shared<SymbolStringPool>();
69
70 if (!D)
71 D = std::make_unique<InPlaceTaskDispatcher>();
72
74 if (!PageSize)
75 return PageSize.takeError();
76
78
79 return std::make_unique<SelfExecutorProcessControl>(
80 std::move(SSP), std::move(D), std::move(TT), *PageSize);
81}
82
86 using MainTy = int (*)(int, char *[]);
87 return orc::runAsMain(MainFnAddr.toPtr<MainTy>(), Args);
88}
89
91 IncomingWFRHandler SendResult,
92 ArrayRef<char> ArgBuffer) {
93 using WrapperFnTy =
94 shared::CWrapperFunctionBuffer (*)(const char *Data, size_t Size);
95 auto *WrapperFn = WrapperFnAddr.toPtr<WrapperFnTy>();
97 WrapperFn(ArgBuffer.data(), ArgBuffer.size())));
98}
99
101 D->shutdown();
102 return Error::success();
103}
104
107 return std::make_unique<jitlink::InProcessMemoryManager>(
109}
110
113 char Prefix = TargetTriple.isOSBinFormatMachO() ? '_' : '\0';
114 return std::make_unique<InProcessDylibManager>(Prefix);
115}
116
119 return std::make_unique<InProcessMemoryAccess>(TargetTriple.isArch64Bit());
120}
121
123SelfExecutorProcessControl::jitDispatchViaWrapperFunctionManager(
124 void *Ctx, const void *FnTag, const char *Data, size_t Size) {
125
126 LLVM_DEBUG({
127 dbgs() << "jit-dispatch call with tag " << FnTag << " and " << Size
128 << " byte payload.\n";
129 });
130
131 std::promise<shared::WrapperFunctionBuffer> ResultP;
132 auto ResultF = ResultP.get_future();
133 static_cast<SelfExecutorProcessControl *>(Ctx)
136 [ResultP = std::move(ResultP)](
137 shared::WrapperFunctionBuffer Result) mutable {
138 ResultP.set_value(std::move(Result));
139 },
142
143 return ResultF.get().release();
144}
145
147 char GlobalManglingPrefix)
148 : GlobalManglingPrefix(GlobalManglingPrefix) {}
149
152 const char *DylibPath) {
153 std::string ErrMsg;
154 auto Dylib = sys::DynamicLibrary::getPermanentLibrary(DylibPath, &ErrMsg);
155 if (!Dylib.isValid())
156 return make_error<StringError>(std::move(ErrMsg), inconvertibleErrorCode());
157 return ExecutorAddr::fromPtr(Dylib.getOSSpecificHandle());
158}
159
164
165 sys::DynamicLibrary Dylib(H.toPtr<void *>());
166 for (auto &KV : Symbols) {
167 auto &Sym = KV.first;
168 std::string Tmp((*Sym).data() + !!GlobalManglingPrefix,
169 (*Sym).size() - !!GlobalManglingPrefix);
170 void *Addr = Dylib.getAddressOfSymbol(Tmp.c_str());
171 if (!Addr && KV.second == SymbolLookupFlags::RequiredSymbol)
172 R.emplace_back();
173 else
174 R.emplace_back(ExecutorAddr::fromPtr(Addr));
175 }
176 Complete(std::move(R));
177}
178
179} // namespace llvm::orc
#define H(x, y, z)
Definition MD5.cpp:56
Provides a library for accessing information about this process and other processes on the operating ...
#define LLVM_DEBUG(...)
Definition Debug.h:119
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
const T * data() const
Definition ArrayRef.h:138
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:48
unique_function< void(Expected< tpctypes::LookupResult >)> SymbolLookupCompleteFn
LLVM_ABI void runJITDispatchHandler(SendResultFunction SendResult, ExecutorAddr HandlerFnTagAddr, shared::WrapperFunctionBuffer ArgBytes)
Run a registered jit-side wrapper function.
Definition Core.cpp:1898
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
ExecutionSession & getExecutionSession()
Return the ExecutionSession associated with this instance.
ExecutorProcessControl(std::shared_ptr< SymbolStringPool > SSP, std::unique_ptr< TaskDispatcher > D)
void lookupSymbolsAsync(tpctypes::DylibHandle H, const SymbolLookupSet &Symbols, DylibManager::SymbolLookupCompleteFn Complete) override
Search for symbols in the target process.
Expected< tpctypes::DylibHandle > loadDylib(const char *DylibPath) override
Load the dynamic library at the given path and return a handle to it.
SelfExecutorProcessControl(std::shared_ptr< SymbolStringPool > SSP, std::unique_ptr< TaskDispatcher > D, Triple TargetTriple, unsigned PageSize)
Expected< std::unique_ptr< jitlink::JITLinkMemoryManager > > createDefaultMemoryManager() override
Create a default JITLinkMemoryManager for the target process.
static Expected< std::unique_ptr< SelfExecutorProcessControl > > Create(std::shared_ptr< SymbolStringPool > SSP=nullptr, std::unique_ptr< TaskDispatcher > D=nullptr)
Create a SelfExecutorProcessControl with the given symbol string pool and memory manager.
Expected< std::unique_ptr< MemoryAccess > > createDefaultMemoryAccess() override
Create a default MemoryAccess for the target process.
Error disconnect() override
Disconnect from the target process.
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.
A set of symbols to look up, each associated with a SymbolLookupFlags value.
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.
This class provides a portable interface to dynamic libraries which also might be known as shared lib...
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...
LLVM_ABI void * getAddressOfSymbol(const char *symbolName)
Searches through the library for the symbol symbolName.
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
void addRunAsFunctionWrappersTo(StringMap< ExecutorAddr > &M)
Adds executor-side wrappers for the run-as function proxies.
LLVM_ABI const char *const DispatchCtxName
LLVM_ABI const char *const DispatchName
std::vector< std::optional< ExecutorAddr > > LookupResult
ExecutorAddr DylibHandle
A handle used to represent a loaded dylib in the target process.
LLVM_ABI void addDefaultBootstrapValuesForHostProcess(StringMap< std::vector< char > > &BootstrapMap, StringMap< ExecutorAddr > &BootstrapSymbols)
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:2653
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:209
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
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:878