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 / v2 / dev / dcall.c

109 lines · 4183 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2#define _GNU_SOURCE
3#include "omega_native.h"
4#include <assert.h>
5#include <errno.h>
6#include <fcntl.h>
7#include <pthread.h>
8#include <stdatomic.h>
9#include <stdint.h>
10#include <stdio.h>
11#include <string.h>
12#include <sys/ioctl.h>
13#include <sys/mman.h>
14#include <time.h>
15#include <unistd.h>
16
17/* WolframLanguageEvaluator: IntegerString[3224389163,16] -> "c030462b";
18 * IntegerString[3223340587,16] -> "c020462b"; IntegerString[21531,16] -> "541b".
19 * The libc FIONREAD value is checked below, not assumed from the generated code. */
20/* WolframLanguageEvaluator: UnitConvert[Quantity[1,"Seconds"],"Nanoseconds"]
21 * -> Quantity[1000000000,"Nanoseconds"]. */
22static int64_t ns(clockid_t id)
23{
24 struct timespec t;
25 assert(clock_gettime(id, &t) == 0);
26 return (int64_t)t.tv_sec * 1000000000 + t.tv_nsec;
27}
28static uint64_t expected;
29static unsigned char *shared;
30static void *consume(void *unused)
31{
32 (void)unused;
33 int64_t deadline = ns(CLOCK_MONOTONIC) + 1000000000;
34 while (!atomic_load_explicit((_Atomic uint32_t *)shared, memory_order_acquire))
35 assert(ns(CLOCK_MONOTONIC) < deadline);
36 uint64_t payload;
37 memcpy(&payload, shared + 8, sizeof(payload));
38 assert(payload == expected);
39 for (unsigned i = 16; i < 32; ++i) assert(shared[i] == 7);
40 return NULL;
41}
42static int64_t neg_ioctl(int fd, unsigned long req, void *p)
43{
44 errno = 0;
45 int r = ioctl(fd, req, p);
46 return r < 0 ? -errno : r;
47}
48int main(void)
49{
50 uint64_t r = 0;
51 int p[2], count;
52 assert(pipe(p) == 0);
53 assert(write(p[1], "hello", 5) == 5);
54 assert(FIONREAD == 21531);
55 assert(ioctl(p[0], FIONREAD, &count) == 0 && count == 5);
56 assert(omega2_nread(&r, p[0]) == 0 && (int64_t)r == count);
57 assert(omega2_nread(&r, -1) == 0 && (int64_t)r == -EBADF);
58 close(p[0]); close(p[1]);
59 int fd = open("/dev/null", O_RDWR | O_CLOEXEC);
60 assert(fd >= 0);
61 unsigned char b[48] = {0};
62 int64_t ref = neg_ioctl(fd, 0xc030462bUL, b);
63 assert(ref == -ENOTTY);
64 assert(omega2_ctl(&r, fd) == 0 && (int64_t)r == ref);
65 ref = neg_ioctl(fd, 0xc020462bUL, b);
66 assert(ref == -ENOTTY);
67 assert(omega2_call(&r, fd) == 0 && (int64_t)r == ref);
68 ref = neg_ioctl(fd, 75, b);
69 assert(ref == -ENOTTY);
70 assert(omega2_uvm(&r, fd) == 0 && (int64_t)r == ref);
71 close(fd);
72
73 fd = memfd_create("omega-bridge-fake-device", MFD_CLOEXEC);
74 assert(fd >= 0 && ftruncate(fd, 4096) == 0);
75 shared = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
76 assert(shared != MAP_FAILED);
77 uint64_t h, address;
78 assert(omega2_mapping(&h, fd) == 0 && (int64_t)h > 0);
79 assert(omega2_address(&address, (int64_t)h) == 0 && address != 0);
80 expected = UINT64_C(0x0123456789abcdef);
81 pthread_t reader;
82 assert(pthread_create(&reader, NULL, consume, NULL) == 0);
83 assert(omega2_publish(&r, (int64_t)h, expected) == 0 && r == 0);
84 assert(pthread_join(reader, NULL) == 0);
85 assert(omega2_fetch(&r, (int64_t)h) == 0 && r == expected + 7);
86 assert(omega2_rd(&r, (int64_t)h) == 0 && r == 1);
87 assert(omega2_drop(&r, (int64_t)h) == 0 && r == 0);
88 r = 99;
89 assert(omega2_rd(&r, (int64_t)h) == 3 && r == 99);
90 assert(omega2_drop(&r, (int64_t)h) == 3 && r == 99);
91 assert(omega2_address(&r, (int64_t)h) == 3 && r == 99);
92 munmap(shared, 4096); close(fd);
93
94 uint64_t handles[128];
95 for (unsigned i = 0; i < 128; ++i)
96 assert(omega2_anon(&handles[i]) == 0 && (int64_t)handles[i] > 0);
97 assert(omega2_anon(&r) == 0 && (int64_t)r == -EMFILE);
98 for (unsigned i = 0; i < 128; ++i)
99 assert(omega2_drop(&r, (int64_t)handles[i]) == 0 && r == 0);
100 int64_t lo = ns(CLOCK_MONOTONIC);
101 assert(omega2_mono(&r) == 0 && (int64_t)r >= lo && (int64_t)r <= ns(CLOCK_MONOTONIC));
102 lo = ns(CLOCK_REALTIME);
103 assert(omega2_wall(&r) == 0 && (int64_t)r >= lo && (int64_t)r <= ns(CLOCK_REALTIME));
104 assert(omega2_pause(&r, 1000) == 0 && r == 0);
105 r = 99;
106 assert(omega2_pause(&r, -1) == 3 && r == 99);
107 puts("NATIVE-DEVICE PASS pipe/ioctl C differential, memfd doorbell, stale/exhaustion, clocks");
108 return 0;
109}