diff --git a/test/constant_time/dudect_judge.py b/test/constant_time/dudect_judge.py index 31b8245..f5ede27 100755 --- a/test/constant_time/dudect_judge.py +++ b/test/constant_time/dudect_judge.py @@ -14,7 +14,10 @@ Finds every DIR/**/dudect_*.output, which b2 wrote for a dudect program of the constant-time lane, prints each in a GitHub log group, and emits a warning annotation for every case whose |t|, -as printed, is 10 or more, with the case's evidence line. dudect_planted, whose high value is its +as printed, is 10 or more, with the case's evidence line. It first prints the CPU it runs on, the +host's, as `dudect host cpu="" microcode=` (read as dudect.hpp's `parse_cpuinfo` +reads it), and each warning repeats that line: a program under wasmtime cannot read the CPU, and +its own machine line says `unavailable`. dudect_planted, whose high value is its pass, and dudect_harness, a test of the harness with made-up values, are printed and never warned about. With --rerun, each judged case whose |t| is 10 or more is measured again in two fresh processes (the program's `--case `, which on wasip2 runs as `COMMAND .wasm @@ -27,6 +30,7 @@ from __future__ import annotations import argparse +import platform import re import shlex import subprocess @@ -42,6 +46,54 @@ NOT_MEASURING = ('dudect_planted', 'dudect_harness') +def parse_cpuinfo(text: str) -> tuple[str, str]: + """The CPU's model and microcode from the text of Linux's /proc/cpuinfo, as dudect.hpp's + `parse_cpuinfo` reads them: the first `model name` and `microcode` (x86); where there is no + `model name` (arm64), `arm64 implementer= part= variant= revision=` with the + fields found; `unavailable` for what the text does not hold. A double quote in the model + becomes a single one, a space or double quote in the microcode an underscore.""" + wanted = ('model name', 'microcode', 'CPU implementer', 'CPU part', 'CPU variant', + 'CPU revision') + found: dict[str, str] = {} + for line in text.splitlines(): + key, colon, value = line.partition(':') + key = key.rstrip(' \t\r') + if colon and key in wanted and key not in found: + value = value.strip(' \t\r') + if value: + found[key] = value + if 'model name' in found: + cpu = found['model name'] + elif 'CPU implementer' in found: + labels = ('implementer', 'part', 'variant', 'revision') + fields = [f'{label}={found[key]}' for label, key in zip(labels, wanted[2:]) + if key in found] + cpu = ' '.join(['arm64'] + fields) + else: + cpu = 'unavailable' + microcode = found.get('microcode', 'unavailable') + return cpu.replace('"', "'"), microcode.replace(' ', '_').replace('"', '_') + + +def host_line() -> str: + """The line naming the CPU this judge runs on: /proc/cpuinfo's on Linux, + `machdep.cpu.brand_string` on macOS with the microcode `unavailable`, `unavailable` + elsewhere.""" + cpu, microcode = 'unavailable', 'unavailable' + system = platform.system() + if system == 'Linux': + try: + cpu, microcode = parse_cpuinfo(Path('/proc/cpuinfo').read_text(errors='replace')) + except OSError: + pass + elif system == 'Darwin': + done = subprocess.run(['sysctl', '-n', 'machdep.cpu.brand_string'], capture_output=True, + text=True, check=False) + if done.returncode == 0 and done.stdout.strip(): + cpu = done.stdout.strip().replace('"', "'") + return f'dudect host cpu="{cpu}" microcode={microcode}' + + def annotation(text: str) -> str: """text as a workflow command's message: the characters it reserves escaped.""" return text.replace('%', '%25').replace('\r', '%0D').replace('\n', '%0A') @@ -83,8 +135,8 @@ def program_command(output: Path, target: str, launcher: str) -> list[str]: return [str(program)] -def judge(output: Path, target: str, rerun: bool, launcher: str) -> bool: - """Prints and judges one output; whether it passes.""" +def judge(output: Path, target: str, rerun: bool, launcher: str, host: str) -> bool: + """Prints and judges one output, host the line naming the judge's CPU; whether it passes.""" program = output.stem text = output.read_text(errors='replace') print(f'::group::{program} ({target})') @@ -101,7 +153,7 @@ def judge(output: Path, target: str, rerun: bool, launcher: str) -> bool: if not value >= THRESHOLD: continue title = f'dudect {program} {name} ({target})'.replace(':', '%3A').replace(',', '%2C') - print(f'::warning title={title}::{annotation(chr(10).join(shown))}') + print(f'::warning title={title}::{annotation(chr(10).join([*shown, host]))}') if not (rerun and judged): continue command = program_command(output, target, launcher) @@ -137,9 +189,12 @@ def main(argv: list[str]) -> int: if not outputs: print(f'dudect: no dudect_*.output under {arguments.bin}', file=sys.stderr) return 1 + host = host_line() + print(host) passed = True for output in outputs: - passed = judge(output, arguments.target, arguments.rerun, arguments.launcher) and passed + passed = judge(output, arguments.target, arguments.rerun, arguments.launcher, + host) and passed return 0 if passed else 1