[Bugfix] Match the mapped filename in find_loaded_library (#47586)

Signed-off-by: Zihua Wu <13583761+lucifer1004@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: Shengqi Chen <harry-chen@outlook.com>
This commit is contained in:
Gabriel Wu
2026-07-07 08:06:29 +00:00
committed by GitHub
co-authored by Claude Fable 5 Shengqi Chen
parent b4cfbc24d3
commit ba50b9763f
+17 -17
View File
@@ -308,26 +308,26 @@ def set_ulimit(target_soft_limit: int = 65535):
def find_loaded_library(lib_name: str) -> str | None: def find_loaded_library(lib_name: str) -> str | None:
""" """
According to according to https://man7.org/linux/man-pages/man5/proc_pid_maps.5.html, According to https://man7.org/linux/man-pages/man5/proc_pid_maps.5.html,
the file `/proc/self/maps` contains the memory maps of the process, which includes the the file `/proc/self/maps` contains the memory maps of the process, which includes the
shared libraries loaded by the process. We can use this file to find the path of the shared libraries loaded by the process. We can use this file to find the path of the
loaded library. loaded library.
""" # noqa """ # noqa
found_line = None # Match the mapped file's name, not the whole line: an unrelated library
# whose name merely contains lib_name (e.g. TileLang's libcudart_stub.so
# when looking for libcudart) or a directory component containing it must
# not win. Legitimate filenames are {lib_name}.so[.*] or a name-mangled
# {lib_name}-<hash>.so[.*], and /proc/self/maps is ordered by mapping
# address rather than load order, so a substring hit is a
# nondeterministic hijack.
with open("/proc/self/maps") as f: with open("/proc/self/maps") as f:
for line in f: for line in f:
if lib_name in line: start = line.find("/")
found_line = line if start == -1:
break continue
if found_line is None: path = line[start:].strip()
# the library is not loaded in the current process filename = path.rsplit("/", maxsplit=1)[-1]
return None if filename.startswith((f"{lib_name}.", f"{lib_name}-")):
# if lib_name is libcudart, we need to match a line with: return path
# address /path/to/libcudart-hash.so.11.0 # the library is not loaded in the current process
start = found_line.index("/") return None
path = found_line[start:].strip()
filename = path.split("/")[-1]
assert filename.rpartition(".so")[0].startswith(lib_name), (
f"Unexpected filename: {filename} for library {lib_name}"
)
return path