@@ -308,26 +308,26 @@ def set_ulimit(target_soft_limit: int = 65535):
308308
309309def find_loaded_library (lib_name : str ) -> str | None :
310310 """
311- According to according to https://man7.org/linux/man-pages/man5/proc_pid_maps.5.html,
311+ According to https://man7.org/linux/man-pages/man5/proc_pid_maps.5.html,
312312 the file `/proc/self/maps` contains the memory maps of the process, which includes the
313313 shared libraries loaded by the process. We can use this file to find the path of the
314314 loaded library.
315315 """ # noqa
316- found_line = None
316+ # Match the mapped file's name, not the whole line: an unrelated library
317+ # whose name merely contains lib_name (e.g. TileLang's libcudart_stub.so
318+ # when looking for libcudart) or a directory component containing it must
319+ # not win. Legitimate filenames are {lib_name}.so[.*] or a name-mangled
320+ # {lib_name}-<hash>.so[.*], and /proc/self/maps is ordered by mapping
321+ # address rather than load order, so a substring hit is a
322+ # nondeterministic hijack.
317323 with open ("/proc/self/maps" ) as f :
318324 for line in f :
319- if lib_name in line :
320- found_line = line
321- break
322- if found_line is None :
323- # the library is not loaded in the current process
324- return None
325- # if lib_name is libcudart, we need to match a line with:
326- # address /path/to/libcudart-hash.so.11.0
327- start = found_line .index ("/" )
328- path = found_line [start :].strip ()
329- filename = path .split ("/" )[- 1 ]
330- assert filename .rpartition (".so" )[0 ].startswith (lib_name ), (
331- f"Unexpected filename: { filename } for library { lib_name } "
332- )
333- return path
325+ start = line .find ("/" )
326+ if start == - 1 :
327+ continue
328+ path = line [start :].strip ()
329+ filename = path .rsplit ("/" , maxsplit = 1 )[- 1 ]
330+ if filename .startswith ((f"{ lib_name } ." , f"{ lib_name } -" )):
331+ return path
332+ # the library is not loaded in the current process
333+ return None
0 commit comments