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