Files
leleka/engine/strata/tools/test_source_nul_bytes.py
T
Pavlo RatushnyiandClaude Sonnet 5.5 87e2633afe engine/strata: bring the Strata subtree to upstream 41cf05a0 (engine 0.1.50) with the local patches re-applied
Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree
(570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base =
the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main).

Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR
experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details,
conflict table and left-out files are in docs/upstream.md.

Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61),
docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out.

scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run").
Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR
macros on. No GPU run, no quality gate.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 12:49:15 +01:00

56 lines
2.4 KiB
Python

"""No raw NUL byte in a source file. A shell heredoc that loses the backslash of '\\0' leaves a literal 0x00 inside a
character constant: gcc only warns ("null character(s) preserved in literal"), MSVC stops with an error (0.1.43.1's
generate.cpp; expert_source.cpp and pinned.cu after it). No binary, no GPU.
python -m unittest tools.test_source_nul_bytes
"""
from __future__ import annotations
import subprocess
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
SOURCE_SUFFIXES = {".c", ".cc", ".cpp", ".cxx", ".h", ".hpp", ".cu", ".cuh", ".hip", ".mm", ".metal", ".py", ".cmake",
".txt", ".sh", ".bat", ".ps1", ".js", ".html", ".css", ".json", ".jinja", ".md", ".in"}
SOURCE_DIRS = ("src", "include", "sycl", "metal", "serve", "tools", "tests", "cmake", "bench", "docs")
def source_files() -> list[Path]:
try:
out = subprocess.run(["git", "ls-files", "-z", "--", *SOURCE_DIRS, "*.py", "*.txt", "*.sh", "*.bat"],
cwd=ROOT, capture_output=True, check=True).stdout
names = [n.decode("utf-8", "surrogateescape") for n in out.split(b"\0") if n]
paths = [ROOT / n for n in names]
except (OSError, subprocess.CalledProcessError): # not a git checkout (a release zip): walk the tree instead
paths = [p for d in SOURCE_DIRS if (ROOT / d).is_dir() for p in (ROOT / d).rglob("*")]
paths += [p for p in ROOT.iterdir() if p.is_file()]
return sorted({p for p in paths if p.is_file() and p.suffix.lower() in SOURCE_SUFFIXES})
def nul_lines(path: Path) -> list[int]:
data = path.read_bytes()
if b"\0" not in data:
return []
return [i + 1 for i, line in enumerate(data.split(b"\n")) if b"\0" in line]
class SourceNulBytes(unittest.TestCase):
def test_no_source_file_holds_a_nul_byte(self):
files = source_files()
self.assertGreater(len(files), 50, "the source scan found almost nothing")
bad = {str(p.relative_to(ROOT)): nul_lines(p) for p in files}
bad = {k: v for k, v in bad.items() if v}
self.assertEqual(bad, {}, "raw NUL bytes (write '\\0' instead) at file: [lines]")
def test_the_check_sees_a_nul(self):
import tempfile
with tempfile.TemporaryDirectory() as d:
p = Path(d) / "x.cpp"
p.write_bytes(b"int a;\nchar c = '\0';\n")
self.assertEqual(nul_lines(p), [2])
if __name__ == "__main__":
unittest.main()