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 / tests / swap_disk_bound_test.c
| 1 | /* Hosted verification oracle for swap_disk_read.h (QEMU-only test-disk inbox). A mocked |
| 2 | * sector reader stands in for AHCI and records every sector it is asked to write, so the |
| 3 | * test sees both the accept/reject decision and that a rejected object is never read. */ |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include <stdint.h> |
| 8 | #include <stddef.h> |
| 9 | #define ck_puts(s) ((void)(s)) |
| 10 | #define ck_printf(...) ((void)0) |
| 11 | #include "../swap_disk_read.h" |
| 12 | |
| 13 | #define CHECK(v) do { if (!(v)) { fprintf(stderr, "swap disk bound failed at %d\n", __LINE__); return 1; } count++; } while (0) |
| 14 | |
| 15 | static uint64_t g_img_len; /* image_len the mocked disk reports in the manifest */ |
| 16 | static unsigned g_reads; /* reads of more than the header sector */ |
| 17 | static unsigned g_max_sectors; /* largest bulk read requested */ |
| 18 | static int g_bad_magic, g_fail_bulk; |
| 19 | |
| 20 | static int mock_reader(uint64_t lba, unsigned count, uint8_t *buf) |
| 21 | { |
| 22 | (void)lba; |
| 23 | memset(buf, 0xA5, (size_t)count * CK_SWAP_SECTOR); |
| 24 | if (count == 1) { |
| 25 | memset(buf, 0, CK_SWAP_SECTOR); |
| 26 | memcpy(buf, g_bad_magic ? "NOTASLOT" : CK_SLOT_MANIFEST_MAGIC, 8); |
| 27 | memcpy(buf + 48, &g_img_len, 8); |
| 28 | return 0; |
| 29 | } |
| 30 | g_reads++; |
| 31 | if (g_fail_bulk) |
| 32 | return -1; |
| 33 | if (count > g_max_sectors) |
| 34 | g_max_sectors = count; |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | /* Returns the load result; fills *bulk with the number of bulk reads issued. */ |
| 39 | static int load(size_t cap, uint64_t img_len, size_t *len, unsigned *bulk, uint8_t *buf) |
| 40 | { |
| 41 | g_img_len = img_len; g_reads = 0; g_max_sectors = 0; |
| 42 | int rc = ck_swap_disk_load(mock_reader, 2048, buf, cap, len); |
| 43 | *bulk = g_reads; |
| 44 | return rc; |
| 45 | } |
| 46 | |
| 47 | int main(void) |
| 48 | { |
| 49 | unsigned count = 0, bulk; |
| 50 | size_t len, total; |
| 51 | unsigned ns; |
| 52 | enum { CAP = 4u << 20 }; |
| 53 | uint8_t *buf = malloc(CAP + 8192); |
| 54 | CHECK(buf); |
| 55 | |
| 56 | /* Exact boundary through the real function: cap - manifest accepted, one over rejected. */ |
| 57 | CHECK(load(CAP, CAP - CK_SLOT_UNIT, &len, &bulk, buf) == 0); |
| 58 | CHECK(len == CAP && bulk == 1 && g_max_sectors == CAP / 512); |
| 59 | CHECK(load(CAP, CAP - CK_SLOT_UNIT + 1, &len, &bulk, buf) != 0); |
| 60 | CHECK(len == 0 && bulk == 0); |
| 61 | /* The old check let img_len == cap through; the read then ran 4096 bytes past cap. */ |
| 62 | CHECK(load(CAP, CAP, &len, &bulk, buf) != 0 && bulk == 0); |
| 63 | CHECK(load(CAP, CAP + 1, &len, &bulk, buf) != 0 && bulk == 0); |
| 64 | CHECK(load(CAP, 0, &len, &bulk, buf) == 0 && len == CK_SLOT_UNIT && g_max_sectors == 8); |
| 65 | |
| 66 | /* Buffer smaller than the manifest unit, including zero. */ |
| 67 | CHECK(load(0, 0, &len, &bulk, buf) != 0 && bulk == 0); |
| 68 | CHECK(load(CK_SLOT_UNIT - 1, 0, &len, &bulk, buf) != 0 && bulk == 0); |
| 69 | CHECK(load(CK_SLOT_UNIT, 0, &len, &bulk, buf) == 0 && len == CK_SLOT_UNIT); |
| 70 | CHECK(load(CK_SLOT_UNIT, 1, &len, &bulk, buf) != 0 && bulk == 0); |
| 71 | |
| 72 | /* Partial-sector rounding: the object fits in bytes but its rounded read does not. |
| 73 | * cap = manifest + 100 leaves a 100-byte image needing a 4608-byte (9-sector) read. */ |
| 74 | CHECK(load(CK_SLOT_UNIT + 100, 100, &len, &bulk, buf) != 0 && bulk == 0); |
| 75 | CHECK(load(CK_SLOT_UNIT + 512, 512, &len, &bulk, buf) == 0 && len == CK_SLOT_UNIT + 512); |
| 76 | CHECK(load(CK_SLOT_UNIT + 512, 513, &len, &bulk, buf) != 0 && bulk == 0); |
| 77 | CHECK(load(CK_SLOT_UNIT + 511, 511, &len, &bulk, buf) != 0 && bulk == 0); |
| 78 | /* Image shorter than a sector under a sector-aligned cap rounds up inside cap. */ |
| 79 | CHECK(load(CK_SLOT_UNIT + 512, 100, &len, &bulk, buf) == 0 && |
| 80 | len == CK_SLOT_UNIT + 100 && g_max_sectors == 9); |
| 81 | |
| 82 | /* Overflow-safe: lengths near the top of u64 and size_t are refused, never wrapped. */ |
| 83 | CHECK(load(CAP, UINT64_MAX, &len, &bulk, buf) != 0 && bulk == 0); |
| 84 | CHECK(load(CAP, UINT64_MAX - CK_SLOT_UNIT + 1, &len, &bulk, buf) != 0 && bulk == 0); |
| 85 | CHECK(load(CAP, (uint64_t)SIZE_MAX, &len, &bulk, buf) != 0 && bulk == 0); |
| 86 | CHECK(ck_swap_disk_extent(SIZE_MAX, UINT64_MAX, &total, &ns) != 0); |
| 87 | CHECK(ck_swap_disk_extent(SIZE_MAX, SIZE_MAX - CK_SLOT_UNIT, &total, &ns) != 0); |
| 88 | CHECK(ck_swap_disk_extent((size_t)1 << 45, ((uint64_t)1 << 45) - CK_SLOT_UNIT, &total, &ns) != 0); /* sector count over u32 */ |
| 89 | |
| 90 | /* Helper contract: success fills total and a nonzero sector count. */ |
| 91 | CHECK(ck_swap_disk_extent(CAP, 0, &total, &ns) == 0 && total == CK_SLOT_UNIT && ns == 8); |
| 92 | CHECK(ck_swap_disk_extent(CAP, 1, &total, &ns) == 0 && total == CK_SLOT_UNIT + 1 && ns == 9); |
| 93 | |
| 94 | /* Bad magic and an unreadable disk are refused before any bulk read. */ |
| 95 | g_bad_magic = 1; |
| 96 | CHECK(load(CAP, 0, &len, &bulk, buf) != 0 && bulk == 0 && len == 0); |
| 97 | g_bad_magic = 0; g_fail_bulk = 1; |
| 98 | CHECK(load(CAP, 0, &len, &bulk, buf) != 0 && bulk == 1 && len == 0); |
| 99 | free(buf); |
| 100 | printf("SWAP_DISK_BOUND: PASS checks=%u\n", count); |
| 101 | return 0; |
| 102 | } |