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 / src / visor / visor_semantic.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Pure seeded OM-2 Visor lane 1: read-only semantic inspection API |
| 3 | note (replaces src/visor/visor_semantic.c). Depends on visor.om and omega_lower.om. |
| 4 | |
| 5 | text vsem_s_type "TYPE" |
| 6 | text vsem_s_value "VALUE" |
| 7 | text vsem_s_op "OPERATION" |
| 8 | text vsem_s_apply "APPLY" |
| 9 | text vsem_s_opaque "OPAQUE" |
| 10 | text vsem_op_add "ADD" |
| 11 | text vsem_op_sub "SUB" |
| 12 | text vsem_op_mul "MUL" |
| 13 | text vsem_op_div "DIV" |
| 14 | text vsem_op_and "AND" |
| 15 | text vsem_op_or "OR" |
| 16 | text vsem_op_xor "XOR" |
| 17 | text vsem_op_unk "UNKNOWN" |
| 18 | text vsem_ov_def "DEFAULT" |
| 19 | text vsem_ov_wrap "WRAP" |
| 20 | text vsem_ov_sat "SATURATE" |
| 21 | text vsem_ov_fc "FAIL_CLOSED" |
| 22 | text vsem_t_bool "bool" |
| 23 | text vsem_t_u "u" |
| 24 | text vsem_t_bv1 "bitvector<" |
| 25 | text vsem_t_bv2 ">" |
| 26 | text vsem_t_seq1 "seq<" |
| 27 | text vsem_t_seq2 "," |
| 28 | text vsem_m_kind "invalid kind" |
| 29 | text vsem_m_plen "invalid payload_len" |
| 30 | text vsem_m_ovf "invalid overflow policy" |
| 31 | text vsem_m_id "stored id does not match canonical digest" |
| 32 | text vsem_m_miss "MISSING sha256:" |
| 33 | text vsem_f_id "id: " |
| 34 | text vsem_f_kind "kind: " |
| 35 | text vsem_f_malf1 "status: MALFORMED (" |
| 36 | text vsem_f_malf2 ")\n" |
| 37 | text vsem_f_sh "shape: " |
| 38 | text vsem_f_ty "type: " |
| 39 | text vsem_f_tag "tag: " |
| 40 | text vsem_f_wid "width: " |
| 41 | text vsem_f_val "value: " |
| 42 | text vsem_f_true "true" |
| 43 | text vsem_f_false "false" |
| 44 | text vsem_f_opc "opcode: " |
| 45 | text vsem_f_ovf "overflow: " |
| 46 | text vsem_f_ops "operands: " |
| 47 | text vsem_f_d1 "dangling: " |
| 48 | text vsem_f_d2 " (DANGLING)\n" |
| 49 | text vsem_j1 "{\"id\":\"" |
| 50 | text vsem_j2 "\",\"kind\":\"" |
| 51 | text vsem_j3 "\",\"status\":\"" |
| 52 | text vsem_j4 "malformed\"}" |
| 53 | text vsem_j5 "ok\",\"shape\":\"" |
| 54 | text vsem_j6 "\",\"canonical_len\":" |
| 55 | text vsem_j7 "}" |
| 56 | text vsem_sp2 " " |
| 57 | text vsem_sha_sp " sha256:" |
| 58 | |
| 59 | global vsem_status long 1 |
| 60 | global vsem_oidx ulong 1 |
| 61 | global vsem_shape ulong 1 |
| 62 | global vsem_canon_len ulong 1 |
| 63 | global vsem_id_match ulong 1 |
| 64 | global vsem_dangling ulong 1 |
| 65 | global vsem_reason uchar 128 |
| 66 | global vsem_rlen ulong 1 |
| 67 | global vsem_seen uchar 256 |
| 68 | |
| 69 | fn vsem_set_reason s view uchar * to void |
| 70 | n = len s |
| 71 | if gt n 127 |
| 72 | n = 127 |
| 73 | end |
| 74 | for i ulong n bound 128 |
| 75 | put vsem_reason i get s i |
| 76 | end |
| 77 | put vsem_rlen 0 n |
| 78 | end |
| 79 | |
| 80 | fn vsem_put_opcode opc ulong to void |
| 81 | if eq opc V_OP_ADD |
| 82 | ob_puts vsem_op_add |
| 83 | ret |
| 84 | end |
| 85 | if eq opc V_OP_SUB |
| 86 | ob_puts vsem_op_sub |
| 87 | ret |
| 88 | end |
| 89 | if eq opc V_OP_MUL |
| 90 | ob_puts vsem_op_mul |
| 91 | ret |
| 92 | end |
| 93 | if eq opc V_OP_DIV |
| 94 | ob_puts vsem_op_div |
| 95 | ret |
| 96 | end |
| 97 | if eq opc V_OP_AND |
| 98 | ob_puts vsem_op_and |
| 99 | ret |
| 100 | end |
| 101 | if eq opc V_OP_OR |
| 102 | ob_puts vsem_op_or |
| 103 | ret |
| 104 | end |
| 105 | if eq opc V_OP_XOR |
| 106 | ob_puts vsem_op_xor |
| 107 | ret |
| 108 | end |
| 109 | ob_puts vsem_op_unk |
| 110 | end |
| 111 | |
| 112 | fn vsem_put_ovf ovf ulong to void |
| 113 | if eq ovf V_OVF_DEFAULT |
| 114 | ob_puts vsem_ov_def |
| 115 | ret |
| 116 | end |
| 117 | if eq ovf V_OVF_WRAP |
| 118 | ob_puts vsem_ov_wrap |
| 119 | ret |
| 120 | end |
| 121 | if eq ovf V_OVF_SATURATE |
| 122 | ob_puts vsem_ov_sat |
| 123 | ret |
| 124 | end |
| 125 | if eq ovf V_OVF_FAIL_CLOSED |
| 126 | ob_puts vsem_ov_fc |
| 127 | ret |
| 128 | end |
| 129 | ob_puts vsem_op_unk |
| 130 | end |
| 131 | |
| 132 | fn vsem_put_shape sh ulong to void |
| 133 | if eq sh 1 |
| 134 | ob_puts vsem_s_type |
| 135 | ret |
| 136 | end |
| 137 | if eq sh 2 |
| 138 | ob_puts vsem_s_value |
| 139 | ret |
| 140 | end |
| 141 | if eq sh 3 |
| 142 | ob_puts vsem_s_op |
| 143 | ret |
| 144 | end |
| 145 | if eq sh 4 |
| 146 | ob_puts vsem_s_apply |
| 147 | ret |
| 148 | end |
| 149 | ob_puts vsem_s_opaque |
| 150 | end |
| 151 | |
| 152 | fn vsem_put_type_str tidx ulong to void |
| 153 | pb = mul tidx V_MAX_PAYLOAD |
| 154 | tag = v_dec_u32le_pay pb 0 |
| 155 | wid = v_dec_u16le_pay pb 4 |
| 156 | slen = v_dec_u32le_pay pb 8 |
| 157 | if eq tag V_TY_BOOL |
| 158 | ob_puts vsem_t_bool |
| 159 | ret |
| 160 | end |
| 161 | if eq tag V_TY_UINT |
| 162 | ob_puts vsem_t_u |
| 163 | ob_put_u64 wid |
| 164 | ret |
| 165 | end |
| 166 | if eq tag V_TY_BITVEC |
| 167 | ob_puts vsem_t_bv1 |
| 168 | ob_put_u64 wid |
| 169 | ob_puts vsem_t_bv2 |
| 170 | ret |
| 171 | end |
| 172 | if eq tag V_TY_SEQ |
| 173 | ob_puts vsem_t_seq1 |
| 174 | for i ulong 32 |
| 175 | put v_tmp_id i get v_og_pay add add pb 12 i |
| 176 | end |
| 177 | ei = og_find_by_tmp |
| 178 | if ge ei 0 |
| 179 | vsem_put_type_str ulong ei |
| 180 | else |
| 181 | ob_puts vsem_op_unk |
| 182 | end |
| 183 | ob_puts vsem_t_seq2 |
| 184 | ob_put_u64 slen |
| 185 | ob_puts vsem_t_bv2 |
| 186 | ret |
| 187 | end |
| 188 | ob_puts vsem_op_unk |
| 189 | end |
| 190 | |
| 191 | fn vsem_count_dangling idx ulong to void |
| 192 | rc = get v_og_rcnt idx |
| 193 | for r ulong rc bound 4 |
| 194 | rslot = add mul idx V_MAX_RELS r |
| 195 | tb = mul rslot 32 |
| 196 | for i ulong 32 |
| 197 | put v_tmp_id i get v_og_rtgt add tb i |
| 198 | end |
| 199 | if lt og_find_by_tmp 0 |
| 200 | put vsem_dangling 0 add get vsem_dangling 0 1 |
| 201 | end |
| 202 | end |
| 203 | end |
| 204 | |
| 205 | fn vsem_inspect idx ulong to long |
| 206 | cnt = get v_og_cnt 0 |
| 207 | if ge idx cnt |
| 208 | ret -1 |
| 209 | end |
| 210 | put vsem_oidx 0 idx |
| 211 | put vsem_status 0 0 |
| 212 | put vsem_rlen 0 0 |
| 213 | put vsem_dangling 0 0 |
| 214 | kind = get v_og_kind idx |
| 215 | pl = get v_og_plen idx |
| 216 | if or eq kind 0 gt kind 6 |
| 217 | put vsem_status 0 -2 |
| 218 | vsem_set_reason vsem_m_kind |
| 219 | ret -2 |
| 220 | end |
| 221 | if gt pl V_MAX_PAYLOAD |
| 222 | put vsem_status 0 -2 |
| 223 | vsem_set_reason vsem_m_plen |
| 224 | ret -2 |
| 225 | end |
| 226 | elen = og_canonical_encode idx |
| 227 | if lt elen 0 |
| 228 | put vsem_status 0 -2 |
| 229 | vsem_set_reason vsem_m_plen |
| 230 | ret -2 |
| 231 | end |
| 232 | put vsem_canon_len 0 ulong elen |
| 233 | for i ulong ulong elen bound 4096 |
| 234 | put v_sha_in i get v_enc i |
| 235 | end |
| 236 | sha256_hash ulong elen |
| 237 | if og_id_eq_sha idx |
| 238 | put vsem_id_match 0 1 |
| 239 | else |
| 240 | put vsem_id_match 0 0 |
| 241 | put vsem_status 0 -2 |
| 242 | vsem_set_reason vsem_m_id |
| 243 | ret -2 |
| 244 | end |
| 245 | pb = mul idx V_MAX_PAYLOAD |
| 246 | if eq kind V_KIND_TYPE |
| 247 | if lt pl 44 |
| 248 | put vsem_status 0 -2 |
| 249 | vsem_set_reason vsem_m_plen |
| 250 | ret -2 |
| 251 | end |
| 252 | put vsem_shape 0 1 |
| 253 | else |
| 254 | if eq kind V_KIND_VALUE |
| 255 | if lt pl 98 |
| 256 | put vsem_status 0 -2 |
| 257 | vsem_set_reason vsem_m_plen |
| 258 | ret -2 |
| 259 | end |
| 260 | put vsem_shape 0 2 |
| 261 | else |
| 262 | if eq kind V_KIND_OP |
| 263 | if ge pl 204 |
| 264 | ovf = v_dec_u32le_pay pb 4 |
| 265 | if eq ovf V_OVF_DEFAULT |
| 266 | put vsem_status 0 -2 |
| 267 | vsem_set_reason vsem_m_ovf |
| 268 | ret -2 |
| 269 | end |
| 270 | put vsem_shape 0 3 |
| 271 | else |
| 272 | if ge pl 161 |
| 273 | put vsem_shape 0 4 |
| 274 | else |
| 275 | put vsem_status 0 -2 |
| 276 | vsem_set_reason vsem_m_plen |
| 277 | ret -2 |
| 278 | end |
| 279 | end |
| 280 | else |
| 281 | put vsem_shape 0 5 |
| 282 | end |
| 283 | end |
| 284 | end |
| 285 | vsem_count_dangling idx |
| 286 | ret 0 |
| 287 | end |
| 288 | |
| 289 | fn vsem_format_text cap ulong to long |
| 290 | ob_reset cap |
| 291 | idx = get vsem_oidx 0 |
| 292 | ob_puts vsem_f_id |
| 293 | ob_puts vt_sha256_pfx |
| 294 | ob_put_hex_id32 mul idx 32 1 |
| 295 | ob_putc uchar 10 |
| 296 | ob_puts vsem_f_kind |
| 297 | v_put_kind_name get v_og_kind idx |
| 298 | ob_putc uchar 10 |
| 299 | if ne get vsem_status 0 0 |
| 300 | ob_puts vsem_f_malf1 |
| 301 | rl = get vsem_rlen 0 |
| 302 | for i ulong rl bound 128 |
| 303 | ob_putc get vsem_reason i |
| 304 | end |
| 305 | ob_puts vsem_f_malf2 |
| 306 | else |
| 307 | ob_puts vsem_f_sh |
| 308 | sh = get vsem_shape 0 |
| 309 | vsem_put_shape sh |
| 310 | ob_putc uchar 10 |
| 311 | pb = mul idx V_MAX_PAYLOAD |
| 312 | if eq sh 1 |
| 313 | ob_puts vsem_f_ty |
| 314 | vsem_put_type_str idx |
| 315 | ob_putc uchar 10 |
| 316 | ob_puts vsem_f_tag |
| 317 | ob_put_u64 v_dec_u32le_pay pb 0 |
| 318 | ob_putc uchar 10 |
| 319 | ob_puts vsem_f_wid |
| 320 | ob_put_u64 v_dec_u16le_pay pb 4 |
| 321 | ob_putc uchar 10 |
| 322 | end |
| 323 | if eq sh 2 |
| 324 | ev = olw_eval_obj_u64 idx 0 |
| 325 | if eq ev 0 |
| 326 | ob_puts vsem_f_val |
| 327 | if ne get olw_val_is_bool 0 0 |
| 328 | if ne get olw_val_out 0 0 |
| 329 | ob_puts vsem_f_true |
| 330 | else |
| 331 | ob_puts vsem_f_false |
| 332 | end |
| 333 | else |
| 334 | ob_put_u64 get olw_val_out 0 |
| 335 | end |
| 336 | ob_putc uchar 10 |
| 337 | end |
| 338 | end |
| 339 | if eq sh 3 |
| 340 | ob_puts vsem_f_opc |
| 341 | vsem_put_opcode v_dec_u32le_pay pb 0 |
| 342 | ob_putc uchar 10 |
| 343 | ob_puts vsem_f_ovf |
| 344 | vsem_put_ovf v_dec_u32le_pay pb 4 |
| 345 | ob_putc uchar 10 |
| 346 | end |
| 347 | if eq sh 4 |
| 348 | ob_puts vsem_f_ops |
| 349 | ob_put_u64 ulong get v_og_pay add pb 32 |
| 350 | ob_putc uchar 10 |
| 351 | end |
| 352 | if gt get vsem_dangling 0 0 |
| 353 | ob_puts vsem_f_d1 |
| 354 | ob_put_u64 get vsem_dangling 0 |
| 355 | ob_puts vsem_f_d2 |
| 356 | end |
| 357 | end |
| 358 | if ne get v_ob_ovf 0 0 |
| 359 | ret -1 |
| 360 | end |
| 361 | ret long get v_ob_len 0 |
| 362 | end |
| 363 | |
| 364 | fn vsem_format_json cap ulong to long |
| 365 | ob_reset cap |
| 366 | idx = get vsem_oidx 0 |
| 367 | ob_puts vsem_j1 |
| 368 | ob_puts vt_sha256_pfx |
| 369 | ob_put_hex_id32 mul idx 32 1 |
| 370 | ob_puts vsem_j2 |
| 371 | v_put_kind_name get v_og_kind idx |
| 372 | ob_puts vsem_j3 |
| 373 | if ne get vsem_status 0 0 |
| 374 | ob_puts vsem_j4 |
| 375 | else |
| 376 | ob_puts vsem_j5 |
| 377 | vsem_put_shape get vsem_shape 0 |
| 378 | ob_puts vsem_j6 |
| 379 | ob_put_u64 get vsem_canon_len 0 |
| 380 | ob_puts vsem_j7 |
| 381 | end |
| 382 | if ne get v_ob_ovf 0 0 |
| 383 | ret -1 |
| 384 | end |
| 385 | ret long get v_ob_len 0 |
| 386 | end |
| 387 | |
| 388 | fn vsem_graph_dfs idx ulong depth ulong to void |
| 389 | if gt depth 16 |
| 390 | ret |
| 391 | end |
| 392 | if ne get vsem_seen idx uchar 0 |
| 393 | ret |
| 394 | end |
| 395 | put vsem_seen idx uchar 1 |
| 396 | for d ulong depth bound 16 |
| 397 | ob_puts vsem_sp2 |
| 398 | end |
| 399 | v_put_kind_name get v_og_kind idx |
| 400 | ob_puts vsem_sha_sp |
| 401 | ob_put_hex_id32 mul idx 32 1 |
| 402 | ob_putc uchar 10 |
| 403 | rc = get v_og_rcnt idx |
| 404 | for r ulong rc bound 4 |
| 405 | rslot = add mul idx V_MAX_RELS r |
| 406 | tb = mul rslot 32 |
| 407 | for i ulong 32 |
| 408 | put v_tmp_id i get v_og_rtgt add tb i |
| 409 | end |
| 410 | ti = og_find_by_tmp |
| 411 | if ge ti 0 |
| 412 | vsem_graph_dfs ulong ti add depth 1 |
| 413 | else |
| 414 | for d ulong add depth 1 bound 17 |
| 415 | ob_puts vsem_sp2 |
| 416 | end |
| 417 | ob_puts vsem_m_miss |
| 418 | ob_put_hex_id32 0 0 |
| 419 | ob_putc uchar 10 |
| 420 | end |
| 421 | end |
| 422 | k = get v_og_kind idx |
| 423 | pb = mul idx V_MAX_PAYLOAD |
| 424 | if eq k V_KIND_VALUE |
| 425 | for i ulong 32 |
| 426 | put v_tmp_id i get v_og_pay add pb i |
| 427 | end |
| 428 | ti = og_find_by_tmp |
| 429 | if ge ti 0 |
| 430 | vsem_graph_dfs ulong ti add depth 1 |
| 431 | end |
| 432 | end |
| 433 | if and eq k V_KIND_OP eq get v_og_plen idx 161 |
| 434 | for i ulong 32 |
| 435 | put v_tmp_id i get v_og_pay add pb i |
| 436 | end |
| 437 | oi = og_find_by_tmp |
| 438 | if ge oi 0 |
| 439 | vsem_graph_dfs ulong oi add depth 1 |
| 440 | end |
| 441 | for i ulong 32 |
| 442 | put v_tmp_id i get v_og_pay add add pb 33 i |
| 443 | end |
| 444 | a1 = og_find_by_tmp |
| 445 | if ge a1 0 |
| 446 | vsem_graph_dfs ulong a1 add depth 1 |
| 447 | end |
| 448 | for i ulong 32 |
| 449 | put v_tmp_id i get v_og_pay add add pb 65 i |
| 450 | end |
| 451 | a2 = og_find_by_tmp |
| 452 | if ge a2 0 |
| 453 | vsem_graph_dfs ulong a2 add depth 1 |
| 454 | end |
| 455 | end |
| 456 | end |
| 457 | |
| 458 | fn vsem_graph_text root ulong cap ulong to long |
| 459 | if ge root get v_og_cnt 0 |
| 460 | ret -1 |
| 461 | end |
| 462 | for i ulong 256 |
| 463 | put vsem_seen i uchar 0 |
| 464 | end |
| 465 | ob_reset cap |
| 466 | vsem_graph_dfs root 0 |
| 467 | if ne get v_ob_ovf 0 0 |
| 468 | ret -1 |
| 469 | end |
| 470 | ret long get v_ob_len 0 |
| 471 | end |