LLVM 23.0.0git
ExecutorProcessControl.h
Go to the documentation of this file.
1//===- ExecutorProcessControl.h - Executor process control APIs -*- C++ -*-===//
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// Utilities for interacting with the executor processes.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_EXECUTIONENGINE_ORC_EXECUTORPROCESSCONTROL_H
14#define LLVM_EXECUTIONENGINE_ORC_EXECUTORPROCESSCONTROL_H
15
16#include "llvm/ADT/StringRef.h"
25
26#include <future>
27#include <mutex>
28#include <vector>
29
30namespace llvm::jitlink {
31
33
34} // namespace llvm::jitlink
35
36namespace llvm::orc {
37
38class DylibManager;
40class MemoryAccess;
41
42/// ExecutorProcessControl supports interaction with a JIT target process.
44 friend class ExecutionSession;
45public:
46
47 /// A handler or incoming WrapperFunctionBuffers -- either return values from
48 /// callWrapper* calls, or incoming JIT-dispatch requests.
49 ///
50 /// IncomingWFRHandlers are constructible from
51 /// unique_function<void(shared::WrapperFunctionBuffer)>s using the
52 /// runInPlace function or a RunWithDispatch object.
55 public:
56 IncomingWFRHandler() = default;
57 explicit operator bool() const { return !!H; }
58 void operator()(shared::WrapperFunctionBuffer WFR) { H(std::move(WFR)); }
59 private:
60 template <typename FnT> IncomingWFRHandler(FnT &&Fn)
61 : H(std::forward<FnT>(Fn)) {}
62
64 };
65
66 /// Constructs an IncomingWFRHandler from a function object that is callable
67 /// as void(shared::WrapperFunctionBuffer). The function object will be called
68 /// directly. This should be used with care as it may block listener threads
69 /// in remote EPCs. It is only suitable for simple tasks (e.g. setting a
70 /// future), or for performing some quick analysis before dispatching "real"
71 /// work as a Task.
72 class RunInPlace {
73 public:
74 template <typename FnT>
76 return IncomingWFRHandler(std::forward<FnT>(Fn));
77 }
78 };
79
80 /// Constructs an IncomingWFRHandler from a function object by creating a new
81 /// function object that dispatches the original using a TaskDispatcher,
82 /// wrapping the original as a GenericNamedTask.
83 ///
84 /// This is the default approach for running WFR handlers.
85 class RunAsTask {
86 public:
88
89 template <typename FnT>
91 return IncomingWFRHandler(
92 [&D = this->D, Fn = std::move(Fn)]
93 (shared::WrapperFunctionBuffer WFR) mutable {
94 D.dispatch(
96 [Fn = std::move(Fn), WFR = std::move(WFR)]() mutable {
97 Fn(std::move(WFR));
98 }, "WFR handler task"));
99 });
100 }
101 private:
103 };
104
105 /// Contains the address of the dispatch function and context that the ORC
106 /// runtime can use to call functions in the JIT.
111
112 ExecutorProcessControl(std::shared_ptr<SymbolStringPool> SSP,
113 std::unique_ptr<TaskDispatcher> D)
114 : SSP(std::move(SSP)), D(std::move(D)) {}
115
117
118 /// Return the ExecutionSession associated with this instance.
119 /// Not callable until the ExecutionSession has been associated.
121 assert(ES && "No ExecutionSession associated yet");
122 return *ES;
123 }
124
125 /// Intern a symbol name in the SymbolStringPool.
126 SymbolStringPtr intern(StringRef SymName) { return SSP->intern(SymName); }
127
128 /// Return a shared pointer to the SymbolStringPool for this instance.
129 std::shared_ptr<SymbolStringPool> getSymbolStringPool() const { return SSP; }
130
132
133 /// Return the Triple for the target process.
134 const Triple &getTargetTriple() const { return TargetTriple; }
135
136 /// Get the page size for the target process.
137 unsigned getPageSize() const { return PageSize; }
138
139 /// Get the JIT dispatch function and context address for the executor.
140 const JITDispatchInfo &getJITDispatchInfo() const { return JDI; }
141
142 /// Create a default JITLinkMemoryManager for the target process.
145
146 /// Create a default DylibManager for the target process.
148
149 /// Create a default MemoryAccess for the target process.
152
153 /// Returns the bootstrap map.
155 return BootstrapMap;
156 }
157
158 /// Look up and SPS-deserialize a bootstrap map value.
159 template <typename T, typename SPSTagT>
160 Error getBootstrapMapValue(StringRef Key, std::optional<T> &Val) const {
161 Val = std::nullopt;
162
163 auto I = BootstrapMap.find(Key);
164 if (I == BootstrapMap.end())
165 return Error::success();
166
167 T Tmp;
168 shared::SPSInputBuffer IB(I->second.data(), I->second.size());
170 return make_error<StringError>("Could not deserialize value for key " +
171 Key,
173
174 Val = std::move(Tmp);
175 return Error::success();
176 }
177
178 /// Returns the bootstrap symbol map.
182
183 /// For each (ExecutorAddr&, StringRef) pair, looks up the string in the
184 /// bootstrap symbols map and writes its address to the ExecutorAddr if
185 /// found. If any symbol is not found then the function returns an error.
187 ArrayRef<std::pair<ExecutorAddr &, StringRef>> Pairs) const {
188 for (const auto &KV : Pairs) {
189 auto I = BootstrapSymbols.find(KV.second);
190 if (I == BootstrapSymbols.end())
191 return make_error<StringError>("Symbol \"" + KV.second +
192 "\" not found "
193 "in bootstrap symbols map",
195
196 KV.first = I->second;
197 }
198 return Error::success();
199 }
200
201 /// Run function with a main-like signature.
203 ArrayRef<std::string> Args) = 0;
204
205 // TODO: move this to ORC runtime.
206 /// Run function with a int (*)(void) signature.
208
209 // TODO: move this to ORC runtime.
210 /// Run function with a int (*)(int) signature.
212 int Arg) = 0;
213
214 /// Run a wrapper function in the executor. The given WFRHandler will be
215 /// called on the result when it is returned.
216 ///
217 /// The wrapper function should be callable as:
218 ///
219 /// \code{.cpp}
220 /// CWrapperFunctionBuffer fn(uint8_t *Data, uint64_t Size);
221 /// \endcode{.cpp}
222 virtual void callWrapperAsync(ExecutorAddr WrapperFnAddr,
223 IncomingWFRHandler OnComplete,
224 ArrayRef<char> ArgBuffer) = 0;
225
226 /// Run a wrapper function in the executor using the given Runner to dispatch
227 /// OnComplete when the result is ready.
228 template <typename RunPolicyT, typename FnT>
229 void callWrapperAsync(RunPolicyT &&Runner, ExecutorAddr WrapperFnAddr,
230 FnT &&OnComplete, ArrayRef<char> ArgBuffer) {
232 WrapperFnAddr, Runner(std::forward<FnT>(OnComplete)), ArgBuffer);
233 }
234
235 /// Run a wrapper function in the executor. OnComplete will be dispatched
236 /// as a GenericNamedTask using this instance's TaskDispatch object.
237 template <typename FnT>
238 void callWrapperAsync(ExecutorAddr WrapperFnAddr, FnT &&OnComplete,
239 ArrayRef<char> ArgBuffer) {
240 callWrapperAsync(RunAsTask(*D), WrapperFnAddr,
241 std::forward<FnT>(OnComplete), ArgBuffer);
242 }
243
244 /// Run a wrapper function in the executor. The wrapper function should be
245 /// callable as:
246 ///
247 /// \code{.cpp}
248 /// CWrapperFunctionBuffer fn(uint8_t *Data, uint64_t Size);
249 /// \endcode{.cpp}
251 ArrayRef<char> ArgBuffer) {
252 std::promise<shared::WrapperFunctionBuffer> RP;
253 auto RF = RP.get_future();
255 RunInPlace(), WrapperFnAddr,
257 RP.set_value(std::move(R));
258 }, ArgBuffer);
259 return RF.get();
260 }
261
262 /// Run a wrapper function using SPS to serialize the arguments and
263 /// deserialize the results.
264 template <typename SPSSignature, typename RunPolicyT, typename SendResultT,
265 typename... ArgTs>
266 void callSPSWrapperAsync(RunPolicyT &&Runner, ExecutorAddr WrapperFnAddr,
267 SendResultT &&SendResult, const ArgTs &...Args) {
269 [this, WrapperFnAddr, Runner = std::move(Runner)]
270 (auto &&SendResult, const char *ArgData, size_t ArgSize) mutable {
271 this->callWrapperAsync(std::move(Runner), WrapperFnAddr,
272 std::move(SendResult),
273 ArrayRef<char>(ArgData, ArgSize));
274 },
275 std::forward<SendResultT>(SendResult), Args...);
276 }
277
278 /// Run a wrapper function using SPS to serialize the arguments and
279 /// deserialize the results.
280 template <typename SPSSignature, typename SendResultT, typename... ArgTs>
281 void callSPSWrapperAsync(ExecutorAddr WrapperFnAddr, SendResultT &&SendResult,
282 const ArgTs &...Args) {
284 std::forward<SendResultT>(SendResult),
285 Args...);
286 }
287
288 /// Run a wrapper function using SPS to serialize the arguments and
289 /// deserialize the results.
290 ///
291 /// If SPSSignature is a non-void function signature then the second argument
292 /// (the first in the Args list) should be a reference to a return value.
293 template <typename SPSSignature, typename... WrapperCallArgTs>
295 WrapperCallArgTs &&...WrapperCallArgs) {
297 [this, WrapperFnAddr](const char *ArgData, size_t ArgSize) {
298 return callWrapper(WrapperFnAddr, ArrayRef<char>(ArgData, ArgSize));
299 },
300 std::forward<WrapperCallArgTs>(WrapperCallArgs)...);
301 }
302
303 /// Disconnect from the target process.
304 ///
305 /// This should be called after the JIT session is shut down.
306 virtual Error disconnect() = 0;
307
308protected:
309
310 std::shared_ptr<SymbolStringPool> SSP;
311 std::unique_ptr<TaskDispatcher> D;
314 unsigned PageSize = 0;
318};
319
320} // namespace llvm::orc
321
322#endif // LLVM_EXECUTIONENGINE_ORC_EXECUTORPROCESSCONTROL_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition Compiler.h:213
#define I(x, y, z)
Definition MD5.cpp:57
#define H(x, y, z)
Definition MD5.cpp:56
#define T
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
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
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:133
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
An ExecutionSession represents a running JIT program.
Definition Core.h:1355
Represents an address in the executor process.
A handler or incoming WrapperFunctionBuffers – either return values from callWrapper* calls,...
Constructs an IncomingWFRHandler from a function object by creating a new function object that dispat...
Constructs an IncomingWFRHandler from a function object that is callable as void(shared::WrapperFunct...
void callWrapperAsync(ExecutorAddr WrapperFnAddr, FnT &&OnComplete, ArrayRef< char > ArgBuffer)
Run a wrapper function in the executor.
virtual Expected< int32_t > runAsIntFunction(ExecutorAddr IntFnAddr, int Arg)=0
Run function with a int (*)(int) signature.
void callSPSWrapperAsync(RunPolicyT &&Runner, ExecutorAddr WrapperFnAddr, SendResultT &&SendResult, const ArgTs &...Args)
Run a wrapper function using SPS to serialize the arguments and deserialize the results.
virtual Expected< int32_t > runAsMain(ExecutorAddr MainFnAddr, ArrayRef< std::string > Args)=0
Run function with a main-like signature.
Error getBootstrapMapValue(StringRef Key, std::optional< T > &Val) const
Look up and SPS-deserialize a bootstrap map value.
std::unique_ptr< TaskDispatcher > D
void callSPSWrapperAsync(ExecutorAddr WrapperFnAddr, SendResultT &&SendResult, const ArgTs &...Args)
Run a wrapper function using SPS to serialize the arguments and deserialize the results.
virtual void callWrapperAsync(ExecutorAddr WrapperFnAddr, IncomingWFRHandler OnComplete, ArrayRef< char > ArgBuffer)=0
Run a wrapper function in the executor.
virtual Expected< std::unique_ptr< MemoryAccess > > createDefaultMemoryAccess()=0
Create a default MemoryAccess for the target process.
const StringMap< std::vector< char > > & getBootstrapMap() const
Returns the bootstrap map.
virtual Expected< std::unique_ptr< DylibManager > > createDefaultDylibMgr()=0
Create a default DylibManager for the target process.
std::shared_ptr< SymbolStringPool > SSP
const Triple & getTargetTriple() const
Return the Triple for the target process.
StringMap< ExecutorAddr > BootstrapSymbols
SymbolStringPtr intern(StringRef SymName)
Intern a symbol name in the SymbolStringPool.
virtual Error disconnect()=0
Disconnect from the target process.
shared::WrapperFunctionBuffer callWrapper(ExecutorAddr WrapperFnAddr, ArrayRef< char > ArgBuffer)
Run a wrapper function in the executor.
StringMap< std::vector< char > > BootstrapMap
std::shared_ptr< SymbolStringPool > getSymbolStringPool() const
Return a shared pointer to the SymbolStringPool for this instance.
const StringMap< ExecutorAddr > & getBootstrapSymbolsMap() const
Returns the bootstrap symbol map.
virtual Expected< int32_t > runAsVoidFunction(ExecutorAddr VoidFnAddr)=0
Run function with a int (*)(void) signature.
unsigned getPageSize() const
Get the page size for the target process.
virtual Expected< std::unique_ptr< jitlink::JITLinkMemoryManager > > createDefaultMemoryManager()=0
Create a default JITLinkMemoryManager for the target process.
const JITDispatchInfo & getJITDispatchInfo() const
Get the JIT dispatch function and context address for the executor.
Error callSPSWrapper(ExecutorAddr WrapperFnAddr, WrapperCallArgTs &&...WrapperCallArgs)
Run a wrapper function using SPS to serialize the arguments and deserialize the results.
void callWrapperAsync(RunPolicyT &&Runner, ExecutorAddr WrapperFnAddr, FnT &&OnComplete, ArrayRef< char > ArgBuffer)
Run a wrapper function in the executor using the given Runner to dispatch OnComplete when the result ...
Error getBootstrapSymbols(ArrayRef< std::pair< ExecutorAddr &, StringRef > > Pairs) const
For each (ExecutorAddr&, StringRef) pair, looks up the string in the bootstrap symbols map and writes...
ExecutionSession & getExecutionSession()
Return the ExecutionSession associated with this instance.
ExecutorProcessControl(std::shared_ptr< SymbolStringPool > SSP, std::unique_ptr< TaskDispatcher > D)
APIs for manipulating memory in the target process.
Pointer to a pooled string representing a symbol name.
Abstract base for classes that dispatch ORC Tasks.
A utility class for serializing to a blob from a variadic list.
Input char buffer with underflow check.
C++ wrapper function buffer: Same as CWrapperFunctionBuffer but auto-releases memory.
unique_function is a type-erasing functor similar to std::function.
std::unique_ptr< GenericNamedTask > makeGenericNamedTask(FnT &&Fn, std::string Desc)
Create a generic named task from a std::string description.
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_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
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:1916
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:870
Contains the address of the dispatch function and context that the ORC runtime can use to call functi...