1899 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "OrcTestCommon.h"10#include "llvm/ADT/ScopeExit.h"11#include "llvm/Config/llvm-config.h"12#include "llvm/ExecutionEngine/Orc/AbsoluteSymbols.h"13#include "llvm/ExecutionEngine/Orc/Core.h"14#include "llvm/ExecutionEngine/Orc/Shared/OrcError.h"15#include "llvm/Testing/Support/Error.h"16 17#include <deque>18#include <thread>19 20using namespace llvm;21using namespace llvm::orc;22 23class CoreAPIsStandardTest : public CoreAPIsBasedStandardTest {};24 25namespace {26 27class CustomError : public ErrorInfo<CustomError> {28public:29 static char ID;30 void log(raw_ostream &OS) const override { OS << "CustomError"; }31 std::error_code convertToErrorCode() const override { return {}; }32};33char CustomError::ID = 0;34 35TEST_F(CoreAPIsStandardTest, ErrorReporter) {36 // Check that errors reported via ExecutionSession::reportError are sent to37 // the registered error reporter, and that the error reporter can hold38 // uniquely owned state.39 40 Error ReportedError = Error::success();41 42 ES.setErrorReporter(43 // Make sure error reporter can capture uniquely-owned state.44 [&, State = std::make_unique<int>(42)](Error Err) {45 ReportedError = joinErrors(std::move(Err), std::move(ReportedError));46 });47 48 ES.reportError(make_error<CustomError>());49 50 EXPECT_THAT_ERROR(std::move(ReportedError), Failed<CustomError>());51}52 53TEST_F(CoreAPIsStandardTest, JITDylibAddToLinkOrder) {54 // Check that the JITDylib::addToLinkOrder methods behave as expected.55 auto &JD2 = ES.createBareJITDylib("JD2");56 auto &JD3 = ES.createBareJITDylib("JD3");57 58 JD.addToLinkOrder(JD2);59 JD.withLinkOrderDo([&](const JITDylibSearchOrder &SO) {60 EXPECT_EQ(SO.size(), 2U);61 EXPECT_EQ(SO[0].first, &JD);62 EXPECT_EQ(SO[1].first, &JD2);63 });64 65 JD.addToLinkOrder(makeJITDylibSearchOrder({&JD2, &JD3}));66 JD.withLinkOrderDo([&](const JITDylibSearchOrder &SO) {67 // JD2 was already in the search order, so we expect just one extra item68 // here.69 EXPECT_EQ(SO.size(), 3U);70 EXPECT_EQ(SO[0].first, &JD);71 EXPECT_EQ(SO[1].first, &JD2);72 EXPECT_EQ(SO[2].first, &JD3);73 });74}75 76TEST_F(CoreAPIsStandardTest, BasicSuccessfulLookup) {77 bool OnCompletionRun = false;78 79 auto OnCompletion = [&](Expected<SymbolMap> Result) {80 EXPECT_TRUE(!!Result) << "Resolution unexpectedly returned error";81 auto &Resolved = *Result;82 auto I = Resolved.find(Foo);83 EXPECT_NE(I, Resolved.end()) << "Could not find symbol definition";84 EXPECT_EQ(I->second.getAddress(), FooAddr)85 << "Resolution returned incorrect result";86 OnCompletionRun = true;87 };88 89 std::unique_ptr<MaterializationResponsibility> FooMR;90 91 cantFail(JD.define(std::make_unique<SimpleMaterializationUnit>(92 SymbolFlagsMap({{Foo, FooSym.getFlags()}}),93 [&](std::unique_ptr<MaterializationResponsibility> R) {94 FooMR = std::move(R);95 })));96 97 ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),98 SymbolLookupSet(Foo), SymbolState::Ready, OnCompletion,99 NoDependenciesToRegister);100 101 EXPECT_FALSE(OnCompletionRun) << "Should not have been resolved yet";102 103 cantFail(FooMR->notifyResolved({{Foo, FooSym}}));104 105 EXPECT_FALSE(OnCompletionRun) << "Should not be ready yet";106 107 cantFail(FooMR->notifyEmitted({}));108 109 EXPECT_TRUE(OnCompletionRun) << "Should have been marked ready";110}111 112TEST_F(CoreAPIsStandardTest, EmptyLookup) {113 bool OnCompletionRun = false;114 115 auto OnCompletion = [&](Expected<SymbolMap> Result) {116 cantFail(std::move(Result));117 OnCompletionRun = true;118 };119 120 ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD), SymbolLookupSet(),121 SymbolState::Ready, OnCompletion, NoDependenciesToRegister);122 123 EXPECT_TRUE(OnCompletionRun) << "OnCompletion was not run for empty query";124}125 126TEST_F(CoreAPIsStandardTest, ResolveUnrequestedSymbol) {127 // Test that all symbols in a MaterializationUnit materialize corretly when128 // only a subset of symbols is looked up.129 // The aim here is to ensure that we're not relying on the query to set up130 // state needed to materialize the unrequested symbols.131 132 cantFail(JD.define(std::make_unique<SimpleMaterializationUnit>(133 SymbolFlagsMap({{Foo, FooSym.getFlags()}, {Bar, BarSym.getFlags()}}),134 [this](std::unique_ptr<MaterializationResponsibility> R) {135 cantFail(R->notifyResolved({{Foo, FooSym}, {Bar, BarSym}}));136 cantFail(R->notifyEmitted({}));137 })));138 139 auto Result =140 cantFail(ES.lookup(makeJITDylibSearchOrder(&JD), SymbolLookupSet({Foo})));141 EXPECT_EQ(Result.size(), 1U) << "Unexpected number of results";142 EXPECT_TRUE(Result.count(Foo)) << "Expected result for \"Foo\"";143}144 145TEST_F(CoreAPIsStandardTest, MaterializationSideEffctsOnlyBasic) {146 // Test that basic materialization-side-effects-only symbols work as expected:147 // that they can be emitted without being resolved, that queries for them148 // don't return until they're emitted, and that they don't appear in query149 // results.150 151 std::unique_ptr<MaterializationResponsibility> FooR;152 std::optional<SymbolMap> Result;153 154 cantFail(JD.define(std::make_unique<SimpleMaterializationUnit>(155 SymbolFlagsMap(156 {{Foo, JITSymbolFlags::Exported |157 JITSymbolFlags::MaterializationSideEffectsOnly}}),158 [&](std::unique_ptr<MaterializationResponsibility> R) {159 FooR = std::move(R);160 })));161 162 ES.lookup(163 LookupKind::Static, makeJITDylibSearchOrder(&JD),164 SymbolLookupSet(Foo, SymbolLookupFlags::WeaklyReferencedSymbol),165 SymbolState::Ready,166 [&](Expected<SymbolMap> LookupResult) {167 if (LookupResult)168 Result = std::move(*LookupResult);169 else170 ADD_FAILURE() << "Unexpected lookup error: "171 << toString(LookupResult.takeError());172 },173 NoDependenciesToRegister);174 175 EXPECT_FALSE(Result) << "Lookup returned unexpectedly";176 EXPECT_TRUE(FooR) << "Lookup failed to trigger materialization";177 EXPECT_THAT_ERROR(FooR->notifyEmitted({}), Succeeded())178 << "Emission of materialization-side-effects-only symbol failed";179 180 EXPECT_TRUE(Result) << "Lookup failed to return";181 EXPECT_TRUE(Result->empty()) << "Lookup result contained unexpected value";182}183 184TEST_F(CoreAPIsStandardTest, MaterializationSideEffectsOnlyFailuresPersist) {185 // Test that when a MaterializationSideEffectsOnly symbol is failed it186 // remains in the failure state rather than vanishing.187 188 cantFail(JD.define(std::make_unique<SimpleMaterializationUnit>(189 SymbolFlagsMap(190 {{Foo, JITSymbolFlags::Exported |191 JITSymbolFlags::MaterializationSideEffectsOnly}}),192 [&](std::unique_ptr<MaterializationResponsibility> R) {193 R->failMaterialization();194 })));195 196 EXPECT_THAT_EXPECTED(197 ES.lookup(makeJITDylibSearchOrder(&JD), SymbolLookupSet({Foo})),198 Failed());199 EXPECT_THAT_EXPECTED(200 ES.lookup(makeJITDylibSearchOrder(&JD), SymbolLookupSet({Foo})),201 Failed());202}203 204TEST_F(CoreAPIsStandardTest, RemoveSymbolsTest) {205 // Test that:206 // (1) Missing symbols generate a SymbolsNotFound error.207 // (2) Materializing symbols generate a SymbolCouldNotBeRemoved error.208 // (3) Removal of unmaterialized symbols triggers discard on the209 // materialization unit.210 // (4) Removal of symbols destroys empty materialization units.211 // (5) Removal of materialized symbols works.212 213 // Foo will be fully materialized.214 cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));215 216 // Bar will be unmaterialized.217 bool BarDiscarded = false;218 bool BarMaterializerDestructed = false;219 cantFail(JD.define(std::make_unique<SimpleMaterializationUnit>(220 SymbolFlagsMap({{Bar, BarSym.getFlags()}}),221 [this](std::unique_ptr<MaterializationResponsibility> R) {222 ADD_FAILURE() << "Unexpected materialization of \"Bar\"";223 cantFail(R->notifyResolved({{Bar, BarSym}}));224 cantFail(R->notifyEmitted({}));225 },226 nullptr,227 [&](const JITDylib &JD, const SymbolStringPtr &Name) {228 EXPECT_EQ(Name, Bar) << "Expected \"Bar\" to be discarded";229 if (Name == Bar)230 BarDiscarded = true;231 },232 [&]() { BarMaterializerDestructed = true; })));233 234 // Baz will be in the materializing state initially, then235 // materialized for the final removal attempt.236 std::unique_ptr<MaterializationResponsibility> BazR;237 cantFail(JD.define(std::make_unique<SimpleMaterializationUnit>(238 SymbolFlagsMap({{Baz, BazSym.getFlags()}}),239 [&](std::unique_ptr<MaterializationResponsibility> R) {240 BazR = std::move(R);241 },242 nullptr,243 [](const JITDylib &JD, const SymbolStringPtr &Name) {244 ADD_FAILURE() << "\"Baz\" discarded unexpectedly";245 })));246 247 bool OnCompletionRun = false;248 ES.lookup(249 LookupKind::Static, makeJITDylibSearchOrder(&JD),250 SymbolLookupSet({Foo, Baz}), SymbolState::Ready,251 [&](Expected<SymbolMap> Result) {252 cantFail(Result.takeError());253 OnCompletionRun = true;254 },255 NoDependenciesToRegister);256 257 {258 // Attempt 1: Search for a missing symbol, Qux.259 auto Err = JD.remove({Foo, Bar, Baz, Qux});260 EXPECT_TRUE(!!Err) << "Expected failure";261 EXPECT_TRUE(Err.isA<SymbolsNotFound>())262 << "Expected a SymbolsNotFound error";263 consumeError(std::move(Err));264 }265 266 {267 // Attempt 2: Search for a symbol that is still materializing, Baz.268 auto Err = JD.remove({Foo, Bar, Baz});269 EXPECT_TRUE(!!Err) << "Expected failure";270 EXPECT_TRUE(Err.isA<SymbolsCouldNotBeRemoved>())271 << "Expected a SymbolsNotFound error";272 consumeError(std::move(Err));273 }274 275 cantFail(BazR->notifyResolved({{Baz, BazSym}}));276 cantFail(BazR->notifyEmitted({}));277 {278 // Attempt 3: Search now that all symbols are fully materialized279 // (Foo, Baz), or not yet materialized (Bar).280 auto Err = JD.remove({Foo, Bar, Baz});281 EXPECT_FALSE(!!Err) << "Expected success";282 }283 284 EXPECT_TRUE(BarDiscarded) << "\"Bar\" should have been discarded";285 EXPECT_TRUE(BarMaterializerDestructed)286 << "\"Bar\"'s materializer should have been destructed";287 EXPECT_TRUE(OnCompletionRun) << "OnCompletion should have been run";288}289 290TEST_F(CoreAPIsStandardTest, DiscardInitSymbol) {291 SymbolStringPtr ForwardedDiscardSym = nullptr;292 293 auto MU = std::make_unique<SimpleMaterializationUnit>(294 SymbolFlagsMap({{Foo, FooSym.getFlags()}, {Bar, BarSym.getFlags()}}),295 [](std::unique_ptr<MaterializationResponsibility> R) {296 llvm_unreachable("Materialize called unexpectedly?");297 },298 Foo,299 [&](const JITDylib &, SymbolStringPtr Sym) {300 ForwardedDiscardSym = std::move(Sym);301 });302 303 MU->doDiscard(JD, Foo);304 305 EXPECT_EQ(ForwardedDiscardSym, Foo);306 EXPECT_EQ(MU->getSymbols().size(), 1U);307 EXPECT_TRUE(MU->getSymbols().count(Bar));308 EXPECT_EQ(MU->getInitializerSymbol(), nullptr);309}310 311TEST_F(CoreAPIsStandardTest, LookupWithHiddenSymbols) {312 auto BarHiddenFlags = BarSym.getFlags() & ~JITSymbolFlags::Exported;313 auto BarHiddenSym = ExecutorSymbolDef(BarSym.getAddress(), BarHiddenFlags);314 315 cantFail(JD.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarHiddenSym}})));316 317 auto &JD2 = ES.createBareJITDylib("JD2");318 cantFail(JD2.define(absoluteSymbols({{Bar, QuxSym}})));319 320 /// Try a blocking lookup.321 auto Result = cantFail(ES.lookup(makeJITDylibSearchOrder({&JD, &JD2}),322 SymbolLookupSet({Foo, Bar})));323 324 EXPECT_EQ(Result.size(), 2U) << "Unexpected number of results";325 EXPECT_EQ(Result.count(Foo), 1U) << "Missing result for \"Foo\"";326 EXPECT_EQ(Result.count(Bar), 1U) << "Missing result for \"Bar\"";327 EXPECT_EQ(Result[Bar].getAddress(), QuxSym.getAddress())328 << "Wrong result for \"Bar\"";329}330 331TEST_F(CoreAPIsStandardTest, LookupFlagsTest) {332 // Test that lookupFlags works on a predefined symbol, and does not trigger333 // materialization of a lazy symbol. Make the lazy symbol weak to test that334 // the weak flag is propagated correctly.335 336 BarSym.setFlags(static_cast<JITSymbolFlags::FlagNames>(337 JITSymbolFlags::Exported | JITSymbolFlags::Weak));338 auto MU = std::make_unique<SimpleMaterializationUnit>(339 SymbolFlagsMap({{Bar, BarSym.getFlags()}}),340 [](std::unique_ptr<MaterializationResponsibility> R) {341 llvm_unreachable("Symbol materialized on flags lookup");342 });343 344 cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));345 cantFail(JD.define(std::move(MU)));346 347 auto SymbolFlags = cantFail(ES.lookupFlags(348 LookupKind::Static,349 {{&JD, JITDylibLookupFlags::MatchExportedSymbolsOnly}},350 SymbolLookupSet({Foo, Bar, Baz},351 SymbolLookupFlags::WeaklyReferencedSymbol)));352 353 EXPECT_EQ(SymbolFlags.size(), 2U)354 << "Returned symbol flags contains unexpected results";355 EXPECT_EQ(SymbolFlags.count(Foo), 1U) << "Missing lookupFlags result for Foo";356 EXPECT_EQ(SymbolFlags[Foo], FooSym.getFlags())357 << "Incorrect flags returned for Foo";358 EXPECT_EQ(SymbolFlags.count(Bar), 1U)359 << "Missing lookupFlags result for Bar";360 EXPECT_EQ(SymbolFlags[Bar], BarSym.getFlags())361 << "Incorrect flags returned for Bar";362}363 364TEST_F(CoreAPIsStandardTest, LookupWithGeneratorFailure) {365 366 class BadGenerator : public DefinitionGenerator {367 public:368 Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &,369 JITDylibLookupFlags, const SymbolLookupSet &) override {370 return make_error<StringError>("BadGenerator", inconvertibleErrorCode());371 }372 };373 374 JD.addGenerator(std::make_unique<BadGenerator>());375 376 EXPECT_THAT_ERROR(377 ES.lookupFlags(LookupKind::Static,378 {{&JD, JITDylibLookupFlags::MatchExportedSymbolsOnly}},379 SymbolLookupSet(Foo))380 .takeError(),381 Failed<StringError>())382 << "Generator failure did not propagate through lookupFlags";383 384 EXPECT_THAT_ERROR(385 ES.lookup(makeJITDylibSearchOrder(&JD), SymbolLookupSet(Foo)).takeError(),386 Failed<StringError>())387 << "Generator failure did not propagate through lookup";388}389 390TEST_F(CoreAPIsStandardTest, TestBasicAliases) {391 cantFail(JD.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarSym}})));392 cantFail(JD.define(symbolAliases({{Baz, {Foo, JITSymbolFlags::Exported}},393 {Qux, {Bar, JITSymbolFlags::Weak}}})));394 cantFail(JD.define(absoluteSymbols({{Qux, QuxSym}})));395 396 auto Result =397 ES.lookup(makeJITDylibSearchOrder(&JD), SymbolLookupSet({Baz, Qux}));398 EXPECT_TRUE(!!Result) << "Unexpected lookup failure";399 EXPECT_EQ(Result->count(Baz), 1U) << "No result for \"baz\"";400 EXPECT_EQ(Result->count(Qux), 1U) << "No result for \"qux\"";401 EXPECT_EQ((*Result)[Baz].getAddress(), FooSym.getAddress())402 << "\"Baz\"'s address should match \"Foo\"'s";403 EXPECT_EQ((*Result)[Qux].getAddress(), QuxSym.getAddress())404 << "The \"Qux\" alias should have been overriden";405}406 407TEST_F(CoreAPIsStandardTest, TestChainedAliases) {408 cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));409 cantFail(JD.define(symbolAliases(410 {{Baz, {Bar, BazSym.getFlags()}}, {Bar, {Foo, BarSym.getFlags()}}})));411 412 auto Result =413 ES.lookup(makeJITDylibSearchOrder(&JD), SymbolLookupSet({Bar, Baz}));414 EXPECT_TRUE(!!Result) << "Unexpected lookup failure";415 EXPECT_EQ(Result->count(Bar), 1U) << "No result for \"bar\"";416 EXPECT_EQ(Result->count(Baz), 1U) << "No result for \"baz\"";417 EXPECT_EQ((*Result)[Bar].getAddress(), FooSym.getAddress())418 << "\"Bar\"'s address should match \"Foo\"'s";419 EXPECT_EQ((*Result)[Baz].getAddress(), FooSym.getAddress())420 << "\"Baz\"'s address should match \"Foo\"'s";421}422 423TEST_F(CoreAPIsStandardTest, TestBasicReExports) {424 // Test that the basic use case of re-exporting a single symbol from another425 // JITDylib works.426 cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));427 428 auto &JD2 = ES.createBareJITDylib("JD2");429 430 cantFail(JD2.define(reexports(JD, {{Bar, {Foo, BarSym.getFlags()}}})));431 432 auto Result = cantFail(ES.lookup(makeJITDylibSearchOrder(&JD2), Bar));433 EXPECT_EQ(Result.getAddress(), FooSym.getAddress())434 << "Re-export Bar for symbol Foo should match FooSym's address";435}436 437TEST_F(CoreAPIsStandardTest, TestThatReExportsDontUnnecessarilyMaterialize) {438 // Test that re-exports do not materialize symbols that have not been queried439 // for.440 cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));441 442 bool BarMaterialized = false;443 auto BarMU = std::make_unique<SimpleMaterializationUnit>(444 SymbolFlagsMap({{Bar, BarSym.getFlags()}}),445 [&](std::unique_ptr<MaterializationResponsibility> R) {446 BarMaterialized = true;447 cantFail(R->notifyResolved({{Bar, BarSym}}));448 cantFail(R->notifyEmitted({}));449 });450 451 cantFail(JD.define(BarMU));452 453 auto &JD2 = ES.createBareJITDylib("JD2");454 455 cantFail(JD2.define(reexports(456 JD, {{Baz, {Foo, BazSym.getFlags()}}, {Qux, {Bar, QuxSym.getFlags()}}})));457 458 auto Result = cantFail(ES.lookup(makeJITDylibSearchOrder(&JD2), Baz));459 EXPECT_EQ(Result.getAddress(), FooSym.getAddress())460 << "Re-export Baz for symbol Foo should match FooSym's address";461 462 EXPECT_FALSE(BarMaterialized) << "Bar should not have been materialized";463}464 465TEST_F(CoreAPIsStandardTest, TestReexportsGenerator) {466 // Test that a re-exports generator can dynamically generate reexports.467 468 auto &JD2 = ES.createBareJITDylib("JD2");469 cantFail(JD2.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarSym}})));470 471 auto Filter = [this](SymbolStringPtr Name) { return Name != Bar; };472 473 JD.addGenerator(std::make_unique<ReexportsGenerator>(474 JD2, JITDylibLookupFlags::MatchExportedSymbolsOnly, Filter));475 476 auto Flags = cantFail(ES.lookupFlags(477 LookupKind::Static,478 {{&JD, JITDylibLookupFlags::MatchExportedSymbolsOnly}},479 SymbolLookupSet({Foo, Bar, Baz},480 SymbolLookupFlags::WeaklyReferencedSymbol)));481 EXPECT_EQ(Flags.size(), 1U) << "Unexpected number of results";482 EXPECT_EQ(Flags[Foo], FooSym.getFlags()) << "Unexpected flags for Foo";483 484 auto Result = cantFail(ES.lookup(makeJITDylibSearchOrder(&JD), Foo));485 486 EXPECT_EQ(Result.getAddress(), FooSym.getAddress())487 << "Incorrect reexported symbol address";488}489 490TEST_F(CoreAPIsStandardTest, TestTrivialCircularDependency) {491 std::unique_ptr<MaterializationResponsibility> FooR;492 auto FooMU = std::make_unique<SimpleMaterializationUnit>(493 SymbolFlagsMap({{Foo, FooSym.getFlags()}}),494 [&](std::unique_ptr<MaterializationResponsibility> R) {495 FooR = std::move(R);496 });497 498 cantFail(JD.define(FooMU));499 500 bool FooReady = false;501 auto OnCompletion = [&](Expected<SymbolMap> Result) {502 cantFail(std::move(Result));503 FooReady = true;504 };505 506 ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),507 SymbolLookupSet({Foo}), SymbolState::Ready, OnCompletion,508 NoDependenciesToRegister);509 510 EXPECT_THAT_ERROR(FooR->notifyResolved({{Foo, FooSym}}), Succeeded())511 << "No symbols marked failed, but Foo failed to resolve";512 SymbolDependenceGroup SDG({{Foo}, {{&JD, SymbolNameSet({Foo})}}});513 EXPECT_THAT_ERROR(FooR->notifyEmitted(SDG), Succeeded())514 << "No symbols marked failed, but Foo failed to emit";515 516 EXPECT_TRUE(FooReady)517 << "Self-dependency prevented symbol from being marked ready";518}519 520TEST_F(CoreAPIsStandardTest, TestBasicQueryDependenciesReporting) {521 // Test that dependencies are reported as expected.522 523 bool DependenciesCallbackRan = false;524 525 std::unique_ptr<MaterializationResponsibility> FooR;526 std::unique_ptr<MaterializationResponsibility> BarR;527 528 cantFail(JD.define(std::make_unique<SimpleMaterializationUnit>(529 SymbolFlagsMap({{Foo, FooSym.getFlags()}}),530 [&](std::unique_ptr<MaterializationResponsibility> R) {531 FooR = std::move(R);532 })));533 534 cantFail(JD.define(std::make_unique<SimpleMaterializationUnit>(535 SymbolFlagsMap({{Bar, BarSym.getFlags()}}),536 [&](std::unique_ptr<MaterializationResponsibility> R) {537 BarR = std::move(R);538 })));539 540 cantFail(JD.define(std::make_unique<SimpleMaterializationUnit>(541 SymbolFlagsMap({{Baz, BazSym.getFlags()}}),542 [&](std::unique_ptr<MaterializationResponsibility> R) {543 cantFail(R->notifyResolved({{Baz, BazSym}}));544 cantFail(R->notifyEmitted({}));545 })));546 547 // First issue a lookup for Foo and Bar so that we can put them548 // into the required states for the test lookup below.549 ES.lookup(550 LookupKind::Static, makeJITDylibSearchOrder(&JD),551 SymbolLookupSet({Foo, Bar}), SymbolState::Resolved,552 [](Expected<SymbolMap> Result) {553 EXPECT_THAT_EXPECTED(std::move(Result), Succeeded());554 },555 NoDependenciesToRegister);556 557 cantFail(FooR->notifyResolved({{Foo, FooSym}}));558 cantFail(FooR->notifyEmitted({}));559 560 cantFail(BarR->notifyResolved({{Bar, BarSym}}));561 562 ES.lookup(563 LookupKind::Static, makeJITDylibSearchOrder(&JD),564 SymbolLookupSet({Foo, Bar, Baz}), SymbolState::Resolved,565 [](Expected<SymbolMap> Result) {566 EXPECT_THAT_EXPECTED(std::move(Result), Succeeded());567 },568 [&](const SymbolDependenceMap &Dependencies) {569 EXPECT_EQ(Dependencies.size(), 1U)570 << "Expect dependencies on only one JITDylib";571 EXPECT_TRUE(Dependencies.count(&JD))572 << "Expect dependencies on JD only";573 auto &Deps = Dependencies.begin()->second;574 EXPECT_EQ(Deps.size(), 2U);575 EXPECT_TRUE(Deps.count(Bar));576 EXPECT_TRUE(Deps.count(Baz));577 DependenciesCallbackRan = true;578 });579 580 cantFail(BarR->notifyEmitted({}));581 582 EXPECT_TRUE(DependenciesCallbackRan);583}584 585TEST_F(CoreAPIsStandardTest, TestCircularDependenceInOneJITDylib) {586 // Test that a circular symbol dependency between three symbols in a JITDylib587 // does not prevent any symbol from becoming 'ready' once all symbols are588 // emitted.589 590 std::unique_ptr<MaterializationResponsibility> FooR;591 std::unique_ptr<MaterializationResponsibility> BarR;592 std::unique_ptr<MaterializationResponsibility> BazR;593 594 // Create a MaterializationUnit for each symbol that moves the595 // MaterializationResponsibility into one of the locals above.596 auto FooMU = std::make_unique<SimpleMaterializationUnit>(597 SymbolFlagsMap({{Foo, FooSym.getFlags()}}),598 [&](std::unique_ptr<MaterializationResponsibility> R) {599 FooR = std::move(R);600 });601 602 auto BarMU = std::make_unique<SimpleMaterializationUnit>(603 SymbolFlagsMap({{Bar, BarSym.getFlags()}}),604 [&](std::unique_ptr<MaterializationResponsibility> R) {605 BarR = std::move(R);606 });607 608 auto BazMU = std::make_unique<SimpleMaterializationUnit>(609 SymbolFlagsMap({{Baz, BazSym.getFlags()}}),610 [&](std::unique_ptr<MaterializationResponsibility> R) {611 BazR = std::move(R);612 });613 614 // Define the symbols.615 cantFail(JD.define(FooMU));616 cantFail(JD.define(BarMU));617 cantFail(JD.define(BazMU));618 619 // Query each of the symbols to trigger materialization.620 bool FooResolved = false;621 bool FooReady = false;622 623 auto OnFooResolution = [&](Expected<SymbolMap> Result) {624 cantFail(std::move(Result));625 FooResolved = true;626 };627 628 auto OnFooReady = [&](Expected<SymbolMap> Result) {629 cantFail(std::move(Result));630 FooReady = true;631 };632 633 // Issue lookups for Foo. Use NoDependenciesToRegister: We're going to add634 // the dependencies manually below.635 ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),636 SymbolLookupSet(Foo), SymbolState::Resolved,637 std::move(OnFooResolution), NoDependenciesToRegister);638 639 ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),640 SymbolLookupSet(Foo), SymbolState::Ready, std::move(OnFooReady),641 NoDependenciesToRegister);642 643 bool BarResolved = false;644 bool BarReady = false;645 auto OnBarResolution = [&](Expected<SymbolMap> Result) {646 cantFail(std::move(Result));647 BarResolved = true;648 };649 650 auto OnBarReady = [&](Expected<SymbolMap> Result) {651 cantFail(std::move(Result));652 BarReady = true;653 };654 655 ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),656 SymbolLookupSet(Bar), SymbolState::Resolved,657 std::move(OnBarResolution), NoDependenciesToRegister);658 659 ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),660 SymbolLookupSet(Bar), SymbolState::Ready, std::move(OnBarReady),661 NoDependenciesToRegister);662 663 bool BazResolved = false;664 bool BazReady = false;665 666 auto OnBazResolution = [&](Expected<SymbolMap> Result) {667 cantFail(std::move(Result));668 BazResolved = true;669 };670 671 auto OnBazReady = [&](Expected<SymbolMap> Result) {672 cantFail(std::move(Result));673 BazReady = true;674 };675 676 ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),677 SymbolLookupSet(Baz), SymbolState::Resolved,678 std::move(OnBazResolution), NoDependenciesToRegister);679 680 ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),681 SymbolLookupSet(Baz), SymbolState::Ready, std::move(OnBazReady),682 NoDependenciesToRegister);683 684 // Check that nothing has been resolved yet.685 EXPECT_FALSE(FooResolved) << "\"Foo\" should not be resolved yet";686 EXPECT_FALSE(BarResolved) << "\"Bar\" should not be resolved yet";687 EXPECT_FALSE(BazResolved) << "\"Baz\" should not be resolved yet";688 689 // Resolve the symbols (but do not emit them).690 EXPECT_THAT_ERROR(FooR->notifyResolved({{Foo, FooSym}}), Succeeded())691 << "No symbols failed, but Foo failed to resolve";692 EXPECT_THAT_ERROR(BarR->notifyResolved({{Bar, BarSym}}), Succeeded())693 << "No symbols failed, but Bar failed to resolve";694 EXPECT_THAT_ERROR(BazR->notifyResolved({{Baz, BazSym}}), Succeeded())695 << "No symbols failed, but Baz failed to resolve";696 697 // Verify that the symbols have been resolved, but are not ready yet.698 EXPECT_TRUE(FooResolved) << "\"Foo\" should be resolved now";699 EXPECT_TRUE(BarResolved) << "\"Bar\" should be resolved now";700 EXPECT_TRUE(BazResolved) << "\"Baz\" should be resolved now";701 702 EXPECT_FALSE(FooReady) << "\"Foo\" should not be ready yet";703 EXPECT_FALSE(BarReady) << "\"Bar\" should not be ready yet";704 EXPECT_FALSE(BazReady) << "\"Baz\" should not be ready yet";705 706 // Emit two of the symbols.707 {708 SymbolDependenceGroup FooDeps({{Foo}, {{&JD, {Foo, Bar}}}});709 EXPECT_THAT_ERROR(FooR->notifyEmitted(FooDeps), Succeeded())710 << "No symbols failed, but Foo failed to emit";711 712 SymbolDependenceGroup BarDeps({{Bar}, {{&JD, {Bar, Baz}}}});713 EXPECT_THAT_ERROR(BarR->notifyEmitted(BarDeps), Succeeded())714 << "No symbols failed, but Bar failed to emit";715 }716 717 // Verify that nothing is ready until the circular dependence is resolved.718 EXPECT_FALSE(FooReady) << "\"Foo\" still should not be ready";719 EXPECT_FALSE(BarReady) << "\"Bar\" still should not be ready";720 EXPECT_FALSE(BazReady) << "\"Baz\" still should not be ready";721 722 // Emit the last symbol.723 {724 SymbolDependenceGroup BazDeps({{Baz}, {{&JD, {Baz, Foo}}}});725 EXPECT_THAT_ERROR(BazR->notifyEmitted(BazDeps), Succeeded())726 << "No symbols failed, but Baz failed to emit";727 }728 729 // Verify that everything becomes ready once the circular dependence resolved.730 EXPECT_TRUE(FooReady) << "\"Foo\" should be ready now";731 EXPECT_TRUE(BarReady) << "\"Bar\" should be ready now";732 EXPECT_TRUE(BazReady) << "\"Baz\" should be ready now";733}734 735TEST_F(CoreAPIsStandardTest, FailureInDependency) {736 std::unique_ptr<MaterializationResponsibility> FooR;737 std::unique_ptr<MaterializationResponsibility> BarR;738 739 // Create a MaterializationUnit for each symbol that moves the740 // MaterializationResponsibility into one of the locals above.741 auto FooMU = std::make_unique<SimpleMaterializationUnit>(742 SymbolFlagsMap({{Foo, FooSym.getFlags()}}),743 [&](std::unique_ptr<MaterializationResponsibility> R) {744 FooR = std::move(R);745 });746 747 auto BarMU = std::make_unique<SimpleMaterializationUnit>(748 SymbolFlagsMap({{Bar, BarSym.getFlags()}}),749 [&](std::unique_ptr<MaterializationResponsibility> R) {750 BarR = std::move(R);751 });752 753 // Define the symbols.754 cantFail(JD.define(FooMU));755 cantFail(JD.define(BarMU));756 757 bool OnFooReadyRun = false;758 auto OnFooReady = [&](Expected<SymbolMap> Result) {759 EXPECT_THAT_EXPECTED(std::move(Result), Failed());760 OnFooReadyRun = true;761 };762 763 ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),764 SymbolLookupSet(Foo), SymbolState::Ready, std::move(OnFooReady),765 NoDependenciesToRegister);766 767 bool OnBarReadyRun = false;768 auto OnBarReady = [&](Expected<SymbolMap> Result) {769 EXPECT_THAT_EXPECTED(std::move(Result), Failed());770 OnBarReadyRun = true;771 };772 773 ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),774 SymbolLookupSet(Bar), SymbolState::Ready, std::move(OnBarReady),775 NoDependenciesToRegister);776 777 // Fail bar.778 BarR->failMaterialization();779 780 // Verify that queries on Bar failed, but queries on Foo have not yet.781 EXPECT_TRUE(OnBarReadyRun) << "Query for \"Bar\" was not run";782 EXPECT_FALSE(OnFooReadyRun) << "Query for \"Foo\" was run unexpectedly";783 784 // Check that we can still resolve Foo (even though it has been failed).785 EXPECT_THAT_ERROR(FooR->notifyResolved({{Foo, FooSym}}), Succeeded())786 << "Expected resolution for \"Foo\" to succeed despite error state.";787 788 FooR->failMaterialization();789 790 // Verify that queries on Foo have now failed.791 EXPECT_TRUE(OnFooReadyRun) << "Query for \"Foo\" was not run";792 793 // Verify that subsequent lookups on Bar and Foo fail.794 EXPECT_THAT_EXPECTED(ES.lookup({&JD}, {Bar}), Failed())795 << "Lookup on failed symbol should fail";796 797 EXPECT_THAT_EXPECTED(ES.lookup({&JD}, {Foo}), Failed())798 << "Lookup on failed symbol should fail";799}800 801TEST_F(CoreAPIsStandardTest, AddDependencyOnFailedSymbol) {802 std::unique_ptr<MaterializationResponsibility> FooR;803 std::unique_ptr<MaterializationResponsibility> BarR;804 805 // Create a MaterializationUnit for each symbol that moves the806 // MaterializationResponsibility into one of the locals above.807 auto FooMU = std::make_unique<SimpleMaterializationUnit>(808 SymbolFlagsMap({{Foo, FooSym.getFlags()}}),809 [&](std::unique_ptr<MaterializationResponsibility> R) {810 FooR = std::move(R);811 });812 813 auto BarMU = std::make_unique<SimpleMaterializationUnit>(814 SymbolFlagsMap({{Bar, BarSym.getFlags()}}),815 [&](std::unique_ptr<MaterializationResponsibility> R) {816 BarR = std::move(R);817 });818 819 // Define the symbols.820 cantFail(JD.define(FooMU));821 cantFail(JD.define(BarMU));822 823 bool OnFooReadyRun = false;824 auto OnFooReady = [&](Expected<SymbolMap> Result) {825 EXPECT_THAT_EXPECTED(std::move(Result), Failed());826 OnFooReadyRun = true;827 };828 829 ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),830 SymbolLookupSet(Foo), SymbolState::Ready, std::move(OnFooReady),831 NoDependenciesToRegister);832 833 bool OnBarReadyRun = false;834 auto OnBarReady = [&](Expected<SymbolMap> Result) {835 EXPECT_THAT_EXPECTED(std::move(Result), Failed());836 OnBarReadyRun = true;837 };838 839 ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),840 SymbolLookupSet(Bar), SymbolState::Ready, std::move(OnBarReady),841 NoDependenciesToRegister);842 843 // Fail bar.844 BarR->failMaterialization();845 846 // We expect Bar's query to fail immediately, but Foo's query not to have run847 // yet.848 EXPECT_TRUE(OnBarReadyRun) << "Query for \"Bar\" was not run";849 EXPECT_FALSE(OnFooReadyRun) << "Query for \"Foo\" should not have run yet";850 851 EXPECT_THAT_ERROR(FooR->notifyResolved({{Foo, FooSym}}), Succeeded())852 << "Expected resolution for \"Foo\" to succeed.";853 854 // Check that emission of Foo fails.855 {856 SymbolDependenceGroup FooDeps({{Foo}, {{&JD, {Bar}}}});857 EXPECT_THAT_ERROR(FooR->notifyEmitted(FooDeps), Failed());858 }859 860 FooR->failMaterialization();861 862 // Foo's query should have failed before we return from addDependencies.863 EXPECT_TRUE(OnFooReadyRun) << "Query for \"Foo\" was not run";864 865 // Verify that subsequent lookups on Bar and Foo fail.866 EXPECT_THAT_EXPECTED(ES.lookup({&JD}, {Bar}), Failed())867 << "Lookup on failed symbol should fail";868 869 EXPECT_THAT_EXPECTED(ES.lookup({&JD}, {Foo}), Failed())870 << "Lookup on failed symbol should fail";871}872 873TEST_F(CoreAPIsStandardTest, FailAfterMaterialization) {874 std::unique_ptr<MaterializationResponsibility> FooR;875 std::unique_ptr<MaterializationResponsibility> BarR;876 877 // Create a MaterializationUnit for each symbol that moves the878 // MaterializationResponsibility into one of the locals above.879 auto FooMU = std::make_unique<SimpleMaterializationUnit>(880 SymbolFlagsMap({{Foo, FooSym.getFlags()}}),881 [&](std::unique_ptr<MaterializationResponsibility> R) {882 FooR = std::move(R);883 });884 885 auto BarMU = std::make_unique<SimpleMaterializationUnit>(886 SymbolFlagsMap({{Bar, BarSym.getFlags()}}),887 [&](std::unique_ptr<MaterializationResponsibility> R) {888 BarR = std::move(R);889 });890 891 // Define the symbols.892 cantFail(JD.define(FooMU));893 cantFail(JD.define(BarMU));894 895 bool OnFooReadyRun = false;896 auto OnFooReady = [&](Expected<SymbolMap> Result) {897 EXPECT_THAT_EXPECTED(std::move(Result), Failed());898 OnFooReadyRun = true;899 };900 901 ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),902 SymbolLookupSet(Foo), SymbolState::Ready, std::move(OnFooReady),903 NoDependenciesToRegister);904 905 bool OnBarReadyRun = false;906 auto OnBarReady = [&](Expected<SymbolMap> Result) {907 EXPECT_THAT_EXPECTED(std::move(Result), Failed());908 OnBarReadyRun = true;909 };910 911 ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),912 SymbolLookupSet(Bar), SymbolState::Ready, std::move(OnBarReady),913 NoDependenciesToRegister);914 915 // Materialize Foo.916 EXPECT_THAT_ERROR(FooR->notifyResolved({{Foo, FooSym}}), Succeeded())917 << "Expected resolution for \"Foo\" to succeed.";918 {919 SymbolDependenceGroup FooDeps({{Foo}, {{&JD, {Bar}}}});920 EXPECT_THAT_ERROR(FooR->notifyEmitted(FooDeps), Succeeded())921 << "Expected emission for \"Foo\" to succeed.";922 }923 924 // Fail bar.925 BarR->failMaterialization();926 927 // Verify that both queries failed.928 EXPECT_TRUE(OnFooReadyRun) << "Query for Foo did not run";929 EXPECT_TRUE(OnBarReadyRun) << "Query for Bar did not run";930}931 932TEST_F(CoreAPIsStandardTest, FailMaterializerWithUnqueriedSymbols) {933 // Make sure that symbols with no queries aganist them still934 // fail correctly.935 936 bool MaterializerRun = false;937 auto MU = std::make_unique<SimpleMaterializationUnit>(938 SymbolFlagsMap(939 {{Foo, JITSymbolFlags::Exported}, {Bar, JITSymbolFlags::Exported}}),940 [&](std::unique_ptr<MaterializationResponsibility> R) {941 MaterializerRun = true;942 R->failMaterialization();943 });944 945 cantFail(JD.define(std::move(MU)));946 947 // Issue a query for Foo, but not bar.948 EXPECT_THAT_EXPECTED(ES.lookup({&JD}, {Foo}), Failed())949 << "Expected lookup to fail.";950 951 // Check that the materializer (and therefore failMaterialization) ran.952 EXPECT_TRUE(MaterializerRun) << "Expected materializer to have run by now";953 954 // Check that subsequent queries against both symbols fail.955 EXPECT_THAT_EXPECTED(ES.lookup({&JD}, {Foo}), Failed())956 << "Expected lookup for Foo to fail.";957 EXPECT_THAT_EXPECTED(ES.lookup({&JD}, {Bar}), Failed())958 << "Expected lookup for Bar to fail.";959}960 961TEST_F(CoreAPIsStandardTest, DropMaterializerWhenEmpty) {962 bool DestructorRun = false;963 964 JITSymbolFlags WeakExported(JITSymbolFlags::Exported);965 WeakExported |= JITSymbolFlags::Weak;966 967 auto MU = std::make_unique<SimpleMaterializationUnit>(968 SymbolFlagsMap({{Foo, WeakExported}, {Bar, WeakExported}}),969 [](std::unique_ptr<MaterializationResponsibility> R) {970 llvm_unreachable("Unexpected call to materialize");971 },972 nullptr,973 [&](const JITDylib &JD, SymbolStringPtr Name) {974 EXPECT_TRUE(Name == Foo || Name == Bar)975 << "Discard of unexpected symbol?";976 },977 [&]() { DestructorRun = true; });978 979 cantFail(JD.define(MU));980 981 cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));982 983 EXPECT_FALSE(DestructorRun)984 << "MaterializationUnit should not have been destroyed yet";985 986 cantFail(JD.define(absoluteSymbols({{Bar, BarSym}})));987 988 EXPECT_TRUE(DestructorRun)989 << "MaterializationUnit should have been destroyed";990}991 992TEST_F(CoreAPIsStandardTest, AddAndMaterializeLazySymbol) {993 bool FooMaterialized = false;994 bool BarDiscarded = false;995 996 JITSymbolFlags WeakExported(JITSymbolFlags::Exported);997 WeakExported |= JITSymbolFlags::Weak;998 999 auto MU = std::make_unique<SimpleMaterializationUnit>(1000 SymbolFlagsMap({{Foo, JITSymbolFlags::Exported}, {Bar, WeakExported}}),1001 [&](std::unique_ptr<MaterializationResponsibility> R) {1002 assert(BarDiscarded && "Bar should have been discarded by this point");1003 cantFail(R->notifyResolved(SymbolMap({{Foo, FooSym}})));1004 cantFail(R->notifyEmitted({}));1005 FooMaterialized = true;1006 },1007 nullptr,1008 [&](const JITDylib &JD, SymbolStringPtr Name) {1009 EXPECT_EQ(Name, Bar) << "Expected Name to be Bar";1010 BarDiscarded = true;1011 });1012 1013 cantFail(JD.define(MU));1014 cantFail(JD.define(absoluteSymbols({{Bar, BarSym}})));1015 1016 bool OnCompletionRun = false;1017 1018 auto OnCompletion = [&](Expected<SymbolMap> Result) {1019 EXPECT_TRUE(!!Result) << "Resolution unexpectedly returned error";1020 auto I = Result->find(Foo);1021 EXPECT_NE(I, Result->end()) << "Could not find symbol definition";1022 EXPECT_EQ(I->second.getAddress(), FooSym.getAddress())1023 << "Resolution returned incorrect result";1024 OnCompletionRun = true;1025 };1026 1027 ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),1028 SymbolLookupSet(Foo), SymbolState::Ready, std::move(OnCompletion),1029 NoDependenciesToRegister);1030 1031 EXPECT_TRUE(FooMaterialized) << "Foo was not materialized";1032 EXPECT_TRUE(BarDiscarded) << "Bar was not discarded";1033 EXPECT_TRUE(OnCompletionRun) << "OnResolutionCallback was not run";1034}1035 1036TEST_F(CoreAPIsStandardTest, TestBasicWeakSymbolMaterialization) {1037 // Test that weak symbols are materialized correctly when we look them up.1038 BarSym.setFlags(BarSym.getFlags() | JITSymbolFlags::Weak);1039 1040 bool BarMaterialized = false;1041 auto MU1 = std::make_unique<SimpleMaterializationUnit>(1042 SymbolFlagsMap({{Foo, FooSym.getFlags()}, {Bar, BarSym.getFlags()}}),1043 [&](std::unique_ptr<MaterializationResponsibility> R) {1044 cantFail(R->notifyResolved(SymbolMap({{Foo, FooSym}, {Bar, BarSym}})));1045 cantFail(R->notifyEmitted({}));1046 BarMaterialized = true;1047 });1048 1049 bool DuplicateBarDiscarded = false;1050 auto MU2 = std::make_unique<SimpleMaterializationUnit>(1051 SymbolFlagsMap({{Bar, BarSym.getFlags()}}),1052 [&](std::unique_ptr<MaterializationResponsibility> R) {1053 ADD_FAILURE() << "Attempt to materialize Bar from the wrong unit";1054 R->failMaterialization();1055 },1056 nullptr,1057 [&](const JITDylib &JD, SymbolStringPtr Name) {1058 EXPECT_EQ(Name, Bar) << "Expected \"Bar\" to be discarded";1059 DuplicateBarDiscarded = true;1060 });1061 1062 cantFail(JD.define(MU1));1063 cantFail(JD.define(MU2));1064 1065 bool OnCompletionRun = false;1066 1067 auto OnCompletion = [&](Expected<SymbolMap> Result) {1068 cantFail(std::move(Result));1069 OnCompletionRun = true;1070 };1071 1072 ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),1073 SymbolLookupSet(Bar), SymbolState::Ready, std::move(OnCompletion),1074 NoDependenciesToRegister);1075 1076 EXPECT_TRUE(OnCompletionRun) << "OnCompletion not run";1077 EXPECT_TRUE(BarMaterialized) << "Bar was not materialized at all";1078 EXPECT_TRUE(DuplicateBarDiscarded)1079 << "Duplicate bar definition not discarded";1080}1081 1082TEST_F(CoreAPIsStandardTest, RedefineBoundWeakSymbol) {1083 // Check that redefinition of a bound weak symbol fails.1084 1085 JITSymbolFlags WeakExported(JITSymbolFlags::Exported);1086 WeakExported |= JITSymbolFlags::Weak;1087 1088 // Define "Foo" as weak, force materialization.1089 cantFail(JD.define(absoluteSymbols({{Foo, {FooAddr, WeakExported}}})));1090 cantFail(ES.lookup({&JD}, Foo));1091 1092 // Attempt to redefine "Foo". Expect failure, despite "Foo" being weak,1093 // since it has already been bound.1094 EXPECT_THAT_ERROR(JD.define(absoluteSymbols({{Foo, FooSym}})), Failed());1095}1096 1097TEST_F(CoreAPIsStandardTest, DefineMaterializingSymbol) {1098 bool ExpectNoMoreMaterialization = false;1099 DispatchOverride = [&](std::unique_ptr<Task> T) {1100 if (ExpectNoMoreMaterialization && isa<MaterializationTask>(*T))1101 ADD_FAILURE() << "Unexpected materialization";1102 T->run();1103 };1104 1105 auto MU = std::make_unique<SimpleMaterializationUnit>(1106 SymbolFlagsMap({{Foo, FooSym.getFlags()}}),1107 [&](std::unique_ptr<MaterializationResponsibility> R) {1108 cantFail(1109 R->defineMaterializing(SymbolFlagsMap({{Bar, BarSym.getFlags()}})));1110 cantFail(R->notifyResolved(SymbolMap({{Foo, FooSym}, {Bar, BarSym}})));1111 cantFail(R->notifyEmitted({}));1112 });1113 1114 cantFail(JD.define(MU));1115 cantFail(ES.lookup(makeJITDylibSearchOrder(&JD), Foo));1116 1117 // Assert that materialization is complete by now.1118 ExpectNoMoreMaterialization = true;1119 1120 // Look up bar to verify that no further materialization happens.1121 auto BarResult = cantFail(ES.lookup(makeJITDylibSearchOrder(&JD), Bar));1122 EXPECT_EQ(BarResult.getAddress(), BarSym.getAddress())1123 << "Expected Bar == BarSym";1124}1125 1126TEST_F(CoreAPIsStandardTest, GeneratorTest) {1127 ExecutorSymbolDef BazHiddenSym(BazSym.getAddress(),1128 BazSym.getFlags() & ~JITSymbolFlags::Exported);1129 cantFail(JD.define(absoluteSymbols({{Foo, FooSym}, {Baz, BazHiddenSym}})));1130 1131 class TestGenerator : public DefinitionGenerator {1132 public:1133 TestGenerator(SymbolMap Symbols) : Symbols(std::move(Symbols)) {}1134 Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD,1135 JITDylibLookupFlags JDLookupFlags,1136 const SymbolLookupSet &Names) override {1137 SymbolMap NewDefs;1138 1139 for (const auto &KV : Names) {1140 const auto &Name = KV.first;1141 if (Symbols.count(Name))1142 NewDefs[Name] = Symbols[Name];1143 }1144 1145 cantFail(JD.define(absoluteSymbols(std::move(NewDefs))));1146 return Error::success();1147 };1148 1149 private:1150 SymbolMap Symbols;1151 };1152 1153 JD.addGenerator(std::make_unique<TestGenerator>(1154 SymbolMap({{Bar, BarSym}, {Baz, BazSym}})));1155 1156 auto Result = cantFail(1157 ES.lookup(makeJITDylibSearchOrder(&JD),1158 SymbolLookupSet({Foo, Bar})1159 .add(Baz, SymbolLookupFlags::WeaklyReferencedSymbol)));1160 1161 EXPECT_EQ(Result.count(Bar), 1U) << "Expected to find fallback def for 'bar'";1162 EXPECT_EQ(Result[Bar].getAddress(), BarSym.getAddress())1163 << "Expected fallback def for Bar to be equal to BarSym";1164}1165 1166/// By default appends LookupStates to a queue.1167/// Behavior can be overridden by setting TryToGenerateOverride.1168class SimpleAsyncGenerator : public DefinitionGenerator {1169public:1170 struct SuspendedLookupInfo {1171 LookupState LS;1172 LookupKind K;1173 JITDylibSP JD;1174 JITDylibLookupFlags JDLookupFlags;1175 SymbolLookupSet Names;1176 };1177 1178 unique_function<Error(LookupState &, LookupKind, JITDylib &,1179 JITDylibLookupFlags, const SymbolLookupSet &)>1180 TryToGenerateOverride;1181 1182 Error tryToGenerate(LookupState &LS, LookupKind K, JITDylib &JD,1183 JITDylibLookupFlags JDLookupFlags,1184 const SymbolLookupSet &Names) override {1185 if (TryToGenerateOverride)1186 return TryToGenerateOverride(LS, K, JD, JDLookupFlags, Names);1187 Lookup = SuspendedLookupInfo{std::move(LS), K, &JD, JDLookupFlags, Names};1188 return Error::success();1189 }1190 1191 SuspendedLookupInfo takeLookup() {1192 std::optional<SuspendedLookupInfo> Tmp;1193 std::swap(Tmp, Lookup);1194 return std::move(*Tmp);1195 }1196 1197 std::optional<SuspendedLookupInfo> Lookup;1198};1199 1200TEST_F(CoreAPIsStandardTest, SimpleAsynchronousGeneratorTest) {1201 1202 auto &G = JD.addGenerator(std::make_unique<SimpleAsyncGenerator>());1203 1204 bool LookupCompleted = false;1205 1206 ES.lookup(1207 LookupKind::Static, makeJITDylibSearchOrder(&JD), SymbolLookupSet(Foo),1208 SymbolState::Ready,1209 [&](Expected<SymbolMap> Result) {1210 LookupCompleted = true;1211 EXPECT_THAT_EXPECTED(Result, Succeeded());1212 if (Result) {1213 EXPECT_EQ(*Result, SymbolMap({{Foo, FooSym}}));1214 }1215 },1216 NoDependenciesToRegister);1217 1218 EXPECT_FALSE(LookupCompleted);1219 1220 cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));1221 G.takeLookup().LS.continueLookup(Error::success());1222 1223 EXPECT_TRUE(LookupCompleted);1224}1225 1226TEST_F(CoreAPIsStandardTest, ErrorFromSuspendedAsynchronousGeneratorTest) {1227 1228 auto &G = JD.addGenerator(std::make_unique<SimpleAsyncGenerator>());1229 1230 bool LookupCompleted = false;1231 1232 ES.lookup(1233 LookupKind::Static, makeJITDylibSearchOrder(&JD), SymbolLookupSet(Foo),1234 SymbolState::Ready,1235 [&](Expected<SymbolMap> Result) {1236 LookupCompleted = true;1237 EXPECT_THAT_EXPECTED(Result, Failed());1238 },1239 NoDependenciesToRegister);1240 1241 EXPECT_FALSE(LookupCompleted);1242 1243 G.takeLookup().LS.continueLookup(1244 make_error<StringError>("boom", inconvertibleErrorCode()));1245 1246 EXPECT_TRUE(LookupCompleted);1247}1248 1249TEST_F(CoreAPIsStandardTest, ErrorFromAutoSuspendedAsynchronousGeneratorTest) {1250 1251 auto &G = JD.addGenerator(std::make_unique<SimpleAsyncGenerator>());1252 1253 std::atomic_size_t LookupsCompleted = 0;1254 1255 ES.lookup(1256 LookupKind::Static, makeJITDylibSearchOrder(&JD), SymbolLookupSet(Foo),1257 SymbolState::Ready,1258 [&](Expected<SymbolMap> Result) {1259 ++LookupsCompleted;1260 EXPECT_THAT_EXPECTED(Result, Failed());1261 },1262 NoDependenciesToRegister);1263 1264 EXPECT_EQ(LookupsCompleted, 0U);1265 1266 // Suspend the first lookup.1267 auto LS1 = std::move(G.takeLookup().LS);1268 1269 // Start a second lookup that should be auto-suspended.1270 ES.lookup(1271 LookupKind::Static, makeJITDylibSearchOrder(&JD), SymbolLookupSet(Foo),1272 SymbolState::Ready,1273 [&](Expected<SymbolMap> Result) {1274 ++LookupsCompleted;1275 EXPECT_THAT_EXPECTED(Result, Failed());1276 },1277 NoDependenciesToRegister);1278 1279 EXPECT_EQ(LookupsCompleted, 0U);1280 1281 // Unsuspend the first lookup.1282 LS1.continueLookup(make_error<StringError>("boom", inconvertibleErrorCode()));1283 1284 // Unsuspend the second.1285 G.takeLookup().LS.continueLookup(1286 make_error<StringError>("boom", inconvertibleErrorCode()));1287 1288 EXPECT_EQ(LookupsCompleted, 2U);1289}1290 1291TEST_F(CoreAPIsStandardTest, BlockedGeneratorAutoSuspensionTest) {1292 // Test that repeated lookups while a generator is in use cause automatic1293 // lookup suspension / resumption.1294 1295 auto &G = JD.addGenerator(std::make_unique<SimpleAsyncGenerator>());1296 1297 bool Lookup1Completed = false;1298 bool Lookup2Completed = false;1299 bool Lookup3Completed = false;1300 bool Lookup4Completed = false;1301 1302 // Add lookup 1.1303 //1304 // Tests that tryToGenerate-suspended lookups resume auto-suspended lookups1305 // when the tryToGenerate-suspended lookup continues (i.e. the call to1306 // OL_resumeLookupAfterGeneration at the top of OL_applyQueryPhase1).1307 ES.lookup(1308 LookupKind::Static, makeJITDylibSearchOrder(&JD), SymbolLookupSet(Foo),1309 SymbolState::Ready,1310 [&](Expected<SymbolMap> Result) {1311 Lookup1Completed = true;1312 EXPECT_THAT_EXPECTED(Result, Succeeded());1313 if (Result) {1314 EXPECT_EQ(*Result, SymbolMap({{Foo, FooSym}}));1315 }1316 },1317 NoDependenciesToRegister);1318 1319 // The generator should immediately see the first lookup.1320 EXPECT_NE(G.Lookup, std::nullopt);1321 1322 // Add lookup 2.1323 //1324 // Tests that lookups that pass through tryToGenerate without being captured1325 // resume auto-suspended lookups. We set a one-shot TryToGenerateOverride to1326 // prevent capture of lookup 2 by tryToGenerate. This tests the call to1327 // OL_resumeLookupAfterGeneration inside the generator loop.1328 G.TryToGenerateOverride = [&](LookupState &LS, LookupKind K, JITDylib &JD,1329 JITDylibLookupFlags JDLookupFlags,1330 const SymbolLookupSet &Names) -> Error {1331 cantFail(JD.define(absoluteSymbols({{Bar, BarSym}})));1332 G.TryToGenerateOverride = nullptr;1333 return Error::success();1334 };1335 1336 ES.lookup(1337 LookupKind::Static, makeJITDylibSearchOrder(&JD), SymbolLookupSet(Bar),1338 SymbolState::Ready,1339 [&](Expected<SymbolMap> Result) {1340 Lookup2Completed = true;1341 EXPECT_THAT_EXPECTED(Result, Succeeded());1342 if (Result) {1343 EXPECT_EQ(*Result, SymbolMap({{Bar, BarSym}}));1344 }1345 },1346 NoDependenciesToRegister);1347 1348 // Add lookup 3.1349 //1350 // Test that if a lookup's symbols have already been generated (and it1351 // consequently skips the generator loop entirely) it still resumes the next1352 // suspended lookup. This tests the call to OL_resumeLookupAfterGeneration1353 // just above the generator loop.1354 ES.lookup(1355 LookupKind::Static, makeJITDylibSearchOrder(&JD), SymbolLookupSet(Bar),1356 SymbolState::Ready,1357 [&](Expected<SymbolMap> Result) {1358 Lookup3Completed = true;1359 EXPECT_THAT_EXPECTED(Result, Succeeded());1360 if (Result) {1361 EXPECT_EQ(*Result, SymbolMap({{Bar, BarSym}}));1362 }1363 },1364 NoDependenciesToRegister);1365 1366 // Add lookup 4.1367 //1368 // This is just used to verify that lookup 3 triggered resumption of the next1369 // lookup as expected.1370 ES.lookup(1371 LookupKind::Static, makeJITDylibSearchOrder(&JD), SymbolLookupSet(Baz),1372 SymbolState::Ready,1373 [&](Expected<SymbolMap> Result) {1374 Lookup4Completed = true;1375 EXPECT_THAT_EXPECTED(Result, Succeeded());1376 if (Result) {1377 EXPECT_EQ(*Result, SymbolMap({{Baz, BazSym}}));1378 }1379 },1380 NoDependenciesToRegister);1381 1382 // All lookups have been started, but none should have been completed yet.1383 EXPECT_FALSE(Lookup1Completed);1384 EXPECT_FALSE(Lookup2Completed);1385 EXPECT_FALSE(Lookup3Completed);1386 EXPECT_FALSE(Lookup4Completed);1387 1388 // Start continuing lookups.1389 1390 // First Define foo and continue lookup 1. We expect this to complete lookups1391 // 1, 2 and 3: the TryToGenerateOverride set above will define bar, which will1392 // allow both 2 and 3 to complete.1393 cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));1394 G.takeLookup().LS.continueLookup(Error::success());1395 1396 EXPECT_TRUE(Lookup1Completed);1397 EXPECT_TRUE(Lookup2Completed);1398 EXPECT_TRUE(Lookup3Completed);1399 EXPECT_FALSE(Lookup4Completed);1400 EXPECT_NE(G.Lookup, std::nullopt);1401 1402 // Check that the most recently captured lookup is lookup 4 (for baz).1403 if (G.Lookup) {1404 EXPECT_EQ(G.Lookup->Names.begin()->first, Baz);1405 }1406 1407 cantFail(JD.define(absoluteSymbols({{Baz, BazSym}})));1408 G.takeLookup().LS.continueLookup(Error::success());1409 1410 EXPECT_TRUE(Lookup4Completed);1411}1412 1413TEST_F(CoreAPIsStandardTest, FailResolution) {1414 auto MU = std::make_unique<SimpleMaterializationUnit>(1415 SymbolFlagsMap({{Foo, JITSymbolFlags::Exported | JITSymbolFlags::Weak},1416 {Bar, JITSymbolFlags::Exported | JITSymbolFlags::Weak}}),1417 [&](std::unique_ptr<MaterializationResponsibility> R) {1418 R->failMaterialization();1419 });1420 1421 cantFail(JD.define(MU));1422 1423 SymbolNameSet Names({Foo, Bar});1424 auto Result = ES.lookup(makeJITDylibSearchOrder(&JD), SymbolLookupSet(Names));1425 1426 EXPECT_FALSE(!!Result) << "Expected failure";1427 if (!Result) {1428 handleAllErrors(1429 Result.takeError(),1430 [&](FailedToMaterialize &F) {1431 EXPECT_TRUE(F.getSymbols().count(&JD))1432 << "Expected to fail on JITDylib JD";1433 EXPECT_EQ(F.getSymbols().find(&JD)->second, Names)1434 << "Expected to fail on symbols in Names";1435 },1436 [](ErrorInfoBase &EIB) {1437 std::string ErrMsg;1438 {1439 raw_string_ostream ErrOut(ErrMsg);1440 EIB.log(ErrOut);1441 }1442 ADD_FAILURE() << "Expected a FailedToResolve error. Got:\n" << ErrMsg;1443 });1444 }1445}1446 1447TEST_F(CoreAPIsStandardTest, FailEmissionAfterResolution) {1448 1449 cantFail(JD.define(absoluteSymbols({{Baz, BazSym}})));1450 1451 auto MU = std::make_unique<SimpleMaterializationUnit>(1452 SymbolFlagsMap({{Foo, FooSym.getFlags()}, {Bar, BarSym.getFlags()}}),1453 [&](std::unique_ptr<MaterializationResponsibility> R) {1454 cantFail(R->notifyResolved(SymbolMap({{Foo, FooSym}, {Bar, BarSym}})));1455 ES.lookup(1456 LookupKind::Static, makeJITDylibSearchOrder(&JD),1457 SymbolLookupSet({Baz}), SymbolState::Resolved,1458 [&](Expected<SymbolMap> Result) {1459 // Called when "baz" is resolved. We don't actually depend1460 // on or care about baz, but use it to trigger failure of1461 // this materialization before Baz has been finalized in1462 // order to test that error propagation is correct in this1463 // scenario.1464 cantFail(std::move(Result));1465 R->failMaterialization();1466 },1467 NoDependenciesToRegister);1468 });1469 1470 cantFail(JD.define(MU));1471 1472 auto Result =1473 ES.lookup(makeJITDylibSearchOrder(&JD), SymbolLookupSet({Foo, Bar}));1474 1475 EXPECT_THAT_EXPECTED(std::move(Result), Failed())1476 << "Unexpected success while trying to test error propagation";1477}1478 1479TEST_F(CoreAPIsStandardTest, FailAfterPartialResolution) {1480 1481 cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));1482 1483 // Fail materialization of bar.1484 auto BarMU = std::make_unique<SimpleMaterializationUnit>(1485 SymbolFlagsMap({{Bar, BarSym.getFlags()}}),1486 [&](std::unique_ptr<MaterializationResponsibility> R) {1487 R->failMaterialization();1488 });1489 1490 cantFail(JD.define(std::move(BarMU)));1491 1492 bool QueryHandlerRun = false;1493 ES.lookup(1494 LookupKind::Static, makeJITDylibSearchOrder(&JD),1495 SymbolLookupSet({Foo, Bar}), SymbolState::Resolved,1496 [&](Expected<SymbolMap> Result) {1497 EXPECT_THAT_EXPECTED(std::move(Result), Failed())1498 << "Expected query to fail";1499 QueryHandlerRun = true;1500 },1501 NoDependenciesToRegister);1502 EXPECT_TRUE(QueryHandlerRun) << "Query handler never ran";1503}1504 1505TEST_F(CoreAPIsStandardTest, FailDefineMaterializingDueToDefunctTracker) {1506 // Check that a defunct resource tracker causes defineMaterializing to error1507 // immediately.1508 1509 std::unique_ptr<MaterializationResponsibility> FooMR;1510 auto MU = std::make_unique<SimpleMaterializationUnit>(1511 SymbolFlagsMap({{Foo, FooSym.getFlags()}}),1512 [&](std::unique_ptr<MaterializationResponsibility> R) {1513 FooMR = std::move(R);1514 });1515 1516 auto RT = JD.createResourceTracker();1517 cantFail(JD.define(std::move(MU), RT));1518 1519 bool OnCompletionRan = false;1520 auto OnCompletion = [&](Expected<SymbolMap> Result) {1521 EXPECT_THAT_EXPECTED(Result, Failed());1522 OnCompletionRan = true;1523 };1524 1525 ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),1526 SymbolLookupSet(Foo), SymbolState::Ready, OnCompletion,1527 NoDependenciesToRegister);1528 1529 cantFail(RT->remove());1530 1531 EXPECT_THAT_ERROR(FooMR->defineMaterializing(SymbolFlagsMap()), Failed())1532 << "defineMaterializing should have failed due to a defunct tracker";1533 1534 FooMR->failMaterialization();1535 1536 EXPECT_TRUE(OnCompletionRan) << "OnCompletion handler did not run.";1537}1538 1539TEST_F(CoreAPIsStandardTest, TestLookupWithUnthreadedMaterialization) {1540 auto MU = std::make_unique<SimpleMaterializationUnit>(1541 SymbolFlagsMap({{Foo, JITSymbolFlags::Exported}}),1542 [&](std::unique_ptr<MaterializationResponsibility> R) {1543 cantFail(R->notifyResolved({{Foo, FooSym}}));1544 cantFail(R->notifyEmitted({}));1545 });1546 1547 cantFail(JD.define(MU));1548 1549 auto FooLookupResult = cantFail(ES.lookup(makeJITDylibSearchOrder(&JD), Foo));1550 1551 EXPECT_EQ(FooLookupResult.getAddress(), FooSym.getAddress())1552 << "lookup returned an incorrect address";1553 EXPECT_EQ(FooLookupResult.getFlags(), FooSym.getFlags())1554 << "lookup returned incorrect flags";1555}1556 1557TEST_F(CoreAPIsStandardTest, TestLookupWithThreadedMaterialization) {1558#if LLVM_ENABLE_THREADS1559 1560 std::mutex WorkThreadsMutex;1561 SmallVector<std::thread, 0> WorkThreads;1562 DispatchOverride = [&](std::unique_ptr<Task> T) {1563 std::lock_guard<std::mutex> Lock(WorkThreadsMutex);1564 WorkThreads.push_back(1565 std::thread([T = std::move(T)]() mutable { T->run(); }));1566 };1567 1568 cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));1569 1570 auto FooLookupResult = cantFail(ES.lookup(makeJITDylibSearchOrder(&JD), Foo));1571 1572 EXPECT_EQ(FooLookupResult.getAddress(), FooSym.getAddress())1573 << "lookup returned an incorrect address";1574 EXPECT_EQ(FooLookupResult.getFlags(), FooSym.getFlags())1575 << "lookup returned incorrect flags";1576 1577 std::unique_lock<std::mutex> Lock(WorkThreadsMutex);1578 // This works because every child thread that is allowed to use WorkThreads1579 // must either be in WorkThreads or its parent must be in WorkThreads.1580 while (!WorkThreads.empty()) {1581 auto WT = WorkThreads.pop_back_val();1582 Lock.unlock();1583 WT.join();1584 Lock.lock();1585 }1586#endif1587}1588 1589TEST_F(CoreAPIsStandardTest, TestGetRequestedSymbolsAndReplace) {1590 // Test that GetRequestedSymbols returns the set of symbols that currently1591 // have pending queries, and test that MaterializationResponsibility's1592 // replace method can be used to return definitions to the JITDylib in a new1593 // MaterializationUnit.1594 bool FooMaterialized = false;1595 bool BarMaterialized = false;1596 1597 auto MU = std::make_unique<SimpleMaterializationUnit>(1598 SymbolFlagsMap({{Foo, FooSym.getFlags()}, {Bar, BarSym.getFlags()}}),1599 [&](std::unique_ptr<MaterializationResponsibility> R) {1600 auto Requested = R->getRequestedSymbols();1601 EXPECT_EQ(Requested.size(), 1U) << "Expected one symbol requested";1602 EXPECT_EQ(*Requested.begin(), Foo) << "Expected \"Foo\" requested";1603 1604 auto NewMU = std::make_unique<SimpleMaterializationUnit>(1605 SymbolFlagsMap({{Bar, BarSym.getFlags()}}),1606 [&](std::unique_ptr<MaterializationResponsibility> R2) {1607 cantFail(R2->notifyResolved(SymbolMap({{Bar, BarSym}})));1608 cantFail(R2->notifyEmitted({}));1609 BarMaterialized = true;1610 });1611 1612 cantFail(R->replace(std::move(NewMU)));1613 1614 cantFail(R->notifyResolved(SymbolMap({{Foo, FooSym}})));1615 cantFail(R->notifyEmitted({}));1616 1617 FooMaterialized = true;1618 });1619 1620 cantFail(JD.define(MU));1621 1622 EXPECT_FALSE(FooMaterialized) << "Foo should not be materialized yet";1623 EXPECT_FALSE(BarMaterialized) << "Bar should not be materialized yet";1624 1625 auto FooSymResult = cantFail(ES.lookup(makeJITDylibSearchOrder(&JD), Foo));1626 EXPECT_EQ(FooSymResult.getAddress(), FooSym.getAddress())1627 << "Address mismatch for Foo";1628 1629 EXPECT_TRUE(FooMaterialized) << "Foo should be materialized now";1630 EXPECT_FALSE(BarMaterialized) << "Bar still should not be materialized";1631 1632 auto BarSymResult = cantFail(ES.lookup(makeJITDylibSearchOrder(&JD), Bar));1633 EXPECT_EQ(BarSymResult.getAddress(), BarSym.getAddress())1634 << "Address mismatch for Bar";1635 EXPECT_TRUE(BarMaterialized) << "Bar should be materialized now";1636}1637 1638TEST_F(CoreAPIsStandardTest, TestMaterializationResponsibilityDelegation) {1639 auto MU = std::make_unique<SimpleMaterializationUnit>(1640 SymbolFlagsMap({{Foo, FooSym.getFlags()}, {Bar, BarSym.getFlags()}}),1641 [&](std::unique_ptr<MaterializationResponsibility> R) {1642 auto R2 = cantFail(R->delegate({Bar}));1643 1644 cantFail(R->notifyResolved({{Foo, FooSym}}));1645 cantFail(R->notifyEmitted({}));1646 cantFail(R2->notifyResolved({{Bar, BarSym}}));1647 cantFail(R2->notifyEmitted({}));1648 });1649 1650 cantFail(JD.define(MU));1651 1652 auto Result =1653 ES.lookup(makeJITDylibSearchOrder(&JD), SymbolLookupSet({Foo, Bar}));1654 1655 EXPECT_TRUE(!!Result) << "Result should be a success value";1656 EXPECT_EQ(Result->count(Foo), 1U) << "\"Foo\" entry missing";1657 EXPECT_EQ(Result->count(Bar), 1U) << "\"Bar\" entry missing";1658 EXPECT_EQ((*Result)[Foo].getAddress(), FooSym.getAddress())1659 << "Address mismatch for \"Foo\"";1660 EXPECT_EQ((*Result)[Bar].getAddress(), BarSym.getAddress())1661 << "Address mismatch for \"Bar\"";1662}1663 1664TEST_F(CoreAPIsStandardTest, TestMaterializeWeakSymbol) {1665 // Confirm that once a weak definition is selected for materialization it is1666 // treated as strong.1667 JITSymbolFlags WeakExported = JITSymbolFlags::Exported;1668 WeakExported &= JITSymbolFlags::Weak;1669 1670 std::unique_ptr<MaterializationResponsibility> FooR;1671 auto MU = std::make_unique<SimpleMaterializationUnit>(1672 SymbolFlagsMap({{Foo, FooSym.getFlags()}}),1673 [&](std::unique_ptr<MaterializationResponsibility> R) {1674 FooR = std::move(R);1675 });1676 1677 cantFail(JD.define(MU));1678 auto OnCompletion = [](Expected<SymbolMap> Result) {1679 cantFail(std::move(Result));1680 };1681 1682 ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),1683 SymbolLookupSet({Foo}), SymbolState::Ready, std::move(OnCompletion),1684 NoDependenciesToRegister);1685 1686 auto MU2 = std::make_unique<SimpleMaterializationUnit>(1687 SymbolFlagsMap({{Foo, JITSymbolFlags::Exported}}),1688 [](std::unique_ptr<MaterializationResponsibility> R) {1689 llvm_unreachable("This unit should never be materialized");1690 });1691 1692 auto Err = JD.define(MU2);1693 EXPECT_TRUE(!!Err) << "Expected failure value";1694 EXPECT_TRUE(Err.isA<DuplicateDefinition>())1695 << "Expected a duplicate definition error";1696 consumeError(std::move(Err));1697 1698 // No dependencies registered, can't fail:1699 cantFail(FooR->notifyResolved(SymbolMap({{Foo, FooSym}})));1700 cantFail(FooR->notifyEmitted({}));1701}1702 1703static bool linkOrdersEqual(const std::vector<JITDylibSP> &LHS,1704 ArrayRef<JITDylib *> RHS) {1705 if (LHS.size() != RHS.size())1706 return false;1707 auto *RHSE = RHS.begin();1708 for (auto &LHSE : LHS)1709 if (LHSE.get() != *RHSE)1710 return false;1711 else1712 ++RHSE;1713 return true;1714}1715 1716TEST(JITDylibTest, GetDFSLinkOrderTree) {1717 // Test that DFS ordering behaves as expected when the linkage relationships1718 // form a tree.1719 1720 ExecutionSession ES{std::make_unique<UnsupportedExecutorProcessControl>()};1721 auto _ = make_scope_exit([&]() { cantFail(ES.endSession()); });1722 1723 auto &LibA = ES.createBareJITDylib("A");1724 auto &LibB = ES.createBareJITDylib("B");1725 auto &LibC = ES.createBareJITDylib("C");1726 auto &LibD = ES.createBareJITDylib("D");1727 auto &LibE = ES.createBareJITDylib("E");1728 auto &LibF = ES.createBareJITDylib("F");1729 1730 // Linkage relationships:1731 // A --- B -- D1732 // \ \- E1733 // \- C -- F1734 LibA.setLinkOrder(makeJITDylibSearchOrder({&LibB, &LibC}));1735 LibB.setLinkOrder(makeJITDylibSearchOrder({&LibD, &LibE}));1736 LibC.setLinkOrder(makeJITDylibSearchOrder({&LibF}));1737 1738 auto DFSOrderFromB = cantFail(JITDylib::getDFSLinkOrder({&LibB}));1739 EXPECT_TRUE(linkOrdersEqual(DFSOrderFromB, {&LibB, &LibD, &LibE}))1740 << "Incorrect DFS link order for LibB";1741 1742 auto DFSOrderFromA = cantFail(JITDylib::getDFSLinkOrder({&LibA}));1743 EXPECT_TRUE(linkOrdersEqual(DFSOrderFromA,1744 {&LibA, &LibB, &LibD, &LibE, &LibC, &LibF}))1745 << "Incorrect DFS link order for libA";1746 1747 auto DFSOrderFromAB = cantFail(JITDylib::getDFSLinkOrder({&LibA, &LibB}));1748 EXPECT_TRUE(linkOrdersEqual(DFSOrderFromAB,1749 {&LibA, &LibB, &LibD, &LibE, &LibC, &LibF}))1750 << "Incorrect DFS link order for { libA, libB }";1751 1752 auto DFSOrderFromBA = cantFail(JITDylib::getDFSLinkOrder({&LibB, &LibA}));1753 EXPECT_TRUE(linkOrdersEqual(DFSOrderFromBA,1754 {&LibB, &LibD, &LibE, &LibA, &LibC, &LibF}))1755 << "Incorrect DFS link order for { libB, libA }";1756}1757 1758TEST(JITDylibTest, GetDFSLinkOrderDiamond) {1759 // Test that DFS ordering behaves as expected when the linkage relationships1760 // contain a diamond.1761 1762 ExecutionSession ES{std::make_unique<UnsupportedExecutorProcessControl>()};1763 auto _ = make_scope_exit([&]() { cantFail(ES.endSession()); });1764 1765 auto &LibA = ES.createBareJITDylib("A");1766 auto &LibB = ES.createBareJITDylib("B");1767 auto &LibC = ES.createBareJITDylib("C");1768 auto &LibD = ES.createBareJITDylib("D");1769 1770 // Linkage relationships:1771 // A -- B --- D1772 // \-- C --/1773 LibA.setLinkOrder(makeJITDylibSearchOrder({&LibB, &LibC}));1774 LibB.setLinkOrder(makeJITDylibSearchOrder({&LibD}));1775 LibC.setLinkOrder(makeJITDylibSearchOrder({&LibD}));1776 1777 auto DFSOrderFromA = cantFail(JITDylib::getDFSLinkOrder({&LibA}));1778 EXPECT_TRUE(linkOrdersEqual(DFSOrderFromA, {&LibA, &LibB, &LibD, &LibC}))1779 << "Incorrect DFS link order for libA";1780}1781 1782TEST(JITDylibTest, GetDFSLinkOrderCycle) {1783 // Test that DFS ordering behaves as expected when the linkage relationships1784 // contain a cycle.1785 1786 ExecutionSession ES{std::make_unique<UnsupportedExecutorProcessControl>()};1787 auto _ = make_scope_exit([&]() { cantFail(ES.endSession()); });1788 1789 auto &LibA = ES.createBareJITDylib("A");1790 auto &LibB = ES.createBareJITDylib("B");1791 auto &LibC = ES.createBareJITDylib("C");1792 1793 // Linkage relationships:1794 // A -- B --- C -- A1795 LibA.setLinkOrder(makeJITDylibSearchOrder({&LibB}));1796 LibB.setLinkOrder(makeJITDylibSearchOrder({&LibC}));1797 LibC.setLinkOrder(makeJITDylibSearchOrder({&LibA}));1798 1799 auto DFSOrderFromA = cantFail(JITDylib::getDFSLinkOrder({&LibA}));1800 EXPECT_TRUE(linkOrdersEqual(DFSOrderFromA, {&LibA, &LibB, &LibC}))1801 << "Incorrect DFS link order for libA";1802 1803 auto DFSOrderFromB = cantFail(JITDylib::getDFSLinkOrder({&LibB}));1804 EXPECT_TRUE(linkOrdersEqual(DFSOrderFromB, {&LibB, &LibC, &LibA}))1805 << "Incorrect DFS link order for libB";1806 1807 auto DFSOrderFromC = cantFail(JITDylib::getDFSLinkOrder({&LibC}));1808 EXPECT_TRUE(linkOrdersEqual(DFSOrderFromC, {&LibC, &LibA, &LibB}))1809 << "Incorrect DFS link order for libC";1810}1811 1812TEST_F(CoreAPIsStandardTest, RemoveJITDylibs) {1813 // Foo will be fully materialized.1814 cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));1815 1816 // Bar should not be materialized at all.1817 bool BarMaterializerDestroyed = false;1818 cantFail(JD.define(std::make_unique<SimpleMaterializationUnit>(1819 SymbolFlagsMap({{Bar, BarSym.getFlags()}}),1820 [&](std::unique_ptr<MaterializationResponsibility> MR) {1821 llvm_unreachable("Unexpected call to materialize");1822 },1823 nullptr,1824 [](const JITDylib &, SymbolStringPtr Name) {1825 llvm_unreachable("Unexpected call to discard");1826 },1827 [&]() { BarMaterializerDestroyed = true; })));1828 1829 // Baz will be in the materializing state.1830 std::unique_ptr<MaterializationResponsibility> BazMR;1831 cantFail(JD.define(std::make_unique<SimpleMaterializationUnit>(1832 SymbolFlagsMap({{Baz, BazSym.getFlags()}}),1833 [&](std::unique_ptr<MaterializationResponsibility> MR) {1834 BazMR = std::move(MR);1835 })));1836 1837 // Lookup to force materialization of Foo.1838 cantFail(ES.lookup(makeJITDylibSearchOrder(&JD), SymbolLookupSet({Foo})));1839 1840 // Start a lookup to force materialization of Baz.1841 bool BazLookupFailed = false;1842 ES.lookup(1843 LookupKind::Static, makeJITDylibSearchOrder(&JD), SymbolLookupSet({Baz}),1844 SymbolState::Ready,1845 [&](Expected<SymbolMap> Result) {1846 if (!Result) {1847 BazLookupFailed = true;1848 consumeError(Result.takeError());1849 }1850 },1851 NoDependenciesToRegister);1852 1853 // Remove the JITDylib.1854 auto Err = ES.removeJITDylib(JD);1855 EXPECT_THAT_ERROR(std::move(Err), Succeeded());1856 1857 EXPECT_TRUE(BarMaterializerDestroyed);1858 EXPECT_TRUE(BazLookupFailed);1859 1860 EXPECT_THAT_ERROR(BazMR->notifyResolved({{Baz, BazSym}}), Failed());1861 1862 EXPECT_THAT_EXPECTED(JD.getDFSLinkOrder(), Failed());1863 1864 BazMR->failMaterialization();1865}1866 1867TEST(CoreAPIsExtraTest, SessionTeardownByFailedToMaterialize) {1868 1869 auto RunTestCase = []() -> Error {1870 ExecutionSession ES{std::make_unique<UnsupportedExecutorProcessControl>(1871 std::make_shared<SymbolStringPool>())};1872 auto Foo = ES.intern("foo");1873 auto FooFlags = JITSymbolFlags::Exported;1874 1875 auto &JD = ES.createBareJITDylib("Foo");1876 cantFail(JD.define(std::make_unique<SimpleMaterializationUnit>(1877 SymbolFlagsMap({{Foo, FooFlags}}),1878 [&](std::unique_ptr<MaterializationResponsibility> R) {1879 R->failMaterialization();1880 })));1881 1882 auto Sym = ES.lookup({&JD}, Foo);1883 assert(!Sym && "Query should have failed");1884 cantFail(ES.endSession());1885 return Sym.takeError();1886 };1887 1888 auto Err = RunTestCase();1889 EXPECT_TRUE(!!Err); // Expect that error occurred.1890 EXPECT_TRUE(1891 Err.isA<FailedToMaterialize>()); // Expect FailedToMaterialize error.1892 1893 // Make sure that we can log errors, even though the session has been1894 // destroyed.1895 logAllUnhandledErrors(std::move(Err), nulls(), "");1896}1897 1898} // namespace1899