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 / model / world_explore.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * world_explore.c -- breadth-first search over every reachable model state |
| 19 | * (bounded only by WM_EXT_MAX outside publications and WM_TICK_MAX clock |
| 20 | * steps). Breadth-first, so the first trace found for a violation is a |
| 21 | * shortest one. |
| 22 | */ |
| 23 | #include "world_explore.h" |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | |
| 29 | #define KEY_MAX 48 |
| 30 | #define TABLE_BITS 20u |
| 31 | #define TABLE_SIZE (1u << TABLE_BITS) |
| 32 | |
| 33 | typedef struct { |
| 34 | WmState s; |
| 35 | uint32_t parent; |
| 36 | WmOp op; |
| 37 | uint8_t key[KEY_MAX]; |
| 38 | uint8_t klen; |
| 39 | } Node; |
| 40 | |
| 41 | int wx_bit_index(uint32_t bit) { |
| 42 | for (int i = 0; i < 5; i++) if (bit == (1u << i)) return i; |
| 43 | return -1; |
| 44 | } |
| 45 | |
| 46 | static uint64_t fnv(const uint8_t *k, int n) { |
| 47 | uint64_t h = 1469598103934665603ull; |
| 48 | for (int i = 0; i < n; i++) { h ^= k[i]; h *= 1099511628211ull; } |
| 49 | return h; |
| 50 | } |
| 51 | |
| 52 | static int trace_of(const Node *nodes, uint32_t idx, WmOp *out) { |
| 53 | WmOp rev[WX_MAX_TRACE]; |
| 54 | int n = 0; |
| 55 | while (idx != 0 && n < WX_MAX_TRACE) { rev[n++] = nodes[idx].op; idx = nodes[idx].parent; } |
| 56 | for (int i = 0; i < n; i++) out[i] = rev[n - 1 - i]; |
| 57 | return n; |
| 58 | } |
| 59 | |
| 60 | int wx_explore(const WmProfile *p, WxResult *res) { |
| 61 | memset(res, 0, sizeof *res); |
| 62 | for (int i = 0; i < 5; i++) res->trace_len[i] = -1; |
| 63 | uint32_t cap = TABLE_SIZE / 2; |
| 64 | Node *nodes = calloc(cap, sizeof *nodes); |
| 65 | uint32_t *table = malloc(sizeof(uint32_t) * TABLE_SIZE); |
| 66 | if (!nodes || !table) { free(nodes); free(table); return -1; } |
| 67 | memset(table, 0xff, sizeof(uint32_t) * TABLE_SIZE); |
| 68 | uint32_t n = 0; |
| 69 | wm_init(&nodes[0].s); |
| 70 | nodes[0].klen = (uint8_t)wm_key(&nodes[0].s, nodes[0].key); |
| 71 | table[fnv(nodes[0].key, nodes[0].klen) & (TABLE_SIZE - 1)] = 0; |
| 72 | n = 1; |
| 73 | WmOp ops[16]; |
| 74 | int nops = wm_all_ops(ops); |
| 75 | for (uint32_t head = 0; head < n; head++) { |
| 76 | for (int o = 0; o < nops; o++) { |
| 77 | if (!wm_enabled(p, &nodes[head].s, ops[o])) continue; |
| 78 | WmState t = nodes[head].s; |
| 79 | uint32_t v = wm_step(p, &t, ops[o]); |
| 80 | res->transitions++; |
| 81 | uint8_t key[KEY_MAX]; |
| 82 | int kl = wm_key(&t, key); |
| 83 | uint64_t h = fnv(key, kl); |
| 84 | uint32_t slot = (uint32_t)(h & (TABLE_SIZE - 1)); |
| 85 | int found = 0; |
| 86 | while (table[slot] != UINT32_MAX) { |
| 87 | const Node *q = &nodes[table[slot]]; |
| 88 | if (q->klen == kl && memcmp(q->key, key, (size_t)kl) == 0) { found = 1; break; } |
| 89 | slot = (slot + 1) & (TABLE_SIZE - 1); |
| 90 | } |
| 91 | uint32_t at; |
| 92 | if (found) { |
| 93 | at = UINT32_MAX; |
| 94 | } else { |
| 95 | if (n >= cap) { res->truncated = 1; continue; } |
| 96 | at = n++; |
| 97 | nodes[at].s = t; |
| 98 | nodes[at].parent = head; |
| 99 | nodes[at].op = ops[o]; |
| 100 | memcpy(nodes[at].key, key, (size_t)kl); |
| 101 | nodes[at].klen = (uint8_t)kl; |
| 102 | table[slot] = at; |
| 103 | } |
| 104 | if (!v) continue; |
| 105 | res->viol_seen |= v; |
| 106 | for (int b = 0; b < 5; b++) { |
| 107 | if (!(v & (1u << b)) || res->trace_len[b] >= 0) continue; |
| 108 | /* Path to head, then this op. */ |
| 109 | int len = trace_of(nodes, head, res->trace[b]); |
| 110 | if (len < WX_MAX_TRACE) res->trace[b][len++] = ops[o]; |
| 111 | res->trace_len[b] = len; |
| 112 | } |
| 113 | (void)at; |
| 114 | } |
| 115 | } |
| 116 | res->states = n; |
| 117 | free(nodes); |
| 118 | free(table); |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | void wx_print_trace(const WmOp *t, int n) { |
| 123 | for (int i = 0; i < n; i++) { |
| 124 | char b[32]; |
| 125 | wm_op_str(t[i], b, sizeof b); |
| 126 | printf("%s%s", i ? " -> " : "", b); |
| 127 | } |
| 128 | } |