Skip to content

[ORC] Fix transfer to unknown ResourceTrackers #114063

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions llvm/lib/ExecutionEngine/Orc/ObjectLinkingLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,16 +701,15 @@ Error ObjectLinkingLayer::handleRemoveResources(JITDylib &JD, ResourceKey K) {
void ObjectLinkingLayer::handleTransferResources(JITDylib &JD,
ResourceKey DstKey,
ResourceKey SrcKey) {
auto I = Allocs.find(SrcKey);
if (I != Allocs.end()) {
auto &SrcAllocs = I->second;
if (Allocs.contains(SrcKey)) {
// DstKey may not be in the DenseMap yet, so the following line may resize
// the container and invalidate iterators and value references.
auto &DstAllocs = Allocs[DstKey];
auto &SrcAllocs = Allocs[SrcKey];
DstAllocs.reserve(DstAllocs.size() + SrcAllocs.size());
for (auto &Alloc : SrcAllocs)
DstAllocs.push_back(std::move(Alloc));

// Erase SrcKey entry using value rather than iterator I: I may have been
// invalidated when we looked up DstKey.
Allocs.erase(SrcKey);
}

Expand Down
9 changes: 4 additions & 5 deletions llvm/lib/ExecutionEngine/Orc/RTDyldObjectLinkingLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,16 +430,15 @@ Error RTDyldObjectLinkingLayer::handleRemoveResources(JITDylib &JD,
void RTDyldObjectLinkingLayer::handleTransferResources(JITDylib &JD,
ResourceKey DstKey,
ResourceKey SrcKey) {
auto I = MemMgrs.find(SrcKey);
if (I != MemMgrs.end()) {
auto &SrcMemMgrs = I->second;
if (MemMgrs.contains(SrcKey)) {
// DstKey may not be in the DenseMap yet, so the following line may resize
// the container and invalidate iterators and value references.
auto &DstMemMgrs = MemMgrs[DstKey];
auto &SrcMemMgrs = MemMgrs[SrcKey];
DstMemMgrs.reserve(DstMemMgrs.size() + SrcMemMgrs.size());
for (auto &MemMgr : SrcMemMgrs)
DstMemMgrs.push_back(std::move(MemMgr));

// Erase SrcKey entry using value rather than iterator I: I may have been
// invalidated when we looked up DstKey.
MemMgrs.erase(SrcKey);
}
}
Expand Down
30 changes: 30 additions & 0 deletions llvm/unittests/ExecutionEngine/Orc/ObjectLinkingLayerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,36 @@ TEST_F(ObjectLinkingLayerTest, AddLinkGraph) {
EXPECT_THAT_EXPECTED(ES.lookup(&JD, "_X"), Succeeded());
}

TEST_F(ObjectLinkingLayerTest, ResourceTracker) {
// This test transfers allocations to previously unknown ResourceTrackers,
// while increasing the number of trackers in the ObjectLinkingLayer, which
// may invalidate some iterators internally.
std::vector<ResourceTrackerSP> Trackers;
for (unsigned I = 0; I < 64; I++) {
auto G = std::make_unique<LinkGraph>("foo", Triple("x86_64-apple-darwin"),
8, llvm::endianness::little,
x86_64::getEdgeKindName);

auto &Sec1 = G->createSection("__data", MemProt::Read | MemProt::Write);
auto &B1 = G->createContentBlock(Sec1, BlockContent,
orc::ExecutorAddr(0x1000), 8, 0);
llvm::SmallString<0> SymbolName;
SymbolName += "_X";
SymbolName += std::to_string(I);
G->addDefinedSymbol(B1, 4, SymbolName, 4, Linkage::Strong, Scope::Default,
false, false);

auto RT1 = JD.createResourceTracker();
EXPECT_THAT_ERROR(ObjLinkingLayer.add(RT1, std::move(G)), Succeeded());
EXPECT_THAT_EXPECTED(ES.lookup(&JD, SymbolName), Succeeded());

auto RT2 = JD.createResourceTracker();
RT1->transferTo(*RT2);

Trackers.push_back(RT2);
}
}

TEST_F(ObjectLinkingLayerTest, ClaimLateDefinedWeakSymbols) {
// Check that claiming weak symbols works as expected.
//
Expand Down
Loading