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 / seed / qualification / om2 / oracle / s5s7 / abi / driver.c

167 lines · 8839 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * OM-2 qualification, lane s5s7 (#748, ledger L836): ABI v2 ORACLE driver for contract section 6
3 * (spec/omegese-om2.md lines 180-210) and errata E1/E3. Outside the delivered closure.
4 *
5 * Written from the contract only, against the API it describes (ASSUMPTIONS, README A10-A13):
6 * uint32_t omega2_<export>(uint64_t *result, ...) v1 shape (omega-bridge docs/NATIVE-ABI.md)
7 * unsized view/edit -> (const void *ptr, uint64_t len) / (void *ptr, uint64_t len), len in elements
8 * layout, out -> one pointer, no count (contract 156-157, erratum E1)
9 * const char *omega2_ident_<unit>(void) NUL-terminated text holding the source
10 * sha256, the omc pin and "OM-2" (E1)
11 * Units: sec/s5s7/abi/abiv.om (unit abiv), abiw.om (unit abiw), abi1.om (OM-1, v1 omega_madd1).
12 * Expected values are computed here in C from the unit's stated semantics, never from Omega.
13 * Usage: driver SHA_ABIV SHA_ABIW OMC_PIN (hex digests computed by /usr/bin/sha256sum)
14 * Output: one PASS/FAIL line per case id (ids match candidate.cases abi-* rows); exit 0 iff no FAIL. */
15#include <inttypes.h>
16#include <signal.h>
17#include <stdio.h>
18#include <string.h>
19#include <sys/time.h>
20
21uint32_t omega2_vsum(uint64_t *result, const void *v, uint64_t v_len);
22uint32_t omega2_bcnt(uint64_t *result, const void *b, uint64_t b_len);
23uint32_t omega2_vcnt(uint64_t *result, const void *v, uint64_t v_len);
24uint32_t omega2_ecpy(uint64_t *result, void *d, uint64_t d_len, const void *s, uint64_t s_len);
25uint32_t omega2_rev(uint64_t *result, const void *s, uint64_t s_len, void *r);
26uint32_t omega2_revtrap(uint64_t *result, const void *s, uint64_t s_len, void *r);
27uint32_t omega2_lay(uint64_t *result, void *p, uint64_t v);
28uint32_t omega2_spin(uint64_t *result, uint64_t n);
29uint32_t omega2_madd2(uint64_t *result, uint64_t a, uint64_t b);
30uint32_t omega_madd1(uint64_t *result, uint64_t a, uint64_t b);
31const char *omega2_ident_abiv(void);
32const char *omega2_ident_abiw(void);
33
34#define SENT UINT64_C(0x5555555555555555)
35static int fails;
36static void check(const char *id, int ok, const char *what)
37{
38 printf("%s %s %s\n", ok ? "PASS" : "FAIL", id, what);
39 if (!ok)
40 fails++;
41}
42
43static volatile uint32_t inner = 99;
44static void on_alarm(int sig)
45{
46 uint64_t r = SENT;
47 uint8_t b[2] = {0, 0};
48 (void)sig;
49 if (inner != 33)
50 inner = omega2_bcnt(&r, b, 2);
51}
52
53static int ident_ok(const char *s, const char *src, const char *pin)
54{
55 return s && strstr(s, src) && strstr(s, pin) && strstr(s, "OM-2") && !strstr(s, "OM-1");
56}
57
58int main(int argc, char **argv)
59{
60 static uint64_t w[8] __attribute__((aligned(16)));
61 static uint8_t buf[64] __attribute__((aligned(16)));
62 uint64_t r;
63 uint32_t st;
64 int i;
65
66 if (argc != 4) {
67 fprintf(stderr, "usage: driver SHA_ABIV SHA_ABIW OMC_PIN\n");
68 return 90;
69 }
70 /* success baselines */
71 w[0] = 1; w[1] = 2; w[2] = 3;
72 r = SENT; st = omega2_vsum(&r, w, 3);
73 check("abi-vsum-ok", st == 0 && r == 6, "vsum {1,2,3} = 6");
74 r = SENT; st = omega2_bcnt(&r, buf, 64);
75 check("abi-bcnt-ok", st == 0 && r == 64, "bcnt len 64");
76 /* status 32 refusals (contract 187-193) */
77 r = SENT; st = omega2_bcnt(&r, NULL, 1);
78 check("abi-null-len1", st == 32 && r == SENT, "len 1, ptr NULL -> 32, result untouched");
79 r = SENT; st = omega2_vcnt(&r, (const uint8_t *)w + 1, 1);
80 check("abi-misaligned", st == 32 && r == SENT, "ulong view at w+1 byte -> 32 (mutant: 0, result 1)");
81 r = SENT; st = omega2_vcnt(&r, w, UINT64_C(1) << 61);
82 check("abi-size-overflow", st == 32 && r == SENT, "ulong len 2^61: len*8 overflows -> 32 (mutant: 0, result 2^61)");
83 r = SENT; st = omega2_bcnt(&r, buf, UINT64_MAX);
84 check("abi-golden-wrap", st == 32 && r == SENT, "uchar len 2^64-1 at buf: ptr+len wraps -> 32 (#748 golden extent)");
85 r = SENT; st = omega2_bcnt(&r, (const void *)(uintptr_t)(UINTPTR_MAX - 7), 16);
86 check("abi-wrap-high", st == 32 && r == SENT, "ptr 2^64-8, len 16 wraps -> 32");
87 /* empty views (contract 193-194) */
88 r = SENT; st = omega2_bcnt(&r, NULL, 0);
89 check("abi-null-len0", st == 0 && r == 0, "len 0, ptr NULL -> empty view, status 0");
90 r = SENT; st = omega2_vsum(&r, NULL, 0);
91 check("abi-null-len0-ulong", st == 0 && r == 0, "ulong len 0, NULL -> sum 0");
92 r = SENT; st = omega2_bcnt(&r, (const void *)(uintptr_t)1, 0);
93 check("abi-bogus-len0", st == 0 && r == 0, "uchar len 0, ptr 1 -> empty view");
94 /* edit overlap (contract 192-193) */
95 for (i = 0; i < 16; i++)
96 buf[i] = (uint8_t)(i + 1);
97 r = SENT; st = omega2_ecpy(&r, buf, 8, buf + 4, 8);
98 check("abi-edit-overlap", st == 32 && r == SENT && buf[0] == 1 && buf[7] == 8,
99 "edit [0,8) overlaps view [4,12) -> 32, nothing written");
100 r = SENT; st = omega2_ecpy(&r, buf, 4, buf + 4, 4);
101 check("abi-edit-adjacent", st == 0 && r == 4 && buf[0] == 5 && buf[3] == 8,
102 "edit [0,4) and view [4,8) are disjoint (half-open) -> copy");
103 r = SENT; st = omega2_ecpy(&r, buf + 2, 0, buf, 8);
104 check("abi-edit-empty", st == 0 && r == 0, "empty edit inside a view overlaps nothing");
105 r = SENT; st = omega2_ecpy((uint64_t *)(void *)(buf + 8), buf, 16, buf + 32, 4);
106 check("abi-edit-result", st == 32, "result buffer inside the edit range -> 32");
107 /* out: memmove semantics, untouched on trap (contract 196-198) */
108 buf[0] = 1; buf[1] = 2; buf[2] = 3; buf[3] = 4;
109 r = SENT; st = omega2_rev(&r, buf, 4, buf);
110 check("abi-out-overlap", st == 0 && buf[0] == 4 && buf[1] == 3 && buf[2] == 2 && buf[3] == 1,
111 "out overlapping its input: computed from the original input (4,3,2,1; direct writes give 4,3,3,4)");
112 memset(buf + 16, 0xAA, 4);
113 buf[0] = 1; buf[1] = 2; buf[2] = 3; buf[3] = 4;
114 r = SENT; st = omega2_revtrap(&r, buf, 4, buf + 16);
115 check("abi-out-trap", st == 9 && r == SENT && buf[16] == 0xAA && buf[19] == 0xAA,
116 "trap 9 after writing out: status 9, out and result untouched");
117 memset(buf + 16, 0xAA, 4);
118 r = SENT; st = omega2_rev(&r, buf, 3, buf + 16);
119 check("abi-out-bounds", st == 3 && buf[16] == 0xAA, "len 3 view read at index 3 -> trap 3, out untouched");
120 /* layout parameter: one pointer, fixed SIZE 16 (contract 156-158) */
121 memset(buf, 0, 16);
122 r = SENT; st = omega2_lay(&r, buf, UINT64_C(0x0102030405060708));
123 {
124 static const uint8_t want[16] = {8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8};
125 check("abi-layout", st == 0 && memcmp(buf, want, 16) == 0, "lo little-endian at 0, hi big-endian at 8");
126 }
127 r = SENT; st = omega2_lay(&r, NULL, 1);
128 check("abi-layout-null", st == 32, "layout pointer NULL -> 32 (v1 null-pointer rule)");
129 r = SENT; st = omega2_madd2(NULL, 1, 2);
130 check("abi-null-result", st == 32, "result pointer NULL -> 32 (v1 rule kept)");
131 /* status 33 reentry (contract 195) */
132 {
133 struct sigaction sa;
134 struct itimerval it;
135 memset(&sa, 0, sizeof sa);
136 sa.sa_handler = on_alarm;
137 sigaction(SIGALRM, &sa, NULL);
138 memset(&it, 0, sizeof it);
139 /* A dynamic Omega loop is bounded to 1000000 iterations. A recurring
140 * alarm reaches the legal workload even if the first signal arrives
141 * before bridge entry; the handler latches only an actual status 33. */
142 it.it_value.tv_usec = 100;
143 it.it_interval.tv_usec = 100;
144 setitimer(ITIMER_REAL, &it, NULL);
145 r = SENT; st = omega2_spin(&r, UINT64_C(1000000));
146 memset(&it, 0, sizeof it);
147 setitimer(ITIMER_REAL, &it, NULL);
148 check("abi-reentry", st == 0 && inner == 33, "bcnt from SIGALRM during spin -> 33; spin still 0");
149 }
150 /* two units in one binary, interleaved (contract 207-210); OM-1 unit stays v1 (E1) */
151 {
152 uint64_t a = SENT, b = SENT, c = SENT, a1;
153 uint32_t s1 = omega2_madd2(&a, 5, 7), s2, s3, s4;
154 a1 = a;
155 s2 = omega2_bcnt(&b, buf, 5);
156 s3 = omega_madd1(&c, 5, 7);
157 s4 = omega2_madd2(&a, 6, 1);
158 check("abi-two-units", s1 == 0 && s2 == 0 && s3 == 0 && s4 == 0 && a1 == 17 && a == 13 && b == 5 && c == 22,
159 "abiw, abiv and the OM-1 unit interleaved: 5*2+7=17 then 6*2+1=13, len 5, 5*3+7=22");
160 }
161 /* identities (erratum E1) */
162 check("abi-ident-abiv", ident_ok(omega2_ident_abiv(), argv[1], argv[3]), "abiv: source sha256, omc pin, OM-2");
163 check("abi-ident-abiw", ident_ok(omega2_ident_abiw(), argv[2], argv[3]), "abiw: source sha256, omc pin, OM-2");
164 check("abi-ident-distinct", strcmp(omega2_ident_abiv(), omega2_ident_abiw()) != 0, "per-unit identities differ");
165 printf("%s ABI v2 driver: %d failure(s)\n", fails ? "FAIL" : "PASS", fails);
166 return fails ? 1 : 0;
167}