From 8212f13a19992a3e2ba4742b05f874e867c315f3 Mon Sep 17 00:00:00 2001 From: faumaray Date: Sat, 1 Aug 2026 18:07:06 +0300 Subject: [PATCH] Verify debug information in SDK release artifacts --- .../test_independent_component_release.py | 18 +++++ scripts/verify-relwithdebinfo.py | 70 +++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100755 scripts/verify-relwithdebinfo.py diff --git a/scripts/tests/test_independent_component_release.py b/scripts/tests/test_independent_component_release.py index f133c39..637c4ed 100755 --- a/scripts/tests/test_independent_component_release.py +++ b/scripts/tests/test_independent_component_release.py @@ -18,6 +18,7 @@ ROOT = Path(__file__).resolve().parents[2] PACKAGE = ROOT / "scripts" / "package-component.py" PUBLISH = ROOT / "scripts" / "publish-component-release.py" VERIFY = ROOT / "scripts" / "verify-locked-component-feeds.py" +VERIFY_RELWITHDEBINFO = ROOT / "scripts" / "verify-relwithdebinfo.py" class IndependentComponentReleaseTests(unittest.TestCase): @@ -111,12 +112,29 @@ class IndependentComponentReleaseTests(unittest.TestCase): ) with self.assertRaisesRegex(SystemExit, "has no debug information"): module.require_debug_information(package_root) + rejected = subprocess.run( + [str(VERIFY_RELWITHDEBINFO), "--root", str(package_root)], + check=False, + text=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + self.assertNotEqual(rejected.returncode, 0) + self.assertIn("has no debug information", rejected.stderr) subprocess.run( [compiler, "-O2", "-g", str(source), "-o", str(executable)], check=True, ) module.require_debug_information(package_root) + accepted = subprocess.run( + [str(VERIFY_RELWITHDEBINFO), "--root", str(package_root)], + check=False, + text=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + ) + self.assertEqual(accepted.returncode, 0, accepted.stderr) def package(self, component: str, package_id: str, version: str) -> tuple[Path, Path]: package_root = self.root / f"stage-{component}" diff --git a/scripts/verify-relwithdebinfo.py b/scripts/verify-relwithdebinfo.py new file mode 100755 index 0000000..77ad158 --- /dev/null +++ b/scripts/verify-relwithdebinfo.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 +"""Reject release trees containing ELF files without debug information.""" + +from __future__ import annotations + +import argparse +import os +from pathlib import Path +import re +import shutil +import subprocess + + +DEBUG_SECTION = re.compile(r"\]\s+\.(?:z)?debug_info\s") + + +def is_elf(path: Path) -> bool: + with path.open("rb") as stream: + return stream.read(4) == b"\x7fELF" + + +def verify(root: Path) -> int: + readelf = shutil.which("readelf") + if readelf is None: + raise ValueError("readelf is required") + verified = 0 + for path in sorted(root.rglob("*")): + if path.is_symlink() or not path.is_file() or not is_elf(path): + continue + completed = subprocess.run( + [readelf, "-SW", os.fspath(path)], + check=False, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + env={**os.environ, "LC_ALL": "C"}, + ) + if completed.returncode != 0: + message = completed.stderr.strip() or completed.stdout.strip() + raise ValueError( + f"readelf failed for {path.relative_to(root)}: {message}" + ) + if DEBUG_SECTION.search(completed.stdout) is None: + raise ValueError( + f"ELF {path.relative_to(root)} has no debug information; " + "expected a RelWithDebInfo artifact" + ) + verified += 1 + if verified == 0: + raise ValueError("release tree contains no ELF files to verify") + return verified + + +def main() -> int: + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--root", required=True, type=Path) + arguments = parser.parse_args() + try: + root = arguments.root.resolve(strict=True) + if root.is_symlink() or not root.is_dir(): + raise ValueError("--root must be a real directory") + verified = verify(root) + except (OSError, ValueError) as error: + parser.error(str(error)) + print(f"verified debug information in {verified} ELF files") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main())