LLVM  13.0.0git
TargetProcessControl.h
Go to the documentation of this file.
1 //===--- TargetProcessControl.h - Target 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 target processes.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_EXECUTIONENGINE_ORC_TARGETPROCESSCONTROL_H
14 #define LLVM_EXECUTIONENGINE_ORC_TARGETPROCESSCONTROL_H
15 
16 #include "llvm/ADT/Optional.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Triple.h"
24 
25 #include <future>
26 #include <vector>
27 
28 namespace llvm {
29 namespace orc {
30 
31 /// TargetProcessControl supports interaction with a JIT target process.
33 public:
34  /// APIs for manipulating memory in the target process.
35  class MemoryAccess {
36  public:
37  /// Callback function for asynchronous writes.
39 
40  virtual ~MemoryAccess();
41 
43  WriteResultFn OnWriteComplete) = 0;
44 
46  WriteResultFn OnWriteComplete) = 0;
47 
49  WriteResultFn OnWriteComplete) = 0;
50 
52  WriteResultFn OnWriteComplete) = 0;
53 
55  WriteResultFn OnWriteComplete) = 0;
56 
58  std::promise<MSVCPError> ResultP;
59  auto ResultF = ResultP.get_future();
60  writeUInt8s(Ws, [&](Error Err) { ResultP.set_value(std::move(Err)); });
61  return ResultF.get();
62  }
63 
65  std::promise<MSVCPError> ResultP;
66  auto ResultF = ResultP.get_future();
67  writeUInt16s(Ws, [&](Error Err) { ResultP.set_value(std::move(Err)); });
68  return ResultF.get();
69  }
70 
72  std::promise<MSVCPError> ResultP;
73  auto ResultF = ResultP.get_future();
74  writeUInt32s(Ws, [&](Error Err) { ResultP.set_value(std::move(Err)); });
75  return ResultF.get();
76  }
77 
79  std::promise<MSVCPError> ResultP;
80  auto ResultF = ResultP.get_future();
81  writeUInt64s(Ws, [&](Error Err) { ResultP.set_value(std::move(Err)); });
82  return ResultF.get();
83  }
84 
86  std::promise<MSVCPError> ResultP;
87  auto ResultF = ResultP.get_future();
88  writeBuffers(Ws, [&](Error Err) { ResultP.set_value(std::move(Err)); });
89  return ResultF.get();
90  }
91  };
92 
93  /// A pair of a dylib and a set of symbols to be looked up.
94  struct LookupRequest {
99  };
100 
101  virtual ~TargetProcessControl();
102 
103  /// Intern a symbol name in the SymbolStringPool.
104  SymbolStringPtr intern(StringRef SymName) { return SSP->intern(SymName); }
105 
106  /// Return a shared pointer to the SymbolStringPool for this instance.
107  std::shared_ptr<SymbolStringPool> getSymbolStringPool() const { return SSP; }
108 
109  /// Return the Triple for the target process.
110  const Triple &getTargetTriple() const { return TargetTriple; }
111 
112  /// Get the page size for the target process.
113  unsigned getPageSize() const { return PageSize; }
114 
115  /// Return a MemoryAccess object for the target process.
116  MemoryAccess &getMemoryAccess() const { return *MemAccess; }
117 
118  /// Return a JITLinkMemoryManager for the target process.
120 
121  /// Load the dynamic library at the given path and return a handle to it.
122  /// If LibraryPath is null this function will return the global handle for
123  /// the target process.
124  virtual Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) = 0;
125 
126  /// Search for symbols in the target process.
127  ///
128  /// The result of the lookup is a 2-dimentional array of target addresses
129  /// that correspond to the lookup order. If a required symbol is not
130  /// found then this method will return an error. If a weakly referenced
131  /// symbol is not found then it be assigned a '0' value in the result.
132  /// that correspond to the lookup order.
135 
136  /// Run function with a main-like signature.
137  virtual Expected<int32_t> runAsMain(JITTargetAddress MainFnAddr,
139 
140  /// Run a wrapper function with signature:
141  ///
142  /// \code{.cpp}
143  /// CWrapperFunctionResult fn(uint8_t *Data, uint64_t Size);
144  /// \endcode{.cpp}
145  ///
147  runWrapper(JITTargetAddress WrapperFnAddr, ArrayRef<uint8_t> ArgBuffer) = 0;
148 
149  /// Disconnect from the target process.
150  ///
151  /// This should be called after the JIT session is shut down.
152  virtual Error disconnect() = 0;
153 
154 protected:
155  TargetProcessControl(std::shared_ptr<SymbolStringPool> SSP)
156  : SSP(std::move(SSP)) {}
157 
158  std::shared_ptr<SymbolStringPool> SSP;
160  unsigned PageSize = 0;
163 };
164 
165 /// A TargetProcessControl implementation targeting the current process.
168 public:
170  std::shared_ptr<SymbolStringPool> SSP, Triple TargetTriple,
171  unsigned PageSize, std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr);
172 
173  /// Create a SelfTargetProcessControl with the given memory manager.
174  /// If no memory manager is given a jitlink::InProcessMemoryManager will
175  /// be used by default.
177  Create(std::shared_ptr<SymbolStringPool> SSP,
178  std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr = nullptr);
179 
180  Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) override;
181 
183  lookupSymbols(ArrayRef<LookupRequest> Request) override;
184 
186  ArrayRef<std::string> Args) override;
187 
189  runWrapper(JITTargetAddress WrapperFnAddr,
190  ArrayRef<uint8_t> ArgBuffer) override;
191 
192  Error disconnect() override;
193 
194 private:
195  void writeUInt8s(ArrayRef<tpctypes::UInt8Write> Ws,
196  WriteResultFn OnWriteComplete) override;
197 
198  void writeUInt16s(ArrayRef<tpctypes::UInt16Write> Ws,
199  WriteResultFn OnWriteComplete) override;
200 
201  void writeUInt32s(ArrayRef<tpctypes::UInt32Write> Ws,
202  WriteResultFn OnWriteComplete) override;
203 
204  void writeUInt64s(ArrayRef<tpctypes::UInt64Write> Ws,
205  WriteResultFn OnWriteComplete) override;
206 
207  void writeBuffers(ArrayRef<tpctypes::BufferWrite> Ws,
208  WriteResultFn OnWriteComplete) override;
209 
210  std::unique_ptr<jitlink::JITLinkMemoryManager> OwnedMemMgr;
211  char GlobalManglingPrefix = 0;
212  std::vector<std::unique_ptr<sys::DynamicLibrary>> DynamicLibraries;
213 };
214 
215 } // end namespace orc
216 } // end namespace llvm
217 
218 #endif // LLVM_EXECUTIONENGINE_ORC_TARGETPROCESSCONTROL_H
llvm::orc::TargetProcessControl::MemoryAccess::writeBuffers
virtual void writeBuffers(ArrayRef< tpctypes::BufferWrite > Ws, WriteResultFn OnWriteComplete)=0
TargetProcessControlTypes.h
llvm
Definition: AllocatorList.h:23
llvm::orc::TargetProcessControl::getSymbolStringPool
std::shared_ptr< SymbolStringPool > getSymbolStringPool() const
Return a shared pointer to the SymbolStringPool for this instance.
Definition: TargetProcessControl.h:107
Optional.h
llvm::unique_function< void(Error)>
llvm::orc::TargetProcessControl::LookupRequest::LookupRequest
LookupRequest(tpctypes::DylibHandle Handle, const SymbolLookupSet &Symbols)
Definition: TargetProcessControl.h:95
llvm::orc::SelfTargetProcessControl::runAsMain
Expected< int32_t > runAsMain(JITTargetAddress MainFnAddr, ArrayRef< std::string > Args) override
Run function with a main-like signature.
Definition: TargetProcessControl.cpp:99
StringRef.h
llvm::orc::TargetProcessControl::MemoryAccess
APIs for manipulating memory in the target process.
Definition: TargetProcessControl.h:35
llvm::orc::TargetProcessControl::SSP
std::shared_ptr< SymbolStringPool > SSP
Definition: TargetProcessControl.h:158
llvm::orc::SymbolLookupSet
A set of symbols to look up, each associated with a SymbolLookupFlags value.
Definition: Core.h:173
llvm::orc::SelfTargetProcessControl::disconnect
Error disconnect() override
Disconnect from the target process.
Definition: TargetProcessControl.cpp:114
llvm::orc::SymbolStringPtr
Pointer to a pooled string representing a symbol name.
Definition: SymbolStringPool.h:50
llvm::orc::TargetProcessControl::LookupRequest::Symbols
const SymbolLookupSet & Symbols
Definition: TargetProcessControl.h:98
llvm::orc::TargetProcessControl::runAsMain
virtual Expected< int32_t > runAsMain(JITTargetAddress MainFnAddr, ArrayRef< std::string > Args)=0
Run function with a main-like signature.
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
llvm::orc::TargetProcessControl::LookupRequest::Handle
tpctypes::DylibHandle Handle
Definition: TargetProcessControl.h:97
DynamicLibrary.h
MSVCErrorWorkarounds.h
llvm::orc::TargetProcessControl::LookupRequest
A pair of a dylib and a set of symbols to be looked up.
Definition: TargetProcessControl.h:94
llvm::orc::TargetProcessControl::MemoryAccess::writeUInt64s
virtual void writeUInt64s(ArrayRef< tpctypes::UInt64Write > Ws, WriteResultFn OnWriteComplete)=0
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::orc::SelfTargetProcessControl::loadDylib
Expected< tpctypes::DylibHandle > loadDylib(const char *DylibPath) override
Load the dynamic library at the given path and return a handle to it.
Definition: TargetProcessControl.cpp:57
llvm::orc::TargetProcessControl::MemoryAccess::~MemoryAccess
virtual ~MemoryAccess()
Definition: TargetProcessControl.cpp:21
llvm::orc::SelfTargetProcessControl::runWrapper
Expected< tpctypes::WrapperFunctionResult > runWrapper(JITTargetAddress WrapperFnAddr, ArrayRef< uint8_t > ArgBuffer) override
Run a wrapper function with signature:
Definition: TargetProcessControl.cpp:106
llvm::orc::TargetProcessControl::MemoryAccess::writeBuffers
Error writeBuffers(ArrayRef< tpctypes::BufferWrite > Ws)
Definition: TargetProcessControl.h:85
llvm::orc::TargetProcessControl::TargetProcessControl
TargetProcessControl(std::shared_ptr< SymbolStringPool > SSP)
Definition: TargetProcessControl.h:155
llvm::orc::TargetProcessControl::getTargetTriple
const Triple & getTargetTriple() const
Return the Triple for the target process.
Definition: TargetProcessControl.h:110
llvm::orc::TargetProcessControl::MemoryAccess::writeUInt16s
virtual void writeUInt16s(ArrayRef< tpctypes::UInt16Write > Ws, WriteResultFn OnWriteComplete)=0
llvm::orc::tpctypes::DylibHandle
JITTargetAddress DylibHandle
A handle used to represent a loaded dylib in the target process.
Definition: TargetProcessControlTypes.h:60
llvm::orc::TargetProcessControl::intern
SymbolStringPtr intern(StringRef SymName)
Intern a symbol name in the SymbolStringPool.
Definition: TargetProcessControl.h:104
llvm::orc::TargetProcessControl::loadDylib
virtual Expected< tpctypes::DylibHandle > loadDylib(const char *DylibPath)=0
Load the dynamic library at the given path and return a handle to it.
llvm::orc::TargetProcessControl::MemoryAccess::writeUInt64s
Error writeUInt64s(ArrayRef< tpctypes::UInt64Write > Ws)
Definition: TargetProcessControl.h:78
llvm::orc::SelfTargetProcessControl::Create
static Expected< std::unique_ptr< SelfTargetProcessControl > > Create(std::shared_ptr< SymbolStringPool > SSP, std::unique_ptr< jitlink::JITLinkMemoryManager > MemMgr=nullptr)
Create a SelfTargetProcessControl with the given memory manager.
Definition: TargetProcessControl.cpp:43
llvm::orc::TargetProcessControl
TargetProcessControl supports interaction with a JIT target process.
Definition: TargetProcessControl.h:32
llvm::orc::TargetProcessControl::MemoryAccess::writeUInt32s
virtual void writeUInt32s(ArrayRef< tpctypes::UInt32Write > Ws, WriteResultFn OnWriteComplete)=0
llvm::orc::TargetProcessControl::getPageSize
unsigned getPageSize() const
Get the page size for the target process.
Definition: TargetProcessControl.h:113
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::orc::SelfTargetProcessControl::SelfTargetProcessControl
SelfTargetProcessControl(std::shared_ptr< SymbolStringPool > SSP, Triple TargetTriple, unsigned PageSize, std::unique_ptr< jitlink::JITLinkMemoryManager > MemMgr)
Definition: TargetProcessControl.cpp:25
llvm::move
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:1540
Triple.h
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::orc::TargetProcessControl::lookupSymbols
virtual Expected< std::vector< tpctypes::LookupResult > > lookupSymbols(ArrayRef< LookupRequest > Request)=0
Search for symbols in the target process.
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::orc::TargetProcessControl::MemMgr
jitlink::JITLinkMemoryManager * MemMgr
Definition: TargetProcessControl.h:162
JITLinkMemoryManager.h
llvm::orc::TargetProcessControl::MemoryAccess::writeUInt32s
Error writeUInt32s(ArrayRef< tpctypes::UInt32Write > Ws)
Definition: TargetProcessControl.h:71
llvm::orc::TargetProcessControl::getMemoryAccess
MemoryAccess & getMemoryAccess() const
Return a MemoryAccess object for the target process.
Definition: TargetProcessControl.h:116
llvm::orc::SelfTargetProcessControl
A TargetProcessControl implementation targeting the current process.
Definition: TargetProcessControl.h:166
llvm::orc::TargetProcessControl::PageSize
unsigned PageSize
Definition: TargetProcessControl.h:160
llvm::orc::TargetProcessControl::TargetTriple
Triple TargetTriple
Definition: TargetProcessControl.h:159
std
Definition: BitVector.h:838
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::orc::TargetProcessControl::MemAccess
MemoryAccess * MemAccess
Definition: TargetProcessControl.h:161
llvm::orc::TargetProcessControl::disconnect
virtual Error disconnect()=0
Disconnect from the target process.
llvm::JITTargetAddress
uint64_t JITTargetAddress
Represents an address in the target process's address space.
Definition: JITSymbol.h:42
llvm::orc::TargetProcessControl::~TargetProcessControl
virtual ~TargetProcessControl()
Definition: TargetProcessControl.cpp:23
Core.h
llvm::orc::TargetProcessControl::getMemMgr
jitlink::JITLinkMemoryManager & getMemMgr() const
Return a JITLinkMemoryManager for the target process.
Definition: TargetProcessControl.h:119
llvm::orc::TargetProcessControl::MemoryAccess::writeUInt16s
Error writeUInt16s(ArrayRef< tpctypes::UInt16Write > Ws)
Definition: TargetProcessControl.h:64
llvm::orc::TargetProcessControl::MemoryAccess::writeUInt8s
Error writeUInt8s(ArrayRef< tpctypes::UInt8Write > Ws)
Definition: TargetProcessControl.h:57
llvm::orc::SelfTargetProcessControl::lookupSymbols
Expected< std::vector< tpctypes::LookupResult > > lookupSymbols(ArrayRef< LookupRequest > Request) override
Search for symbols in the target process.
Definition: TargetProcessControl.cpp:68
llvm::orc::TargetProcessControl::MemoryAccess::writeUInt8s
virtual void writeUInt8s(ArrayRef< tpctypes::UInt8Write > Ws, WriteResultFn OnWriteComplete)=0
llvm::AMDGPU::HSAMD::Kernel::Key::Args
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
Definition: AMDGPUMetadata.h:389
llvm::orc::TargetProcessControl::runWrapper
virtual Expected< tpctypes::WrapperFunctionResult > runWrapper(JITTargetAddress WrapperFnAddr, ArrayRef< uint8_t > ArgBuffer)=0
Run a wrapper function with signature: