code wiki / _hdl_build / nx_js_fuel_gate.nx
nx_js_fuel_gate.nx source
↩ module page · 41 lines · 2418 B
1// nx_js_fuel_gate.nx -- non-vacuous tooth for the GLOBAL JS execution budget (debt 1785032369).
2// Proves BOTH directions: (T1) a hostile unbounded loop -- the exact class that degraded the crawler to
3// minutes-per-page and that per-loop caps cannot bound compositionally -- ABORTS with an eval error
4// instead of hanging; (T2) a legitimate script still runs to completion under the same budget (the guard
5// must not overreach); (T3) budget RESETS between programs (a spent budget must not poison the next run).
6// Rides nx_gate_verdict (D001). license_tier: ORIGINAL expect_exit: 0
7import "nx_syscalls.nx"
8import "nx_js_eval.nx"
9import "nx_gate_verdict.nx"
10
11func main() -> i64 {
12 gv_head("=== nx_js_fuel_gate: global statement-fuel bounds hostile scripts, spares legit ones ===" as *u8)
13 let ctr: *i64 = gv_ctr()
14 let out: *i64 = sys_mmap(16) as *i64
15 // T1: unbounded nested loop (outer while(true) defeats any per-loop cap) -> MUST error, not hang.
16 let hostile: *u8 = "var i=0; while(true){ var j=0; while(j<100000){ j=j+1 } i=i+1 }" as *u8
17 var hl: i64 = 0
18 while hostile[hl] != (0 as u8) { hl = hl + 1 }
19 let t0: i64 = sys_now_realtime_sec()
20 let rc1: i64 = js_run_source(hostile, hl, out)
21 let t1s: i64 = sys_now_realtime_sec() - t0
22 var t1: i64 = 0
23 if rc1 == 1 { if t1s < 60 { t1 = 1 } }
24 gv_check("T1 hostile while(true) aborts with eval error in bounded time" as *u8, t1, ctr)
25 // T2: legitimate work (10k-iteration loop + function calls) still completes under the budget.
26 let legit: *u8 = "function f(x){return x+1} var s=0; var i=0; while(i<10000){ s=f(s); i=i+1 } s" as *u8
27 var ll: i64 = 0
28 while legit[ll] != (0 as u8) { ll = ll + 1 }
29 let rc2: i64 = js_run_source(legit, ll, out)
30 var t2: i64 = 0
31 if rc2 == 0 { if out[0] == VAL_NUM { if out[1] == 10000 { t2 = 1 } } }
32 gv_check("T2 legit 10k-loop with calls completes, s==10000" as *u8, t2, ctr)
33 // T3: a fresh program after exhaustion gets a fresh budget (per-run reset, no poisoning).
34 let rc3: i64 = js_run_source(legit, ll, out)
35 var t3: i64 = 0
36 if rc3 == 0 { if out[1] == 10000 { t3 = 1 } }
37 gv_check("T3 budget resets per program (run-after-exhaustion still completes)" as *u8, t3, ctr)
38 let rc: i64 = gv_verdict("JS-FUEL-GATE" as *u8, ctr, "global statement budget: hostile bounded, legit spared, reset per run" as *u8)
39 sys_exit(rc)
40 return rc
41}