aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / tests / fixtures / osc_unit / launch / osc-launch-check.c
| 1 | /* osc-launch-check.c: reference for the launch-argument rule (spec 9.2). |
| 2 | * Same logic as omega osc_ir_slice_args_ok (osc_ir.c:689-705 at d64ccb3) plus |
| 3 | * the scalar-fits-kind rule. Usage: osc-launch-check KINDS ARGS |
| 4 | * Prints OK | REFUSED LAUNCH_ARG_SHAPE 41. C99, libc only. */ |
| 5 | #include <stdint.h> |
| 6 | #include <stdio.h> |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | #define MAXR 6 |
| 10 | static int split(char *s, uint64_t *out) { |
| 11 | int n = 0; |
| 12 | for (char *t = strtok(s, ","); t; t = strtok(NULL, ",")) { |
| 13 | if (n >= 64) return -1; |
| 14 | out[n++] = strtoull(t, NULL, 0); |
| 15 | } |
| 16 | return n; |
| 17 | } |
| 18 | static int is_slice(uint64_t k) { return k == 11 || k == 12; } |
| 19 | static int fits(uint64_t k, uint64_t v) { |
| 20 | switch (k) { |
| 21 | case 1: return v <= 1; |
| 22 | case 2: return v <= 0xff; |
| 23 | case 3: return v <= 0xffff; |
| 24 | case 4: return v <= 0xffffffffull; |
| 25 | case 6: return (int64_t)v == (int8_t)v; |
| 26 | case 7: return (int64_t)v == (int16_t)v; |
| 27 | case 8: return (int64_t)v == (int32_t)v; |
| 28 | default: return 1; /* 5, 9, slice registers */ |
| 29 | } |
| 30 | } |
| 31 | int main(int argc, char **argv) { |
| 32 | if (argc != 3) return 2; |
| 33 | uint64_t kinds[64], args[64]; |
| 34 | char a[512], b[512]; |
| 35 | snprintf(a, sizeof a, "%s", argv[1]); |
| 36 | snprintf(b, sizeof b, "%s", argv[2]); |
| 37 | int nk = split(a, kinds), na = split(b, args); |
| 38 | int bad = (nk < 0 || na != nk || nk > MAXR); |
| 39 | for (int p = 0; !bad && p < nk; p++) { |
| 40 | if (!is_slice(kinds[p]) && !fits(kinds[p], args[p])) bad = 1; |
| 41 | } |
| 42 | for (int p = 0; !bad && p + 1 < nk; p++) { |
| 43 | if (!is_slice(kinds[p])) continue; |
| 44 | uint64_t ptr = args[p], n = args[p + 1], sz = n * (kinds[p] == 12 ? 8 : 1); |
| 45 | if ((kinds[p] == 12 && (n > UINT64_MAX / 8 || (n && (ptr & 7)))) || (n && !ptr) || sz > UINT64_MAX - ptr) bad = 1; |
| 46 | for (int q = 0; !bad && q + 1 < nk; q++) { |
| 47 | if (q == p || !is_slice(kinds[q])) continue; |
| 48 | if (kinds[p] != 12 && kinds[q] != 12) continue; |
| 49 | uint64_t qn = args[q + 1], qsz = qn * (kinds[q] == 12 ? 8 : 1); |
| 50 | if (!sz || !qn || (kinds[q] == 12 && qn > UINT64_MAX / 8) || qsz > UINT64_MAX - args[q]) continue; |
| 51 | if (ptr < args[q] + qsz && args[q] < ptr + sz) bad = 1; |
| 52 | } |
| 53 | } |
| 54 | puts(bad ? "REFUSED LAUNCH_ARG_SHAPE 41" : "OK"); |
| 55 | return 0; |
| 56 | } |