From a8b948018e45f8375d34db67ca96d65d88e0fe99 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Fri, 30 Oct 2026 09:54:32 -0500 Subject: [PATCH 1/2] feat(trace): local trace segment v1 with CRC-32 records and torn-tail recovery (Refs #608) seed/trace/segment.om: 16-byte header, 64-byte records (type, length, sequence, 52-byte payload, CRC-32/IEEE), read-only scan, append refused over a torn tail, recovery that keeps the valid prefix and quarantines the damaged file as .bad via stage/link/rename. O_EXCL create (never truncates), O_NOFOLLOW on every open, 0600 files. Reworks the trace segment from Drake's commit 13fb585 (fix/security673-qualified): that version had no CRC, recovery kept 0 records, and segensure recreated a segment with a bad header. seed/trace/gate.sh + tests/gates/omega-trace.gate (run.sh trace): 71 checks against expected.txt and 4 source mutants (crc, torn, align, excl) that must each fail their own check. --- .crumb | 20 +- seed/README.md | 11 + seed/trace/README.md | 68 ++++++ seed/trace/expected.txt | 72 ++++++ seed/trace/gate.sh | 60 +++++ seed/trace/segment-test.om | 333 +++++++++++++++++++++++++++ seed/trace/segment.om | 424 +++++++++++++++++++++++++++++++++++ tests/.crumb | 8 +- tests/gates/omega-trace.gate | 23 ++ tests/gates/run.sh | 8 + 10 files changed, 1013 insertions(+), 14 deletions(-) create mode 100644 seed/trace/README.md create mode 100644 seed/trace/expected.txt create mode 100755 seed/trace/gate.sh create mode 100644 seed/trace/segment-test.om create mode 100644 seed/trace/segment.om create mode 100644 tests/gates/omega-trace.gate diff --git a/.crumb b/.crumb index f3e51fe46..39d7d632d 100644 --- a/.crumb +++ b/.crumb @@ -72,38 +72,38 @@ }, { "digest": "f765ad87fdddd9b47f690b07298c951a0f7e7675dc5de4ffe44e33a0c14b1986", - "last_commit": "a8a6a3d3c", + "last_commit": "62d1d0d66", "name": "spec", "purpose": "Directory spec. Purpose not yet described by a human." }, { "digest": "aaf5acb82fd4c720cd60c85fd6087f636c72a98e2f3740c9295dcfa525dc64d7", - "last_commit": "bf54b19fa", + "last_commit": "089e5b053", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "5395ff8ff9afceebe8e453788f80a3064b831513cc4cb92fd55869115e06b874", - "last_commit": "a8a6a3d3c", + "digest": "543e15250e80c074f91b7b42b15306015f1cfa1eba231c4f11b7bdffa3579918", + "last_commit": "62d1d0d66", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { "digest": "308db0e6cf45c0a48f01f5f5f228450f191389de68006915b9ecf9c65b9a2e29", - "last_commit": "a8a6a3d3c", + "last_commit": "62d1d0d66", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:07bf1706dcffbad66c0353871ff67a6749055da03cee8d8ebdf9a4c91e36bddc", - "compiled_at": "2026-10-30T12:31:50Z", + "children_root": "sha256:017c58a76fedce135a80ec135362c81d24a037f6be660f5dc7205517aa57e097", + "compiled_at": "2026-10-30T14:54:20Z", "compiler": "crumb-compile/1", - "digest": "sha256:904fca8c0e0cf3e3a5e5adb73393e01c7018b920bb542d289904240f70e400d6", + "digest": "sha256:ec105f5395316c53b90fb6f7a7cb58aaf08dd2d1094651aa0788051681352b75", "evidence_files": 12622, "evidence_root": "sha256:b1a08e2ce56e6feada0d8a31f06fbd7adee57adc1c1ad8dec220293ee5d439f9", "files": 17849, - "generated_at_commit": "52a82f392fa513ebbc2d9d719a0f14c385289b06", - "source_tree": "sha256:75458da6553a0ee5ea2af95f46afb658e8a3f6e53cca6e3b4b9874035f78c2ed" + "generated_at_commit": "62d1d0d668f897386a0e311374f8dbcc5c32dde8", + "source_tree": "sha256:11a68b9a3f8fc4cda04fc1107e95108517b4758bf72fe5fd5663452343b25023" }, "owner_lane": "", "provenance": { diff --git a/seed/README.md b/seed/README.md index b37d60984..51215b6b9 100644 --- a/seed/README.md +++ b/seed/README.md @@ -984,6 +984,17 @@ or unrelated runtime failures are not accepted as control evidence. These checks qualify host parsing only. They do not access or qualify a GPU, alter the empirical scheduling model, or rewrite historical evidence. +## Local trace segment (omega-mv #608) + +`seed/trace/segment.om` is a library unit (no `main`) for a local, append-only trace segment: a +16-byte header and 64-byte records, each with its sequence number and a CRC-32. It can scan a segment +read-only, and its recovery keeps the valid prefix and quarantines the damaged file as `.bad`. +Run `sh seed/trace/gate.sh` (or `tests/gates/run.sh trace`) from the repository root on an +AArch64 Linux host. The gate checks `seed/out/omc` against its recorded SHA-256, then requires the 71 checks +in `seed/trace/segment-test.om` to print exactly `seed/trace/expected.txt`. It also requires four source +mutants to fail their own checks: CRC comparison removed, torn tail ignored, append alignment guard +weakened, and create with O_TRUNC in place of O_EXCL. `seed/trace/README.md` describes the format. + ## How the checks stay honest - `check/a64check.c` and `check/audit.sh` are reference checks only. They call the repo's existing C diff --git a/seed/trace/README.md b/seed/trace/README.md new file mode 100644 index 000000000..db3fa0756 --- /dev/null +++ b/seed/trace/README.md @@ -0,0 +1,68 @@ +# seed/trace: local trace segment (omega-mv #608) + +`segment.om` is a library unit in seeded Omega. It has no `main`, so concatenate it in front of a program +that defines one (units concatenate; Omega has no import). Host words: `fopen`, `read`, `write`, +`fseek`, `fsync`, `fclose`, `unlink`, `link`, `renames` (OMEGESE section 10). Linux AArch64 only. + +## Format version 1 + +All integers are little-endian. + +| Part | Bytes | Fields | +|------|-------|--------| +| header | 16 | `AITR` (65 73 84 82), version u16 = 1, record size u16 = 64, header size u32 = 16, reserved u32 = 0 | +| record | 64 | type u16, payload length u16 (at most 52), sequence u32, payload 52 bytes (zero past the length), CRC-32 u32 | + +- The sequence is the record index, assigned by the writer. A scan refuses a record whose sequence is + not its position, so a duplicated or reordered record is detected. +- The CRC is CRC-32/IEEE (reflected polynomial 0xEDB88320 = 3988292384, initial value and final xor + 0xFFFFFFFF) over record bytes 0..59. Its check value `CRC-32("123456789") = 3421780262` is the first + test (Wolfram: `Hash["123456789","CRC32"] -> 3421780262`). +- A segment holds at most 4096 records, so the largest segment is `16 + 64*4096 = 262160` bytes. + +## Operations + +| Function | Behaviour | +|----------|-----------| +| `segcreate` | creates `tpath` with O_WRONLY, O_CREAT, O_EXCL and O_NOFOLLOW, mode 0600, and writes and fsyncs the header. An existing path is refused (10); it is never truncated | +| `segready` | creates a missing segment. For an existing one it requires a valid header, and it never recreates the file | +| `segappend t n` | appends one record with payload `pay[0..n)`. It refuses if the file length is not header plus whole records (25: torn tail, recover first), when the segment is full (26), or when the payload is over 52 bytes (20). Each record is fsynced | +| `segscan` | read-only. Sets `scount` (valid records before the first damage), `sreason` (0 clean, 1 torn tail, 2 bad length, 3 CRC mismatch, 4 sequence mismatch, 5 over capacity), `stotal` (file bytes) and `slost` (bytes after the valid prefix). A header that is short, has bad magic (11), an unsupported version (15), or bad geometry or nonzero reserved bytes (16) is refused | +| `segrecover` | returns 0 for a clean segment. Otherwise it writes the header and valid prefix to `.new` (O_EXCL, fsync), hard-links the damaged file to `.bad` (refused with 32 if that exists, so a quarantine is never overwritten), renames the new file over the segment and returns 1. Header errors are returned, not recovered | + +Every open uses O_NOFOLLOW, so a symbolic link as the segment path is refused (ELOOP, `serrno = -40`). +The caller creates directories with mode 0700 (`makedir ... 448`); the test does so. + +## Gate + +`sh seed/trace/gate.sh`, or `tests/gates/run.sh trace` (manifest `tests/gates/omega-trace.gate`). It +checks `seed/out/omc` against `seed/out/omc.sha256`, compiles `segment.om` plus `segment-test.om`, and +runs the test in `build/seed/trace/main`. The test must exit 0 and its stdout must equal `expected.txt`. +The 71 checks cover: + +- appending 100 records; +- truncating the last record to 30 of its 64 bytes; the scan must then keep 99 records and report a + torn tail; +- append refused until recovery; recovery keeps 99 records and quarantines the 6382-byte file; the next + append gets sequence 99; +- a flipped payload byte in record 50; the scan must stop at 50 records with a CRC mismatch, and + recovery is refused while `.bad` exists; +- crafted records with payload length 60 and with sequence 7; +- header version, magic, geometry and reserved-byte refusals; +- a symbolic-link path refused by scan, append and recover. + +Four source mutants must each compile, exit 1 and fail their own check: `crc` (CRC no longer compared, +so `crc-reason` fails), `torn` (`torn-reason`), `align` (`torn-append-refused`) and `excl` (create with +O_TRUNC, so `create-again-refused`). + +The expected sizes and offsets were computed in Wolfram Language: +`{16+64*100, 16+64*99+30, 16+64*99, 16+64*50+8, (16+64*100)-16-64*50, 16+64*50, Mod[6382-16,64]} +-> {6416, 6382, 6352, 3224, 3200, 3216, 30}`. The flags were checked the same way: +`{BitOr[1,1024,32768], BitOr[1,32768], BitOr[1,64,128,32768], BitOr[1,64,512,32768]} -> {33793, 32769, +32961, 33345}`. The remaining values (errno 2, 17 and 40, and the result codes) are pinned by the source. + +## Not in this unit + +Issue #608 also lists items this unit does not provide: an index, rotation and retention, an async +writer, schema migration beyond refusing other versions, export, consent and redaction. Concurrent +writers are not supported either: there is no lock, and the unit assumes a single writer. diff --git a/seed/trace/expected.txt b/seed/trace/expected.txt new file mode 100644 index 000000000..9e0b44220 --- /dev/null +++ b/seed/trace/expected.txt @@ -0,0 +1,72 @@ +crc-123456789 3421780262 +crc-empty 0 +missing-scan 10 +missing-errno 2 +create 0 +create-again-refused 10 +create-again-errno 17 +append-100-errors 0 +append-last-seq 99 +scan 0 +scan-count 100 +scan-reason 0 +scan-total 6416 +scan-lost 0 +append-oversize-refused 20 +truncate-last-record 0 +torn-scan 0 +torn-count 99 +torn-reason 1 +torn-lost 30 +torn-append-refused 25 +torn-size-unchanged 6382 +torn-recover 1 +torn-recover-lost 30 +recovered-scan 0 +recovered-count 99 +recovered-reason 0 +recovered-total 6352 +quarantine-size 6382 +append-after-recover 0 +append-after-seq 99 +recover-clean 0 +clean-count 100 +crc-flip-poke 0 +crc-scan 0 +crc-count 50 +crc-reason 3 +crc-lost 3200 +crc-recover-bad-exists 32 +crc-segment-unchanged 6416 +unlink-bad 0 +crc-recover 1 +crc-recovered-scan 0 +crc-recovered-count 50 +crc-recovered-reason 0 +len-raw-append 0 +len-scan 0 +len-count 50 +len-reason 2 +len-recover 1 +seq-raw-append 0 +seq-scan 0 +seq-count 50 +seq-reason 4 +seq-recover 1 +version-poke 0 +version-scan 15 +version-append-refused 15 +version-size-unchanged 3216 +magic-scan 11 +geometry-scan 16 +reserved-scan 16 +restored-scan 0 +restored-count 50 +symlink-create 0 +symlink-scan 10 +symlink-errno 40 +symlink-append-refused 10 +symlink-recover-refused 10 +final-scan 0 +final-count 50 +segment-test checks=71 fails=0 diff --git a/seed/trace/gate.sh b/seed/trace/gate.sh new file mode 100755 index 000000000..ac6ee4b33 --- /dev/null +++ b/seed/trace/gate.sh @@ -0,0 +1,60 @@ +#!/bin/sh +# seed/trace/gate.sh -- host gate for the local trace segment (omega-mv #608). +# Run from anywhere: sh seed/trace/gate.sh +# Compiles segment.om + segment-test.om with seed/out/omc (sha checked against seed/out/omc.sha256), runs +# the test in a fresh scratch directory and requires exit 0 and stdout equal to expected.txt byte for byte. +# The expected values (sizes, offsets, CRC check value, flags) were computed in Wolfram Language; see +# README.md. Then each source mutant (a literal swap of a line that must occur exactly once in segment.om) +# must compile, exit 1, and fail on its own check label: +# crc record CRC no longer compared -> crc-reason FAIL +# torn partial tail not reported as torn -> torn-reason FAIL +# align append ignores a misaligned tail -> torn-append-refused FAIL +# excl create opens with O_TRUNC, not O_EXCL -> create-again-refused FAIL +# Prints PASS trace and exits 0, or FAIL and exits 1. +set -eu +R=$(CDPATH= cd -- "$(dirname "$0")/../.." && pwd) +cd "$R" +D=seed/trace +B=build/seed/trace +rm -rf "$B" +mkdir -p "$B" +bad() { printf 'FAIL trace: %s\n' "$*" >&2; exit 1; } + +want=$(cut -c1-64 seed/out/omc.sha256) +have=$(sha256sum < seed/out/omc | cut -c1-64) +[ "$want" = "$have" ] || bad "seed/out/omc is $have, omc.sha256 says $want" +OMC="$R/seed/out/omc" + +# run NAME SRC: compile SRC to $B/NAME/t, run it inside $B/NAME, stdout to $B/NAME/out.txt, sets rc +run() { + mkdir -p "$B/$1" + "$OMC" < "$2" > "$B/$1/t" || bad "omc refused $2" + chmod 700 "$B/$1/t" + rc=0 + (cd "$B/$1" && ./t > out.txt) || rc=$? +} + +cat "$D/segment.om" "$D/segment-test.om" > "$B/test.om" +run main "$B/test.om" +printf 'omc %s\nsegment-test %s\n' "$have" "$(sha256sum < "$B/main/t" | cut -c1-64)" +[ "$rc" -eq 0 ] || { cat "$B/main/out.txt" >&2; bad "segment-test exited $rc"; } +cmp -s "$B/main/out.txt" "$D/expected.txt" || { diff "$D/expected.txt" "$B/main/out.txt" >&2 || true; bad "output differs from expected.txt"; } + +n=0 +mutant() { # NAME SITE REPLACEMENT LABEL + c=$(grep -c -x -F "$2" "$D/segment.om" || true) + [ "$c" = 1 ] || bad "mutant $1: site found $c times" + awk -v s="$2" -v r="$3" '$0 == s { print r; next } { print }' "$D/segment.om" > "$B/$1.om" + cmp -s "$B/$1.om" "$D/segment.om" && bad "mutant $1: source unchanged" + cat "$B/$1.om" "$D/segment-test.om" > "$B/$1-test.om" + run "$1" "$B/$1-test.om" + [ "$rc" -eq 1 ] || bad "mutant $1 exited $rc, need 1" + grep -q "^$4 [0-9]* FAIL want=" "$B/$1/out.txt" || bad "mutant $1 did not fail $4" + echo "mutant $1: exit 1, $4 FAIL" + n=$((n + 1)) +} +mutant crc 'if ne want segcrc 60' 'if ne want want' crc-reason +mutant torn 'put sreason 0 1' 'put sreason 0 0' torn-reason +mutant align 'if ne rem r 64 0' 'if ne rem r 1 0' torn-append-refused +mutant excl 'fd = fopen tpath 0 get tlen 0 32961 384' 'fd = fopen tpath 0 get tlen 0 33345 384' create-again-refused +printf 'PASS trace %s checks, %s/4 mutants caught\n' "$(sed -n 's/^segment-test checks=\([0-9]*\) fails=0$/\1/p' "$B/main/out.txt")" "$n" diff --git a/seed/trace/segment-test.om b/seed/trace/segment-test.om new file mode 100644 index 000000000..5f291b2f7 --- /dev/null +++ b/seed/trace/segment-test.om @@ -0,0 +1,333 @@ +note SPDX-License-Identifier: AGPL-3.0-only +note seed/trace/segment-test.om: checks for segment.om (#608). Concatenate after segment.om and compile. +note Runs in the current directory under build/trace-test/. Each check prints "