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 / store / tests / store_test_util.h
| 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 | /* Shared helpers for the native Store tests (hosted: libc allowed here). */ |
| 18 | #ifndef STORE_TEST_UTIL_H |
| 19 | #define STORE_TEST_UTIL_H |
| 20 | #include <stdint.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include "store_engine.h" |
| 25 | |
| 26 | static int g_checks __attribute__((unused)), g_failed __attribute__((unused)); |
| 27 | #define CHECK(cond, ...) \ |
| 28 | do { \ |
| 29 | g_checks++; \ |
| 30 | if (!(cond)) { \ |
| 31 | g_failed++; \ |
| 32 | fprintf(stderr, "FAIL %s:%d: ", __FILE__, __LINE__); \ |
| 33 | fprintf(stderr, __VA_ARGS__); \ |
| 34 | fprintf(stderr, "\n"); \ |
| 35 | } \ |
| 36 | } while (0) |
| 37 | #define CHECK_EQ(got, want, what) \ |
| 38 | do { \ |
| 39 | long long g_ = (long long)(got), w_ = (long long)(want); \ |
| 40 | CHECK(g_ == w_, "%s: got %lld (%s), want %lld (%s)", what, g_, st_strerror((int)g_), \ |
| 41 | w_, st_strerror((int)w_)); \ |
| 42 | } while (0) |
| 43 | |
| 44 | static inline void to_hex(const uint8_t *b, size_t n, char *out) |
| 45 | { |
| 46 | static const char d[] = "0123456789abcdef"; |
| 47 | for (size_t i = 0; i < n; i++) { |
| 48 | out[2 * i] = d[b[i] >> 4]; |
| 49 | out[2 * i + 1] = d[b[i] & 15]; |
| 50 | } |
| 51 | out[2 * n] = 0; |
| 52 | } |
| 53 | |
| 54 | /* RAM-backed st_dev that records reads, writes and flushes and can be told |
| 55 | * to fail one unit's read or write, or every flush (the Rust MemoryDevice). */ |
| 56 | #define MEM_LOG 4096 |
| 57 | typedef struct { |
| 58 | uint8_t (*units)[SV1_UNIT]; |
| 59 | uint64_t n; |
| 60 | uint64_t reads[MEM_LOG], writes[MEM_LOG]; |
| 61 | size_t nreads, nwrites; |
| 62 | unsigned flushes; |
| 63 | int64_t fail_read, fail_write; |
| 64 | int fail_flush; |
| 65 | } memdev; |
| 66 | |
| 67 | static int mem_read(void *ctx, uint64_t u, uint8_t out[SV1_UNIT]) |
| 68 | { |
| 69 | memdev *m = ctx; |
| 70 | if (m->nreads < MEM_LOG) |
| 71 | m->reads[m->nreads++] = u; |
| 72 | if ((int64_t)u == m->fail_read || u >= m->n) |
| 73 | return -1; |
| 74 | memcpy(out, m->units[u], SV1_UNIT); |
| 75 | return 0; |
| 76 | } |
| 77 | static int mem_write(void *ctx, uint64_t u, const uint8_t in[SV1_UNIT]) |
| 78 | { |
| 79 | memdev *m = ctx; |
| 80 | if (m->nwrites < MEM_LOG) |
| 81 | m->writes[m->nwrites++] = u; |
| 82 | if ((int64_t)u == m->fail_write || u >= m->n) |
| 83 | return -1; |
| 84 | memcpy(m->units[u], in, SV1_UNIT); |
| 85 | return 0; |
| 86 | } |
| 87 | static int mem_flush(void *ctx) |
| 88 | { |
| 89 | memdev *m = ctx; |
| 90 | m->flushes++; |
| 91 | return m->fail_flush ? -1 : 0; |
| 92 | } |
| 93 | static inline void mem_init(memdev *m, uint64_t n) |
| 94 | { |
| 95 | memset(m, 0, sizeof *m); |
| 96 | m->units = calloc((size_t)n, SV1_UNIT); |
| 97 | if (!m->units) { |
| 98 | perror("calloc"); |
| 99 | exit(2); |
| 100 | } |
| 101 | m->n = n; |
| 102 | m->fail_read = m->fail_write = -1; |
| 103 | } |
| 104 | static inline void mem_free(memdev *m) |
| 105 | { |
| 106 | free(m->units); |
| 107 | m->units = NULL; |
| 108 | } |
| 109 | static inline void mem_clear_log(memdev *m) |
| 110 | { |
| 111 | m->nreads = m->nwrites = 0; |
| 112 | m->flushes = 0; |
| 113 | } |
| 114 | static inline void mem_clone(memdev *dst, const memdev *src) |
| 115 | { |
| 116 | mem_init(dst, src->n); |
| 117 | memcpy(dst->units, src->units, (size_t)src->n * SV1_UNIT); |
| 118 | } |
| 119 | static inline st_dev mem_dev(memdev *m) |
| 120 | { |
| 121 | st_dev d = {m, m->n, mem_read, mem_write, mem_flush}; |
| 122 | return d; |
| 123 | } |
| 124 | static inline void rechecksum(uint8_t *u) |
| 125 | { |
| 126 | memset(u + SV1_SB_CRC_OFFSET, 0, 4); |
| 127 | sv1_put32(u + SV1_SB_CRC_OFFSET, sv1_crc32c(u, SV1_UNIT)); |
| 128 | } |
| 129 | |
| 130 | #endif |