Skip to content

Commit f710b04

Browse files
committed
[ORC] Fail early in ExecutionSession::registerJITDispatchHandlers.
Check that we're not reusing any handler tag addresses before installing any handlers. This ensures that either all of the handlers are installed*, or none of them are, simplifying error recovery. * Ignoring handlers whose tags couldn't be resolved at all: these were never installed.
1 parent 2918a47 commit f710b04

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

llvm/lib/ExecutionEngine/Orc/Core.cpp

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,31 +1878,39 @@ ExecutionSession::lookup(ArrayRef<JITDylib *> SearchOrder, StringRef Name,
18781878
Error ExecutionSession::registerJITDispatchHandlers(
18791879
JITDylib &JD, JITDispatchHandlerAssociationMap WFs) {
18801880

1881-
auto TagAddrs = lookup({{&JD, JITDylibLookupFlags::MatchAllSymbols}},
1882-
SymbolLookupSet::fromMapKeys(
1883-
WFs, SymbolLookupFlags::WeaklyReferencedSymbol));
1884-
if (!TagAddrs)
1885-
return TagAddrs.takeError();
1881+
auto TagSyms = lookup({{&JD, JITDylibLookupFlags::MatchAllSymbols}},
1882+
SymbolLookupSet::fromMapKeys(
1883+
WFs, SymbolLookupFlags::WeaklyReferencedSymbol));
1884+
if (!TagSyms)
1885+
return TagSyms.takeError();
18861886

18871887
// Associate tag addresses with implementations.
18881888
std::lock_guard<std::mutex> Lock(JITDispatchHandlersMutex);
1889-
for (auto &KV : *TagAddrs) {
1890-
auto TagAddr = KV.second.getAddress();
1889+
1890+
// Check that no tags are being overwritten.
1891+
for (auto &[TagName, TagSym] : *TagSyms) {
1892+
auto TagAddr = TagSym.getAddress();
18911893
if (JITDispatchHandlers.count(TagAddr))
1892-
return make_error<StringError>("Tag " + formatv("{0:x16}", TagAddr) +
1893-
" (for " + *KV.first +
1894+
return make_error<StringError>("Tag " + formatv("{0:x}", TagAddr) +
1895+
" (for " + *TagName +
18941896
") already registered",
18951897
inconvertibleErrorCode());
1896-
auto I = WFs.find(KV.first);
1898+
}
1899+
1900+
// At this point we're guaranteed to succeed. Install the handlers.
1901+
for (auto &[TagName, TagSym] : *TagSyms) {
1902+
auto TagAddr = TagSym.getAddress();
1903+
auto I = WFs.find(TagName);
18971904
assert(I != WFs.end() && I->second &&
18981905
"JITDispatchHandler implementation missing");
1899-
JITDispatchHandlers[KV.second.getAddress()] =
1906+
JITDispatchHandlers[TagAddr] =
19001907
std::make_shared<JITDispatchHandlerFunction>(std::move(I->second));
19011908
LLVM_DEBUG({
1902-
dbgs() << "Associated function tag \"" << *KV.first << "\" ("
1903-
<< formatv("{0:x}", KV.second.getAddress()) << ") with handler\n";
1909+
dbgs() << "Associated function tag \"" << *TagName << "\" ("
1910+
<< formatv("{0:x}", TagAddr) << ") with handler\n";
19041911
});
19051912
}
1913+
19061914
return Error::success();
19071915
}
19081916

0 commit comments

Comments
 (0)