From d3184ea2050f85e1ed664aaa5bae48de160745f7 Mon Sep 17 00:00:00 2001 From: salman Date: Sun, 25 Oct 2026 16:08:41 +0100 Subject: [PATCH] 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. --- CHANGELOG.md | 6 + packaging/runtime/README.md | 42 ++++ packaging/runtime/build-runtime-appimage.sh | 6 +- packaging/runtime/build-runtime-rpm.sh | 2 +- packaging/runtime/build-runtime.sh | 14 +- packaging/runtime/build.sh | 2 + packaging/runtime/runtime-manifest.py | 208 ++++++++++++++++++++ packaging/runtime/validate-runtime.sh | 16 +- packaging/wsl2/build-wsl2.sh | 3 +- tests/runtime-validation.test.js | 131 +++++++++++- 10 files changed, 418 insertions(+), 12 deletions(-) create mode 100644 packaging/runtime/runtime-manifest.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 7dce16c..a2a91a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -86,6 +86,12 @@ release path documented in [`TODO.md`](TODO.md). selection and actual RUN/ADDRUN entries in the MSR. ### Bundled ROOT + musrfit runtime +- Runtime artifacts now carry a versioned identity record with the actual + ROOT/musrfit versions, musrfit revision, native architecture, and tool/core + library SHA-256 fingerprints. Source builds also record their checkout and + CMake settings. Packaging rejects missing or mismatched records instead of + trusting the GUI release filename to identify the runtime. Existing + runtimes can be recorded explicitly without inventing historical build data. - Runtime production now fails if a required tool, ROOT configuration tool, library directory, or startup XML is missing, or if a bounded version check cannot start a tool. The runtime build, RPM/AppImage packagers, and WSL2 diff --git a/packaging/runtime/README.md b/packaging/runtime/README.md index 02c930d..84bcf88 100644 --- a/packaging/runtime/README.md +++ b/packaging/runtime/README.md @@ -19,6 +19,7 @@ runtime/ etc/ # ROOT/system config share/ musrfit_startup.xml # default startup file found via MUSRFITPATH + runtime-manifest.json # actual versions, native architecture, build provenance ``` ROOT is configured with `-Dgnuinstall=OFF` so its libraries land in `lib/` @@ -130,3 +131,44 @@ the seconds) and uses only the runtime's own ROOT/musrfit library path. This is a headless startup check, not a substitute for a Fit/Plot smoke test on a clean target system. It does not change how the application uses an existing local ROOT/musrfit installation. + +## Runtime identity + +Runtime artifacts now require `runtime-manifest.json`. The build records it only +after the required tools start successfully. Packaging checks the record but +never regenerates it automatically. Python 3 is needed on the build/packaging +machine (and in the WSL staging container), not by the installed application. + +The versioned JSON record contains: + +- A `runtimeId` independent of the GUI version, derived from the platform, + architecture, ROOT/musrfit versions, musrfit revision, and file fingerprints. +- The actual native architecture from ELF headers, rather than an artifact name. +- SHA-256 fingerprints of the six tools and the principal ROOT/musrfit libraries. +- For source builds, the source checkout's full revision and dirty status, the + container base-image reference, and relevant options from the CMake caches. +- A UTC recording date, distinct from any claimed historical build date. + +Artifact filenames still carry the GUI release label; `runtimeId` identifies +the underlying runtime independently, allowing its reuse across GUI releases. + +Every musrfit tool must report the same version/revision and the same ROOT +version as `root-config`. The source revision must also match the built tools +when a source checkout is supplied. RPM/AppImage packaging currently targets +x86_64; other architecture labels are rejected. The WSL payload retains its +Linux identity because its executables run inside Linux. + +For an existing runtime predating this metadata, explicitly record its current +contents once, then validate normally: + +```sh +bash packaging/runtime/validate-runtime.sh --record-manifest runtime +bash packaging/runtime/validate-runtime.sh runtime +``` + +This inspects the actual binaries without rebuilding or altering them. Unknown +historical build settings remain `null` with `build.origin` set to `existing`; +the current source checkout is not assumed to have produced those binaries. +Fingerprint checks cover the listed tools/libraries, not the entire runtime +tree; archive checksums and clean-system Fit/Plot tests remain necessary. +No manifest is required for a user's ordinary host-installed musrfit/ROOT. diff --git a/packaging/runtime/build-runtime-appimage.sh b/packaging/runtime/build-runtime-appimage.sh index e54b4a6..0c6634b 100755 --- a/packaging/runtime/build-runtime-appimage.sh +++ b/packaging/runtime/build-runtime-appimage.sh @@ -19,9 +19,13 @@ runtime_dir="${RUNTIME_DIR:-$root/runtime}" out_dir="${OUT_DIR:-$root/dist}" version="$(node -p "require('$root/package.json').version")" arch="${ARCH:-x86_64}" +case "$arch" in + x86_64|x64) arch=x86_64 ;; + *) echo "error: runtime AppImage launcher currently supports only x86_64, not $arch" >&2; exit 1 ;; +esac # Reject an incomplete or unloadable runtime before touching existing artifacts. -bash "$here/validate-runtime.sh" "$runtime_dir" +bash "$here/validate-runtime.sh" "$runtime_dir" --arch "$arch" cache="${ELECTRON_BUILDER_CACHE:-${XDG_CACHE_HOME:-$HOME/.cache}/electron-builder}" diff --git a/packaging/runtime/build-runtime-rpm.sh b/packaging/runtime/build-runtime-rpm.sh index ce89183..3b47b15 100755 --- a/packaging/runtime/build-runtime-rpm.sh +++ b/packaging/runtime/build-runtime-rpm.sh @@ -14,7 +14,7 @@ version="$(node -p "require('$root/package.json').version")" rpm_version="$(printf '%s' "$version" | tr '-' '~')" # Reject an incomplete or unloadable runtime before touching existing artifacts. -bash "$here/validate-runtime.sh" "$runtime_dir" +bash "$here/validate-runtime.sh" "$runtime_dir" --arch x86_64 cache="${ELECTRON_BUILDER_CACHE:-${XDG_CACHE_HOME:-$HOME/.cache}/electron-builder}" diff --git a/packaging/runtime/build-runtime.sh b/packaging/runtime/build-runtime.sh index 5d4534f..30f7a98 100755 --- a/packaging/runtime/build-runtime.sh +++ b/packaging/runtime/build-runtime.sh @@ -23,6 +23,10 @@ MUSRFIT_REPO="${MUSRFIT_REPO:-/src/musrfit}" PREFIX="${PREFIX:-/work/runtime}" JOBS="${JOBS:-$(nproc)}" ROOT_TARBALL_URL="https://root.cern/download/root_v${ROOT_VERSION}.source.tar.gz" +case "$ROOT_SOURCE" in + build|binary) ;; + *) echo "ERROR: ROOT_SOURCE must be build or binary" >&2; exit 1 ;; +esac # ROOT 6.38 predates CMake 4 and its CMakeLists uses a construct that CMake 4 # rejects ("Unknown arguments specified"). The manylinux base image ships a @@ -139,7 +143,15 @@ bundle_deps() { bundle_deps log "Verifying required tools" -bash "$(dirname "${BASH_SOURCE[0]}")/validate-runtime.sh" "$PREFIX" +build_info=(--source-repo "$MUSRFIT_REPO" --root-version "$ROOT_VERSION" + --root-source "$ROOT_SOURCE" --musrfit-cache /tmp/musrfit-build/CMakeCache.txt) +if [ "$ROOT_SOURCE" = "build" ]; then + build_info+=(--root-cache /tmp/root-build/CMakeCache.txt) +fi +if [ -n "${BASE_IMAGE:-}" ]; then + build_info+=(--base-image "$BASE_IMAGE") +fi +bash "$(dirname "${BASH_SOURCE[0]}")/validate-runtime.sh" --record-manifest "$PREFIX" "${build_info[@]}" log "Result" du -sh "$PREFIX" diff --git a/packaging/runtime/build.sh b/packaging/runtime/build.sh index cbb1bf8..79835dd 100755 --- a/packaging/runtime/build.sh +++ b/packaging/runtime/build.sh @@ -50,6 +50,8 @@ echo "Building runtime into $REPO/runtime" -v "$MUSRFIT_REPO:/src/musrfit:ro" \ -e "ROOT_VERSION=$ROOT_VERSION" \ -e "ROOT_SOURCE=$ROOT_SOURCE" \ + -e "ROOT_BINARY_URL=${ROOT_BINARY_URL:-}" \ + -e "BASE_IMAGE=$BASE_IMAGE" \ -e "MUSRFIT_REPO=/src/musrfit" \ -e "PREFIX=/work/runtime" \ -e "JOBS=$JOBS" \ diff --git a/packaging/runtime/runtime-manifest.py b/packaging/runtime/runtime-manifest.py new file mode 100644 index 0000000..9ce12e6 --- /dev/null +++ b/packaging/runtime/runtime-manifest.py @@ -0,0 +1,208 @@ +#!/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) diff --git a/packaging/runtime/validate-runtime.sh b/packaging/runtime/validate-runtime.sh index 88f80df..6410d16 100755 --- a/packaging/runtime/validate-runtime.sh +++ b/packaging/runtime/validate-runtime.sh @@ -6,8 +6,15 @@ # also runs inside the WSL staging container, after the tree has been relocated. set -euo pipefail -if [ "$#" -ne 1 ]; then - echo "Usage: bash validate-runtime.sh RUNTIME_DIRECTORY" >&2 +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +working_dir="$PWD" +mode=check +if [ "${1:-}" = "--record-manifest" ]; then + mode=record + shift +fi +if [ "$#" -lt 1 ]; then + echo "Usage: bash validate-runtime.sh [--record-manifest] RUNTIME_DIRECTORY [--arch ARCH]" >&2 exit 2 fi if [ ! -d "$1" ]; then @@ -15,6 +22,7 @@ if [ ! -d "$1" ]; then exit 1 fi runtime_dir="$(cd -- "$1" && pwd -P)" +shift check_seconds="${RUNTIME_CHECK_TIMEOUT:-15}" if [[ ! "$check_seconds" =~ ^[1-9][0-9]*$ ]]; then echo "ERROR: RUNTIME_CHECK_TIMEOUT must be a positive number of seconds" >&2 @@ -61,6 +69,7 @@ echo "Checking runtime: $runtime_dir" for tool in "${tools[@]}"; do if output="$("$timeout_bin" --kill-after=2s "${check_seconds}s" "$runtime_dir/bin/$tool" --version 2>&1)"; then printf ' ok %s\n%s\n' "$tool" "$output" + printf '%s\n' "$output" > "$check_dir/$tool.version" else status=$? printf 'ERROR: %s --version failed (exit %s)\n%s\n' "$tool" "$status" "$output" >&2 @@ -70,4 +79,7 @@ done if [ "$failed" -ne 0 ]; then exit 1 fi +# Parse JSON and bind its identity to the actual tools, not the GUI release name. +cd "$working_dir" +LD_LIBRARY_PATH= python3 "$here/runtime-manifest.py" "$mode" "$runtime_dir" "$check_dir" "$@" echo "Runtime validation passed." diff --git a/packaging/wsl2/build-wsl2.sh b/packaging/wsl2/build-wsl2.sh index 0349a37..87230dc 100755 --- a/packaging/wsl2/build-wsl2.sh +++ b/packaging/wsl2/build-wsl2.sh @@ -51,6 +51,7 @@ echo "Staging WSL2 rootfs in $BASE_IMAGE" "$ENGINE" run --name "$CONTAINER" \ -v "$RUNTIME_DIR:/tmp/runtime:ro" \ -v "$REPO/packaging/runtime/validate-runtime.sh:/tmp/validate-runtime.sh:ro" \ + -v "$REPO/packaging/runtime/runtime-manifest.py:/tmp/runtime-manifest.py:ro" \ -e "PREFIX=$PREFIX" \ -e "DISTRO_NAME=$DISTRO_NAME" \ "$BASE_IMAGE" \ @@ -65,7 +66,7 @@ echo "Staging WSL2 rootfs in $BASE_IMAGE" libX11 libXext libXft libXpm libXcursor libXrandr libXi \ freetype fontconfig libpng libjpeg-turbo libtiff \ zlib xz-libs pcre2 \ - bash file which \ + bash file which python3 \ >/dev/null dnf clean all >/dev/null diff --git a/tests/runtime-validation.test.js b/tests/runtime-validation.test.js index 4a23d48..7ee2ec0 100644 --- a/tests/runtime-validation.test.js +++ b/tests/runtime-validation.test.js @@ -23,6 +23,12 @@ function createRuntime(name) { const prefix = path.join(temporary, name); fs.mkdirSync(path.join(prefix, 'bin'), { recursive: true }); fs.mkdirSync(path.join(prefix, 'lib')); + const library = Buffer.alloc(64); + Buffer.from([127, 69, 76, 70, 2, 1, 1]).copy(library); + library.writeUInt16LE(3, 16); + library.writeUInt16LE(process.arch === 'arm64' ? 183 : 62, 18); + fs.writeFileSync(path.join(prefix, 'lib', 'libCore.so'), library); + fs.writeFileSync(path.join(prefix, 'lib', 'libPMusr.so'), library); fs.writeFileSync(path.join(prefix, 'musrfit_startup.xml'), '\n'); for (const tool of tools) { fs.writeFileSync(path.join(prefix, 'bin', tool), `#!/bin/sh @@ -44,9 +50,19 @@ if [ "$tool" = "$TEST_HANG_TOOL" ]; then exec /bin/sleep 30 fi printf '%s --version\\n' "$tool" >> "$TEST_RUNTIME_LOG" -echo "$tool test version" +if [ "$tool" = root-config ]; then + echo "$TEST_ROOT_VERSION" +else + version="$TEST_VERSION" + if [ "$tool" = "$TEST_STALE_TOOL" ]; then + version=1.11.1 + fi + echo "$tool version: $version, git-branch: root6, git-rev: test - $TEST_REVISION (Release), ROOT version: $TEST_ROOT_VERSION" +fi `, { mode: 0o755 }); } + const recorded = runScript(validator, ['--record-manifest', prefix]); + assert.strictEqual(recorded.status, 0, recorded.stderr); return prefix; } @@ -58,7 +74,8 @@ function runScript(script, args, overrides = {}) { LD_LIBRARY_PATH: '/unrelated/libraries', RUNTIME_CHECK_TIMEOUT: '2', TEST_RUNTIME_LOG: path.join(temporary, 'tools.log'), - TEST_FAIL_TOOL: '', TEST_HANG_TOOL: '' + TEST_FAIL_TOOL: '', TEST_HANG_TOOL: '', TEST_STALE_TOOL: '', + TEST_VERSION: '1.12.1', TEST_ROOT_VERSION: '6.38.00', TEST_REVISION: '708f777b' }, overrides), encoding: 'utf8', timeout: 10000 }); @@ -68,6 +85,7 @@ function runScript(script, args, overrides = {}) { try { const complete = createRuntime('complete runtime with spaces'); + fs.writeFileSync(path.join(temporary, 'tools.log'), ''); const result = runScript(validator, [complete]); assert.strictEqual(result.status, 0, result.stderr); assert(result.stdout.includes('Runtime validation passed.')); @@ -76,6 +94,100 @@ try { return tool + ' --version'; })); + const manifestPath = path.join(complete, 'runtime-manifest.json'); + const savedManifest = fs.readFileSync(manifestPath, 'utf8'); + const manifest = JSON.parse(savedManifest); + assert.strictEqual(manifest.schemaVersion, 1); + assert.strictEqual(manifest.platform, 'linux'); + assert.strictEqual(manifest.arch, process.arch); + assert.strictEqual(manifest.root.version, '6.38.00'); + assert.deepStrictEqual(manifest.musrfit, { version: '1.12.1', revision: '708f777b' }); + assert.strictEqual(manifest.build.origin, 'existing'); + assert.strictEqual(manifest.build.sourceRevision, null); + assert.strictEqual(manifest.build.options.root, null); + assert.strictEqual(manifest.build.options.musrfit, null); + assert(!manifest.runtimeId.includes(require('../package.json').version)); + assert.strictEqual(Object.keys(manifest.files).length, 8); + + // Ordinary packaging checks never silently create or repair metadata. + const unrecorded = createRuntime('unrecorded'); + fs.unlinkSync(path.join(unrecorded, 'runtime-manifest.json')); + const absentManifest = runScript(validator, [unrecorded]); + assert.strictEqual(absentManifest.status, 1); + assert(absentManifest.stderr.includes('runtime manifest is missing')); + assert(!fs.existsSync(path.join(unrecorded, 'runtime-manifest.json'))); + const adopted = runScript(validator, ['--record-manifest', unrecorded]); + assert.strictEqual(adopted.status, 0, adopted.stderr); + + for (const replacement of ['{', '[]', JSON.stringify({ ...manifest, schemaVersion: 99 }), + JSON.stringify({ ...manifest, platform: 'darwin' }), + JSON.stringify({ ...manifest, arch: process.arch === 'x64' ? 'arm64' : 'x64' })]) { + fs.writeFileSync(manifestPath, replacement); + const invalid = runScript(validator, [complete]); + assert.strictEqual(invalid.status, 1); + assert(!invalid.stderr.includes('Traceback')); + assert.strictEqual(fs.readFileSync(manifestPath, 'utf8'), replacement); + } + fs.writeFileSync(manifestPath, savedManifest); + const wrongTarget = runScript(validator, [complete, '--arch', process.arch === 'x64' ? 'arm64' : 'x64']); + assert.strictEqual(wrongTarget.status, 1); + assert(wrongTarget.stderr.includes('does not match target')); + const stale = runScript(validator, [complete], { TEST_STALE_TOOL: 'msr2data' }); + assert.strictEqual(stale.status, 1); + assert(stale.stderr.includes('different musrfit versions or revisions')); + const differentRoot = runScript(validator, [complete], { TEST_ROOT_VERSION: '6.36.00' }); + assert.strictEqual(differentRoot.status, 1); + assert(differentRoot.stderr.includes('runtime manifest')); + const changed = createRuntime('changed executable'); + fs.appendFileSync(path.join(changed, 'bin', 'musrview'), '# changed after recording\n'); + const changedFile = runScript(validator, [changed]); + assert.strictEqual(changedFile.status, 1); + assert(changedFile.stderr.includes('runtime file does not match manifest: bin/musrview')); + const mixed = createRuntime('mixed architectures'); + const foreignLibrary = fs.readFileSync(path.join(mixed, 'lib', 'libCore.so')); + foreignLibrary.writeUInt16LE(process.arch === 'arm64' ? 62 : 183, 18); + fs.writeFileSync(path.join(mixed, 'lib', 'libCore.so'), foreignLibrary); + assert(runScript(validator, [mixed]).stderr.includes('mixed native architectures')); + + // Source builds record the exact checkout and real CMake settings. Refuse + // to attribute an existing binary to a different checkout or ROOT release. + const source = path.join(temporary, 'source'); + const gitOptions = { encoding: 'utf8', env: Object.assign({}, process.env, { + GIT_CONFIG_GLOBAL: '/dev/null', GIT_CONFIG_NOSYSTEM: '1' + }) }; + childProcess.execFileSync('git', ['init', '--quiet', source], gitOptions); + childProcess.execFileSync('git', ['-C', source, '-c', 'user.name=Runtime test', + '-c', 'user.email=runtime-test@example.invalid', 'commit', '--quiet', '--no-gpg-sign', + '--allow-empty', '-m', 'Runtime fixture'], gitOptions); + const revision = childProcess.execFileSync('git', ['-C', source, 'rev-parse', 'HEAD'], gitOptions).trim(); + const rootCache = path.join(temporary, 'ROOT.CMakeCache.txt'); + const musrfitCache = path.join(temporary, 'musrfit.CMakeCache.txt'); + fs.writeFileSync(rootCache, 'gnuinstall:BOOL=OFF\nx11:BOOL=ON\nCMAKE_BUILD_TYPE:STRING=Release\n'); + fs.writeFileSync(musrfitCache, 'qt_based_tools:BOOL=OFF\ntry_OpenMP:BOOL=ON\n'); + const sourceArgs = ['--record-manifest', complete, '--source-repo', source, '--root-source', 'build', + '--root-version', '6.38.00', '--root-cache', rootCache, '--musrfit-cache', musrfitCache, + '--base-image', 'example/runtime-builder:test']; + const mismatchedSource = runScript(validator, sourceArgs); + assert.strictEqual(mismatchedSource.status, 1); + assert(mismatchedSource.stderr.includes('does not match the source checkout')); + assert.strictEqual(fs.readFileSync(manifestPath, 'utf8'), savedManifest); + const wrongRoot = runScript(validator, ['--record-manifest', complete, '--root-version', '9.99.00']); + assert.strictEqual(wrongRoot.status, 1); + assert.strictEqual(fs.readFileSync(manifestPath, 'utf8'), savedManifest); + const recordedSource = runScript(validator, sourceArgs, { TEST_REVISION: revision }); + assert.strictEqual(recordedSource.status, 0, recordedSource.stderr); + const sourceManifest = JSON.parse(fs.readFileSync(manifestPath, 'utf8')); + assert.strictEqual(sourceManifest.build.sourceRevision, revision); + assert.strictEqual(sourceManifest.build.sourceDirty, false); + assert.strictEqual(sourceManifest.build.options.root.x11, 'ON'); + assert.strictEqual(sourceManifest.build.options.musrfit.try_OpenMP, 'ON'); + assert.strictEqual(sourceManifest.build.baseImage, 'example/runtime-builder:test'); + assert.strictEqual(runScript(validator, [complete], { TEST_REVISION: revision }).status, 0); + fs.writeFileSync(path.join(source, 'local-change.cpp'), '// Local source change\n'); + assert.strictEqual(runScript(validator, sourceArgs, { TEST_REVISION: revision }).status, 0); + assert.strictEqual(JSON.parse(fs.readFileSync(manifestPath, 'utf8')).build.sourceDirty, true); + fs.writeFileSync(manifestPath, savedManifest); + const missing = createRuntime('missing tools'); fs.unlinkSync(path.join(missing, 'bin', 'dump_header')); fs.chmodSync(path.join(missing, 'bin', 'musrt0'), 0o644); @@ -93,7 +205,7 @@ try { assert.strictEqual(emptyStartup.status, 1); assert(emptyStartup.stderr.includes('startup XML')); const noLibraries = createRuntime('missing libraries'); - fs.rmdirSync(path.join(noLibraries, 'lib')); + fs.rmSync(path.join(noLibraries, 'lib'), { recursive: true }); assert.strictEqual(runScript(validator, [noLibraries]).status, 1); const broken = runScript(validator, [complete], { TEST_FAIL_TOOL: 'musrview' }); @@ -121,9 +233,10 @@ try { const artifact = path.join(output, 'musrflow-runtime-' + version + '-x86_64.' + (target === 'rpm' ? 'rpm' : 'AppImage')); fs.writeFileSync(artifact, 'previous artifact'); - for (const prefix of [missing, complete]) { + for (const prefix of [missing, complete, changed]) { const rejected = runScript(script, [], { - RUNTIME_DIR: prefix, OUT_DIR: output, TEST_FAIL_TOOL: 'musrview', + RUNTIME_DIR: prefix, OUT_DIR: output, ARCH: 'x86_64', + TEST_FAIL_TOOL: prefix === complete ? 'musrview' : '', FPM_BIN: packager, MKSQUASHFS_BIN: packager, APPIMAGE_RUNTIME_BIN: packager, TEST_PACKAGER_LOG: path.join(temporary, 'packager.log') }); @@ -134,11 +247,17 @@ try { } const build = fs.readFileSync(path.join(root, 'packaging/runtime/build-runtime.sh'), 'utf8'); - assert(build.includes('/validate-runtime.sh" "$PREFIX"')); + assert(build.includes('/validate-runtime.sh" --record-manifest "$PREFIX"')); const wsl = fs.readFileSync(path.join(root, 'packaging/wsl2/build-wsl2.sh'), 'utf8'); assert(wsl.includes('validate-runtime.sh:/tmp/validate-runtime.sh:ro')); + assert(wsl.includes('runtime-manifest.py:/tmp/runtime-manifest.py:ro')); assert(wsl.includes('bash /tmp/validate-runtime.sh "$PREFIX"')); assert(!wsl.includes('WARNING: runtime self-check failed')); + const unsupportedAppImage = runScript(path.join(root, 'packaging/runtime/build-runtime-appimage.sh'), [], { + RUNTIME_DIR: complete, ARCH: 'arm64' + }); + assert.strictEqual(unsupportedAppImage.status, 1); + assert(unsupportedAppImage.stderr.includes('launcher currently supports only x86_64')); console.log('Runtime validation tests passed'); } finally { fs.rmSync(temporary, { recursive: true, force: true });