diff --git a/tests/offline/test_engine_analysis_and_kcore_tools.py b/tests/offline/test_engine_analysis_and_kcore_tools.py index 76856829..78b84a48 100644 --- a/tests/offline/test_engine_analysis_and_kcore_tools.py +++ b/tests/offline/test_engine_analysis_and_kcore_tools.py @@ -5,8 +5,10 @@ gpu_firmware_trace_map, gpu_firmware_probe_prefixes, and gpu_perf_kcore_bytes. import contextlib import importlib.util import io +import json import os from pathlib import Path +import struct import tempfile import unittest from unittest import mock @@ -14,6 +16,49 @@ from unittest import mock ROOT = Path(__file__).resolve().parents[2] +def _make_minimal_module_elf(text_bytes: bytes, sym_name: bytes = b"test_fn") -> bytes: + """Build a minimal 64-bit LE ELF object with .text, .strtab, .symtab, .shstrtab.""" + shstr = b"\x00.text\x00.strtab\x00.symtab\x00.shstrtab\x00" + strtab = b"\x00" + sym_name + b"\x00" + # Two Elf64_Sym entries: STN_UNDEF + FUNC symbol in .text (shndx=1) at offset 0. + sym0 = struct.pack(" bytes: + """Build a minimal 64-bit LE ELF with a single PT_LOAD segment.""" + off_phdr = 64 + off_data = 64 + 56 + ehdr = bytearray(64) + ehdr[0:16] = b"\x7fELF\x02\x01\x01" + b"\x00" * 9 + struct.pack_into(" 32 + ip_ok = textbase + 0x04 # in-range offset 4..20 + (t / "errors.txt").write_text( + f"err1 ip {hex(ip_neg)}\nerr2 ip {hex(ip_past)}\nerr3 ip {hex(ip_ok)}\n", + encoding="utf-8", + ) + out_path = t / "out.json" + with contextlib.redirect_stdout(io.StringIO()): + mod.main([ + "--cache", str(t), + "--errors", str(t / "errors.txt"), + "--output", str(out_path), + "--nvidia-ko", str(mod_ko), + ]) + report = json.loads(out_path.read_text(encoding="utf-8")) + rows = report["initial_mismatch_code_comparisons"] + self.assertEqual(len(rows), 3) + self.assertFalse(rows[0]["in_module_text"]) + self.assertEqual(rows[0]["module_offset"], "-0x10") + self.assertFalse(rows[1]["in_module_text"]) + self.assertEqual(rows[1]["module_offset"], "0x18") + self.assertTrue(rows[2]["in_module_text"]) + self.assertTrue(rows[2]["equal"]) + self.assertEqual(rows[2]["symbol"], "nv_test_fn") + self.assertEqual(rows[2]["installed_file_hex"], text_bytes[4:20].hex()) + if __name__ == "__main__": unittest.main() diff --git a/tools/gpu_firmware_probe_prefixes.py b/tools/gpu_firmware_probe_prefixes.py index 7e8a2ecd..885cad74 100644 --- a/tools/gpu_firmware_probe_prefixes.py +++ b/tools/gpu_firmware_probe_prefixes.py @@ -14,9 +14,11 @@ ROOT = Path(__file__).resolve().parents[1] def find_nvidia_ko(explicit: Path | None = None) -> Path: - candidates = [] if explicit is not None: - candidates.append(explicit) + if explicit.is_file(): + return explicit + raise SystemExit(f'Explicit --nvidia-ko not found: {explicit}') + candidates = [] if os.environ.get('NV_RS_NVIDIA_KO'): candidates.append(Path(os.environ['NV_RS_NVIDIA_KO'])) release = platform.release() diff --git a/tools/gpu_perf_kcore_bytes.py b/tools/gpu_perf_kcore_bytes.py index b9d2ccc1..1d8edd0b 100644 --- a/tools/gpu_perf_kcore_bytes.py +++ b/tools/gpu_perf_kcore_bytes.py @@ -14,9 +14,11 @@ PT_LOAD = 1 def find_nvidia_ko(explicit: Path | None = None) -> Path: - candidates = [] if explicit is not None: - candidates.append(explicit) + if explicit.is_file(): + return explicit + raise SystemExit(f'Explicit --nvidia-ko not found: {explicit}') + candidates = [] if os.environ.get('NV_RS_NVIDIA_KO'): candidates.append(Path(os.environ['NV_RS_NVIDIA_KO'])) release = platform.release()