code wiki / (root) / nx_install_plan_test.nx

nx_install_plan_test.nx source

↩ module page · 195 lines · 8187 B

1// nx_install_plan_test.nx -- smoke for nx_install_plan SA-6/SA-7 bridge. 2// 3// Exercises: 4// 1. Allocation + canary integrity 5// 2. Bad-input gates (null plan / probe / calib / policy / verdict) 6// 3. Capture from a real 3-node selector run -- verifies snapshots 7// match the source structs + variant_ids match the picked 8// manifests 9// 4. variant_id_at boundary checks 10// 5. is_valid gate 11// 12// SA-6/SA-7 bridge: this is the durable record SA-6 feeds to compile 13// and SA-7 cryptographically attests. 14 15import "nx_syscalls.nx" 16import "nx_probe.nx" 17import "nx_calibrate.nx" 18import "nx_select.nx" 19import "nx_select_joint.nx" 20import "nx_install_plan.nx" 21 22func _make_probe() -> *NxProbeRecord { 23 let p: *NxProbeRecord = nx_probe_new() 24 p.actual_isa_family = NX_ISA_RV64 25 p.actual_endianness = NX_ENDIAN_LITTLE 26 p.actual_pointer_width_bits = 64 27 p.actual_page_size_bytes = 4096 28 p.actual_mmap_works = 1 29 p.actual_write_works = 1 30 p.actual_mono_clock_works = 1 31 return p 32} 33 34func _make_calib() -> *NxCalibrationRecord { 35 // Synthetic calibration values for a LAPTOP-class machine so the 36 // tier inference resolves deterministically. 37 let c: *NxCalibrationRecord = nx_calibrate_new() 38 c.int_alu_ps_per_op = 500 39 c.mem_ns_per_access_l1 = 5 40 c.mem_ns_per_access_l2 = 20 41 c.mem_ns_per_access_ram = 300 42 c.mem_bw_mib_per_s = 5000 43 c.syscall_ns_clock_gettime = 50 44 c.syscall_ns_mmap = 1000 45 c.inferred_tier = NX_TIER_INF_LAPTOP 46 return c 47} 48 49func _mk(variant_id: i64, cost: i64, in_lay: i64, out_lay: i64) -> *NxManifest { 50 let m: *NxManifest = nx_manifest_new( 51 100, variant_id, 52 nx_isa_bit(NX_ISA_RV64), 53 0, 4096, 54 NX_TIER_INF_MOBILE, NX_TIER_INF_HPC, 55 cost, 0, 0, 0) 56 nx_manifest_set_layout(m, in_lay, out_lay) 57 return m 58} 59 60func main() -> i64 { 61 let probe: *NxProbeRecord = _make_probe() 62 let calib: *NxCalibrationRecord = _make_calib() 63 let policy: *NxPolicy = nx_policy_throughput_default() 64 65 // ----- 1. Allocation + canaries ----- 66 let p: *NxInstallPlan = nx_install_plan_new() 67 if (p as i64) == 0 { return 1 } 68 if p.canary_pre != NX_PLAN_CANARY_PRE { return 2 } 69 if p.canary_post != NX_PLAN_CANARY_POST { return 3 } 70 if p.schema_version != NX_PLAN_SCHEMA_VERSION { return 4 } 71 if p.selection_verdict != NX_PLAN_VERDICT_UNKNOWN { return 5 } 72 if p.n_selected != 0 { return 6 } 73 if nx_install_plan_is_valid(p) != 1 { return 7 } 74 75 // ----- 2. Bad-input gates ----- 76 let dummy_ptrs: *i64 = (sys_mmap(8)) as *i64 77 let dummy_sel: *i64 = (sys_mmap(8)) as *i64 78 let null_probe: *NxProbeRecord = (0 as i64) as *NxProbeRecord 79 let rc_np: i64 = nx_install_plan_capture(p, null_probe, calib, policy, 80 dummy_ptrs, 1, dummy_sel, 81 0, NX_PLAN_VERDICT_OK) 82 if rc_np != 4 { return 8 } 83 84 let null_calib: *NxCalibrationRecord = (0 as i64) as *NxCalibrationRecord 85 let rc_nc: i64 = nx_install_plan_capture(p, probe, null_calib, policy, 86 dummy_ptrs, 1, dummy_sel, 87 0, NX_PLAN_VERDICT_OK) 88 if rc_nc != 5 { return 9 } 89 90 let null_pol: *NxPolicy = (0 as i64) as *NxPolicy 91 let rc_npol: i64 = nx_install_plan_capture(p, probe, calib, null_pol, 92 dummy_ptrs, 1, dummy_sel, 93 0, NX_PLAN_VERDICT_OK) 94 if rc_npol != 6 { return 10 } 95 96 let rc_bv: i64 = nx_install_plan_capture(p, probe, calib, policy, 97 dummy_ptrs, 0, dummy_sel, 98 0, 99) 99 if rc_bv != 8 { return 11 } 100 101 // ----- 3. Real 3-node chain capture ----- 102 // Build the same 3-node fixture used by nx_select_joint_chain_test. 103 let a_med_col: *NxManifest = _mk(2002, 300, NX_LAYOUT_OPAQUE_PASS, NX_LAYOUT_COL_MAJOR) 104 let a_cheap_row: *NxManifest = _mk(2001, 100, NX_LAYOUT_OPAQUE_PASS, NX_LAYOUT_ROW_MAJOR) 105 let b_med_col: *NxManifest = _mk(2102, 200, NX_LAYOUT_COL_MAJOR, NX_LAYOUT_COL_MAJOR) 106 let b_cheap_row: *NxManifest = _mk(2101, 100, NX_LAYOUT_ROW_MAJOR, NX_LAYOUT_ROW_MAJOR) 107 let c_col: *NxManifest = _mk(2201, 100, NX_LAYOUT_COL_MAJOR, NX_LAYOUT_OPAQUE_PASS) 108 109 let a_ptrs: *i64 = (sys_mmap(16)) as *i64 110 a_ptrs[0] = a_cheap_row as i64 111 a_ptrs[1] = a_med_col as i64 112 let b_ptrs: *i64 = (sys_mmap(16)) as *i64 113 b_ptrs[0] = b_cheap_row as i64 114 b_ptrs[1] = b_med_col as i64 115 let c_ptrs: *i64 = (sys_mmap(8)) as *i64 116 c_ptrs[0] = c_col as i64 117 118 let a_node: *NxJointNode = nx_joint_node_new(100, a_ptrs, 2) 119 let b_node: *NxJointNode = nx_joint_node_new(200, b_ptrs, 2) 120 let c_node: *NxJointNode = nx_joint_node_new(300, c_ptrs, 1) 121 122 let chain_nodes: *i64 = (sys_mmap(24)) as *i64 123 chain_nodes[0] = a_node as i64 124 chain_nodes[1] = b_node as i64 125 chain_nodes[2] = c_node as i64 126 127 let sel_out: *i64 = (sys_mmap(24)) as *i64 128 let rc_chain: i64 = nx_select_joint_chain(chain_nodes, 3, probe, 129 calib.inferred_tier, 130 1048576, 0, 131 policy, sel_out) 132 if rc_chain != NX_SEL_JOINT_OK { return 12 } 133 if sel_out[0] != 1 { return 13 } // a_med_col 134 if sel_out[1] != 1 { return 14 } // b_med_col 135 if sel_out[2] != 0 { return 15 } // c_col 136 137 // Capture into plan. 138 let total_cost: i64 = 600 * 1024 // 600 Q0 scaled to Q10 if needed 139 let plan: *NxInstallPlan = nx_install_plan_new() 140 let rc_cap: i64 = nx_install_plan_capture(plan, probe, calib, policy, 141 chain_nodes, 3, sel_out, 142 total_cost, NX_PLAN_VERDICT_OK) 143 if rc_cap != 0 { return 16 } 144 if nx_install_plan_is_valid(plan) != 1 { return 17 } 145 146 // ----- 4. Snapshot verification ----- 147 // Probe snapshot. 148 if plan.probe_isa_family != NX_ISA_RV64 { return 18 } 149 if plan.probe_endianness != NX_ENDIAN_LITTLE { return 19 } 150 if plan.probe_pointer_width_bits != 64 { return 20 } 151 if plan.probe_page_size_bytes != 4096 { return 21 } 152 if plan.probe_mmap_works != 1 { return 22 } 153 if plan.probe_mono_clock_works != 1 { return 23 } 154 155 // Calibration snapshot. 156 if plan.calib_int_alu_ps_per_op != 500 { return 24 } 157 if plan.calib_mem_ns_l1 != 5 { return 25 } 158 if plan.calib_mem_ns_ram != 300 { return 26 } 159 if plan.calib_mem_bw_mib_per_s != 5000 { return 27 } 160 if plan.calib_inferred_tier != NX_TIER_INF_LAPTOP { return 28 } 161 162 // Policy snapshot (throughput default has weight_throughput=1024). 163 if plan.policy_weight_throughput != 1024 { return 29 } 164 165 // Selection result. 166 if plan.n_selected != 3 { return 30 } 167 if plan.total_cost_q10 != total_cost { return 31 } 168 if plan.selection_verdict != NX_PLAN_VERDICT_OK { return 32 } 169 170 // Variant IDs match the PICKED manifests (not the indices!). 171 if nx_install_plan_variant_id_at(plan, 0) != 2002 { return 33 } // a_med_col 172 if nx_install_plan_variant_id_at(plan, 1) != 2102 { return 34 } // b_med_col 173 if nx_install_plan_variant_id_at(plan, 2) != 2201 { return 35 } // c_col 174 175 // Boundary checks on variant_id_at. 176 if nx_install_plan_variant_id_at(plan, -1) != -1 { return 36 } 177 if nx_install_plan_variant_id_at(plan, 3) != -1 { return 37 } 178 if nx_install_plan_variant_id_at(plan, 999) != -1 { return 38 } 179 180 // Timestamp populated. 181 if plan.ts_us <= 0 { return 39 } 182 183 // ----- 5. Sealed-enum gate ---------- 184 if nx_plan_verdict_is_valid(NX_PLAN_VERDICT_OK) != 1 { return 40 } 185 if nx_plan_verdict_is_valid(-1) != 0 { return 41 } 186 if nx_plan_verdict_is_valid(NX_PLAN_VERDICT_N) != 0 { return 42 } 187 188 // ----- 6. Tamper detection --------- 189 // Stomp the post-canary; is_valid must report false. 190 plan.canary_post = 0xDEADBEEF 191 if nx_install_plan_is_valid(plan) != 0 { return 43 } 192 if nx_install_plan_variant_id_at(plan, 0) != -1 { return 44 } 193 194 return 0 195}