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/omega-mv/patches/17ba1854eb3b1aa4 # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers ## osh: cross-adapter pipeline differential tests, Omega host now, AIENOS column reserved (#568) closed `17ba1854eb3b1aa4` #572 · opened 2026-10-19 13:06 UTC · closed 2026-10-29 19:02 UTC Imported from GitHub aien-dev/omega-mv#572, by drakestapleton Base `a8ffd2b912c84197d08f963b05f9c7d8b30a3f8a` · Head `41c83a71a49828c369ba82624aea43eabed11a66` drakestapleton wrote on 2026-10-19 13:06 UTC Cross-adapter pipeline differential tests for the Omega shell (Omega Linux host now, AIENOS native column reserved). Closes nothing yet: the AIENOS acceptance part of #568 waits for aienos-mv#429/#430. ## What is in it - `src/osh/host/osh_shell.{c,h}`: a TEST-ONLY read-only observer (`OshDiffHooks`) of each request record, its host result and each core refusal. Compiled only with `-DOSH_DIFF_TEST_HOOKS`; `make test-osh-diff` checks with `nm` that the shipped `osh` does not contain it. - `tests/osh/diff/`: 33 fixtures (28 portable, 5 Linux-only) written from the OSH platform ABI draft, the toy stage program, the runner, 21 mutant red/green proofs, and a README with the fixture format and the result-file format AIENOS must print. - `mk/osh.mk`: `make test-osh-diff` (plain + ASan/UBSan + mutants + observer-absence check). `OSH_DIFF_NATIVE=FILE` feeds an AIENOS result file; `OSH_DIFF_REQUIRE_NATIVE=1` turns a missing native result into a failure. - Crumbs recompiled with the pinned tool (crumb.lock `d0f0b95d`), last commit. Every scenario runs on the compiled shell core and on the interpreter, and the two must agree byte for byte (request records, output, exit status, stage endings, no leaked descriptors or processes). The AIENOS column says NOT_RUN, never PASS, until AIENOS can run multi-stage pipelines. ## Verification actually run - Agent (handoff `~/handoffs/2026-10-18-omega-568.md` on the Spark): `make test-osh-diff` PASS (host 66/66 plain, 64 + 2 required NOT_RUN under ASan for the RLIMIT_AS case), mutants 21/21, 40 plain + 6 ASan repeat runs all PASS; existing osh suites (host, caps, journal, e2e, lex, parse, expand, trace) all PASS. gcc 13.3.0, GNU Make 4.3, bash 5.2.21. - Desk rerun on the Spark at `f362edd9` (own OUT_DIR): `make test-osh-diff` rc=0, `mutants: PASS (21 checks)`, `test-osh-diff: PASS`. - `crumb verify .` OK (157 crumbs current). - QEMU: not run (no native AIENOS multi-stage adapter exists yet). ## Not done here - No Spark gate runs the osh suites yet (tests/gates has no OSH step). Registering `test-osh-diff` (and the other osh suites) as a gate is a pipeline-configuration change and goes in its own PR. - AIENOS native fixture matching (2-stage and 3-stage) is blocked on aienos-mv#429/#430. ## UNVERIFIED (the ABI draft does not pin these) - Wire form (7.3): OUT serialized as `out_used` single bytes after the cells. - Request line grammar: ` direct` after `conn=N` when flags bit 0 is set. - Stage categories: SIGPIPE -> `broken_pipe`, SIGINT -> `interrupted`, SIGXFSZ -> `limit`. - A failed input redirection is `exit:1` for that stage on every adapter. - Result-file format, `core` digest recipe and pin keys are defined here first; the AIENOS side has not reviewed them. - Toy operations as Omega-native ops on AIENOS is a proposal for aienos-mv#430. Refs #568 This PR was opened by the Hermes PR desk on Drake's behalf. github-advanced-security[bot] commented on `tests/osh/diff/osh_diff.c` line 448 on 2026-10-19 13:12 UTC ## CodeQL / Potentially overflowing call to snprintf The size argument of this snprintf call is derived from its return value, which may exceed the size of the buffer and overflow. Show more details github-advanced-security[bot] commented on `tests/osh/diff/osh_diff.c` line 449 on 2026-10-19 13:12 UTC ## CodeQL / Potentially overflowing call to snprintf The size argument of this snprintf call is derived from its return value, which may exceed the size of the buffer and overflow. Show more details github-advanced-security[bot] commented on `tests/osh/diff/osh_diff.c` line 459 on 2026-10-19 13:12 UTC ## CodeQL / Potentially overflowing call to snprintf The size argument of this snprintf call is derived from its return value, which may exceed the size of the buffer and overflow. Show more details github-advanced-security[bot] commented on `tests/osh/diff/osh_diff.c` line 777 on 2026-10-19 13:12 UTC ## CodeQL / Potentially overflowing call to snprintf The size argument of this snprintf call is derived from its return value, which may exceed the size of the buffer and overflow. Show more details github-advanced-security[bot] commented on `tests/osh/diff/osh_diff.c` line 778 on 2026-10-19 13:12 UTC ## CodeQL / Potentially overflowing call to snprintf The size argument of this snprintf call is derived from its return value, which may exceed the size of the buffer and overflow. Show more details github-advanced-security[bot] commented on `tests/osh/diff/osh_diff.c` line 793 on 2026-10-19 13:12 UTC ## CodeQL / Potentially overflowing call to snprintf The size argument of this snprintf call is derived from its return value, which may exceed the size of the buffer and overflow. Show more details github-advanced-security[bot] commented on `tests/osh/diff/osh_diff.c` line 811 on 2026-10-19 13:12 UTC ## CodeQL / Potentially overflowing call to snprintf The size argument of this snprintf call is derived from its return value, which may exceed the size of the buffer and overflow. Show more details github-advanced-security[bot] reviewed (COMMENTED) on 2026-10-19 13:12 UTC github-advanced-security[bot] commented on `tests/osh/diff/osh_diff.c` line 816 on 2026-10-19 13:12 UTC ## CodeQL / Potentially overflowing call to snprintf The size argument of this snprintf call is derived from its return value, which may exceed the size of the buffer and overflow. Show more details github-advanced-security[bot] commented on `tests/osh/diff/osh_diff.c` line 819 on 2026-10-19 13:12 UTC ## CodeQL / Potentially overflowing call to snprintf The size argument of this snprintf call is derived from its return value, which may exceed the size of the buffer and overflow. Show more details github-advanced-security[bot] commented on `tests/osh/diff/osh_diff.c` line 1086 on 2026-10-19 13:12 UTC ## CodeQL / File created without restricting permissions A file may be created here with mode 0666, which would make it world-writable. Show more details drakestapleton wrote on 2026-10-29 19:02 UTC Closing in org cleanup (Drake, 2026-10-29): abandoned: no activity in 10 days with merge conflicts and failing CI checks. Reopen if still needed. ### Apply it ``` git checkout -b patch-17ba1854eb3b1aa4 a8ffd2b912c84197d08f963b05f9c7d8b30a3f8a curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/17ba1854eb3b1aa4.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/omega-mv 572 41c83a71a49828c369ba82624aea43eabed11a66 ``` ``` {"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":572,"url":"https://github.com/aien-dev/omega-mv/pull/572","export_sha256":"7319147b350ad908bf0b731068ec3be7b8dee848041dc862f71455e8e4680140","imported_utc":"2026-11-18T16:35:54Z"} ``` ### Changes ``` diff --git a/.crumb b/.crumb index d121d2dad..7a07e566d 100644 --- a/.crumb +++ b/.crumb @@ -59,8 +59,8 @@ "purpose": "Directory doctrine. Purpose not yet described by a human." }, { - "digest": "e7d38c87a51c3c0ee6e3b18419ef4a01d95029de12ac9bf69eb672654f0ace55", - "last_commit": "ae4ad9c0", + "digest": "6048786776018b601d8cad5f6b303396725ba6f83e159c2f111c62810bec9c7b", + "last_commit": "f362edd9", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, @@ -77,14 +77,14 @@ "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "dfa36de2ac8649f66f80587abea51a7f166285ee0b92e0e09774c98689055250", - "last_commit": "333091a6", + "digest": "cdde4ae127ee80e56d345d44f3a770260a05c05028cc6968ec117ec53f51a4bf", + "last_commit": "ee138918", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "4eb9d4d76234448c0ed691f0aeb9c90d785f8a47b1b1291d728bbe3e1509dc10", - "last_commit": "88e24bdd", + "digest": "3adc4863795df2eb053dafe86b6200ada71b22d23ce5d1b7a6ad578daa8b322f", + "last_commit": "9d02dff5", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, @@ -95,15 +95,15 @@ "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:e1a8c008214ad5dbc33a3e184d7e882dc56d1d50f9bd14c58f8773992961557e", - "compiled_at": "2026-10-18T21:10:38Z", + "children_root": "sha256:2f3c786e815f871220b918e31c47584416ce313e8aa11fc24e1a45af4a3d43d8", + "compiled_at": "2026-10-19T21:47:48Z", "compiler": "crumb-compile/1", - "digest": "sha256:be7220db494238bb4c33023d1df17286c35f947336bee5fc2e0405c136ec3e73", + "digest": "sha256:29503951e2583772e59985d6938a8cf747a9c4b04f09a199718d9f4983e74b92", "evidence_files": 11127, "evidence_root": "sha256:4528085ec1661c442b64fb5dc8def65067ea2620ff88e42bcf52d58b7642ea6e", - "files": 14100, - "generated_at_commit": "f4a9849fb127f8d2d664dfc56890752ad180c507", - "source_tree": "sha256:21f93defce6c9ba159a4bd31083db787e156dfaa4afb80c3427cf88f732f8258" + "files": 14106, + "generated_at_commit": "9d02dff53e084f0426e246002c4839114111812c", + "source_tree": "sha256:f23e8d0bbd02edc5b120abca243cb9ce9fb3817a0f57d963b96f666556967bfb" }, "owner_lane": "", "provenance": { diff --git a/mk/.crumb b/mk/.crumb index cd51a1c73..77d72ff0c 100644 --- a/mk/.crumb +++ b/mk/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-16T02:24:36Z", + "compiled_at": "2026-10-19T13:05:43Z", "compiler": "crumb-compile/1", - "digest": "sha256:e7d38c87a51c3c0ee6e3b18419ef4a01d95029de12ac9bf69eb672654f0ace55", + "digest": "sha256:6048786776018b601d8cad5f6b303396725ba6f83e159c2f111c62810bec9c7b", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 71, - "generated_at_commit": "ccf021b944b0128a537dfd3dd301e6d83eefa311", - "source_tree": "sha256:e7a2b37daa8149cb73da719737df82c74a61685b4076bf2c5b4e0c26bee61dc7" + "generated_at_commit": "f362edd931b50de8d10e9880eb15c0d29680b869", + "source_tree": "sha256:3d1d5654e3c1edb802ae459dabc0000c6cb13a1ddf66b4260f29b51ae75d4362" }, "owner_lane": "", "provenance": { diff --git a/mk/osh.mk b/mk/osh.mk index ab6f6697e..1a3619c39 100644 --- a/mk/osh.mk +++ b/mk/osh.mk @@ -7,9 +7,10 @@ # make test-osh-lex check + lexer driver, plain and ASan/UBSan (exact-size buffers); OSH_FUZZ=N (default 100000), OSH_FUZZ_SEED=S # make osh-bash-verify prove tests/osh/vectors/bash_vectors.tsv against real bash 5.2 (needs bash, not part of test-osh-lex) # make test-osh-parse check + parser driver (lexer + parser units, reference tokenizer + parser), plain and ASan/UBSan +# make test-osh-diff cross-adapter pipeline differential (host native + interpreter; aienos NOT_RUN without OSH_DIFF_NATIVE) ifndef OSH_MK OSH_MK := 1 -.PHONY: osh-gen osh-gen-check test-osh-lex test-osh-parse test-osh-expand osh test-osh-e2e osh-bash-verify +.PHONY: osh-gen osh-gen-check test-osh-lex test-osh-parse test-osh-expand osh test-osh-e2e osh-bash-verify test-osh-diff OSH_DIR = $(OUT_DIR)/osh OSH_FUZZ ?= 100000 OSH_GEN = src/osh/osh_gen.sh @@ -152,4 +153,42 @@ osh-trace: osh-gen-check $(OSH_DIR)/osh_trace { tail -n 1 $(OSH_DIR)/$$b.native | grep -q '^event exit=' && grep -v '^event exit=' $(OSH_DIR)/$$b.native | grep -q .; } || { echo "osh-trace: empty or cut-off trace for $$f"; exit 1; }; \ cmp $(OSH_DIR)/$$b.native tests/osh/trace/$$b.trace || { echo "osh-trace: $$f differs from the committed trace"; exit 1; }; \ n=$$((n+1)); done; echo "osh-trace: PASS ($$n scripts, native == interpreter == committed trace)" + +# Cross-adapter pipeline differential (omega#568): tests/osh/diff/fixtures.txt through the real compiled shell core on the +# Linux host adapter, native and interpreted, against the expectations, with injected faults and resource limits; plus the +# aienos column, which is NOT_RUN unless a native result file is given (OSH_DIFF_NATIVE=FILE; a raw QEMU serial log is +# fine). Plain and ASan/UBSan, the comparator mutants (tests/osh/diff/mutants.sh), and a check that the TEST-ONLY observer +# is absent from the shipped osh. Format and toy operations: tests/osh/diff/README.md. +# make test-osh-diff [OSH_DIFF_NATIVE=FILE] [OSH_DIFF_REQUIRE_NATIVE=1] +.PHONY: test-osh-diff +OSH_DIFF_SRCS = tests/osh/diff/osh_diff.c $(OSH_DIR)/osh_units.c $(filter-out src/osh/host/osh_main.c,$(OSH_PROG_HOSTSRCS)) $(OSC_LIB) src/runtime/rx_caproot.c +OSH_DIFF_HDRS = $(OSH_XHDRS) src/osh/host/osh_shell.h src/osh/host/osh_priv.h +OSH_DIFF_FLAGS = $(OSH_XFLAGS) -DOSH_DIFF_TEST_HOOKS +# identity pins (README "Pins"), recursive so they are only computed when the recipe runs +OSH_DIFF_PIN_OMEGA = $(shell git rev-parse HEAD 2>/dev/null || echo unknown)$(shell test -n "$$(git status --porcelain -- src tests mk 2>/dev/null)" && echo -dirty) +OSH_DIFF_PIN_CC = $(notdir $(CC))-$(shell $(CC) -dumpfullversion 2>/dev/null || $(CC) -dumpversion)@$(shell sha256sum "$$(command -v $(CC))" | cut -c1-16) +OSH_DIFF_PIN_HARNESS = $(shell cat tests/osh/diff/osh_diff.c tests/osh/diff/osh_toy.c tests/osh/diff/mutants.sh | sha256sum | cut -c1-64) +OSH_DIFF_PIN_AIENOS = $(shell head -1 aienos.lock 2>/dev/null || echo unknown) +OSH_DIFF_PINS = --pin omega=$(OSH_DIFF_PIN_OMEGA) --pin cc=$(OSH_DIFF_PIN_CC) --pin harness=$(OSH_DIFF_PIN_HARNESS) --pin aienos=$(OSH_DIFF_PIN_AIENOS) +OSH_DIFF_NATIVE_ARG = $(if $(OSH_DIFF_NATIVE),--native $(abspath $(OSH_DIFF_NATIVE))) $(if $(OSH_DIFF_REQUIRE_NATIVE),--require-native) + +$(OSH_DIR)/osh_diff: $(OSH_DIFF_SRCS) $(OSH_DIFF_HDRS) + $(CC) $(OSH_DIFF_FLAGS) -O2 -o $@ $(OSH_DIFF_SRCS) + +$(OSH_DIR)/osh_diff_asan: $(OSH_DIFF_SRCS) $(OSH_DIFF_HDRS) + $(CC) $(OSH_DIFF_FLAGS) $(OSC_ASAN) -o $@ $(OSH_DIFF_SRCS) + +$(OSH_DIR)/osh_toy: tests/osh/diff/osh_toy.c + @mkdir -p $(OSH_DIR) + $(CC) -std=gnu11 -Wall -Wextra -Werror -O2 -o $@ $< + +# The ASan/UBSan run must PASS with exactly two host NOT_RUN: fixture f33 (RLIMIT_AS cannot bind an ASan process), both runs. +test-osh-diff: osh-gen-check $(OSH_DIR)/osh $(OSH_DIR)/osh_diff $(OSH_DIR)/osh_diff_asan $(OSH_DIR)/osh_toy + @if nm $(OSH_DIR)/osh | grep -q osh_diff_test_hooks_compiled; then echo "FAIL: shipped osh contains the TEST-ONLY diff observer"; exit 1; fi + @nm $(OSH_DIR)/osh_diff | grep -q osh_diff_test_hooks_compiled || { echo "FAIL: the diff runner lacks the observer"; exit 1; } + @echo "diff observer: absent from the shipped osh, present in the runner" + $(abspath $(OSH_DIR)/osh_diff) --strict-host $(OSH_DIFF_PINS) --emit $(abspath $(OSH_DIR))/diff-host.result $(OSH_DIFF_NATIVE_ARG) $(abspath $(OSH_DIR)/osh_toy) tests/osh/diff/fixtures.txt > $(OSH_DIR)/diff.out; rc=$$?; cat $(OSH_DIR)/diff.out; test $$rc -eq 0 && tail -1 $(OSH_DIR)/diff.out | grep -q '^OSH_DIFF_PASS$$' + $(abspath $(OSH_DIR)/osh_diff_asan) $(OSH_DIFF_PINS) $(OSH_DIFF_NATIVE_ARG) $(abspath $(OSH_DIR)/osh_toy) tests/osh/diff/fixtures.txt > $(OSH_DIR)/diff_asan.out; rc=$$?; tail -3 $(OSH_DIR)/diff_asan.out; test $$rc -eq 0 && tail -1 $(OSH_DIR)/diff_asan.out | grep -q '^OSH_DIFF_PASS$$' && grep -q '^summary: host PASS [0-9]* FAIL 0 NOT_RUN 2 ' $(OSH_DIR)/diff_asan.out && grep -q '^f33_bounded_memory .* NOT_RUN .*RLIMIT_AS cannot apply under ASan' $(OSH_DIR)/diff_asan.out + sh tests/osh/diff/mutants.sh $(abspath $(OSH_DIR)/osh_diff) $(abspath $(OSH_DIR)/osh_toy) tests/osh/diff/fixtures.txt $(abspath $(OSH_DIR))/diff-host.result $(abspath $(OSH_DIR))/diff-mutants + @echo "test-osh-diff: PASS (host adapter: native + interpreter units, plain and ASan/UBSan, faults and limits, comparator mutants; aienos column $(if $(OSH_DIFF_NATIVE),compared against $(OSH_DIFF_NATIVE),NOT_RUN: no native result supplied))" endif diff --git a/src/.crumb b/src/.crumb index 4fe5298a4..1eafa8b21 100644 --- a/src/.crumb +++ b/src/.crumb @@ -115,8 +115,8 @@ "purpose": "Directory src/language. Purpose not yet described by a human." }, { - "digest": "651cad69999ba000a9be247d7decaf02ef9c4653f718255b62efb248139ad74b", - "last_commit": "6d8d92ad", + "digest": "14f692811876ccb68592174eefad221bcad32234e43b2a8b8911c92e015c8405", + "last_commit": "ee138918", "name": "osh", "purpose": "osh (Omega-native shell, arch#158) lexer unit: layout list, generator, osh_lex.osc, C reference " }, @@ -181,15 +181,15 @@ "purpose": "Directory src/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:15012a017b26a269feb256b64875ece2912f5f82c57804baafa3777c4fcb80b9", - "compiled_at": "2026-10-16T07:33:43Z", + "children_root": "sha256:d1175fa27ad8c00073f8ccf2a96556090191c8d950bc9c9a0cab055660ae66ae", + "compiled_at": "2026-10-19T13:05:43Z", "compiler": "crumb-compile/1", - "digest": "sha256:dfa36de2ac8649f66f80587abea51a7f166285ee0b92e0e09774c98689055250", + "digest": "sha256:cdde4ae127ee80e56d345d44f3a770260a05c05028cc6968ec117ec53f51a4bf", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 537, - "generated_at_commit": "1d481ab853a14d3c11eff926ce1e25993085f76a", - "source_tree": "sha256:200c9243151691313acb5f86d3e9cb3058f1a1b92ece2a255b3340b691dfb894" + "generated_at_commit": "f362edd931b50de8d10e9880eb15c0d29680b869", + "source_tree": "sha256:2c4bf6855012183aaf836a0480e9c690ca5a10b0dd1bf9d23d1cbd54fc5e22d8" }, "owner_lane": "", "provenance": { diff --git a/src/osh/.crumb b/src/osh/.crumb index 8e8507e46..c3a9feae9 100644 --- a/src/osh/.crumb +++ b/src/osh/.crumb @@ -3,21 +3,21 @@ "generated": { "children": [ { - "digest": "6707743fc8ca99c32f1d008c4ef5404f45cf2085a19fb1a049baad88f7034cbf", - "last_commit": "181e2ea8", + "digest": "71be9f2680ae23c32b18689ca881d5d4b377f14fd307dffb4cc05d0eb8cb27fe", + "last_commit": "ee138918", "name": "host", "purpose": "osh Linux host: execution service (request decoder, resolution, builtins, redirections, pipeline" } ], - "children_root": "sha256:eaf775d2389d930370aefb38da468501e0b107dd4524142613c8e7fe4f7b7e80", - "compiled_at": "2026-10-10T08:29:30Z", + "children_root": "sha256:576a39969b9baa6c1a10fd959933224e825fa23c49cc634b57f5fb73049cbd5d", + "compiled_at": "2026-10-19T13:05:43Z", "compiler": "crumb-compile/1", - "digest": "sha256:651cad69999ba000a9be247d7decaf02ef9c4653f718255b62efb248139ad74b", + "digest": "sha256:14f692811876ccb68592174eefad221bcad32234e43b2a8b8911c92e015c8405", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 31, - "generated_at_commit": "181e2ea8961e1067efc0337b6b4dddc9f19d6947", - "source_tree": "sha256:ec53b8c244ce7ebaf35ef0ca78b5d55c25ce85d1788bcf7d570f345be6a2e8c9" + "generated_at_commit": "f362edd931b50de8d10e9880eb15c0d29680b869", + "source_tree": "sha256:2e6b627577a9fc81f3ce435b0442fb0f4dc11a706cf217e8b4966b36b11079af" } }, "name": "osh", diff --git a/src/osh/host/.crumb b/src/osh/host/.crumb index 7952e3461..9887dd171 100644 --- a/src/osh/host/.crumb +++ b/src/osh/host/.crumb @@ -3,14 +3,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-10T08:29:30Z", + "compiled_at": "2026-10-19T13:05:43Z", "compiler": "crumb-compile/1", - "digest": "sha256:6707743fc8ca99c32f1d008c4ef5404f45cf2085a19fb1a049baad88f7034cbf", + "digest": "sha256:71be9f2680ae23c32b18689ca881d5d4b377f14fd307dffb4cc05d0eb8cb27fe", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 16, - "generated_at_commit": "181e2ea8961e1067efc0337b6b4dddc9f19d6947", - "source_tree": "sha256:aa00f87d84ca18f16da789f61a3dc89be53d701400f0154583e465ce25a113de" + "generated_at_commit": "f362edd931b50de8d10e9880eb15c0d29680b869", + "source_tree": "sha256:5d2a2ece1c0ac61fc9666d3331e8917fc15be52590d8c6dd1dd03a1851269340" } }, "name": "host", diff --git a/src/osh/host/osh_shell.c b/src/osh/host/osh_shell.c index 49a67c953..335baafe0 100644 --- a/src/osh/host/osh_shell.c +++ b/src/osh/host/osh_shell.c @@ -223,6 +223,9 @@ static void refusal(OshShell *sh, uint64_t code, int syntax) fprintf(stderr, "osh: %s: %s at byte %llu\n", syntax ? "syntax error" : "refused", nm, (unsigned long long)sh->w[OSH_S_ERR_OFF]); sh->s.last_status = 2; sh->refused++; +#ifdef OSH_DIFF_TEST_HOOKS + if (sh->diff.on_refusal) sh->diff.on_refusal(sh->diff.ctx, code, sh->w[OSH_S_ERR_OFF]); +#endif } /* expand and run the parsed list. Returns 1 when the shell must stop (exit builtin, interrupted, fault). */ @@ -239,6 +242,10 @@ static int run_list(OshShell *sh) uint64_t nc = w[OSH_REQUEST + 1]; OshResult res; osh_exec_record(&sh->s, w + OSH_REQUEST, (size_t)(OSH_REQ_HDR_CELLS + nc * OSH_CMD_CELLS), w + OSH_OUT, (size_t)w[OSH_REQUEST + 4], &res); +#ifdef OSH_DIFF_TEST_HOOKS + if (sh->diff.on_pipeline) + sh->diff.on_pipeline(sh->diff.ctx, w + OSH_REQUEST, (size_t)(OSH_REQ_HDR_CELLS + nc * OSH_CMD_CELLS), w + OSH_OUT, (size_t)w[OSH_REQUEST + 4], &res); +#endif w[OSH_S_LAST_STATUS] = (uint64_t)(unsigned)sh->s.last_status; if (sh->s.exit_requested) return 1; if (res.killed_by_int) return 0; @@ -318,3 +325,8 @@ int osh_shell_run_fd(OshShell *sh, int fd) LineSrc src = {NULL, 0, 0, fd, 0}; return run_src(sh, &src); } + +#ifdef OSH_DIFF_TEST_HOOKS +/* marker symbol: the build checks the shipped osh does NOT contain it (so no test hook is compiled into production) */ +int osh_diff_test_hooks_compiled(void) { return 1; } +#endif diff --git a/src/osh/host/osh_shell.h b/src/osh/host/osh_shell.h index ae39ec173..21e9f096d 100644 --- a/src/osh/host/osh_shell.h +++ b/src/osh/host/osh_shell.h @@ -11,6 +11,17 @@ #include "osh_core.h" #include "osh_host.h" +#ifdef OSH_DIFF_TEST_HOOKS +/* TEST-ONLY (tests/osh/diff, omega#568): a read-only observer of every request record the core produced, the result the + * host gave it, and every core refusal. It receives const views only and cannot change the record, the result or the + * session. Compiled only with -DOSH_DIFF_TEST_HOOKS; `make test-osh-diff` checks the shipped osh does not contain it. */ +typedef struct { + void (*on_pipeline)(void *ctx, const uint64_t *rec, size_t nrec, const uint64_t *out, size_t nout, const OshResult *res); + void (*on_refusal)(void *ctx, uint64_t code, uint64_t offset); + void *ctx; +} OshDiffHooks; +#endif + typedef struct { OshCore core; OshSession s; @@ -23,6 +34,9 @@ typedef struct { uint8_t *buf; /* bytes of the list in progress */ size_t blen, bcap; unsigned long lists, refused; +#ifdef OSH_DIFF_TEST_HOOKS + OshDiffHooks diff; /* TEST-ONLY observer, all NULL unless a test sets it */ +#endif } OshShell; /* Compile the units (embedded sources) and set up the session from envp. 0 ok, else -1 with a message on stderr. */ diff --git a/tests/.crumb b/tests/.crumb index bd5535a8b..b299f421c 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -199,8 +199,8 @@ "purpose": "Simulated M16 driver for numeric resource-lifecycle tests. No device execution or numeric parity" }, { - "digest": "f3085ce1facdcdb22bfedc85de659dd5e512aa7b59cd8103666c24a4456681c3", - "last_commit": "6d8d92ad", + "digest": "b9e4a14092b80eb56b47892fd1627b139a9742c1e0152c9cebce004922464978", + "last_commit": "9d02dff5", "name": "osh", "purpose": "osh lexer host driver, vendored aien-protocols PR#16 draft vectors (5ae5292)" }, @@ -295,15 +295,15 @@ "purpose": "Directory tests/visor. Purpose not yet described by a human." } ], - "children_root": "sha256:ddc3618190458bc5073e3bee67ffccc68a2ee9f065f2cb0d86545386a29fddf8", - "compiled_at": "2026-10-16T19:04:20Z", + "children_root": "sha256:d0bb5176da37d921c83273ea46e15c2ae09d9a656fb6b15b2b873386180db34d", + "compiled_at": "2026-10-19T21:47:48Z", "compiler": "crumb-compile/1", - "digest": "sha256:4eb9d4d76234448c0ed691f0aeb9c90d785f8a47b1b1291d728bbe3e1509dc10", + "digest": "sha256:3adc4863795df2eb053dafe86b6200ada71b22d23ce5d1b7a6ad578daa8b322f", "evidence_files": 16, "evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a", - "files": 1270, - "generated_at_commit": "88e24bdd57b27817d030d0ad95f62c3f3eacf426", - "source_tree": "sha256:e6c4f0021d383ba2e59da0d85d06a0b98170f6d188289c891b5745200548e6fc" + "files": 1276, + "generated_at_commit": "9d02dff53e084f0426e246002c4839114111812c", + "source_tree": "sha256:eae0dd7bd07041539e8529369b0057df89075d16d44c377ae6cf9ba53c6dd83d" }, "owner_lane": "", "provenance": { diff --git a/tests/gates/omega-osh-diff.gate b/tests/gates/omega-osh-diff.gate new file mode 100644 index 000000000..740dc0234 --- /dev/null +++ b/tests/gates/omega-osh-diff.gate @@ -0,0 +1,24 @@ +# tests/gates/omega-osh-diff.gate +# Host-only OSH differential suite (omega #568): make test-osh-diff (plain and ASan/UBSan). +gate: OMEGA-OSH-DIFF +manifest_version: 2 +owner: omega +requires: + - host +inputs: + - Makefile + - mk/ + - src/ + - tests/ + - tools/ + - physics.lock + - tests/gates/ +run: + exec: tests/gates/run.sh + args: ["osh-diff"] +expects: + exit: 0 + verdict: PASS +timeout: 20m +exactness: EXACT +cache_scope: machine diff --git a/tests/gates/run.sh b/tests/gates/run.sh index 6c5c918d5..68be42b0b 100755 --- a/tests/gates/run.sh +++ b/tests/gates/run.sh @@ -190,6 +190,7 @@ case "$step" in tools/rx_host_receipts.sh native "$native" "$OMEGA_CANDIDATE_COMMIT" tools/rx_host_receipts.sh generation "$generation" "$OMEGA_CANDIDATE_COMMIT" ;; + osh-diff) make $NOPHYS test-osh-diff ;; faculty-arm) pin physics-src-6d7cf0d physics 6d7cf0d4d8eb2cda7b512100ff6058e25dbb3ddf export PHYSICS_DIR="$PINS/physics-src-6d7cf0d" diff --git a/tests/osh/.crumb b/tests/osh/.crumb index 07209abd9..37c1b2ab2 100644 --- a/tests/osh/.crumb +++ b/tests/osh/.crumb @@ -4,7 +4,7 @@ "children": [ { "digest": "d16052a41951da04f9d5581286bf1b523c4fd4a8b22b2f5a9f77ab724f000c38", - "last_commit": "181e2ea8", + "last_commit": "6d8d92ad", "name": "e2e", "purpose": "end-to-end tests of the osh program: argprint helper plus a runner comparing native and OSH_INTE" }, @@ -22,14 +22,14 @@ } ], "children_root": "sha256:0c30d758ea0a085e950c5df69c53069c317df4141043e14a83e19247106399f3", - "compiled_at": "2026-10-10T08:29:30Z", + "compiled_at": "2026-10-19T21:47:48Z", "compiler": "crumb-compile/1", - "digest": "sha256:f3085ce1facdcdb22bfedc85de659dd5e512aa7b59cd8103666c24a4456681c3", + "digest": "sha256:b9e4a14092b80eb56b47892fd1627b139a9742c1e0152c9cebce004922464978", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 93, - "generated_at_commit": "181e2ea8961e1067efc0337b6b4dddc9f19d6947", - "source_tree": "sha256:67f4c363f1a849cf10cbae3aa94fb4675c59d42a075e6a260cc264bebc52319d" + "files": 98, + "generated_at_commit": "9d02dff53e084f0426e246002c4839114111812c", + "source_tree": "sha256:92c676c22ee040f93887b8b665ffd38f1b04618424eb88b26cb5704931b1eef9" } }, "name": "osh-tests", diff --git a/tests/osh/diff/README.md b/tests/osh/diff/README.md new file mode 100644 index 000000000..6a9093d27 --- /dev/null +++ b/tests/osh/diff/README.md @@ -0,0 +1,142 @@ +# OSH cross-adapter pipeline differential (omega#568) + +Status: host side runs; the AIENOS native column reports NOT_RUN until native multi-command execution exists +(aienos-mv#429/#430). Parent: aien-architecture-mv#158. Contract: aien-protocols-mv `specs/osh-platform/OSH_PLATFORM_ABI.md` +v1 DRAFT at `fb5fca98e6676bb104888c73268e0f9a81e9c63e` (no `feat/osh-stream-abi-43` branch existed when this was written). + +`make test-osh-diff` builds and runs: + +- `osh_diff` (plain, then ASan/UBSan): every fixture in `fixtures.txt` through the real compiled Omega shell core (the three + `.osc` units embedded in `build/osh/osh_units.c`, driven by `osh_shell_run_string`, the same loop as the `osh` program) on + the Linux host adapter, once with native AArch64 units and once with the OSC interpreter; +- `osh_toy`, the toy stage operations, linked under each name into a private PATH directory; +- `mutants.sh`, which proves the runner goes red; +- a symbol check that the TEST-ONLY observer (`-DOSH_DIFF_TEST_HOOKS`, `OshDiffHooks` in `src/osh/host/osh_shell.h`) is + absent from the shipped `build/osh/osh`. + +Options: `OSH_DIFF_NATIVE=FILE` compares a native result file (a raw QEMU serial log is fine); +`OSH_DIFF_REQUIRE_NATIVE=1` makes any portable NOT_RUN a failure (acceptance mode). + +## Columns and verdicts + +| column | PASS means | NOT_RUN means | +|---|---|---| +| host-native | the run matched every expectation of the fixture, used only the native core backend, left no descriptor open and no process in the pipeline group (ABI section 12) | the fixture cannot apply in this build (the RLIMIT_AS fixture under ASan; RLIMIT_NPROC when run as root) | +| host-interp | same, interpreter backend, and the normalized result line equals host-native's | same | +| aienos | the native line for the fixture equals the host-native line, field by field | no native file, no line for that fixture, or the fixture is `scope linux` | + +A supplied native file that is invalid fails every portable fixture. Invalid means: no `OSHDIFF-RESULT 1` header, no pin +line, a missing pin, no end line or a wrong count (a truncated log), a duplicate or unknown fixture, any unknown OSHDIFF line, +`adapter` not `aienos-native`, or `abi`, `fixtures` or `core` different from the host run. NOT_RUN is never PASS: the +final line is `OSH_DIFF_PASS` only when nothing failed, and with `OSH_DIFF_REQUIRE_NATIVE=1` only when every portable +fixture has a native PASS. + +## Fixture format (`fixtures.txt`) + +Line oriented; `#` comments; one `abi` pin line; fixtures from `fixture ID` to `end`. Strings are double quoted with the +vectors/README escapes `\"`, `\\`, `\xHH`, plus `\n` and `\t` for readability. + +| key | meaning | +|---|---| +| `scope portable` / `scope linux` | portable: every adapter must agree. linux: the mechanism is Linux-only (rlimits, stop signals, EMFILE); native NOT_RUN by design | +| `covers TEXT` | what the fixture proves | +| `text "..."` | shell input bytes, handed to the core as one `-c` string | +| `direct "a","b" \| "c"` | AIEN direct-argv submission (ABI section 11), one pipeline; no shell text, the core is not called | +| `stdin "..."`, `file NAME "..."` | standard input; files created in the run directory first | +| `fault ...` | injected fault (below) | +| `limit ...` | resource limit (below) | +| `req LINE` | every executed request record, in the vectors/README grammar, pipelines `P0`, `P1`, ... in execution order (a skipped `&&`/`\|\|` pipeline produces no record) | +| `refuse CODE NAME off=N` | the core refused a list (ABI 5.3, 5.4) | +| `stdout "..."`, `status N` | exact standard output bytes and final shell status | +| `stages P0=a,b P1=c`, `err P0=NAME` | stage categories and pipeline platform error (ABI 8.6) per pipeline | +| `outfile NAME "..."`, `outlen NAME N` | file content, or only its length, after the run | +| `stderr_has "..."` | host-only: a diagnostic substring (diagnostic text is not compared across adapters) | + +Grammar extension (UNVERIFIED, the vectors grammar has no direct form): ` direct` follows `conn=N` when request flags bit 0 +is set. + +### Faults (portable meaning; each adapter injects them its own way) + +| fault | meaning | Linux host injection | +|---|---|---| +| `launch_fail_at=N` | launching stage N fails | `OshSession.fail_fork_at` | +| `revoke_spawn=NAME` | the capability for spawning NAME is revoked before its effect | effect hook returns `REVOKED` | +| `interrupt_after_launch` | cancel in interrupt mode once every stage is launched | `kill(0, SIGINT)` from `after_launch_hook` | +| `stop_first_after_launch` | Linux only: one member receives SIGSTOP | SIGSTOP to the first child listed in `/proc/self/task/*/children` | + +`mutant_leak_fd` and `mutant_leak_process` exist only for `mutants.sh`; they break cleanup on purpose. + +### Limits (Linux only) + +`nofile=tight` (RLIMIT_NOFILE leaves exactly one free descriptor), `nproc=N`, `fsize=N` and `as=BYTES`, applied in the run +child just before the shell runs, so the shell and every stage inherit them. + +## Toy operations + +The contract both adapters implement. On Linux they are `osh_toy.c`; on AIENOS they are Omega-native operations +(resolution class 1) with the same names and the same bytes. + +| name | behaviour | +|---|---| +| `emit ARGS...` | each argument plus a newline | +| `args ARGS...` | `N\n`, then `LEN:BYTES\n` per argument | +| `pass` | copy input to output, reads of up to 4096 bytes | +| `trickle` | copy one byte per read and one byte per write | +| `flood N` | N bytes, byte i = `'a' + i % 26` | +| `bytes` | the 256 byte values 0 to 255 once | +| `take N` | read until N bytes or end of input, write them, end without reading more | +| `sink` | read to end of input, print `n=COUNT sum=FNV1A32\n` (8 lower-case hex digits) | +| `status N` | end with status N, no output | +| `oe` | `O` on output, then `E` on error output | +| `stall` | wait until cancelled | + +## Stage categories (the receipt categories compared across adapters) + +Each command's ABI section 10 outcome plus how it ended. UNVERIFIED: the signal mappings are this harness's choice. + +| category | Linux host source | +|---|---| +| `ok`, `exit:N` | COMPLETED, no signal, status 0 or N | +| `broken_pipe` | COMPLETED, killed by SIGPIPE (native: a write returned `BROKEN_PIPE`) | +| `interrupted` | COMPLETED, killed by SIGINT | +| `limit` | COMPLETED, killed by SIGXFSZ | +| `signal:N` | COMPLETED, any other signal (Linux only) | +| `cancelled`, `not_started`, `unknown` | CANCELLED, NOT_STARTED, OUTCOME_UNKNOWN | +| `not_found` | FAILED_NO_EFFECT, `NOT_FOUND` | +| `refused:NAME` | FAILED_NO_EFFECT, `DENIED`, `REVOKED`, `STALE`, `CAP_DOMAIN_MISMATCH` or `CAP_GEN_NARROW` | +| `failed:NAME` | FAILED_NO_EFFECT, any other platform error | + +## Result file (what the AIENOS native harness must print) + +The host writes the same format with `--emit` (`build/osh/diff-host.result`, adapter `linux-host`); copy it for a worked +example. Only lines that start with `OSHDIFF` count, after removing a trailing CR, so the native harness can print them on +the serial console among other output and the QEMU log is passed as is. + +``` +OSHDIFF-RESULT 1 +OSHDIFF pin adapter=aienos-native aienos= omega= core= abi= cc= harness= fixtures= +OSHDIFF vec req=[,...]|none refuse=:|none status= stages=P0=,[;P1=...]|none err=P0=[;P1=...]|none out=: files=::[,...]|none +OSHDIFF end n= +``` + +- `req`: sha256 of each executed request record in ABI 7.3 wire form: 16-byte header `{magic u32 0x4F534851, abi_version + u16 1, kind u16 1, total_len u32, reserved u32 0}`, the record cells (8 header + 164 per command) as u64 little-endian, + then `out_used` bytes of OUT, one byte per OUT cell (UNVERIFIED: the draft says "out_used bytes of OUT" without saying + one byte per cell). The same core gives the same bytes on every adapter; any difference fails. +- `out`: length and sha256 of standard output. `files`: for each `outfile`/`outlen` key of the fixture, in fixture order. +- `core`: sha256 over, for lex, parse, expand in that order, the source length as u64 little-endian then the `.osc` + source bytes. `fixtures`: sha256 of `fixtures.txt`. Both must equal the host's. +- One line per fixture the native harness actually ran. A fixture it cannot run (a fault it cannot inject) is left out and + shows NOT_RUN. + +## Pins + +The host pin line records `omega` (git HEAD, `-dirty` if `src`, `tests` or `mk` differ), `core`, `abi`, `cc` (compiler +name, version, first 16 hex digits of the sha256 of the compiler driver), `harness` (sha256 of `osh_diff.c`, `osh_toy.c` +and `mutants.sh`), `fixtures`, and `aienos=lock:` (the AIENOS commit `aienos.lock` pins; the host runs no AIENOS). + +## What this does not prove + +Host and interpreter results only. No QEMU or hardware run exists: the AIENOS column is NOT_RUN. Revocation is checked at +the spawn effect boundary only; revocation during a blocked native read or write needs the native stream service. The +durable journal is not enabled in these runs (`test-osh-journal` covers it). diff --git a/tests/osh/diff/fixtures.txt b/tests/osh/diff/fixtures.txt new file mode 100644 index 000000000..b169f6770 --- /dev/null +++ b/tests/osh/diff/fixtures.txt @@ -0,0 +1,387 @@ +# OSH cross-adapter stage/stream fixtures (omega#568, parent aien-architecture-mv#158). Format: README.md in this directory. +# Implementation-agnostic: every fixture names toy operations (README, "Toy operations"), never a host path, so the request +# record the shell core builds is the same bytes on every adapter. Expected values were written by hand from +# OSH_PLATFORM_ABI.md v1 DRAFT (sections 5, 7, 8, 10, 12) and the toy contract; stream checksums are FNV-1a 32 computed by +# separate code, not by the runner. scope portable = every adapter must agree; scope linux = Linux-only mechanism +# (rlimits, stop signals, EMFILE), host column only, native reports NOT_RUN. +abi aien-protocols-mv@fb5fca98e6676bb104888c73268e0f9a81e9c63e specs/osh-platform/OSH_PLATFORM_ABI.md v1-draft + +fixture f01_two_stage +scope portable +covers two-stage pipeline; producer shutdown: EOF reaches the consumer when the only writer exits +text "emit a b | pass\n" +req P0 conn=1 | C0 b=0 argv="emit","a","b" as= rd= | C1 b=0 argv="pass" as= rd= +stdout "a\nb\n" +status 0 +stages P0=ok,ok +err P0=OK +end + +fixture f02_three_stage +scope portable +covers three-stage pipeline with a short-read/short-write stage in the middle position +text "emit x y | pass | trickle\n" +req P0 conn=1 | C0 b=0 argv="emit","x","y" as= rd= | C1 b=0 argv="pass" as= rd= | C2 b=0 argv="trickle" as= rd= +stdout "x\ny\n" +status 0 +stages P0=ok,ok,ok +err P0=OK +end + +fixture f03_argv_boundary_text +scope portable +covers argument boundaries from shell text: quoted blanks, empty argument, escaped quote +text "args a 'b c' '' \"x\\\"y\"\n" +req P0 conn=1 | C0 b=0 argv="args","a","b c","","x\"y" as= rd= +stdout "4\n1:a\n4:b c\n0:\n3:x\"y\n" +status 0 +stages P0=ok +err P0=OK +end + +fixture f04_argv_boundary_direct +scope portable +covers AIEN direct-argv submission (ABI section 11): no shell text, blank, empty, non-ASCII and glob bytes pass as given +direct "args","a b","","\x80\xff","*" +req P0 conn=0 direct | C0 b=0 argv="args","a b","","\x80\xff","*" as= rd= +stdout "4\n3:a b\n0:\n2:\x80\xff\n1:*\n" +status 0 +stages P0=ok +err P0=OK +end + +fixture f05_direct_pipeline +scope portable +covers direct-argv two-stage pipeline (ABI section 11 allows ncmds 1 to 8) +direct "bytes" | "sink" +req P0 conn=0 direct | C0 b=0 argv="bytes" as= rd= | C1 b=0 argv="sink" as= rd= +stdout "n=256 sum=90a458c5\n" +status 0 +stages P0=ok,ok +err P0=OK +end + +fixture f06_binary_bytes +scope portable +covers all 256 byte values through two pipes unchanged (bytes are bytes, ABI section 2) +text "bytes | pass | sink\n" +req P0 conn=1 | C0 b=0 argv="bytes" as= rd= | C1 b=0 argv="pass" as= rd= | C2 b=0 argv="sink" as= rd= +stdout "n=256 sum=90a458c5\n" +status 0 +stages P0=ok,ok,ok +err P0=OK +end + +fixture f07_large_output +scope portable +covers 4 MiB through a bounded pipe (64 KiB minimum, ABI 8.5): backpressure, no loss, no truncation +text "flood 4194304 | pass | sink\n" +req P0 conn=1 | C0 b=0 argv="flood","4194304" as= rd= | C1 b=0 argv="pass" as= rd= | C2 b=0 argv="sink" as= rd= +stdout "n=4194304 sum=2d569a25\n" +status 0 +stages P0=ok,ok,ok +err P0=OK +end + +fixture f08_short_reads_writes +scope portable +covers one-byte reads and writes on both sides of a pipe, four stages, odd total length +text "flood 100003 | trickle | trickle | sink\n" +req P0 conn=1 | C0 b=0 argv="flood","100003" as= rd= | C1 b=0 argv="trickle" as= rd= | C2 b=0 argv="trickle" as= rd= | C3 b=0 argv="sink" as= rd= +stdout "n=100003 sum=05db6bd3\n" +status 0 +stages P0=ok,ok,ok,ok +err P0=OK +end + +fixture f09_consumer_early_exit +scope portable +covers consumer early exit: the writer of a pipe with no reader gets BROKEN_PIPE (Linux: SIGPIPE); pipeline status is the last stage +text "flood 4194304 | take 5\n" +req P0 conn=1 | C0 b=0 argv="flood","4194304" as= rd= | C1 b=0 argv="take","5" as= rd= +stdout "abcde" +status 0 +stages P0=broken_pipe,ok +err P0=OK +end + +fixture f10_early_exit_middle +scope portable +covers early exit in the middle of three stages: upstream broken, downstream sees EOF after the bytes it got +text "flood 4194304 | take 3 | sink\n" +req P0 conn=1 | C0 b=0 argv="flood","4194304" as= rd= | C1 b=0 argv="take","3" as= rd= | C2 b=0 argv="sink" as= rd= +stdout "n=3 sum=1a47e90b\n" +status 0 +stages P0=broken_pipe,ok,ok +err P0=OK +end + +fixture f11_zero_length_stream +scope portable +covers a producer that writes nothing: zero-length stream, EOF only when all writers close +text "emit | pass | sink\n" +req P0 conn=1 | C0 b=0 argv="emit" as= rd= | C1 b=0 argv="pass" as= rd= | C2 b=0 argv="sink" as= rd= +stdout "n=0 sum=811c9dc5\n" +status 0 +stages P0=ok,ok,ok +err P0=OK +end + +fixture f12_exit_statuses +scope portable +covers stage exit statuses; pipeline status = last stage; $? feeds the next pipeline's request +text "status 3 | status 4; emit $?\n" +req P0 conn=1 | C0 b=0 argv="status","3" as= rd= | C1 b=0 argv="status","4" as= rd= | P1 conn=1 | C0 b=0 argv="emit","4" as= rd= +stdout "4\n" +status 0 +stages P0=exit:3,exit:4 P1=ok +err P0=OK P1=OK +end + +fixture f13_failed_producer +scope portable +covers a producer that fails without output: the consumer still completes and owns the pipeline status +text "status 9 | sink\n" +req P0 conn=1 | C0 b=0 argv="status","9" as= rd= | C1 b=0 argv="sink" as= rd= +stdout "n=0 sum=811c9dc5\n" +status 0 +stages P0=exit:9,ok +err P0=OK +end + +fixture f14_and_or +scope portable +covers && and || connectors: a skipped pipeline is never expanded and never produces a request +text "status 1 && emit no || emit yes\n" +req P0 conn=2 | C0 b=0 argv="status","1" as= rd= | P1 conn=1 | C0 b=0 argv="emit","yes" as= rd= +stdout "yes\n" +status 0 +stages P0=exit:1 P1=ok +err P0=OK P1=OK +end + +fixture f15_redir_order_dup_first +scope portable +covers redirection order 2>&1 >FILE: stderr follows the old stdout, stdout goes to the file (ABI 7.2) +text "oe 2>&1 >o1\n" +req P0 conn=1 | C0 b=0 argv="oe" as= rd=DUP2:1,OUT1:"o1" +stdout "E" +outfile o1 "O" +status 0 +stages P0=ok +err P0=OK +end + +fixture f16_redir_order_file_first +scope portable +covers redirection order >FILE 2>&1: both streams in the file +text "oe >o2 2>&1\n" +req P0 conn=1 | C0 b=0 argv="oe" as= rd=OUT1:"o2",DUP2:1 +stdout "" +outfile o2 "OE" +status 0 +stages P0=ok +err P0=OK +end + +fixture f17_redir_in_pipeline +scope portable +covers input redirection on the first stage and output redirection on the last stage of a pipeline +file in1 "q\nr\n" +text "pass o3\n" +req P0 conn=1 | C0 b=0 argv="pass" as= rd=IN0:"in1" | C1 b=0 argv="pass" as= rd=OUT1:"o3" +stdout "" +outfile o3 "q\nr\n" +status 0 +stages P0=ok,ok +err P0=OK +end + +fixture f18_failed_redirection +scope portable +covers a failed input redirection: that stage does not run (status 1), its neighbour still completes +text "pass o4\n" +req P0 conn=1 | C0 b=0 argv="flood","100000" as= rd=OUT1:"o4" +stdout "" +outlen o4 4096 +status 153 +stages P0=limit +err P0=OK +end + +fixture f33_bounded_memory +scope linux +covers 4 MiB through three stages under RLIMIT_AS 256 MiB on the shell and every stage: streaming, never whole-stream buffering +limit as=268435456 +text "flood 4194304 | pass | sink\n" +req P0 conn=1 | C0 b=0 argv="flood","4194304" as= rd= | C1 b=0 argv="pass" as= rd= | C2 b=0 argv="sink" as= rd= +stdout "n=4194304 sum=2d569a25\n" +status 0 +stages P0=ok,ok,ok +err P0=OK +end diff --git a/tests/osh/diff/mutants.sh b/tests/osh/diff/mutants.sh new file mode 100644 index 000000000..afd6c39c4 --- /dev/null +++ b/tests/osh/diff/mutants.sh @@ -0,0 +1,123 @@ +#!/bin/sh +# mutants.sh -- prove the differential runner goes red (omega#568). Run by `make test-osh-diff`. +# Usage: mutants.sh RUNNER TOYBIN FIXTURES HOST_RESULT WORKDIR +# HOST_RESULT is the --emit file of the plain run that just passed. The comparator mutants run in compare-only mode +# (--host-results), whose final line is OSH_DIFF_COMPARE_ONLY_*, never OSH_DIFF_PASS; their "native" files are forged +# from host results and are never evidence. The expectation and cleanup mutants run the real shell on one fixture each. +set -u +R=$1 TOY=$2 FX=$3 HOST=$4 W=$5 +rm -rf "$W" && mkdir -p "$W" || exit 1 +fails=0 n=0 +portable=$(grep -c '^scope portable$' "$FX") + +# expect NAME WANT_RC LAST_LINE [GREP_PATTERN] -- after the command has written $W/NAME.out +expect() { + n=$((n + 1)) + rc=$(cat "$W/$1.rc") + last=$(tail -n 1 "$W/$1.out") + if [ "$rc" != "$2" ] || [ "$last" != "$3" ]; then + echo "mutant $1: want rc=$2 last=$3, got rc=$rc last=$last"; fails=$((fails + 1)); return + fi + if [ $# -ge 4 ] && ! grep -q -- "$4" "$W/$1.out"; then + echo "mutant $1: output lacks /$4/"; fails=$((fails + 1)); return + fi + echo "mutant $1: as required (rc=$rc, $last)" +} + +cmp_only() { # NAME NATIVE_FILE [extra options] + name=$1 nat=$2; shift 2 + "$R" "$@" --host-results "$HOST" ${nat:+--native "$nat"} "$TOY" "$FX" > "$W/$name.out" 2>&1 + echo $? > "$W/$name.rc" +} + +forge() { sed -e 's/ adapter=linux-host / adapter=aienos-native /' -e 's/ aienos=lock:[^ ]* / aienos=forged-by-mutants.sh /' "$HOST"; } + +# m00 positive control: a native file that agrees on every fixture gives aienos PASS on every portable fixture +forge > "$W/m00.native" +cmp_only m00 "$W/m00.native" +expect m00 0 OSH_DIFF_COMPARE_ONLY_PASS "aienos PASS $portable FAIL 0 " + +# m01 one stdout digest differs (large-output fixture) +forge | awk '/^OSHDIFF vec f07_large_output /{sub(/ out=[0-9]+:[0-9a-f]/, " out=4194304:X")}1' > "$W/m01.native" +cmp_only m01 "$W/m01.native" +expect m01 1 OSH_DIFF_COMPARE_ONLY_FAIL '^f07_large_output .*FAIL.*differs from host' + +# m02 one request-record digest differs (byte-exact request comparison) +forge | awk '/^OSHDIFF vec f01_two_stage /{sub(/ req=[0-9a-f]/, " req=X")}1' > "$W/m02.native" +cmp_only m02 "$W/m02.native" +expect m02 1 OSH_DIFF_COMPARE_ONLY_FAIL '^f01_two_stage .*FAIL.*differs from host' + +# m03 a stage category differs (receipt category comparison) +forge | awk '/^OSHDIFF vec f09_consumer_early_exit /{sub(/P0=broken_pipe,ok/, "P0=ok,ok")}1' > "$W/m03.native" +cmp_only m03 "$W/m03.native" +expect m03 1 OSH_DIFF_COMPARE_ONLY_FAIL '^f09_consumer_early_exit .*FAIL' + +# m04 truncated serial log: no end line, so every portable fixture fails (never partly green) +forge | grep -v '^OSHDIFF end ' > "$W/m04.native" +cmp_only m04 "$W/m04.native" +expect m04 1 OSH_DIFF_COMPARE_ONLY_FAIL "no end line" + +# m05 one fixture missing from the native file: NOT_RUN, not PASS; acceptable without --require-native only +forge | grep -v '^OSHDIFF vec f26_partial_launch_2 ' | awk '/^OSHDIFF end n=/{split($0, a, "="); $0 = "OSHDIFF end n=" a[2] - 1}1' > "$W/m05.native" +cmp_only m05 "$W/m05.native" +expect m05 0 OSH_DIFF_COMPARE_ONLY_PASS '^f26_partial_launch_2 .*NOT_RUN .*no native line' +cmp_only m05r "$W/m05.native" --require-native +expect m05r 1 OSH_DIFF_COMPARE_ONLY_FAIL "acceptance: 1 portable fixtures have no native AIENOS result" + +# m06 no native file at all, acceptance mode: NOT_RUN is never green +cmp_only m06 "" --require-native +expect m06 1 OSH_DIFF_COMPARE_ONLY_FAIL "acceptance: $portable portable fixtures" + +# m07 host results passed off as native (adapter=linux-host): refused +cmp_only m07 "$HOST" +expect m07 1 OSH_DIFF_COMPARE_ONLY_FAIL "not aienos-native" + +# m08 a different ABI pin +forge | sed 's/ abi=[^ ]* / abi=aien-protocols-mv@0000000 /' > "$W/m08.native" +cmp_only m08 "$W/m08.native" +expect m08 1 OSH_DIFF_COMPARE_ONLY_FAIL "abi pin" + +# m09 a different compiled shell core +forge | sed 's/ core=[0-9a-f]* / core=00 /' > "$W/m09.native" +cmp_only m09 "$W/m09.native" +expect m09 1 OSH_DIFF_COMPARE_ONLY_FAIL "not the same compiled core" + +# m10 a vec line for a fixture that does not exist +{ forge | grep -v '^OSHDIFF end '; echo "OSHDIFF vec f99_none req=none"; forge | grep '^OSHDIFF end '; } > "$W/m10.native" +cmp_only m10 "$W/m10.native" +expect m10 1 OSH_DIFF_COMPARE_ONLY_FAIL "unknown fixture f99_none" + +# m11 a raw serial log with noise and CR line ends is accepted (positive control for the QEMU path) +{ printf 'AIENOS boot\r\n[ 0.1] console up\r\n'; forge | sed 's/$/\r/'; printf 'osh$ \r\n'; } > "$W/m11.native" +cmp_only m11 "$W/m11.native" +expect m11 0 OSH_DIFF_COMPARE_ONLY_PASS "aienos PASS $portable FAIL 0 " + +# Expectation mutants: the real shell runs one fixture whose expectation was altered; the host column must fail. +one() { # NAME AWK_PROGRAM: writes a one-fixture corpus (f01 or as selected by the program) and runs it + name=$1 + { grep '^abi ' "$FX"; awk "$2" "$FX"; } > "$W/$name.fx" + "$R" "$TOY" "$W/$name.fx" > "$W/$name.out" 2>&1 + echo $? > "$W/$name.rc" +} +F01='/^fixture f01_two_stage$/{p=1} p{print} p&&/^end$/{p=0}' +one e00 "$F01" +expect e00 0 OSH_DIFF_PASS "host PASS 2 FAIL 0" +sed 's/^stdout "a\\nb\\n"$/stdout "a\\nc\\n"/' "$W/e00.fx" > "$W/e01.fx"; "$R" "$TOY" "$W/e01.fx" > "$W/e01.out" 2>&1; echo $? > "$W/e01.rc" +expect e01 1 OSH_DIFF_FAIL "stdout: want" +sed 's/argv="pass"/argv="trickle"/' "$W/e00.fx" > "$W/e02.fx"; "$R" "$TOY" "$W/e02.fx" > "$W/e02.out" 2>&1; echo $? > "$W/e02.rc" +expect e02 1 OSH_DIFF_FAIL "request records differ" +sed 's/^stages P0=ok,ok$/stages P0=ok,exit:1/' "$W/e00.fx" > "$W/e03.fx"; "$R" "$TOY" "$W/e03.fx" > "$W/e03.out" 2>&1; echo $? > "$W/e03.rc" +expect e03 1 OSH_DIFF_FAIL "stages: want" +sed 's/^status 0$/status 1/' "$W/e00.fx" > "$W/e04.fx"; "$R" "$TOY" "$W/e04.fx" > "$W/e04.out" 2>&1; echo $? > "$W/e04.rc" +expect e04 1 OSH_DIFF_FAIL "status: want 1, got 0" +sed 's/^err P0=OK$/err P0=BROKEN_PIPE/' "$W/e00.fx" > "$W/e05.fx"; "$R" "$TOY" "$W/e05.fx" > "$W/e05.out" 2>&1; echo $? > "$W/e05.rc" +expect e05 1 OSH_DIFF_FAIL "platform errors: want" + +# Cleanup mutants: TEST-ONLY faults that break cleanup on purpose; the ABI section 12 checks must catch them. +sed 's/^scope portable$/scope portable\nfault mutant_leak_fd/' "$W/e00.fx" > "$W/c01.fx"; "$R" "$TOY" "$W/c01.fx" > "$W/c01.out" 2>&1; echo $? > "$W/c01.rc" +expect c01 1 OSH_DIFF_FAIL "descriptor leak" +sed 's/^scope portable$/scope portable\nfault mutant_leak_process/' "$W/e00.fx" > "$W/c02.fx"; "$R" "$TOY" "$W/c02.fx" > "$W/c02.out" 2>&1; echo $? > "$W/c02.rc" +expect c02 1 OSH_DIFF_FAIL "outlived the shell" + +if [ "$fails" -ne 0 ]; then echo "mutants: FAIL ($fails of $n)"; exit 1; fi +echo "mutants: PASS ($n checks: every mutant red, every control green)" diff --git a/tests/osh/diff/osh_diff.c b/tests/osh/diff/osh_diff.c new file mode 100644 index 000000000..cbf89ee42 --- /dev/null +++ b/tests/osh/diff/osh_diff.c @@ -0,0 +1,1102 @@ +/* + * osh_diff.c -- cross-adapter pipeline differential runner (omega#568, parent aien-architecture-mv#158). + * Contract: aien-protocols-mv specs/osh-platform/OSH_PLATFORM_ABI.md v1 DRAFT at fb5fca98e6676bb104888c73268e0f9a81e9c63e + * (sections 5, 7.1, 7.3, 8, 10, 12); the corpus and the result-file format are tests/osh/diff/README.md. + * + * For every fixture in fixtures.txt the runner drives the ACTUAL compiled Omega shell core (the three .osc units embedded + * in build/osh/osh_units.c, through osh_shell_run_string, the same driver loop as the osh program) on the Linux host + * adapter (src/osh/host), twice: native AArch64 units and the OSC interpreter. Each run is a forked child in its own + * process group, in a private directory, with a PATH that holds only the toy operations (osh_toy.c), and with the + * fixture's injected faults and resource limits. A read-only TEST-ONLY observer (OshDiffHooks, -DOSH_DIFF_TEST_HOOKS) + * hands out every request record the core produced, the host result for it, and every core refusal. + * + * Columns: host-native, host-interp, aienos. A host column is PASS when the run matches every expectation of the fixture + * and the cleanup checks (no descriptor left open, no process left in the pipeline group); the two host columns must also + * give the same normalized result line. The aienos column compares a supplied native result file (--native FILE; a raw + * QEMU serial log is fine, only OSHDIFF lines are read) against the host-native line, field by field. No file, or no + * line for a fixture, is NOT_RUN, never PASS. A file that is supplied but invalid (bad header, missing end line, wrong + * pins, adapter not aienos-native) fails every portable fixture. scope linux fixtures are NOT_RUN on aienos by design. + * + * Usage: osh_diff [--pin KEY=VALUE]... [--native FILE] [--require-native] [--emit FILE] [--host-results FILE] [--strict-host] TOYBIN FIXTURES + * --emit FILE write the host-native results in the result-file format (adapter=linux-host) + * --host-results FILE compare only: take host results from FILE instead of running; final line is then + * OSH_DIFF_COMPARE_ONLY_PASS/FAIL, never OSH_DIFF_PASS (used by the comparator mutants) + * --require-native an aienos NOT_RUN on a portable fixture is a failure (acceptance mode) + * --strict-host a host NOT_RUN is a failure (the plain build; the ASan build allows the RLIMIT_AS fixture to be NOT_RUN) + * Final line OSH_DIFF_PASS or OSH_DIFF_FAIL. + * + * UNVERIFIED (the ABI draft does not pin these; also listed in README.md): + * - wire form (7.3): OUT is serialized as out_used single bytes (one per OUT cell), after the cells. + * - the request line grammar extension " direct" after conn= when flags bit 0 is set (vectors/README has no direct form). + * - stage categories (README "Stage categories") map host outcomes to portable names; the mapping of SIGPIPE to + * broken_pipe, SIGINT to interrupted and SIGXFSZ to limit is this harness's choice. + * - a failed input redirection is exit:1 for that stage on every adapter (bash convention, osh_exec.c DECISIONS). + */ +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "osh_host.h" +#include "osh_layout.h" +#include "osh_shell.h" +#include "sha256.h" + +#ifndef OSH_DIFF_TEST_HOOKS +#error "osh_diff needs the TEST-ONLY observer: build with -DOSH_DIFF_TEST_HOOKS" +#endif +#if defined(__SANITIZE_ADDRESS__) +#define OSH_DIFF_ASAN 1 +#else +#define OSH_DIFF_ASAN 0 +#endif + +#define MAXFX 128 +#define MAXP 16 /* pipelines per fixture (CAP_PIPELINES) */ +#define LINE 16384 +#define RUN_TIMEOUT_S 60 + +static const char *const TOYS[] = {"emit", "args", "pass", "trickle", "flood", "bytes", "take", "sink", "status", "oe", "stall"}; + +/* ---------------- byte strings ---------------- */ + +typedef struct { + unsigned char *p; + size_t n; +} Bytes; + +static int hexv(int c) { return c >= '0' && c <= '9' ? c - '0' : c >= 'a' && c <= 'f' ? c - 'a' + 10 : c >= 'A' && c <= 'F' ? c - 'A' + 10 : -1; } + +/* "..." with \" \\ \xHH (vectors grammar) and the fixture conveniences \n \t. Advances *sp past the closing quote. */ +static int q_decode(const char **sp, Bytes *b) +{ + const char *s = *sp; + if (*s != '"') return -1; + s++; + b->p = malloc(strlen(s) + 1); + b->n = 0; + for (; *s && *s != '"'; s++) { + if (*s != '\\') { b->p[b->n++] = (unsigned char)*s; continue; } + s++; + if (*s == '"' || *s == '\\') b->p[b->n++] = (unsigned char)*s; + else if (*s == 'n') b->p[b->n++] = '\n'; + else if (*s == 't') b->p[b->n++] = '\t'; + else if (*s == 'x' && hexv(s[1]) >= 0 && hexv(s[2]) >= 0) { b->p[b->n++] = (unsigned char)(hexv(s[1]) * 16 + hexv(s[2])); s += 2; } + else { free(b->p); return -1; } + } + if (*s != '"') { free(b->p); return -1; } + *sp = s + 1; + return 0; +} + +/* vectors grammar encoder (same rule as tests/osh/test_osh_expand.c fq, without the repetition shorthand) */ +static void q_encode(char *out, size_t *o, size_t cap, const unsigned char *s, size_t n) +{ + if (*o + 4 * n + 3 >= cap) { out[*o] = 0; return; } + out[(*o)++] = '"'; + for (size_t i = 0; i < n; i++) { + unsigned char c = s[i]; + if (c == '"' || c == '\\') { out[(*o)++] = '\\'; out[(*o)++] = (char)c; } + else if (c < 0x20 || c > 0x7e) *o += (size_t)sprintf(out + *o, "\\x%02x", c); + else out[(*o)++] = (char)c; + } + out[(*o)++] = '"'; + out[*o] = 0; +} + +static void hex32(const uint8_t d[32], char out[65]) +{ + for (int i = 0; i < 32; i++) sprintf(out + 2 * i, "%02x", d[i]); + out[64] = 0; +} + +static void sha_hex(const void *p, size_t n, char out[65]) +{ + uint8_t d[32]; + sha256_hash(p, n, d); + hex32(d, out); +} + +/* ---------------- fixtures ---------------- */ + +/* F_MUTANT_*: break cleanup on purpose so mutants.sh can prove the ABI section 12 checks go red; never in fixtures.txt */ +enum { F_NONE = 0, F_REVOKE_SPAWN, F_INTERRUPT, F_LAUNCH_FAIL, F_STOP_FIRST, F_MUTANT_LEAK_FD, F_MUTANT_LEAK_PROCESS }; +enum { L_NONE = 0, L_NOFILE_TIGHT, L_NPROC, L_FSIZE, L_AS }; + +typedef struct { + char name[64]; + Bytes content; +} FxFile; + +typedef struct { + char id[64]; + int linux_only; + char covers[256]; + int direct; /* 0 text, 1 direct argv */ + Bytes text; /* text mode */ + Bytes dargv[OSH_MAX_CMDS][OSH_MAX_ARGV]; + int dncmds, dnargv[OSH_MAX_CMDS]; + Bytes in; /* stdin, may be empty */ + int has_in; + FxFile setup[4]; + int nsetup; + int fault, fault_arg; + char fault_name[64]; + int limit; + unsigned long long limit_arg; + /* expectations */ + char req[LINE]; + int has_req; + int refuse_code, refuse_off, has_refuse; + char refuse_name[32]; + Bytes out; + int has_out; + Bytes errhas; + int has_errhas; + FxFile outfile[4]; + int noutfile; + char outlen_name[64]; + long long outlen; + int status, has_status; + char stages[LINE], errs[1024]; + int lineno; +} Fixture; + +static Fixture FX[MAXFX]; +static int NFX; +static char ABI_PIN[256], FIXTURES_SHA[65], CORE_SHA[65]; +/* identity the build system passes with --pin KEY=VALUE (mk/osh.mk computes them); "unknown" when not given */ +static const char *PIN_OMEGA = "unknown", *PIN_CC = "unknown", *PIN_HARNESS = "unknown", *PIN_AIENOS_LOCK = "unknown"; + +static int fx_fail(const char *path, int ln, const char *why) +{ + fprintf(stderr, "%s:%d: %s\n", path, ln, why); + return -1; +} + +static char *rest(char *line, const char *key) +{ + size_t k = strlen(key); + if (strncmp(line, key, k) != 0 || line[k] != ' ') return NULL; + return line + k + 1; +} + +static int load_fixtures(const char *path) +{ + FILE *f = fopen(path, "rb"); + if (!f) { fprintf(stderr, "cannot open %s: %s\n", path, strerror(errno)); return -1; } + { + sha256_ctx c; + sha256_init(&c); + unsigned char b[4096]; + size_t r; + while ((r = fread(b, 1, sizeof b, f)) > 0) sha256_update(&c, b, r); + uint8_t d[32]; + sha256_final(&c, d); + hex32(d, FIXTURES_SHA); + rewind(f); + } + static char line[LINE]; + int ln = 0; + Fixture *x = NULL; + while (fgets(line, sizeof line, f)) { + ln++; + size_t n = strlen(line); + if (n && line[n - 1] == '\n') line[--n] = 0; + if (n == 0 || line[0] == '#') continue; + char *v; + if ((v = rest(line, "abi"))) { snprintf(ABI_PIN, sizeof ABI_PIN, "%.*s", (int)strcspn(v, " "), v); continue; } + if ((v = rest(line, "fixture"))) { + if (x) return fx_fail(path, ln, "fixture without end"); + if (NFX >= MAXFX) return fx_fail(path, ln, "too many fixtures"); + x = &FX[NFX]; + memset(x, 0, sizeof *x); + x->lineno = ln; + if (strlen(v) == 0 || strlen(v) >= sizeof x->id || strchr(v, 0x20)) return fx_fail(path, ln, "fixture id must be one word under 64 bytes"); + memcpy(x->id, v, strlen(v) + 1); + for (int i = 0; i < NFX; i++) + if (strcmp(FX[i].id, x->id) == 0) return fx_fail(path, ln, "duplicate fixture id"); + continue; + } + if (!x) return fx_fail(path, ln, "line outside a fixture"); + if (strcmp(line, "end") == 0) { + if (!x->direct && !x->text.p) return fx_fail(path, ln, "fixture has no text or direct line"); + if (!x->has_status || !x->has_out) return fx_fail(path, ln, "fixture lacks status or stdout expectation"); + if (!x->has_req && !x->has_refuse) return fx_fail(path, ln, "fixture lacks req or refuse expectation"); + NFX++; + x = NULL; + continue; + } + const char *s; + if ((v = rest(line, "scope"))) { + if (strcmp(v, "portable") == 0) x->linux_only = 0; + else if (strcmp(v, "linux") == 0) x->linux_only = 1; + else return fx_fail(path, ln, "scope must be portable or linux"); + } else if ((v = rest(line, "covers"))) snprintf(x->covers, sizeof x->covers, "%s", v); + else if ((v = rest(line, "text"))) { + s = v; + if (q_decode(&s, &x->text) || *s) return fx_fail(path, ln, "bad text string"); + } else if ((v = rest(line, "direct"))) { + x->direct = 1; + s = v; + for (;;) { + if (x->dncmds >= OSH_MAX_CMDS) return fx_fail(path, ln, "too many direct commands"); + int c = x->dncmds; + for (;;) { + if (x->dnargv[c] >= OSH_MAX_ARGV || q_decode(&s, &x->dargv[c][x->dnargv[c]])) return fx_fail(path, ln, "bad direct argv"); + if (memchr(x->dargv[c][x->dnargv[c]].p, 0, x->dargv[c][x->dnargv[c]].n)) return fx_fail(path, ln, "NUL in a direct argument (not expressible to the builder)"); + x->dnargv[c]++; + if (*s != ',') break; + s++; + } + x->dncmds++; + if (*s == 0) break; + if (strncmp(s, " | ", 3) != 0) return fx_fail(path, ln, "direct commands are separated by \" | \""); + s += 3; + } + } else if ((v = rest(line, "stdin"))) { + s = v; + if (q_decode(&s, &x->in) || *s) return fx_fail(path, ln, "bad stdin string"); + x->has_in = 1; + } else if ((v = rest(line, "file"))) { + if (x->nsetup >= 4) return fx_fail(path, ln, "too many files"); + FxFile *ff = &x->setup[x->nsetup++]; + size_t k = strcspn(v, " "); + if (k == 0 || k >= sizeof ff->name || v[k] != ' ' || memchr(v, '/', k)) return fx_fail(path, ln, "bad file name"); + snprintf(ff->name, sizeof ff->name, "%.*s", (int)k, v); + s = v + k + 1; + if (q_decode(&s, &ff->content) || *s) return fx_fail(path, ln, "bad file content"); + } else if ((v = rest(line, "fault"))) { + if (x->fault) return fx_fail(path, ln, "one fault per fixture"); + if (strncmp(v, "revoke_spawn=", 13) == 0) { x->fault = F_REVOKE_SPAWN; snprintf(x->fault_name, sizeof x->fault_name, "%s", v + 13); } + else if (strcmp(v, "interrupt_after_launch") == 0) x->fault = F_INTERRUPT; + else if (strcmp(v, "stop_first_after_launch") == 0) x->fault = F_STOP_FIRST; + else if (strcmp(v, "mutant_leak_fd") == 0) x->fault = F_MUTANT_LEAK_FD; + else if (strcmp(v, "mutant_leak_process") == 0) x->fault = F_MUTANT_LEAK_PROCESS; + else if (strncmp(v, "launch_fail_at=", 15) == 0) { + x->fault = F_LAUNCH_FAIL; + x->fault_arg = atoi(v + 15); + if (x->fault_arg < 1 || x->fault_arg > OSH_MAX_CMDS) return fx_fail(path, ln, "launch_fail_at must be 1..8"); + } else return fx_fail(path, ln, "unknown fault"); + } else if ((v = rest(line, "limit"))) { + if (x->limit) return fx_fail(path, ln, "one limit per fixture"); + if (strcmp(v, "nofile=tight") == 0) x->limit = L_NOFILE_TIGHT; + else if (strncmp(v, "nproc=", 6) == 0) { x->limit = L_NPROC; x->limit_arg = strtoull(v + 6, NULL, 10); } + else if (strncmp(v, "fsize=", 6) == 0) { x->limit = L_FSIZE; x->limit_arg = strtoull(v + 6, NULL, 10); } + else if (strncmp(v, "as=", 3) == 0) { x->limit = L_AS; x->limit_arg = strtoull(v + 3, NULL, 10); } + else return fx_fail(path, ln, "unknown limit"); + } else if ((v = rest(line, "req"))) { snprintf(x->req, sizeof x->req, "%s", v); x->has_req = 1; } + else if ((v = rest(line, "refuse"))) { + if (sscanf(v, "%d %31s off=%d", &x->refuse_code, x->refuse_name, &x->refuse_off) != 3) return fx_fail(path, ln, "refuse CODE NAME off=N"); + x->has_refuse = 1; + } else if ((v = rest(line, "stdout"))) { + s = v; + if (q_decode(&s, &x->out) || *s) return fx_fail(path, ln, "bad stdout string"); + x->has_out = 1; + } else if ((v = rest(line, "stderr_has"))) { + s = v; + if (q_decode(&s, &x->errhas) || *s || x->errhas.n == 0) return fx_fail(path, ln, "bad stderr_has string"); + x->has_errhas = 1; + } else if ((v = rest(line, "outfile"))) { + if (x->noutfile >= 4) return fx_fail(path, ln, "too many outfiles"); + FxFile *ff = &x->outfile[x->noutfile++]; + size_t k = strcspn(v, " "); + if (k == 0 || k >= sizeof ff->name || v[k] != ' ' || memchr(v, '/', k)) return fx_fail(path, ln, "bad outfile name"); + snprintf(ff->name, sizeof ff->name, "%.*s", (int)k, v); + s = v + k + 1; + if (q_decode(&s, &ff->content) || *s) return fx_fail(path, ln, "bad outfile content"); + } else if ((v = rest(line, "outlen"))) { + if (sscanf(v, "%63s %lld", x->outlen_name, &x->outlen) != 2 || strchr(x->outlen_name, '/')) return fx_fail(path, ln, "outlen NAME N"); + } else if ((v = rest(line, "status"))) { x->status = atoi(v); x->has_status = 1; } + else if ((v = rest(line, "stages"))) snprintf(x->stages, sizeof x->stages, "%s", v); + else if ((v = rest(line, "err"))) snprintf(x->errs, sizeof x->errs, "%s", v); + else return fx_fail(path, ln, "unknown key"); + } + fclose(f); + if (x) return fx_fail(path, ln, "last fixture has no end"); + if (!ABI_PIN[0]) return fx_fail(path, ln, "no abi pin line"); + if (NFX == 0) return fx_fail(path, ln, "no fixtures"); + return 0; +} + +/* ---------------- observation shared between the run child and the runner ---------------- */ + +typedef struct { + char line[2048]; /* request rendered in the vectors grammar, without the "P " prefix */ + char wire[65]; /* sha256 of the 7.3 wire form */ + char stages[512]; /* stage categories, comma separated */ + char err[32]; /* pipeline platform error name */ +} ObsPipe; + +typedef struct { + int npipes, overflow, bad_out_cell; + ObsPipe p[MAXP]; + int refused; + uint64_t refuse_code, refuse_off; + int fds_before, fds_after; + unsigned long calls_native, calls_interp; /* core entry calls by mode: proves which backend ran */ + int setup_failed; + char setup_why[256]; + int not_run; /* the fixture cannot run in this build/environment (reason in setup_why) */ +} Obs; + +static Obs *OBS; + +static const char *e_name(int e) +{ + static const char *const n[] = {"OK", "DENIED", "REVOKED", "STALE", "NOT_FOUND", "UNAVAILABLE", "INVALID_ARG", "LIMIT", + "INTERRUPTED", "BROKEN_PIPE", "IO", "OUTCOME_UNKNOWN", "CAP_DOMAIN_MISMATCH", + "CAP_GEN_NARROW", "NOT_SUPPORTED", "WOULD_BLOCK", "PARTIAL_LAUNCH"}; + return e >= 0 && e <= 16 ? n[e] : "IO"; /* unknown codes are IO, never OK (ABI 8.6) */ +} + +/* README "Stage categories": the portable name of one command's end. */ +static void stage_cat(const OshCmdResult *c, char *o, size_t cap) +{ + switch (c->outcome) { + case OSH_OUT_COMPLETED: + if (c->termsig == SIGPIPE) snprintf(o, cap, "broken_pipe"); + else if (c->termsig == SIGINT) snprintf(o, cap, "interrupted"); + else if (c->termsig == SIGXFSZ) snprintf(o, cap, "limit"); + else if (c->termsig) snprintf(o, cap, "signal:%d", c->termsig); /* Linux-only */ + else if (c->status == 0) snprintf(o, cap, "ok"); + else snprintf(o, cap, "exit:%d", c->status); + break; + case OSH_OUT_CANCELLED: snprintf(o, cap, "cancelled"); break; + case OSH_OUT_NOT_STARTED: snprintf(o, cap, "not_started"); break; + case OSH_OUT_FAILED_NO_EFFECT: + if (c->err == OSH_E_NOT_FOUND) snprintf(o, cap, "not_found"); + else if (c->err == OSH_E_DENIED || c->err == OSH_E_REVOKED || c->err == OSH_E_STALE || c->err == OSH_E_CAP_DOMAIN_MISMATCH || + c->err == OSH_E_CAP_GEN_NARROW) + snprintf(o, cap, "refused:%s", e_name(c->err)); + else snprintf(o, cap, "failed:%s", e_name(c->err)); + break; + default: snprintf(o, cap, "unknown"); break; + } +} + +static void put_le(unsigned char *p, uint64_t v, int n) +{ + for (int i = 0; i < n; i++) p[i] = (unsigned char)(v >> (8 * i)); +} + +static void on_pipeline(void *ctx, const uint64_t *rec, size_t nrec, const uint64_t *out, size_t nout, const OshResult *res) +{ + (void)ctx; + if (OBS->npipes >= MAXP) { OBS->overflow = 1; return; } + ObsPipe *p = &OBS->p[OBS->npipes++]; + unsigned char ob[OSH_OUT_CAP]; + size_t no = nout <= OSH_OUT_CAP ? nout : OSH_OUT_CAP; + for (size_t i = 0; i < no; i++) { + if (out[i] > 255) OBS->bad_out_cell = 1; + ob[i] = (unsigned char)out[i]; + } + /* wire form, ABI 7.3: {magic u32 "OSHQ", abi_version u16, kind u16 = 1, total_len u32, reserved u32}, cells as u64 LE, + * then out_used bytes of OUT (UNVERIFIED: one byte per OUT cell). */ + size_t total = 16 + 8 * nrec + no; + unsigned char *w = malloc(total); + put_le(w, 0x4F534851u, 4); + put_le(w + 4, 1, 2); + put_le(w + 6, 1, 2); + put_le(w + 8, (uint64_t)total, 4); + put_le(w + 12, 0, 4); + for (size_t i = 0; i < nrec; i++) put_le(w + 16 + 8 * i, rec[i], 8); + memcpy(w + 16 + 8 * nrec, ob, no); + sha_hex(w, total, p->wire); + free(w); + /* render (vectors/README grammar; " direct" when flags bit 0 is set, UNVERIFIED extension) */ + char *L = p->line; + size_t o = 0, cap = sizeof p->line; + o += (size_t)snprintf(L, cap, "conn=%llu%s", (unsigned long long)rec[2], (rec[3] & OSH_REQ_FLAG_DIRECT) ? " direct" : ""); + uint64_t nc = rec[1]; + for (uint64_t c = 0; c < nc && OSH_REQ_HDR_CELLS + (c + 1) * OSH_CMD_CELLS <= nrec && o + 64 < cap; c++) { + const uint64_t *b = rec + OSH_REQ_HDR_CELLS + c * OSH_CMD_CELLS; + o += (size_t)snprintf(L + o, cap - o, " | C%llu b=%llu argv=", (unsigned long long)c, (unsigned long long)b[3]); + for (uint64_t k = 0; k < b[0] && k < OSH_MAX_ARGV; k++) { + if (k && o + 2 < cap) L[o++] = ','; + uint64_t off = b[4 + 2 * k], len = b[5 + 2 * k]; + if (off + len <= no) q_encode(L, &o, cap, ob + off, (size_t)len); + } + o += (size_t)snprintf(L + o, cap - o, " as="); + for (uint64_t k = 0; k < b[1] && k < OSH_MAX_ASSIGN; k++) { + const uint64_t *e = b + 68 + 4 * k; + if (k && o + 2 < cap) L[o++] = ','; + if (e[0] + e[1] <= no && o + e[1] + 2 < cap) { memcpy(L + o, ob + e[0], (size_t)e[1]); o += (size_t)e[1]; } + if (o + 2 < cap) L[o++] = '='; + if (e[2] + e[3] <= no) q_encode(L, &o, cap, ob + e[2], (size_t)e[3]); + } + o += (size_t)snprintf(L + o, cap - o, " rd="); + for (uint64_t k = 0; k < b[2] && k < OSH_MAX_REDIR; k++) { + const uint64_t *e = b + 132 + 4 * k; + static const char *const kn[] = {"?", "IN", "OUT", "APPEND", "DUP"}; + if (k && o + 2 < cap) L[o++] = ','; + o += (size_t)snprintf(L + o, cap - o, "%s%llu:", kn[e[0] < 5 ? e[0] : 0], (unsigned long long)e[1]); + if (e[0] == OSH_R_DUP) o += (size_t)snprintf(L + o, cap - o, "%llu", (unsigned long long)e[2]); + else if (e[2] + e[3] <= no) q_encode(L, &o, cap, ob + e[2], (size_t)e[3]); + } + } + if (o >= cap) o = cap - 1; + L[o] = 0; + size_t so = 0; + for (int i = 0; i < res->ncmds && i < OSH_MAX_CMDS; i++) { + char c[48]; + stage_cat(&res->cmd[i], c, sizeof c); + so += (size_t)snprintf(p->stages + so, sizeof p->stages - so, "%s%s", i ? "," : "", c); + } + snprintf(p->err, sizeof p->err, "%s", e_name(res->err)); +} + +static void on_refusal(void *ctx, uint64_t code, uint64_t off) +{ + (void)ctx; + OBS->refused = 1; + OBS->refuse_code = code; + OBS->refuse_off = off; +} + +/* ---------------- fault injection (run child only) ---------------- */ + +static const char *g_revoke; +static int effect_revoke(void *ctx, const OshBinding *b, int op, const char *path) +{ + (void)ctx; + (void)b; + const char *base = strrchr(path, '/'); + base = base ? base + 1 : path; + return op == OSH_OP_SPAWN && g_revoke && strcmp(base, g_revoke) == 0 ? OSH_E_REVOKED : OSH_E_OK; +} + +static void after_launch_interrupt(void *ctx) +{ + (void)ctx; + kill(0, SIGINT); /* the run child leads its own group, so this is a terminal ^C to the shell and its pipeline only */ +} + +static void after_launch_stop_first(void *ctx) +{ + (void)ctx; + char p[96], b[512]; + snprintf(p, sizeof p, "/proc/self/task/%ld/children", (long)syscall(SYS_gettid)); + int fd = open(p, O_RDONLY | O_CLOEXEC); + ssize_t n = fd >= 0 ? read(fd, b, sizeof b - 1) : -1; + if (fd >= 0) close(fd); + if (n <= 0) { OBS->not_run = 1; snprintf(OBS->setup_why, sizeof OBS->setup_why, "no /proc children list"); kill(0, SIGINT); return; } + b[n] = 0; + pid_t first = (pid_t)atol(b); + if (first > 0) kill(first, SIGSTOP); +} + +static void after_launch_leak_fd(void *ctx) +{ + (void)ctx; + (void)open("/dev/null", O_RDONLY); /* deliberately never closed */ +} + +static void after_launch_leak_process(void *ctx) +{ + (void)ctx; + if (fork() == 0) { /* stays in the pipeline group, never reaped by the shell */ + sleep(30); + _exit(0); + } +} + +static int count_fds(void) +{ + DIR *d = opendir("/proc/self/fd"); + if (!d) return -1; + int n = 0; + struct dirent *e; + while ((e = readdir(d))) if (e->d_name[0] != '.') n++; + closedir(d); + return n - 1; /* the directory's own descriptor */ +} + +static int lowest_free_fd(void) +{ + for (int fd = 0; fd < 4096; fd++) + if (fcntl(fd, F_GETFD) < 0 && errno == EBADF) return fd; + return -1; +} + +static int write_file(const char *dir, const char *name, const Bytes *b) +{ + char p[1024]; + snprintf(p, sizeof p, "%s/%s", dir, name); + int fd = open(p, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644); + if (fd < 0) return -1; + ssize_t w = b->n ? write(fd, b->p, b->n) : 0; + close(fd); + return w == (ssize_t)b->n ? 0 : -1; +} + +static void setup_fail(const char *why) +{ + OBS->setup_failed = 1; + snprintf(OBS->setup_why, sizeof OBS->setup_why, "%s: %s", why, strerror(errno)); + _exit(99); +} + +/* The run child: fresh group, private directory, toy PATH, faults, limits, then the real shell core. Never returns. */ +static void run_child(const Fixture *x, int native, const char *dir, const char *bindir) +{ + setpgid(0, 0); + /* The run child stands in for a shell started from a terminal: SIGINT and SIGQUIT at their defaults. + * A harness started under nohup or `cmd &` (the Spark judge, a CI runner) inherits them as ignored; + * osh then rightly keeps them ignored in its children (POSIX), the stalled stages of f25 never see the + * ^C and the fixture times out. Reset them so the fixture's precondition holds wherever it runs. */ + signal(SIGINT, SIG_DFL); + signal(SIGQUIT, SIG_DFL); + if (chdir(dir) != 0) setup_fail("chdir"); + for (int i = 0; i < x->nsetup; i++) + if (write_file(dir, x->setup[i].name, &x->setup[i].content)) setup_fail("fixture file"); + Bytes none = {NULL, 0}; + if (write_file(dir, ".stdin", x->has_in ? &x->in : &none)) setup_fail("stdin file"); + int fi = open(".stdin", O_RDONLY), fo = open(".stdout", O_WRONLY | O_CREAT | O_TRUNC, 0644), + fe = open(".stderr", O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (fi < 0 || fo < 0 || fe < 0) setup_fail("stdio files"); + if (dup2(fi, 0) < 0 || dup2(fo, 1) < 0 || dup2(fe, 2) < 0) setup_fail("dup2"); + close(fi); close(fo); close(fe); + static char pathv[1100]; + snprintf(pathv, sizeof pathv, "PATH=%s", bindir); + char *envp[] = {pathv, "LC_ALL=C", NULL}; + OshShell *sh = calloc(1, sizeof *sh); + if (!sh || osh_shell_init(sh, envp, native, 0, "osh", NULL, 0) != 0) setup_fail("osh_shell_init"); + sh->diff.on_pipeline = on_pipeline; + sh->diff.on_refusal = on_refusal; + if (x->fault == F_REVOKE_SPAWN) { g_revoke = x->fault_name; sh->s.effect_hook = effect_revoke; } + if (x->fault == F_INTERRUPT) sh->s.after_launch_hook = after_launch_interrupt; + if (x->fault == F_STOP_FIRST) sh->s.after_launch_hook = after_launch_stop_first; + if (x->fault == F_LAUNCH_FAIL) sh->s.fail_fork_at = x->fault_arg; + if (x->fault == F_MUTANT_LEAK_FD) sh->s.after_launch_hook = after_launch_leak_fd; + if (x->fault == F_MUTANT_LEAK_PROCESS) sh->s.after_launch_hook = after_launch_leak_process; + + OshBuilder *b = NULL; + if (x->direct) { /* ABI section 11, the sequence osh_submit_argv uses, for ncmds 1 to 8 */ + b = malloc(sizeof *b); + if (!b) setup_fail("builder"); + osh_rb_init(b, OSH_REQ_FLAG_DIRECT, OSH_CONN_NONE); + for (int c = 0; c < x->dncmds; c++) { + osh_rb_cmd(b, OSH_B_NONE); + for (int k = 0; k < x->dnargv[c]; k++) { + char *a = strndup((const char *)x->dargv[c][k].p, x->dargv[c][k].n); + osh_rb_arg(b, a); + free(a); + } + } + osh_rb_seal(b); + if (b->err) setup_fail("direct record overflow"); + } + OBS->fds_before = count_fds(); + struct rlimit rl; + if (x->limit == L_NOFILE_TIGHT) { + int lf = lowest_free_fd(); /* exactly one free descriptor below the limit: pipe2 needs two */ + rl.rlim_cur = rl.rlim_max = (rlim_t)lf + 1; + if (lf < 0 || setrlimit(RLIMIT_NOFILE, &rl) != 0) setup_fail("RLIMIT_NOFILE"); + } else if (x->limit == L_NPROC) { + if (geteuid() == 0) { OBS->not_run = 1; snprintf(OBS->setup_why, sizeof OBS->setup_why, "RLIMIT_NPROC does not bind root"); _exit(98); } + rl.rlim_cur = rl.rlim_max = (rlim_t)x->limit_arg; + if (setrlimit(RLIMIT_NPROC, &rl) != 0) setup_fail("RLIMIT_NPROC"); + } else if (x->limit == L_FSIZE) { + rl.rlim_cur = rl.rlim_max = (rlim_t)x->limit_arg; + if (setrlimit(RLIMIT_FSIZE, &rl) != 0) setup_fail("RLIMIT_FSIZE"); + } else if (x->limit == L_AS) { + if (OSH_DIFF_ASAN) { OBS->not_run = 1; snprintf(OBS->setup_why, sizeof OBS->setup_why, "RLIMIT_AS cannot apply under ASan"); _exit(98); } + rl.rlim_cur = rl.rlim_max = (rlim_t)x->limit_arg; + if (setrlimit(RLIMIT_AS, &rl) != 0) setup_fail("RLIMIT_AS"); + } + int st; + if (x->direct) { + OshResult res; + st = osh_exec_record(&sh->s, b->rec, osh_rb_cells(b), b->out, b->out_used, &res); + on_pipeline(NULL, b->rec, osh_rb_cells(b), b->out, b->out_used, &res); + free(b); + } else { + st = osh_shell_run_string(sh, (const char *)x->text.p, x->text.n); + } + if (x->limit == L_NOFILE_TIGHT) { /* lift it again so the count below can open /proc/self/fd */ + rl.rlim_cur = rl.rlim_max = 1024; + setrlimit(RLIMIT_NOFILE, &rl); + } + OBS->fds_after = count_fds(); + OBS->calls_native = sh->core.calls_native; + OBS->calls_interp = sh->core.calls_interp; + osh_shell_free(sh); + free(sh); + fflush(NULL); + exit(st & 255); +} + +/* ---------------- one host run ---------------- */ + +typedef struct { + int not_run, fail; + char why[512]; + char norm[LINE]; /* normalized result line fields after "OSHDIFF vec " */ +} Verdict; + +static void vfail(Verdict *v, const char *fmt, ...) +{ + if (v->fail) return; /* first failure is the reason shown */ + v->fail = 1; + va_list ap; + va_start(ap, fmt); + vsnprintf(v->why, sizeof v->why, fmt, ap); + va_end(ap); +} + +static Bytes slurp(const char *dir, const char *name, int *ok) +{ + char p[1024]; + snprintf(p, sizeof p, "%s/%s", dir, name); + Bytes b = {NULL, 0}; + *ok = 0; + FILE *f = fopen(p, "rb"); + if (!f) return b; + size_t cap = 1 << 16; + b.p = malloc(cap); + size_t r; + while ((r = fread(b.p + b.n, 1, cap - b.n, f)) > 0) { + b.n += r; + if (b.n == cap) { cap *= 2; b.p = realloc(b.p, cap); } + } + fclose(f); + *ok = 1; + return b; +} + +static void rm_tree(const char *dir) +{ + DIR *d = opendir(dir); + if (!d) return; + struct dirent *e; + char p[1024]; + while ((e = readdir(d))) { + if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0) continue; + snprintf(p, sizeof p, "%s/%s", dir, e->d_name); + unlink(p); + } + closedir(d); + rmdir(dir); +} + +static void host_run(const Fixture *x, int native, const char *root, const char *bindir, Verdict *v) +{ + memset(v, 0, sizeof *v); + char dir[1024]; + snprintf(dir, sizeof dir, "%s/%s.%s", root, x->id, native ? "native" : "interp"); + if (mkdir(dir, 0700) != 0) { vfail(v, "mkdir %s: %s", dir, strerror(errno)); return; } + memset(OBS, 0, sizeof *OBS); + fflush(NULL); + pid_t pid = fork(); + if (pid < 0) { vfail(v, "fork: %s", strerror(errno)); return; } + if (pid == 0) run_child(x, native, dir, bindir); + setpgid(pid, pid); + int st = 0, done = 0; + struct timespec t0, now, nap = {0, 2000000}; + clock_gettime(CLOCK_MONOTONIC, &t0); + for (;;) { + pid_t r = waitpid(pid, &st, WNOHANG); + if (r == pid) { done = 1; break; } + if (r < 0 && errno != EINTR) break; + clock_gettime(CLOCK_MONOTONIC, &now); + if (now.tv_sec - t0.tv_sec > RUN_TIMEOUT_S) break; + nanosleep(&nap, NULL); + } + if (!done) { + kill(-pid, SIGKILL); + waitpid(pid, &st, 0); + vfail(v, "timeout after %d s (pipeline group killed)", RUN_TIMEOUT_S); + rm_tree(dir); + return; + } + /* cleanup (ABI 12): nothing of the pipeline may outlive the shell's return */ + int leftover = kill(-pid, 0) == 0; + if (leftover) { kill(-pid, SIGKILL); usleep(20000); } + if (OBS->not_run) { v->not_run = 1; snprintf(v->why, sizeof v->why, "%s", OBS->setup_why); rm_tree(dir); return; } + if (OBS->setup_failed) { vfail(v, "setup: %s", OBS->setup_why); rm_tree(dir); return; } + if (!WIFEXITED(st)) { vfail(v, "run child died of signal %d", WTERMSIG(st)); rm_tree(dir); return; } + int status = WEXITSTATUS(st); + int ok1, ok2; + Bytes out = slurp(dir, ".stdout", &ok1), err = slurp(dir, ".stderr", &ok2); + if (!ok1 || !ok2) vfail(v, "stdout/stderr capture missing"); + if (leftover) vfail(v, "a process of the pipeline outlived the shell (cleanup, ABI 12)"); + if (OBS->fds_before < 0 || OBS->fds_after != OBS->fds_before) + vfail(v, "descriptor leak: %d open before, %d after (cleanup, ABI 12)", OBS->fds_before, OBS->fds_after); + /* text runs must call the core only in the requested backend; direct runs never call it (ABI 11) */ + if (x->direct ? (OBS->calls_native || OBS->calls_interp) + : native ? (!OBS->calls_native || OBS->calls_interp) : (!OBS->calls_interp || OBS->calls_native)) + vfail(v, "core backend: %lu native and %lu interpreter calls in a %s %s run", OBS->calls_native, OBS->calls_interp, + x->direct ? "direct" : "text", native ? "native" : "interpreter"); + if (OBS->overflow) vfail(v, "more than %d pipelines", MAXP); + if (OBS->bad_out_cell) vfail(v, "an OUT cell above 255"); + + /* expectations */ + char got[LINE]; + size_t o = 0; + got[0] = 0; + for (int i = 0; i < OBS->npipes && o + 64 < sizeof got; i++) + o += (size_t)snprintf(got + o, sizeof got - o, "%sP%d %s", i ? " | " : "", i, OBS->p[i].line); + if (x->has_req && strcmp(got, x->req) != 0) vfail(v, "request records differ:\n want %s\n got %s", x->req, got); + if (!x->has_req && OBS->npipes) vfail(v, "a request was produced but the fixture expects none: %s", got); + if (x->has_refuse != OBS->refused || (x->has_refuse && ((int)OBS->refuse_code != x->refuse_code || (int)OBS->refuse_off != x->refuse_off))) + vfail(v, "refusal: want %s %d off=%d, got %s %llu off=%llu", x->has_refuse ? "refuse" : "none", x->refuse_code, x->refuse_off, + OBS->refused ? "refuse" : "none", (unsigned long long)OBS->refuse_code, (unsigned long long)OBS->refuse_off); + if (x->has_refuse && x->refuse_name[0]) { + const char *nm = osh_code_name((unsigned)x->refuse_code); + if (!nm || strcmp(nm, x->refuse_name) != 0) vfail(v, "refusal name %s is not the core's name for %d", x->refuse_name, x->refuse_code); + } + if (out.n != x->out.n || (out.n && memcmp(out.p, x->out.p, out.n) != 0)) { + char a[512], b[512]; + size_t oa = 0, ob = 0; + q_encode(a, &oa, sizeof a, x->out.p, x->out.n < 100 ? x->out.n : 100); + q_encode(b, &ob, sizeof b, out.p, out.n < 100 ? out.n : 100); + vfail(v, "stdout: want %zu bytes %s, got %zu bytes %s", x->out.n, a, out.n, b); + } + if (x->has_errhas && !memmem(err.p, err.n, x->errhas.p, x->errhas.n)) vfail(v, "stderr lacks the expected diagnostic"); + if (status != x->status) vfail(v, "status: want %d, got %d", x->status, status); + char gst[LINE], ger[1024]; + size_t a = 0, b = 0; + gst[0] = ger[0] = 0; + for (int i = 0; i < OBS->npipes; i++) { + a += (size_t)snprintf(gst + a, sizeof gst - a, "%sP%d=%s", i ? " " : "", i, OBS->p[i].stages); + b += (size_t)snprintf(ger + b, sizeof ger - b, "%sP%d=%s", i ? " " : "", i, OBS->p[i].err); + } + if (strcmp(gst, x->stages) != 0) vfail(v, "stages: want '%s', got '%s'", x->stages, gst); + if (strcmp(ger, x->errs) != 0) vfail(v, "platform errors: want '%s', got '%s'", x->errs, ger); + char files[1024]; + size_t fo = 0; + files[0] = 0; + for (int i = 0; i < x->noutfile; i++) { + int okf; + Bytes fb = slurp(dir, x->outfile[i].name, &okf); + if (!okf) vfail(v, "output file %s missing", x->outfile[i].name); + else if (fb.n != x->outfile[i].content.n || (fb.n && memcmp(fb.p, x->outfile[i].content.p, fb.n) != 0)) + vfail(v, "output file %s: content differs (%zu bytes)", x->outfile[i].name, fb.n); + char h[65]; + sha_hex(fb.p ? fb.p : (unsigned char *)"", fb.n, h); + fo += (size_t)snprintf(files + fo, sizeof files - fo, "%s%s:%zu:%s", fo ? "," : "", x->outfile[i].name, fb.n, h); + free(fb.p); + } + if (x->outlen_name[0]) { + int okf; + Bytes fb = slurp(dir, x->outlen_name, &okf); + if (!okf || (long long)fb.n != x->outlen) vfail(v, "output file %s: want %lld bytes, got %zu", x->outlen_name, x->outlen, fb.n); + char h[65]; + sha_hex(fb.p ? fb.p : (unsigned char *)"", fb.n, h); + fo += (size_t)snprintf(files + fo, sizeof files - fo, "%s%s:%zu:%s", fo ? "," : "", x->outlen_name, fb.n, h); + free(fb.p); + } + + /* normalized line (README "Result file"): the fields every adapter reports */ + char outh[65]; + sha_hex(out.p ? out.p : (unsigned char *)"", out.n, outh); + size_t n = 0; + n += (size_t)snprintf(v->norm + n, sizeof v->norm - n, "req="); + for (int i = 0; i < OBS->npipes; i++) n += (size_t)snprintf(v->norm + n, sizeof v->norm - n, "%s%s", i ? "," : "", OBS->p[i].wire); + if (!OBS->npipes) n += (size_t)snprintf(v->norm + n, sizeof v->norm - n, "none"); + if (OBS->refused) n += (size_t)snprintf(v->norm + n, sizeof v->norm - n, " refuse=%llu:%llu", (unsigned long long)OBS->refuse_code, (unsigned long long)OBS->refuse_off); + else n += (size_t)snprintf(v->norm + n, sizeof v->norm - n, " refuse=none"); + n += (size_t)snprintf(v->norm + n, sizeof v->norm - n, " status=%d stages=", status); + for (int i = 0; i < OBS->npipes; i++) n += (size_t)snprintf(v->norm + n, sizeof v->norm - n, "%sP%d=%s", i ? ";" : "", i, OBS->p[i].stages); + if (!OBS->npipes) n += (size_t)snprintf(v->norm + n, sizeof v->norm - n, "none"); + n += (size_t)snprintf(v->norm + n, sizeof v->norm - n, " err="); + for (int i = 0; i < OBS->npipes; i++) n += (size_t)snprintf(v->norm + n, sizeof v->norm - n, "%sP%d=%s", i ? ";" : "", i, OBS->p[i].err); + if (!OBS->npipes) n += (size_t)snprintf(v->norm + n, sizeof v->norm - n, "none"); + snprintf(v->norm + n, sizeof v->norm - n, " out=%zu:%s files=%s", out.n, outh, fo ? files : "none"); + free(out.p); + free(err.p); + rm_tree(dir); +} + +/* ---------------- result files (README "Result file") ---------------- */ + +static const char *const PIN_KEYS[] = {"adapter", "aienos", "omega", "core", "abi", "cc", "harness", "fixtures"}; +#define NPIN 8 + +typedef struct { + int present, valid; + char why[256]; + char pin[NPIN][300]; + char *vec[MAXFX]; /* normalized fields per fixture index, NULL = no line */ +} ResultFile; + +static int pin_index(const char *k, size_t n) +{ + for (int i = 0; i < NPIN; i++) + if (strlen(PIN_KEYS[i]) == n && strncmp(PIN_KEYS[i], k, n) == 0) return i; + return -1; +} + +static int fx_index(const char *id) +{ + for (int i = 0; i < NFX; i++) + if (strcmp(FX[i].id, id) == 0) return i; + return -1; +} + +static void rf_invalid(ResultFile *r, const char *fmt, const char *arg) +{ + if (!r->valid) return; + r->valid = 0; + snprintf(r->why, sizeof r->why, fmt, arg); +} + +/* Only lines starting with "OSHDIFF" count (after an optional CR at the end), so a raw QEMU serial log is accepted. */ +static void load_results(const char *path, ResultFile *r) +{ + memset(r, 0, sizeof *r); + r->present = 1; + r->valid = 1; + FILE *f = fopen(path, "rb"); + if (!f) { rf_invalid(r, "cannot open %s", path); return; } + static char line[LINE]; + int header = 0, pins = 0, ended = 0, nvec = 0; + while (fgets(line, sizeof line, f)) { + size_t n = strlen(line); + while (n && (line[n - 1] == '\n' || line[n - 1] == '\r')) line[--n] = 0; + if (strncmp(line, "OSHDIFF", 7) != 0) continue; + if (ended) { rf_invalid(r, "%s", "an OSHDIFF line after the end line"); continue; } + if (strcmp(line, "OSHDIFF-RESULT 1") == 0) { + if (header) rf_invalid(r, "%s", "two header lines"); + header = 1; + continue; + } + if (!header) { rf_invalid(r, "%s", "first OSHDIFF line is not \"OSHDIFF-RESULT 1\""); continue; } + if (strncmp(line, "OSHDIFF pin ", 12) == 0) { + if (pins) rf_invalid(r, "%s", "two pin lines"); + pins = 1; + for (char *s = line + 12; *s;) { + size_t k = strcspn(s, "="); + char *val = s + k + 1; + size_t vl = strcspn(val, " "); + int pi = s[k] == '=' ? pin_index(s, k) : -1; + if (pi < 0) { rf_invalid(r, "unknown pin key in: %s", s); break; } + if (vl == 0 || vl >= sizeof r->pin[pi]) { rf_invalid(r, "empty or long pin value for %s", PIN_KEYS[pi]); break; } + snprintf(r->pin[pi], sizeof r->pin[pi], "%.*s", (int)vl, val); + s = val + vl; + while (*s == ' ') s++; + } + continue; + } + if (strncmp(line, "OSHDIFF vec ", 12) == 0) { + char *id = line + 12; + size_t k = strcspn(id, " "); + if (id[k] != ' ') { rf_invalid(r, "%s", "vec line without fields"); continue; } + id[k] = 0; + int i = fx_index(id); + if (i < 0) { rf_invalid(r, "unknown fixture %s", id); continue; } + if (r->vec[i]) { rf_invalid(r, "duplicate line for %s", id); continue; } + r->vec[i] = strdup(id + k + 1); + nvec++; + continue; + } + if (strncmp(line, "OSHDIFF end n=", 14) == 0) { + char *e; + long want = strtol(line + 14, &e, 10); + if (*e || want != nvec) rf_invalid(r, "%s", "end line count does not match the vec lines"); + ended = 1; + continue; + } + rf_invalid(r, "unknown OSHDIFF line: %s", line); + } + fclose(f); + if (!header) rf_invalid(r, "%s", "no \"OSHDIFF-RESULT 1\" header"); + if (!pins) rf_invalid(r, "%s", "no pin line"); + if (!ended) rf_invalid(r, "%s", "no end line (truncated log?)"); + for (int i = 0; i < NPIN && r->valid; i++) + if (!r->pin[i][0]) rf_invalid(r, "pin %s missing", PIN_KEYS[i]); +} + +/* ---------------- main ---------------- */ + +static void core_digest(void) +{ + sha256_ctx c; + sha256_init(&c); + const unsigned char *u[3] = {osh_unit_lex, osh_unit_parse, osh_unit_expand}; + size_t n[3] = {osh_unit_lex_len, osh_unit_parse_len, osh_unit_expand_len}; + for (int i = 0; i < 3; i++) { + unsigned char l[8]; + put_le(l, n[i], 8); + sha256_update(&c, l, 8); + sha256_update(&c, u[i], n[i]); + } + uint8_t d[32]; + sha256_final(&c, d); + hex32(d, CORE_SHA); +} + +static int make_bindir(const char *toy, const char *bindir) +{ + if (mkdir(bindir, 0700) != 0) return -1; + for (size_t i = 0; i < sizeof TOYS / sizeof TOYS[0]; i++) { + char p[2400]; + snprintf(p, sizeof p, "%s/%s", bindir, TOYS[i]); + if (symlink(toy, p) != 0) return -1; + } + return 0; +} + +int main(int argc, char **argv) +{ + const char *native_path = NULL, *emit_path = NULL, *host_path = NULL; + int require_native = 0, strict_host = 0; + int ai = 1; + for (; ai < argc && strncmp(argv[ai], "--", 2) == 0; ai++) { + if (strcmp(argv[ai], "--native") == 0 && ai + 1 < argc) native_path = argv[++ai]; + else if (strcmp(argv[ai], "--emit") == 0 && ai + 1 < argc) emit_path = argv[++ai]; + else if (strcmp(argv[ai], "--host-results") == 0 && ai + 1 < argc) host_path = argv[++ai]; + else if (strcmp(argv[ai], "--require-native") == 0) require_native = 1; + else if (strcmp(argv[ai], "--strict-host") == 0) strict_host = 1; + else if (strcmp(argv[ai], "--pin") == 0 && ai + 1 < argc) { + const char *p = argv[++ai]; + if (strncmp(p, "omega=", 6) == 0) PIN_OMEGA = p + 6; + else if (strncmp(p, "cc=", 3) == 0) PIN_CC = p + 3; + else if (strncmp(p, "harness=", 8) == 0) PIN_HARNESS = p + 8; + else if (strncmp(p, "aienos=", 7) == 0) PIN_AIENOS_LOCK = p + 7; + else { fprintf(stderr, "unknown pin %s\n", p); return 2; } + if (!*strchr(p, 0x3d) || strchr(p, 0x20)) { fprintf(stderr, "pin values are one word: %s\n", p); return 2; } + } + else { fprintf(stderr, "unknown option %s\n", argv[ai]); return 2; } + } + if (argc - ai != 2) { + fprintf(stderr, "usage: osh_diff [--native FILE] [--require-native] [--emit FILE] [--host-results FILE] [--strict-host] TOYBIN FIXTURES\n"); + return 2; + } + const char *toy = argv[ai], *fixtures = argv[ai + 1]; + if (toy[0] != '/') { fprintf(stderr, "TOYBIN must be an absolute path\n"); return 2; } + if (load_fixtures(fixtures) != 0) { printf("OSH_DIFF_FAIL\n"); return 1; } + core_digest(); + OBS = mmap(NULL, sizeof *OBS, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); + if (OBS == MAP_FAILED) { perror("mmap"); return 1; } + + char root[] = "/tmp/osh_diff.XXXXXX"; + if (!mkdtemp(root)) { perror("mkdtemp"); return 1; } + char bindir[1100]; + snprintf(bindir, sizeof bindir, "%s/bin", root); + if (make_bindir(toy, bindir) != 0) { perror("toy bin directory"); return 1; } + + static Verdict hn[MAXFX], hi[MAXFX]; + ResultFile hostf, nat; + memset(&nat, 0, sizeof nat); + if (host_path) { + load_results(host_path, &hostf); + if (!hostf.valid) { printf("host results %s invalid: %s\nOSH_DIFF_COMPARE_ONLY_FAIL\n", host_path, hostf.why); return 1; } + for (int i = 0; i < NFX; i++) { + memset(&hn[i], 0, sizeof hn[i]); + if (hostf.vec[i]) snprintf(hn[i].norm, sizeof hn[i].norm, "%s", hostf.vec[i]); + else { hn[i].not_run = 1; snprintf(hn[i].why, sizeof hn[i].why, "no line in host results"); } + hi[i] = hn[i]; + } + } else { + for (int i = 0; i < NFX; i++) { + host_run(&FX[i], 1, root, bindir, &hn[i]); + host_run(&FX[i], 0, root, bindir, &hi[i]); + if (!hn[i].fail && !hi[i].fail && !hn[i].not_run && !hi[i].not_run && strcmp(hn[i].norm, hi[i].norm) != 0) + vfail(&hi[i], "native and interpreter results differ:\n native %s\n interp %s", hn[i].norm, hi[i].norm); + } + } + rm_tree(bindir); + rmdir(root); + if (native_path) load_results(native_path, &nat); + + /* host pins (README "Pins") */ + char hpin[NPIN][300]; + snprintf(hpin[0], sizeof hpin[0], "linux-host"); + snprintf(hpin[1], sizeof hpin[1], "lock:%s", PIN_AIENOS_LOCK); + snprintf(hpin[2], sizeof hpin[2], "%s", PIN_OMEGA); + snprintf(hpin[3], sizeof hpin[3], "%s", CORE_SHA); + snprintf(hpin[4], sizeof hpin[4], "%s", ABI_PIN); + snprintf(hpin[5], sizeof hpin[5], "%s", PIN_CC); + snprintf(hpin[6], sizeof hpin[6], "%s", PIN_HARNESS); + snprintf(hpin[7], sizeof hpin[7], "%s", FIXTURES_SHA); + if (host_path) for (int i = 0; i < NPIN; i++) snprintf(hpin[i], sizeof hpin[i], "%s", hostf.pin[i]); + + if (nat.present && nat.valid) { + if (strcmp(nat.pin[0], "aienos-native") != 0) rf_invalid(&nat, "adapter is %s, not aienos-native", nat.pin[0]); + else if (strcmp(nat.pin[4], hpin[4]) != 0) rf_invalid(&nat, "abi pin %s differs from the host's", nat.pin[4]); + else if (strcmp(nat.pin[7], hpin[7]) != 0) rf_invalid(&nat, "fixtures digest %s differs from the corpus run here", nat.pin[7]); + else if (strcmp(nat.pin[3], hpin[3]) != 0) rf_invalid(&nat, "shell core digest %s differs: not the same compiled core", nat.pin[3]); + } + + printf("OSH cross-adapter pipeline differential (omega#568)%s\n", host_path ? " -- COMPARE ONLY, host results read from a file, not run" : ""); + printf("pins host: adapter=%s aienos=%s omega=%s\n core=%s\n abi=%s\n cc=%s harness=%s\n fixtures=%s\n", + hpin[0], hpin[1], hpin[2], hpin[3], hpin[4], hpin[5], hpin[6], hpin[7]); + if (!nat.present) printf("pins aienos: none supplied (no native result file)\n"); + else if (!nat.valid) printf("pins aienos: INVALID result file %s: %s\n", native_path, nat.why); + else printf("pins aienos: adapter=%s aienos=%s omega=%s cc=%s harness=%s\n", nat.pin[0], nat.pin[1], nat.pin[2], nat.pin[5], nat.pin[6]); + printf("%-28s %-8s %-11s %-11s %-8s %s\n", "fixture", "scope", "host-native", "host-interp", "aienos", "reason"); + + int hp = 0, hf = 0, hnr = 0, np = 0, nf = 0, nnr = 0; + for (int i = 0; i < NFX; i++) { + const Fixture *x = &FX[i]; + const char *c1 = hn[i].fail ? "FAIL" : hn[i].not_run ? "NOT_RUN" : "PASS"; + const char *c2 = hi[i].fail ? "FAIL" : hi[i].not_run ? "NOT_RUN" : "PASS"; + if (host_path) c1 = c2 = hn[i].not_run ? "NOT_RUN" : "(file)"; + const char *c3, *why3 = ""; + char nwhy[LINE]; + nwhy[0] = 0; + if (x->linux_only) { c3 = "NOT_RUN"; why3 = "platform-specific (linux)"; } + else if (!nat.present) { c3 = "NOT_RUN"; why3 = "no native result supplied"; } + else if (!nat.valid) { c3 = "FAIL"; why3 = "native result file invalid"; } + else if (!nat.vec[i]) { c3 = "NOT_RUN"; why3 = "no native line for this fixture"; } + else if (hn[i].fail || hn[i].not_run) { c3 = "FAIL"; why3 = "host reference did not pass; nothing to compare"; } + else if (strcmp(nat.vec[i], hn[i].norm) != 0) { + c3 = "FAIL"; + snprintf(nwhy, sizeof nwhy, "differs from host:\n host %s\n aienos %s", hn[i].norm, nat.vec[i]); + why3 = nwhy; + } else c3 = "PASS"; + const char *why = hn[i].fail || hn[i].not_run ? hn[i].why : hi[i].fail || hi[i].not_run ? hi[i].why : why3; + printf("%-28s %-8s %-11s %-11s %-8s %s\n", x->id, x->linux_only ? "linux" : "portable", c1, c2, c3, why); + if (!host_path) { + for (int k = 0; k < 2; k++) { + const Verdict *h = k ? &hi[i] : &hn[i]; + if (h->fail) hf++; + else if (h->not_run) hnr++; + else hp++; + } + } else if (hn[i].not_run) hnr++; + if (!strcmp(c3, "PASS")) np++; + else if (!strcmp(c3, "FAIL")) nf++; + else nnr++; + } + int portable_nr = 0; + for (int i = 0; i < NFX; i++) + if (!FX[i].linux_only && (!nat.present || (nat.valid && !nat.vec[i]))) portable_nr++; + printf("summary: host PASS %d FAIL %d NOT_RUN %d (two runs per fixture) | aienos PASS %d FAIL %d NOT_RUN %d (portable NOT_RUN %d)\n", + hp, hf, hnr, np, nf, nnr, portable_nr); + + if (emit_path && !host_path) { + FILE *e = fopen(emit_path, "w"); + if (!e) { perror(emit_path); return 1; } + fprintf(e, "OSHDIFF-RESULT 1\nOSHDIFF pin"); + for (int k = 0; k < NPIN; k++) fprintf(e, " %s=%s", PIN_KEYS[k], hpin[k]); + fprintf(e, "\n"); + int ne = 0; + for (int i = 0; i < NFX; i++) + if (!hn[i].fail && !hn[i].not_run) { fprintf(e, "OSHDIFF vec %s %s\n", FX[i].id, hn[i].norm); ne++; } + fprintf(e, "OSHDIFF end n=%d\n", ne); + fclose(e); + } + int bad = hf || nf || (strict_host && hnr) || (require_native && portable_nr); + if (require_native && portable_nr) printf("acceptance: %d portable fixtures have no native AIENOS result (NOT_RUN is never green)\n", portable_nr); + if (host_path) printf("%s\n", bad ? "OSH_DIFF_COMPARE_ONLY_FAIL" : "OSH_DIFF_COMPARE_ONLY_PASS"); + else printf("%s\n", bad ? "OSH_DIFF_FAIL" : "OSH_DIFF_PASS"); + return bad ? 1 : 0; +} diff --git a/tests/osh/diff/osh_toy.c b/tests/osh/diff/osh_toy.c new file mode 100644 index 000000000..2896f5c4e --- /dev/null +++ b/tests/osh/diff/osh_toy.c @@ -0,0 +1,134 @@ +/* + * osh_toy.c -- the toy stage operations of the cross-adapter differential corpus (omega#568, tests/osh/diff/README.md). + * One multicall program: the operation is the last path element of argv[0], so the runner links it into a private PATH + * directory under each name. The AIENOS native harness implements the same names as Omega-native ops with the same + * observable bytes (README, "Toy operations"); the semantics below are the contract, this file is only the Linux build. + * emit ARGS... each argument followed by a newline, on stdout + * args ARGS... "N\n" then "LEN:BYTES\n" per argument (argument boundaries, empty and non-ASCII bytes) + * pass copy stdin to stdout in reads of up to 4096 bytes + * trickle copy stdin to stdout one byte per read and one byte per write (short reads and writes) + * flood N write N bytes, byte i = 'a' + i % 26, in writes of up to 4093 bytes (partial writes looped) + * bytes write the 256 byte values 0..255 once + * take N read until N bytes or EOF, write them, exit 0 without reading more (consumer early exit) + * sink read stdin to EOF, print "n=COUNT sum=FNV1A32\n" (8 lower-case hex digits) + * status N exit with status N, no output + * oe write "O" to stdout, then "E" to stderr + * stall wait for a signal forever (cancellation fixtures) + * Not part of osh. No shell is involved anywhere. + */ +#include +#include +#include +#include +#include +#include + +static int put(int fd, const void *p, size_t n) +{ + const unsigned char *b = p; + while (n > 0) { + ssize_t w = write(fd, b, n); + if (w < 0 && errno == EINTR) continue; + if (w <= 0) return -1; + b += w; + n -= (size_t)w; + } + return 0; +} + +static long num(const char *s) +{ + char *e; + long v = strtol(s, &e, 10); + return (*s && !*e && v >= 0) ? v : -1; +} + +int main(int argc, char **argv) +{ + const char *op = strrchr(argv[0], '/'); + op = op ? op + 1 : argv[0]; + if (strcmp(op, "emit") == 0) { + for (int i = 1; i < argc; i++) + if (put(1, argv[i], strlen(argv[i])) || put(1, "\n", 1)) return 1; + return 0; + } + if (strcmp(op, "args") == 0) { + char h[32]; + int k = snprintf(h, sizeof h, "%d\n", argc - 1); + if (put(1, h, (size_t)k)) return 1; + for (int i = 1; i < argc; i++) { + k = snprintf(h, sizeof h, "%zu:", strlen(argv[i])); + if (put(1, h, (size_t)k) || put(1, argv[i], strlen(argv[i])) || put(1, "\n", 1)) return 1; + } + return 0; + } + if (strcmp(op, "pass") == 0 || strcmp(op, "trickle") == 0) { + size_t chunk = op[0] == 'p' ? 4096 : 1; + char b[4096]; + for (;;) { + ssize_t r = read(0, b, chunk); + if (r < 0 && errno == EINTR) continue; + if (r < 0) return 1; + if (r == 0) return 0; + for (ssize_t i = 0; i < r; i += (ssize_t)chunk) + if (put(1, b + i, (size_t)(r - i) < chunk ? (size_t)(r - i) : chunk)) return 1; + } + } + if (strcmp(op, "flood") == 0) { + long n = argc == 2 ? num(argv[1]) : -1; + if (n < 0) return 2; + char b[4093]; + long done = 0; + while (done < n) { + size_t k = (size_t)(n - done) < sizeof b ? (size_t)(n - done) : sizeof b; + for (size_t i = 0; i < k; i++) b[i] = (char)('a' + (done + (long)i) % 26); + if (put(1, b, k)) return 1; + done += (long)k; + } + return 0; + } + if (strcmp(op, "bytes") == 0) { + unsigned char b[256]; + for (int i = 0; i < 256; i++) b[i] = (unsigned char)i; + return put(1, b, sizeof b) ? 1 : 0; + } + if (strcmp(op, "take") == 0) { + long n = argc == 2 ? num(argv[1]) : -1; + if (n < 0 || n > 65536) return 2; + static char b[65536]; + long got = 0; + while (got < n) { + ssize_t r = read(0, b + got, (size_t)(n - got)); + if (r < 0 && errno == EINTR) continue; + if (r <= 0) break; + got += r; + } + return put(1, b, (size_t)got) ? 1 : 0; + } + if (strcmp(op, "sink") == 0) { + uint32_t h = 2166136261u; + unsigned long long n = 0; + unsigned char b[4096]; + for (;;) { + ssize_t r = read(0, b, sizeof b); + if (r < 0 && errno == EINTR) continue; + if (r < 0) return 1; + if (r == 0) break; + for (ssize_t i = 0; i < r; i++) h = (h ^ b[i]) * 16777619u; + n += (unsigned long long)r; + } + char o[64]; + int k = snprintf(o, sizeof o, "n=%llu sum=%08x\n", n, h); + return put(1, o, (size_t)k) ? 1 : 0; + } + if (strcmp(op, "status") == 0) { + long n = argc == 2 ? num(argv[1]) : -1; + return n < 0 || n > 255 ? 2 : (int)n; + } + if (strcmp(op, "oe") == 0) return put(1, "O", 1) || put(2, "E", 1) ? 1 : 0; + if (strcmp(op, "stall") == 0) { + for (;;) pause(); + } + fprintf(stderr, "osh_toy: unknown operation %s\n", op); + return 2; +} ```