2018-01-10 01:09:38 +01:00
|
|
|
//===----------- CoreAPIsTest.cpp - Unit tests for Core ORC APIs ----------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "OrcTestCommon.h"
|
2018-04-30 16:59:11 +02:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2018-01-10 01:09:38 +01:00
|
|
|
#include "llvm/ExecutionEngine/Orc/Core.h"
|
2018-06-14 23:16:29 +02:00
|
|
|
#include "llvm/ExecutionEngine/Orc/OrcError.h"
|
2018-01-10 01:09:38 +01:00
|
|
|
|
|
|
|
#include <set>
|
2018-03-14 05:18:04 +01:00
|
|
|
#include <thread>
|
2018-01-10 01:09:38 +01:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::orc;
|
|
|
|
|
2018-07-20 20:31:50 +02:00
|
|
|
class CoreAPIsStandardTest : public CoreAPIsBasedStandardTest {};
|
|
|
|
|
2018-01-10 01:09:38 +01:00
|
|
|
namespace {
|
|
|
|
|
2018-07-21 00:22:19 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, BasicSuccessfulLookup) {
|
2018-01-10 01:09:38 +01:00
|
|
|
bool OnResolutionRun = false;
|
|
|
|
bool OnReadyRun = false;
|
2018-07-21 00:22:19 +02:00
|
|
|
|
|
|
|
auto OnResolution = [&](Expected<SymbolMap> Result) {
|
|
|
|
EXPECT_TRUE(!!Result) << "Resolution unexpectedly returned error";
|
|
|
|
auto &Resolved = *Result;
|
|
|
|
auto I = Resolved.find(Foo);
|
|
|
|
EXPECT_NE(I, Resolved.end()) << "Could not find symbol definition";
|
|
|
|
EXPECT_EQ(I->second.getAddress(), FooAddr)
|
|
|
|
<< "Resolution returned incorrect result";
|
|
|
|
OnResolutionRun = true;
|
|
|
|
};
|
2018-01-10 01:09:38 +01:00
|
|
|
auto OnReady = [&](Error Err) {
|
|
|
|
cantFail(std::move(Err));
|
|
|
|
OnReadyRun = true;
|
|
|
|
};
|
|
|
|
|
2018-07-21 00:22:19 +02:00
|
|
|
std::shared_ptr<MaterializationResponsibility> FooMR;
|
2018-05-17 00:24:30 +02:00
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(llvm::make_unique<SimpleMaterializationUnit>(
|
2018-07-21 00:22:19 +02:00
|
|
|
SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
|
|
|
|
[&](MaterializationResponsibility R) {
|
|
|
|
FooMR = std::make_shared<MaterializationResponsibility>(std::move(R));
|
|
|
|
})));
|
2018-05-17 00:24:30 +02:00
|
|
|
|
2018-10-23 22:54:43 +02:00
|
|
|
ES.lookup({&JD}, {Foo}, OnResolution, OnReady, NoDependenciesToRegister);
|
2018-05-17 00:24:30 +02:00
|
|
|
|
2018-07-21 00:22:19 +02:00
|
|
|
EXPECT_FALSE(OnResolutionRun) << "Should not have been resolved yet";
|
|
|
|
EXPECT_FALSE(OnReadyRun) << "Should not have been marked ready yet";
|
2018-07-20 20:31:53 +02:00
|
|
|
|
2018-07-21 00:22:19 +02:00
|
|
|
FooMR->resolve({{Foo, FooSym}});
|
2018-07-20 20:31:53 +02:00
|
|
|
|
2018-07-21 00:22:19 +02:00
|
|
|
EXPECT_TRUE(OnResolutionRun) << "Should have been resolved";
|
|
|
|
EXPECT_FALSE(OnReadyRun) << "Should not have been marked ready yet";
|
|
|
|
|
2018-08-18 04:06:18 +02:00
|
|
|
FooMR->emit();
|
2018-07-21 00:22:19 +02:00
|
|
|
|
|
|
|
EXPECT_TRUE(OnReadyRun) << "Should have been marked ready";
|
2018-01-10 01:09:38 +01:00
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, ExecutionSessionFailQuery) {
|
2018-01-10 01:09:38 +01:00
|
|
|
bool OnResolutionRun = false;
|
|
|
|
bool OnReadyRun = false;
|
|
|
|
|
2018-07-21 00:22:19 +02:00
|
|
|
auto OnResolution = [&](Expected<SymbolMap> Result) {
|
|
|
|
EXPECT_FALSE(!!Result) << "Resolution unexpectedly returned success";
|
|
|
|
auto Msg = toString(Result.takeError());
|
|
|
|
EXPECT_EQ(Msg, "xyz") << "Resolution returned incorrect result";
|
|
|
|
OnResolutionRun = true;
|
|
|
|
};
|
2018-01-10 01:09:38 +01:00
|
|
|
auto OnReady = [&](Error Err) {
|
|
|
|
cantFail(std::move(Err));
|
|
|
|
OnReadyRun = true;
|
|
|
|
};
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
AsynchronousSymbolQuery Q(SymbolNameSet({Foo}), OnResolution, OnReady);
|
2018-01-10 01:09:38 +01:00
|
|
|
|
2018-07-21 00:22:19 +02:00
|
|
|
ES.legacyFailQuery(Q,
|
|
|
|
make_error<StringError>("xyz", inconvertibleErrorCode()));
|
2018-01-10 01:09:38 +01:00
|
|
|
|
|
|
|
EXPECT_TRUE(OnResolutionRun) << "OnResolutionCallback was not run";
|
|
|
|
EXPECT_FALSE(OnReadyRun) << "OnReady unexpectedly run";
|
|
|
|
}
|
|
|
|
|
2018-07-21 00:22:19 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, EmptyLookup) {
|
|
|
|
bool OnResolvedRun = false;
|
2018-01-10 01:09:38 +01:00
|
|
|
bool OnReadyRun = false;
|
|
|
|
|
2018-07-21 00:22:19 +02:00
|
|
|
auto OnResolution = [&](Expected<SymbolMap> Result) {
|
|
|
|
cantFail(std::move(Result));
|
|
|
|
OnResolvedRun = true;
|
|
|
|
};
|
2018-01-10 01:09:38 +01:00
|
|
|
|
|
|
|
auto OnReady = [&](Error Err) {
|
|
|
|
cantFail(std::move(Err));
|
|
|
|
OnReadyRun = true;
|
|
|
|
};
|
|
|
|
|
2018-10-23 22:54:43 +02:00
|
|
|
ES.lookup({&JD}, {}, OnResolution, OnReady, NoDependenciesToRegister);
|
2018-06-17 20:59:01 +02:00
|
|
|
|
|
|
|
EXPECT_TRUE(OnResolvedRun) << "OnResolved was not run for empty query";
|
|
|
|
EXPECT_TRUE(OnReadyRun) << "OnReady was not run for empty query";
|
|
|
|
}
|
|
|
|
|
2018-10-07 01:03:59 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, RemoveSymbolsTest) {
|
|
|
|
// Test that:
|
|
|
|
// (1) Missing symbols generate a SymbolsNotFound error.
|
|
|
|
// (2) Materializing symbols generate a SymbolCouldNotBeRemoved error.
|
|
|
|
// (3) Removal of unmaterialized symbols triggers discard on the
|
|
|
|
// materialization unit.
|
|
|
|
// (4) Removal of symbols destroys empty materialization units.
|
|
|
|
// (5) Removal of materialized symbols works.
|
|
|
|
|
|
|
|
// Foo will be fully materialized.
|
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));
|
|
|
|
|
|
|
|
// Bar will be unmaterialized.
|
|
|
|
bool BarDiscarded = false;
|
|
|
|
bool BarMaterializerDestructed = false;
|
|
|
|
cantFail(JD.define(llvm::make_unique<SimpleMaterializationUnit>(
|
|
|
|
SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
|
|
|
|
[this](MaterializationResponsibility R) {
|
|
|
|
ADD_FAILURE() << "Unexpected materialization of \"Bar\"";
|
|
|
|
R.resolve({{Bar, BarSym}});
|
|
|
|
R.emit();
|
|
|
|
},
|
|
|
|
[&](const JITDylib &JD, const SymbolStringPtr &Name) {
|
|
|
|
EXPECT_EQ(Name, Bar) << "Expected \"Bar\" to be discarded";
|
|
|
|
if (Name == Bar)
|
|
|
|
BarDiscarded = true;
|
|
|
|
},
|
|
|
|
[&]() { BarMaterializerDestructed = true; })));
|
|
|
|
|
|
|
|
// Baz will be in the materializing state initially, then
|
|
|
|
// materialized for the final removal attempt.
|
|
|
|
Optional<MaterializationResponsibility> BazR;
|
|
|
|
cantFail(JD.define(llvm::make_unique<SimpleMaterializationUnit>(
|
|
|
|
SymbolFlagsMap({{Baz, BazSym.getFlags()}}),
|
|
|
|
[&](MaterializationResponsibility R) { BazR.emplace(std::move(R)); },
|
|
|
|
[](const JITDylib &JD, const SymbolStringPtr &Name) {
|
|
|
|
ADD_FAILURE() << "\"Baz\" discarded unexpectedly";
|
|
|
|
})));
|
|
|
|
|
|
|
|
bool OnResolvedRun = false;
|
|
|
|
bool OnReadyRun = false;
|
2018-10-23 22:54:43 +02:00
|
|
|
ES.lookup({&JD}, {Foo, Baz},
|
2018-10-07 01:03:59 +02:00
|
|
|
[&](Expected<SymbolMap> Result) {
|
|
|
|
EXPECT_TRUE(!!Result) << "OnResolved failed unexpectedly";
|
|
|
|
consumeError(Result.takeError());
|
|
|
|
OnResolvedRun = true;
|
|
|
|
},
|
|
|
|
[&](Error Err) {
|
|
|
|
EXPECT_FALSE(!!Err) << "OnReady failed unexpectedly";
|
|
|
|
consumeError(std::move(Err));
|
|
|
|
OnReadyRun = true;
|
|
|
|
},
|
|
|
|
NoDependenciesToRegister);
|
|
|
|
|
|
|
|
{
|
|
|
|
// Attempt 1: Search for a missing symbol, Qux.
|
|
|
|
auto Err = JD.remove({Foo, Bar, Baz, Qux});
|
|
|
|
EXPECT_TRUE(!!Err) << "Expected failure";
|
|
|
|
EXPECT_TRUE(Err.isA<SymbolsNotFound>())
|
|
|
|
<< "Expected a SymbolsNotFound error";
|
2018-10-07 03:08:02 +02:00
|
|
|
consumeError(std::move(Err));
|
2018-10-07 01:03:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Attempt 2: Search for a symbol that is still materializing, Baz.
|
|
|
|
auto Err = JD.remove({Foo, Bar, Baz});
|
|
|
|
EXPECT_TRUE(!!Err) << "Expected failure";
|
|
|
|
EXPECT_TRUE(Err.isA<SymbolsCouldNotBeRemoved>())
|
|
|
|
<< "Expected a SymbolsNotFound error";
|
2018-10-07 03:08:02 +02:00
|
|
|
consumeError(std::move(Err));
|
2018-10-07 01:03:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
BazR->resolve({{Baz, BazSym}});
|
|
|
|
BazR->emit();
|
|
|
|
{
|
|
|
|
// Attempt 3: Search now that all symbols are fully materialized
|
|
|
|
// (Foo, Baz), or not yet materialized (Bar).
|
|
|
|
auto Err = JD.remove({Foo, Bar, Baz});
|
|
|
|
EXPECT_FALSE(!!Err) << "Expected failure";
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_TRUE(BarDiscarded) << "\"Bar\" should have been discarded";
|
|
|
|
EXPECT_TRUE(BarMaterializerDestructed)
|
|
|
|
<< "\"Bar\"'s materializer should have been destructed";
|
|
|
|
EXPECT_TRUE(OnResolvedRun) << "OnResolved should have been run";
|
|
|
|
EXPECT_TRUE(OnReadyRun) << "OnReady should have been run";
|
|
|
|
}
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, ChainedJITDylibLookup) {
|
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));
|
2018-06-17 20:59:01 +02:00
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
auto &JD2 = ES.createJITDylib("JD2");
|
2018-06-17 20:59:01 +02:00
|
|
|
|
|
|
|
bool OnResolvedRun = false;
|
|
|
|
bool OnReadyRun = false;
|
|
|
|
|
|
|
|
auto Q = std::make_shared<AsynchronousSymbolQuery>(
|
|
|
|
SymbolNameSet({Foo}),
|
2018-07-21 00:22:19 +02:00
|
|
|
[&](Expected<SymbolMap> Result) {
|
|
|
|
cantFail(std::move(Result));
|
2018-06-17 20:59:01 +02:00
|
|
|
OnResolvedRun = true;
|
|
|
|
},
|
|
|
|
[&](Error Err) {
|
|
|
|
cantFail(std::move(Err));
|
|
|
|
OnReadyRun = true;
|
|
|
|
});
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
JD2.legacyLookup(Q, JD.legacyLookup(Q, {Foo}));
|
2018-06-17 20:59:01 +02:00
|
|
|
|
|
|
|
EXPECT_TRUE(OnResolvedRun) << "OnResolved was not run for empty query";
|
|
|
|
EXPECT_TRUE(OnReadyRun) << "OnReady was not run for empty query";
|
|
|
|
}
|
|
|
|
|
2018-10-13 23:53:40 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, LookupWithHiddenSymbols) {
|
|
|
|
auto BarHiddenFlags = BarSym.getFlags() & ~JITSymbolFlags::Exported;
|
|
|
|
auto BarHiddenSym = JITEvaluatedSymbol(BarSym.getAddress(), BarHiddenFlags);
|
|
|
|
|
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarHiddenSym}})));
|
|
|
|
|
|
|
|
auto &JD2 = ES.createJITDylib("JD2");
|
|
|
|
cantFail(JD2.define(absoluteSymbols({{Bar, QuxSym}})));
|
|
|
|
|
2018-10-23 22:54:43 +02:00
|
|
|
auto Result = cantFail(ES.lookup({&JD, &JD2}, {Foo, Bar}));
|
2018-10-13 23:53:40 +02:00
|
|
|
|
|
|
|
EXPECT_EQ(Result.size(), 2U) << "Unexpected number of results";
|
|
|
|
EXPECT_EQ(Result.count(Foo), 1U) << "Missing result for \"Foo\"";
|
|
|
|
EXPECT_EQ(Result.count(Bar), 1U) << "Missing result for \"Bar\"";
|
|
|
|
EXPECT_EQ(Result[Bar].getAddress(), QuxSym.getAddress())
|
|
|
|
<< "Wrong result for \"Bar\"";
|
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, LookupFlagsTest) {
|
2018-01-21 04:20:39 +01:00
|
|
|
// Test that lookupFlags works on a predefined symbol, and does not trigger
|
2018-07-11 05:09:36 +02:00
|
|
|
// materialization of a lazy symbol. Make the lazy symbol weak to test that
|
|
|
|
// the weak flag is propagated correctly.
|
2018-01-21 04:20:39 +01:00
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
BarSym.setFlags(static_cast<JITSymbolFlags::FlagNames>(
|
|
|
|
JITSymbolFlags::Exported | JITSymbolFlags::Weak));
|
2018-03-20 04:49:29 +01:00
|
|
|
auto MU = llvm::make_unique<SimpleMaterializationUnit>(
|
2018-07-11 05:09:36 +02:00
|
|
|
SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
|
2018-04-16 20:05:24 +02:00
|
|
|
[](MaterializationResponsibility R) {
|
2018-01-21 04:20:39 +01:00
|
|
|
llvm_unreachable("Symbol materialized on flags lookup");
|
|
|
|
});
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));
|
|
|
|
cantFail(JD.define(std::move(MU)));
|
2018-01-21 04:20:39 +01:00
|
|
|
|
|
|
|
SymbolNameSet Names({Foo, Bar, Baz});
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
auto SymbolFlags = JD.lookupFlags(Names);
|
2018-01-21 04:20:39 +01:00
|
|
|
|
2018-01-25 02:43:00 +01:00
|
|
|
EXPECT_EQ(SymbolFlags.size(), 2U)
|
2018-01-21 04:20:39 +01:00
|
|
|
<< "Returned symbol flags contains unexpected results";
|
2018-01-25 02:43:00 +01:00
|
|
|
EXPECT_EQ(SymbolFlags.count(Foo), 1U) << "Missing lookupFlags result for Foo";
|
2018-07-11 05:09:36 +02:00
|
|
|
EXPECT_EQ(SymbolFlags[Foo], FooSym.getFlags())
|
|
|
|
<< "Incorrect flags returned for Foo";
|
2018-01-25 02:43:00 +01:00
|
|
|
EXPECT_EQ(SymbolFlags.count(Bar), 1U)
|
2018-01-21 04:20:39 +01:00
|
|
|
<< "Missing lookupFlags result for Bar";
|
2018-07-11 05:09:36 +02:00
|
|
|
EXPECT_EQ(SymbolFlags[Bar], BarSym.getFlags())
|
|
|
|
<< "Incorrect flags returned for Bar";
|
2018-01-21 04:20:39 +01:00
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestBasicAliases) {
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarSym}})));
|
|
|
|
cantFail(JD.define(symbolAliases({{Baz, {Foo, JITSymbolFlags::Exported}},
|
|
|
|
{Qux, {Bar, JITSymbolFlags::Weak}}})));
|
|
|
|
cantFail(JD.define(absoluteSymbols({{Qux, QuxSym}})));
|
2018-06-26 03:22:29 +02:00
|
|
|
|
2018-10-23 22:54:43 +02:00
|
|
|
auto Result = ES.lookup({&JD}, {Baz, Qux});
|
2018-06-26 03:22:29 +02:00
|
|
|
EXPECT_TRUE(!!Result) << "Unexpected lookup failure";
|
|
|
|
EXPECT_EQ(Result->count(Baz), 1U) << "No result for \"baz\"";
|
|
|
|
EXPECT_EQ(Result->count(Qux), 1U) << "No result for \"qux\"";
|
|
|
|
EXPECT_EQ((*Result)[Baz].getAddress(), FooSym.getAddress())
|
|
|
|
<< "\"Baz\"'s address should match \"Foo\"'s";
|
|
|
|
EXPECT_EQ((*Result)[Qux].getAddress(), QuxSym.getAddress())
|
|
|
|
<< "The \"Qux\" alias should have been overriden";
|
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestChainedAliases) {
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));
|
|
|
|
cantFail(JD.define(symbolAliases(
|
2018-07-11 05:09:36 +02:00
|
|
|
{{Baz, {Bar, BazSym.getFlags()}}, {Bar, {Foo, BarSym.getFlags()}}})));
|
2018-07-11 01:34:56 +02:00
|
|
|
|
2018-10-23 22:54:43 +02:00
|
|
|
auto Result = ES.lookup({&JD}, {Bar, Baz});
|
2018-07-11 01:34:56 +02:00
|
|
|
EXPECT_TRUE(!!Result) << "Unexpected lookup failure";
|
|
|
|
EXPECT_EQ(Result->count(Bar), 1U) << "No result for \"bar\"";
|
|
|
|
EXPECT_EQ(Result->count(Baz), 1U) << "No result for \"baz\"";
|
|
|
|
EXPECT_EQ((*Result)[Bar].getAddress(), FooSym.getAddress())
|
|
|
|
<< "\"Bar\"'s address should match \"Foo\"'s";
|
|
|
|
EXPECT_EQ((*Result)[Baz].getAddress(), FooSym.getAddress())
|
|
|
|
<< "\"Baz\"'s address should match \"Foo\"'s";
|
|
|
|
}
|
|
|
|
|
2018-07-21 02:12:05 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestBasicReExports) {
|
|
|
|
// Test that the basic use case of re-exporting a single symbol from another
|
2018-08-17 23:18:18 +02:00
|
|
|
// JITDylib works.
|
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));
|
2018-07-21 02:12:05 +02:00
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
auto &JD2 = ES.createJITDylib("JD2");
|
2018-07-21 02:12:05 +02:00
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD2.define(reexports(JD, {{Bar, {Foo, BarSym.getFlags()}}})));
|
2018-07-21 02:12:05 +02:00
|
|
|
|
2018-10-23 22:54:43 +02:00
|
|
|
auto Result = cantFail(ES.lookup({&JD2}, Bar));
|
2018-07-21 02:12:05 +02:00
|
|
|
EXPECT_EQ(Result.getAddress(), FooSym.getAddress())
|
|
|
|
<< "Re-export Bar for symbol Foo should match FooSym's address";
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(CoreAPIsStandardTest, TestThatReExportsDontUnnecessarilyMaterialize) {
|
|
|
|
// Test that re-exports do not materialize symbols that have not been queried
|
|
|
|
// for.
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));
|
2018-07-21 02:12:05 +02:00
|
|
|
|
|
|
|
bool BarMaterialized = false;
|
|
|
|
auto BarMU = llvm::make_unique<SimpleMaterializationUnit>(
|
|
|
|
SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
|
|
|
|
[&](MaterializationResponsibility R) {
|
|
|
|
BarMaterialized = true;
|
|
|
|
R.resolve({{Bar, BarSym}});
|
2018-08-18 04:06:18 +02:00
|
|
|
R.emit();
|
2018-07-21 02:12:05 +02:00
|
|
|
});
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(BarMU));
|
2018-07-21 02:12:05 +02:00
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
auto &JD2 = ES.createJITDylib("JD2");
|
2018-07-21 02:12:05 +02:00
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD2.define(reexports(
|
|
|
|
JD, {{Baz, {Foo, BazSym.getFlags()}}, {Qux, {Bar, QuxSym.getFlags()}}})));
|
2018-07-21 02:12:05 +02:00
|
|
|
|
2018-10-23 22:54:43 +02:00
|
|
|
auto Result = cantFail(ES.lookup({&JD2}, Baz));
|
2018-07-21 02:12:05 +02:00
|
|
|
EXPECT_EQ(Result.getAddress(), FooSym.getAddress())
|
|
|
|
<< "Re-export Baz for symbol Foo should match FooSym's address";
|
|
|
|
|
|
|
|
EXPECT_FALSE(BarMaterialized) << "Bar should not have been materialized";
|
|
|
|
}
|
|
|
|
|
2018-10-15 07:07:54 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestReexportsGenerator) {
|
|
|
|
// Test that a re-exports generator can dynamically generate reexports.
|
2018-08-02 22:13:58 +02:00
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
auto &JD2 = ES.createJITDylib("JD2");
|
|
|
|
cantFail(JD2.define(absoluteSymbols({{Foo, FooSym}, {Bar, BarSym}})));
|
2018-08-02 22:13:58 +02:00
|
|
|
|
|
|
|
auto Filter = [this](SymbolStringPtr Name) { return Name != Bar; };
|
|
|
|
|
2018-10-23 22:54:43 +02:00
|
|
|
JD.setGenerator(ReexportsGenerator(JD2, Filter));
|
2018-08-02 22:13:58 +02:00
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
auto Flags = JD.lookupFlags({Foo, Bar, Baz});
|
2018-08-02 22:13:58 +02:00
|
|
|
EXPECT_EQ(Flags.size(), 1U) << "Unexpected number of results";
|
|
|
|
EXPECT_EQ(Flags[Foo], FooSym.getFlags()) << "Unexpected flags for Foo";
|
|
|
|
|
2018-10-23 22:54:43 +02:00
|
|
|
auto Result = cantFail(ES.lookup({&JD}, Foo));
|
2018-08-02 22:13:58 +02:00
|
|
|
|
|
|
|
EXPECT_EQ(Result.getAddress(), FooSym.getAddress())
|
|
|
|
<< "Incorrect reexported symbol address";
|
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestTrivialCircularDependency) {
|
2018-06-17 18:59:53 +02:00
|
|
|
Optional<MaterializationResponsibility> FooR;
|
|
|
|
auto FooMU = llvm::make_unique<SimpleMaterializationUnit>(
|
2018-07-11 05:09:36 +02:00
|
|
|
SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
|
2018-06-17 18:59:53 +02:00
|
|
|
[&](MaterializationResponsibility R) { FooR.emplace(std::move(R)); });
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(FooMU));
|
2018-06-17 18:59:53 +02:00
|
|
|
|
|
|
|
bool FooReady = false;
|
2018-07-21 00:22:19 +02:00
|
|
|
auto OnResolution = [](Expected<SymbolMap> R) { cantFail(std::move(R)); };
|
|
|
|
auto OnReady = [&](Error Err) {
|
|
|
|
cantFail(std::move(Err));
|
|
|
|
FooReady = true;
|
|
|
|
};
|
2018-06-17 18:59:53 +02:00
|
|
|
|
2018-10-23 22:54:43 +02:00
|
|
|
ES.lookup({&JD}, {Foo}, std::move(OnResolution), std::move(OnReady),
|
2018-07-21 00:22:19 +02:00
|
|
|
NoDependenciesToRegister);
|
2018-06-17 18:59:53 +02:00
|
|
|
|
|
|
|
FooR->resolve({{Foo, FooSym}});
|
2018-08-18 04:06:18 +02:00
|
|
|
FooR->emit();
|
2018-06-17 18:59:53 +02:00
|
|
|
|
|
|
|
EXPECT_TRUE(FooReady)
|
|
|
|
<< "Self-dependency prevented symbol from being marked ready";
|
|
|
|
}
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestCircularDependenceInOneJITDylib) {
|
|
|
|
// Test that a circular symbol dependency between three symbols in a JITDylib
|
|
|
|
// does not prevent any symbol from becoming 'ready' once all symbols are
|
2018-08-18 04:06:18 +02:00
|
|
|
// emitted.
|
2018-05-17 00:24:30 +02:00
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
// Create three MaterializationResponsibility objects: one for each of Foo,
|
|
|
|
// Bar and Baz. These are optional because MaterializationResponsibility
|
|
|
|
// does not have a default constructor).
|
2018-05-17 00:24:30 +02:00
|
|
|
Optional<MaterializationResponsibility> FooR;
|
|
|
|
Optional<MaterializationResponsibility> BarR;
|
|
|
|
Optional<MaterializationResponsibility> BazR;
|
|
|
|
|
|
|
|
// Create a MaterializationUnit for each symbol that moves the
|
|
|
|
// MaterializationResponsibility into one of the locals above.
|
|
|
|
auto FooMU = llvm::make_unique<SimpleMaterializationUnit>(
|
2018-07-11 05:09:36 +02:00
|
|
|
SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
|
2018-05-17 00:24:30 +02:00
|
|
|
[&](MaterializationResponsibility R) { FooR.emplace(std::move(R)); });
|
|
|
|
|
|
|
|
auto BarMU = llvm::make_unique<SimpleMaterializationUnit>(
|
2018-07-11 05:09:36 +02:00
|
|
|
SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
|
2018-05-17 00:24:30 +02:00
|
|
|
[&](MaterializationResponsibility R) { BarR.emplace(std::move(R)); });
|
|
|
|
|
|
|
|
auto BazMU = llvm::make_unique<SimpleMaterializationUnit>(
|
2018-07-11 05:09:36 +02:00
|
|
|
SymbolFlagsMap({{Baz, BazSym.getFlags()}}),
|
2018-05-17 00:24:30 +02:00
|
|
|
[&](MaterializationResponsibility R) { BazR.emplace(std::move(R)); });
|
|
|
|
|
|
|
|
// Define the symbols.
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(FooMU));
|
|
|
|
cantFail(JD.define(BarMU));
|
|
|
|
cantFail(JD.define(BazMU));
|
2018-05-17 00:24:30 +02:00
|
|
|
|
|
|
|
// Query each of the symbols to trigger materialization.
|
|
|
|
bool FooResolved = false;
|
|
|
|
bool FooReady = false;
|
2018-07-21 00:22:19 +02:00
|
|
|
|
|
|
|
auto OnFooResolution = [&](Expected<SymbolMap> Result) {
|
|
|
|
cantFail(std::move(Result));
|
|
|
|
FooResolved = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto OnFooReady = [&](Error Err) {
|
|
|
|
cantFail(std::move(Err));
|
|
|
|
FooReady = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Issue a lookup for Foo. Use NoDependenciesToRegister: We're going to add
|
|
|
|
// the dependencies manually below.
|
2018-10-23 22:54:43 +02:00
|
|
|
ES.lookup({&JD}, {Foo}, std::move(OnFooResolution), std::move(OnFooReady),
|
|
|
|
NoDependenciesToRegister);
|
2018-05-17 00:24:30 +02:00
|
|
|
|
|
|
|
bool BarResolved = false;
|
|
|
|
bool BarReady = false;
|
2018-07-21 00:22:19 +02:00
|
|
|
auto OnBarResolution = [&](Expected<SymbolMap> Result) {
|
|
|
|
cantFail(std::move(Result));
|
|
|
|
BarResolved = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto OnBarReady = [&](Error Err) {
|
|
|
|
cantFail(std::move(Err));
|
|
|
|
BarReady = true;
|
|
|
|
};
|
|
|
|
|
2018-10-23 22:54:43 +02:00
|
|
|
ES.lookup({&JD}, {Bar}, std::move(OnBarResolution), std::move(OnBarReady),
|
|
|
|
NoDependenciesToRegister);
|
2018-05-17 00:24:30 +02:00
|
|
|
|
|
|
|
bool BazResolved = false;
|
|
|
|
bool BazReady = false;
|
2018-07-21 00:22:19 +02:00
|
|
|
|
|
|
|
auto OnBazResolution = [&](Expected<SymbolMap> Result) {
|
|
|
|
cantFail(std::move(Result));
|
|
|
|
BazResolved = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto OnBazReady = [&](Error Err) {
|
|
|
|
cantFail(std::move(Err));
|
|
|
|
BazReady = true;
|
|
|
|
};
|
|
|
|
|
2018-10-23 22:54:43 +02:00
|
|
|
ES.lookup({&JD}, {Baz}, std::move(OnBazResolution), std::move(OnBazReady),
|
|
|
|
NoDependenciesToRegister);
|
2018-05-17 00:24:30 +02:00
|
|
|
|
2018-06-14 17:32:59 +02:00
|
|
|
// Add a circular dependency: Foo -> Bar, Bar -> Baz, Baz -> Foo.
|
2018-08-17 23:18:18 +02:00
|
|
|
FooR->addDependenciesForAll({{&JD, SymbolNameSet({Bar})}});
|
|
|
|
BarR->addDependenciesForAll({{&JD, SymbolNameSet({Baz})}});
|
|
|
|
BazR->addDependenciesForAll({{&JD, SymbolNameSet({Foo})}});
|
2018-06-14 17:32:59 +02:00
|
|
|
|
|
|
|
// Add self-dependencies for good measure. This tests that the implementation
|
|
|
|
// of addDependencies filters these out.
|
2018-08-17 23:18:18 +02:00
|
|
|
FooR->addDependenciesForAll({{&JD, SymbolNameSet({Foo})}});
|
|
|
|
BarR->addDependenciesForAll({{&JD, SymbolNameSet({Bar})}});
|
|
|
|
BazR->addDependenciesForAll({{&JD, SymbolNameSet({Baz})}});
|
2018-05-17 00:24:30 +02:00
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
// Check that nothing has been resolved yet.
|
2018-05-17 00:24:30 +02:00
|
|
|
EXPECT_FALSE(FooResolved) << "\"Foo\" should not be resolved yet";
|
|
|
|
EXPECT_FALSE(BarResolved) << "\"Bar\" should not be resolved yet";
|
|
|
|
EXPECT_FALSE(BazResolved) << "\"Baz\" should not be resolved yet";
|
|
|
|
|
2018-08-18 04:06:18 +02:00
|
|
|
// Resolve the symbols (but do not emit them).
|
2018-05-17 00:24:30 +02:00
|
|
|
FooR->resolve({{Foo, FooSym}});
|
|
|
|
BarR->resolve({{Bar, BarSym}});
|
|
|
|
BazR->resolve({{Baz, BazSym}});
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
// Verify that the symbols have been resolved, but are not ready yet.
|
2018-05-17 00:24:30 +02:00
|
|
|
EXPECT_TRUE(FooResolved) << "\"Foo\" should be resolved now";
|
|
|
|
EXPECT_TRUE(BarResolved) << "\"Bar\" should be resolved now";
|
|
|
|
EXPECT_TRUE(BazResolved) << "\"Baz\" should be resolved now";
|
|
|
|
|
|
|
|
EXPECT_FALSE(FooReady) << "\"Foo\" should not be ready yet";
|
|
|
|
EXPECT_FALSE(BarReady) << "\"Bar\" should not be ready yet";
|
|
|
|
EXPECT_FALSE(BazReady) << "\"Baz\" should not be ready yet";
|
|
|
|
|
2018-08-18 04:06:18 +02:00
|
|
|
// Emit two of the symbols.
|
|
|
|
FooR->emit();
|
|
|
|
BarR->emit();
|
2018-05-17 00:24:30 +02:00
|
|
|
|
|
|
|
// Verify that nothing is ready until the circular dependence is resolved.
|
|
|
|
EXPECT_FALSE(FooReady) << "\"Foo\" still should not be ready";
|
|
|
|
EXPECT_FALSE(BarReady) << "\"Bar\" still should not be ready";
|
|
|
|
EXPECT_FALSE(BazReady) << "\"Baz\" still should not be ready";
|
|
|
|
|
2018-08-18 04:06:18 +02:00
|
|
|
// Emit the last symbol.
|
|
|
|
BazR->emit();
|
2018-05-17 00:24:30 +02:00
|
|
|
|
|
|
|
// Verify that everything becomes ready once the circular dependence resolved.
|
|
|
|
EXPECT_TRUE(FooReady) << "\"Foo\" should be ready now";
|
|
|
|
EXPECT_TRUE(BarReady) << "\"Bar\" should be ready now";
|
|
|
|
EXPECT_TRUE(BazReady) << "\"Baz\" should be ready now";
|
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, DropMaterializerWhenEmpty) {
|
2018-04-12 20:35:08 +02:00
|
|
|
bool DestructorRun = false;
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
JITSymbolFlags WeakExported(JITSymbolFlags::Exported);
|
|
|
|
WeakExported |= JITSymbolFlags::Weak;
|
|
|
|
|
2018-04-12 20:35:08 +02:00
|
|
|
auto MU = llvm::make_unique<SimpleMaterializationUnit>(
|
2018-07-11 05:09:36 +02:00
|
|
|
SymbolFlagsMap({{Foo, WeakExported}, {Bar, WeakExported}}),
|
2018-04-16 20:05:24 +02:00
|
|
|
[](MaterializationResponsibility R) {
|
2018-04-12 20:35:08 +02:00
|
|
|
llvm_unreachable("Unexpected call to materialize");
|
|
|
|
},
|
2018-08-17 23:18:18 +02:00
|
|
|
[&](const JITDylib &JD, SymbolStringPtr Name) {
|
2018-04-12 20:35:08 +02:00
|
|
|
EXPECT_TRUE(Name == Foo || Name == Bar)
|
|
|
|
<< "Discard of unexpected symbol?";
|
|
|
|
},
|
|
|
|
[&]() { DestructorRun = true; });
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(MU));
|
2018-04-12 20:35:08 +02:00
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));
|
2018-04-12 20:35:08 +02:00
|
|
|
|
|
|
|
EXPECT_FALSE(DestructorRun)
|
|
|
|
<< "MaterializationUnit should not have been destroyed yet";
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(absoluteSymbols({{Bar, BarSym}})));
|
2018-04-12 20:35:08 +02:00
|
|
|
|
|
|
|
EXPECT_TRUE(DestructorRun)
|
|
|
|
<< "MaterializationUnit should have been destroyed";
|
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, AddAndMaterializeLazySymbol) {
|
2018-01-10 01:09:38 +01:00
|
|
|
bool FooMaterialized = false;
|
|
|
|
bool BarDiscarded = false;
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
JITSymbolFlags WeakExported(JITSymbolFlags::Exported);
|
|
|
|
WeakExported |= JITSymbolFlags::Weak;
|
2018-01-10 01:09:38 +01:00
|
|
|
|
2018-03-20 04:49:29 +01:00
|
|
|
auto MU = llvm::make_unique<SimpleMaterializationUnit>(
|
2018-07-11 05:09:36 +02:00
|
|
|
SymbolFlagsMap({{Foo, JITSymbolFlags::Exported}, {Bar, WeakExported}}),
|
2018-04-16 20:05:24 +02:00
|
|
|
[&](MaterializationResponsibility R) {
|
2018-03-20 04:49:29 +01:00
|
|
|
assert(BarDiscarded && "Bar should have been discarded by this point");
|
2018-07-11 05:09:36 +02:00
|
|
|
R.resolve(SymbolMap({{Foo, FooSym}}));
|
2018-08-18 04:06:18 +02:00
|
|
|
R.emit();
|
2018-01-10 01:09:38 +01:00
|
|
|
FooMaterialized = true;
|
|
|
|
},
|
2018-08-17 23:18:18 +02:00
|
|
|
[&](const JITDylib &JD, SymbolStringPtr Name) {
|
2018-01-10 01:09:38 +01:00
|
|
|
EXPECT_EQ(Name, Bar) << "Expected Name to be Bar";
|
|
|
|
BarDiscarded = true;
|
|
|
|
});
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(MU));
|
|
|
|
cantFail(JD.define(absoluteSymbols({{Bar, BarSym}})));
|
2018-01-10 01:09:38 +01:00
|
|
|
|
|
|
|
SymbolNameSet Names({Foo});
|
|
|
|
|
|
|
|
bool OnResolutionRun = false;
|
|
|
|
bool OnReadyRun = false;
|
|
|
|
|
2018-07-21 00:22:19 +02:00
|
|
|
auto OnResolution = [&](Expected<SymbolMap> Result) {
|
|
|
|
EXPECT_TRUE(!!Result) << "Resolution unexpectedly returned error";
|
|
|
|
auto I = Result->find(Foo);
|
|
|
|
EXPECT_NE(I, Result->end()) << "Could not find symbol definition";
|
|
|
|
EXPECT_EQ(I->second.getAddress(), FooSym.getAddress())
|
|
|
|
<< "Resolution returned incorrect result";
|
|
|
|
OnResolutionRun = true;
|
|
|
|
};
|
2018-01-10 01:09:38 +01:00
|
|
|
|
|
|
|
auto OnReady = [&](Error Err) {
|
|
|
|
cantFail(std::move(Err));
|
|
|
|
OnReadyRun = true;
|
|
|
|
};
|
|
|
|
|
2018-10-23 22:54:43 +02:00
|
|
|
ES.lookup({&JD}, Names, std::move(OnResolution), std::move(OnReady),
|
2018-07-21 00:22:19 +02:00
|
|
|
NoDependenciesToRegister);
|
2018-01-10 01:09:38 +01:00
|
|
|
|
|
|
|
EXPECT_TRUE(FooMaterialized) << "Foo was not materialized";
|
|
|
|
EXPECT_TRUE(BarDiscarded) << "Bar was not discarded";
|
|
|
|
EXPECT_TRUE(OnResolutionRun) << "OnResolutionCallback was not run";
|
|
|
|
EXPECT_TRUE(OnReadyRun) << "OnReady was not run";
|
|
|
|
}
|
|
|
|
|
2018-08-28 23:18:05 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestBasicWeakSymbolMaterialization) {
|
|
|
|
// Test that weak symbols are materialized correctly when we look them up.
|
2018-09-02 03:28:26 +02:00
|
|
|
BarSym.setFlags(BarSym.getFlags() | JITSymbolFlags::Weak);
|
2018-08-28 23:18:05 +02:00
|
|
|
|
|
|
|
bool BarMaterialized = false;
|
|
|
|
auto MU1 = llvm::make_unique<SimpleMaterializationUnit>(
|
|
|
|
SymbolFlagsMap({{Foo, FooSym.getFlags()}, {Bar, BarSym.getFlags()}}),
|
|
|
|
[&](MaterializationResponsibility R) {
|
|
|
|
R.resolve(SymbolMap({{Foo, FooSym}, {Bar, BarSym}})), R.emit();
|
|
|
|
BarMaterialized = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
bool DuplicateBarDiscarded = false;
|
|
|
|
auto MU2 = llvm::make_unique<SimpleMaterializationUnit>(
|
|
|
|
SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
|
|
|
|
[&](MaterializationResponsibility R) {
|
|
|
|
ADD_FAILURE() << "Attempt to materialize Bar from the wrong unit";
|
|
|
|
R.failMaterialization();
|
|
|
|
},
|
|
|
|
[&](const JITDylib &JD, SymbolStringPtr Name) {
|
|
|
|
EXPECT_EQ(Name, Bar) << "Expected \"Bar\" to be discarded";
|
|
|
|
DuplicateBarDiscarded = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
cantFail(JD.define(MU1));
|
|
|
|
cantFail(JD.define(MU2));
|
|
|
|
|
|
|
|
bool OnResolvedRun = false;
|
|
|
|
bool OnReadyRun = false;
|
|
|
|
|
|
|
|
auto OnResolution = [&](Expected<SymbolMap> Result) {
|
|
|
|
cantFail(std::move(Result));
|
|
|
|
OnResolvedRun = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto OnReady = [&](Error Err) {
|
|
|
|
cantFail(std::move(Err));
|
|
|
|
OnReadyRun = true;
|
|
|
|
};
|
|
|
|
|
2018-10-23 22:54:43 +02:00
|
|
|
ES.lookup({&JD}, {Bar}, std::move(OnResolution), std::move(OnReady),
|
2018-08-28 23:18:05 +02:00
|
|
|
NoDependenciesToRegister);
|
|
|
|
|
|
|
|
EXPECT_TRUE(OnResolvedRun) << "OnResolved not run";
|
|
|
|
EXPECT_TRUE(OnReadyRun) << "OnReady not run";
|
|
|
|
EXPECT_TRUE(BarMaterialized) << "Bar was not materialized at all";
|
|
|
|
EXPECT_TRUE(DuplicateBarDiscarded)
|
|
|
|
<< "Duplicate bar definition not discarded";
|
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, DefineMaterializingSymbol) {
|
2018-05-17 00:24:30 +02:00
|
|
|
bool ExpectNoMoreMaterialization = false;
|
|
|
|
ES.setDispatchMaterialization(
|
2018-08-17 23:18:18 +02:00
|
|
|
[&](JITDylib &JD, std::unique_ptr<MaterializationUnit> MU) {
|
2018-05-17 00:24:30 +02:00
|
|
|
if (ExpectNoMoreMaterialization)
|
|
|
|
ADD_FAILURE() << "Unexpected materialization";
|
2018-08-17 23:18:18 +02:00
|
|
|
MU->doMaterialize(JD);
|
2018-05-17 00:24:30 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
auto MU = llvm::make_unique<SimpleMaterializationUnit>(
|
|
|
|
SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
|
|
|
|
[&](MaterializationResponsibility R) {
|
|
|
|
cantFail(
|
|
|
|
R.defineMaterializing(SymbolFlagsMap({{Bar, BarSym.getFlags()}})));
|
|
|
|
R.resolve(SymbolMap({{Foo, FooSym}, {Bar, BarSym}}));
|
2018-08-18 04:06:18 +02:00
|
|
|
R.emit();
|
2018-05-17 00:24:30 +02:00
|
|
|
});
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(MU));
|
2018-10-23 22:54:43 +02:00
|
|
|
cantFail(ES.lookup({&JD}, Foo));
|
2018-05-17 00:24:30 +02:00
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
// Assert that materialization is complete by now.
|
2018-05-17 00:24:30 +02:00
|
|
|
ExpectNoMoreMaterialization = true;
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
// Look up bar to verify that no further materialization happens.
|
2018-10-23 22:54:43 +02:00
|
|
|
auto BarResult = cantFail(ES.lookup({&JD}, Bar));
|
2018-07-11 05:09:36 +02:00
|
|
|
EXPECT_EQ(BarResult.getAddress(), BarSym.getAddress())
|
|
|
|
<< "Expected Bar == BarSym";
|
2018-05-17 00:24:30 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 07:07:54 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, GeneratorTest) {
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));
|
2018-06-12 22:43:18 +02:00
|
|
|
|
2018-10-15 07:07:54 +02:00
|
|
|
JD.setGenerator([&](JITDylib &JD2, const SymbolNameSet &Names) {
|
|
|
|
cantFail(JD2.define(absoluteSymbols({{Bar, BarSym}})));
|
|
|
|
return SymbolNameSet({Bar});
|
|
|
|
});
|
2018-06-12 22:43:18 +02:00
|
|
|
|
2018-10-23 22:54:43 +02:00
|
|
|
auto Result = cantFail(ES.lookup({&JD}, {Foo, Bar}));
|
2018-06-12 22:43:18 +02:00
|
|
|
|
|
|
|
EXPECT_EQ(Result.count(Bar), 1U) << "Expected to find fallback def for 'bar'";
|
2018-07-11 05:09:36 +02:00
|
|
|
EXPECT_EQ(Result[Bar].getAddress(), BarSym.getAddress())
|
|
|
|
<< "Expected fallback def for Bar to be equal to BarSym";
|
2018-06-12 22:43:18 +02:00
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, FailResolution) {
|
2018-04-12 20:35:08 +02:00
|
|
|
auto MU = llvm::make_unique<SimpleMaterializationUnit>(
|
2018-10-13 23:53:40 +02:00
|
|
|
SymbolFlagsMap({{Foo, JITSymbolFlags::Exported | JITSymbolFlags::Weak},
|
|
|
|
{Bar, JITSymbolFlags::Exported | JITSymbolFlags::Weak}}),
|
2018-05-17 22:48:58 +02:00
|
|
|
[&](MaterializationResponsibility R) { R.failMaterialization(); });
|
2018-04-12 20:35:08 +02:00
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(MU));
|
2018-04-12 20:35:08 +02:00
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
SymbolNameSet Names({Foo, Bar});
|
2018-10-23 22:54:43 +02:00
|
|
|
auto Result = ES.lookup({&JD}, Names);
|
2018-07-11 05:09:36 +02:00
|
|
|
|
|
|
|
EXPECT_FALSE(!!Result) << "Expected failure";
|
|
|
|
if (!Result) {
|
|
|
|
handleAllErrors(Result.takeError(),
|
|
|
|
[&](FailedToMaterialize &F) {
|
|
|
|
EXPECT_EQ(F.getSymbols(), Names)
|
|
|
|
<< "Expected to fail on symbols in Names";
|
|
|
|
},
|
|
|
|
[](ErrorInfoBase &EIB) {
|
|
|
|
std::string ErrMsg;
|
|
|
|
{
|
|
|
|
raw_string_ostream ErrOut(ErrMsg);
|
|
|
|
EIB.log(ErrOut);
|
|
|
|
}
|
|
|
|
ADD_FAILURE()
|
|
|
|
<< "Expected a FailedToResolve error. Got:\n"
|
|
|
|
<< ErrMsg;
|
|
|
|
});
|
|
|
|
}
|
2018-04-12 20:35:08 +02:00
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestLookupWithUnthreadedMaterialization) {
|
2018-03-20 04:49:29 +01:00
|
|
|
auto MU = llvm::make_unique<SimpleMaterializationUnit>(
|
2018-05-17 00:24:30 +02:00
|
|
|
SymbolFlagsMap({{Foo, JITSymbolFlags::Exported}}),
|
2018-04-16 20:05:24 +02:00
|
|
|
[&](MaterializationResponsibility R) {
|
|
|
|
R.resolve({{Foo, FooSym}});
|
2018-08-18 04:06:18 +02:00
|
|
|
R.emit();
|
2018-03-14 05:18:04 +01:00
|
|
|
});
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(MU));
|
2018-03-14 05:18:04 +01:00
|
|
|
|
2018-10-23 22:54:43 +02:00
|
|
|
auto FooLookupResult = cantFail(ES.lookup({&JD}, Foo));
|
2018-03-14 05:18:04 +01:00
|
|
|
|
|
|
|
EXPECT_EQ(FooLookupResult.getAddress(), FooSym.getAddress())
|
|
|
|
<< "lookup returned an incorrect address";
|
|
|
|
EXPECT_EQ(FooLookupResult.getFlags(), FooSym.getFlags())
|
|
|
|
<< "lookup returned incorrect flags";
|
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestLookupWithThreadedMaterialization) {
|
2018-03-14 05:18:04 +01:00
|
|
|
#if LLVM_ENABLE_THREADS
|
|
|
|
|
2018-05-17 00:24:30 +02:00
|
|
|
std::thread MaterializationThread;
|
|
|
|
ES.setDispatchMaterialization(
|
2018-08-17 23:18:18 +02:00
|
|
|
[&](JITDylib &JD, std::unique_ptr<MaterializationUnit> MU) {
|
2018-05-17 00:24:30 +02:00
|
|
|
auto SharedMU = std::shared_ptr<MaterializationUnit>(std::move(MU));
|
|
|
|
MaterializationThread =
|
2018-08-17 23:18:18 +02:00
|
|
|
std::thread([SharedMU, &JD]() { SharedMU->doMaterialize(JD); });
|
2018-03-14 05:18:04 +01:00
|
|
|
});
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(absoluteSymbols({{Foo, FooSym}})));
|
2018-03-14 05:18:04 +01:00
|
|
|
|
2018-10-23 22:54:43 +02:00
|
|
|
auto FooLookupResult = cantFail(ES.lookup({&JD}, Foo));
|
2018-03-14 05:18:04 +01:00
|
|
|
|
|
|
|
EXPECT_EQ(FooLookupResult.getAddress(), FooSym.getAddress())
|
|
|
|
<< "lookup returned an incorrect address";
|
|
|
|
EXPECT_EQ(FooLookupResult.getFlags(), FooSym.getFlags())
|
|
|
|
<< "lookup returned incorrect flags";
|
2018-03-23 11:14:19 +01:00
|
|
|
MaterializationThread.join();
|
2018-03-14 05:18:04 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestGetRequestedSymbolsAndReplace) {
|
|
|
|
// Test that GetRequestedSymbols returns the set of symbols that currently
|
|
|
|
// have pending queries, and test that MaterializationResponsibility's
|
2018-08-17 23:18:18 +02:00
|
|
|
// replace method can be used to return definitions to the JITDylib in a new
|
2018-07-11 05:09:36 +02:00
|
|
|
// MaterializationUnit.
|
2018-05-31 21:29:03 +02:00
|
|
|
SymbolNameSet Names({Foo, Bar});
|
|
|
|
|
|
|
|
bool FooMaterialized = false;
|
|
|
|
bool BarMaterialized = false;
|
|
|
|
|
|
|
|
auto MU = llvm::make_unique<SimpleMaterializationUnit>(
|
|
|
|
SymbolFlagsMap({{Foo, FooSym.getFlags()}, {Bar, BarSym.getFlags()}}),
|
|
|
|
[&](MaterializationResponsibility R) {
|
|
|
|
auto Requested = R.getRequestedSymbols();
|
|
|
|
EXPECT_EQ(Requested.size(), 1U) << "Expected one symbol requested";
|
|
|
|
EXPECT_EQ(*Requested.begin(), Foo) << "Expected \"Foo\" requested";
|
|
|
|
|
|
|
|
auto NewMU = llvm::make_unique<SimpleMaterializationUnit>(
|
|
|
|
SymbolFlagsMap({{Bar, BarSym.getFlags()}}),
|
|
|
|
[&](MaterializationResponsibility R2) {
|
|
|
|
R2.resolve(SymbolMap({{Bar, BarSym}}));
|
2018-08-18 04:06:18 +02:00
|
|
|
R2.emit();
|
2018-05-31 21:29:03 +02:00
|
|
|
BarMaterialized = true;
|
|
|
|
});
|
|
|
|
|
2018-07-09 22:54:36 +02:00
|
|
|
R.replace(std::move(NewMU));
|
2018-05-31 21:29:03 +02:00
|
|
|
|
|
|
|
R.resolve(SymbolMap({{Foo, FooSym}}));
|
2018-08-18 04:06:18 +02:00
|
|
|
R.emit();
|
2018-05-31 21:29:03 +02:00
|
|
|
|
|
|
|
FooMaterialized = true;
|
|
|
|
});
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(MU));
|
2018-05-31 21:29:03 +02:00
|
|
|
|
|
|
|
EXPECT_FALSE(FooMaterialized) << "Foo should not be materialized yet";
|
|
|
|
EXPECT_FALSE(BarMaterialized) << "Bar should not be materialized yet";
|
|
|
|
|
2018-10-23 22:54:43 +02:00
|
|
|
auto FooSymResult = cantFail(ES.lookup({&JD}, Foo));
|
2018-05-31 21:29:03 +02:00
|
|
|
EXPECT_EQ(FooSymResult.getAddress(), FooSym.getAddress())
|
|
|
|
<< "Address mismatch for Foo";
|
|
|
|
|
|
|
|
EXPECT_TRUE(FooMaterialized) << "Foo should be materialized now";
|
|
|
|
EXPECT_FALSE(BarMaterialized) << "Bar still should not be materialized";
|
|
|
|
|
2018-10-23 22:54:43 +02:00
|
|
|
auto BarSymResult = cantFail(ES.lookup({&JD}, Bar));
|
2018-05-31 21:29:03 +02:00
|
|
|
EXPECT_EQ(BarSymResult.getAddress(), BarSym.getAddress())
|
|
|
|
<< "Address mismatch for Bar";
|
|
|
|
EXPECT_TRUE(BarMaterialized) << "Bar should be materialized now";
|
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestMaterializationResponsibilityDelegation) {
|
2018-07-09 22:54:36 +02:00
|
|
|
auto MU = llvm::make_unique<SimpleMaterializationUnit>(
|
|
|
|
SymbolFlagsMap({{Foo, FooSym.getFlags()}, {Bar, BarSym.getFlags()}}),
|
|
|
|
[&](MaterializationResponsibility R) {
|
|
|
|
auto R2 = R.delegate({Bar});
|
|
|
|
|
|
|
|
R.resolve({{Foo, FooSym}});
|
2018-08-18 04:06:18 +02:00
|
|
|
R.emit();
|
2018-07-09 22:54:36 +02:00
|
|
|
R2.resolve({{Bar, BarSym}});
|
2018-08-18 04:06:18 +02:00
|
|
|
R2.emit();
|
2018-07-09 22:54:36 +02:00
|
|
|
});
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(MU));
|
2018-07-09 22:54:36 +02:00
|
|
|
|
2018-10-23 22:54:43 +02:00
|
|
|
auto Result = ES.lookup({&JD}, {Foo, Bar});
|
2018-07-09 22:54:36 +02:00
|
|
|
|
|
|
|
EXPECT_TRUE(!!Result) << "Result should be a success value";
|
|
|
|
EXPECT_EQ(Result->count(Foo), 1U) << "\"Foo\" entry missing";
|
|
|
|
EXPECT_EQ(Result->count(Bar), 1U) << "\"Bar\" entry missing";
|
|
|
|
EXPECT_EQ((*Result)[Foo].getAddress(), FooSym.getAddress())
|
|
|
|
<< "Address mismatch for \"Foo\"";
|
|
|
|
EXPECT_EQ((*Result)[Bar].getAddress(), BarSym.getAddress())
|
|
|
|
<< "Address mismatch for \"Bar\"";
|
|
|
|
}
|
|
|
|
|
2018-07-11 05:09:36 +02:00
|
|
|
TEST_F(CoreAPIsStandardTest, TestMaterializeWeakSymbol) {
|
2018-06-14 23:16:29 +02:00
|
|
|
// Confirm that once a weak definition is selected for materialization it is
|
|
|
|
// treated as strong.
|
2018-07-11 05:09:36 +02:00
|
|
|
JITSymbolFlags WeakExported = JITSymbolFlags::Exported;
|
|
|
|
WeakExported &= JITSymbolFlags::Weak;
|
2018-06-14 23:16:29 +02:00
|
|
|
|
|
|
|
std::unique_ptr<MaterializationResponsibility> FooResponsibility;
|
|
|
|
auto MU = llvm::make_unique<SimpleMaterializationUnit>(
|
2018-07-11 05:09:36 +02:00
|
|
|
SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
|
|
|
|
[&](MaterializationResponsibility R) {
|
2018-06-14 23:16:29 +02:00
|
|
|
FooResponsibility =
|
|
|
|
llvm::make_unique<MaterializationResponsibility>(std::move(R));
|
|
|
|
});
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
cantFail(JD.define(MU));
|
2018-07-21 00:22:19 +02:00
|
|
|
auto OnResolution = [](Expected<SymbolMap> Result) {
|
|
|
|
cantFail(std::move(Result));
|
|
|
|
};
|
|
|
|
|
|
|
|
auto OnReady = [](Error Err) { cantFail(std::move(Err)); };
|
|
|
|
|
2018-10-23 22:54:43 +02:00
|
|
|
ES.lookup({&JD}, {Foo}, std::move(OnResolution), std::move(OnReady),
|
2018-07-21 00:22:19 +02:00
|
|
|
NoDependenciesToRegister);
|
2018-06-14 23:16:29 +02:00
|
|
|
|
|
|
|
auto MU2 = llvm::make_unique<SimpleMaterializationUnit>(
|
|
|
|
SymbolFlagsMap({{Foo, JITSymbolFlags::Exported}}),
|
|
|
|
[](MaterializationResponsibility R) {
|
|
|
|
llvm_unreachable("This unit should never be materialized");
|
|
|
|
});
|
|
|
|
|
2018-08-17 23:18:18 +02:00
|
|
|
auto Err = JD.define(MU2);
|
2018-06-14 23:16:29 +02:00
|
|
|
EXPECT_TRUE(!!Err) << "Expected failure value";
|
|
|
|
EXPECT_TRUE(Err.isA<DuplicateDefinition>())
|
|
|
|
<< "Expected a duplicate definition error";
|
|
|
|
consumeError(std::move(Err));
|
|
|
|
|
|
|
|
FooResponsibility->resolve(SymbolMap({{Foo, FooSym}}));
|
2018-08-18 04:06:18 +02:00
|
|
|
FooResponsibility->emit();
|
2018-06-14 23:16:29 +02:00
|
|
|
}
|
|
|
|
|
2018-01-10 01:09:38 +01:00
|
|
|
} // namespace
|