interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / omega_validate.c

239 lines · 10741 bytes Raw Blame History
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/* Semantic object and graph validator backed by pure seeded OM-2 (om/visor/validate_om2.om). */
18#include "omega_validate.h"
19#include "omega_core.h"
20#include "omega_canonical.h"
21#include "omega_native.h"
22#include <string.h>
23#include <stdio.h>
24#include <stdlib.h>
25
26static const OmegaObject* find_object_by_id(const OmegaGraph *graph, const SemanticId *id) {
27 if (!graph || !id) return NULL;
28 for (uint16_t i = 0; i < graph->object_count; ++i) {
29 if (graph->objects[i].has_id && omega_compare_semantic_id(&graph->objects[i].id, id) == 0) {
30 return &graph->objects[i];
31 }
32 }
33 return NULL;
34}
35
36int omega_validate_object(const OmegaGraph *graph, const OmegaObject *obj, char *err_msg, size_t err_msg_len) {
37 uint64_t r = 0;
38 if (!obj) {
39 snprintf(err_msg, err_msg_len, "Null object pointer");
40 return -1;
41 }
42 if (omega2_om_val_kind(&r, (uint64_t)obj->kind) != OMEGA_OK) abort();
43 if ((int64_t)r != 0) {
44 snprintf(err_msg, err_msg_len, "Invalid semantic kind tag 0x%02x", obj->kind);
45 return -1;
46 }
47
48 for (uint16_t i = 0; i < obj->attr_count; ++i) {
49 if (strlen(obj->attributes[i].key) == 0) {
50 snprintf(err_msg, err_msg_len, "Empty attribute key at index %u", i);
51 return -1;
52 }
53 }
54
55 for (uint16_t i = 0; i < obj->rel_count; ++i) {
56 if (omega2_om_val_rel_kind(&r, (uint64_t)obj->relations[i].kind) != OMEGA_OK) abort();
57 if ((int64_t)r != 0) {
58 snprintf(err_msg, err_msg_len, "Invalid relation kind 0x%04x at index %u", obj->relations[i].kind, i);
59 return -1;
60 }
61 if (graph) {
62 const OmegaObject *target = find_object_by_id(graph, &obj->relations[i].target_id);
63 if (!target) {
64 char hex[65];
65 omega_hex_semantic_id(&obj->relations[i].target_id, hex);
66 snprintf(err_msg, err_msg_len, "Dangling relation target reference %s", hex);
67 return -1;
68 }
69 }
70 }
71
72 for (uint16_t i = 0; i < obj->const_count; ++i) {
73 if (omega2_om_val_const_kind(&r, (uint64_t)obj->constraints[i].kind) != OMEGA_OK) abort();
74 if ((int64_t)r != 0) {
75 snprintf(err_msg, err_msg_len, "Invalid constraint kind 0x%04x at index %u", obj->constraints[i].kind, i);
76 return -1;
77 }
78 }
79
80 switch (obj->kind) {
81 case KIND_TYPE: {
82 if (obj->payload_len < sizeof(TypePayload)) {
83 snprintf(err_msg, err_msg_len, "Type payload length too short (%u bytes)", obj->payload_len);
84 return -1;
85 }
86 const TypePayload *tp = (const TypePayload*)obj->payload;
87 if (omega2_om_val_type_fields(&r, (uint64_t)tp->tag, (uint64_t)tp->width, (uint64_t)tp->length) != OMEGA_OK) abort();
88 switch ((int64_t)r) {
89 case 0: break;
90 case 1: snprintf(err_msg, err_msg_len, "Invalid type tag 0x%02x", tp->tag); return -1;
91 case 2: snprintf(err_msg, err_msg_len, "Unsigned integer width %u out of bounds [1..256]", tp->width); return -1;
92 case 3: snprintf(err_msg, err_msg_len, "Signed integer width %u out of bounds [2..256]", tp->width); return -1;
93 case 4: snprintf(err_msg, err_msg_len, "FP32 width %u is not 32", tp->width); return -1;
94 case 5: snprintf(err_msg, err_msg_len, "Bitvector width %u out of bounds [1..256]", tp->width); return -1;
95 case 6: snprintf(err_msg, err_msg_len, "Sequence length %u exceeds 65535", tp->length); return -1;
96 default: return -1;
97 }
98 if (tp->tag == TYPE_SEQUENCE && graph) {
99 const OmegaObject *elem_tp = find_object_by_id(graph, &tp->elem_type);
100 if (!elem_tp || elem_tp->kind != KIND_TYPE) {
101 snprintf(err_msg, err_msg_len, "Sequence element type unresolved or not KIND_TYPE");
102 return -1;
103 }
104 }
105 break;
106 }
107 case KIND_VALUE: {
108 if (obj->payload_len < sizeof(ValuePayload)) {
109 snprintf(err_msg, err_msg_len, "Value payload length too short (%u bytes)", obj->payload_len);
110 return -1;
111 }
112 const ValuePayload *vp = (const ValuePayload*)obj->payload;
113 if (graph) {
114 const OmegaObject *tp_obj = find_object_by_id(graph, &vp->type_id);
115 if (!tp_obj || tp_obj->kind != KIND_TYPE) {
116 snprintf(err_msg, err_msg_len, "Value type_id unresolved or not KIND_TYPE");
117 return -1;
118 }
119 const TypePayload *tp = (const TypePayload*)tp_obj->payload;
120 if (omega2_om_val_val_bytes(&r, (uint64_t)tp->tag, (uint64_t)tp->width, (uint64_t)vp->byte_len) != OMEGA_OK) abort();
121 if ((int64_t)r > 0) {
122 snprintf(err_msg, err_msg_len, "Value byte length %u does not match declared type width %u (expected %u bytes)",
123 vp->byte_len, tp->width, (unsigned)r);
124 return -1;
125 }
126 }
127 break;
128 }
129 case KIND_OPERATION: {
130 if (obj->payload_len >= sizeof(OperationPayload)) {
131 const OperationPayload *op = (const OperationPayload*)obj->payload;
132 if (omega2_om_val_op_fields(&r, (uint64_t)op->opcode, (uint64_t)op->overflow) != OMEGA_OK) abort();
133 if ((int64_t)r == 1) {
134 snprintf(err_msg, err_msg_len, "Invalid opcode 0x%02x", op->opcode);
135 return -1;
136 }
137 if ((int64_t)r == 2) {
138 snprintf(err_msg, err_msg_len, "Operation overflow policy not explicitly declared");
139 return -1;
140 }
141 } else if (obj->payload_len >= sizeof(ApplyPayload)) {
142 const ApplyPayload *app = (const ApplyPayload*)obj->payload;
143 if (graph) {
144 const OmegaObject *op_obj = find_object_by_id(graph, &app->op_id);
145 if (!op_obj || op_obj->kind != KIND_OPERATION) {
146 snprintf(err_msg, err_msg_len, "Apply op_id unresolved or not KIND_OPERATION");
147 return -1;
148 }
149 for (uint8_t i = 0; i < app->operand_count; ++i) {
150 const OmegaObject *arg = find_object_by_id(graph, &app->operands[i]);
151 if (!arg || (arg->kind != KIND_VALUE && arg->kind != KIND_OPERATION)) {
152 snprintf(err_msg, err_msg_len, "Apply operand %u unresolved or invalid kind", i);
153 return -1;
154 }
155 }
156 }
157 }
158 break;
159 }
160 case KIND_EFFECT: {
161 EffectPayload eff;
162 int erc = omega_effect_read(obj, &eff);
163 if (erc != OMEGA_EFFECT_OK) {
164 snprintf(err_msg, err_msg_len, "Effect payload refused: %s (%u bytes)",
165 omega_effect_strerror(erc), obj->payload_len);
166 return -1;
167 }
168 break;
169 }
170 default:
171 break;
172 }
173
174 return 0;
175}
176
177int omega_validate_graph(const OmegaGraph *graph, char *err_msg, size_t err_msg_len) {
178 uint64_t r = 0;
179 if (!graph) {
180 snprintf(err_msg, err_msg_len, "Null graph pointer");
181 return -1;
182 }
183 if (omega2_om_val_graph_count(&r, (uint64_t)graph->object_count, OMEGA_MAX_GRAPH_OBJECTS) != OMEGA_OK) abort();
184 if ((int64_t)r == 1) {
185 snprintf(err_msg, err_msg_len, "Empty graph (0 objects)");
186 return -1;
187 }
188 if ((int64_t)r == 2) {
189 snprintf(err_msg, err_msg_len, "Graph object count %u exceeds maximum %d",
190 graph->object_count, OMEGA_MAX_GRAPH_OBJECTS);
191 return -1;
192 }
193
194 for (uint16_t i = 0; i < graph->object_count; ++i) {
195 if (omega_validate_object(graph, &graph->objects[i], err_msg, err_msg_len) != 0) {
196 return -1;
197 }
198 }
199
200 /* Build adjacency list and run iterative 3-color DFS cycle detection in OM-2 */
201 static uint16_t edges[OMEGA_MAX_GRAPH_OBJECTS * 128];
202 uint16_t deg[OMEGA_MAX_GRAPH_OBJECTS];
203 memset(deg, 0, sizeof(deg));
204 for (uint16_t i = 0; i < graph->object_count; ++i) {
205 const OmegaObject *obj = &graph->objects[i];
206 uint16_t d = 0;
207 for (uint16_t ri = 0; ri < obj->rel_count && d < 128; ++ri) {
208 for (uint16_t j = 0; j < graph->object_count && d < 128; ++j) {
209 if (graph->objects[j].has_id && omega_compare_semantic_id(&graph->objects[j].id, &obj->relations[ri].target_id) == 0) {
210 edges[i * 128 + d++] = j;
211 }
212 }
213 }
214 if (obj->kind == KIND_OPERATION && obj->payload_len >= sizeof(ApplyPayload)) {
215 const ApplyPayload *app = (const ApplyPayload*)obj->payload;
216 for (uint8_t oi = 0; oi < app->operand_count && d < 128; ++oi) {
217 for (uint16_t j = 0; j < graph->object_count && d < 128; ++j) {
218 if (graph->objects[j].has_id && omega_compare_semantic_id(&graph->objects[j].id, &app->operands[oi]) == 0) {
219 edges[i * 128 + d++] = (uint16_t)((1U << 9) | j);
220 }
221 }
222 }
223 }
224 deg[i] = d;
225 }
226
227 uint64_t info[2] = {0, 0};
228 if (omega2_om_val_find_cycle(&r, edges, OMEGA_MAX_GRAPH_OBJECTS * 128, deg, OMEGA_MAX_GRAPH_OBJECTS, graph->object_count, info, 2) != OMEGA_OK) abort();
229 if ((int64_t)r != 0) {
230 if (info[1] == 0) {
231 snprintf(err_msg, err_msg_len, "Structural cycle detected involving relation target at object index %u", (unsigned)info[0]);
232 } else {
233 snprintf(err_msg, err_msg_len, "Structural cycle detected involving operand target at object index %u", (unsigned)info[0]);
234 }
235 return -1;
236 }
237
238 return 0;
239}