code wiki / _hdl_build / nx_stage_emitter_note_gate_t188.nx

nx_stage_emitter_note_gate_t188.nx source

↩ module page · 72 lines · 4264 B

1// nx_stage_emitter_note_gate_t188.nx -- Validates and processes a JSON receipt for stage emission, ensuring all required fields and note are present. 2import "nx_deploy_stage_lib.nx" 3import "nx_json.nx" 4import "nx_signal.nx" 5import "nx_gate_verdict.nx" 6func eg_token_eq(t:*NxJsonTok,s:*u8)->i64{ 7 let n:i64=ds_len(s);if t.tok_len!=n{return 0} 8 var i:i64=0;while i<n{if t.src[t.tok_off+i]!=s[i]{return 0};i=i+1};return 1 9} 10// Parse the complete flat receipt grammar, require each legacy field plus note once. 11func eg_contract(t:*NxJsonTok)->i64{ 12 t.pos=0;var mask:i64=0 13 if nx_json_next(t)!=NX_JSON_LBRACE{return 0} 14 while 1==1{ 15 if nx_json_next(t)!=NX_JSON_STRING{return 0} 16 var bit:i64=0;var value_kind:i64=NX_JSON_STRING 17 if eg_token_eq(t,"\"organ\"")==1{bit=1} 18 if eg_token_eq(t,"\"src\"")==1{bit=2} 19 if eg_token_eq(t,"\"dst\"")==1{bit=4} 20 if eg_token_eq(t,"\"rc\"")==1{bit=8;value_kind=NX_JSON_NUMBER} 21 if eg_token_eq(t,"\"bytes\"")==1{bit=16;value_kind=NX_JSON_NUMBER} 22 if eg_token_eq(t,"\"verdict\"")==1{bit=32} 23 if eg_token_eq(t,"\"note\"")==1{bit=64} 24 if bit!=0 && (mask&bit)!=0{return 0};mask=mask|bit 25 if nx_json_next(t)!=NX_JSON_COLON{return 0} 26 let kind:i64=nx_json_next(t) 27 if kind!=NX_JSON_STRING && kind!=NX_JSON_NUMBER && kind!=NX_JSON_TRUE && kind!=NX_JSON_FALSE && kind!=NX_JSON_NULL{return 0} 28 if bit!=0 && kind!=value_kind{return 0} 29 if bit==1 && eg_token_eq(t,"\"nx_deploy_stage\"")!=1{return 0} 30 if bit==8 && eg_token_eq(t,"-9223372036854775808")!=1{return 0} 31 if bit==32 && eg_token_eq(t,"\"VISIBLE_UNCONFIRMED\"")!=1{return 0} 32 if bit==64 && eg_token_eq(t,"\"ELF-magic admission; destination must end .new; staging preserves source permissions and verifies copied bytes; guarded promotion and runtime health are separate.\"")!=1{return 0} 33 let next:i64=nx_json_next(t) 34 if next==NX_JSON_RBRACE{break};if next!=NX_JSON_COMMA{return 0} 35 } 36 return (mask==127 && nx_json_next(t)==NX_JSON_EOF) as i64 37} 38func main()->i64{ 39 let c:*i64=gv_ctr();let r:*NxDeployStageResult=sys_mmap(__size_of(NxDeployStageResult)) as *NxDeployStageResult 40 ds_stage_result_init(r);r.code=0-9223372036854775807-1;r.visible=1;r.stage="test\nquote\"slash\\" 41 let length:i64=131073 42 let src:*u8=sys_mmap(length+1);var i:i64=0;while i<length{src[i]=97 as u8;i=i+1};src[length]=0 as u8 43 src[0]=10 as u8;src[1]=34 as u8;src[2]=92 as u8 44 let path:*u8="knowledge/gates/stage-large-receipt-t188.json" 45 let fd:i64=sys_openat_wr(path,MODE_0644) 46 gv_check("large escaped receipt emitted",ds_emit_result(fd,src,"example.new",r)==0,c) 47 gv_check("receipt fd closed",sys_close(fd)==0,c) 48 let n:*i64=sys_mmap(8) as *i64 49 let bytes:*u8=sys_read_file(path,n) 50 gv_check("receipt not limited to1024 or65536 bytes",*n>length,c) 51 let t:*NxJsonTok=sys_mmap(__size_of(NxJsonTok)) as *NxJsonTok 52 t.src=bytes;t.len=*n;t.pos=0;t.kind=0;t.tok_off=0;t.tok_len=0 53 gv_check("complete receipt grammar and named legacy fields including accurate note",eg_contract(t)==1,c) 54 gv_check("failed receipt fd reports failure",ds_emit_result(0-1,src,"example.new",r)!=0,c) 55 let old:*i64=sys_mmap(16) as *i64;let lim:*i64=sys_mmap(16) as *i64;let status:*i64=sys_mmap(8) as *i64 56 let childfd:i64=sys_openat_wr("knowledge/gates/stage-noalloc-receipt-t188.json",MODE_0644) 57 let pid:i64=sys_fork() 58 if pid==0{ 59 if nx_prlimit(0,RLIMIT_AS,0 as *u8,old as *u8)!=0{sys_exit(2)} 60 lim[0]=0;lim[1]=old[1] 61 if nx_prlimit(0,RLIMIT_AS,lim as *u8,0 as *u8)!=0{sys_exit(3)} 62 if ds_emit_result(childfd,"source","target.new",r)!=0{sys_exit(4)};sys_exit(0) 63 } 64 var waited:i64=0-1;if pid>0{waited=sys_wait4(pid,status,0)};sys_close(childfd) 65 gv_check("receipt emission works under actual zero address-space limit",pid>0&&waited==pid&&wait_status_rc(status[0])==0,c) 66 let killed:i64=sys_fork() 67 if killed==0{if nx_signal_default(15)!=0{sys_exit(77)};nx_signal_raise(15);sys_exit(78)} 68 var killedwait:i64=0-1;if killed>0{killedwait=sys_wait4(killed,status,0)} 69 gv_check("signal-killed owned child cannot pass exit0",killed>0&&killedwait==killed&&wait_term_signal(status[0])==15&&wait_status_rc(status[0])!=0,c) 70 gv_puts("receipt_bytes=");gv_num(*n);gv_puts("\n") 71 return gv_verdict("stage-receipt",c,"Native JSON tokenization and real resource denial; synthetic long string is not a filesystem-path claim") 72}