okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221packaging: bundle libatomic.so.1 in the Linux AMD packages; check imports against the base system1a5c6dd19

main
9.8 KiB186 linesraw
1"""The Linux rule of an engine package: every library a bundled file needs (DT_NEEDED) is in the package or on the short base-system list.
2
3boxia 2026-11-15: the hip-rx6000 / rx9070 / rx7900 packages of 0.2.14 hold librocm_sysdeps_numa.so.1, which needs libatomic.so.1; no package
4bundled it, because the old list called libatomic "a library every distribution has". A container without the libatomic1 package could not start
5the engine. Proves: the base list is glibc and the C++ runtime only; a finished archive is checked member by member (a symlink is not bundled,
6the install keeps none); collect_libs.py bundles libatomic.so.1 from the build machine as a real file under its soname, with its notice; and,
7on the owner PC where the published 0.2.14 files are (C:\\rc214\\FINAL4\\UPLOAD, read only, or STRATA_FINAL4_DIR), the archive check finds
8exactly today's gap: libatomic.so.1 in the three AMD packages, nothing in hip-halo or the CUDA ones. Tiny real ELF files, no toolchain needed."""
9
10import io
11import os
12import struct
13import sys
14import tarfile
15import tempfile
16import unittest
17from pathlib import Path
18from unittest import mock
19
20ROOT = Path(__file__).resolve().parents[2]
21sys.path.insert(0, str(ROOT / "packaging"))
22import binary_deps  # noqa: E402
23import collect_libs  # noqa: E402
24
25FINAL4 = Path(os.environ.get("STRATA_FINAL4_DIR", r"C:\rc214\FINAL4\UPLOAD"))
26
27
28def make_elf(needed, soname=None) -> bytes:
29    """A minimal ELF64 little-endian file with a .dynstr and a .dynamic section naming `needed` (DT_NEEDED) and `soname` (DT_SONAME)."""
30    strtab = bytearray(b"\0")
31    entries = []
32    for name in needed:
33        entries.append((1, len(strtab)))
34        strtab += name.encode() + b"\0"
35    if soname:
36        entries.append((14, len(strtab)))
37        strtab += soname.encode() + b"\0"
38    entries.append((0, 0))
39    str_off = 64
40    dyn_off = (str_off + len(strtab) + 7) // 8 * 8
41    dynamic = b"".join(struct.pack("<qQ", tag, val) for tag, val in entries)
42    sh_off = (dyn_off + len(dynamic) + 7) // 8 * 8
43    header = bytearray(64)
44    header[:4] = b"\x7fELF"
45    header[4], header[5], header[6] = 2, 1, 1
46    struct.pack_into("<HHI", header, 16, 3, 0x3E, 1)
47    struct.pack_into("<Q", header, 0x28, sh_off)
48    struct.pack_into("<HHHHHH", header, 0x34, 64, 0, 0, 64, 3, 1)
49
50    def section(stype, offset, size, link=0, entsize=0):
51        return struct.pack("<IIQQQQIIQQ", 0, stype, 0, 0, offset, size, link, 0, 8, entsize)
52    sections = section(0, 0, 0) + section(3, str_off, len(strtab)) + section(6, dyn_off, len(dynamic), link=1, entsize=16)
53    out = bytearray(header)
54    out += b"\0" * (str_off - len(out)) + strtab
55    out += b"\0" * (dyn_off - len(out)) + dynamic
56    out += b"\0" * (sh_off - len(out)) + sections
57    return bytes(out)
58
59
60def make_tar(path: Path, files: dict, links: dict | None = None) -> None:
61    with tarfile.open(path, "w:gz") as tar:
62        for name, data in files.items():
63            info = tarfile.TarInfo(name)
64            info.size = len(data)
65            tar.addfile(info, io.BytesIO(data))
66        for name, target in (links or {}).items():
67            info = tarfile.TarInfo(name)
68            info.type = tarfile.SYMTYPE
69            info.linkname = target
70            tar.addfile(info)
71
72
73class BaseList(unittest.TestCase):
74    def test_the_reader_reads_the_tiny_elf(self):
75        self.assertEqual(binary_deps.elf_needed_bytes(make_elf(["libatomic.so.1", "libc.so.6"], soname="librocm_sysdeps_numa.so.1")),
76                         ["libatomic.so.1", "libc.so.6"])
77
78    def test_the_base_list_is_glibc_and_the_cpp_runtime(self):
79        for name in ("libc.so.6", "libm.so.6", "libdl.so.2", "libpthread.so.0", "librt.so.1", "ld-linux-x86-64.so.2", "libgcc_s.so.1", "libstdc++.so.6"):
80            self.assertTrue(binary_deps._match(name, binary_deps.LINUX_OS, False), name)
81        for name in ("libatomic.so.1", "libgomp.so.1", "libz.so.1", "libnuma.so.1", "libdrm.so.2", "libelf.so.1", "libtinfo.so.6"):
82            self.assertFalse(binary_deps._match(name, binary_deps.LINUX_OS, False), name)
83
84    def test_a_package_folder_without_libatomic_is_refused_and_with_it_passes(self):
85        with tempfile.TemporaryDirectory() as d:
86            pkg = Path(d)
87            (pkg / "strata").write_bytes(make_elf(["librocm_sysdeps_numa.so.1", "libc.so.6", "libstdc++.so.6"]))
88            (pkg / "librocm_sysdeps_numa.so.1").write_bytes(make_elf(["libatomic.so.1", "libc.so.6"]))
89            self.assertEqual(binary_deps.uncovered(pkg), [("librocm_sysdeps_numa.so.1", "libatomic.so.1")])
90            (pkg / "libatomic.so.1").write_bytes(make_elf(["libc.so.6"], soname="libatomic.so.1"))
91            self.assertEqual(binary_deps.uncovered(pkg), [])
92
93
94class Archive(unittest.TestCase):
95    def setUp(self):
96        tmp = tempfile.TemporaryDirectory()
97        self.addCleanup(tmp.cleanup)
98        self.root = Path(tmp.name)
99        self.files = {"strata": make_elf(["librocm_sysdeps_numa.so.1", "libhsa-runtime64.so.1", "libc.so.6"]),
100                      "librocm_sysdeps_numa.so.1": make_elf(["libatomic.so.1", "libc.so.6"]),
101                      "BUILD.json": b"{}", "licenses/notice.txt": b"text"}
102
103    def test_the_archive_check_names_the_gap(self):
104        tar = self.root / "a.tar.gz"
105        make_tar(tar, self.files)
106        self.assertEqual(binary_deps.archive_uncovered(tar), [("librocm_sysdeps_numa.so.1", "libatomic.so.1")])
107        self.assertEqual(binary_deps.main(["binary_deps.py", str(tar)]), 1)
108
109    def test_a_bundled_libatomic_closes_it_and_a_symlink_does_not(self):
110        tar = self.root / "b.tar.gz"
111        make_tar(tar, self.files, links={"libatomic.so.1": "libatomic.so.1.2.0"})
112        self.assertEqual(binary_deps.archive_uncovered(tar), [("librocm_sysdeps_numa.so.1", "libatomic.so.1")])
113        tar = self.root / "c.tar.gz"
114        make_tar(tar, {**self.files, "libatomic.so.1": make_elf(["libc.so.6"], soname="libatomic.so.1")})
115        self.assertEqual(binary_deps.archive_uncovered(tar), [])
116        self.assertEqual(binary_deps.main(["binary_deps.py", str(tar)]), 0)
117
118
119class Collect(unittest.TestCase):
120    """collect_libs.py with ldd faked: the build machine's libatomic is copied as a real file under its soname, with its notice; libc stays the OS's."""
121
122    def test_libatomic_is_bundled_with_its_notice(self):
123        with tempfile.TemporaryDirectory() as d:
124            root = Path(d)
125            system = root / "usr" / "lib"
126            system.mkdir(parents=True)
127            real = system / "libatomic.so.1.2.0"
128            real.write_bytes(make_elf(["libc.so.6"], soname="libatomic.so.1"))
129            (system / "libc.so.6").write_bytes(make_elf([]))
130            rocm = root / "rocm"
131            rocm.mkdir()
132            (rocm / "librocm_sysdeps_numa.so.1").write_bytes(make_elf(["libatomic.so.1", "libc.so.6"]))
133            binary = root / "strata"
134            binary.write_bytes(make_elf(["librocm_sysdeps_numa.so.1", "libc.so.6"]))
135            out = root / "stage"
136
137            def deps(item, _search):
138                name = Path(item).name
139                if name == "strata":
140                    return {"librocm_sysdeps_numa.so.1": str(rocm / "librocm_sysdeps_numa.so.1"), "libc.so.6": str(system / "libc.so.6")}, []
141                if name == "librocm_sysdeps_numa.so.1":
142                    return {"libatomic.so.1": str(real), "libc.so.6": str(system / "libc.so.6")}, []
143                return {"libc.so.6": str(system / "libc.so.6")}, []
144            with mock.patch.object(collect_libs, "dependencies", deps), \
145                    mock.patch.object(collect_libs, "is_system", lambda p: str(p).startswith(str(system))), \
146                    mock.patch.object(collect_libs, "_owner_package", return_value=("libatomic1", "14.2.0-4ubuntu2~24.04.1")):
147                self.assertEqual(collect_libs.main(["--binary", str(binary), "--out", str(out)]), 0)
148            atomic = out / "libatomic.so.1"
149            self.assertTrue(atomic.is_file() and not atomic.is_symlink())
150            self.assertEqual(atomic.read_bytes(), real.read_bytes())
151            self.assertFalse((out / "libc.so.6").exists())
152            self.assertEqual((out / "SYSTEM-LIBS.txt").read_text().split(), ["libc.so.6"])
153            notice = (out / "licenses" / "libatomic.txt").read_text()
154            self.assertIn("GPL-3.0-or-later WITH GCC-exception-3.1", notice)
155            self.assertIn("(distribution package libatomic1 14.2.0-4ubuntu2~24.04.1)", notice)
156            self.assertEqual(binary_deps.uncovered(out), [])
157
158    def test_the_bundle_list(self):
159        self.assertTrue(collect_libs.bundled_anyway("libatomic.so.1"))
160        self.assertFalse(collect_libs.bundled_anyway("libc.so.6"))
161        self.assertTrue(collect_libs.bundled_anyway("libfoo.so.2", ["libfoo.so*"]))
162        self.assertTrue((ROOT / "packaging" / "notices" / "libatomic.txt").is_file())
163
164
165@unittest.skipUnless(FINAL4.is_dir(), "the published 0.2.14 files are only on the owner PC (C:\\rc214\\FINAL4\\UPLOAD) or in STRATA_FINAL4_DIR")
166class PublishedPackages(unittest.TestCase):
167    """The check on the real 0.2.14 Linux engine archives (read only): it would have refused the three AMD packages for libatomic.so.1."""
168
169    def check(self, name):
170        path = FINAL4 / name
171        if not path.is_file():
172            self.skipTest(f"{name} is not in {FINAL4}")
173        return binary_deps.archive_uncovered(path)
174
175    def test_the_three_amd_packages_lack_libatomic(self):
176        for variant in ("rx6000", "rx9070", "rx7900"):
177            self.assertEqual(self.check(f"strata-engine-0.1.60-linux-x86_64-hip-{variant}.tar.gz"), [("librocm_sysdeps_numa.so.1", "libatomic.so.1")], variant)
178
179    def test_halo_and_cuda_need_nothing_else(self):
180        for name in ("strata-engine-0.1.60-linux-x86_64-hip-halo.tar.gz", "strata-engine-0.1.60-linux-x86_64-cuda-cuda13.tar.gz",
181                     "strata-engine-0.1.60-linux-x86_64-cuda-cuda12.tar.gz", "strata-engine-0.1.59-linux-aarch64-cuda-cuda13.tar.gz"):
182            self.assertEqual(self.check(name), [], name)
183
184
185if __name__ == "__main__":
186    unittest.main()