ExecutorProcessControl.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ExecutorProcessControl.h - Executor process control APIs -*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // Utilities for interacting with the executor processes.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_EXECUTIONENGINE_ORC_EXECUTORPROCESSCONTROL_H
  18. #define LLVM_EXECUTIONENGINE_ORC_EXECUTORPROCESSCONTROL_H
  19. #include "llvm/ADT/StringRef.h"
  20. #include "llvm/ADT/Triple.h"
  21. #include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
  22. #include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
  23. #include "llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h"
  24. #include "llvm/ExecutionEngine/Orc/Shared/WrapperFunctionUtils.h"
  25. #include "llvm/ExecutionEngine/Orc/SymbolStringPool.h"
  26. #include "llvm/ExecutionEngine/Orc/TaskDispatch.h"
  27. #include "llvm/Support/DynamicLibrary.h"
  28. #include "llvm/Support/MSVCErrorWorkarounds.h"
  29. #include <future>
  30. #include <mutex>
  31. #include <vector>
  32. namespace llvm {
  33. namespace orc {
  34. class ExecutionSession;
  35. class SymbolLookupSet;
  36. /// ExecutorProcessControl supports interaction with a JIT target process.
  37. class ExecutorProcessControl {
  38. friend class ExecutionSession;
  39. public:
  40. /// A handler or incoming WrapperFunctionResults -- either return values from
  41. /// callWrapper* calls, or incoming JIT-dispatch requests.
  42. ///
  43. /// IncomingWFRHandlers are constructible from
  44. /// unique_function<void(shared::WrapperFunctionResult)>s using the
  45. /// runInPlace function or a RunWithDispatch object.
  46. class IncomingWFRHandler {
  47. friend class ExecutorProcessControl;
  48. public:
  49. IncomingWFRHandler() = default;
  50. explicit operator bool() const { return !!H; }
  51. void operator()(shared::WrapperFunctionResult WFR) { H(std::move(WFR)); }
  52. private:
  53. template <typename FnT> IncomingWFRHandler(FnT &&Fn)
  54. : H(std::forward<FnT>(Fn)) {}
  55. unique_function<void(shared::WrapperFunctionResult)> H;
  56. };
  57. /// Constructs an IncomingWFRHandler from a function object that is callable
  58. /// as void(shared::WrapperFunctionResult). The function object will be called
  59. /// directly. This should be used with care as it may block listener threads
  60. /// in remote EPCs. It is only suitable for simple tasks (e.g. setting a
  61. /// future), or for performing some quick analysis before dispatching "real"
  62. /// work as a Task.
  63. class RunInPlace {
  64. public:
  65. template <typename FnT>
  66. IncomingWFRHandler operator()(FnT &&Fn) {
  67. return IncomingWFRHandler(std::forward<FnT>(Fn));
  68. }
  69. };
  70. /// Constructs an IncomingWFRHandler from a function object by creating a new
  71. /// function object that dispatches the original using a TaskDispatcher,
  72. /// wrapping the original as a GenericNamedTask.
  73. ///
  74. /// This is the default approach for running WFR handlers.
  75. class RunAsTask {
  76. public:
  77. RunAsTask(TaskDispatcher &D) : D(D) {}
  78. template <typename FnT>
  79. IncomingWFRHandler operator()(FnT &&Fn) {
  80. return IncomingWFRHandler(
  81. [&D = this->D, Fn = std::move(Fn)]
  82. (shared::WrapperFunctionResult WFR) mutable {
  83. D.dispatch(
  84. makeGenericNamedTask(
  85. [Fn = std::move(Fn), WFR = std::move(WFR)]() mutable {
  86. Fn(std::move(WFR));
  87. }, "WFR handler task"));
  88. });
  89. }
  90. private:
  91. TaskDispatcher &D;
  92. };
  93. /// APIs for manipulating memory in the target process.
  94. class MemoryAccess {
  95. public:
  96. /// Callback function for asynchronous writes.
  97. using WriteResultFn = unique_function<void(Error)>;
  98. virtual ~MemoryAccess();
  99. virtual void writeUInt8sAsync(ArrayRef<tpctypes::UInt8Write> Ws,
  100. WriteResultFn OnWriteComplete) = 0;
  101. virtual void writeUInt16sAsync(ArrayRef<tpctypes::UInt16Write> Ws,
  102. WriteResultFn OnWriteComplete) = 0;
  103. virtual void writeUInt32sAsync(ArrayRef<tpctypes::UInt32Write> Ws,
  104. WriteResultFn OnWriteComplete) = 0;
  105. virtual void writeUInt64sAsync(ArrayRef<tpctypes::UInt64Write> Ws,
  106. WriteResultFn OnWriteComplete) = 0;
  107. virtual void writeBuffersAsync(ArrayRef<tpctypes::BufferWrite> Ws,
  108. WriteResultFn OnWriteComplete) = 0;
  109. Error writeUInt8s(ArrayRef<tpctypes::UInt8Write> Ws) {
  110. std::promise<MSVCPError> ResultP;
  111. auto ResultF = ResultP.get_future();
  112. writeUInt8sAsync(Ws,
  113. [&](Error Err) { ResultP.set_value(std::move(Err)); });
  114. return ResultF.get();
  115. }
  116. Error writeUInt16s(ArrayRef<tpctypes::UInt16Write> Ws) {
  117. std::promise<MSVCPError> ResultP;
  118. auto ResultF = ResultP.get_future();
  119. writeUInt16sAsync(Ws,
  120. [&](Error Err) { ResultP.set_value(std::move(Err)); });
  121. return ResultF.get();
  122. }
  123. Error writeUInt32s(ArrayRef<tpctypes::UInt32Write> Ws) {
  124. std::promise<MSVCPError> ResultP;
  125. auto ResultF = ResultP.get_future();
  126. writeUInt32sAsync(Ws,
  127. [&](Error Err) { ResultP.set_value(std::move(Err)); });
  128. return ResultF.get();
  129. }
  130. Error writeUInt64s(ArrayRef<tpctypes::UInt64Write> Ws) {
  131. std::promise<MSVCPError> ResultP;
  132. auto ResultF = ResultP.get_future();
  133. writeUInt64sAsync(Ws,
  134. [&](Error Err) { ResultP.set_value(std::move(Err)); });
  135. return ResultF.get();
  136. }
  137. Error writeBuffers(ArrayRef<tpctypes::BufferWrite> Ws) {
  138. std::promise<MSVCPError> ResultP;
  139. auto ResultF = ResultP.get_future();
  140. writeBuffersAsync(Ws,
  141. [&](Error Err) { ResultP.set_value(std::move(Err)); });
  142. return ResultF.get();
  143. }
  144. };
  145. /// A pair of a dylib and a set of symbols to be looked up.
  146. struct LookupRequest {
  147. LookupRequest(tpctypes::DylibHandle Handle, const SymbolLookupSet &Symbols)
  148. : Handle(Handle), Symbols(Symbols) {}
  149. tpctypes::DylibHandle Handle;
  150. const SymbolLookupSet &Symbols;
  151. };
  152. /// Contains the address of the dispatch function and context that the ORC
  153. /// runtime can use to call functions in the JIT.
  154. struct JITDispatchInfo {
  155. ExecutorAddr JITDispatchFunction;
  156. ExecutorAddr JITDispatchContext;
  157. };
  158. ExecutorProcessControl(std::shared_ptr<SymbolStringPool> SSP,
  159. std::unique_ptr<TaskDispatcher> D)
  160. : SSP(std::move(SSP)), D(std::move(D)) {}
  161. virtual ~ExecutorProcessControl();
  162. /// Return the ExecutionSession associated with this instance.
  163. /// Not callable until the ExecutionSession has been associated.
  164. ExecutionSession &getExecutionSession() {
  165. assert(ES && "No ExecutionSession associated yet");
  166. return *ES;
  167. }
  168. /// Intern a symbol name in the SymbolStringPool.
  169. SymbolStringPtr intern(StringRef SymName) { return SSP->intern(SymName); }
  170. /// Return a shared pointer to the SymbolStringPool for this instance.
  171. std::shared_ptr<SymbolStringPool> getSymbolStringPool() const { return SSP; }
  172. TaskDispatcher &getDispatcher() { return *D; }
  173. /// Return the Triple for the target process.
  174. const Triple &getTargetTriple() const { return TargetTriple; }
  175. /// Get the page size for the target process.
  176. unsigned getPageSize() const { return PageSize; }
  177. /// Get the JIT dispatch function and context address for the executor.
  178. const JITDispatchInfo &getJITDispatchInfo() const { return JDI; }
  179. /// Return a MemoryAccess object for the target process.
  180. MemoryAccess &getMemoryAccess() const {
  181. assert(MemAccess && "No MemAccess object set.");
  182. return *MemAccess;
  183. }
  184. /// Return a JITLinkMemoryManager for the target process.
  185. jitlink::JITLinkMemoryManager &getMemMgr() const {
  186. assert(MemMgr && "No MemMgr object set");
  187. return *MemMgr;
  188. }
  189. /// Returns the bootstrap symbol map.
  190. const StringMap<ExecutorAddr> &getBootstrapSymbolsMap() const {
  191. return BootstrapSymbols;
  192. }
  193. /// For each (ExecutorAddr&, StringRef) pair, looks up the string in the
  194. /// bootstrap symbols map and writes its address to the ExecutorAddr if
  195. /// found. If any symbol is not found then the function returns an error.
  196. Error getBootstrapSymbols(
  197. ArrayRef<std::pair<ExecutorAddr &, StringRef>> Pairs) const {
  198. for (auto &KV : Pairs) {
  199. auto I = BootstrapSymbols.find(KV.second);
  200. if (I == BootstrapSymbols.end())
  201. return make_error<StringError>("Symbol \"" + KV.second +
  202. "\" not found "
  203. "in bootstrap symbols map",
  204. inconvertibleErrorCode());
  205. KV.first = I->second;
  206. }
  207. return Error::success();
  208. }
  209. /// Load the dynamic library at the given path and return a handle to it.
  210. /// If LibraryPath is null this function will return the global handle for
  211. /// the target process.
  212. virtual Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) = 0;
  213. /// Search for symbols in the target process.
  214. ///
  215. /// The result of the lookup is a 2-dimentional array of target addresses
  216. /// that correspond to the lookup order. If a required symbol is not
  217. /// found then this method will return an error. If a weakly referenced
  218. /// symbol is not found then it be assigned a '0' value.
  219. virtual Expected<std::vector<tpctypes::LookupResult>>
  220. lookupSymbols(ArrayRef<LookupRequest> Request) = 0;
  221. /// Run function with a main-like signature.
  222. virtual Expected<int32_t> runAsMain(ExecutorAddr MainFnAddr,
  223. ArrayRef<std::string> Args) = 0;
  224. /// Run a wrapper function in the executor. The given WFRHandler will be
  225. /// called on the result when it is returned.
  226. ///
  227. /// The wrapper function should be callable as:
  228. ///
  229. /// \code{.cpp}
  230. /// CWrapperFunctionResult fn(uint8_t *Data, uint64_t Size);
  231. /// \endcode{.cpp}
  232. virtual void callWrapperAsync(ExecutorAddr WrapperFnAddr,
  233. IncomingWFRHandler OnComplete,
  234. ArrayRef<char> ArgBuffer) = 0;
  235. /// Run a wrapper function in the executor using the given Runner to dispatch
  236. /// OnComplete when the result is ready.
  237. template <typename RunPolicyT, typename FnT>
  238. void callWrapperAsync(RunPolicyT &&Runner, ExecutorAddr WrapperFnAddr,
  239. FnT &&OnComplete, ArrayRef<char> ArgBuffer) {
  240. callWrapperAsync(
  241. WrapperFnAddr, Runner(std::forward<FnT>(OnComplete)), ArgBuffer);
  242. }
  243. /// Run a wrapper function in the executor. OnComplete will be dispatched
  244. /// as a GenericNamedTask using this instance's TaskDispatch object.
  245. template <typename FnT>
  246. void callWrapperAsync(ExecutorAddr WrapperFnAddr, FnT &&OnComplete,
  247. ArrayRef<char> ArgBuffer) {
  248. callWrapperAsync(RunAsTask(*D), WrapperFnAddr,
  249. std::forward<FnT>(OnComplete), ArgBuffer);
  250. }
  251. /// Run a wrapper function in the executor. The wrapper function should be
  252. /// callable as:
  253. ///
  254. /// \code{.cpp}
  255. /// CWrapperFunctionResult fn(uint8_t *Data, uint64_t Size);
  256. /// \endcode{.cpp}
  257. shared::WrapperFunctionResult callWrapper(ExecutorAddr WrapperFnAddr,
  258. ArrayRef<char> ArgBuffer) {
  259. std::promise<shared::WrapperFunctionResult> RP;
  260. auto RF = RP.get_future();
  261. callWrapperAsync(
  262. RunInPlace(), WrapperFnAddr,
  263. [&](shared::WrapperFunctionResult R) {
  264. RP.set_value(std::move(R));
  265. }, ArgBuffer);
  266. return RF.get();
  267. }
  268. /// Run a wrapper function using SPS to serialize the arguments and
  269. /// deserialize the results.
  270. template <typename SPSSignature, typename RunPolicyT, typename SendResultT,
  271. typename... ArgTs>
  272. void callSPSWrapperAsync(RunPolicyT &&Runner, ExecutorAddr WrapperFnAddr,
  273. SendResultT &&SendResult, const ArgTs &...Args) {
  274. shared::WrapperFunction<SPSSignature>::callAsync(
  275. [this, WrapperFnAddr, Runner = std::move(Runner)]
  276. (auto &&SendResult, const char *ArgData, size_t ArgSize) mutable {
  277. this->callWrapperAsync(std::move(Runner), WrapperFnAddr,
  278. std::move(SendResult),
  279. ArrayRef<char>(ArgData, ArgSize));
  280. },
  281. std::forward<SendResultT>(SendResult), Args...);
  282. }
  283. /// Run a wrapper function using SPS to serialize the arguments and
  284. /// deserialize the results.
  285. template <typename SPSSignature, typename SendResultT, typename... ArgTs>
  286. void callSPSWrapperAsync(ExecutorAddr WrapperFnAddr, SendResultT &&SendResult,
  287. const ArgTs &...Args) {
  288. callSPSWrapperAsync<SPSSignature>(RunAsTask(*D), WrapperFnAddr,
  289. std::forward<SendResultT>(SendResult),
  290. Args...);
  291. }
  292. /// Run a wrapper function using SPS to serialize the arguments and
  293. /// deserialize the results.
  294. ///
  295. /// If SPSSignature is a non-void function signature then the second argument
  296. /// (the first in the Args list) should be a reference to a return value.
  297. template <typename SPSSignature, typename... WrapperCallArgTs>
  298. Error callSPSWrapper(ExecutorAddr WrapperFnAddr,
  299. WrapperCallArgTs &&...WrapperCallArgs) {
  300. return shared::WrapperFunction<SPSSignature>::call(
  301. [this, WrapperFnAddr](const char *ArgData, size_t ArgSize) {
  302. return callWrapper(WrapperFnAddr, ArrayRef<char>(ArgData, ArgSize));
  303. },
  304. std::forward<WrapperCallArgTs>(WrapperCallArgs)...);
  305. }
  306. /// Disconnect from the target process.
  307. ///
  308. /// This should be called after the JIT session is shut down.
  309. virtual Error disconnect() = 0;
  310. protected:
  311. std::shared_ptr<SymbolStringPool> SSP;
  312. std::unique_ptr<TaskDispatcher> D;
  313. ExecutionSession *ES = nullptr;
  314. Triple TargetTriple;
  315. unsigned PageSize = 0;
  316. JITDispatchInfo JDI;
  317. MemoryAccess *MemAccess = nullptr;
  318. jitlink::JITLinkMemoryManager *MemMgr = nullptr;
  319. StringMap<ExecutorAddr> BootstrapSymbols;
  320. };
  321. /// A ExecutorProcessControl instance that asserts if any of its methods are
  322. /// used. Suitable for use is unit tests, and by ORC clients who haven't moved
  323. /// to ExecutorProcessControl-based APIs yet.
  324. class UnsupportedExecutorProcessControl : public ExecutorProcessControl {
  325. public:
  326. UnsupportedExecutorProcessControl(
  327. std::shared_ptr<SymbolStringPool> SSP = nullptr,
  328. std::unique_ptr<TaskDispatcher> D = nullptr,
  329. const std::string &TT = "", unsigned PageSize = 0)
  330. : ExecutorProcessControl(SSP ? std::move(SSP)
  331. : std::make_shared<SymbolStringPool>(),
  332. D ? std::move(D)
  333. : std::make_unique<InPlaceTaskDispatcher>()) {
  334. this->TargetTriple = Triple(TT);
  335. this->PageSize = PageSize;
  336. }
  337. Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) override {
  338. llvm_unreachable("Unsupported");
  339. }
  340. Expected<std::vector<tpctypes::LookupResult>>
  341. lookupSymbols(ArrayRef<LookupRequest> Request) override {
  342. llvm_unreachable("Unsupported");
  343. }
  344. Expected<int32_t> runAsMain(ExecutorAddr MainFnAddr,
  345. ArrayRef<std::string> Args) override {
  346. llvm_unreachable("Unsupported");
  347. }
  348. void callWrapperAsync(ExecutorAddr WrapperFnAddr,
  349. IncomingWFRHandler OnComplete,
  350. ArrayRef<char> ArgBuffer) override {
  351. llvm_unreachable("Unsupported");
  352. }
  353. Error disconnect() override { return Error::success(); }
  354. };
  355. /// A ExecutorProcessControl implementation targeting the current process.
  356. class SelfExecutorProcessControl
  357. : public ExecutorProcessControl,
  358. private ExecutorProcessControl::MemoryAccess {
  359. public:
  360. SelfExecutorProcessControl(
  361. std::shared_ptr<SymbolStringPool> SSP, std::unique_ptr<TaskDispatcher> D,
  362. Triple TargetTriple, unsigned PageSize,
  363. std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr);
  364. /// Create a SelfExecutorProcessControl with the given symbol string pool and
  365. /// memory manager.
  366. /// If no symbol string pool is given then one will be created.
  367. /// If no memory manager is given a jitlink::InProcessMemoryManager will
  368. /// be created and used by default.
  369. static Expected<std::unique_ptr<SelfExecutorProcessControl>>
  370. Create(std::shared_ptr<SymbolStringPool> SSP = nullptr,
  371. std::unique_ptr<TaskDispatcher> D = nullptr,
  372. std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr = nullptr);
  373. Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) override;
  374. Expected<std::vector<tpctypes::LookupResult>>
  375. lookupSymbols(ArrayRef<LookupRequest> Request) override;
  376. Expected<int32_t> runAsMain(ExecutorAddr MainFnAddr,
  377. ArrayRef<std::string> Args) override;
  378. void callWrapperAsync(ExecutorAddr WrapperFnAddr,
  379. IncomingWFRHandler OnComplete,
  380. ArrayRef<char> ArgBuffer) override;
  381. Error disconnect() override;
  382. private:
  383. void writeUInt8sAsync(ArrayRef<tpctypes::UInt8Write> Ws,
  384. WriteResultFn OnWriteComplete) override;
  385. void writeUInt16sAsync(ArrayRef<tpctypes::UInt16Write> Ws,
  386. WriteResultFn OnWriteComplete) override;
  387. void writeUInt32sAsync(ArrayRef<tpctypes::UInt32Write> Ws,
  388. WriteResultFn OnWriteComplete) override;
  389. void writeUInt64sAsync(ArrayRef<tpctypes::UInt64Write> Ws,
  390. WriteResultFn OnWriteComplete) override;
  391. void writeBuffersAsync(ArrayRef<tpctypes::BufferWrite> Ws,
  392. WriteResultFn OnWriteComplete) override;
  393. static shared::CWrapperFunctionResult
  394. jitDispatchViaWrapperFunctionManager(void *Ctx, const void *FnTag,
  395. const char *Data, size_t Size);
  396. std::unique_ptr<jitlink::JITLinkMemoryManager> OwnedMemMgr;
  397. char GlobalManglingPrefix = 0;
  398. std::vector<std::unique_ptr<sys::DynamicLibrary>> DynamicLibraries;
  399. };
  400. } // end namespace orc
  401. } // end namespace llvm
  402. #endif // LLVM_EXECUTIONENGINE_ORC_EXECUTORPROCESSCONTROL_H
  403. #ifdef __GNUC__
  404. #pragma GCC diagnostic pop
  405. #endif