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/tests/store_powercut_test.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 / tests / store_powercut_test.c` 221 lines ยท 8875 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 /* Power cut at every block boundary of one transaction, plain Store and 18 * sealed store, 512- and 4096-byte geometry, file-backed (disk_file). After 19 * each cut the device is reopened and must show exactly the old state or 20 * exactly the new state. */ 21 #include "store_test_util.h" 22 #include "store_rig.h" 23 #include "store_v1.h" 24 25 static ss_workspace g_ws; 26 static uint8_t base_img[RIG_BYTES]; 27 static char g_path[512]; 28 static uint8_t A[3000], B[9000], C[100]; 29 30 static void fill(uint8_t *p, size_t n, uint32_t seed) 31 { 32 for (size_t i = 0; i < n; i++) p[i] = (uint8_t)((i * 37u + seed * 11u + (i >> 8)) & 0xff); 33 } 34 35 static int plain_has(st_store *s, uint16_t kind, const uint8_t *p, size_t n) 36 { 37 uint8_t id[32]; 38 static uint8_t out[16384]; 39 size_t len; 40 if (sv1_object_id(kind, 1, p, n, id) != 0) return 0; 41 if (st_read_object(s, id, out, sizeof out, &len) != 0) return 0; 42 return len == n && memcmp(out, p, n) == 0; 43 } 44 45 static int sealed_has(ss_store *s, const uint8_t sid[32], const uint8_t *p, size_t n) 46 { 47 static uint8_t out[SS_MAX_PLAINTEXT]; 48 size_t len; 49 if (ss_read(s, sid, out, sizeof out, &len, NULL) != 0) return 0; 50 return len == n && memcmp(out, p, n) == 0; 51 } 52 53 /* ---- plain ---- */ 54 static void plain_base(uint32_t bs) 55 { 56 rig r; 57 CHECK(rig_open(&r, g_path, bs, 1) == 0, "create"); 58 CHECK_EQ(st_format(&r.sdev, RIG_UUID), 0, "format"); 59 st_store s; 60 CHECK_EQ(st_open(&s, &r.sdev, &g_ws.st), 0, "open"); 61 st_object o = {30, 1, A, sizeof A}; 62 CHECK_EQ(st_transact(&s, &o, 1, NULL, NULL), 0, "gen 2"); 63 rig_close(&r); 64 CHECK(file_load(g_path, base_img, RIG_BYTES) == 0, "snapshot"); 65 } 66 67 static int plain_tx(rig *r) 68 { 69 st_store s; 70 int rc = st_open(&s, &r->sdev, &g_ws.st); 71 if (rc != 0) return rc; 72 st_object o[2] = {{31, 1, B, sizeof B}, {32, 1, C, sizeof C}}; 73 return st_transact(&s, o, 2, NULL, NULL); 74 } 75 76 static void plain_cuts(uint32_t bs) 77 { 78 plain_base(bs); 79 rig r; 80 CHECK(rig_open(&r, g_path, bs, 0) == 0, "open"); 81 uint64_t w0 = r.f.blocks_written; 82 CHECK_EQ(plain_tx(&r), 0, "uncut transaction"); 83 uint64_t W = r.f.blocks_written - w0; 84 rig_close(&r); 85 int nold = 0, nnew = 0, ndeg = 0; 86 for (uint64_t cut = 0; cut <= W; cut++) { 87 CHECK(file_save(g_path, base_img, RIG_BYTES) == 0, "restore"); 88 CHECK(rig_open(&r, g_path, bs, 0) == 0, "open"); 89 disk_file_arm_cut(&r.f, cut); 90 (void)plain_tx(&r); 91 disk_file_disarm(&r.f); 92 rig_close(&r); 93 CHECK(rig_open(&r, g_path, bs, 0) == 0, "reopen"); 94 st_store s; 95 int rc = st_open(&s, &r.sdev, &g_ws.st); 96 CHECK(rc == 0, "plain %u cut %llu: mount %s", bs, (unsigned long long)cut, st_strerror(rc)); 97 if (rc == 0) { 98 uint32_t n; 99 st_catalog(&s, &n); 100 int a = plain_has(&s, 30, A, sizeof A), b = plain_has(&s, 31, B, sizeof B), 101 c = plain_has(&s, 32, C, sizeof C); 102 int old = st_generation(&s) == 2 && n == 1 && a; 103 int neu = st_generation(&s) == 3 && n == 3 && a && b && c; 104 CHECK(old || neu, "plain %u cut %llu: neither old nor new (gen %llu n %u)", bs, 105 (unsigned long long)cut, (unsigned long long)st_generation(&s), n); 106 if (cut == 0) CHECK(old, "cut 0 must be old"); 107 if (cut == W) CHECK(neu, "full write must be new"); 108 nold += old; 109 nnew += neu; 110 ndeg += s.state == ST_DEGRADED_RECOVERY; 111 } 112 rig_close(&r); 113 } 114 printf("power cut, plain, %4u-byte blocks: %llu block boundaries, %d old, %d new (%d of them read-only degraded)\n", 115 bs, (unsigned long long)W + 1, nold, nnew, ndeg); 116 } 117 118 /* ---- sealed ---- */ 119 static ss_keys K; 120 static uint8_t g_ids[2][32], a_id[32]; 121 122 static void sealed_base(uint32_t bs) 123 { 124 rig r; 125 CHECK(rig_open(&r, g_path, bs, 1) == 0, "create"); 126 CHECK_EQ(ss_format(&r.sdev, &r.tdev, 0, RIG_UUID, &K, &g_ws), 0, "format"); 127 ss_store s; 128 CHECK_EQ(ss_open(&s, &r.sdev, &r.tdev, 0, &K, &g_ws), 0, "open"); 129 ss_object o = {30, 1, A, sizeof A}; 130 uint8_t id[1][32]; 131 CHECK_EQ(ss_transact(&s, &o, 1, NULL, NULL, id), 0, "gen 2"); 132 memcpy(a_id, id[0], 32); 133 rig_close(&r); 134 CHECK(file_load(g_path, base_img, RIG_BYTES) == 0, "snapshot"); 135 } 136 137 static int sealed_tx(rig *r) 138 { 139 ss_store s; 140 int rc = ss_open(&s, &r->sdev, &r->tdev, 0, &K, &g_ws); 141 if (rc != 0) return rc; 142 ss_object o[2] = {{31, 1, B, sizeof B}, {32, 1, C, sizeof C}}; 143 return ss_transact(&s, o, 2, NULL, NULL, g_ids); 144 } 145 146 static void sealed_cuts(uint32_t bs) 147 { 148 sealed_base(bs); 149 rig r; 150 CHECK(rig_open(&r, g_path, bs, 0) == 0, "open"); 151 uint64_t w0 = r.f.blocks_written; 152 CHECK_EQ(sealed_tx(&r), 0, "uncut transaction"); 153 uint64_t W = r.f.blocks_written - w0; 154 rig_close(&r); 155 int nold = 0, nnew = 0, nprep = 0, ndeg = 0, ncatch = 0; 156 for (uint64_t cut = 0; cut <= W; cut++) { 157 CHECK(file_save(g_path, base_img, RIG_BYTES) == 0, "restore"); 158 CHECK(rig_open(&r, g_path, bs, 0) == 0, "open"); 159 disk_file_arm_cut(&r.f, cut); 160 (void)sealed_tx(&r); 161 disk_file_disarm(&r.f); 162 rig_close(&r); 163 CHECK(rig_open(&r, g_path, bs, 0) == 0, "reopen"); 164 ss_store s; 165 int rc = ss_open(&s, &r.sdev, &r.tdev, 0, &K, &g_ws); 166 CHECK(rc == 0, "sealed %u cut %llu: mount %s", bs, (unsigned long long)cut, ss_strerror(rc)); 167 if (rc == 0) { 168 int a = sealed_has(&s, a_id, A, sizeof A); 169 int b = sealed_has(&s, g_ids[0], B, sizeof B), c = sealed_has(&s, g_ids[1], C, sizeof C); 170 int old = ss_generation(&s) == 2 && s.nclaims == 1 && a && s.rb == SS_RB_VALID_RESUME; 171 int neu = ss_generation(&s) == 3 && s.nclaims == 3 && a && b && c && 172 (s.rb == SS_RB_VALID_RESUME || s.rb == SS_RB_PREPARED_ADVANCE); 173 CHECK(old || neu, "sealed %u cut %llu: neither old nor new (gen %llu claims %u rb %d)", bs, 174 (unsigned long long)cut, (unsigned long long)ss_generation(&s), s.nclaims, s.rb); 175 if (cut == 0) CHECK(old, "cut 0 must be old"); 176 if (cut == W) CHECK(neu && s.rb == SS_RB_VALID_RESUME, "full write must be new and anchored"); 177 nold += old; 178 nnew += neu; 179 nprep += neu && s.rb == SS_RB_PREPARED_ADVANCE; 180 ndeg += s.st.state == ST_DEGRADED_RECOVERY; 181 /* the store keeps working: one more transaction, then reopen */ 182 if (s.st.state == ST_VALID) { 183 ss_object o = {33, 1, C, 10}; 184 CHECK_EQ(ss_transact(&s, &o, 1, NULL, NULL, NULL), 0, "follow-up transaction"); 185 uint64_t g = ss_generation(&s); 186 rig_close(&r); 187 CHECK(rig_open(&r, g_path, bs, 0) == 0, "reopen"); 188 rc = ss_open(&s, &r.sdev, &r.tdev, 0, &K, &g_ws); 189 CHECK(rc == 0 && ss_generation(&s) == g && s.rb == SS_RB_VALID_RESUME, 190 "sealed %u cut %llu: follow-up reopen %s", bs, (unsigned long long)cut, ss_strerror(rc)); 191 ncatch++; 192 } else { 193 ss_object o = {33, 1, C, 10}; 194 CHECK_EQ(ss_transact(&s, &o, 1, NULL, NULL, NULL), ST_E_READ_ONLY_DEGRADED, "degraded is read-only"); 195 } 196 } 197 rig_close(&r); 198 } 199 printf("power cut, sealed, %4u-byte blocks: %llu block boundaries, %d old, %d new (%d anchor one behind), " 200 "%d read-only degraded, %d follow-ups ok\n", 201 bs, (unsigned long long)W + 1, nold, nnew, nprep, ndeg, ncatch); 202 } 203 204 int main(int argc, char **argv) 205 { 206 const char *dir = argc > 1 ? argv[1] : "."; 207 fill(A, sizeof A, 1); 208 fill(B, sizeof B, 2); 209 fill(C, sizeof C, 3); 210 rig_keys(M5_ID_PRODUCTION, 0x33, &K); 211 static const uint32_t sizes[2] = {512, 4096}; 212 for (int k = 0; k < 2; k++) { 213 snprintf(g_path, sizeof g_path, "%s/powercut_%u.img", dir, sizes[k]); 214 plain_cuts(sizes[k]); 215 sealed_cuts(sizes[k]); 216 unlink(g_path); 217 } 218 printf("store power cut: %d checks, %d failed\n", g_checks, g_failed); 219 printf("STORE_POWERCUT: %s\n", g_failed ? "FAIL" : "PASS"); 220 return g_failed ? 1 : 0; 221 }