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 int rc = RPC.WriteBytes(Data, Size);
36 if (rc != -1 && (size_t)rc == Size)
39 ErrorMsg = "WriteBytes: ";
40 RPC.ReportError(rc, Size, ErrorMsg);
44 bool ReadBytes(void *Data, size_t Size) {
45 int rc = RPC.ReadBytes(Data, Size);
46 if (rc != -1 && (size_t)rc == Size)
49 ErrorMsg = "ReadBytes: ";
50 RPC.ReportError(rc, Size, ErrorMsg);
55 /// Allocate space in the remote target address space.
57 /// @param Size Amount of space, in bytes, to allocate.
58 /// @param Alignment Required minimum alignment for allocated space.
59 /// @param[out] Address Remote address of the allocated memory.
61 /// @returns True on success. On failure, ErrorMsg is updated with
62 /// descriptive text of the encountered error.
63 virtual bool allocateSpace(size_t Size,
67 /// Load data into the target address space.
69 /// @param Address Destination address in the target process.
70 /// @param Data Source address in the host process.
71 /// @param Size Number of bytes to copy.
73 /// @returns True on success. On failure, ErrorMsg is updated with
74 /// descriptive text of the encountered error.
75 virtual bool loadData(uint64_t Address, const void *Data, size_t Size);
77 /// Load code into the target address space and prepare it for execution.
79 /// @param Address Destination address in the target process.
80 /// @param Data Source address in the host process.
81 /// @param Size Number of bytes to copy.
83 /// @returns True on success. On failure, ErrorMsg is updated with
84 /// descriptive text of the encountered error.
85 virtual bool loadCode(uint64_t Address, const void *Data, size_t Size);
87 /// Execute code in the target process. The called function is required
88 /// to be of signature int "(*)(void)".
90 /// @param Address Address of the loaded function in the target
92 /// @param[out] RetVal The integer return value of the called function.
94 /// @returns True on success. On failure, ErrorMsg is updated with
95 /// descriptive text of the encountered error.
96 virtual bool executeCode(uint64_t Address, int &RetVal);
98 /// Minimum alignment for memory permissions. Used to seperate code and
99 /// data regions to make sure data doesn't get marked as code or vice
102 /// @returns Page alignment return value. Default of 4k.
103 virtual unsigned getPageAlignment() { return 4096; }
105 virtual bool create() {
106 RPC.ChildName = ChildName;
107 if (!RPC.createServer())
110 // We must get Ack from the client (blocking read)
111 if (!Receive(LLI_ChildActive)) {
112 ErrorMsg += ", (RPCChannel::create) - Stopping process!";
120 /// Terminate the remote process.
123 RemoteTargetExternal(std::string &Name) : RemoteTarget(), ChildName(Name) {}
124 virtual ~RemoteTargetExternal() {}
127 std::string ChildName;
129 bool SendAllocateSpace(uint32_t Alignment, uint32_t Size);
130 bool SendLoadSection(uint64_t Addr,
134 bool SendExecute(uint64_t Addr);
135 bool SendTerminate();
137 // High-level wrappers for receiving data
138 bool Receive(LLIMessageType Msg);
139 bool Receive(LLIMessageType Msg, int32_t &Data);
140 bool Receive(LLIMessageType Msg, uint64_t &Data);
142 // Lower level target-independent read/write to deal with errors
143 bool ReceiveHeader(LLIMessageType Msg);
144 bool ReceivePayload();
145 bool SendHeader(LLIMessageType Msg);
148 // Functions to append/retrieve data from the payload
149 SmallVector<const void *, 2> SendData;
150 SmallVector<void *, 1> ReceiveData; // Future proof
151 SmallVector<int, 2> Sizes;
152 void AppendWrite(const void *Data, uint32_t Size);
153 void AppendRead(void *Data, uint32_t Size);
156 } // end namespace llvm
158 #endif // LLI_REMOTETARGETEXTERNAL_H