summaryrefslogtreecommitdiff
path: root/tools/lli/RemoteTargetExternal.cpp
blob: c1bc8dfe99ca991144abbebd99b7bf2365445aac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//===---- RemoteTargetExternal.cpp - LLVM out-of-process JIT execution ----===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Implementation of the RemoteTargetExternal class which executes JITed code
// in a separate process from where it was built.
//
//===----------------------------------------------------------------------===//

#include "llvm/Config/config.h"
#include "RemoteTarget.h"
#include "RemoteTargetExternal.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/Memory.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/raw_ostream.h"
#include <string>

using namespace llvm;

bool RemoteTargetExternal::allocateSpace(size_t Size, unsigned Alignment,
                                 uint64_t &Address) {
  DEBUG(dbgs() << "Message [allocate space] size: " << Size <<
                  ", align: " << Alignment << "\n");
  if (!SendAllocateSpace(Alignment, Size)) {
    ErrorMsg += ", (RemoteTargetExternal::allocateSpace)";
    return false;
  }
  if (!Receive(LLI_AllocationResult, Address)) {
    ErrorMsg += ", (RemoteTargetExternal::allocateSpace)";
    return false;
  }
  if (Address == 0) {
    ErrorMsg += "failed allocation, (RemoteTargetExternal::allocateSpace)";
    return false;
  }
  DEBUG(dbgs() << "Message [allocate space] addr: 0x" <<
                  format("%llx", Address) << "\n");
  return true;
}

bool RemoteTargetExternal::loadData(uint64_t Address, const void *Data, size_t Size) {
  DEBUG(dbgs() << "Message [load data] addr: 0x" << format("%llx", Address) <<
                  ", size: " << Size << "\n");
  if (!SendLoadSection(Address, Data, (uint32_t)Size, false)) {
    ErrorMsg += ", (RemoteTargetExternal::loadData)";
    return false;
  }
  int Status = LLI_Status_Success;
  if (!Receive(LLI_LoadResult, Status)) {
    ErrorMsg += ", (RemoteTargetExternal::loadData)";
    return false;
  }
  if (Status == LLI_Status_IncompleteMsg) {
    ErrorMsg += "incomplete load data, (RemoteTargetExternal::loadData)";
    return false;
  }
  if (Status == LLI_Status_NotAllocated) {
    ErrorMsg += "data memory not allocated, (RemoteTargetExternal::loadData)";
    return false;
  }
  DEBUG(dbgs() << "Message [load data] complete\n");
  return true;
}

bool RemoteTargetExternal::loadCode(uint64_t Address, const void *Data, size_t Size) {
  DEBUG(dbgs() << "Message [load code] addr: 0x" << format("%llx", Address) <<
                  ", size: " << Size << "\n");
  if (!SendLoadSection(Address, Data, (uint32_t)Size, true)) {
    ErrorMsg += ", (RemoteTargetExternal::loadCode)";
    return false;
  }
  int Status = LLI_Status_Success;
  if (!Receive(LLI_LoadResult, Status)) {
    ErrorMsg += ", (RemoteTargetExternal::loadCode)";
    return false;
  }
  if (Status == LLI_Status_IncompleteMsg) {
    ErrorMsg += "incomplete load data, (RemoteTargetExternal::loadData)";
    return false;
  }
  if (Status == LLI_Status_NotAllocated) {
    ErrorMsg += "data memory not allocated, (RemoteTargetExternal::loadData)";
    return false;
  }
  DEBUG(dbgs() << "Message [load code] complete\n");
  return true;
}

bool RemoteTargetExternal::executeCode(uint64_t Address, int32_t &RetVal) {
  DEBUG(dbgs() << "Message [exectue code] addr: " << Address << "\n");
  if (!SendExecute(Address)) {
    ErrorMsg += ", (RemoteTargetExternal::executeCode)";
    return false;
  }
  if (!Receive(LLI_ExecutionResult, RetVal)) {
    ErrorMsg += ", (RemoteTargetExternal::executeCode)";
    return false;
  }
  DEBUG(dbgs() << "Message [exectue code] return: " << RetVal << "\n");
  return true;
}

void RemoteTargetExternal::stop() {
  SendTerminate();
  RPC.Wait();
}

bool RemoteTargetExternal::SendAllocateSpace(uint32_t Alignment, uint32_t Size) {
  if (!SendHeader(LLI_AllocateSpace)) {
    ErrorMsg += ", (RemoteTargetExternal::SendAllocateSpace)";
    return false;
  }

  AppendWrite((const void *)&Alignment, 4);
  AppendWrite((const void *)&Size, 4);

  if (!SendPayload()) {
    ErrorMsg += ", (RemoteTargetExternal::SendAllocateSpace)";
    return false;
  }
  return true;
}

bool RemoteTargetExternal::SendLoadSection(uint64_t Addr,
                                       const void *Data,
                                       uint32_t Size,
                                       bool IsCode) {
  LLIMessageType MsgType = IsCode ? LLI_LoadCodeSection : LLI_LoadDataSection;
  if (!SendHeader(MsgType)) {
    ErrorMsg += ", (RemoteTargetExternal::SendLoadSection)";
    return false;
  }

  AppendWrite((const void *)&Addr, 8);
  AppendWrite(Data, Size);

  if (!SendPayload()) {
    ErrorMsg += ", (RemoteTargetExternal::SendLoadSection)";
    return false;
  }
  return true;
}

bool RemoteTargetExternal::SendExecute(uint64_t Addr) {
  if (!SendHeader(LLI_Execute)) {
    ErrorMsg += ", (RemoteTargetExternal::SendExecute)";
    return false;
  }

  AppendWrite((const void *)&Addr, 8);

  if (!SendPayload()) {
    ErrorMsg += ", (RemoteTargetExternal::SendExecute)";
    return false;
  }
  return true;
}

bool RemoteTargetExternal::SendTerminate() {
  return SendHeader(LLI_Terminate);
  // No data or data size is sent with Terminate
}

bool RemoteTargetExternal::Receive(LLIMessageType Msg) {
  if (!ReceiveHeader(Msg))
    return false;
  int Unused;
  AppendRead(&Unused, 0);
  if (!ReceivePayload())
    return false;
  ReceiveData.clear();
  Sizes.clear();
  return true;
}

bool RemoteTargetExternal::Receive(LLIMessageType Msg, int32_t &Data) {
  if (!ReceiveHeader(Msg))
    return false;
  AppendRead(&Data, 4);
  if (!ReceivePayload())
    return false;
  ReceiveData.clear();
  Sizes.clear();
  return true;
}

bool RemoteTargetExternal::Receive(LLIMessageType Msg, uint64_t &Data) {
  if (!ReceiveHeader(Msg))
    return false;
  AppendRead(&Data, 8);
  if (!ReceivePayload())
    return false;
  ReceiveData.clear();
  Sizes.clear();
  return true;
}

bool RemoteTargetExternal::ReceiveHeader(LLIMessageType ExpectedMsgType) {
  assert(ReceiveData.empty() && Sizes.empty() &&
         "Payload vector not empty to receive header");

  // Message header, with type to follow
  uint32_t MsgType;
  if (!ReadBytes(&MsgType, 4)) {
    ErrorMsg += ", (RemoteTargetExternal::ReceiveHeader)";
    return false;
  }
  if (MsgType != (uint32_t)ExpectedMsgType) {
    ErrorMsg = "received unexpected message type";
    ErrorMsg += ". Expecting: ";
    ErrorMsg += ExpectedMsgType;
    ErrorMsg += ", Got: ";
    ErrorMsg += MsgType;
    return false;
  }
  return true;
}

bool RemoteTargetExternal::ReceivePayload() {
  assert(!ReceiveData.empty() &&
         "Payload vector empty to receive");
  assert(ReceiveData.size() == Sizes.size() &&
         "Unexpected mismatch between data and size");

  uint32_t TotalSize = 0;
  for (int I=0, E=Sizes.size(); I < E; I++)
    TotalSize += Sizes[I];

  // Payload size header
  uint32_t DataSize;
  if (!ReadBytes(&DataSize, 4)) {
    ErrorMsg += ", invalid data size";
    return false;
  }
  if (DataSize != TotalSize) {
    ErrorMsg = "unexpected data size";
    ErrorMsg += ". Expecting: ";
    ErrorMsg += TotalSize;
    ErrorMsg += ", Got: ";
    ErrorMsg += DataSize;
    return false;
  }
  if (DataSize == 0)
    return true;

  // Payload itself
  for (int I=0, E=Sizes.size(); I < E; I++) {
    if (!ReadBytes(ReceiveData[I], Sizes[I])) {
      ErrorMsg = "unexpected data while reading message";
      return false;
    }
  }

  return true;
}

bool RemoteTargetExternal::SendHeader(LLIMessageType MsgType) {
  assert(SendData.empty() && Sizes.empty() &&
         "Payload vector not empty to send header");

  // Message header, with type to follow
  if (!WriteBytes(&MsgType, 4)) {
    ErrorMsg += ", (RemoteTargetExternal::SendHeader)";
    return false;
  }
  return true;
}

bool RemoteTargetExternal::SendPayload() {
  assert(!SendData.empty() && !Sizes.empty() &&
         "Payload vector empty to send");
  assert(SendData.size() == Sizes.size() &&
         "Unexpected mismatch between data and size");

  uint32_t TotalSize = 0;
  for (int I=0, E=Sizes.size(); I < E; I++)
    TotalSize += Sizes[I];

  // Payload size header
  if (!WriteBytes(&TotalSize, 4)) {
    ErrorMsg += ", invalid data size";
    return false;
  }
  if (TotalSize == 0)
    return true;

  // Payload itself
  for (int I=0, E=Sizes.size(); I < E; I++) {
    if (!WriteBytes(SendData[I], Sizes[I])) {
      ErrorMsg = "unexpected data while writing message";
      return false;
    }
  }

  SendData.clear();
  Sizes.clear();
  return true;
}

void RemoteTargetExternal::AppendWrite(const void *Data, uint32_t Size) {
  SendData.push_back(Data);
  Sizes.push_back(Size);
}

void RemoteTargetExternal::AppendRead(void *Data, uint32_t Size) {
  ReceiveData.push_back(Data);
  Sizes.push_back(Size);
}

#ifdef LLVM_ON_UNIX
#include "Unix/RPCChannel.inc"
#endif

#ifdef LLVM_ON_WIN32
#include "Windows/RPCChannel.inc"
#endif