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 / test_omega_program_ir.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 | /* test_omega_program_ir.c: the canonical program IR and the id recomputation behind the |
| 18 | * mandatory source/IR recheck (VC1 stage 6, SPEC 6 step 3). Each guard in |
| 19 | * src/omega_program_ir.c carries a VC1I tag; the Makefile target test-program-ir breaks each tag |
| 20 | * in a copy and requires the named check to FAIL. CPU only. */ |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | |
| 25 | #include "omega_program.h" |
| 26 | #include "omega_program_ir.h" |
| 27 | |
| 28 | static int g_total, g_failed; |
| 29 | static void check(const char *name, int ok) { |
| 30 | printf("%s %s\n", ok ? "PASS" : "FAIL", name); |
| 31 | g_total++; |
| 32 | if (!ok) g_failed++; |
| 33 | } |
| 34 | static OmegaProgram *newprog(int op, uint64_t imm) { |
| 35 | OmegaProgram *p = calloc(1, sizeof *p); |
| 36 | if (!p || omega_program_build_unary_op(p, "ir-test", op, imm) != 0) { fprintf(stderr, "setup failed\n"); exit(2); } |
| 37 | return p; |
| 38 | } |
| 39 | /* fixed offsets of the layout in omega_program_ir.h */ |
| 40 | enum { OFF_IN_TYPE = 24, OFF_STEPS = 24 + 1 + 2 + 1 + 2 + 32 + 32, OFF_FIRST_STEP = OFF_STEPS + 2 }; |
| 41 | |
| 42 | static void t_roundtrip(void) { |
| 43 | static const int ops[5] = { OP_ADD, OP_SUB, OP_MUL, OP_AND, OP_OR }; |
| 44 | static const uint64_t imms[4] = { 0, 1, 0x01234567ULL, 0xffffffffULL }; |
| 45 | int all = 1, same_id = 1, canon = 1, distinct = 1; |
| 46 | uint8_t seen[20][32]; int ns = 0; |
| 47 | for (int a = 0; a < 5; a++) |
| 48 | for (int b = 0; b < 4; b++) { |
| 49 | OmegaProgram *p = newprog(ops[a], imms[b]); |
| 50 | uint8_t *ir = NULL; size_t n = 0; |
| 51 | OmegaProgram *q = calloc(1, sizeof *q); |
| 52 | if (omega_program_ir_encode(p, &ir, &n) != 0) { all = 0; goto next; } |
| 53 | if (omega_program_ir_decode(ir, n, q) != 0) { all = 0; goto next; } |
| 54 | if (memcmp(q->program_id.bytes, p->program_id.bytes, 32) != 0) same_id = 0; |
| 55 | uint8_t id[32]; |
| 56 | if (omega_program_ir_recompute_id(ir, n, id) != 0 || memcmp(id, p->program_id.bytes, 32) != 0) same_id = 0; |
| 57 | uint8_t *ir2 = NULL; size_t n2 = 0; |
| 58 | if (omega_program_ir_encode(q, &ir2, &n2) != 0 || n2 != n || memcmp(ir, ir2, n) != 0) canon = 0; |
| 59 | free(ir2); |
| 60 | for (int k = 0; k < ns; k++) if (memcmp(seen[k], id, 32) == 0) distinct = 0; |
| 61 | memcpy(seen[ns++], id, 32); |
| 62 | next: |
| 63 | free(ir); free(q); free(p); |
| 64 | } |
| 65 | check("ir-roundtrips-every-op-and-immediate", all); |
| 66 | check("ir-recomputes-the-program-id-exactly", same_id); |
| 67 | check("ir-encoding-is-canonical-decode-then-encode-is-identical", canon); |
| 68 | check("ir-different-programs-have-different-recomputed-ids", distinct); |
| 69 | } |
| 70 | |
| 71 | static void t_refusals(void) { |
| 72 | OmegaProgram *p = newprog(OP_ADD, 17); |
| 73 | uint8_t *ir = NULL; size_t n = 0; |
| 74 | if (omega_program_ir_encode(p, &ir, &n) != 0) { fprintf(stderr, "setup failed\n"); exit(2); } |
| 75 | OmegaProgram *q = calloc(1, sizeof *q); |
| 76 | uint8_t id[32]; |
| 77 | |
| 78 | check("ir-has-the-documented-length-for-one-step", n == (size_t)OFF_FIRST_STEP + 9); |
| 79 | check("ir-refuses-null-arguments", omega_program_ir_decode(NULL, n, q) != 0 && omega_program_ir_decode(ir, n, NULL) != 0 && omega_program_ir_recompute_id(NULL, 0, id) != 0); |
| 80 | |
| 81 | int all = 1; |
| 82 | for (size_t k = 0; k < n; k++) { /* an exact-size heap copy, so a read past the end is a sanitizer error, not a silent success */ |
| 83 | uint8_t *cut = malloc(k ? k : 1); |
| 84 | if (!cut) { fprintf(stderr, "setup failed\n"); exit(2); } |
| 85 | memcpy(cut, ir, k); |
| 86 | if (omega_program_ir_decode(cut, k, q) == 0) all = 0; |
| 87 | free(cut); |
| 88 | } |
| 89 | check("ir-refuses-truncation-at-every-length", all); |
| 90 | |
| 91 | uint8_t *t = malloc(n + 1); memcpy(t, ir, n); t[n] = 0; |
| 92 | check("ir-refuses-trailing-bytes", omega_program_ir_decode(t, n + 1, q) != 0 && omega_program_ir_decode(ir, n, q) == 0); |
| 93 | |
| 94 | memcpy(t, ir, n); t[0] ^= 1; |
| 95 | check("ir-refuses-wrong-tag", omega_program_ir_decode(t, n, q) != 0); |
| 96 | |
| 97 | /* 65 steps, all of them present: the limit refuses, not truncation */ |
| 98 | { |
| 99 | size_t m = (size_t)OFF_STEPS + 2 + 65 * 9; |
| 100 | uint8_t *big = calloc(1, m); |
| 101 | memcpy(big, ir, OFF_STEPS); |
| 102 | big[OFF_STEPS] = 0; big[OFF_STEPS + 1] = 65; |
| 103 | for (int i = 0; i < 65; i++) big[OFF_FIRST_STEP + 9 * i] = OP_ADD; |
| 104 | check("ir-refuses-more-steps-than-the-program-limit", omega_program_ir_decode(big, m, q) != 0); |
| 105 | free(big); |
| 106 | } |
| 107 | /* far over the limit with all the data present: the decoder must stop before it writes past its output */ |
| 108 | { |
| 109 | size_t m = (size_t)OFF_STEPS + 2 + 4000 * 9; |
| 110 | uint8_t *big = calloc(1, m); |
| 111 | memcpy(big, ir, OFF_STEPS); |
| 112 | big[OFF_STEPS] = (uint8_t)(4000 >> 8); big[OFF_STEPS + 1] = (uint8_t)(4000 & 255); |
| 113 | for (int i = 0; i < 4000; i++) big[OFF_FIRST_STEP + 9 * i] = OP_ADD; |
| 114 | check("ir-refuses-a-huge-step-count-without-writing-past-its-output", omega_program_ir_decode(big, m, q) != 0); |
| 115 | free(big); |
| 116 | } |
| 117 | |
| 118 | /* an input type the program id does not accept: refused, never a made-up id */ |
| 119 | memcpy(t, ir, n); t[OFF_IN_TYPE] = 0xEE; |
| 120 | check("ir-refuses-a-type-the-program-id-cannot-take", omega_program_ir_decode(t, n, q) != 0); |
| 121 | |
| 122 | /* injectivity: flipping any single bit either refuses or changes the id (one program, one byte string) */ |
| 123 | int inj = 1; |
| 124 | for (size_t i = 0; i < n; i++) |
| 125 | for (int b = 0; b < 8; b++) { |
| 126 | memcpy(t, ir, n); t[i] ^= (uint8_t)(1u << b); |
| 127 | if (omega_program_ir_recompute_id(t, n, id) == 0 && memcmp(id, p->program_id.bytes, 32) == 0) inj = 0; |
| 128 | } |
| 129 | check("ir-no-single-bit-change-keeps-the-same-id", inj); |
| 130 | |
| 131 | check("ir-encode-refuses-a-program-with-no-body", ({ OmegaProgram *e = calloc(1, sizeof *e); uint8_t *o = NULL; size_t l = 0; int r = omega_program_ir_encode(e, &o, &l); free(e); r != 0; })); |
| 132 | free(t); free(q); free(ir); free(p); |
| 133 | } |
| 134 | |
| 135 | int main(void) { |
| 136 | t_roundtrip(); |
| 137 | t_refusals(); |
| 138 | printf("%d checks, %d failed\n", g_total, g_failed); |
| 139 | if (g_failed) return 1; |
| 140 | printf("test-program-ir: PASS\n"); |
| 141 | return 0; |
| 142 | } |