Skip to content

Commit 199034e

Browse files
committed
[ORC] In defineMaterializing, error out early if tracker is defunct.
An in-flight materialization may try to claim responsibility for new symbols (via MaterializationResponsibility::defineMaterializing) after the tracker that is associated with the materialization is removed, leaving the tracker defunct. Failure to error out early here could leave the JITDylib in an invalid state, with defineMaterializing associating new symbols with the already-defunct tracker. Erroring out early prevents this.
1 parent f7ad7d1 commit 199034e

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

llvm/include/llvm/ExecutionEngine/Orc/Core.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,9 @@ class JITDylib : public ThreadSafeRefCountedBase<JITDylib>,
12581258
const SymbolStringPtr &DependantName,
12591259
MaterializingInfo &EmittedMI);
12601260

1261-
Expected<SymbolFlagsMap> defineMaterializing(SymbolFlagsMap SymbolFlags);
1261+
Expected<SymbolFlagsMap>
1262+
defineMaterializing(MaterializationResponsibility &FromMR,
1263+
SymbolFlagsMap SymbolFlags);
12621264

12631265
Error replace(MaterializationResponsibility &FromMR,
12641266
std::unique_ptr<MaterializationUnit> MU);

llvm/lib/ExecutionEngine/Orc/Core.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -689,10 +689,13 @@ void JITDylib::removeGenerator(DefinitionGenerator &G) {
689689
}
690690

691691
Expected<SymbolFlagsMap>
692-
JITDylib::defineMaterializing(SymbolFlagsMap SymbolFlags) {
693-
// TODO: Should we bail out early here if MR is defunct?
692+
JITDylib::defineMaterializing(MaterializationResponsibility &FromMR,
693+
SymbolFlagsMap SymbolFlags) {
694694

695695
return ES.runSessionLocked([&]() -> Expected<SymbolFlagsMap> {
696+
if (FromMR.RT->isDefunct())
697+
return make_error<ResourceTrackerDefunct>(FromMR.RT);
698+
696699
std::vector<NonOwningSymbolStringPtr> AddedSyms;
697700
std::vector<NonOwningSymbolStringPtr> RejectedWeakDefs;
698701

@@ -2967,7 +2970,7 @@ Error ExecutionSession::OL_defineMaterializing(
29672970
<< NewSymbolFlags << "\n";
29682971
});
29692972
if (auto AcceptedDefs =
2970-
MR.JD.defineMaterializing(std::move(NewSymbolFlags))) {
2973+
MR.JD.defineMaterializing(MR, std::move(NewSymbolFlags))) {
29712974
// Add all newly accepted symbols to this responsibility object.
29722975
for (auto &KV : *AcceptedDefs)
29732976
MR.SymbolFlags.insert(KV);

llvm/unittests/ExecutionEngine/Orc/CoreAPIsTest.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,40 @@ TEST_F(CoreAPIsStandardTest, FailAfterPartialResolution) {
12881288
EXPECT_TRUE(QueryHandlerRun) << "Query handler never ran";
12891289
}
12901290

1291+
TEST_F(CoreAPIsStandardTest, FailDefineMaterializingDueToDefunctTracker) {
1292+
// Check that a defunct resource tracker causes defineMaterializing to error
1293+
// immediately.
1294+
1295+
std::unique_ptr<MaterializationResponsibility> FooMR;
1296+
auto MU = std::make_unique<SimpleMaterializationUnit>(
1297+
SymbolFlagsMap({{Foo, FooSym.getFlags()}}),
1298+
[&](std::unique_ptr<MaterializationResponsibility> R) {
1299+
FooMR = std::move(R);
1300+
});
1301+
1302+
auto RT = JD.createResourceTracker();
1303+
cantFail(JD.define(std::move(MU), RT));
1304+
1305+
bool OnCompletionRan = false;
1306+
auto OnCompletion = [&](Expected<SymbolMap> Result) {
1307+
EXPECT_THAT_EXPECTED(Result, Failed());
1308+
OnCompletionRan = true;
1309+
};
1310+
1311+
ES.lookup(LookupKind::Static, makeJITDylibSearchOrder(&JD),
1312+
SymbolLookupSet(Foo), SymbolState::Ready, OnCompletion,
1313+
NoDependenciesToRegister);
1314+
1315+
cantFail(RT->remove());
1316+
1317+
EXPECT_THAT_ERROR(FooMR->defineMaterializing(SymbolFlagsMap()), Failed())
1318+
<< "defineMaterializing should have failed due to a defunct tracker";
1319+
1320+
FooMR->failMaterialization();
1321+
1322+
EXPECT_TRUE(OnCompletionRan) << "OnCompletion handler did not run.";
1323+
}
1324+
12911325
TEST_F(CoreAPIsStandardTest, TestLookupWithUnthreadedMaterialization) {
12921326
auto MU = std::make_unique<SimpleMaterializationUnit>(
12931327
SymbolFlagsMap({{Foo, JITSymbolFlags::Exported}}),

0 commit comments

Comments
 (0)