code wiki / _hdl_build / nx_ale_aggregate_tamper_stub.nx

nx_ale_aggregate_tamper_stub.nx source

↩ module page · 147 lines · 4683 B

1// nx_ale_aggregate_tamper_stub.nx -- the TAMPER fixture for ALE-R1c: a BROKEN aggregator. 2// It DROPS a task from the denominator (divides the honest sum by N-1 instead of N) AND 3// SKIPS the final clamp, so on a fixture whose honest mean is in range it emits an INFLATED, 4// OUT-OF-[0,1000] mean (overshoot_set.txt: honest 2100/3=700 -> tamper 2100/2=1050). The 5// diff-lane gate _ale_aggregate_gate runs THIS and REJECTS the out-of-range / wrong aggregate, 6// proving the real organ's BOUNDED + MEAN-EXACT contracts are enforced, not assumed. This is 7// the negative control. NEVER on the runtime path; fixture only. Lives in runtime/_hdl_build/ 8// so `import "nx_syscalls.nx"` resolves (the import walker reaches runtime/). Same I/O surface 9// as nx_ale_aggregate: argv[1]=fixture, argv[2]=scratch out (agg| line + breakdown). 10// license_tier: ORIGINAL 11import "nx_syscalls.nx" 12const K_MAGIC_262144: i64 = 262144 13 14func ts_read(path: *u8, buf: *u8, cap: i64) -> i64 { 15 let fd: i64 = sys_openat_rd(path) 16 if fd < 0 { return 0 } 17 var n: i64 = 0 18 var go: i64 = 1 19 while go == 1 { 20 let r: i64 = sys_read(fd, (buf as i64 + n) as *u8, cap - 1 - n) 21 if r <= 0 { go = 0 } else { n = n + r } 22 if n >= cap - 1 { go = 0 } 23 } 24 sys_close(fd) 25 return n 26} 27 28func ts_len(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 29 30func ts_is_bol(buf: *u8, pos: i64) -> i64 { 31 if pos == 0 { return 1 } 32 if buf[pos - 1] == (10 as u8) { return 1 } 33 return 0 34} 35 36func ts_line_end(buf: *u8, n: i64, start: i64) -> i64 { 37 var e: i64 = start 38 while e < n { 39 if buf[e] == (10 as u8) { return e } 40 e = e + 1 41 } 42 return n 43} 44 45func ts_pfx_eq(pfx: *u8, plen: i64, buf: *u8, pos: i64) -> i64 { 46 var k: i64 = 0 47 while k < plen { 48 if pfx[k] != buf[pos + k] { return 0 } 49 k = k + 1 50 } 51 return 1 52} 53 54func ts_milli_start(buf: *u8, start: i64, end: i64) -> i64 { 55 var bars: i64 = 0 56 var j: i64 = start 57 while j < end { 58 if buf[j] == (124 as u8) { bars = bars + 1; if bars == 2 { return j + 1 } } 59 j = j + 1 60 } 61 return 0 - 1 62} 63 64func ts_parse_int(buf: *u8, from: i64, end: i64) -> i64 { 65 var seen: i64 = 0 66 var v: i64 = 0 67 var j: i64 = from 68 while j < end { 69 let c: i64 = buf[j] as i64 70 if c >= 48 { if c <= 57 { v = (v * 10) + (c - 48); seen = 1 } else { j = end } } else { if seen == 1 { j = end } } 71 j = j + 1 72 } 73 if seen == 0 { return 0 - 1 } 74 return v 75} 76 77func ts_clamp(v: i64) -> i64 { 78 var c: i64 = v 79 if c < 0 { c = 0 } 80 if c > 1000 { c = 1000 } 81 return c 82} 83 84func ts_wd(fd: i64, v: i64) -> i64 { 85 let bb: *u8 = sys_mmap(28) 86 var m: i64 = v 87 if m < 0 { m = 0 - m } 88 let t: *u8 = sys_mmap(28) 89 var k: i64 = 0 90 if m == 0 { t[0] = 48; k = 1 } 91 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 92 var i: i64 = 0 93 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 94 sys_write(fd, bb, k) 95 return 0 96} 97 98func main(argc: i64, argv: *i64) -> i64 { 99 if argc < 3 { sys_exit(2); return 2 } 100 let fixp: *u8 = argv[1] as *u8 101 let outp: *u8 = argv[2] as *u8 102 103 let fbuf: *u8 = sys_mmap(K_MAGIC_262144) 104 let fn: i64 = ts_read(fixp, fbuf, K_MAGIC_262144) 105 let pfx: *u8 = "task|" as *u8 106 let pl: i64 = ts_len(pfx) 107 108 var total: i64 = 0 109 var sum: i64 = 0 110 var i: i64 = 0 111 while i < fn { 112 if ts_is_bol(fbuf, i) == 1 { 113 var istask: i64 = 0 114 if i + pl <= fn { 115 if ts_pfx_eq(pfx, pl, fbuf, i) == 1 { istask = 1 } 116 } 117 if istask == 1 { 118 let lineend: i64 = ts_line_end(fbuf, fn, i) 119 let ms: i64 = ts_milli_start(fbuf, i, lineend) 120 if ms >= 0 { 121 let raw: i64 = ts_parse_int(fbuf, ms, lineend) 122 if raw >= 0 { total = total + 1; sum = sum + ts_clamp(raw) } 123 } 124 i = lineend 125 } 126 } 127 i = i + 1 128 } 129 130 // TAMPER by construction: DROP a task from the denominator (N-1) and SKIP the clamp. 131 // On overshoot_set.txt: honest 2100/3=700 -> tampered 2100/2=1050 (escapes [0,1000]). 132 var denom: i64 = total - 1 133 if denom < 1 { denom = 1 } 134 var mean: i64 = 0 135 if total > 0 { mean = sum / denom } 136 // NO CLAMP: the wrong/out-of-range mean is emitted as-is (that is the defect the gate bites) 137 let agg: i64 = mean 138 139 let out: i64 = sys_openat_wr(outp, 0x1a4) 140 if out < 0 { sys_exit(3); return 3 } 141 sys_write(out, "agg|" as *u8, 4) 142 ts_wd(out, agg) 143 sys_write(out, "\n" as *u8, 1) 144 sys_close(out) 145 sys_exit(0) 146 return 0 147}