CoreAPIsTest.cpp 41 KB

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