Files
nvdriver/tools/check_root_migration_oracle.py
morebrackets e763a670fa Address Greptile findings on #287
- tools/check_ramin_oracle.py, tools/check_system_memory_abi.py,
  tools/check_root_migration_oracle.py (4777570886): require matching 580.178.04
  release metadata (version.mk / nvUnixVersion.h) before accepting any source
  checkout or standalone file, and replace hardcoded /home/geodd defaults with
  Path.home().
- tests/offline/test_native_shader_and_abi_tools.py (4777570880): run the
  check_native_shader fallback ELF test inside a python3 -O child process with
  pyelftools explicitly blocked via sys.modules and verify _FallbackELFFile
  executes; also test rejection of unversioned and 620.70.06 source trees.
2026-12-02 17:20:40 -08:00

141 lines
6.2 KiB
Python
Executable File

#!/usr/bin/env python3
"""Execute exact580 shared-root clipping callbacks with memory-only callback mocks."""
import hashlib
import json
import os
from pathlib import Path
import re
import subprocess
import tempfile
R = Path(__file__).resolve().parents[1]
EXPECTED_RELEASE = '580.178.04'
DEFAULT_ANALYSIS_ROOT = Path.home() / 'nvidia-driver/analysis-580.178.04'
VASPACE_REL = 'nvidia/src/kernel/mem_mgr/gpu_vaspace.c'
def _detect_release(path: Path) -> str | None:
cur = path.resolve()
if cur.is_file():
cur = cur.parent
for _ in range(7):
ver_mk = cur / 'version.mk'
if ver_mk.is_file():
m = re.search(r'^\s*NVIDIA_VERSION\s*[:?]?=\s*(\S+)', ver_mk.read_text(encoding='utf-8', errors='replace'), re.M)
if m:
return m.group(1).strip()
unix_ver = cur / 'src/common/sdk/nvidia/inc/nvUnixVersion.h'
if unix_ver.is_file():
m = re.search(r'#define\s+NV_VERSION_STRING\s+"([^"]+)"', unix_ver.read_text(encoding='utf-8', errors='replace'))
if m:
return m.group(1).strip()
if cur.parent == cur:
break
cur = cur.parent
return None
def find_gpu_vaspace_source() -> Path:
candidates: list[Path] = []
for env_name in ('NV_RS_OPEN_GPU_SRC', 'OPEN_GPU_ROOT'):
val = os.environ.get(env_name)
if val:
base = Path(val)
candidates.extend([base / 'src' / VASPACE_REL, base / VASPACE_REL, base])
analysis = Path(os.environ.get('NV_RS_ANALYSIS_ROOT', str(DEFAULT_ANALYSIS_ROOT)))
candidates.append(analysis / f'reference/open-gpu-kernel-modules-{EXPECTED_RELEASE}/src' / VASPACE_REL)
for candidate in candidates:
if candidate.is_file() and candidate.name == 'gpu_vaspace.c' and _detect_release(candidate) == EXPECTED_RELEASE:
return candidate.resolve()
raise SystemExit(
f'Cannot find open-gpu-kernel-modules-{EXPECTED_RELEASE} gpu_vaspace.c (matching release metadata required). '
'Set NV_RS_OPEN_GPU_SRC (or OPEN_GPU_ROOT / NV_RS_ANALYSIS_ROOT).'
)
def function(text: str, name: str) -> str:
start = text.rfind('static void', 0, text.index(name + '\n'))
pos = text.index('{', start)
level = 1
end = pos + 1
while level:
level += (text[end] == '{') - (text[end] == '}')
end += 1
return text[start:end]
PREFIX = r'''
#include <stdint.h>
#include <stdio.h>
#include <assert.h>
typedef uint32_t NvU32;typedef int MMU_WALK_FILL_STATE;
typedef struct {uint64_t vasStart,vaLimitInternal;} OBJGVASPACE;
typedef OBJGVASPACE OBJVASPACE;
typedef struct {OBJGVASPACE*pGVAS;} MMU_WALK_USER_CTX;
typedef struct {int unused;} MMU_FMT_LEVEL;
typedef struct {uint64_t data[4];} MMU_WALK_MEMDESC;
#define staticCast(v,t) ((t*)(v))
#define NV_MAX(a,b) ((a)>(b)?(a):(b))
#define NV_MIN(a,b) ((a)<(b)?(a):(b))
#define NV_ASSERT_OR_RETURN_VOID(v) assert(v)
static NvU32 mmuFmtVirtAddrToEntryIndex(const MMU_FMT_LEVEL*f,uint64_t v){(void)f;return v;}
static void fill(MMU_WALK_USER_CTX*c,const MMU_FMT_LEVEL*f,const MMU_WALK_MEMDESC*m,NvU32 lo,NvU32 hi,MMU_WALK_FILL_STATE state,NvU32*p){(void)c;(void)f;(void)state;for(unsigned i=lo;i<=hi;i++)((MMU_WALK_MEMDESC*)m)->data[i]=0x40;*p=hi-lo+1;}
static void copy(MMU_WALK_USER_CTX*c,const MMU_FMT_LEVEL*f,const MMU_WALK_MEMDESC*s,const MMU_WALK_MEMDESC*d,NvU32 lo,NvU32 hi,NvU32*p){(void)c;(void)f;for(unsigned i=lo;i<=hi;i++)((MMU_WALK_MEMDESC*)d)->data[i]=s->data[i];*p=hi-lo+1;}
static struct {void(*FillEntries)(MMU_WALK_USER_CTX*,const MMU_FMT_LEVEL*,const MMU_WALK_MEMDESC*,NvU32,NvU32,MMU_WALK_FILL_STATE,NvU32*);void(*CopyEntries)(MMU_WALK_USER_CTX*,const MMU_FMT_LEVEL*,const MMU_WALK_MEMDESC*,const MMU_WALK_MEMDESC*,NvU32,NvU32,NvU32*);}g_gmmuWalkCallbacks={fill,copy};
'''
MAIN_C = r'''
int main(void){for(unsigned old=1;old<=4;old++)for(unsigned new=1;new<=4;new++)for(unsigned lo=0;lo<6;lo++)for(unsigned hi=lo;hi<6;hi++){OBJGVASPACE v={lo,hi};MMU_WALK_USER_CTX c={&v};MMU_FMT_LEVEL f={0};MMU_WALK_MEMDESC src={{11,12,13,14}},dst={{99,99,99,99}};NvU32 p=0;_gmmuWalkCBCopyEntries_SkipExternal(&c,&f,&src,&dst,0,(old<new?old:new)-1,&p);if(p!=(old<new?old:new))return 1;if(new>old){_gmmuWalkCBFillEntries_SkipExternal(&c,&f,&dst,old,new-1,0,&p);if(p!=new-old)return 1;}printf("%u %u %u %u",old,new,lo,hi);for(unsigned i=0;i<new;i++)printf(" %llu",(unsigned long long)dst.data[i]);puts("");}return 0;}
'''
def main() -> None:
source_path = find_gpu_vaspace_source()
s = source_path.read_text(encoding='utf-8')
with tempfile.TemporaryDirectory() as t:
p = Path(t)
(p / 'oracle.c').write_text(
PREFIX
+ function(s, '_gmmuWalkCBFillEntries_SkipExternal')
+ function(s, '_gmmuWalkCBCopyEntries_SkipExternal')
+ MAIN_C,
encoding='utf-8',
)
subprocess.run(
['cc', '-std=c11', '-UNDEBUG', '-Wall', '-Werror', '-O2', str(p / 'oracle.c'), '-o', str(p / 'oracle')],
check=True,
)
rows = subprocess.check_output([str(p / 'oracle')], text=True).splitlines()
lines = [
'// Exact580 original shared-root callback clipping with memory-only normal callbacks.',
'pub type Vector = (u32,u32,u32,u32,&\'static [u64]);',
'#[rustfmt::skip]',
'pub const VECTORS: &[Vector] = &[',
]
for row in rows:
old, new, lo, hi, *data = map(int, row.split())
lines.append(f'({old},{new},{lo},{hi},&{data}),')
lines.append('];')
(R / 'src/nvidia/src/mmu/root_migration_oracle_vectors.rs').write_text('\n'.join(lines) + '\n', encoding='utf-8')
report = R / 'reports/root-migration-oracle.json'
report.parent.mkdir(parents=True, exist_ok=True)
report.write_text(
json.dumps(
{
'passed': True,
'vectors': len(rows),
'source': str(source_path),
'sha256': hashlib.sha256(source_path.read_bytes()).hexdigest(),
'executed': 'Original two SkipExternal callbacks with normal memory-only copy/fill callback mocks; no driver/GPU execution',
},
indent=2,
)
+ '\n',
encoding='utf-8',
)
print(len(rows), 'actual C root-copy vectors')
if __name__ == '__main__':
main()