Files
nvdriver/tools/live_control_test.py
morebrackets 3be23c54da Merge remote-tracking branch 'origin/master' into pr-276
# Conflicts:
#	tools/live_control_test.py
2026-12-02 23:09:50 -08:00

153 lines
6.3 KiB
Python

#!/usr/bin/env python3
"""Test only the isolated Rust control node; never opens a GPU device."""
import ctypes,os,struct,errno,json,hashlib,pathlib
_LIBC=ctypes.CDLL(None,use_errno=True)
_LIBC.ioctl.argtypes=[ctypes.c_int,ctypes.c_ulong,ctypes.c_void_p]
_LIBC.ioctl.restype=ctypes.c_int
R=pathlib.Path(__file__).resolve().parent.parent
def req(n,size):return 0xc0004600|(size<<16)|n
def call(fd,n,buf):
arr=(ctypes.c_char*len(buf)).from_buffer(buf) if len(buf) else None
ptr=ctypes.addressof(arr) if arr is not None else 0
ctypes.set_errno(0)
if _LIBC.ioctl(fd,req(n,len(buf)),ptr)<0:
err=ctypes.get_errno()
raise OSError(err,os.strerror(err))
return buf
def check(checks, name, value):
passed = bool(value)
checks.append({'check': name, 'passed': passed})
if not passed:
raise RuntimeError(f"live control check failed: {name}")
def root(checks, fd):
b = bytearray(48)
call(fd, 0x2b, b)
check(checks, 'root allocation NV_OK', struct.unpack_from('<I', b, 40)[0] == 0)
return struct.unpack_from('<I', b, 8)[0]
def expect_errno(checks, name, fn, value):
try:
fn()
except OSError as e:
check(checks, name, e.errno == value)
else:
check(checks, name, False)
def _read_optional_text(path, default):
try:
return path.read_text(encoding='utf-8').strip()
except OSError:
return default
def expected_default_version():
p = pathlib.Path('/sys/module/nvidia_rs/parameters')
dv = _read_optional_text(p / 'driver_version', '580.178.04')
if not dv or dv == 'auto':
abi = _read_optional_text(p / 'gsp_abi', '620')
return b'580.178.04' if abi == '580' else b'620.73.04'
return dv.encode()
def is_strict_mode():
p = pathlib.Path('/sys/module/nvidia_rs/parameters/strict_version_check')
return _read_optional_text(p, '').lower() in ('y', '1', 'true')
def main():
checks = []
exp_ver = expected_default_version()
fd = None
other = None
try:
fd = os.open('/dev/nvidia-rsctl', os.O_RDWR)
other = os.open('/dev/nvidia-rsctl', os.O_RDWR)
b = bytearray(72)
struct.pack_into('<I', b, 0, ord('2'))
call(fd, 210, b)
check(checks, 'version query', bytes(b[8:]).split(b'\0')[0] == exp_ver)
bad = bytearray(72)
bad[8:12] = b'bad\0'
expect_errno(checks, 'mismatched version EINVAL', lambda: call(fd, 210, bad), errno.EINVAL)
check(checks, 'mismatch still copies correct version', bytes(bad[8:]).split(b'\0')[0] == exp_ver)
if not is_strict_mode():
n1 = bytearray(72)
n1[8:19] = b'580.178.04\0'
call(fd, 210, n1)
check(checks, 'permissive negotiate fd', bytes(n1[8:]).split(b'\0')[0] == b'580.178.04')
n2 = bytearray(72)
n2[8:19] = b'570.124.06\0'
call(other, 210, n2)
check(checks, 'permissive negotiate other', bytes(n2[8:]).split(b'\0')[0] == b'570.124.06')
q1 = bytearray(72)
struct.pack_into('<I', q1, 0, ord('2'))
call(fd, 210, q1)
check(checks, 'per-file version query fd', bytes(q1[8:]).split(b'\0')[0] == b'580.178.04')
q2 = bytearray(72)
struct.pack_into('<I', q2, 0, ord('2'))
call(other, 210, q2)
check(checks, 'per-file version query other', bytes(q2[8:]).split(b'\0')[0] == b'570.124.06')
h = root(checks, fd)
g = root(checks, other)
check(checks, 'per-file roots distinct', h != g)
c = bytearray(struct.pack('<IIIIQII', h, h, 0xd02, 0, 0, 0, 0))
call(other, 0x2a, c)
check(checks, 'foreign client rejected', struct.unpack_from('<I', c, 28)[0] == 0x23)
e = bytearray(struct.pack('<IIII', h, 0, 1234, 0))
call(fd, 206, e)
check(checks, 'event allocation', struct.unpack_from('<I', e, 12)[0] == 0)
dup = bytearray(struct.pack('<IIII', h, 0, 1234, 0))
call(fd, 206, dup)
check(checks, 'duplicate event status', struct.unpack_from('<I', dup, 12)[0] == 0x1a)
d = bytearray(48)
struct.pack_into('<IIII', d, 0, h, h, 100, 0x80)
call(fd, 0x2b, d)
check(checks, 'unimplemented GPU allocation returns NOT_SUPPORTED', struct.unpack_from('<I', d, 40)[0] == 0x56)
invalid = bytearray(struct.pack('<IIIIQII', h, h, 0xde000001, 0, 1, 1, 0))
call(fd, 0x2a, invalid)
check(checks, 'nested copy fault NV_INVALID_ADDRESS', struct.unpack_from('<I', invalid, 28)[0] == 0x1e)
expect_errno(checks, 'malformed control wrapper EINVAL', lambda: call(fd, 0x2a, bytearray(31)), errno.EINVAL)
expect_errno(checks, 'unknown request ENOTTY', lambda: call(fd, 0, bytearray()), errno.ENOTTY)
f = bytearray(struct.pack('<IIII', h, 0xdeadbeef, h, 0))
call(fd, 0x29, f)
check(checks, 'root free ignores unused parent', struct.unpack_from('<I', f, 12)[0] == 0)
free_event = bytearray(struct.pack('<IIII', h, 0, 1234, 0))
call(fd, 207, free_event)
check(checks, 'root free purges client events', struct.unpack_from('<I', free_event, 12)[0] == 0x28)
for _ in range(100):
handle = root(checks, fd)
f = bytearray(struct.pack('<IIII', handle, 0, handle, 0))
call(fd, 0x29, f)
if struct.unpack_from('<I', f, 12)[0] != 0:
raise RuntimeError('repeated client lifecycle free failed')
check(checks, '100 repeated client lifecycles', True)
finally:
if other is not None:
os.close(other)
if fd is not None:
os.close(fd)
mod_path = R / 'kernel-open/nvidia_rs.ko'
try:
mod_sha = hashlib.sha256(mod_path.read_bytes()).hexdigest()
except OSError:
mod_sha = None
report = {
'scope': 'Live execution of isolated Rust control module, not GPU/CUDA behavior. No proprietary kernel implementation used by these ioctls.',
'passed': bool(checks) and all(c['passed'] for c in checks),
'checks': checks,
'module_sha256': mod_sha,
'loaded_module_version': _read_optional_text(pathlib.Path('/sys/module/nvidia_rs/version'), 'unknown'),
'original_nvidia_version': _read_optional_text(pathlib.Path('/sys/module/nvidia/version'), 'unloaded'),
}
print(json.dumps(report, indent=2))
return 0 if report['passed'] else 1
if __name__ == '__main__':
raise SystemExit(main())