ZX revision 30 runs ordinary main(argc, argv) programs unchanged: the guest sees its own bundle as its executable, checked program exports link to their foreign declarations, and declared native support libraries travel as verified bundle assets with per-target library and symbol mappings. Linux x86-64 bridges variadic and indirect C calls, callbacks from native threads, naturally aligned records and checked C pointers. Each native entry keeps its runtime state in R13, so packaged runtimes load after threads start without static TLS; inline assembly meters fuel through the same prefix. Authored C free and realloc accept runtime builder storage, checked TextFromCString measures C strings through libc strlen, and copied deferred cleanup loops keep their own identities. Pointer-type interning removes repeated full-table scans in large programs. Runtime ABI 3, machine image 4 and lowered schema 7 require fresh bundles and native caches. Bootstrap seeds refreshed for 89 roots.
314 lines
17 KiB
Ziran
314 lines
17 KiB
Ziran
// 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, and ARM images through the disk cache.
|
|
|
|
using Vectors :: #import "std/vec"
|
|
using TextTools :: #import "std/text_tools"
|
|
using Cache :: #import "compiler_backend_cache"
|
|
using Backend :: #import "compiler_backend_process"
|
|
using DiskCache :: #import "compiler_native_cache"
|
|
using Image :: #import "compiler_machine_image"
|
|
using Target :: #import "compiler_target"
|
|
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
|
|
}
|
|
|
|
// Android apps store ARMv7 and AArch64 images, and an ARMv7 image carries
|
|
// runtime helper relocations: both must decode to their own target.
|
|
ArmImages :: () -> bool {
|
|
code: [8]u8
|
|
data: [8]u8
|
|
functions := s64.[0]
|
|
helpers := Image.MachineRelocation.[.{section = .Code, width = 4, kind = .RuntimeHelper}]
|
|
no_relocations: [0]Image.MachineRelocation
|
|
for arch: Target.TargetArch.[.ArmV7, .AArch64] {
|
|
target := Target.TargetDescribe(arch, .Android).target
|
|
relocations := ifx arch == .ArmV7 then helpers[:] else no_relocations[:]
|
|
image := Image.MachineImage.{version = Image.MachineImageVersion, target = target, runtime_abi = .Revision3,
|
|
code = .{bytes = code[:], alignment = 8}, constants = .{alignment = 8}, data = .{bytes = data[:], alignment = 8},
|
|
function_offsets = functions[:], relocations = relocations, stack_alignment = 16, stack_bytes = 16,
|
|
stack_limit_offset = -1, trap_symbols = 1}
|
|
globals: [0]DiskCache.NativeCacheGlobal
|
|
key: DiskCache.NativeCacheDigest
|
|
bytes: Vec(u8)
|
|
defer VecFree(bytes)
|
|
restored: DiskCache.NativeCacheOwner
|
|
defer DiskCache.NativeCacheFree(*restored)
|
|
if !Check(DiskCache.NativeCacheEncode(key, image, globals[:], true, *bytes), "encode an Android machine image") ||
|
|
!Check(DiskCache.NativeCacheDecodeChecked(bytes[:], key, *restored) == .None, "decode an Android machine image") ||
|
|
!Check(restored.image.image.target.arch == arch && restored.image.image.target.os == .Android &&
|
|
restored.image.image.target.abi == target.abi && restored.image.image.relocations.count == relocations.count,
|
|
"restore the Android target and its runtime relocations") { return false }
|
|
}
|
|
return true
|
|
}
|
|
|
|
main :: () -> s32 {
|
|
if !Options() { return 1 }
|
|
if !Probe() { return 1 }
|
|
if !Parse() { return 1 }
|
|
if !Environment() { return 1 }
|
|
if !Workers() { return 1 }
|
|
if !ArmImages() { return 1 }
|
|
print("Native cache modules: pipe requests, probe parsing, fingerprints, environment boundaries, the affinity worker limit and ARM disk images passed\n")
|
|
return 0
|
|
}
|