RemoteObjectLayerTest.cpp
18.7 KB
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
//===---------------------- RemoteObjectLayerTest.cpp ---------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/ExecutionEngine/Orc/CompileUtils.h"
#include "llvm/ExecutionEngine/Orc/NullResolver.h"
#include "llvm/ExecutionEngine/Orc/RemoteObjectLayer.h"
#include "OrcTestCommon.h"
#include "QueueChannel.h"
#include "gtest/gtest.h"
using namespace llvm;
using namespace llvm::orc;
namespace {
class MockObjectLayer {
public:
using ObjHandleT = uint64_t;
using ObjectPtr = std::unique_ptr<MemoryBuffer>;
using LookupFn = std::function<JITSymbol(StringRef, bool)>;
using SymbolLookupTable = std::map<ObjHandleT, LookupFn>;
using AddObjectFtor =
std::function<Expected<ObjHandleT>(ObjectPtr, SymbolLookupTable&)>;
class ObjectNotFound : public remote::ResourceNotFound<ObjHandleT> {
public:
ObjectNotFound(ObjHandleT H) : ResourceNotFound(H, "Object handle") {}
};
MockObjectLayer(AddObjectFtor AddObject)
: AddObject(std::move(AddObject)) {}
Expected<ObjHandleT> addObject(ObjectPtr Obj,
std::shared_ptr<JITSymbolResolver> Resolver) {
return AddObject(std::move(Obj), SymTab);
}
Error removeObject(ObjHandleT H) {
if (SymTab.count(H))
return Error::success();
else
return make_error<ObjectNotFound>(H);
}
JITSymbol findSymbol(StringRef Name, bool ExportedSymbolsOnly) {
for (auto KV : SymTab) {
if (auto Sym = KV.second(Name, ExportedSymbolsOnly))
return Sym;
else if (auto Err = Sym.takeError())
return std::move(Err);
}
return JITSymbol(nullptr);
}
JITSymbol findSymbolIn(ObjHandleT H, StringRef Name,
bool ExportedSymbolsOnly) {
auto LI = SymTab.find(H);
if (LI != SymTab.end())
return LI->second(Name, ExportedSymbolsOnly);
else
return make_error<ObjectNotFound>(H);
}
Error emitAndFinalize(ObjHandleT H) {
if (SymTab.count(H))
return Error::success();
else
return make_error<ObjectNotFound>(H);
}
private:
AddObjectFtor AddObject;
SymbolLookupTable SymTab;
};
using RPCEndpoint = rpc::SingleThreadedRPCEndpoint<rpc::RawByteChannel>;
MockObjectLayer::ObjectPtr createTestObject() {
OrcNativeTarget::initialize();
auto TM = std::unique_ptr<TargetMachine>(EngineBuilder().selectTarget());
if (!TM)
return nullptr;
LLVMContext Ctx;
ModuleBuilder MB(Ctx, TM->getTargetTriple().str(), "TestModule");
MB.getModule()->setDataLayout(TM->createDataLayout());
auto *Main = MB.createFunctionDecl(
FunctionType::get(Type::getInt32Ty(Ctx),
{Type::getInt32Ty(Ctx),
Type::getInt8PtrTy(Ctx)->getPointerTo()},
false),
"main");
Main->getBasicBlockList().push_back(BasicBlock::Create(Ctx));
IRBuilder<> B(&Main->back());
B.CreateRet(ConstantInt::getSigned(Type::getInt32Ty(Ctx), 42));
SimpleCompiler IRCompiler(*TM);
return cantFail(IRCompiler(*MB.getModule()));
}
TEST(RemoteObjectLayer, AddObject) {
llvm::orc::rpc::registerStringError<rpc::RawByteChannel>();
auto TestObject = createTestObject();
if (!TestObject)
return;
auto Channels = createPairedQueueChannels();
auto ReportError = [](Error Err) {
logAllUnhandledErrors(std::move(Err), llvm::errs());
};
// Copy the bytes out of the test object: the copy will be used to verify
// that the original is correctly transmitted over RPC to the mock layer.
StringRef ObjBytes = TestObject->getBuffer();
std::vector<char> ObjContents(ObjBytes.size());
std::copy(ObjBytes.begin(), ObjBytes.end(), ObjContents.begin());
RPCEndpoint ClientEP(*Channels.first, true);
RemoteObjectClientLayer<RPCEndpoint> Client(AcknowledgeORCv1Deprecation,
ClientEP, ReportError);
RPCEndpoint ServerEP(*Channels.second, true);
MockObjectLayer BaseLayer(
[&ObjContents](MockObjectLayer::ObjectPtr Obj,
MockObjectLayer::SymbolLookupTable &SymTab) {
// Check that the received object file content matches the original.
StringRef RPCObjContents = Obj->getBuffer();
EXPECT_EQ(RPCObjContents.size(), ObjContents.size())
<< "RPC'd object file has incorrect size";
EXPECT_TRUE(std::equal(RPCObjContents.begin(), RPCObjContents.end(),
ObjContents.begin()))
<< "RPC'd object file content does not match original content";
return 1;
});
RemoteObjectServerLayer<MockObjectLayer, RPCEndpoint> Server(
AcknowledgeORCv1Deprecation, BaseLayer, ServerEP, ReportError);
bool Finished = false;
ServerEP.addHandler<remote::utils::TerminateSession>(
[&]() { Finished = true; }
);
auto ServerThread =
std::thread([&]() {
while (!Finished)
cantFail(ServerEP.handleOne());
});
cantFail(Client.addObject(std::move(TestObject),
std::make_shared<NullLegacyResolver>()));
cantFail(ClientEP.callB<remote::utils::TerminateSession>());
ServerThread.join();
}
TEST(RemoteObjectLayer, AddObjectFailure) {
llvm::orc::rpc::registerStringError<rpc::RawByteChannel>();
auto TestObject = createTestObject();
if (!TestObject)
return;
auto Channels = createPairedQueueChannels();
auto ReportError =
[](Error Err) {
auto ErrMsg = toString(std::move(Err));
EXPECT_EQ(ErrMsg, "AddObjectFailure - Test Message")
<< "Expected error string to be \"AddObjectFailure - Test Message\"";
};
RPCEndpoint ClientEP(*Channels.first, true);
RemoteObjectClientLayer<RPCEndpoint> Client(AcknowledgeORCv1Deprecation,
ClientEP, ReportError);
RPCEndpoint ServerEP(*Channels.second, true);
MockObjectLayer BaseLayer(
[](MockObjectLayer::ObjectPtr Obj,
MockObjectLayer::SymbolLookupTable &SymTab)
-> Expected<MockObjectLayer::ObjHandleT> {
return make_error<StringError>("AddObjectFailure - Test Message",
inconvertibleErrorCode());
});
RemoteObjectServerLayer<MockObjectLayer, RPCEndpoint> Server(
AcknowledgeORCv1Deprecation, BaseLayer, ServerEP, ReportError);
bool Finished = false;
ServerEP.addHandler<remote::utils::TerminateSession>(
[&]() { Finished = true; }
);
auto ServerThread =
std::thread([&]() {
while (!Finished)
cantFail(ServerEP.handleOne());
});
auto HandleOrErr = Client.addObject(std::move(TestObject),
std::make_shared<NullLegacyResolver>());
EXPECT_FALSE(HandleOrErr) << "Expected error from addObject";
auto ErrMsg = toString(HandleOrErr.takeError());
EXPECT_EQ(ErrMsg, "AddObjectFailure - Test Message")
<< "Expected error string to be \"AddObjectFailure - Test Message\"";
cantFail(ClientEP.callB<remote::utils::TerminateSession>());
ServerThread.join();
}
TEST(RemoteObjectLayer, RemoveObject) {
llvm::orc::rpc::registerStringError<rpc::RawByteChannel>();
auto TestObject = createTestObject();
if (!TestObject)
return;
auto Channels = createPairedQueueChannels();
auto ReportError = [](Error Err) {
logAllUnhandledErrors(std::move(Err), llvm::errs());
};
RPCEndpoint ClientEP(*Channels.first, true);
RemoteObjectClientLayer<RPCEndpoint> Client(AcknowledgeORCv1Deprecation,
ClientEP, ReportError);
RPCEndpoint ServerEP(*Channels.second, true);
MockObjectLayer BaseLayer(
[](MockObjectLayer::ObjectPtr Obj,
MockObjectLayer::SymbolLookupTable &SymTab) {
SymTab[1] = MockObjectLayer::LookupFn();
return 1;
});
RemoteObjectServerLayer<MockObjectLayer, RPCEndpoint> Server(
AcknowledgeORCv1Deprecation, BaseLayer, ServerEP, ReportError);
bool Finished = false;
ServerEP.addHandler<remote::utils::TerminateSession>(
[&]() { Finished = true; }
);
auto ServerThread =
std::thread([&]() {
while (!Finished)
cantFail(ServerEP.handleOne());
});
auto H = cantFail(Client.addObject(std::move(TestObject),
std::make_shared<NullLegacyResolver>()));
cantFail(Client.removeObject(H));
cantFail(ClientEP.callB<remote::utils::TerminateSession>());
ServerThread.join();
}
TEST(RemoteObjectLayer, RemoveObjectFailure) {
llvm::orc::rpc::registerStringError<rpc::RawByteChannel>();
auto TestObject = createTestObject();
if (!TestObject)
return;
auto Channels = createPairedQueueChannels();
auto ReportError =
[](Error Err) {
auto ErrMsg = toString(std::move(Err));
EXPECT_EQ(ErrMsg, "Object handle 42 not found")
<< "Expected error string to be \"Object handle 42 not found\"";
};
RPCEndpoint ClientEP(*Channels.first, true);
RemoteObjectClientLayer<RPCEndpoint> Client(AcknowledgeORCv1Deprecation,
ClientEP, ReportError);
RPCEndpoint ServerEP(*Channels.second, true);
// AddObject lambda does not update symbol table, so removeObject will treat
// this as a bad object handle.
MockObjectLayer BaseLayer(
[](MockObjectLayer::ObjectPtr Obj,
MockObjectLayer::SymbolLookupTable &SymTab) {
return 42;
});
RemoteObjectServerLayer<MockObjectLayer, RPCEndpoint> Server(
AcknowledgeORCv1Deprecation, BaseLayer, ServerEP, ReportError);
bool Finished = false;
ServerEP.addHandler<remote::utils::TerminateSession>(
[&]() { Finished = true; }
);
auto ServerThread =
std::thread([&]() {
while (!Finished)
cantFail(ServerEP.handleOne());
});
auto H = cantFail(Client.addObject(std::move(TestObject),
std::make_shared<NullLegacyResolver>()));
auto Err = Client.removeObject(H);
EXPECT_TRUE(!!Err) << "Expected error from removeObject";
auto ErrMsg = toString(std::move(Err));
EXPECT_EQ(ErrMsg, "Object handle 42 not found")
<< "Expected error string to be \"Object handle 42 not found\"";
cantFail(ClientEP.callB<remote::utils::TerminateSession>());
ServerThread.join();
}
TEST(RemoteObjectLayer, FindSymbol) {
llvm::orc::rpc::registerStringError<rpc::RawByteChannel>();
auto TestObject = createTestObject();
if (!TestObject)
return;
auto Channels = createPairedQueueChannels();
auto ReportError =
[](Error Err) {
auto ErrMsg = toString(std::move(Err));
EXPECT_EQ(ErrMsg, "Could not find symbol 'badsymbol'")
<< "Expected error string to be \"Object handle 42 not found\"";
};
RPCEndpoint ClientEP(*Channels.first, true);
RemoteObjectClientLayer<RPCEndpoint> Client(AcknowledgeORCv1Deprecation,
ClientEP, ReportError);
RPCEndpoint ServerEP(*Channels.second, true);
// AddObject lambda does not update symbol table, so removeObject will treat
// this as a bad object handle.
MockObjectLayer BaseLayer(
[](MockObjectLayer::ObjectPtr Obj,
MockObjectLayer::SymbolLookupTable &SymTab) {
SymTab[42] =
[](StringRef Name, bool ExportedSymbolsOnly) -> JITSymbol {
if (Name == "foobar")
return JITSymbol(0x12348765, JITSymbolFlags::Exported);
if (Name == "badsymbol")
return make_error<JITSymbolNotFound>(Name);
return nullptr;
};
return 42;
});
RemoteObjectServerLayer<MockObjectLayer, RPCEndpoint> Server(
AcknowledgeORCv1Deprecation, BaseLayer, ServerEP, ReportError);
bool Finished = false;
ServerEP.addHandler<remote::utils::TerminateSession>(
[&]() { Finished = true; }
);
auto ServerThread =
std::thread([&]() {
while (!Finished)
cantFail(ServerEP.handleOne());
});
cantFail(Client.addObject(std::move(TestObject),
std::make_shared<NullLegacyResolver>()));
// Check that we can find and materialize a valid symbol.
auto Sym1 = Client.findSymbol("foobar", true);
EXPECT_TRUE(!!Sym1) << "Symbol 'foobar' should be findable";
EXPECT_EQ(cantFail(Sym1.getAddress()), 0x12348765ULL)
<< "Symbol 'foobar' does not return the correct address";
{
// Check that we can return a symbol containing an error.
auto Sym2 = Client.findSymbol("badsymbol", true);
EXPECT_FALSE(!!Sym2) << "Symbol 'badsymbol' should not be findable";
auto Err = Sym2.takeError();
EXPECT_TRUE(!!Err) << "Sym2 should contain an error value";
auto ErrMsg = toString(std::move(Err));
EXPECT_EQ(ErrMsg, "Could not find symbol 'badsymbol'")
<< "Expected symbol-not-found error for Sym2";
}
{
// Check that we can return a 'null' symbol.
auto Sym3 = Client.findSymbol("baz", true);
EXPECT_FALSE(!!Sym3) << "Symbol 'baz' should convert to false";
auto Err = Sym3.takeError();
EXPECT_FALSE(!!Err) << "Symbol 'baz' should not contain an error";
}
cantFail(ClientEP.callB<remote::utils::TerminateSession>());
ServerThread.join();
}
TEST(RemoteObjectLayer, FindSymbolIn) {
llvm::orc::rpc::registerStringError<rpc::RawByteChannel>();
auto TestObject = createTestObject();
if (!TestObject)
return;
auto Channels = createPairedQueueChannels();
auto ReportError =
[](Error Err) {
auto ErrMsg = toString(std::move(Err));
EXPECT_EQ(ErrMsg, "Could not find symbol 'barbaz'")
<< "Expected error string to be \"Object handle 42 not found\"";
};
RPCEndpoint ClientEP(*Channels.first, true);
RemoteObjectClientLayer<RPCEndpoint> Client(AcknowledgeORCv1Deprecation,
ClientEP, ReportError);
RPCEndpoint ServerEP(*Channels.second, true);
// AddObject lambda does not update symbol table, so removeObject will treat
// this as a bad object handle.
MockObjectLayer BaseLayer(
[](MockObjectLayer::ObjectPtr Obj,
MockObjectLayer::SymbolLookupTable &SymTab) {
SymTab[42] =
[](StringRef Name, bool ExportedSymbolsOnly) -> JITSymbol {
if (Name == "foobar")
return JITSymbol(0x12348765, JITSymbolFlags::Exported);
return make_error<JITSymbolNotFound>(Name);
};
// Dummy symbol table entry - this should not be visible to
// findSymbolIn.
SymTab[43] =
[](StringRef Name, bool ExportedSymbolsOnly) -> JITSymbol {
if (Name == "barbaz")
return JITSymbol(0xdeadbeef, JITSymbolFlags::Exported);
return make_error<JITSymbolNotFound>(Name);
};
return 42;
});
RemoteObjectServerLayer<MockObjectLayer, RPCEndpoint> Server(
AcknowledgeORCv1Deprecation, BaseLayer, ServerEP, ReportError);
bool Finished = false;
ServerEP.addHandler<remote::utils::TerminateSession>(
[&]() { Finished = true; }
);
auto ServerThread =
std::thread([&]() {
while (!Finished)
cantFail(ServerEP.handleOne());
});
auto H = cantFail(Client.addObject(std::move(TestObject),
std::make_shared<NullLegacyResolver>()));
auto Sym1 = Client.findSymbolIn(H, "foobar", true);
EXPECT_TRUE(!!Sym1) << "Symbol 'foobar' should be findable";
EXPECT_EQ(cantFail(Sym1.getAddress()), 0x12348765ULL)
<< "Symbol 'foobar' does not return the correct address";
auto Sym2 = Client.findSymbolIn(H, "barbaz", true);
EXPECT_FALSE(!!Sym2) << "Symbol 'barbaz' should not be findable";
auto Err = Sym2.takeError();
EXPECT_TRUE(!!Err) << "Sym2 should contain an error value";
auto ErrMsg = toString(std::move(Err));
EXPECT_EQ(ErrMsg, "Could not find symbol 'barbaz'")
<< "Expected symbol-not-found error for Sym2";
cantFail(ClientEP.callB<remote::utils::TerminateSession>());
ServerThread.join();
}
TEST(RemoteObjectLayer, EmitAndFinalize) {
llvm::orc::rpc::registerStringError<rpc::RawByteChannel>();
auto TestObject = createTestObject();
if (!TestObject)
return;
auto Channels = createPairedQueueChannels();
auto ReportError = [](Error Err) {
logAllUnhandledErrors(std::move(Err), llvm::errs());
};
RPCEndpoint ClientEP(*Channels.first, true);
RemoteObjectClientLayer<RPCEndpoint> Client(AcknowledgeORCv1Deprecation,
ClientEP, ReportError);
RPCEndpoint ServerEP(*Channels.second, true);
MockObjectLayer BaseLayer(
[](MockObjectLayer::ObjectPtr Obj,
MockObjectLayer::SymbolLookupTable &SymTab) {
SymTab[1] = MockObjectLayer::LookupFn();
return 1;
});
RemoteObjectServerLayer<MockObjectLayer, RPCEndpoint> Server(
AcknowledgeORCv1Deprecation, BaseLayer, ServerEP, ReportError);
bool Finished = false;
ServerEP.addHandler<remote::utils::TerminateSession>(
[&]() { Finished = true; }
);
auto ServerThread =
std::thread([&]() {
while (!Finished)
cantFail(ServerEP.handleOne());
});
auto H = cantFail(Client.addObject(std::move(TestObject),
std::make_shared<NullLegacyResolver>()));
auto Err = Client.emitAndFinalize(H);
EXPECT_FALSE(!!Err) << "emitAndFinalize should work";
cantFail(ClientEP.callB<remote::utils::TerminateSession>());
ServerThread.join();
}
TEST(RemoteObjectLayer, EmitAndFinalizeFailure) {
llvm::orc::rpc::registerStringError<rpc::RawByteChannel>();
auto TestObject = createTestObject();
if (!TestObject)
return;
auto Channels = createPairedQueueChannels();
auto ReportError =
[](Error Err) {
auto ErrMsg = toString(std::move(Err));
EXPECT_EQ(ErrMsg, "Object handle 1 not found")
<< "Expected bad handle error";
};
RPCEndpoint ClientEP(*Channels.first, true);
RemoteObjectClientLayer<RPCEndpoint> Client(AcknowledgeORCv1Deprecation,
ClientEP, ReportError);
RPCEndpoint ServerEP(*Channels.second, true);
MockObjectLayer BaseLayer(
[](MockObjectLayer::ObjectPtr Obj,
MockObjectLayer::SymbolLookupTable &SymTab) {
return 1;
});
RemoteObjectServerLayer<MockObjectLayer, RPCEndpoint> Server(
AcknowledgeORCv1Deprecation, BaseLayer, ServerEP, ReportError);
bool Finished = false;
ServerEP.addHandler<remote::utils::TerminateSession>(
[&]() { Finished = true; }
);
auto ServerThread =
std::thread([&]() {
while (!Finished)
cantFail(ServerEP.handleOne());
});
auto H = cantFail(Client.addObject(std::move(TestObject),
std::make_shared<NullLegacyResolver>()));
auto Err = Client.emitAndFinalize(H);
EXPECT_TRUE(!!Err) << "emitAndFinalize should work";
auto ErrMsg = toString(std::move(Err));
EXPECT_EQ(ErrMsg, "Object handle 1 not found")
<< "emitAndFinalize returned incorrect error";
cantFail(ClientEP.callB<remote::utils::TerminateSession>());
ServerThread.join();
}
}