1 //===----------- CoreAPIsTest.cpp - Unit tests for Core ORC APIs ----------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "OrcTestCommon.h" 11 #include "llvm/Config/llvm-config.h" 12 #include "llvm/ExecutionEngine/Orc/Core.h" 13 #include "llvm/ExecutionEngine/Orc/OrcError.h" 14 15 #include <set> 16 #include <thread> 17 18 using namespace llvm; 19 using namespace llvm::orc; 20 21 class CoreAPIsStandardTest : public CoreAPIsBasedStandardTest {}; 22 23 namespace { 24 25 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 JD2.legacyLookup(Q, 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 = 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, TestBasicAliases) { 277 cantFail(JD.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarSym}}))); 278 cantFail(JD.define(symbolAliases({{Baz, {Foo, JITSymbolFlags::Exported}}, 279 {Qux, {Bar, JITSymbolFlags::Weak}}}))); 280 cantFail(JD.define(absoluteSymbols({{Qux, QuxSym}}))); 281 282 auto Result = ES.lookup(JITDylibSearchList({{&JD, false}}), {Baz, Qux}); 283 EXPECT_TRUE(!!Result) << "Unexpected lookup failure"; 284 EXPECT_EQ(Result->count(Baz), 1U) << "No result for \"baz\""; 285 EXPECT_EQ(Result->count(Qux), 1U) << "No result for \"qux\""; 286 EXPECT_EQ((*Result)[Baz].getAddress(), FooSym.getAddress()) 287 << "\"Baz\"'s address should match \"Foo\"'s"; 288 EXPECT_EQ((*Result)[Qux].getAddress(), QuxSym.getAddress()) 289 << "The \"Qux\" alias should have been overriden"; 290 } 291 292 TEST_F(CoreAPIsStandardTest, TestChainedAliases) { 293 cantFail(JD.define(absoluteSymbols({{Foo, FooSym}}))); 294 cantFail(JD.define(symbolAliases( 295 {{Baz, {Bar, BazSym.getFlags()}}, {Bar, {Foo, BarSym.getFlags()}}}))); 296 297 auto Result = ES.lookup(JITDylibSearchList({{&JD, false}}), {Bar, Baz}); 298 EXPECT_TRUE(!!Result) << "Unexpected lookup failure"; 299 EXPECT_EQ(Result->count(Bar), 1U) << "No result for \"bar\""; 300 EXPECT_EQ(Result->count(Baz), 1U) << "No result for \"baz\""; 301 EXPECT_EQ((*Result)[Bar].getAddress(), FooSym.getAddress()) 302 << "\"Bar\"'s address should match \"Foo\"'s"; 303 EXPECT_EQ((*Result)[Baz].getAddress(), FooSym.getAddress()) 304 << "\"Baz\"'s address should match \"Foo\"'s"; 305 } 306 307 TEST_F(CoreAPIsStandardTest, TestBasicReExports) { 308 // Test that the basic use case of re-exporting a single symbol from another 309 // JITDylib works. 310 cantFail(JD.define(absoluteSymbols({{Foo, FooSym}}))); 311 312 auto &JD2 = ES.createJITDylib("JD2"); 313 314 cantFail(JD2.define(reexports(JD, {{Bar, {Foo, BarSym.getFlags()}}}))); 315 316 auto Result = cantFail(ES.lookup(JITDylibSearchList({{&JD2, false}}), Bar)); 317 EXPECT_EQ(Result.getAddress(), FooSym.getAddress()) 318 << "Re-export Bar for symbol Foo should match FooSym's address"; 319 } 320 321 TEST_F(CoreAPIsStandardTest, TestThatReExportsDontUnnecessarilyMaterialize) { 322 // Test that re-exports do not materialize symbols that have not been queried 323 // for. 324 cantFail(JD.define(absoluteSymbols({{Foo, FooSym}}))); 325 326 bool BarMaterialized = false; 327 auto BarMU = llvm::make_unique<SimpleMaterializationUnit>( 328 SymbolFlagsMap({{Bar, BarSym.getFlags()}}), 329 [&](MaterializationResponsibility R) { 330 BarMaterialized = true; 331 R.resolve({{Bar, BarSym}}); 332 R.emit(); 333 }); 334 335 cantFail(JD.define(BarMU)); 336 337 auto &JD2 = ES.createJITDylib("JD2"); 338 339 cantFail(JD2.define(reexports( 340 JD, {{Baz, {Foo, BazSym.getFlags()}}, {Qux, {Bar, QuxSym.getFlags()}}}))); 341 342 auto Result = cantFail(ES.lookup(JITDylibSearchList({{&JD2, false}}), Baz)); 343 EXPECT_EQ(Result.getAddress(), FooSym.getAddress()) 344 << "Re-export Baz for symbol Foo should match FooSym's address"; 345 346 EXPECT_FALSE(BarMaterialized) << "Bar should not have been materialized"; 347 } 348 349 TEST_F(CoreAPIsStandardTest, TestReexportsGenerator) { 350 // Test that a re-exports generator can dynamically generate reexports. 351 352 auto &JD2 = ES.createJITDylib("JD2"); 353 cantFail(JD2.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarSym}}))); 354 355 auto Filter = [this](SymbolStringPtr Name) { return Name != Bar; }; 356 357 JD.setGenerator(ReexportsGenerator(JD2, false, Filter)); 358 359 auto Flags = JD.lookupFlags({Foo, Bar, Baz}); 360 EXPECT_EQ(Flags.size(), 1U) << "Unexpected number of results"; 361 EXPECT_EQ(Flags[Foo], FooSym.getFlags()) << "Unexpected flags for Foo"; 362 363 auto Result = cantFail(ES.lookup(JITDylibSearchList({{&JD, false}}), Foo)); 364 365 EXPECT_EQ(Result.getAddress(), FooSym.getAddress()) 366 << "Incorrect reexported symbol address"; 367 } 368 369 TEST_F(CoreAPIsStandardTest, TestTrivialCircularDependency) { 370 Optional<MaterializationResponsibility> FooR; 371 auto FooMU = llvm::make_unique<SimpleMaterializationUnit>( 372 SymbolFlagsMap({{Foo, FooSym.getFlags()}}), 373 [&](MaterializationResponsibility R) { FooR.emplace(std::move(R)); }); 374 375 cantFail(JD.define(FooMU)); 376 377 bool FooReady = false; 378 auto OnResolution = [](Expected<SymbolMap> R) { cantFail(std::move(R)); }; 379 auto OnReady = [&](Error Err) { 380 cantFail(std::move(Err)); 381 FooReady = true; 382 }; 383 384 ES.lookup(JITDylibSearchList({{&JD, false}}), {Foo}, std::move(OnResolution), 385 std::move(OnReady), NoDependenciesToRegister); 386 387 FooR->resolve({{Foo, FooSym}}); 388 FooR->emit(); 389 390 EXPECT_TRUE(FooReady) 391 << "Self-dependency prevented symbol from being marked ready"; 392 } 393 394 TEST_F(CoreAPIsStandardTest, TestCircularDependenceInOneJITDylib) { 395 // Test that a circular symbol dependency between three symbols in a JITDylib 396 // does not prevent any symbol from becoming 'ready' once all symbols are 397 // emitted. 398 399 // Create three MaterializationResponsibility objects: one for each of Foo, 400 // Bar and Baz. These are optional because MaterializationResponsibility 401 // does not have a default constructor). 402 Optional<MaterializationResponsibility> FooR; 403 Optional<MaterializationResponsibility> BarR; 404 Optional<MaterializationResponsibility> BazR; 405 406 // Create a MaterializationUnit for each symbol that moves the 407 // MaterializationResponsibility into one of the locals above. 408 auto FooMU = llvm::make_unique<SimpleMaterializationUnit>( 409 SymbolFlagsMap({{Foo, FooSym.getFlags()}}), 410 [&](MaterializationResponsibility R) { FooR.emplace(std::move(R)); }); 411 412 auto BarMU = llvm::make_unique<SimpleMaterializationUnit>( 413 SymbolFlagsMap({{Bar, BarSym.getFlags()}}), 414 [&](MaterializationResponsibility R) { BarR.emplace(std::move(R)); }); 415 416 auto BazMU = llvm::make_unique<SimpleMaterializationUnit>( 417 SymbolFlagsMap({{Baz, BazSym.getFlags()}}), 418 [&](MaterializationResponsibility R) { BazR.emplace(std::move(R)); }); 419 420 // Define the symbols. 421 cantFail(JD.define(FooMU)); 422 cantFail(JD.define(BarMU)); 423 cantFail(JD.define(BazMU)); 424 425 // Query each of the symbols to trigger materialization. 426 bool FooResolved = false; 427 bool FooReady = false; 428 429 auto OnFooResolution = [&](Expected<SymbolMap> Result) { 430 cantFail(std::move(Result)); 431 FooResolved = true; 432 }; 433 434 auto OnFooReady = [&](Error Err) { 435 cantFail(std::move(Err)); 436 FooReady = true; 437 }; 438 439 // Issue a lookup for Foo. Use NoDependenciesToRegister: We're going to add 440 // the dependencies manually below. 441 ES.lookup(JITDylibSearchList({{&JD, false}}), {Foo}, 442 std::move(OnFooResolution), std::move(OnFooReady), 443 NoDependenciesToRegister); 444 445 bool BarResolved = false; 446 bool BarReady = false; 447 auto OnBarResolution = [&](Expected<SymbolMap> Result) { 448 cantFail(std::move(Result)); 449 BarResolved = true; 450 }; 451 452 auto OnBarReady = [&](Error Err) { 453 cantFail(std::move(Err)); 454 BarReady = true; 455 }; 456 457 ES.lookup(JITDylibSearchList({{&JD, false}}), {Bar}, 458 std::move(OnBarResolution), std::move(OnBarReady), 459 NoDependenciesToRegister); 460 461 bool BazResolved = false; 462 bool BazReady = false; 463 464 auto OnBazResolution = [&](Expected<SymbolMap> Result) { 465 cantFail(std::move(Result)); 466 BazResolved = true; 467 }; 468 469 auto OnBazReady = [&](Error Err) { 470 cantFail(std::move(Err)); 471 BazReady = true; 472 }; 473 474 ES.lookup(JITDylibSearchList({{&JD, false}}), {Baz}, 475 std::move(OnBazResolution), std::move(OnBazReady), 476 NoDependenciesToRegister); 477 478 // Add a circular dependency: Foo -> Bar, Bar -> Baz, Baz -> Foo. 479 FooR->addDependenciesForAll({{&JD, SymbolNameSet({Bar})}}); 480 BarR->addDependenciesForAll({{&JD, SymbolNameSet({Baz})}}); 481 BazR->addDependenciesForAll({{&JD, SymbolNameSet({Foo})}}); 482 483 // Add self-dependencies for good measure. This tests that the implementation 484 // of addDependencies filters these out. 485 FooR->addDependenciesForAll({{&JD, SymbolNameSet({Foo})}}); 486 BarR->addDependenciesForAll({{&JD, SymbolNameSet({Bar})}}); 487 BazR->addDependenciesForAll({{&JD, SymbolNameSet({Baz})}}); 488 489 // Check that nothing has been resolved yet. 490 EXPECT_FALSE(FooResolved) << "\"Foo\" should not be resolved yet"; 491 EXPECT_FALSE(BarResolved) << "\"Bar\" should not be resolved yet"; 492 EXPECT_FALSE(BazResolved) << "\"Baz\" should not be resolved yet"; 493 494 // Resolve the symbols (but do not emit them). 495 FooR->resolve({{Foo, FooSym}}); 496 BarR->resolve({{Bar, BarSym}}); 497 BazR->resolve({{Baz, BazSym}}); 498 499 // Verify that the symbols have been resolved, but are not ready yet. 500 EXPECT_TRUE(FooResolved) << "\"Foo\" should be resolved now"; 501 EXPECT_TRUE(BarResolved) << "\"Bar\" should be resolved now"; 502 EXPECT_TRUE(BazResolved) << "\"Baz\" should be resolved now"; 503 504 EXPECT_FALSE(FooReady) << "\"Foo\" should not be ready yet"; 505 EXPECT_FALSE(BarReady) << "\"Bar\" should not be ready yet"; 506 EXPECT_FALSE(BazReady) << "\"Baz\" should not be ready yet"; 507 508 // Emit two of the symbols. 509 FooR->emit(); 510 BarR->emit(); 511 512 // Verify that nothing is ready until the circular dependence is resolved. 513 EXPECT_FALSE(FooReady) << "\"Foo\" still should not be ready"; 514 EXPECT_FALSE(BarReady) << "\"Bar\" still should not be ready"; 515 EXPECT_FALSE(BazReady) << "\"Baz\" still should not be ready"; 516 517 // Emit the last symbol. 518 BazR->emit(); 519 520 // Verify that everything becomes ready once the circular dependence resolved. 521 EXPECT_TRUE(FooReady) << "\"Foo\" should be ready now"; 522 EXPECT_TRUE(BarReady) << "\"Bar\" should be ready now"; 523 EXPECT_TRUE(BazReady) << "\"Baz\" should be ready now"; 524 } 525 526 TEST_F(CoreAPIsStandardTest, DropMaterializerWhenEmpty) { 527 bool DestructorRun = false; 528 529 JITSymbolFlags WeakExported(JITSymbolFlags::Exported); 530 WeakExported |= JITSymbolFlags::Weak; 531 532 auto MU = llvm::make_unique<SimpleMaterializationUnit>( 533 SymbolFlagsMap({{Foo, WeakExported}, {Bar, WeakExported}}), 534 [](MaterializationResponsibility R) { 535 llvm_unreachable("Unexpected call to materialize"); 536 }, 537 [&](const JITDylib &JD, SymbolStringPtr Name) { 538 EXPECT_TRUE(Name == Foo || Name == Bar) 539 << "Discard of unexpected symbol?"; 540 }, 541 [&]() { DestructorRun = true; }); 542 543 cantFail(JD.define(MU)); 544 545 cantFail(JD.define(absoluteSymbols({{Foo, FooSym}}))); 546 547 EXPECT_FALSE(DestructorRun) 548 << "MaterializationUnit should not have been destroyed yet"; 549 550 cantFail(JD.define(absoluteSymbols({{Bar, BarSym}}))); 551 552 EXPECT_TRUE(DestructorRun) 553 << "MaterializationUnit should have been destroyed"; 554 } 555 556 TEST_F(CoreAPIsStandardTest, AddAndMaterializeLazySymbol) { 557 bool FooMaterialized = false; 558 bool BarDiscarded = false; 559 560 JITSymbolFlags WeakExported(JITSymbolFlags::Exported); 561 WeakExported |= JITSymbolFlags::Weak; 562 563 auto MU = llvm::make_unique<SimpleMaterializationUnit>( 564 SymbolFlagsMap({{Foo, JITSymbolFlags::Exported}, {Bar, WeakExported}}), 565 [&](MaterializationResponsibility R) { 566 assert(BarDiscarded && "Bar should have been discarded by this point"); 567 R.resolve(SymbolMap({{Foo, FooSym}})); 568 R.emit(); 569 FooMaterialized = true; 570 }, 571 [&](const JITDylib &JD, SymbolStringPtr Name) { 572 EXPECT_EQ(Name, Bar) << "Expected Name to be Bar"; 573 BarDiscarded = true; 574 }); 575 576 cantFail(JD.define(MU)); 577 cantFail(JD.define(absoluteSymbols({{Bar, BarSym}}))); 578 579 SymbolNameSet Names({Foo}); 580 581 bool OnResolutionRun = false; 582 bool OnReadyRun = false; 583 584 auto OnResolution = [&](Expected<SymbolMap> Result) { 585 EXPECT_TRUE(!!Result) << "Resolution unexpectedly returned error"; 586 auto I = Result->find(Foo); 587 EXPECT_NE(I, Result->end()) << "Could not find symbol definition"; 588 EXPECT_EQ(I->second.getAddress(), FooSym.getAddress()) 589 << "Resolution returned incorrect result"; 590 OnResolutionRun = true; 591 }; 592 593 auto OnReady = [&](Error Err) { 594 cantFail(std::move(Err)); 595 OnReadyRun = true; 596 }; 597 598 ES.lookup(JITDylibSearchList({{&JD, false}}), Names, std::move(OnResolution), 599 std::move(OnReady), NoDependenciesToRegister); 600 601 EXPECT_TRUE(FooMaterialized) << "Foo was not materialized"; 602 EXPECT_TRUE(BarDiscarded) << "Bar was not discarded"; 603 EXPECT_TRUE(OnResolutionRun) << "OnResolutionCallback was not run"; 604 EXPECT_TRUE(OnReadyRun) << "OnReady was not run"; 605 } 606 607 TEST_F(CoreAPIsStandardTest, TestBasicWeakSymbolMaterialization) { 608 // Test that weak symbols are materialized correctly when we look them up. 609 BarSym.setFlags(BarSym.getFlags() | JITSymbolFlags::Weak); 610 611 bool BarMaterialized = false; 612 auto MU1 = llvm::make_unique<SimpleMaterializationUnit>( 613 SymbolFlagsMap({{Foo, FooSym.getFlags()}, {Bar, BarSym.getFlags()}}), 614 [&](MaterializationResponsibility R) { 615 R.resolve(SymbolMap({{Foo, FooSym}, {Bar, BarSym}})), R.emit(); 616 BarMaterialized = true; 617 }); 618 619 bool DuplicateBarDiscarded = false; 620 auto MU2 = llvm::make_unique<SimpleMaterializationUnit>( 621 SymbolFlagsMap({{Bar, BarSym.getFlags()}}), 622 [&](MaterializationResponsibility R) { 623 ADD_FAILURE() << "Attempt to materialize Bar from the wrong unit"; 624 R.failMaterialization(); 625 }, 626 [&](const JITDylib &JD, SymbolStringPtr Name) { 627 EXPECT_EQ(Name, Bar) << "Expected \"Bar\" to be discarded"; 628 DuplicateBarDiscarded = true; 629 }); 630 631 cantFail(JD.define(MU1)); 632 cantFail(JD.define(MU2)); 633 634 bool OnResolvedRun = false; 635 bool OnReadyRun = false; 636 637 auto OnResolution = [&](Expected<SymbolMap> Result) { 638 cantFail(std::move(Result)); 639 OnResolvedRun = true; 640 }; 641 642 auto OnReady = [&](Error Err) { 643 cantFail(std::move(Err)); 644 OnReadyRun = true; 645 }; 646 647 ES.lookup(JITDylibSearchList({{&JD, false}}), {Bar}, std::move(OnResolution), 648 std::move(OnReady), NoDependenciesToRegister); 649 650 EXPECT_TRUE(OnResolvedRun) << "OnResolved not run"; 651 EXPECT_TRUE(OnReadyRun) << "OnReady not run"; 652 EXPECT_TRUE(BarMaterialized) << "Bar was not materialized at all"; 653 EXPECT_TRUE(DuplicateBarDiscarded) 654 << "Duplicate bar definition not discarded"; 655 } 656 657 TEST_F(CoreAPIsStandardTest, DefineMaterializingSymbol) { 658 bool ExpectNoMoreMaterialization = false; 659 ES.setDispatchMaterialization( 660 [&](JITDylib &JD, std::unique_ptr<MaterializationUnit> MU) { 661 if (ExpectNoMoreMaterialization) 662 ADD_FAILURE() << "Unexpected materialization"; 663 MU->doMaterialize(JD); 664 }); 665 666 auto MU = llvm::make_unique<SimpleMaterializationUnit>( 667 SymbolFlagsMap({{Foo, FooSym.getFlags()}}), 668 [&](MaterializationResponsibility R) { 669 cantFail( 670 R.defineMaterializing(SymbolFlagsMap({{Bar, BarSym.getFlags()}}))); 671 R.resolve(SymbolMap({{Foo, FooSym}, {Bar, BarSym}})); 672 R.emit(); 673 }); 674 675 cantFail(JD.define(MU)); 676 cantFail(ES.lookup(JITDylibSearchList({{&JD, false}}), Foo)); 677 678 // Assert that materialization is complete by now. 679 ExpectNoMoreMaterialization = true; 680 681 // Look up bar to verify that no further materialization happens. 682 auto BarResult = cantFail(ES.lookup(JITDylibSearchList({{&JD, false}}), Bar)); 683 EXPECT_EQ(BarResult.getAddress(), BarSym.getAddress()) 684 << "Expected Bar == BarSym"; 685 } 686 687 TEST_F(CoreAPIsStandardTest, GeneratorTest) { 688 cantFail(JD.define(absoluteSymbols({{Foo, FooSym}}))); 689 690 JD.setGenerator([&](JITDylib &JD2, const SymbolNameSet &Names) { 691 cantFail(JD2.define(absoluteSymbols({{Bar, BarSym}}))); 692 return SymbolNameSet({Bar}); 693 }); 694 695 auto Result = 696 cantFail(ES.lookup(JITDylibSearchList({{&JD, false}}), {Foo, Bar})); 697 698 EXPECT_EQ(Result.count(Bar), 1U) << "Expected to find fallback def for 'bar'"; 699 EXPECT_EQ(Result[Bar].getAddress(), BarSym.getAddress()) 700 << "Expected fallback def for Bar to be equal to BarSym"; 701 } 702 703 TEST_F(CoreAPIsStandardTest, FailResolution) { 704 auto MU = llvm::make_unique<SimpleMaterializationUnit>( 705 SymbolFlagsMap({{Foo, JITSymbolFlags::Exported | JITSymbolFlags::Weak}, 706 {Bar, JITSymbolFlags::Exported | JITSymbolFlags::Weak}}), 707 [&](MaterializationResponsibility R) { R.failMaterialization(); }); 708 709 cantFail(JD.define(MU)); 710 711 SymbolNameSet Names({Foo, Bar}); 712 auto Result = ES.lookup(JITDylibSearchList({{&JD, false}}), Names); 713 714 EXPECT_FALSE(!!Result) << "Expected failure"; 715 if (!Result) { 716 handleAllErrors(Result.takeError(), 717 [&](FailedToMaterialize &F) { 718 EXPECT_EQ(F.getSymbols(), Names) 719 << "Expected to fail on symbols in Names"; 720 }, 721 [](ErrorInfoBase &EIB) { 722 std::string ErrMsg; 723 { 724 raw_string_ostream ErrOut(ErrMsg); 725 EIB.log(ErrOut); 726 } 727 ADD_FAILURE() 728 << "Expected a FailedToResolve error. Got:\n" 729 << ErrMsg; 730 }); 731 } 732 } 733 734 TEST_F(CoreAPIsStandardTest, TestLookupWithUnthreadedMaterialization) { 735 auto MU = llvm::make_unique<SimpleMaterializationUnit>( 736 SymbolFlagsMap({{Foo, JITSymbolFlags::Exported}}), 737 [&](MaterializationResponsibility R) { 738 R.resolve({{Foo, FooSym}}); 739 R.emit(); 740 }); 741 742 cantFail(JD.define(MU)); 743 744 auto FooLookupResult = 745 cantFail(ES.lookup(JITDylibSearchList({{&JD, false}}), Foo)); 746 747 EXPECT_EQ(FooLookupResult.getAddress(), FooSym.getAddress()) 748 << "lookup returned an incorrect address"; 749 EXPECT_EQ(FooLookupResult.getFlags(), FooSym.getFlags()) 750 << "lookup returned incorrect flags"; 751 } 752 753 TEST_F(CoreAPIsStandardTest, TestLookupWithThreadedMaterialization) { 754 #if LLVM_ENABLE_THREADS 755 756 std::thread MaterializationThread; 757 ES.setDispatchMaterialization( 758 [&](JITDylib &JD, std::unique_ptr<MaterializationUnit> MU) { 759 auto SharedMU = std::shared_ptr<MaterializationUnit>(std::move(MU)); 760 MaterializationThread = 761 std::thread([SharedMU, &JD]() { SharedMU->doMaterialize(JD); }); 762 }); 763 764 cantFail(JD.define(absoluteSymbols({{Foo, FooSym}}))); 765 766 auto FooLookupResult = 767 cantFail(ES.lookup(JITDylibSearchList({{&JD, false}}), Foo)); 768 769 EXPECT_EQ(FooLookupResult.getAddress(), FooSym.getAddress()) 770 << "lookup returned an incorrect address"; 771 EXPECT_EQ(FooLookupResult.getFlags(), FooSym.getFlags()) 772 << "lookup returned incorrect flags"; 773 MaterializationThread.join(); 774 #endif 775 } 776 777 TEST_F(CoreAPIsStandardTest, TestGetRequestedSymbolsAndReplace) { 778 // Test that GetRequestedSymbols returns the set of symbols that currently 779 // have pending queries, and test that MaterializationResponsibility's 780 // replace method can be used to return definitions to the JITDylib in a new 781 // MaterializationUnit. 782 SymbolNameSet Names({Foo, Bar}); 783 784 bool FooMaterialized = false; 785 bool BarMaterialized = false; 786 787 auto MU = llvm::make_unique<SimpleMaterializationUnit>( 788 SymbolFlagsMap({{Foo, FooSym.getFlags()}, {Bar, BarSym.getFlags()}}), 789 [&](MaterializationResponsibility R) { 790 auto Requested = R.getRequestedSymbols(); 791 EXPECT_EQ(Requested.size(), 1U) << "Expected one symbol requested"; 792 EXPECT_EQ(*Requested.begin(), Foo) << "Expected \"Foo\" requested"; 793 794 auto NewMU = llvm::make_unique<SimpleMaterializationUnit>( 795 SymbolFlagsMap({{Bar, BarSym.getFlags()}}), 796 [&](MaterializationResponsibility R2) { 797 R2.resolve(SymbolMap({{Bar, BarSym}})); 798 R2.emit(); 799 BarMaterialized = true; 800 }); 801 802 R.replace(std::move(NewMU)); 803 804 R.resolve(SymbolMap({{Foo, FooSym}})); 805 R.emit(); 806 807 FooMaterialized = true; 808 }); 809 810 cantFail(JD.define(MU)); 811 812 EXPECT_FALSE(FooMaterialized) << "Foo should not be materialized yet"; 813 EXPECT_FALSE(BarMaterialized) << "Bar should not be materialized yet"; 814 815 auto FooSymResult = 816 cantFail(ES.lookup(JITDylibSearchList({{&JD, false}}), Foo)); 817 EXPECT_EQ(FooSymResult.getAddress(), FooSym.getAddress()) 818 << "Address mismatch for Foo"; 819 820 EXPECT_TRUE(FooMaterialized) << "Foo should be materialized now"; 821 EXPECT_FALSE(BarMaterialized) << "Bar still should not be materialized"; 822 823 auto BarSymResult = 824 cantFail(ES.lookup(JITDylibSearchList({{&JD, false}}), Bar)); 825 EXPECT_EQ(BarSymResult.getAddress(), BarSym.getAddress()) 826 << "Address mismatch for Bar"; 827 EXPECT_TRUE(BarMaterialized) << "Bar should be materialized now"; 828 } 829 830 TEST_F(CoreAPIsStandardTest, TestMaterializationResponsibilityDelegation) { 831 auto MU = llvm::make_unique<SimpleMaterializationUnit>( 832 SymbolFlagsMap({{Foo, FooSym.getFlags()}, {Bar, BarSym.getFlags()}}), 833 [&](MaterializationResponsibility R) { 834 auto R2 = R.delegate({Bar}); 835 836 R.resolve({{Foo, FooSym}}); 837 R.emit(); 838 R2.resolve({{Bar, BarSym}}); 839 R2.emit(); 840 }); 841 842 cantFail(JD.define(MU)); 843 844 auto Result = ES.lookup(JITDylibSearchList({{&JD, false}}), {Foo, Bar}); 845 846 EXPECT_TRUE(!!Result) << "Result should be a success value"; 847 EXPECT_EQ(Result->count(Foo), 1U) << "\"Foo\" entry missing"; 848 EXPECT_EQ(Result->count(Bar), 1U) << "\"Bar\" entry missing"; 849 EXPECT_EQ((*Result)[Foo].getAddress(), FooSym.getAddress()) 850 << "Address mismatch for \"Foo\""; 851 EXPECT_EQ((*Result)[Bar].getAddress(), BarSym.getAddress()) 852 << "Address mismatch for \"Bar\""; 853 } 854 855 TEST_F(CoreAPIsStandardTest, TestMaterializeWeakSymbol) { 856 // Confirm that once a weak definition is selected for materialization it is 857 // treated as strong. 858 JITSymbolFlags WeakExported = JITSymbolFlags::Exported; 859 WeakExported &= JITSymbolFlags::Weak; 860 861 std::unique_ptr<MaterializationResponsibility> FooResponsibility; 862 auto MU = llvm::make_unique<SimpleMaterializationUnit>( 863 SymbolFlagsMap({{Foo, FooSym.getFlags()}}), 864 [&](MaterializationResponsibility R) { 865 FooResponsibility = 866 llvm::make_unique<MaterializationResponsibility>(std::move(R)); 867 }); 868 869 cantFail(JD.define(MU)); 870 auto OnResolution = [](Expected<SymbolMap> Result) { 871 cantFail(std::move(Result)); 872 }; 873 874 auto OnReady = [](Error Err) { cantFail(std::move(Err)); }; 875 876 ES.lookup(JITDylibSearchList({{&JD, false}}), {Foo}, std::move(OnResolution), 877 std::move(OnReady), NoDependenciesToRegister); 878 879 auto MU2 = llvm::make_unique<SimpleMaterializationUnit>( 880 SymbolFlagsMap({{Foo, JITSymbolFlags::Exported}}), 881 [](MaterializationResponsibility R) { 882 llvm_unreachable("This unit should never be materialized"); 883 }); 884 885 auto Err = JD.define(MU2); 886 EXPECT_TRUE(!!Err) << "Expected failure value"; 887 EXPECT_TRUE(Err.isA<DuplicateDefinition>()) 888 << "Expected a duplicate definition error"; 889 consumeError(std::move(Err)); 890 891 FooResponsibility->resolve(SymbolMap({{Foo, FooSym}})); 892 FooResponsibility->emit(); 893 } 894 895 } // namespace 896