nx_optenforce_gate.nx source
↩ module page · 139 lines · 7697 B
1// nx_optenforce_gate.nx -- GATE for LN2 option / null enforcement (lang.plan rung LN2, symbol
2// opt_enforce_unwrap). The rung's done-rule: a pointer or Option value must be unwrapped through a
3// checked path before deref in checked mode; the sentinel-zero deref class is REFUSED at compile
4// time on a fixture, and the stdlib still builds. Subjects: the toolchain compiler (argv[1],
5// default the live builder) + runtime/nx_option_enforce.nx (hazard) + nx_option_enforce_ok.nx
6// (every narrowing idiom, the positive control) + nx_option_enforce_reassign.nx (un-narrowing).
7//
8// Driver = nx_ccgate_lib. Data captures in /tmp/nxoe/, RUNNABLES under _build/ (NAS /tmp noexec).
9// Usage: nx_optenforce_gate [compiler_elf] (CWD = the tree root, like nx_cc_equiv_gate)
10// license_tier: ORIGINAL No hw writes (Rule 26).
11import "nx_syscalls.nx"
12import "nx_gate_verdict.nx"
13import "nx_ccgate_lib.nx"
14
15func main(argc: i64, argv: *i64) -> i64 {
16 var cc: *u8 = "_offc/nx_cc_sovereign.elf\x00"
17 if argc >= 2 { cc = argv[1] as *u8 }
18 let FLAG: *u8 = "--optenforce\x00"
19 ccg_anchor_root()
20 sys_mkdir("/tmp/nxoe\x00" as *u8, CCG_MODE_X)
21 let pid: i64 = ccg_pid()
22
23 // hazard (h), control (c), reassign (r) x default (d) / mode (m)
24 let p_hds: *u8 = ccg_path("/tmp/nxoe/hd_\x00" as *u8, pid, ".s\x00" as *u8)
25 let p_hde: *u8 = ccg_path("/tmp/nxoe/hd_\x00" as *u8, pid, ".err\x00" as *u8)
26 let p_hdelf: *u8 = ccg_path("_build/nxoe_hd_\x00" as *u8, pid, ".elf\x00" as *u8)
27 let p_hms: *u8 = ccg_path("/tmp/nxoe/hm_\x00" as *u8, pid, ".s\x00" as *u8)
28 let p_hme: *u8 = ccg_path("/tmp/nxoe/hm_\x00" as *u8, pid, ".err\x00" as *u8)
29 let p_hmelf: *u8 = ccg_path("_build/nxoe_hm_\x00" as *u8, pid, ".elf\x00" as *u8)
30 let p_cds: *u8 = ccg_path("/tmp/nxoe/cd_\x00" as *u8, pid, ".s\x00" as *u8)
31 let p_cde: *u8 = ccg_path("/tmp/nxoe/cd_\x00" as *u8, pid, ".err\x00" as *u8)
32 let p_cdelf: *u8 = ccg_path("_build/nxoe_cd_\x00" as *u8, pid, ".elf\x00" as *u8)
33 let p_cms: *u8 = ccg_path("/tmp/nxoe/cm_\x00" as *u8, pid, ".s\x00" as *u8)
34 let p_cme: *u8 = ccg_path("/tmp/nxoe/cm_\x00" as *u8, pid, ".err\x00" as *u8)
35 let p_cmelf: *u8 = ccg_path("_build/nxoe_cm_\x00" as *u8, pid, ".elf\x00" as *u8)
36 let p_rds: *u8 = ccg_path("/tmp/nxoe/rd_\x00" as *u8, pid, ".s\x00" as *u8)
37 let p_rde: *u8 = ccg_path("/tmp/nxoe/rd_\x00" as *u8, pid, ".err\x00" as *u8)
38 let p_rdelf: *u8 = ccg_path("_build/nxoe_rd_\x00" as *u8, pid, ".elf\x00" as *u8)
39 let p_rms: *u8 = ccg_path("/tmp/nxoe/rm_\x00" as *u8, pid, ".s\x00" as *u8)
40 let p_rme: *u8 = ccg_path("/tmp/nxoe/rm_\x00" as *u8, pid, ".err\x00" as *u8)
41 let p_rmelf: *u8 = ccg_path("_build/nxoe_rm_\x00" as *u8, pid, ".elf\x00" as *u8)
42 let p_asm: *u8 = ccg_path("_build/nxoe_asm_\x00" as *u8, pid, ".elf\x00" as *u8)
43
44 let ctr: *i64 = gv_ctr()
45 gv_head("=== nx_optenforce_gate -- LN2 option/null enforcement: --optenforce refuses the sentinel-zero deref, accepts every narrowing idiom, the stdlib still builds ===" as *u8)
46
47 let hfx: *u8 = "runtime/nx_option_enforce.nx\x00"
48 let cfx: *u8 = "runtime/nx_option_enforce_ok.nx\x00"
49 let rfx: *u8 = "runtime/nx_option_enforce_reassign.nx\x00"
50
51 // -- default mode: the hazard record (everything compiles and is right by accident) --
52 let bhd: i64 = ccg_build(cc, 0 as *u8, hfx, p_hds, p_hdelf, p_hde, p_asm)
53 let bcd: i64 = ccg_build(cc, 0 as *u8, cfx, p_cds, p_cdelf, p_cde, p_asm)
54 let brd: i64 = ccg_build(cc, 0 as *u8, rfx, p_rds, p_rdelf, p_rde, p_asm)
55 var hd_run: i64 = 0 - 1
56 var cd_run: i64 = 0 - 1
57 var rd_run: i64 = 0 - 1
58 if bhd == 0 { hd_run = ccg_phase(p_hdelf, 0 as *u8) }
59 if bcd == 0 { cd_run = ccg_phase(p_cdelf, 0 as *u8) }
60 if brd == 0 { rd_run = ccg_phase(p_rdelf, 0 as *u8) }
61 ccg_val("hazard_default_build_rc" as *u8, bhd)
62 ccg_val("hazard_default_exit" as *u8, hd_run)
63 ccg_val("control_default_build_rc" as *u8, bcd)
64 ccg_val("control_default_exit" as *u8, cd_run)
65 ccg_val("reassign_default_build_rc" as *u8, brd)
66 ccg_val("reassign_default_exit" as *u8, rd_run)
67 var t1: i64 = 0
68 if bhd == 0 { t1 = 1 }
69 gv_check("T1 neg-control-hazard-compiles-under-default (the sentinel-zero deref is legal source today)" as *u8, t1, ctr)
70 var t2: i64 = 0
71 if hd_run == 0 { t2 = 1 }
72 gv_check("T2 neg-control-hazard-runs-exit0-under-default (right by accident of its input -- the class)" as *u8, t2, ctr)
73 var t3: i64 = 0
74 if bcd == 0 { t3 = 1 }
75 gv_check("T3 control-compiles-under-default" as *u8, t3, ctr)
76 var t4: i64 = 0
77 if cd_run == 0 { t4 = 1 }
78 gv_check("T4 control-runs-exit0-under-default" as *u8, t4, ctr)
79 var t5: i64 = 0
80 if brd == 0 { t5 = 1 }
81 gv_check("T5 neg-control-reassign-compiles-under-default" as *u8, t5, ctr)
82 var t6: i64 = 0
83 if rd_run == 0 { t6 = 1 }
84 gv_check("T6 neg-control-reassign-runs-exit0-under-default" as *u8, t6, ctr)
85
86 // -- optenforce mode --
87 let bhm: i64 = ccg_build(cc, FLAG, hfx, p_hms, p_hmelf, p_hme, p_asm)
88 let bcm: i64 = ccg_build(cc, FLAG, cfx, p_cms, p_cmelf, p_cme, p_asm)
89 let brm: i64 = ccg_build(cc, FLAG, rfx, p_rms, p_rmelf, p_rme, p_asm)
90 var cm_run: i64 = 0 - 1
91 if bcm == 0 { cm_run = ccg_phase(p_cmelf, 0 as *u8) }
92 ccg_val("hazard_mode_build_rc" as *u8, bhm)
93 ccg_val("control_mode_build_rc" as *u8, bcm)
94 ccg_val("control_mode_exit" as *u8, cm_run)
95 ccg_val("reassign_mode_build_rc" as *u8, brm)
96 var t7: i64 = 0
97 if bhm != 0 { t7 = 1 }
98 gv_check("T7 optenforce-REFUSES-the-sentinel-zero-deref (the rung's done-rule: unchecked call result dereferenced)" as *u8, t7, ctr)
99 let t8: i64 = ccg_file_has(p_hme, "capability=option-enforce-unwrap\x00" as *u8)
100 gv_check("T8 refusal-names-the-option-enforce-rule (not some other error wearing the refusal)" as *u8, t8, ctr)
101 let t9: i64 = ccg_file_has(p_hme, "dereference of 'p'\x00" as *u8)
102 gv_check("T9 refusal-names-the-unchecked-local (p, the call result)" as *u8, t9, ctr)
103 var t10: i64 = 0
104 if bcm == 0 { t10 = 1 }
105 gv_check("T10 control-COMPILES-under-optenforce (every idiom accepted AND the imported stdlib builds -- a deny-everything mode fails HERE)" as *u8, t10, ctr)
106 var t11: i64 = 0
107 if cm_run == 0 { t11 = 1 }
108 gv_check("T11 control-runs-exit0-under-optenforce" as *u8, t11, ctr)
109 // the mode emits NO IR: the control's asm under the flag equals its asm under the default
110 var t12: i64 = 0
111 let szd: *i64 = sys_mmap(16) as *i64
112 let szm: *i64 = sys_mmap(16) as *i64
113 let bd: *u8 = sys_read_file(p_cds, szd)
114 let bm: *u8 = sys_read_file(p_cms, szm)
115 if (bd as i64) != 0 { if (bm as i64) != 0 { if szd[0] == szm[0] {
116 var same: i64 = 1
117 var i: i64 = 0
118 var go: i64 = 1
119 while go == 1 {
120 if i >= szd[0] { go = 0 } else {
121 if bd[i] != bm[i] { same = 0; go = 0 }
122 i = i + 1
123 }
124 }
125 t12 = same
126 } } }
127 ccg_val("control_asm_default_bytes" as *u8, szd[0])
128 ccg_val("control_asm_mode_bytes" as *u8, szm[0])
129 gv_check("T12 mode-emits-no-IR (control asm byte-identical default vs --optenforce: enforcement is compile-time only)" as *u8, t12, ctr)
130 var t13: i64 = 0
131 if brm != 0 { t13 = 1 }
132 gv_check("T13 optenforce-REFUSES-deref-after-reassignment (a stale check does not survive `p = call()`)" as *u8, t13, ctr)
133 let t14: i64 = ccg_file_has(p_rme, "capability=option-enforce-unwrap\x00" as *u8)
134 gv_check("T14 reassign-refusal-names-the-rule" as *u8, t14, ctr)
135
136 let rc: i64 = gv_verdict("OPTENFORCE-LN2" as *u8, ctr, "optenforce declared mode" as *u8)
137 sys_exit(rc)
138 return rc
139}