mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
tools: harden probe_original_* scripts against FD leaks, -O asserts, and missing sysfs/modinfo
This commit is contained in:
54
tests/offline/test_original_probe_scripts.py
Normal file
54
tests/offline/test_original_probe_scripts.py
Normal file
@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Offline tests for the six probe_original_*.py control-node probe scripts."""
|
||||
import importlib.util
|
||||
import os
|
||||
from pathlib import Path
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
SCRIPTS = [
|
||||
"tools/probe_original_clients.py",
|
||||
"tools/probe_original_control_copies.py",
|
||||
"tools/probe_original_null_control.py",
|
||||
"tools/probe_original_software_controls.py",
|
||||
"tools/probe_original_software_controls_followup.py",
|
||||
"tools/probe_original_software_controls_replay.py",
|
||||
]
|
||||
|
||||
|
||||
def _load(name: str, rel: str):
|
||||
spec = importlib.util.spec_from_file_location(name, ROOT / rel)
|
||||
mod = importlib.util.module_from_spec(spec)
|
||||
assert spec.loader is not None
|
||||
spec.loader.exec_module(mod)
|
||||
return mod
|
||||
|
||||
|
||||
class OriginalProbeScriptsTest(unittest.TestCase):
|
||||
def test_all_probe_scripts_import_cleanly_without_opening_devices(self):
|
||||
with mock.patch("os.open", side_effect=AssertionError("must not open devices on import")):
|
||||
for idx, rel in enumerate(SCRIPTS):
|
||||
mod = _load(f"probe_mod_{idx}", rel)
|
||||
self.assertTrue(callable(mod.main), rel)
|
||||
|
||||
def test_module_version_fallback_returns_unloaded_when_missing(self):
|
||||
clients = _load("probe_clients_ver", "tools/probe_original_clients.py")
|
||||
self.assertEqual(clients._read_module_version("nonexistent_nv_module_xyz"), "unloaded")
|
||||
|
||||
sw = _load("probe_sw_ver", "tools/probe_original_software_controls.py")
|
||||
with mock.patch("pathlib.Path.read_text", side_effect=FileNotFoundError):
|
||||
self.assertEqual(sw._read_module_version("/dev/nvidia-rsctl"), "unloaded")
|
||||
|
||||
def test_replay_rejects_unrecognized_device_node_and_finds_modinfo(self):
|
||||
replay = _load("probe_replay", "tools/probe_original_software_controls_replay.py")
|
||||
with mock.patch.dict(os.environ, {"NV_RS_NODE": "/dev/bogus"}):
|
||||
with self.assertRaisesRegex(SystemExit, "Explicit original or isolated Rust node required"):
|
||||
replay.main()
|
||||
with mock.patch("shutil.which", return_value=None):
|
||||
found = replay._find_modinfo()
|
||||
self.assertTrue(found.endswith("modinfo"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@ -1,26 +1,85 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Bounded software-client lifecycle probe against the original control node."""
|
||||
import os,fcntl,struct,json,ctypes as C,pathlib,time,sys
|
||||
R=pathlib.Path(__file__).resolve().parent.parent
|
||||
records=[]
|
||||
def ioctl(fd,label,n,b):
|
||||
fcntl.ioctl(fd,0xc0004600|(len(b)<<16)|n,b,True)
|
||||
status=struct.unpack_from('<I',b,{0x2b:40,0x2a:28,0x29:12}[n])[0]
|
||||
records.append({'label':label,'status':status,'response':b.hex()});return status
|
||||
node=sys.argv[1] if len(sys.argv)>1 else '/dev/nvidiactl'
|
||||
fd=os.open(node,os.O_RDWR);fd2=os.open(node,os.O_RDWR);handles=[]
|
||||
try:
|
||||
out=C.c_uint32(0);b=bytearray(48);struct.pack_into('<Q',b,16,C.addressof(out));assert ioctl(fd,'create_root_output_parameter',0x2b,b)==0;h=struct.unpack_from('<I',b,8)[0];handles.append(h);records[-1]['parameter']=out.value;assert out.value==h
|
||||
f=bytearray(struct.pack('<IIII',h,h,0xfffffff0,0));ioctl(fd,'free_unknown_object',0x29,f)
|
||||
c=bytearray(struct.pack('<IIIIQII',h,h,0,0,0,0,0));ioctl(fd,'same_file_unknown_control',0x2a,c)
|
||||
c=bytearray(struct.pack('<IIIIQII',h,h,0,0,0,0,0));ioctl(fd2,'other_file_same_process_unknown_control',0x2a,c)
|
||||
for descriptor,label in [(fd,'same_file_root_class_query'),(fd2,'other_file_root_class_query')]:
|
||||
payload=(C.c_uint32*4)(h,2,0,0);c=bytearray(struct.pack('<IIIIQII',h,h,0xd02,0,C.addressof(payload),16,0));ioctl(descriptor,label,0x2a,c);records[-1]['parameter_words']=list(payload)
|
||||
f=bytearray(struct.pack('<IIII',h,0xdeadbeef,h,0));assert ioctl(fd,'free_root_unused_parent',0x29,f)==0;handles.remove(h)
|
||||
f=bytearray(struct.pack('<IIII',h,0,h,0));ioctl(fd,'double_free_root',0x29,f)
|
||||
finally:
|
||||
for h in handles:
|
||||
try:ioctl(fd,'cleanup_root',0x29,bytearray(struct.pack('<IIII',h,0,h,0)))
|
||||
except OSError:pass
|
||||
os.close(fd2);os.close(fd)
|
||||
report={'scope':'Only owned software root-client requests on original nvidiactl; no GPU resources, memory mappings or hardware commands requested.','device_node':node,'original_driver':pathlib.Path('/sys/module/nvidia/version').read_text().strip() if pathlib.Path('/sys/module/nvidia/version').exists() else 'unloaded','records':records,'cleanup_handles_remaining':handles,'time_ns':time.time_ns()};print(json.dumps(report,indent=2))
|
||||
import os, fcntl, struct, json, ctypes as C, pathlib, time, sys
|
||||
|
||||
R = pathlib.Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
def _read_module_version(module_name: str) -> str:
|
||||
path = pathlib.Path('/sys/module') / module_name / 'version'
|
||||
try:
|
||||
return path.read_text(encoding='utf-8').strip()
|
||||
except OSError:
|
||||
return 'unloaded'
|
||||
|
||||
|
||||
def main(argv=None):
|
||||
args = sys.argv[1:] if argv is None else argv
|
||||
node = args[0] if args else '/dev/nvidiactl'
|
||||
records = []
|
||||
|
||||
def ioctl(fd, label, n, b):
|
||||
fcntl.ioctl(fd, 0xc0004600 | (len(b) << 16) | n, b, True)
|
||||
status = struct.unpack_from('<I', b, {0x2b: 40, 0x2a: 28, 0x29: 12}[n])[0]
|
||||
records.append({'label': label, 'status': status, 'response': b.hex()})
|
||||
return status
|
||||
|
||||
fd = None
|
||||
fd2 = None
|
||||
handles = []
|
||||
try:
|
||||
fd = os.open(node, os.O_RDWR)
|
||||
fd2 = os.open(node, os.O_RDWR)
|
||||
out = C.c_uint32(0)
|
||||
b = bytearray(48)
|
||||
struct.pack_into('<Q', b, 16, C.addressof(out))
|
||||
if ioctl(fd, 'create_root_output_parameter', 0x2b, b) != 0:
|
||||
raise RuntimeError('create_root_output_parameter failed')
|
||||
h = struct.unpack_from('<I', b, 8)[0]
|
||||
handles.append(h)
|
||||
records[-1]['parameter'] = out.value
|
||||
if out.value != h:
|
||||
raise RuntimeError(f'output parameter {out.value:#x} != handle {h:#x}')
|
||||
f = bytearray(struct.pack('<IIII', h, h, 0xfffffff0, 0))
|
||||
ioctl(fd, 'free_unknown_object', 0x29, f)
|
||||
c = bytearray(struct.pack('<IIIIQII', h, h, 0, 0, 0, 0, 0))
|
||||
ioctl(fd, 'same_file_unknown_control', 0x2a, c)
|
||||
c = bytearray(struct.pack('<IIIIQII', h, h, 0, 0, 0, 0, 0))
|
||||
ioctl(fd2, 'other_file_same_process_unknown_control', 0x2a, c)
|
||||
for descriptor, label in [(fd, 'same_file_root_class_query'), (fd2, 'other_file_root_class_query')]:
|
||||
payload = (C.c_uint32 * 4)(h, 2, 0, 0)
|
||||
c = bytearray(struct.pack('<IIIIQII', h, h, 0xd02, 0, C.addressof(payload), 16, 0))
|
||||
ioctl(descriptor, label, 0x2a, c)
|
||||
records[-1]['parameter_words'] = list(payload)
|
||||
f = bytearray(struct.pack('<IIII', h, 0xdeadbeef, h, 0))
|
||||
if ioctl(fd, 'free_root_unused_parent', 0x29, f) != 0:
|
||||
raise RuntimeError('free_root_unused_parent failed')
|
||||
handles.remove(h)
|
||||
f = bytearray(struct.pack('<IIII', h, 0, h, 0))
|
||||
ioctl(fd, 'double_free_root', 0x29, f)
|
||||
finally:
|
||||
if fd is not None:
|
||||
for h in list(handles):
|
||||
try:
|
||||
ioctl(fd, 'cleanup_root', 0x29, bytearray(struct.pack('<IIII', h, 0, h, 0)))
|
||||
handles.remove(h)
|
||||
except OSError:
|
||||
pass
|
||||
if fd2 is not None:
|
||||
os.close(fd2)
|
||||
if fd is not None:
|
||||
os.close(fd)
|
||||
report = {
|
||||
'scope': 'Only owned software root-client requests on original nvidiactl; no GPU resources, memory mappings or hardware commands requested.',
|
||||
'device_node': node,
|
||||
'original_driver': _read_module_version('nvidia'),
|
||||
'records': records,
|
||||
'cleanup_handles_remaining': handles,
|
||||
'time_ns': time.time_ns(),
|
||||
}
|
||||
print(json.dumps(report, indent=2))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
|
||||
@ -1,26 +1,77 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Software root only: distinguish transport failure from nested-copy NV status."""
|
||||
import ctypes as C,fcntl,os,struct,json,pathlib
|
||||
fd=os.open('/dev/nvidiactl',os.O_RDWR);records=[];root=0
|
||||
try:
|
||||
top=bytearray(48);fcntl.ioctl(fd,0xc030462b,top,True);assert struct.unpack_from('<I',top,40)[0]==0;root=struct.unpack_from('<I',top,8)[0]
|
||||
for label,client,target,pointer,size in [('valid_target_bad_pointer',root,root,0xffffffffffffffff,16),('missing_target_bad_pointer',root,0xfffffff0,0xffffffffffffffff,16),('bad_client_bad_pointer',0,root,0xffffffffffffffff,16),('valid_target_null',root,root,0,16),('missing_target_wrong_size',root,0xfffffff0,0xffffffffffffffff,15)]:
|
||||
top=bytearray(struct.pack('<IIIIQII',client,target,0xd02,0,pointer,size,0xa5a5a5a5));before=top.hex();err=0
|
||||
try:fcntl.ioctl(fd,0xc020462a,top,True)
|
||||
except OSError as e:err=e.errno
|
||||
records.append(dict(label=label,before=before,after=top.hex(),linux_errno=err,status=struct.unpack_from('<I',top,28)[0]))
|
||||
libc=C.CDLL(None);libc.mmap.restype=C.c_void_p;libc.mmap.argtypes=[C.c_void_p,C.c_size_t,C.c_int,C.c_int,C.c_int,C.c_long];libc.mprotect.argtypes=[C.c_void_p,C.c_size_t,C.c_int];libc.munmap.argtypes=[C.c_void_p,C.c_size_t]
|
||||
address=libc.mmap(None,4096,3,0x22,-1,0);assert address not in [None,C.c_void_p(-1).value]
|
||||
try:
|
||||
for label,obj in [('readonly_output_root',root),('readonly_output_missing',0xfffffff0)]:
|
||||
assert libc.mprotect(address,4096,3)==0;C.memmove(address,struct.pack('<IIQ',obj,2,0xa5a5a5a5a5a5a5a5),16);assert libc.mprotect(address,4096,1)==0
|
||||
top=bytearray(struct.pack('<IIIIQII',root,root,0xd02,0,address,16,0xa5a5a5a5));before=top.hex();err=0
|
||||
try:fcntl.ioctl(fd,0xc020462a,top,True)
|
||||
except OSError as e:err=e.errno
|
||||
records.append(dict(label=label,before=before,after=top.hex(),linux_errno=err,status=struct.unpack_from('<I',top,28)[0],payload=C.string_at(address,16).hex()))
|
||||
finally:assert libc.munmap(address,4096)==0
|
||||
finally:
|
||||
if root:
|
||||
top=bytearray(struct.pack('<IIII',root,0,root,0));fcntl.ioctl(fd,0xc0104629,top,True)
|
||||
os.close(fd)
|
||||
print(json.dumps(dict(scope='Software root and root query only; no GPU resources',driver_version=pathlib.Path('/sys/module/nvidia/version').read_text().strip(),records=records),indent=2))
|
||||
import ctypes as C, fcntl, os, struct, json, pathlib
|
||||
|
||||
|
||||
def _read_module_version(module_name: str) -> str:
|
||||
try:
|
||||
return (pathlib.Path('/sys/module') / module_name / 'version').read_text(encoding='utf-8').strip()
|
||||
except OSError:
|
||||
return 'unloaded'
|
||||
|
||||
|
||||
def main():
|
||||
fd = os.open('/dev/nvidiactl', os.O_RDWR)
|
||||
records = []
|
||||
root = 0
|
||||
try:
|
||||
top = bytearray(48)
|
||||
fcntl.ioctl(fd, 0xc030462b, top, True)
|
||||
if struct.unpack_from('<I', top, 40)[0] != 0:
|
||||
raise RuntimeError('root allocation failed')
|
||||
root = struct.unpack_from('<I', top, 8)[0]
|
||||
for label, client, target, pointer, size in [
|
||||
('valid_target_bad_pointer', root, root, 0xffffffffffffffff, 16),
|
||||
('missing_target_bad_pointer', root, 0xfffffff0, 0xffffffffffffffff, 16),
|
||||
('bad_client_bad_pointer', 0, root, 0xffffffffffffffff, 16),
|
||||
('valid_target_null', root, root, 0, 16),
|
||||
('missing_target_wrong_size', root, 0xfffffff0, 0xffffffffffffffff, 15),
|
||||
]:
|
||||
top = bytearray(struct.pack('<IIIIQII', client, target, 0xd02, 0, pointer, size, 0xa5a5a5a5))
|
||||
before = top.hex()
|
||||
err = 0
|
||||
try:
|
||||
fcntl.ioctl(fd, 0xc020462a, top, True)
|
||||
except OSError as e:
|
||||
err = e.errno
|
||||
records.append(dict(label=label, before=before, after=top.hex(), linux_errno=err, status=struct.unpack_from('<I', top, 28)[0]))
|
||||
libc = C.CDLL(None)
|
||||
libc.mmap.restype = C.c_void_p
|
||||
libc.mmap.argtypes = [C.c_void_p, C.c_size_t, C.c_int, C.c_int, C.c_int, C.c_long]
|
||||
libc.mprotect.argtypes = [C.c_void_p, C.c_size_t, C.c_int]
|
||||
libc.munmap.argtypes = [C.c_void_p, C.c_size_t]
|
||||
address = libc.mmap(None, 4096, 3, 0x22, -1, 0)
|
||||
if address in (None, C.c_void_p(-1).value):
|
||||
raise OSError('mmap failed')
|
||||
try:
|
||||
for label, obj in [('readonly_output_root', root), ('readonly_output_missing', 0xfffffff0)]:
|
||||
if libc.mprotect(address, 4096, 3) != 0:
|
||||
raise OSError('mprotect(RW) failed')
|
||||
C.memmove(address, struct.pack('<IIQ', obj, 2, 0xa5a5a5a5a5a5a5a5), 16)
|
||||
if libc.mprotect(address, 4096, 1) != 0:
|
||||
raise OSError('mprotect(RO) failed')
|
||||
top = bytearray(struct.pack('<IIIIQII', root, root, 0xd02, 0, address, 16, 0xa5a5a5a5))
|
||||
before = top.hex()
|
||||
err = 0
|
||||
try:
|
||||
fcntl.ioctl(fd, 0xc020462a, top, True)
|
||||
except OSError as e:
|
||||
err = e.errno
|
||||
records.append(dict(label=label, before=before, after=top.hex(), linux_errno=err, status=struct.unpack_from('<I', top, 28)[0], payload=C.string_at(address, 16).hex()))
|
||||
finally:
|
||||
if libc.munmap(address, 4096) != 0:
|
||||
raise OSError('munmap failed')
|
||||
finally:
|
||||
if root:
|
||||
try:
|
||||
top = bytearray(struct.pack('<IIII', root, 0, root, 0))
|
||||
fcntl.ioctl(fd, 0xc0104629, top, True)
|
||||
except OSError:
|
||||
pass
|
||||
os.close(fd)
|
||||
print(json.dumps(dict(scope='Software root and root query only; no GPU resources', driver_version=_read_module_version('nvidia'), records=records), indent=2))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
|
||||
@ -1,13 +1,35 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Bounded NULL-control transport probe. No GPU objects/commands are requested."""
|
||||
import os,fcntl,struct,json,pathlib,errno
|
||||
records=[];fd=os.open('/dev/nvidiactl',os.O_RDWR)
|
||||
try:
|
||||
for command in [0,0x20800000,0x4000]:
|
||||
for pointer,size,flags in [(0,0,0),(0,4,0),(0xffffffffffffffff,4,0),(0xffffffffffffffff,4,1)]:
|
||||
b=bytearray(struct.pack('<IIIIQII',0xdeadbeef,0xdeadbeef,command,flags,pointer,size,0xa5a5a5a5));rc=0
|
||||
try:fcntl.ioctl(fd,0xc020462a,b,True)
|
||||
except OSError as e:rc=e.errno
|
||||
records.append(dict(command=hex(command),pointer=hex(pointer),size=size,flags=flags,linux_errno=rc,NV_status=struct.unpack_from('<I',b,28)[0],response=b.hex()))
|
||||
finally:os.close(fd)
|
||||
print(json.dumps(dict(scope='NULL controls only; no resource allocations or GPU commands',version=pathlib.Path('/sys/module/nvidia/version').read_text().strip(),records=records),indent=2))
|
||||
import os, fcntl, struct, json, pathlib
|
||||
|
||||
|
||||
def _read_module_version(module_name: str) -> str:
|
||||
try:
|
||||
return (pathlib.Path('/sys/module') / module_name / 'version').read_text(encoding='utf-8').strip()
|
||||
except OSError:
|
||||
return 'unloaded'
|
||||
|
||||
|
||||
def main():
|
||||
node = os.environ.get('NV_RS_NODE', '/dev/nvidiactl')
|
||||
records = []
|
||||
fd = os.open(node, os.O_RDWR)
|
||||
try:
|
||||
for command in [0, 0x20800000, 0x4000]:
|
||||
for pointer, size, flags in [(0, 0, 0), (0, 4, 0), (0xffffffffffffffff, 4, 0), (0xffffffffffffffff, 4, 1)]:
|
||||
b = bytearray(struct.pack('<IIIIQII', 0xdeadbeef, 0xdeadbeef, command, flags, pointer, size, 0xa5a5a5a5))
|
||||
rc = 0
|
||||
try:
|
||||
fcntl.ioctl(fd, 0xc020462a, b, True)
|
||||
except OSError as e:
|
||||
rc = e.errno
|
||||
records.append(dict(command=hex(command), pointer=hex(pointer), size=size, flags=flags, linux_errno=rc, NV_status=struct.unpack_from('<I', b, 28)[0], response=b.hex()))
|
||||
finally:
|
||||
os.close(fd)
|
||||
module_name = 'nvidia_rs' if node == '/dev/nvidia-rsctl' else 'nvidia'
|
||||
print(json.dumps(dict(scope='NULL controls only; no resource allocations or GPU commands', version=_read_module_version(module_name), records=records), indent=2))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
|
||||
@ -1,41 +1,92 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Bounded original-driver CPU client/control parity; no GPU resources allocated."""
|
||||
import ctypes as C,fcntl,os,struct,json,pathlib,time
|
||||
node=os.environ.get('NV_RS_NODE','/dev/nvidiactl');records=[];clients=[]
|
||||
fds=[os.open(node,os.O_RDWR),os.open(node,os.O_RDWR)]
|
||||
def request(fd,label,n,top,payload=None):
|
||||
errno=0
|
||||
try:fcntl.ioctl(fd,0xc0004600|(len(top)<<16)|n,top,True)
|
||||
except OSError as e:errno=e.errno
|
||||
offset={0x2b:40 if len(top)==48 else 28,0x2a:28,0x29:12}[n]
|
||||
row={'label':label,'linux_errno':errno,'status':struct.unpack_from('<I',top,offset)[0],'response':top.hex()}
|
||||
if payload is not None:row['payload']=bytes(payload).hex()
|
||||
records.append(row);return row
|
||||
try:
|
||||
def alloc(label,legacy=False,root=0,parent=0,new=0,cls=0x41):
|
||||
top=bytearray(32 if legacy else 48);struct.pack_into('<IIII',top,0,root,parent,new,cls);row=request(fds[0],label,0x2b,top)
|
||||
if not row['linux_errno'] and row['status']==0:
|
||||
handle=struct.unpack_from('<I',top,8)[0];clients.append(handle);return handle
|
||||
return 0
|
||||
root=alloc('root64');assert root
|
||||
cases=[('root_parent',root,1),('root_class',root,2),('invalid_index',root,0),('unknown_index',root,3),('missing_object',0xfffffff0,2),('missing_object_invalid_index',0xfffffff0,0)]
|
||||
for label,obj,index in cases:
|
||||
data=(C.c_ubyte*16).from_buffer_copy(struct.pack('<IIQ',obj,index,0xa5a5a5a5a5a5a5a5));top=bytearray(struct.pack('<IIIIQII',root,root,0xd02,0,C.addressof(data),16,0));request(fds[0],label,0x2a,top,data)
|
||||
for size in [0,8,15,17]:
|
||||
data=(C.c_ubyte*32).from_buffer_copy(struct.pack('<IIQ',root,2,0xa5a5a5a5a5a5a5a5)+bytes([0xa5])*16);top=bytearray(struct.pack('<IIIIQII',root,root,0xd02,0,C.addressof(data),size,0));request(fds[0],f'handle_size_{size}',0x2a,top,data)
|
||||
for target in [0,0xfffffff0,root]:
|
||||
data=(C.c_ubyte*16).from_buffer_copy(struct.pack('<IIII',root,2,0xa5a5a5a5,0xa5a5a5a5));top=bytearray(struct.pack('<IIIIQII',root,target,0xd02,0,C.addressof(data),16,0));request(fds[0],f'control_target_{target:#x}',0x2a,top,data)
|
||||
for obj in [root,0,0xfffffff0]:
|
||||
data=(C.c_ubyte*12).from_buffer_copy(struct.pack('<III',obj,0,0xa5a5a5a5));top=bytearray(struct.pack('<IIIIQII',root,root,0xd01,0,C.addressof(data),12,0));request(fds[0],f'address_space_{obj:#x}',0x2a,top,data)
|
||||
for size in [8,16]:
|
||||
data=(C.c_ubyte*16).from_buffer_copy(struct.pack('<IIII',root,0,0xa5a5a5a5,0xa5a5a5a5));top=bytearray(struct.pack('<IIIIQII',root,root,0xd01,0,C.addressof(data),size,0));request(fds[0],f'address_space_size_{size}',0x2a,top,data)
|
||||
for invalidclient,size in [(0,16),(0,15),(0xfffffff0,16),(0xfffffff0,15)]:
|
||||
data=(C.c_ubyte*16).from_buffer_copy(struct.pack('<IIQ',root,2,0xa5a5a5a5a5a5a5a5));top=bytearray(struct.pack('<IIIIQII',invalidclient,root,0xd02,0,C.addressof(data),size,0));request(fds[0],f'client_{invalidclient:#x}_size_{size}',0x2a,top,data)
|
||||
for flag in [1,2]:
|
||||
data=(C.c_ubyte*16).from_buffer_copy(struct.pack('<IIQ',root,2,0xa5a5a5a5a5a5a5a5));top=bytearray(struct.pack('<IIIIQII',root,root,0xd02,flag,C.addressof(data),16,0));request(fds[0],f'control_flags_{flag}',0x2a,top,data)
|
||||
for label,legacy,r,p,n in [('root21',True,0,0,0),('requested_root21',True,0,0,0x24680001),('requested_root64',False,0,0,0x24680002),('nonzero_root_field',False,root,0,0),('nonzero_parent_field',False,0,root,0)]:alloc(label,legacy,r,p,n)
|
||||
finally:
|
||||
for h in clients:
|
||||
request(fds[0],'cleanup',0x29,bytearray(struct.pack('<IIII',h,0,h,0)))
|
||||
for fd in fds:os.close(fd)
|
||||
print(json.dumps({'scope':'Software roots and root controls only; no GPU objects, GPU memory or hardware operations requested','node':node,'driver_version':pathlib.Path('/sys/module/nvidia/version').read_text().strip(),'records':records,'time_ns':time.time_ns()},indent=2))
|
||||
import ctypes as C, fcntl, os, struct, json, pathlib, time
|
||||
|
||||
|
||||
def _read_module_version(node: str) -> str:
|
||||
module_name = 'nvidia_rs' if node == '/dev/nvidia-rsctl' else 'nvidia'
|
||||
try:
|
||||
return (pathlib.Path('/sys/module') / module_name / 'version').read_text(encoding='utf-8').strip()
|
||||
except OSError:
|
||||
return 'unloaded'
|
||||
|
||||
|
||||
def main():
|
||||
node = os.environ.get('NV_RS_NODE', '/dev/nvidiactl')
|
||||
records = []
|
||||
clients = []
|
||||
fds = []
|
||||
|
||||
def request(fd, label, n, top, payload=None):
|
||||
errno = 0
|
||||
try:
|
||||
fcntl.ioctl(fd, 0xc0004600 | (len(top) << 16) | n, top, True)
|
||||
except OSError as e:
|
||||
errno = e.errno
|
||||
offset = {0x2b: 40 if len(top) == 48 else 28, 0x2a: 28, 0x29: 12}[n]
|
||||
row = {'label': label, 'linux_errno': errno, 'status': struct.unpack_from('<I', top, offset)[0], 'response': top.hex()}
|
||||
if payload is not None:
|
||||
row['payload'] = bytes(payload).hex()
|
||||
records.append(row)
|
||||
return row
|
||||
|
||||
try:
|
||||
fds.append(os.open(node, os.O_RDWR))
|
||||
fds.append(os.open(node, os.O_RDWR))
|
||||
|
||||
def alloc(label, legacy=False, root=0, parent=0, new=0, cls=0x41):
|
||||
top = bytearray(32 if legacy else 48)
|
||||
struct.pack_into('<IIII', top, 0, root, parent, new, cls)
|
||||
row = request(fds[0], label, 0x2b, top)
|
||||
if not row['linux_errno'] and row['status'] == 0:
|
||||
handle = struct.unpack_from('<I', top, 8)[0]
|
||||
clients.append(handle)
|
||||
return handle
|
||||
return 0
|
||||
|
||||
root = alloc('root64')
|
||||
if not root:
|
||||
raise RuntimeError('root64 allocation failed')
|
||||
cases = [('root_parent', root, 1), ('root_class', root, 2), ('invalid_index', root, 0), ('unknown_index', root, 3), ('missing_object', 0xfffffff0, 2), ('missing_object_invalid_index', 0xfffffff0, 0)]
|
||||
for label, obj, index in cases:
|
||||
data = (C.c_ubyte * 16).from_buffer_copy(struct.pack('<IIQ', obj, index, 0xa5a5a5a5a5a5a5a5))
|
||||
top = bytearray(struct.pack('<IIIIQII', root, root, 0xd02, 0, C.addressof(data), 16, 0))
|
||||
request(fds[0], label, 0x2a, top, data)
|
||||
for size in [0, 8, 15, 17]:
|
||||
data = (C.c_ubyte * 32).from_buffer_copy(struct.pack('<IIQ', root, 2, 0xa5a5a5a5a5a5a5a5) + bytes([0xa5]) * 16)
|
||||
top = bytearray(struct.pack('<IIIIQII', root, root, 0xd02, 0, C.addressof(data), size, 0))
|
||||
request(fds[0], f'handle_size_{size}', 0x2a, top, data)
|
||||
for target in [0, 0xfffffff0, root]:
|
||||
data = (C.c_ubyte * 16).from_buffer_copy(struct.pack('<IIII', root, 2, 0xa5a5a5a5, 0xa5a5a5a5))
|
||||
top = bytearray(struct.pack('<IIIIQII', root, target, 0xd02, 0, C.addressof(data), 16, 0))
|
||||
request(fds[0], f'control_target_{target:#x}', 0x2a, top, data)
|
||||
for obj in [root, 0, 0xfffffff0]:
|
||||
data = (C.c_ubyte * 12).from_buffer_copy(struct.pack('<III', obj, 0, 0xa5a5a5a5))
|
||||
top = bytearray(struct.pack('<IIIIQII', root, root, 0xd01, 0, C.addressof(data), 12, 0))
|
||||
request(fds[0], f'address_space_{obj:#x}', 0x2a, top, data)
|
||||
for size in [8, 16]:
|
||||
data = (C.c_ubyte * 16).from_buffer_copy(struct.pack('<IIII', root, 0, 0xa5a5a5a5, 0xa5a5a5a5))
|
||||
top = bytearray(struct.pack('<IIIIQII', root, root, 0xd01, 0, C.addressof(data), size, 0))
|
||||
request(fds[0], f'address_space_size_{size}', 0x2a, top, data)
|
||||
for invalidclient, size in [(0, 16), (0, 15), (0xfffffff0, 16), (0xfffffff0, 15)]:
|
||||
data = (C.c_ubyte * 16).from_buffer_copy(struct.pack('<IIQ', root, 2, 0xa5a5a5a5a5a5a5a5))
|
||||
top = bytearray(struct.pack('<IIIIQII', invalidclient, root, 0xd02, 0, C.addressof(data), size, 0))
|
||||
request(fds[0], f'client_{invalidclient:#x}_size_{size}', 0x2a, top, data)
|
||||
for flag in [1, 2]:
|
||||
data = (C.c_ubyte * 16).from_buffer_copy(struct.pack('<IIQ', root, 2, 0xa5a5a5a5a5a5a5a5))
|
||||
top = bytearray(struct.pack('<IIIIQII', root, root, 0xd02, flag, C.addressof(data), 16, 0))
|
||||
request(fds[0], f'control_flags_{flag}', 0x2a, top, data)
|
||||
for label, legacy, r, p, n in [('root21', True, 0, 0, 0), ('requested_root21', True, 0, 0, 0x24680001), ('requested_root64', False, 0, 0, 0x24680002), ('nonzero_root_field', False, root, 0, 0), ('nonzero_parent_field', False, 0, root, 0)]:
|
||||
alloc(label, legacy, r, p, n)
|
||||
finally:
|
||||
if fds:
|
||||
for h in clients:
|
||||
request(fds[0], 'cleanup', 0x29, bytearray(struct.pack('<IIII', h, 0, h, 0)))
|
||||
for fd in fds:
|
||||
os.close(fd)
|
||||
print(json.dumps({'scope': 'Software roots and root controls only; no GPU objects, GPU memory or hardware operations requested', 'node': node, 'driver_version': _read_module_version(node), 'records': records, 'time_ns': time.time_ns()}, indent=2))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
|
||||
@ -1,44 +1,99 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Bounded original-driver CPU client/control parity; no GPU resources allocated."""
|
||||
import ctypes as C,fcntl,os,struct,json,pathlib,time
|
||||
node=os.environ.get('NV_RS_NODE','/dev/nvidiactl');records=[];clients=[]
|
||||
fds=[os.open(node,os.O_RDWR),os.open(node,os.O_RDWR)]
|
||||
def request(fd,label,n,top,payload=None):
|
||||
errno=0
|
||||
try:fcntl.ioctl(fd,0xc0004600|(len(top)<<16)|n,top,True)
|
||||
except OSError as e:errno=e.errno
|
||||
offset={0x2b:40 if len(top)==48 else 28,0x2a:28,0x29:12}[n]
|
||||
row={'label':label,'linux_errno':errno,'status':struct.unpack_from('<I',top,offset)[0],'response':top.hex()}
|
||||
if payload is not None:row['payload']=bytes(payload).hex()
|
||||
records.append(row);return row
|
||||
try:
|
||||
def alloc(label,legacy=False,root=0,parent=0,new=0,cls=0x41):
|
||||
top=bytearray(32 if legacy else 48);struct.pack_into('<IIII',top,0,root,parent,new,cls);row=request(fds[0],label,0x2b,top)
|
||||
if not row['linux_errno'] and row['status']==0:
|
||||
handle=struct.unpack_from('<I',top,8)[0];clients.append(handle);return handle
|
||||
if struct.unpack_from('<I',top,8)[0]:
|
||||
failed=struct.unpack_from('<I',top,8)[0];payload=(C.c_uint32*4)(failed,2,0xa5a5a5a5,0xa5a5a5a5);query=bytearray(struct.pack('<IIIIQII',failed,failed,0xd02,0,C.addressof(payload),16,0));check=request(fds[0],label+'_post_failure_lookup',0x2a,query,payload)
|
||||
if check['status']==0:clients.append(failed)
|
||||
return 0
|
||||
root=alloc('root64');assert root
|
||||
cases=[('root_parent',root,1),('root_class',root,2),('invalid_index',root,0),('unknown_index',root,3),('missing_object',0xfffffff0,2),('missing_object_invalid_index',0xfffffff0,0)]
|
||||
for label,obj,index in cases:
|
||||
data=(C.c_ubyte*16).from_buffer_copy(struct.pack('<IIQ',obj,index,0xa5a5a5a5a5a5a5a5));top=bytearray(struct.pack('<IIIIQII',root,root,0xd02,0,C.addressof(data),16,0));request(fds[0],label,0x2a,top,data)
|
||||
for size in [0,8,15,17]:
|
||||
data=(C.c_ubyte*32).from_buffer_copy(struct.pack('<IIQ',root,2,0xa5a5a5a5a5a5a5a5)+bytes([0xa5])*16);top=bytearray(struct.pack('<IIIIQII',root,root,0xd02,0,C.addressof(data),size,0));request(fds[0],f'handle_size_{size}',0x2a,top,data)
|
||||
for target in [0,0xfffffff0,root]:
|
||||
data=(C.c_ubyte*16).from_buffer_copy(struct.pack('<IIII',root,2,0xa5a5a5a5,0xa5a5a5a5));top=bytearray(struct.pack('<IIIIQII',root,target,0xd02,0,C.addressof(data),16,0));request(fds[0],f'control_target_{target:#x}',0x2a,top,data)
|
||||
for obj in [root,0,0xfffffff0]:
|
||||
data=(C.c_ubyte*12).from_buffer_copy(struct.pack('<III',obj,0,0xa5a5a5a5));top=bytearray(struct.pack('<IIIIQII',root,root,0xd01,0,C.addressof(data),12,0));request(fds[0],f'address_space_{obj:#x}',0x2a,top,data)
|
||||
for size in [8,16]:
|
||||
data=(C.c_ubyte*16).from_buffer_copy(struct.pack('<IIII',root,0,0xa5a5a5a5,0xa5a5a5a5));top=bytearray(struct.pack('<IIIIQII',root,root,0xd01,0,C.addressof(data),size,0));request(fds[0],f'address_space_size_{size}',0x2a,top,data)
|
||||
for invalidclient,size in [(0,16),(0,15),(0xfffffff0,16),(0xfffffff0,15)]:
|
||||
data=(C.c_ubyte*16).from_buffer_copy(struct.pack('<IIQ',root,2,0xa5a5a5a5a5a5a5a5));top=bytearray(struct.pack('<IIIIQII',invalidclient,root,0xd02,0,C.addressof(data),size,0));request(fds[0],f'client_{invalidclient:#x}_size_{size}',0x2a,top,data)
|
||||
for flag in [1,2,3,4,8,16,32]:
|
||||
data=(C.c_ubyte*16).from_buffer_copy(struct.pack('<IIQ',root,2,0xa5a5a5a5a5a5a5a5));top=bytearray(struct.pack('<IIIIQII',root,root,0xd02,flag,C.addressof(data),16,0));request(fds[0],f'control_flags_{flag}',0x2a,top,data)
|
||||
for label,legacy,r,p,n in [('root21',True,0,0,0),('requested_root21',True,0,0,0x24680001),('requested_root64',False,0,0,0x24680002),('nonzero_root_field',False,root,0,0),('requested_root_field',False,0x24680004,0,0),('nonzero_parent_field',False,0,root,0)]:alloc(label,legacy,r,p,n)
|
||||
finally:
|
||||
for h in clients:
|
||||
request(fds[0],'cleanup',0x29,bytearray(struct.pack('<IIII',h,0,h,0)))
|
||||
for fd in fds:os.close(fd)
|
||||
print(json.dumps({'scope':'Software roots and root controls only; no GPU objects, GPU memory or hardware operations requested','node':node,'driver_version':pathlib.Path('/sys/module/nvidia/version').read_text().strip(),'records':records,'time_ns':time.time_ns()},indent=2))
|
||||
import ctypes as C, fcntl, os, struct, json, pathlib, time
|
||||
|
||||
|
||||
def _read_module_version(node: str) -> str:
|
||||
module_name = 'nvidia_rs' if node == '/dev/nvidia-rsctl' else 'nvidia'
|
||||
try:
|
||||
return (pathlib.Path('/sys/module') / module_name / 'version').read_text(encoding='utf-8').strip()
|
||||
except OSError:
|
||||
return 'unloaded'
|
||||
|
||||
|
||||
def main():
|
||||
node = os.environ.get('NV_RS_NODE', '/dev/nvidiactl')
|
||||
records = []
|
||||
clients = []
|
||||
fds = []
|
||||
|
||||
def request(fd, label, n, top, payload=None):
|
||||
errno = 0
|
||||
try:
|
||||
fcntl.ioctl(fd, 0xc0004600 | (len(top) << 16) | n, top, True)
|
||||
except OSError as e:
|
||||
errno = e.errno
|
||||
offset = {0x2b: 40 if len(top) == 48 else 28, 0x2a: 28, 0x29: 12}[n]
|
||||
row = {'label': label, 'linux_errno': errno, 'status': struct.unpack_from('<I', top, offset)[0], 'response': top.hex()}
|
||||
if payload is not None:
|
||||
row['payload'] = bytes(payload).hex()
|
||||
records.append(row)
|
||||
return row
|
||||
|
||||
try:
|
||||
fds.append(os.open(node, os.O_RDWR))
|
||||
fds.append(os.open(node, os.O_RDWR))
|
||||
|
||||
def alloc(label, legacy=False, root=0, parent=0, new=0, cls=0x41):
|
||||
top = bytearray(32 if legacy else 48)
|
||||
struct.pack_into('<IIII', top, 0, root, parent, new, cls)
|
||||
row = request(fds[0], label, 0x2b, top)
|
||||
if not row['linux_errno'] and row['status'] == 0:
|
||||
handle = struct.unpack_from('<I', top, 8)[0]
|
||||
clients.append(handle)
|
||||
return handle
|
||||
if struct.unpack_from('<I', top, 8)[0]:
|
||||
failed = struct.unpack_from('<I', top, 8)[0]
|
||||
payload = (C.c_uint32 * 4)(failed, 2, 0xa5a5a5a5, 0xa5a5a5a5)
|
||||
query = bytearray(struct.pack('<IIIIQII', failed, failed, 0xd02, 0, C.addressof(payload), 16, 0))
|
||||
check = request(fds[0], label + '_post_failure_lookup', 0x2a, query, payload)
|
||||
if check['status'] == 0:
|
||||
clients.append(failed)
|
||||
return 0
|
||||
|
||||
root = alloc('root64')
|
||||
if not root:
|
||||
raise RuntimeError('root64 allocation failed')
|
||||
cases = [('root_parent', root, 1), ('root_class', root, 2), ('invalid_index', root, 0), ('unknown_index', root, 3), ('missing_object', 0xfffffff0, 2), ('missing_object_invalid_index', 0xfffffff0, 0)]
|
||||
for label, obj, index in cases:
|
||||
data = (C.c_ubyte * 16).from_buffer_copy(struct.pack('<IIQ', obj, index, 0xa5a5a5a5a5a5a5a5))
|
||||
top = bytearray(struct.pack('<IIIIQII', root, root, 0xd02, 0, C.addressof(data), 16, 0))
|
||||
request(fds[0], label, 0x2a, top, data)
|
||||
for size in [0, 8, 15, 17]:
|
||||
data = (C.c_ubyte * 32).from_buffer_copy(struct.pack('<IIQ', root, 2, 0xa5a5a5a5a5a5a5a5) + bytes([0xa5]) * 16)
|
||||
top = bytearray(struct.pack('<IIIIQII', root, root, 0xd02, 0, C.addressof(data), size, 0))
|
||||
request(fds[0], f'handle_size_{size}', 0x2a, top, data)
|
||||
for target in [0, 0xfffffff0, root]:
|
||||
data = (C.c_ubyte * 16).from_buffer_copy(struct.pack('<IIII', root, 2, 0xa5a5a5a5, 0xa5a5a5a5))
|
||||
top = bytearray(struct.pack('<IIIIQII', root, target, 0xd02, 0, C.addressof(data), 16, 0))
|
||||
request(fds[0], f'control_target_{target:#x}', 0x2a, top, data)
|
||||
for obj in [root, 0, 0xfffffff0]:
|
||||
data = (C.c_ubyte * 12).from_buffer_copy(struct.pack('<III', obj, 0, 0xa5a5a5a5))
|
||||
top = bytearray(struct.pack('<IIIIQII', root, root, 0xd01, 0, C.addressof(data), 12, 0))
|
||||
request(fds[0], f'address_space_{obj:#x}', 0x2a, top, data)
|
||||
for size in [8, 16]:
|
||||
data = (C.c_ubyte * 16).from_buffer_copy(struct.pack('<IIII', root, 0, 0xa5a5a5a5, 0xa5a5a5a5))
|
||||
top = bytearray(struct.pack('<IIIIQII', root, root, 0xd01, 0, C.addressof(data), size, 0))
|
||||
request(fds[0], f'address_space_size_{size}', 0x2a, top, data)
|
||||
for invalidclient, size in [(0, 16), (0, 15), (0xfffffff0, 16), (0xfffffff0, 15)]:
|
||||
data = (C.c_ubyte * 16).from_buffer_copy(struct.pack('<IIQ', root, 2, 0xa5a5a5a5a5a5a5a5))
|
||||
top = bytearray(struct.pack('<IIIIQII', invalidclient, root, 0xd02, 0, C.addressof(data), size, 0))
|
||||
request(fds[0], f'client_{invalidclient:#x}_size_{size}', 0x2a, top, data)
|
||||
for flag in [1, 2, 3, 4, 8, 16, 32]:
|
||||
data = (C.c_ubyte * 16).from_buffer_copy(struct.pack('<IIQ', root, 2, 0xa5a5a5a5a5a5a5a5))
|
||||
top = bytearray(struct.pack('<IIIIQII', root, root, 0xd02, flag, C.addressof(data), 16, 0))
|
||||
request(fds[0], f'control_flags_{flag}', 0x2a, top, data)
|
||||
for label, legacy, r, p, n in [('root21', True, 0, 0, 0), ('requested_root21', True, 0, 0, 0x24680001), ('requested_root64', False, 0, 0, 0x24680002), ('nonzero_root_field', False, root, 0, 0), ('requested_root_field', False, 0x24680004, 0, 0), ('nonzero_parent_field', False, 0, root, 0)]:
|
||||
alloc(label, legacy, r, p, n)
|
||||
finally:
|
||||
if fds:
|
||||
for h in clients:
|
||||
request(fds[0], 'cleanup', 0x29, bytearray(struct.pack('<IIII', h, 0, h, 0)))
|
||||
for fd in fds:
|
||||
os.close(fd)
|
||||
print(json.dumps({'scope': 'Software roots and root controls only; no GPU objects, GPU memory or hardware operations requested', 'node': node, 'driver_version': _read_module_version(node), 'records': records, 'time_ns': time.time_ns()}, indent=2))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
|
||||
@ -1,49 +1,122 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Bounded original-driver CPU client/control parity; no GPU resources allocated."""
|
||||
import ctypes as C,fcntl,os,struct,json,pathlib,time,hashlib,subprocess
|
||||
node=os.environ.get('NV_RS_NODE','/dev/nvidiactl');records=[];clients=[]
|
||||
if node not in ['/dev/nvidiactl','/dev/nvidia-rsctl']:raise SystemExit('Explicit original or isolated Rust node required')
|
||||
module_name='nvidia_rs' if node=='/dev/nvidia-rsctl' else 'nvidia'
|
||||
module_sys=pathlib.Path('/sys/module')/module_name
|
||||
module_file=pathlib.Path(__file__).resolve().parent.parent/'kernel-open/nvidia_rs.ko' if module_name=='nvidia_rs' else pathlib.Path(subprocess.check_output(['modinfo','-n','nvidia'],text=True).strip())
|
||||
module_identity={'name':module_name,'version':(module_sys/'version').read_text().strip(),'file':str(module_file),'file_sha256':hashlib.sha256(module_file.read_bytes()).hexdigest(),'srcversion':(module_sys/'srcversion').read_text().strip() if (module_sys/'srcversion').exists() else None,'boundary':'File digest and loaded sysfs metadata sampled before requests; wrapper records the module load operation separately.'}
|
||||
fds=[os.open(node,os.O_RDWR),os.open(node,os.O_RDWR)]
|
||||
def request(fd,label,n,top,payload=None):
|
||||
before=top.hex();payload_before=bytes(payload).hex() if payload is not None else None;errno=0
|
||||
try:fcntl.ioctl(fd,0xc0004600|(len(top)<<16)|n,top,True)
|
||||
except OSError as e:errno=e.errno
|
||||
offset={0x2b:40 if len(top)==48 else 28,0x2a:28,0x29:12}[n]
|
||||
row={'input_top':before,'input_payload':payload_before,'request':hex(0xc0004600|(len(top)<<16)|n),'label':label,'linux_errno':errno,'status':struct.unpack_from('<I',top,offset)[0],'response':top.hex()}
|
||||
if payload is not None:row['payload']=bytes(payload).hex()
|
||||
records.append(row);return row
|
||||
try:
|
||||
def alloc(label,legacy=False,root=0,parent=0,new=0,cls=0x41):
|
||||
top=bytearray(32 if legacy else 48);struct.pack_into('<IIII',top,0,root,parent,new,cls);row=request(fds[0],label,0x2b,top)
|
||||
if not row['linux_errno'] and row['status']==0:
|
||||
handle=struct.unpack_from('<I',top,8)[0];clients.append(handle);return handle
|
||||
if struct.unpack_from('<I',top,8)[0]:
|
||||
failed=struct.unpack_from('<I',top,8)[0];payload=(C.c_uint32*4)(failed,2,0xa5a5a5a5,0xa5a5a5a5);query=bytearray(struct.pack('<IIIIQII',failed,failed,0xd02,0,C.addressof(payload),16,0));check=request(fds[0],label+'_post_failure_lookup',0x2a,query,payload)
|
||||
if check['status']==0:clients.append(failed)
|
||||
return 0
|
||||
root=alloc('root64');assert root
|
||||
cases=[('root_parent',root,1),('root_class',root,2),('invalid_index',root,0),('unknown_index',root,3),('missing_object',0xfffffff0,2),('missing_object_invalid_index',0xfffffff0,0)]
|
||||
for label,obj,index in cases:
|
||||
data=(C.c_ubyte*16).from_buffer_copy(struct.pack('<IIQ',obj,index,0xa5a5a5a5a5a5a5a5));top=bytearray(struct.pack('<IIIIQII',root,root,0xd02,0,C.addressof(data),16,0));request(fds[0],label,0x2a,top,data)
|
||||
for size in [0,8,15,17]:
|
||||
data=(C.c_ubyte*32).from_buffer_copy(struct.pack('<IIQ',root,2,0xa5a5a5a5a5a5a5a5)+bytes([0xa5])*16);top=bytearray(struct.pack('<IIIIQII',root,root,0xd02,0,C.addressof(data),size,0));request(fds[0],f'handle_size_{size}',0x2a,top,data)
|
||||
for target in [0,0xfffffff0,root]:
|
||||
data=(C.c_ubyte*16).from_buffer_copy(struct.pack('<IIII',root,2,0xa5a5a5a5,0xa5a5a5a5));top=bytearray(struct.pack('<IIIIQII',root,target,0xd02,0,C.addressof(data),16,0));request(fds[0],f'control_target_{target:#x}',0x2a,top,data)
|
||||
for obj in [root,0,0xfffffff0]:
|
||||
data=(C.c_ubyte*12).from_buffer_copy(struct.pack('<III',obj,0,0xa5a5a5a5));top=bytearray(struct.pack('<IIIIQII',root,root,0xd01,0,C.addressof(data),12,0));request(fds[0],f'address_space_{obj:#x}',0x2a,top,data)
|
||||
for size in [8,16]:
|
||||
data=(C.c_ubyte*16).from_buffer_copy(struct.pack('<IIII',root,0,0xa5a5a5a5,0xa5a5a5a5));top=bytearray(struct.pack('<IIIIQII',root,root,0xd01,0,C.addressof(data),size,0));request(fds[0],f'address_space_size_{size}',0x2a,top,data)
|
||||
for invalidclient,size in [(0,16),(0,15),(0xfffffff0,16),(0xfffffff0,15)]:
|
||||
data=(C.c_ubyte*16).from_buffer_copy(struct.pack('<IIQ',root,2,0xa5a5a5a5a5a5a5a5));top=bytearray(struct.pack('<IIIIQII',invalidclient,root,0xd02,0,C.addressof(data),size,0));request(fds[0],f'client_{invalidclient:#x}_size_{size}',0x2a,top,data)
|
||||
for flag in [1,2,3,4,8,16,32]:
|
||||
data=(C.c_ubyte*16).from_buffer_copy(struct.pack('<IIQ',root,2,0xa5a5a5a5a5a5a5a5));top=bytearray(struct.pack('<IIIIQII',root,root,0xd02,flag,C.addressof(data),16,0));request(fds[0],f'control_flags_{flag}',0x2a,top,data)
|
||||
for label,legacy,r,p,n in [('root21',True,0,0,0),('requested_root21',True,0,0,0x24680001),('requested_root64',False,0,0,0x24680002),('nonzero_root_field',False,root,0,0),('requested_root_field',False,0x24680004,0,0),('nonzero_parent_field',False,0,root,0)]:alloc(label,legacy,r,p,n)
|
||||
finally:
|
||||
for h in clients:
|
||||
request(fds[0],'cleanup',0x29,bytearray(struct.pack('<IIII',h,0,h,0)))
|
||||
for fd in fds:os.close(fd)
|
||||
print(json.dumps({'scope':'Software roots and root controls only; no GPU objects, GPU memory or hardware operations requested','node':node,'driver_version':module_identity['version'],'module_identity':module_identity,'records':records,'time_ns':time.time_ns()},indent=2))
|
||||
import ctypes as C, fcntl, os, shutil, struct, json, pathlib, time, hashlib, subprocess
|
||||
|
||||
|
||||
def _find_modinfo() -> str:
|
||||
found = shutil.which('modinfo')
|
||||
if found:
|
||||
return found
|
||||
for candidate in ('/usr/sbin/modinfo', '/sbin/modinfo'):
|
||||
if pathlib.Path(candidate).is_file():
|
||||
return candidate
|
||||
return 'modinfo'
|
||||
|
||||
|
||||
def main():
|
||||
node = os.environ.get('NV_RS_NODE', '/dev/nvidiactl')
|
||||
records = []
|
||||
clients = []
|
||||
if node not in ('/dev/nvidiactl', '/dev/nvidia-rsctl'):
|
||||
raise SystemExit('Explicit original or isolated Rust node required')
|
||||
module_name = 'nvidia_rs' if node == '/dev/nvidia-rsctl' else 'nvidia'
|
||||
module_sys = pathlib.Path('/sys/module') / module_name
|
||||
if module_name == 'nvidia_rs':
|
||||
module_file = pathlib.Path(__file__).resolve().parent.parent / 'kernel-open/nvidia_rs.ko'
|
||||
else:
|
||||
try:
|
||||
module_file = pathlib.Path(subprocess.check_output([_find_modinfo(), '-n', 'nvidia'], text=True).strip())
|
||||
except (OSError, subprocess.CalledProcessError) as error:
|
||||
raise SystemExit(f'failed to locate nvidia module via modinfo: {error}') from error
|
||||
module_identity = {
|
||||
'name': module_name,
|
||||
'version': (module_sys / 'version').read_text(encoding='utf-8').strip(),
|
||||
'file': str(module_file),
|
||||
'file_sha256': hashlib.sha256(module_file.read_bytes()).hexdigest(),
|
||||
'srcversion': (module_sys / 'srcversion').read_text(encoding='utf-8').strip() if (module_sys / 'srcversion').exists() else None,
|
||||
'boundary': 'File digest and loaded sysfs metadata sampled before requests; wrapper records the module load operation separately.',
|
||||
}
|
||||
fds = []
|
||||
|
||||
def request(fd, label, n, top, payload=None):
|
||||
before = top.hex()
|
||||
payload_before = bytes(payload).hex() if payload is not None else None
|
||||
errno = 0
|
||||
try:
|
||||
fcntl.ioctl(fd, 0xc0004600 | (len(top) << 16) | n, top, True)
|
||||
except OSError as e:
|
||||
errno = e.errno
|
||||
offset = {0x2b: 40 if len(top) == 48 else 28, 0x2a: 28, 0x29: 12}[n]
|
||||
row = {'input_top': before, 'input_payload': payload_before, 'request': hex(0xc0004600 | (len(top) << 16) | n), 'label': label, 'linux_errno': errno, 'status': struct.unpack_from('<I', top, offset)[0], 'response': top.hex()}
|
||||
if payload is not None:
|
||||
row['payload'] = bytes(payload).hex()
|
||||
records.append(row)
|
||||
return row
|
||||
|
||||
try:
|
||||
fds.append(os.open(node, os.O_RDWR))
|
||||
fds.append(os.open(node, os.O_RDWR))
|
||||
|
||||
def alloc(label, legacy=False, root=0, parent=0, new=0, cls=0x41):
|
||||
top = bytearray(32 if legacy else 48)
|
||||
struct.pack_into('<IIII', top, 0, root, parent, new, cls)
|
||||
row = request(fds[0], label, 0x2b, top)
|
||||
if not row['linux_errno'] and row['status'] == 0:
|
||||
handle = struct.unpack_from('<I', top, 8)[0]
|
||||
clients.append(handle)
|
||||
return handle
|
||||
if struct.unpack_from('<I', top, 8)[0]:
|
||||
failed = struct.unpack_from('<I', top, 8)[0]
|
||||
payload = (C.c_uint32 * 4)(failed, 2, 0xa5a5a5a5, 0xa5a5a5a5)
|
||||
query = bytearray(struct.pack('<IIIIQII', failed, failed, 0xd02, 0, C.addressof(payload), 16, 0))
|
||||
check = request(fds[0], label + '_post_failure_lookup', 0x2a, query, payload)
|
||||
if check['status'] == 0:
|
||||
clients.append(failed)
|
||||
return 0
|
||||
|
||||
root = alloc('root64')
|
||||
if not root:
|
||||
raise RuntimeError('root64 allocation failed')
|
||||
cases = [('root_parent', root, 1), ('root_class', root, 2), ('invalid_index', root, 0), ('unknown_index', root, 3), ('missing_object', 0xfffffff0, 2), ('missing_object_invalid_index', 0xfffffff0, 0)]
|
||||
for label, obj, index in cases:
|
||||
data = (C.c_ubyte * 16).from_buffer_copy(struct.pack('<IIQ', obj, index, 0xa5a5a5a5a5a5a5a5))
|
||||
top = bytearray(struct.pack('<IIIIQII', root, root, 0xd02, 0, C.addressof(data), 16, 0))
|
||||
request(fds[0], label, 0x2a, top, data)
|
||||
for size in [0, 8, 15, 17]:
|
||||
data = (C.c_ubyte * 32).from_buffer_copy(struct.pack('<IIQ', root, 2, 0xa5a5a5a5a5a5a5a5) + bytes([0xa5]) * 16)
|
||||
top = bytearray(struct.pack('<IIIIQII', root, root, 0xd02, 0, C.addressof(data), size, 0))
|
||||
request(fds[0], f'handle_size_{size}', 0x2a, top, data)
|
||||
for target in [0, 0xfffffff0, root]:
|
||||
data = (C.c_ubyte * 16).from_buffer_copy(struct.pack('<IIII', root, 2, 0xa5a5a5a5, 0xa5a5a5a5))
|
||||
top = bytearray(struct.pack('<IIIIQII', root, target, 0xd02, 0, C.addressof(data), 16, 0))
|
||||
request(fds[0], f'control_target_{target:#x}', 0x2a, top, data)
|
||||
for obj in [root, 0, 0xfffffff0]:
|
||||
data = (C.c_ubyte * 12).from_buffer_copy(struct.pack('<III', obj, 0, 0xa5a5a5a5))
|
||||
top = bytearray(struct.pack('<IIIIQII', root, root, 0xd01, 0, C.addressof(data), 12, 0))
|
||||
request(fds[0], f'address_space_{obj:#x}', 0x2a, top, data)
|
||||
for size in [8, 16]:
|
||||
data = (C.c_ubyte * 16).from_buffer_copy(struct.pack('<IIII', root, 0, 0xa5a5a5a5, 0xa5a5a5a5))
|
||||
top = bytearray(struct.pack('<IIIIQII', root, root, 0xd01, 0, C.addressof(data), size, 0))
|
||||
request(fds[0], f'address_space_size_{size}', 0x2a, top, data)
|
||||
for invalidclient, size in [(0, 16), (0, 15), (0xfffffff0, 16), (0xfffffff0, 15)]:
|
||||
data = (C.c_ubyte * 16).from_buffer_copy(struct.pack('<IIQ', root, 2, 0xa5a5a5a5a5a5a5a5))
|
||||
top = bytearray(struct.pack('<IIIIQII', invalidclient, root, 0xd02, 0, C.addressof(data), size, 0))
|
||||
request(fds[0], f'client_{invalidclient:#x}_size_{size}', 0x2a, top, data)
|
||||
for flag in [1, 2, 3, 4, 8, 16, 32]:
|
||||
data = (C.c_ubyte * 16).from_buffer_copy(struct.pack('<IIQ', root, 2, 0xa5a5a5a5a5a5a5a5))
|
||||
top = bytearray(struct.pack('<IIIIQII', root, root, 0xd02, flag, C.addressof(data), 16, 0))
|
||||
request(fds[0], f'control_flags_{flag}', 0x2a, top, data)
|
||||
for label, legacy, r, p, n in [('root21', True, 0, 0, 0), ('requested_root21', True, 0, 0, 0x24680001), ('requested_root64', False, 0, 0, 0x24680002), ('nonzero_root_field', False, root, 0, 0), ('requested_root_field', False, 0x24680004, 0, 0), ('nonzero_parent_field', False, 0, root, 0)]:
|
||||
alloc(label, legacy, r, p, n)
|
||||
finally:
|
||||
if fds:
|
||||
for h in clients:
|
||||
request(fds[0], 'cleanup', 0x29, bytearray(struct.pack('<IIII', h, 0, h, 0)))
|
||||
for fd in fds:
|
||||
os.close(fd)
|
||||
print(json.dumps({'scope': 'Software roots and root controls only; no GPU objects, GPU memory or hardware operations requested', 'node': node, 'driver_version': module_identity['version'], 'module_identity': module_identity, 'records': records, 'time_ns': time.time_ns()}, indent=2))
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
|
||||
Reference in New Issue
Block a user