"""Compare the JS geometry with two reviewed functions from pinned fio source. Run: python3 verify-fio-histogram.py Requires network access, Node.js and a C compiler. No device I/O is performed. Upstream source is fetched into a temporary directory and is not redistributed. """ import hashlib import json from pathlib import Path import subprocess import tempfile from urllib.request import urlopen COMMIT = "ed675d3477a70a42d2e757b713f6c7125a27cdca" HERE = Path(__file__).resolve().parent def function(source, signature): start = source.index(signature) brace = source.index("{", start) depth = 0 for i in range(brace, len(source)): if source[i] == "{": depth += 1 elif source[i] == "}": depth -= 1 if depth == 0: return source[start:i + 1] raise ValueError("Unclosed reviewed function") def main(): sources = {} for name in ("stat.h", "stat.c"): with urlopen(f"https://raw.githubusercontent.com/axboe/fio/{COMMIT}/{name}", timeout=30) as response: sources[name] = response.read() header = sources["stat.h"].decode() for constant, value in (("FIO_IO_U_PLAT_BITS", "6"), ("FIO_IO_U_PLAT_GROUP_NR", "29")): line = next(line for line in header.splitlines() if line.split()[:2] == ["#define", constant]) if line.split()[-1] != value: raise ValueError("Pinned geometry changed") source = sources["stat.c"].decode() code = """#include #include #define FIO_IO_U_PLAT_BITS 6 #define FIO_IO_U_PLAT_VAL 64 #define FIO_IO_U_PLAT_NR (29 * 64) """ code += function(source, "static unsigned int plat_val_to_idx(") + "\n" code += function(source, "static unsigned long long plat_idx_to_val(") + "\n" code += """int main(void) { unsigned long long value; while (scanf("%llu", &value) == 1) { printf("%llu\\n", plat_idx_to_val(plat_val_to_idx(value))); } return 0; } """ values = set(range(132)) for exponent in range(1, 28): width = 2 ** exponent for offset in range(64): lower = (64 + offset) * width values.update((lower - 1, lower, lower + width // 2, lower + width - 1, lower + width)) values.update((2 ** 34, 2 ** 40, 2 ** 53 - 1)) values = sorted(values) with tempfile.TemporaryDirectory(prefix="hesela-fio-geometry-") as tmp: root = Path(tmp) (root / "oracle.c").write_text(code) subprocess.run(["cc", "-std=c11", "-Wall", "-Wextra", "-Werror", "-fsanitize=undefined", str(root / "oracle.c"), "-o", str(root / "oracle")], check=True, capture_output=True) native = subprocess.run([str(root / "oracle")], input="".join(f"{v}\n" for v in values), text=True, capture_output=True, check=True, timeout=30) if native.stderr: raise RuntimeError(native.stderr) js = """import fs from 'node:fs'; import { binForLatency } from './fio-histogram.mjs'; console.log(JSON.stringify(JSON.parse(fs.readFileSync(0, 'utf8')).map(v => binForLatency(v).representativeNs))); """ actual = subprocess.run(["node", "--input-type=module", "-e", js], cwd=HERE, input=json.dumps(values), text=True, capture_output=True, check=True, timeout=30) expected = [int(v) for v in native.stdout.splitlines()] observed = json.loads(actual.stdout) if expected != observed: raise AssertionError("Native fio mapping and JavaScript mapping differ") receipt = { "evidenceType": "source-function-conformance-check", "fioCommit": COMMIT, "cases": len(values), "mismatches": 0, "limits": "Reviewed bin mapping functions only; no fio workload, device test or percentile confidence study.", "sourceSha256": {name: hashlib.sha256(raw).hexdigest() for name, raw in sources.items()}, "checkerSha256": hashlib.sha256(Path(__file__).read_bytes()).hexdigest(), "modelSha256": hashlib.sha256((HERE / "fio-histogram.mjs").read_bytes()).hexdigest(), "node": subprocess.check_output(["node", "--version"], text=True).strip(), "compiler": subprocess.check_output(["cc", "--version"], text=True).splitlines()[0], } print(json.dumps(receipt, indent=2)) if __name__ == "__main__": main()