vinix-ports / tests / library-upgrades.py
88 lines · 82 sloc · 4.52 KB · c23f1a759c45eb3adceceabd3ab7a9b9e581c5c8
Raw
1"""Compare the release's shared-library interface to its package-owned replacement."""
2import argparse
3import csv
4import hashlib
5from pathlib import Path
6import shutil
7import subprocess
8import tarfile
9import tempfile
10
11
12def interface(path, arch):
13 report = subprocess.check_output(["readelf", "-W", "-h", "-d", "--dyn-syms", path], text=True)
14 machine = "AArch64" if arch == "aarch64" else "Advanced Micro Devices X86-64"
15 fields = [line.strip() for line in report.splitlines()]
16 if not any(line.startswith("Machine:") and line.split(":", 1)[1].strip() == machine for line in fields):
17 raise ValueError(f"unexpected ELF target in {path}")
18 soname, = [line.split("[", 1)[1].split("]", 1)[0] for line in fields if "(SONAME)" in line]
19 exports = set()
20 for line in fields:
21 row = line.split()
22 if (len(row) >= 8 and row[0].endswith(":") and row[0][:-1].isdigit()
23 and row[4] in ("GLOBAL", "WEAK", "UNIQUE") and row[5] in ("DEFAULT", "PROTECTED")
24 and row[6] not in ("UND", "ABS")):
25 kind = "FUNC" if row[3] in ("FUNC", "IFUNC") else row[3]
26 exports.add((row[7], kind, row[2] if kind in ("OBJECT", "TLS") else ""))
27 if not exports:
28 raise ValueError(f"no exported dynamic symbols in {path}")
29 return soname, exports
30
31
32def run(args):
33 with args.initramfs.open("rb") as source:
34 if hashlib.file_digest(source, "sha256").hexdigest() != args.sha256:
35 raise ValueError("release initramfs checksum mismatch")
36 subprocess.run([args.auditor, args.arch, args.client], check=True)
37 root = (args.client / "root").resolve()
38 expected = {path: (package, soname) for package, path, soname in args.library}
39 if len(expected) != len(args.library):
40 raise ValueError("each release path must occur only once")
41 results = {}
42 with tempfile.TemporaryDirectory() as temp, tarfile.open(args.initramfs, "r|gz") as contents:
43 for entry in contents:
44 path = entry.name.removeprefix("./")
45 if path not in expected:
46 continue
47 package, name = expected[path]
48 if not entry.isfile() or path in results or Path(name).name != name:
49 raise ValueError(f"expected one regular release library at {path}")
50 reference = Path(temp) / "reference"
51 with contents.extractfile(entry) as source, reference.open("wb") as copy:
52 shutil.copyfileobj(source, copy)
53 old_name, old = interface(reference, args.arch)
54 alias = Path(path).with_name(name)
55 current = (root / alias).resolve()
56 current_path = current.relative_to(root)
57 records = [d for d in (args.client / "db/local").glob("*/desc")
58 if f"\n%NAME%\n{package}\n" in "\n" + d.read_text()]
59 record, = records
60 owned = set(record.with_name("files").read_text().splitlines())
61 if alias.as_posix() not in owned or current_path.as_posix() not in owned:
62 raise ValueError(f"{package} must own both {alias} and {current_path}")
63 new_name, new = interface(current, args.arch)
64 if old_name != name or new_name != name:
65 raise ValueError(f"{path}: expected {name}, found {old_name} and {new_name}")
66 missing = sorted(old - new)
67 results[path] = (package, name, current_path, len(old), len(new), len(missing),
68 ",".join(symbol for symbol, _, _ in missing))
69 if set(results) != set(expected):
70 raise ValueError(f"missing release libraries: {sorted(set(expected) - set(results))}")
71 with args.output.open("x") as stream:
72 table = csv.writer(stream, delimiter="\t", lineterminator="\n")
73 table.writerow(("release_path", "package", "soname", "installed_path", "old_exports",
74 "new_exports", "missing_count", "missing_symbols"))
75 for path, result in sorted(results.items()):
76 table.writerow((path, *result))
77 return int(any(row[-2] for row in results.values()))
78
79
80if __name__ == "__main__":
81 parser = argparse.ArgumentParser(description=__doc__)
82 parser.add_argument("arch", choices=("aarch64", "x86_64"))
83 for arg in ("client", "auditor", "initramfs", "output"):
84 parser.add_argument(arg, type=Path)
85 parser.add_argument("sha256", help="published initramfs digest")
86 parser.add_argument("--library", nargs=3, action="append", metavar=("PACKAGE", "RELEASE_PATH", "SONAME"),
87 required=True)
88 raise SystemExit(run(parser.parse_args()))
89