interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / dev / tests / disk_part_test.c

371 lines · 16374 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/* disk_part_test.c -- host tests for the GPT parser and the LBA translation
18 * layer (dev/disk_part.c), hostile inputs included, plus a whole sealed-Store
19 * boot on the AIENOS partition view proving nothing outside the partition
20 * changes. Hosted test code only.
21 *
22 * Built twice by stage.mk (make disk-part-test): normally (must PASS) and
23 * with -DCK_TEST_DISK_XLATE_BYPASS (the translation-layer bypass mutation,
24 * which must FAIL these checks). */
25#include <fcntl.h>
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <unistd.h>
30#include "ck.h"
31#include "ck_host.h"
32#include "disk_file.h"
33#include "disk_layout.h"
34#include "disk_part.h"
35#include "gpt_write.h"
36#include "store_boot.h"
37
38static int checks, failures;
39#define CHECK(c) \
40 do { \
41 checks++; \
42 if (!(c)) { \
43 failures++; \
44 printf(" FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); \
45 } \
46 } while (0)
47
48static char g_path[256];
49#define MIB 64u
50
51static void put32(uint8_t *p, uint32_t v) { for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (8 * i)); }
52static void put64(uint8_t *p, uint64_t v) { for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i)); }
53
54/* Standard layout of tools/ck_gpt_image.c "aienos-middle" (MiB units). */
55static int make_image(uint32_t bs, const uint8_t *mid_type, const uint8_t *b_type, uint64_t mid_first_mib,
56 uint64_t mid_end_mib, int nparts, unsigned flags, uint64_t *first, uint64_t *last)
57{
58 uint64_t per = (1024u * 1024u) / bs;
59 gw_part p[3] = {
60 {gw_linux_type, 1 * per, 9 * per - 1u, "sentinel-a", 0xa5},
61 {mid_type, mid_first_mib * per, mid_end_mib * per - 1u, "AIENOS", 0},
62 {b_type, (MIB - 7u) * per, (MIB - 1u) * per - 1u, "sentinel-b", 0x5a},
63 };
64 if (first) *first = p[1].first;
65 if (last) *last = p[1].last;
66 return gw_write(g_path, bs, MIB * per, p, nparts, flags);
67}
68static int good_image(uint32_t bs, uint64_t *first, uint64_t *last)
69{
70 return make_image(bs, ck_aienos_part_type, gw_linux_type, 9, MIB - 7u, 3, 0, first, last);
71}
72
73static int find(uint32_t bs, ck_part *p, disk_file *f, disk_dev *d)
74{
75 ck_gpt_info gi;
76 if (disk_file_open(f, d, g_path, bs, 0, 0)) return 9999;
77 return ck_gpt_find_aienos(d, p, &gi);
78}
79static int find_rc(uint32_t bs)
80{
81 static ck_part p;
82 disk_file f;
83 disk_dev d;
84 int rc = find(bs, &p, &f, &d);
85 if (rc != 9999) disk_file_close(&f);
86 return rc;
87}
88
89static uint8_t *slurp(size_t *len)
90{
91 FILE *fp = fopen(g_path, "rb");
92 if (!fp) return NULL;
93 fseek(fp, 0, SEEK_END);
94 long n = ftell(fp);
95 fseek(fp, 0, SEEK_SET);
96 uint8_t *b = malloc((size_t)n);
97 if (b && fread(b, 1, (size_t)n, fp) != (size_t)n) {
98 free(b);
99 b = NULL;
100 }
101 fclose(fp);
102 *len = (size_t)n;
103 return b;
104}
105
106/* Rewrite one GPT header in place (field patch) and fix its CRC32. */
107static void patch_header(uint32_t bs, uint64_t lba, size_t off, uint64_t v, int width)
108{
109 int fd = open(g_path, O_RDWR);
110 uint8_t h[4096];
111 if (fd < 0 || pread(fd, h, bs, (off_t)(lba * bs)) != (ssize_t)bs) {
112 CHECK(!"patch_header read");
113 if (fd >= 0) close(fd);
114 return;
115 }
116 if (width == 4) put32(h + off, (uint32_t)v); else put64(h + off, v);
117 uint32_t hs = h[12] | (uint32_t)h[13] << 8;
118 if (hs > bs) hs = 92;
119 put32(h + 16, 0);
120 put32(h + 16, ck_crc32(0, h, hs));
121 CHECK(pwrite(fd, h, bs, (off_t)(lba * bs)) == (ssize_t)bs);
122 close(fd);
123}
124
125/* Crafted GPT with num entries of esize bytes; `used` consecutive 8-block
126 * Linux partitions starting at first_usable; the last one is AIENOS. */
127static void craft(uint32_t bs, uint64_t blocks, uint32_t num, uint32_t esize, uint32_t used)
128{
129 uint64_t eb = ((uint64_t)num * esize + bs - 1u) / bs, last = blocks - 1u;
130 uint64_t fu = 2u + eb, lu = last - 1u - eb;
131 uint8_t *ent = calloc(eb, bs), *h = calloc(1, bs), *m = calloc(1, bs);
132 for (uint32_t i = 0; i < used && i < num; i++) {
133 uint8_t *e = ent + (size_t)i * esize;
134 memcpy(e, i + 1 == used ? ck_aienos_part_type : gw_linux_type, 16);
135 e[16] = (uint8_t)(i + 1);
136 put64(e + 32, fu + 8u * i);
137 put64(e + 40, fu + 8u * i + 7u);
138 }
139 uint32_t ec = ck_crc32(0, ent, (size_t)num * esize);
140 int fd = open(g_path, O_RDWR | O_CREAT | O_TRUNC, 0644);
141 CHECK(fd >= 0 && ftruncate(fd, (off_t)(blocks * bs)) == 0);
142 m[446 + 4] = 0xee;
143 put32(m + 446 + 8, 1);
144 m[510] = 0x55;
145 m[511] = 0xaa;
146 CHECK(pwrite(fd, m, bs, 0) == (ssize_t)bs);
147 for (int k = 0; k < 2; k++) {
148 uint64_t my = k ? last : 1, alt = k ? 1 : last, el = k ? last - eb : 2;
149 memset(h, 0, bs);
150 memcpy(h, "EFI PART", 8);
151 put32(h + 8, 0x00010000u);
152 put32(h + 12, 92);
153 put64(h + 24, my);
154 put64(h + 32, alt);
155 put64(h + 40, fu);
156 put64(h + 48, lu);
157 memcpy(h + 56, "CRAFTED-DISKGUID", 16);
158 put64(h + 72, el);
159 put32(h + 80, num);
160 put32(h + 84, esize);
161 put32(h + 88, ec);
162 put32(h + 16, ck_crc32(0, h, 92));
163 CHECK(pwrite(fd, h, bs, (off_t)(my * bs)) == (ssize_t)bs);
164 CHECK(pwrite(fd, ent, (size_t)(eb * bs), (off_t)(el * bs)) == (ssize_t)(eb * bs));
165 }
166 close(fd);
167 free(ent);
168 free(h);
169 free(m);
170}
171
172static void test_crc32(void)
173{
174 CHECK(ck_crc32(0, (const uint8_t *)"123456789", 9) == 0xcbf43926u); /* standard check value */
175 uint32_t c = ck_crc32(0, (const uint8_t *)"1234", 4);
176 CHECK(ck_crc32(c, (const uint8_t *)"56789", 5) == 0xcbf43926u); /* chaining */
177}
178
179static void test_good_and_xlate(uint32_t bs)
180{
181 uint64_t f0 = 0, l0 = 0;
182 CHECK(good_image(bs, &f0, &l0) == 0);
183 static ck_part p;
184 disk_file f;
185 disk_dev d;
186 CHECK(find(bs, &p, &f, &d) == CK_GPT_OK);
187 CHECK(p.valid == 1 && p.first_lba == f0 && p.blocks == l0 - f0 + 1u);
188 CHECK(p.dev.block_count == p.blocks && p.dev.block_size == bs && p.dev.ctx == &p);
189 uint64_t a = 0;
190 CHECK(ck_part_xlate(&p, 0, 1, &a) == DISK_OK && a == f0);
191 CHECK(ck_part_xlate(&p, p.blocks - 1u, 1, &a) == DISK_OK && a == l0);
192 CHECK(ck_part_xlate(&p, p.blocks, 1, &a) == DISK_ERANGE);
193 CHECK(ck_part_xlate(&p, p.blocks - 1u, 2, &a) == DISK_ERANGE);
194 CHECK(ck_part_xlate(&p, ~(uint64_t)0, 1, &a) == DISK_ERANGE);
195 CHECK(ck_part_xlate(&p, ~(uint64_t)0 - p.first_lba + 1u, 1, &a) == DISK_ERANGE); /* offset wrap */
196 CHECK(ck_part_xlate(&p, 0, 0, &a) == DISK_EARG);
197 ck_part bad = p;
198 bad.valid = 0;
199 CHECK(ck_part_xlate(&bad, 0, 1, &a) == DISK_ESTATE);
200
201 /* Real writes through the view: first and last partition block land at
202 * the partition's absolute first/last LBA; past the end is refused both
203 * by disk.c and by the translation layer itself (backend callback called
204 * directly); nothing outside [f0, l0] changes. */
205 size_t n0 = 0, n1 = 0;
206 disk_file_close(&f);
207 uint8_t *before = slurp(&n0);
208 CHECK(find(bs, &p, &f, &d) == CK_GPT_OK);
209 uint8_t *blk = malloc(bs);
210 memset(blk, 0xc3, bs);
211 CHECK(disk_write(&p.dev, 0, 1, blk) == DISK_OK);
212 CHECK(disk_write(&p.dev, p.blocks - 1u, 1, blk) == DISK_OK);
213 CHECK(disk_write(&p.dev, p.blocks, 1, blk) == DISK_ERANGE);
214 CHECK(p.dev.write(p.dev.ctx, p.blocks, 1, blk) == DISK_ERANGE);
215 CHECK(p.dev.write(p.dev.ctx, p.blocks - 1u, 2, blk) == DISK_ERANGE);
216 CHECK(p.dev.read(p.dev.ctx, p.blocks, 1, blk) == DISK_ERANGE);
217 CHECK(disk_flush(&p.dev) == DISK_OK);
218 disk_file_close(&f);
219 uint8_t *after = slurp(&n1);
220 CHECK(before && after && n0 == n1);
221 if (before && after && n0 == n1) {
222 CHECK(memcmp(before, after, (size_t)(f0 * bs)) == 0); /* MBR, primary GPT, sentinel-a */
223 CHECK(memcmp(before + (l0 + 1u) * bs, after + (l0 + 1u) * bs, n0 - (size_t)((l0 + 1u) * bs)) == 0); /* sentinel-b, backup */
224 CHECK(after[f0 * bs] == 0xc3 && after[l0 * bs] == 0xc3);
225 }
226 free(before);
227 free(after);
228 free(blk);
229 /* The image still parses after the writes (GPT untouched). */
230 CHECK(find_rc(bs) == CK_GPT_OK);
231}
232
233/* A whole sealed-Store boot (format + two commits) on the AIENOS partition
234 * view: the GPT, both sentinels and the backup stay byte-identical. */
235static void test_store_on_partition(uint32_t bs)
236{
237 uint64_t f0 = 0, l0 = 0;
238 CHECK(good_image(bs, &f0, &l0) == 0);
239 size_t n0 = 0, n1 = 0;
240 uint8_t *before = slurp(&n0);
241 static ck_part p;
242 disk_file f;
243 disk_dev d;
244 CHECK(find(bs, &p, &f, &d) == CK_GPT_OK);
245 ss_keys k;
246 ck_store_report r;
247 ck_store_test_keys(&k);
248 ck_host_quiet = 1;
249 int rc1 = store_boot_run(&p.dev, &k, ck_store_test_uuid, "disk-part-test", &r);
250 int fm = r.formatted;
251 int rc2 = store_boot_run(&p.dev, &k, ck_store_test_uuid, "disk-part-test", &r);
252 ck_host_quiet = 0;
253 CHECK(rc1 == 0 && fm == 1);
254 CHECK(rc2 == 0 && r.boot_count_new == 2 && r.anchor_lba == 0);
255 CHECK(r.store_units == p.blocks / (CK_LAYOUT_UNIT / bs) - CK_LAYOUT_ANCHOR_UNITS - CK_LAYOUT_PROBE_UNITS);
256 disk_file_close(&f);
257 uint8_t *after = slurp(&n1);
258 CHECK(before && after && n0 == n1);
259 if (before && after && n0 == n1) {
260 CHECK(memcmp(before, after, (size_t)(f0 * bs)) == 0);
261 CHECK(memcmp(before + (l0 + 1u) * bs, after + (l0 + 1u) * bs, n0 - (size_t)((l0 + 1u) * bs)) == 0);
262 CHECK(memcmp(before + f0 * bs, after + f0 * bs, (size_t)((l0 - f0 + 1u) * bs)) != 0); /* the Store wrote inside */
263 }
264 free(before);
265 free(after);
266 memset(&k, 0, sizeof k);
267 CHECK(find_rc(bs) == CK_GPT_OK);
268}
269
270static void test_hostile(uint32_t bs)
271{
272 uint64_t per = (1024u * 1024u) / bs, blocks = MIB * per, last = blocks - 1u;
273 /* No GPT at all (sentinel data only), and a GPT without a protective MBR. */
274 CHECK(make_image(bs, ck_aienos_part_type, gw_linux_type, 9, MIB - 7u, 3, GW_NO_GPT, 0, 0) == 0);
275 CHECK(find_rc(bs) == CK_GPT_ENOMBR);
276 CHECK(make_image(bs, ck_aienos_part_type, gw_linux_type, 9, MIB - 7u, 3, GW_NO_PMBR, 0, 0) == 0);
277 CHECK(find_rc(bs) == CK_GPT_ENOMBR);
278 /* CRC32: primary header (valid backup present: still refused, no fallback). */
279 CHECK(make_image(bs, ck_aienos_part_type, gw_linux_type, 9, MIB - 7u, 3, GW_BAD_PRIMARY_CRC, 0, 0) == 0);
280 CHECK(find_rc(bs) == CK_GPT_EHDR_CRC);
281 CHECK(make_image(bs, ck_aienos_part_type, gw_linux_type, 9, MIB - 7u, 3, GW_BAD_PRIMARY_CRC | GW_BAD_BACKUP_CRC, 0, 0) == 0);
282 CHECK(find_rc(bs) == CK_GPT_EHDR_CRC);
283 CHECK(make_image(bs, ck_aienos_part_type, gw_linux_type, 9, MIB - 7u, 3, GW_BAD_PRIMARY_ENT, 0, 0) == 0);
284 CHECK(find_rc(bs) == CK_GPT_EENT_CRC);
285 CHECK(make_image(bs, ck_aienos_part_type, gw_linux_type, 9, MIB - 7u, 3, GW_BAD_PRIMARY_ENT | GW_BAD_BACKUP_ENT, 0, 0) == 0);
286 CHECK(find_rc(bs) == CK_GPT_EENT_CRC);
287 CHECK(make_image(bs, ck_aienos_part_type, gw_linux_type, 9, MIB - 7u, 3, GW_BAD_BACKUP_CRC, 0, 0) == 0);
288 CHECK(find_rc(bs) == CK_GPT_EBACKUP);
289 CHECK(make_image(bs, ck_aienos_part_type, gw_linux_type, 9, MIB - 7u, 3, GW_BAD_BACKUP_ENT, 0, 0) == 0);
290 CHECK(find_rc(bs) == CK_GPT_EBACKUP);
291 /* Partition table semantics. */
292 CHECK(make_image(bs, gw_linux_type, gw_linux_type, 9, MIB - 7u, 3, 0, 0, 0) == 0);
293 CHECK(find_rc(bs) == CK_GPT_ENOPART);
294 CHECK(make_image(bs, ck_aienos_part_type, ck_aienos_part_type, 9, MIB - 7u, 3, 0, 0, 0) == 0);
295 CHECK(find_rc(bs) == CK_GPT_EMULTI);
296 CHECK(make_image(bs, ck_aienos_part_type, gw_linux_type, 9, MIB + 1u, 2, 0, 0, 0) == 0); /* ends past the disk */
297 CHECK(find_rc(bs) == CK_GPT_EOUTSIDE);
298 CHECK(make_image(bs, ck_aienos_part_type, gw_linux_type, 0, MIB - 7u, 2, 0, 0, 0) == 0); /* starts on the GPT */
299 CHECK(find_rc(bs) == CK_GPT_EOUTSIDE);
300 CHECK(make_image(bs, ck_aienos_part_type, gw_linux_type, 5, MIB - 7u, 3, 0, 0, 0) == 0); /* overlaps sentinel-a */
301 CHECK(find_rc(bs) == CK_GPT_EOVERLAP);
302
303 /* Header field attacks on an otherwise good image (CRC re-computed). */
304 struct { size_t off; uint64_t v; int w; int want; } hp[] = {
305 {80, 0xffffffffu, 4, CK_GPT_EENT}, /* entry count overflow */
306 {80, 0, 4, CK_GPT_EENT}, /* no entries */
307 {84, 0x80000000u, 4, CK_GPT_EENT}, /* entry size overflow */
308 {84, 64, 4, CK_GPT_EENT}, /* entry size below 128 */
309 {84, 136, 4, CK_GPT_EENT}, /* entry size not 128 x 2^n */
310 {24, 2, 8, CK_GPT_EHDR}, /* MyLBA not 1 */
311 {32, 7, 8, CK_GPT_EHDR}, /* AlternateLBA not the last LBA */
312 {72, ~(uint64_t)0, 8, CK_GPT_EHDR}, /* entry LBA overflow */
313 {72, 1, 8, CK_GPT_EHDR}, /* entries on the header */
314 {40, 3, 8, CK_GPT_EHDR}, /* first usable inside the entry array */
315 {48, ~(uint64_t)0, 8, CK_GPT_EHDR}, /* last usable past the disk */
316 {12, 8192, 4, CK_GPT_ESIG}, /* header size larger than a block */
317 {12, 91, 4, CK_GPT_ESIG}, /* header size below 92 */
318 {8, 0x00020000u, 4, CK_GPT_ESIG}, /* unknown revision */
319 };
320 for (size_t i = 0; i < sizeof hp / sizeof hp[0]; i++) {
321 CHECK(good_image(bs, 0, 0) == 0);
322 patch_header(bs, 1, hp[i].off, hp[i].v, hp[i].w);
323 int rc = find_rc(bs);
324 if (rc != hp[i].want) printf(" header patch %zu: rc=%d want %d\n", i, rc, hp[i].want);
325 CHECK(rc == hp[i].want);
326 }
327 /* Backup header disagreeing with the primary (both CRCs valid). */
328 CHECK(good_image(bs, 0, 0) == 0);
329 patch_header(bs, last, 48, 100u, 8);
330 CHECK(find_rc(bs) == CK_GPT_EBACKUP);
331 /* Disk grown after partitioning: the backup is no longer at the last LBA. */
332 CHECK(good_image(bs, 0, 0) == 0);
333 CHECK(truncate(g_path, (off_t)((blocks + per) * bs)) == 0);
334 CHECK(find_rc(bs) == CK_GPT_EHDR);
335 /* Disk too small for any GPT. */
336 CHECK(truncate(g_path, (off_t)(4u * bs)) == 0);
337 CHECK(find_rc(bs) == CK_GPT_EGEOM);
338
339 /* Crafted tables (CRCs valid): 256 entries of 128 bytes with 129 used is
340 * over the used-entry bound; 256 x 128 with 3 used and 128 x 256 with 2
341 * used are accepted. */
342 craft(bs, blocks, 256, 128, 129);
343 CHECK(find_rc(bs) == CK_GPT_ETOOMANY);
344 craft(bs, blocks, 256, 128, 3);
345 CHECK(find_rc(bs) == CK_GPT_OK);
346 craft(bs, blocks, 128, 256, 2);
347 CHECK(find_rc(bs) == CK_GPT_OK);
348 /* A 1024 x 512-byte entry array (512 KiB) overruns the first usable LBA. */
349 CHECK(good_image(bs, 0, 0) == 0);
350 patch_header(bs, 1, 80, 1024, 4);
351 patch_header(bs, 1, 84, 512, 4);
352 CHECK(find_rc(bs) == CK_GPT_EHDR);
353}
354
355int main(void)
356{
357 snprintf(g_path, sizeof g_path, "/tmp/aienos-disk-part-test-%d.img", (int)getpid());
358#ifdef CK_TEST_DISK_XLATE_BYPASS
359 printf("disk_part_test: TEST-ONLY translation bypass mutation build (these checks must FAIL)\n");
360#endif
361 test_crc32();
362 for (uint32_t bs = 512; bs <= 4096; bs *= 8) {
363 test_good_and_xlate(bs);
364 test_store_on_partition(bs);
365 test_hostile(bs);
366 }
367 unlink(g_path);
368 printf("disk_part_test: %d checks, %d failures\n", checks, failures);
369 printf("CK_DISK_PART_HOST: %s\n", failures ? "FAIL" : "PASS");
370 return failures ? 1 : 0;
371}