omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / tests / native / v2 / caller.c
| 1 | /* Native ABI v2 oracle (tests/native/v2/run.sh): calls the OM-2 test units vec, lay, one and hoist, and the |
| 2 | * OM-1 unit madd, through the generated declarations only (omega_native.h, omega_api.h). Expected |
| 3 | * values are computed here independently of Omega. Prints one PASS or FAIL line per check; exit 0 |
| 4 | * only when all pass. The hoist checks are AArch64 only: they probe AArch64 callee-saved registers, |
| 5 | * and the x86-64 suite (tests/native/x86) links madd, vec, lay and one without hoist. */ |
| 6 | #define _GNU_SOURCE |
| 7 | #include "omega_api.h" |
| 8 | #include <dlfcn.h> |
| 9 | #include <pthread.h> |
| 10 | #include <signal.h> |
| 11 | #include <stdio.h> |
| 12 | #include <string.h> |
| 13 | #include <sys/mman.h> |
| 14 | #include <sys/time.h> |
| 15 | |
| 16 | static int fails; |
| 17 | static void check(const char *name, int ok) |
| 18 | { |
| 19 | printf("%s %s\n", ok ? "PASS" : "FAIL", name); |
| 20 | if (!ok) |
| 21 | fails++; |
| 22 | } |
| 23 | |
| 24 | #define R0 0x5a5a5a5a5a5a5a5aull |
| 25 | |
| 26 | static uint64_t refsum(const uint8_t *d, uint64_t n) |
| 27 | { |
| 28 | uint64_t s = 0; |
| 29 | for (uint64_t i = 0; i < n; i++) |
| 30 | s += d[i]; |
| 31 | return s; |
| 32 | } |
| 33 | |
| 34 | static uint64_t rng = 0x9e3779b97f4a7c15ull; |
| 35 | static uint64_t next(void) |
| 36 | { |
| 37 | rng ^= rng << 13; |
| 38 | rng ^= rng >> 7; |
| 39 | rng ^= rng << 17; |
| 40 | return rng; |
| 41 | } |
| 42 | |
| 43 | static volatile uint32_t sig_status = 99; |
| 44 | static volatile int sig_fired; |
| 45 | static void on_alarm(int s) |
| 46 | { |
| 47 | (void)s; |
| 48 | uint64_t r = 0, d[1] = {1}; |
| 49 | sig_status = omega2_vlen(&r, d, 1); |
| 50 | sig_fired = 1; |
| 51 | } |
| 52 | |
| 53 | static void *swap_thread(void *arg) |
| 54 | { |
| 55 | uint8_t *ok = arg, d[8], o[8]; |
| 56 | *ok = 1; |
| 57 | for (int k = 0; k < 20000; k++) { |
| 58 | for (int i = 0; i < 8; i++) |
| 59 | d[i] = (uint8_t)(k + i); |
| 60 | uint64_t r = R0; |
| 61 | if (omega2_swap8(&r, d, 8, o) != OMEGA_OK || r != 8) |
| 62 | *ok = 0; |
| 63 | for (int i = 0; i < 8; i++) |
| 64 | if (o[i] != (uint8_t)(k + 7 - i)) |
| 65 | *ok = 0; |
| 66 | } |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | #if defined(__aarch64__) |
| 71 | /* hoist_thread: 5000 calls of the hoisting unit on this thread's own Omega stack; every fifth call traps |
| 72 | * in inner, after the loop wrote this thread's array. */ |
| 73 | static void *hoist_thread(void *arg) |
| 74 | { |
| 75 | uint8_t *ok = arg; |
| 76 | uint64_t d[64] = {0}; |
| 77 | *ok = 1; |
| 78 | for (int k = 0; k < 5000; k++) { |
| 79 | uint64_t r = R0, t = (uint64_t)(k % 5 == 4); |
| 80 | uint32_t s = omega2_hadd(&r, d, 64, t); |
| 81 | if (t ? s != 9 || r != R0 : s != OMEGA_OK || r != 1920569) |
| 82 | *ok = 0; |
| 83 | for (int i = 0; i < 64; i++) |
| 84 | if (d[i] != 70000ull * (uint64_t)(k + 1)) |
| 85 | *ok = 0; |
| 86 | } |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | /* ombr_probe(fn, result, a, b, c): set x19..x28 and d8..d15 to known values, call fn(result, a, b, c), |
| 91 | * then return the status in the low 32 bits and the number of changed registers in the high 32 bits |
| 92 | * (as tests/native/caller.c). */ |
| 93 | uint64_t ombr_probe(void *fn, uint64_t *result, uint64_t a, uint64_t b, uint64_t c); |
| 94 | __asm__(".text\n.balign 4\n.globl ombr_probe\n.type ombr_probe,%function\nombr_probe:\nhint #34\n" |
| 95 | "stp x29,x30,[sp,#-160]!\nmov x29,sp\nstp x19,x20,[sp,#16]\nstp x21,x22,[sp,#32]\nstp x23,x24,[sp,#48]\n" |
| 96 | "stp x25,x26,[sp,#64]\nstp x27,x28,[sp,#80]\nstp d8,d9,[sp,#96]\nstp d10,d11,[sp,#112]\n" |
| 97 | "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" |
| 98 | "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" |
| 99 | "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" |
| 100 | "blr x16\nmov w0,w0\nmov x9,#0\n" |
| 101 | "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" |
| 102 | "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" |
| 103 | "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" |
| 104 | "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" |
| 105 | "ldp x29,x30,[sp],#160\nret\n.size ombr_probe,.-ombr_probe\n"); |
| 106 | #endif |
| 107 | |
| 108 | int main(void) |
| 109 | { |
| 110 | uint64_t r; |
| 111 | uint32_t s; |
| 112 | setvbuf(stdout, 0, _IOLBF, 0); /* each line survives a crash (the negative controls rely on it) */ |
| 113 | |
| 114 | /* Identity: the library's string equals the one the generated header carries. */ |
| 115 | check("ident vec: library == header", strcmp(omega2_ident_vec(), OMEGA2_IDENT_vec) == 0); |
| 116 | check("ident lay: library == header", strcmp(omega2_ident_lay(), OMEGA2_IDENT_lay) == 0); |
| 117 | check("ident one: library == header", strcmp(omega2_ident_one(), OMEGA2_IDENT_one) == 0); |
| 118 | printf("IDENT %s\nIDENT %s\nIDENT %s\n", omega2_ident_vec(), omega2_ident_lay(), omega2_ident_one()); |
| 119 | |
| 120 | /* Values. */ |
| 121 | const uint8_t abc[3] = {'a', 'b', 'c'}; |
| 122 | r = R0; |
| 123 | check("bsum abc = 294", omega2_bsum(&r, abc, 3) == OMEGA_OK && r == 294); |
| 124 | uint32_t w4[4] = {1, 2, 3, 0xffffffffu}; |
| 125 | r = R0; |
| 126 | check("wsum {1,2,3,2^32-1} = 4294967301", omega2_wsum(&r, w4, 4) == OMEGA_OK && r == 4294967301ull); |
| 127 | int64_t l3[3] = {-5, 7, -9}; |
| 128 | r = R0; |
| 129 | check("lsum {-5,7,-9} = -7", omega2_lsum(&r, l3, 3) == OMEGA_OK && (int64_t)r == -7); |
| 130 | int64_t lo[2] = {INT64_MAX, 1}; |
| 131 | r = R0; |
| 132 | check("lsum overflow traps 1, result unchanged", omega2_lsum(&r, lo, 2) == 1 && r == R0); |
| 133 | |
| 134 | /* Empty views: len 0 with any pointer, NULL included, is the empty view. */ |
| 135 | r = R0; |
| 136 | check("bsum (NULL, 0) = 0", omega2_bsum(&r, NULL, 0) == OMEGA_OK && r == 0); |
| 137 | r = R0; |
| 138 | check("wsum (misaligned, 0) = 0", omega2_wsum(&r, (const uint32_t *)(abc + 1), 0) == OMEGA_OK && r == 0); |
| 139 | r = R0; |
| 140 | check("vlen (NULL, 0) = 0", omega2_vlen(&r, NULL, 0) == OMEGA_OK && r == 0); |
| 141 | |
| 142 | /* Refusals before any Omega code runs: status 32, result unchanged. */ |
| 143 | r = R0; |
| 144 | check("NULL with len 1 refused", omega2_bsum(&r, NULL, 1) == OMEGA_BAD_ARG && r == R0); |
| 145 | _Alignas(8) uint8_t raw[64] = {0}; |
| 146 | r = R0; |
| 147 | check("wsum misaligned by 1 refused", omega2_wsum(&r, (const uint32_t *)(raw + 1), 1) == OMEGA_BAD_ARG && r == R0); |
| 148 | r = R0; |
| 149 | check("wsum misaligned by 2 refused", omega2_wsum(&r, (const uint32_t *)(raw + 2), 1) == OMEGA_BAD_ARG && r == R0); |
| 150 | r = R0; |
| 151 | check("vlen misaligned by 4 refused", omega2_vlen(&r, (const uint64_t *)(raw + 4), 1) == OMEGA_BAD_ARG && r == R0); |
| 152 | r = R0; |
| 153 | check("vcopy edit misaligned by 1 refused", |
| 154 | omega2_vcopy(&r, (const uint16_t *)raw, 1, (uint16_t *)(raw + 33), 1) == OMEGA_BAD_ARG && r == R0); |
| 155 | r = R0; |
| 156 | check("wsum len*4 = 2^64 (len 2^62) refused", omega2_wsum(&r, w4, 1ull << 62) == OMEGA_BAD_ARG && r == R0); |
| 157 | r = R0; |
| 158 | check("vlen len*8 overflows (len 2^61) refused", omega2_vlen(&r, (const uint64_t *)raw, 1ull << 61) == OMEGA_BAD_ARG); |
| 159 | r = R0; |
| 160 | check("vlen len 2^64-1 refused", omega2_vlen(&r, (const uint64_t *)raw, UINT64_MAX) == OMEGA_BAD_ARG && r == R0); |
| 161 | /* #748 golden at the bridge boundary: a view of len 2^64-1 bytes cannot be formed, whatever o and k. */ |
| 162 | r = R0; |
| 163 | check("#748 golden: vpart len 2^64-1, o 2^64-2, k 4 refused (32)", |
| 164 | omega2_vpart(&r, raw, UINT64_MAX, UINT64_MAX - 1, 4) == OMEGA_BAD_ARG && r == R0); |
| 165 | r = R0; |
| 166 | check("bsum len 2^64-1 refused", omega2_bsum(&r, raw, UINT64_MAX) == OMEGA_BAD_ARG && r == R0); |
| 167 | /* Address wrap: end exactly 2^64 is refused; end 2^64-8 is accepted (vlen reads no element). */ |
| 168 | const uint64_t *top = (const uint64_t *)(uintptr_t)(UINTPTR_MAX - 15u); |
| 169 | r = R0; |
| 170 | check("vlen at 2^64-16, len 2 (end 2^64) refused", omega2_vlen(&r, top, 2) == OMEGA_BAD_ARG && r == R0); |
| 171 | r = R0; |
| 172 | check("vlen at 2^64-16, len 1 (end 2^64-8) accepted", omega2_vlen(&r, top, 1) == OMEGA_OK && r == 1); |
| 173 | r = R0; |
| 174 | check("wsum at 2^64-16, len 2^62-4 (wraps) refused", |
| 175 | omega2_wsum(&r, (const uint32_t *)top, (1ull << 62) - 4) == OMEGA_BAD_ARG && r == R0); |
| 176 | /* Huge accepted length: 2^32+5 elements of a PROT_NONE NORESERVE mapping; vlen reads none. */ |
| 177 | uint64_t big = (1ull << 32) + 5; |
| 178 | void *m = mmap(0, big * 8, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); |
| 179 | if (m != MAP_FAILED) { |
| 180 | r = R0; |
| 181 | check("vlen len 2^32+5 accepted, len arrives whole", omega2_vlen(&r, m, big) == OMEGA_OK && r == big); |
| 182 | munmap(m, big * 8); |
| 183 | } else |
| 184 | check("mmap 32 GiB NORESERVE for the huge-len check", 0); |
| 185 | r = R0; |
| 186 | check("result NULL refused", omega2_bsum(NULL, abc, 3) == OMEGA_BAD_ARG); |
| 187 | |
| 188 | /* #748 golden inside the code: the part checks are o <= len then k <= len - o, never o + k. */ |
| 189 | uint8_t d16[16]; |
| 190 | for (int i = 0; i < 16; i++) |
| 191 | d16[i] = (uint8_t)(i + 1); |
| 192 | r = R0; |
| 193 | check("#748 golden: vpart len 16, o 2^64-2, k 4 traps 3 (wrapped o+k = 2 not used)", |
| 194 | omega2_vpart(&r, d16, 16, UINT64_MAX - 1, 4) == 3 && r == R0); |
| 195 | int grid = 1; |
| 196 | for (uint64_t o = 0; o <= 18; o++) |
| 197 | for (uint64_t k = 0; k <= 18; k++) { |
| 198 | r = R0; |
| 199 | s = omega2_vpart(&r, d16, 16, o, k); |
| 200 | if (o <= 16 && k <= 16 - o) { |
| 201 | if (s != OMEGA_OK || r != refsum(d16 + o, k)) |
| 202 | grid = 0; |
| 203 | } else if (s != 3 || r != R0) |
| 204 | grid = 0; |
| 205 | } |
| 206 | check("vpart over every o, k in 0..18 of a 16-byte view (sum or trap 3)", grid); |
| 207 | r = R0; |
| 208 | check("vpart o = len, k = 0 (empty part at the end) = 0", omega2_vpart(&r, d16, 16, 16, 0) == OMEGA_OK && r == 0); |
| 209 | r = R0; |
| 210 | check("vpart o 2^64-1, k 1 traps 3", omega2_vpart(&r, d16, 16, UINT64_MAX, 1) == 3); |
| 211 | |
| 212 | /* Split inputs: every split point of a 64-byte buffer, whole = left + right. */ |
| 213 | uint8_t b64[64]; |
| 214 | for (int i = 0; i < 64; i++) |
| 215 | b64[i] = (uint8_t)next(); |
| 216 | int split = 1; |
| 217 | uint64_t whole = 0; |
| 218 | split &= omega2_bsum(&whole, b64, 64) == OMEGA_OK && whole == refsum(b64, 64); |
| 219 | for (uint64_t k = 0; k <= 64; k++) { |
| 220 | uint64_t a = R0, b = R0; |
| 221 | split &= omega2_bsum(&a, b64, k) == OMEGA_OK && omega2_bsum(&b, b64 + k, 64 - k) == OMEGA_OK && a + b == whole; |
| 222 | } |
| 223 | check("bsum split at every point 0..64", split); |
| 224 | |
| 225 | /* Large inputs: 16 MiB of bytes, 16 Mi uint words, summed by the unit in parts of 1000000. */ |
| 226 | uint64_t nb = 16777216; |
| 227 | uint8_t *lb = malloc(nb); |
| 228 | uint32_t *lw = malloc(nb * 4); |
| 229 | if (lb && lw) { |
| 230 | uint64_t rs = 0, rw = 0; |
| 231 | for (uint64_t i = 0; i < nb; i++) { |
| 232 | lb[i] = (uint8_t)next(); |
| 233 | lw[i] = (uint32_t)next(); |
| 234 | rs += lb[i]; |
| 235 | rw += lw[i]; |
| 236 | } |
| 237 | r = R0; |
| 238 | check("bsum 16 MiB = reference", omega2_bsum(&r, lb, nb) == OMEGA_OK && r == rs); |
| 239 | r = R0; |
| 240 | check("wsum 16 Mi words (64 MiB) = reference", omega2_wsum(&r, lw, nb) == OMEGA_OK && r == rw); |
| 241 | r = R0; |
| 242 | check("bsum 16 MiB at an odd address (uchar needs no alignment)", |
| 243 | omega2_bsum(&r, lb + 1, nb - 1) == OMEGA_OK && r == rs - lb[0]); |
| 244 | } else |
| 245 | check("malloc 80 MiB", 0); |
| 246 | |
| 247 | /* Overlap: an edit range against any other array or the result; half-open; empty overlaps nothing. */ |
| 248 | _Alignas(8) uint16_t h[16]; |
| 249 | for (int i = 0; i < 16; i++) |
| 250 | h[i] = (uint16_t)(100 + i); |
| 251 | r = R0; |
| 252 | check("vcopy edit == view refused", omega2_vcopy(&r, h, 4, h, 4) == OMEGA_BAD_ARG && r == R0); |
| 253 | r = R0; |
| 254 | check("vcopy edit overlapping the view by one element refused", |
| 255 | omega2_vcopy(&r, h, 4, h + 3, 4) == OMEGA_BAD_ARG && h[3] == 103 && r == R0); |
| 256 | r = R0; |
| 257 | check("vcopy adjacent ranges (half-open) accepted", |
| 258 | omega2_vcopy(&r, h, 4, h + 4, 4) == OMEGA_OK && r == 4 && h[4] == 100 && h[7] == 103); |
| 259 | r = R0; |
| 260 | check("vcopy both empty at one address = 0", omega2_vcopy(&r, h, 0, h, 0) == OMEGA_OK && r == 0); |
| 261 | r = R0; |
| 262 | check("vcopy empty edit inside the view passes the bridge (code traps 3)", omega2_vcopy(&r, h, 4, h + 1, 0) == 3); |
| 263 | r = R0; |
| 264 | check("vcopy empty view inside the edit = 0", omega2_vcopy(&r, h + 2, 0, h, 8) == OMEGA_OK && r == 0); |
| 265 | uint64_t *rin = (uint64_t *)(void *)h; |
| 266 | check("vcopy result inside the edit refused", omega2_vcopy(rin, h + 8, 2, h, 4) == OMEGA_BAD_ARG); |
| 267 | uint64_t rv[2] = {R0, R0}; |
| 268 | check("bsum result inside the view refused (v1 rule)", omega2_bsum(&rv[0], (const uint8_t *)rv, 16) == OMEGA_BAD_ARG); |
| 269 | r = R0; |
| 270 | check("vcopy short edit traps 3 after the writes it made", |
| 271 | omega2_vcopy(&r, h, 4, h + 8, 2) == 3 && h[8] == 100 && h[9] == 101 && r == R0); |
| 272 | |
| 273 | /* Layout and out parameters (lay), through the generated API header and the raw symbols. */ |
| 274 | lay_blk c; |
| 275 | memset(&c, 0, sizeof c); |
| 276 | for (int i = 0; i < 8; i++) |
| 277 | c.tag[i] = (uint8_t)(0xa0 + i); |
| 278 | absorb(&c, abc, 3); |
| 279 | absorb(&c, b64, 64); |
| 280 | check("absorb twice: a = byte sum, n = 67", c.a == (uint32_t)(294 + refsum(b64, 64)) && c.n == 67); |
| 281 | uint8_t o16[16]; |
| 282 | memset(o16, 0xee, sizeof o16); |
| 283 | uint64_t n = digest(&c, o16); |
| 284 | int dok = n == 67; |
| 285 | for (int i = 0; i < 8; i++) |
| 286 | dok &= o16[i] == 0xa0 + i; |
| 287 | for (int i = 0; i < 4; i++) |
| 288 | dok &= o16[8 + i] == (uint8_t)(c.a >> (8 * i)) && o16[12 + i] == 0; |
| 289 | check("digest: tag, a's bytes, zeros; result n", dok); |
| 290 | r = R0; |
| 291 | check("absorb NULL layout refused", omega2_absorb(&r, NULL, abc, 3) == OMEGA_BAD_ARG && r == R0); |
| 292 | r = R0; |
| 293 | check("absorb layout overlapping the view refused", |
| 294 | omega2_absorb(&r, (uint8_t *)&c, (const uint8_t *)&c + 20, 4) == OMEGA_BAD_ARG && c.n == 67); |
| 295 | r = R0; |
| 296 | check("digest NULL out refused", omega2_digest(&r, (const uint8_t *)&c, NULL) == OMEGA_BAD_ARG && r == R0); |
| 297 | r = R0; |
| 298 | check("digest out overlapping the result refused", omega2_digest(&r, (const uint8_t *)&c, (uint8_t *)&r) == OMEGA_BAD_ARG); |
| 299 | /* An out may overlap a view: written after the code returns, memmove semantics. */ |
| 300 | uint8_t s8[8] = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 301 | r = R0; |
| 302 | s = omega2_swap8(&r, s8, 8, s8); |
| 303 | check("swap8 out == view: reversed in place (memmove after return)", |
| 304 | s == OMEGA_OK && r == 8 && s8[0] == 8 && s8[3] == 5 && s8[4] == 4 && s8[7] == 1); |
| 305 | uint8_t s12[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; |
| 306 | r = R0; |
| 307 | check("swap8 out overlapping the view by 4 bytes", |
| 308 | omega2_swap8(&r, s12, 12, s12 + 4) == OMEGA_OK && s12[3] == 4 && s12[4] == 8 && s12[11] == 1 && r == 12); |
| 309 | lay_blk c2 = c; |
| 310 | r = R0; |
| 311 | check("digest out over its own layout view", |
| 312 | omega2_digest(&r, (const uint8_t *)&c2, (uint8_t *)&c2) == OMEGA_OK && r == 67 && c2.tag[0] == 0xa0 |
| 313 | && ((uint8_t *)&c2)[0] == 0xa0 && ((uint8_t *)&c2)[8] == (uint8_t)c.a); |
| 314 | /* Nothing reaches the caller's out buffer on a trap. */ |
| 315 | memset(o16, 0xee, sizeof o16); |
| 316 | r = R0; |
| 317 | check("swap8 short view traps 3, out untouched, result unchanged", |
| 318 | omega2_swap8(&r, s8, 7, o16) == 3 && o16[0] == 0xee && o16[7] == 0xee && r == R0); |
| 319 | uint64_t ho[2] = {11, 22}; |
| 320 | r = R0; |
| 321 | check("halt traps 9 after writing its out buffer: caller's out untouched", |
| 322 | omega2_halt(&r, ho) == 9 && ho[0] == 11 && ho[1] == 22 && r == R0); |
| 323 | r = R0; |
| 324 | check("halt out misaligned refused", omega2_halt(&r, (uint64_t *)(void *)(raw + 4)) == OMEGA_BAD_ARG); |
| 325 | |
| 326 | /* Two out parameters: adjacent is fine, overlapping is refused (both would be copied to the bytes). */ |
| 327 | uint8_t t8[8] = {0}; |
| 328 | r = R0; |
| 329 | check("two outs adjacent accepted", omega2_two(&r, t8, t8 + 4) == OMEGA_OK && t8[0] == 1 && t8[4] == 5 && t8[7] == 8); |
| 330 | memset(t8, 0, sizeof t8); |
| 331 | r = R0; |
| 332 | check("two outs overlapping by one byte refused", omega2_two(&r, t8, t8 + 3) == OMEGA_BAD_ARG && t8[0] == 0); |
| 333 | r = R0; |
| 334 | check("two outs at one address refused", omega2_two(&r, t8, t8) == OMEGA_BAD_ARG && t8[0] == 0); |
| 335 | |
| 336 | /* A unit without export: its last fn is bound. */ |
| 337 | uint64_t f5[5] = {0}; |
| 338 | fillv(f5, 5, 42); |
| 339 | check("fillv (OM-2, no export) fills 5 elements", f5[0] == 42 && f5[4] == 42); |
| 340 | |
| 341 | /* Module boundary: three separately compiled OM-2 units and one OM-1 unit in one binary, calls |
| 342 | * interleaved, each checked against its reference. */ |
| 343 | int inter = 1; |
| 344 | lay_blk ci; |
| 345 | memset(&ci, 0, sizeof ci); |
| 346 | uint64_t f3[3], expa = 0; |
| 347 | for (uint64_t k = 0; k < 2000; k++) { |
| 348 | uint8_t d[5]; |
| 349 | for (int i = 0; i < 5; i++) |
| 350 | d[i] = (uint8_t)next(); |
| 351 | r = R0; |
| 352 | inter &= omega2_bsum(&r, d, 5) == OMEGA_OK && r == refsum(d, 5); |
| 353 | absorb(&ci, d, 5); |
| 354 | expa += refsum(d, 5); |
| 355 | inter &= omega2_fillv(&r, f3, 3, k) == OMEGA_OK && f3[2] == k; |
| 356 | r = R0; |
| 357 | inter &= omega_madd(&r, k, 3, 1) == OMEGA_OK && r == 3 * k + 1; |
| 358 | inter &= ci.a == (uint32_t)expa && ci.n == 5 * (k + 1); |
| 359 | } |
| 360 | check("2000 interleaved rounds: vec, lay, one (OM-2) and madd (OM-1)", inter); |
| 361 | |
| 362 | /* Only exports are symbols: the private helper and v1 names for v2 units do not exist. */ |
| 363 | void *self = dlopen(0, RTLD_NOW); |
| 364 | check("no symbol for the private helper psum (omega2_psum, omega_psum)", |
| 365 | !dlsym(self, "omega2_psum") && !dlsym(self, "omega_psum")); |
| 366 | check("no v1 symbol for a v2 unit (omega_bsum, omega_fillv, omega_vec)", |
| 367 | !dlsym(self, "omega_bsum") && !dlsym(self, "omega_fillv") && !dlsym(self, "omega_vec")); |
| 368 | check("no v2 symbol for the v1 unit (omega2_madd)", !dlsym(self, "omega2_madd")); |
| 369 | |
| 370 | /* Reentry: a call from a signal handler during a call returns 33. */ |
| 371 | struct sigaction sa; |
| 372 | memset(&sa, 0, sizeof sa); |
| 373 | sa.sa_handler = on_alarm; |
| 374 | sigaction(SIGALRM, &sa, 0); |
| 375 | struct itimerval it = {{0, 0}, {0, 2000}}; |
| 376 | setitimer(ITIMER_REAL, &it, 0); |
| 377 | for (int k = 0; k < 2000 && !sig_fired && lb; k++) |
| 378 | omega2_bsum(&r, lb, nb); |
| 379 | check("reentry from a signal handler during a v2 call returns 33", sig_fired && sig_status == OMEGA_REENTRY); |
| 380 | |
| 381 | /* Two threads, each with its own Omega stack and out buffers. */ |
| 382 | pthread_t t1, t2; |
| 383 | uint8_t ok1 = 0, ok2 = 0; |
| 384 | pthread_create(&t1, 0, swap_thread, &ok1); |
| 385 | pthread_create(&t2, 0, swap_thread, &ok2); |
| 386 | pthread_join(t1, 0); |
| 387 | pthread_join(t2, 0); |
| 388 | check("two threads x 20000 swap8 calls", ok1 && ok2); |
| 389 | |
| 390 | #if defined(__aarch64__) |
| 391 | /* Callee-saved registers: hoist's code keeps hadd's seven loop constants in x20..x26 and inner's in x20 |
| 392 | * and x21, and inner's conversions write d8..d15 (with a hoisting OM-2 omc; run.sh fails unless the code |
| 393 | * writes them). The caller's x19..x28 and d8..d15 survive a normal return and a trap 9 raised in inner, |
| 394 | * the exported function's callee. 1920569 = 64 * 29997 + 761 (inner's result for k = 0). */ |
| 395 | uint64_t hd[64], pv; |
| 396 | for (int i = 0; i < 64; i++) |
| 397 | hd[i] = (uint64_t)i; |
| 398 | r = R0; |
| 399 | pv = ombr_probe((void *)omega2_hadd, &r, (uint64_t)(uintptr_t)hd, 64, 0); |
| 400 | int hok = 1; |
| 401 | for (int i = 0; i < 64; i++) |
| 402 | hok &= hd[i] == (uint64_t)i + 70000; |
| 403 | check("hoist: x19..x28 and d8..d15 kept across a normal return", (uint32_t)pv == OMEGA_OK && (pv >> 32) == 0 && r == 1920569 && hok); |
| 404 | r = R0; |
| 405 | pv = ombr_probe((void *)omega2_hadd, &r, (uint64_t)(uintptr_t)hd, 64, 1); |
| 406 | check("hoist: x19..x28 and d8..d15 kept across a trap 9 in a callee", (uint32_t)pv == 9 && (pv >> 32) == 0 && r == R0); |
| 407 | uint8_t hk1 = 0, hk2 = 0; |
| 408 | pthread_create(&t1, 0, hoist_thread, &hk1); |
| 409 | pthread_create(&t2, 0, hoist_thread, &hk2); |
| 410 | pthread_join(t1, 0); |
| 411 | pthread_join(t2, 0); |
| 412 | check("two threads x 5000 hadd calls (every fifth a trap)", hk1 && hk2); |
| 413 | #endif |
| 414 | |
| 415 | free(lb); |
| 416 | free(lw); |
| 417 | printf("%s %d\n", fails ? "C-V2 FAIL" : "C-V2 PASS", fails); |
| 418 | return fails != 0; |
| 419 | } |