OSDN Git Service

[ORC] Simplify VSO::lookupFlags to return the flags map.
[android-x86/external-llvm.git] / unittests / ExecutionEngine / Orc / CoreAPIsTest.cpp
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
63 TEST_F(CoreAPIsStandardTest, AsynchronousSymbolQuerySuccessfulResolutionOnly) {
64   bool OnResolutionRun = false;
65   bool OnReadyRun = false;
66   auto OnResolution =
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;
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   Q.resolve(Foo, FooSym);
84
85   EXPECT_TRUE(Q.isFullyResolved()) << "Expected query to be fully resolved";
86
87   if (!Q.isFullyResolved())
88     return;
89
90   Q.handleFullyResolved();
91
92   EXPECT_TRUE(OnResolutionRun) << "OnResolutionCallback was not run";
93   EXPECT_FALSE(OnReadyRun) << "OnReady unexpectedly run";
94 }
95
96 TEST_F(CoreAPIsStandardTest, ExecutionSessionFailQuery) {
97   bool OnResolutionRun = false;
98   bool OnReadyRun = false;
99
100   auto OnResolution =
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;
106       };
107   auto OnReady = [&](Error Err) {
108     cantFail(std::move(Err));
109     OnReadyRun = true;
110   };
111
112   AsynchronousSymbolQuery Q(SymbolNameSet({Foo}), OnResolution, OnReady);
113
114   ES.failQuery(Q, make_error<StringError>("xyz", inconvertibleErrorCode()));
115
116   EXPECT_TRUE(OnResolutionRun) << "OnResolutionCallback was not run";
117   EXPECT_FALSE(OnReadyRun) << "OnReady unexpectedly run";
118 }
119
120 TEST_F(CoreAPIsStandardTest, SimpleAsynchronousSymbolQueryAgainstVSO) {
121   bool OnResolutionRun = false;
122   bool OnReadyRun = false;
123
124   auto OnResolution =
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;
133       };
134
135   auto OnReady = [&](Error Err) {
136     cantFail(std::move(Err));
137     OnReadyRun = true;
138   };
139
140   SymbolNameSet Names({Foo});
141
142   auto Q =
143       std::make_shared<AsynchronousSymbolQuery>(Names, OnResolution, OnReady);
144
145   auto Defs = absoluteSymbols({{Foo, FooSym}});
146   cantFail(V.define(Defs));
147   assert(Defs == nullptr && "Defs should have been accepted");
148   V.lookup(Q, Names);
149
150   EXPECT_TRUE(OnResolutionRun) << "OnResolutionCallback was not run";
151   EXPECT_TRUE(OnReadyRun) << "OnReady was not run";
152 }
153
154 TEST_F(CoreAPIsStandardTest, EmptyVSOAndQueryLookup) {
155   bool OnResolvedRun = false;
156   bool OnReadyRun = false;
157
158   auto Q = std::make_shared<AsynchronousSymbolQuery>(
159       SymbolNameSet(),
160       [&](Expected<AsynchronousSymbolQuery::ResolutionResult> RR) {
161         cantFail(std::move(RR));
162         OnResolvedRun = true;
163       },
164       [&](Error Err) {
165         cantFail(std::move(Err));
166         OnReadyRun = true;
167       });
168
169   V.lookup(std::move(Q), {});
170
171   EXPECT_TRUE(OnResolvedRun) << "OnResolved was not run for empty query";
172   EXPECT_TRUE(OnReadyRun) << "OnReady was not run for empty query";
173 }
174
175 TEST_F(CoreAPIsStandardTest, ChainedVSOLookup) {
176   cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
177
178   auto &V2 = ES.createVSO("V2");
179
180   bool OnResolvedRun = false;
181   bool OnReadyRun = false;
182
183   auto Q = std::make_shared<AsynchronousSymbolQuery>(
184       SymbolNameSet({Foo}),
185       [&](Expected<AsynchronousSymbolQuery::ResolutionResult> RR) {
186         cantFail(std::move(RR));
187         OnResolvedRun = true;
188       },
189       [&](Error Err) {
190         cantFail(std::move(Err));
191         OnReadyRun = true;
192       });
193
194   V2.lookup(Q, V.lookup(Q, {Foo}));
195
196   EXPECT_TRUE(OnResolvedRun) << "OnResolved was not run for empty query";
197   EXPECT_TRUE(OnReadyRun) << "OnReady was not run for empty query";
198 }
199
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.
204
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");
211       });
212
213   cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
214   cantFail(V.define(std::move(MU)));
215
216   SymbolNameSet Names({Foo, Bar, Baz});
217
218   auto SymbolFlags = V.lookupFlags(Names);
219
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";
229 }
230
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}})));
236
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";
245 }
246
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()}}})));
251
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";
260 }
261
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)); });
267
268   cantFail(V.define(FooMU));
269
270   bool FooReady = false;
271   auto Q =
272     std::make_shared<AsynchronousSymbolQuery>(
273       SymbolNameSet({ Foo }),
274       [](Expected<AsynchronousSymbolQuery::ResolutionResult> R) {
275         cantFail(std::move(R));
276       },
277       [&](Error Err) {
278         cantFail(std::move(Err));
279         FooReady = true;
280       });
281
282   V.lookup(std::move(Q), { Foo });
283
284   FooR->addDependencies({{&V, {Foo}}});
285   FooR->resolve({{Foo, FooSym}});
286   FooR->finalize();
287
288   EXPECT_TRUE(FooReady)
289     << "Self-dependency prevented symbol from being marked ready";
290 }
291
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
295   // finalized.
296
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;
303
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)); });
309
310   auto BarMU = llvm::make_unique<SimpleMaterializationUnit>(
311       SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
312       [&](MaterializationResponsibility R) { BarR.emplace(std::move(R)); });
313
314   auto BazMU = llvm::make_unique<SimpleMaterializationUnit>(
315       SymbolFlagsMap({{Baz, BazSym.getFlags()}}),
316       [&](MaterializationResponsibility R) { BazR.emplace(std::move(R)); });
317
318   // Define the symbols.
319   cantFail(V.define(FooMU));
320   cantFail(V.define(BarMU));
321   cantFail(V.define(BazMU));
322
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));
330         FooResolved = true;
331       },
332       [&](Error Err) {
333         cantFail(std::move(Err));
334         FooReady = true;
335       });
336   {
337     auto Unresolved = V.lookup(FooQ, {Foo});
338     EXPECT_TRUE(Unresolved.empty()) << "Failed to resolve \"Foo\"";
339   }
340
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));
347         BarResolved = true;
348       },
349       [&](Error Err) {
350         cantFail(std::move(Err));
351         BarReady = true;
352       });
353   {
354     auto Unresolved = V.lookup(BarQ, {Bar});
355     EXPECT_TRUE(Unresolved.empty()) << "Failed to resolve \"Bar\"";
356   }
357
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));
364         BazResolved = true;
365       },
366       [&](Error Err) {
367         cantFail(std::move(Err));
368         BazReady = true;
369       });
370   {
371     auto Unresolved = V.lookup(BazQ, {Baz});
372     EXPECT_TRUE(Unresolved.empty()) << "Failed to resolve \"Baz\"";
373   }
374
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})}});
379
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})}});
385
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";
390
391   // Resolve the symbols (but do not finalized them).
392   FooR->resolve({{Foo, FooSym}});
393   BarR->resolve({{Bar, BarSym}});
394   BazR->resolve({{Baz, BazSym}});
395
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";
400
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";
404
405   // Finalize two of the symbols.
406   FooR->finalize();
407   BarR->finalize();
408
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";
413
414   // Finalize the last symbol.
415   BazR->finalize();
416
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";
421 }
422
423 TEST_F(CoreAPIsStandardTest, DropMaterializerWhenEmpty) {
424   bool DestructorRun = false;
425
426   JITSymbolFlags WeakExported(JITSymbolFlags::Exported);
427   WeakExported |= JITSymbolFlags::Weak;
428
429   auto MU = llvm::make_unique<SimpleMaterializationUnit>(
430       SymbolFlagsMap({{Foo, WeakExported}, {Bar, WeakExported}}),
431       [](MaterializationResponsibility R) {
432         llvm_unreachable("Unexpected call to materialize");
433       },
434       [&](const VSO &V, SymbolStringPtr Name) {
435         EXPECT_TRUE(Name == Foo || Name == Bar)
436             << "Discard of unexpected symbol?";
437       },
438       [&]() { DestructorRun = true; });
439
440   cantFail(V.define(MU));
441
442   cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
443
444   EXPECT_FALSE(DestructorRun)
445       << "MaterializationUnit should not have been destroyed yet";
446
447   cantFail(V.define(absoluteSymbols({{Bar, BarSym}})));
448
449   EXPECT_TRUE(DestructorRun)
450       << "MaterializationUnit should have been destroyed";
451 }
452
453 TEST_F(CoreAPIsStandardTest, AddAndMaterializeLazySymbol) {
454   bool FooMaterialized = false;
455   bool BarDiscarded = false;
456
457   JITSymbolFlags WeakExported(JITSymbolFlags::Exported);
458   WeakExported |= JITSymbolFlags::Weak;
459
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}}));
465         R.finalize();
466         FooMaterialized = true;
467       },
468       [&](const VSO &V, SymbolStringPtr Name) {
469         EXPECT_EQ(Name, Bar) << "Expected Name to be Bar";
470         BarDiscarded = true;
471       });
472
473   cantFail(V.define(MU));
474   cantFail(V.define(absoluteSymbols({{Bar, BarSym}})));
475
476   SymbolNameSet Names({Foo});
477
478   bool OnResolutionRun = false;
479   bool OnReadyRun = false;
480
481   auto OnResolution =
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;
490       };
491
492   auto OnReady = [&](Error Err) {
493     cantFail(std::move(Err));
494     OnReadyRun = true;
495   };
496
497   auto Q =
498       std::make_shared<AsynchronousSymbolQuery>(Names, OnResolution, OnReady);
499
500   auto Unresolved = V.lookup(std::move(Q), Names);
501
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";
507 }
508
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);
516       });
517
518   auto MU = llvm::make_unique<SimpleMaterializationUnit>(
519       SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
520       [&](MaterializationResponsibility R) {
521         cantFail(
522             R.defineMaterializing(SymbolFlagsMap({{Bar, BarSym.getFlags()}})));
523         R.resolve(SymbolMap({{Foo, FooSym}, {Bar, BarSym}}));
524         R.finalize();
525       });
526
527   cantFail(V.define(MU));
528   cantFail(lookup({&V}, Foo));
529
530   // Assert that materialization is complete by now.
531   ExpectNoMoreMaterialization = true;
532
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";
537 }
538
539 TEST_F(CoreAPIsStandardTest, FallbackDefinitionGeneratorTest) {
540   cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
541
542   V.setFallbackDefinitionGenerator([&](VSO &W, const SymbolNameSet &Names) {
543     cantFail(W.define(absoluteSymbols({{Bar, BarSym}})));
544     return SymbolNameSet({Bar});
545   });
546
547   auto Result = cantFail(lookup({&V}, {Foo, Bar}));
548
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";
552 }
553
554 TEST_F(CoreAPIsStandardTest, FailResolution) {
555   auto MU = llvm::make_unique<SimpleMaterializationUnit>(
556       SymbolFlagsMap(
557           {{Foo, JITSymbolFlags::Weak}, {Bar, JITSymbolFlags::Weak}}),
558       [&](MaterializationResponsibility R) { R.failMaterialization(); });
559
560   cantFail(V.define(MU));
561
562   SymbolNameSet Names({Foo, Bar});
563   auto Result = lookup({&V}, Names);
564
565   EXPECT_FALSE(!!Result) << "Expected failure";
566   if (!Result) {
567     handleAllErrors(Result.takeError(),
568                     [&](FailedToMaterialize &F) {
569                       EXPECT_EQ(F.getSymbols(), Names)
570                           << "Expected to fail on symbols in Names";
571                     },
572                     [](ErrorInfoBase &EIB) {
573                       std::string ErrMsg;
574                       {
575                         raw_string_ostream ErrOut(ErrMsg);
576                         EIB.log(ErrOut);
577                       }
578                       ADD_FAILURE()
579                           << "Expected a FailedToResolve error. Got:\n"
580                           << ErrMsg;
581                     });
582   }
583 }
584
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}});
590         R.finalize();
591       });
592
593   cantFail(V.define(MU));
594
595   auto FooLookupResult = cantFail(lookup({&V}, Foo));
596
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";
601 }
602
603 TEST_F(CoreAPIsStandardTest, TestLookupWithThreadedMaterialization) {
604 #if LLVM_ENABLE_THREADS
605
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); });
612       });
613
614   cantFail(V.define(absoluteSymbols({{Foo, FooSym}})));
615
616   auto FooLookupResult = cantFail(lookup({&V}, Foo));
617
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();
623 #endif
624 }
625
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});
632
633   bool FooMaterialized = false;
634   bool BarMaterialized = false;
635
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";
642
643         auto NewMU = llvm::make_unique<SimpleMaterializationUnit>(
644             SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
645             [&](MaterializationResponsibility R2) {
646               R2.resolve(SymbolMap({{Bar, BarSym}}));
647               R2.finalize();
648               BarMaterialized = true;
649             });
650
651         R.replace(std::move(NewMU));
652
653         R.resolve(SymbolMap({{Foo, FooSym}}));
654         R.finalize();
655
656         FooMaterialized = true;
657       });
658
659   cantFail(V.define(MU));
660
661   EXPECT_FALSE(FooMaterialized) << "Foo should not be materialized yet";
662   EXPECT_FALSE(BarMaterialized) << "Bar should not be materialized yet";
663
664   auto FooSymResult = cantFail(lookup({&V}, Foo));
665   EXPECT_EQ(FooSymResult.getAddress(), FooSym.getAddress())
666       << "Address mismatch for Foo";
667
668   EXPECT_TRUE(FooMaterialized) << "Foo should be materialized now";
669   EXPECT_FALSE(BarMaterialized) << "Bar still should not be materialized";
670
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";
675 }
676
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});
682
683         R.resolve({{Foo, FooSym}});
684         R.finalize();
685         R2.resolve({{Bar, BarSym}});
686         R2.finalize();
687       });
688
689   cantFail(V.define(MU));
690
691   auto Result = lookup({&V}, {Foo, Bar});
692
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\"";
700 }
701
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;
707
708   std::unique_ptr<MaterializationResponsibility> FooResponsibility;
709   auto MU = llvm::make_unique<SimpleMaterializationUnit>(
710       SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
711       [&](MaterializationResponsibility R) {
712         FooResponsibility =
713             llvm::make_unique<MaterializationResponsibility>(std::move(R));
714       });
715
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));
721       },
722       [](Error Err) { cantFail(std::move(Err)); });
723   V.lookup(std::move(Q), SymbolNameSet({Foo}));
724
725   auto MU2 = llvm::make_unique<SimpleMaterializationUnit>(
726       SymbolFlagsMap({{Foo, JITSymbolFlags::Exported}}),
727       [](MaterializationResponsibility R) {
728         llvm_unreachable("This unit should never be materialized");
729       });
730
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));
736
737   FooResponsibility->resolve(SymbolMap({{Foo, FooSym}}));
738   FooResponsibility->finalize();
739 }
740
741 } // namespace