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_xcheck.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_xcheck.c` 190 lines ยท 6713 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 * store_xcheck.c -- small hosted CLI used by tests/rust_crosscheck.sh to hand 19 * C-written Store images to the Rust aienos-store-tool and to classify images 20 * the Rust tool has changed. Image I/O goes through native/disk disk_file. 21 * 22 * store_xcheck mkimage IMG BS BASE_BLOCKS UNITS 23 * format + generation 2 (kinds 3, 7) + generation 3 (kind 9, and kind 3 24 * again, deduplicated) 25 * store_xcheck mkpair OLD NEW BS BASE_BLOCKS UNITS 26 * same as mkimage up to generation 2, then generation 3 into NEW while 27 * copying the image to OLD at the after_first_flush checkpoint (all 28 * payload/catalog/commit units durable, root not yet written) 29 * store_xcheck advance IMG BS BASE_BLOCKS UNITS 30 * one more generation (kind 11) 31 * store_xcheck classify IMG BS BASE_BLOCKS UNITS 32 * prints "MOUNT state=<..> peer=<..> gen= objects_ok=" or 33 * "REFUSE " 34 */ 35 #include "store_test_util.h" 36 #include "disk_file.h" 37 38 static st_workspace ws; 39 static const uint8_t UUID[16] = {0xc0, 0x5e, 0x11, 0x0b, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; 40 41 static int open_img(const char *path, uint32_t bs, uint64_t base, uint64_t units, int create, 42 disk_file *f, disk_dev *d, st_disk *a, st_dev *sd) 43 { 44 uint64_t bpu = SV1_UNIT / bs; 45 if (disk_file_open(f, d, path, bs, base + units * bpu, create)) { 46 fprintf(stderr, "cannot open %s\n", path); 47 return -1; 48 } 49 if (st_disk_bind(a, d, base, units, sd)) { 50 fprintf(stderr, "bind failed\n"); 51 return -1; 52 } 53 return 0; 54 } 55 56 static const char *copy_to; 57 static const char *copy_from; 58 static void copy_hook(void *arg, int cp) 59 { 60 (void)arg; 61 if (cp != ST_CP_AFTER_FIRST_FLUSH || !copy_to) 62 return; 63 FILE *in = fopen(copy_from, "rb"), *out = fopen(copy_to, "wb"); 64 char buf[65536]; 65 size_t n; 66 while (in && out && (n = fread(buf, 1, sizeof buf, in)) > 0) 67 fwrite(buf, 1, n, out); 68 if (in) 69 fclose(in); 70 if (out) 71 fclose(out); 72 } 73 74 static uint8_t p3[100], p7[5000], p9[9000]; 75 static void payloads(void) 76 { 77 for (size_t i = 0; i < sizeof p3; i++) 78 p3[i] = (uint8_t)(i * 3 + 1); 79 for (size_t i = 0; i < sizeof p7; i++) 80 p7[i] = (uint8_t)(i * 7 + 2); 81 for (size_t i = 0; i < sizeof p9; i++) 82 p9[i] = (uint8_t)(i * 9 + 3); 83 } 84 85 static int build(const char *img, const char *old, uint32_t bs, uint64_t base, uint64_t units) 86 { 87 disk_file f; 88 disk_dev d; 89 st_disk a; 90 st_dev sd; 91 st_store s; 92 int r; 93 if (open_img(img, bs, base, units, 1, &f, &d, &a, &sd)) 94 return 1; 95 payloads(); 96 if ((r = st_format(&sd, UUID)) || (r = st_open(&s, &sd, &ws))) 97 goto fail; 98 st_object g2[2] = {{3, 1, p3, sizeof p3}, {7, 1, p7, sizeof p7}}; 99 if ((r = st_transact(&s, g2, 2, NULL, NULL))) 100 goto fail; 101 st_object g3[2] = {{9, 1, p9, sizeof p9}, {3, 1, p3, sizeof p3}}; 102 copy_to = old; 103 copy_from = img; 104 if ((r = st_transact(&s, g3, 2, copy_hook, NULL))) 105 goto fail; 106 disk_file_close(&f); 107 printf("C image %s: generation %llu, active slot %u\n", img, 108 (unsigned long long)st_generation(&s), st_active_slot(&s)); 109 return 0; 110 fail: 111 fprintf(stderr, "build failed: %s\n", st_strerror(r)); 112 disk_file_close(&f); 113 return 1; 114 } 115 116 static int classify(const char *img, uint32_t bs, uint64_t base, uint64_t units) 117 { 118 disk_file f; 119 disk_dev d; 120 st_disk a; 121 st_dev sd; 122 st_store s; 123 if (open_img(img, bs, base, units, 0, &f, &d, &a, &sd)) 124 return 1; 125 int r = st_open(&s, &sd, &ws); 126 if (r) { 127 printf("REFUSE %s\n", st_strerror(r)); 128 disk_file_close(&f); 129 return 0; 130 } 131 static const char *states[] = {"Valid", "DegradedRecovery"}; 132 static const char *peers[] = {"Zero", "Valid", "Malformed", "GraphBadNewer", "GraphBadOlder"}; 133 uint32_t n; 134 const sv1_entry *e = st_catalog(&s, &n); 135 static uint8_t buf[SV1_MAX_OBJECT_BYTES / 64]; 136 unsigned ok = 0; 137 for (uint32_t i = 0; i < n; i++) { 138 size_t len; 139 if (e[i].byte_length <= sizeof buf && st_read_object(&s, e[i].object_id, buf, sizeof buf, &len) == 0) 140 ok++; 141 } 142 printf("MOUNT state=%s peer=%s gen=%llu objects_ok=%u/%u\n", states[s.state], peers[s.peer], 143 (unsigned long long)st_generation(&s), ok, n); 144 disk_file_close(&f); 145 return 0; 146 } 147 148 static int advance(const char *img, uint32_t bs, uint64_t base, uint64_t units) 149 { 150 disk_file f; 151 disk_dev d; 152 st_disk a; 153 st_dev sd; 154 st_store s; 155 static uint8_t p[300]; 156 for (size_t i = 0; i < sizeof p; i++) 157 p[i] = (uint8_t)(i ^ 0x5c); 158 if (open_img(img, bs, base, units, 0, &f, &d, &a, &sd)) 159 return 1; 160 st_object o = {11, 1, p, sizeof p}; 161 int r = st_open(&s, &sd, &ws); 162 if (!r) 163 r = st_transact(&s, &o, 1, NULL, NULL); 164 disk_file_close(&f); 165 if (r) { 166 fprintf(stderr, "advance failed: %s\n", st_strerror(r)); 167 return 1; 168 } 169 printf("C image %s: generation %llu, active slot %u\n", img, 170 (unsigned long long)st_generation(&s), st_active_slot(&s)); 171 return 0; 172 } 173 174 int main(int argc, char **argv) 175 { 176 if (argc == 6 && !strcmp(argv[1], "advance")) 177 return advance(argv[2], (uint32_t)strtoul(argv[3], 0, 10), strtoull(argv[4], 0, 10), 178 strtoull(argv[5], 0, 10)); 179 if (argc == 6 && !strcmp(argv[1], "mkimage")) 180 return build(argv[2], NULL, (uint32_t)strtoul(argv[3], 0, 10), strtoull(argv[4], 0, 10), 181 strtoull(argv[5], 0, 10)); 182 if (argc == 7 && !strcmp(argv[1], "mkpair")) 183 return build(argv[3], argv[2], (uint32_t)strtoul(argv[4], 0, 10), strtoull(argv[5], 0, 10), 184 strtoull(argv[6], 0, 10)); 185 if (argc == 6 && !strcmp(argv[1], "classify")) 186 return classify(argv[2], (uint32_t)strtoul(argv[3], 0, 10), strtoull(argv[4], 0, 10), 187 strtoull(argv[5], 0, 10)); 188 fprintf(stderr, "usage: store_xcheck mkimage|mkpair|classify ...\n"); 189 return 2; 190 }