code wiki / _hdl_build / nx_varfacts_gate.nx

nx_varfacts_gate.nx source

↩ module page · 227 lines · 11705 B

1// nx_varfacts_gate.nx -- PROVE THE .var LANE READS THE MANIFEST, NEVER THE ASSETS, AND REFUSES 2// RATHER THAN UNDERCOUNTS. The fixture ZIP is hand-built from the published PKZIP layout (local 3// headers + central directory + EOCD), not by our writer -- cross-implementation discipline. 4// license_tier: ORIGINAL No hw writes (Rule 26). 5import "nx_syscalls.nx" 6import "nx_gate_verdict.nx" 7import "nx_varfacts_lib.nx" 8 9func zg_u16(b: *u8, at: i64, v: i64) -> i64 { b[at] = (v & 0xff) as u8; b[at+1] = ((v >> 8) & 0xff) as u8; return at + 2 } 10func zg_u32(b: *u8, at: i64, v: i64) -> i64 { b[at]=(v&0xff) as u8; b[at+1]=((v>>8)&0xff) as u8; b[at+2]=((v>>16)&0xff) as u8; b[at+3]=((v>>24)&0xff) as u8; return at + 4 } 11func zg_s(b: *u8, at: i64, s: *u8) -> i64 { var i: i64 = 0; var p: i64 = at; while s[i] != (0 as u8) { b[p] = s[i]; p = p + 1; i = i + 1 } return p } 12func zg_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 13 14// one local header + body; returns new offset. Records the entry's lho into lhos[idx]. 15func zg_local(b: *u8, at: i64, name: *u8, body: *u8, blen: i64, method: i64, usize: i64, lhos: *i64, idx: i64) -> i64 { 16 lhos[idx] = at 17 var p: i64 = at 18 p = zg_u32(b, p, 0x04034b50) 19 p = zg_u16(b, p, 20) 20 p = zg_u16(b, p, 0) 21 p = zg_u16(b, p, method) 22 p = zg_u16(b, p, 0) 23 p = zg_u16(b, p, 0) 24 p = zg_u32(b, p, 0) 25 p = zg_u32(b, p, blen) 26 p = zg_u32(b, p, usize) 27 p = zg_u16(b, p, zg_len(name)) 28 p = zg_u16(b, p, 0) 29 p = zg_s(b, p, name) 30 var i: i64 = 0 31 while i < blen { b[p] = body[i]; p = p + 1; i = i + 1 } 32 return p 33} 34// one central-directory entry; returns new offset 35func zg_cd(b: *u8, at: i64, name: *u8, blen: i64, method: i64, usize: i64, lho: i64) -> i64 { 36 var p: i64 = at 37 p = zg_u32(b, p, 0x02014b50) 38 p = zg_u16(b, p, 20) 39 p = zg_u16(b, p, 20) 40 p = zg_u16(b, p, 0) 41 p = zg_u16(b, p, method) 42 p = zg_u16(b, p, 0) 43 p = zg_u16(b, p, 0) 44 p = zg_u32(b, p, 0) 45 p = zg_u32(b, p, blen) 46 p = zg_u32(b, p, usize) 47 p = zg_u16(b, p, zg_len(name)) 48 p = zg_u16(b, p, 0) 49 p = zg_u16(b, p, 0) 50 p = zg_u16(b, p, 0) 51 p = zg_u16(b, p, 0) 52 p = zg_u32(b, p, 0) 53 p = zg_u32(b, p, lho) 54 p = zg_s(b, p, name) 55 return p 56} 57 58func main(argc: i64, argv: *i64) -> i64 { 59 let ctr: *i64 = gv_ctr() 60 gv_head("nx_varfacts_gate -- the manifest is read, the assets are not, refusal beats undercount" as *u8) 61 62 let b: *u8 = sys_mmap(16384) 63 let lhos: *i64 = sys_mmap(16 * 8) as *i64 64 let facts: *i64 = sys_mmap(VF_N_SLOTS * 8) as *i64 65 66 // ---- fixture: 8 entries covering every class; meta.json STORED ---- 67 let meta: *u8 = "{\"licenseType\":\"CC BY\",\"contentList\":[]}" as *u8 68 let ml: i64 = zg_len(meta) 69 let tiny: *u8 = "{}" as *u8 70 var p: i64 = 0 71 p = zg_local(b, p, "meta.json" as *u8, meta, ml, 0, ml, lhos, 0) 72 p = zg_local(b, p, "Custom/Atom/Person/Morphs/female/a.vmi" as *u8, tiny, 2, 0, 2, lhos, 1) 73 p = zg_local(b, p, "Saves/scene/s.json" as *u8, tiny, 2, 0, 2, lhos, 2) 74 p = zg_local(b, p, "Custom/Skin/t.jpg" as *u8, tiny, 2, 0, 2, lhos, 3) 75 p = zg_local(b, p, "Custom/Clothing/x/i.vam" as *u8, tiny, 2, 0, 2, lhos, 4) 76 p = zg_local(b, p, "Custom/Hair/h/z.vam" as *u8, tiny, 2, 0, 2, lhos, 5) 77 p = zg_local(b, p, "ADD.cs" as *u8, tiny, 2, 0, 2, lhos, 6) 78 p = zg_local(b, p, "look.vap" as *u8, tiny, 2, 0, 2, lhos, 7) 79 let cd0: i64 = p 80 p = zg_cd(b, p, "meta.json" as *u8, ml, 0, ml, lhos[0]) 81 p = zg_cd(b, p, "Custom/Atom/Person/Morphs/female/a.vmi" as *u8, 2, 0, 2, lhos[1]) 82 p = zg_cd(b, p, "Saves/scene/s.json" as *u8, 2, 0, 2, lhos[2]) 83 p = zg_cd(b, p, "Custom/Skin/t.jpg" as *u8, 2, 0, 2, lhos[3]) 84 p = zg_cd(b, p, "Custom/Clothing/x/i.vam" as *u8, 2, 0, 2, lhos[4]) 85 p = zg_cd(b, p, "Custom/Hair/h/z.vam" as *u8, 2, 0, 2, lhos[5]) 86 p = zg_cd(b, p, "ADD.cs" as *u8, 2, 0, 2, lhos[6]) 87 p = zg_cd(b, p, "look.vap" as *u8, 2, 0, 2, lhos[7]) 88 let cdsz: i64 = p - cd0 89 p = zg_u32(b, p, 0x06054b50) 90 p = zg_u16(b, p, 0) 91 p = zg_u16(b, p, 0) 92 p = zg_u16(b, p, 8) 93 p = zg_u16(b, p, 8) 94 p = zg_u32(b, p, cdsz) 95 p = zg_u32(b, p, cd0) 96 p = zg_u16(b, p, 0) 97 let total: i64 = p 98 99 // T1 full census from the central directory 100 vf_probe(b, total, total, facts) 101 var t1: i64 = 0 102 if facts[0] == 1 { if facts[1] == 8 { if facts[2] == 1 { if facts[3] == 1 { if facts[4] == 1 { if facts[5] == 1 { if facts[6] == 1 { if facts[7] == 1 { if facts[8] == 1 { if facts[9] == 1 { t1 = 1 } } } } } } } } } } 103 gv_check("T1 MANIFEST CENSUS: 8 entries classify into morphs/scenes/textures/clothing/hair/plugins/appearance and meta.json is located -- all from central-directory NAMES, no member opened" as *u8, t1, ctr) 104 105 // T2 the declared license reads from a STORED meta.json body 106 let lic: *u8 = sys_mmap(64) 107 let mbody: *u8 = ((b as i64) + lhos[0] + 30 + 9) as *u8 108 let r2: i64 = vf_license(mbody, ml, 0, ml, lic) 109 var t2: i64 = 0 110 if r2 > 0 { if lic[0] == 67 { if lic[1] == 67 { if lic[2] == 32 { if lic[3] == 66 { if lic[4] == 89 { t2 = 1 } } } } } } 111 gv_check("T2 DECLARED LICENSE READ (stored): licenseType CC BY extracted from the package's own meta.json" as *u8, t2, ctr) 112 113 // T3 method-8 meta.json: a raw-DEFLATE stored-block wrap of the same body decodes through the 114 // INCUMBENT nx_deflate_inflate and yields the same label 115 let dz: *u8 = sys_mmap(256) 116 dz[0] = 1 as u8 117 zg_u16(dz, 1, ml) 118 zg_u16(dz, 3, 65535 - ml) 119 var di: i64 = 0 120 while di < ml { dz[5+di] = meta[di]; di = di + 1 } 121 let r3: i64 = vf_license(dz, 5 + ml, 8, ml, lic) 122 var t3: i64 = 0 123 if r3 > 0 { if lic[0] == 67 { if lic[4] == 89 { t3 = 1 } } } 124 gv_check("T3 DEFLATED meta.json DECODES: ZIP method 8 rides the incumbent raw-DEFLATE core (nx_deflate_inflate) and yields the same CC BY" as *u8, t3, ctr) 125 126 // T4 garbage refuses: no EOCD -> ok=0 and no counts 127 let g: *u8 = sys_mmap(256) 128 var gi: i64 = 0 129 while gi < 200 { g[gi] = 0x41 as u8; gi = gi + 1 } 130 vf_probe(g, 200, 200, facts) 131 var t4: i64 = 0 132 if facts[0] == 0 { if facts[1] == 0 - 1 { t4 = 1 } } 133 gv_check("T4 NOT-A-ZIP REFUSES: no EOCD yields ok=0 and every count stays -1" as *u8, t4, ctr) 134 135 // T5 window honesty: pretend the file is 1000 bytes longer than the window -> the CD's absolute 136 // offset lands before the window start -> REFUSE, never a partial census 137 vf_probe(b, total, total + 1000, facts) 138 var t5: i64 = 0 139 if facts[0] == 0 { t5 = 1 } 140 gv_check("T5 A PARTIAL CENSUS IS REFUSED: when the central directory starts before the tail window the probe returns ok=0 rather than an undercount" as *u8, t5, ctr) 141 142 // T6 tail semantics: hand the probe only the LAST total-50 bytes (as a real tail read would) 143 // and the census still resolves through absolute-offset arithmetic 144 let tailp: *u8 = ((b as i64) + 50) as *u8 145 vf_probe(tailp, total - 50, total, facts) 146 var t6: i64 = 0 147 if facts[0] == 1 { if facts[1] == 8 { if facts[2] == 1 { t6 = 1 } } } 148 gv_check("T6 TAIL-WINDOW CONTRACT: the census is identical when only the archive tail is in memory -- a 519MB look pack measures from its last few MB" as *u8, t6, ctr) 149 150 // T7 ZIP64 EXTRA RESOLUTION -- MEASURED 2026-09-11 on real VaM packages (101,040 members in 1,633 of 2,179 readable 151 // packages): a writer marks the CD sizes 0xFFFFFFFF and carries the true values in extra 0x0001 under a plain 32-bit EOCD. 152 let z: *u8 = sys_mmap(4096) 153 let zl: *i64 = sys_mmap(16 * 8) as *i64 154 var zp: i64 = 0 155 zp = zg_local(z, zp, "meta.json" as *u8, meta, ml, 0, ml, zl, 0) 156 let zcd0: i64 = zp 157 zp = zg_u32(z, zp, 0x02014b50) 158 zp = zg_u16(z, zp, 45) 159 zp = zg_u16(z, zp, 45) 160 zp = zg_u16(z, zp, 0) 161 zp = zg_u16(z, zp, 0) 162 zp = zg_u16(z, zp, 0) 163 zp = zg_u16(z, zp, 0) 164 zp = zg_u32(z, zp, 0) 165 zp = zg_u32(z, zp, 0xffffffff) 166 zp = zg_u32(z, zp, 0xffffffff) 167 zp = zg_u16(z, zp, 9) 168 zp = zg_u16(z, zp, 20) 169 zp = zg_u16(z, zp, 0) 170 zp = zg_u16(z, zp, 0) 171 zp = zg_u16(z, zp, 0) 172 zp = zg_u32(z, zp, 0) 173 zp = zg_u32(z, zp, zl[0]) 174 zp = zg_s(z, zp, "meta.json" as *u8) 175 let zxid: i64 = zp 176 zp = zg_u16(z, zp, 1) 177 zp = zg_u16(z, zp, 16) 178 zp = zg_u32(z, zp, ml) 179 zp = zg_u32(z, zp, 0) 180 zp = zg_u32(z, zp, ml) 181 zp = zg_u32(z, zp, 0) 182 let zcdsz: i64 = zp - zcd0 183 zp = zg_u32(z, zp, 0x06054b50) 184 zp = zg_u16(z, zp, 0) 185 zp = zg_u16(z, zp, 0) 186 zp = zg_u16(z, zp, 1) 187 zp = zg_u16(z, zp, 1) 188 zp = zg_u32(z, zp, zcdsz) 189 zp = zg_u32(z, zp, zcd0) 190 zp = zg_u16(z, zp, 0) 191 let ztotal: i64 = zp 192 let zbox: *i64 = sys_mmap(VF_L_N * 8) as *i64 193 let zrec: *i64 = sys_mmap(VF_E_N * 8) as *i64 194 gv_check_eq("T7 ZIP64-EXTRA ARCHIVE LOCATES: a plain 32-bit EOCD over overflowed CD sizes is still an OK archive" as *u8, vf_cd_locate(z, ztotal, ztotal, zbox), VF_LOC_OK, ctr) 195 vf_cd_entry(z, ztotal, zbox[VF_L_CD], zrec) 196 gv_check_eq("T7a usize RESOLVED from extra 0x0001, not read as 4294967295" as *u8, zrec[VF_E_USIZE], ml, ctr) 197 gv_check_eq("T7b csize RESOLVED from extra 0x0001" as *u8, zrec[VF_E_CSIZE], ml, ctr) 198 gv_check_eq("T7c the record names its state RESOLVED" as *u8, zrec[VF_E_Z64], VF_Z64_RESOLVED, ctr) 199 vf_probe(z, ztotal, ztotal, facts) 200 gv_check_eq("T7d the legacy probe now carries the REAL meta.json csize, so its licence read can succeed" as *u8, facts[12], ml, ctr) 201 z[zxid] = 2 as u8 202 vf_cd_entry(z, ztotal, zbox[VF_L_CD], zrec) 203 gv_check_eq("neg-control-T7e an overflowed size with no 0x0001 record reads UNRESOLVED, never a guessed size" as *u8, zrec[VF_E_Z64], VF_Z64_UNRESOLVED, ctr) 204 gv_check_eq("neg-control-T7f and the unresolved usize stays the overflow marker for the caller to refuse by name" as *u8, zrec[VF_E_USIZE], VF_U32_ALL, ctr) 205 206 // T8 DEPTH-AWARE meta.json: a nested dependency's licenseType PRECEDES the package's own in the bytes 207 let jj: *u8 = "{\"dependencies\":{\"X.Y.1\":{\"licenseType\":\"PC\",\"dependencies\":{}}},\"licenseType\":\"CC BY\"}" as *u8 208 let jn: i64 = zg_len(jj) 209 let jlen: *i64 = sys_mmap(16) as *i64 210 let jbox: *i64 = sys_mmap(VF_JM_N * 8) as *i64 211 let jtop: *u8 = vf_json_get_str(jj, jn, 0, "licenseType" as *u8, jlen) 212 var t8: i64 = 0 213 if (jtop as i64) != 0 { if jlen[0] == 5 { if jtop[0] == 67 { if jtop[3] == 66 { if jtop[4] == 89 { t8 = 1 } } } } } 214 gv_check("T8 DEPTH-AWARE licenseType: the package's DIRECT member answers CC BY, not the nested PC that precedes it" as *u8, t8, ctr) 215 let jleg: *u8 = sys_mmap(64) 216 gv_check_eq("T8a the legacy first-occurrence reader returns the nested label (length 2) -- the flaw the census checks for on every package (census run 1 measured 0 disagreements over 2,177 parsed metas)" as *u8, vf_license(jj, jn, 0, jn, jleg), 2, ctr) 217 gv_check_eq("T8b one DIRECT dependency member, its nested dependencies not counted" as *u8, vf_json_count_members(jj, jn, vf_json_find(jj, jn, 0, "dependencies" as *u8, jbox)), 1, ctr) 218 219 // T9 THE WINDOW IS NAMED AND DERIVED: a CD before the window is WINDOW, and the window to read is the archive's own cd_abs 220 let wbox: *i64 = sys_mmap(VF_L_N * 8) as *i64 221 gv_check_eq("T9 a central directory before the window reads WINDOW, not a generic refusal" as *u8, vf_cd_locate(b, total, total + 1000, wbox), VF_LOC_WINDOW, ctr) 222 gv_check_eq("T9a the caller's window is derived from cd_abs in the EOCD" as *u8, wbox[VF_L_CDABS], cd0, ctr) 223 224 let rc: i64 = gv_verdict("VARFACTS-GATE", ctr, "hand-built PKZIP fixture, manifest-only reads, refusal over undercount, incumbent deflate" as *u8) 225 sys_exit(rc) 226 return rc 227}