#!/usr/bin/env python3 """Assemble each gate fixture and assert what the checker reports for that case alone.""" import json import os import shutil import tempfile import struct import subprocess import sys import unittest from pathlib import Path ROOT = Path(__file__).resolve().parents[2] FIXTURES = ROOT / "tests/kernel-module-gate" OUT = ROOT / "target/kernel-module-gate" CHECKER = ROOT / "tools/check_kernel_module.py" SHN_XINDEX = 0xffff SHN_ABS = 0xfff1 SHT_SYMTAB_SHNDX = 18 # spec Table 3.2 # Cases whose result does not depend on how a tool prints a name, because the checker takes the # target's identity from the ELF tables. Every case states the counts, so a fixture that produces no # relocation at all cannot pass vacuously. Every case states the export set too, so a case that exports # nothing asserts that it exports nothing instead of passing on whatever the checker happened to see. CASES = { "entry": dict(resolves=True, unresolved=0, ambiguous=0, recorded=1, not_entry_pad=0, endbr_rule=True, exports=False, duplicate=0), "long-name": dict(resolves=True, unresolved=0, ambiguous=0, recorded=1, not_entry_pad=0, endbr_rule=True, exports=False, duplicate=0), "mid-instruction": dict(resolves=False, unresolved=1, ambiguous=0, recorded=0, not_entry_pad=0, endbr_rule=True, exports=False, duplicate=0), # objdump shows one symbol per address, so the alias name is not in its output. Resolved through # the symbol table both targets are recorded: the alias at the entry is an entry pad, the alias # inside the instruction is recorded as a target that is not one. Resolved by name both were # dropped, so the ENDBR rule passed over an empty set. "alias": dict(resolves=True, unresolved=0, ambiguous=0, recorded=2, not_entry_pad=1, endbr_rule=False, exports=False, duplicate=0), # Two symbol names share their first 256 characters, one of them exactly 256 long. The relocation # names the longer one, whose entry is the endbr64, so not_entry_pad 0 means it resolved to the # entity the fixture names; the first symbol, a ret, is the wrong answer. "duplicate-symbol-prefix": dict(resolves=True, unresolved=0, ambiguous=0, recorded=1, not_entry_pad=0, endbr_rule=True, exports=False, duplicate=0), # The export set is built from the symbol table, so an entry for a name that is only referenced # (st_shndx == SHN_UNDEF) must not count as an export. This fixture references all nine protocol # names and defines none: before the fix the check passed on an object that exports nothing, which # is what the nm --defined-only check it replaced rejected. defined-exports.s is the control - the # same nine names, defined, still count. "undefined-exports": dict(resolves=True, unresolved=0, ambiguous=0, recorded=0, not_entry_pad=0, endbr_rule=True, exports=False, duplicate=0), "defined-exports": dict(resolves=True, unresolved=0, ambiguous=0, recorded=0, not_entry_pad=0, endbr_rule=True, exports=True, duplicate=0), # Two distinct sections share one name, which as cannot produce, hence the linker script beside the # fixture. Before the fix the single disassembly header for the shared name was attributed to the # last index, so the endbr64 recorded under the empty section made the .quad to it pass. Duplicate # names are not a truncation, so they are counted separately from ambiguous printed names. "duplicate-section-name": dict(resolves=False, unresolved=0, ambiguous=0, recorded=0, not_entry_pad=0, endbr_rule=True, exports=False, duplicate=1), # A code entry that is not an endbr64 pad, recorded and refused while IBT is in effect. Asserted from # the enforced side; the disabled side is the test below. "no-endbr-entry": dict(resolves=True, unresolved=0, ambiguous=0, recorded=1, not_entry_pad=1, endbr_rule=False, exports=False, duplicate=0), } # The section case is the one place a printed name still decides something: the target's identity is # known from the ELF section table, but objdump's disassembly has to be attributed to a section, and a # printed header that belongs to two sections cannot be attributed. The expectation is the rule, so it # allows either outcome - refused when objdump prints the shared form, otherwise recorded against the # entity the fixture names, whose entry is a ret - and records which printing it saw. Judging the # relocation against the other section would report its endbr64 entry instead, so not_entry_pad 0 fails # whichever branch applies. SHARED_PREFIX = { "duplicate-prefix": { "full": dict(resolves=True, unresolved=0, ambiguous=0, recorded=1, not_entry_pad=1, endbr_rule=False, exports=False, duplicate=0), "shared": dict(resolves=False, unresolved=0, ambiguous=1, recorded=0, not_entry_pad=0, endbr_rule=True, exports=False, duplicate=0), }, } PRINTING = {} CASES_RUN = [] def rule(report): return dict(resolves=report["checks"]["data_relocation_targets_resolve_to_instructions"], unresolved=report["unresolved_code_targets"], ambiguous=report["ambiguous_truncated_names"], recorded=report["data_relocations_to_code"], not_entry_pad=report["data_relocation_targets_without_endbr"], endbr_rule=report["checks"]["endbr64_at_data_relocation_targets"], exports=report["checks"]["protocol_exports_present"], duplicate=report["unattributable_duplicate_names"]) # The checker is not executable in the tree, so call it through the running interpreter. Object-only # mode keeps the committed report from being overwritten by fixture runs. IBT is set explicitly for every # case instead of being taken from the host's kernel config, so the expectations hold on a host without # CONFIG_X86_KERNEL_IBT=y; the disabled path is asserted separately below. def check(object_path, module_path=None, ibt="1"): args = [sys.executable, str(CHECKER), str(object_path)] args += [str(module_path)] if module_path else ["--object-only"] env = dict(os.environ, NV_RS_KERNEL_IBT=ibt) return json.loads(subprocess.run(args, capture_output=True, text=True, env=env).stdout) def case_object(name): OUT.mkdir(parents=True, exist_ok=True) assembled = OUT / f"{name}.input.o" subprocess.check_call(["as", str(FIXTURES / f"{name}.s"), "-o", str(assembled)]) script = FIXTURES / f"{name}.ld" if not script.exists(): return assembled # as merges sections that share a name, so the duplicate-name case only exists after the linker. object_path = OUT / f"{name}.o" subprocess.check_call(["ld", "-r", "-T", str(script), "-o", str(object_path), str(assembled)]) return object_path def craft_extended_object(with_shndx_table=True): # ELF64 relocatable object with extended section numbering, which as and ld cannot emit: e_shnum is 0 # with the real count in sh_size of header 0, and e_shstrndx is the escape value SHN_XINDEX with the # real string table index in sh_link of header 0 (spec Table 3.5). The .text entry is a ret, so the # two data relocations are code targets that are not entry pads. The second symbol's st_shndx is # SHN_XINDEX, so its section index exists only in the associated SHT_SYMTAB_SHNDX section. OUT.mkdir(parents=True, exist_ok=True) names = [".text", ".rodata", ".rela.rodata", ".symtab", ".strtab", ".shstrtab"] if with_shndx_table: names.append(".symtab_shndx") blob = b"\0" + b"".join(n.encode() + b"\0" for n in names) nameoff = {n: blob.find(n.encode()) for n in names} strtab = b"\0symEntry\0symXindex\0" symtab = [struct.pack("= SHN_LORESERVE, so the real count is in sh_size of header 0 section_names = {1: ".rodata", 2: ".rela.rodata", 3: ".symtab", 4: ".strtab", 5: ".shstrtab", 6: ".symtab_shndx", SHN_ABS: "code_at_65521"} order = sorted(section_names) blob = b"\0" + b"".join(section_names[i].encode() + b"\0" for i in order) nameoff = {i: blob.find(section_names[i].encode()) for i in order} exports = ["nv_rs_protocol_version", "nv_rs_gpu_capabilities", "nv_rs_event_selftest", "nv_rs_gpu_prepare_owned", "nv_rs_gpu_set_probe_identity", "nv_rs_gpu_registry_publish", "nv_rs_gpu_registry_retire", "nv_rs_sysmem_prepare", "nv_rs_sysmem_close"] strtab = b"\0" + b"".join(n.encode() + b"\0" for n in exports) + b"symXindex\0" symoff = {n: strtab.find(n.encode()) for n in exports + ["symXindex"]} symbols = [(0, 0, 0, 0, 0, 0)] for n in exports: symbols.append((symoff[n], 17, 0, SHN_ABS, 0, 1)) # raw st_shndx 65521, a special value symbols.append((symoff["symXindex"], 14, 0, SHN_XINDEX, 0, 1)) # the index exists only in the shndx table contents = {1: b"\0" * 16, 2: struct.pack("