okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1a5c6dd19
main
1#!/usr/bin/env python3 2"""What an engine file needs from the machine it runs on: PE (Windows), ELF (Linux) and Mach-O (macOS) import lists, read 3with no tools and no third-party packages, and the rule "everything imported is bundled or belongs to the OS or the GPU driver". 4 5Why: on 2026-10-17 a tester's clean Windows 11 PC failed to start the engine with 0xC0000135 (a DLL was not found): the 6package held strata.exe and strata_kq.dll but not cublas64_13.dll / cublasLt64_13.dll, which strata.exe imports. Our own 7clean-profile test passed only because the build PC has the CUDA Toolkit on PATH. The packager now refuses a package whose imports 8are not covered, and `--check-deps` / the test suite run the same rule on any finished package. 9 10python packaging/binary_deps.py <package folder> # exit 0 when every import is bundled or system, 1 with the list 11 12Library names are compared case-insensitively on Windows and by soname elsewhere. 13""" 14 15from __future__import annotations 16 17import fnmatch 18import struct 19import sys 20from pathlib import Path 21 22# ---- Windows --------------------------------------------------------------------------------------------------------------- 23# Libraries Windows itself ships (a clean Windows 10/11 has them) or the GPU driver installs in System32. Anything else an 24# engine file imports (the Visual C++ runtime, CUDA, HIP, SYCL/Level Zero loaders, OpenMP...) must travel in the package. 25WINDOWS_OS = ( 26"kernel32.dll" ,"kernelbase.dll" ,"user32.dll" ,"gdi32.dll" ,"advapi32.dll" ,"shell32.dll" ,"ole32.dll" ,"oleaut32.dll" , 27"ws2_32.dll" ,"wsock32.dll" ,"mswsock.dll" ,"iphlpapi.dll" ,"bcrypt.dll" ,"crypt32.dll" ,"secur32.dll" ,"sechost.dll" , 28"ntdll.dll" ,"msvcrt.dll" ,"ucrtbase.dll" ,"rpcrt4.dll" ,"shlwapi.dll" ,"setupapi.dll" ,"cfgmgr32.dll" ,"version.dll" , 29"winmm.dll" ,"imm32.dll" ,"comctl32.dll" ,"comdlg32.dll" ,"dbghelp.dll" ,"psapi.dll" ,"pdh.dll" ,"powrprof.dll" , 30"userenv.dll" ,"netapi32.dll" ,"wininet.dll" ,"winhttp.dll" ,"dxgi.dll" ,"d3d11.dll" ,"d3d12.dll" ,"dwmapi.dll" , 31"dxcore.dll" ,"wtsapi32.dll" ,"ncrypt.dll" ,"normaliz.dll" ,"api-ms-win-*.dll" ,"ext-ms-win-*.dll" ,"hid.dll" , 32"opengl32.dll" ,"glu32.dll" ,"wldap32.dll" ,"authz.dll" ,"msi.dll" ,"propsys.dll" ,"uxtheme.dll" ,"dnsapi.dll" , 33) 34# Installed by the GPU driver into System32 (never redistributable by us): the CUDA driver API, the NVIDIA management library, 35# and the AMD / Intel user-mode driver pieces the driver package owns. 36WINDOWS_DRIVER = ( 37"nvcuda.dll" ,"nvml.dll" ,"nvapi64.dll" ,"nvapi.dll" ,"nvcuvid.dll" ,"nvEncodeAPI64.dll" ,"nvfatbinaryloader.dll" , 38"amdxx64.dll" ,"amdhip64_*.dll" ,"amdhip64.dll" ,"atiadlxx.dll" ,"atiadlxy.dll" ,"amdadlx64.dll" , 39"igdrcl64.dll" ,"ze_intel_gpu64.dll" ,"ze_loader.dll" ,"OpenCL.dll" ,"vulkan-1.dll" , 40) 41 42# ---- Linux ----------------------------------------------------------------------------------------------------------------- 43# The base system: what every Linux has, a container and a minimal install included (glibc, the C++ runtime). Names are sonames or glob 44# patterns. Anything else an engine file needs travels in the package. Until 0.2.14 this list also held libgomp, libatomic, libz, libnuma, 45# libdrm, libelf, libtinfo, libcrypt, libffi, libpci, libudev, libX11 and libxcb as "every distribution has these": the AMD packages then 46# shipped without libatomic.so.1 (librocm_sysdeps_numa.so.1 needs it) and a container without the libatomic1 package could not start the 47# engine (boxia, 2026-11-15). Every Linux engine package of 0.2.14 needs nothing else outside this list (archive_uncovered on the release files). 48LINUX_OS = ( 49"linux-vdso.so.1" ,"ld-linux-x86-64.so.2" ,"ld-linux-aarch64.so.1" ,"libc.so.6" ,"libm.so.6" ,"libdl.so.2" ,"libpthread.so.0" , 50"librt.so.1" ,"libutil.so.1" ,"libresolv.so.2" ,"libnss_*.so.*" ,"libgcc_s.so.1" ,"libstdc++.so.6" , 51) 52# From the GPU driver package: never bundled. 53LINUX_DRIVER = ( 54"libcuda.so.1" ,"libcuda.so" ,"libnvidia-*.so*" ,"libnvcuvid.so*" ,"libze_intel_gpu.so*" ,"libze_loader.so*" , 55"libOpenCL.so*" ,"libvulkan.so*" ,"libGLX*.so*" ,"libEGL*.so*" ,"libamdgpu*.so*" ,"libhsa-runtime64.so*" , 56"libdrm_amdgpu.so*" ,"libigdrcl.so*" ,"libnvidia-ml.so*" , 57) 58 59# ---- macOS ----------------------------------------------------------------------------------------------------------------- 60MAC_OS_PREFIXES = ("/usr/lib/" ,"/System/Library/" ) 61 62 63def _match (name :str ,patterns :tuple [str , ...],fold :bool )-> bool : 64n = name .lower ()if fold else name 65return any (fnmatch .fnmatchcase (n , (p .lower ()if fold else p ))for p in patterns ) 66 67 68# ---- readers --------------------------------------------------------------------------------------------------------------- 69def pe_imports (path :Path )-> list [str ]: 70"""DLL names a PE file imports at load time (import table) plus delay-load imports (loaded at first call).""" 71data = path .read_bytes () 72if data [:2 ]!= b"MZ" : 73raise ValueError (f" { path } : not a PE file" ) 74pe = struct .unpack_from ("<I" ,data ,0x3C )[0 ] 75if data [pe :pe + 4 ]!= b"PE\0\0" : 76raise ValueError (f" { path } : bad PE header" ) 77nsec ,= struct .unpack_from ("<H" ,data ,pe + 6 ) 78opt_size ,= struct .unpack_from ("<H" ,data ,pe + 20 ) 79opt = pe + 24 80magic ,= struct .unpack_from ("<H" ,data ,opt ) 81dd = opt + (112 if magic == 0x20B else 96 ) 82sections = [] 83sec = opt + opt_size 84for i in range (nsec ): 85vsize ,va ,rsize ,raw = struct .unpack_from ("<IIII" ,data ,sec + i * 40 + 8 ) 86sections .append ((va ,max (vsize ,rsize ),raw )) 87 88def off (rva :int )-> int : 89for va ,size ,raw in sections : 90if va <= rva < va + size : 91return raw + rva - va 92raise ValueError (f" { path } : RVA { rva :#x } is outside every section" ) 93 94def cstr (o :int )-> str : 95end = data .index (b"\0" ,o ) 96return data [o :end ].decode ("ascii" ,"replace" ) 97 98names :list [str ]= [] 99imp_rva ,imp_size = struct .unpack_from ("<II" ,data ,dd + 8 ) 100if imp_rva and imp_size : 101o = off (imp_rva ) 102while True : 103_ ,_ ,_ ,name_rva ,_ = struct .unpack_from ("<IIIII" ,data ,o ) 104if not name_rva : 105break 106names .append (cstr (off (name_rva ))) 107o += 20 108delay_rva ,delay_size = struct .unpack_from ("<II" ,data ,dd + 13 * 8 ) 109if delay_rva and delay_size : 110o = off (delay_rva ) 111while True : 112attrs ,name_rva = struct .unpack_from ("<II" ,data ,o ) 113if not name_rva : 114break 115names .append (cstr (off (name_rva ))) 116o += 32 117return names 118 119 120def elf_needed (path :Path )-> list [str ]: 121"""DT_NEEDED sonames of an ELF file (what ldd lists); libraries opened with dlopen are not visible here.""" 122return elf_needed_bytes (path .read_bytes (),str (path )) 123 124 125def elf_needed_bytes (data :bytes ,where :str = "the file" )-> list [str ]: 126"""elf_needed() for the bytes of an ELF file (a member of a package archive that is never unpacked).""" 127if data [:4 ]!= b"\x7fELF" : 128raise ValueError (f" { where } : not an ELF file" ) 129is64 = data [4 ]== 2 130le = "<" if data [5 ]== 1 else ">" 131if is64 : 132shoff ,= struct .unpack_from (le + "Q" ,data ,0x28 ) 133shentsize ,shnum = struct .unpack_from (le + "HH" ,data ,0x3A ) 134else : 135shoff ,= struct .unpack_from (le + "I" ,data ,0x20 ) 136shentsize ,shnum = struct .unpack_from (le + "HH" ,data ,0x2E ) 137out :list [str ]= [] 138for i in range (shnum ): 139base = shoff + i * shentsize 140if is64 : 141stype ,= struct .unpack_from (le + "I" ,data ,base + 4 ) 142offset ,size ,link = struct .unpack_from (le + "QQI" ,data ,base + 0x18 ) 143else : 144stype ,= struct .unpack_from (le + "I" ,data ,base + 4 ) 145offset ,size ,link = struct .unpack_from (le + "III" ,data ,base + 0x10 ) 146if stype != 6 :# SHT_DYNAMIC 147continue 148sbase = shoff + link * shentsize 149stroff = struct .unpack_from (le + ("Q" if is64 else "I" ),data ,sbase + (0x18 if is64 else 0x10 ))[0 ] 150step = 16 if is64 else 8 151for o in range (offset ,offset + size ,step ): 152tag ,val = struct .unpack_from (le + ("qQ" if is64 else "iI" ),data ,o ) 153if tag == 0 : 154break 155if tag == 1 : 156s = stroff + val 157out .append (data [s :data .index (b"\0" ,s )].decode ("ascii" ,"replace" )) 158return out 159 160 161def macho_dylibs (path :Path )-> list [str ]: 162"""Install names a Mach-O file loads (LC_LOAD_DYLIB, weak, reexport). A fat file reads its first slice.""" 163data = path .read_bytes () 164magic = struct .unpack_from (">I" ,data ,0 )[0 ] 165base = 0 166if magic in (0xCAFEBABE ,0xCAFEBABF ): 167base = struct .unpack_from (">I" ,data ,16 )[0 ]if magic == 0xCAFEBABE else struct .unpack_from (">Q" ,data ,24 )[0 ] 168magic = struct .unpack_from (">I" ,data ,base )[0 ] 169if struct .unpack_from ("<I" ,data ,base )[0 ]not in (0xFEEDFACF ,0xFEEDFACE ): 170raise ValueError (f" { path } : not a Mach-O file" ) 171is64 = struct .unpack_from ("<I" ,data ,base )[0 ]== 0xFEEDFACF 172ncmds ,= struct .unpack_from ("<I" ,data ,base + 16 ) 173o = base + (32 if is64 else 28 ) 174out = [] 175for _ in range (ncmds ): 176cmd ,size = struct .unpack_from ("<II" ,data ,o ) 177if cmd in (0xC ,0x80000018 ,0x8000001F ):# LOAD_DYLIB, LOAD_WEAK_DYLIB, REEXPORT_DYLIB 178name_off ,= struct .unpack_from ("<I" ,data ,o + 8 ) 179s = o + name_off 180out .append (data [s :data .index (b"\0" ,s )].decode ("utf-8" ,"replace" )) 181o += size 182return out 183 184 185def imports (path :Path )-> tuple [str ,list [str ]]: 186"""(kind, names) for a binary, or ("", []) when it is not an executable format this module reads.""" 187with path .open ("rb" )as f : 188head = f .read (4 ) 189if head [:2 ]== b"MZ" : 190return "pe" ,pe_imports (path ) 191if head == b"\x7fELF" : 192return "elf" ,elf_needed (path ) 193if head in (b"\xcf\xfa\xed\xfe" ,b"\xce\xfa\xed\xfe" ,b"\xca\xfe\xba\xbe" ,b"\xbe\xba\xfe\xca" ): 194return "macho" ,macho_dylibs (path ) 195return "" , [] 196 197 198# ---- the rule -------------------------------------------------------------------------------------------------------------- 199def uncovered (folder :Path ,files :list [str ]| None = None )-> list [tuple [str ,str ]]: 200"""(file, imported library) pairs that are neither in the package nor the OS's nor the GPU driver's. Empty = a clean PC runs it.""" 201folder = Path (folder ) 202rels = files if files is not None else [p .relative_to (folder ).as_posix ()for p in sorted (folder .rglob ("*" ))if p .is_file ()and not p .is_symlink ()] 203# Windows loads a DLL from the program's own folder, System32 and PATH; a DLL in a sub-folder counts only when BUILD.json lists 204# that folder in "lib_dirs" (Control and the app put those folders on PATH when they start the engine). Found 2026-10-18: the 205# RX 9000 package kept rocblas.dll under rocm/bin, which only Control's PATH finds, and a plain start died with 0xC0000135. 206lib_dirs = {"" } 207if any (r == "BUILD.json" for r in rels ): 208try : 209import json 210lib_dirs |= {str (d ).strip ("/\\" ).replace ("\\" ,"/" )for d in json .loads ((folder / "BUILD.json" ).read_text (encoding = "utf-8" )).get ("lib_dirs" , [])} 211except (OSError ,ValueError ): 212pass 213bundled_win = {Path (r ).name .lower ()for r in rels if ("" if Path (r ).parent .as_posix ()== "." else Path (r ).parent .as_posix ())in lib_dirs } 214bundled = {Path (r ).name for r in rels } 215problems :list [tuple [str ,str ]]= [] 216for rel in rels : 217p = folder / rel 218if p .suffix .lower ()in (".json" ,".txt" ,".md" ,".cu" ,".h" ,".py" ,".yaml" ,".yml" ,".metallib" ,".bin" ): 219continue 220try : 221kind ,names = imports (p ) 222except (ValueError ,OSError ,struct .error ): 223continue 224for name in names : 225if kind == "pe" : 226if name .lower ()in bundled_win or _match (name ,WINDOWS_OS ,True )or _match (name ,WINDOWS_DRIVER ,True ): 227continue 228elif kind == "elf" : 229if name in bundled or _match (name ,LINUX_OS ,False )or _match (name ,LINUX_DRIVER ,False ): 230continue 231else : 232base = name .rsplit ("/" ,1 )[- 1 ] 233if name .startswith (MAC_OS_PREFIXES )or base in bundled : 234continue 235problems .append ((rel ,name )) 236return problems 237 238 239def archive_uncovered (archive :Path ,base :tuple [str , ...]| None = None )-> list [tuple [str ,str ]]: 240"""The Linux rule of `uncovered` for a finished package archive (.tar.gz), read member by member and never unpacked: (file, imported 241library) pairs whose library is neither a file in the archive nor on ``base`` (default LINUX_OS) nor a GPU driver library. A symlink in the 242archive does not count as bundled: the package manifest and the install never keep symlinks (package_engine.py, engine_component.verify).""" 243import tarfile 244base = LINUX_OS if base is None else base 245needs :list [tuple [str ,str ]]= [] 246bundled :set [str ]= set () 247with tarfile .open (archive ,"r:*" )as tar : 248for member in tar : 249if not member .isfile (): 250continue 251bundled .add (member .name .rsplit ("/" ,1 )[- 1 ]) 252stream = tar .extractfile (member ) 253if stream is None or stream .read (4 )!= b"\x7fELF" : 254continue 255data = b"\x7fELF" + stream .read () 256try : 257needs += [(member .name ,n )for n in elf_needed_bytes (data ,member .name )] 258except (ValueError ,struct .error ): 259continue 260return [(rel ,name )for rel ,name in needs if name not in bundled and not _match (name ,base ,False )and not _match (name ,LINUX_DRIVER ,False )] 261 262 263def main (argv :list [str ])-> int : 264if len (argv )!= 2 : 265__doc__ ) 266return 2 267if argv [1 ].endswith ((".tar.gz" ,".tgz" ,".tar" ))and Path (argv [1 ]).is_file (): 268bad = archive_uncovered (Path (argv [1 ])) 269for rel ,name in bad : 270f" { rel } : needs { name } , which is not in the package and not an OS or driver library" ) 271if not bad : 272f" { argv [ 1 ] } : every imported library is bundled or belongs to the OS / GPU driver" ) 273return 1 if bad else 0 274bad = uncovered (Path (argv [1 ])) 275for rel ,name in bad : 276f" { rel } : needs { name } , which is not in the package and not an OS or driver library" ) 277if not bad : 278f" { argv [ 1 ] } : every imported library is bundled or belongs to the OS / GPU driver" ) 279return 1 if bad else 0 280 281 282if __name__ == "__main__" : 283raise SystemExit (main (sys .argv ))