okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2ca90e48e
main
1"""An engine package must carry every library its binaries import, except the OS's and the GPU driver's. 2 3Tester report 2026-10-17: a clean Windows 11 PC failed with 0xC0000135 because strata.exe imports cublas64_13.dll and the package 4did not hold it (the build PC had the CUDA Toolkit on PATH, which hid the problem). These tests build tiny real PE files, so they 5run anywhere and need no GPU or toolkit.""" 6 7import os 8import struct 9import sys 10import tempfile 11import unittest 12from pathlib import Path 13 14ROOT = Path (__file__ ).resolve ().parents [2 ] 15sys .path .insert (0 ,str (ROOT / "packaging" )) 16import binary_deps # noqa: E402 17import package_engine # noqa: E402 18 19 20def make_pe (imports ,delay = ()): 21"""A minimal PE32+ file with a .idata section that imports the named DLLs (and delay-loads `delay`).""" 22va ,raw = 0x1000 ,0x400 23body = bytearray () 24descriptors = (len (imports )+ 1 )* 20 25delays = (len (delay )+ 1 )* 32 26names_at = descriptors + delays 27name_rvas = [] 28blob = bytearray () 29for n in list (imports )+ list (delay ): 30name_rvas .append (va + names_at + len (blob )) 31blob += n .encode ("ascii" )+ b"\0" 32for i ,_ in enumerate (imports ): 33body += struct .pack ("<IIIII" ,0 ,0 ,0 ,name_rvas [i ],0 ) 34body += b"\0" * 20 35for j ,_ in enumerate (delay ): 36body += struct .pack ("<II" ,1 ,name_rvas [len (imports )+ j ])+ b"\0" * 24 37body += b"\0" * 32 38body += blob 39size = len (body ) 40dos = bytearray (0x40 ) 41dos [:2 ]= b"MZ" 42struct .pack_into ("<I" ,dos ,0x3C ,0x40 ) 43coff = b"PE\0\0" + struct .pack ("<HHIIIHH" ,0x8664 ,1 ,0 ,0 ,0 ,240 ,0x22 ) 44opt = bytearray (240 ) 45struct .pack_into ("<H" ,opt ,0 ,0x20B ) 46struct .pack_into ("<II" ,opt ,112 + 8 ,va if imports else 0 ,descriptors if imports else 0 ) 47struct .pack_into ("<II" ,opt ,112 + 13 * 8 ,va + descriptors if delay else 0 ,delays if delay else 0 ) 48section = b".idata\0\0" + struct .pack ("<IIIIIIHHI" ,size ,va ,size ,raw ,0 ,0 ,0 ,0 ,0x40000040 ) 49head = bytes (dos )+ coff + bytes (opt )+ section 50return head + b"\0" * (raw - len (head ))+ bytes (body ) 51 52 53class Imports (unittest .TestCase ): 54def test_pe_reader_lists_load_time_and_delay_load_imports (self ): 55with tempfile .TemporaryDirectory ()as d : 56p = Path (d )/ "a.exe" 57p .write_bytes (make_pe (["cublas64_13.dll" ,"KERNEL32.dll" ],delay = ["cudart64_13.dll" ])) 58self .assertEqual (binary_deps .pe_imports (p ), ["cublas64_13.dll" ,"KERNEL32.dll" ,"cudart64_13.dll" ]) 59 60@ unittest . skipUnless ( sys . platform . startswith ( "linux" ), "needs a real ELF" ) 61def test_elf_reader_on_a_system_binary (self ): 62names = binary_deps .elf_needed (Path ("/bin/ls" )if Path ("/bin/ls" ).exists ()else Path (sys .executable )) 63self .assertTrue (any (n .startswith ("libc.so" )for n in names ),names ) 64 65 66class Rule (unittest .TestCase ): 67def setUp (self ): 68self .dir = tempfile .TemporaryDirectory () 69self .addCleanup (self .dir .cleanup ) 70self .pkg = Path (self .dir .name )/ "pkg" 71self .pkg .mkdir () 72 73def test_a_missing_cuda_dll_is_reported_and_the_driver_and_os_are_not (self ): 74 (self .pkg / "strata.exe" ).write_bytes (make_pe (["cublas64_13.dll" ,"KERNEL32.dll" ,"ADVAPI32.dll" ,"nvcuda.dll" ,"api-ms-win-crt-heap-l1-1-0.dll" ])) 75self .assertEqual (binary_deps .uncovered (self .pkg ), [("strata.exe" ,"cublas64_13.dll" )]) 76 77def test_the_visual_cpp_runtime_is_not_an_os_library (self ): 78 (self .pkg / "strata.exe" ).write_bytes (make_pe (["VCRUNTIME140.dll" ,"MSVCP140.dll" ])) 79self .assertEqual ({n for _ ,n in binary_deps .uncovered (self .pkg )}, {"VCRUNTIME140.dll" ,"MSVCP140.dll" }) 80 81def test_bundled_libraries_count_and_their_own_imports_are_checked (self ): 82 (self .pkg / "strata.exe" ).write_bytes (make_pe (["cublas64_13.dll" ])) 83 (self .pkg / "cublas64_13.dll" ).write_bytes (make_pe (["cublasLt64_13.dll" ,"KERNEL32.dll" ])) 84self .assertEqual (binary_deps .uncovered (self .pkg ), [("cublas64_13.dll" ,"cublasLt64_13.dll" )]) 85 (self .pkg / "CUBLASLT64_13.DLL" ).write_bytes (make_pe (["KERNEL32.dll" ])) 86self .assertEqual (binary_deps .uncovered (self .pkg ), []) 87 88 89@ unittest . skipUnless ( os . name == "nt" , "the packager only copies DLLs for Windows targets" ) 90class Packager (unittest .TestCase ): 91def setUp (self ): 92self .dir = tempfile .TemporaryDirectory () 93self .addCleanup (self .dir .cleanup ) 94self .root = Path (self .dir .name ) 95self .src = self .root / "src" 96self .src .mkdir () 97 (self .src / "strata.exe" ).write_bytes (make_pe (["cublas64_13.dll" ,"KERNEL32.dll" ])) 98self .toolkit = self .root / "toolkit" 99self .toolkit .mkdir () 100 (self .toolkit / "cublas64_13.dll" ).write_bytes (make_pe (["cublasLt64_13.dll" ,"KERNEL32.dll" ])) 101 (self .toolkit / "cublasLt64_13.dll" ).write_bytes (make_pe (["KERNEL32.dll" ])) 102self .out = self .root / "out" 103self .out .mkdir () 104 105def build (self ,search ): 106return package_engine .build (self .src ,"cuda" ,"0.1.45.1" ,"windows-x86_64" ,self .out ,archs = ("sm_86" ,),build_id = "t" ,search = search ) 107 108def test_it_bundles_the_imported_dlls_and_what_they_import (self ): 109folder = self .build ([self .toolkit ]) 110self .assertTrue ((folder / "cublas64_13.dll" ).is_file ()and (folder / "cublasLt64_13.dll" ).is_file ()) 111self .assertEqual (binary_deps .uncovered (folder ), []) 112self .assertEqual (package_engine .main (["--verify" ,str (folder )]),0 ) 113 114def test_it_refuses_when_a_dll_cannot_be_found (self ): 115empty = self .root / "empty" 116empty .mkdir () 117with self .assertRaises (SystemExit )as raised : 118self .build ([empty ]) 119self .assertIn ("cublas64_13.dll" ,str (raised .exception )) 120self .assertFalse (any (self .out .iterdir ())and any (p .name .startswith ("0.1.45.1" )for p in self .out .iterdir ())) 121 122 123 124 125class SubfolderDlls (unittest .TestCase ): 126"""A DLL only in a sub-folder is found only when BUILD.json lists the folder (Control puts those on PATH).""" 127 128def make (self ,d ,lib_dirs ): 129import json 130d = Path (d ) 131 (d / "strata.exe" ).write_bytes (make_pe (["rocblas.dll" ,"KERNEL32.dll" ])) 132 (d / "rocm" / "bin" ).mkdir (parents = True ) 133 (d / "rocm" / "bin" / "rocblas.dll" ).write_bytes (make_pe (["KERNEL32.dll" ])) 134 (d / "BUILD.json" ).write_text (json .dumps ({"lib_dirs" :lib_dirs })) 135 136def test_subfolder_dll_not_on_the_path_is_flagged (self ): 137with tempfile .TemporaryDirectory ()as d : 138self .make (d , []) 139self .assertEqual (binary_deps .uncovered (Path (d )), [("strata.exe" ,"rocblas.dll" )]) 140 141def test_subfolder_dll_listed_in_lib_dirs_passes (self ): 142with tempfile .TemporaryDirectory ()as d : 143self .make (d , ["rocm/bin" ]) 144self .assertEqual (binary_deps .uncovered (Path (d )), []) 145 146 147if __name__ == "__main__" : 148unittest .main ()