1 //===----------- CoreAPIsTest.cpp - Unit tests for Core ORC APIs ----------===//
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
7 //===----------------------------------------------------------------------===//
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 #include "llvm/Testing/Support/Error.h"
19 using namespace llvm::orc
;
21 class CoreAPIsStandardTest
: public CoreAPIsBasedStandardTest
{};
25 TEST_F(CoreAPIsStandardTest
, BasicSuccessfulLookup
) {
26 bool OnCompletionRun
= false;
28 auto OnCompletion
= [&](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 OnCompletionRun
= true;
38 std::shared_ptr
<MaterializationResponsibility
> FooMR
;
40 cantFail(JD
.define(std::make_unique
<SimpleMaterializationUnit
>(
41 SymbolFlagsMap({{Foo
, FooSym
.getFlags()}}),
42 [&](MaterializationResponsibility R
) {
43 FooMR
= std::make_shared
<MaterializationResponsibility
>(std::move(R
));
46 ES
.lookup(JITDylibSearchList({{&JD
, false}}), {Foo
}, SymbolState::Ready
,
47 OnCompletion
, NoDependenciesToRegister
);
49 EXPECT_FALSE(OnCompletionRun
) << "Should not have been resolved yet";
51 cantFail(FooMR
->notifyResolved({{Foo
, FooSym
}}));
53 EXPECT_FALSE(OnCompletionRun
) << "Should not be ready yet";
55 cantFail(FooMR
->notifyEmitted());
57 EXPECT_TRUE(OnCompletionRun
) << "Should have been marked ready";
60 TEST_F(CoreAPIsStandardTest
, ExecutionSessionFailQuery
) {
61 bool OnCompletionRun
= false;
63 auto OnCompletion
= [&](Expected
<SymbolMap
> Result
) {
64 EXPECT_FALSE(!!Result
) << "Resolution unexpectedly returned success";
65 auto Msg
= toString(Result
.takeError());
66 EXPECT_EQ(Msg
, "xyz") << "Resolution returned incorrect result";
67 OnCompletionRun
= true;
70 AsynchronousSymbolQuery
Q(SymbolNameSet({Foo
}), SymbolState::Ready
,
74 make_error
<StringError
>("xyz", inconvertibleErrorCode()));
76 EXPECT_TRUE(OnCompletionRun
) << "OnCompletionCallback was not run";
79 TEST_F(CoreAPIsStandardTest
, EmptyLookup
) {
80 bool OnCompletionRun
= false;
82 auto OnCompletion
= [&](Expected
<SymbolMap
> Result
) {
83 cantFail(std::move(Result
));
84 OnCompletionRun
= true;
87 ES
.lookup(JITDylibSearchList({{&JD
, false}}), {}, SymbolState::Ready
,
88 OnCompletion
, NoDependenciesToRegister
);
90 EXPECT_TRUE(OnCompletionRun
) << "OnCompletion was not run for empty query";
93 TEST_F(CoreAPIsStandardTest
, RemoveSymbolsTest
) {
95 // (1) Missing symbols generate a SymbolsNotFound error.
96 // (2) Materializing symbols generate a SymbolCouldNotBeRemoved error.
97 // (3) Removal of unmaterialized symbols triggers discard on the
98 // materialization unit.
99 // (4) Removal of symbols destroys empty materialization units.
100 // (5) Removal of materialized symbols works.
102 // Foo will be fully materialized.
103 cantFail(JD
.define(absoluteSymbols({{Foo
, FooSym
}})));
105 // Bar will be unmaterialized.
106 bool BarDiscarded
= false;
107 bool BarMaterializerDestructed
= false;
108 cantFail(JD
.define(std::make_unique
<SimpleMaterializationUnit
>(
109 SymbolFlagsMap({{Bar
, BarSym
.getFlags()}}),
110 [this](MaterializationResponsibility R
) {
111 ADD_FAILURE() << "Unexpected materialization of \"Bar\"";
112 cantFail(R
.notifyResolved({{Bar
, BarSym
}}));
113 cantFail(R
.notifyEmitted());
115 [&](const JITDylib
&JD
, const SymbolStringPtr
&Name
) {
116 EXPECT_EQ(Name
, Bar
) << "Expected \"Bar\" to be discarded";
120 [&]() { BarMaterializerDestructed
= true; })));
122 // Baz will be in the materializing state initially, then
123 // materialized for the final removal attempt.
124 Optional
<MaterializationResponsibility
> BazR
;
125 cantFail(JD
.define(std::make_unique
<SimpleMaterializationUnit
>(
126 SymbolFlagsMap({{Baz
, BazSym
.getFlags()}}),
127 [&](MaterializationResponsibility R
) { BazR
.emplace(std::move(R
)); },
128 [](const JITDylib
&JD
, const SymbolStringPtr
&Name
) {
129 ADD_FAILURE() << "\"Baz\" discarded unexpectedly";
132 bool OnCompletionRun
= false;
134 JITDylibSearchList({{&JD
, false}}), {Foo
, Baz
}, SymbolState::Ready
,
135 [&](Expected
<SymbolMap
> Result
) {
136 cantFail(Result
.takeError());
137 OnCompletionRun
= true;
139 NoDependenciesToRegister
);
142 // Attempt 1: Search for a missing symbol, Qux.
143 auto Err
= JD
.remove({Foo
, Bar
, Baz
, Qux
});
144 EXPECT_TRUE(!!Err
) << "Expected failure";
145 EXPECT_TRUE(Err
.isA
<SymbolsNotFound
>())
146 << "Expected a SymbolsNotFound error";
147 consumeError(std::move(Err
));
151 // Attempt 2: Search for a symbol that is still materializing, Baz.
152 auto Err
= JD
.remove({Foo
, Bar
, Baz
});
153 EXPECT_TRUE(!!Err
) << "Expected failure";
154 EXPECT_TRUE(Err
.isA
<SymbolsCouldNotBeRemoved
>())
155 << "Expected a SymbolsNotFound error";
156 consumeError(std::move(Err
));
159 cantFail(BazR
->notifyResolved({{Baz
, BazSym
}}));
160 cantFail(BazR
->notifyEmitted());
162 // Attempt 3: Search now that all symbols are fully materialized
163 // (Foo, Baz), or not yet materialized (Bar).
164 auto Err
= JD
.remove({Foo
, Bar
, Baz
});
165 EXPECT_FALSE(!!Err
) << "Expected failure";
168 EXPECT_TRUE(BarDiscarded
) << "\"Bar\" should have been discarded";
169 EXPECT_TRUE(BarMaterializerDestructed
)
170 << "\"Bar\"'s materializer should have been destructed";
171 EXPECT_TRUE(OnCompletionRun
) << "OnCompletion should have been run";
174 TEST_F(CoreAPIsStandardTest
, ChainedJITDylibLookup
) {
175 cantFail(JD
.define(absoluteSymbols({{Foo
, FooSym
}})));
177 auto &JD2
= ES
.createJITDylib("JD2");
179 bool OnCompletionRun
= false;
181 auto Q
= std::make_shared
<AsynchronousSymbolQuery
>(
182 SymbolNameSet({Foo
}), SymbolState::Ready
,
183 [&](Expected
<SymbolMap
> Result
) {
184 cantFail(std::move(Result
));
185 OnCompletionRun
= true;
188 cantFail(JD2
.legacyLookup(Q
, cantFail(JD
.legacyLookup(Q
, {Foo
}))));
190 EXPECT_TRUE(OnCompletionRun
) << "OnCompletion was not run for empty query";
193 TEST_F(CoreAPIsStandardTest
, LookupWithHiddenSymbols
) {
194 auto BarHiddenFlags
= BarSym
.getFlags() & ~JITSymbolFlags::Exported
;
195 auto BarHiddenSym
= JITEvaluatedSymbol(BarSym
.getAddress(), BarHiddenFlags
);
197 cantFail(JD
.define(absoluteSymbols({{Foo
, FooSym
}, {Bar
, BarHiddenSym
}})));
199 auto &JD2
= ES
.createJITDylib("JD2");
200 cantFail(JD2
.define(absoluteSymbols({{Bar
, QuxSym
}})));
202 /// Try a blocking lookup.
203 auto Result
= cantFail(
204 ES
.lookup(JITDylibSearchList({{&JD
, false}, {&JD2
, false}}), {Foo
, Bar
}));
206 EXPECT_EQ(Result
.size(), 2U) << "Unexpected number of results";
207 EXPECT_EQ(Result
.count(Foo
), 1U) << "Missing result for \"Foo\"";
208 EXPECT_EQ(Result
.count(Bar
), 1U) << "Missing result for \"Bar\"";
209 EXPECT_EQ(Result
[Bar
].getAddress(), QuxSym
.getAddress())
210 << "Wrong result for \"Bar\"";
213 TEST_F(CoreAPIsStandardTest
, LookupFlagsTest
) {
214 // Test that lookupFlags works on a predefined symbol, and does not trigger
215 // materialization of a lazy symbol. Make the lazy symbol weak to test that
216 // the weak flag is propagated correctly.
218 BarSym
.setFlags(static_cast<JITSymbolFlags::FlagNames
>(
219 JITSymbolFlags::Exported
| JITSymbolFlags::Weak
));
220 auto MU
= std::make_unique
<SimpleMaterializationUnit
>(
221 SymbolFlagsMap({{Bar
, BarSym
.getFlags()}}),
222 [](MaterializationResponsibility R
) {
223 llvm_unreachable("Symbol materialized on flags lookup");
226 cantFail(JD
.define(absoluteSymbols({{Foo
, FooSym
}})));
227 cantFail(JD
.define(std::move(MU
)));
229 SymbolNameSet
Names({Foo
, Bar
, Baz
});
231 auto SymbolFlags
= cantFail(JD
.lookupFlags(Names
));
233 EXPECT_EQ(SymbolFlags
.size(), 2U)
234 << "Returned symbol flags contains unexpected results";
235 EXPECT_EQ(SymbolFlags
.count(Foo
), 1U) << "Missing lookupFlags result for Foo";
236 EXPECT_EQ(SymbolFlags
[Foo
], FooSym
.getFlags())
237 << "Incorrect flags returned for Foo";
238 EXPECT_EQ(SymbolFlags
.count(Bar
), 1U)
239 << "Missing lookupFlags result for Bar";
240 EXPECT_EQ(SymbolFlags
[Bar
], BarSym
.getFlags())
241 << "Incorrect flags returned for Bar";
244 TEST_F(CoreAPIsStandardTest
, LookupWithGeneratorFailure
) {
246 class BadGenerator
: public JITDylib::DefinitionGenerator
{
248 Expected
<SymbolNameSet
> tryToGenerate(JITDylib
&,
249 const SymbolNameSet
&) override
{
250 return make_error
<StringError
>("BadGenerator", inconvertibleErrorCode());
254 JD
.addGenerator(std::make_unique
<BadGenerator
>());
256 EXPECT_THAT_ERROR(JD
.lookupFlags({Foo
}).takeError(), Failed
<StringError
>())
257 << "Generator failure did not propagate through lookupFlags";
260 ES
.lookup(JITDylibSearchList({{&JD
, false}}), SymbolNameSet({Foo
}))
262 Failed
<StringError
>())
263 << "Generator failure did not propagate through lookup";
266 TEST_F(CoreAPIsStandardTest
, TestBasicAliases
) {
267 cantFail(JD
.define(absoluteSymbols({{Foo
, FooSym
}, {Bar
, BarSym
}})));
268 cantFail(JD
.define(symbolAliases({{Baz
, {Foo
, JITSymbolFlags::Exported
}},
269 {Qux
, {Bar
, JITSymbolFlags::Weak
}}})));
270 cantFail(JD
.define(absoluteSymbols({{Qux
, QuxSym
}})));
272 auto Result
= ES
.lookup(JITDylibSearchList({{&JD
, false}}), {Baz
, Qux
});
273 EXPECT_TRUE(!!Result
) << "Unexpected lookup failure";
274 EXPECT_EQ(Result
->count(Baz
), 1U) << "No result for \"baz\"";
275 EXPECT_EQ(Result
->count(Qux
), 1U) << "No result for \"qux\"";
276 EXPECT_EQ((*Result
)[Baz
].getAddress(), FooSym
.getAddress())
277 << "\"Baz\"'s address should match \"Foo\"'s";
278 EXPECT_EQ((*Result
)[Qux
].getAddress(), QuxSym
.getAddress())
279 << "The \"Qux\" alias should have been overriden";
282 TEST_F(CoreAPIsStandardTest
, TestChainedAliases
) {
283 cantFail(JD
.define(absoluteSymbols({{Foo
, FooSym
}})));
284 cantFail(JD
.define(symbolAliases(
285 {{Baz
, {Bar
, BazSym
.getFlags()}}, {Bar
, {Foo
, BarSym
.getFlags()}}})));
287 auto Result
= ES
.lookup(JITDylibSearchList({{&JD
, false}}), {Bar
, Baz
});
288 EXPECT_TRUE(!!Result
) << "Unexpected lookup failure";
289 EXPECT_EQ(Result
->count(Bar
), 1U) << "No result for \"bar\"";
290 EXPECT_EQ(Result
->count(Baz
), 1U) << "No result for \"baz\"";
291 EXPECT_EQ((*Result
)[Bar
].getAddress(), FooSym
.getAddress())
292 << "\"Bar\"'s address should match \"Foo\"'s";
293 EXPECT_EQ((*Result
)[Baz
].getAddress(), FooSym
.getAddress())
294 << "\"Baz\"'s address should match \"Foo\"'s";
297 TEST_F(CoreAPIsStandardTest
, TestBasicReExports
) {
298 // Test that the basic use case of re-exporting a single symbol from another
300 cantFail(JD
.define(absoluteSymbols({{Foo
, FooSym
}})));
302 auto &JD2
= ES
.createJITDylib("JD2");
304 cantFail(JD2
.define(reexports(JD
, {{Bar
, {Foo
, BarSym
.getFlags()}}})));
306 auto Result
= cantFail(ES
.lookup(JITDylibSearchList({{&JD2
, false}}), Bar
));
307 EXPECT_EQ(Result
.getAddress(), FooSym
.getAddress())
308 << "Re-export Bar for symbol Foo should match FooSym's address";
311 TEST_F(CoreAPIsStandardTest
, TestThatReExportsDontUnnecessarilyMaterialize
) {
312 // Test that re-exports do not materialize symbols that have not been queried
314 cantFail(JD
.define(absoluteSymbols({{Foo
, FooSym
}})));
316 bool BarMaterialized
= false;
317 auto BarMU
= std::make_unique
<SimpleMaterializationUnit
>(
318 SymbolFlagsMap({{Bar
, BarSym
.getFlags()}}),
319 [&](MaterializationResponsibility R
) {
320 BarMaterialized
= true;
321 cantFail(R
.notifyResolved({{Bar
, BarSym
}}));
322 cantFail(R
.notifyEmitted());
325 cantFail(JD
.define(BarMU
));
327 auto &JD2
= ES
.createJITDylib("JD2");
329 cantFail(JD2
.define(reexports(
330 JD
, {{Baz
, {Foo
, BazSym
.getFlags()}}, {Qux
, {Bar
, QuxSym
.getFlags()}}})));
332 auto Result
= cantFail(ES
.lookup(JITDylibSearchList({{&JD2
, false}}), Baz
));
333 EXPECT_EQ(Result
.getAddress(), FooSym
.getAddress())
334 << "Re-export Baz for symbol Foo should match FooSym's address";
336 EXPECT_FALSE(BarMaterialized
) << "Bar should not have been materialized";
339 TEST_F(CoreAPIsStandardTest
, TestReexportsGenerator
) {
340 // Test that a re-exports generator can dynamically generate reexports.
342 auto &JD2
= ES
.createJITDylib("JD2");
343 cantFail(JD2
.define(absoluteSymbols({{Foo
, FooSym
}, {Bar
, BarSym
}})));
345 auto Filter
= [this](SymbolStringPtr Name
) { return Name
!= Bar
; };
347 JD
.addGenerator(std::make_unique
<ReexportsGenerator
>(JD2
, false, Filter
));
349 auto Flags
= cantFail(JD
.lookupFlags({Foo
, Bar
, Baz
}));
350 EXPECT_EQ(Flags
.size(), 1U) << "Unexpected number of results";
351 EXPECT_EQ(Flags
[Foo
], FooSym
.getFlags()) << "Unexpected flags for Foo";
353 auto Result
= cantFail(ES
.lookup(JITDylibSearchList({{&JD
, false}}), Foo
));
355 EXPECT_EQ(Result
.getAddress(), FooSym
.getAddress())
356 << "Incorrect reexported symbol address";
359 TEST_F(CoreAPIsStandardTest
, TestTrivialCircularDependency
) {
360 Optional
<MaterializationResponsibility
> FooR
;
361 auto FooMU
= std::make_unique
<SimpleMaterializationUnit
>(
362 SymbolFlagsMap({{Foo
, FooSym
.getFlags()}}),
363 [&](MaterializationResponsibility R
) { FooR
.emplace(std::move(R
)); });
365 cantFail(JD
.define(FooMU
));
367 bool FooReady
= false;
368 auto OnCompletion
= [&](Expected
<SymbolMap
> Result
) {
369 cantFail(std::move(Result
));
373 ES
.lookup(JITDylibSearchList({{&JD
, false}}), {Foo
}, SymbolState::Ready
,
374 OnCompletion
, NoDependenciesToRegister
);
376 FooR
->addDependenciesForAll({{&JD
, SymbolNameSet({Foo
})}});
377 EXPECT_THAT_ERROR(FooR
->notifyResolved({{Foo
, FooSym
}}), Succeeded())
378 << "No symbols marked failed, but Foo failed to resolve";
379 EXPECT_THAT_ERROR(FooR
->notifyEmitted(), Succeeded())
380 << "No symbols marked failed, but Foo failed to emit";
382 EXPECT_TRUE(FooReady
)
383 << "Self-dependency prevented symbol from being marked ready";
386 TEST_F(CoreAPIsStandardTest
, TestCircularDependenceInOneJITDylib
) {
387 // Test that a circular symbol dependency between three symbols in a JITDylib
388 // does not prevent any symbol from becoming 'ready' once all symbols are
391 // Create three MaterializationResponsibility objects: one for each of Foo,
392 // Bar and Baz. These are optional because MaterializationResponsibility
393 // does not have a default constructor).
394 Optional
<MaterializationResponsibility
> FooR
;
395 Optional
<MaterializationResponsibility
> BarR
;
396 Optional
<MaterializationResponsibility
> BazR
;
398 // Create a MaterializationUnit for each symbol that moves the
399 // MaterializationResponsibility into one of the locals above.
400 auto FooMU
= std::make_unique
<SimpleMaterializationUnit
>(
401 SymbolFlagsMap({{Foo
, FooSym
.getFlags()}}),
402 [&](MaterializationResponsibility R
) { FooR
.emplace(std::move(R
)); });
404 auto BarMU
= std::make_unique
<SimpleMaterializationUnit
>(
405 SymbolFlagsMap({{Bar
, BarSym
.getFlags()}}),
406 [&](MaterializationResponsibility R
) { BarR
.emplace(std::move(R
)); });
408 auto BazMU
= std::make_unique
<SimpleMaterializationUnit
>(
409 SymbolFlagsMap({{Baz
, BazSym
.getFlags()}}),
410 [&](MaterializationResponsibility R
) { BazR
.emplace(std::move(R
)); });
412 // Define the symbols.
413 cantFail(JD
.define(FooMU
));
414 cantFail(JD
.define(BarMU
));
415 cantFail(JD
.define(BazMU
));
417 // Query each of the symbols to trigger materialization.
418 bool FooResolved
= false;
419 bool FooReady
= false;
421 auto OnFooResolution
= [&](Expected
<SymbolMap
> Result
) {
422 cantFail(std::move(Result
));
426 auto OnFooReady
= [&](Expected
<SymbolMap
> Result
) {
427 cantFail(std::move(Result
));
431 // Issue lookups for Foo. Use NoDependenciesToRegister: We're going to add
432 // the dependencies manually below.
433 ES
.lookup(JITDylibSearchList({{&JD
, false}}), {Foo
}, SymbolState::Resolved
,
434 std::move(OnFooResolution
), NoDependenciesToRegister
);
436 ES
.lookup(JITDylibSearchList({{&JD
, false}}), {Foo
}, SymbolState::Ready
,
437 std::move(OnFooReady
), NoDependenciesToRegister
);
439 bool BarResolved
= false;
440 bool BarReady
= false;
441 auto OnBarResolution
= [&](Expected
<SymbolMap
> Result
) {
442 cantFail(std::move(Result
));
446 auto OnBarReady
= [&](Expected
<SymbolMap
> Result
) {
447 cantFail(std::move(Result
));
451 ES
.lookup(JITDylibSearchList({{&JD
, false}}), {Bar
}, SymbolState::Resolved
,
452 std::move(OnBarResolution
), NoDependenciesToRegister
);
454 ES
.lookup(JITDylibSearchList({{&JD
, false}}), {Bar
}, SymbolState::Ready
,
455 std::move(OnBarReady
), NoDependenciesToRegister
);
457 bool BazResolved
= false;
458 bool BazReady
= false;
460 auto OnBazResolution
= [&](Expected
<SymbolMap
> Result
) {
461 cantFail(std::move(Result
));
465 auto OnBazReady
= [&](Expected
<SymbolMap
> Result
) {
466 cantFail(std::move(Result
));
470 ES
.lookup(JITDylibSearchList({{&JD
, false}}), {Baz
}, SymbolState::Resolved
,
471 std::move(OnBazResolution
), NoDependenciesToRegister
);
473 ES
.lookup(JITDylibSearchList({{&JD
, false}}), {Baz
}, SymbolState::Ready
,
474 std::move(OnBazReady
), NoDependenciesToRegister
);
476 // Add a circular dependency: Foo -> Bar, Bar -> Baz, Baz -> Foo.
477 FooR
->addDependenciesForAll({{&JD
, SymbolNameSet({Bar
})}});
478 BarR
->addDependenciesForAll({{&JD
, SymbolNameSet({Baz
})}});
479 BazR
->addDependenciesForAll({{&JD
, SymbolNameSet({Foo
})}});
481 // Add self-dependencies for good measure. This tests that the implementation
482 // of addDependencies filters these out.
483 FooR
->addDependenciesForAll({{&JD
, SymbolNameSet({Foo
})}});
484 BarR
->addDependenciesForAll({{&JD
, SymbolNameSet({Bar
})}});
485 BazR
->addDependenciesForAll({{&JD
, SymbolNameSet({Baz
})}});
487 // Check that nothing has been resolved yet.
488 EXPECT_FALSE(FooResolved
) << "\"Foo\" should not be resolved yet";
489 EXPECT_FALSE(BarResolved
) << "\"Bar\" should not be resolved yet";
490 EXPECT_FALSE(BazResolved
) << "\"Baz\" should not be resolved yet";
492 // Resolve the symbols (but do not emit them).
493 EXPECT_THAT_ERROR(FooR
->notifyResolved({{Foo
, FooSym
}}), Succeeded())
494 << "No symbols failed, but Foo failed to resolve";
495 EXPECT_THAT_ERROR(BarR
->notifyResolved({{Bar
, BarSym
}}), Succeeded())
496 << "No symbols failed, but Bar failed to resolve";
497 EXPECT_THAT_ERROR(BazR
->notifyResolved({{Baz
, BazSym
}}), Succeeded())
498 << "No symbols failed, but Baz failed to resolve";
500 // Verify that the symbols have been resolved, but are not ready yet.
501 EXPECT_TRUE(FooResolved
) << "\"Foo\" should be resolved now";
502 EXPECT_TRUE(BarResolved
) << "\"Bar\" should be resolved now";
503 EXPECT_TRUE(BazResolved
) << "\"Baz\" should be resolved now";
505 EXPECT_FALSE(FooReady
) << "\"Foo\" should not be ready yet";
506 EXPECT_FALSE(BarReady
) << "\"Bar\" should not be ready yet";
507 EXPECT_FALSE(BazReady
) << "\"Baz\" should not be ready yet";
509 // Emit two of the symbols.
510 EXPECT_THAT_ERROR(FooR
->notifyEmitted(), Succeeded())
511 << "No symbols failed, but Foo failed to emit";
512 EXPECT_THAT_ERROR(BarR
->notifyEmitted(), Succeeded())
513 << "No symbols failed, but Bar failed to emit";
515 // Verify that nothing is ready until the circular dependence is resolved.
516 EXPECT_FALSE(FooReady
) << "\"Foo\" still should not be ready";
517 EXPECT_FALSE(BarReady
) << "\"Bar\" still should not be ready";
518 EXPECT_FALSE(BazReady
) << "\"Baz\" still should not be ready";
520 // Emit the last symbol.
521 EXPECT_THAT_ERROR(BazR
->notifyEmitted(), Succeeded())
522 << "No symbols failed, but Baz failed to emit";
524 // Verify that everything becomes ready once the circular dependence resolved.
525 EXPECT_TRUE(FooReady
) << "\"Foo\" should be ready now";
526 EXPECT_TRUE(BarReady
) << "\"Bar\" should be ready now";
527 EXPECT_TRUE(BazReady
) << "\"Baz\" should be ready now";
530 TEST_F(CoreAPIsStandardTest
, FailureInDependency
) {
531 Optional
<MaterializationResponsibility
> FooR
;
532 Optional
<MaterializationResponsibility
> BarR
;
534 // Create a MaterializationUnit for each symbol that moves the
535 // MaterializationResponsibility into one of the locals above.
536 auto FooMU
= std::make_unique
<SimpleMaterializationUnit
>(
537 SymbolFlagsMap({{Foo
, FooSym
.getFlags()}}),
538 [&](MaterializationResponsibility R
) { FooR
.emplace(std::move(R
)); });
540 auto BarMU
= std::make_unique
<SimpleMaterializationUnit
>(
541 SymbolFlagsMap({{Bar
, BarSym
.getFlags()}}),
542 [&](MaterializationResponsibility R
) { BarR
.emplace(std::move(R
)); });
544 // Define the symbols.
545 cantFail(JD
.define(FooMU
));
546 cantFail(JD
.define(BarMU
));
548 bool OnFooReadyRun
= false;
549 auto OnFooReady
= [&](Expected
<SymbolMap
> Result
) {
550 EXPECT_THAT_EXPECTED(std::move(Result
), Failed());
551 OnFooReadyRun
= true;
554 ES
.lookup(JITDylibSearchList({{&JD
, false}}), {Foo
}, SymbolState::Ready
,
555 std::move(OnFooReady
), NoDependenciesToRegister
);
557 bool OnBarReadyRun
= false;
558 auto OnBarReady
= [&](Expected
<SymbolMap
> Result
) {
559 EXPECT_THAT_EXPECTED(std::move(Result
), Failed());
560 OnBarReadyRun
= true;
563 ES
.lookup(JITDylibSearchList({{&JD
, false}}), {Bar
}, SymbolState::Ready
,
564 std::move(OnBarReady
), NoDependenciesToRegister
);
566 // Add a dependency by Foo on Bar.
567 FooR
->addDependenciesForAll({{&JD
, SymbolNameSet({Bar
})}});
570 BarR
->failMaterialization();
572 // Verify that queries on Bar failed, but queries on Foo have not yet.
573 EXPECT_TRUE(OnBarReadyRun
) << "Query for \"Bar\" was not run";
574 EXPECT_FALSE(OnFooReadyRun
) << "Query for \"Foo\" was run unexpectedly";
576 // Check that we can still resolve Foo (even though it has been failed).
577 EXPECT_THAT_ERROR(FooR
->notifyResolved({{Foo
, FooSym
}}), Failed())
578 << "Expected resolution for \"Foo\" to fail.";
580 FooR
->failMaterialization();
582 // Verify that queries on Foo have now failed.
583 EXPECT_TRUE(OnFooReadyRun
) << "Query for \"Foo\" was not run";
585 // Verify that subsequent lookups on Bar and Foo fail.
586 EXPECT_THAT_EXPECTED(ES
.lookup({&JD
}, {Bar
}), Failed())
587 << "Lookup on failed symbol should fail";
589 EXPECT_THAT_EXPECTED(ES
.lookup({&JD
}, {Foo
}), Failed())
590 << "Lookup on failed symbol should fail";
593 TEST_F(CoreAPIsStandardTest
, FailureInCircularDependency
) {
594 Optional
<MaterializationResponsibility
> FooR
;
595 Optional
<MaterializationResponsibility
> BarR
;
597 // Create a MaterializationUnit for each symbol that moves the
598 // MaterializationResponsibility into one of the locals above.
599 auto FooMU
= std::make_unique
<SimpleMaterializationUnit
>(
600 SymbolFlagsMap({{Foo
, FooSym
.getFlags()}}),
601 [&](MaterializationResponsibility R
) { FooR
.emplace(std::move(R
)); });
603 auto BarMU
= std::make_unique
<SimpleMaterializationUnit
>(
604 SymbolFlagsMap({{Bar
, BarSym
.getFlags()}}),
605 [&](MaterializationResponsibility R
) { BarR
.emplace(std::move(R
)); });
607 // Define the symbols.
608 cantFail(JD
.define(FooMU
));
609 cantFail(JD
.define(BarMU
));
611 bool OnFooReadyRun
= false;
612 auto OnFooReady
= [&](Expected
<SymbolMap
> Result
) {
613 EXPECT_THAT_EXPECTED(std::move(Result
), Failed());
614 OnFooReadyRun
= true;
617 ES
.lookup(JITDylibSearchList({{&JD
, false}}), {Foo
}, SymbolState::Ready
,
618 std::move(OnFooReady
), NoDependenciesToRegister
);
620 bool OnBarReadyRun
= false;
621 auto OnBarReady
= [&](Expected
<SymbolMap
> Result
) {
622 EXPECT_THAT_EXPECTED(std::move(Result
), Failed());
623 OnBarReadyRun
= true;
626 ES
.lookup(JITDylibSearchList({{&JD
, false}}), {Bar
}, SymbolState::Ready
,
627 std::move(OnBarReady
), NoDependenciesToRegister
);
629 // Add a dependency by Foo on Bar and vice-versa.
630 FooR
->addDependenciesForAll({{&JD
, SymbolNameSet({Bar
})}});
631 BarR
->addDependenciesForAll({{&JD
, SymbolNameSet({Foo
})}});
634 BarR
->failMaterialization();
636 // Verify that queries on Bar failed, but queries on Foo have not yet.
637 EXPECT_TRUE(OnBarReadyRun
) << "Query for \"Bar\" was not run";
638 EXPECT_FALSE(OnFooReadyRun
) << "Query for \"Foo\" was run unexpectedly";
640 // Verify that trying to resolve Foo fails.
641 EXPECT_THAT_ERROR(FooR
->notifyResolved({{Foo
, FooSym
}}), Failed())
642 << "Expected resolution for \"Foo\" to fail.";
644 FooR
->failMaterialization();
646 // Verify that queries on Foo have now failed.
647 EXPECT_TRUE(OnFooReadyRun
) << "Query for \"Foo\" was not run";
649 // Verify that subsequent lookups on Bar and Foo fail.
650 EXPECT_THAT_EXPECTED(ES
.lookup({&JD
}, {Bar
}), Failed())
651 << "Lookup on failed symbol should fail";
653 EXPECT_THAT_EXPECTED(ES
.lookup({&JD
}, {Foo
}), Failed())
654 << "Lookup on failed symbol should fail";
657 TEST_F(CoreAPIsStandardTest
, AddDependencyOnFailedSymbol
) {
658 Optional
<MaterializationResponsibility
> FooR
;
659 Optional
<MaterializationResponsibility
> BarR
;
661 // Create a MaterializationUnit for each symbol that moves the
662 // MaterializationResponsibility into one of the locals above.
663 auto FooMU
= std::make_unique
<SimpleMaterializationUnit
>(
664 SymbolFlagsMap({{Foo
, FooSym
.getFlags()}}),
665 [&](MaterializationResponsibility R
) { FooR
.emplace(std::move(R
)); });
667 auto BarMU
= std::make_unique
<SimpleMaterializationUnit
>(
668 SymbolFlagsMap({{Bar
, BarSym
.getFlags()}}),
669 [&](MaterializationResponsibility R
) { BarR
.emplace(std::move(R
)); });
671 // Define the symbols.
672 cantFail(JD
.define(FooMU
));
673 cantFail(JD
.define(BarMU
));
675 bool OnFooReadyRun
= false;
676 auto OnFooReady
= [&](Expected
<SymbolMap
> Result
) {
677 EXPECT_THAT_EXPECTED(std::move(Result
), Failed());
678 OnFooReadyRun
= true;
681 ES
.lookup(JITDylibSearchList({{&JD
, false}}), {Foo
}, SymbolState::Ready
,
682 std::move(OnFooReady
), NoDependenciesToRegister
);
684 bool OnBarReadyRun
= false;
685 auto OnBarReady
= [&](Expected
<SymbolMap
> Result
) {
686 EXPECT_THAT_EXPECTED(std::move(Result
), Failed());
687 OnBarReadyRun
= true;
690 ES
.lookup(JITDylibSearchList({{&JD
, false}}), {Bar
}, SymbolState::Ready
,
691 std::move(OnBarReady
), NoDependenciesToRegister
);
694 BarR
->failMaterialization();
696 // We expect Bar's query to fail immediately, but Foo's query not to have run
698 EXPECT_TRUE(OnBarReadyRun
) << "Query for \"Bar\" was not run";
699 EXPECT_FALSE(OnFooReadyRun
) << "Query for \"Foo\" should not have run yet";
701 // Add dependency of Foo on Bar.
702 FooR
->addDependenciesForAll({{&JD
, SymbolNameSet({Bar
})}});
704 // Check that we can still resolve Foo (even though it has been failed).
705 EXPECT_THAT_ERROR(FooR
->notifyResolved({{Foo
, FooSym
}}), Failed())
706 << "Expected resolution for \"Foo\" to fail.";
708 FooR
->failMaterialization();
710 // Foo's query should have failed before we return from addDependencies.
711 EXPECT_TRUE(OnFooReadyRun
) << "Query for \"Foo\" was not run";
713 // Verify that subsequent lookups on Bar and Foo fail.
714 EXPECT_THAT_EXPECTED(ES
.lookup({&JD
}, {Bar
}), Failed())
715 << "Lookup on failed symbol should fail";
717 EXPECT_THAT_EXPECTED(ES
.lookup({&JD
}, {Foo
}), Failed())
718 << "Lookup on failed symbol should fail";
721 TEST_F(CoreAPIsStandardTest
, FailAfterMaterialization
) {
722 Optional
<MaterializationResponsibility
> FooR
;
723 Optional
<MaterializationResponsibility
> BarR
;
725 // Create a MaterializationUnit for each symbol that moves the
726 // MaterializationResponsibility into one of the locals above.
727 auto FooMU
= std::make_unique
<SimpleMaterializationUnit
>(
728 SymbolFlagsMap({{Foo
, FooSym
.getFlags()}}),
729 [&](MaterializationResponsibility R
) { FooR
.emplace(std::move(R
)); });
731 auto BarMU
= std::make_unique
<SimpleMaterializationUnit
>(
732 SymbolFlagsMap({{Bar
, BarSym
.getFlags()}}),
733 [&](MaterializationResponsibility R
) { BarR
.emplace(std::move(R
)); });
735 // Define the symbols.
736 cantFail(JD
.define(FooMU
));
737 cantFail(JD
.define(BarMU
));
739 bool OnFooReadyRun
= false;
740 auto OnFooReady
= [&](Expected
<SymbolMap
> Result
) {
741 EXPECT_THAT_EXPECTED(std::move(Result
), Failed());
742 OnFooReadyRun
= true;
745 ES
.lookup(JITDylibSearchList({{&JD
, false}}), {Foo
}, SymbolState::Ready
,
746 std::move(OnFooReady
), NoDependenciesToRegister
);
748 bool OnBarReadyRun
= false;
749 auto OnBarReady
= [&](Expected
<SymbolMap
> Result
) {
750 EXPECT_THAT_EXPECTED(std::move(Result
), Failed());
751 OnBarReadyRun
= true;
754 ES
.lookup(JITDylibSearchList({{&JD
, false}}), {Bar
}, SymbolState::Ready
,
755 std::move(OnBarReady
), NoDependenciesToRegister
);
757 // Add a dependency by Foo on Bar and vice-versa.
758 FooR
->addDependenciesForAll({{&JD
, SymbolNameSet({Bar
})}});
759 BarR
->addDependenciesForAll({{&JD
, SymbolNameSet({Foo
})}});
762 EXPECT_THAT_ERROR(FooR
->notifyResolved({{Foo
, FooSym
}}), Succeeded())
763 << "Expected resolution for \"Foo\" to succeed.";
764 EXPECT_THAT_ERROR(FooR
->notifyEmitted(), Succeeded())
765 << "Expected emission for \"Foo\" to succeed.";
768 BarR
->failMaterialization();
770 // Verify that both queries failed.
771 EXPECT_TRUE(OnFooReadyRun
) << "Query for Foo did not run";
772 EXPECT_TRUE(OnBarReadyRun
) << "Query for Bar did not run";
775 TEST_F(CoreAPIsStandardTest
, FailMaterializerWithUnqueriedSymbols
) {
776 // Make sure that symbols with no queries aganist them still
779 bool MaterializerRun
= false;
780 auto MU
= std::make_unique
<SimpleMaterializationUnit
>(
782 {{Foo
, JITSymbolFlags::Exported
}, {Bar
, JITSymbolFlags::Exported
}}),
783 [&](MaterializationResponsibility R
) {
784 MaterializerRun
= true;
785 R
.failMaterialization();
788 cantFail(JD
.define(std::move(MU
)));
790 // Issue a query for Foo, but not bar.
791 EXPECT_THAT_EXPECTED(ES
.lookup({&JD
}, {Foo
}), Failed())
792 << "Expected lookup to fail.";
794 // Check that the materializer (and therefore failMaterialization) ran.
795 EXPECT_TRUE(MaterializerRun
) << "Expected materializer to have run by now";
797 // Check that subsequent queries against both symbols fail.
798 EXPECT_THAT_EXPECTED(ES
.lookup({&JD
}, {Foo
}), Failed())
799 << "Expected lookup for Foo to fail.";
800 EXPECT_THAT_EXPECTED(ES
.lookup({&JD
}, {Bar
}), Failed())
801 << "Expected lookup for Bar to fail.";
804 TEST_F(CoreAPIsStandardTest
, DropMaterializerWhenEmpty
) {
805 bool DestructorRun
= false;
807 JITSymbolFlags
WeakExported(JITSymbolFlags::Exported
);
808 WeakExported
|= JITSymbolFlags::Weak
;
810 auto MU
= std::make_unique
<SimpleMaterializationUnit
>(
811 SymbolFlagsMap({{Foo
, WeakExported
}, {Bar
, WeakExported
}}),
812 [](MaterializationResponsibility R
) {
813 llvm_unreachable("Unexpected call to materialize");
815 [&](const JITDylib
&JD
, SymbolStringPtr Name
) {
816 EXPECT_TRUE(Name
== Foo
|| Name
== Bar
)
817 << "Discard of unexpected symbol?";
819 [&]() { DestructorRun
= true; });
821 cantFail(JD
.define(MU
));
823 cantFail(JD
.define(absoluteSymbols({{Foo
, FooSym
}})));
825 EXPECT_FALSE(DestructorRun
)
826 << "MaterializationUnit should not have been destroyed yet";
828 cantFail(JD
.define(absoluteSymbols({{Bar
, BarSym
}})));
830 EXPECT_TRUE(DestructorRun
)
831 << "MaterializationUnit should have been destroyed";
834 TEST_F(CoreAPIsStandardTest
, AddAndMaterializeLazySymbol
) {
835 bool FooMaterialized
= false;
836 bool BarDiscarded
= false;
838 JITSymbolFlags
WeakExported(JITSymbolFlags::Exported
);
839 WeakExported
|= JITSymbolFlags::Weak
;
841 auto MU
= std::make_unique
<SimpleMaterializationUnit
>(
842 SymbolFlagsMap({{Foo
, JITSymbolFlags::Exported
}, {Bar
, WeakExported
}}),
843 [&](MaterializationResponsibility R
) {
844 assert(BarDiscarded
&& "Bar should have been discarded by this point");
845 cantFail(R
.notifyResolved(SymbolMap({{Foo
, FooSym
}})));
846 cantFail(R
.notifyEmitted());
847 FooMaterialized
= true;
849 [&](const JITDylib
&JD
, SymbolStringPtr Name
) {
850 EXPECT_EQ(Name
, Bar
) << "Expected Name to be Bar";
854 cantFail(JD
.define(MU
));
855 cantFail(JD
.define(absoluteSymbols({{Bar
, BarSym
}})));
857 SymbolNameSet
Names({Foo
});
859 bool OnCompletionRun
= false;
861 auto OnCompletion
= [&](Expected
<SymbolMap
> Result
) {
862 EXPECT_TRUE(!!Result
) << "Resolution unexpectedly returned error";
863 auto I
= Result
->find(Foo
);
864 EXPECT_NE(I
, Result
->end()) << "Could not find symbol definition";
865 EXPECT_EQ(I
->second
.getAddress(), FooSym
.getAddress())
866 << "Resolution returned incorrect result";
867 OnCompletionRun
= true;
870 ES
.lookup(JITDylibSearchList({{&JD
, false}}), Names
, SymbolState::Ready
,
871 std::move(OnCompletion
), NoDependenciesToRegister
);
873 EXPECT_TRUE(FooMaterialized
) << "Foo was not materialized";
874 EXPECT_TRUE(BarDiscarded
) << "Bar was not discarded";
875 EXPECT_TRUE(OnCompletionRun
) << "OnResolutionCallback was not run";
878 TEST_F(CoreAPIsStandardTest
, TestBasicWeakSymbolMaterialization
) {
879 // Test that weak symbols are materialized correctly when we look them up.
880 BarSym
.setFlags(BarSym
.getFlags() | JITSymbolFlags::Weak
);
882 bool BarMaterialized
= false;
883 auto MU1
= std::make_unique
<SimpleMaterializationUnit
>(
884 SymbolFlagsMap({{Foo
, FooSym
.getFlags()}, {Bar
, BarSym
.getFlags()}}),
885 [&](MaterializationResponsibility R
) {
886 cantFail(R
.notifyResolved(SymbolMap({{Foo
, FooSym
}, {Bar
, BarSym
}})));
887 cantFail(R
.notifyEmitted());
888 BarMaterialized
= true;
891 bool DuplicateBarDiscarded
= false;
892 auto MU2
= std::make_unique
<SimpleMaterializationUnit
>(
893 SymbolFlagsMap({{Bar
, BarSym
.getFlags()}}),
894 [&](MaterializationResponsibility R
) {
895 ADD_FAILURE() << "Attempt to materialize Bar from the wrong unit";
896 R
.failMaterialization();
898 [&](const JITDylib
&JD
, SymbolStringPtr Name
) {
899 EXPECT_EQ(Name
, Bar
) << "Expected \"Bar\" to be discarded";
900 DuplicateBarDiscarded
= true;
903 cantFail(JD
.define(MU1
));
904 cantFail(JD
.define(MU2
));
906 bool OnCompletionRun
= false;
908 auto OnCompletion
= [&](Expected
<SymbolMap
> Result
) {
909 cantFail(std::move(Result
));
910 OnCompletionRun
= true;
913 ES
.lookup(JITDylibSearchList({{&JD
, false}}), {Bar
}, SymbolState::Ready
,
914 std::move(OnCompletion
), NoDependenciesToRegister
);
916 EXPECT_TRUE(OnCompletionRun
) << "OnCompletion not run";
917 EXPECT_TRUE(BarMaterialized
) << "Bar was not materialized at all";
918 EXPECT_TRUE(DuplicateBarDiscarded
)
919 << "Duplicate bar definition not discarded";
922 TEST_F(CoreAPIsStandardTest
, DefineMaterializingSymbol
) {
923 bool ExpectNoMoreMaterialization
= false;
924 ES
.setDispatchMaterialization(
925 [&](JITDylib
&JD
, std::unique_ptr
<MaterializationUnit
> MU
) {
926 if (ExpectNoMoreMaterialization
)
927 ADD_FAILURE() << "Unexpected materialization";
928 MU
->doMaterialize(JD
);
931 auto MU
= std::make_unique
<SimpleMaterializationUnit
>(
932 SymbolFlagsMap({{Foo
, FooSym
.getFlags()}}),
933 [&](MaterializationResponsibility R
) {
935 R
.defineMaterializing(SymbolFlagsMap({{Bar
, BarSym
.getFlags()}})));
936 cantFail(R
.notifyResolved(SymbolMap({{Foo
, FooSym
}, {Bar
, BarSym
}})));
937 cantFail(R
.notifyEmitted());
940 cantFail(JD
.define(MU
));
941 cantFail(ES
.lookup(JITDylibSearchList({{&JD
, false}}), Foo
));
943 // Assert that materialization is complete by now.
944 ExpectNoMoreMaterialization
= true;
946 // Look up bar to verify that no further materialization happens.
947 auto BarResult
= cantFail(ES
.lookup(JITDylibSearchList({{&JD
, false}}), Bar
));
948 EXPECT_EQ(BarResult
.getAddress(), BarSym
.getAddress())
949 << "Expected Bar == BarSym";
952 TEST_F(CoreAPIsStandardTest
, GeneratorTest
) {
953 cantFail(JD
.define(absoluteSymbols({{Foo
, FooSym
}})));
955 class TestGenerator
: public JITDylib::DefinitionGenerator
{
957 TestGenerator(SymbolMap Symbols
) : Symbols(std::move(Symbols
)) {}
958 Expected
<SymbolNameSet
> tryToGenerate(JITDylib
&JD
,
959 const SymbolNameSet
&Names
) {
961 SymbolNameSet NewNames
;
963 for (auto &Name
: Names
) {
964 if (Symbols
.count(Name
)) {
965 NewDefs
[Name
] = Symbols
[Name
];
966 NewNames
.insert(Name
);
969 cantFail(JD
.define(absoluteSymbols(std::move(NewDefs
))));
977 JD
.addGenerator(std::make_unique
<TestGenerator
>(SymbolMap({{Bar
, BarSym
}})));
980 cantFail(ES
.lookup(JITDylibSearchList({{&JD
, false}}), {Foo
, Bar
}));
982 EXPECT_EQ(Result
.count(Bar
), 1U) << "Expected to find fallback def for 'bar'";
983 EXPECT_EQ(Result
[Bar
].getAddress(), BarSym
.getAddress())
984 << "Expected fallback def for Bar to be equal to BarSym";
987 TEST_F(CoreAPIsStandardTest
, FailResolution
) {
988 auto MU
= std::make_unique
<SimpleMaterializationUnit
>(
989 SymbolFlagsMap({{Foo
, JITSymbolFlags::Exported
| JITSymbolFlags::Weak
},
990 {Bar
, JITSymbolFlags::Exported
| JITSymbolFlags::Weak
}}),
991 [&](MaterializationResponsibility R
) {
992 R
.failMaterialization();
995 cantFail(JD
.define(MU
));
997 SymbolNameSet
Names({Foo
, Bar
});
998 auto Result
= ES
.lookup(JITDylibSearchList({{&JD
, false}}), Names
);
1000 EXPECT_FALSE(!!Result
) << "Expected failure";
1004 [&](FailedToMaterialize
&F
) {
1005 EXPECT_TRUE(F
.getSymbols().count(&JD
))
1006 << "Expected to fail on JITDylib JD";
1007 EXPECT_EQ(F
.getSymbols().find(&JD
)->second
, Names
)
1008 << "Expected to fail on symbols in Names";
1010 [](ErrorInfoBase
&EIB
) {
1013 raw_string_ostream
ErrOut(ErrMsg
);
1016 ADD_FAILURE() << "Expected a FailedToResolve error. Got:\n" << ErrMsg
;
1021 TEST_F(CoreAPIsStandardTest
, FailEmissionAfterResolution
) {
1023 cantFail(JD
.define(absoluteSymbols({{Baz
, BazSym
}})));
1025 auto MU
= std::make_unique
<SimpleMaterializationUnit
>(
1026 SymbolFlagsMap({{Foo
, FooSym
.getFlags()}, {Bar
, BarSym
.getFlags()}}),
1027 [&](MaterializationResponsibility R
) {
1028 cantFail(R
.notifyResolved(SymbolMap({{Foo
, FooSym
}, {Bar
, BarSym
}})));
1031 JITDylibSearchList({{&JD
, false}}), SymbolNameSet({Baz
}),
1032 SymbolState::Resolved
,
1033 [&R
](Expected
<SymbolMap
> Result
) {
1034 // Called when "baz" is resolved. We don't actually depend
1035 // on or care about baz, but use it to trigger failure of
1036 // this materialization before Baz has been finalized in
1037 // order to test that error propagation is correct in this
1039 cantFail(std::move(Result
));
1040 R
.failMaterialization();
1042 [&](const SymbolDependenceMap
&Deps
) {
1043 R
.addDependenciesForAll(Deps
);
1047 cantFail(JD
.define(MU
));
1049 SymbolNameSet
Names({Foo
, Bar
});
1050 auto Result
= ES
.lookup(JITDylibSearchList({{&JD
, false}}), Names
);
1052 EXPECT_THAT_EXPECTED(std::move(Result
), Failed())
1053 << "Unexpected success while trying to test error propagation";
1056 TEST_F(CoreAPIsStandardTest
, FailAfterPartialResolution
) {
1058 cantFail(JD
.define(absoluteSymbols({{Foo
, FooSym
}})));
1060 // Fail materialization of bar.
1061 auto BarMU
= std::make_unique
<SimpleMaterializationUnit
>(
1062 SymbolFlagsMap({{Bar
, BarSym
.getFlags()}}),
1063 [&](MaterializationResponsibility R
) { R
.failMaterialization(); });
1065 cantFail(JD
.define(std::move(BarMU
)));
1067 bool QueryHandlerRun
= false;
1069 JITDylibSearchList({{&JD
, false}}), SymbolNameSet({Foo
, Bar
}),
1070 SymbolState::Resolved
,
1071 [&](Expected
<SymbolMap
> Result
) {
1072 EXPECT_THAT_EXPECTED(std::move(Result
), Failed())
1073 << "Expected query to fail";
1074 QueryHandlerRun
= true;
1076 NoDependenciesToRegister
);
1077 EXPECT_TRUE(QueryHandlerRun
) << "Query handler never ran";
1080 TEST_F(CoreAPIsStandardTest
, TestLookupWithUnthreadedMaterialization
) {
1081 auto MU
= std::make_unique
<SimpleMaterializationUnit
>(
1082 SymbolFlagsMap({{Foo
, JITSymbolFlags::Exported
}}),
1083 [&](MaterializationResponsibility R
) {
1084 cantFail(R
.notifyResolved({{Foo
, FooSym
}}));
1085 cantFail(R
.notifyEmitted());
1088 cantFail(JD
.define(MU
));
1090 auto FooLookupResult
=
1091 cantFail(ES
.lookup(JITDylibSearchList({{&JD
, false}}), Foo
));
1093 EXPECT_EQ(FooLookupResult
.getAddress(), FooSym
.getAddress())
1094 << "lookup returned an incorrect address";
1095 EXPECT_EQ(FooLookupResult
.getFlags(), FooSym
.getFlags())
1096 << "lookup returned incorrect flags";
1099 TEST_F(CoreAPIsStandardTest
, TestLookupWithThreadedMaterialization
) {
1100 #if LLVM_ENABLE_THREADS
1102 std::thread MaterializationThread
;
1103 ES
.setDispatchMaterialization(
1104 [&](JITDylib
&JD
, std::unique_ptr
<MaterializationUnit
> MU
) {
1105 MaterializationThread
=
1106 std::thread([MU
= std::move(MU
), &JD
] { MU
->doMaterialize(JD
); });
1109 cantFail(JD
.define(absoluteSymbols({{Foo
, FooSym
}})));
1111 auto FooLookupResult
=
1112 cantFail(ES
.lookup(JITDylibSearchList({{&JD
, false}}), Foo
));
1114 EXPECT_EQ(FooLookupResult
.getAddress(), FooSym
.getAddress())
1115 << "lookup returned an incorrect address";
1116 EXPECT_EQ(FooLookupResult
.getFlags(), FooSym
.getFlags())
1117 << "lookup returned incorrect flags";
1118 MaterializationThread
.join();
1122 TEST_F(CoreAPIsStandardTest
, TestGetRequestedSymbolsAndReplace
) {
1123 // Test that GetRequestedSymbols returns the set of symbols that currently
1124 // have pending queries, and test that MaterializationResponsibility's
1125 // replace method can be used to return definitions to the JITDylib in a new
1126 // MaterializationUnit.
1127 SymbolNameSet
Names({Foo
, Bar
});
1129 bool FooMaterialized
= false;
1130 bool BarMaterialized
= false;
1132 auto MU
= std::make_unique
<SimpleMaterializationUnit
>(
1133 SymbolFlagsMap({{Foo
, FooSym
.getFlags()}, {Bar
, BarSym
.getFlags()}}),
1134 [&](MaterializationResponsibility R
) {
1135 auto Requested
= R
.getRequestedSymbols();
1136 EXPECT_EQ(Requested
.size(), 1U) << "Expected one symbol requested";
1137 EXPECT_EQ(*Requested
.begin(), Foo
) << "Expected \"Foo\" requested";
1139 auto NewMU
= std::make_unique
<SimpleMaterializationUnit
>(
1140 SymbolFlagsMap({{Bar
, BarSym
.getFlags()}}),
1141 [&](MaterializationResponsibility R2
) {
1142 cantFail(R2
.notifyResolved(SymbolMap({{Bar
, BarSym
}})));
1143 cantFail(R2
.notifyEmitted());
1144 BarMaterialized
= true;
1147 R
.replace(std::move(NewMU
));
1149 cantFail(R
.notifyResolved(SymbolMap({{Foo
, FooSym
}})));
1150 cantFail(R
.notifyEmitted());
1152 FooMaterialized
= true;
1155 cantFail(JD
.define(MU
));
1157 EXPECT_FALSE(FooMaterialized
) << "Foo should not be materialized yet";
1158 EXPECT_FALSE(BarMaterialized
) << "Bar should not be materialized yet";
1161 cantFail(ES
.lookup(JITDylibSearchList({{&JD
, false}}), Foo
));
1162 EXPECT_EQ(FooSymResult
.getAddress(), FooSym
.getAddress())
1163 << "Address mismatch for Foo";
1165 EXPECT_TRUE(FooMaterialized
) << "Foo should be materialized now";
1166 EXPECT_FALSE(BarMaterialized
) << "Bar still should not be materialized";
1169 cantFail(ES
.lookup(JITDylibSearchList({{&JD
, false}}), Bar
));
1170 EXPECT_EQ(BarSymResult
.getAddress(), BarSym
.getAddress())
1171 << "Address mismatch for Bar";
1172 EXPECT_TRUE(BarMaterialized
) << "Bar should be materialized now";
1175 TEST_F(CoreAPIsStandardTest
, TestMaterializationResponsibilityDelegation
) {
1176 auto MU
= std::make_unique
<SimpleMaterializationUnit
>(
1177 SymbolFlagsMap({{Foo
, FooSym
.getFlags()}, {Bar
, BarSym
.getFlags()}}),
1178 [&](MaterializationResponsibility R
) {
1179 auto R2
= R
.delegate({Bar
});
1181 cantFail(R
.notifyResolved({{Foo
, FooSym
}}));
1182 cantFail(R
.notifyEmitted());
1183 cantFail(R2
.notifyResolved({{Bar
, BarSym
}}));
1184 cantFail(R2
.notifyEmitted());
1187 cantFail(JD
.define(MU
));
1189 auto Result
= ES
.lookup(JITDylibSearchList({{&JD
, false}}), {Foo
, Bar
});
1191 EXPECT_TRUE(!!Result
) << "Result should be a success value";
1192 EXPECT_EQ(Result
->count(Foo
), 1U) << "\"Foo\" entry missing";
1193 EXPECT_EQ(Result
->count(Bar
), 1U) << "\"Bar\" entry missing";
1194 EXPECT_EQ((*Result
)[Foo
].getAddress(), FooSym
.getAddress())
1195 << "Address mismatch for \"Foo\"";
1196 EXPECT_EQ((*Result
)[Bar
].getAddress(), BarSym
.getAddress())
1197 << "Address mismatch for \"Bar\"";
1200 TEST_F(CoreAPIsStandardTest
, TestMaterializeWeakSymbol
) {
1201 // Confirm that once a weak definition is selected for materialization it is
1202 // treated as strong.
1203 JITSymbolFlags WeakExported
= JITSymbolFlags::Exported
;
1204 WeakExported
&= JITSymbolFlags::Weak
;
1206 std::unique_ptr
<MaterializationResponsibility
> FooResponsibility
;
1207 auto MU
= std::make_unique
<SimpleMaterializationUnit
>(
1208 SymbolFlagsMap({{Foo
, FooSym
.getFlags()}}),
1209 [&](MaterializationResponsibility R
) {
1211 std::make_unique
<MaterializationResponsibility
>(std::move(R
));
1214 cantFail(JD
.define(MU
));
1215 auto OnCompletion
= [](Expected
<SymbolMap
> Result
) {
1216 cantFail(std::move(Result
));
1219 ES
.lookup(JITDylibSearchList({{&JD
, false}}), {Foo
}, SymbolState::Ready
,
1220 std::move(OnCompletion
), NoDependenciesToRegister
);
1222 auto MU2
= std::make_unique
<SimpleMaterializationUnit
>(
1223 SymbolFlagsMap({{Foo
, JITSymbolFlags::Exported
}}),
1224 [](MaterializationResponsibility R
) {
1225 llvm_unreachable("This unit should never be materialized");
1228 auto Err
= JD
.define(MU2
);
1229 EXPECT_TRUE(!!Err
) << "Expected failure value";
1230 EXPECT_TRUE(Err
.isA
<DuplicateDefinition
>())
1231 << "Expected a duplicate definition error";
1232 consumeError(std::move(Err
));
1234 // No dependencies registered, can't fail:
1235 cantFail(FooResponsibility
->notifyResolved(SymbolMap({{Foo
, FooSym
}})));
1236 cantFail(FooResponsibility
->notifyEmitted());