"""Offline arithmetic audit, not execution of the device-mapper tutorial.""" import argparse import hashlib import json import subprocess from pathlib import Path EXTENT_SECTORS = 8192 SECTOR_BYTES = 512 CASES = [0, 1, 8191, 8192, 8193, 16383, 16384, 16385, 24576, 24577, 32767, 32768] SOURCE_URL = "https://raw.githubusercontent.com/torvalds/linux/v6.12/Documentation/admin-guide/device-mapper/linear.rst" SOURCE_SHA256 = "ec3ef874783f6a2201c15cd481c5e78b3abe356baf0db61652052ce2783cb66b" EXPRESSION = "int($dev_size / $extent_size) - (($dev_size % $extent_size) ? 1 : 0)" PERL = ( "use strict; use warnings; my $extent_size = 8192; " "for my $dev_size (@ARGV) { " 'my $extents = ' + EXPRESSION + '; ' 'print "$dev_size $extents\\n"; }' ) def parse_rows(stdout): rows = [] seen = [] for line in stdout.splitlines(): size, raw_count = map(int, line.split()) seen.append(size) full, remainder = divmod(size, EXTENT_SECTORS) generated = max(raw_count, 0) expected = full - bool(remainder) if raw_count != expected: raise ValueError("Perl and integer oracle disagree") rows.append({ "device_sectors": size, "complete_extents_available": full, "trailing_sectors": remainder, "perl_initial_extent_count": raw_count, "loop_iterations": generated, "mapped_sectors": generated * EXTENT_SECTORS, "omitted_sectors": size - generated * EXTENT_SECTORS, "extra_complete_extents_omitted": full - generated, }) if seen != CASES: raise ValueError("Incomplete or reordered observation set") return rows def observe(source): if hashlib.sha256(source).hexdigest() != SOURCE_SHA256: raise ValueError("Source bytes differ from the pinned v6.12 document") normalized = " ".join(source.decode("utf-8").split()) if EXPRESSION not in normalized: raise ValueError("Pinned example no longer contains the audited expression") run = subprocess.run(["perl", "-e", PERL, *map(str, CASES)], check=True, capture_output=True, text=True) if run.stderr: raise RuntimeError(run.stderr) rows = parse_rows(run.stdout) return { "method": "Native Perl expression evaluation with independent integer divmod oracle; tutorial shell commands and device access excluded.", "source_url": SOURCE_URL, "source_sha256": hashlib.sha256(source).hexdigest(), "perl_version": subprocess.check_output(["perl", "-e", 'print "$^V"'], text=True), "extent_sectors": EXTENT_SECTORS, "sector_bytes": SECTOR_BYTES, "rows": rows, } def main(): parser = argparse.ArgumentParser() parser.add_argument("source", type=Path) parser.add_argument("--check", type=Path) args = parser.parse_args() result = observe(args.source.read_bytes()) if args.check: if result != json.loads(args.check.read_text()): raise SystemExit("Observation replay differs") print("Exact native-expression replay passed") else: print(json.dumps(result, indent=2)) if __name__ == "__main__": main()