1 //===--- TargetProcessControl.h - Target process control APIs ---*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // Utilities for interacting with target processes.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_EXECUTIONENGINE_ORC_TARGETPROCESSCONTROL_H
14 #define LLVM_EXECUTIONENGINE_ORC_TARGETPROCESSCONTROL_H
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"
31 /// TargetProcessControl supports interaction with a JIT target process.
32 class TargetProcessControl {
34 /// APIs for manipulating memory in the target process.
37 /// Callback function for asynchronous writes.
38 using WriteResultFn = unique_function<void(Error)>;
40 virtual ~MemoryAccess();
42 virtual void writeUInt8s(ArrayRef<tpctypes::UInt8Write> Ws,
43 WriteResultFn OnWriteComplete) = 0;
45 virtual void writeUInt16s(ArrayRef<tpctypes::UInt16Write> Ws,
46 WriteResultFn OnWriteComplete) = 0;
48 virtual void writeUInt32s(ArrayRef<tpctypes::UInt32Write> Ws,
49 WriteResultFn OnWriteComplete) = 0;
51 virtual void writeUInt64s(ArrayRef<tpctypes::UInt64Write> Ws,
52 WriteResultFn OnWriteComplete) = 0;
54 virtual void writeBuffers(ArrayRef<tpctypes::BufferWrite> Ws,
55 WriteResultFn OnWriteComplete) = 0;
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)); });
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)); });
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)); });
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)); });
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)); });
93 virtual ~TargetProcessControl();
95 /// Intern a symbol name in the SymbolStringPool.
96 SymbolStringPtr intern(StringRef SymName) { return SSP->intern(SymName); }
98 /// Return a shared pointer to the SymbolStringPool for this instance.
99 std::shared_ptr<SymbolStringPool> getSymbolStringPool() const { return SSP; }
101 /// Return the Triple for the target process.
102 const Triple &getTargetTriple() const { return TargetTriple; }
104 /// Get the page size for the target process.
105 unsigned getPageSize() const { return PageSize; }
107 /// Return a MemoryAccess object for the target process.
108 MemoryAccess &getMemoryAccess() const { return *MemAccess; }
110 /// Return a JITLinkMemoryManager for the target process.
111 jitlink::JITLinkMemoryManager &getMemMgr() const { return *MemMgr; }
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;
118 /// Search for symbols in the target process.
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;
128 /// Run function with a main-like signature.
129 virtual Expected<int32_t> runAsMain(JITTargetAddress MainFnAddr,
130 ArrayRef<std::string> Args) = 0;
132 /// Run a wrapper function with signature:
135 /// CWrapperFunctionResult fn(uint8_t *Data, uint64_t Size);
138 virtual Expected<tpctypes::WrapperFunctionResult>
139 runWrapper(JITTargetAddress WrapperFnAddr, ArrayRef<uint8_t> ArgBuffer) = 0;
141 /// Disconnect from the target process.
143 /// This should be called after the JIT session is shut down.
144 virtual Error disconnect() = 0;
147 TargetProcessControl(std::shared_ptr<SymbolStringPool> SSP)
148 : SSP(std::move(SSP)) {}
150 std::shared_ptr<SymbolStringPool> SSP;
152 unsigned PageSize = 0;
153 MemoryAccess *MemAccess = nullptr;
154 jitlink::JITLinkMemoryManager *MemMgr = nullptr;
157 /// A TargetProcessControl implementation targeting the current process.
158 class SelfTargetProcessControl : public TargetProcessControl,
159 private TargetProcessControl::MemoryAccess {
161 SelfTargetProcessControl(
162 std::shared_ptr<SymbolStringPool> SSP, Triple TargetTriple,
163 unsigned PageSize, std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr);
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);
172 Expected<tpctypes::DylibHandle> loadDylib(const char *DylibPath) override;
174 Expected<std::vector<tpctypes::LookupResult>>
175 lookupSymbols(ArrayRef<tpctypes::LookupRequest> Request) override;
177 Expected<int32_t> runAsMain(JITTargetAddress MainFnAddr,
178 ArrayRef<std::string> Args) override;
180 Expected<tpctypes::WrapperFunctionResult>
181 runWrapper(JITTargetAddress WrapperFnAddr,
182 ArrayRef<uint8_t> ArgBuffer) override;
184 Error disconnect() override;
187 void writeUInt8s(ArrayRef<tpctypes::UInt8Write> Ws,
188 WriteResultFn OnWriteComplete) override;
190 void writeUInt16s(ArrayRef<tpctypes::UInt16Write> Ws,
191 WriteResultFn OnWriteComplete) override;
193 void writeUInt32s(ArrayRef<tpctypes::UInt32Write> Ws,
194 WriteResultFn OnWriteComplete) override;
196 void writeUInt64s(ArrayRef<tpctypes::UInt64Write> Ws,
197 WriteResultFn OnWriteComplete) override;
199 void writeBuffers(ArrayRef<tpctypes::BufferWrite> Ws,
200 WriteResultFn OnWriteComplete) override;
202 std::unique_ptr<jitlink::JITLinkMemoryManager> OwnedMemMgr;
203 char GlobalManglingPrefix = 0;
204 std::vector<std::unique_ptr<sys::DynamicLibrary>> DynamicLibraries;
207 } // end namespace orc
208 } // end namespace llvm
210 #endif // LLVM_EXECUTIONENGINE_ORC_TARGETPROCESSCONTROL_H