ExecutorProcessControl.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 (const 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. // TODO: move this to ORC runtime.
  225. /// Run function with a int (*)(void) signature.
  226. virtual Expected<int32_t> runAsVoidFunction(ExecutorAddr VoidFnAddr) = 0;
  227. // TODO: move this to ORC runtime.
  228. /// Run function with a int (*)(int) signature.
  229. virtual Expected<int32_t> runAsIntFunction(ExecutorAddr IntFnAddr,
  230. int Arg) = 0;
  231. /// Run a wrapper function in the executor. The given WFRHandler will be
  232. /// called on the result when it is returned.
  233. ///
  234. /// The wrapper function should be callable as:
  235. ///
  236. /// \code{.cpp}
  237. /// CWrapperFunctionResult fn(uint8_t *Data, uint64_t Size);
  238. /// \endcode{.cpp}
  239. virtual void callWrapperAsync(ExecutorAddr WrapperFnAddr,
  240. IncomingWFRHandler OnComplete,
  241. ArrayRef<char> ArgBuffer) = 0;
  242. /// Run a wrapper function in the executor using the given Runner to dispatch
  243. /// OnComplete when the result is ready.
  244. template <typename RunPolicyT, typename FnT>
  245. void callWrapperAsync(RunPolicyT &&Runner, ExecutorAddr WrapperFnAddr,
  246. FnT &&OnComplete, ArrayRef<char> ArgBuffer) {
  247. callWrapperAsync(
  248. WrapperFnAddr, Runner(std::forward<FnT>(OnComplete)), ArgBuffer);
  249. }
  250. /// Run a wrapper function in the executor. OnComplete will be dispatched
  251. /// as a GenericNamedTask using this instance's TaskDispatch object.
  252. template <typename FnT>
  253. void callWrapperAsync(ExecutorAddr WrapperFnAddr, FnT &&OnComplete,
  254. ArrayRef<char> ArgBuffer) {
  255. callWrapperAsync(RunAsTask(*D), WrapperFnAddr,
  256. std::forward<FnT>(OnComplete), ArgBuffer);
  257. }
  258. /// Run a wrapper function in the executor. The wrapper function should be
  259. /// callable as:
  260. ///
  261. /// \code{.cpp}
  262. /// CWrapperFunctionResult fn(uint8_t *Data, uint64_t Size);
  263. /// \endcode{.cpp}
  264. shared::WrapperFunctionResult callWrapper(ExecutorAddr WrapperFnAddr,
  265. ArrayRef<char> ArgBuffer) {
  266. std::promise<shared::WrapperFunctionResult> RP;
  267. auto RF = RP.get_future();
  268. callWrapperAsync(
  269. RunInPlace(), WrapperFnAddr,
  270. [&](shared::WrapperFunctionResult R) {
  271. RP.set_value(std::move(R));
  272. }, ArgBuffer);
  273. return RF.get();
  274. }
  275. /// Run a wrapper function using SPS to serialize the arguments and
  276. /// deserialize the results.
  277. template <typename SPSSignature, typename RunPolicyT, typename SendResultT,
  278. typename... ArgTs>
  279. void callSPSWrapperAsync(RunPolicyT &&Runner, ExecutorAddr WrapperFnAddr,
  280. SendResultT &&SendResult, const ArgTs &...Args) {
  281. shared::WrapperFunction<SPSSignature>::callAsync(
  282. [this, WrapperFnAddr, Runner = std::move(Runner)]
  283. (auto &&SendResult, const char *ArgData, size_t ArgSize) mutable {
  284. this->callWrapperAsync(std::move(Runner), WrapperFnAddr,
  285. std::move(SendResult),
  286. ArrayRef<char>(ArgData, ArgSize));
  287. },
  288. std::forward<SendResultT>(SendResult), Args...);
  289. }
  290. /// Run a wrapper function using SPS to serialize the arguments and
  291. /// deserialize the results.
  292. template <typename SPSSignature, typename SendResultT, typename... ArgTs>
  293. void callSPSWrapperAsync(ExecutorAddr WrapperFnAddr, SendResultT &&SendResult,
  294. const ArgTs &...Args) {
  295. callSPSWrapperAsync<SPSSignature>(RunAsTask(*D), WrapperFnAddr,
  296. std::forward<SendResultT>(SendResult),
  297. Args...);
  298. }
  299. /// Run a wrapper function using SPS to serialize the arguments and
  300. /// deserialize the results.
  301. ///
  302. /// If SPSSignature is a non-void function signature then the second argument
  303. /// (the first in the Args list) should be a reference to a return value.
  304. template <typename SPSSignature, typename... WrapperCallArgTs>
  305. Error callSPSWrapper(ExecutorAddr WrapperFnAddr,
  306. WrapperCallArgTs &&...WrapperCallArgs) {
  307. return shared::WrapperFunction<SPSSignature>::call(
  308. [this, WrapperFnAddr](const char *ArgData, size_t ArgSize) {
  309. return callWrapper(WrapperFnAddr, ArrayRef<char>(ArgData, ArgSize));
  310. },
  311. std::forward<WrapperCallArgTs>(WrapperCallArgs)...);
  312. }
  313. /// Disconnect from the target process.
  314. ///
  315. /// This should be called after the JIT session is shut down.
  316. virtual Error disconnect() = 0;
  317. protected:
  318. std::shared_ptr<SymbolStringPool> SSP;
  319. std::unique_ptr<TaskDispatcher> D;
  320. ExecutionSession *ES = nullptr;
  321. Triple TargetTriple;
  322. unsigned PageSize = 0;
  323. JITDispatchInfo JDI;
  324. MemoryAccess *MemAccess = nullptr;
  325. jitlink::JITLinkMemoryManager *MemMgr = nullptr;
  326. StringMap<ExecutorAddr> BootstrapSymbols;
  327. };
  328. /// A ExecutorProcessControl instance that asserts if any of its methods are
  329. /// used. Suitable for use is unit tests, and by ORC clients who haven't moved
  330. /// to ExecutorProcessControl-based APIs yet.
  331. class UnsupportedExecutorProcessControl : public ExecutorProcessControl {
  332. public:
  333. UnsupportedExecutorProcessControl(
  334. std::shared_ptr<SymbolStringPool> SSP = nullptr,
  335. std::unique_ptr<TaskDispatcher> D = nullptr,
  336. const std::string &TT = "", unsigned PageSize = 0)
  337. : ExecutorProcessControl(SSP ? std::move(SSP)
  338. : std::make_shared<SymbolStringPool>(),
  339. D ? std::move(D)
  340. : std::make_unique<InPlaceTaskDispatcher>()) {
  341. this->TargetTriple = Triple(TT);
  342. this->PageSize = PageSize;
  343. }
  344. Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) override {
  345. llvm_unreachable("Unsupported");
  346. }
  347. Expected<std::vector<tpctypes::LookupResult>>
  348. lookupSymbols(ArrayRef<LookupRequest> Request) override {
  349. llvm_unreachable("Unsupported");
  350. }
  351. Expected<int32_t> runAsMain(ExecutorAddr MainFnAddr,
  352. ArrayRef<std::string> Args) override {
  353. llvm_unreachable("Unsupported");
  354. }
  355. Expected<int32_t> runAsVoidFunction(ExecutorAddr VoidFnAddr) override {
  356. llvm_unreachable("Unsupported");
  357. }
  358. Expected<int32_t> runAsIntFunction(ExecutorAddr IntFnAddr, int Arg) override {
  359. llvm_unreachable("Unsupported");
  360. }
  361. void callWrapperAsync(ExecutorAddr WrapperFnAddr,
  362. IncomingWFRHandler OnComplete,
  363. ArrayRef<char> ArgBuffer) override {
  364. llvm_unreachable("Unsupported");
  365. }
  366. Error disconnect() override { return Error::success(); }
  367. };
  368. /// A ExecutorProcessControl implementation targeting the current process.
  369. class SelfExecutorProcessControl
  370. : public ExecutorProcessControl,
  371. private ExecutorProcessControl::MemoryAccess {
  372. public:
  373. SelfExecutorProcessControl(
  374. std::shared_ptr<SymbolStringPool> SSP, std::unique_ptr<TaskDispatcher> D,
  375. Triple TargetTriple, unsigned PageSize,
  376. std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr);
  377. /// Create a SelfExecutorProcessControl with the given symbol string pool and
  378. /// memory manager.
  379. /// If no symbol string pool is given then one will be created.
  380. /// If no memory manager is given a jitlink::InProcessMemoryManager will
  381. /// be created and used by default.
  382. static Expected<std::unique_ptr<SelfExecutorProcessControl>>
  383. Create(std::shared_ptr<SymbolStringPool> SSP = nullptr,
  384. std::unique_ptr<TaskDispatcher> D = nullptr,
  385. std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr = nullptr);
  386. Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) override;
  387. Expected<std::vector<tpctypes::LookupResult>>
  388. lookupSymbols(ArrayRef<LookupRequest> Request) override;
  389. Expected<int32_t> runAsMain(ExecutorAddr MainFnAddr,
  390. ArrayRef<std::string> Args) override;
  391. Expected<int32_t> runAsVoidFunction(ExecutorAddr VoidFnAddr) override;
  392. Expected<int32_t> runAsIntFunction(ExecutorAddr IntFnAddr, int Arg) override;
  393. void callWrapperAsync(ExecutorAddr WrapperFnAddr,
  394. IncomingWFRHandler OnComplete,
  395. ArrayRef<char> ArgBuffer) override;
  396. Error disconnect() override;
  397. private:
  398. void writeUInt8sAsync(ArrayRef<tpctypes::UInt8Write> Ws,
  399. WriteResultFn OnWriteComplete) override;
  400. void writeUInt16sAsync(ArrayRef<tpctypes::UInt16Write> Ws,
  401. WriteResultFn OnWriteComplete) override;
  402. void writeUInt32sAsync(ArrayRef<tpctypes::UInt32Write> Ws,
  403. WriteResultFn OnWriteComplete) override;
  404. void writeUInt64sAsync(ArrayRef<tpctypes::UInt64Write> Ws,
  405. WriteResultFn OnWriteComplete) override;
  406. void writeBuffersAsync(ArrayRef<tpctypes::BufferWrite> Ws,
  407. WriteResultFn OnWriteComplete) override;
  408. static shared::CWrapperFunctionResult
  409. jitDispatchViaWrapperFunctionManager(void *Ctx, const void *FnTag,
  410. const char *Data, size_t Size);
  411. std::unique_ptr<jitlink::JITLinkMemoryManager> OwnedMemMgr;
  412. char GlobalManglingPrefix = 0;
  413. };
  414. } // end namespace orc
  415. } // end namespace llvm
  416. #endif // LLVM_EXECUTIONENGINE_ORC_EXECUTORPROCESSCONTROL_H
  417. #ifdef __GNUC__
  418. #pragma GCC diagnostic pop
  419. #endif