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 / x86 / local_tests.inc
| 1 | /* Deliberately hostile machine code, only in CK_LOCAL_TEST images. These paths |
| 2 | * are absent from release builds: network callers can only name signed tools. */ |
| 3 | static unsigned local_checks; |
| 4 | static char wire_answer[256]; |
| 5 | static int wire_send(const char *s, unsigned n) |
| 6 | { |
| 7 | if (n >= sizeof wire_answer) return -1; |
| 8 | memcpy(wire_answer, s, n); wire_answer[n] = 0; return 0; |
| 9 | } |
| 10 | static void check(int ok, const char *name) |
| 11 | { |
| 12 | if (!ok) ck_panic("local_test: FAIL %s", name); |
| 13 | local_checks++; |
| 14 | } |
| 15 | static int zeroed(const void *p, size_t n) |
| 16 | { |
| 17 | const uint8_t *b = p; |
| 18 | for (size_t i = 0; i < n; i++) if (b[i]) return 0; |
| 19 | return 1; |
| 20 | } |
| 21 | static void reclaimed(void) |
| 22 | { |
| 23 | check(!running && ck_rd_cr3() == kernel_root && zeroed(root, sizeof root) && |
| 24 | zeroed(pt, sizeof pt) && zeroed(code, sizeof code) && zeroed(input, sizeof input) && |
| 25 | zeroed(workspace, sizeof workspace) && zeroed(stack, sizeof stack) && |
| 26 | zeroed(meta, sizeof meta) && zeroed(stub, sizeof stub) && zeroed(&frame, sizeof frame) && |
| 27 | zeroed(ck_local_user_fp, sizeof ck_local_user_fp) && zeroed(irqstack, sizeof irqstack), |
| 28 | "private bytes and translations reclaimed"); |
| 29 | } |
| 30 | static void run_done(void) |
| 31 | { |
| 32 | for (unsigned i = 0; running && i < 2000; i++) ck_local_step(); |
| 33 | check(!running, "bounded completion"); |
| 34 | reclaimed(); |
| 35 | } |
| 36 | static void raw(const uint8_t *bytes, size_t len, unsigned state, unsigned why) |
| 37 | { |
| 38 | ck_local_begin(91); |
| 39 | running = &calls[0].result; |
| 40 | running->state = CK_LOCAL_RUNNING; |
| 41 | check(!prepare(bytes, len, 0, 0), "hostile fixture prepared"); |
| 42 | run_done(); |
| 43 | check(calls[0].result.state == state && calls[0].result.reason == why && |
| 44 | !calls[0].result.length, "hostile fixture contained"); |
| 45 | int64_t a[3] = {1, 20, 22}; |
| 46 | ck_local_begin(92); |
| 47 | const struct ck_local_result *r = ck_local_start(92,1,1,a,sizeof a); |
| 48 | run_done(); |
| 49 | check(r->state == CK_LOCAL_RETURNED && r->length == 8 && r->output[0] == 42, |
| 50 | "successful tool immediately after fault"); |
| 51 | } |
| 52 | static void access_probe(uint64_t address, int write, int execute) |
| 53 | { |
| 54 | uint8_t b[16] = {0x48,0xb8}; /* MOVABS RAX, address; access; RET */ |
| 55 | memcpy(b + 2, &address, 8); |
| 56 | b[10] = execute ? 0xff : 0x48; |
| 57 | b[11] = execute ? 0xe0 : write ? 0x89 : 0x8b; |
| 58 | b[12] = 0x00; b[13] = 0xc3; |
| 59 | raw(b, 14, CK_LOCAL_ABNORMAL, CK_LOCAL_FAULT); |
| 60 | } |
| 61 | #ifdef CK_LOCAL_MODEL |
| 62 | #include "model/tests.inc" |
| 63 | #endif |
| 64 | void ck_local_selftest(void) |
| 65 | { |
| 66 | check(approved[0].ok && approved[1].ok && approved[2].ok, "signed tools admitted"); |
| 67 | static uint8_t mutated[66000]; |
| 68 | struct ck_local_artifact mutant = ck_local_artifacts[0]; |
| 69 | mutant.bytes = mutated; |
| 70 | const uint8_t *loaded; size_t loaded_len; |
| 71 | const unsigned offsets[] = {12,14,16,36,128+2,192,240,0}; |
| 72 | for (unsigned i = 0; i < sizeof offsets / sizeof offsets[0]; i++) { |
| 73 | memcpy(mutated, ck_local_artifacts[0].bytes, mutant.length); |
| 74 | mutated[offsets[i]] ^= 0x40; |
| 75 | check(admit(&mutant,&loaded,&loaded_len) != 0, "tampered artifact refused"); |
| 76 | } |
| 77 | memcpy(mutated, ck_local_artifacts[0].bytes, mutant.length); |
| 78 | mutated[mutant.length-1] ^= 1; |
| 79 | check(admit(&mutant,&loaded,&loaded_len) != 0, "bad signature refused"); |
| 80 | memcpy(mutated, ck_local_artifacts[0].bytes, mutant.length); |
| 81 | mutant.length--; |
| 82 | check(admit(&mutant,&loaded,&loaded_len) != 0, "truncated artifact refused"); |
| 83 | mutant = ck_local_artifacts[0]; mutant.hash[0] ^= 1; |
| 84 | check(admit(&mutant,&loaded,&loaded_len) != 0, "nonallowlisted identity refused"); |
| 85 | struct cka_parsed parsed; |
| 86 | check(cka_parse(ck_local_artifacts[0].bytes, ck_local_artifacts[0].length, &parsed) == CKA_WRONG_TARGET, |
| 87 | "ARM admission still excludes x86"); |
| 88 | int64_t args[3] = {1, 20, 22}; |
| 89 | ck_local_begin(1); |
| 90 | const struct ck_local_result *r = ck_local_start(1, 1, 1, args, sizeof args); |
| 91 | run_done(); |
| 92 | int64_t value; memcpy(&value, r->output, 8); |
| 93 | check(r->state == CK_LOCAL_RETURNED && r->length == 8 && value == 42, "Omega arithmetic ABI"); |
| 94 | check(ck_local_start(1, 1, 1, args, sizeof args) == r && !running && used == 1, "idempotent replay"); |
| 95 | args[1]++; |
| 96 | check(ck_local_start(1, 1, 1, args, sizeof args)->state == CK_LOCAL_REFUSED, "changed replay refused"); |
| 97 | check(ck_local_start(2, 1, 1, args, sizeof args)->state == CK_LOCAL_REFUSED, "stale request refused"); |
| 98 | for (unsigned i = 2; i <= 4; i++) { ck_local_start(1, i, 1, args, sizeof args); run_done(); } |
| 99 | check(ck_local_start(1, 5, 1, args, sizeof args)->state == CK_LOCAL_REFUSED, "four call ceiling"); |
| 100 | ck_local_begin(2); |
| 101 | args[0] = 1; args[1] = INT64_MAX; args[2] = 1; |
| 102 | r = ck_local_start(2, 1, 1, args, sizeof args); run_done(); |
| 103 | check(r->state == CK_LOCAL_TRAPPED && r->reason == 1 && !r->length, "signed overflow"); |
| 104 | args[0] = 4; args[2] = 0; |
| 105 | r = ck_local_start(2, 2, 1, args, sizeof args); run_done(); |
| 106 | check(r->state == CK_LOCAL_TRAPPED && r->reason == 2 && !r->length, "division by zero"); |
| 107 | const char text[] = "one\ttwo\nthree "; |
| 108 | r = ck_local_start(2, 3, 2, text, sizeof text - 1); run_done(); |
| 109 | uint64_t counts[3]; memcpy(counts, r->output, sizeof counts); |
| 110 | check(r->state == CK_LOCAL_RETURNED && r->length == 24 && counts[0] == sizeof text - 1 && |
| 111 | counts[1] == 3 && counts[2] == 1, "Omega text ABI"); |
| 112 | r = ck_local_start(2, 4, 3, 0, 0); run_done(); |
| 113 | check(r->state == CK_LOCAL_RETURNED && r->length == 40 && equal(r->output, (void *)snapshot, 40), |
| 114 | "Omega cached system ABI"); |
| 115 | ck_local_begin(12); args[0] = 2; args[1] = -8; args[2] = -5; |
| 116 | r = ck_local_start(12,1,1,args,sizeof args); run_done(); memcpy(&value,r->output,8); |
| 117 | check(r->state == CK_LOCAL_RETURNED && value == -3, "signed subtraction"); |
| 118 | args[0] = 4; args[1] = -22; args[2] = 7; |
| 119 | r = ck_local_start(12,2,1,args,sizeof args); run_done(); memcpy(&value,r->output,8); |
| 120 | check(r->state == CK_LOCAL_RETURNED && value == -3, "signed division truncates toward zero"); |
| 121 | args[1] = INT64_MIN; args[2] = -1; |
| 122 | r = ck_local_start(12,3,1,args,sizeof args); run_done(); |
| 123 | check(r->state == CK_LOCAL_TRAPPED && r->reason == 1, "minimum divided by minus one"); |
| 124 | static uint8_t longtext[4096]; memset(longtext,'a',sizeof longtext); longtext[4095] = '\n'; |
| 125 | r = ck_local_start(12,4,2,longtext,sizeof longtext); run_done(); memcpy(counts,r->output,sizeof counts); |
| 126 | check(r->state == CK_LOCAL_RETURNED && counts[0] == 4096 && counts[1] == 1 && counts[2] == 1, |
| 127 | "full input page"); |
| 128 | ck_local_begin(13); |
| 129 | int64_t overflow[4][3] = {{3, INT64_MAX, 2}, {2, INT64_MIN, 1}, |
| 130 | {2, INT64_MAX, -1}, {3, INT64_MIN, -1}}; |
| 131 | for (unsigned i = 0; i < 4; i++) { |
| 132 | r = ck_local_start(13, i + 1, 1, overflow[i], sizeof overflow[i]); run_done(); |
| 133 | check(r->state == CK_LOCAL_TRAPPED && r->reason == 1 && !r->length, |
| 134 | "multiply/subtract overflow reports range failure without output"); |
| 135 | } |
| 136 | |
| 137 | access_probe(kernel_root, 0, 0); access_probe(kernel_root, 1, 0); |
| 138 | access_probe(CODE, 1, 0); access_probe(INPUT, 1, 0); access_probe(META, 1, 0); |
| 139 | access_probe(STACK - PAGE, 0, 0); access_probe(TOP, 1, 0); |
| 140 | access_probe(WORK, 0, 1); access_probe(STACK, 0, 1); |
| 141 | access_probe(0xfee00000, 0, 0); /* LAPIC device memory */ |
| 142 | const uint8_t cli[] = {0xfa,0xc3}, hlt[] = {0xf4,0xc3}, io[] = {0xe6,0x80,0xc3}; |
| 143 | const uint8_t syscall[] = {0x0f,0x05,0xc3}, sysenter[] = {0x0f,0x34,0xc3}; |
| 144 | raw(cli, sizeof cli, CK_LOCAL_ABNORMAL, CK_LOCAL_FAULT); |
| 145 | raw(hlt, sizeof hlt, CK_LOCAL_ABNORMAL, CK_LOCAL_FAULT); |
| 146 | raw(io, sizeof io, CK_LOCAL_ABNORMAL, CK_LOCAL_FAULT); |
| 147 | raw(syscall, sizeof syscall, CK_LOCAL_ABNORMAL, CK_LOCAL_FAULT); |
| 148 | raw(sysenter, sizeof sysenter, CK_LOCAL_ABNORMAL, CK_LOCAL_FAULT); |
| 149 | const uint8_t avx[] = {0xc5,0xf8,0x77,0xc3}; /* VZEROUPPER: outside the SSE2 local ABI */ |
| 150 | raw(avx, sizeof avx, CK_LOCAL_ABNORMAL, CK_LOCAL_FAULT); |
| 151 | const uint8_t spoof[] = {0xcd,0x80,0xc3}; |
| 152 | raw(spoof, sizeof spoof, CK_LOCAL_ABNORMAL, CK_LOCAL_BAD_RETURN); |
| 153 | /* Dirty output followed by UD2 must not publish even one byte. */ |
| 154 | uint8_t dirty[] = {0x48,0xb8,0,0,0,0,0,0,0,0,0xc6,0x00,0x41,0x0f,0x0b}; |
| 155 | uint64_t dest = WORK; memcpy(dirty+2,&dest,8); |
| 156 | raw(dirty,sizeof dirty,CK_LOCAL_ABNORMAL,CK_LOCAL_FAULT); |
| 157 | /* XMM0 and caller XMM14 survive timer exits; the initial user XMM14 is |
| 158 | * zero rather than the kernel sentinel. The long loop forces preemption. */ |
| 159 | uint8_t fpcode[] = {0x48,0xb8,42,0,0,0,0,0,0,0,0x66,0x48,0x0f,0x6e,0xc0, |
| 160 | 0xb9,0x40,0x4b,0x4c,0, /* ECX = 5000000 */ |
| 161 | 0xff,0xc9,0x75,0xfc,0x48,0xb8,0,0,0,0,0,0,0,0, |
| 162 | 0x66,0x0f,0xd6,0x00,0x66,0x44,0x0f,0xd6,0x70,0x08, |
| 163 | 0xb8,16,0,0,0,0xc3}; |
| 164 | dest = WORK; memcpy(fpcode+26,&dest,8); |
| 165 | uint8_t saved_fp[512] __attribute__((aligned(16))); |
| 166 | uint64_t sentinel[2] = {0x12345678,0x87654321}, observed[2]; |
| 167 | __asm__ volatile("fxsave64 %0; movdqu %1, %%xmm14" : "=m"(saved_fp) : "m"(sentinel) : "memory"); |
| 168 | ck_local_begin(93); running = &calls[0].result; running->state = CK_LOCAL_RUNNING; |
| 169 | check(!prepare(fpcode,sizeof fpcode,0,0), "FP fixture prepared"); |
| 170 | ck_local_step(); |
| 171 | check(running != 0, "timer preempted register fixture"); |
| 172 | run_done(); |
| 173 | __asm__ volatile("movdqu %%xmm14, %0; fxrstor64 %1" : "=m"(observed) : "m"(saved_fp) : "memory"); |
| 174 | check(equal((void *)sentinel,(void *)observed,16), "kernel FP preserved"); |
| 175 | check(calls[0].result.state == CK_LOCAL_RETURNED && calls[0].result.length == 16 && |
| 176 | calls[0].result.output[0] == 42 && zeroed(calls[0].result.output+8,8), |
| 177 | "user FP preserved and kernel FP not disclosed"); |
| 178 | const uint8_t loop[] = {0xeb,0xfe}; |
| 179 | raw(loop, sizeof loop, CK_LOCAL_ABNORMAL, CK_LOCAL_CPU); |
| 180 | ck_local_begin(3); running = &calls[0].result; running->state = CK_LOCAL_RUNNING; |
| 181 | check(!prepare(loop, sizeof loop, 0, 0), "cancel fixture prepared"); |
| 182 | ck_local_step(); ck_local_cancel(); reclaimed(); |
| 183 | check(calls[0].result.reason == CK_LOCAL_CANCELLED, "cancel between slices"); |
| 184 | ck_local_begin(4); running = &calls[0].result; running->state = CK_LOCAL_RUNNING; |
| 185 | check(!prepare(loop, sizeof loop, 0, 0), "elapsed fixture prepared"); |
| 186 | started = ck_rdtsc() - hz; ck_local_step(); reclaimed(); |
| 187 | check(calls[0].result.reason == CK_LOCAL_ELAPSED, "elapsed ceiling"); |
| 188 | ck_local_wire_begin(77); |
| 189 | ck_local_wire_line("CALL 77 1 1 24 0 010000000000000014000000000000001600000000000000"); |
| 190 | run_done(); |
| 191 | const ck_courier fake = {.send=wire_send}; |
| 192 | ck_local_wire_step(&fake); |
| 193 | check(!memcmp(wire_answer, "RESULT 77 1 2 0 2a00000000000000", 31), "wire to real Omega result"); |
| 194 | wire_answer[0] = 0; |
| 195 | ck_local_wire_line("POLL 76 1"); ck_local_wire_step(&fake); |
| 196 | check(!wire_answer[0], "old wire request ignored"); |
| 197 | ck_local_wire_line("POLL 77 1"); ck_local_wire_step(&fake); |
| 198 | check(wire_answer[0] == 'R', "result replay"); |
| 199 | ck_local_wire_begin(0); |
| 200 | args[0] = 3; args[1] = -7; args[2] = 6; |
| 201 | for (unsigned i = 0; i < 1000; i++) { |
| 202 | ck_local_begin(100 + i); |
| 203 | r = ck_local_start(100 + i, 1, 1, args, sizeof args); run_done(); |
| 204 | memcpy(&value, r->output, 8); |
| 205 | check(r->state == CK_LOCAL_RETURNED && value == -42, "repeated successful cleanup"); |
| 206 | } |
| 207 | ck_local_begin(0); |
| 208 | #ifdef CK_LOCAL_MODEL |
| 209 | model_selftest(); |
| 210 | #endif |
| 211 | ck_printf("local_test: PASS checks=%u repeated=1000 real_cpl3=yes\n", local_checks); |
| 212 | /* Optional COM2 fixture is exercised by the full courier oracle. The |
| 213 | * model selects calls; every result comes from this real CPL3 runner. */ |
| 214 | const ck_courier *wire = ck_x86_courier_init(); |
| 215 | if (wire) { |
| 216 | uint64_t start = ck_rdtsc(); int sent = 0, done = 0; |
| 217 | ck_local_wire_begin(4242); |
| 218 | while (ck_rdtsc() - start < 30 * hz && !done) { |
| 219 | char line[320]; |
| 220 | if (wire->ready() && !sent) { |
| 221 | const char job[] = "JOB 4242 do Exercise the approved local tools.\tCheck arithmetic, text and system.\tlocal-tools=v1"; |
| 222 | check(!wire->send(job, sizeof job - 1), "courier test job sent"); sent = 1; |
| 223 | } |
| 224 | while (wire->take(line, sizeof line)) { |
| 225 | if (ck_local_wire_line(line)) continue; |
| 226 | if (!memcmp(line,"SAY 4242 1/1 VERIFIED",20)) done = 1; |
| 227 | } |
| 228 | ck_local_step(); ck_local_wire_step(wire); |
| 229 | } |
| 230 | check(done, "courier model loop completed"); |
| 231 | ck_local_wire_begin(0); |
| 232 | ck_puts("local_courier: PASS real_cpl3_results=yes\n"); |
| 233 | /* The model oracle has now exited. This must reach the same native |
| 234 | * arithmetic program and complete without any model connection. */ |
| 235 | start = ck_rdtsc(); sent = done = 0; |
| 236 | ck_local_wire_begin(4243); |
| 237 | while (ck_rdtsc() - start < 5 * hz && !done) { |
| 238 | char line[320]; |
| 239 | if (wire->ready() && !sent) { |
| 240 | const char job[] = "JOB 4243 do Calculate locally.\tWhat is 123 multiplied by 456?\tlocal-tools=v1"; |
| 241 | check(!wire->send(job, sizeof job - 1), "direct arithmetic job sent"); sent = 1; |
| 242 | } |
| 243 | while (wire->take(line, sizeof line)) { |
| 244 | if (ck_local_wire_line(line)) continue; |
| 245 | if (strlen(line) == sizeof("SAY 4243 1/1 56,088.") - 1 && !memcmp(line, "SAY 4243 1/1 56,088.", sizeof("SAY 4243 1/1 56,088.") - 1)) done = 1; |
| 246 | } |
| 247 | ck_local_step(); ck_local_wire_step(wire); |
| 248 | } |
| 249 | check(done, "direct native arithmetic without model completed"); |
| 250 | ck_local_wire_begin(0); |
| 251 | ck_printf("local_direct: PASS result=56088 elapsed_ms=%llu real_cpl3=yes\n", |
| 252 | (unsigned long long)((ck_rdtsc() - start) / (hz / 1000))); |
| 253 | } |
| 254 | } |