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/tests/test_osh_op.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 / tests / test_osh_op.c` 344 lines ยท 14354 bytes Raw Blame History 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_osh_op.c -- host tests for core/osh_op.c: the AIENOS adapter's capability-authorized console write (`klog`). 18 * Requests are built as the expander's request record (OSH_PLATFORM_ABI section 7) without calling the shell core. */ 19 #include 20 #include "ck_test.h" 21 #include "osh_op.h" 22 23 static char cap_buf[4096]; 24 static int cap_calls; 25 static char logs[16][480]; 26 static int nlogs; 27 28 static int sink(void *ctx, const char *line, size_t len) 29 { 30 (void)ctx; 31 cap_calls++; 32 CHECK(line[len] == 0 && len > 0 && line[len - 1] == '\n'); /* a C string the kernel sink may print */ 33 if (strlen(cap_buf) + len < sizeof cap_buf) 34 { 35 size_t at = strlen(cap_buf); 36 memcpy(cap_buf + at, line, len); 37 cap_buf[at + len] = 0; 38 } 39 return 0; 40 } 41 static void logger(void *ctx, const char *line) 42 { 43 (void)ctx; 44 if (nlogs < 16) 45 snprintf(logs[nlogs++], sizeof logs[0], "%s", line); 46 } 47 static const struct osh_env env = { 0, sink, logger }; 48 49 static void reset_capture(void) 50 { 51 cap_buf[0] = 0; 52 cap_calls = 0; 53 nlogs = 0; 54 } 55 56 static uint64_t req[8 + 8 * 164]; 57 static uint64_t out[8192]; 58 59 /* one command per block; args NULL-terminated; extra fields set by the caller afterwards */ 60 static void build(unsigned ncmds, const char *const *argv, const char *const *argv2) 61 { 62 memset(req, 0, sizeof req); 63 memset(out, 0, sizeof out); 64 req[0] = 0x4F524551ull; 65 req[1] = ncmds; 66 req[3] = 0; 67 req[5] = 1; 68 uint64_t used = 0; 69 for (unsigned c = 0; c < ncmds; c++) { 70 const char *const *a = c == 0 ? argv : argv2; 71 uint64_t *b = req + 8 + c * 164; 72 unsigned n = 0; 73 for (; a[n]; n++) { 74 size_t l = strlen(a[n]); 75 for (size_t i = 0; i < l; i++) 76 out[used + i] = (uint8_t)a[n][i]; 77 b[4 + 2 * n] = used; 78 b[5 + 2 * n] = l; 79 used += l; 80 } 81 b[0] = n; 82 } 83 req[4] = used; 84 } 85 86 static struct osh_result run(struct osh_session *s, const char *const *argv) 87 { 88 struct osh_result r; 89 build(1, argv, 0); 90 osh_op_exec(s, &env, req, out, 8192, &r); 91 return r; 92 } 93 94 int main(void) 95 { 96 struct osh_session s, t; 97 struct osh_result r; 98 const char *const hello[] = { "klog", "hello", "world", 0 }; 99 100 /* allowed: the task holds WRITE on the console resource */ 101 reset_capture(); 102 osh_session_init(&s, CK_R_WRITE); 103 r = run(&s, hello); 104 CHECK(r.error == OSH_E_OK && r.outcome == OSH_O_COMPLETED && r.status == 0 && !r.stop); 105 CHECK(strcmp(cap_buf, "osh_op_out: hello world\n") == 0 && cap_calls == 1); 106 CHECK(s.intents[0].used && s.intents[0].closed && s.intents[0].outcome == OSH_O_COMPLETED && s.intents[0].seq == 1); 107 CHECK(memcmp(s.intents[0].digest, r.digest, 32) == 0); 108 uint8_t d1[32]; 109 memcpy(d1, r.digest, 32); 110 r = run(&s, hello); 111 CHECK(memcmp(d1, r.digest, 32) == 0 && r.seq == 2); /* the same request, the same digest */ 112 const char *const other[] = { "klog", "hello", "worlD", 0 }; 113 r = run(&s, other); 114 CHECK(memcmp(d1, r.digest, 32) != 0); 115 116 /* no capability at all: refused before any effect, with the named refusal */ 117 reset_capture(); 118 osh_session_init(&s, 0); 119 r = run(&s, hello); 120 CHECK(r.error == OSH_E_DENIED && r.outcome == OSH_O_FAILED_NO_EFFECT && r.status == 126); 121 CHECK(cap_calls == 0 && cap_buf[0] == 0); 122 CHECK(s.intents[0].closed && s.intents[0].outcome == OSH_O_FAILED_NO_EFFECT && s.intents[0].err == OSH_E_DENIED); 123 CHECK(nlogs == 1 && strstr(logs[0], "osh_op_receipt:") && strstr(logs[0], "result=DENIED") && strstr(logs[0], "effect=none")); 124 125 /* a capability without WRITE: denied */ 126 reset_capture(); 127 osh_session_init(&s, CK_R_READ); 128 r = run(&s, hello); 129 CHECK(r.error == OSH_E_DENIED && cap_calls == 0); 130 131 /* revoked between building the request and using it */ 132 reset_capture(); 133 osh_session_init(&s, CK_R_WRITE); 134 s.hooks.revoke_before_perm_at = 1; 135 r = run(&s, hello); 136 CHECK(r.error == OSH_E_REVOKED && r.outcome == OSH_O_FAILED_NO_EFFECT && cap_calls == 0); 137 r = run(&s, hello); /* and it stays revoked */ 138 CHECK(r.error == OSH_E_REVOKED && cap_calls == 0); 139 140 /* the binding checks, called directly */ 141 { 142 struct osh_binding b; 143 struct ck_handle h2; 144 osh_session_init(&s, CK_R_WRITE); 145 memset(&b, 0, sizeof b); 146 memcpy(b.principal_id, s.principal, 32); 147 b.domain = 1; 148 b.cap_index = s.console_cap.index; 149 b.cap_generation = s.console_cap.generation; 150 b.resource_class = OSH_RC_CONSOLE_LOG; 151 b.operation = OSH_OP_WRITE; 152 CHECK(osh_op_perm_check(&s, &b) == OSH_E_OK); 153 b.domain = 2; 154 CHECK(osh_op_perm_check(&s, &b) == OSH_E_CAP_DOMAIN_MISMATCH); 155 b.domain = 1; 156 b.cap_generation = 0x100000001ull; 157 CHECK(osh_op_perm_check(&s, &b) == OSH_E_CAP_GEN_NARROW); 158 b.cap_generation = s.console_cap.generation; 159 b.resource_class = 2; 160 CHECK(osh_op_perm_check(&s, &b) == OSH_E_DENIED); 161 b.resource_class = OSH_RC_CONSOLE_LOG; 162 b.operation = 2; 163 CHECK(osh_op_perm_check(&s, &b) == OSH_E_DENIED); 164 b.operation = OSH_OP_WRITE; 165 b.principal_id[0] ^= 1; 166 CHECK(osh_op_perm_check(&s, &b) == OSH_E_DENIED); 167 b.principal_id[0] ^= 1; 168 b.cap_generation = s.console_cap.generation + 5; /* never issued */ 169 CHECK(osh_op_perm_check(&s, &b) == OSH_E_DENIED); 170 b.cap_generation = s.console_cap.generation; 171 b.cap_index = 99; 172 CHECK(osh_op_perm_check(&s, &b) == OSH_E_DENIED); 173 b.cap_index = s.console_cap.index; 174 /* revoke, then the old handle is REVOKED; a new capability in the same slot makes the old handle STALE */ 175 CHECK(ck_cap_remove(&s.caps, s.console_cap) == CK_CAP_OK); 176 CHECK(osh_op_perm_check(&s, &b) == OSH_E_REVOKED); 177 CHECK(ck_cap_insert(&s.caps, OSH_RES_CONSOLE_LOG, CK_R_WRITE, &h2) == CK_CAP_OK); 178 CHECK(h2.index == b.cap_index && h2.generation > s.console_cap.generation); 179 CHECK(osh_op_perm_check(&s, &b) == OSH_E_STALE); 180 /* a capability on another resource does not authorize the console */ 181 osh_session_init(&s, 0); 182 CHECK(ck_cap_insert(&s.caps, 0x1234u, CK_R_WRITE, &s.console_cap) == CK_CAP_OK); 183 memcpy(s.cap_holder, s.principal, 32); /* the principal matches: only the resource is wrong */ 184 r = run(&s, hello); 185 CHECK(r.error == OSH_E_DENIED && cap_calls == 0); 186 } 187 188 /* an interrupt before the effect: cancelled, no bytes, the list stops */ 189 reset_capture(); 190 osh_session_init(&s, CK_R_WRITE); 191 s.hooks.interrupt_before_effect_at = 2; 192 r = run(&s, hello); 193 CHECK(r.error == OSH_E_OK && cap_calls == 1); 194 r = run(&s, hello); 195 CHECK(r.error == OSH_E_INTERRUPTED && r.outcome == OSH_O_CANCELLED && r.stop && r.status == 130 && cap_calls == 1); 196 CHECK(s.intents[1].closed && s.intents[1].outcome == OSH_O_CANCELLED); 197 CHECK(osh_session_close(&s) == 1); 198 CHECK(ck_cap_lookup(&s.caps, s.console_cap, CK_R_WRITE, 0) != CK_CAP_OK); /* capability released */ 199 200 /* lost after the effect: the outcome is unknown, restart reports it and never replays it */ 201 reset_capture(); 202 osh_session_init(&s, CK_R_WRITE); 203 s.hooks.crash_after_effect_at = 1; 204 r = run(&s, hello); 205 CHECK(r.crashed && r.stop && r.outcome == OSH_O_OUTCOME_UNKNOWN && cap_calls == 1); 206 CHECK(s.intents[0].used && !s.intents[0].closed); 207 osh_session_restart(&t, &s); 208 CHECK(t.console_cap.generation == 0); /* the restarted adapter holds no capability */ 209 nlogs = 0; 210 CHECK(osh_op_recover(&t, &env) == 1); 211 CHECK(cap_calls == 1); /* not replayed */ 212 CHECK(t.intents[0].closed && t.intents[0].outcome == OSH_O_OUTCOME_UNKNOWN); 213 CHECK(nlogs == 1 && strstr(logs[0], "outcome=OUTCOME_UNKNOWN") && strstr(logs[0], "replayed=0")); 214 CHECK(osh_op_recover(&t, &env) == 0 && nlogs == 1 && cap_calls == 1); /* idempotent */ 215 216 /* a failed console write is unknown, not "no effect" */ 217 reset_capture(); 218 osh_session_init(&s, CK_R_WRITE); 219 s.hooks.sink_fail_at = 1; 220 r = run(&s, hello); 221 CHECK(r.error == OSH_E_IO && r.outcome == OSH_O_OUTCOME_UNKNOWN && s.intents[0].outcome == OSH_O_OUTCOME_UNKNOWN); 222 223 /* refused before any effect or intent: unknown name, and what this slice cannot do yet */ 224 reset_capture(); 225 osh_session_init(&s, CK_R_WRITE); 226 const char *const ls[] = { "ls", 0 }; 227 r = run(&s, ls); 228 CHECK(r.error == OSH_E_NOT_FOUND && r.status == 127 && r.outcome == OSH_O_NOT_STARTED && !s.intents[0].used); 229 const char *const kl[] = { "klog", "a", 0 }; 230 build(2, kl, kl); 231 osh_op_exec(&s, &env, req, out, 8192, &r); 232 CHECK(r.error == OSH_E_NOT_SUPPORTED && r.status == 126 && cap_calls == 0 && !s.intents[0].used); /* a pipe */ 233 build(1, kl, 0); 234 req[8 + 2] = 1; /* nredir */ 235 osh_op_exec(&s, &env, req, out, 8192, &r); 236 CHECK(r.error == OSH_E_NOT_SUPPORTED && cap_calls == 0); 237 build(1, kl, 0); 238 req[8 + 1] = 1; /* nassign */ 239 osh_op_exec(&s, &env, req, out, 8192, &r); 240 CHECK(r.error == OSH_E_NOT_SUPPORTED && cap_calls == 0); 241 build(1, kl, 0); 242 req[8 + 3] = 3; /* builtin printf */ 243 osh_op_exec(&s, &env, req, out, 8192, &r); 244 CHECK(r.error == OSH_E_NOT_SUPPORTED && cap_calls == 0); 245 CHECK(!s.intents[0].used); 246 247 /* malformed request records are refused without using the memory */ 248 build(1, kl, 0); 249 req[0] ^= 1; 250 osh_op_exec(&s, &env, req, out, 8192, &r); 251 CHECK(r.error == OSH_E_INVALID_ARG && cap_calls == 0); 252 build(1, kl, 0); 253 req[8 + 5] = 99; /* argv[0] length past out_used */ 254 osh_op_exec(&s, &env, req, out, 8192, &r); 255 CHECK(r.error == OSH_E_INVALID_ARG && cap_calls == 0); 256 build(1, kl, 0); 257 out[0] = 0x14b; /* a cell that is not a byte */ 258 osh_op_exec(&s, &env, req, out, 8192, &r); 259 CHECK(r.error == OSH_E_INVALID_ARG && cap_calls == 0); 260 build(1, kl, 0); 261 req[1] = 9; /* ncmds over 8 */ 262 osh_op_exec(&s, &env, req, out, 8192, &r); 263 CHECK(r.error == OSH_E_INVALID_ARG && cap_calls == 0); 264 build(1, kl, 0); 265 req[4] = 9000; /* out_used over the OUT region */ 266 osh_op_exec(&s, &env, req, out, 8192, &r); 267 CHECK(r.error == OSH_E_INVALID_ARG && cap_calls == 0); 268 269 /* an argument cannot forge a record line, and an over-long line is a LIMIT before the effect */ 270 reset_capture(); 271 const char *const nl[] = { "klog", "a\nosh_op_record: forged", 0 }; 272 r = run(&s, nl); 273 CHECK(r.error == OSH_E_OK && strcmp(cap_buf, "osh_op_out: a.osh_op_record: forged\n") == 0); 274 char big[300]; 275 memset(big, 'x', 299); 276 big[299] = 0; 277 const char *const bg[] = { "klog", big, 0 }; 278 reset_capture(); 279 r = run(&s, bg); 280 CHECK(r.error == OSH_E_LIMIT && cap_calls == 0); 281 282 /* closed intents are reclaimed (their outcome is already logged): 40 sequential klogs succeed, each with a 283 * "grant used" receipt; only OPEN intents count against the limit */ 284 reset_capture(); 285 osh_session_init(&s, CK_R_WRITE); 286 int okn = 0; 287 for (int i = 0; i < 40; i++) 288 okn += run(&s, hello).error == OSH_E_OK; 289 CHECK(okn == 40 && cap_calls == 40); 290 CHECK(nlogs == 16); /* the log array keeps the first 16 lines */ 291 CHECK(strstr(logs[0], "osh_op_receipt: seq=1 op=console_write result=OK ") && strstr(logs[0], "effect=console_write")); 292 CHECK(strstr(logs[0], "cap_domain=1 cap_index=0 cap_generation=1 resource_class=1 operation=1") && strstr(logs[0], "principal=")); 293 /* the receipt names the request digest */ 294 { 295 reset_capture(); 296 osh_session_init(&s, CK_R_WRITE); 297 r = run(&s, hello); 298 static const char hex[] = "`0123456789abcdef`"; 299 char want[64 + 1]; 300 for (int i = 0; i < 32; i++) { 301 want[2 * i] = hex[r.digest[i] >> 4]; 302 want[2 * i + 1] = hex[r.digest[i] & 0xf]; 303 } 304 want[64] = '\0'; 305 CHECK(nlogs == 1 && strstr(logs[0], want)); 306 } 307 /* 16 open intents (the adapter lost after each effect) fill the log; the next pipeline is refused LIMIT with a receipt */ 308 reset_capture(); 309 osh_session_init(&s, CK_R_WRITE); 310 for (int i = 0; i < OSH_INTENT_MAX; i++) { 311 s.hooks.crash_after_effect_at = s.pipelines + 1; 312 r = run(&s, hello); 313 CHECK(r.crashed); 314 } 315 CHECK(cap_calls == OSH_INTENT_MAX); 316 nlogs = 0; 317 r = run(&s, hello); 318 CHECK(r.error == OSH_E_LIMIT && r.outcome == OSH_O_NOT_STARTED && cap_calls == OSH_INTENT_MAX); 319 CHECK(nlogs == 1 && strstr(logs[0], "osh_op_receipt:") && strstr(logs[0], "result=LIMIT") && strstr(logs[0], "effect=none")); 320 /* the denial receipts carry the binding too */ 321 reset_capture(); 322 osh_session_init(&s, 0); 323 r = run(&s, hello); 324 CHECK(nlogs == 1 && strstr(logs[0], "result=DENIED") && strstr(logs[0], "cap_domain=1") && strstr(logs[0], "principal=")); 325 326 /* the capability belongs to the principal it was granted to: another principal presenting it is DENIED */ 327 reset_capture(); 328 osh_session_init(&s, CK_R_WRITE); 329 CHECK(memcmp(s.cap_holder, s.principal, 32) == 0); 330 s.principal[0] ^= 1; /* a different task now runs the shell session */ 331 r = run(&s, hello); 332 CHECK(r.error == OSH_E_DENIED && cap_calls == 0); 333 osh_session_init(&s, 0); 334 { 335 static const uint8_t zero32[32]; 336 CHECK(memcmp(s.cap_holder, zero32, 32) == 0); /* nothing granted, no holder */ 337 } 338 339 /* names */ 340 CHECK(strcmp(osh_err_name(OSH_E_PARTIAL_LAUNCH), "PARTIAL_LAUNCH") == 0 && strcmp(osh_err_name(99), "IO") == 0); 341 CHECK(strcmp(osh_outcome_name(OSH_O_CANCELLED), "CANCELLED") == 0); 342 343 return ck_t_verdict("test_osh_op"); 344 }