mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 06:28:39 +00:00
55 lines
4.9 KiB
Python
55 lines
4.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Actual original-vs-loaded Rust parity; normalize only opaque handles/pointers."""
|
|
from pathlib import Path
|
|
import argparse,json,hashlib
|
|
R=Path(__file__).resolve().parent.parent
|
|
p=argparse.ArgumentParser();p.add_argument('--original',type=Path,default=R/'reports/software-controls-replay-original.json');p.add_argument('--rust',type=Path,default=R/'reports/software-controls-rust-live.json');p.add_argument('--out',type=Path,default=R/'reports/software-controls-live-comparison.json');a=p.parse_args()
|
|
original=json.loads(a.original.read_text());rust=json.loads(a.rust.read_text());mapping={};cases=[]
|
|
def word(b,offset):return int.from_bytes(b[offset:offset+4],'little')
|
|
def mapped(b,offset):
|
|
key=word(b,offset)
|
|
if key in mapping:b[offset:offset+4]=mapping[key].to_bytes(4,'little')
|
|
def normalize_top(data,kind):
|
|
data=bytearray.fromhex(data)
|
|
for offset in ({0x2b:[0,4,8],0x2a:[0,4],0x29:[0,4,8]}[kind]):mapped(data,offset)
|
|
return data
|
|
def normalize_payload(data,kind):
|
|
data=bytearray.fromhex(data or '')
|
|
if kind==0x2a and len(data)>=4:mapped(data,0)
|
|
return data
|
|
if len(original['records'])!=len(rust['records']):raise SystemExit('Request sequence lengths differ')
|
|
if not original['records']:
|
|
raise SystemExit('No records to compare')
|
|
for i,(old,new) in enumerate(zip(original['records'],rust['records'])):
|
|
issues=[];req=int(old['request'],16);kind=req&255
|
|
if req!=int(new['request'],16):issues.append('request ioctl differs')
|
|
inp=normalize_top(old['input_top'],kind);actual_input=bytearray.fromhex(new['input_top']);ptr_offset=16 if kind in [0x2a,0x2b] else None
|
|
# Only known Python-backed non-null parameter pointers are normalized. NULL and
|
|
# sentinel/non-backed addresses would remain exact and expose validation gaps.
|
|
pointer_pair=None
|
|
if ptr_offset is not None and old.get('input_payload') is not None and new.get('input_payload') is not None:
|
|
oldptr=int.from_bytes(inp[ptr_offset:ptr_offset+8],'little');newptr=int.from_bytes(actual_input[ptr_offset:ptr_offset+8],'little')
|
|
if oldptr and newptr:pointer_pair=(oldptr,newptr);inp[ptr_offset:ptr_offset+8]=newptr.to_bytes(8,'little')
|
|
if inp!=actual_input:issues.append('normalized request wrapper differs')
|
|
if normalize_payload(old.get('input_payload'),kind)!=bytearray.fromhex(new.get('input_payload') or ''):issues.append('normalized input payload differs')
|
|
if old['linux_errno']!=new['linux_errno']:issues.append('Linux errno differs')
|
|
if old['status']!=new['status']:issues.append('NV status differs')
|
|
if kind==0x2b:
|
|
expected_obj=word(bytes.fromhex(old['response']),8);actual_obj=word(bytes.fromhex(new['response']),8)
|
|
if word(bytes.fromhex(old['input_top']),0)==0 and expected_obj and (old['status']==0 or expected_obj!=word(bytes.fromhex(old['input_top']),8)):
|
|
if expected_obj in mapping and mapping[expected_obj]!=actual_obj:issues.append('client handle mapping changed')
|
|
if actual_obj==0:issues.append('generated client became NULL')
|
|
if any(k!=expected_obj and v==actual_obj for k,v in mapping.items()):issues.append('opaque client mapping collapsed distinct handles')
|
|
mapping[expected_obj]=actual_obj
|
|
out=normalize_top(old['response'],kind);actual_out=bytearray.fromhex(new['response'])
|
|
if pointer_pair is not None:
|
|
oldptr,newptr=pointer_pair
|
|
if int.from_bytes(out[ptr_offset:ptr_offset+8],'little')==oldptr:out[ptr_offset:ptr_offset+8]=newptr.to_bytes(8,'little')
|
|
if out!=actual_out:issues.append('normalized response wrapper differs')
|
|
if normalize_payload(old.get('payload'),kind)!=bytearray.fromhex(new.get('payload') or ''):issues.append('normalized output payload differs')
|
|
label=old['label']
|
|
for before,after in mapping.items():label=label.replace(hex(before),hex(after))
|
|
if label!=new['label']:issues.append('case identity/order differs after handle normalization')
|
|
cases.append({'original_label':old['label'],'rust_label':new['label'],'passed':not issues,'issues':issues})
|
|
report={'passed':all(c['passed'] for c in cases),'scope':'Actual loaded Rust software-only controls vs original responses. Not CUDA or physical GPU parity.','cases':cases,'case_count':len(cases),'source_sha256':{str(f):hashlib.sha256(f.read_bytes()).hexdigest() for f in [a.original,a.rust]},'rust_module_identity':rust.get('module_identity'),'normalization':['Generated client handles mapped consistently/injectively at root allocation responses.','Only known non-null Python-backed nested parameter pointer fields are normalized.','No NV status, Linux errno, padding, method values or output payload data are excluded.'],'limits':['Runs occur at different times; identical deterministic control inputs and normalized opaque identifiers are compared.','This finite software-client suite does not establish GPU/backend/security/concurrency parity.']};a.out.write_text(json.dumps(report,indent=2)+'\n');print(json.dumps({'passed':report['passed'],'cases':len(cases),'failures':[c for c in cases if not c['passed']]},indent=2));raise SystemExit(0 if report['passed'] else 1)
|