1 //===----- RemoteTargetExternal.h - LLVM out-of-process JIT execution -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Definition of the RemoteTargetExternal class which executes JITed code in a
11 // separate process from where it was built.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLI_REMOTETARGETEXTERNAL_H
16 #define LLI_REMOTETARGETEXTERNAL_H
18 #include "RPCChannel.h"
19 #include "RemoteTarget.h"
20 #include "RemoteTargetMessage.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/Config/config.h"
24 #include "llvm/Support/DataTypes.h"
25 #include "llvm/Support/Memory.h"
31 class RemoteTargetExternal : public RemoteTarget {
34 bool WriteBytes(const void *Data, size_t Size) {
35 return RPC.WriteBytes(Data, Size);
38 bool ReadBytes(void *Data, size_t Size) { return RPC.ReadBytes(Data, Size); }
41 /// Allocate space in the remote target address space.
43 /// @param Size Amount of space, in bytes, to allocate.
44 /// @param Alignment Required minimum alignment for allocated space.
45 /// @param[out] Address Remote address of the allocated memory.
47 /// @returns True on success. On failure, ErrorMsg is updated with
48 /// descriptive text of the encountered error.
49 virtual bool allocateSpace(size_t Size,
53 /// Load data into the target address space.
55 /// @param Address Destination address in the target process.
56 /// @param Data Source address in the host process.
57 /// @param Size Number of bytes to copy.
59 /// @returns True on success. On failure, ErrorMsg is updated with
60 /// descriptive text of the encountered error.
61 virtual bool loadData(uint64_t Address, const void *Data, size_t Size);
63 /// Load code into the target address space and prepare it for execution.
65 /// @param Address Destination address in the target process.
66 /// @param Data Source address in the host process.
67 /// @param Size Number of bytes to copy.
69 /// @returns True on success. On failure, ErrorMsg is updated with
70 /// descriptive text of the encountered error.
71 virtual bool loadCode(uint64_t Address, const void *Data, size_t Size);
73 /// Execute code in the target process. The called function is required
74 /// to be of signature int "(*)(void)".
76 /// @param Address Address of the loaded function in the target
78 /// @param[out] RetVal The integer return value of the called function.
80 /// @returns True on success. On failure, ErrorMsg is updated with
81 /// descriptive text of the encountered error.
82 virtual bool executeCode(uint64_t Address, int &RetVal);
84 /// Minimum alignment for memory permissions. Used to separate code and
85 /// data regions to make sure data doesn't get marked as code or vice
88 /// @returns Page alignment return value. Default of 4k.
89 virtual unsigned getPageAlignment() { return 4096; }
91 virtual bool create() {
92 RPC.ChildName = ChildName;
93 if (!RPC.createServer())
96 // We must get Ack from the client (blocking read)
97 if (!Receive(LLI_ChildActive)) {
98 ErrorMsg += ", (RPCChannel::create) - Stopping process!";
106 /// Terminate the remote process.
109 RemoteTargetExternal(std::string &Name) : RemoteTarget(), ChildName(Name) {}
110 virtual ~RemoteTargetExternal() {}
113 std::string ChildName;
115 bool SendAllocateSpace(uint32_t Alignment, uint32_t Size);
116 bool SendLoadSection(uint64_t Addr,
120 bool SendExecute(uint64_t Addr);
121 bool SendTerminate();
123 // High-level wrappers for receiving data
124 bool Receive(LLIMessageType Msg);
125 bool Receive(LLIMessageType Msg, int32_t &Data);
126 bool Receive(LLIMessageType Msg, uint64_t &Data);
128 // Lower level target-independent read/write to deal with errors
129 bool ReceiveHeader(LLIMessageType Msg);
130 bool ReceivePayload();
131 bool SendHeader(LLIMessageType Msg);
134 // Functions to append/retrieve data from the payload
135 SmallVector<const void *, 2> SendData;
136 SmallVector<void *, 1> ReceiveData; // Future proof
137 SmallVector<int, 2> Sizes;
138 void AppendWrite(const void *Data, uint32_t Size);
139 void AppendRead(void *Data, uint32_t Size);
142 } // end namespace llvm
144 #endif // LLI_REMOTETARGETEXTERNAL_H