mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
192 lines
8.1 KiB
Python
192 lines
8.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Offline unit and live-artifact tests for src/nvidia/kernel/verify-object.py."""
|
|
from __future__ import annotations
|
|
|
|
import importlib.util
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import subprocess
|
|
import sys
|
|
import unittest
|
|
from unittest import mock
|
|
|
|
ROOT = pathlib.Path(__file__).resolve().parents[2]
|
|
SCRIPT = ROOT / "src/nvidia/kernel/verify-object.py"
|
|
|
|
|
|
def _load_module():
|
|
spec = importlib.util.spec_from_file_location("nv_verify_object", SCRIPT)
|
|
if spec is None or spec.loader is None:
|
|
raise RuntimeError(f"failed to load {SCRIPT}")
|
|
mod = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(mod)
|
|
return mod
|
|
|
|
|
|
MOD = _load_module()
|
|
|
|
|
|
def _base_inputs() -> dict[str, object]:
|
|
obj_defined_lines = [
|
|
"0000000000000000 T nv_rs_gpu_registry_publish",
|
|
"0000000000000010 T nv_rs_gpu_registry_retire",
|
|
"0000000000000020 T nv_rs_gpu_set_probe_identity",
|
|
"0000000000000030 T nv_rs_protocol_version",
|
|
"0000000000000040 T nv_rs_event_selftest",
|
|
"0000000000000050 T nv_rs_gpu_capabilities",
|
|
] + [
|
|
f"00000000000001{idx:02x} T nv_rs_sysmem_{name}"
|
|
for idx, name in enumerate(MOD.REQUIRED_SYSMEM_EXPORTS)
|
|
]
|
|
module_defined_lines = [
|
|
"0000000000000000 T nv_rs_custom_adapter",
|
|
"0000000000000010 T nv_rs_gpu_registry_lock",
|
|
"0000000000000020 T nv_rs_gpu_registry_unlock",
|
|
] + [
|
|
f"00000000000001{idx:02x} T {name}"
|
|
for idx, name in enumerate(sorted(MOD.NANO_ADAPTERS))
|
|
]
|
|
return {
|
|
"assembly": (
|
|
"0000000000000000 <nv_rs_protocol_version>:\n"
|
|
" 0:\t55\tpush %rbp\n"
|
|
" 1:\t48 81 ec 00 02 00 00\tsub $0x200,%rsp\n"
|
|
" 8:\tc3\tret\n"
|
|
),
|
|
"sections": " [ 1] .text PROGBITS 0000000000000000 000040 000100 00 AX 0 0 16\n",
|
|
"obj_undefined_nm": (
|
|
"\n"
|
|
" U nv_rs_custom_adapter\n"
|
|
" U __x86_return_thunk\n"
|
|
" U __x86_indirect_thunk_r11\n"
|
|
" U strlen\n"
|
|
),
|
|
"obj_defined_nm": "\n".join(obj_defined_lines) + "\n",
|
|
"module_defined_nm": "\n".join(module_defined_lines) + "\n",
|
|
"module_undefined_nm": (
|
|
" U __x86_return_thunk\n"
|
|
" U __x86_indirect_thunk_r11\n"
|
|
" U strlen\n"
|
|
),
|
|
"module_relocs": (
|
|
"Relocation section '.rela.text' at offset 0x100 contains 2 entries:\n"
|
|
" Offset Info Type Sym. Value Sym. Name + Addend\n"
|
|
"0000000000000010 0000000100000002 R_X86_64_PC32 0000000000000000 MY_GOT_NAMED_SYM - 4\n"
|
|
"0000000000000020 0000000200000002 R_X86_64_PC32 0000000000000000 R_GOT - 4\n"
|
|
),
|
|
"modinfo_name": "nvidia_rs\n",
|
|
"modinfo_alias": "\n",
|
|
"modinfo_vermagic": "7.0.0-test SMP preempt mod_unload\n",
|
|
"sha256_map": {"obj": "0" * 64, "module": "1" * 64},
|
|
}
|
|
|
|
|
|
class VerifyObjectTests(unittest.TestCase):
|
|
def test_clean_inputs_pass_and_ignore_got_in_symbol_name(self) -> None:
|
|
report = MOD.evaluate_outputs(**_base_inputs())
|
|
self.assertTrue(all(report["checks"].values()), report["checks"])
|
|
self.assertEqual(report["unresolved_private_adapters"], [])
|
|
self.assertEqual(report["unexpected_external_symbols"], [])
|
|
self.assertEqual(report["largest_conservative_function_stack_bytes"], 520)
|
|
|
|
def test_rejects_got_relocation_type(self) -> None:
|
|
inputs = _base_inputs()
|
|
inputs["module_relocs"] = (
|
|
"0000000000000010 000000010000002a R_X86_64_REX_GOTPCRELX 0000000000000000 foo - 4\n"
|
|
)
|
|
report = MOD.evaluate_outputs(**inputs)
|
|
self.assertFalse(report["checks"]["no_kernel_unsupported_got_relocations"])
|
|
|
|
def test_rejects_unresolved_private_adapter_and_forbidden_memcpy(self) -> None:
|
|
inputs = _base_inputs()
|
|
inputs["obj_undefined_nm"] = (
|
|
" U nv_rs_missing_adapter\n"
|
|
" U memcpy\n"
|
|
" U rust_eh_personality\n"
|
|
)
|
|
report = MOD.evaluate_outputs(**inputs)
|
|
self.assertFalse(report["checks"]["private_adapters_resolved_in_module"])
|
|
self.assertFalse(report["checks"]["no_forbidden_memcpy_memset_memmove"])
|
|
self.assertFalse(report["checks"]["no_unresolved_rust_or_userspace_symbols"])
|
|
self.assertEqual(report["unresolved_private_adapters"], ["nv_rs_missing_adapter"])
|
|
self.assertEqual(report["unexpected_external_symbols"], ["memcpy", "rust_eh_personality"])
|
|
|
|
def test_rejects_prefix_spoofed_exports(self) -> None:
|
|
inputs = _base_inputs()
|
|
inputs["obj_defined_nm"] = str(inputs["obj_defined_nm"]).replace(
|
|
"nv_rs_gpu_set_probe_identity",
|
|
"nv_rs_gpu_set_probe_identity_extra",
|
|
)
|
|
report = MOD.evaluate_outputs(**inputs)
|
|
self.assertFalse(report["checks"]["pci_probe_identity_entry_present"])
|
|
|
|
def test_cumulative_function_stack_overflow_detected(self) -> None:
|
|
inputs = _base_inputs()
|
|
inputs["assembly"] = (
|
|
"0000000000000000 <heavy_fn>:\n"
|
|
" 0:\t48 81 ec 00 03 00 00\tsub $0x300,%rsp\n"
|
|
" 7:\t48 81 ec 00 02 00 00\tsub $0x200,%rsp\n"
|
|
)
|
|
report = MOD.evaluate_outputs(**inputs)
|
|
self.assertEqual(report["largest_conservative_function_stack_bytes"], 1280)
|
|
self.assertFalse(report["checks"]["no_stack_reservation_above_1024_bytes"])
|
|
|
|
def test_aarch64_preindex_writeback_counted_in_stack_limit(self) -> None:
|
|
# 16 + 0x3f0 = 1024 bytes -> passes at exact boundary.
|
|
ok_inputs = _base_inputs()
|
|
ok_inputs["assembly"] = (
|
|
"0000000000000000 <arm64_exact_limit>:\n"
|
|
" 0:\ta9bf7bfd\tstp\tx29, x30, [sp, #-16]!\n"
|
|
" 4:\td10fc3ff\tsub\tsp, sp, #0x3f0\n"
|
|
)
|
|
ok_report = MOD.evaluate_outputs(**ok_inputs)
|
|
self.assertEqual(ok_report["largest_conservative_function_stack_bytes"], 1024)
|
|
self.assertTrue(ok_report["checks"]["no_stack_reservation_above_1024_bytes"])
|
|
|
|
# 16 + 0x400 = 1040 bytes -> rejected.
|
|
over_inputs = _base_inputs()
|
|
over_inputs["assembly"] = (
|
|
"0000000000000000 <arm64_over_limit>:\n"
|
|
" 0:\ta9bf7bfd\tstp\tx29, x30, [sp, #-16]!\n"
|
|
" 4:\td11003ff\tsub\tsp, sp, #0x400\n"
|
|
)
|
|
over_report = MOD.evaluate_outputs(**over_inputs)
|
|
self.assertEqual(over_report["largest_conservative_function_stack_bytes"], 1040)
|
|
self.assertFalse(over_report["checks"]["no_stack_reservation_above_1024_bytes"])
|
|
|
|
def test_find_modinfo_falls_back_to_sbin(self) -> None:
|
|
with mock.patch.object(MOD.shutil, "which", return_value=None):
|
|
with mock.patch.object(
|
|
MOD.os.path, "isfile", side_effect=lambda p: p == "/sbin/modinfo"
|
|
):
|
|
with mock.patch.object(MOD.os, "access", return_value=True):
|
|
self.assertEqual(MOD.find_modinfo(), "/sbin/modinfo")
|
|
|
|
def test_missing_artifact_returns_nonzero_cleanly(self) -> None:
|
|
proc = subprocess.run(
|
|
[sys.executable, "-O", str(SCRIPT), "--object", "/nonexistent/nv-rust.o_binary"],
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
self.assertNotEqual(proc.returncode, 0)
|
|
self.assertIn("Rust object not found", proc.stderr)
|
|
|
|
def test_live_built_artifacts_if_present(self) -> None:
|
|
if not (MOD.DEFAULT_OBJ.is_file() and MOD.DEFAULT_MODULE.is_file()):
|
|
self.skipTest("kernel-open build artifacts not present")
|
|
proc = subprocess.run(
|
|
[sys.executable, "-O", str(SCRIPT)],
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
self.assertEqual(proc.returncode, 0, proc.stdout + proc.stderr)
|
|
report = json.loads(proc.stdout)
|
|
self.assertTrue(all(report["checks"].values()), report["checks"])
|
|
self.assertEqual(report["unresolved_private_adapters"], [])
|
|
self.assertEqual(report["unexpected_external_symbols"], [])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|