nx_craft_static_resident_gate.nx source
↩ module page · 157 lines · 10517 B
1// ORIGINAL. Build against the reviewed canonical craft candidate, not its dated name.
2import "nx_syscalls.nx"
3import "nx_gate_verdict.nx"
4import "nx_wasm_craft.nx"
5
6func crg_copy(dst: *u8,src: *u8,n: i64) -> i64 {
7 var i: i64 = 0
8 while i < n { dst[i] = src[i]; i = i+1 }
9 return 0
10}
11func crg_run(path: *u8,ctr: *i64) -> i64 {
12 let size: *i64 = sys_mmap(MVC_WORD) as *i64
13 if (size as i64) <= 0 { return 0-1 }
14 let raw: *u8 = sys_read_file(path,size)
15 if (raw as i64) <= 0 { return 0-1 }
16 let flen: i64 = size[0]
17 if flen%MVC_WORD != 0 { return WCR_E_STAGE }
18 let raw_hash: i64 = nxa_check2(1,raw as *i64,flen/MVC_WORD)
19 if flen > wc_resident_stage_cap() { return WCR_E_STAGE }
20 let h: *i64 = raw as *i64
21 let sk: i64 = nxa_counted_section(raw,flen,nxa_tag4("SKEL" as *u8),NA_SKEL_W)
22 let an: i64 = nxa_section_entry(raw,flen,nxa_tag4("ANIM" as *u8))
23 let po: i64 = nxa_section_entry(raw,flen,nxa_tag4("POSE" as *u8))
24 if sk < 0 { return sk }; if an < 0 { return an }
25 let nj: i64 = h[sk]; let naw: i64 = h[an+2]
26 var npw: i64 = 0
27 if po >= 0 { npw = h[po+2] }
28 if po < 0 { if po != 0-1 { return po } }
29 if nj > NXA_NJ_CAP { return NXA_RC_NJ_CAP }
30 if naw > NXA_ANIM_CAP_W { return NXA_RC_NAW_CAP }
31 if npw > NXA_POSE_CAP_W { return NXA_RC_NPW_CAP }
32 let initial: i64 = sys_mmap(CRAFT_EXT_TOTAL) as i64
33 if initial <= 0 { return 0-1 }
34 gv_check_eq("pre-ingest zero token refuses without optional allocation" as *u8,wc_resident_cast(initial,1,0),WCR_E_TOKEN,ctr)
35 gv_check_eq("static floor export shares source owner" as *u8,wasm_arena_pages(),CRAFT_STATIC_BASE_PAGES,ctr)
36 gv_check_eq("static upper export shares source owner" as *u8,wasm_static_limit_pages(),CRAFT_STATIC_LIMIT_PAGES,ctr)
37 gv_check_eq("static base uses page unit" as *u8,wasm_static_base_off(),wasm_arena_pages()*WASM_PAGE_BYTES,ctr)
38 gv_check_eq("static limit uses page unit" as *u8,wasm_static_limit_off(),wasm_static_limit_pages()*WASM_PAGE_BYTES,ctr)
39 gv_check("base world ends before compiler statics" as *u8,CRAFT_TOTAL <= CRAFT_STATIC_BASE,ctr)
40 gv_check_eq("clip begins exactly after static reservation" as *u8,O_NXA,CRAFT_STATIC_LIMIT,ctr)
41 gv_check("optional resident tail follows statics" as *u8,O_WCM > CRAFT_STATIC_LIMIT,ctr)
42 gv_check_eq("existing dynamic bank follows clip extension" as *u8,O_WCM,CRAFT_EXT_TOTAL,ctr)
43 let static_words: i64 = (CRAFT_STATIC_LIMIT-CRAFT_STATIC_BASE)/MVC_WORD
44 let reserved: *i64 = (initial+CRAFT_STATIC_BASE) as *i64
45 var static_i: i64 = 0
46 while static_i < static_words { reserved[static_i] = static_i; static_i = static_i+1 }
47 let static_hash: i64 = nxa_check2(1,reserved,static_words)
48 crg_copy((initial+O_FB) as *u8,raw,flen)
49 wc_morf_copy((initial+O_NXA_SKEL) as *i64,((raw as i64)+(sk+1)*MVC_WORD) as *i64,nj*NA_SKEL_W)
50 wc_morf_copy((initial+O_NXA_ANIM) as *i64,((raw as i64)+h[an+1]) as *i64,naw)
51 if npw > 0 { wc_morf_copy((initial+O_NXA_POSE) as *i64,((raw as i64)+h[po+1]) as *i64,npw) }
52 let ingested: i64 = wc_nxa_ingest(initial,nj,naw,npw)
53 gv_check_eq("real source clip admitted by existing engine" as *u8,ingested,0,ctr)
54 if ingested < 0 { return ingested }
55 gv_check_eq("actual clip copy and ingestion preserve full static reservation" as *u8,nxa_check2(1,reserved,static_words),static_hash,ctr)
56 gv_check_eq("inactive token refuses without allocated optional bank" as *u8,wc_resident_cast(initial,1,0),WCR_E_TOKEN,ctr)
57 let pages: i64 = wc_resident_pages(initial,flen)
58 gv_check("real staged asset derives positive page request" as *u8,pages > 0,ctr)
59 if pages < 0 { return pages }
60 let m: *NmiAsset = wc_resident_morph(initial,flen)
61 let needed: i64 = wc_morf_storage_bytes(wc_resident_draw(initial,flen),m.tag_words)
62 let exact: i64 = O_WCM+needed
63 gv_check_eq("page request covers exactly rounded dynamic footprint" as *u8,pages,(exact+WASM_PAGE_BYTES-1)/WASM_PAGE_BYTES,ctr)
64 let base: i64 = sys_mmap(exact+MVC_WORD) as i64
65 if base <= 0 { return 0-1 }
66 crg_copy(base as *u8,initial as *u8,CRAFT_EXT_TOTAL)
67 let sentinel: *i64 = (base+exact) as *i64
68 sentinel[0] = 771
69 gv_check_eq("one-byte-short real allocation refuses" as *u8,wc_resident_bind(base,flen,exact-1,1),WCM_RC_CAPACITY,ctr)
70 gv_check_eq("capacity refusal does not publish token" as *u8,wc_resident_current(base,1),0,ctr)
71 gv_check_eq("relocated arena reconstructs views and binds real asset" as *u8,wc_resident_bind(base,flen,exact,1),0,ctr)
72 gv_check_eq("exact allocation tail remains untouched" as *u8,sentinel[0],771,ctr)
73 gv_check_eq("actual resident bind preserves full static reservation" as *u8,nxa_check2(1,(base+CRAFT_STATIC_BASE) as *i64,static_words),static_hash,ctr)
74 gv_check_eq("matching resident token published" as *u8,wc_resident_current(base,1),1,ctr)
75 gv_check_eq("different resident token refuses" as *u8,wc_resident_tag(base,2,0),WCR_E_TOKEN,ctr)
76 let mobs: *i64 = mobp(base)
77 en_init(mobs,MOB_CAP,MOB_NCOMP)
78 gv_check("actual entity spawned" as *u8,en_spawn(mobs) > 0,ctr)
79 gv_check_eq("new bank zero weights retain incumbent float path" as *u8,wc_resident_cast(base,1,0),0,ctr)
80 let d: *WcmDraw = wc_morf_descriptor(base)
81 let a: *MrdAsset = d.asset; let v: *MvcView = a.morph
82 var selected: i64 = 0-1; var c: i64 = 0
83 while c < v.channels {
84 var j: i64 = 0
85 while j < v.face_words*MVC_AXES {
86 if v.delta[c*v.face_words*MVC_AXES+j] != 0 { if selected < 0 { selected = c } }
87 j = j+1
88 }
89 c = c+1
90 }
91 gv_check("real asset contains a nonzero channel" as *u8,selected >= 0,ctr)
92 if selected < 0 { return WCR_E_STAGE }
93 gv_check_eq("actual entity accepts source-unit channel weight" as *u8,wc_resident_weight(base,1,0,selected,v.unit),0,ctr)
94 gv_check("owned real geometry actually moves" as *u8,wc_resident_cast(base,1,0) > 0,ctr)
95 gv_check_eq("actual owner reads prepared position address" as *u8,wc_resident_positions(base,1,0),d.positions as i64,ctr)
96 let ready_before: i64 = d.ready
97 gv_check_eq("same operation retry succeeds without resetting state" as *u8,wc_resident_bind(base,flen,exact,1),0,ctr)
98 gv_check_eq("same operation retry preserves active weight" as *u8,d.weights[selected],v.unit,ctr)
99 gv_check_eq("same operation retry preserves prepared output" as *u8,d.ready,ready_before,ctr)
100 let changed: *MvcView = wc_resident_view(base,flen)
101 let old_delta: i64 = changed.delta[0]
102 changed.delta[0] = old_delta+1
103 let sh: *i64 = (base+O_FB) as *i64
104 // Find the TOC entry directly: mutation intentionally invalidated checksum.
105 var me: i64 = 0-1; var si: i64 = 0
106 while si < sh[2] { if sh[4+si*4] == nxa_tag4("MORF" as *u8) { me = 4+si*4 }; si = si+1 }
107 if me < 0 { return WCR_E_STAGE }
108 sh[me+3] = nxa_check2(1,(base+O_FB+sh[me+1]) as *i64,sh[me+2])
109 sh[3] = nxa_check2(1,(base+O_FB+4*MVC_WORD) as *i64,sh[2]*4)
110 gv_check_eq("changed checksummed MORF remains canonically admitted" as *u8,wc_resident_admit(base,flen),0,ctr)
111 gv_check_eq("same token with changed native operation conflicts" as *u8,wc_resident_bind(base,flen,exact,1),WCR_E_CONFLICT,ctr)
112 gv_check_eq("conflict preserves active weight" as *u8,d.weights[selected],v.unit,ctr)
113 gv_check_eq("conflict preserves prepared output" as *u8,d.ready,ready_before,ctr)
114 crg_copy((base+O_FB) as *u8,raw,flen)
115 // A later framebuffer render cannot change any admitted bank payload.
116 let bank_hash: i64 = nxa_check2(1,(base+O_WCM) as *i64,needed/MVC_WORD)
117 let staged_header: *i64 = (base+O_FB) as *i64
118 staged_header[0] = 0
119 gv_check("malformed overwritten staging refuses rebind" as *u8,wc_resident_bind(base,flen,exact,2) < 0,ctr)
120 gv_check_eq("refused replacement retains old resident" as *u8,wc_resident_current(base,1),1,ctr)
121 gv_check_eq("overwritten staging leaves owned bank identical" as *u8,nxa_check2(1,(base+O_WCM) as *i64,needed/MVC_WORD),bank_hash,ctr)
122 crg_copy((base+O_FB) as *u8,raw,flen)
123 let clip: *i64 = (base+O_NXA_SKEL) as *i64
124 let before: i64 = clip[0]; clip[0] = before+1
125 gv_check_eq("different actual rig bytes refuse binding" as *u8,wc_resident_bind(base,flen,exact,2),WCR_E_CLIP,ctr)
126 clip[0] = before
127 let anim: *i64 = (base+O_NXA_ANIM) as *i64
128 let old_anim: i64 = anim[0]; anim[0] = old_anim+1
129 gv_check_eq("different actual animation bytes refuse binding" as *u8,wc_resident_bind(base,flen,exact,2),WCR_E_CLIP,ctr)
130 anim[0] = old_anim
131 gv_check("fixture has actual POSE payload for identity witness" as *u8,npw > 0,ctr)
132 if npw < 1 { return WCR_E_CLIP }
133 let pose: *i64 = (base+O_NXA_POSE) as *i64
134 let old_pose: i64 = pose[0]; pose[0] = old_pose+1
135 gv_check_eq("different actual pose bytes refuse binding" as *u8,wc_resident_bind(base,flen,exact,2),WCR_E_CLIP,ctr)
136 pose[0] = old_pose
137 gv_check_eq("identity refusals preserve previous optional resident" as *u8,wc_resident_current(base,1),1,ctr)
138 gv_check_eq("restored exact rig allows replacement token" as *u8,wc_resident_bind(base,flen,exact,2),0,ctr)
139 gv_check_eq("old token cannot address replacement" as *u8,wc_resident_cast(base,1,0),WCR_E_TOKEN,ctr)
140 gv_check_eq("replacement clears entity weights" as *u8,wc_resident_cast(base,2,0),0,ctr)
141 gv_check_eq("existing clip reload remains successful" as *u8,wc_nxa_ingest(base,nj,naw,npw),0,ctr)
142 gv_check_eq("every clip reload invalidates optional resident" as *u8,wc_resident_cast(base,2,0),WCR_E_TOKEN,ctr)
143 let final_header: *i64 = nxa_hp(base)
144 gv_check_eq("baseline clip remains loaded" as *u8,final_header[NXA_H_LOADED],1,ctr)
145 gv_check_eq("entire borrowed real asset remains unchanged" as *u8,nxa_check2(1,raw as *i64,flen/MVC_WORD),raw_hash,ctr)
146 gv_check_eq("full resident lifecycle preserves static reservation" as *u8,nxa_check2(1,(base+CRAFT_STATIC_BASE) as *i64,static_words),static_hash,ctr)
147 return 0
148}
149func main(argc: i64,argv: *i64) -> i64 {
150 let ctr: *i64 = gv_ctr()
151 gv_head("resident MORF staging and exact clip identity" as *u8)
152 var rc: i64 = 0-1
153 if argc == 1 { rc = crg_run("/tmp/nxmorfgate/d1.nxa" as *u8,ctr) }
154 if argc == 2 { rc = crg_run(argv[1] as *u8,ctr) }
155 gv_check_eq("real resident loader lifecycle completes" as *u8,rc,0,ctr)
156 return gv_verdict("NX-CRAFT-STATIC-RESIDENT" as *u8,ctr,"native allocation and real asset; browser growth, worker barrier and GPU delivery remain open" as *u8)
157}