okami.mom/strata

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

Niko1221binary_deps: a Windows DLL found only in a sub-folder counts as bundled only when BUILD.json lists that folder in lib_dirs (the RX 9000 package's rocblas.dll under rocm/bin made a plain start die with 0xC0000135)2ca90e48e

main
6.7 KiB148 linesraw
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`)."""
22    va, raw = 0x1000, 0x400
23    body = bytearray()
24    descriptors = (len(imports) + 1) * 20
25    delays = (len(delay) + 1) * 32
26    names_at = descriptors + delays
27    name_rvas = []
28    blob = bytearray()
29    for n in list(imports) + list(delay):
30        name_rvas.append(va + names_at + len(blob))
31        blob += n.encode("ascii") + b"\0"
32    for i, _ in enumerate(imports):
33        body += struct.pack("<IIIII", 0, 0, 0, name_rvas[i], 0)
34    body += b"\0" * 20
35    for j, _ in enumerate(delay):
36        body += struct.pack("<II", 1, name_rvas[len(imports) + j]) + b"\0" * 24
37    body += b"\0" * 32
38    body += blob
39    size = len(body)
40    dos = bytearray(0x40)
41    dos[:2] = b"MZ"
42    struct.pack_into("<I", dos, 0x3C, 0x40)
43    coff = b"PE\0\0" + struct.pack("<HHIIIHH", 0x8664, 1, 0, 0, 0, 240, 0x22)
44    opt = bytearray(240)
45    struct.pack_into("<H", opt, 0, 0x20B)
46    struct.pack_into("<II", opt, 112 + 8, va if imports else 0, descriptors if imports else 0)
47    struct.pack_into("<II", opt, 112 + 13 * 8, va + descriptors if delay else 0, delays if delay else 0)
48    section = b".idata\0\0" + struct.pack("<IIIIIIHHI", size, va, size, raw, 0, 0, 0, 0, 0x40000040)
49    head = bytes(dos) + coff + bytes(opt) + section
50    return head + b"\0" * (raw - len(head)) + bytes(body)
51
52
53class Imports(unittest.TestCase):
54    def test_pe_reader_lists_load_time_and_delay_load_imports(self):
55        with tempfile.TemporaryDirectory() as d:
56            p = Path(d) / "a.exe"
57            p.write_bytes(make_pe(["cublas64_13.dll", "KERNEL32.dll"], delay=["cudart64_13.dll"]))
58            self.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")
61    def test_elf_reader_on_a_system_binary(self):
62        names = binary_deps.elf_needed(Path("/bin/ls") if Path("/bin/ls").exists() else Path(sys.executable))
63        self.assertTrue(any(n.startswith("libc.so") for n in names), names)
64
65
66class Rule(unittest.TestCase):
67    def setUp(self):
68        self.dir = tempfile.TemporaryDirectory()
69        self.addCleanup(self.dir.cleanup)
70        self.pkg = Path(self.dir.name) / "pkg"
71        self.pkg.mkdir()
72
73    def 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"]))
75        self.assertEqual(binary_deps.uncovered(self.pkg), [("strata.exe", "cublas64_13.dll")])
76
77    def test_the_visual_cpp_runtime_is_not_an_os_library(self):
78        (self.pkg / "strata.exe").write_bytes(make_pe(["VCRUNTIME140.dll", "MSVCP140.dll"]))
79        self.assertEqual({n for _, n in binary_deps.uncovered(self.pkg)}, {"VCRUNTIME140.dll", "MSVCP140.dll"})
80
81    def 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"]))
84        self.assertEqual(binary_deps.uncovered(self.pkg), [("cublas64_13.dll", "cublasLt64_13.dll")])
85        (self.pkg / "CUBLASLT64_13.DLL").write_bytes(make_pe(["KERNEL32.dll"]))
86        self.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):
91    def setUp(self):
92        self.dir = tempfile.TemporaryDirectory()
93        self.addCleanup(self.dir.cleanup)
94        self.root = Path(self.dir.name)
95        self.src = self.root / "src"
96        self.src.mkdir()
97        (self.src / "strata.exe").write_bytes(make_pe(["cublas64_13.dll", "KERNEL32.dll"]))
98        self.toolkit = self.root / "toolkit"
99        self.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"]))
102        self.out = self.root / "out"
103        self.out.mkdir()
104
105    def build(self, search):
106        return package_engine.build(self.src, "cuda", "0.1.45.1", "windows-x86_64", self.out, archs=("sm_86",), build_id="t", search=search)
107
108    def test_it_bundles_the_imported_dlls_and_what_they_import(self):
109        folder = self.build([self.toolkit])
110        self.assertTrue((folder / "cublas64_13.dll").is_file() and (folder / "cublasLt64_13.dll").is_file())
111        self.assertEqual(binary_deps.uncovered(folder), [])
112        self.assertEqual(package_engine.main(["--verify", str(folder)]), 0)
113
114    def test_it_refuses_when_a_dll_cannot_be_found(self):
115        empty = self.root / "empty"
116        empty.mkdir()
117        with self.assertRaises(SystemExit) as raised:
118            self.build([empty])
119        self.assertIn("cublas64_13.dll", str(raised.exception))
120        self.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
128    def make(self, d, lib_dirs):
129        import json
130        d = 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
136    def test_subfolder_dll_not_on_the_path_is_flagged(self):
137        with tempfile.TemporaryDirectory() as d:
138            self.make(d, [])
139            self.assertEqual(binary_deps.uncovered(Path(d)), [("strata.exe", "rocblas.dll")])
140
141    def test_subfolder_dll_listed_in_lib_dirs_passes(self):
142        with tempfile.TemporaryDirectory() as d:
143            self.make(d, ["rocm/bin"])
144            self.assertEqual(binary_deps.uncovered(Path(d)), [])
145
146
147if __name__ == "__main__":
148    unittest.main()