TargetProcessControl.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- TargetProcessControl.h - Target 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 target processes.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_EXECUTIONENGINE_ORC_TARGETPROCESSCONTROL_H
  18. #define LLVM_EXECUTIONENGINE_ORC_TARGETPROCESSCONTROL_H
  19. #include "llvm/ADT/Optional.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/ADT/Triple.h"
  22. #include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
  23. #include "llvm/ExecutionEngine/Orc/Core.h"
  24. #include "llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h"
  25. #include "llvm/Support/DynamicLibrary.h"
  26. #include "llvm/Support/MSVCErrorWorkarounds.h"
  27. #include <future>
  28. #include <vector>
  29. namespace llvm {
  30. namespace orc {
  31. /// TargetProcessControl supports interaction with a JIT target process.
  32. class TargetProcessControl {
  33. public:
  34. /// APIs for manipulating memory in the target process.
  35. class MemoryAccess {
  36. public:
  37. /// Callback function for asynchronous writes.
  38. using WriteResultFn = unique_function<void(Error)>;
  39. virtual ~MemoryAccess();
  40. virtual void writeUInt8s(ArrayRef<tpctypes::UInt8Write> Ws,
  41. WriteResultFn OnWriteComplete) = 0;
  42. virtual void writeUInt16s(ArrayRef<tpctypes::UInt16Write> Ws,
  43. WriteResultFn OnWriteComplete) = 0;
  44. virtual void writeUInt32s(ArrayRef<tpctypes::UInt32Write> Ws,
  45. WriteResultFn OnWriteComplete) = 0;
  46. virtual void writeUInt64s(ArrayRef<tpctypes::UInt64Write> Ws,
  47. WriteResultFn OnWriteComplete) = 0;
  48. virtual void writeBuffers(ArrayRef<tpctypes::BufferWrite> Ws,
  49. WriteResultFn OnWriteComplete) = 0;
  50. Error writeUInt8s(ArrayRef<tpctypes::UInt8Write> Ws) {
  51. std::promise<MSVCPError> ResultP;
  52. auto ResultF = ResultP.get_future();
  53. writeUInt8s(Ws, [&](Error Err) { ResultP.set_value(std::move(Err)); });
  54. return ResultF.get();
  55. }
  56. Error writeUInt16s(ArrayRef<tpctypes::UInt16Write> Ws) {
  57. std::promise<MSVCPError> ResultP;
  58. auto ResultF = ResultP.get_future();
  59. writeUInt16s(Ws, [&](Error Err) { ResultP.set_value(std::move(Err)); });
  60. return ResultF.get();
  61. }
  62. Error writeUInt32s(ArrayRef<tpctypes::UInt32Write> Ws) {
  63. std::promise<MSVCPError> ResultP;
  64. auto ResultF = ResultP.get_future();
  65. writeUInt32s(Ws, [&](Error Err) { ResultP.set_value(std::move(Err)); });
  66. return ResultF.get();
  67. }
  68. Error writeUInt64s(ArrayRef<tpctypes::UInt64Write> Ws) {
  69. std::promise<MSVCPError> ResultP;
  70. auto ResultF = ResultP.get_future();
  71. writeUInt64s(Ws, [&](Error Err) { ResultP.set_value(std::move(Err)); });
  72. return ResultF.get();
  73. }
  74. Error writeBuffers(ArrayRef<tpctypes::BufferWrite> Ws) {
  75. std::promise<MSVCPError> ResultP;
  76. auto ResultF = ResultP.get_future();
  77. writeBuffers(Ws, [&](Error Err) { ResultP.set_value(std::move(Err)); });
  78. return ResultF.get();
  79. }
  80. };
  81. /// A pair of a dylib and a set of symbols to be looked up.
  82. struct LookupRequest {
  83. LookupRequest(tpctypes::DylibHandle Handle, const SymbolLookupSet &Symbols)
  84. : Handle(Handle), Symbols(Symbols) {}
  85. tpctypes::DylibHandle Handle;
  86. const SymbolLookupSet &Symbols;
  87. };
  88. virtual ~TargetProcessControl();
  89. /// Intern a symbol name in the SymbolStringPool.
  90. SymbolStringPtr intern(StringRef SymName) { return SSP->intern(SymName); }
  91. /// Return a shared pointer to the SymbolStringPool for this instance.
  92. std::shared_ptr<SymbolStringPool> getSymbolStringPool() const { return SSP; }
  93. /// Return the Triple for the target process.
  94. const Triple &getTargetTriple() const { return TargetTriple; }
  95. /// Get the page size for the target process.
  96. unsigned getPageSize() const { return PageSize; }
  97. /// Return a MemoryAccess object for the target process.
  98. MemoryAccess &getMemoryAccess() const { return *MemAccess; }
  99. /// Return a JITLinkMemoryManager for the target process.
  100. jitlink::JITLinkMemoryManager &getMemMgr() const { return *MemMgr; }
  101. /// Load the dynamic library at the given path and return a handle to it.
  102. /// If LibraryPath is null this function will return the global handle for
  103. /// the target process.
  104. virtual Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) = 0;
  105. /// Search for symbols in the target process.
  106. ///
  107. /// The result of the lookup is a 2-dimentional array of target addresses
  108. /// that correspond to the lookup order. If a required symbol is not
  109. /// found then this method will return an error. If a weakly referenced
  110. /// symbol is not found then it be assigned a '0' value in the result.
  111. /// that correspond to the lookup order.
  112. virtual Expected<std::vector<tpctypes::LookupResult>>
  113. lookupSymbols(ArrayRef<LookupRequest> Request) = 0;
  114. /// Run function with a main-like signature.
  115. virtual Expected<int32_t> runAsMain(JITTargetAddress MainFnAddr,
  116. ArrayRef<std::string> Args) = 0;
  117. /// Run a wrapper function with signature:
  118. ///
  119. /// \code{.cpp}
  120. /// CWrapperFunctionResult fn(uint8_t *Data, uint64_t Size);
  121. /// \endcode{.cpp}
  122. ///
  123. virtual Expected<tpctypes::WrapperFunctionResult>
  124. runWrapper(JITTargetAddress WrapperFnAddr, ArrayRef<uint8_t> ArgBuffer) = 0;
  125. /// Disconnect from the target process.
  126. ///
  127. /// This should be called after the JIT session is shut down.
  128. virtual Error disconnect() = 0;
  129. protected:
  130. TargetProcessControl(std::shared_ptr<SymbolStringPool> SSP)
  131. : SSP(std::move(SSP)) {}
  132. std::shared_ptr<SymbolStringPool> SSP;
  133. Triple TargetTriple;
  134. unsigned PageSize = 0;
  135. MemoryAccess *MemAccess = nullptr;
  136. jitlink::JITLinkMemoryManager *MemMgr = nullptr;
  137. };
  138. /// A TargetProcessControl implementation targeting the current process.
  139. class SelfTargetProcessControl : public TargetProcessControl,
  140. private TargetProcessControl::MemoryAccess {
  141. public:
  142. SelfTargetProcessControl(
  143. std::shared_ptr<SymbolStringPool> SSP, Triple TargetTriple,
  144. unsigned PageSize, std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr);
  145. /// Create a SelfTargetProcessControl with the given memory manager.
  146. /// If no memory manager is given a jitlink::InProcessMemoryManager will
  147. /// be used by default.
  148. static Expected<std::unique_ptr<SelfTargetProcessControl>>
  149. Create(std::shared_ptr<SymbolStringPool> SSP,
  150. std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr = nullptr);
  151. Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) override;
  152. Expected<std::vector<tpctypes::LookupResult>>
  153. lookupSymbols(ArrayRef<LookupRequest> Request) override;
  154. Expected<int32_t> runAsMain(JITTargetAddress MainFnAddr,
  155. ArrayRef<std::string> Args) override;
  156. Expected<tpctypes::WrapperFunctionResult>
  157. runWrapper(JITTargetAddress WrapperFnAddr,
  158. ArrayRef<uint8_t> ArgBuffer) override;
  159. Error disconnect() override;
  160. private:
  161. void writeUInt8s(ArrayRef<tpctypes::UInt8Write> Ws,
  162. WriteResultFn OnWriteComplete) override;
  163. void writeUInt16s(ArrayRef<tpctypes::UInt16Write> Ws,
  164. WriteResultFn OnWriteComplete) override;
  165. void writeUInt32s(ArrayRef<tpctypes::UInt32Write> Ws,
  166. WriteResultFn OnWriteComplete) override;
  167. void writeUInt64s(ArrayRef<tpctypes::UInt64Write> Ws,
  168. WriteResultFn OnWriteComplete) override;
  169. void writeBuffers(ArrayRef<tpctypes::BufferWrite> Ws,
  170. WriteResultFn OnWriteComplete) override;
  171. std::unique_ptr<jitlink::JITLinkMemoryManager> OwnedMemMgr;
  172. char GlobalManglingPrefix = 0;
  173. std::vector<std::unique_ptr<sys::DynamicLibrary>> DynamicLibraries;
  174. };
  175. } // end namespace orc
  176. } // end namespace llvm
  177. #endif // LLVM_EXECUTIONENGINE_ORC_TARGETPROCESSCONTROL_H
  178. #ifdef __GNUC__
  179. #pragma GCC diagnostic pop
  180. #endif