[llvm] cc8f7cd - [ORC][LibraryResolver] Fix ensureFilterBuilt assertion failure and concurrency issue. (#166510)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Nov 5 23:00:25 PST 2025
Author: SahilPatidar
Date: 2025-11-06T12:30:22+05:30
New Revision: cc8f7cd2521c98c53f3cdb053b689543c36671c4
URL: https://github.com/llvm/llvm-project/commit/cc8f7cd2521c98c53f3cdb053b689543c36671c4
DIFF: https://github.com/llvm/llvm-project/commit/cc8f7cd2521c98c53f3cdb053b689543c36671c4.diff
LOG: [ORC][LibraryResolver] Fix ensureFilterBuilt assertion failure and concurrency issue. (#166510)
- Fixed architecture compatibility check.
Previously, we used `sys::getDefaultTriple()`, which caused issues on
build bots
using cross-compilation. We now ensure that the target architecture
where the
shared library (.so) is run or loaded matches the architecture it was
built for.
- Fixed ensureFilterBuilt assertion failure.
- Replaced use of FilteredView with a safer alternative for concurrent
environments.
The old FilteredView approach iterated over shared state without
sufficient
synchronization, which could lead to invalid accesses when libraries
were being
added or removed concurrently.
Added:
Modified:
llvm/include/llvm/ExecutionEngine/Orc/TargetProcess/LibraryResolver.h
llvm/lib/ExecutionEngine/Orc/TargetProcess/LibraryResolver.cpp
llvm/lib/ExecutionEngine/Orc/TargetProcess/LibraryScanner.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ExecutionEngine/Orc/TargetProcess/LibraryResolver.h b/llvm/include/llvm/ExecutionEngine/Orc/TargetProcess/LibraryResolver.h
index 7cc78d4be2792..fc41641fd5cff 100644
--- a/llvm/include/llvm/ExecutionEngine/Orc/TargetProcess/LibraryResolver.h
+++ b/llvm/include/llvm/ExecutionEngine/Orc/TargetProcess/LibraryResolver.h
@@ -211,6 +211,21 @@ class LibraryManager {
return FilteredView(Libraries.begin(), Libraries.end(), S, K);
}
+ using LibraryFilterFn = std::function<bool(const LibraryInfo &)>;
+ void getLibraries(LibState S, PathType K,
+ std::vector<std::shared_ptr<LibraryInfo>> &Outs,
+ LibraryFilterFn Filter = nullptr) const {
+ std::shared_lock<std::shared_mutex> Lock(Mtx);
+ for (const auto &[_, Entry] : Libraries) {
+ const auto &Info = *Entry;
+ if (Info.getKind() != K || Info.getState() != S)
+ continue;
+ if (Filter && !Filter(Info))
+ continue;
+ Outs.push_back(Entry);
+ }
+ }
+
void forEachLibrary(const LibraryVisitor &visitor) const {
std::unique_lock<std::shared_mutex> Lock(Mtx);
for (const auto &[_, entry] : Libraries) {
@@ -220,14 +235,14 @@ class LibraryManager {
}
bool isLoaded(StringRef Path) const {
- std::unique_lock<std::shared_mutex> Lock(Mtx);
+ std::shared_lock<std::shared_mutex> Lock(Mtx);
if (auto It = Libraries.find(Path.str()); It != Libraries.end())
return It->second->getState() == LibState::Loaded;
return false;
}
bool isQueried(StringRef Path) const {
- std::unique_lock<std::shared_mutex> Lock(Mtx);
+ std::shared_lock<std::shared_mutex> Lock(Mtx);
if (auto It = Libraries.find(Path.str()); It != Libraries.end())
return It->second->getState() == LibState::Queried;
return false;
diff --git a/llvm/lib/ExecutionEngine/Orc/TargetProcess/LibraryResolver.cpp b/llvm/lib/ExecutionEngine/Orc/TargetProcess/LibraryResolver.cpp
index 35da82a10306a..7e1d5285463c7 100644
--- a/llvm/lib/ExecutionEngine/Orc/TargetProcess/LibraryResolver.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/TargetProcess/LibraryResolver.cpp
@@ -184,9 +184,9 @@ class SymbolSearchContext {
public:
SymbolSearchContext(SymbolQuery &Q) : Q(Q) {}
- bool hasSearched(LibraryInfo *Lib) const { return Searched.count(Lib); }
+ bool hasSearched(const LibraryInfo *Lib) const { return Searched.count(Lib); }
- void markSearched(LibraryInfo *Lib) { Searched.insert(Lib); }
+ void markSearched(const LibraryInfo *Lib) { Searched.insert(Lib); }
inline bool allResolved() const { return Q.allResolved(); }
@@ -194,7 +194,7 @@ class SymbolSearchContext {
private:
SymbolQuery &Q;
- DenseSet<LibraryInfo *> Searched;
+ DenseSet<const LibraryInfo *> Searched;
};
void LibraryResolver::resolveSymbolsInLibrary(
@@ -226,19 +226,18 @@ void LibraryResolver::resolveSymbolsInLibrary(
return EnumerateResult::Continue;
},
Opts);
+ };
+ if (!Lib.hasFilter()) {
+ LLVM_DEBUG(dbgs() << "Building filter for library: " << Lib.getFullPath()
+ << "\n";);
+ enumerateSymbolsIfNeeded();
if (DiscoveredSymbols.empty()) {
LLVM_DEBUG(dbgs() << " No symbols and remove library : "
<< Lib.getFullPath() << "\n";);
LibMgr.removeLibrary(Lib.getFullPath());
return;
}
- };
-
- if (!Lib.hasFilter()) {
- LLVM_DEBUG(dbgs() << "Building filter for library: " << Lib.getFullPath()
- << "\n";);
- enumerateSymbolsIfNeeded();
SmallVector<StringRef> SymbolVec;
SymbolVec.reserve(DiscoveredSymbols.size());
for (const auto &KV : DiscoveredSymbols)
@@ -288,11 +287,15 @@ void LibraryResolver::searchSymbolsInLibraries(
SymbolSearchContext Ctx(Q);
while (!Ctx.allResolved()) {
+ std::vector<std::shared_ptr<LibraryInfo>> Libs;
+ LibMgr.getLibraries(S, K, Libs, [&](const LibraryInfo &Lib) {
+ return !Ctx.hasSearched(&Lib);
+ });
- for (auto &Lib : LibMgr.getView(S, K)) {
- if (Ctx.hasSearched(Lib.get()))
- continue;
+ if (Libs.empty() && !scanLibrariesIfNeeded(K, scanBatchSize))
+ break; // no more new libs to scan
+ for (auto &Lib : Libs) {
// can use Async here?
resolveSymbolsInLibrary(*Lib, Ctx.query(), Config.Options);
Ctx.markSearched(Lib.get());
@@ -300,12 +303,6 @@ void LibraryResolver::searchSymbolsInLibraries(
if (Ctx.allResolved())
return;
}
-
- if (Ctx.allResolved())
- return;
-
- if (!scanLibrariesIfNeeded(K, scanBatchSize))
- break; // no more new libs to scan
}
};
diff --git a/llvm/lib/ExecutionEngine/Orc/TargetProcess/LibraryScanner.cpp b/llvm/lib/ExecutionEngine/Orc/TargetProcess/LibraryScanner.cpp
index d93f68622fcc2..32f6dbefb8480 100644
--- a/llvm/lib/ExecutionEngine/Orc/TargetProcess/LibraryScanner.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/TargetProcess/LibraryScanner.cpp
@@ -50,7 +50,7 @@ void handleError(Error Err, StringRef context = "") {
}
bool ObjectFileLoader::isArchitectureCompatible(const object::ObjectFile &Obj) {
- Triple HostTriple(sys::getDefaultTargetTriple());
+ Triple HostTriple(sys::getProcessTriple());
Triple ObjTriple = Obj.makeTriple();
LLVM_DEBUG({
More information about the llvm-commits
mailing list