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