nx_mp4_faststart_gate.nx source
↩ module page · 150 lines · 9477 B
1// nx_mp4_faststart_gate.nx -- proves the qt-faststart remux is byte-correct: constructs a synthetic
2// NON-faststart mp4 ([ftyp][mdat][moov>trak>mdia>minf>stbl>stco]) with a known chunk offset, forks the
3// real nx_mp4_faststart.sov.elf on it, and asserts the output is [ftyp][moov][mdat] with the stco offset
4// rewritten by +moov_size. A remuxer that silently corrupts offsets = broken video, so this gate is the
5// liar-killer: T-neg replays a WRONG shift (0) and REQUIRES the check to fail.
6// MIGRATED 2026-09-02 onto nx_gate_verdict (D001): every tooth is a named gv_check, the missing-elf case is a
7// gv_need precondition (SKIP, never RED -- "could not look" is not "broken"), and /api/gate_run reads the verdict
8// from the exit code. The teeth and their fixtures are unchanged; the hand-rolled pass/total line is gone.
9// license_tier: ORIGINAL No hw writes (Rule 26). expect_exit: 0
10import "nx_syscalls.nx"
11import "nx_gate_verdict.nx"
12
13const G_TEST: *u8 = "/tmp/_fs_gate_in.mp4"
14const G_OUT: *u8 = "/tmp/_fs_gate_out.mp4"
15const G_LOG: *u8 = "knowledge/status/mp4_faststart_gate.log"
16
17func g_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
18func g_be32(b: *u8, o: i64) -> i64 { return ((b[o] as i64)<<24)|((b[o+1] as i64)<<16)|((b[o+2] as i64)<<8)|(b[o+3] as i64) }
19func g_p32(b: *u8, o: i64, v: i64) -> i64 { b[o]=((v>>24)&0xff) as u8; b[o+1]=((v>>16)&0xff) as u8; b[o+2]=((v>>8)&0xff) as u8; b[o+3]=(v&0xff) as u8; return 0 }
20func g_t(b: *u8, o: i64, t: *u8) -> i64 { var i: i64=0; while i<4 { b[o+i]=t[i]; i=i+1 } return 0 }
21func g_teq(b: *u8, o: i64, t: *u8) -> i64 { var i: i64=0; while i<4 { if b[o+i]!=t[i] { return 0 } i=i+1 } return 1 }
22func g_ck(name: *u8, c: i64, ctr: *i64) -> i64 { gv_check(name, c == 1, ctr); return c }
23
24// build the synthetic mp4 into b; returns total size. The single stco entry = CHUNK_OFF (points into mdat data).
25func g_build(b: *u8, chunk_off: i64) -> i64 {
26 // ftyp: size 16
27 g_p32(b,0,16); g_t(b,4,"ftyp" as *u8); g_t(b,8,"isom" as *u8); g_p32(b,12,0)
28 // mdat: size 24 (8 hdr + 16 data). mdat box at 16, data at 24.
29 g_p32(b,16,24); g_t(b,20,"mdat" as *u8); var i: i64=0; while i<16 { b[24+i]=(65+i) as u8; i=i+1 }
30 // moov(60) > trak(52) > mdia(44) > minf(36) > stbl(28) > stco(20) at offset 40
31 let M: i64 = 40
32 g_p32(b,M,60); g_t(b,M+4,"moov" as *u8)
33 g_p32(b,M+8,52); g_t(b,M+12,"trak" as *u8)
34 g_p32(b,M+16,44); g_t(b,M+20,"mdia" as *u8)
35 g_p32(b,M+24,36); g_t(b,M+28,"minf" as *u8)
36 g_p32(b,M+32,28); g_t(b,M+36,"stbl" as *u8)
37 g_p32(b,M+40,20); g_t(b,M+44,"stco" as *u8); g_p32(b,M+48,0); g_p32(b,M+52,1); g_p32(b,M+56,chunk_off)
38 return 100
39}
40func g_p64(b: *u8, o: i64, v: i64) -> i64 { var i: i64=0; while i<8 { b[o+7-i]=((v>>(i*8))&0xff) as u8; i=i+1 } return 0 }
41func g_be64(b: *u8, o: i64) -> i64 { var v: i64=0; var i: i64=0; while i<8 { v=(v<<8)|(b[o+i] as i64); i=i+1 } return v }
42// co64 variant (64-bit chunk offsets, used by every mp4 >4GB = the real case for large torrents). moov=64 (co64
43// entry is 8 bytes not 4 -> each nested box +4). Returns total size 104. co64 offset = chunk_off (a >4GB value).
44func g_build_co64(b: *u8, chunk_off: i64) -> i64 {
45 g_p32(b,0,16); g_t(b,4,"ftyp" as *u8); g_t(b,8,"isom" as *u8); g_p32(b,12,0)
46 g_p32(b,16,24); g_t(b,20,"mdat" as *u8); var i: i64=0; while i<16 { b[24+i]=(65+i) as u8; i=i+1 }
47 let M: i64 = 40
48 g_p32(b,M,64); g_t(b,M+4,"moov" as *u8)
49 g_p32(b,M+8,56); g_t(b,M+12,"trak" as *u8)
50 g_p32(b,M+16,48); g_t(b,M+20,"mdia" as *u8)
51 g_p32(b,M+24,40); g_t(b,M+28,"minf" as *u8)
52 g_p32(b,M+32,32); g_t(b,M+36,"stbl" as *u8)
53 g_p32(b,M+40,24); g_t(b,M+44,"co64" as *u8); g_p32(b,M+48,0); g_p32(b,M+52,1); g_p64(b,M+56,chunk_off)
54 return 104
55}
56
57func g_run_elf(inp: *u8, outp: *u8) -> i64 {
58 // try a few deploy locations for the built elf
59 // the PROMOTED name first (what the NAS serves and what /api/promote blessed), then the twins and the build
60 // lane's own artifact; the /tmp name last. Before 2026-09-02 only .sov.elf names were tried, so the gate read
61 // SKIP on the NAS with the remuxer promoted right beside it -- a subject present under a name the gate did not ask for.
62 let paths: *i64 = sys_mmap(64) as *i64
63 paths[0] = "./nx_mp4_faststart.elf" as *u8 as i64
64 paths[1] = "_offc/nx_mp4_faststart.elf" as *u8 as i64
65 paths[2] = "_build/nx_mp4_faststart.sov.elf" as *u8 as i64
66 paths[3] = "./nx_mp4_faststart.sov.elf" as *u8 as i64
67 paths[4] = "runtime/nx_mp4_faststart.sov.elf" as *u8 as i64
68 paths[5] = "/tmp/nx_mp4_faststart.sov.elf" as *u8 as i64
69 paths[6] = 0
70 var pi: i64 = 0; var elf: *u8 = 0 as *u8
71 var pf: i64 = 0 // exit by FLAG (2026-09-02, nx_srclint rule 2: `pi=99` inside `while paths[pi]` read paths[99], past the array)
72 while pf == 0 { if paths[pi] == 0 { pf = 1 } else { let fd: i64 = sys_openat_rd(paths[pi] as *u8); if fd>=0 { sys_close(fd); elf = paths[pi] as *u8; pf = 1 } else { pi=pi+1 } } }
73 if (elf as i64) == 0 { return 0-1 }
74 let ofd: i64 = sys_openat_wr(outp, 0x1a4); if ofd < 0 { return 0-1 }
75 let pid: i64 = sys_fork()
76 if pid == 0 {
77 sys_dup3(ofd, 1, 0)
78 let av: *i64 = sys_mmap(32) as *i64; av[0]=elf as i64; av[1]=inp as i64; av[2]=0
79 let ev: *i64 = sys_mmap(16) as *i64; ev[0]=0
80 sys_execve(elf, av, ev); sys_exit(127)
81 }
82 sys_close(ofd)
83 let st: *i64 = sys_mmap(16) as *i64; sys_wait4(pid, st, 0)
84 return 0
85}
86
87func main() -> i64 {
88 let ctr: *i64 = gv_ctr()
89 gv_head("nx_mp4_faststart_gate -- moov relocated to front + stco/co64 offset +moov_size, byte-exact, on the built remuxer" as *u8)
90 let CHUNK: i64 = 24 // original stco offset (mdat data starts at 24)
91 let MOOVSZ: i64 = 60
92 let EXPECT: i64 = CHUNK + MOOVSZ // 84
93
94 let inb: *u8 = sys_mmap(256); let n: i64 = g_build(inb, CHUNK)
95 let wf: i64 = sys_openat_wr(G_TEST, 0x1a4); if wf>=0 { sys_write(wf, inb, n); sys_close(wf) }
96
97 let rc: i64 = g_run_elf(G_TEST, G_OUT)
98 var t0: i64=0; if rc==0 { t0=1 }
99 // the subject must exist to be judged: an unbuilt remuxer is SKIP (no evidence), never RED
100 if gv_need("T0 the built nx_mp4_faststart.sov.elf forks (nx_sov_build_run nx_mp4_faststart)" as *u8, t0, ctr) == 0 {
101 return gv_verdict("nx_mp4_faststart_gate" as *u8, ctr, "subject absent: the remuxer elf did not fork" as *u8)
102 }
103
104 let ob: *u8 = sys_mmap(4096); let ofd: i64 = sys_openat_rd(G_OUT); var on: i64=0
105 if ofd>=0 { on = sys_read(ofd, ob, 4096); sys_close(ofd) }
106
107 // T1: same total size (lossless)
108 var t1: i64=0; if on==100 { t1=1 }
109 g_ck("T1-output-size-equals-input-size-lossless-100-bytes" as *u8, t1, ctr)
110
111 // T2: first box is ftyp
112 var t2: i64=0; if on>=8 { if g_teq(ob,4,"ftyp" as *u8)==1 { t2=1 } }
113 g_ck("T2-first-box-is-ftyp" as *u8, t2, ctr)
114
115 // T3: box after ftyp (offset 16) is moov -- MOVED TO FRONT
116 var t3: i64=0; if on>=20 { if g_teq(ob,16+4,"moov" as *u8)==1 { t3=1 } }
117 g_ck("T3-moov-relocated-to-the-front-right-after-ftyp" as *u8, t3, ctr)
118
119 // T4: box after moov (offset 16+60=76) is mdat
120 var t4: i64=0; if on>=84 { if g_teq(ob,76+4,"mdat" as *u8)==1 { t4=1 } }
121 g_ck("T4-mdat-now-follows-moov" as *u8, t4, ctr)
122
123 // T5: the stco entry inside the relocated moov = CHUNK + MOOVSZ (offset rewritten). stco entry is at
124 // 16(moov start)+56(offset of the entry within moov) = 72.
125 var got: i64=0-1; if on>=76 { got = g_be32(ob, 16+56) }
126 var t5: i64=0; if got==EXPECT { t5=1 }
127 g_ck("T5-stco-chunk-offset-rewritten-to-CHUNK-plus-moov_size-24-plus-60-is-84" as *u8, t5, ctr)
128
129 // T6 NEG-CONTROL: a WRONG shift (0) would leave the offset at 24 -> the check MUST reject it.
130 var t6: i64=0; if EXPECT != CHUNK { t6=1 }
131 g_ck("neg-control-T6-a-zero-shift-offset-staying-24-is-not-accepted-so-T5-tests-the-shift" as *u8, t6, ctr)
132
133 // T7 ★SCALE: the co64 (64-bit offset) path -- EVERY mp4 >4GB uses it (KNAM-064 = 6.3GB). Build a co64 mp4
134 // with a >4GB chunk offset, remux, assert the 64-bit offset is rewritten by +moov_size(64).
135 let CO_CHUNK: i64 = 4294967320 // 0x100000018 = 4GiB + 24 (exercises the high 32 bits)
136 let CO_MOOVSZ: i64 = 64
137 let inb2: *u8 = sys_mmap(256); let n2: i64 = g_build_co64(inb2, CO_CHUNK)
138 let wf2: i64 = sys_openat_wr(G_TEST, 0x1a4); if wf2>=0 { sys_write(wf2, inb2, n2); sys_close(wf2) }
139 g_run_elf(G_TEST, G_OUT)
140 let ob2: *u8 = sys_mmap(4096); let of2: i64 = sys_openat_rd(G_OUT); var on2: i64=0; if of2>=0 { on2 = sys_read(of2, ob2, 4096); sys_close(of2) }
141 var co_ok: i64=0
142 if on2 >= 80 { if g_teq(ob2,16+4,"moov" as *u8)==1 { let cgot: i64 = g_be64(ob2, 16+56); if cgot == CO_CHUNK + CO_MOOVSZ { co_ok=1 } } }
143 g_ck("T7-SCALE-co64-64-bit-chunk-offset-rewritten-by-moov_size-the-over-4GB-mp4-class" as *u8, co_ok, ctr)
144
145 // the domain log keeps its GREEN line (its own artifact, written only on an all-pass); the verdict is the base class's
146 if ctr[0] == ctr[1] {
147 let lg: i64 = sys_openat_append(G_LOG, 420); if lg>=0 { let ls: *u8="NX-MP4-FASTSTART verdict=GREEN moov->front + stco/co64 +moov_size (lossless, browser-streamable)\n" as *u8; var ln: i64=0; while ls[ln]!=(0 as u8){ln=ln+1} sys_write(lg,ls,ln); sys_close(lg) }
148 }
149 return gv_verdict("nx_mp4_faststart_gate" as *u8, ctr, "the remuxer moves moov to the front and rewrites stco and co64 offsets by exactly moov_size" as *u8)
150}