interplane.Get Omega
Read in your language

omega-bridge

Omega-first language interoperability: checked conversion backends and native language bindings

Brought by drakestapleton

branch main Branches Tags History

omega-bridge / tests / native / caller.c

167 lines · 9039 bytes Raw Blame History
1/* C caller for the omega-bridge native call ABI v1 test library (libomega_native.so).
2 * Expected values come from aien-calc / Wolfram receipts listed in docs/NATIVE-ABI.md, never from Omega. */
3#include <inttypes.h>
4#include <pthread.h>
5#include <signal.h>
6#include <stdio.h>
7#include <string.h>
8#include <sys/time.h>
9
10#include "omega_native.h" /* every Omega declaration: generated by tools/native, nothing typed by hand */
11
12#define SENTINEL UINT64_C(6148914691236517205)
13static int fails;
14static void check(const char *name, int ok)
15{
16 printf("%s %s\n", ok ? "PASS" : "FAIL", name);
17 if (!ok)
18 fails++;
19}
20
21/* ombr_probe(fn, result, a, b, c): set x19..x28 and d8..d15 to known values, call fn(result, a, b, c),
22 * then return the status in the low 32 bits and the number of changed registers in the high 32 bits. */
23uint64_t ombr_probe(void *fn, uint64_t *result, uint64_t a, uint64_t b, uint64_t c);
24#if defined(__APPLE__) /* Mach-O: C names carry a leading underscore; there is no .type or .size. */
25#define PROBE_BEGIN ".globl _ombr_probe\n_ombr_probe:\n"
26#define PROBE_END ""
27#else
28#define PROBE_BEGIN ".globl ombr_probe\n.type ombr_probe,%function\nombr_probe:\n"
29#define PROBE_END ".size ombr_probe,.-ombr_probe\n"
30#endif
31__asm__(".text\n.balign 4\n" PROBE_BEGIN "hint #34\n"
32 "stp x29,x30,[sp,#-160]!\nmov x29,sp\nstp x19,x20,[sp,#16]\nstp x21,x22,[sp,#32]\nstp x23,x24,[sp,#48]\n"
33 "stp x25,x26,[sp,#64]\nstp x27,x28,[sp,#80]\nstp d8,d9,[sp,#96]\nstp d10,d11,[sp,#112]\n"
34 "stp d12,d13,[sp,#128]\nstp d14,d15,[sp,#144]\nmov x16,x0\nmov x0,x1\nmov x1,x2\nmov x2,x3\nmov x3,x4\n"
35 "mov x19,#119\nmov x20,#120\nmov x21,#121\nmov x22,#122\nmov x23,#123\nmov x24,#124\nmov x25,#125\nmov x26,#126\nmov x27,#127\nmov x28,#128\n"
36 "mov x9,#108\nfmov d8,x9\nmov x9,#109\nfmov d9,x9\nmov x9,#110\nfmov d10,x9\nmov x9,#111\nfmov d11,x9\nmov x9,#112\nfmov d12,x9\nmov x9,#113\nfmov d13,x9\nmov x9,#114\nfmov d14,x9\nmov x9,#115\nfmov d15,x9\n"
37 "blr x16\nmov w0,w0\nmov x9,#0\n"
38 "cmp x19,#119\ncinc x9,x9,ne\ncmp x20,#120\ncinc x9,x9,ne\ncmp x21,#121\ncinc x9,x9,ne\ncmp x22,#122\ncinc x9,x9,ne\ncmp x23,#123\ncinc x9,x9,ne\ncmp x24,#124\ncinc x9,x9,ne\ncmp x25,#125\ncinc x9,x9,ne\ncmp x26,#126\ncinc x9,x9,ne\ncmp x27,#127\ncinc x9,x9,ne\ncmp x28,#128\ncinc x9,x9,ne\n"
39 "fmov x10,d8\ncmp x10,#108\ncinc x9,x9,ne\nfmov x10,d9\ncmp x10,#109\ncinc x9,x9,ne\nfmov x10,d10\ncmp x10,#110\ncinc x9,x9,ne\nfmov x10,d11\ncmp x10,#111\ncinc x9,x9,ne\nfmov x10,d12\ncmp x10,#112\ncinc x9,x9,ne\nfmov x10,d13\ncmp x10,#113\ncinc x9,x9,ne\nfmov x10,d14\ncmp x10,#114\ncinc x9,x9,ne\nfmov x10,d15\ncmp x10,#115\ncinc x9,x9,ne\n"
40 "orr x0,x0,x9,lsl #32\nldp d14,d15,[sp,#144]\nldp d12,d13,[sp,#128]\nldp d10,d11,[sp,#112]\nldp d8,d9,[sp,#96]\n"
41 "ldp x27,x28,[sp,#80]\nldp x25,x26,[sp,#64]\nldp x23,x24,[sp,#48]\nldp x21,x22,[sp,#32]\nldp x19,x20,[sp,#16]\n"
42 "ldp x29,x30,[sp],#160\nret\n" PROBE_END);
43
44static void *big_thread(void *p)
45{
46 uint64_t *r = p;
47 r[1] = omega_big(&r[0], 7);
48 return NULL;
49}
50
51static volatile uint32_t inner_status = 99;
52static void on_alarm(int sig)
53{
54 uint64_t r = SENTINEL;
55 (void)sig;
56 inner_status = omega_madd(&r, 3, 5, 7);
57}
58
59int main(void)
60{
61 uint64_t r;
62 r = 0;
63 check("madd 3*5+7 = 22", omega_madd(&r, 3, 5, 7) == 0 && r == 22);
64 r = 0;
65 check("madd wraps: (2^64-1)*2+5 = 3", omega_madd(&r, UINT64_MAX, 2, 5) == 0 && r == 3);
66 r = 0;
67 check("madd large = 3637949782307626511",
68 omega_madd(&r, UINT64_C(1311768467463790320), UINT64_C(18364758544493064720), UINT64_C(1085102592571150095)) == 0 &&
69 r == UINT64_C(3637949782307626511));
70 r = 0;
71 check("madd signed bits: -3*5+2 = -13", omega_madd(&r, (uint64_t)(int64_t)-3, 5, 2) == 0 && (int64_t)r == -13 &&
72 r == UINT64_C(18446744073709551603));
73 r = 0;
74 check("smadd -3*5+2 = -13", omega_smadd(&r, (uint64_t)(int64_t)-3, 5, 2) == 0 && (int64_t)r == -13);
75 r = SENTINEL;
76 check("smadd 2^62*4 traps 1, result unchanged",
77 omega_smadd(&r, UINT64_C(4611686018427387904), 4, 0) == 1 && r == SENTINEL);
78 uint64_t buf[5] = {0, 0, 0, 0, 0};
79 r = SENTINEL;
80 check("fill k=1 traps 3, result unchanged", omega_fill(&r, buf, 4, 1) == 3 && r == SENTINEL);
81 check("fill k=1 writes before the trap stay visible", buf[0] == 11 && buf[1] == 22 && buf[2] == 0 && buf[3] == 0);
82 memset(buf, 0, sizeof buf);
83 r = 0;
84 check("fill k=0 returns 3, writes 11 22 33", omega_fill(&r, buf, 4, 0) == 0 && r == 3 && buf[0] == 11 && buf[1] == 22 &&
85 buf[2] == 33 && buf[3] == 0 && buf[4] == 0);
86 check("null result refused 32", omega_madd(NULL, 3, 5, 7) == 32);
87 r = SENTINEL;
88 check("null buffer refused 32, result unchanged", omega_fill(&r, NULL, 4, 0) == 32 && r == SENTINEL);
89 check("buffer length 3 refused 32", omega_fill(&r, buf, 3, 0) == 32 && r == SENTINEL);
90 check("misaligned buffer refused 32", omega_fill(&r, (char *)buf + 1, 4, 0) == 32 && r == SENTINEL);
91 memset(buf, 0, sizeof buf);
92 check("result inside buffer refused 32, buffer untouched",
93 omega_fill(&buf[1], buf, 4, 0) == 32 && buf[0] == 0 && buf[1] == 0 && buf[2] == 0);
94 r = 0;
95 check("call after traps still works", omega_madd(&r, 3, 5, 7) == 0 && r == 22);
96
97
98 /* mix: six parameters (ulong, long, view uchar 3, edit uchar 2, view ulong 2, ulong), no result.
99 * 5 + 20 + 7 = 32 (calc:08302bc4); 5 + 20 + 300 is 70 over 255 (calc:a6a8bb76), so the conversion traps 5. */
100 const unsigned char cb[3] = {1, 2, 3};
101 unsigned char db[2] = {0, 0};
102 const uint64_t eb[2] = {10, 20};
103 r = SENTINEL;
104 check("mix six parameters, void result writes 0", omega_mix(&r, 5, 1, cb, 3, db, 2, eb, 2, 7) == 0 && r == 0 &&
105 db[0] == 3 && db[1] == 32);
106 db[0] = db[1] = 0;
107 r = SENTINEL;
108 check("mix negative long traps 9 after both writes, result unchanged",
109 omega_mix(&r, 5, (uint64_t)(int64_t)-1, cb, 3, db, 2, eb, 2, 7) == 9 && r == SENTINEL && db[0] == 3 && db[1] == 32);
110 db[0] = db[1] = 0;
111 check("mix uchar conversion 325 traps 5, first write visible",
112 omega_mix(&r, 5, 1, cb, 3, db, 2, eb, 2, 300) == 5 && r == SENTINEL && db[0] == 3 && db[1] == 0);
113 unsigned char ov[3] = {0, 0, 0};
114 check("mix view overlapping the edit array refused 32, nothing written",
115 omega_mix(&r, 5, 1, ov, 3, ov + 1, 2, eb, 2, 7) == 32 && ov[1] == 0 && ov[2] == 0);
116
117 /* big: a 4,000,000-byte local array (0.954 of Omega's 4 MiB cap, calc:ffff21b2); (7 + 1) + 7 = 15 (calc:e44bce3f). */
118 r = 0;
119 check("big local array on the main thread = 15", omega_big(&r, 7) == 0 && r == 15);
120 r = SENTINEL;
121 check("big traps 3 from a frame holding a local array, result unchanged", omega_big(&r, 0) == 3 && r == SENTINEL);
122 uint64_t tr[2] = {0, 99};
123 pthread_attr_t at;
124 pthread_t th;
125 pthread_attr_init(&at);
126 pthread_attr_setstacksize(&at, 256 * 1024);
127 int made = pthread_create(&th, &at, big_thread, tr) == 0;
128 if (made)
129 pthread_join(th, NULL);
130 check("big on a 256 KiB thread runs on the binding's stack = 15", made && tr[1] == 0 && tr[0] == 15);
131
132 /* The caller's callee-saved registers survive a normal return and a trap. */
133 uint64_t pv = ombr_probe((void *)omega_madd, &r, 3, 5, 7);
134 check("x19..x28 and d8..d15 kept across a normal return", (uint32_t)pv == 0 && (pv >> 32) == 0 && r == 22);
135 r = SENTINEL;
136 pv = ombr_probe((void *)omega_smadd, &r, UINT64_C(4611686018427387904), 4, 0);
137 check("x19..x28 and d8..d15 kept across a trap", (uint32_t)pv == 1 && (pv >> 32) == 0 && r == SENTINEL);
138
139 /* A unit with private helpers: only its last function is bound; helper calls and a helper's trap work. */
140 const unsigned char hb[4] = {1, 2, 3, 4}, hx[4] = {255, 255, 255, 255};
141 check("hsum 1+4+9+16+5 = 35 through two helpers", omega_hsum(&r, hb, 4, 5) == 0 && r == 35);
142 r = SENTINEL;
143 check("hsum: a helper's trap 9 returns 9, result unchanged", omega_hsum(&r, hx, 4, 999000) == 9 && r == SENTINEL);
144
145 struct sigaction sa;
146 memset(&sa, 0, sizeof sa);
147 sa.sa_handler = on_alarm;
148 sigaction(SIGALRM, &sa, NULL);
149 struct itimerval it = {{0, 0}, {0, 50}};
150 int entered = 0;
151 for (int tries = 0; tries < 200 && inner_status != 33; tries++) {
152 inner_status = 99;
153 setitimer(ITIMER_REAL, &it, NULL);
154 r = 0;
155 uint32_t s = omega_spin(&r, 1000000);
156 struct itimerval off = {{0, 0}, {0, 0}};
157 setitimer(ITIMER_REAL, &off, NULL);
158 if (s != 0 || r != UINT64_C(499999500000)) {
159 check("spin result", 0);
160 break;
161 }
162 entered = 1;
163 }
164 check("reentry from a signal handler during a call refused 33", entered && inner_status == 33);
165 printf("%s %d failure(s)\n", fails ? "FAIL" : "PASS", fails);
166 return fails != 0;
167}