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/patches/8bc852a60e444dcf # 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 ## GitHub aien-dev/aienux#226 (code only) closed derived `8bc852a60e444dcf` #226 Imported from GitHub aien-dev/aienux#226 (code only) GitHub's metadata for this pull request is unavailable: its state is DERIVED from the commits (merged: the head is reachable from this project's main; open: GitHub kept a merge ref; else closed). Base `2ee61b4b158a385c5fcf2c925a625ac043babb21` · Head `8264828b70d3939ffbc2bfc5658f428641e2d882` ### Apply it ``` git checkout -b patch-8bc852a60e444dcf 2ee61b4b158a385c5fcf2c925a625ac043babb21 curl -fsSL https://interplane.io/drakestapleton/aienux/patches/8bc852a60e444dcf.patch | git am ``` Raw patch (mbox) · Diff ### Provenance This proposal is imported from GitHub; it is not signed by its author. Its import statement and source record: ``` github-import 1 aien-dev/aienux 226 8264828b70d3939ffbc2bfc5658f428641e2d882 ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"aienux","number":226,"url":"https://github.com/aien-dev/aienux/pull/226","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:00Z","metadata":"unavailable","state_derived":true} ``` ### Changes ``` diff --git a/native/kernel/GATES.md b/native/kernel/GATES.md index 5fe6c724..616bfbb3 100644 --- a/native/kernel/GATES.md +++ b/native/kernel/GATES.md @@ -23,7 +23,7 @@ C gates (one line each from `ck_gates.sh`): `M1` and `M3` (from 96-101 were checked against its exact grep patterns), `P2_ARTIFACT` (from `scripts/qemu_ck_artifact_test.sh`, rows 33-41), `NET` (C-only, from `scripts/qemu_ck_net_test.sh`; rows 102-106 and 110-112; the script now also checks the virtio-net SMMU fence (ACCESS_PLATFORM required, out-of-window device DMA refused), rows 102 and 110-112 are QEMU PASS on this branch from the committed forge log `evidence/ck_net_qemu_8bdbc2e46b85d04c422f9d2830c1c4b263e254b3cfecfe21ff0063c9e33c86e8.log`, hardware NOT_RUN), the kernel entropy rows 107-109 (M1 via `scripts/lib_ck_m1_checks.sh`, ARGUS1_REVOKE and M4_STORE via the Store gate boot 7), and the NOT_RUN gates -`SMP` (C-only, from `scripts/qemu_ck_smp_test.sh`, rows 113-117, NOT_RUN pending forge receipt), `M0_ROLLBACK`, `M4_STORE_CRASH`, +`SMP` (C-only, from `scripts/qemu_ck_smp_test.sh`, rows 113-117, NOT_RUN pending forge receipt), `M0_ROLLBACK`, `M4_STORE_CRASH` (now read from `scripts/qemu_ck_store_crash_test.sh`, rows 73-80; NOT_RUN until a forge receipt exists), `M4_CONTINUITY`, `M4_RECOVERY`, `KEYBOARD`. `M4_NVME` and `SMMU` PASS in the SMMU-confined mode (default `make full` @@ -302,19 +302,33 @@ prints this on the PASS line and records `nvme_dma_mode` in the receipt. | --- | --- | --- | --- | | 71 | `STORE_NVME_INTEGRATION_QEMU` + `STORE_CHECKPOINT_QEMU` (Store over the native NVMe driver) | M4_STORE | DIFFERS (C: sealed C Store (native/store + native/m5, TEST keys) over native/disk, 512 B and 4096 B; blank -> `store: blank disk ... formatted TEST store`, `stage store: ok`, `store: committed generation= boot_count=`) | | 72 | `STORE_REOPEN_QEMU` (reopen from media) | M4_STORE | DIFFERS (C: reopen across boots 1-3, boot_count 1 -> 2 -> 3, generation +1 each boot, prev_commit none then the previous commit) | -| 73 | `STORE_CHECKPOINT_CRASH_QEMU` (kill QEMU at each checkpoint, recover N or N+1) | M4_STORE_CRASH | NOT_RUN (MISSING_IMPLEMENTATION: no C crash/kill campaign) | -| 74 | `STORE_SLOT_REUSE_QEMU` | M4_STORE_CRASH | NOT_RUN (MISSING_IMPLEMENTATION: C Store is append-only, no reclaim/slot reuse) | -| 75 | `STORE_V1_QEMU` summary | M4_STORE_CRASH | NOT_RUN (MISSING_IMPLEMENTATION) | +| 73 | `STORE_CHECKPOINT_CRASH_QEMU` (kill QEMU at each checkpoint, recover N or N+1) | M4_STORE_CRASH | NOT_RUN (pending forge receipt). Implemented, not yet run, by `scripts/qemu_ck_store_crash_test.sh`; will be DIFFERS (C: all 9 commit checkpoints of `st_transact` + `ss_commit_prepared` (before_first_write .. after_final_flush, before_anchor, after_anchor), settle 0 and 3, 4096 B and 512 B; a TEST-ONLY hook image (`make full CK_TEST_STORE_CRASH=1`, svc/store_crash.c) runs the Store on a volatile write cache, lands drop/all/newest/torn of the unflushed writes at the planned checkpoint, prints `store_crash: HALT at`, the host SIGKILLs QEMU; the default image must then open N before the root write, N+1 after the final flush (Rust N / NP1 / N_NP1), with boot_count, prev_commit and anchor of that generation, no refusal, no reformat, and commit on top) | +| 74 | `STORE_SLOT_REUSE_QEMU` | M4_STORE_CRASH | NOT_RUN (MISSING_IMPLEMENTATION: C Store is append-only, no reclaim/slot reuse; the crash script prints `CK_STORE_SLOT_REUSE: NOT_RUN` and does not count it) | +| 75 | `STORE_V1_QEMU` summary | M4_STORE_CRASH | NOT_RUN (pending forge receipt; C summary line `AIENOS_CK_M4_STORE_CRASH: PASS\|FAIL\|NOT_RUN` from `scripts/qemu_ck_store_crash_test.sh`, read by `scripts/ck_gates.sh`; will be DIFFERS: no slot reuse part) | ## M4 Store 512 B: scripts/qemu_store_512b_crash_test.sh -> CK `M4_STORE_CRASH` | # | Rust check (marker) | CK gate | Status | | --- | --- | --- | --- | | 76 | `STORE_NVME_INTEGRATION_512B_QEMU` | M4_STORE | DIFFERS (the 512 B geometry run of qemu_ck_store_test.sh; integration only, no 512B-named marker, no crash campaign) | -| 77 | `STORE_512B_CRASH_OBSERVED_QEMU` (tier 1 kills) | M4_STORE_CRASH | NOT_RUN (MISSING_IMPLEMENTATION: no 512B crash campaign) | -| 78 | `STORE_512B_ROOT_TEAR_CLOSURE` (tier 2a) | M4_STORE_CRASH | NOT_RUN (MISSING_IMPLEMENTATION) | -| 79 | `STORE_512B_INJECTED_ROOT_RECOVERY_QEMU` (tier 2b, degraded read-only / refuse) | M4_STORE_CRASH | NOT_RUN (MISSING_IMPLEMENTATION; nearest: C structural refusal of a corrupt superblock, host + QEMU, which is in M4_STORE) | -| 80 | `STORE_512B_CRASH_RECOVERY_QEMU` summary | M4_STORE_CRASH | NOT_RUN (MISSING_IMPLEMENTATION) | +| 77 | `STORE_512B_CRASH_OBSERVED_QEMU` (tier 1 kills) | M4_STORE_CRASH | NOT_RUN (pending forge receipt; the 512 B half of the row 73 campaign, line `CK_STORE_CRASH_512B_QEMU`; will be DIFFERS: crash point chosen by an in-guest plan, not a serial-triggered kill) | +| 78 | `STORE_512B_ROOT_TEAR_CLOSURE` (tier 2a) | M4_STORE_CRASH | NOT_RUN (pending forge receipt; `CK_STORE_ROOT_TEAR_CLOSURE_QEMU`: on the QEMU images, both geometries, the after_inactive_superblock crash changes exactly one superblock slot, only inside its first 512 bytes, and that slot holds generation N+1; will be DIFFERS: checked on the crashed images, not a host-only unit test) | +| 79 | `STORE_512B_INJECTED_ROOT_RECOVERY_QEMU` (tier 2b, degraded read-only / refuse) | M4_STORE_CRASH | NOT_RUN (pending forge receipt; `CK_STORE_INJECTED_ROOT_QEMU`: one byte of the new root's CRC flipped, both geometries; the verify boot must not open N+1, must print `store: REFUSED` and "disk left as found", commit nothing, image sha256 unchanged; will be DIFFERS: one injection (CRC), not the Rust 9 cases) | +| 80 | `STORE_512B_CRASH_RECOVERY_QEMU` summary | M4_STORE_CRASH | NOT_RUN (pending forge receipt; folded into `AIENOS_CK_M4_STORE_CRASH`) | + +Mutation and refusal checks for `M4_STORE_CRASH` (all NOT_RUN, pending forge +receipt): `bash scripts/qemu_ck_store_crash_test.sh --mutant skip_root_flush` +builds a TEST-ONLY Store with the data flush before the root write removed +(`CK_TEST_STORE_MUTANT_SKIP_ROOT_FLUSH`, native/store/store_engine.c) and must +print `KILLED` (a newest/torn root landing then recovers wrong); `--mutant +accept_bad_root_crc` builds a Store that accepts a bad superblock CRC +(`CK_TEST_STORE_MUTANT_ACCEPT_BAD_ROOT_CRC`, native/store/store_v1.c) and must +print `KILLED` (the injected root is opened). `--self-test` runs the judges on +canned serial text and images (wrong generation, reformat, refusal, skipped +checkpoint, change past sector 0, two slots changed) and checks with `make -n` +that the hook is refused with `CK_HARDWARE_STAGING` and `CK_QEMU_UNSAFE_DMA`. +The default `make full` image must carry no crash hook string (Makefile +`crash_check`). QEMU only; hardware NOT_RUN. ## M4 continuity: scripts/qemu_continuity_test.sh -> CK `M4_CONTINUITY` @@ -452,7 +466,7 @@ No PASS without a forge receipt; hardware NOT_RUN (no GB10 core was started). Rows 113-117 (SMP, C-only, new in this change): 5 NOT_RUN (pending forge receipt), so rows 1-117 plus the M4 continuity/recovery sub-rows: IDENTICAL 28, DIFFERS 39, NOT_RUN 91 (86 from the line below plus these 5). -Rows 1-112 plus the M4 continuity/recovery sub-rows 83a-87i and 88a-91p: IDENTICAL 28, DIFFERS 39, NOT_RUN 86, counted from the table (the CK-5 contract branch split rows 81-91, 11 NOT_RUN rows, into 52 NOT_RUN rows, one per Rust QEMU check and host test, see `native/kernel/CONTINUITY_RECOVERY_CONTRACT.md`; that adds 41 NOT_RUN rows and changes no other row or gate; the CK gate tally is unchanged; recounted again after merging the KEYBOARD DMA-fence branch: 28 IDENTICAL, 39 DIFFERS, 82 NOT_RUN and 4 QEMU PASS rows (102, 110-112) counted as NOT_RUN as below, so 86, unchanged, with sub-rows 24a and 24b (C only) not counted; rows 25-28, 31 and 32 now have a C implementation and a child script, scripts/qemu_ck_keyboard_test.sh, but stay NOT_RUN (pending forge receipt), rows 29-30 stay NOT_RUN (MISSING_IMPLEMENTATION: no USB HID driver, no console shell), and the KEYBOARD gate stays NOT_RUN; rows 107-109, kernel entropy, are C-only and DIFFERS; row 102, virtio-net attach with ACCESS_PLATFORM, and rows 110-112, virtio-net SMMU fence, recovery and ACCESS_PLATFORM fail-closed, are C-only and NOT_RUN on this branch until a forge receipt covers them; the CK gate tally above and its receipt `ck_gates_929f...` predate them; no full `ck_gates.sh` run happened on this branch, only `scripts/qemu_ck_net_test.sh` alone, so rows 102 and 110-112 are shown as QEMU PASS (receipt `evidence/ck_net_qemu_8bdbc2e46b85d04c422f9d2830c1c4b263e254b3cfecfe21ff0063c9e33c86e8.log`, hardware NOT_RUN) and the NOT_RUN count still includes them as originally counted, not recounted); rows 34 and 39 now also cover the NVMe boot-disk source and its hostile disk cases (statuses unchanged) (the previous summary, 20/30/56, was miscounted: the table on main had 21/35/50; rows 33-41 then moved from NOT_RUN to 7 IDENTICAL + 2 DIFFERS with the C artifact loader; rows 92-95 have no CK gate at all; rows 96-112 are C-only). Rows 33-41 were checked against scripts/qemu_ck_artifact_test.sh. Rows 102-106 and 110-112 were checked against scripts/qemu_ck_net_test.sh (text only; rows 102 and 110-112 then run by the forge, see their rows). Rows 13, 24, 47-72, 76 and 96-101 were re-verified against scripts/qemu_ck_store_test.sh and scripts/lib_ck_m1_checks.sh at the commit that adds this line. +Rows 1-112 plus the M4 continuity/recovery sub-rows 83a-87i and 88a-91p: IDENTICAL 28, DIFFERS 39, NOT_RUN 86, counted from the table (the CK-5 contract branch split rows 81-91, 11 NOT_RUN rows, into 52 NOT_RUN rows, one per Rust QEMU check and host test, see `native/kernel/CONTINUITY_RECOVERY_CONTRACT.md`; that adds 41 NOT_RUN rows and changes no other row or gate; the CK gate tally is unchanged; recounted again after merging the KEYBOARD DMA-fence branch: 28 IDENTICAL, 39 DIFFERS, 82 NOT_RUN and 4 QEMU PASS rows (102, 110-112) counted as NOT_RUN as below, so 86, unchanged, with sub-rows 24a and 24b (C only) not counted; rows 25-28, 31 and 32 now have a C implementation and a child script, scripts/qemu_ck_keyboard_test.sh, but stay NOT_RUN (pending forge receipt), rows 29-30 stay NOT_RUN (MISSING_IMPLEMENTATION: no USB HID driver, no console shell), and the KEYBOARD gate stays NOT_RUN; rows 107-109, kernel entropy, are C-only and DIFFERS; row 102, virtio-net attach with ACCESS_PLATFORM, and rows 110-112, virtio-net SMMU fence, recovery and ACCESS_PLATFORM fail-closed, are C-only and NOT_RUN on this branch until a forge receipt covers them; the CK gate tally above and its receipt `ck_gates_929f...` predate them; no full `ck_gates.sh` run happened on this branch, only `scripts/qemu_ck_net_test.sh` alone, so rows 102 and 110-112 are shown as QEMU PASS (receipt `evidence/ck_net_qemu_8bdbc2e46b85d04c422f9d2830c1c4b263e254b3cfecfe21ff0063c9e33c86e8.log`, hardware NOT_RUN) and the NOT_RUN count still includes them as originally counted, not recounted); rows 34 and 39 now also cover the NVMe boot-disk source and its hostile disk cases (statuses unchanged) (the previous summary, 20/30/56, was miscounted: the table on main had 21/35/50; rows 33-41 then moved from NOT_RUN to 7 IDENTICAL + 2 DIFFERS with the C artifact loader; rows 92-95 have no CK gate at all; rows 96-112 are C-only). Rows 33-41 were checked against scripts/qemu_ck_artifact_test.sh. Rows 102-106 and 110-112 were checked against scripts/qemu_ck_net_test.sh (text only; rows 102 and 110-112 then run by the forge, see their rows). Rows 13, 24, 47-72, 76 and 96-101 were re-verified against scripts/qemu_ck_store_test.sh and scripts/lib_ck_m1_checks.sh at the commit that adds this line. Rows 73, 75 and 77-80 now have a C implementation (`scripts/qemu_ck_store_crash_test.sh`, TEST-only hook `native/kernel/svc/store_crash.c`) but no receipt yet, so they stay NOT_RUN and the 28/39/86 count is unchanged by that commit (recounted from the table after rebasing onto main 2ee61b4, #224 SMP, status read from each row's last cell with an escaped pipe inside a cell read as text: 28 IDENTICAL, 39 DIFFERS, 82 NOT_RUN + rows 102 and 110-112, QEMU PASS counted as NOT_RUN as above, = 86, sub-rows 24a and 24b not counted; the SMP rows 113-117 add 5 NOT_RUN for the 91 above). The CK gate tally above is unchanged: `M4_STORE_CRASH` stays NOT_RUN until a forge receipt. `scripts/trust1_m5_qualify.sh --with-qemu` also runs three of these gates as qemu rows: `ck_m1_boot_qemu` (M1), `ck_store_kernel_qemu` (M4_STORE) and diff --git a/native/kernel/Makefile b/native/kernel/Makefile index a0d43cdd..9b0e0e93 100644 --- a/native/kernel/Makefile +++ b/native/kernel/Makefile @@ -22,6 +22,11 @@ # purpose: $(OUT)/full-test-xhci-/ (only # scripts/qemu_ck_keyboard_test.sh --mutation; refused # with CK_HARDWARE_STAGING=1 or CK_QEMU_UNSAFE_DMA=1) +# make full CK_TEST_STORE_CRASH=1 [CK_TEST_STORE_CRASH_MUTANT=] +# TEST-ONLY Store crash hook image +# $(OUT)/full-test-store-crash[-mutant-]/ for +# scripts/qemu_ck_store_crash_test.sh (M4_STORE_CRASH); +# refused with CK_HARDWARE_STAGING / CK_QEMU_UNSAFE_DMA # make owner-gen host tool $(OUT)/host/ck_owner_gen # make [full] CK_TEST_FWCFG_ARTIFACTS=1 # TEST-ONLY: loader takes candidates from the QEMU fw_cfg @@ -148,6 +153,41 @@ $(error CK_TEST_XHCI_MUTATION must be one of bm-left-on no-revoke grant-no-smmu endif endif +# TEST-ONLY Store crash hook (svc/store_crash.h; only +# scripts/qemu_ck_store_crash_test.sh sets it): the Store stage reads a crash +# plan from the scratch unit, runs on a volatile write cache and halts at the +# planned checkpoint. Own OUT subdirectory; announces itself; never counts +# toward a PASS of M4_STORE; refused with CK_HARDWARE_STAGING and +# CK_QEMU_UNSAFE_DMA. CK_TEST_STORE_CRASH_MUTANT (only with it) builds a +# deliberately broken Store the crash gate must catch: +# skip_root_flush no flush between the commit record and the root write +# accept_bad_root_crc superblock CRC never checked +CK_TEST_STORE_CRASH ?= +CK_TEST_STORE_CRASH_MUTANT ?= +ifneq ($(strip $(CK_TEST_STORE_CRASH)),) +ifneq ($(strip $(CK_TEST_STORE_CRASH)),1) +$(error CK_TEST_STORE_CRASH must be empty or 1) +endif +ifneq ($(strip $(CK_HARDWARE_STAGING)),) +$(error CK_TEST_STORE_CRASH (TEST-ONLY Store crash hook) cannot be combined with CK_HARDWARE_STAGING) +endif +ifneq ($(strip $(CK_QEMU_UNSAFE_DMA)),) +$(error CK_TEST_STORE_CRASH (TEST-ONLY Store crash hook) cannot be combined with CK_QEMU_UNSAFE_DMA) +endif +endif +ifneq ($(strip $(CK_TEST_STORE_CRASH_MUTANT)),) +ifneq ($(strip $(CK_TEST_STORE_CRASH)),1) +$(error CK_TEST_STORE_CRASH_MUTANT needs CK_TEST_STORE_CRASH=1 (TEST-ONLY)) +endif +ifeq ($(strip $(CK_TEST_STORE_CRASH_MUTANT)),skip_root_flush) +STORE_MUTANT_FLAGS := -DCK_TEST_STORE_MUTANT_SKIP_ROOT_FLUSH=1 +else ifeq ($(strip $(CK_TEST_STORE_CRASH_MUTANT)),accept_bad_root_crc) +STORE_MUTANT_FLAGS := -DCK_TEST_STORE_MUTANT_ACCEPT_BAD_ROOT_CRC=1 +else +$(error CK_TEST_STORE_CRASH_MUTANT must be skip_root_flush or accept_bad_root_crc) +endif +endif + # Owner provisioning. The hardware staging image never embeds the TEST Store # keys, the TEST store uuid or the TEST machine id: it needs owner PUBLIC # material and the machine identity as files (format: tools/ck_owner_gen.c; @@ -255,6 +295,15 @@ STAGE_DMA_FLAGS += -DCK_HARDWARE_STAGING=1 -I$(dir $(OWNER_HDR)) else OWNER_HDR := endif +ifeq ($(strip $(CK_TEST_STORE_CRASH)),1) +# Own OUT subdirectory: TEST hook objects never mix with a default image. +ifneq ($(strip $(CK_TEST_STORE_CRASH_MUTANT)),) +FULL_NAME := full-test-store-crash-mutant-$(strip $(CK_TEST_STORE_CRASH_MUTANT)) +else +FULL_NAME := full-test-store-crash +endif +STAGE_DMA_FLAGS += -DCK_TEST_STORE_CRASH=1 $(STORE_MUTANT_FLAGS) +endif FULL_OUT := $(OUT)/$(FULL_NAME) STAGE_KOBJ := $(OUT)/obj/stage-$(FULL_NAME) STAGE_HDRS := $(HDRS) $(wildcard $(NATIVE)/*/*.h $(NATIVE)/argus/bridge/*.h $(KDIR)/svc/compat/*.h) @@ -358,6 +407,19 @@ define smp_check fi endef +# crash_check(elf): a full image built without CK_TEST_STORE_CRASH must carry +# no Store crash hook code or strings; a TEST crash image must announce itself. +define crash_check + @if [ "$(strip $(CK_TEST_STORE_CRASH))" = 1 ]; then \ + grep -aqF "TEST-ONLY Store crash hook image" $(1) || { echo "TEST crash image lacks its TEST-ONLY announcement"; rm -f $(1); exit 1; }; \ + else \ + s=$$(grep -aoE "store_crash:|AIENCRSH|TEST-ONLY MUTANT" $(1) | sort -u | tr "\n" " "); \ + y=$$($(KNM) $(1) | grep -E " ck_store_crash_[a-z_]+$$" | tr "\n" " "); \ + if [ -n "$$s$$y" ]; then echo "default image carries the TEST Store crash hook: $$s$$y"; rm -f $(1); exit 1; fi; \ + echo "no Store crash hook code in $(1)"; \ + fi +endef + # link_image(output elf, objects): refuses undefined symbols (weak stage # references excepted) and any relocation other than R_AARCH64_RELATIVE. define link_image @@ -391,6 +453,7 @@ $(FULL_ELF): $(CORE_OBJS) $(STAGE_BUILT) $(BOOT)/image.ld $(call owner_check,$@) $(call xhci_mut_check,$@) $(call smp_check,$@) + $(call crash_check,$@) $(EFI): $(ELF) $(call pe_image,$<,$@) diff --git a/native/kernel/stage.mk b/native/kernel/stage.mk index 63afe459..e47d8814 100644 --- a/native/kernel/stage.mk +++ b/native/kernel/stage.mk @@ -18,7 +18,7 @@ STAGE_OUT ?= $(abspath $(STAGE_DIR)/../../target/stage) STAGE_OWN_SRCS := $(STAGE_DIR)/dev/pci.c $(STAGE_DIR)/dev/nvme_bind.c $(STAGE_DIR)/dev/nvme_shutdown.c $(STAGE_DIR)/dev/virtio_net.c \ $(STAGE_DIR)/dev/net_bind.c $(STAGE_DIR)/dev/net_udp.c $(STAGE_DIR)/dev/xhci_fence.c \ $(STAGE_DIR)/dev/devices.c $(STAGE_DIR)/svc/security.c $(STAGE_DIR)/svc/store_boot.c \ - $(STAGE_DIR)/svc/artifact_store.c + $(STAGE_DIR)/svc/artifact_store.c $(STAGE_DIR)/svc/store_crash.c # Native modules, compiled unmodified. STAGE_NATIVE_SRCS := \ $(STAGE_NATIVE)/disk/disk.c $(STAGE_NATIVE)/disk/disk_nvme.c \ diff --git a/native/kernel/svc/store_boot.c b/native/kernel/svc/store_boot.c index 495e85fb..60ab91b5 100644 --- a/native/kernel/svc/store_boot.c +++ b/native/kernel/svc/store_boot.c @@ -6,6 +6,7 @@ #include "disk_layout.h" #include "m5.h" #include "sha256.h" +#include "store_crash.h" #ifdef CK_HARDWARE_STAGING /* Hardware staging image: owner provisioning generated at build time by @@ -24,6 +25,14 @@ #define CK_SB_HW 0 #endif +/* TEST-ONLY Store crash hook (svc/store_crash.h), only in the image built + * with CK_TEST_STORE_CRASH=1; never with CK_HARDWARE_STAGING. */ +#ifdef CK_TEST_STORE_CRASH +#define CK_SB_HOOK ck_store_crash_hook +#else +#define CK_SB_HOOK 0 +#endif + #ifdef CK_STORE_TEST_KEYS #define LABEL_KVOL "AIENOS-LANE18-TEST-KVOL-NOT-SECRET" @@ -235,7 +244,10 @@ int store_boot_run_policy(const disk_dev *d, const ss_keys *keys, const uint8_t put64(rec + 16, r->boot_count_new); for (size_t i = 0; i < CK_BOOT_COMMIT_MAX && commit[i]; i++) rec[24 + i] = (uint8_t)commit[i]; ss_object obj = {CK_BOOT_KIND, CK_BOOT_VERSION, rec, sizeof rec}; - rc = ss_transact(g_ss, &obj, 1, 0, 0, 0); +#ifdef CK_TEST_STORE_CRASH + ck_store_crash_opened(r->gen_open); +#endif + rc = ss_transact(g_ss, &obj, 1, CK_SB_HOOK, 0, 0); if (rc) return fail(r, "commit", "ss_transact", rc); rc = disk_flush(d); if (rc) return fail(r, "io", "flush", rc); @@ -332,6 +344,9 @@ int ck_stage_store(void) } rc = store_boot_run(d, &keys, ck_owner_store_uuid, commit ? commit : "unknown", &r); #else +#ifdef CK_TEST_STORE_CRASH + d = ck_store_crash_setup(d); +#endif ck_store_test_keys(&keys); rc = store_boot_run(d, &keys, ck_store_test_uuid, commit ? commit : "unknown", &r); #endif diff --git a/native/kernel/svc/store_crash.c b/native/kernel/svc/store_crash.c new file mode 100644 index 00000000..7c3d3068 --- /dev/null +++ b/native/kernel/svc/store_crash.c @@ -0,0 +1,248 @@ +/* store_crash.c -- see store_crash.h. TEST-ONLY; empty unless + * CK_TEST_STORE_CRASH=1 (make full CK_TEST_STORE_CRASH=1). */ +#include "store_crash.h" + +#ifdef CK_TEST_STORE_CRASH +#ifdef CK_HARDWARE_STAGING +#error "CK_TEST_STORE_CRASH (TEST-ONLY Store crash hook) cannot be combined with CK_HARDWARE_STAGING" +#endif +#ifdef CK_QEMU_UNSAFE_DMA +#error "CK_TEST_STORE_CRASH cannot be combined with CK_QEMU_UNSAFE_DMA" +#endif +#include +#include "ck.h" +#include "disk_layout.h" +#include "store_engine.h" +#include "store_sealed.h" + +#define CR_MAX_WRITES 64u +#define CR_MAX_BYTES (256u * 1024u) +#define CR_PLAN_MAGIC "AIENCRSH v1 cp=" + +typedef struct { + uint64_t lba; + uint32_t count; + uint32_t off; +} cr_write; + +static struct { + const disk_dev *real; + disk_dev dev; + cr_write w[CR_MAX_WRITES]; + uint32_t n, used; + uint8_t *buf; + int armed, cp, policy; + uint64_t gen_open; +} cr; + +static const char *cp_name(int cp) +{ + if (cp == SS_CP_BEFORE_ANCHOR) return "before_anchor"; + if (cp == SS_CP_AFTER_ANCHOR) return "after_anchor"; + return st_checkpoint_name(cp); +} +static const char *const policy_names[] = {"drop", "all", "newest", "torn"}; + +static int str_eq_n(const char *a, const char *b, size_t n) +{ + for (size_t i = 0; i < n; i++) + if (a[i] != b[i]) return 0; + return 1; +} +static size_t str_len(const char *s) +{ + size_t n = 0; + while (s[n]) n++; + return n; +} +static void cp_bytes(uint8_t *d, const uint8_t *s, size_t n) +{ + while (n--) *d++ = *s++; +} + +/* Parses "AIENCRSH v1 cp= policy=\n". 0 ok, -1 malformed. */ +static int parse_plan(const char *p, size_t len, int *cp, int *policy) +{ + size_t m = sizeof CR_PLAN_MAGIC - 1, i = m, s; + static const int cps[] = {ST_CP_BEFORE_FIRST_WRITE, ST_CP_AFTER_PAYLOAD_OBJECTS, ST_CP_AFTER_CATALOG, + ST_CP_AFTER_COMMIT_RECORD, ST_CP_AFTER_FIRST_FLUSH, ST_CP_AFTER_INACTIVE_SUPERBLOCK, + ST_CP_AFTER_FINAL_FLUSH, SS_CP_BEFORE_ANCHOR, SS_CP_AFTER_ANCHOR}; + *cp = -1; + *policy = -1; + for (s = i; i < len && p[i] != ' '; i++) + ; + if (i >= len) return -1; + for (size_t k = 0; k < sizeof cps / sizeof cps[0]; k++) { + const char *nm = cp_name(cps[k]); + if (str_len(nm) == i - s && str_eq_n(p + s, nm, i - s)) *cp = cps[k]; + } + if (*cp < 0 || len - i < 8 || !str_eq_n(p + i, " policy=", 8)) return -1; + for (s = i = i + 8; i < len && p[i] != '\n'; i++) + ; + if (i >= len) return -1; + for (int k = 0; k < 4; k++) + if (str_len(policy_names[k]) == i - s && str_eq_n(p + s, policy_names[k], i - s)) *policy = k; + if (*policy < 0) return -1; + for (i = i + 1; i < len; i++) /* nothing after the line */ + if (p[i]) return -1; + return 0; +} + +static uint32_t pending_blocks(void) +{ + uint32_t b = 0; + for (uint32_t i = 0; i < cr.n; i++) b += cr.w[i].count; + return b; +} + +/* Writes cached write i (its first `count` blocks) to the real disk. */ +static int land(uint32_t i, uint32_t count) +{ + if (!count) return 0; + return disk_write(cr.real, cr.w[i].lba, count, cr.buf + cr.w[i].off); +} + +static int cr_read(void *ctx, uint64_t lba, uint32_t count, uint8_t *out) +{ + (void)ctx; + int rc = disk_read(cr.real, lba, count, out); + if (rc) return rc; + uint32_t bs = cr.real->block_size; + for (uint32_t i = 0; i < cr.n; i++) { /* later writes win */ + const cr_write *w = &cr.w[i]; + uint64_t a = lba > w->lba ? lba : w->lba; + uint64_t e1 = lba + count, e2 = w->lba + w->count, b = e1 < e2 ? e1 : e2; + if (a >= b) continue; + cp_bytes(out + (a - lba) * bs, cr.buf + w->off + (a - w->lba) * bs, (size_t)(b - a) * bs); + } + return 0; +} + +static int cr_write_fn(void *ctx, uint64_t lba, uint32_t count, const uint8_t *in) +{ + (void)ctx; + uint32_t bytes = count * cr.real->block_size; + if (cr.n >= CR_MAX_WRITES || cr.used + bytes > CR_MAX_BYTES) { + ck_printf("store_crash: volatile cache full (writes=%u bytes=%u); write refused, fail closed\n", cr.n, cr.used); + return DISK_EIO; + } + cr.w[cr.n] = (cr_write){lba, count, cr.used}; + cp_bytes(cr.buf + cr.used, in, bytes); + cr.n++; + cr.used += bytes; + return 0; +} + +static int cr_flush(void *ctx) +{ + (void)ctx; + for (uint32_t i = 0; i < cr.n; i++) { + int rc = land(i, cr.w[i].count); + if (rc) return rc; + } + cr.n = 0; + cr.used = 0; + return disk_flush(cr.real); +} + +const disk_dev *ck_store_crash_setup(const disk_dev *d) +{ + ck_puts("store_crash: TEST-ONLY Store crash hook image (CK_TEST_STORE_CRASH=1); " + "never counts toward a PASS of M4_STORE\n"); +#ifdef CK_TEST_STORE_MUTANT_SKIP_ROOT_FLUSH + ck_puts("store_crash: TEST-ONLY MUTANT skip_root_flush: the Store does not flush before the root write\n"); +#endif +#ifdef CK_TEST_STORE_MUTANT_ACCEPT_BAD_ROOT_CRC + ck_puts("store_crash: TEST-ONLY MUTANT accept_bad_root_crc: the Store ignores the superblock CRC\n"); +#endif + if (!d || (d->block_size != 512 && d->block_size != 4096)) return d; + uint32_t bpu = CK_LAYOUT_UNIT / d->block_size; + uint64_t units = d->block_count / bpu; + uint8_t *plan = ck_alloc(CK_LAYOUT_UNIT); + if (!units || !plan) { + ck_puts("store_crash: plan REFUSED (no plan buffer or no unit); Store runs on the disk directly\n"); + return d; + } + int rc = disk_read(d, (units - 1u) * bpu, bpu, plan); + if (rc) { + ck_printf("store_crash: plan REFUSED (read rc=%d); Store runs on the disk directly\n", rc); + return d; + } + size_t m = sizeof CR_PLAN_MAGIC - 1; + if (!str_eq_n((const char *)plan, CR_PLAN_MAGIC, m)) { + ck_puts("store_crash: no plan; Store runs on the disk directly\n"); + return d; + } + if (parse_plan((const char *)plan, CK_LAYOUT_UNIT, &cr.cp, &cr.policy)) { + ck_puts("store_crash: plan REFUSED (malformed); Store runs on the disk directly\n"); + return d; + } + cr.buf = ck_alloc(CR_MAX_BYTES); + if (!cr.buf) { + ck_puts("store_crash: plan REFUSED (no cache buffer); Store runs on the disk directly\n"); + return d; + } + cr.real = d; + cr.dev = *d; + cr.dev.ctx = &cr; + cr.dev.read = cr_read; + cr.dev.write = cr_write_fn; + cr.dev.flush = cr_flush; + cr.n = cr.used = 0; + cr.armed = 1; + ck_printf("store_crash: plan halt_at=%s policy=%s; Store runs on a volatile write cache\n", cp_name(cr.cp), + policy_names[cr.policy]); + return &cr.dev; +} + +void ck_store_crash_opened(uint64_t generation) +{ + cr.gen_open = generation; +} + +static __attribute__((noreturn)) void power_cut(int checkpoint) +{ + uint32_t pend = pending_blocks(), landed = 0, nw = cr.n; + int rc = 0; + switch (cr.policy) { + case CK_STORE_CRASH_POLICY_ALL: + for (uint32_t i = 0; i < cr.n && !rc; i++) { + rc = land(i, cr.w[i].count); + landed += cr.w[i].count; + } + break; + case CK_STORE_CRASH_POLICY_NEWEST: + if (cr.n) { + rc = land(cr.n - 1u, cr.w[cr.n - 1u].count); + landed = cr.w[cr.n - 1u].count; + } + break; + case CK_STORE_CRASH_POLICY_TORN: + if (cr.n) { + landed = cr.w[cr.n - 1u].count / 2u; + rc = land(cr.n - 1u, landed); + } + break; + default: + break; + } + if (!rc) rc = disk_flush(cr.real); + cr.n = 0; + cr.used = 0; + ck_printf("store_crash: HALT at %s policy=%s generation_open=%llu target=%llu pending_writes=%u " + "pending_blocks=%u landed_blocks=%u rc=%d (power cut: the host kills QEMU now)\n", + cp_name(checkpoint), policy_names[cr.policy], (unsigned long long)cr.gen_open, + (unsigned long long)(cr.gen_open + 1u), nw, pend, landed, rc); + for (;;) __asm__ volatile("wfi"); +} + +void ck_store_crash_hook(void *arg, int checkpoint) +{ + (void)arg; + ck_printf("store_crash: checkpoint %s generation_open=%llu pending_writes=%u\n", cp_name(checkpoint), + (unsigned long long)cr.gen_open, cr.armed ? cr.n : 0u); + if (cr.armed && checkpoint == cr.cp) power_cut(checkpoint); +} +#else +typedef int ck_store_crash_not_built; /* ISO C: no empty translation unit */ +#endif diff --git a/native/kernel/svc/store_crash.h b/native/kernel/svc/store_crash.h new file mode 100644 index 00000000..556d953e --- /dev/null +++ b/native/kernel/svc/store_crash.h @@ -0,0 +1,44 @@ +/* store_crash.h -- TEST-ONLY Store crash hook for the C kernel QEMU gate + * M4_STORE_CRASH (scripts/qemu_ck_store_crash_test.sh). + * + * Compiled only into the TEST image "make full CK_TEST_STORE_CRASH=1" + * ($(OUT)/full-test-store-crash/). The Makefile and store_crash.c refuse it + * together with CK_HARDWARE_STAGING or CK_QEMU_UNSAFE_DMA, and every default + * full image is checked to carry none of its code or strings. Such an image + * announces itself on the console and never counts toward a PASS of M4_STORE. + * + * Crash plan. The host writes one ASCII line into the last 4096-byte unit of + * the NVMe disk (the scratch unit of the devices-stage rw probe, which the + * probe restores byte for byte, dev/nvme_bind.c ck_disk_rw_probe): + * AIENCRSH v1 cp= policy=\n + * checkpoint: a native/store st_checkpoint_name() name, or before_anchor / + * after_anchor (store_sealed.h SS_CP_*). + * + * With a valid plan the Store stage runs on a volatile write cache wrapped + * around the boot disk: writes stay in RAM until the Store flushes, reads see + * them. At the planned checkpoint the hook models a power cut: per the policy + * none (drop), all (all, = a QEMU process kill), only the newest write call + * (newest, a device that reordered) or the first half of the blocks of the + * newest write call (torn) of the unflushed writes reach the disk, the disk is + * flushed, "store_crash: HALT at ..." is printed and the CPU stops forever. The + * host then kills QEMU. Without a plan the Store runs on the disk directly. */ +#ifndef AIENOS_CK_STORE_CRASH_H +#define AIENOS_CK_STORE_CRASH_H +#include +#include "disk.h" + +#ifdef CK_TEST_STORE_CRASH +#define CK_STORE_CRASH_POLICY_DROP 0 +#define CK_STORE_CRASH_POLICY_ALL 1 +#define CK_STORE_CRASH_POLICY_NEWEST 2 +#define CK_STORE_CRASH_POLICY_TORN 3 + +/* Prints the TEST-ONLY banner, reads the plan from the last unit of d. + * Returns the disk the Store must use: d without a plan, else the cache. */ +const disk_dev *ck_store_crash_setup(const disk_dev *d); +/* Generation the Store opened at (printed on every checkpoint line). */ +void ck_store_crash_opened(uint64_t generation); +/* st_hook: prints every checkpoint; halts at the planned one. */ +void ck_store_crash_hook(void *arg, int checkpoint); +#endif +#endif diff --git a/native/store/store_engine.c b/native/store/store_engine.c index d42cb4af..071e7368 100644 --- a/native/store/store_engine.c +++ b/native/store/store_engine.c @@ -547,10 +547,15 @@ int st_transact(st_store *s, const st_object *objects, size_t n, st_hook hook, v return ST_E_IO; } hook_at(hook, arg, ST_CP_AFTER_COMMIT_RECORD); + /* CK_TEST_STORE_MUTANT_SKIP_ROOT_FLUSH: TEST-ONLY mutant of the C kernel + * crash gate (native/kernel make full CK_TEST_STORE_CRASH=1 + * CK_TEST_STORE_CRASH_MUTANT=skip_root_flush); never defined otherwise. */ +#ifndef CK_TEST_STORE_MUTANT_SKIP_ROOT_FLUSH if (s->dev.flush(s->dev.ctx)) { s->poisoned = 1; return ST_E_IO; } +#endif hook_at(hook, arg, ST_CP_AFTER_FIRST_FLUSH); if (dev_write(&s->dev, p.root.sb.slot_id, ws->plan_sb)) { s->poisoned = 1; diff --git a/native/store/store_v1.c b/native/store/store_v1.c index fad4e1f7..ed338b5e 100644 --- a/native/store/store_v1.c +++ b/native/store/store_v1.c @@ -633,14 +633,25 @@ static uint32_t sb_crc(const uint8_t *b) /* 1 if the CRC field matches (engine helper crc_valid). */ int sv1_superblock_crc_ok(const uint8_t b[SV1_UNIT]) { +#ifdef CK_TEST_STORE_MUTANT_ACCEPT_BAD_ROOT_CRC + /* TEST-ONLY mutant of the C kernel crash gate (native/kernel full image + * with CK_TEST_STORE_CRASH=1 CK_TEST_STORE_CRASH_MUTANT=accept_bad_root_crc): + * every superblock CRC is accepted. Never defined otherwise. */ + (void)sb_crc; + (void)b; + return 1; +#else return sb_crc(b) == sv1_get32(b + SV1_SB_CRC_OFFSET); +#endif } int sv1_superblock_decode(const uint8_t *b, size_t len, uint32_t expected_slot, sv1_superblock *s) { if (len != SV1_UNIT || !sv1_equal(b, SUPERBLOCK_MAGIC, 8)) return SV1_E_BAD_SUPERBLOCK_MAGIC; /* GUARD:sb-magic */ +#ifndef CK_TEST_STORE_MUTANT_ACCEPT_BAD_ROOT_CRC if (sb_crc(b) != sv1_get32(b + SV1_SB_CRC_OFFSET)) return SV1_E_BAD_SUPERBLOCK_CRC; /* GUARD:sb-crc */ +#endif if (!sv1_all_zero(b + SV1_SB_RESERVED_OFFSET, SV1_UNIT - SV1_SB_RESERVED_OFFSET)) return SV1_E_NONZERO_RESERVED; /* GUARD:sb-reserved */ if (sv1_get16(b + 8) != 1 || sv1_get16(b + 10) != 0) return SV1_E_UNSUPPORTED_VERSION; /* GUARD:sb-version */ if (sv1_get64(b + 12) != 0 || sv1_get64(b + 20) != 0) diff --git a/scripts/ck_gates.sh b/scripts/ck_gates.sh index 7362e4b2..2685e6ec 100755 --- a/scripts/ck_gates.sh +++ b/scripts/ck_gates.sh @@ -5,7 +5,7 @@ # It refuses a dirty tree, runs the C kernel QEMU scripts that exist # (scripts/qemu_ck_boot_test.sh, scripts/qemu_ck_store_test.sh, # scripts/qemu_ck_net_test.sh, scripts/qemu_ck_artifact_test.sh, -# scripts/qemu_ck_smp_test.sh), and prints +# scripts/qemu_ck_smp_test.sh, scripts/qemu_ck_store_crash_test.sh), and prints # one line per gate: # AIENOS_CK_: PASS|FAIL|NOT_RUN [(reason)] # for M1 M3 SMMU NVME_SHUTDOWN P2_ARTIFACT M0_ROLLBACK M4_NVME M4_STORE M4_STORE_CRASH @@ -42,6 +42,7 @@ set -uo pipefail # net -> verdict line from scripts/qemu_ck_net_test.sh # artifact -> verdict line from scripts/qemu_ck_artifact_test.sh # smp -> verdict line from scripts/qemu_ck_smp_test.sh +# crash -> verdict line from scripts/qemu_ck_store_crash_test.sh # missing -> NOT_RUN (MISSING_IMPLEMENTATION), never run, never PASS # =========================================================================== CK_GATE_TABLE=' @@ -53,7 +54,7 @@ P2_ARTIFACT|artifact|- M0_ROLLBACK|missing|C loader signatures, A/B, BootNext and rollback are parked (native/boot/README.md) M4_NVME|store|- M4_STORE|store|- -M4_STORE_CRASH|missing|no C Store crash/kill campaign (4 KiB or 512 B) and no slot reuse (C Store is append-only) +M4_STORE_CRASH|crash|- M4_CONTINUITY|missing|no continuity core (ADR 0016 agent identity and memory) in the C kernel M4_RECOVERY|missing|no Recovery Core (ADR 0006) in the C kernel ARGUS1_REVOKE|store|- @@ -61,7 +62,7 @@ KEYBOARD|missing|no xHCI/USB HID keyboard driver in the C kernel NET|net|- SMP|smp|- ' -CHILDREN=(boot store net artifact smp) +CHILDREN=(boot store net artifact smp crash) # =========================================================================== repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" @@ -104,6 +105,7 @@ set_default_children() { [net]="${repo_root}/scripts/qemu_ck_net_test.sh" [artifact]="${repo_root}/scripts/qemu_ck_artifact_test.sh" [smp]="${repo_root}/scripts/qemu_ck_smp_test.sh" + [crash]="${repo_root}/scripts/qemu_ck_store_crash_test.sh" ) } @@ -333,11 +335,11 @@ self_test() { } expect_missing_all() { local g - for g in P2_ARTIFACT M0_ROLLBACK M4_STORE_CRASH M4_CONTINUITY M4_RECOVERY KEYBOARD; do expect "${g}" NOT_RUN; done # P2_ARTIFACT: no artifact child in A..G + for g in P2_ARTIFACT M0_ROLLBACK M4_STORE_CRASH M4_CONTINUITY M4_RECOVERY KEYBOARD; do expect "${g}" NOT_RUN; done # P2_ARTIFACT: no artifact child in A..G; M4_STORE_CRASH: no crash child in A..J and R } - scenario() { # NAME BOOT_SCRIPT STORE_SCRIPT [NET_SCRIPT] [ARTIFACT_SCRIPT] [SMP_SCRIPT] (absent: missing) + scenario() { # NAME BOOT_SCRIPT STORE_SCRIPT [NET_SCRIPT] [ARTIFACT_SCRIPT] [SMP_SCRIPT] [CRASH_SCRIPT] (absent: missing) scen="$1" - declare -gA child_script=([boot]="$2" [store]="$3" [net]="${4:-${tmp}/kids/net_not_present.sh}" [artifact]="${5:-${tmp}/kids/no_artifact_child.sh}" [smp]="${6:-${tmp}/kids/no_smp_child.sh}") + declare -gA child_script=([boot]="$2" [store]="$3" [net]="${4:-${tmp}/kids/net_not_present.sh}" [artifact]="${5:-${tmp}/kids/no_artifact_child.sh}" [smp]="${6:-${tmp}/kids/no_smp_child.sh}" [crash]="${7:-${tmp}/kids/no_crash_child.sh}") run_children 2>/dev/null evaluate_table >"${tmp}/${scen}.out" } @@ -345,7 +347,7 @@ self_test() { # A: all present gates PASS; the store child also claims PASS for gates # with no C implementation, which must stay NOT_RUN. scenario A "$(fake bootA 0 'PASS x' 'AIENOS_CK_M3: PASS' 'AIENOS_CK_M1: PASS')" \ - "$(fake storeA 0 'AIENOS_CK_M4_NVME: PASS' 'AIENOS_CK_M4_STORE: PASS' 'AIENOS_CK_ARGUS1_REVOKE: PASS (narrow)' \ + "$(fake storeA 0 'AIENOS_CK_M4_NVME: PASS' 'AIENOS_CK_M4_STORE: PASS' 'AIENOS_CK_M4_STORE_CRASH: PASS' 'AIENOS_CK_ARGUS1_REVOKE: PASS (narrow)' \ 'AIENOS_CK_SMMU: PASS' 'AIENOS_CK_NVME_SHUTDOWN: PASS' 'AIENOS_CK_KEYBOARD: PASS' 'PASS NVMe DMA granted confined' \ 'PASS DMA outside the window faulted, page intact, controller still usable (differs)' 'PASS NVMe DMA denied without an SMMU' \ 'PASS NVMe DMA granted through the TEST-ONLY bypass')" \ @@ -407,6 +409,22 @@ self_test() { scenario J "$(fake bootJ 0 'AIENOS_CK_M1: PASS')" "$(fake storeJ 0 'AIENOS_CK_M4_NVME: PASS')" "" \ "$(fake artJ 1 'FAIL P25WX' 'AIENOS_CK_P2_ARTIFACT: FAIL')" expect P2_ARTIFACT FAIL + # O: the crash child (scripts/qemu_ck_store_crash_test.sh) PASS -> M4_STORE_CRASH PASS, + # and only that child's line counts (the store child's claim in A was ignored). + # P: crash child FAIL -> FAIL. Q: crash child prints PASS but exits 1 with no + # FAIL line -> FAIL. S: crash child quiet flag held (exit 3) -> NOT_RUN. + scenario O "$(fake bootO 0 'AIENOS_CK_M1: PASS')" "$(fake storeO 0 'AIENOS_CK_M4_NVME: PASS')" "" "" "" \ + "$(fake crashO 0 'CK_STORE_CRASH_512B_QEMU: PASS' 'AIENOS_CK_M4_STORE_CRASH: PASS')" + expect M4_STORE_CRASH PASS; expect M4_CONTINUITY NOT_RUN; expect M4_RECOVERY NOT_RUN + scenario P "$(fake bootP 0 'AIENOS_CK_M1: PASS')" "$(fake storeP 0 'AIENOS_CK_M4_STORE_CRASH: PASS')" "" "" "" \ + "$(fake crashP 1 'bs=4096 settle=3 cp=after_final_flush -> BAD' 'AIENOS_CK_M4_STORE_CRASH: FAIL')" + expect M4_STORE_CRASH FAIL + scenario Q "$(fake bootQ 0 'AIENOS_CK_M1: PASS')" "$(fake storeQ 0 'AIENOS_CK_M4_NVME: PASS')" "" "" "" \ + "$(fake crashQ 1 'AIENOS_CK_M4_STORE_CRASH: PASS')" + expect M4_STORE_CRASH FAIL + scenario S "$(fake bootS 0 'AIENOS_CK_M1: PASS')" "$(fake storeS 0 'AIENOS_CK_M4_NVME: PASS')" "" "" "" \ + "$(fake crashS 3 'NOT_RUN quiet flag held' 'AIENOS_CK_M4_STORE_CRASH: NOT_RUN')" + expect M4_STORE_CRASH NOT_RUN # K: SMP child FAIL (a core never checked in) -> FAIL; L: quiet flag held # (exit 3) -> NOT_RUN; M: PASS line with an unexplained exit 2 -> FAIL; @@ -441,7 +459,7 @@ self_test() { jq -e '.physical == "NOT_RUN" and (.gates | length) == 15 and .verdict == "NOT_ALL_GATES_PASS" and ([.gates[] | select(.id == "M1")][0].verdict == "PASS") and ([.gates[] | select(.id == "ARGUS1_REVOKE")][0].verdict == "FAIL") - and (.children | length) == 5 and .commit_subject == "quote \" backslash \\ tab\tend"' "${receipt_path}" >/dev/null \ + and (.children | length) == 6 and .commit_subject == "quote \" backslash \\ tab\tend"' "${receipt_path}" >/dev/null \ && ok "receipt is valid JSON (jq) with expected fields" || bad "receipt JSON invalid or fields wrong" else echo "SKIP jq not installed; JSON validity not machine-checked" @@ -460,7 +478,7 @@ self_test() { [[ -n "${g}" ]] || continue rows=$((rows + 1)) case "${s}" in - boot|store|net|artifact|smp) [[ "${w}" == - ]] || tbad=1 ;; + boot|store|net|artifact|smp|crash) [[ "${w}" == - ]] || tbad=1 ;; missing) [[ -n "${w}" && "${w}" != - ]] || tbad=1 ;; *) tbad=1 ;; esac diff --git a/scripts/qemu_ck_store_crash_test.sh b/scripts/qemu_ck_store_crash_test.sh new file mode 100755 index 00000000..721ae4b4 --- /dev/null +++ b/scripts/qemu_ck_store_crash_test.sh @@ -0,0 +1,648 @@ +#!/usr/bin/env bash +# Store crash gate for the AIENOS C kernel: CK gate M4_STORE_CRASH. +# C-kernel port of the Rust campaigns scripts/qemu_store_crash_test.sh +# (4096-byte LBA) and scripts/qemu_store_512b_crash_test.sh (512-byte LBA). +# QEMU is not hardware: a PASS here qualifies nothing physical. +# +# Images (native/kernel/Makefile): +# - make full default image: every setup and verify boot +# - make full CK_TEST_STORE_CRASH=1 +# TEST-ONLY crash hook image (svc/store_crash.h): +# only the crash boots. Never counts toward a +# PASS of M4_STORE; refused with +# CK_HARDWARE_STAGING and CK_QEMU_UNSAFE_DMA. +# +# Power-cut model. The host writes a crash plan into the last 4096-byte unit +# of the disk (the rw-probe scratch unit, restored by the devices stage): +# "AIENCRSH v1 cp= policy=". The TEST image +# then runs the Store on a volatile write cache: nothing reaches the disk until +# the Store flushes. At the planned checkpoint it lands, per policy, none / all +# / only the newest write call / the first half of the blocks of the newest +# write call of the unflushed writes, flushes, prints "store_crash: HALT at" +# and stops; the host kills QEMU (SIGKILL). policy=all is the Rust tier 1 +# model (QEMU killed, every accepted write is in the host file); drop, newest +# and torn model a lost or reordered volatile cache and a root write cut +# between sectors, which the Rust campaign does not boot. +# +# Commit stages of one boot commit (native/store/store_engine.c st_transact and +# store_sealed.c ss_commit_prepared), checkpoint name -> what it models: +# before_first_write .. after_commit_record before the data flush +# after_first_flush after data, before the root +# after_inactive_superblock during/after the root write, +# before the root flush +# after_final_flush, before_anchor root durable, anchor behind +# after_anchor commit and anchor durable +# +# Campaign, per geometry (512 B and 4096 B namespaces, fresh 64 MiB image) and +# per settle (0: the crash boot formats a blank disk; 3: three default boots +# first, as the Rust settle 3): every checkpoint with policy=all, plus +# after_commit_record/newest and after_inactive_superblock drop/newest/torn. +# Each case: crash boot (TEST image), plan cleared, verify boot (default +# image). N = the generation the crash boot opened (must be 1 + settle). +# The verify boot must pass the M1 checks, exit 0, print "stage store: ok", no +# "store: REFUSED", no reformat, and open: +# N before the root write landed (Rust: "N") +# N + 1 after the final flush (Rust: "NP1"), and for policy=all at +# after_inactive_superblock (QEMU kill: the root write is in the file) +# N or N+1 newest/torn at after_inactive_superblock (Rust: "N_NP1") +# with boot_count and prev_commit of that generation, anchor +# prepared-advance while the anchor is behind (native/store/README.md "Commit +# order"), then commit generation + 1. +# Root tear closure (Rust tier 2a, here on the QEMU images): for settle 3 the +# after_inactive_superblock/all crash changes exactly one superblock slot, +# that slot then holds generation N + 1, and every changed byte lies in the +# slot's first 512 bytes, so any sector-granular tear of the root write +# leaves either the old or the new slot bytes. +# Injected root (Rust tier 2b, one case): that crash image with one byte of the +# new root's CRC flipped must not open generation N + 1: Store refused, nothing +# committed, disk left as found (image sha256 unchanged). +# Hardware refusal: make must refuse CK_TEST_STORE_CRASH with +# CK_HARDWARE_STAGING and with CK_QEMU_UNSAFE_DMA; the default image must +# carry no crash hook string; the TEST image must announce itself. +# Not covered: Rust STORE_SLOT_REUSE_QEMU (the C Store is append-only, no +# reclaim) is printed NOT_RUN and is not part of this gate. +# +# Usage: bash scripts/qemu_ck_store_crash_test.sh the gate +# bash scripts/qemu_ck_store_crash_test.sh --self-test host only, no QEMU: +# judges, expectation table, plan bytes, tear-closure check and +# make refusals against canned inputs and counterexamples +# bash scripts/qemu_ck_store_crash_test.sh --mutant skip_root_flush +# bash scripts/qemu_ck_store_crash_test.sh --mutant accept_bad_root_crc +# builds the TEST-ONLY broken Store (CK_TEST_STORE_CRASH_MUTANT) and +# runs the cases that must catch it, on that image only. Prints +# AIENOS_CK_STORE_CRASH_MUTANT_: KILLED (exit 0) when the +# gate's checks FAIL on it, SURVIVED (exit 1) otherwise. Never +# prints the AIENOS_CK_M4_STORE_CRASH line. +# Takes the machine quiet flag like qemu_ck_store_test.sh (not in --self-test). +# Final line (gate): AIENOS_CK_M4_STORE_CRASH: PASS|FAIL|NOT_RUN. +# Exit: 0 PASS, 1 FAIL, 2 missing tools/build failure, 3 quiet flag held. +set -uo pipefail + +repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +cd "${repo_root}" + +mode=gate; mutant="" +case "${1:-}" in + "") ;; + --self-test) mode=selftest ;; + --mutant) + mode=mutant; mutant="${2:-}" + [[ "${mutant}" == skip_root_flush || "${mutant}" == accept_bad_root_crc ]] \ + || { echo "usage: $0 [--self-test | --mutant skip_root_flush|accept_bad_root_crc]" >&2; exit 2; } ;; + *) echo "usage: $0 [--self-test | --mutant skip_root_flush|accept_bad_root_crc]" >&2; exit 2 ;; +esac + +verdict() { + if [[ "${mode}" == mutant ]]; then + echo "AIENOS_CK_STORE_CRASH_MUTANT_${mutant^^}: $1" + else + echo "AIENOS_CK_M4_STORE_CRASH: $1" + fi +} + +img_bytes=67108864 +unit=4096 +units=$(( img_bytes / unit )) +plan_unit=$(( units - 1 )) +store_off=16384 # 4 anchor units (dev/disk_layout.h; qemu_ck_store_test.sh checks it) +sb_gen_off=56 # superblock generation (native/store/store_engine.c st_open gen_of) +sb_crc_off=168 # SV1_SB_CRC_OFFSET (native/store/store_v1.h) +cps=(before_first_write after_payload_objects after_catalog after_commit_record after_first_flush + after_inactive_superblock after_final_flush before_anchor after_anchor) +# Gate cases beyond "every checkpoint, policy all" (per geometry and settle). +gate_extra=("after_commit_record newest" "after_inactive_superblock drop" "after_inactive_superblock newest" "after_inactive_superblock torn") +gate_geos=(512 4096); gate_settles=(0 3) +# Result rows a complete gate campaign prints: 2 x 2 x (9 + 4) = 52. +gate_rows_want=$(( ${#gate_geos[@]} * ${#gate_settles[@]} * (${#cps[@]} + ${#gate_extra[@]}) )) +commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" + +# rows_all_ok FILE WANT: port of rows_all_ok in scripts/qemu_store_crash_test.sh +# lines 19-31 (main e4f1513 and later). True only when FILE holds exactly WANT +# result rows (lines with " -> ") and every one ends in "-> OK". A missing +# file, an empty or short campaign, an extra row or any BAD row is false, so a +# loop that never ran (or stopped early) cannot read as PASS. +rows_all_ok() { + local f="$1" want="$2" total=0 ok=0 + rows_why="" + [[ -f "${f}" ]] || { rows_why="results file ${f##*/} missing (want ${want} OK rows)"; return 1; } + total=$(grep -c ' -> ' "${f}" || true) + ok=$(grep -c ' -> OK$' "${f}" || true) + if [[ "${want}" -gt 0 && "${total}" == "${want}" && "${ok}" == "${want}" ]]; then return 0; fi + rows_why="${f##*/} has ${total} rows, ${ok} OK; want exactly ${want} OK" + return 1 +} + +# --------------------------------------------------------------- pure logic +# expect_for CHECKPOINT POLICY -> N | NP1 | N_NP1 (generation the verify boot opens) +expect_for() { + case "$1" in + after_inactive_superblock) + case "$2" in all) echo NP1 ;; drop) echo N ;; *) echo N_NP1 ;; esac ;; + after_final_flush|before_anchor|after_anchor) echo NP1 ;; + *) echo N ;; + esac +} +# anchor_ok CHECKPOINT OPENED_GEN N ANCHOR -> 0 if the anchor state fits +anchor_ok() { + local cp="$1" g="$2" n="$3" a="$4" + if [[ "${g}" == "${n}" ]]; then [[ "${a}" == valid-resume || "${a}" == genesis ]]; return; fi + case "${cp}" in + after_anchor) [[ "${a}" == valid-resume ]] ;; + *) [[ "${a}" == prepared-advance ]] ;; + esac +} + +# plan_bytes CP POLICY: the 4096-byte plan unit on stdout. +plan_bytes() { + { printf 'AIENCRSH v1 cp=%s policy=%s\n' "$1" "$2"; head -c "${unit}" /dev/zero; } | head -c "${unit}" +} +write_plan() { plan_bytes "$2" "$3" | dd of="$1" bs="${unit}" seek="${plan_unit}" conv=notrunc status=none; } +clear_plan() { head -c "${unit}" /dev/zero | dd of="$1" bs="${unit}" seek="${plan_unit}" conv=notrunc status=none; } + +# judge_crash SERIAL CP POLICY SETTLE: sets crash_n and crash_why; returns 0 if +# the crash boot halted exactly at CP with the planned policy. +judge_crash() { + local s="$1" cp="$2" pol="$3" settle="$4" re seq want="" c + crash_n=""; crash_why="" + grep -qF "store_crash: TEST-ONLY Store crash hook image (CK_TEST_STORE_CRASH=1)" "${s}" || { crash_why="no TEST-ONLY banner"; return 1; } + grep -qxF "store_crash: plan halt_at=${cp} policy=${pol}; Store runs on a volatile write cache" "${s}" \ + || { crash_why="plan not armed"; return 1; } + re="^store_crash: HALT at ${cp} policy=${pol} generation_open=([0-9]+) target=([0-9]+) pending_writes=[0-9]+ pending_blocks=[0-9]+ landed_blocks=[0-9]+ rc=0 " + local line; line="$(grep -E "^store_crash: HALT at " "${s}" || true)" + [[ "$(grep -c . <<<"${line}")" == 1 && "${line}" =~ ${re} ]] || { crash_why="no single HALT at ${cp} with rc=0"; return 1; } + crash_n="${BASH_REMATCH[1]}" + [[ "${BASH_REMATCH[2]}" == $(( crash_n + 1 )) ]] || { crash_why="target is not generation_open + 1"; return 1; } + [[ "${crash_n}" == $(( 1 + settle )) ]] || { crash_why="opened generation ${crash_n}, expected $(( 1 + settle ))"; return 1; } + seq="$(sed -nE 's/^store_crash: checkpoint ([a-z_]+) generation_open=([0-9]+) .*/\1:\2/p' "${s}" | paste -sd' ' -)" + for c in "${cps[@]}"; do want+="${want:+ }${c}:${crash_n}"; [[ "${c}" == "${cp}" ]] && break; done + [[ "${seq}" == "${want}" ]] || { crash_why="checkpoint order '${seq}', expected '${want}'"; return 1; } + grep -q "^store: committed" "${s}" && { crash_why="Store reported a commit before the halt"; return 1; } + return 0 +} + +# judge_verify SERIAL CP POLICY SETTLE N: sets v_gen and v_why; 0 if the +# verify boot opened the right committed generation and committed on top. +judge_verify() { + local s="$1" cp="$2" pol="$3" settle="$4" n="$5" exp ore cre oline cline g bc prev anc cg cbc + v_gen=""; v_why="" + exp="$(expect_for "${cp}" "${pol}")" + grep -q "^stage store: ok" "${s}" || { v_why="stage store not ok"; return 1; } + grep -q "store: REFUSED" "${s}" && { v_why="Store refused"; return 1; } + grep -q "formatted TEST store" "${s}" && { v_why="Store reformatted after the crash"; return 1; } + grep -q "^store_crash: HALT" "${s}" && { v_why="verify boot halted"; return 1; } + ore='^store: opened generation=([0-9]+) boot_count=([0-9]+) prev_commit=([^ ]+) anchor=([a-z?-]+)$' + cre='^store: committed generation=([0-9]+) boot_count=([0-9]+)$' + oline="$(grep -E "${ore}" "${s}" || true)"; cline="$(grep -E "${cre}" "${s}" || true)" + [[ "$(grep -c . <<<"${oline}")" == 1 && "${oline}" =~ ${ore} ]] || { v_why="no single opened line"; return 1; } + g="${BASH_REMATCH[1]}"; bc="${BASH_REMATCH[2]}"; prev="${BASH_REMATCH[3]}"; anc="${BASH_REMATCH[4]}" + v_gen="${g}" + [[ "$(grep -c . <<<"${cline}")" == 1 && "${cline}" =~ ${cre} ]] || { v_why="no single committed line"; return 1; } + cg="${BASH_REMATCH[1]}"; cbc="${BASH_REMATCH[2]}" + case "${exp}" in + N) [[ "${g}" == "${n}" ]] ;; + NP1) [[ "${g}" == $(( n + 1 )) ]] ;; + N_NP1) [[ "${g}" == "${n}" || "${g}" == $(( n + 1 )) ]] ;; + esac || { v_why="opened generation ${g}, expected ${exp} (N=${n})"; return 1; } + local want_bc=$(( settle + (g == n + 1 ? 1 : 0) )) + [[ "${bc}" == "${want_bc}" ]] || { v_why="boot_count ${bc} at generation ${g}, expected ${want_bc}"; return 1; } + if [[ "${bc}" == 0 ]]; then [[ "${prev}" == none ]]; else [[ "${prev}" == "${commit}" ]]; fi \ + || { v_why="prev_commit ${prev}"; return 1; } + anchor_ok "${cp}" "${g}" "${n}" "${anc}" || { v_why="anchor ${anc} at generation ${g} (cp ${cp})"; return 1; } + [[ "${cg}" == $(( g + 1 )) && "${cbc}" == $(( bc + 1 )) ]] \ + || { v_why="committed generation ${cg} boot_count ${cbc} after opening ${g}/${bc}"; return 1; } + return 0 +} + +# judge_injected SERIAL N: 0 if the bad-CRC root (generation N+1) was not +# opened and nothing was committed (the image hash is checked by the caller). +judge_injected() { + local s="$1" n="$2" + inj_why="" + grep -q "^store: opened generation=$(( n + 1 )) " "${s}" && { inj_why="opened the corrupt root generation $(( n + 1 ))"; return 1; } + grep -q "^store: committed" "${s}" && { inj_why="committed on top of a corrupt root"; return 1; } + grep -q "^store: REFUSED " "${s}" || { inj_why="no Store refusal"; return 1; } + grep -q "disk left as found, not reformatted" "${s}" || { inj_why="no 'disk left as found'"; return 1; } + grep -q "formatted TEST store" "${s}" && { inj_why="reformatted"; return 1; } + return 0 +} + +u64_at() { od --endian=little -An -tu8 -j "$2" -N 8 "$1" | tr -d ' '; } + +# tear_closure BEFORE AFTER N: 0 if exactly one superblock slot changed, it +# holds generation N+1 and every changed byte is in its first 512 bytes. +tear_closure() { + local b="$1" a="$2" n="$3" diffs slots s off + tc_why=""; tc_slot="" + diffs="$(cmp -l "${b}" "${a}" 2>/dev/null | awk -v lo="${store_off}" -v hi="$(( store_off + 2 * unit ))" '$1-1 >= lo && $1-1 < hi {print $1-1}')" + [[ -n "${diffs}" ]] || { tc_why="no superblock slot changed"; return 1; } + slots="$(awk -v lo="${store_off}" -v u="${unit}" '{print int(($1 - lo) / u)}' <<<"${diffs}" | sort -u)" + [[ "$(grep -c . <<<"${slots}")" == 1 ]] || { tc_why="both superblock slots changed"; return 1; } + s="${slots}"; off=$(( store_off + s * unit )) + awk -v o="${off}" '$1 - o >= 512 {bad=1} END {exit bad}' <<<"${diffs}" \ + || { tc_why="slot ${s} changed beyond its first 512-byte sector"; return 1; } + [[ "$(u64_at "${a}" $(( off + sb_gen_off )))" == $(( n + 1 )) ]] \ + || { tc_why="changed slot ${s} does not hold generation $(( n + 1 ))"; return 1; } + tc_slot="${s}" + return 0 +} + +# make_refusals: the TEST hook must be refused with hardware staging and the +# unsafe DMA build; a mutant needs the hook; unknown mutants refused. Parse +# only (make -n): nothing is built. +make_refusals() { + local out f=0 + mk() { make -n -s -C native/kernel OUT="${1}" full "${@:2}" 2>&1; } + local tmpo; tmpo="$(mktemp -d)" + out="$(mk "${tmpo}" CK_TEST_STORE_CRASH=1 CK_HARDWARE_STAGING=1 CK_OWNER_PUBKEYS=/dev/null CK_MACHINE_ID=/dev/null)" + grep -q "CK_TEST_STORE_CRASH (TEST-ONLY Store crash hook) cannot be combined with CK_HARDWARE_STAGING" <<<"${out}" \ + && echo "PASS make refuses the crash hook with CK_HARDWARE_STAGING" || { echo "FAIL hardware staging accepted the crash hook"; f=1; } + out="$(mk "${tmpo}" CK_TEST_STORE_CRASH=1 CK_QEMU_UNSAFE_DMA=1)" + grep -q "cannot be combined with CK_QEMU_UNSAFE_DMA" <<<"${out}" \ + && echo "PASS make refuses the crash hook with CK_QEMU_UNSAFE_DMA" || { echo "FAIL unsafe DMA build accepted the crash hook"; f=1; } + out="$(mk "${tmpo}" CK_TEST_STORE_CRASH_MUTANT=skip_root_flush)" + grep -q "CK_TEST_STORE_CRASH_MUTANT needs CK_TEST_STORE_CRASH=1" <<<"${out}" \ + && echo "PASS make refuses a mutant without the TEST hook" || { echo "FAIL mutant built without the TEST hook"; f=1; } + out="$(mk "${tmpo}" CK_TEST_STORE_CRASH=1 CK_TEST_STORE_CRASH_MUTANT=bogus)" + grep -q "CK_TEST_STORE_CRASH_MUTANT must be skip_root_flush or accept_bad_root_crc" <<<"${out}" \ + && echo "PASS make refuses an unknown mutant" || { echo "FAIL unknown mutant accepted"; f=1; } + out="$(mk "${tmpo}" CK_TEST_STORE_CRASH=1)" + grep -qE "^\*\*\*|Error|error:" <<<"${out}" && { echo "FAIL plain TEST crash build refused: $(head -3 <<<"${out}")"; f=1; } \ + || echo "PASS make accepts the plain TEST crash build (control)" + rm -rf "${tmpo}" + return "${f}" +} + +# ----------------------------------------------------------------- self-test +self_test() { + local st=0 t s n + t="$(mktemp -d)"; trap 'rm -rf "${t}"' RETURN + ok() { echo "PASS $*"; } + bad() { echo "FAIL $*"; st=1; } + [[ "$(expect_for after_commit_record all)" == N && "$(expect_for after_first_flush all)" == N \ + && "$(expect_for after_inactive_superblock all)" == NP1 && "$(expect_for after_inactive_superblock drop)" == N \ + && "$(expect_for after_inactive_superblock torn)" == N_NP1 && "$(expect_for after_final_flush all)" == NP1 \ + && "$(expect_for before_anchor all)" == NP1 && "$(expect_for after_anchor all)" == NP1 \ + && "$(expect_for before_first_write newest)" == N ]] && ok "expectation table" || bad "expectation table" + + # plan bytes: exact line, zero padded to one unit + plan_bytes after_catalog torn >"${t}/plan" + [[ "$(stat -c %s "${t}/plan")" == "${unit}" && "$(head -c 40 "${t}/plan")" == "AIENCRSH v1 cp=after_catalog policy=torn" \ + && "$(tail -c +41 "${t}/plan" | tr -d '\0' | od -An -c | tr -d ' ')" == '\n' ]] && ok "plan unit bytes" || bad "plan unit bytes" + truncate -s "${img_bytes}" "${t}/img"; write_plan "${t}/img" after_anchor all + cmp -s <(dd if="${t}/img" bs="${unit}" skip="${plan_unit}" count=1 status=none) <(plan_bytes after_anchor all) \ + && [[ "$(stat -c %s "${t}/img")" == "${img_bytes}" ]] && ok "plan written into the last unit only" || bad "plan placement" + clear_plan "${t}/img"; cmp -s "${t}/img" <(head -c "${img_bytes}" /dev/zero) && ok "plan cleared" || bad "plan clear" + + # crash judge + n=4; s="${t}/crash.txt" + { echo "store_crash: TEST-ONLY Store crash hook image (CK_TEST_STORE_CRASH=1); never counts toward a PASS of M4_STORE" + echo "store_crash: plan halt_at=after_catalog policy=newest; Store runs on a volatile write cache" + for c in before_first_write after_payload_objects after_catalog; do echo "store_crash: checkpoint ${c} generation_open=${n} pending_writes=1"; done + echo "store_crash: HALT at after_catalog policy=newest generation_open=${n} target=5 pending_writes=2 pending_blocks=2 landed_blocks=1 rc=0 (power cut: the host kills QEMU now)"; } >"${s}" + judge_crash "${s}" after_catalog newest 3 && ok "crash judge accepts a halt at the planned checkpoint" || bad "crash judge: ${crash_why}" + judge_crash "${s}" after_catalog newest 0 && bad "crash judge accepted the wrong settle" || ok "crash judge refuses N != 1 + settle (${crash_why})" + judge_crash "${s}" after_commit_record newest 3 && bad "crash judge accepted another checkpoint" || ok "crash judge refuses a halt elsewhere (${crash_why})" + grep -v "checkpoint after_payload_objects" "${s}" >"${t}/c2" + judge_crash "${t}/c2" after_catalog newest 3 && bad "crash judge accepted a skipped checkpoint" || ok "crash judge refuses a gap in the checkpoint order (${crash_why})" + sed 's/rc=0/rc=-1/' "${s}" >"${t}/c3" + judge_crash "${t}/c3" after_catalog newest 3 && bad "crash judge accepted rc!=0" || ok "crash judge refuses a failed landing (${crash_why})" + grep -v "TEST-ONLY Store crash hook image" "${s}" >"${t}/c4" + judge_crash "${t}/c4" after_catalog newest 3 && bad "crash judge accepted no banner" || ok "crash judge refuses an image without the banner" + + # verify judge + vf() { # file gen bc prev anchor cgen cbc [extra line] + { echo "stage store: ok"; echo "store: opened generation=$2 boot_count=$3 prev_commit=$4 anchor=$5" + echo "store: committed generation=$6 boot_count=$7"; [[ -z "${8:-}" ]] || echo "$8"; } >"$1"; } + vf "${t}/v1" 4 3 "${commit}" valid-resume 5 4 + judge_verify "${t}/v1" after_catalog all 3 4 && ok "verify judge: N at after_catalog" || bad "verify judge N: ${v_why}" + judge_verify "${t}/v1" after_final_flush all 3 4 && bad "verify judge accepted N where N+1 is required" || ok "verify judge refuses N after the final flush (${v_why})" + vf "${t}/v2" 5 4 "${commit}" prepared-advance 6 5 + judge_verify "${t}/v2" after_final_flush all 3 4 && ok "verify judge: N+1 prepared-advance after the final flush" || bad "verify judge NP1: ${v_why}" + judge_verify "${t}/v2" after_first_flush all 3 4 && bad "verify judge accepted N+1 before the root write" || ok "verify judge refuses N+1 before the root (${v_why})" + judge_verify "${t}/v2" after_inactive_superblock torn 3 4 && ok "verify judge: N+1 allowed for a torn root" || bad "verify judge torn: ${v_why}" + judge_verify "${t}/v2" after_anchor all 3 4 && bad "verify judge accepted prepared-advance after the anchor" || ok "verify judge refuses an anchor behind after_anchor (${v_why})" + vf "${t}/v3" 5 3 "${commit}" prepared-advance 6 4 + judge_verify "${t}/v3" after_final_flush all 3 4 && bad "verify judge accepted a wrong boot_count" || ok "verify judge refuses a wrong boot_count (${v_why})" + vf "${t}/v4" 4 3 "${commit}" valid-resume 5 4 "store: blank disk (all-zero anchor + Store head): formatted TEST store" + judge_verify "${t}/v4" after_catalog all 3 4 && bad "verify judge accepted a reformat" || ok "verify judge refuses a reformat (${v_why})" + vf "${t}/v5" 1 0 none valid-resume 2 1 + judge_verify "${t}/v5" before_first_write all 0 1 && ok "verify judge: settle 0, prev_commit none" || bad "verify judge settle 0: ${v_why}" + vf "${t}/v6" 4 3 "${commit}" valid-resume 6 4 + judge_verify "${t}/v6" after_catalog all 3 4 && bad "verify judge accepted a commit that skipped a generation" || ok "verify judge refuses a skipped commit generation (${v_why})" + { echo "stage store: FAIL"; echo "store: REFUSED proof=commit step=\"ss_transact\" rc=-203 (x); disk left as found, not reformatted"; } >"${t}/v7" + judge_verify "${t}/v7" after_catalog all 3 4 && bad "verify judge accepted a refused Store" || ok "verify judge refuses a refused Store (${v_why})" + + # injected-root judge + judge_injected "${t}/v7" 4 && ok "injected judge: refusal accepted" || bad "injected judge: ${inj_why}" + judge_injected "${t}/v2" 4 && bad "injected judge accepted the corrupt root" || ok "injected judge refuses an opened corrupt root (${inj_why})" + + # tear closure on synthetic images + truncate -s 65536 "${t}/b"; cp "${t}/b" "${t}/a" + printf 'SBMAGIC!' | dd of="${t}/a" bs=1 seek=$(( store_off + unit )) conv=notrunc status=none + printf '\005\0\0\0\0\0\0\0' | dd of="${t}/a" bs=1 seek=$(( store_off + unit + sb_gen_off )) conv=notrunc status=none + tear_closure "${t}/b" "${t}/a" 4 && [[ "${tc_slot}" == 1 ]] && ok "tear closure: one slot, sector 0, generation N+1" || bad "tear closure: ${tc_why}" + tear_closure "${t}/b" "${t}/a" 3 && bad "tear closure accepted the wrong generation" || ok "tear closure refuses the wrong generation (${tc_why})" + cp "${t}/a" "${t}/a2"; printf 'x' | dd of="${t}/a2" bs=1 seek=$(( store_off + unit + 600 )) conv=notrunc status=none + tear_closure "${t}/b" "${t}/a2" 4 && bad "tear closure accepted a change past sector 0" || ok "tear closure refuses a change past sector 0 (${tc_why})" + cp "${t}/a" "${t}/a3"; printf 'x' | dd of="${t}/a3" bs=1 seek=$(( store_off + 3 )) conv=notrunc status=none + tear_closure "${t}/b" "${t}/a3" 4 && bad "tear closure accepted two changed slots" || ok "tear closure refuses two changed slots (${tc_why})" + + # Row tally (mirrors the Rust --verdict-self-test, scripts/qemu_store_crash_test.sh + # lines 33-53): only a complete all-OK table passes. + [[ "${gate_rows_want}" == 52 ]] && ok "gate campaign wants 52 rows (2 geometries x 2 settles x 13 cases)" \ + || bad "gate campaign row count ${gate_rows_want}, expected 52" + want_rows() { # NAME WANT EXPECT(0 pass|1 fail) LINES... + local name="$1" want="$2" expect="$3" got=0; shift 3 + : >"${t}/rows"; for l in "$@"; do printf '%s\n' "${l}" >>"${t}/rows"; done + rows_all_ok "${t}/rows" "${want}" || got=1 + [[ "${got}" == "${expect}" ]] && ok "row tally: ${name} -> $([[ ${got} == 0 ]] && echo PASS || echo FAIL)" \ + || bad "row tally: ${name} gave the wrong verdict" + } + want_rows "empty campaign" 3 1 + want_rows "one row short" 3 1 "a -> OK" "b -> OK" + want_rows "one BAD row" 3 1 "a -> OK" "b -> BAD (x)" "c -> OK" + want_rows "one extra row" 3 1 "a -> OK" "b -> OK" "c -> OK" "d -> OK" + want_rows "zero rows wanted is never a pass" 0 1 + want_rows "complete, all OK" 3 0 "a -> OK" "b -> OK" "c -> OK" + rm -f "${t}/rows"; rows_all_ok "${t}/rows" 3 && bad "row tally: missing file read as PASS" || ok "row tally: missing results file -> FAIL" + # The gate's own row format: a real OK and BAD line from run_case's printf. + printf 'bs=%-4s settle=%s cp=%-26s policy=%-6s saw=%s N=%-2s opened=%-3s expect=%-5s -> %s%s\n' \ + 512 0 after_catalog all 1 1 1 N OK "" 4096 3 after_anchor all 1 4 5 NP1 BAD " (verify: x)" >"${t}/rows" + rows_all_ok "${t}/rows" 2 && bad "row tally: a BAD gate row passed" || ok "row tally: gate-format BAD row -> FAIL (${rows_why})" + rows_all_ok "${t}/rows" 1 && bad "row tally: over-full gate rows passed" || ok "row tally: gate-format extra row -> FAIL" + + if command -v make >/dev/null; then + make_refusals || st=1 + else + bad "make not installed: refusal checks not run" + fi + [[ "${st}" == 0 ]] && echo "CK_STORE_CRASH_SELFTEST: PASS" || echo "CK_STORE_CRASH_SELFTEST: FAIL" + return "${st}" +} + +if [[ "${mode}" == selftest ]]; then + self_test; exit $? +fi + +# ------------------------------------------------------------------ QEMU run +code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" +vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" +command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; verdict NOT_RUN; exit 2; } +[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; verdict NOT_RUN; exit 2; } + +cross="" +if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi +out="${repo_root}/target/native-kernel" +mk_full() { make -s -C native/kernel CROSS="${cross}" OUT="${out}" AIENOS_COMMIT="${commit}" full "$@" >/dev/null; } +if [[ "${mode}" == mutant ]]; then + mk_full CK_TEST_STORE_CRASH=1 CK_TEST_STORE_CRASH_MUTANT="${mutant}" || { echo "mutant build failed"; verdict NOT_RUN; exit 2; } + crash_efi="${out}/full-test-store-crash-mutant-${mutant}/BOOTAA64.EFI" + plain_efi="${crash_efi}" # every boot of a mutant run uses the broken Store +else + mk_full || { echo "build failed"; verdict NOT_RUN; exit 2; } + mk_full CK_TEST_STORE_CRASH=1 || { echo "TEST crash build failed"; verdict NOT_RUN; exit 2; } + crash_efi="${out}/full-test-store-crash/BOOTAA64.EFI" + plain_efi="${out}/full/BOOTAA64.EFI" +fi + +quiet_flag="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}" +quiet_tag="${AIENOS_QUIET_TAG:-qemu_ck_store_crash_test $$}" +if ! ( set -C; echo "${quiet_tag}" > "${quiet_flag}" ) 2>/dev/null; then + echo "NOT_RUN quiet flag ${quiet_flag} is held: $(head -c 200 "${quiet_flag}" 2>/dev/null || true)" + verdict NOT_RUN + exit 3 +fi +own_flag=1 +release_flag() { + if [[ "${own_flag}" == 1 ]]; then + own_flag=0 + if [[ -f "${quiet_flag}" ]] && grep -qxF -- "${quiet_tag}" "${quiet_flag}"; then rm -f "${quiet_flag}"; fi + fi +} +top="$(mktemp -d)" +qemu_pid="" +cleanup() { [[ -n "${qemu_pid}" ]] && kill -9 "${qemu_pid}" 2>/dev/null; rm -rf "${top}"; release_flag; } +trap cleanup EXIT + +# shellcheck source=scripts/lib_ck_m1_checks.sh +source "${repo_root}/scripts/lib_ck_m1_checks.sh" +sha() { sha256sum "$1" | cut -d' ' -f1; } +fail=0; m1_fail=0 + +qemu_args() { # efi-dir image bs + local nvme_dev="nvme,drive=nvme0,serial=aienos-store-crash" + [[ "$3" == 512 ]] || nvme_dev="${nvme_dev},logical_block_size=$3,physical_block_size=$3" + qargs=(-M "virt,virtualization=on,gic-version=3,iommu=smmuv3" -accel tcg,thread=single -cpu max -smp 4 -m 2048 + -drive if=pflash,format=raw,readonly=on,file="${code_fd}" + -drive if=pflash,format=raw,file="${work}/vars.fd" + -drive if=none,id=esp,format=raw,file=fat:rw:"${work}/esp" + -device virtio-blk-pci,drive=esp + -drive if=none,id=nvme0,format=raw,file="$2" + -device "${nvme_dev}" + -device ramfb -display none -nic none + -serial file:"${work}/serial.log" -no-reboot) +} +prep() { # name efi + work="${top}/$1" + mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS" + touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT" + cp "$2" "${work}/esp/EFI/BOOT/BOOTAA64.EFI" + cp "${vars_fd}" "${work}/vars.fd" +} +# boot_full NAME EFI IMAGE BS: a boot that runs to its end (setup/verify). +boot_full() { + prep "$1" "$2"; qemu_args "$1" "$3" "$4" + timeout "${AIENOS_QEMU_TIMEOUT:-180}" qemu-system-aarch64 "${qargs[@]}" + qemu_status=$? + tr -d '\r' <"${work}/serial.log" >"${work}/serial.txt" + [[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${work}/serial.txt" "${AIENOS_LOG_DIR}/qemu_ck_store_crash_$1.log" + failed=0 + ck_m1_checks >"${work}/m1.txt" + if [[ "${failed}" != 0 || "${qemu_status}" != 0 ]]; then + echo "FAIL boot $1: M1 checks or QEMU exit ${qemu_status}"; grep '^FAIL' "${work}/m1.txt" | sed 's/^/ /' + m1_fail=1; return 1 + fi + if [[ "${mode}" == gate ]] && grep -q "store_crash:" "${work}/serial.txt"; then + echo "FAIL boot $1: the default image printed crash hook lines"; m1_fail=1; return 1 + fi + return 0 +} +# boot_crash NAME EFI IMAGE BS: runs until the HALT line, then SIGKILL. +boot_crash() { + prep "$1" "$2"; qemu_args "$1" "$3" "$4" + : >"${work}/serial.log" + qemu-system-aarch64 "${qargs[@]}" & + qemu_pid=$! + local deadline=$(( $(date +%s) + ${AIENOS_QEMU_TIMEOUT:-180} )) + while (( $(date +%s) < deadline )); do + grep -qE '^store_crash: (HALT at |plan REFUSED)' "${work}/serial.log" 2>/dev/null && break + kill -0 "${qemu_pid}" 2>/dev/null || break + sleep 0.1 + done + kill -9 "${qemu_pid}" 2>/dev/null + wait "${qemu_pid}" 2>/dev/null + qemu_pid="" + tr -d '\r' <"${work}/serial.log" >"${work}/serial.txt" + [[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${work}/serial.txt" "${AIENOS_LOG_DIR}/qemu_ck_store_crash_$1.log" +} + +results="${top}/results.txt"; : >"${results}" +closure_ok=1; inject_ok=1; inject_ran=0; control_ok=1 +declare -A geo_ok=([512]=1 [4096]=1) + +# run_case BS SETTLE CP POLICY BASE_IMAGE: one crash + verify; leaves the +# crashed image in ${top}/crashed.img. +run_case() { + local bs="$1" settle="$2" cp="$3" pol="$4" base="$5" name="${1}-s${2}-${3}-${4}" img saw=0 ok=0 why="" + img="${top}/case.img"; cp "${base}" "${img}" + write_plan "${img}" "${cp}" "${pol}" + boot_crash "${name}-crash" "${crash_efi}" "${img}" "${bs}" + if judge_crash "${work}/serial.txt" "${cp}" "${pol}" "${settle}"; then saw=1; else why="crash: ${crash_why}"; fi + grep -q "^kernel: alive" "${work}/serial.txt" && grep -qF "aienos_commit: ${commit}" "${work}/serial.txt" \ + || { saw=0; why="crash boot is not this commit's kernel"; } + clear_plan "${img}" + cp "${img}" "${top}/crashed.img" + local n="${crash_n:-0}" + v_gen="" + if boot_full "${name}-verify" "${plain_efi}" "${img}" "${bs}"; then + if [[ "${saw}" == 1 ]]; then + judge_verify "${work}/serial.txt" "${cp}" "${pol}" "${settle}" "${n}" && ok=1 || why="verify: ${v_why}" + fi + else + why="${why:+${why}; }verify boot failed M1/exit" + fi + printf 'bs=%-4s settle=%s cp=%-26s policy=%-6s saw=%s N=%-2s opened=%-3s expect=%-5s -> %s%s\n' \ + "${bs}" "${settle}" "${cp}" "${pol}" "${saw}" "${n}" "${v_gen:-none}" "$(expect_for "${cp}" "${pol}")" \ + "$([[ "${ok}" == 1 ]] && echo OK || echo BAD)" "${why:+ (${why})}" | tee -a "${results}" + case_saw="${saw}"; case_ok="${ok}"; case_n="${n}" + [[ "${ok}" == 1 ]] || { fail=1; geo_ok[${bs}]=0; } +} + +# setup_image BS SETTLE -> path of an image after SETTLE default-image boots +setup_image() { + local bs="$1" settle="$2" img="${top}/setup-${1}-${2}.img" k + truncate -s "${img_bytes}" "${img}" + for (( k = 1; k <= settle; k++ )); do + if ! boot_full "${bs}-setup${settle}-${k}" "${plain_efi}" "${img}" "${bs}" \ + || ! grep -q "^store: committed generation=$(( k + 1 )) boot_count=${k}$" "${work}/serial.txt"; then + echo "FAIL setup boot ${k} (bs ${bs}) did not commit generation $(( k + 1 ))"; fail=1; geo_ok[${bs}]=0 + fi + done + echo "${img}" +} + +# injected_case BS BASE_SETUP CRASHED N: tear closure + bad-CRC root. +injected_case() { + local bs="$1" before="$2" crashed="$3" n="$4" img off s0 s1 + if tear_closure "${before}" "${crashed}" "${n}"; then + echo "PASS bs=${bs} root tear closure: only slot ${tc_slot} changed, inside its first 512 bytes, generation $(( n + 1 ))" + else + echo "FAIL bs=${bs} root tear closure: ${tc_why}"; closure_ok=0 + return + fi + img="${top}/inject.img"; cp "${crashed}" "${img}" + off=$(( store_off + tc_slot * unit + sb_crc_off )) + printf "\\$(printf '%03o' $(( $(od -An -tu1 -j "${off}" -N 1 "${img}") ^ 0x01 )))" \ + | dd of="${img}" bs=1 seek="${off}" conv=notrunc status=none + cmp -s "${img}" "${crashed}" && { echo "FAIL bs=${bs} CRC byte flip did not change the image"; inject_ok=0; return; } + s0="$(sha "${img}")" + inject_ran=1 + if boot_full "${bs}-inject-bad-crc" "${plain_efi}" "${img}" "${bs}"; then + s1="$(sha "${img}")" + if judge_injected "${work}/serial.txt" "${n}" && [[ "${s1}" == "${s0}" ]]; then + echo "PASS bs=${bs} root with a bad CRC (generation $(( n + 1 ))) not opened, nothing committed, image unchanged" + else + echo "FAIL bs=${bs} bad-CRC root: ${inj_why:-image changed by the boot}"; inject_ok=0 + grep -E '^store: ' "${work}/serial.txt" | sed 's/^/ /' + fi + else + echo "FAIL bs=${bs} bad-CRC root boot failed M1/exit"; inject_ok=0 + fi +} + +hw_ok=1 +if [[ "${mode}" == gate ]]; then + make_refusals || hw_ok=0 + grep -aqF "store_crash:" "${plain_efi}" && { echo "FAIL default image carries crash hook strings"; hw_ok=0; } \ + || echo "PASS default image carries no crash hook strings" + grep -aqF "TEST-ONLY Store crash hook image" "${crash_efi}" && echo "PASS TEST crash image announces itself" \ + || { echo "FAIL TEST crash image lacks its announcement"; hw_ok=0; } +fi + +mutant_hit=0; mutant_cases=0 +for bs in "${gate_geos[@]}"; do + echo "=== ${bs}-byte LBA ===" + for settle in "${gate_settles[@]}"; do + if [[ "${mode}" == mutant && "${mutant}" == accept_bad_root_crc && "${settle}" == 0 ]]; then continue; fi + base="$(setup_image "${bs}" "${settle}")" + plan=() + if [[ "${mode}" == gate ]]; then + for cp in "${cps[@]}"; do plan+=("${cp} all"); done + plan+=("${gate_extra[@]}") + elif [[ "${mutant}" == skip_root_flush ]]; then + plan=("after_inactive_superblock newest" "after_inactive_superblock torn") + else + plan=("after_inactive_superblock all") + fi + for p in "${plan[@]}"; do + read -r cp pol <<<"${p}" + run_case "${bs}" "${settle}" "${cp}" "${pol}" "${base}" + if [[ "${mode}" == mutant ]]; then + mutant_cases=$(( mutant_cases + 1 )) + [[ "${mutant}" == skip_root_flush && "${case_saw}" == 1 && "${case_ok}" == 0 ]] && mutant_hit=1 + # accept_bad_root_crc: the uninjected crash case is the control; it must pass. + [[ "${mutant}" == accept_bad_root_crc && "${case_ok}" != 1 ]] && control_ok=0 + fi + if [[ "${settle}" == 3 && "${cp}" == after_inactive_superblock && "${pol}" == all \ + && ( "${mode}" == gate || "${mutant}" == accept_bad_root_crc ) && "${case_saw}" == 1 ]]; then + injected_case "${bs}" "${base}" "${top}/crashed.img" "${case_n}" + fi + done + done +done +release_flag + +echo "---- crash campaign results ----" +cat "${results}" +if [[ "${fail}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then + for s in "${top}"/*/serial.txt; do + echo "---- serial $(basename "$(dirname "${s}")") (store lines) ----" + grep -E '^(stage store|store:|store_crash:)' "${s}" | head -30 || true + done +fi + +if [[ "${mode}" == mutant ]]; then + if [[ "${mutant}" == skip_root_flush ]]; then + [[ "${mutant_hit}" == 1 ]] && { verdict "KILLED (a crash case after the root write recovered wrong on the Store without its pre-root flush)"; exit 0; } + verdict "SURVIVED (${mutant_cases} cases, none failed)"; exit 1 + fi + if [[ "${control_ok}" != 1 || "${inject_ran}" != 1 || "${closure_ok}" != 1 ]]; then + verdict "SURVIVED (inconclusive: control crash case or tear closure failed, injection ran=${inject_ran})"; exit 1 + fi + [[ "${inject_ok}" == 0 ]] && { verdict "KILLED (the bad-CRC root was opened)"; exit 0; } + verdict "SURVIVED (bad-CRC root still refused)"; exit 1 +fi + +pf() { [[ "$1" == 1 && "${m1_fail}" == 0 ]] && echo PASS || echo FAIL; } +echo +echo "CK_STORE_CRASH_512B_QEMU: $(pf "${geo_ok[512]}")" +echo "CK_STORE_CRASH_4096_QEMU: $(pf "${geo_ok[4096]}")" +echo "CK_STORE_ROOT_TEAR_CLOSURE_QEMU: $(pf "${closure_ok}")" +echo "CK_STORE_INJECTED_ROOT_QEMU: $(pf "$(( inject_ok & inject_ran ))")" +echo "CK_STORE_CRASH_HOOK_REFUSED_ON_HARDWARE: $(pf "${hw_ok}")" +echo "CK_STORE_SLOT_REUSE: NOT_RUN (MISSING_IMPLEMENTATION: the C Store is append-only, no reclaim/slot reuse)" +[[ "${m1_fail}" != 0 ]] && echo "M1 checks or QEMU exit failed on at least one setup/verify boot: no verdict can pass" +# Complete campaign: exactly gate_rows_want rows, all OK (rows_all_ok, as in +# the Rust scripts); an empty, short or over-full table is FAIL. +rows_ok=0 +if rows_all_ok "${results}" "${gate_rows_want}"; then + rows_ok=1; echo "CK_STORE_CRASH_ROWS: PASS (${gate_rows_want} of ${gate_rows_want} rows OK)" +else + echo "CK_STORE_CRASH_ROWS: FAIL (${rows_why})" +fi +if [[ "${rows_ok}" == 1 && "${fail}" == 0 && "${m1_fail}" == 0 && "${closure_ok}" == 1 && "${inject_ok}" == 1 && "${inject_ran}" == 1 \ + && "${hw_ok}" == 1 && "${geo_ok[512]}" == 1 && "${geo_ok[4096]}" == 1 ]]; then + verdict PASS; exit 0 +fi +verdict FAIL +exit 1 ```