|
| 1 | +#!/usr/bin/env python |
| 2 | +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import os |
| 8 | +import sys |
| 9 | +from collections.abc import Sequence |
| 10 | + |
| 11 | +from cuda.pathfinder._dynamic_libs.lib_descriptor import LIB_DESCRIPTORS |
| 12 | +from cuda.pathfinder._dynamic_libs.load_dl_common import DynamicLibNotFoundError, LoadedDL |
| 13 | +from cuda.pathfinder._dynamic_libs.platform_loader import LOADER |
| 14 | +from cuda.pathfinder._dynamic_libs.subprocess_protocol import ( |
| 15 | + MODE_CANARY, |
| 16 | + MODE_LOAD, |
| 17 | + STATUS_NOT_FOUND, |
| 18 | + STATUS_OK, |
| 19 | + VALID_MODES, |
| 20 | + format_dynamic_lib_subprocess_payload, |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +def _probe_canary_abs_path(libname: str) -> str | None: |
| 25 | + desc = LIB_DESCRIPTORS.get(libname) |
| 26 | + if desc is None: |
| 27 | + raise ValueError(f"Unsupported canary library name: {libname!r}") |
| 28 | + try: |
| 29 | + loaded: LoadedDL | None = LOADER.load_with_system_search(desc) |
| 30 | + except DynamicLibNotFoundError: |
| 31 | + return None |
| 32 | + if loaded is None: |
| 33 | + return None |
| 34 | + abs_path = loaded.abs_path |
| 35 | + if not isinstance(abs_path, str): |
| 36 | + return None |
| 37 | + return abs_path |
| 38 | + |
| 39 | + |
| 40 | +def _validate_abs_path(abs_path: str) -> None: |
| 41 | + assert abs_path, f"empty path: {abs_path=!r}" |
| 42 | + assert os.path.isabs(abs_path), f"not absolute: {abs_path=!r}" |
| 43 | + assert os.path.isfile(abs_path), f"not a file: {abs_path=!r}" |
| 44 | + |
| 45 | + |
| 46 | +def _load_nvidia_dynamic_lib_for_test(libname: str) -> str: |
| 47 | + # Keep imports inside the subprocess body so startup stays focused on the |
| 48 | + # code under test rather than the parent test module. |
| 49 | + from cuda.pathfinder import load_nvidia_dynamic_lib |
| 50 | + from cuda.pathfinder._dynamic_libs.load_nvidia_dynamic_lib import _load_lib_no_cache |
| 51 | + from cuda.pathfinder._dynamic_libs.supported_nvidia_libs import ( |
| 52 | + SUPPORTED_LINUX_SONAMES, |
| 53 | + SUPPORTED_WINDOWS_DLLS, |
| 54 | + ) |
| 55 | + from cuda.pathfinder._utils.platform_aware import IS_WINDOWS |
| 56 | + |
| 57 | + loaded_dl_fresh = load_nvidia_dynamic_lib(libname) |
| 58 | + if loaded_dl_fresh.was_already_loaded_from_elsewhere: |
| 59 | + raise RuntimeError("loaded_dl_fresh.was_already_loaded_from_elsewhere") |
| 60 | + |
| 61 | + abs_path = loaded_dl_fresh.abs_path |
| 62 | + if not isinstance(abs_path, str): |
| 63 | + raise RuntimeError(f"loaded_dl_fresh.abs_path is not a string: {abs_path!r}") |
| 64 | + _validate_abs_path(abs_path) |
| 65 | + assert loaded_dl_fresh.found_via is not None |
| 66 | + |
| 67 | + loaded_dl_from_cache = load_nvidia_dynamic_lib(libname) |
| 68 | + if loaded_dl_from_cache is not loaded_dl_fresh: |
| 69 | + raise RuntimeError("loaded_dl_from_cache is not loaded_dl_fresh") |
| 70 | + |
| 71 | + loaded_dl_no_cache = _load_lib_no_cache(libname) |
| 72 | + supported_libs = SUPPORTED_WINDOWS_DLLS if IS_WINDOWS else SUPPORTED_LINUX_SONAMES |
| 73 | + if not loaded_dl_no_cache.was_already_loaded_from_elsewhere and libname in supported_libs: |
| 74 | + raise RuntimeError("not loaded_dl_no_cache.was_already_loaded_from_elsewhere") |
| 75 | + abs_path_no_cache = loaded_dl_no_cache.abs_path |
| 76 | + if not isinstance(abs_path_no_cache, str): |
| 77 | + raise RuntimeError(f"loaded_dl_no_cache.abs_path is not a string: {abs_path_no_cache!r}") |
| 78 | + if not os.path.samefile(abs_path_no_cache, abs_path): |
| 79 | + raise RuntimeError(f"not os.path.samefile({abs_path_no_cache=!r}, {abs_path=!r})") |
| 80 | + _validate_abs_path(abs_path_no_cache) |
| 81 | + return abs_path |
| 82 | + |
| 83 | + |
| 84 | +def probe_dynamic_lib_and_print_json(libname: str, mode: str) -> None: |
| 85 | + if mode == MODE_CANARY: |
| 86 | + abs_path = _probe_canary_abs_path(libname) |
| 87 | + status = STATUS_OK if abs_path is not None else STATUS_NOT_FOUND |
| 88 | + print(format_dynamic_lib_subprocess_payload(status, abs_path)) |
| 89 | + return |
| 90 | + |
| 91 | + if mode == MODE_LOAD: |
| 92 | + try: |
| 93 | + abs_path = _load_nvidia_dynamic_lib_for_test(libname) |
| 94 | + except DynamicLibNotFoundError: |
| 95 | + print(format_dynamic_lib_subprocess_payload(STATUS_NOT_FOUND, None)) |
| 96 | + return |
| 97 | + print(format_dynamic_lib_subprocess_payload(STATUS_OK, abs_path)) |
| 98 | + return |
| 99 | + |
| 100 | + raise ValueError(f"Unsupported subprocess probe mode: {mode!r}") |
| 101 | + |
| 102 | + |
| 103 | +def main(argv: Sequence[str] | None = None) -> int: |
| 104 | + args = list(sys.argv[1:] if argv is None else argv) |
| 105 | + if len(args) != 2 or args[0] not in VALID_MODES: |
| 106 | + modes = ", ".join(VALID_MODES) |
| 107 | + raise SystemExit( |
| 108 | + f"Usage: python -m cuda.pathfinder._dynamic_libs.dynamic_lib_subprocess <mode> <libname>\nModes: {modes}" |
| 109 | + ) |
| 110 | + mode, libname = args |
| 111 | + probe_dynamic_lib_and_print_json(libname, mode) |
| 112 | + return 0 |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + raise SystemExit(main()) |
0 commit comments