Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aienux/blob/main/native/kernel/x86/tests/local_tools_udp_test.c # aienux Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aienux / native / kernel / x86 / tests / local_tools_udp_test.c` 298 lines ยท 12502 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 2 // UDP adapter oracle for the local-tools courier path, synthetic result bytes. 3 // Real CPL3 execution is covered separately in QEMU (scripts/qemu_ck_local_tools_test.sh). 4 // 5 // Usage: local_tools_udp_test ORACLE RELAY 6 // ORACLE the local_tools_model_oracle binary (loopback model server) 7 // RELAY the allen-courier relay binary 8 // Plays the iMac's side of the wire: sends HELLO and JOB frames, answers every 9 // CALL with a hand-made little-endian result, then runs three direct arithmetic 10 // jobs with the model server already gone. 11 #ifndef _DEFAULT_SOURCE 12 #define _DEFAULT_SOURCE 13 #endif 14 #ifndef _XOPEN_SOURCE 15 #define _XOPEN_SOURCE 700 16 #endif 17 #include 18 #include 19 #include 20 #include 21 #include 22 #include 23 #include 24 #include 25 #include 26 #include 27 #include 28 #include 29 #include 30 #include 31 #include 32 #include 33 #include 34 35 static pid_t oracle_pid, relay_pid; 36 static char root[256]; 37 static int sock, relay_port; 38 static const char run_id[] = "`abc0000000000421`"; 39 static int under = 1; 40 41 typedef struct { int seq, part; } seen_key; 42 static seen_key seen[64]; 43 static int nseen; 44 45 static double now(void) { 46 struct timespec t; 47 clock_gettime(CLOCK_MONOTONIC, &t); 48 return (double)t.tv_sec + (double)t.tv_nsec / 1e9; 49 } 50 51 static int rm_entry(const char *path, const struct stat *st, int flag, struct FTW *w) { 52 (void)st; (void)flag; (void)w; 53 return remove(path); 54 } 55 56 static void cleanup(void) { 57 if (oracle_pid > 0) { kill(oracle_pid, SIGTERM); waitpid(oracle_pid, NULL, 0); oracle_pid = 0; } 58 if (relay_pid > 0) { kill(relay_pid, SIGTERM); waitpid(relay_pid, NULL, 0); relay_pid = 0; } 59 if (root[0]) { nftw(root, rm_entry, 16, FTW_DEPTH | FTW_PHYS); root[0] = 0; } 60 } 61 62 static void die(const char *fmt, ...) { 63 va_list ap; 64 fputs("LOCAL_TOOLS_UDP: FAIL ", stderr); 65 va_start(ap, fmt); 66 vfprintf(stderr, fmt, ap); 67 va_end(ap); 68 fputc('\n', stderr); 69 cleanup(); 70 exit(1); 71 } 72 73 #define ASSERT(cond, ...) do { if (!(cond)) die(__VA_ARGS__); } while (0) 74 75 static void put_le64(uint8_t *p, uint64_t v) { for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i)); } 76 static uint64_t get_le64(const uint8_t *p) { uint64_t v = 0; for (int i = 7; i >= 0; i--) v = v << 8 | p[i]; return v; } 77 78 static size_t from_hex(const char *s, uint8_t *out, size_t cap) { 79 size_t n = strlen(s); 80 ASSERT(n % 2 == 0 && n / 2 <= cap, "bad hex %s", s); 81 for (size_t i = 0; i < n / 2; i++) { 82 unsigned v; 83 ASSERT(sscanf(s + 2 * i, "%2x", &v) == 1, "bad hex %s", s); 84 out[i] = (uint8_t)v; 85 } 86 return n / 2; 87 } 88 89 static void to_hex(const uint8_t *p, size_t n, char *out) { 90 if (!n) { strcpy(out, "-"); return; } 91 for (size_t i = 0; i < n; i++) sprintf(out + 2 * i, "%02x", p[i]); 92 } 93 94 static void send_frame(const char *kind, int seq, int part, int total, const char *body) { 95 char data[4096]; 96 int n = snprintf(data, sizeof data, "ALLEN-COURIER v=1 run=%s kind=%s seq=%d part=%d/%d bytes=%zu\n%s", 97 run_id, kind, seq, part, total, strlen(body), body); 98 struct sockaddr_in to = { .sin_family = AF_INET, .sin_port = htons((uint16_t)relay_port), 99 .sin_addr.s_addr = htonl(INADDR_LOOPBACK) }; 100 ASSERT(n > 0 && (size_t)n < sizeof data, "frame too large"); 101 ASSERT(sendto(sock, data, (size_t)n, 0, (struct sockaddr *)&to, sizeof to) == n, "sendto: %s", strerror(errno)); 102 } 103 104 /* One LINE frame from the relay: acknowledges it and reports whether it is new. 105 * Returns 0 on timeout, 1 for a fresh LINE, -1 for anything to skip. */ 106 static int next_line(int *seq, char *body, size_t cap) { 107 char packet[4097]; 108 ssize_t n = recv(sock, packet, sizeof packet - 1, 0); 109 if (n < 0) { ASSERT(errno == EAGAIN || errno == EWOULDBLOCK, "recv: %s", strerror(errno)); return 0; } 110 packet[n] = 0; 111 char *nl = strchr(packet, '\n'); 112 ASSERT(nl, "frame has no header line: %s", packet); 113 *nl = 0; 114 char kind[32] = ""; 115 int s = -1, part = -1; 116 char *save = NULL; 117 for (char *tok = strtok_r(packet, " ", &save), *first = tok; tok; tok = strtok_r(NULL, " ", &save)) { 118 if (tok == first) continue; 119 char *eq = strchr(tok, '='); 120 ASSERT(eq, "bad header field %s", tok); 121 *eq = 0; 122 if (!strcmp(tok, "kind")) snprintf(kind, sizeof kind, "%s", eq + 1); 123 else if (!strcmp(tok, "seq")) s = atoi(eq + 1); 124 else if (!strcmp(tok, "part")) part = atoi(eq + 1); 125 } 126 if (strcmp(kind, "LINE")) return -1; 127 send_frame("GOT", s, part, 0, ""); 128 for (int i = 0; i < nseen; i++) if (seen[i].seq == s && seen[i].part == part) return -1; 129 ASSERT(nseen < 64, "too many frames"); 130 seen[nseen++] = (seen_key){ s, part }; 131 snprintf(body, cap, "%s", nl + 1); 132 *seq = s; 133 return 1; 134 } 135 136 static pid_t spawn(char *const argv[], bool quiet) { 137 pid_t pid = fork(); 138 ASSERT(pid >= 0, "fork: %s", strerror(errno)); 139 if (pid == 0) { 140 if (quiet) { 141 int null = open("/dev/null", O_WRONLY); 142 dup2(null, 1); dup2(null, 2); 143 } 144 execv(argv[0], argv); 145 _exit(127); 146 } 147 return pid; 148 } 149 150 static int read_port(const char *name) { 151 char path[320], text[16] = ""; 152 snprintf(path, sizeof path, "%s/%s", root, name); 153 FILE *f = fopen(path, "r"); 154 if (!f) return 0; 155 if (!fgets(text, sizeof text, f)) text[0] = 0; 156 fclose(f); 157 return atoi(text); 158 } 159 160 static void expect_calc(int job_id, const char *ask, const int64_t operands[3], int state, int reason, 161 const uint8_t *raw, size_t raw_len, const char *said) { 162 char body[4096]; 163 under++; 164 double start = now(); 165 int calls = 0; 166 snprintf(body, sizeof body, "JOB %d do Calculate locally.\t%s\tlocal-tools=v1", job_id, ask); 167 send_frame("JOB", under, 1, 1, body); 168 while (now() - start < 5) { 169 int seq; 170 if (next_line(&seq, body, sizeof body) != 1) continue; 171 ASSERT(seq == under, "seq %d != %d", seq, under); 172 if (!strncmp(body, "WAIT ", 5)) continue; 173 if (!strncmp(body, "SAY ", 4)) { 174 char want[512]; 175 snprintf(want, sizeof want, "SAY %d 1/1 %s", job_id, said); 176 ASSERT(!strcmp(body, want), "unexpected answer %s (wanted %s)", body, want); 177 ASSERT(calls == 1, "calls=%d", calls); 178 printf("LOCAL_DIRECT_UDP: PASS job=%d elapsed_ms=%.2f model=offline\n", job_id, (now() - start) * 1000); 179 return; 180 } 181 char head[64], hex[256]; 182 snprintf(head, sizeof head, "CALL %d 1 1 24 0 ", job_id); 183 ASSERT(!strncmp(body, head, strlen(head)), "unexpected call %s", body); 184 snprintf(hex, sizeof hex, "%s", body + strlen(head)); 185 uint8_t data[64]; 186 size_t dn = from_hex(hex, data, sizeof data); 187 ASSERT(dn == 24, "operand bytes %zu", dn); 188 for (int i = 0; i < 3; i++) ASSERT((int64_t)get_le64(data + 8 * i) == operands[i], "operand %d in %s", i, body); 189 calls++; 190 under++; 191 char rhex[64]; 192 to_hex(raw, raw_len, rhex); 193 snprintf(body, sizeof body, "RESULT %d 1 %d %d %s", job_id, state, reason, rhex); 194 send_frame("JOB", under, 1, 1, body); 195 } 196 die("direct arithmetic stalled with model offline"); 197 } 198 199 int main(int argc, char **argv) { 200 if (argc != 3) { fputs("usage: local_tools_udp_test ORACLE RELAY\n", stderr); return 2; } 201 const char *tmpdir = getenv("TMPDIR"); 202 snprintf(root, sizeof root, "%s/local-tools-udp-XXXXXX", tmpdir && *tmpdir ? tmpdir : "/tmp"); 203 if (!mkdtemp(root)) { perror("mkdtemp"); return 2; } 204 205 char *oracle_argv[] = { argv[1], root, NULL }; 206 oracle_pid = spawn(oracle_argv, false); 207 double until = now() + 5; 208 int model_port; 209 while (!(model_port = read_port("oracle-port.txt"))) { 210 ASSERT(now() < until, "oracle startup"); 211 usleep(20000); 212 } 213 int reserve = socket(AF_INET, SOCK_DGRAM, 0); 214 struct sockaddr_in a = { .sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_LOOPBACK) }; 215 socklen_t alen = sizeof a; 216 ASSERT(reserve >= 0 && !bind(reserve, (struct sockaddr *)&a, sizeof a) && 217 !getsockname(reserve, (struct sockaddr *)&a, &alen), "reserve port"); 218 relay_port = ntohs(a.sin_port); 219 close(reserve); 220 221 char port_s[16], model_s[32], work[300], logs[300]; 222 snprintf(port_s, sizeof port_s, "%d", relay_port); 223 snprintf(model_s, sizeof model_s, "127.0.0.1:%d", model_port); 224 snprintf(work, sizeof work, "%s/work", root); 225 snprintf(logs, sizeof logs, "%s/logs", root); 226 char *relay_argv[] = { argv[2], "--udp", port_s, "--model", model_s, "--work", work, "--log-dir", logs, NULL }; 227 relay_pid = spawn(relay_argv, true); 228 229 sock = socket(AF_INET, SOCK_DGRAM, 0); 230 struct sockaddr_in me = { .sin_family = AF_INET, .sin_addr.s_addr = htonl(INADDR_LOOPBACK) }; 231 struct timeval tv = { 0, 200000 }; 232 ASSERT(sock >= 0 && !bind(sock, (struct sockaddr *)&me, sizeof me) && 233 !setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv), "client socket"); 234 usleep(200000); 235 send_frame("HELLO", 0, 0, 0, ""); 236 send_frame("JOB", under, 1, 1, 237 "JOB 4242 do Check tools.\tCheck arithmetic, text and system.\tlocal-tools=v1"); 238 239 int tools[8], ntools = 0; 240 bool done = false; 241 for (double deadline = now() + 20; now() < deadline && !done;) { 242 char body[4096]; 243 int seq; 244 if (next_line(&seq, body, sizeof body) != 1) continue; 245 ASSERT(seq == under, "transport sequence is not semantic job ID: %d vs %d", seq, under); 246 if (!strncmp(body, "WAIT ", 5)) continue; 247 if (!strcmp(body, "SAY 4242 1/1 VERIFIED")) { done = true; break; } 248 int inv, tool, total, off, used = 0; 249 char hex[1024]; 250 ASSERT(sscanf(body, "CALL 4242 %d %d %d %d %1023s%n", &inv, &tool, &total, &off, hex, &used) == 5, 251 "unexpected line %s", body); 252 uint8_t data[256]; 253 size_t dn = strcmp(hex, "-") ? from_hex(hex, data, sizeof data) : 0; 254 ASSERT(off == 0 && (int)dn == total, "bad call %s", body); 255 ASSERT(ntools < 8, "too many calls"); 256 tools[ntools++] = tool; 257 uint8_t result[40]; 258 size_t rn; 259 if (tool == 1) { 260 ASSERT(dn == 24 && get_le64(data) == 1 && get_le64(data + 8) == 20 && get_le64(data + 16) == 22, "arithmetic operands: %s", body); 261 put_le64(result, 42); rn = 8; 262 } else if (tool == 2) { 263 ASSERT(dn == 13 && !memcmp(data, "one two\nthree", 13), "text operand: %s", body); 264 put_le64(result, 13); put_le64(result + 8, 3); put_le64(result + 16, 1); rn = 24; 265 } else { 266 ASSERT(tool == 3 && dn == 0, "system call: %s", body); 267 put_le64(result, 4); put_le64(result + 8, `1000000`); put_le64(result + 16, 1920); 268 put_le64(result + 24, 1080); put_le64(result + 32, 1); rn = 40; 269 } 270 under++; 271 char rhex[96], reply[256]; 272 to_hex(result, rn, rhex); 273 snprintf(reply, sizeof reply, "RESULT 4242 %d 2 0 %s", inv, rhex); 274 send_frame("JOB", under, 1, 1, reply); 275 } 276 ASSERT(done, "no final answer before deadline"); 277 ASSERT(ntools == 3 && tools[0] == 1 && tools[1] == 2 && tools[2] == 3, "tool order"); 278 int status = -1; 279 for (double wait_until = now() + 3; now() < wait_until;) { 280 pid_t w = waitpid(oracle_pid, &status, WNOHANG); 281 if (w == oracle_pid) { oracle_pid = 0; break; } 282 usleep(20000); 283 } 284 ASSERT(oracle_pid == 0 && WIFEXITED(status) && WEXITSTATUS(status) == 0, "oracle did not exit cleanly"); 285 puts("LOCAL_TOOLS_UDP: PASS distinct transport/job IDs, three calls and results"); 286 287 /* The HTTP model is gone. Direct arithmetic must still call the native tool, 288 * accept only its bound result, and render without a second model. */ 289 uint8_t forty_two[8], none[1] = { 0 }; 290 put_le64(forty_two, 42); 291 expect_calc(4243, "What is 123 multiplied by 456?", (int64_t[]){ 3, 123, 456 }, 2, 0, 292 forty_two, 8, "42."); 293 expect_calc(4244, "1 / 0", (int64_t[]){ 4, 1, 0 }, 3, 2, none, 0, "Division by zero isn't defined."); 294 expect_calc(4245, "`9223372036854775807` + 1", (int64_t[]){ 1, INT64_MAX, 1 }, 3, 1, none, 0, 295 "That result is outside the supported 64-bit integer range."); 296 cleanup(); 297 return 0; 298 }