omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tests / algebra / test_oma_native_failure.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Pure seeded OM-2 native failure, fault-injection, and output-preservation invariant test. |
| 3 | note Replaces tests/algebra/test_oma_native_failure.c. |
| 4 | note Concatenated after src/algebra/oma_lib.om. |
| 5 | |
| 6 | const NF_OMEGA_OK ulong 0 |
| 7 | const NF_OMEGA_REENTRY ulong 1 |
| 8 | const NF_OMEGA_NO_STACK ulong 2 |
| 9 | |
| 10 | global nf_c ulong 2 |
| 11 | note nf_c: 0=checks, 1=fails |
| 12 | global nf_inj ulong 2 |
| 13 | note nf_inj: 0=deserialize_fault, 1=pack_byte_fault |
| 14 | |
| 15 | text nf_pass_a "PASS test_oma_native_failure (" |
| 16 | text nf_pass_b " checks, 0 failures; bridge-fault injection and error paths preserve outputs untouched)\n" |
| 17 | text nf_fail_a "FAIL test_oma_native_failure (check #" |
| 18 | text nf_fail_b ")\n" |
| 19 | |
| 20 | fn nf_chk cond bool to void |
| 21 | n = add get nf_c 0 1 |
| 22 | put nf_c 0 n |
| 23 | if not cond |
| 24 | put nf_c 1 add get nf_c 1 1 |
| 25 | oma_put_err nf_fail_a |
| 26 | oma_put_u64 2 n |
| 27 | oma_put_err nf_fail_b |
| 28 | end |
| 29 | end |
| 30 | |
| 31 | note ---- Part A: Bridge fault-injection wrappers (mirrors __wrap_omega2_oma_om_* in C) ---- |
| 32 | |
| 33 | fn nf_bridge_deserialize src view uchar 16 dst edit ulong 2 to ulong |
| 34 | mode = get nf_inj 0 |
| 35 | if ne mode NF_OMEGA_OK |
| 36 | ret mode |
| 37 | end |
| 38 | rc = oma_block_deserialize src dst |
| 39 | if ne rc OMA_OK |
| 40 | ret NF_OMEGA_REENTRY |
| 41 | end |
| 42 | ret NF_OMEGA_OK |
| 43 | end |
| 44 | |
| 45 | fn nf_wrapped_deserialize src view uchar 16 dst edit ulong 2 to int |
| 46 | tmp = new ulong 2 0 |
| 47 | st = nf_bridge_deserialize src tmp |
| 48 | if ne st NF_OMEGA_OK |
| 49 | ret OMA_E_NATIVE |
| 50 | end |
| 51 | put dst 0 get tmp 0 |
| 52 | put dst 1 get tmp 1 |
| 53 | ret OMA_OK |
| 54 | end |
| 55 | |
| 56 | fn nf_bridge_pack_dense_byte t view byte * start ulong n ulong byte_out edit uchar 1 to ulong |
| 57 | mode = get nf_inj 1 |
| 58 | if and ne mode NF_OMEGA_OK gt start 0 |
| 59 | ret mode |
| 60 | end |
| 61 | put byte_out 0 oma_pack_dense_one t start n |
| 62 | ret NF_OMEGA_OK |
| 63 | end |
| 64 | |
| 65 | fn nf_wrapped_pack_dense t view byte * n ulong dst edit uchar * out_len edit ulong 1 to int |
| 66 | nbytes = oma_dense_bytes n |
| 67 | if lt len t n |
| 68 | ret OMA_E_ARG |
| 69 | end |
| 70 | if lt len dst nbytes |
| 71 | ret OMA_E_OVERFLOW |
| 72 | end |
| 73 | for i ulong n bound 64 |
| 74 | v = int get t i |
| 75 | if or lt v -1 gt v 1 |
| 76 | ret OMA_E_INVALID_TRIT |
| 77 | end |
| 78 | end |
| 79 | stage = new uchar 16 0 |
| 80 | if gt nbytes 16 |
| 81 | ret OMA_E_OVERFLOW |
| 82 | end |
| 83 | b1 = new uchar 1 0 |
| 84 | for k ulong nbytes bound 16 |
| 85 | st = nf_bridge_pack_dense_byte t mul k 5 n b1 |
| 86 | if ne st NF_OMEGA_OK |
| 87 | ret OMA_E_NATIVE |
| 88 | end |
| 89 | put stage k get b1 0 |
| 90 | end |
| 91 | for k ulong nbytes bound 16 |
| 92 | put dst k get stage k |
| 93 | end |
| 94 | put out_len 0 nbytes |
| 95 | ret OMA_OK |
| 96 | end |
| 97 | |
| 98 | fn test_bridge_fault_injection to void |
| 99 | ser = new uchar 16 0 |
| 100 | put ser 0 uchar 5 |
| 101 | put ser 8 uchar 2 |
| 102 | blk = new ulong 2 0 |
| 103 | put blk 0 ulong 123 |
| 104 | put blk 1 ulong 456 |
| 105 | note 1. Inject OMEGA_REENTRY on deserialize: must return OMA_E_NATIVE and preserve blk |
| 106 | put nf_inj 0 NF_OMEGA_REENTRY |
| 107 | nf_chk eq nf_wrapped_deserialize ser blk OMA_E_NATIVE |
| 108 | nf_chk and eq get blk 0 123 eq get blk 1 456 |
| 109 | note Control: disabling fault lets valid deserialize succeed |
| 110 | put nf_inj 0 NF_OMEGA_OK |
| 111 | nf_chk eq nf_wrapped_deserialize ser blk OMA_OK |
| 112 | nf_chk and eq get blk 0 5 eq get blk 1 2 |
| 113 | |
| 114 | note 2. Inject mid-stream OMEGA_NO_STACK on pack_dense (start == 0 succeeds, start == 5 fails) |
| 115 | trits = new byte 6 0 |
| 116 | put trits 0 byte 1 |
| 117 | put trits 5 byte -1 |
| 118 | packed = new uchar 2 0 |
| 119 | put packed 0 uchar 12 |
| 120 | put packed 1 uchar 34 |
| 121 | sz = new ulong 1 42 |
| 122 | put nf_inj 1 NF_OMEGA_NO_STACK |
| 123 | nf_chk eq nf_wrapped_pack_dense trits 6 packed sz OMA_E_NATIVE |
| 124 | nf_chk and eq get packed 0 12 and eq get packed 1 34 eq get sz 0 42 |
| 125 | note Control: disabling fault lets valid pack_dense succeed and update both bytes + sz |
| 126 | put nf_inj 1 NF_OMEGA_OK |
| 127 | nf_chk eq nf_wrapped_pack_dense trits 6 packed sz OMA_OK |
| 128 | nf_chk eq get sz 0 2 |
| 129 | nf_chk and eq get packed 0 uchar 122 eq get packed 1 uchar 120 |
| 130 | end |
| 131 | |
| 132 | note ---- Part B: Direct oma_lib.om error paths & mid-stream atomicity ---- |
| 133 | |
| 134 | fn test_scalar_and_z3_preserve to void |
| 135 | tb = new byte 1 77 |
| 136 | uc = new uchar 1 99 |
| 137 | tb2 = new byte 2 55 |
| 138 | uc2 = new uchar 2 88 |
| 139 | nf_chk eq oma_trit_make 2 tb OMA_E_INVALID_TRIT |
| 140 | nf_chk eq int get tb 0 77 |
| 141 | nf_chk eq oma_trit_make -2 tb OMA_E_INVALID_TRIT |
| 142 | nf_chk eq int get tb 0 77 |
| 143 | nf_chk eq oma_trit_to_code 3 uc OMA_E_INVALID_TRIT |
| 144 | nf_chk eq get uc 0 uchar 99 |
| 145 | nf_chk eq oma_code_to_trit 3 tb OMA_E_INVALID_CODE |
| 146 | nf_chk eq int get tb 0 77 |
| 147 | nf_chk eq oma_code_neg 3 uc OMA_E_INVALID_CODE |
| 148 | nf_chk eq get uc 0 uchar 99 |
| 149 | nf_chk eq oma_code_add 1 3 uc2 OMA_E_INVALID_CODE |
| 150 | nf_chk and eq get uc2 0 uchar 88 eq get uc2 1 uchar 88 |
| 151 | nf_chk eq oma_code_mul 3 1 uc OMA_E_INVALID_CODE |
| 152 | nf_chk eq get uc 0 uchar 99 |
| 153 | nf_chk eq oma_trit_neg 2 tb OMA_E_INVALID_TRIT |
| 154 | nf_chk eq int get tb 0 77 |
| 155 | nf_chk eq oma_trit_add 1 2 tb2 OMA_E_INVALID_TRIT |
| 156 | nf_chk and eq int get tb2 0 55 eq int get tb2 1 55 |
| 157 | nf_chk eq oma_trit_mul -2 1 tb OMA_E_INVALID_TRIT |
| 158 | nf_chk eq int get tb 0 77 |
| 159 | nf_chk eq oma_z3_make 3 uc OMA_E_INVALID_Z3 |
| 160 | nf_chk eq get uc 0 uchar 99 |
| 161 | nf_chk eq oma_z3_add 0 -1 uc OMA_E_INVALID_Z3 |
| 162 | nf_chk eq get uc 0 uchar 99 |
| 163 | nf_chk eq oma_z3_mul 2 3 uc OMA_E_INVALID_Z3 |
| 164 | nf_chk eq get uc 0 uchar 99 |
| 165 | nf_chk eq oma_z3_neg 3 uc OMA_E_INVALID_Z3 |
| 166 | nf_chk eq get uc 0 uchar 99 |
| 167 | nf_chk eq oma_z3_to_trit 3 tb OMA_E_INVALID_Z3 |
| 168 | nf_chk eq int get tb 0 77 |
| 169 | nf_chk eq oma_trit_to_z3 2 uc OMA_E_INVALID_TRIT |
| 170 | nf_chk eq get uc 0 uchar 99 |
| 171 | end |
| 172 | |
| 173 | fn test_block_ops_preserve to void |
| 174 | bad_b = new ulong 2 0 |
| 175 | put bad_b 0 ulong 0x10 |
| 176 | put bad_b 1 ulong 0x10 |
| 177 | ok_b = new ulong 2 0 |
| 178 | put ok_b 0 ulong 1 |
| 179 | put ok_b 1 ulong 2 |
| 180 | dst_b = new ulong 2 0 |
| 181 | put dst_b 0 ulong 123 |
| 182 | put dst_b 1 ulong 456 |
| 183 | cy_b = new ulong 2 0 |
| 184 | put cy_b 0 ulong 789 |
| 185 | put cy_b 1 ulong 321 |
| 186 | nf_chk eq oma_block_neg bad_b dst_b OMA_E_INVALID_PLANES |
| 187 | nf_chk and eq get dst_b 0 123 eq get dst_b 1 456 |
| 188 | nf_chk eq oma_block_add ok_b bad_b dst_b cy_b OMA_E_INVALID_PLANES |
| 189 | nf_chk and eq get dst_b 0 123 eq get dst_b 1 456 |
| 190 | nf_chk and eq get cy_b 0 789 eq get cy_b 1 321 |
| 191 | nf_chk eq oma_block_mul bad_b ok_b dst_b OMA_E_INVALID_PLANES |
| 192 | nf_chk and eq get dst_b 0 123 eq get dst_b 1 456 |
| 193 | dot_o = new int 1 777 |
| 194 | nf_chk eq oma_block_dot ok_b bad_b dot_o OMA_E_INVALID_PLANES |
| 195 | nf_chk eq get dot_o 0 777 |
| 196 | ser_o = new uchar 16 0xaa |
| 197 | nf_chk eq oma_block_serialize bad_b ser_o OMA_E_INVALID_PLANES |
| 198 | nf_chk and eq get ser_o 0 uchar 0xaa eq get ser_o 15 uchar 0xaa |
| 199 | bad_ser = new uchar 16 0 |
| 200 | put bad_ser 0 uchar 1 |
| 201 | put bad_ser 8 uchar 1 |
| 202 | nf_chk eq oma_block_deserialize bad_ser dst_b OMA_E_INVALID_PLANES |
| 203 | nf_chk and eq get dst_b 0 123 eq get dst_b 1 456 |
| 204 | note Mid-stream block_encode failure at last lane (lane 63) |
| 205 | src64 = new byte 64 1 |
| 206 | put src64 63 byte 2 |
| 207 | nf_chk eq oma_block_encode src64 dst_b OMA_E_INVALID_TRIT |
| 208 | nf_chk and eq get dst_b 0 123 eq get dst_b 1 456 |
| 209 | dec64 = new byte 64 55 |
| 210 | nf_chk eq oma_block_decode bad_b dec64 OMA_E_INVALID_PLANES |
| 211 | nf_chk and eq int get dec64 0 55 eq int get dec64 63 55 |
| 212 | zsrc64 = new uchar 64 1 |
| 213 | put zsrc64 63 uchar 3 |
| 214 | nf_chk eq oma_z3_block_encode zsrc64 dst_b OMA_E_INVALID_Z3 |
| 215 | nf_chk and eq get dst_b 0 123 eq get dst_b 1 456 |
| 216 | zdec64 = new uchar 64 88 |
| 217 | nf_chk eq oma_z3_block_decode bad_b zdec64 OMA_E_INVALID_PLANES |
| 218 | nf_chk and eq get zdec64 0 uchar 88 eq get zdec64 63 uchar 88 |
| 219 | nf_chk eq oma_z3_block_from_trits bad_b dst_b OMA_E_INVALID_PLANES |
| 220 | nf_chk and eq get dst_b 0 123 eq get dst_b 1 456 |
| 221 | nf_chk eq oma_z3_block_to_trits bad_b dst_b OMA_E_INVALID_PLANES |
| 222 | nf_chk and eq get dst_b 0 123 eq get dst_b 1 456 |
| 223 | end |
| 224 | |
| 225 | fn test_dense_pack_unpack_preserve to void |
| 226 | note Mid-stream pack_dense failure on byte 1 (trit 5 invalid, trits 0..4 valid) |
| 227 | trits = new byte 6 1 |
| 228 | put trits 5 byte 2 |
| 229 | packed = new uchar 2 0 |
| 230 | put packed 0 uchar 12 |
| 231 | put packed 1 uchar 34 |
| 232 | sz = new ulong 1 42 |
| 233 | nf_chk eq oma_pack_dense trits 6 packed sz OMA_E_INVALID_TRIT |
| 234 | nf_chk and eq get packed 0 12 and eq get packed 1 34 eq get sz 0 42 |
| 235 | note Overflow when output slice too short |
| 236 | put trits 5 byte 0 |
| 237 | nf_chk eq oma_pack_dense trits 6 part packed 0 1 sz OMA_E_OVERFLOW |
| 238 | nf_chk and eq get packed 0 12 and eq get packed 1 34 eq get sz 0 42 |
| 239 | note Short input slice -> OMA_E_ARG |
| 240 | nf_chk eq oma_pack_dense part trits 0 5 6 packed sz OMA_E_ARG |
| 241 | nf_chk and eq get packed 0 12 and eq get packed 1 34 eq get sz 0 42 |
| 242 | note Mid-stream unpack_dense failure: byte 0 valid (121), byte 1 invalid (243) |
| 243 | in_d = new uchar 2 0 |
| 244 | put in_d 0 uchar 121 |
| 245 | put in_d 1 uchar 243 |
| 246 | out_t = new byte 6 77 |
| 247 | nf_chk eq oma_unpack_dense in_d 2 6 out_t OMA_E_INVALID_BYTE |
| 248 | for i ulong 6 |
| 249 | nf_chk eq int get out_t i 77 |
| 250 | end |
| 251 | note Non-zero padding trit in final byte (byte 1 = 124 has trit 1 != 0 for n=6) |
| 252 | put in_d 1 uchar 124 |
| 253 | nf_chk eq oma_unpack_dense in_d 2 6 out_t OMA_E_INVALID_BYTE |
| 254 | for i ulong 6 |
| 255 | nf_chk eq int get out_t i 77 |
| 256 | end |
| 257 | note Short in_len -> OMA_E_ARG |
| 258 | put in_d 1 uchar 121 |
| 259 | nf_chk eq oma_unpack_dense in_d 1 6 out_t OMA_E_ARG |
| 260 | nf_chk eq int get out_t 0 77 |
| 261 | end |
| 262 | |
| 263 | fn test_bitplane_and_dot_preserve to void |
| 264 | note Mid-stream pack_bitplane failure in block 1 (trit 64 invalid) |
| 265 | t66 = new byte 66 1 |
| 266 | put t66 64 byte -2 |
| 267 | bp = new ulong 4 0 |
| 268 | put bp 0 11 |
| 269 | put bp 1 22 |
| 270 | put bp 2 33 |
| 271 | put bp 3 44 |
| 272 | nf_chk eq oma_pack_bitplane t66 66 bp 2 OMA_E_INVALID_TRIT |
| 273 | nf_chk and eq get bp 0 11 and eq get bp 1 22 and eq get bp 2 33 eq get bp 3 44 |
| 274 | put t66 64 byte 0 |
| 275 | nf_chk eq oma_pack_bitplane t66 66 bp 1 OMA_E_OVERFLOW |
| 276 | nf_chk and eq get bp 0 11 eq get bp 2 33 |
| 277 | note Mid-stream unpack_bitplane failure: block 0 valid, block 1 invalid planes |
| 278 | put bp 0 1 |
| 279 | put bp 1 2 |
| 280 | put bp 2 4 |
| 281 | put bp 3 4 |
| 282 | ub = new byte 66 66 |
| 283 | nf_chk eq oma_unpack_bitplane bp 2 66 ub OMA_E_INVALID_PLANES |
| 284 | nf_chk and eq int get ub 0 66 eq int get ub 65 66 |
| 285 | note Mid-stream unpack_bitplane stray bit in block 1 (bit 2 set when only 2 live lanes) |
| 286 | put bp 2 4 |
| 287 | put bp 3 0 |
| 288 | nf_chk eq oma_unpack_bitplane bp 2 66 ub OMA_E_ARG |
| 289 | nf_chk and eq int get ub 0 66 eq int get ub 65 66 |
| 290 | note dot_tw_i8 errors preserve dst |
| 291 | x66 = new byte 66 3 |
| 292 | dot_o = new int 1 999 |
| 293 | nf_chk eq oma_dot_tw_i8 bp x66 add OMA_DOT_I8_MAX_N 1 dot_o OMA_E_OVERFLOW |
| 294 | nf_chk eq get dot_o 0 999 |
| 295 | nf_chk eq oma_dot_tw_i8 bp x66 66 dot_o OMA_E_ARG |
| 296 | nf_chk eq get dot_o 0 999 |
| 297 | put bp 2 1 |
| 298 | put bp 3 1 |
| 299 | nf_chk eq oma_dot_tw_i8 bp x66 66 dot_o OMA_E_INVALID_PLANES |
| 300 | nf_chk eq get dot_o 0 999 |
| 301 | end |
| 302 | |
| 303 | fn test_bt_and_quant_preserve to void |
| 304 | note int_to_bt overflow preserves dst and ndigits |
| 305 | bt = new byte 4 55 |
| 306 | nd = new ulong 1 88 |
| 307 | nf_chk eq oma_int_to_bt long 1000 bt 4 nd OMA_E_OVERFLOW |
| 308 | nf_chk and eq int get bt 0 55 eq get nd 0 88 |
| 309 | nf_chk eq oma_int_to_bt_fixed long 1000 bt 4 OMA_E_OVERFLOW |
| 310 | nf_chk eq int get bt 0 55 |
| 311 | note bt_to_int mid-stream invalid trit and i64 overflow preserve dst |
| 312 | d41 = new byte 41 1 |
| 313 | out_i = new long 1 12345 |
| 314 | nf_chk eq oma_bt_to_int d41 41 out_i OMA_E_OVERFLOW |
| 315 | nf_chk eq get out_i 0 12345 |
| 316 | put d41 3 byte 2 |
| 317 | nf_chk eq oma_bt_to_int part d41 0 4 4 out_i OMA_E_INVALID_TRIT |
| 318 | nf_chk eq get out_i 0 12345 |
| 319 | note quant_absmean mid-stream NaN and underflow preserve q and scale |
| 320 | w = new f32 4 1.0f32 |
| 321 | put w 3 bitcast f32 uint 0x7fc00000 |
| 322 | q = new byte 4 33 |
| 323 | sc = new f32 1 9.5f32 |
| 324 | nf_chk eq oma_quant_absmean w 4 q sc OMA_E_ARG |
| 325 | nf_chk and eq int get q 0 33 and eq int get q 3 33 eq get sc 0 9.5f32 |
| 326 | for i ulong 4 |
| 327 | put w i bitcast f32 uint 0x00000001 |
| 328 | end |
| 329 | nf_chk eq oma_quant_absmean w 4 q sc OMA_E_UNDERFLOW |
| 330 | nf_chk and eq int get q 0 33 eq get sc 0 9.5f32 |
| 331 | note quant_rel_l2 errors preserve err |
| 332 | er = new f64 1 12.25f64 |
| 333 | for i ulong 4 |
| 334 | put w i 1.0f32 |
| 335 | put q i byte 1 |
| 336 | end |
| 337 | nf_chk eq oma_quant_rel_l2 w q -1.0f32 er OMA_E_ARG |
| 338 | nf_chk eq get er 0 12.25f64 |
| 339 | put q 3 byte 2 |
| 340 | nf_chk eq oma_quant_rel_l2 w q 1.0f32 er OMA_E_INVALID_TRIT |
| 341 | nf_chk eq get er 0 12.25f64 |
| 342 | put q 3 byte 1 |
| 343 | put w 3 bitcast f32 uint 0x7f800000 |
| 344 | nf_chk eq oma_quant_rel_l2 w q 1.0f32 er OMA_E_ARG |
| 345 | nf_chk eq get er 0 12.25f64 |
| 346 | for i ulong 4 |
| 347 | put w i 0.0f32 |
| 348 | end |
| 349 | nf_chk eq oma_quant_rel_l2 w q 1.0f32 er OMA_E_ARG |
| 350 | nf_chk eq get er 0 12.25f64 |
| 351 | end |
| 352 | |
| 353 | fn main to int |
| 354 | test_bridge_fault_injection |
| 355 | test_scalar_and_z3_preserve |
| 356 | test_block_ops_preserve |
| 357 | test_dense_pack_unpack_preserve |
| 358 | test_bitplane_and_dot_preserve |
| 359 | test_bt_and_quant_preserve |
| 360 | if ne get nf_c 1 0 |
| 361 | ret 1 |
| 362 | end |
| 363 | oma_put_str nf_pass_a |
| 364 | oma_put_u64 1 get nf_c 0 |
| 365 | oma_put_str nf_pass_b |
| 366 | ret 0 |
| 367 | end |