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_ipc.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_ipc.c` 211 lines ยท 9665 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_ipc.c -- host tests for core/ipc.c: the caps.rs and ipc.rs test lists 18 * (generation checks, attenuation, cross-table revoke, tombstones, bounded 19 * FIFO channel, gated IPC calls) plus the exact sequence the EL0 IPC demo runs. */ 20 #include "ck_test.h" 21 #include "ipc.h" 22 23 #define H(i, g) ((struct ck_handle){ (i), (g) }) 24 25 static struct ck_msg msg(uint32_t kind) 26 { 27 struct ck_msg m = { 0 }; 28 m.kind = kind; 29 m.payload[0] = kind * 3u; 30 m.object = kind; 31 return m; 32 } 33 34 static int msg_eq(const struct ck_msg *a, const struct ck_msg *b) 35 { 36 return a->kind == b->kind && a->payload[0] == b->payload[0] && 37 a->payload[1] == b->payload[1] && a->payload[2] == b->payload[2] && 38 a->region_base == b->region_base && a->region_pages == b->region_pages && 39 a->object == b->object; 40 } 41 42 int main(void) 43 { 44 struct ck_cap_table t, u, v; 45 struct ck_handle h, h2, c, gc; 46 uint32_t res = 0; 47 48 /* raw handles: round trip, generation 0 is forged */ 49 struct ck_handle r; 50 CHECK(ck_handle_raw(H(3, 7)) == ((7ull << 32) | 3)); 51 CHECK(ck_handle_from_raw((7ull << 32) | 3, &r) == 0 && r.index == 3 && r.generation == 7); 52 CHECK(ck_handle_from_raw(5, &r) == -1); 53 54 /* stale handle after slot reuse */ 55 ck_cap_init(&t, 1, 2); 56 CHECK(ck_cap_insert(&t, 10, CK_R_READ, &h) == 0); 57 CHECK(ck_cap_remove(&t, h) == 0); 58 CHECK(ck_cap_insert(&t, 11, CK_R_READ, &h2) == 0); 59 CHECK(h2.index == h.index && h2.generation != h.generation); 60 CHECK(ck_cap_lookup(&t, h, CK_R_READ, &res) == CK_CAP_INVALID); 61 CHECK(ck_cap_lookup(&t, h2, CK_R_READ, &res) == 0 && res == 11); 62 CHECK(ck_cap_lookup(&t, h2, CK_R_WRITE, &res) == CK_CAP_MISSING_RIGHTS); 63 CHECK(ck_cap_lookup(&t, H(9, 1), CK_R_READ, &res) == CK_CAP_INVALID); 64 65 /* generation exhaustion retires the slot */ 66 ck_cap_init(&t, 1, 1); 67 t.generations[0] = 0xfffffffeu; 68 CHECK(ck_cap_insert(&t, 1, CK_R_READ, &h) == 0 && h.generation == 0xffffffffu); 69 CHECK(ck_cap_remove(&t, h) == 0); 70 CHECK(ck_cap_insert(&t, 1, CK_R_READ, &h2) == CK_CAP_FULL); 71 72 /* derive cannot add rights; needs DERIVE */ 73 ck_cap_init(&t, 1, 8); 74 ck_cap_insert(&t, 5, CK_R_READ | CK_R_DERIVE, &h); 75 CHECK(ck_cap_derive(&t, h, CK_R_READ | CK_R_WRITE, &c) == CK_CAP_ESCALATION); 76 CHECK(ck_cap_derive(&t, h, CK_R_READ, &c) == 0); 77 CHECK(ck_cap_derive(&t, c, CK_R_READ, &gc) == CK_CAP_MISSING_RIGHTS); 78 CHECK(ck_cap_insert(&t, 5, 0x40, &h2) == CK_CAP_ESCALATION); 79 80 /* revoke cascades two levels across tables */ 81 ck_cap_init(&t, 1, 8); 82 ck_cap_init(&u, 2, 8); 83 ck_cap_init(&v, 3, 8); 84 ck_cap_insert(&t, 5, CK_R_ALL, &h); 85 CHECK(ck_cap_derive_into(&t, h, &u, CK_R_READ | CK_R_DERIVE, &c) == 0); 86 CHECK(ck_cap_derive_into(&u, c, &v, CK_R_READ, &gc) == 0); 87 ck_cap_insert(&u, 6, CK_R_READ, &h2); /* unrelated survivor */ 88 struct ck_cap_table *const three[3] = { &t, &u, &v }; 89 CHECK(ck_cap_revoke(2, c, three, 3) == CK_CAP_MISSING_RIGHTS); 90 CHECK(ck_cap_revoke(1, h, three, 3) == 0); 91 CHECK(ck_cap_lookup(&t, h, 0, 0) == CK_CAP_INVALID); 92 CHECK(ck_cap_lookup(&u, c, 0, 0) == CK_CAP_INVALID); 93 CHECK(ck_cap_lookup(&v, gc, 0, 0) == CK_CAP_INVALID); 94 CHECK(ck_cap_lookup(&u, h2, CK_R_READ, &res) == 0 && res == 6); 95 struct ck_cap_table *const dup[2] = { &t, &t }; 96 CHECK(ck_cap_revoke(1, h, dup, 2) == CK_CAP_DUPLICATE_TABLE); 97 98 /* revoke with single-slot tables */ 99 ck_cap_init(&t, 1, 1); 100 ck_cap_init(&u, 2, 1); 101 ck_cap_insert(&t, 5, CK_R_ALL, &h); 102 CHECK(ck_cap_derive_into(&t, h, &u, CK_R_READ, &c) == 0); 103 struct ck_cap_table *const two[2] = { &t, &u }; 104 CHECK(ck_cap_revoke(1, h, two, 2) == 0); 105 CHECK(ck_cap_insert(&t, 5, CK_R_READ, &h2) == 0 && ck_cap_insert(&u, 5, CK_R_READ, &h2) == 0); 106 107 /* cyclic parent metadata fails closed before removing anything */ 108 ck_cap_init(&t, 1, 2); 109 ck_cap_init(&u, 2, 2); 110 ck_cap_insert(&t, 5, CK_R_ALL, &h); 111 ck_cap_insert(&u, 5, CK_R_ALL, &c); 112 ck_cap_insert(&u, 5, CK_R_ALL, &gc); 113 u.slots[c.index].has_parent = 1; 114 u.slots[c.index].parent_table = 2; 115 u.slots[c.index].parent = gc; 116 u.slots[gc.index].has_parent = 1; 117 u.slots[gc.index].parent_table = 2; 118 u.slots[gc.index].parent = c; 119 CHECK(ck_cap_revoke(1, h, two, 2) == CK_CAP_FULL); 120 CHECK(ck_cap_lookup(&t, h, 0, 0) == 0); 121 122 /* transfer needs GRANT; full table reported */ 123 ck_cap_init(&t, 1, 8); 124 ck_cap_init(&u, 2, 1); 125 ck_cap_insert(&t, 5, CK_R_READ, &h); 126 CHECK(ck_cap_transfer(&t, h, &u, &c) == CK_CAP_MISSING_RIGHTS); 127 ck_cap_insert(&t, 5, CK_R_READ | CK_R_GRANT, &h2); 128 CHECK(ck_cap_transfer(&t, h2, &u, &c) == 0); 129 CHECK(ck_cap_transfer(&t, h2, &u, &gc) == CK_CAP_FULL); 130 131 /* removing a derived parent tombstones it; subtree stays revocable */ 132 ck_cap_init(&t, 1, 8); 133 ck_cap_init(&u, 2, 8); 134 ck_cap_insert(&t, 5, CK_R_ALL, &h); 135 ck_cap_derive_into(&t, h, &u, CK_R_READ | CK_R_DERIVE | CK_R_REVOKE, &c); 136 ck_cap_derive_into(&u, c, &t, CK_R_READ, &gc); 137 CHECK(ck_cap_remove(&u, c) == 0); 138 CHECK(ck_cap_lookup(&u, c, 0, 0) == CK_CAP_INVALID); 139 CHECK(u.slots[c.index].tombstone == 1); 140 CHECK(ck_cap_lookup(&t, gc, CK_R_READ, 0) == 0); 141 CHECK(ck_cap_revoke(1, h, two, 2) == 0); 142 CHECK(ck_cap_lookup(&t, gc, 0, 0) == CK_CAP_INVALID); 143 CHECK(u.slots[c.index].live == 0); 144 /* removing a never-derived capability frees its slot */ 145 ck_cap_init(&t, 1, 1); 146 ck_cap_insert(&t, 5, CK_R_READ, &h); 147 CHECK(ck_cap_remove(&t, h) == 0 && t.slots[0].live == 0); 148 CHECK(ck_cap_remove(&t, h) == CK_CAP_INVALID); 149 150 /* channel is FIFO and bounded */ 151 struct ck_channel ch; 152 struct ck_msg m1 = msg(1), m2 = msg(2), out; 153 ck_chan_init(&ch, 2); 154 CHECK(ck_chan_send(&ch, &m1) == 0 && ck_chan_send(&ch, &m2) == 0); 155 struct ck_msg m3 = msg(3); 156 CHECK(ck_chan_send(&ch, &m3) == -1); 157 CHECK(ck_chan_recv(&ch, &out) == 0 && msg_eq(&out, &m1)); 158 CHECK(ck_chan_send(&ch, &m3) == 0); 159 CHECK(ck_chan_recv(&ch, &out) == 0 && msg_eq(&out, &m2)); 160 CHECK(ck_chan_recv(&ch, &out) == 0 && msg_eq(&out, &m3)); 161 CHECK(ck_chan_recv(&ch, &out) == -1); 162 163 /* gated IPC */ 164 static struct ck_ipc ipc; 165 struct ck_handle a_send, a_obj, b_recv, child; 166 uint64_t val = 0; 167 ck_ipc_reset(&ipc, &a_send, &a_obj, &b_recv); 168 struct ck_msg m7 = msg(7); 169 CHECK(ck_ipc_recv(&ipc, 0, a_send, &out) == CK_IPC_MISSING_RIGHTS); 170 CHECK(ck_ipc_send(&ipc, 1, b_recv, &m7) == CK_IPC_MISSING_RIGHTS); 171 CHECK(ck_ipc_send(&ipc, 0, a_obj, &m7) == CK_IPC_WRONG_RESOURCE); 172 CHECK(ck_ipc_send(&ipc, 0, H(a_send.index, a_send.generation + 1), &m7) == 173 CK_IPC_INVALID_HANDLE); 174 CHECK(ck_ipc_recv(&ipc, 1, b_recv, &out) == CK_IPC_CHANNEL_EMPTY); 175 /* delegation without DERIVE, and escalation, are MissingRights */ 176 struct ck_handle plain; 177 ck_cap_insert(&ipc.tables[0], CK_IPC_OBJECT_RESOURCE, CK_R_READ, &plain); 178 CHECK(ck_ipc_delegate(&ipc, 0, plain, 1, CK_R_READ, &child) == CK_IPC_MISSING_RIGHTS); 179 CHECK(ck_ipc_delegate(&ipc, 0, a_obj, 1, CK_R_READ | CK_R_MAP, &child) == 180 CK_IPC_MISSING_RIGHTS); 181 CHECK(ck_ipc_delegate(&ipc, 0, a_obj, 1, CK_R_READ | CK_R_WRITE, &child) == 0); 182 CHECK(ck_ipc_object_write(&ipc, 1, child, 3, 77) == 0 && ipc.object[3] == 77); 183 CHECK(ck_ipc_object_read(&ipc, 1, child, 4, &val) == CK_IPC_WRONG_RESOURCE); 184 185 /* end to end: the exact EL0 demo sequence */ 186 ck_ipc_reset(&ipc, &a_send, &a_obj, &b_recv); 187 struct ck_msg sent = { .kind = 7, .payload = { 0x1111, 0x2222, 0x3333 }, 188 .region_base = 0xa000, .region_pages = 2, .object = 0 }; 189 CHECK(ck_ipc_send(&ipc, 0, a_send, &sent) == 0); 190 CHECK(ck_ipc_delegate(&ipc, 0, a_obj, 1, CK_R_READ, &child) == 0); 191 CHECK(ck_ipc_recv(&ipc, 1, b_recv, &out) == 0 && msg_eq(&out, &sent)); 192 CHECK(ck_ipc_object_read(&ipc, 1, child, 0, &val) == 0 && val == 0xc0ffee01); 193 CHECK(ck_ipc_object_write(&ipc, 1, child, 0, 0xbad) == CK_IPC_MISSING_RIGHTS); 194 struct ck_handle forged; 195 CHECK(ck_handle_from_raw(ck_handle_raw(child) ^ (1ull << 32), &forged) == -1); 196 forged = H(child.index, child.generation + 1); 197 CHECK(ck_ipc_object_read(&ipc, 1, forged, 0, &val) == CK_IPC_INVALID_HANDLE); 198 CHECK(ck_ipc_revoke(&ipc, 1, child) == CK_IPC_MISSING_RIGHTS); 199 CHECK(ck_ipc_revoke(&ipc, 0, a_obj) == 0); 200 CHECK(ck_ipc_object_read(&ipc, 1, child, 1, &val) == CK_IPC_INVALID_HANDLE); 201 CHECK(ck_ipc_object_read(&ipc, 0, a_obj, 1, &val) == CK_IPC_INVALID_HANDLE); 202 CHECK(ck_ipc_send(&ipc, 0, a_send, &sent) == 0); /* unrelated caps survive */ 203 204 /* receiver table full: source untouched */ 205 ck_ipc_reset(&ipc, &a_send, &a_obj, &b_recv); 206 for (int i = 0; i < CK_CAP_SLOTS - 1; i++) 207 ck_cap_insert(&ipc.tables[1], 1, CK_R_READ, &h); 208 CHECK(ck_ipc_delegate(&ipc, 0, a_obj, 1, CK_R_READ, &child) == CK_IPC_DELEGATION_FAILED); 209 CHECK(ipc.tables[0].slots[a_obj.index].derived == 0); 210 return ck_t_verdict("test_ipc"); 211 }