okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1a5c6dd19
main
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).""" 30strtab = bytearray (b"\0" ) 31entries = [] 32for name in needed : 33entries .append ((1 ,len (strtab ))) 34strtab += name .encode ()+ b"\0" 35if soname : 36entries .append ((14 ,len (strtab ))) 37strtab += soname .encode ()+ b"\0" 38entries .append ((0 ,0 )) 39str_off = 64 40dyn_off = (str_off + len (strtab )+ 7 )// 8 * 8 41dynamic = b"" .join (struct .pack ("<qQ" ,tag ,val )for tag ,val in entries ) 42sh_off = (dyn_off + len (dynamic )+ 7 )// 8 * 8 43header = bytearray (64 ) 44header [:4 ]= b"\x7fELF" 45header [4 ],header [5 ],header [6 ]= 2 ,1 ,1 46struct .pack_into ("<HHI" ,header ,16 ,3 ,0x3E ,1 ) 47struct .pack_into ("<Q" ,header ,0x28 ,sh_off ) 48struct .pack_into ("<HHHHHH" ,header ,0x34 ,64 ,0 ,0 ,64 ,3 ,1 ) 49 50def section (stype ,offset ,size ,link = 0 ,entsize = 0 ): 51return struct .pack ("<IIQQQQIIQQ" ,0 ,stype ,0 ,0 ,offset ,size ,link ,0 ,8 ,entsize ) 52sections = section (0 ,0 ,0 )+ section (3 ,str_off ,len (strtab ))+ section (6 ,dyn_off ,len (dynamic ),link = 1 ,entsize = 16 ) 53out = bytearray (header ) 54out += b"\0" * (str_off - len (out ))+ strtab 55out += b"\0" * (dyn_off - len (out ))+ dynamic 56out += b"\0" * (sh_off - len (out ))+ sections 57return bytes (out ) 58 59 60def make_tar (path :Path ,files :dict ,links :dict | None = None )-> None : 61with tarfile .open (path ,"w:gz" )as tar : 62for name ,data in files .items (): 63info = tarfile .TarInfo (name ) 64info .size = len (data ) 65tar .addfile (info ,io .BytesIO (data )) 66for name ,target in (links or {}).items (): 67info = tarfile .TarInfo (name ) 68info .type = tarfile .SYMTYPE 69info .linkname = target 70tar .addfile (info ) 71 72 73class BaseList (unittest .TestCase ): 74def test_the_reader_reads_the_tiny_elf (self ): 75self .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 78def test_the_base_list_is_glibc_and_the_cpp_runtime (self ): 79for 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" ): 80self .assertTrue (binary_deps ._match (name ,binary_deps .LINUX_OS ,False ),name ) 81for name in ("libatomic.so.1" ,"libgomp.so.1" ,"libz.so.1" ,"libnuma.so.1" ,"libdrm.so.2" ,"libelf.so.1" ,"libtinfo.so.6" ): 82self .assertFalse (binary_deps ._match (name ,binary_deps .LINUX_OS ,False ),name ) 83 84def test_a_package_folder_without_libatomic_is_refused_and_with_it_passes (self ): 85with tempfile .TemporaryDirectory ()as d : 86pkg = 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" ])) 89self .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" )) 91self .assertEqual (binary_deps .uncovered (pkg ), []) 92 93 94class Archive (unittest .TestCase ): 95def setUp (self ): 96tmp = tempfile .TemporaryDirectory () 97self .addCleanup (tmp .cleanup ) 98self .root = Path (tmp .name ) 99self .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 103def test_the_archive_check_names_the_gap (self ): 104tar = self .root / "a.tar.gz" 105make_tar (tar ,self .files ) 106self .assertEqual (binary_deps .archive_uncovered (tar ), [("librocm_sysdeps_numa.so.1" ,"libatomic.so.1" )]) 107self .assertEqual (binary_deps .main (["binary_deps.py" ,str (tar )]),1 ) 108 109def test_a_bundled_libatomic_closes_it_and_a_symlink_does_not (self ): 110tar = self .root / "b.tar.gz" 111make_tar (tar ,self .files ,links = {"libatomic.so.1" :"libatomic.so.1.2.0" }) 112self .assertEqual (binary_deps .archive_uncovered (tar ), [("librocm_sysdeps_numa.so.1" ,"libatomic.so.1" )]) 113tar = self .root / "c.tar.gz" 114make_tar (tar , {** self .files ,"libatomic.so.1" :make_elf (["libc.so.6" ],soname = "libatomic.so.1" )}) 115self .assertEqual (binary_deps .archive_uncovered (tar ), []) 116self .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 122def test_libatomic_is_bundled_with_its_notice (self ): 123with tempfile .TemporaryDirectory ()as d : 124root = Path (d ) 125system = root / "usr" / "lib" 126system .mkdir (parents = True ) 127real = system / "libatomic.so.1.2.0" 128real .write_bytes (make_elf (["libc.so.6" ],soname = "libatomic.so.1" )) 129 (system / "libc.so.6" ).write_bytes (make_elf ([])) 130rocm = root / "rocm" 131rocm .mkdir () 132 (rocm / "librocm_sysdeps_numa.so.1" ).write_bytes (make_elf (["libatomic.so.1" ,"libc.so.6" ])) 133binary = root / "strata" 134binary .write_bytes (make_elf (["librocm_sysdeps_numa.so.1" ,"libc.so.6" ])) 135out = root / "stage" 136 137def deps (item ,_search ): 138name = Path (item ).name 139if name == "strata" : 140return {"librocm_sysdeps_numa.so.1" :str (rocm / "librocm_sysdeps_numa.so.1" ),"libc.so.6" :str (system / "libc.so.6" )}, [] 141if name == "librocm_sysdeps_numa.so.1" : 142return {"libatomic.so.1" :str (real ),"libc.so.6" :str (system / "libc.so.6" )}, [] 143return {"libc.so.6" :str (system / "libc.so.6" )}, [] 144with mock .patch .object (collect_libs ,"dependencies" ,deps ), \ 145mock .patch .object (collect_libs ,"is_system" ,lambda p :str (p ).startswith (str (system ))), \ 146mock .patch .object (collect_libs ,"_owner_package" ,return_value = ("libatomic1" ,"14.2.0-4ubuntu2~24.04.1" )): 147self .assertEqual (collect_libs .main (["--binary" ,str (binary ),"--out" ,str (out )]),0 ) 148atomic = out / "libatomic.so.1" 149self .assertTrue (atomic .is_file ()and not atomic .is_symlink ()) 150self .assertEqual (atomic .read_bytes (),real .read_bytes ()) 151self .assertFalse ((out / "libc.so.6" ).exists ()) 152self .assertEqual ((out / "SYSTEM-LIBS.txt" ).read_text ().split (), ["libc.so.6" ]) 153notice = (out / "licenses" / "libatomic.txt" ).read_text () 154self .assertIn ("GPL-3.0-or-later WITH GCC-exception-3.1" ,notice ) 155self .assertIn ("(distribution package libatomic1 14.2.0-4ubuntu2~24.04.1)" ,notice ) 156self .assertEqual (binary_deps .uncovered (out ), []) 157 158def test_the_bundle_list (self ): 159self .assertTrue (collect_libs .bundled_anyway ("libatomic.so.1" )) 160self .assertFalse (collect_libs .bundled_anyway ("libc.so.6" )) 161self .assertTrue (collect_libs .bundled_anyway ("libfoo.so.2" , ["libfoo.so*" ])) 162self .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 169def check (self ,name ): 170path = FINAL4 / name 171if not path .is_file (): 172self .skipTest (f" { name } is not in { FINAL4 } " ) 173return binary_deps .archive_uncovered (path ) 174 175def test_the_three_amd_packages_lack_libatomic (self ): 176for variant in ("rx6000" ,"rx9070" ,"rx7900" ): 177self .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 179def test_halo_and_cuda_need_nothing_else (self ): 180for 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" ): 182self .assertEqual (self .check (name ), [],name ) 183 184 185if __name__ == "__main__" : 186unittest .main ()