Files
muSRFlow/packaging/runtime/runtime-manifest.py
T
salman d3184ea205 Record and verify runtime build identity
Add a versioned manifest that records actual ROOT/musrfit versions, native ELF architecture, musrfit revision, and SHA-256 fingerprints independently of the GUI version. Source builds also record checkout provenance and CMake options.

Require matching metadata during Linux packaging and WSL staging, reject mixed or incorrectly labelled runtimes, and support explicit recording of existing trees without inventing historical build settings. Add regression coverage and document the build-only metadata workflow.
2026-10-25 16:08:41 +01:00

209 lines
10 KiB
Python

#!/usr/bin/env python3
# Copyright (C) 2026 Zaher Salman
# SPDX-License-Identifier: GPL-3.0-or-later
"""Record/check build-time runtime identity without depending on the GUI version."""
import argparse
import datetime
import hashlib
import json
import os
from pathlib import Path
import platform
import re
import subprocess
import sys
import tempfile
TOOLS = ("musrfit", "musrt0", "musrview", "msr2data", "dump_header")
MANIFEST = "runtime-manifest.json"
def inspect_runtime(prefix, versions):
# All output was collected by the bounded checks in validate-runtime.sh.
# Compare every tool so an old executable left in the prefix is not relabelled.
root_versions = re.findall(r"(?m)^(\d+\.\d+[./]\d+)\s*$", (versions / "root-config.version").read_text())
if len(root_versions) != 1:
raise ValueError("cannot identify the runtime ROOT version")
root_version = root_versions[0].replace("/", ".")
identities = []
for tool in TOOLS:
output = (versions / (tool + ".version")).read_text()
version = re.search(re.escape(tool) + r" version:\s*([^,\s]+)", output)
root_used = re.search(r"ROOT version:\s*([^,\s]+)", output)
if not version or not root_used or version.group(1) == "unknown":
raise ValueError("cannot identify the version of " + tool)
if root_used.group(1).replace("/", ".") != root_version:
raise ValueError(tool + " was built against a different ROOT version")
git = re.search(r"git-rev:\s*(.*?)\s*\(", output)
revision = None
if git:
match = re.search(r"(?:^|[\s-])([0-9a-f]{7,40})(?:-dirty)?$", git.group(1))
if not match:
raise ValueError("cannot identify the git revision of " + tool)
revision = match.group(1)
identities.append({"version": version.group(1), "revision": revision})
if any(identity != identities[0] for identity in identities[1:]):
raise ValueError("runtime tools have different musrfit versions or revisions")
files = {}
architectures = set()
paths = ["bin/" + tool for tool in TOOLS + ("root-config",)]
paths.extend(("lib/libCore.so", "lib/libPMusr.so"))
for relative in paths:
digest = hashlib.sha256()
with (prefix / relative).open("rb") as stream:
header = stream.read(20)
# Read ELF identity rather than trusting the build host or filename.
# Shell wrappers are architecture-independent; native objects are not.
if header.startswith(b"\x7fELF") and len(header) == 20 and header[4] == 2 and header[5] in (1, 2):
machine = int.from_bytes(header[18:20], "little" if header[5] == 1 else "big")
if machine not in (62, 183):
raise ValueError("unsupported runtime architecture in " + relative)
architectures.add("x64" if machine == 62 else "arm64")
elif relative.startswith("lib/") or not header.startswith(b"#!"):
raise ValueError("expected a Linux ELF binary or shell wrapper: " + relative)
digest.update(header)
while True:
block = stream.read(1024 * 1024)
if not block:
break
digest.update(block)
files[relative] = digest.hexdigest()
if len(architectures) != 1:
raise ValueError("runtime contains mixed native architectures")
arch = architectures.pop()
content = hashlib.sha256(json.dumps(files, sort_keys=True).encode("utf-8")).hexdigest()[:12]
identity = identities[0]
return {
"schemaVersion": 1,
"runtimeId": "linux-{}-root-{}-musrfit-{}-{}-{}".format(
arch, root_version, identity["version"], identity["revision"] or "unknown", content),
"platform": "linux",
"arch": arch,
"root": {"version": root_version},
"musrfit": identity,
"files": files,
}
def read_build_options(filename):
if filename is None:
return None
names = {
"CMAKE_BUILD_TYPE", "gnuinstall", "roofit", "tmva", "http", "sqlite",
"builtin_xrootd", "mathmore", "x11", "opengl", "python", "pythia8",
"qt_based_tools", "try_OpenMP",
}
options = {}
for line in filename.read_text().splitlines():
match = re.match(r"([^:#=]+):(?:BOOL|STRING)=(.*)$", line)
if match and match.group(1) in names:
options[match.group(1)] = match.group(2)
return options
def record_build(args, identity):
build = {
"origin": "existing", "sourceRevision": None, "sourceDirty": None,
"baseImage": None, "rootSource": None,
"options": {"root": None, "musrfit": None},
}
if args.source_repo is not None:
repo = str(args.source_repo.resolve())
git = ["git", "-c", "safe.directory=" + repo, "-C", repo]
revision = subprocess.check_output(git + ["rev-parse", "HEAD"],
universal_newlines=True, timeout=15).strip()
reported = identity["musrfit"]["revision"]
if not re.fullmatch(r"[0-9a-f]{40}", revision) or not reported or not revision.startswith(reported):
raise ValueError("built musrfit revision does not match the source checkout")
status = subprocess.check_output(git + ["status", "--porcelain"],
universal_newlines=True, timeout=15)
build.update(origin="source-build", sourceRevision=revision, sourceDirty=bool(status.strip()),
baseImage=args.base_image, rootSource=args.root_source,
options={"root": read_build_options(args.root_cache),
"musrfit": read_build_options(args.musrfit_cache)})
if args.root_version and args.root_version.replace("/", ".") != identity["root"]["version"]:
raise ValueError("built ROOT version does not match the requested ROOT version")
return build
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("mode", choices=("record", "check"))
parser.add_argument("prefix", type=Path)
parser.add_argument("versions", type=Path)
parser.add_argument("--arch", default=platform.machine())
parser.add_argument("--source-repo", type=Path)
parser.add_argument("--root-version")
parser.add_argument("--root-source", choices=("build", "binary"))
parser.add_argument("--base-image")
parser.add_argument("--root-cache", type=Path)
parser.add_argument("--musrfit-cache", type=Path)
args = parser.parse_args()
prefix = args.prefix.resolve()
target = prefix / MANIFEST
if args.mode == "check" and not target.is_file():
raise ValueError("runtime manifest is missing; rebuild the runtime or explicitly record the existing tree "
"with validate-runtime.sh --record-manifest RUNTIME_DIRECTORY")
identity = inspect_runtime(prefix, args.versions)
aliases = {"x86_64": "x64", "amd64": "x64", "x64": "x64", "aarch64": "arm64", "arm64": "arm64"}
expected_arch = aliases.get(args.arch.lower())
if expected_arch is None or identity["arch"] != expected_arch:
raise ValueError("runtime architecture {} does not match target {}".format(identity["arch"], args.arch))
if args.mode == "record":
identity["recordedAt"] = datetime.datetime.now(datetime.timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
identity["build"] = record_build(args, identity)
# A failed probe or interrupted write must leave the previous record intact.
temporary = None
try:
with tempfile.NamedTemporaryFile(mode="w", encoding="utf-8", dir=str(prefix),
prefix=".runtime-manifest-", delete=False) as stream:
temporary = stream.name
json.dump(identity, stream, indent=2, sort_keys=True)
stream.write("\n")
os.chmod(temporary, 0o644)
os.replace(temporary, str(target))
finally:
if temporary is not None and os.path.exists(temporary):
os.unlink(temporary)
print("Recorded runtime manifest: " + identity["runtimeId"])
else:
manifest = json.loads(target.read_text())
if not isinstance(manifest, dict) or type(manifest.get("schemaVersion")) is not int:
raise ValueError("invalid runtime manifest structure")
recorded_files = manifest.get("files")
if not isinstance(recorded_files, dict):
raise ValueError("runtime manifest is missing file checksums")
for relative, checksum in identity["files"].items():
if recorded_files.get(relative) != checksum:
raise ValueError("runtime file does not match manifest: " + relative)
for field, value in identity.items():
if manifest.get(field) != value:
raise ValueError("runtime manifest {} does not match the installed files and tool versions".format(field))
build = manifest.get("build")
if not isinstance(build, dict) or build.get("origin") not in ("existing", "source-build"):
raise ValueError("invalid runtime build provenance")
if build["origin"] == "source-build":
revision = build.get("sourceRevision")
reported = identity["musrfit"]["revision"]
if not isinstance(revision, str) or not re.fullmatch(r"[0-9a-f]{40}", revision) \
or not reported or not revision.startswith(reported) or type(build.get("sourceDirty")) is not bool:
raise ValueError("runtime source provenance does not match the musrfit binary")
recorded_at = manifest.get("recordedAt")
if not isinstance(recorded_at, str):
raise ValueError("runtime manifest is missing its recording date")
datetime.datetime.strptime(recorded_at, "%Y-%m-%dT%H:%M:%SZ")
print("Verified runtime manifest: " + identity["runtimeId"])
if __name__ == "__main__":
try:
main()
except (OSError, ValueError, subprocess.SubprocessError) as error:
print("ERROR: " + str(error), file=sys.stderr)
sys.exit(1)