// Module-level checks for the generated native build/cache modules: pipe // requests inside linker arguments, probe fingerprints across recognized // version interleaving, compiler-environment boundaries, and the worker // limit's CPU-affinity contract. using Vectors :: #import "std/vec" using TextTools :: #import "std/text_tools" using Cache :: #import "compiler_backend_cache" using Backend :: #import "compiler_backend_process" libc :: #system_library "libc"; AffinityBytes :: 128 SchedGetAffinity :: (process: s32, size: usize, mask: *u8) -> s32 #foreign libc "sched_getaffinity"; SchedSetAffinity :: (process: s32, size: usize, mask: *u8) -> s32 #foreign libc "sched_setaffinity"; Check :: (condition: bool, message: string) -> bool { if condition { return true } print("FAIL: %\n", message) return false } Join2 :: (a: string, b: string) -> string { bytes: Vec(u8) unused BuilderAppend(bytes, a); unused BuilderAppend(bytes, b) return TextDetach(TextFromBuilder(bytes)) } Join3 :: (a: string, b: string, c: string) -> string { bytes: Vec(u8) unused BuilderAppend(bytes, a); unused BuilderAppend(bytes, b); unused BuilderAppend(bytes, c) return TextDetach(TextFromBuilder(bytes)) } Join4 :: (a: string, b: string, c: string, d: string) -> string { bytes: Vec(u8) unused BuilderAppend(bytes, a); unused BuilderAppend(bytes, b); unused BuilderAppend(bytes, c); unused BuilderAppend(bytes, d) return TextDetach(TextFromBuilder(bytes)) } Join5 :: (a: string, b: string, c: string, d: string, e: string) -> string { bytes: Vec(u8) unused BuilderAppend(bytes, a); unused BuilderAppend(bytes, b); unused BuilderAppend(bytes, c); unused BuilderAppend(bytes, d); unused BuilderAppend(bytes, e) return TextDetach(TextFromBuilder(bytes)) } Join6 :: (a: string, b: string, c: string, d: string, e: string, f: string) -> string { bytes: Vec(u8) unused BuilderAppend(bytes, a); unused BuilderAppend(bytes, b); unused BuilderAppend(bytes, c); unused BuilderAppend(bytes, d); unused BuilderAppend(bytes, e); unused BuilderAppend(bytes, f) return TextDetach(TextFromBuilder(bytes)) } Join7 :: (a: string, b: string, c: string, d: string, e: string, f: string, g: string) -> string { bytes: Vec(u8) unused BuilderAppend(bytes, a); unused BuilderAppend(bytes, b); unused BuilderAppend(bytes, c); unused BuilderAppend(bytes, d); unused BuilderAppend(bytes, e); unused BuilderAppend(bytes, f); unused BuilderAppend(bytes, g) return TextDetach(TextFromBuilder(bytes)) } Join8 :: (a: string, b: string, c: string, d: string, e: string, f: string, g: string, h: string) -> string { bytes: Vec(u8) unused BuilderAppend(bytes, a); unused BuilderAppend(bytes, b); unused BuilderAppend(bytes, c); unused BuilderAppend(bytes, d); unused BuilderAppend(bytes, e); unused BuilderAppend(bytes, f); unused BuilderAppend(bytes, g); unused BuilderAppend(bytes, h) return TextDetach(TextFromBuilder(bytes)) } Options :: () -> bool { plain := string.["cc", "-Xlinker", "-pipe", "-B", "-pipe"] requested := string.["cc", "-Xlinker", "-pipe", "-B", "tools", "-pipe"] joined := string.["cc", "-Wl,-pipe", "-B-pipe", "-fuse-ld=lld"] return Check(!Cache.RequestedPipe(plain[:]), "a trailing -pipe flag alone is not a pipe request") && Check(Cache.RequestedPipe(requested[:]), "a -pipe output request is recognized") && Check(!Cache.RequestedPipe(joined[:]), "joined -Wl,-pipe, -B-pipe and -fuse-ld=lld are not pipe requests") } Parse :: () -> bool { cache: Cache.BuildCache hash := Cache.HashNew() probe := "Debian clang version 19.1.7\n (in-process)\n \"/bin/true\" -cc1 -v\n#include \"...\" search starts here:\n /tmp\nEnd of search list.\n" if !Check(Cache.ParseProbe(*cache, *hash, probe, false), "clang's in-process line is not a tool") { return false } if !Check(cache.roots.count == 1 && cache.roots[0] == "/tmp", "clang search roots remain parseable") { Cache.ClearNames(*cache.roots); VecFree(cache.roots); return false } Cache.ClearNames(*cache.roots) VecFree(cache.roots) return true } DigestOf :: (text: string) -> Cache.Digest { hash := Cache.HashNew() unused Cache.HashProbeOutput(*hash, text) result: Cache.Digest Cache.HashFinish(*hash, *result) return result } Same :: (left: string, right: string, equal: bool, label: string) -> bool { same := Cache.SameDigest(DigestOf(left), DigestOf(right)) return Check(same == equal, label) } Changed :: (text: string, index: s64) -> string { bytes: Vec(u8) for at: cast(s64)0..text.count - 1 { value := text[at] if at == index { value = ifx value == cast(u8)#char "X" then cast(u8)#char "Y" else cast(u8)#char "X" } unused VecPush(bytes, value) } return TextDetach(TextFromBuilder(bytes)) } Mutations :: (reference: string, text: string, label: string) -> bool { for index: cast(s64)0..text.count - 1 { if text[index] == cast(u8)#char "\n" { continue } if !Same(reference, Changed(text, index), false, label) { return false } } return true } Probe :: () -> bool { compiler := "\tcompiled by GNU C version 99.0.0, " other_compiler := "\tcompiled by GNU C version 99.0.1, " unknown_compiler := "Other compiler version, " libraries := "GMP version 6.3.0, MPFR version 4.2.1, MPC version 1.3.1, isl version 0.27" other_libraries := "GMP version 7.0.0, MPFR version 4.2.1, MPC version 1.3.1, isl version 0.27" unknown_libraries := "Other library metadata" unknown_metadata := "Unknown library metadata" assembler := "GNU assembler version 2.44 (target) using BFD version 2.44" other_assembler := "GNU assembler version 2.45 (target) using BFD version 2.44" bare_assembler := "GNU assembler version 2.44 (target)" options := " /tools/cc1 -DVALUE=1 -DVALUE=2 -o - |\n /tools/as -o /dev/null\n" swapped_options := " /tools/cc1 -DVALUE=2 -DVALUE=1 -o - |\n /tools/as -o /dev/null\n" unrecognized := "unrecognized prefix, " newline := "\n" crlf := "\r\n" blank := "" ordinary := Join6(options, compiler, libraries, newline, assembler, newline) interleaved := Join6(options, compiler, assembler, newline, libraries, newline) if !Same(ordinary, interleaved, true, "recognized version interleaving keeps the fingerprint") { return false } if !Same(ordinary, Join6(assembler, newline, options, compiler, libraries, newline), true, "leading assembler lines keep the fingerprint") { return false } if !Same(Join4(compiler, libraries, newline, assembler), Join4(compiler, assembler, newline, libraries), true, "interleaving without options keeps the fingerprint") { return false } ordinary_crlf := Join5(compiler, libraries, crlf, assembler, crlf) if !Same(ordinary_crlf, Join5(compiler, assembler, crlf, libraries, crlf), true, "CRLF interleaving keeps the fingerprint") { return false } if !Same(ordinary, Join6(options, other_compiler, libraries, newline, assembler, newline), false, "a compiler version change changes the fingerprint") { return false } if !Same(ordinary, Join6(options, compiler, other_libraries, newline, assembler, newline), false, "a library version change changes the fingerprint") { return false } if !Same(ordinary, Join6(options, compiler, libraries, newline, other_assembler, newline), false, "an assembler version change changes the fingerprint") { return false } if !Same(ordinary, Join6(options, other_compiler, assembler, newline, libraries, newline), false, "a compiler version change under interleaving changes the fingerprint") { return false } if !Same(ordinary, Join6(options, compiler, assembler, newline, other_libraries, newline), false, "a library version change under interleaving changes the fingerprint") { return false } if !Same(ordinary, Join6(options, compiler, other_assembler, newline, libraries, newline), false, "an assembler version change under interleaving changes the fingerprint") { return false } if !Same(ordinary, Join6(swapped_options, compiler, libraries, newline, assembler, newline), false, "an option order change changes the fingerprint") { return false } trailing := "\nextra metadata\n" if !Same(ordinary, Join6(options, compiler, libraries, newline, assembler, trailing), false, "trailing metadata changes the fingerprint") { return false } if !Same(ordinary, Join8(options, compiler, libraries, newline, assembler, newline, assembler, newline), false, "a duplicated assembler line changes the fingerprint") { return false } if !Same(ordinary, Join5(options, compiler, assembler, newline, unknown_metadata), false, "unknown interleaved metadata changes the fingerprint") { return false } if !Same(ordinary, Join6(options, unrecognized, assembler, newline, libraries, newline), false, "an unrecognized prefix changes the fingerprint") { return false } if !Same(Join5(unknown_compiler, libraries, newline, assembler, newline), Join5(unknown_compiler, assembler, newline, libraries, newline), false, "an unrecognized compiler version never interleaves") { return false } if !Same(Join5(compiler, unknown_libraries, newline, assembler, newline), Join5(compiler, assembler, newline, unknown_libraries, newline), false, "unknown library metadata never interleaves") { return false } if !Same(Join5(compiler, libraries, newline, bare_assembler, newline), Join5(compiler, bare_assembler, newline, libraries, newline), false, "an assembler line without a BFD version never interleaves") { return false } quoted := " cc1 -D'TEXT=" quoted_close := "'\n" if !Same(Join6(quoted, compiler, assembler, quoted_close, libraries, newline), Join6(quoted, compiler, libraries, quoted_close, assembler, newline), false, "quoted option text never interleaves") { return false } block := Join5(compiler, assembler, newline, libraries, newline) if !Same(Join2(block, block), Join7(compiler, libraries, newline, assembler, newline, block, blank), true, "repeated recognized blocks keep the fingerprint") { return false } checksum := "Compiler executable checksum: " digits := "0051dfc1fa4e89ba97760da93f2546f3" checked := Join6(options, checksum, digits, newline, assembler, newline) split_digit := "0051df" split_tail := "c1fa4e89ba97760da93f2546f3\n" split := Join6(options, checksum, split_digit, assembler, newline, split_tail) if !Same(checked, split, true, "checksum digits split across interleaving keep the fingerprint") { return false } if !Same(checked, Join6(options, checksum, assembler, newline, digits, newline), true, "a fully interleaved checksum keeps the fingerprint") { return false } checked_crlf := Join5(checksum, digits, crlf, assembler, crlf) if !Same(checked_crlf, Join5(checksum, digits, assembler, crlf, crlf), true, "CRLF checksum interleaving keeps the fingerprint") { return false } split_digit4 := "0051df" split_tail4 := "c1fa4e89ba97760da93f2546f4\n" if !Same(checked, Join6(options, checksum, split_digit4, newline, assembler, split_tail4), false, "a changed checksum digit changes the fingerprint") { return false } split_tail_truncated := "c1fa4e89ba97760da93f2546f\n" if !Same(checked, Join6(options, checksum, split_digit, newline, assembler, split_tail_truncated), false, "a truncated checksum changes the fingerprint") { return false } if !Same(checked, Join6(options, checksum, digits, newline, assembler, trailing), false, "trailing metadata after a checksum changes the fingerprint") { return false } if !Same(checked, Join6(options, checksum, digits, newline, bare_assembler, newline), false, "a checksum with a BFD-less assembler line changes the fingerprint") { return false } if !Same(checked, Join6(options, checksum, digits, newline, other_assembler, newline), false, "a checksum with a changed assembler version changes the fingerprint") { return false } other_checksum := "Other checksum: " if !Same(checked, Join6(options, other_checksum, digits, newline, assembler, newline), false, "another checksum label changes the fingerprint") { return false } if !Mutations(ordinary, interleaved, "every ordinary probe byte change changes the fingerprint") { return false } if !Mutations(checked, split, "every checksum probe byte change changes the fingerprint") { return false } return true } SameMask :: (left: []u8, right: []u8) -> bool { for index: cast(s64)0..left.count - 1 { if left[index] != right[index] { return false } } return true } CountMask :: (mask: []u8) -> s64 { count: s64 = 0 for byte: mask { for bit: 0..7 { if (byte & (cast(u8)1 << cast(u8)bit)) != 0 { count += 1 } } } return count } Workers :: () -> bool { limit := Backend.WorkerLimit() if !Check(limit >= 1, "the worker limit is positive") { return false } original: [AffinityBytes]u8 if !Check(SchedGetAffinity(0, AffinityBytes, *original[0]) == 0, "read the original affinity mask") { return false } available := CountMask(original[:]) if !Check(available > 0 && limit <= available, "the worker limit respects available CPUs") { return false } first: s64 = -1 for index: cast(s64)0..AffinityBytes - 1 { for bit: 0..7 { if (original[index] & (cast(u8)1 << cast(u8)bit)) != 0 { first = index * 8 + bit; break } } if first >= 0 { break } } if !Check(first >= 0, "the affinity mask has a CPU") { return false } one: [AffinityBytes]u8 one[first / 8] = cast(u8)1 << cast(u8)(first % 8) if !Check(SchedSetAffinity(0, AffinityBytes, *one[0]) == 0, "restrict affinity to one CPU") { return false } restricted := Backend.WorkerLimit() if !Check(SchedSetAffinity(0, AffinityBytes, *original[0]) == 0, "restore the original affinity mask") { return false } if !Check(restricted == 1, "one-CPU affinity limits workers to one") { return false } restored: [AffinityBytes]u8 if !Check(SchedGetAffinity(0, AffinityBytes, *restored[0]) == 0, "read the restored affinity mask") { return false } return Check(SameMask(original[:], restored[:]), "restoring leaves the parent mask unchanged") } Environment :: () -> bool { bytes: [12]u8 = .[67, 67, 61, 116, 101, 115, 116, 0, 76, 65, 78, 71] entries: Vectors.Vec(string) if !Check(Cache.EnvironmentEntries(bytes[:], *entries), "environment parsing accepts a final entry") { return false } if !Check(entries.count == 2, "NUL separators close each environment entry") { return false } if !Check(entries[0] == "CC=test" && entries[1] == "LANG", "environment entries preserve text") { VecFree(entries); return false } VecFree(entries) return true } main :: () -> s32 { if !Options() { return 1 } if !Probe() { return 1 } if !Parse() { return 1 } if !Environment() { return 1 } if !Workers() { return 1 } print("Native cache modules: pipe requests, probe parsing, fingerprints, environment boundaries and the affinity worker limit passed\n") return 0 }