Files
kdm-re/cmake/check_pure_coverage.py
T

104 lines
3.4 KiB
Python

#!/usr/bin/env python3
"""Fail a PURE build unless every selected reachable RVA is verified."""
from __future__ import annotations
import argparse
import json
import sys
from pathlib import Path
from typing import Final, TypedDict
class FunctionRecord(TypedDict):
rva: str
name: str
status: str
callees: list[str]
SLICE_ROOTS: Final = ("0x0000f510", "0x000e4140", "0x001ab6d0")
SLICE_SUBSTITUTIONS: Final = {
"0x001ab5b0", # clamp helper reconstructed inline
"0x001ab640", # clamp helper reconstructed inline
"0x001db674", # MSVC security cookie supplied by the new toolchain
}
def fail(detail: str) -> None:
"""Report a closed coverage gate with a stable diagnostic."""
print(f"PURE gate: not yet at coverage: {detail}", file=sys.stderr)
raise SystemExit(1)
def main() -> None:
"""Parse the RVA graph and enforce the selected PURE-build policy."""
parser = argparse.ArgumentParser()
parser.add_argument("database", type=Path)
parser.add_argument("--mode", choices=("full", "slice"), required=True)
parser.add_argument("--slice-status", type=Path)
parser.add_argument("--require-rva", action="append", default=[])
args = parser.parse_args()
try:
raw = json.loads(args.database.read_text(encoding="utf-8"))
records: list[FunctionRecord] = raw["functions"]
except (OSError, json.JSONDecodeError, KeyError, TypeError) as error:
fail(f"cannot parse {args.database}: {error}")
by_rva: dict[str, FunctionRecord] = {}
for record in records:
rva = record["rva"].lower()
if rva in by_rva:
fail(f"duplicate function RVA {rva}")
by_rva[rva] = record
slice_verified: set[str] = set()
if args.mode == "slice":
if args.slice_status is None:
fail("slice verification status file was not supplied")
try:
claims = json.loads(
args.slice_status.read_text(encoding="utf-8")
)["functions"]
slice_verified = {
claim["rva"].lower()
for claim in claims
if claim["status"] == "verified"
}
except (OSError, json.JSONDecodeError, KeyError, TypeError) as error:
fail(f"cannot parse {args.slice_status}: {error}")
roots = list(by_rva) if args.mode == "full" else list(SLICE_ROOTS)
roots.extend(rva.lower() for rva in args.require_rva)
pending = roots
visited: set[str] = set()
while pending:
rva = pending.pop()
if rva in visited:
continue
visited.add(rva)
record = by_rva.get(rva)
if record is None:
fail(f"reachable RVA {rva} is missing from the database")
verified_here = record["status"] == "verified" or rva in slice_verified
if not verified_here:
fail(f"{rva} {record['name']} has status={record['status']}")
for callee in record["callees"]:
callee_rva = callee.lower()
if args.mode == "slice" and callee_rva in SLICE_SUBSTITUTIONS:
continue
pending.append(callee_rva)
verified = sum(record["status"] == "verified" for record in records)
if args.mode == "full" and verified != len(records):
fail(f"verified coverage is {verified}/{len(records)}, expected 100%")
print(
f"PURE gate: PASS mode={args.mode} "
f"verified={verified}/{len(records)} reachable={len(visited)}"
)
if __name__ == "__main__":
main()