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 / test_osc_unit.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* test_osc_unit.c -- host conformance harness for artifact/osc_unit.c. |
| 18 | * Runs every line of the frozen OSC_UNIT_ARTIFACT v1 vector files (copied to |
| 19 | * tests/fixtures/osc_unit, provenance there): expected.txt (container |
| 20 | * verdicts), lookups.txt (exact-name lookups) and state.txt (loader-state |
| 21 | * scenarios, codes 29 and 30), and requires the stated verdict byte for byte. |
| 22 | * Keys are the spec's THROWAWAY TEST keys. */ |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include "ck_test.h" |
| 27 | #include "osc_unit.h" |
| 28 | #include "osc_unit_test_anchor.h" |
| 29 | #include "sha256.h" |
| 30 | |
| 31 | #ifndef OSC_FIXTURE_DIR |
| 32 | #define OSC_FIXTURE_DIR "native/kernel/tests/fixtures/osc_unit" |
| 33 | #endif |
| 34 | |
| 35 | static const char *fixdir = OSC_FIXTURE_DIR; |
| 36 | static uint8_t key_test[32], key_owner[32]; |
| 37 | |
| 38 | static int hexval(int c) { return c >= '0' && c <= '9' ? c - '0' : c >= 'a' && c <= 'f' ? c - 'a' + 10 : -1; } |
| 39 | |
| 40 | static int load_key(const char *name, uint8_t out[32]) |
| 41 | { |
| 42 | char path[512], line[256]; |
| 43 | snprintf(path, sizeof path, "%s/keys/%s", fixdir, name); |
| 44 | FILE *f = fopen(path, "r"); |
| 45 | if (!f) return 0; |
| 46 | if (!fgets(line, sizeof line, f)) { fclose(f); return 0; } |
| 47 | fclose(f); |
| 48 | for (int i = 0; i < 32; i++) { |
| 49 | int h = hexval(line[2 * i]), l = hexval(line[2 * i + 1]); |
| 50 | if (h < 0 || l < 0) return 0; |
| 51 | out[i] = (uint8_t)(h * 16 + l); |
| 52 | } |
| 53 | return 1; |
| 54 | } |
| 55 | |
| 56 | static uint8_t *slurp(const char *path, size_t *n) |
| 57 | { |
| 58 | FILE *f = fopen(path, "rb"); |
| 59 | if (!f) return 0; |
| 60 | fseek(f, 0, SEEK_END); |
| 61 | long l = ftell(f); |
| 62 | fseek(f, 0, SEEK_SET); |
| 63 | uint8_t *b = malloc(l > 0 ? (size_t)l : 1); |
| 64 | if (b && fread(b, 1, (size_t)l, f) != (size_t)l) { free(b); b = 0; } |
| 65 | fclose(f); |
| 66 | *n = b ? (size_t)l : 0; |
| 67 | return b; |
| 68 | } |
| 69 | |
| 70 | static void hex(const uint8_t *p, size_t n, char *out) |
| 71 | { |
| 72 | for (size_t i = 0; i < n; i++) sprintf(out + 2 * i, "%02x", p[i]); |
| 73 | } |
| 74 | |
| 75 | /* loader-state scenario */ |
| 76 | struct state { int noreserve; int have_gens; unsigned dom[8], kind[8]; uint32_t id[8]; uint64_t gen[8]; int n; }; |
| 77 | |
| 78 | static int st_lookup(void *ctx, unsigned dom, unsigned kind, uint32_t id, uint64_t *cur) |
| 79 | { |
| 80 | struct state *s = ctx; |
| 81 | for (int i = 0; i < s->n; i++) |
| 82 | if (s->dom[i] == dom && s->kind[i] == kind && s->id[i] == id) { *cur = s->gen[i]; return 0; } |
| 83 | return 1; |
| 84 | } |
| 85 | static int st_reserve(void *ctx, const struct osc_accept *a) { (void)a; return ((struct state *)ctx)->noreserve; } |
| 86 | |
| 87 | static struct osc_policy policy_for(const char *mode, const char *domains, const char *anchors, struct state *st) |
| 88 | { |
| 89 | static uint8_t t[1][32], o[1][32]; |
| 90 | struct osc_policy p; |
| 91 | memset(&p, 0, sizeof p); |
| 92 | p.release = strcmp(mode, "release") == 0; |
| 93 | memcpy(t[0], key_test, 32); |
| 94 | memcpy(o[0], key_owner, 32); |
| 95 | if (strchr(anchors, 'T')) { p.test_anchors = t; p.n_test = 1; } |
| 96 | if (strchr(anchors, 'O')) { p.owner_anchors = o; p.n_owner = 1; } |
| 97 | p.unit_formats = OSC_PROFILE_UNIT_FORMATS; |
| 98 | p.abi_versions = OSC_PROFILE_ABI; |
| 99 | for (const char *c = domains; *c; c++) |
| 100 | if (*c >= '0' && *c <= '9') p.cap_domains |= 1u << (*c - '0'); |
| 101 | p.ctx = st; |
| 102 | if (st && st->have_gens) p.gen_lookup = st_lookup; |
| 103 | if (st && st->noreserve) p.reserve = st_reserve; |
| 104 | return p; |
| 105 | } |
| 106 | |
| 107 | static void verdict_text(unsigned rc, const struct osc_accept *a, char *out, size_t n) |
| 108 | { |
| 109 | if (rc == OSC_OK) { |
| 110 | char d[65], i[65]; |
| 111 | hex(a->unit_digest, 32, d); |
| 112 | hex(a->ir_sha256, 32, i); |
| 113 | snprintf(out, n, "ACCEPT unit_digest=%s program_id=%s", d, i); |
| 114 | } else { |
| 115 | snprintf(out, n, "REFUSED %s %u", osc_code_name(rc), rc); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | static unsigned admit_file(const char *vec, const struct osc_policy *p, struct osc_accept *a, uint8_t **keep, size_t *kn) |
| 120 | { |
| 121 | char path[512]; |
| 122 | size_t n; |
| 123 | snprintf(path, sizeof path, "%s/vectors/%s", fixdir, vec); |
| 124 | uint8_t *b = slurp(path, &n); |
| 125 | if (!b) return 999; |
| 126 | unsigned rc = osc_unit_admit(b, n, p, a); |
| 127 | if (keep) { *keep = b; *kn = n; } else free(b); |
| 128 | return rc; |
| 129 | } |
| 130 | |
| 131 | /* run lines of a vector file; kind 0 expected, 1 lookups, 2 state. Returns lines checked. */ |
| 132 | static int run_file(const char *name, int kind) |
| 133 | { |
| 134 | char path[512], line[1024]; |
| 135 | int count = 0; |
| 136 | snprintf(path, sizeof path, "%s/vectors/%s", fixdir, name); |
| 137 | FILE *f = fopen(path, "r"); |
| 138 | CHECK(f != 0); |
| 139 | if (!f) return 0; |
| 140 | static struct osc_accept acc; |
| 141 | while (fgets(line, sizeof line, f)) { |
| 142 | char vec[128], mode[32], dom[32], anc[8], arg[128] = "", want[600]; |
| 143 | size_t l = strlen(line); |
| 144 | while (l && (line[l - 1] == '\n' || line[l - 1] == '\r')) line[--l] = 0; |
| 145 | if (!l || line[0] == '#') continue; |
| 146 | int ok; |
| 147 | if (kind == 0) ok = sscanf(line, "%127s %31s %31s %7s %599[^\n]", vec, mode, dom, anc, want) == 5; |
| 148 | else ok = sscanf(line, "%127s %31s %31s %7s %127s %599[^\n]", vec, mode, dom, anc, arg, want) == 6; |
| 149 | CHECK(ok); |
| 150 | if (!ok) continue; |
| 151 | struct state st; |
| 152 | memset(&st, 0, sizeof st); |
| 153 | if (kind == 2) { |
| 154 | if (strcmp(arg, "noreserve") == 0) st.noreserve = 1; |
| 155 | else if (strncmp(arg, "gens=", 5) == 0) { |
| 156 | st.have_gens = 1; |
| 157 | unsigned d, k, id; unsigned long long g; |
| 158 | if (sscanf(arg + 5, "%u:%u:%u=%llu", &d, &k, &id, &g) == 4) { |
| 159 | st.dom[0] = d; st.kind[0] = k; st.id[0] = id; st.gen[0] = g; st.n = 1; |
| 160 | } else CHECK(0); |
| 161 | } else CHECK(0); |
| 162 | } |
| 163 | struct osc_policy p = policy_for(mode, dom, anc, &st); |
| 164 | uint8_t *bytes = 0; size_t bn = 0; |
| 165 | unsigned rc = admit_file(vec, &p, &acc, &bytes, &bn); |
| 166 | char got[700]; |
| 167 | if (kind == 1) { |
| 168 | if (rc != OSC_OK) { snprintf(got, sizeof got, "REFUSED %s %u", osc_code_name(rc), rc); } |
| 169 | else { |
| 170 | int fi = osc_unit_lookup(&acc, arg, strlen(arg)); |
| 171 | if (fi < 0) snprintf(got, sizeof got, "REFUSED %s %d", osc_code_name(OSC_LAUNCH_BAD_ENTRY), OSC_LAUNCH_BAD_ENTRY); |
| 172 | else snprintf(got, sizeof got, "ACCEPT fn_index=%d", fi); |
| 173 | } |
| 174 | } else { |
| 175 | verdict_text(rc, &acc, got, sizeof got); |
| 176 | } |
| 177 | free(bytes); |
| 178 | if (strcmp(got, want) != 0) { |
| 179 | printf(" MISMATCH %s [%s]\n want: %s\n got: %s\n", vec, arg, want, got); |
| 180 | ck_t_fail++; |
| 181 | } |
| 182 | ck_t_run++; |
| 183 | count++; |
| 184 | } |
| 185 | fclose(f); |
| 186 | return count; |
| 187 | } |
| 188 | |
| 189 | /* Every single-byte flip and every truncation of the valid vector must be refused, never crash |
| 190 | * (run under ASAN/UBSAN by `make sanitize`). */ |
| 191 | static void test_flips_and_truncations(void) |
| 192 | { |
| 193 | char path[512]; |
| 194 | size_t n; |
| 195 | snprintf(path, sizeof path, "%s/vectors/a01_valid_min.unit", fixdir); |
| 196 | uint8_t *b = slurp(path, &n); |
| 197 | CHECK(b != 0); |
| 198 | if (!b) return; |
| 199 | struct state st; memset(&st, 0, sizeof st); |
| 200 | struct osc_policy p = policy_for("qualification", "1", "T", &st); |
| 201 | static struct osc_accept acc; |
| 202 | CHECK(osc_unit_admit(b, n, &p, &acc) == OSC_OK); |
| 203 | int accepted = 0; |
| 204 | for (size_t i = 0; i < n; i++) { |
| 205 | b[i] ^= 0x01; |
| 206 | if (osc_unit_admit(b, n, &p, &acc) == OSC_OK) accepted++; |
| 207 | b[i] ^= 0x01; |
| 208 | } |
| 209 | CHECK(accepted == 0); |
| 210 | for (size_t l = 0; l < n; l++) { |
| 211 | unsigned rc = osc_unit_admit(b, l, &p, &acc); |
| 212 | CHECK(rc != OSC_OK); |
| 213 | } |
| 214 | CHECK(osc_unit_admit(0, 0, &p, &acc) != OSC_OK); |
| 215 | free(b); |
| 216 | } |
| 217 | |
| 218 | /* Section 8.4 on hand-picked words. */ |
| 219 | static void test_words(void) |
| 220 | { |
| 221 | static const uint32_t allowed[] = { |
| 222 | 0x8B010002u, /* add x2, x0, x1 */ 0xD2800020u, /* movz x0, #1 */ |
| 223 | 0xD65F03C0u, /* ret */ 0xD63F0200u, /* blr x16 */ |
| 224 | 0xA9BF7BFDu, /* stp x29, x30, [sp, #-16]! */ 0xA8C17BFDu, /* ldp x29, x30, [sp], #16 */ |
| 225 | 0xD4200060u, /* brk #3 */ 0x94000010u, /* bl */ |
| 226 | 0x38606800u | (1u << 16) | (2u << 5) | 3u, /* ldrb w3, [x2, x0] shape */ |
| 227 | }; |
| 228 | static const uint32_t refused[] = { |
| 229 | 0xD4000001u, /* svc #0 */ 0xD4000002u, /* hvc #0 */ |
| 230 | 0xD4000003u, /* smc #0 */ 0xD5033FDFu, /* isb */ |
| 231 | 0xD503201Fu, /* nop */ 0xD53B4200u, /* mrs x0, nzcv-ish */ |
| 232 | 0xD51B4200u, /* msr */ 0xD69F03E0u, /* eret */ |
| 233 | 0xC85F7C00u, /* ldxr */ 0x1E602800u, /* fadd d0, d0, d0 */ |
| 234 | 0xD4200000u, /* brk #0 */ 0xD42001E0u, /* brk #15 */ |
| 235 | 0xD63F03E0u, /* blr xzr-slot (rn=31) */ 0xD4400000u, /* hlt */ |
| 236 | 0x00000000u, /* udf */ 0xFFFFFFFFu, |
| 237 | 0xCB000000u | (3u << 22), /* sub with ROR shift */ |
| 238 | 0x9A80F400u, /* csinc cond NV */ 0x5400000Eu, /* b.al */ |
| 239 | 0x5400000Fu, /* b.nv */ |
| 240 | }; |
| 241 | for (size_t i = 0; i < sizeof allowed / sizeof allowed[0]; i++) CHECK(osc_a64_word_allowed(allowed[i])); |
| 242 | for (size_t i = 0; i < sizeof refused / sizeof refused[0]; i++) { |
| 243 | CHECK(!osc_a64_word_allowed(refused[i])); |
| 244 | } |
| 245 | /* ldp Rt == Rt2 (x1, x1) refused by the round trip; stp with Rt == Rt2 is allowed. */ |
| 246 | CHECK(!osc_a64_word_allowed(0xA9400000u | (1u << 10) | (2u << 5) | 1u)); |
| 247 | CHECK(osc_a64_word_allowed(0xA9000000u | (1u << 10) | (2u << 5) | 1u)); |
| 248 | /* writeback base equal to a transfer register */ |
| 249 | CHECK(!osc_a64_word_allowed(0xA9800000u | (3u << 10) | (2u << 5) | 2u)); |
| 250 | } |
| 251 | |
| 252 | /* Lookup is by exact name only. Two entries forced to share a name_hash (a |
| 253 | * case no signed vector can carry: it needs a SHA-256 collision) must still |
| 254 | * resolve by name, and an absent name with that hash must not match. */ |
| 255 | static void test_lookup_ignores_name_hash(void) |
| 256 | { |
| 257 | static struct osc_accept a; |
| 258 | memset(&a, 0, sizeof a); |
| 259 | a.function_count = 2; |
| 260 | const char *nm[2] = {"add", "sub"}; |
| 261 | for (unsigned i = 0; i < 2; i++) { |
| 262 | a.entry[i].fn_index = (uint16_t)(7 + i); |
| 263 | a.entry[i].name_len = 3; |
| 264 | memcpy(a.entry[i].name, nm[i], 4); |
| 265 | memset(a.entry[i].name_hash, 0xAB, 16); |
| 266 | } |
| 267 | CHECK(osc_unit_lookup(&a, "add", 3) == 7); |
| 268 | CHECK(osc_unit_lookup(&a, "sub", 3) == 8); |
| 269 | CHECK(osc_unit_lookup(&a, "mul", 3) == -1); |
| 270 | CHECK(osc_unit_lookup(&a, "ad", 2) == -1); |
| 271 | CHECK(osc_unit_lookup(&a, "addx", 4) == -1); |
| 272 | } |
| 273 | |
| 274 | static void test_anchor_matches_fixture(void) |
| 275 | { |
| 276 | CHECK(memcmp(osc_unit_test1_pk, key_test, 32) == 0); |
| 277 | } |
| 278 | |
| 279 | /* Spec 8.2 step 16: a loader whose staging maximum is smaller than the file refuses with |
| 280 | * RESOURCE_UNAVAILABLE before parsing; a file that fits is judged normally. */ |
| 281 | static void test_staging_maximum(void) |
| 282 | { |
| 283 | static struct osc_accept acc; |
| 284 | struct state st; |
| 285 | memset(&st, 0, sizeof st); |
| 286 | struct osc_policy p = policy_for("qualification", "1", "T", &st); |
| 287 | uint8_t *b = 0; size_t n = 0; |
| 288 | CHECK(admit_file("a01_valid_min.unit", &p, &acc, &b, &n) == OSC_OK); |
| 289 | CHECK(n > 128); |
| 290 | p.staging_max = n - 1; |
| 291 | CHECK(osc_unit_admit(b, n, &p, &acc) == OSC_RESOURCE_UNAVAILABLE); |
| 292 | p.staging_max = n; |
| 293 | CHECK(osc_unit_admit(b, n, &p, &acc) == OSC_OK); |
| 294 | free(b); |
| 295 | } |
| 296 | |
| 297 | int main(int argc, char **argv) |
| 298 | { |
| 299 | if (argc > 1) fixdir = argv[1]; |
| 300 | CHECK(load_key("test1.pub", key_test)); |
| 301 | CHECK(load_key("owner1.pub", key_owner)); |
| 302 | test_anchor_matches_fixture(); |
| 303 | int ne = run_file("expected.txt", 0); |
| 304 | int nl = run_file("lookups.txt", 1); |
| 305 | int ns = run_file("state.txt", 2); |
| 306 | printf("osc_unit conformance: container=%d lookups=%d state=%d\n", ne, nl, ns); |
| 307 | CHECK(ne == 57 && nl == 6 && ns == 3); |
| 308 | test_staging_maximum(); |
| 309 | test_words(); |
| 310 | test_lookup_ignores_name_hash(); |
| 311 | test_flips_and_truncations(); |
| 312 | return ck_t_verdict("test_osc_unit"); |
| 313 | } |