mirror of
https://github.com/QuixiAI/nvdriver.git
synced 2026-12-06 14:38:47 +00:00
227 lines
10 KiB
Python
227 lines
10 KiB
Python
#!/usr/bin/env python3
|
|
"""Execute original ordinary thread registration and timeout initialization.
|
|
|
|
This is an original-behavior oracle, not proof of an implemented Rust registry.
|
|
OS identity, map/list, synchronization and diagnostics are intercepted.
|
|
"""
|
|
import argparse
|
|
import hashlib
|
|
import itertools
|
|
import json
|
|
import struct
|
|
import warnings
|
|
from pathlib import Path
|
|
|
|
warnings.filterwarnings("ignore", category=UserWarning, module="unicorn")
|
|
from elftools.elf.elffile import ELFFile
|
|
from unicorn import Uc, UC_ARCH_X86, UC_MODE_64, UC_HOOK_CODE
|
|
from unicorn.x86_const import *
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--check", action="store_true")
|
|
options = parser.parse_args()
|
|
ARTIFACT = Path("/home/geodd/nvidia-driver/analysis-580.178.04/extracted/kernel/nvidia/nv-kernel.o_binary")
|
|
SHA = "5269a23ffba19a17fe16f076463e7ad4151254659441d1711bee6c6221ff6f4b"
|
|
assert hashlib.sha256(ARTIFACT.read_bytes()).hexdigest() == SHA
|
|
TEXT, BSS, RODATA, DATA, STACK, STOP = (0x10000000, 0x20000000, 0x30000000,
|
|
0x40000000, 0x50000000, 0x60000000)
|
|
NAMES = ["_nv016372rm", "_nv016370rm", "_nv059466rm"]
|
|
with ARTIFACT.open("rb") as stream:
|
|
elf = ELFFile(stream)
|
|
symbols = list(elf.get_section_by_name(".symtab").iter_symbols())
|
|
syms = {s.name: s for s in symbols}
|
|
text = bytearray(elf.get_section_by_name(".text").data())
|
|
section_bases = {elf.get_section_index(n): base for n, base in
|
|
[(".text", TEXT), (".bss", BSS), (".rodata", RODATA), (".data", DATA)]}
|
|
spans = [(syms[n]["st_value"], syms[n]["st_size"]) for n in NAMES]
|
|
for rel in elf.get_section_by_name(".rela.text").iter_relocations():
|
|
off = rel["r_offset"]
|
|
if not any(start <= off < start + size for start, size in spans):
|
|
continue
|
|
symbol = symbols[rel["r_info_sym"]]
|
|
assert symbol["st_shndx"] in section_bases, symbol.name
|
|
value = section_bases[symbol["st_shndx"]] + symbol["st_value"] + rel["r_addend"]
|
|
if rel["r_info_type"] in (2, 4):
|
|
value -= TEXT + off
|
|
else:
|
|
assert rel["r_info_type"] == 11
|
|
struct.pack_into("<i", text, off, value)
|
|
|
|
uc = Uc(UC_ARCH_X86, UC_MODE_64)
|
|
for base, size in [(TEXT, (len(text) + 4095) & ~4095), (BSS, 4096),
|
|
(STACK, 0x10000), (STOP, 4096), (DATA, 4096)]:
|
|
uc.mem_map(base, size)
|
|
uc.mem_write(TEXT, bytes(text))
|
|
NODE = DATA + 0x100
|
|
DB = BSS + syms["_nv059471rm"]["st_value"]
|
|
|
|
|
|
def write32(address, value):
|
|
uc.mem_write(address, struct.pack("<I", value))
|
|
|
|
|
|
def write64(address, value):
|
|
uc.mem_write(address, struct.pack("<Q", value))
|
|
|
|
|
|
def read32(address):
|
|
return struct.unpack("<I", uc.mem_read(address, 4))[0]
|
|
|
|
|
|
def read64(address):
|
|
return struct.unpack("<Q", uc.mem_read(address, 8))[0]
|
|
|
|
|
|
def return_from_hook(value):
|
|
sp = uc.reg_read(UC_X86_REG_RSP)
|
|
uc.reg_write(UC_X86_REG_RAX, value)
|
|
uc.reg_write(UC_X86_REG_RIP, read64(sp))
|
|
uc.reg_write(UC_X86_REG_RSP, sp + 8)
|
|
|
|
|
|
hook_names = ["_nv052705rm", "_nv049647rm", "_nv052691rm", "_nv042695rm",
|
|
"_nv049665rm", "_nv049648rm", "_nv052743rm", "_nv052747rm",
|
|
"_nv043258rm", "_nv016373rm", "_nv059466rm", "_nv047869rm", "_nv046745rm"]
|
|
hooks = {TEXT + syms[n]["st_value"]: n for n in hook_names}
|
|
events = []
|
|
locked = False
|
|
clock_calls = 0
|
|
scenario = None
|
|
|
|
|
|
def on_code(emu, pc, size, user):
|
|
global locked, clock_calls
|
|
if pc == STOP:
|
|
emu.emu_stop()
|
|
return
|
|
name = hooks.get(pc)
|
|
if name is None:
|
|
return
|
|
a, b, c = [emu.reg_read(reg) for reg in (UC_X86_REG_RDI, UC_X86_REG_RSI, UC_X86_REG_RDX)]
|
|
events.append(name)
|
|
if name == "_nv059466rm":
|
|
# Observe entry without replacing execution: this exact release's
|
|
# selected priority-allocation body is ENDBR64; RET.
|
|
assert bytes(emu.mem_read(pc, 5)) == b"\xf3\x0f\x1e\xfa\xc3"
|
|
return
|
|
if name == "_nv052705rm":
|
|
assert (a, b, c) == (NODE, 0, 0xa0)
|
|
emu.mem_write(a, bytes(c))
|
|
value = a
|
|
elif name == "_nv049647rm":
|
|
value = 4 if scenario["failure"] == "cpu" else 2
|
|
elif name == "_nv052691rm":
|
|
value = 0x1234
|
|
elif name == "_nv042695rm":
|
|
assert (a, b, c) == (NODE + 0x78, 0x1234, 16)
|
|
value = 0 # The empty-list owner is intercepted, not reconstructed here.
|
|
elif name == "_nv049665rm":
|
|
value = 100 + clock_calls
|
|
clock_calls += 1
|
|
elif name == "_nv049648rm":
|
|
assert a == NODE
|
|
write64(a, 0x9876)
|
|
value = 0x56 if scenario["failure"] == "identity" else 0
|
|
elif name == "_nv052743rm":
|
|
assert not locked and a == 0x2345
|
|
locked = True
|
|
value = 0
|
|
elif name == "_nv052747rm":
|
|
assert locked and a == 0x2345
|
|
locked = False
|
|
value = 0
|
|
elif name == "_nv043258rm":
|
|
assert locked and (a, b, c) == (DB + 0x18, 0x9876, NODE)
|
|
value = int(scenario["failure"] != "duplicate")
|
|
else:
|
|
assert name in ("_nv016373rm", "_nv047869rm", "_nv046745rm")
|
|
assert not locked
|
|
value = 0
|
|
return_from_hook(value)
|
|
|
|
|
|
uc.hook_add(UC_HOOK_CODE, on_code)
|
|
rows = []
|
|
for flags, mode, failure, sequence, heap, trace in itertools.product(
|
|
(0, 0x80, 0x400), (8, 16, 0), ("none", "identity", "cpu", "duplicate"),
|
|
(0, 0xffffffff), (0, 1), (0, 7)):
|
|
scenario = dict(flags=flags, mode=mode, failure=failure, sequence=sequence, heap=heap, trace=trace)
|
|
uc.mem_write(BSS, bytes(4096))
|
|
uc.mem_write(DATA, bytes([0xcc]) * 4096)
|
|
write32(DB + 4, 4)
|
|
write32(DB + 8, sequence)
|
|
write64(DB + 0x10, 0x2345)
|
|
write64(DB + 0x38, 13)
|
|
write64(DB + 0x40, 17)
|
|
write32(DB + 0x4c, mode)
|
|
write32(DB + 0x50, trace)
|
|
events, locked, clock_calls = [], False, 0
|
|
uc.reg_write(UC_X86_REG_RDI, NODE)
|
|
uc.reg_write(UC_X86_REG_RSI, flags)
|
|
uc.reg_write(UC_X86_REG_RDX, heap)
|
|
uc.reg_write(UC_X86_REG_RSP, STACK + 0xfff8)
|
|
write64(STACK + 0xfff8, STOP)
|
|
uc.emu_start(TEXT + syms["_nv016372rm"]["st_value"], STOP, count=2000)
|
|
assert uc.reg_read(UC_X86_REG_RIP) == STOP and not locked and clock_calls == 2
|
|
status = uc.reg_read(UC_X86_REG_RAX) & 0xffffffff
|
|
expected = {"none": 0, "identity": 0x56, "cpu": 0x40, "duplicate": 0xffff}[failure]
|
|
assert status == expected, (scenario, status)
|
|
assert read32(DB + 8) == read32(NODE + 8) == (sequence + 1) & 0xffffffff
|
|
assert bytes(uc.mem_read(NODE + 0xc, 2)) == bytes([int(failure == "none"), heap])
|
|
assert read64(NODE) == (0 if failure == "duplicate" else 0x9876)
|
|
expected_flags = flags | 0x100 | (0x20 if mode else 0)
|
|
assert read32(NODE + 0x48) == expected_flags
|
|
assert read64(NODE + 0x10) == 100 and read64(NODE + 0x28) == 2_000_000_101
|
|
interval = 60000 if flags & 0x80 else 13
|
|
assert read64(NODE + 0x18) == (100 + interval * 1000000 if mode == 8 else interval * 1000 if mode == 16 else 0)
|
|
compute = 60000 if flags & 0x80 else 17
|
|
assert read64(NODE + 0x20) == (100 + compute * 1000000 if mode == 8 else compute * 1000 if mode == 16 else 0)
|
|
assert read32(NODE + 0x40) == 0 and read64(NODE + 0x30) == 0
|
|
assert read32(NODE + 0x44) == (4 if failure == "cpu" else 2)
|
|
assert read32(DB + 0x50) == ((trace + 1) & 7 if failure == "none" else trace)
|
|
if failure == "none":
|
|
assert read64(DB + 0x58 + trace * 16) == STOP
|
|
assert read32(DB + 0x60 + trace * 16) == expected_flags
|
|
assert events[-3:] == ["_nv052747rm", "_nv016373rm", "_nv059466rm"]
|
|
else:
|
|
assert "_nv059466rm" not in events
|
|
rows.append({**scenario, "status": status, "node_flags": expected_flags,
|
|
"timeout": [read64(NODE + offset) for offset in (0x10, 0x18, 0x20, 0x28, 0x30)],
|
|
"events": events})
|
|
|
|
report = {
|
|
"passed": True, "artifact_sha256": SHA, "cases": len(rows), "functions": NAMES,
|
|
"observations": [
|
|
"Timeout initialization failure does not prevent ordinary registration; TIMEOUT_INITED remains clear.",
|
|
"Sequence increment wraps at u32 and occurs before identity/map errors.",
|
|
"Duplicate map insertion zeroes new node thread ID and leaves valid false; it does not push a nested current node.",
|
|
"Only successful insertion publishes valid, advances the eight-entry trace, and executes the selected priority-allocation callback.",
|
|
"The selected priority-allocation body is exactly ENDBR64;RET and runs without interception; it does not allocate state in this artifact.",
|
|
"Identity errors and CPU range failure happen after callback-list and timeout initialization."
|
|
],
|
|
"limits": [
|
|
"Original .text/.bss instructions in Unicorn; no native target or hardware execution.",
|
|
"Actual thread identity, callback-list allocation, spinlock, map insertion and diagnostics are intercepted.",
|
|
"No Rust registration/registry implementation is validated by this oracle; construction, ownership and teardown remain required."
|
|
], "rows": rows,
|
|
}
|
|
(ROOT / "reports/thread-registration-original.json").write_text(json.dumps(report, indent=2) + "\n")
|
|
vectors = []
|
|
for row in rows:
|
|
# Owned kernel entries use heap storage; Linux identity acquisition itself
|
|
# cannot return the artificial injected NV0x56 helper error.
|
|
if row["heap"] != 1 or row["failure"] == "identity":
|
|
continue
|
|
failure = {"none": 0, "cpu": 1, "duplicate": 2}[row["failure"]]
|
|
vectors.append("Case { flags: %d, mode: %d, failure: %d, sequence: %d, trace: %d, status: %d, node_flags: %d, timeout: %s }," % (
|
|
row["flags"], row["mode"], failure, row["sequence"], row["trace"], row["status"], row["node_flags"], row["timeout"]))
|
|
directory = ROOT / "tests/thread-state"
|
|
directory.mkdir(exist_ok=True)
|
|
generated = "// Generated from original ELF execution, heap-owned ordinary entries.\n#[rustfmt::skip]\nconst ORIGINAL: &[Case] = &[\n" + "\n".join(vectors) + "\n];\n"
|
|
if options.check:
|
|
assert (directory / "original_vectors.rs").read_text() == generated, "Original registration vectors changed"
|
|
else:
|
|
(directory / "original_vectors.rs").write_text(generated)
|
|
print(len(rows), "original ordinary-thread registration scenarios")
|