Files
nvdriver/tools/live_control_test.py

49 lines
3.5 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;checks=[]
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(name,value):
checks.append({'check':name,'passed':bool(value)});assert value,name
def root(fd):
b=bytearray(48);call(fd,0x2b,b);check('root allocation NV_OK',struct.unpack_from('<I',b,40)[0]==0);return struct.unpack_from('<I',b,8)[0]
def expect_errno(name,fn,value):
try:fn()
except OSError as e:check(name,e.errno==value)
else:check(name,False)
fd=os.open('/dev/nvidia-rsctl',os.O_RDWR);other=os.open('/dev/nvidia-rsctl',os.O_RDWR)
try:
b=bytearray(72);struct.pack_into('<I',b,0,ord('2'));call(fd,210,b);check('version query',bytes(b[8:]).split(b'\0')[0]==b'580.178.04')
bad=bytearray(72);bad[8:12]=b'bad\0';expect_errno('mismatched version EINVAL',lambda:call(fd,210,bad),errno.EINVAL);check('mismatch still copies correct version',bytes(bad[8:]).split(b'\0')[0]==b'580.178.04')
h=root(fd);g=root(other);check('per-file roots distinct',h!=g)
c=bytearray(struct.pack('<IIIIQII',h,h,0xd02,0,0,0,0));call(other,0x2a,c);check('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('event allocation',struct.unpack_from('<I',e,12)[0]==0)
dup=bytearray(struct.pack('<IIII',h,0,1234,0));call(fd,206,dup);check('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('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('nested copy fault NV_INVALID_ADDRESS',struct.unpack_from('<I',invalid,28)[0]==0x1e)
expect_errno('malformed control wrapper EINVAL',lambda:call(fd,0x2a,bytearray(31)),errno.EINVAL)
expect_errno('unknown request ENOTTY',lambda:call(fd,0,bytearray()),errno.ENOTTY)
f=bytearray(struct.pack('<IIII',h,0xdeadbeef,h,0));call(fd,0x29,f);check('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('root free purges client events',struct.unpack_from('<I',free_event,12)[0]==0x28)
for _ in range(100):
handle=root(fd);f=bytearray(struct.pack('<IIII',handle,0,handle,0));call(fd,0x29,f);assert struct.unpack_from('<I',f,12)[0]==0
check('100 repeated client lifecycles',True)
finally:
os.close(other);os.close(fd)
report={'scope':'Live execution of isolated Rust control module, not GPU/CUDA behavior. No proprietary kernel implementation used by these ioctls.','passed':all(c['passed'] for c in checks),'checks':checks,'module_sha256':hashlib.sha256((R/'kernel-open/nvidia_rs.ko').read_bytes()).hexdigest(),'loaded_module_version':pathlib.Path('/sys/module/nvidia_rs/version').read_text().strip(),'original_nvidia_version':pathlib.Path('/sys/module/nvidia/version').read_text().strip() if pathlib.Path('/sys/module/nvidia/version').exists() else 'unloaded'};print(json.dumps(report,indent=2))