nx_galx_thumb_gate.nx source
↩ module page · 139 lines · 7448 B
1// nx_galx_thumb_gate.nx -- referee for /compare/mediaingest R3 (th_decode_any). Forks the LIVE nx_galx_thumb.elf
2// end to end over a fixture per capability class and asserts a valid thumbnail comes out. The per-format decode
3// correctness (JPEG baseline+progressive, GIF, WebP-lossless, BMP, TIFF, TGA, PCX, ICO, PNM, PNG all colour types)
4// is ALREADY gate-proven inside nx_img_bytes_to_rgb (2026-08-05), which th_decode_any composes rather than
5// reimplements -- so this gate proves the WIRING: PNG still thumbnails (regression), a NON-PNG format now
6// thumbnails through th_decode_any (the R3 capability), and garbage is refused (never a wrong image).
7// Scratch lives in /tmp so no production fixture is shared with a beat. license_tier: ORIGINAL No hw writes.
8import "nx_syscalls.nx"
9import "nx_gate_verdict.nx"
10
11const GT_ELF: *u8 = "nx_galx_thumb.elf"
12const GT_REALPNG:*u8 = "knowledge/compare/sample_front.png" // a stable checked-in PNG fixture
13const GT_DIR: *u8 = "/tmp/nx_galx_thumb_gate"
14const GT_PPM: *u8 = "/tmp/nx_galx_thumb_gate/in.ppm"
15const GT_BAD: *u8 = "/tmp/nx_galx_thumb_gate/bad.bin"
16const GT_OUT_PNG:*u8 = "/tmp/nx_galx_thumb_gate/out_png.png"
17const GT_OUT_PPM:*u8 = "/tmp/nx_galx_thumb_gate/out_ppm.png"
18const GT_OUT_BAD:*u8 = "/tmp/nx_galx_thumb_gate/out_bad.png"
19const GT_MODE: i64 = 0x1a4 // 0644 for fixture writes
20const GT_CAP: i64 = 4096
21
22func gt_strlen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
23
24func gt_has(hay: *u8, hn: i64, needle: *u8) -> i64 {
25 let nl: i64 = gt_strlen(needle)
26 if nl == 0 { return 0 }
27 var i: i64 = 0
28 while i + nl <= hn { var k: i64 = 0; var ok: i64 = 1; while k < nl { if hay[i+k] != needle[k] { ok = 0; k = nl } k = k + 1 } if ok == 1 { return 1 } i = i + 1 }
29 return 0
30}
31
32func gt_write_bytes(path: *u8, buf: *u8, n: i64) -> i64 { let fd: i64 = sys_openat_wr(path, GT_MODE); if fd < 0 { return 0 - 1 } sys_write(fd, buf, n); sys_close(fd); return 0 }
33
34// is the file at path a PNG (first 8 bytes == the PNG signature)?
35func gt_is_png(path: *u8) -> i64 {
36 let fd: i64 = sys_openat_rd(path)
37 if fd < 0 { return 0 }
38 let b: *u8 = sys_mmap(8)
39 let n: i64 = sys_read(fd, b, 8)
40 sys_close(fd)
41 if n < 8 { return 0 }
42 if b[0] != (137 as u8) { return 0 }
43 if b[1] != (80 as u8) { return 0 }
44 if b[2] != (78 as u8) { return 0 }
45 if b[3] != (71 as u8) { return 0 }
46 return 1
47}
48
49// fork + exec the elf with up to 5 args, capture stdout into out, wait; returns captured byte count.
50func gt_run(elf: *u8, a1: *u8, a2: *u8, a3: *u8, a4: *u8, a5: *u8, out: *u8, cap: i64) -> i64 {
51 let pb: *i64 = sys_mmap(16) as *i64
52 sys_pipe2(pb, 0)
53 let rfd: i64 = pb[0] & 0xFFFFFFFF
54 let wfd: i64 = (pb[0] >> 32) & 0xFFFFFFFF
55 let pid: i64 = sys_fork()
56 if pid == 0 {
57 sys_dup3(wfd, 1, 0)
58 sys_close(rfd); sys_close(wfd)
59 let av: *i64 = sys_mmap(80) as *i64
60 av[0] = elf as i64
61 var ac: i64 = 1
62 if a1 as i64 != 0 { av[ac] = a1 as i64; ac = ac + 1 }
63 if a2 as i64 != 0 { av[ac] = a2 as i64; ac = ac + 1 }
64 if a3 as i64 != 0 { av[ac] = a3 as i64; ac = ac + 1 }
65 if a4 as i64 != 0 { av[ac] = a4 as i64; ac = ac + 1 }
66 if a5 as i64 != 0 { av[ac] = a5 as i64; ac = ac + 1 }
67 av[ac] = 0
68 sys_execve(elf, av, 0 as *i64)
69 sys_exit(127)
70 }
71 sys_close(wfd)
72 var tot: i64 = 0
73 var n: i64 = sys_read(rfd, out, cap - 1)
74 while n > 0 { tot = tot + n; if tot >= cap - 1 { n = 0 } else { n = sys_read(rfd, (out as i64 + tot) as *u8, cap - 1 - tot) } }
75 sys_close(rfd)
76 let st: *i64 = sys_mmap(16) as *i64
77 sys_wait4(pid, st, 0)
78 out[tot] = 0 as u8
79 return tot
80}
81
82func main() -> i64 {
83 gv_head("=== nx_galx_thumb_gate -- thumbnail decoder coverage (mediaingest R3) ===" as *u8)
84 let c: *i64 = gv_ctr()
85
86 // ---- setup: private scratch dir; clear any prior outputs so a produced file proves THIS run ----
87 sys_mkdir(GT_DIR, 0x1ff) // 0777
88 sys_unlinkat(GT_OUT_PNG); sys_unlinkat(GT_OUT_PPM); sys_unlinkat(GT_OUT_BAD)
89
90 // build a P6 PPM fixture (2x2, a NON-PNG format the unified decoder supports) byte by byte
91 let ppm: *u8 = sys_mmap(64)
92 var p: i64 = 0
93 ppm[p]=80 as u8; p=p+1 // 'P'
94 ppm[p]=54 as u8; p=p+1 // '6'
95 ppm[p]=10 as u8; p=p+1 // \n
96 ppm[p]=50 as u8; p=p+1 // '2'
97 ppm[p]=32 as u8; p=p+1 // ' '
98 ppm[p]=50 as u8; p=p+1 // '2'
99 ppm[p]=10 as u8; p=p+1 // \n
100 ppm[p]=50 as u8; p=p+1 // '2'
101 ppm[p]=53 as u8; p=p+1 // '5'
102 ppm[p]=53 as u8; p=p+1 // '5'
103 ppm[p]=10 as u8; p=p+1 // \n
104 ppm[p]=255 as u8; ppm[p+1]=0 as u8; ppm[p+2]=0 as u8; p=p+3 // red
105 ppm[p]=0 as u8; ppm[p+1]=255 as u8; ppm[p+2]=0 as u8; p=p+3 // green
106 ppm[p]=0 as u8; ppm[p+1]=0 as u8; ppm[p+2]=255 as u8; p=p+3 // blue
107 ppm[p]=255 as u8; ppm[p+1]=255 as u8; ppm[p+2]=0 as u8; p=p+3 // yellow
108 let ppm_wrote: i64 = gt_write_bytes(GT_PPM, ppm, p)
109 gt_write_bytes(GT_BAD, "this is definitely not any image format at all" as *u8, 46)
110
111 let out: *u8 = sys_mmap(GT_CAP)
112
113 // ---- Tooth 1: the PNG path is preserved -- a real PNG still thumbnails to a valid PNG ----
114 let n1: i64 = gt_run(GT_ELF, "one" as *u8, GT_REALPNG, GT_OUT_PNG, 0 as *u8, 0 as *u8, out, GT_CAP)
115 var t1: i64 = 0
116 if gt_has(out, n1, "THUMB ok" as *u8) == 1 { if gt_is_png(GT_OUT_PNG) == 1 { t1 = 1 } }
117 gv_check("png-path-preserved-real-png-thumbnails-to-a-valid-png" as *u8, t1, c)
118
119 // ---- Tooth 2 (the R3 capability): a NON-PNG format now thumbnails end to end via th_decode_any ----
120 let n2: i64 = gt_run(GT_ELF, "one" as *u8, GT_PPM, GT_OUT_PPM, 0 as *u8, 0 as *u8, out, GT_CAP)
121 var t2: i64 = 0
122 if gt_has(out, n2, "THUMB ok" as *u8) == 1 { if gt_is_png(GT_OUT_PPM) == 1 { t2 = 1 } }
123 gv_check("non-png-ppm-thumbnails-end-to-end-proving-th_decode_any-dispatches-beyond-png" as *u8, t2, c)
124
125 // ---- Tooth 3 (neg-control): garbage is refused and NO output image is written ----
126 let n3: i64 = gt_run(GT_ELF, "one" as *u8, GT_BAD, GT_OUT_BAD, 0 as *u8, 0 as *u8, out, GT_CAP)
127 var t3: i64 = 0
128 if gt_has(out, n3, "THUMB FAIL" as *u8) == 1 { if gt_is_png(GT_OUT_BAD) == 0 { t3 = 1 } }
129 gv_check("neg-control-garbage-refused-and-no-wrong-image-written" as *u8, t3, c)
130
131 // ---- Tooth 4 (anti-vacuity): the fixtures actually reached the subject ----
132 // the PPM was written and the subject produced captured output for BOTH valid runs; without this a
133 // run where the elf never executed (n1==n2==0) could not silently score a pass on the file checks alone.
134 var t4: i64 = 0
135 if ppm_wrote == 0 { if n1 > 0 { if n2 > 0 { t4 = 1 } } }
136 gv_check("fixture-reached-subject-ran-and-emitted-output" as *u8, t4, c)
137
138 return gv_verdict("nx_galx_thumb_gate" as *u8, c, "forks the LIVE nx_galx_thumb.elf over a real PNG, a constructed P6 PPM and garbage; per-format decode correctness is proven inside nx_img_bytes_to_rgb which th_decode_any composes, so this referee proves the wiring end to end and the anti-vacuity tooth requires the subject to have actually run" as *u8)
139}