|
1 | 1 | # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
2 | 2 | # SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
| 4 | +import json |
4 | 5 | import os |
5 | 6 | import platform |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | +import textwrap |
6 | 10 |
|
7 | 11 | import pytest |
8 | 12 | from child_load_nvidia_dynamic_lib_helper import run_load_nvidia_dynamic_lib_in_subprocess |
|
13 | 17 | DynamicLibUnknownError, |
14 | 18 | find_nvidia_dynamic_lib, |
15 | 19 | ) |
| 20 | +from cuda.pathfinder._dynamic_libs import find_nvidia_dynamic_lib as find_nvidia_dynamic_lib_module |
16 | 21 | from cuda.pathfinder._dynamic_libs import load_nvidia_dynamic_lib as load_nvidia_dynamic_lib_module |
17 | 22 | from cuda.pathfinder._dynamic_libs import supported_nvidia_libs |
18 | 23 | from cuda.pathfinder._dynamic_libs.subprocess_protocol import ( |
| 24 | + STATUS_NOT_FOUND, |
19 | 25 | STATUS_OK, |
| 26 | + DynamicLibSubprocessPayload, |
20 | 27 | parse_dynamic_lib_subprocess_payload, |
21 | 28 | ) |
22 | 29 | from cuda.pathfinder._utils.platform_aware import IS_WINDOWS, quote_for_shell |
@@ -94,17 +101,74 @@ def test_find_matches_load_in_subprocess(info_summary_append): |
94 | 101 | assert os.path.samefile(find_abs_path, load_payload.abs_path) |
95 | 102 |
|
96 | 103 |
|
97 | | -def test_find_nvidia_dynamic_lib_does_not_load_in_caller_process(): |
98 | | - if IS_WINDOWS or not os.path.exists("/proc/self/maps"): |
99 | | - pytest.skip("Requires /proc/self/maps for in-process load detection") |
| 104 | +def test_find_nvidia_dynamic_lib_propagates_subprocess_not_found_message(monkeypatch): |
| 105 | + # End-to-end of the structured-error path: the subprocess child encodes |
| 106 | + # DynamicLibNotFoundError into the JSON payload; the parent re-raises |
| 107 | + # with the original message preserved. |
| 108 | + find_nvidia_dynamic_lib.cache_clear() |
| 109 | + expected = "child loader said: cudart could not be located" |
| 110 | + |
| 111 | + def fake_run(mode, libname, *, timeout, error_label): |
| 112 | + return DynamicLibSubprocessPayload( |
| 113 | + status=STATUS_NOT_FOUND, |
| 114 | + abs_path=None, |
| 115 | + error={"type": "DynamicLibNotFoundError", "message": expected}, |
| 116 | + ) |
| 117 | + |
| 118 | + monkeypatch.setattr(find_nvidia_dynamic_lib_module, "run_dynamic_lib_subprocess", fake_run) |
| 119 | + with pytest.raises(DynamicLibNotFoundError, match=expected): |
| 120 | + find_nvidia_dynamic_lib("cudart") |
100 | 121 |
|
| 122 | + |
| 123 | +def test_find_nvidia_dynamic_lib_falls_back_when_subprocess_not_found_omits_message(monkeypatch): |
101 | 124 | find_nvidia_dynamic_lib.cache_clear() |
102 | | - libname = "cudart" |
| 125 | + |
| 126 | + def fake_run(mode, libname, *, timeout, error_label): |
| 127 | + return DynamicLibSubprocessPayload(status=STATUS_NOT_FOUND, abs_path=None, error=None) |
| 128 | + |
| 129 | + monkeypatch.setattr(find_nvidia_dynamic_lib_module, "run_dynamic_lib_subprocess", fake_run) |
| 130 | + with pytest.raises(DynamicLibNotFoundError, match=r"could not locate 'cudart'"): |
| 131 | + find_nvidia_dynamic_lib("cudart") |
| 132 | + |
| 133 | + |
| 134 | +_DOES_NOT_LOAD_PROBE = textwrap.dedent( |
| 135 | + """ |
| 136 | + import json |
| 137 | + import os |
| 138 | + import sys |
| 139 | +
|
| 140 | + from cuda.pathfinder import DynamicLibNotFoundError, find_nvidia_dynamic_lib |
| 141 | +
|
| 142 | + libname = sys.argv[1] |
103 | 143 | try: |
104 | 144 | find_nvidia_dynamic_lib(libname) |
| 145 | + find_status = "found" |
105 | 146 | except DynamicLibNotFoundError: |
106 | | - pytest.skip(f"{libname} not available on this host") |
| 147 | + find_status = "not-found" |
107 | 148 |
|
108 | 149 | with open("/proc/self/maps") as f: |
109 | 150 | maps = f.read() |
110 | | - assert "libcudart" not in maps, "find_nvidia_dynamic_lib must not load the library into the caller process" |
| 151 | + print(json.dumps({"find_status": find_status, "loaded": ("lib" + libname) in maps})) |
| 152 | + """ |
| 153 | +).strip() |
| 154 | + |
| 155 | + |
| 156 | +def test_find_nvidia_dynamic_lib_does_not_load_in_caller_process(): |
| 157 | + if IS_WINDOWS or not os.path.exists("/proc/self/maps"): |
| 158 | + pytest.skip("Requires /proc/self/maps for in-process load detection") |
| 159 | + |
| 160 | + libname = "cudart" |
| 161 | + # Run in a fresh interpreter so test ordering / other pathfinder tests |
| 162 | + # in the same process can't have pre-loaded the library. |
| 163 | + result = subprocess.run( # noqa: S603 - trusted argv: current interpreter + inline probe |
| 164 | + [sys.executable, "-c", _DOES_NOT_LOAD_PROBE, libname], |
| 165 | + capture_output=True, |
| 166 | + text=True, |
| 167 | + timeout=60, |
| 168 | + check=False, |
| 169 | + ) |
| 170 | + assert result.returncode == 0, f"probe failed:\nstdout={result.stdout!r}\nstderr={result.stderr!r}" |
| 171 | + payload = json.loads(result.stdout.strip().splitlines()[-1]) |
| 172 | + if payload["find_status"] == "not-found": |
| 173 | + pytest.skip(f"{libname} not available on this host") |
| 174 | + assert payload["loaded"] is False, "find_nvidia_dynamic_lib must not load the library into the caller process" |
0 commit comments