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 / tests / test_klog_slot.c

99 lines · 4116 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_klog_slot.c -- core/klog_slot.c: the map check and the header match
18 * refuse every way a write could land somewhere it should not. */
19#include <string.h>
20#include "ck_test.h"
21#include "klog_slot.h"
22
23static struct ck_klog_map good_map(void)
24{
25 struct ck_klog_map m;
26 memset(&m, 0, sizeof m);
27 memcpy(m.magic, CK_KLOG_MAP_MAGIC, 8);
28 m.version = 1;
29 m.sector_size = 512;
30 m.lba = 123456;
31 m.count = 4096;
32 for (int i = 0; i < 16; i++)
33 m.nonce[i] = (uint8_t)(0xa0 + i);
34 return m;
35}
36
37static struct ck_klog_hdr empty_hdr(const struct ck_klog_map *m)
38{
39 struct ck_klog_hdr h;
40 memset(&h, 0, sizeof h);
41 memcpy(h.magic, CK_KLOG_HDR_MAGIC, 8);
42 h.version = 1;
43 memcpy(h.nonce, m->nonce, 16);
44 h.lba = m->lba;
45 h.count = m->count;
46 return h;
47}
48
49int main(void)
50{
51 const char *why = 0;
52 struct ck_klog_map m = good_map(), b;
53 CHECK(ck_klog_map_check(&m, 0, &why) == 0);
54 CHECK(ck_klog_map_check(&m, 123456 + 4095, &why) == 0); /* last sector is the card's last */
55 CHECK(ck_klog_map_check(&m, 123456 + 4094, &why) == -1 && strstr(why, "past the end"));
56 b = m; b.magic[0] = 'X';
57 CHECK(ck_klog_map_check(&b, 0, &why) == -1 && strstr(why, "magic"));
58 b = m; b.version = 2;
59 CHECK(ck_klog_map_check(&b, 0, &why) == -1 && strstr(why, "version"));
60 b = m; b.sector_size = 4096;
61 CHECK(ck_klog_map_check(&b, 0, &why) == -1 && strstr(why, "sector size"));
62 b = m; b.count = 1;
63 CHECK(ck_klog_map_check(&b, 0, &why) == -1 && strstr(why, "length"));
64 b = m; b.count = CK_KLOG_MAX_SECTORS + 1;
65 CHECK(ck_klog_map_check(&b, 0, &why) == -1 && strstr(why, "length"));
66 b = m; b.lba = 0;
67 CHECK(ck_klog_map_check(&b, 0, &why) == -1 && strstr(why, "sector 0"));
68 b = m; b.lba = UINT64_MAX - 10;
69 CHECK(ck_klog_map_check(&b, 0, &why) == -1 && strstr(why, "overflows"));
70 b = m; memset(b.nonce, 0, 16);
71 CHECK(ck_klog_map_check(&b, 0, &why) == -1 && strstr(why, "nonce"));
72
73 struct ck_klog_hdr h = empty_hdr(&m), c;
74 CHECK(ck_klog_hdr_match(&h, &m, &why) == 0);
75 c = h; c.magic[7] = '2';
76 CHECK(ck_klog_hdr_match(&c, &m, &why) == -1 && strstr(why, "no slot header"));
77 c = h; c.version = 9;
78 CHECK(ck_klog_hdr_match(&c, &m, &why) == -1 && strstr(why, "version"));
79 c = h; c.nonce[15] ^= 1; /* another run's slot, or another card */
80 CHECK(ck_klog_hdr_match(&c, &m, &why) == -1 && strstr(why, "nonce"));
81 c = h; c.lba++; /* this header was copied somewhere else */
82 CHECK(ck_klog_hdr_match(&c, &m, &why) == -1 && strstr(why, "position"));
83 c = h; c.count--;
84 CHECK(ck_klog_hdr_match(&c, &m, &why) == -1 && strstr(why, "position"));
85 struct ck_klog_hdr z;
86 memset(&z, 0, sizeof z); /* blank sector: a wrong LBA on a fresh card */
87 CHECK(ck_klog_hdr_match(&z, &m, &why) == -1);
88
89 /* The WRITTEN header still matches the map (a rerun can find it). */
90 struct ck_klog_hdr w;
91 memset(&w, 0x5a, sizeof w);
92 ck_klog_hdr_written(&w, &m, 1000, 2000, 0x1234, "final_stage_name_that_is_far_too_long_to_fit");
93 CHECK(ck_klog_hdr_match(&w, &m, &why) == 0);
94 CHECK(w.state == CK_KLOG_STATE_WRITTEN && w.log_len == 1000 && w.log_total == 2000 && w.commit == 0x1234);
95 CHECK(w.stage[sizeof w.stage - 1] == 0 && strncmp(w.stage, "final_stage", 11) == 0);
96 CHECK(w.pad[0] == 0 && w.pad[sizeof w.pad - 1] == 0);
97 CHECK(ck_klog_slot_bytes(&m) == 4095u * 512u);
98 return ck_t_verdict("test_klog_slot");
99}