• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
19 #include "llvm/ExecutionEngine/JITLink/JITLinkMemoryManager.h"
20 #include "llvm/ExecutionEngine/Orc/Core.h"
21 #include "llvm/ExecutionEngine/Orc/Shared/TargetProcessControlTypes.h"
22 #include "llvm/Support/DynamicLibrary.h"
23 #include "llvm/Support/MSVCErrorWorkarounds.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.
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 
40     virtual ~MemoryAccess();
41 
42     virtual void writeUInt8s(ArrayRef<tpctypes::UInt8Write> Ws,
43                              WriteResultFn OnWriteComplete) = 0;
44 
45     virtual void writeUInt16s(ArrayRef<tpctypes::UInt16Write> Ws,
46                               WriteResultFn OnWriteComplete) = 0;
47 
48     virtual void writeUInt32s(ArrayRef<tpctypes::UInt32Write> Ws,
49                               WriteResultFn OnWriteComplete) = 0;
50 
51     virtual void writeUInt64s(ArrayRef<tpctypes::UInt64Write> Ws,
52                               WriteResultFn OnWriteComplete) = 0;
53 
54     virtual void writeBuffers(ArrayRef<tpctypes::BufferWrite> Ws,
55                               WriteResultFn OnWriteComplete) = 0;
56 
writeUInt8s(ArrayRef<tpctypes::UInt8Write> Ws)57     Error writeUInt8s(ArrayRef<tpctypes::UInt8Write> Ws) {
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 
writeUInt16s(ArrayRef<tpctypes::UInt16Write> Ws)64     Error writeUInt16s(ArrayRef<tpctypes::UInt16Write> Ws) {
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 
writeUInt32s(ArrayRef<tpctypes::UInt32Write> Ws)71     Error writeUInt32s(ArrayRef<tpctypes::UInt32Write> Ws) {
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 
writeUInt64s(ArrayRef<tpctypes::UInt64Write> Ws)78     Error writeUInt64s(ArrayRef<tpctypes::UInt64Write> Ws) {
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 
writeBuffers(ArrayRef<tpctypes::BufferWrite> Ws)85     Error writeBuffers(ArrayRef<tpctypes::BufferWrite> Ws) {
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   virtual ~TargetProcessControl();
94 
95   /// Intern a symbol name in the SymbolStringPool.
intern(StringRef SymName)96   SymbolStringPtr intern(StringRef SymName) { return SSP->intern(SymName); }
97 
98   /// Return a shared pointer to the SymbolStringPool for this instance.
getSymbolStringPool()99   std::shared_ptr<SymbolStringPool> getSymbolStringPool() const { return SSP; }
100 
101   /// Return the Triple for the target process.
getTargetTriple()102   const Triple &getTargetTriple() const { return TargetTriple; }
103 
104   /// Get the page size for the target process.
getPageSize()105   unsigned getPageSize() const { return PageSize; }
106 
107   /// Return a MemoryAccess object for the target process.
getMemoryAccess()108   MemoryAccess &getMemoryAccess() const { return *MemAccess; }
109 
110   /// Return a JITLinkMemoryManager for the target process.
getMemMgr()111   jitlink::JITLinkMemoryManager &getMemMgr() const { return *MemMgr; }
112 
113   /// Load the dynamic library at the given path and return a handle to it.
114   /// If LibraryPath is null this function will return the global handle for
115   /// the target process.
116   virtual Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) = 0;
117 
118   /// Search for symbols in the target process.
119   ///
120   /// The result of the lookup is a 2-dimentional array of target addresses
121   /// that correspond to the lookup order. If a required symbol is not
122   /// found then this method will return an error. If a weakly referenced
123   /// symbol is not found then it be assigned a '0' value in the result.
124   /// that correspond to the lookup order.
125   virtual Expected<std::vector<tpctypes::LookupResult>>
126   lookupSymbols(ArrayRef<tpctypes::LookupRequest> Request) = 0;
127 
128   /// Run function with a main-like signature.
129   virtual Expected<int32_t> runAsMain(JITTargetAddress MainFnAddr,
130                                       ArrayRef<std::string> Args) = 0;
131 
132   /// Run a wrapper function with signature:
133   ///
134   /// \code{.cpp}
135   ///   CWrapperFunctionResult fn(uint8_t *Data, uint64_t Size);
136   /// \endcode{.cpp}
137   ///
138   virtual Expected<tpctypes::WrapperFunctionResult>
139   runWrapper(JITTargetAddress WrapperFnAddr, ArrayRef<uint8_t> ArgBuffer) = 0;
140 
141   /// Disconnect from the target process.
142   ///
143   /// This should be called after the JIT session is shut down.
144   virtual Error disconnect() = 0;
145 
146 protected:
TargetProcessControl(std::shared_ptr<SymbolStringPool> SSP)147   TargetProcessControl(std::shared_ptr<SymbolStringPool> SSP)
148       : SSP(std::move(SSP)) {}
149 
150   std::shared_ptr<SymbolStringPool> SSP;
151   Triple TargetTriple;
152   unsigned PageSize = 0;
153   MemoryAccess *MemAccess = nullptr;
154   jitlink::JITLinkMemoryManager *MemMgr = nullptr;
155 };
156 
157 /// A TargetProcessControl implementation targeting the current process.
158 class SelfTargetProcessControl : public TargetProcessControl,
159                                  private TargetProcessControl::MemoryAccess {
160 public:
161   SelfTargetProcessControl(
162       std::shared_ptr<SymbolStringPool> SSP, Triple TargetTriple,
163       unsigned PageSize, std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr);
164 
165   /// Create a SelfTargetProcessControl with the given memory manager.
166   /// If no memory manager is given a jitlink::InProcessMemoryManager will
167   /// be used by default.
168   static Expected<std::unique_ptr<SelfTargetProcessControl>>
169   Create(std::shared_ptr<SymbolStringPool> SSP,
170          std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr = nullptr);
171 
172   Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) override;
173 
174   Expected<std::vector<tpctypes::LookupResult>>
175   lookupSymbols(ArrayRef<tpctypes::LookupRequest> Request) override;
176 
177   Expected<int32_t> runAsMain(JITTargetAddress MainFnAddr,
178                               ArrayRef<std::string> Args) override;
179 
180   Expected<tpctypes::WrapperFunctionResult>
181   runWrapper(JITTargetAddress WrapperFnAddr,
182              ArrayRef<uint8_t> ArgBuffer) override;
183 
184   Error disconnect() override;
185 
186 private:
187   void writeUInt8s(ArrayRef<tpctypes::UInt8Write> Ws,
188                    WriteResultFn OnWriteComplete) override;
189 
190   void writeUInt16s(ArrayRef<tpctypes::UInt16Write> Ws,
191                     WriteResultFn OnWriteComplete) override;
192 
193   void writeUInt32s(ArrayRef<tpctypes::UInt32Write> Ws,
194                     WriteResultFn OnWriteComplete) override;
195 
196   void writeUInt64s(ArrayRef<tpctypes::UInt64Write> Ws,
197                     WriteResultFn OnWriteComplete) override;
198 
199   void writeBuffers(ArrayRef<tpctypes::BufferWrite> Ws,
200                     WriteResultFn OnWriteComplete) override;
201 
202   std::unique_ptr<jitlink::JITLinkMemoryManager> OwnedMemMgr;
203   char GlobalManglingPrefix = 0;
204   std::vector<std::unique_ptr<sys::DynamicLibrary>> DynamicLibraries;
205 };
206 
207 } // end namespace orc
208 } // end namespace llvm
209 
210 #endif // LLVM_EXECUTIONENGINE_ORC_TARGETPROCESSCONTROL_H
211