CoreAPIsTest.cpp 33 KB

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