Files
nvdriver/tools/check_system_memory_abi.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

93 lines
3.8 KiB
Python

#!/usr/bin/env python3
"""Compile exact shipped NV_MEMORY_ALLOCATION_PARAMS ABI assertions."""
import json
import os
from pathlib import Path
import re
import subprocess
import tempfile
ROOT = Path(__file__).resolve().parents[1]
EXPECTED_RELEASE = '580.178.04'
DEFAULT_ANALYSIS_ROOT = Path.home() / 'nvidia-driver/analysis-580.178.04'
FIELDS = {'owner': 0, 'type': 4, 'flags': 8, 'width': 12, 'height': 16,
'pitch': 20, 'attr': 24, 'attr2': 28, 'format': 32, 'comprCovg': 36,
'zcullCovg': 40, 'rangeLo': 48, 'rangeHi': 56, 'size': 64,
'alignment': 72, 'offset': 80, 'limit': 88, 'address': 96,
'ctagOffset': 104, 'hVASpace': 108, 'internalflags': 112,
'tag': 116, 'numaNode': 120}
def _detect_release(path: Path) -> str | None:
cur = path.resolve()
if cur.is_file():
cur = cur.parent
for _ in range(6):
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 / '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_sdk_include_dir() -> 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/common/sdk/nvidia/inc',
base / 'common/sdk/nvidia/inc',
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/common/sdk/nvidia/inc')
for candidate in candidates:
if (candidate / 'nvos.h').is_file() and _detect_release(candidate) == EXPECTED_RELEASE:
return candidate.resolve()
raise SystemExit(
f'Cannot find {EXPECTED_RELEASE} SDK header nvos.h (matching release metadata required). '
f'Set NV_RS_OPEN_GPU_SRC or OPEN_GPU_ROOT to an open-gpu-kernel-modules-{EXPECTED_RELEASE} '
'checkout, or NV_RS_ANALYSIS_ROOT to the analysis directory containing reference/.'
)
def main() -> None:
inc = find_sdk_include_dir()
source = '#include <stddef.h>\n#include "nvos.h"\n'
source += '_Static_assert(sizeof(NV_MEMORY_ALLOCATION_PARAMS)==128,"size");\n'
source += ''.join(
f'_Static_assert(offsetof(NV_MEMORY_ALLOCATION_PARAMS,{field})=={offset},"{field}");\n'
for field, offset in FIELDS.items()
)
results = []
with tempfile.TemporaryDirectory(prefix='nv-rs-sysmem-abi-') as directory:
cfile = Path(directory) / 'layout.c'
cfile.write_text(source, encoding='utf-8')
for abi, flag in [('x86_64', '-m64'), ('i386', '-m32')]:
command = [
'cc', flag, '-std=c11', '-Wall', '-Wextra', '-Werror',
'-I', str(inc), '-c', str(cfile), '-o', str(Path(directory) / (abi + '.o')),
]
subprocess.run(command, check=True, capture_output=True, text=True)
results.append({'abi': abi, 'status': 'passed'})
report = {'header': str(inc / 'nvos.h'), 'size': 128, 'offsets': FIELDS, 'compiler_checks': results}
out = ROOT / 'reports/system-memory-abi.json'
out.parent.mkdir(parents=True, exist_ok=True)
out.write_text(json.dumps(report, indent=2) + '\n', encoding='utf-8')
print('NV_MEMORY_ALLOCATION_PARAMS: native and i386 layouts passed')
if __name__ == '__main__':
main()