1 //===---- RemoteTargetExternal.cpp - 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 // Implementation of the RemoteTargetExternal class which executes JITed code
11 // in a separate process from where it was built.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Config/config.h"
16 #include "RemoteTarget.h"
17 #include "RemoteTargetExternal.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/DataTypes.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/Format.h"
22 #include "llvm/Support/Memory.h"
23 #include "llvm/Support/Program.h"
24 #include "llvm/Support/raw_ostream.h"
29 bool RemoteTargetExternal::allocateSpace(size_t Size, unsigned Alignment,
31 DEBUG(dbgs() << "Message [allocate space] size: " << Size <<
32 ", align: " << Alignment << "\n");
33 if (!SendAllocateSpace(Alignment, Size)) {
34 ErrorMsg += ", (RemoteTargetExternal::allocateSpace)";
37 if (!Receive(LLI_AllocationResult, Address)) {
38 ErrorMsg += ", (RemoteTargetExternal::allocateSpace)";
42 ErrorMsg += "failed allocation, (RemoteTargetExternal::allocateSpace)";
45 DEBUG(dbgs() << "Message [allocate space] addr: 0x" <<
46 format("%llx", Address) << "\n");
50 bool RemoteTargetExternal::loadData(uint64_t Address, const void *Data, size_t Size) {
51 DEBUG(dbgs() << "Message [load data] addr: 0x" << format("%llx", Address) <<
52 ", size: " << Size << "\n");
53 if (!SendLoadSection(Address, Data, (uint32_t)Size, false)) {
54 ErrorMsg += ", (RemoteTargetExternal::loadData)";
57 int Status = LLI_Status_Success;
58 if (!Receive(LLI_LoadResult, Status)) {
59 ErrorMsg += ", (RemoteTargetExternal::loadData)";
62 if (Status == LLI_Status_IncompleteMsg) {
63 ErrorMsg += "incomplete load data, (RemoteTargetExternal::loadData)";
66 if (Status == LLI_Status_NotAllocated) {
67 ErrorMsg += "data memory not allocated, (RemoteTargetExternal::loadData)";
70 DEBUG(dbgs() << "Message [load data] complete\n");
74 bool RemoteTargetExternal::loadCode(uint64_t Address, const void *Data, size_t Size) {
75 DEBUG(dbgs() << "Message [load code] addr: 0x" << format("%llx", Address) <<
76 ", size: " << Size << "\n");
77 if (!SendLoadSection(Address, Data, (uint32_t)Size, true)) {
78 ErrorMsg += ", (RemoteTargetExternal::loadCode)";
81 int Status = LLI_Status_Success;
82 if (!Receive(LLI_LoadResult, Status)) {
83 ErrorMsg += ", (RemoteTargetExternal::loadCode)";
86 if (Status == LLI_Status_IncompleteMsg) {
87 ErrorMsg += "incomplete load data, (RemoteTargetExternal::loadData)";
90 if (Status == LLI_Status_NotAllocated) {
91 ErrorMsg += "data memory not allocated, (RemoteTargetExternal::loadData)";
94 DEBUG(dbgs() << "Message [load code] complete\n");
98 bool RemoteTargetExternal::executeCode(uint64_t Address, int32_t &RetVal) {
99 DEBUG(dbgs() << "Message [exectue code] addr: " << Address << "\n");
100 if (!SendExecute(Address)) {
101 ErrorMsg += ", (RemoteTargetExternal::executeCode)";
104 if (!Receive(LLI_ExecutionResult, RetVal)) {
105 ErrorMsg += ", (RemoteTargetExternal::executeCode)";
108 DEBUG(dbgs() << "Message [exectue code] return: " << RetVal << "\n");
112 void RemoteTargetExternal::stop() {
117 bool RemoteTargetExternal::SendAllocateSpace(uint32_t Alignment, uint32_t Size) {
118 if (!SendHeader(LLI_AllocateSpace)) {
119 ErrorMsg += ", (RemoteTargetExternal::SendAllocateSpace)";
123 AppendWrite((const void *)&Alignment, 4);
124 AppendWrite((const void *)&Size, 4);
126 if (!SendPayload()) {
127 ErrorMsg += ", (RemoteTargetExternal::SendAllocateSpace)";
133 bool RemoteTargetExternal::SendLoadSection(uint64_t Addr,
137 LLIMessageType MsgType = IsCode ? LLI_LoadCodeSection : LLI_LoadDataSection;
138 if (!SendHeader(MsgType)) {
139 ErrorMsg += ", (RemoteTargetExternal::SendLoadSection)";
143 AppendWrite((const void *)&Addr, 8);
144 AppendWrite(Data, Size);
146 if (!SendPayload()) {
147 ErrorMsg += ", (RemoteTargetExternal::SendLoadSection)";
153 bool RemoteTargetExternal::SendExecute(uint64_t Addr) {
154 if (!SendHeader(LLI_Execute)) {
155 ErrorMsg += ", (RemoteTargetExternal::SendExecute)";
159 AppendWrite((const void *)&Addr, 8);
161 if (!SendPayload()) {
162 ErrorMsg += ", (RemoteTargetExternal::SendExecute)";
168 bool RemoteTargetExternal::SendTerminate() {
169 return SendHeader(LLI_Terminate);
170 // No data or data size is sent with Terminate
173 bool RemoteTargetExternal::Receive(LLIMessageType Msg) {
174 if (!ReceiveHeader(Msg))
177 AppendRead(&Unused, 0);
178 if (!ReceivePayload())
185 bool RemoteTargetExternal::Receive(LLIMessageType Msg, int32_t &Data) {
186 if (!ReceiveHeader(Msg))
188 AppendRead(&Data, 4);
189 if (!ReceivePayload())
196 bool RemoteTargetExternal::Receive(LLIMessageType Msg, uint64_t &Data) {
197 if (!ReceiveHeader(Msg))
199 AppendRead(&Data, 8);
200 if (!ReceivePayload())
207 bool RemoteTargetExternal::ReceiveHeader(LLIMessageType ExpectedMsgType) {
208 assert(ReceiveData.empty() && Sizes.empty() &&
209 "Payload vector not empty to receive header");
211 // Message header, with type to follow
213 if (!ReadBytes(&MsgType, 4)) {
214 ErrorMsg += ", (RemoteTargetExternal::ReceiveHeader)";
217 if (MsgType != (uint32_t)ExpectedMsgType) {
218 ErrorMsg = "received unexpected message type";
219 ErrorMsg += ". Expecting: ";
220 ErrorMsg += ExpectedMsgType;
221 ErrorMsg += ", Got: ";
228 bool RemoteTargetExternal::ReceivePayload() {
229 assert(!ReceiveData.empty() &&
230 "Payload vector empty to receive");
231 assert(ReceiveData.size() == Sizes.size() &&
232 "Unexpected mismatch between data and size");
234 uint32_t TotalSize = 0;
235 for (int I=0, E=Sizes.size(); I < E; I++)
236 TotalSize += Sizes[I];
238 // Payload size header
240 if (!ReadBytes(&DataSize, 4)) {
241 ErrorMsg += ", invalid data size";
244 if (DataSize != TotalSize) {
245 ErrorMsg = "unexpected data size";
246 ErrorMsg += ". Expecting: ";
247 ErrorMsg += TotalSize;
248 ErrorMsg += ", Got: ";
249 ErrorMsg += DataSize;
256 for (int I=0, E=Sizes.size(); I < E; I++) {
257 if (!ReadBytes(ReceiveData[I], Sizes[I])) {
258 ErrorMsg = "unexpected data while reading message";
266 bool RemoteTargetExternal::SendHeader(LLIMessageType MsgType) {
267 assert(SendData.empty() && Sizes.empty() &&
268 "Payload vector not empty to send header");
270 // Message header, with type to follow
271 if (!WriteBytes(&MsgType, 4)) {
272 ErrorMsg += ", (RemoteTargetExternal::SendHeader)";
278 bool RemoteTargetExternal::SendPayload() {
279 assert(!SendData.empty() && !Sizes.empty() &&
280 "Payload vector empty to send");
281 assert(SendData.size() == Sizes.size() &&
282 "Unexpected mismatch between data and size");
284 uint32_t TotalSize = 0;
285 for (int I=0, E=Sizes.size(); I < E; I++)
286 TotalSize += Sizes[I];
288 // Payload size header
289 if (!WriteBytes(&TotalSize, 4)) {
290 ErrorMsg += ", invalid data size";
297 for (int I=0, E=Sizes.size(); I < E; I++) {
298 if (!WriteBytes(SendData[I], Sizes[I])) {
299 ErrorMsg = "unexpected data while writing message";
309 void RemoteTargetExternal::AppendWrite(const void *Data, uint32_t Size) {
310 SendData.push_back(Data);
311 Sizes.push_back(Size);
314 void RemoteTargetExternal::AppendRead(void *Data, uint32_t Size) {
315 ReceiveData.push_back(Data);
316 Sizes.push_back(Size);
320 #include "Unix/RPCChannel.inc"
324 #include "Windows/RPCChannel.inc"