1 //===----------- CoreAPIsTest.cpp - Unit tests for Core ORC APIs ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
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"
14 
15 #include <set>
16 #include <thread>
17 
18 using namespace llvm;
19 using namespace llvm::orc;
20 
21 class CoreAPIsStandardTest : public CoreAPIsBasedStandardTest {};
22 
23 namespace {
24 
25 class SimpleMaterializationUnit : public MaterializationUnit {
26 public:
27   using MaterializeFunction =
28       std::function<void(MaterializationResponsibility)>;
29   using DiscardFunction = std::function<void(const VSO &, SymbolStringPtr)>;
30   using DestructorFunction = std::function<void()>;
31 
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)) {}
39 
40   ~SimpleMaterializationUnit() override {
41     if (Destructor)
42       Destructor();
43   }
44 
45   void materialize(MaterializationResponsibility R) override {
46     Materialize(std::move(R));
47   }
48 
49   void discard(const VSO &V, SymbolStringPtr Name) override {
50     if (Discard)
51       Discard(V, std::move(Name));
52     else
53       llvm_unreachable("Discard not supported");
54   }
55 
56 private:
57   MaterializeFunction Materialize;
58   DiscardFunction Discard;
59   DestructorFunction Destructor;
60 };
61 
62 TEST_F(CoreAPIsStandardTest, BasicSuccessfulLookup) {
63   bool OnResolutionRun = false;
64   bool OnReadyRun = false;
65 
66   auto OnResolution = [&](Expected<SymbolMap> Result) {
67     EXPECT_TRUE(!!Result) << "Resolution unexpectedly returned error";
68     auto &Resolved = *Result;
69     auto I = Resolved.find(Foo);
70     EXPECT_NE(I, Resolved.end()) << "Could not find symbol definition";
71     EXPECT_EQ(I->second.getAddress(), FooAddr)
72         << "Resolution returned incorrect result";
73     OnResolutionRun = true;
74   };
75   auto OnReady = [&](Error Err) {
76     cantFail(std::move(Err));
77     OnReadyRun = true;
78   };
79 
80   std::shared_ptr<MaterializationResponsibility> FooMR;
81 
82   cantFail(V.define(llvm::make_unique<SimpleMaterializationUnit>(
83       SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
84       [&](MaterializationResponsibility R) {
85         FooMR = std::make_shared<MaterializationResponsibility>(std::move(R));
86       })));
87 
88   ES.lookup({&V}, {Foo}, OnResolution, OnReady, NoDependenciesToRegister);
89 
90   EXPECT_FALSE(OnResolutionRun) << "Should not have been resolved yet";
91   EXPECT_FALSE(OnReadyRun) << "Should not have been marked ready yet";
92 
93   FooMR->resolve({{Foo, FooSym}});
94 
95   EXPECT_TRUE(OnResolutionRun) << "Should have been resolved";
96   EXPECT_FALSE(OnReadyRun) << "Should not have been marked ready yet";
97 
98   FooMR->finalize();
99 
100   EXPECT_TRUE(OnReadyRun) << "Should have been marked ready";
101 }
102 
103 TEST_F(CoreAPIsStandardTest, ExecutionSessionFailQuery) {
104   bool OnResolutionRun = false;
105   bool OnReadyRun = false;
106 
107   auto OnResolution = [&](Expected<SymbolMap> Result) {
108     EXPECT_FALSE(!!Result) << "Resolution unexpectedly returned success";
109     auto Msg = toString(Result.takeError());
110     EXPECT_EQ(Msg, "xyz") << "Resolution returned incorrect result";
111     OnResolutionRun = true;
112   };
113   auto OnReady = [&](Error Err) {
114     cantFail(std::move(Err));
115     OnReadyRun = true;
116   };
117 
118   AsynchronousSymbolQuery Q(SymbolNameSet({Foo}), OnResolution, OnReady);
119 
120   ES.legacyFailQuery(Q,
121                      make_error<StringError>("xyz", inconvertibleErrorCode()));
122 
123   EXPECT_TRUE(OnResolutionRun) << "OnResolutionCallback was not run";
124   EXPECT_FALSE(OnReadyRun) << "OnReady unexpectedly run";
125 }
126 
127 TEST_F(CoreAPIsStandardTest, EmptyLookup) {
128   bool OnResolvedRun = false;
129   bool OnReadyRun = false;
130 
131   auto OnResolution = [&](Expected<SymbolMap> Result) {
132     cantFail(std::move(Result));
133     OnResolvedRun = true;
134   };
135 
136   auto OnReady = [&](Error Err) {
137     cantFail(std::move(Err));
138     OnReadyRun = true;
139   };
140 
141   ES.lookup({&V}, {}, OnResolution, OnReady, NoDependenciesToRegister);
142 
143   EXPECT_TRUE(OnResolvedRun) << "OnResolved was not run for empty query";
144   EXPECT_TRUE(OnReadyRun) << "OnReady was not run for empty query";
145 }
146 
147 TEST_F(CoreAPIsStandardTest, ChainedVSOLookup) {
148   cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
149 
150   auto &V2 = ES.createVSO("V2");
151 
152   bool OnResolvedRun = false;
153   bool OnReadyRun = false;
154 
155   auto Q = std::make_shared<AsynchronousSymbolQuery>(
156       SymbolNameSet({Foo}),
157       [&](Expected<SymbolMap> Result) {
158         cantFail(std::move(Result));
159         OnResolvedRun = true;
160       },
161       [&](Error Err) {
162         cantFail(std::move(Err));
163         OnReadyRun = true;
164       });
165 
166   V2.legacyLookup(Q, V.legacyLookup(Q, {Foo}));
167 
168   EXPECT_TRUE(OnResolvedRun) << "OnResolved was not run for empty query";
169   EXPECT_TRUE(OnReadyRun) << "OnReady was not run for empty query";
170 }
171 
172 TEST_F(CoreAPIsStandardTest, LookupFlagsTest) {
173   // Test that lookupFlags works on a predefined symbol, and does not trigger
174   // materialization of a lazy symbol. Make the lazy symbol weak to test that
175   // the weak flag is propagated correctly.
176 
177   BarSym.setFlags(static_cast<JITSymbolFlags::FlagNames>(
178       JITSymbolFlags::Exported | JITSymbolFlags::Weak));
179   auto MU = llvm::make_unique<SimpleMaterializationUnit>(
180       SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
181       [](MaterializationResponsibility R) {
182         llvm_unreachable("Symbol materialized on flags lookup");
183       });
184 
185   cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
186   cantFail(V.define(std::move(MU)));
187 
188   SymbolNameSet Names({Foo, Bar, Baz});
189 
190   auto SymbolFlags = V.lookupFlags(Names);
191 
192   EXPECT_EQ(SymbolFlags.size(), 2U)
193       << "Returned symbol flags contains unexpected results";
194   EXPECT_EQ(SymbolFlags.count(Foo), 1U) << "Missing lookupFlags result for Foo";
195   EXPECT_EQ(SymbolFlags[Foo], FooSym.getFlags())
196       << "Incorrect flags returned for Foo";
197   EXPECT_EQ(SymbolFlags.count(Bar), 1U)
198       << "Missing  lookupFlags result for Bar";
199   EXPECT_EQ(SymbolFlags[Bar], BarSym.getFlags())
200       << "Incorrect flags returned for Bar";
201 }
202 
203 TEST_F(CoreAPIsStandardTest, TestBasicAliases) {
204   cantFail(V.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarSym}})));
205   cantFail(V.define(symbolAliases({{Baz, {Foo, JITSymbolFlags::Exported}},
206                                    {Qux, {Bar, JITSymbolFlags::Weak}}})));
207   cantFail(V.define(absoluteSymbols({{Qux, QuxSym}})));
208 
209   auto Result = lookup({&V}, {Baz, Qux});
210   EXPECT_TRUE(!!Result) << "Unexpected lookup failure";
211   EXPECT_EQ(Result->count(Baz), 1U) << "No result for \"baz\"";
212   EXPECT_EQ(Result->count(Qux), 1U) << "No result for \"qux\"";
213   EXPECT_EQ((*Result)[Baz].getAddress(), FooSym.getAddress())
214       << "\"Baz\"'s address should match \"Foo\"'s";
215   EXPECT_EQ((*Result)[Qux].getAddress(), QuxSym.getAddress())
216       << "The \"Qux\" alias should have been overriden";
217 }
218 
219 TEST_F(CoreAPIsStandardTest, TestChainedAliases) {
220   cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
221   cantFail(V.define(symbolAliases(
222       {{Baz, {Bar, BazSym.getFlags()}}, {Bar, {Foo, BarSym.getFlags()}}})));
223 
224   auto Result = lookup({&V}, {Bar, Baz});
225   EXPECT_TRUE(!!Result) << "Unexpected lookup failure";
226   EXPECT_EQ(Result->count(Bar), 1U) << "No result for \"bar\"";
227   EXPECT_EQ(Result->count(Baz), 1U) << "No result for \"baz\"";
228   EXPECT_EQ((*Result)[Bar].getAddress(), FooSym.getAddress())
229       << "\"Bar\"'s address should match \"Foo\"'s";
230   EXPECT_EQ((*Result)[Baz].getAddress(), FooSym.getAddress())
231       << "\"Baz\"'s address should match \"Foo\"'s";
232 }
233 
234 TEST_F(CoreAPIsStandardTest, TestBasicReExports) {
235   // Test that the basic use case of re-exporting a single symbol from another
236   // VSO works.
237   cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
238 
239   auto &V2 = ES.createVSO("V2");
240 
241   cantFail(V2.define(reexports(V, {{Bar, {Foo, BarSym.getFlags()}}})));
242 
243   auto Result = cantFail(lookup({&V2}, Bar));
244   EXPECT_EQ(Result.getAddress(), FooSym.getAddress())
245       << "Re-export Bar for symbol Foo should match FooSym's address";
246 }
247 
248 TEST_F(CoreAPIsStandardTest, TestThatReExportsDontUnnecessarilyMaterialize) {
249   // Test that re-exports do not materialize symbols that have not been queried
250   // for.
251   cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
252 
253   bool BarMaterialized = false;
254   auto BarMU = llvm::make_unique<SimpleMaterializationUnit>(
255       SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
256       [&](MaterializationResponsibility R) {
257         BarMaterialized = true;
258         R.resolve({{Bar, BarSym}});
259         R.finalize();
260       });
261 
262   cantFail(V.define(BarMU));
263 
264   auto &V2 = ES.createVSO("V2");
265 
266   cantFail(V2.define(reexports(
267       V, {{Baz, {Foo, BazSym.getFlags()}}, {Qux, {Bar, QuxSym.getFlags()}}})));
268 
269   auto Result = cantFail(lookup({&V2}, Baz));
270   EXPECT_EQ(Result.getAddress(), FooSym.getAddress())
271       << "Re-export Baz for symbol Foo should match FooSym's address";
272 
273   EXPECT_FALSE(BarMaterialized) << "Bar should not have been materialized";
274 }
275 
276 TEST_F(CoreAPIsStandardTest, TestReexportsFallbackGenerator) {
277   // Test that a re-exports fallback generator can dynamically generate
278   // reexports.
279 
280   auto &V2 = ES.createVSO("V2");
281   cantFail(V2.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarSym}})));
282 
283   auto Filter = [this](SymbolStringPtr Name) { return Name != Bar; };
284 
285   V.setFallbackDefinitionGenerator(
286       ReexportsFallbackDefinitionGenerator(V2, Filter));
287 
288   auto Flags = V.lookupFlags({Foo, Bar, Baz});
289   EXPECT_EQ(Flags.size(), 1U) << "Unexpected number of results";
290   EXPECT_EQ(Flags[Foo], FooSym.getFlags()) << "Unexpected flags for Foo";
291 
292   auto Result = cantFail(lookup({&V}, Foo));
293 
294   EXPECT_EQ(Result.getAddress(), FooSym.getAddress())
295       << "Incorrect reexported symbol address";
296 }
297 
298 TEST_F(CoreAPIsStandardTest, TestTrivialCircularDependency) {
299   Optional<MaterializationResponsibility> FooR;
300   auto FooMU = llvm::make_unique<SimpleMaterializationUnit>(
301       SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
302       [&](MaterializationResponsibility R) { FooR.emplace(std::move(R)); });
303 
304   cantFail(V.define(FooMU));
305 
306   bool FooReady = false;
307   auto OnResolution = [](Expected<SymbolMap> R) { cantFail(std::move(R)); };
308   auto OnReady = [&](Error Err) {
309     cantFail(std::move(Err));
310     FooReady = true;
311   };
312 
313   ES.lookup({&V}, {Foo}, std::move(OnResolution), std::move(OnReady),
314             NoDependenciesToRegister);
315 
316   FooR->resolve({{Foo, FooSym}});
317   FooR->finalize();
318 
319   EXPECT_TRUE(FooReady)
320     << "Self-dependency prevented symbol from being marked ready";
321 }
322 
323 TEST_F(CoreAPIsStandardTest, TestCircularDependenceInOneVSO) {
324   // Test that a circular symbol dependency between three symbols in a VSO does
325   // not prevent any symbol from becoming 'ready' once all symbols are
326   // finalized.
327 
328   // Create three MaterializationResponsibility objects: one for each of Foo,
329   // Bar and Baz. These are optional because MaterializationResponsibility
330   // does not have a default constructor).
331   Optional<MaterializationResponsibility> FooR;
332   Optional<MaterializationResponsibility> BarR;
333   Optional<MaterializationResponsibility> BazR;
334 
335   // Create a MaterializationUnit for each symbol that moves the
336   // MaterializationResponsibility into one of the locals above.
337   auto FooMU = llvm::make_unique<SimpleMaterializationUnit>(
338       SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
339       [&](MaterializationResponsibility R) { FooR.emplace(std::move(R)); });
340 
341   auto BarMU = llvm::make_unique<SimpleMaterializationUnit>(
342       SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
343       [&](MaterializationResponsibility R) { BarR.emplace(std::move(R)); });
344 
345   auto BazMU = llvm::make_unique<SimpleMaterializationUnit>(
346       SymbolFlagsMap({{Baz, BazSym.getFlags()}}),
347       [&](MaterializationResponsibility R) { BazR.emplace(std::move(R)); });
348 
349   // Define the symbols.
350   cantFail(V.define(FooMU));
351   cantFail(V.define(BarMU));
352   cantFail(V.define(BazMU));
353 
354   // Query each of the symbols to trigger materialization.
355   bool FooResolved = false;
356   bool FooReady = false;
357 
358   auto OnFooResolution = [&](Expected<SymbolMap> Result) {
359     cantFail(std::move(Result));
360     FooResolved = true;
361   };
362 
363   auto OnFooReady = [&](Error Err) {
364     cantFail(std::move(Err));
365     FooReady = true;
366   };
367 
368   // Issue a lookup for Foo. Use NoDependenciesToRegister: We're going to add
369   // the dependencies manually below.
370   ES.lookup({&V}, {Foo}, std::move(OnFooResolution), std::move(OnFooReady),
371             NoDependenciesToRegister);
372 
373   bool BarResolved = false;
374   bool BarReady = false;
375   auto OnBarResolution = [&](Expected<SymbolMap> Result) {
376     cantFail(std::move(Result));
377     BarResolved = true;
378   };
379 
380   auto OnBarReady = [&](Error Err) {
381     cantFail(std::move(Err));
382     BarReady = true;
383   };
384 
385   ES.lookup({&V}, {Bar}, std::move(OnBarResolution), std::move(OnBarReady),
386             NoDependenciesToRegister);
387 
388   bool BazResolved = false;
389   bool BazReady = false;
390 
391   auto OnBazResolution = [&](Expected<SymbolMap> Result) {
392     cantFail(std::move(Result));
393     BazResolved = true;
394   };
395 
396   auto OnBazReady = [&](Error Err) {
397     cantFail(std::move(Err));
398     BazReady = true;
399   };
400 
401   ES.lookup({&V}, {Baz}, std::move(OnBazResolution), std::move(OnBazReady),
402             NoDependenciesToRegister);
403 
404   // Add a circular dependency: Foo -> Bar, Bar -> Baz, Baz -> Foo.
405   FooR->addDependenciesForAll({{&V, SymbolNameSet({Bar})}});
406   BarR->addDependenciesForAll({{&V, SymbolNameSet({Baz})}});
407   BazR->addDependenciesForAll({{&V, SymbolNameSet({Foo})}});
408 
409   // Add self-dependencies for good measure. This tests that the implementation
410   // of addDependencies filters these out.
411   FooR->addDependenciesForAll({{&V, SymbolNameSet({Foo})}});
412   BarR->addDependenciesForAll({{&V, SymbolNameSet({Bar})}});
413   BazR->addDependenciesForAll({{&V, SymbolNameSet({Baz})}});
414 
415   // Check that nothing has been resolved yet.
416   EXPECT_FALSE(FooResolved) << "\"Foo\" should not be resolved yet";
417   EXPECT_FALSE(BarResolved) << "\"Bar\" should not be resolved yet";
418   EXPECT_FALSE(BazResolved) << "\"Baz\" should not be resolved yet";
419 
420   // Resolve the symbols (but do not finalized them).
421   FooR->resolve({{Foo, FooSym}});
422   BarR->resolve({{Bar, BarSym}});
423   BazR->resolve({{Baz, BazSym}});
424 
425   // Verify that the symbols have been resolved, but are not ready yet.
426   EXPECT_TRUE(FooResolved) << "\"Foo\" should be resolved now";
427   EXPECT_TRUE(BarResolved) << "\"Bar\" should be resolved now";
428   EXPECT_TRUE(BazResolved) << "\"Baz\" should be resolved now";
429 
430   EXPECT_FALSE(FooReady) << "\"Foo\" should not be ready yet";
431   EXPECT_FALSE(BarReady) << "\"Bar\" should not be ready yet";
432   EXPECT_FALSE(BazReady) << "\"Baz\" should not be ready yet";
433 
434   // Finalize two of the symbols.
435   FooR->finalize();
436   BarR->finalize();
437 
438   // Verify that nothing is ready until the circular dependence is resolved.
439   EXPECT_FALSE(FooReady) << "\"Foo\" still should not be ready";
440   EXPECT_FALSE(BarReady) << "\"Bar\" still should not be ready";
441   EXPECT_FALSE(BazReady) << "\"Baz\" still should not be ready";
442 
443   // Finalize the last symbol.
444   BazR->finalize();
445 
446   // Verify that everything becomes ready once the circular dependence resolved.
447   EXPECT_TRUE(FooReady) << "\"Foo\" should be ready now";
448   EXPECT_TRUE(BarReady) << "\"Bar\" should be ready now";
449   EXPECT_TRUE(BazReady) << "\"Baz\" should be ready now";
450 }
451 
452 TEST_F(CoreAPIsStandardTest, DropMaterializerWhenEmpty) {
453   bool DestructorRun = false;
454 
455   JITSymbolFlags WeakExported(JITSymbolFlags::Exported);
456   WeakExported |= JITSymbolFlags::Weak;
457 
458   auto MU = llvm::make_unique<SimpleMaterializationUnit>(
459       SymbolFlagsMap({{Foo, WeakExported}, {Bar, WeakExported}}),
460       [](MaterializationResponsibility R) {
461         llvm_unreachable("Unexpected call to materialize");
462       },
463       [&](const VSO &V, SymbolStringPtr Name) {
464         EXPECT_TRUE(Name == Foo || Name == Bar)
465             << "Discard of unexpected symbol?";
466       },
467       [&]() { DestructorRun = true; });
468 
469   cantFail(V.define(MU));
470 
471   cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
472 
473   EXPECT_FALSE(DestructorRun)
474       << "MaterializationUnit should not have been destroyed yet";
475 
476   cantFail(V.define(absoluteSymbols({{Bar, BarSym}})));
477 
478   EXPECT_TRUE(DestructorRun)
479       << "MaterializationUnit should have been destroyed";
480 }
481 
482 TEST_F(CoreAPIsStandardTest, AddAndMaterializeLazySymbol) {
483   bool FooMaterialized = false;
484   bool BarDiscarded = false;
485 
486   JITSymbolFlags WeakExported(JITSymbolFlags::Exported);
487   WeakExported |= JITSymbolFlags::Weak;
488 
489   auto MU = llvm::make_unique<SimpleMaterializationUnit>(
490       SymbolFlagsMap({{Foo, JITSymbolFlags::Exported}, {Bar, WeakExported}}),
491       [&](MaterializationResponsibility R) {
492         assert(BarDiscarded && "Bar should have been discarded by this point");
493         R.resolve(SymbolMap({{Foo, FooSym}}));
494         R.finalize();
495         FooMaterialized = true;
496       },
497       [&](const VSO &V, SymbolStringPtr Name) {
498         EXPECT_EQ(Name, Bar) << "Expected Name to be Bar";
499         BarDiscarded = true;
500       });
501 
502   cantFail(V.define(MU));
503   cantFail(V.define(absoluteSymbols({{Bar, BarSym}})));
504 
505   SymbolNameSet Names({Foo});
506 
507   bool OnResolutionRun = false;
508   bool OnReadyRun = false;
509 
510   auto OnResolution = [&](Expected<SymbolMap> Result) {
511     EXPECT_TRUE(!!Result) << "Resolution unexpectedly returned error";
512     auto I = Result->find(Foo);
513     EXPECT_NE(I, Result->end()) << "Could not find symbol definition";
514     EXPECT_EQ(I->second.getAddress(), FooSym.getAddress())
515         << "Resolution returned incorrect result";
516     OnResolutionRun = true;
517   };
518 
519   auto OnReady = [&](Error Err) {
520     cantFail(std::move(Err));
521     OnReadyRun = true;
522   };
523 
524   ES.lookup({&V}, Names, std::move(OnResolution), std::move(OnReady),
525             NoDependenciesToRegister);
526 
527   EXPECT_TRUE(FooMaterialized) << "Foo was not materialized";
528   EXPECT_TRUE(BarDiscarded) << "Bar was not discarded";
529   EXPECT_TRUE(OnResolutionRun) << "OnResolutionCallback was not run";
530   EXPECT_TRUE(OnReadyRun) << "OnReady was not run";
531 }
532 
533 TEST_F(CoreAPIsStandardTest, DefineMaterializingSymbol) {
534   bool ExpectNoMoreMaterialization = false;
535   ES.setDispatchMaterialization(
536       [&](VSO &V, std::unique_ptr<MaterializationUnit> MU) {
537         if (ExpectNoMoreMaterialization)
538           ADD_FAILURE() << "Unexpected materialization";
539         MU->doMaterialize(V);
540       });
541 
542   auto MU = llvm::make_unique<SimpleMaterializationUnit>(
543       SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
544       [&](MaterializationResponsibility R) {
545         cantFail(
546             R.defineMaterializing(SymbolFlagsMap({{Bar, BarSym.getFlags()}})));
547         R.resolve(SymbolMap({{Foo, FooSym}, {Bar, BarSym}}));
548         R.finalize();
549       });
550 
551   cantFail(V.define(MU));
552   cantFail(lookup({&V}, Foo));
553 
554   // Assert that materialization is complete by now.
555   ExpectNoMoreMaterialization = true;
556 
557   // Look up bar to verify that no further materialization happens.
558   auto BarResult = cantFail(lookup({&V}, Bar));
559   EXPECT_EQ(BarResult.getAddress(), BarSym.getAddress())
560       << "Expected Bar == BarSym";
561 }
562 
563 TEST_F(CoreAPIsStandardTest, FallbackDefinitionGeneratorTest) {
564   cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
565 
566   V.setFallbackDefinitionGenerator([&](VSO &W, const SymbolNameSet &Names) {
567     cantFail(W.define(absoluteSymbols({{Bar, BarSym}})));
568     return SymbolNameSet({Bar});
569   });
570 
571   auto Result = cantFail(lookup({&V}, {Foo, Bar}));
572 
573   EXPECT_EQ(Result.count(Bar), 1U) << "Expected to find fallback def for 'bar'";
574   EXPECT_EQ(Result[Bar].getAddress(), BarSym.getAddress())
575       << "Expected fallback def for Bar to be equal to BarSym";
576 }
577 
578 TEST_F(CoreAPIsStandardTest, FailResolution) {
579   auto MU = llvm::make_unique<SimpleMaterializationUnit>(
580       SymbolFlagsMap(
581           {{Foo, JITSymbolFlags::Weak}, {Bar, JITSymbolFlags::Weak}}),
582       [&](MaterializationResponsibility R) { R.failMaterialization(); });
583 
584   cantFail(V.define(MU));
585 
586   SymbolNameSet Names({Foo, Bar});
587   auto Result = lookup({&V}, Names);
588 
589   EXPECT_FALSE(!!Result) << "Expected failure";
590   if (!Result) {
591     handleAllErrors(Result.takeError(),
592                     [&](FailedToMaterialize &F) {
593                       EXPECT_EQ(F.getSymbols(), Names)
594                           << "Expected to fail on symbols in Names";
595                     },
596                     [](ErrorInfoBase &EIB) {
597                       std::string ErrMsg;
598                       {
599                         raw_string_ostream ErrOut(ErrMsg);
600                         EIB.log(ErrOut);
601                       }
602                       ADD_FAILURE()
603                           << "Expected a FailedToResolve error. Got:\n"
604                           << ErrMsg;
605                     });
606   }
607 }
608 
609 TEST_F(CoreAPIsStandardTest, TestLookupWithUnthreadedMaterialization) {
610   auto MU = llvm::make_unique<SimpleMaterializationUnit>(
611       SymbolFlagsMap({{Foo, JITSymbolFlags::Exported}}),
612       [&](MaterializationResponsibility R) {
613         R.resolve({{Foo, FooSym}});
614         R.finalize();
615       });
616 
617   cantFail(V.define(MU));
618 
619   auto FooLookupResult = cantFail(lookup({&V}, Foo));
620 
621   EXPECT_EQ(FooLookupResult.getAddress(), FooSym.getAddress())
622       << "lookup returned an incorrect address";
623   EXPECT_EQ(FooLookupResult.getFlags(), FooSym.getFlags())
624       << "lookup returned incorrect flags";
625 }
626 
627 TEST_F(CoreAPIsStandardTest, TestLookupWithThreadedMaterialization) {
628 #if LLVM_ENABLE_THREADS
629 
630   std::thread MaterializationThread;
631   ES.setDispatchMaterialization(
632       [&](VSO &V, std::unique_ptr<MaterializationUnit> MU) {
633         auto SharedMU = std::shared_ptr<MaterializationUnit>(std::move(MU));
634         MaterializationThread =
635             std::thread([SharedMU, &V]() { SharedMU->doMaterialize(V); });
636       });
637 
638   cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
639 
640   auto FooLookupResult = cantFail(lookup({&V}, Foo));
641 
642   EXPECT_EQ(FooLookupResult.getAddress(), FooSym.getAddress())
643       << "lookup returned an incorrect address";
644   EXPECT_EQ(FooLookupResult.getFlags(), FooSym.getFlags())
645       << "lookup returned incorrect flags";
646   MaterializationThread.join();
647 #endif
648 }
649 
650 TEST_F(CoreAPIsStandardTest, TestGetRequestedSymbolsAndReplace) {
651   // Test that GetRequestedSymbols returns the set of symbols that currently
652   // have pending queries, and test that MaterializationResponsibility's
653   // replace method can be used to return definitions to the VSO in a new
654   // MaterializationUnit.
655   SymbolNameSet Names({Foo, Bar});
656 
657   bool FooMaterialized = false;
658   bool BarMaterialized = false;
659 
660   auto MU = llvm::make_unique<SimpleMaterializationUnit>(
661       SymbolFlagsMap({{Foo, FooSym.getFlags()}, {Bar, BarSym.getFlags()}}),
662       [&](MaterializationResponsibility R) {
663         auto Requested = R.getRequestedSymbols();
664         EXPECT_EQ(Requested.size(), 1U) << "Expected one symbol requested";
665         EXPECT_EQ(*Requested.begin(), Foo) << "Expected \"Foo\" requested";
666 
667         auto NewMU = llvm::make_unique<SimpleMaterializationUnit>(
668             SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
669             [&](MaterializationResponsibility R2) {
670               R2.resolve(SymbolMap({{Bar, BarSym}}));
671               R2.finalize();
672               BarMaterialized = true;
673             });
674 
675         R.replace(std::move(NewMU));
676 
677         R.resolve(SymbolMap({{Foo, FooSym}}));
678         R.finalize();
679 
680         FooMaterialized = true;
681       });
682 
683   cantFail(V.define(MU));
684 
685   EXPECT_FALSE(FooMaterialized) << "Foo should not be materialized yet";
686   EXPECT_FALSE(BarMaterialized) << "Bar should not be materialized yet";
687 
688   auto FooSymResult = cantFail(lookup({&V}, Foo));
689   EXPECT_EQ(FooSymResult.getAddress(), FooSym.getAddress())
690       << "Address mismatch for Foo";
691 
692   EXPECT_TRUE(FooMaterialized) << "Foo should be materialized now";
693   EXPECT_FALSE(BarMaterialized) << "Bar still should not be materialized";
694 
695   auto BarSymResult = cantFail(lookup({&V}, Bar));
696   EXPECT_EQ(BarSymResult.getAddress(), BarSym.getAddress())
697       << "Address mismatch for Bar";
698   EXPECT_TRUE(BarMaterialized) << "Bar should be materialized now";
699 }
700 
701 TEST_F(CoreAPIsStandardTest, TestMaterializationResponsibilityDelegation) {
702   auto MU = llvm::make_unique<SimpleMaterializationUnit>(
703       SymbolFlagsMap({{Foo, FooSym.getFlags()}, {Bar, BarSym.getFlags()}}),
704       [&](MaterializationResponsibility R) {
705         auto R2 = R.delegate({Bar});
706 
707         R.resolve({{Foo, FooSym}});
708         R.finalize();
709         R2.resolve({{Bar, BarSym}});
710         R2.finalize();
711       });
712 
713   cantFail(V.define(MU));
714 
715   auto Result = lookup({&V}, {Foo, Bar});
716 
717   EXPECT_TRUE(!!Result) << "Result should be a success value";
718   EXPECT_EQ(Result->count(Foo), 1U) << "\"Foo\" entry missing";
719   EXPECT_EQ(Result->count(Bar), 1U) << "\"Bar\" entry missing";
720   EXPECT_EQ((*Result)[Foo].getAddress(), FooSym.getAddress())
721       << "Address mismatch for \"Foo\"";
722   EXPECT_EQ((*Result)[Bar].getAddress(), BarSym.getAddress())
723       << "Address mismatch for \"Bar\"";
724 }
725 
726 TEST_F(CoreAPIsStandardTest, TestMaterializeWeakSymbol) {
727   // Confirm that once a weak definition is selected for materialization it is
728   // treated as strong.
729   JITSymbolFlags WeakExported = JITSymbolFlags::Exported;
730   WeakExported &= JITSymbolFlags::Weak;
731 
732   std::unique_ptr<MaterializationResponsibility> FooResponsibility;
733   auto MU = llvm::make_unique<SimpleMaterializationUnit>(
734       SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
735       [&](MaterializationResponsibility R) {
736         FooResponsibility =
737             llvm::make_unique<MaterializationResponsibility>(std::move(R));
738       });
739 
740   cantFail(V.define(MU));
741   auto OnResolution = [](Expected<SymbolMap> Result) {
742     cantFail(std::move(Result));
743   };
744 
745   auto OnReady = [](Error Err) { cantFail(std::move(Err)); };
746 
747   ES.lookup({&V}, {Foo}, std::move(OnResolution), std::move(OnReady),
748             NoDependenciesToRegister);
749 
750   auto MU2 = llvm::make_unique<SimpleMaterializationUnit>(
751       SymbolFlagsMap({{Foo, JITSymbolFlags::Exported}}),
752       [](MaterializationResponsibility R) {
753         llvm_unreachable("This unit should never be materialized");
754       });
755 
756   auto Err = V.define(MU2);
757   EXPECT_TRUE(!!Err) << "Expected failure value";
758   EXPECT_TRUE(Err.isA<DuplicateDefinition>())
759       << "Expected a duplicate definition error";
760   consumeError(std::move(Err));
761 
762   FooResponsibility->resolve(SymbolMap({{Foo, FooSym}}));
763   FooResponsibility->finalize();
764 }
765 
766 } // namespace
767