1 //===----------- CoreAPIsTest.cpp - Unit tests for Core ORC APIs ----------===//
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 #include "OrcTestCommon.h"
11 #include "llvm/Config/llvm-config.h"
12 #include "llvm/ExecutionEngine/Orc/Core.h"
13 #include "llvm/ExecutionEngine/Orc/OrcError.h"
19 using namespace llvm::orc;
21 class CoreAPIsStandardTest : public CoreAPIsBasedStandardTest {};
25 class SimpleMaterializationUnit : public MaterializationUnit {
27 using MaterializeFunction =
28 std::function<void(MaterializationResponsibility)>;
29 using DiscardFunction = std::function<void(const VSO &, SymbolStringPtr)>;
30 using DestructorFunction = std::function<void()>;
32 SimpleMaterializationUnit(
33 SymbolFlagsMap SymbolFlags, MaterializeFunction Materialize,
34 DiscardFunction Discard = DiscardFunction(),
35 DestructorFunction Destructor = DestructorFunction())
36 : MaterializationUnit(std::move(SymbolFlags)),
37 Materialize(std::move(Materialize)), Discard(std::move(Discard)),
38 Destructor(std::move(Destructor)) {}
40 ~SimpleMaterializationUnit() override {
45 void materialize(MaterializationResponsibility R) override {
46 Materialize(std::move(R));
49 void discard(const VSO &V, SymbolStringPtr Name) override {
51 Discard(V, std::move(Name));
53 llvm_unreachable("Discard not supported");
57 MaterializeFunction Materialize;
58 DiscardFunction Discard;
59 DestructorFunction Destructor;
63 TEST_F(CoreAPIsStandardTest, AsynchronousSymbolQuerySuccessfulResolutionOnly) {
64 bool OnResolutionRun = false;
65 bool OnReadyRun = false;
67 [&](Expected<AsynchronousSymbolQuery::ResolutionResult> Result) {
68 EXPECT_TRUE(!!Result) << "Resolution unexpectedly returned error";
69 auto &Resolved = Result->Symbols;
70 auto I = Resolved.find(Foo);
71 EXPECT_NE(I, Resolved.end()) << "Could not find symbol definition";
72 EXPECT_EQ(I->second.getAddress(), FooAddr)
73 << "Resolution returned incorrect result";
74 OnResolutionRun = true;
76 auto OnReady = [&](Error Err) {
77 cantFail(std::move(Err));
81 AsynchronousSymbolQuery Q(SymbolNameSet({Foo}), OnResolution, OnReady);
83 Q.resolve(Foo, FooSym);
85 EXPECT_TRUE(Q.isFullyResolved()) << "Expected query to be fully resolved";
87 if (!Q.isFullyResolved())
90 Q.handleFullyResolved();
92 EXPECT_TRUE(OnResolutionRun) << "OnResolutionCallback was not run";
93 EXPECT_FALSE(OnReadyRun) << "OnReady unexpectedly run";
96 TEST_F(CoreAPIsStandardTest, ExecutionSessionFailQuery) {
97 bool OnResolutionRun = false;
98 bool OnReadyRun = false;
101 [&](Expected<AsynchronousSymbolQuery::ResolutionResult> Result) {
102 EXPECT_FALSE(!!Result) << "Resolution unexpectedly returned success";
103 auto Msg = toString(Result.takeError());
104 EXPECT_EQ(Msg, "xyz") << "Resolution returned incorrect result";
105 OnResolutionRun = true;
107 auto OnReady = [&](Error Err) {
108 cantFail(std::move(Err));
112 AsynchronousSymbolQuery Q(SymbolNameSet({Foo}), OnResolution, OnReady);
114 ES.failQuery(Q, make_error<StringError>("xyz", inconvertibleErrorCode()));
116 EXPECT_TRUE(OnResolutionRun) << "OnResolutionCallback was not run";
117 EXPECT_FALSE(OnReadyRun) << "OnReady unexpectedly run";
120 TEST_F(CoreAPIsStandardTest, SimpleAsynchronousSymbolQueryAgainstVSO) {
121 bool OnResolutionRun = false;
122 bool OnReadyRun = false;
125 [&](Expected<AsynchronousSymbolQuery::ResolutionResult> Result) {
126 EXPECT_TRUE(!!Result) << "Query unexpectedly returned error";
127 auto &Resolved = Result->Symbols;
128 auto I = Resolved.find(Foo);
129 EXPECT_NE(I, Resolved.end()) << "Could not find symbol definition";
130 EXPECT_EQ(I->second.getAddress(), FooSym.getAddress())
131 << "Resolution returned incorrect result";
132 OnResolutionRun = true;
135 auto OnReady = [&](Error Err) {
136 cantFail(std::move(Err));
140 SymbolNameSet Names({Foo});
143 std::make_shared<AsynchronousSymbolQuery>(Names, OnResolution, OnReady);
145 auto Defs = absoluteSymbols({{Foo, FooSym}});
146 cantFail(V.define(Defs));
147 assert(Defs == nullptr && "Defs should have been accepted");
150 EXPECT_TRUE(OnResolutionRun) << "OnResolutionCallback was not run";
151 EXPECT_TRUE(OnReadyRun) << "OnReady was not run";
154 TEST_F(CoreAPIsStandardTest, EmptyVSOAndQueryLookup) {
155 bool OnResolvedRun = false;
156 bool OnReadyRun = false;
158 auto Q = std::make_shared<AsynchronousSymbolQuery>(
160 [&](Expected<AsynchronousSymbolQuery::ResolutionResult> RR) {
161 cantFail(std::move(RR));
162 OnResolvedRun = true;
165 cantFail(std::move(Err));
169 V.lookup(std::move(Q), {});
171 EXPECT_TRUE(OnResolvedRun) << "OnResolved was not run for empty query";
172 EXPECT_TRUE(OnReadyRun) << "OnReady was not run for empty query";
175 TEST_F(CoreAPIsStandardTest, ChainedVSOLookup) {
176 cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
178 auto &V2 = ES.createVSO("V2");
180 bool OnResolvedRun = false;
181 bool OnReadyRun = false;
183 auto Q = std::make_shared<AsynchronousSymbolQuery>(
184 SymbolNameSet({Foo}),
185 [&](Expected<AsynchronousSymbolQuery::ResolutionResult> RR) {
186 cantFail(std::move(RR));
187 OnResolvedRun = true;
190 cantFail(std::move(Err));
194 V2.lookup(Q, V.lookup(Q, {Foo}));
196 EXPECT_TRUE(OnResolvedRun) << "OnResolved was not run for empty query";
197 EXPECT_TRUE(OnReadyRun) << "OnReady was not run for empty query";
200 TEST_F(CoreAPIsStandardTest, LookupFlagsTest) {
201 // Test that lookupFlags works on a predefined symbol, and does not trigger
202 // materialization of a lazy symbol. Make the lazy symbol weak to test that
203 // the weak flag is propagated correctly.
205 BarSym.setFlags(static_cast<JITSymbolFlags::FlagNames>(
206 JITSymbolFlags::Exported | JITSymbolFlags::Weak));
207 auto MU = llvm::make_unique<SimpleMaterializationUnit>(
208 SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
209 [](MaterializationResponsibility R) {
210 llvm_unreachable("Symbol materialized on flags lookup");
213 cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
214 cantFail(V.define(std::move(MU)));
216 SymbolNameSet Names({Foo, Bar, Baz});
218 auto SymbolFlags = V.lookupFlags(Names);
220 EXPECT_EQ(SymbolFlags.size(), 2U)
221 << "Returned symbol flags contains unexpected results";
222 EXPECT_EQ(SymbolFlags.count(Foo), 1U) << "Missing lookupFlags result for Foo";
223 EXPECT_EQ(SymbolFlags[Foo], FooSym.getFlags())
224 << "Incorrect flags returned for Foo";
225 EXPECT_EQ(SymbolFlags.count(Bar), 1U)
226 << "Missing lookupFlags result for Bar";
227 EXPECT_EQ(SymbolFlags[Bar], BarSym.getFlags())
228 << "Incorrect flags returned for Bar";
231 TEST_F(CoreAPIsStandardTest, TestBasicAliases) {
232 cantFail(V.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarSym}})));
233 cantFail(V.define(symbolAliases({{Baz, {Foo, JITSymbolFlags::Exported}},
234 {Qux, {Bar, JITSymbolFlags::Weak}}})));
235 cantFail(V.define(absoluteSymbols({{Qux, QuxSym}})));
237 auto Result = lookup({&V}, {Baz, Qux});
238 EXPECT_TRUE(!!Result) << "Unexpected lookup failure";
239 EXPECT_EQ(Result->count(Baz), 1U) << "No result for \"baz\"";
240 EXPECT_EQ(Result->count(Qux), 1U) << "No result for \"qux\"";
241 EXPECT_EQ((*Result)[Baz].getAddress(), FooSym.getAddress())
242 << "\"Baz\"'s address should match \"Foo\"'s";
243 EXPECT_EQ((*Result)[Qux].getAddress(), QuxSym.getAddress())
244 << "The \"Qux\" alias should have been overriden";
247 TEST_F(CoreAPIsStandardTest, TestChainedAliases) {
248 cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
249 cantFail(V.define(symbolAliases(
250 {{Baz, {Bar, BazSym.getFlags()}}, {Bar, {Foo, BarSym.getFlags()}}})));
252 auto Result = lookup({&V}, {Bar, Baz});
253 EXPECT_TRUE(!!Result) << "Unexpected lookup failure";
254 EXPECT_EQ(Result->count(Bar), 1U) << "No result for \"bar\"";
255 EXPECT_EQ(Result->count(Baz), 1U) << "No result for \"baz\"";
256 EXPECT_EQ((*Result)[Bar].getAddress(), FooSym.getAddress())
257 << "\"Bar\"'s address should match \"Foo\"'s";
258 EXPECT_EQ((*Result)[Baz].getAddress(), FooSym.getAddress())
259 << "\"Baz\"'s address should match \"Foo\"'s";
262 TEST_F(CoreAPIsStandardTest, TestTrivialCircularDependency) {
263 Optional<MaterializationResponsibility> FooR;
264 auto FooMU = llvm::make_unique<SimpleMaterializationUnit>(
265 SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
266 [&](MaterializationResponsibility R) { FooR.emplace(std::move(R)); });
268 cantFail(V.define(FooMU));
270 bool FooReady = false;
272 std::make_shared<AsynchronousSymbolQuery>(
273 SymbolNameSet({ Foo }),
274 [](Expected<AsynchronousSymbolQuery::ResolutionResult> R) {
275 cantFail(std::move(R));
278 cantFail(std::move(Err));
282 V.lookup(std::move(Q), { Foo });
284 FooR->addDependencies({{&V, {Foo}}});
285 FooR->resolve({{Foo, FooSym}});
288 EXPECT_TRUE(FooReady)
289 << "Self-dependency prevented symbol from being marked ready";
292 TEST_F(CoreAPIsStandardTest, TestCircularDependenceInOneVSO) {
293 // Test that a circular symbol dependency between three symbols in a VSO does
294 // not prevent any symbol from becoming 'ready' once all symbols are
297 // Create three MaterializationResponsibility objects: one for each of Foo,
298 // Bar and Baz. These are optional because MaterializationResponsibility
299 // does not have a default constructor).
300 Optional<MaterializationResponsibility> FooR;
301 Optional<MaterializationResponsibility> BarR;
302 Optional<MaterializationResponsibility> BazR;
304 // Create a MaterializationUnit for each symbol that moves the
305 // MaterializationResponsibility into one of the locals above.
306 auto FooMU = llvm::make_unique<SimpleMaterializationUnit>(
307 SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
308 [&](MaterializationResponsibility R) { FooR.emplace(std::move(R)); });
310 auto BarMU = llvm::make_unique<SimpleMaterializationUnit>(
311 SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
312 [&](MaterializationResponsibility R) { BarR.emplace(std::move(R)); });
314 auto BazMU = llvm::make_unique<SimpleMaterializationUnit>(
315 SymbolFlagsMap({{Baz, BazSym.getFlags()}}),
316 [&](MaterializationResponsibility R) { BazR.emplace(std::move(R)); });
318 // Define the symbols.
319 cantFail(V.define(FooMU));
320 cantFail(V.define(BarMU));
321 cantFail(V.define(BazMU));
323 // Query each of the symbols to trigger materialization.
324 bool FooResolved = false;
325 bool FooReady = false;
326 auto FooQ = std::make_shared<AsynchronousSymbolQuery>(
327 SymbolNameSet({Foo}),
328 [&](Expected<AsynchronousSymbolQuery::ResolutionResult> RR) {
329 cantFail(std::move(RR));
333 cantFail(std::move(Err));
337 auto Unresolved = V.lookup(FooQ, {Foo});
338 EXPECT_TRUE(Unresolved.empty()) << "Failed to resolve \"Foo\"";
341 bool BarResolved = false;
342 bool BarReady = false;
343 auto BarQ = std::make_shared<AsynchronousSymbolQuery>(
344 SymbolNameSet({Bar}),
345 [&](Expected<AsynchronousSymbolQuery::ResolutionResult> RR) {
346 cantFail(std::move(RR));
350 cantFail(std::move(Err));
354 auto Unresolved = V.lookup(BarQ, {Bar});
355 EXPECT_TRUE(Unresolved.empty()) << "Failed to resolve \"Bar\"";
358 bool BazResolved = false;
359 bool BazReady = false;
360 auto BazQ = std::make_shared<AsynchronousSymbolQuery>(
361 SymbolNameSet({Baz}),
362 [&](Expected<AsynchronousSymbolQuery::ResolutionResult> RR) {
363 cantFail(std::move(RR));
367 cantFail(std::move(Err));
371 auto Unresolved = V.lookup(BazQ, {Baz});
372 EXPECT_TRUE(Unresolved.empty()) << "Failed to resolve \"Baz\"";
375 // Add a circular dependency: Foo -> Bar, Bar -> Baz, Baz -> Foo.
376 FooR->addDependencies({{&V, SymbolNameSet({Bar})}});
377 BarR->addDependencies({{&V, SymbolNameSet({Baz})}});
378 BazR->addDependencies({{&V, SymbolNameSet({Foo})}});
380 // Add self-dependencies for good measure. This tests that the implementation
381 // of addDependencies filters these out.
382 FooR->addDependencies({{&V, SymbolNameSet({Foo})}});
383 BarR->addDependencies({{&V, SymbolNameSet({Bar})}});
384 BazR->addDependencies({{&V, SymbolNameSet({Baz})}});
386 // Check that nothing has been resolved yet.
387 EXPECT_FALSE(FooResolved) << "\"Foo\" should not be resolved yet";
388 EXPECT_FALSE(BarResolved) << "\"Bar\" should not be resolved yet";
389 EXPECT_FALSE(BazResolved) << "\"Baz\" should not be resolved yet";
391 // Resolve the symbols (but do not finalized them).
392 FooR->resolve({{Foo, FooSym}});
393 BarR->resolve({{Bar, BarSym}});
394 BazR->resolve({{Baz, BazSym}});
396 // Verify that the symbols have been resolved, but are not ready yet.
397 EXPECT_TRUE(FooResolved) << "\"Foo\" should be resolved now";
398 EXPECT_TRUE(BarResolved) << "\"Bar\" should be resolved now";
399 EXPECT_TRUE(BazResolved) << "\"Baz\" should be resolved now";
401 EXPECT_FALSE(FooReady) << "\"Foo\" should not be ready yet";
402 EXPECT_FALSE(BarReady) << "\"Bar\" should not be ready yet";
403 EXPECT_FALSE(BazReady) << "\"Baz\" should not be ready yet";
405 // Finalize two of the symbols.
409 // Verify that nothing is ready until the circular dependence is resolved.
410 EXPECT_FALSE(FooReady) << "\"Foo\" still should not be ready";
411 EXPECT_FALSE(BarReady) << "\"Bar\" still should not be ready";
412 EXPECT_FALSE(BazReady) << "\"Baz\" still should not be ready";
414 // Finalize the last symbol.
417 // Verify that everything becomes ready once the circular dependence resolved.
418 EXPECT_TRUE(FooReady) << "\"Foo\" should be ready now";
419 EXPECT_TRUE(BarReady) << "\"Bar\" should be ready now";
420 EXPECT_TRUE(BazReady) << "\"Baz\" should be ready now";
423 TEST_F(CoreAPIsStandardTest, DropMaterializerWhenEmpty) {
424 bool DestructorRun = false;
426 JITSymbolFlags WeakExported(JITSymbolFlags::Exported);
427 WeakExported |= JITSymbolFlags::Weak;
429 auto MU = llvm::make_unique<SimpleMaterializationUnit>(
430 SymbolFlagsMap({{Foo, WeakExported}, {Bar, WeakExported}}),
431 [](MaterializationResponsibility R) {
432 llvm_unreachable("Unexpected call to materialize");
434 [&](const VSO &V, SymbolStringPtr Name) {
435 EXPECT_TRUE(Name == Foo || Name == Bar)
436 << "Discard of unexpected symbol?";
438 [&]() { DestructorRun = true; });
440 cantFail(V.define(MU));
442 cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
444 EXPECT_FALSE(DestructorRun)
445 << "MaterializationUnit should not have been destroyed yet";
447 cantFail(V.define(absoluteSymbols({{Bar, BarSym}})));
449 EXPECT_TRUE(DestructorRun)
450 << "MaterializationUnit should have been destroyed";
453 TEST_F(CoreAPIsStandardTest, AddAndMaterializeLazySymbol) {
454 bool FooMaterialized = false;
455 bool BarDiscarded = false;
457 JITSymbolFlags WeakExported(JITSymbolFlags::Exported);
458 WeakExported |= JITSymbolFlags::Weak;
460 auto MU = llvm::make_unique<SimpleMaterializationUnit>(
461 SymbolFlagsMap({{Foo, JITSymbolFlags::Exported}, {Bar, WeakExported}}),
462 [&](MaterializationResponsibility R) {
463 assert(BarDiscarded && "Bar should have been discarded by this point");
464 R.resolve(SymbolMap({{Foo, FooSym}}));
466 FooMaterialized = true;
468 [&](const VSO &V, SymbolStringPtr Name) {
469 EXPECT_EQ(Name, Bar) << "Expected Name to be Bar";
473 cantFail(V.define(MU));
474 cantFail(V.define(absoluteSymbols({{Bar, BarSym}})));
476 SymbolNameSet Names({Foo});
478 bool OnResolutionRun = false;
479 bool OnReadyRun = false;
482 [&](Expected<AsynchronousSymbolQuery::ResolutionResult> Result) {
483 EXPECT_TRUE(!!Result) << "Resolution unexpectedly returned error";
484 auto I = Result->Symbols.find(Foo);
485 EXPECT_NE(I, Result->Symbols.end())
486 << "Could not find symbol definition";
487 EXPECT_EQ(I->second.getAddress(), FooSym.getAddress())
488 << "Resolution returned incorrect result";
489 OnResolutionRun = true;
492 auto OnReady = [&](Error Err) {
493 cantFail(std::move(Err));
498 std::make_shared<AsynchronousSymbolQuery>(Names, OnResolution, OnReady);
500 auto Unresolved = V.lookup(std::move(Q), Names);
502 EXPECT_TRUE(Unresolved.empty()) << "Could not find Foo in dylib";
503 EXPECT_TRUE(FooMaterialized) << "Foo was not materialized";
504 EXPECT_TRUE(BarDiscarded) << "Bar was not discarded";
505 EXPECT_TRUE(OnResolutionRun) << "OnResolutionCallback was not run";
506 EXPECT_TRUE(OnReadyRun) << "OnReady was not run";
509 TEST_F(CoreAPIsStandardTest, DefineMaterializingSymbol) {
510 bool ExpectNoMoreMaterialization = false;
511 ES.setDispatchMaterialization(
512 [&](VSO &V, std::unique_ptr<MaterializationUnit> MU) {
513 if (ExpectNoMoreMaterialization)
514 ADD_FAILURE() << "Unexpected materialization";
515 MU->doMaterialize(V);
518 auto MU = llvm::make_unique<SimpleMaterializationUnit>(
519 SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
520 [&](MaterializationResponsibility R) {
522 R.defineMaterializing(SymbolFlagsMap({{Bar, BarSym.getFlags()}})));
523 R.resolve(SymbolMap({{Foo, FooSym}, {Bar, BarSym}}));
527 cantFail(V.define(MU));
528 cantFail(lookup({&V}, Foo));
530 // Assert that materialization is complete by now.
531 ExpectNoMoreMaterialization = true;
533 // Look up bar to verify that no further materialization happens.
534 auto BarResult = cantFail(lookup({&V}, Bar));
535 EXPECT_EQ(BarResult.getAddress(), BarSym.getAddress())
536 << "Expected Bar == BarSym";
539 TEST_F(CoreAPIsStandardTest, FallbackDefinitionGeneratorTest) {
540 cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
542 V.setFallbackDefinitionGenerator([&](VSO &W, const SymbolNameSet &Names) {
543 cantFail(W.define(absoluteSymbols({{Bar, BarSym}})));
544 return SymbolNameSet({Bar});
547 auto Result = cantFail(lookup({&V}, {Foo, Bar}));
549 EXPECT_EQ(Result.count(Bar), 1U) << "Expected to find fallback def for 'bar'";
550 EXPECT_EQ(Result[Bar].getAddress(), BarSym.getAddress())
551 << "Expected fallback def for Bar to be equal to BarSym";
554 TEST_F(CoreAPIsStandardTest, FailResolution) {
555 auto MU = llvm::make_unique<SimpleMaterializationUnit>(
557 {{Foo, JITSymbolFlags::Weak}, {Bar, JITSymbolFlags::Weak}}),
558 [&](MaterializationResponsibility R) { R.failMaterialization(); });
560 cantFail(V.define(MU));
562 SymbolNameSet Names({Foo, Bar});
563 auto Result = lookup({&V}, Names);
565 EXPECT_FALSE(!!Result) << "Expected failure";
567 handleAllErrors(Result.takeError(),
568 [&](FailedToMaterialize &F) {
569 EXPECT_EQ(F.getSymbols(), Names)
570 << "Expected to fail on symbols in Names";
572 [](ErrorInfoBase &EIB) {
575 raw_string_ostream ErrOut(ErrMsg);
579 << "Expected a FailedToResolve error. Got:\n"
585 TEST_F(CoreAPIsStandardTest, TestLookupWithUnthreadedMaterialization) {
586 auto MU = llvm::make_unique<SimpleMaterializationUnit>(
587 SymbolFlagsMap({{Foo, JITSymbolFlags::Exported}}),
588 [&](MaterializationResponsibility R) {
589 R.resolve({{Foo, FooSym}});
593 cantFail(V.define(MU));
595 auto FooLookupResult = cantFail(lookup({&V}, Foo));
597 EXPECT_EQ(FooLookupResult.getAddress(), FooSym.getAddress())
598 << "lookup returned an incorrect address";
599 EXPECT_EQ(FooLookupResult.getFlags(), FooSym.getFlags())
600 << "lookup returned incorrect flags";
603 TEST_F(CoreAPIsStandardTest, TestLookupWithThreadedMaterialization) {
604 #if LLVM_ENABLE_THREADS
606 std::thread MaterializationThread;
607 ES.setDispatchMaterialization(
608 [&](VSO &V, std::unique_ptr<MaterializationUnit> MU) {
609 auto SharedMU = std::shared_ptr<MaterializationUnit>(std::move(MU));
610 MaterializationThread =
611 std::thread([SharedMU, &V]() { SharedMU->doMaterialize(V); });
614 cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
616 auto FooLookupResult = cantFail(lookup({&V}, Foo));
618 EXPECT_EQ(FooLookupResult.getAddress(), FooSym.getAddress())
619 << "lookup returned an incorrect address";
620 EXPECT_EQ(FooLookupResult.getFlags(), FooSym.getFlags())
621 << "lookup returned incorrect flags";
622 MaterializationThread.join();
626 TEST_F(CoreAPIsStandardTest, TestGetRequestedSymbolsAndReplace) {
627 // Test that GetRequestedSymbols returns the set of symbols that currently
628 // have pending queries, and test that MaterializationResponsibility's
629 // replace method can be used to return definitions to the VSO in a new
630 // MaterializationUnit.
631 SymbolNameSet Names({Foo, Bar});
633 bool FooMaterialized = false;
634 bool BarMaterialized = false;
636 auto MU = llvm::make_unique<SimpleMaterializationUnit>(
637 SymbolFlagsMap({{Foo, FooSym.getFlags()}, {Bar, BarSym.getFlags()}}),
638 [&](MaterializationResponsibility R) {
639 auto Requested = R.getRequestedSymbols();
640 EXPECT_EQ(Requested.size(), 1U) << "Expected one symbol requested";
641 EXPECT_EQ(*Requested.begin(), Foo) << "Expected \"Foo\" requested";
643 auto NewMU = llvm::make_unique<SimpleMaterializationUnit>(
644 SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
645 [&](MaterializationResponsibility R2) {
646 R2.resolve(SymbolMap({{Bar, BarSym}}));
648 BarMaterialized = true;
651 R.replace(std::move(NewMU));
653 R.resolve(SymbolMap({{Foo, FooSym}}));
656 FooMaterialized = true;
659 cantFail(V.define(MU));
661 EXPECT_FALSE(FooMaterialized) << "Foo should not be materialized yet";
662 EXPECT_FALSE(BarMaterialized) << "Bar should not be materialized yet";
664 auto FooSymResult = cantFail(lookup({&V}, Foo));
665 EXPECT_EQ(FooSymResult.getAddress(), FooSym.getAddress())
666 << "Address mismatch for Foo";
668 EXPECT_TRUE(FooMaterialized) << "Foo should be materialized now";
669 EXPECT_FALSE(BarMaterialized) << "Bar still should not be materialized";
671 auto BarSymResult = cantFail(lookup({&V}, Bar));
672 EXPECT_EQ(BarSymResult.getAddress(), BarSym.getAddress())
673 << "Address mismatch for Bar";
674 EXPECT_TRUE(BarMaterialized) << "Bar should be materialized now";
677 TEST_F(CoreAPIsStandardTest, TestMaterializationResponsibilityDelegation) {
678 auto MU = llvm::make_unique<SimpleMaterializationUnit>(
679 SymbolFlagsMap({{Foo, FooSym.getFlags()}, {Bar, BarSym.getFlags()}}),
680 [&](MaterializationResponsibility R) {
681 auto R2 = R.delegate({Bar});
683 R.resolve({{Foo, FooSym}});
685 R2.resolve({{Bar, BarSym}});
689 cantFail(V.define(MU));
691 auto Result = lookup({&V}, {Foo, Bar});
693 EXPECT_TRUE(!!Result) << "Result should be a success value";
694 EXPECT_EQ(Result->count(Foo), 1U) << "\"Foo\" entry missing";
695 EXPECT_EQ(Result->count(Bar), 1U) << "\"Bar\" entry missing";
696 EXPECT_EQ((*Result)[Foo].getAddress(), FooSym.getAddress())
697 << "Address mismatch for \"Foo\"";
698 EXPECT_EQ((*Result)[Bar].getAddress(), BarSym.getAddress())
699 << "Address mismatch for \"Bar\"";
702 TEST_F(CoreAPIsStandardTest, TestMaterializeWeakSymbol) {
703 // Confirm that once a weak definition is selected for materialization it is
704 // treated as strong.
705 JITSymbolFlags WeakExported = JITSymbolFlags::Exported;
706 WeakExported &= JITSymbolFlags::Weak;
708 std::unique_ptr<MaterializationResponsibility> FooResponsibility;
709 auto MU = llvm::make_unique<SimpleMaterializationUnit>(
710 SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
711 [&](MaterializationResponsibility R) {
713 llvm::make_unique<MaterializationResponsibility>(std::move(R));
716 cantFail(V.define(MU));
717 auto Q = std::make_shared<AsynchronousSymbolQuery>(
718 SymbolNameSet({Foo}),
719 [](Expected<AsynchronousSymbolQuery::ResolutionResult> R) {
720 cantFail(std::move(R));
722 [](Error Err) { cantFail(std::move(Err)); });
723 V.lookup(std::move(Q), SymbolNameSet({Foo}));
725 auto MU2 = llvm::make_unique<SimpleMaterializationUnit>(
726 SymbolFlagsMap({{Foo, JITSymbolFlags::Exported}}),
727 [](MaterializationResponsibility R) {
728 llvm_unreachable("This unit should never be materialized");
731 auto Err = V.define(MU2);
732 EXPECT_TRUE(!!Err) << "Expected failure value";
733 EXPECT_TRUE(Err.isA<DuplicateDefinition>())
734 << "Expected a duplicate definition error";
735 consumeError(std::move(Err));
737 FooResponsibility->resolve(SymbolMap({{Foo, FooSym}}));
738 FooResponsibility->finalize();