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/store/torn_slot.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 / store / torn_slot.c` 278 lines ยท 11160 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 /* 18 * torn_slot.c -- see torn_slot.h for the protocol and its argument. 19 * No heap, no clock, no I/O except through the ts_device callbacks. 20 */ 21 #include "torn_slot.h" 22 #include "../argus/sha256.h" 23 24 #include 25 26 static const uint8_t MAGIC[8] = {'A', 'I', 'E', 'N', 'T', 'W', 'S', '1'}; 27 static const char RECORD_DOMAIN[] = "AIENOS-TORN-SLOT-RECORD-V1"; /* + its NUL */ 28 static const char HEADER_DOMAIN[] = "AIENOS-TORN-SLOT-HEADER-V1"; /* + its NUL */ 29 30 /* Header layout, little-endian, bytes 0..87 of the commit unit: 31 * 0 8 magic "AIENTWS1" 32 * 8 2 version = 1 33 * 10 1 slot id (0 = A, 1 = B) 34 * 11 1 reserved, zero 35 * 12 4 record bytes = 4096 36 * 16 8 seq (>= 1) 37 * 24 32 record digest = SHA256(RECORD_DOMAIN\0 || slot:u8 || seq:u64le || record[4096]) 38 * 56 32 header digest = SHA256(HEADER_DOMAIN\0 || bytes[0..56)) 39 * 88..4095 zero 40 */ 41 #define OFF_VERSION 8 42 #define OFF_SLOT 10 43 #define OFF_RESERVED 11 44 #define OFF_RECBYTES 12 45 #define OFF_SEQ 16 46 #define OFF_RECDIGEST 24 47 #define OFF_HDRDIGEST 56 48 49 static void put_u16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); } 50 static void put_u32(uint8_t *p, uint32_t v) { for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (8 * i)); } 51 static void put_u64(uint8_t *p, uint64_t v) { for (int i = 0; i < 8; i++) p[i] = (uint8_t)(v >> (8 * i)); } 52 static uint16_t get_u16(const uint8_t *p) { return (uint16_t)(p[0] | (p[1] << 8)); } 53 static uint32_t get_u32(const uint8_t *p) { uint32_t v = 0; for (int i = 3; i >= 0; i--) v = (v << 8) | p[i]; return v; } 54 static uint64_t get_u64(const uint8_t *p) { uint64_t v = 0; for (int i = 7; i >= 0; i--) v = (v << 8) | p[i]; return v; } 55 56 static void record_digest(int slot, uint64_t seq, const uint8_t rec[TS_RECORD_BYTES], 57 uint8_t out[SHA256_DIGEST_SIZE]) 58 { 59 sha256_ctx c; 60 uint8_t sb[9]; 61 sb[0] = (uint8_t)slot; 62 put_u64(sb + 1, seq); 63 sha256_init(&c); 64 sha256_update(&c, (const uint8_t *)RECORD_DOMAIN, sizeof RECORD_DOMAIN); /* includes NUL */ 65 sha256_update(&c, sb, sizeof sb); 66 sha256_update(&c, rec, TS_RECORD_BYTES); 67 sha256_final(&c, out); 68 } 69 70 static void header_digest(const uint8_t *hdr, uint8_t out[SHA256_DIGEST_SIZE]) 71 { 72 sha256_ctx c; 73 sha256_init(&c); 74 sha256_update(&c, (const uint8_t *)HEADER_DOMAIN, sizeof HEADER_DOMAIN); 75 sha256_update(&c, hdr, OFF_HDRDIGEST); 76 sha256_final(&c, out); 77 } 78 79 /* Constant-time compare is not needed (no secret), but avoid early exit 80 * anyway so timing never depends on where a tear landed. */ 81 static int digest_eq(const uint8_t *a, const uint8_t *b) 82 { 83 uint8_t d = 0; 84 for (unsigned i = 0; i < SHA256_DIGEST_SIZE; i++) d |= (uint8_t)(a[i] ^ b[i]); 85 return d == 0; 86 } 87 88 static int all_zero(const uint8_t *p, size_t n) 89 { 90 uint8_t acc = 0; 91 for (size_t i = 0; i < n; i++) acc |= p[i]; 92 return acc == 0; 93 } 94 95 void ts_encode_commit(uint8_t unit[TS_RECORD_BYTES], int slot, uint64_t seq, 96 const uint8_t rec[TS_RECORD_BYTES]) 97 { 98 memset(unit, 0, TS_RECORD_BYTES); 99 memcpy(unit, MAGIC, sizeof MAGIC); 100 put_u16(unit + OFF_VERSION, 1); 101 unit[OFF_SLOT] = (uint8_t)slot; 102 unit[OFF_RESERVED] = 0; 103 put_u32(unit + OFF_RECBYTES, TS_RECORD_BYTES); 104 put_u64(unit + OFF_SEQ, seq); 105 record_digest(slot, seq, rec, unit + OFF_RECDIGEST); 106 header_digest(unit, unit + OFF_HDRDIGEST); 107 } 108 109 int ts_open(ts_region *r, const ts_device *dev, uint64_t base_lba) 110 { 111 memset(r, 0, sizeof *r); 112 r->newest_slot = -1; 113 if (!dev || !dev->read || !dev->write || !dev->flush) return TS_EGEOMETRY; 114 if (dev->block_size != 512 && dev->block_size != 4096) return TS_EGEOMETRY; 115 uint32_t bpu = TS_RECORD_BYTES / dev->block_size; /* 8 or 1: a whole multiple */ 116 uint64_t need = (uint64_t)TS_UNITS * bpu; 117 if (base_lba > dev->block_count || dev->block_count - base_lba < need) return TS_EGEOMETRY; 118 r->dev = *dev; 119 r->base_lba = base_lba; 120 r->blocks_per_unit = bpu; 121 return TS_OK; 122 } 123 124 static int read_unit(ts_region *r, unsigned unit, uint8_t *buf) 125 { 126 uint64_t lba = r->base_lba + (uint64_t)unit * r->blocks_per_unit; 127 return r->dev.read(r->dev.ctx, lba, r->blocks_per_unit, buf) ? TS_EIO : TS_OK; 128 } 129 130 static int write_unit(ts_region *r, unsigned unit, const uint8_t *buf) 131 { 132 uint64_t lba = r->base_lba + (uint64_t)unit * r->blocks_per_unit; 133 return r->dev.write(r->dev.ctx, lba, r->blocks_per_unit, buf) ? TS_EIO : TS_OK; 134 } 135 136 /* Validate one slot. body receives the body bytes when valid. */ 137 static int check_slot(ts_region *r, int slot, uint8_t body[TS_RECORD_BYTES], 138 uint64_t *seq, ts_slot_state *st) 139 { 140 uint8_t cu[TS_RECORD_BYTES]; 141 uint8_t d[SHA256_DIGEST_SIZE]; 142 int e = read_unit(r, (unsigned)(2 * slot + 1), cu); 143 if (e) return e; 144 *st = TS_SLOT_INVALID; 145 if (all_zero(cu, TS_RECORD_BYTES)) { *st = TS_SLOT_BLANK; return TS_OK; } 146 if (memcmp(cu, MAGIC, sizeof MAGIC) != 0) return TS_OK; 147 if (get_u16(cu + OFF_VERSION) != 1) return TS_OK; 148 if (cu[OFF_SLOT] != (uint8_t)slot || cu[OFF_RESERVED] != 0) return TS_OK; 149 if (get_u32(cu + OFF_RECBYTES) != TS_RECORD_BYTES) return TS_OK; 150 if (!all_zero(cu + TS_HEADER_BYTES, TS_RECORD_BYTES - TS_HEADER_BYTES)) return TS_OK; 151 header_digest(cu, d); 152 if (!digest_eq(d, cu + OFF_HDRDIGEST)) return TS_OK; 153 uint64_t s = get_u64(cu + OFF_SEQ); 154 if (s == 0) return TS_OK; 155 e = read_unit(r, (unsigned)(2 * slot), body); 156 if (e) return e; 157 record_digest(slot, s, body, d); 158 if (!digest_eq(d, cu + OFF_RECDIGEST)) return TS_OK; 159 *seq = s; 160 *st = TS_SLOT_VALID; 161 return TS_OK; 162 } 163 164 /* With no valid slot and slot B blank, slot A's commit unit must look like 165 * what a crash during the very first commit can leave: every byte either 166 * zero (the blank unit before) or the byte the first header writes. The 167 * fixed fields of that header are known (slot 0, seq 1), so a nonzero byte 168 * that differs from them, or anything past the header, is corruption and 169 * not a torn write. The digest bytes cannot be checked this way; damage 170 * confined to them before any second commit reads as EMPTY. */ 171 static int unfinished_first_commit(ts_region *r, int *plausible) 172 { 173 uint8_t cu[TS_RECORD_BYTES]; 174 uint8_t t[OFF_RECDIGEST]; 175 *plausible = 0; 176 int e = read_unit(r, 1, cu); 177 if (e) return e; 178 memcpy(t, MAGIC, sizeof MAGIC); 179 put_u16(t + OFF_VERSION, 1); 180 t[OFF_SLOT] = 0; 181 t[OFF_RESERVED] = 0; 182 put_u32(t + OFF_RECBYTES, TS_RECORD_BYTES); 183 put_u64(t + OFF_SEQ, 1); 184 for (unsigned i = 0; i < OFF_RECDIGEST; i++) 185 if (cu[i] != 0 && cu[i] != t[i]) return TS_OK; 186 if (!all_zero(cu + TS_HEADER_BYTES, TS_RECORD_BYTES - TS_HEADER_BYTES)) return TS_OK; 187 *plausible = 1; 188 return TS_OK; 189 } 190 191 int ts_recover(ts_region *r, uint8_t out[TS_RECORD_BYTES], uint64_t *seq_out, 192 ts_slot_state slot_states[2]) 193 { 194 uint8_t body[2][TS_RECORD_BYTES]; 195 uint64_t seq[2] = {0, 0}; 196 ts_slot_state st[2] = {TS_SLOT_INVALID, TS_SLOT_INVALID}; 197 198 r->known = 0; 199 r->newest_slot = -1; 200 r->newest_seq = 0; 201 memset(out, 0, TS_RECORD_BYTES); 202 if (seq_out) *seq_out = 0; 203 if (r->blocks_per_unit == 0) return TS_EGEOMETRY; 204 205 for (int s = 0; s < 2; s++) { 206 int e = check_slot(r, s, body[s], &seq[s], &st[s]); 207 if (e) return e; 208 } 209 if (slot_states) { slot_states[0] = st[0]; slot_states[1] = st[1]; } 210 211 /* Lineage rules. The first commit goes to slot A with seq 1 and each 212 * commit goes to the slot not holding the newest record with seq + 1, so 213 * slot A only ever holds odd seqs and slot B even ones, two valid slots 214 * differ by exactly 1, and slot B is valid only after slot A was 215 * written (a written commit unit never tears back to all zero: its magic 216 * bytes are nonzero in both the old and the new header). A valid slot 217 * that breaks these was not made by this protocol plus crashes, so 218 * recovery fails closed. Each rule line carries a GUARD tag; the mutant 219 * test removes each one in turn and expects the suite to fail. */ 220 if (st[0] == TS_SLOT_VALID && st[1] == TS_SLOT_VALID && seq[0] == seq[1]) return TS_CONFLICT; 221 if (st[0] == TS_SLOT_VALID && (seq[0] & 1) != 1) return TS_CORRUPT; /* GUARD:parity-a */ 222 if (st[1] == TS_SLOT_VALID && (seq[1] & 1) != 0) return TS_CORRUPT; /* GUARD:parity-b */ 223 if (st[1] == TS_SLOT_VALID && st[0] == TS_SLOT_BLANK) return TS_CORRUPT; /* GUARD:b-needs-a */ 224 225 int pick = -1; 226 if (st[0] == TS_SLOT_VALID && st[1] == TS_SLOT_VALID) { 227 if (seq[0] - seq[1] != 1 && seq[1] - seq[0] != 1) return TS_CORRUPT; /* GUARD:adjacent */ 228 pick = seq[0] > seq[1] ? 0 : 1; 229 } else if (st[0] == TS_SLOT_VALID) { 230 pick = 0; 231 } else if (st[1] == TS_SLOT_VALID) { 232 pick = 1; 233 } 234 235 if (pick < 0) { 236 /* Slot B is only ever written after slot A held a valid record, and 237 * a valid newest slot is never rewritten, so "no valid slot" is a 238 * crash outcome only while B is still blank and A holds a possible 239 * remnant of the first commit. Anything else fails closed. */ 240 if (st[1] != TS_SLOT_BLANK) return TS_CORRUPT; 241 int plausible = 0; 242 int e = unfinished_first_commit(r, &plausible); 243 if (e) return e; 244 if (!plausible) return TS_CORRUPT; /* GUARD:first-remnant */ 245 r->known = 1; 246 return TS_EMPTY; 247 } 248 /* A valid slot A beside a blank slot B can only be the first commit. */ 249 if (pick == 0 && st[1] == TS_SLOT_BLANK && seq[0] != 1) return TS_CORRUPT; /* GUARD:a-alone-seq1 */ 250 memcpy(out, body[pick], TS_RECORD_BYTES); 251 if (seq_out) *seq_out = seq[pick]; 252 r->newest_slot = pick; 253 r->newest_seq = seq[pick]; 254 r->known = 1; 255 return TS_OK; 256 } 257 258 int ts_commit(ts_region *r, const uint8_t rec[TS_RECORD_BYTES]) 259 { 260 uint8_t cu[TS_RECORD_BYTES]; 261 if (!r->known) return TS_ESTATE; 262 if (r->newest_seq == UINT64_MAX) return TS_ESTATE; 263 int target = r->newest_slot < 0 ? 0 : 1 - r->newest_slot; 264 uint64_t seq = r->newest_seq + 1; 265 266 r->known = 0; /* any failure below requires a fresh recover() */ 267 int e = write_unit(r, (unsigned)(2 * target), rec); 268 if (e) return e; 269 if (r->dev.flush(r->dev.ctx)) return TS_EIO; 270 ts_encode_commit(cu, target, seq, rec); 271 e = write_unit(r, (unsigned)(2 * target + 1), cu); 272 if (e) return e; 273 if (r->dev.flush(r->dev.ctx)) return TS_EIO; 274 r->newest_slot = target; 275 r->newest_seq = seq; 276 r->known = 1; 277 return TS_OK; 278 }