code wiki / (root) / nx_wat_data_artifact_gate.nx

nx_wat_data_artifact_gate.nx source

↩ module page · 95 lines · 6290 B

1// Actual assembler output -> existing native WASM VM. Private fixtures only. 2// The counted data suffix is a fixed fixture oracle, not a second WASM parser. 3import "nx_wat_refuse_gate.nx" 4import "nx_wasm_vm.nx" 5import "nx_gate_verdict.nx" 6const WD_PAGE_BYTES: i64=65536 7const WD_PRIVATE: i64=448 8const WD_DATA_BYTES: i64=12 9func wd_run(subject: *u8, input: *u8, output: *u8, log: *u8, count: *i64) -> i64 { 10 let av: *i64=sys_mmap(WR_ARGV_SLOTS*WR_SPAN) as *i64 11 if (av as i64) <= 0 { count[0]=0; return 0-1 } 12 av[0]=subject as i64; av[1]=input as i64; av[2]=output as i64; av[3]=0 13 let rc: i64=tr_run_capture(subject,av,log,WR_CAP-1,count) 14 if count[0] >= 0 && count[0] < WR_CAP { log[count[0]]=0 as u8 } 15 sys_munmap(av as *u8,WR_ARGV_SLOTS*WR_SPAN) 16 return rc 17} 18func wd_artifact(path: *u8, out: *u8, cap: i64) -> i64 { 19 let fd: i64=sys_openat_rd(path) 20 if fd < 0 { return 0-1 } 21 var n: i64=0; var eof: i64=0 22 while n < cap { 23 let r: i64=sys_read(fd,((out as i64)+n) as *u8,cap-n) 24 if r == 0 { eof=1; break }; if r < 0 { break }; n=n+r 25 } 26 sys_close(fd) 27 if eof != 1 { return 0-1 }; return n 28} 29func wd_suffix(bytes: *u8, n: i64) -> i64 { 30 if n < WD_DATA_BYTES { return 0 } 31 let tail: *u8=sys_mmap(WD_DATA_BYTES) 32 if (tail as i64) <= 0 { return 0 } 33 // section11, payload10, one active segment, offset i32.const16, four exact payload bytes. 34 tail[0]=11 as u8; tail[1]=10 as u8; tail[2]=1 as u8; tail[3]=0 as u8 35 tail[4]=65 as u8; tail[5]=16 as u8; tail[6]=11 as u8; tail[7]=4 as u8 36 tail[8]=0 as u8; tail[9]=127 as u8; tail[10]=128 as u8; tail[11]=255 as u8 37 var i: i64=0; var same: i64=1 38 while i < WD_DATA_BYTES { if bytes[n-WD_DATA_BYTES+i] != tail[i] { same=0 }; i=i+1 } 39 sys_munmap(tail,WD_DATA_BYTES); return same 40} 41func main(argc: i64, argv: **u8) -> i64 { 42 if argc != 3 { gv_puts("usage: wat-data-artifact-gate <subject-assembler> /tmp/fresh-fixture-root\n" as *u8); return 3 } 43 let subject: *u8=argv[1] as *u8; let dir: *u8=argv[2] as *u8 44 let dn: i64=gk_len(dir) 45 if dn <= 5 || dn >= WR_PATH/2 { return 3 } 46 if dir[5] == (46 as u8) { return 3 } 47 if dir[0] != (47 as u8) || dir[1] != (116 as u8) || dir[2] != (109 as u8) || dir[3] != (112 as u8) || dir[4] != (47 as u8) { return 3 } 48 var i: i64=5 49 while i < dn { if dir[i] == (47 as u8) || dir[i] <= (32 as u8) { return 3 }; i=i+1 } 50 let ctr: *i64=gv_ctr(); gv_head("WAT-DATA-ARTIFACT / subject assembler and actual VM" as *u8) 51 gv_need("subject assembler present" as *u8,gk_exists(subject),ctr) 52 let made: i64=sys_mkdir(dir,WD_PRIVATE) 53 gv_check("exclusive private fixture root established" as *u8,made == 0,ctr) 54 if made != 0 { return gv_verdict("WAT-DATA-ARTIFACT" as *u8,ctr,"fixture isolation refused" as *u8) } 55 let input: *u8=sys_mmap(WR_PATH); let output: *u8=sys_mmap(WR_PATH); let badout: *u8=sys_mmap(WR_PATH) 56 let log: *u8=sys_mmap(WR_CAP); let bytes: *u8=sys_mmap(WR_CAP); let count: *i64=sys_mmap(WR_SPAN) as *i64 57 if (input as i64) <= 0 || (output as i64) <= 0 || (badout as i64) <= 0 || (log as i64) <= 0 || (bytes as i64) <= 0 || (count as i64) <= 0 { gv_check("fixture buffers allocated" as *u8,0,ctr); return gv_verdict("WAT-DATA-ARTIFACT" as *u8,ctr,"allocation refused" as *u8) } 58 gk_join(input,dir,"input.wat" as *u8); gk_join(output,dir,"valid.wasm" as *u8); gk_join(badout,dir,"invalid.wasm" as *u8) 59 let valid: *u8="(module\n (memory (export \"memory\") 1)\n (data (i32.const 16) \"\\00\\7f\\80\\ff\")\n (func (export \"after_data\") (result i64) i64.const 42)\n)\n" as *u8 60 let written: i64=gk_write(input,valid) 61 gv_check("entire valid data-before-function fixture written" as *u8,written == gk_len(valid),ctr) 62 if written != gk_len(valid) { return gv_verdict("WAT-DATA-ARTIFACT" as *u8,ctr,"fixture write failed" as *u8) } 63 let rc: i64=wd_run(subject,input,output,log,count) 64 gv_check("subject assembler exits zero for valid input" as *u8,rc == 0,ctr) 65 gv_check("valid compiler log below capture bound" as *u8,count[0] >= 0 && count[0] < WR_CAP-1,ctr) 66 let n: i64=wd_artifact(output,bytes,WR_CAP) 67 gv_check("actual output read through EOF within artifact bound" as *u8,n > 8,ctr) 68 gv_check("actual output retains exact active data section and all four byte values" as *u8,wd_suffix(bytes,n),ctr) 69 if n > 8 { 70 let mod: *WasmMod=wm_new(bytes,n) 71 if (mod as i64) > 0 { 72 let parsed: i64=wm_parse(mod) 73 gv_check("actual output parses in existing native WASM VM" as *u8,parsed == 0,ctr) 74 if parsed == 0 { 75 gv_check("declared one-page memory extent retained" as *u8,mod.mem_bytes == WD_PAGE_BYTES,ctr) 76 let found: i64=wm_find_export(mod,"after_data" as *u8) 77 gv_check("function after data is present in actual artifact" as *u8,found >= 0,ctr) 78 if found >= 0 { gv_check("actual post-data export executes to exact42" as *u8,wm_run(mod,"after_data" as *u8,0,0,0,0,0,0) == 42,ctr) } 79 } 80 } else { gv_check("VM module allocated" as *u8,0,ctr) } 81 } 82 let invalid: *u8="(module (memory 1) (data (i32.const 16) \"\\0g\") (func (export \"after_data\") (result i64) i64.const 42))\n" as *u8 83 let badwritten: i64=gk_write(input,invalid) 84 gv_check("entire invalid-hex fixture written" as *u8,badwritten == gk_len(invalid),ctr) 85 if badwritten == gk_len(invalid) { 86 let badrc: i64=wd_run(subject,input,badout,log,count) 87 gv_check("invalid capture complete within inherited bound" as *u8,count[0] > 0 && count[0] < WR_CAP-1,ctr) 88 gv_check("invalid data refused by exact named hex cause" as *u8,badrc > 0 && gk_has(log,"WATC DATA: invalid hex digit" as *u8) == 1,ctr) 89 gv_check("invalid data leaves no output artifact" as *u8,gk_exists(badout) == 0,ctr) 90 gv_values_head(); gv_kv("invalid_assembler_exit" as *u8,badrc) 91 } 92 gv_values_head(); gv_kv("actual_wasm_bytes" as *u8,n) 93 gv_puts("retained_fixture=" as *u8); gv_puts(dir); gv_puts("\n" as *u8) 94 return gv_verdict("WAT-DATA-ARTIFACT" as *u8,ctr,"actual artifact retains active data bytes and post-data function executes in native VM; malformed hex refused; no browser or static-memory initialization claim" as *u8) 95}