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
GitHub aien-dev/omega-mv#151 (code only)
Apply it
git checkout -b patch-05e5946c40dea96a c77362242b13d7df127eeabec9eda75119f934cc curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/05e5946c40dea96a.patch | git am
Provenance
github-import 1
aien-dev/omega-mv
151
4b2688e7d8ea18387d7f12c96f1d524c2a1331d5
{"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":151,"url":"https://github.com/aien-dev/omega-mv/pull/151","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:32Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/.github/workflows/host-suites.yml b/.github/workflows/host-suites.yml index c189e7f29..739544f91 100644 --- a/.github/workflows/host-suites.yml +++ b/.github/workflows/host-suites.yml @@ -64,7 +64,8 @@ on: - 'src/train/**' - 'tests/train/**' - 'mk/train.mk' - # OSC-1 compiler slice + OSC-0B model (compiler job only; see its guard step) + # OSC-1 compiler slice + OSC-0B model (compiler job only; see its guard step; + # the job also runs on src/aarch64_* changes, already listed above: OSC-2 item 4) - 'src/compiler/**' - 'tests/compiler/**' - 'mk/compiler.mk' @@ -137,7 +138,7 @@ jobs: run: | if [ "${{ github.event_name }}" != "pull_request" ]; then echo "run=true" >> "$GITHUB_OUTPUT"; exit 0; fi if git diff --name-only "${{ github.event.pull_request.base.sha }}" HEAD \ - | grep -Eq '^(src/compiler/|tests/compiler/|mk/compiler\.mk$|src/sha256\.|\.github/workflows/host-suites\.yml$)'; then + | grep -Eq '^(src/compiler/|tests/compiler/|mk/compiler\.mk$|src/sha256\.|src/aarch64_|\.github/workflows/host-suites\.yml$)'; then echo "run=true" >> "$GITHUB_OUTPUT" else echo "run=false" >> "$GITHUB_OUTPUT"; echo "no OSC-1 compiler files changed; skipping" diff --git a/docs/osc/OSC-2-DESIGN.md b/docs/osc/OSC-2-DESIGN.md index 4436ee1c9..76fb0c868 100644 --- a/docs/osc/OSC-2-DESIGN.md +++ b/docs/osc/OSC-2-DESIGN.md @@ -416,3 +416,126 @@ New trap: TRAP ARENA_FULL = 11 (`OSC_TRAP_ARENA_FULL`, now `OSC_TRAP_MAX`). program's runtime behaviour satisfies the model.** - `make osc2-receipt ITEM=arenas` records the arena fuzz lines, the destruction order, the runtime model replay lines (plain and ASan) and the ARENA_FULL run count. + +## 4. Legacy AArch64 writer + +### 4.1 Statement + +`src/aarch64_encoder.c` is the instruction writer used by the existing Omega code +(`omega_program_realize`, the matvec emitters, `omega_self_host`, `omega_exec`, the +polyglot encoder, `rx_omega`, the M5/M6/M9/M14 gates). Up to OSC-1 it trimmed fields +silently (`rm & 0x1f`, `imm19 & 0x7FFFF`, `(uoff >> scale) & 0xFFF`, `simm9 & 0x1FF`, +`(shift / 16) & 3`, ...), which OSC-0 III.6 says must be hardened (OSC-1-DESIGN.md +section 9 left it untouched). OSC-2 item 4 takes option A (fix, not retire): + +- every emitter checks every field against its exact architectural range before it + encodes anything; +- an in-range call produces the same word as before: the encoding formulas are + unchanged and their masks are now no-ops; +- an out-of-range field is refused, never trimmed (4.3); +- the set of files that use the writer is frozen (4.4). New code uses the validating + encoder `src/compiler/osc_a64.c`. + +Option B (keep trimming for a caller that relies on it) was not needed: an audit of all +280 call sites found no caller that passes an out-of-range field. Every constant is in +range; computed values are either range-checked by the caller first +(`src/polyglot/omx_encoder.c`, its `resolve()` branch fix-ups), already cast to the +parameter type by the caller (`omega_self_host.c` instruction counts), or registers +X0..X17/XZR. The M6/M9/M14 gates and the host suites in 4.5 run through the strict writer +and pass unchanged. + +### 4.2 Ranges (normative) + +| Emitter | Field | Accepted | +|---|---|---| +| all | `rd` `rn` `rm` `rt` | 0..31 (31 is XZR or SP, as the instruction defines) | +| all with `sf` | `sf` | C `bool` (0 or 1 by type) | +| `movz` `movk` | `imm16` | 0..65535 (`uint16_t`) | +| `movz` `movk` | `shift` | 0, 16, 32, 48 when `sf` = 1; 0, 16 when `sf` = 0 | +| `b` | `imm26` (words) | -2^25 .. 2^25 - 1 | +| `b_cond` | `cond` | 0..15 | +| `b_cond` `cbz` `cbnz` | `imm19` (words) | -2^18 .. 2^18 - 1 | +| `adr` | `imm21` (bytes) | -2^20 .. 2^20 - 1 | +| `ldr_uoff` `str_uoff` | `uoff` (bytes) | multiple of 8 (`sf` = 1) or 4 (`sf` = 0), `uoff / size` <= 4095 | +| `ldrb_uoff` `strb_uoff` | `uoff` (bytes) | 0..4095 | +| `ldr_post` `str_post` `ldr_x_post` `str_x_post` | `simm9` | -256..255 | +| `subs_imm` | `imm12` | 0..4095 | + +`ret` has no fields. The "buffer full" result (-1, nothing written) is unchanged. + +### 4.3 Refusal path + +A refused call writes nothing and does not advance `*pos`. It prints exactly one line on +stderr, + +``` +A64_LEGACY_FIELD_OUT_OF_RANGE <emitter> <field>=<value> +``` + +(`<emitter>` is the C function name, e.g. `aarch64_emit_cbz`; `<field>` is the parameter +name; `<value>` is the received value in signed decimal), and then calls `abort()` +(SIGABRT). Abort is deliberate and deterministic: several existing callers ignore the +emitter's `int` result (for example the matvec emitters), and this item may not change +callers, so a soft error would silently drop an instruction and produce wrong code. A +refusal is a bug in the caller. + +### 4.4 No new callers + +`tests/compiler/legacy_a64_allowlist.txt` lists the 16 C files that include +`aarch64_encoder.h` or name an `aarch64_emit_*` function (14 callers, the writer itself +and its header, plus this item's test). `tests/compiler/legacy_a64_callers.sh` scans every +tracked and untracked (not ignored) `*.c` / `*.h` file and requires the set to equal the +list: + +- `A64_LEGACY_NEW_CALLER <file>`: a file not on the list uses the writer; +- `A64_LEGACY_ALLOWLIST_STALE <file>`: a listed file no longer uses it (drop the line; + the list may only shrink). + +`--selftest` first proves both refusals fire on a synthetic tree (a new includer, a new +direct caller, a stale line). It runs in `make test-compiler`. The CI `compiler` job +guard now also triggers on `src/aarch64_*` changes. + +### 4.5 Evidence + +- `tests/compiler/test_legacy_a64.c` (in `make test-compiler`, plain and ASan/UBSan): + - **differential:** the pre-OSC-2 formulas are kept in the test as the oracle + (`old_*`). For all 25 emitters, in-range fields give the oracle's word. The field + space is covered exhaustively for the register-register forms, `mov_reg`, + `movz`/`movk` (all registers, immediates and shifts), `b_cond` (all conditions and + offsets), `ldr`/`str` unsigned offset, `ldrb`/`strb`, the post-index forms and + `subs_imm`; `b`, `cbz`, `cbnz` and `adr` use 400 000 seeded random samples each with + every range edge (about 71 million words in all). Each word also decodes through + `src/aarch64_decoder.c` to the same operation and to every field that decoder reports + (`sf`, registers, `imm16`/`hw`, condition, raw branch field). The decoder does not + report load/store or `subs` immediates or the ADR offset, so the test checks the + `imm12` field of `ldr`/`str` and the reassembled ADR offset from the word directly. + Line: `legacy a64 differential: emitters=25 mode=full words=.. decoded=.. mismatches=0`. + The ASan leg runs `quick` (strided samples, same edges). + - **refusal:** 90 cases, every field of every emitter that has one, just outside its + range and at extremes (for example `rd=32`, `shift=17`, `shift=32` with `sf` = 0, + `imm26=2^25`, `cond=16`, misaligned `uoff`, `simm9=-257`). Each runs in a child + process with output buffer and position in shared memory; the child must die by + SIGABRT with exactly the line of 4.3 on stderr and must not touch buffer or position + (core dumps disabled in the child). Line: + `legacy a64 refusal: cases=90 named_abort_and_no_write=90`. + - mutation check (done once, not in the suite): removing the `simm9` check, or changing + one bit of the `mul_reg` formula, makes the test fail. +- M6/M9/M14 gate output (from `omegatool --run` on) is byte-identical to the baseline + taken before this item. +- Host suites that link the writer were run on one core and pass (list and results in + the item report); suites that need the GB10, the resident seat or the quiet flag were + not run. +- `make osc2-receipt ITEM=encoder` records the differential, ASan and refusal lines, the + caller check line and, with `PHYSICS_DIR` set, the M6/M9/M14 gates. + +### 4.6 Not covered + +- Instructions are checked field by field; the writer still does not know whether a + register choice is legal for a particular form beyond 0..31 (for example it does not + refuse SP where an instruction reads XZR). Callers keep that responsibility; the OSC-1 + encoder `osc_a64.c` is the one that checks SP vs XZR. +- `src/aarch64_decoder.c` is unchanged: it still decodes only the subset above and keeps + no load/store or `subs` immediate. +- The allowlist check scans C files only; it does not stop a caller written in another + language or one that declares the functions itself under another spelling. +- Refusal ends the process; there is no recoverable error API for the legacy writer. diff --git a/evidence/OSC-2/receipts/osc2-encoder-cb452b9889ad972086f304448645168791c52ec7a33eff284a306da237eca1c9.json b/evidence/OSC-2/receipts/osc2-encoder-cb452b9889ad972086f304448645168791c52ec7a33eff284a306da237eca1c9.json new file mode 100644 index 000000000..c8ac7b1de --- /dev/null +++ b/evidence/OSC-2/receipts/osc2-encoder-cb452b9889ad972086f304448645168791c52ec7a33eff284a306da237eca1c9.json @@ -0,0 +1,83 @@ +{ + "schema": "omega.osc2.compiler.receipt.v1", + "verdict": "PASS", + "item": "encoder", + "statement": "OSC-2 slice; not a general Omega compiler; no self-hosting.", + "subset": "integer scalars u8 u16 u32 u64 (wrap) and i8 i16 i32 i64 (checked, trap on overflow), bool; let / let mut; arithmetic, bitwise, shift, compare, logical (short-circuit), checked as-casts; if/else; while with static bound N (trap past N) and for over literal ranges; calls to earlier functions in the unit (no recursion, <= 6 params); unique allocation own [T; N] (N <= 64), moves, shared and mutable borrows with lexical lifetimes, bounds-checked indexing, deterministic destruction at scope end; OSC-2 item 1: requires/ensures are pure bool expressions over parameters (and result in ensures; element reads through parameters, ensures only via shared borrows; no calls), refused statically when constant folding decides them false (CONTRACT_VIOLATION), otherwise checked at run time (TRAP REQUIRES = 9 at entry, TRAP ENSURES = 10 at each return before releases; statically true clauses elided); OSC-2 item 2: unit-level struct declarations (<= 16 structs, 1..16 fields of integer, bool or [T; N], <= 64 cells, declared before use, no recursion) with a fixed layout of 8-byte cells in declaration order (no padding, bound into the IR digest), struct literals initialising every field exactly once in let own, field reads and writes (array fields bounds-checked, TRAP BOUNDS), moves, own / & / &mut parameters with the array borrow rules, deterministic destruction with the same alloc/release events; requires reads fields through any struct parameter, ensures only through a shared & parameter; returning a struct is refused (UNSUPPORTED); OSC-2 item 3: arena blocks 'arena r bound K { ... }' (K = 1..64 cells) wired to the OSC-0B region model; 'let x: own [T; N] in r = alloc(v);' and 'let s: own S in r = S { .. };' allocate from the arena (bump allocation, never released one by one); at block end the unique owners declared in the block are released first, then REGION_DESTROY frees the whole arena (nested: inner first); a borrow of an arena object outliving the arena is ARENA_ESCAPE (the model rejection arena-escape), every move into or out of an arena object is ARENA_MOVE (slice restriction stricter than the model), a definite over-capacity allocation is ARENA_CAPACITY, otherwise TRAP ARENA_FULL = 11 at run time; REGION_OPEN / ARENA_ALLOC / REGION_DESTROY are logged in the runtime event log by both engines, and the log of every native run in the test replays through the OSC-0B model (executed runs only, not a proof for all programs); OSC-2 item 4: the legacy AArch64 writer src/aarch64_encoder.c (used by omegatool, the realize/matvec/self-host paths, polyglot omx_encoder and rx_omega; not by the OSC compiler slice, whose back end is the validating osc_a64) now validates every field (registers 0..31, imm26/imm19/adr imm21/simm9 signed ranges, imm12 0..4095, cond 0..15, movz/movk shift, ldr/str scaled offsets) and refuses out-of-range input with the named abort A64_LEGACY_FIELD_OUT_OF_RANGE <emitter> <field>=<value> before writing anything, instead of silently masking; in-range words are bit-identical to the old masking formulas (differential test), and new callers outside a frozen allowlist are refused by tests/compiler/legacy_a64_callers.sh", + "pipeline": "surface text -> lexer/parser -> typed AST -> name/type + ownership/borrow analysis -> typed IR (canonical encoding + sha256) -> AArch64 via src/compiler/osc_a64 (validating encoder, decoder mirror)", + "repo_commit": "ae62f38ed800db95287a1fd7c1dee966a75e31b8", + "base_commit": "c77362242b13d7df127eeabec9eda75119f934cc", + "tree_clean": true, + "host_arch": "aarch64", + "c_compiler": "cc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0", + "osc0b_model": {"seed": "0x05c0b5eed0010001", "sequences": 1000000, "steps": 21905422, "false_accepts": 0, "wrong_name": 0, "false_rejects": 0, "result": "OSC0B_MODEL_PASS"}, + "backend": "units=15 funcs_fuzzed=361 diff_runs=722000 words_roundtrip=13027 enc_random_ok=45779 enc_random_refused=126221 dec_random_words=76497 validator_neg=49; checks=799002 failed=0; native == interpreter on every run", + "compiler": "golden=37/37 funcs=129 entries=92 expect_runs=100 diff_runs=115136 trace_replays=89 rt_replays=70929 rt_events=351214 neg=96/96 neg_traced=18 checks=319518 failed=0", + "golden_trap_coverage": {"none": 73471, "overflow": 3408, "div0": 23, "bounds": 5986, "loop_bound": 2100, "cast": 1499, "oom": 365, "shift": 2805, "runtime": 0, "requires": 1793, "ensures": 2135}, + "contract_fuzz": "contract fuzz: units=200 compiled=174 static_refused=26 runs=11136 ok=882 requires=5435 ensures=4384 overflow=435 other_traps=0 mismatches=0; native == interpreter on every run", + "contract_fuzz_asan": "contract fuzz: units=200 compiled=174 static_refused=26 runs=11136 ok=882 requires=5435 ensures=4384 overflow=435 other_traps=0 mismatches=0", + "struct_fuzz": "struct fuzz: units=125 runs=6000 ok=3805 bounds=1087 requires=155 overflow=224 cast=729 mismatches=0; native == interpreter on every run", + "struct_fuzz_asan": "struct fuzz: units=125 runs=6000 ok=3805 bounds=1087 requires=155 overflow=224 cast=729 mismatches=0", + "struct_layout": "struct layout: L offsets 0 1 4 5 7 cells 8; M cells 64; digest binds layout", + "struct_destruction_order": "struct destruction order: alloc 1 2 3, release 3 2, alloc 4, release 4 1 (interp == native)", + "arena_fuzz": "arena fuzz: units=125 allocs=557 runs=6000 ok=2542 arena_full=2606 bounds=639 overflow=213 mismatches=0; native == interpreter on every run", + "arena_fuzz_asan": "arena fuzz: units=125 allocs=557 runs=6000 ok=2542 arena_full=2606 bounds=639 overflow=213 mismatches=0", + "arena_destruction_order": "arena destruction order: alloc 1, open 2, aalloc 3, open 4, aalloc 5, alloc 6, release 6, destroy 4, alloc 7, release 7, destroy 2, release 1 (interp == native)", + "runtime_model_replay": "runtime model replay: runs=115238 events=351214 accepted=115238 rejected=0; every native run of the test (golden fuzz, expect-run lines, contract, struct and arena fuzz, destruction-order tests) replays its runtime event log through the OSC-0B model, trapping runs as the logged prefix; this checks executed runs, not all programs", + "runtime_model_replay_asan": "runtime model replay: runs=115238 events=351214 accepted=115238 rejected=0", + "arena_full_trap_runs": 4415, + "legacy_a64_differential": "legacy a64 differential: emitters=25 mode=full words=71266833 decoded=71266817 mismatches=0; every emitted word equals the old masking formula; the decoded words were also checked field by field through src/aarch64_decoder (the remainder are repeated edge words already decoded in the sweep)", + "legacy_a64_differential_asan": "legacy a64 differential: emitters=25 mode=quick words=703601 decoded=703201 mismatches=0", + "legacy_a64_refusal": "legacy a64 refusal: cases=90 named_abort_and_no_write=90; each out-of-range case aborts (SIGABRT) in a child with the exact named line on stderr and leaves the buffer and position untouched", + "legacy_a64_callers": "OSC2_LEGACY_CALLERS_PASS callers=16 selftest=3/3; files naming aarch64_encoder.h or an aarch64_emit_ function must be on tests/compiler/legacy_a64_allowlist.txt", + "determinism": "programs=37 identical=37 (oscc in separate processes, byte-identical IR digest and machine code)", + "sanitizers": "all three test binaries also pass under -fsanitize=address,undefined -fno-sanitize-recover=all (model sweep at 10^5)", + "golden_programs": 37, + "negative_programs": 96, + "golden_corpus_sha256": "f8a4be89ebf700e778aa154001148651491fc9b07384d031560f2a63130710eb", + "golden_corpus": [ + {"program": "tests/compiler/progs/arena_basic.osc", "source_sha256": "4d7572fef4094024a1f8e84aa54f29e95c6d34c65bce9bfe9d171a0c6015236d", "ir_sha256": "c1cbf604876cbc775f118dd68b867836197a9fd82ea133aacbea3672b8016f3e", "code_sha256": "3fb18af64f18ec3f52d1c5bc600d13af869282a2e56160a9972a6d7a54e54623"}, + {"program": "tests/compiler/progs/arena_borrows.osc", "source_sha256": "a90474fb7d52cf9d24db60c5752aba7db3dbbb2d1527b51a81410522e05bd96c", "ir_sha256": "b2b2737fd79346c4881c63053eeb7c8a493ece93c733b254986f1b88ac39c4b3", "code_sha256": "69209e13ee4f303a9177c8ff8560291da04c6613e0a6bbb7ff25167270cc2ce8"}, + {"program": "tests/compiler/progs/arena_dtor.osc", "source_sha256": "59bb1cc1a7a3d94852581a8465e290f7fc806f82c0ed0e5d5614f724cd47a2db", "ir_sha256": "0853cf1d750b7aa7f89bedce30bab8abed21b7c03688b3ec1d149ed6a4ff1ecc", "code_sha256": "39e09aa8abe6d0527d1bcdf4769099f5e4fb35783949f436b20b25ff29e33a59"}, + {"program": "tests/compiler/progs/arena_full.osc", "source_sha256": "048b757f8e0997ec6eacc1be7065f10a9621eb8147c3a638faff6883b50db7d6", "ir_sha256": "2095deb65522e3782d66abeb601a02fd4f16d76d05bfec32345931f970c0f3bc", "code_sha256": "c58d9cf04f27ea71370cc0fdfe903113ef4449db1d1e8c265adf4a5f5488f902"}, + {"program": "tests/compiler/progs/arena_nested.osc", "source_sha256": "6fbcb009abcd1e47188ccac0adadf4c2a110e060f44028ef5194e637f6b59659", "ir_sha256": "0d910fe9717796b0f1a3afe70e3ad8af94bb9d43675bbc3568635b0f175ec81d", "code_sha256": "6a45e3becc6c0179bab0eeffb4864795a5cf1d12163ac738272ec10d2e3a008e"}, + {"program": "tests/compiler/progs/arena_structs.osc", "source_sha256": "2ccd4e8a6d4eab0e15f1018545ba4604e54dd2f85b21aed38711c3d34a0cf1a4", "ir_sha256": "f9029fbc626e4a06b12f928b60ec3739c935c0d6afbafe0df9daba7580312bfc", "code_sha256": "c1d5a138f01a4625b2eafc626f3d35f4476189f89fd38d9e777345aa0dee65d1"}, + {"program": "tests/compiler/progs/arrays.osc", "source_sha256": "0d41e301111e2c132ceba7d7272eb42ae0d1b1cb0341c4f76d3fec2ce38a6ee8", "ir_sha256": "3c3b6e50085dd82cde7ec97d2feb580d38b714f8cb4f60db35b9a4f36530df0d", "code_sha256": "e6b45cce88509c39fa0503302691906d79e4189de57244a9bc1036ba15c91ac5"}, + {"program": "tests/compiler/progs/bitrev.osc", "source_sha256": "7a831a9f47434a76b65bdf837fecaf96f4a85d2c31c4682b280301b8aa588d1c", "ir_sha256": "e3aa5e5e5c58fbc686025370e8c87eceb5c4805b2a9d21d55bfbf7bfdba6cc36", "code_sha256": "94674978c5ca8c96ccc0fed847668ab4b73c0dd1f3f7527a613b26cd307c2362"}, + {"program": "tests/compiler/progs/bounds.osc", "source_sha256": "0519478f54c98ca06f3414ff9ac5dc4193ed5b2b43a7a89b2333ef3bb54b3094", "ir_sha256": "7e7bb041550f9a5a4445c0e028650f6e0cf9afcea61819a95e29b92eca13d966", "code_sha256": "07bb15370990980cd1b27be4ee87612133a58463e305e067870b6668ebc9a2d9"}, + {"program": "tests/compiler/progs/calls.osc", "source_sha256": "add5b82ec5ea73b322e9b1620220f9ff73ba9f76aeff031e0654980e2f36bcbc", "ir_sha256": "9d50bea67d3928a457201dfe35541b912b9acfbdaf97e901e7ea2e0f3185524d", "code_sha256": "a6434af2275da292a0a573b355cab439bfb26f85be6b0b0dcdc709edc66d7a94"}, + {"program": "tests/compiler/progs/casts.osc", "source_sha256": "a5093b32ee0d204d20920e5fd8f8c39c8e10b92c89aabb41fb703e7e28637fb3", "ir_sha256": "cc35e86f43ba0ebe29ea6906c0af5e01fa30964c75b0a69b05370c4100ae80b3", "code_sha256": "fdff78f367ea211da5e5058ffe715ea7fb14a1bee2727e69aefd9fda2b27bece"}, + {"program": "tests/compiler/progs/collatz.osc", "source_sha256": "912412fa1ffd990786a055d2b43a35f9fd629fa59299b1181463f3b314cd741f", "ir_sha256": "fdf94380724fa0994a074cbed2cf789c3ed4dfeb14450e648b9144ada4e57edd", "code_sha256": "9b06e959179f262f45ef72015ee1001a026262754804c107b640813131e014bf"}, + {"program": "tests/compiler/progs/contracts_basic.osc", "source_sha256": "8a1370cec47deb9671ef6e4a55a66a94b97d3f34754fa7c64ae741853134bb99", "ir_sha256": "94f2e0f6c9c8c15630725d5105007a7ecdb26212c898219b4d99be340d027ede", "code_sha256": "b7d9a360486ca56847ee83a0dc7731026305a20a9dfd250d2d6bc3197b74a0fc"}, + {"program": "tests/compiler/progs/contracts_ensures.osc", "source_sha256": "4bade10adc6078e9a8cb77b12fd529f0cb733bcf24629018a95402528a0bc3f0", "ir_sha256": "c2a7c5cca6ffd933c90d89d20e0b8dcb5c56fbf831fd9b882ad729de1efefa9f", "code_sha256": "de5746dd6d6c22049987e7bd5bdde6a238fe24785e4c654a852e383a0e657f6a"}, + {"program": "tests/compiler/progs/contracts_requires.osc", "source_sha256": "83f5842fba1f7f027cb655fcff243f1650761afedecb1cd168992a08b7924224", "ir_sha256": "16bc4b6267ee1d4a75cc60d35514baf10cc3f326b3a3e47222ca271587c9dac3", "code_sha256": "599922ff8eff90977309cf593da6cac4f951eb182bdf8a8b156c36ca6c7917ff"}, + {"program": "tests/compiler/progs/factorial.osc", "source_sha256": "3e410e9214a30628b199f7bfe8f99d7e9f439e73af2d279719fa7b523bc74e21", "ir_sha256": "cc36ae8edf9a203d4f82bd1b351b92f2df818427d26239ae8c6542e175674700", "code_sha256": "a3fc5f8c4a5ae8ce6381a0db02a03e391df791a0ce2d6f778face3a30532ecd3"}, + {"program": "tests/compiler/progs/fib.osc", "source_sha256": "9490114a0c756edab0ee7af37252b5883cc7c11b69e54ba5481530b2dcb8e03c", "ir_sha256": "bfe54f4d1502277153becb83faa0fd2975496a1875837fa8e8aa72431aa0ad00", "code_sha256": "d2e25ea89ebd59e49e29303696b7b2bff4ff39d45f62bd5566db609830d153a8"}, + {"program": "tests/compiler/progs/gcd.osc", "source_sha256": "0fad11202f1f6ce6e845b266f24df86cbc0e6160a4772c043a15b60fa54a720f", "ir_sha256": "5d649066a565d7e7ab680ea6b2827306701714da7b23755836814da7cccfa328", "code_sha256": "eb4cbe78da3176853323b2d11b970444c51fb61a33dbc770ec355cc10b94d45d"}, + {"program": "tests/compiler/progs/ifchain.osc", "source_sha256": "89fd46fc610516121dbd38cca232a98abccb55abf1593409ca575ab7e293699e", "ir_sha256": "2323f2753d8e2eb269d65db1b8e9a6006b0e09ed98e0f3f74268143cf870342d", "code_sha256": "f7c3aa9e0ae96c5d1a1c0fbcb44b7504843aac8ed5035b2400d50819cc999e03"}, + {"program": "tests/compiler/progs/isort.osc", "source_sha256": "7dceeaf139addf172152b8861602d02d654e5d2f9fab451d2c38d823ddd1b7db", "ir_sha256": "fe5d0090baeb54d9afd33a9745b7a60136e513aaca0c03f5275d5efab4de3091", "code_sha256": "b99188f630088cd1c48b4e57d48431982eb3671c50fbe5ea42e17db680baf100"}, + {"program": "tests/compiler/progs/loopbound.osc", "source_sha256": "71def740b4af102745e954e5d06582cf28b5e90e633a254a3f248bd1ed8c10c3", "ir_sha256": "d5994fccb2c93ba53d8e3a73222aa54f28f2159509f992167c8e77e779734db2", "code_sha256": "e8b5a7ca1240adbcfc66c6e1e117b01ad692c4faed32381742e0ba9a7313bd83"}, + {"program": "tests/compiler/progs/minmax.osc", "source_sha256": "95c17e1cb5ffc836ef2bcf16ab39246b5c0bbb8acb1c05a6e08b838a12b60b7e", "ir_sha256": "a5ee50c4f631cfda3282c6a6a42842f20986ae4129fa4e22656ea836c8a01543", "code_sha256": "387756318095cc219eec337923bf12440927b254cac189872b17ea617f17ae41"}, + {"program": "tests/compiler/progs/nested.osc", "source_sha256": "8e9aaf037c142ea43dcc4e9013013688eb19f03f24a9e8511058623ced32dc35", "ir_sha256": "94e13f928a06a8d2ddff59030bb412cfcb5e4888570f9b7fc44786c135415c04", "code_sha256": "0eb5b788611a907d360730c08b78c6b0cfbf3a454323b42808f77f8aed300e71"}, + {"program": "tests/compiler/progs/oom.osc", "source_sha256": "1d82ff5262c21e4760b2404dc8fc649863a35ac4a164e4ae6684adcd7cecfc7e", "ir_sha256": "6ce4fcbb5b507668183dd67678a93dad7fe11d7b3f02c2466815712fb300116c", "code_sha256": "5c788e2eef2be25b04013f7bd8378d715756293c36f9509c14e9d5d465e5552a"}, + {"program": "tests/compiler/progs/ops.osc", "source_sha256": "2f68f1cf7d7d4569c90d79e4afc2bbdb9cf1bc2fc39cc8f8e23555c90cdd0223", "ir_sha256": "9742eb592d1cfcd72c622d2ab28fe1dd1fa921f14cd3418662f303be851e1162", "code_sha256": "0c038096a7f33e702937c2979eba15d6fc5a25dd66ba12c5b746df8529e7a959"}, + {"program": "tests/compiler/progs/popcount.osc", "source_sha256": "eab7407d3cfc923d15f52e04e00d96a9e7331b0e2496552b8258e7f88bc65236", "ir_sha256": "15aa9536feb7cbbe9cd03efcf2517321964bb479c0331f45610225c64af186ac", "code_sha256": "04510e1bf63889d5dd0bb7b85eeffb7b016ad87a65a7e0e2209df082f1a9d1db"}, + {"program": "tests/compiler/progs/prefix.osc", "source_sha256": "a4852da0953c8e11e5a9e51d00be81a6cc9e1f9e938cbd9d186f34ccb53cc524", "ir_sha256": "6feaa9af48e493a02a3de461aeae3b88b1c813c9011a462537e88c661704ee63", "code_sha256": "f4c78d8644d86ce7f1edd36ebd6958d5250a34dedde3435587e34dfb2d474362"}, + {"program": "tests/compiler/progs/reborrow.osc", "source_sha256": "caf5e895fcd0660d6ca7c6360228901cb4eee5a7afe128b59334aec904e63feb", "ir_sha256": "34086f590aeaec09669b9656cfca7db57beffb67a59d7e1ba22548336de146b2", "code_sha256": "a6a0a251ed5ddcbc431e38ed73602ee9202fd2d601bd7085ae70ccb5974daf62"}, + {"program": "tests/compiler/progs/scopes.osc", "source_sha256": "9904f63cb89734f2411f04b098640cb0ebe8069a49ae7b1a40c728d434770070", "ir_sha256": "81e996a3c8c6515d57375609aba9ae7c6c5822626b1a8da9cd0943b70db4a1ed", "code_sha256": "e3faba53225b4f3a9398b5d39f6a224b24df08cf9b6a67ca5652a439f0717127"}, + {"program": "tests/compiler/progs/shifts.osc", "source_sha256": "dd33980fb0f3b8d61477b6c197508e3c46d894c0ebc0f99244c01b5e86ee8780", "ir_sha256": "7d2e542f2241ccfacbd6a812c1671733dda94410ede4e3847b43c0955a6f69b3", "code_sha256": "c07f4547cac6e250b1c76021f0955fd530844fcc593a33192592ee0f917ecf59"}, + {"program": "tests/compiler/progs/shortcircuit.osc", "source_sha256": "c3f272b3f6b6d27c9cde9887197e73dd622ea30ff802fb87c250a5e2f704e1d0", "ir_sha256": "863365c4653fb44aac42316b1cde491107ad739d58b7af5b4b55e3a2774c5d41", "code_sha256": "bbd44412068130fdf65d66f718f74c6e0e7e36ee19282f19e5c2f3bbcf66ba2e"}, + {"program": "tests/compiler/progs/sixparams.osc", "source_sha256": "535fead297b7c4b7946c25b0b1f716634b4342701c3ff486dbd8189ed8bc3770", "ir_sha256": "176dee2ab6b66ff28dc04f095964c51f59dcda5518a9696a95a9c588dd78083e", "code_sha256": "c1e8cf802ef21c2b08b08dd266303c715e64ab7323cb5267fe2492de1e54cee8"}, + {"program": "tests/compiler/progs/structs_contracts.osc", "source_sha256": "06573d57946360c9e65090b61696a005ffe3fd3f3c1ca117765d9a3028a6f43a", "ir_sha256": "06b233f9e16e229fc606c8bda44b24e43473a2cd376053341e6ee644e124fe99", "code_sha256": "d52177fe222b3e548cc1df3bb294d524b41bcb093b64e8fc41f153094eacebf0"}, + {"program": "tests/compiler/progs/structs_dtor.osc", "source_sha256": "71f951fae6f1471dc52c5edb43abfa7b7692835e0d9fe8c63ae18a2e44e97ad5", "ir_sha256": "c7b4d22c623f0b1c4d004474c532ca74cbf23911cdcd299ad81843f4ef9a27df", "code_sha256": "3d73cda95a76bd70f7a5e4aa7673a360b2c40423381db88cabadd3bbfe363b0d"}, + {"program": "tests/compiler/progs/structs_fields.osc", "source_sha256": "4da454a3b5633ddec4f39f35c67236b26b87dde922d829ff5eb39df2195b5f2e", "ir_sha256": "865a9a09510cf16363e38345c47544ac1c9b48f7eddec67729f7e1ca40d03461", "code_sha256": "69be8bd0763aec308f5a002820ae496bc86dd1ac93111e532790dab27dd10586"}, + {"program": "tests/compiler/progs/structs_owner.osc", "source_sha256": "babf04cbcfbece1e1c4c4d23c59b97fb7644f639f77d987d6732c613313170cc", "ir_sha256": "11dd47aca0387de3b7fc3f546272b165c9f2d2675626dda03a255a4dd4942807", "code_sha256": "3e1df27680d16d47f1c2568d9e7f3f066ef29eb13e0faa89a16e12c182ec3da7"}, + {"program": "tests/compiler/progs/types.osc", "source_sha256": "ca4d49a4aca05b04e9935dd7cc560a023c4e4cffa36ff84c7755883a90c2b0d5", "ir_sha256": "f623556285814fbd02241fe4f24025bf93811581655beab6073e8d867b42b3a5", "code_sha256": "45c042eeb15cebf30615b7ad341b58a41bd805d83a08c358304b4284785eee35"} + ], + "omega_core_sources_unchanged_vs_base": true, + "m6_m9_m14_gates": {"m6": {"total": 10, "passed": 10, "failed": 0, "gate_log_sha256": "7fcc69abb701878b6a400b9ebdceb83bcb98496c8b884b01a2b7587d5bd8b178", "identical_to_baseline": true}, "m9": {"total": 10, "passed": 10, "failed": 0, "gate_log_sha256": "4d8a42eab694e06c2ee49accf9e7325a59912f80d74358bdabbfd57f2301f184", "identical_to_baseline": true}, "m14": {"total": 10, "passed": 10, "failed": 0, "gate_log_sha256": "5e96e93e2d5b381917fb145f1f6d4757f0fd311361d39bc6f24bb82ec8d17e35", "identical_to_baseline": true}}, + "self_host": "no: the OSC-2 compiler slice cannot compile any part of itself (docs/osc/OSC-1-SELF-HOST-STATEMENT.md)", + "legacy_a64_not_covered": "SP versus XZR meaning of register 31 is not checked (both encode as 31); the decoder is unchanged; the caller scan covers C sources only; a refusal ends the process (abort), it is not a recoverable error; the 280 existing call sites were audited to stay in range, and the M6/M9/M14 gates show byte-identical output, but other generated code paths are covered only by the host suites that ran", + "not_covered": "moving arena objects (all arena moves refused: slice restriction, stricter than the model), arena handles as values or parameters, arenas outlasting their block, per-object release inside an arena, struct return values, struct-typed and owner/borrow fields, recursive structs, whole-struct copy or compare, contracts beyond constant folding (no symbolic proof), effects/capabilities, strings, generations, unsafe physical, FFI, Flow IR optimisation, self-hosting (OSC-14); the runtime model replay checks executed runs only and is not a proof for all programs; not a general Omega compiler", + "timing_measured": false +} diff --git a/mk/compiler.mk b/mk/compiler.mk index a93f437d7..8163bac58 100644 --- a/mk/compiler.mk +++ b/mk/compiler.mk @@ -1,14 +1,16 @@ # OSC-1 compiler slice (docs/osc/OSC-1-DESIGN.md) and the OSC-0B exit gate # (II.11 executable memory model). Files under src/compiler/ and tests/compiler/. # Picked up by `-include mk/*.mk`; not part of `all` or `test`. Links nothing -# but src/compiler/** and src/sha256.c (no physics, no runtime, no gate suites). +# but src/compiler/** and src/sha256.c (no physics, no runtime, no gate suites); +# the OSC-2 item 4 test also links the legacy writer src/aarch64_encoder.c + decoder. # OSC-1 slice; not a general Omega compiler; no self-hosting. # # test-compiler model sweep (10^6) + back end + compiler golden/negative/model # agreement + cross-process determinism, plain and ASan/UBSan +# + legacy AArch64 writer differential/refusal + frozen-caller check # oscc build/compiler/oscc <file.osc>: prints IR and code digests # compiler-receipt digest-named receipt under evidence/OSC-1/receipts (clean tree only) -# osc2-receipt ITEM=contracts|structs (default contracts): receipt under evidence/OSC-2/receipts (clean tree only) +# osc2-receipt ITEM=contracts|structs|arenas|encoder (default contracts): receipt under evidence/OSC-2/receipts (clean tree only) ifndef COMPILER_MK COMPILER_MK := 1 .PHONY: test-compiler oscc compiler-receipt osc0b-model-receipt osc2-receipt @@ -50,11 +52,25 @@ $(OSC_DIR)/oscc: src/compiler/oscc_main.c $(OSC_LIB) $(OSC_HDRS) oscc: $(OSC_DIR)/oscc +# OSC-2 item 4: the legacy AArch64 writer (src/aarch64_encoder.c) and its decoder, +# differential against the pre-OSC-2 formulas + refusal path (docs/osc/OSC-2-DESIGN.md s.4) +OSC_LEGACY_A64 = src/aarch64_encoder.c src/aarch64_decoder.c +OSC_LEGACY_A64_HDRS = src/aarch64_encoder.h src/aarch64_decoder.h src/aarch64_target.h + +$(OSC_DIR)/test_legacy_a64: tests/compiler/test_legacy_a64.c $(OSC_LEGACY_A64) $(OSC_LEGACY_A64_HDRS) + @mkdir -p $(OSC_DIR) + $(CC) $(OSC_CFLAGS) -o $@ tests/compiler/test_legacy_a64.c $(OSC_LEGACY_A64) + +$(OSC_DIR)/test_legacy_a64_asan: tests/compiler/test_legacy_a64.c $(OSC_LEGACY_A64) $(OSC_LEGACY_A64_HDRS) + @mkdir -p $(OSC_DIR) + $(CC) $(OSC_CFLAGS) $(OSC_ASAN) -o $@ tests/compiler/test_legacy_a64.c $(OSC_LEGACY_A64) + # The ASan legs use smaller sweeps (stated in each test's output) to stay inside # the 3-minute single-core budget; the plain legs run the full counts. test-compiler: $(OSC_DIR)/test_osc_model $(OSC_DIR)/test_osc_model_asan \ $(OSC_DIR)/test_osc_backend $(OSC_DIR)/test_osc_backend_asan \ - $(OSC_DIR)/test_osc_compiler $(OSC_DIR)/test_osc_compiler_asan $(OSC_DIR)/oscc + $(OSC_DIR)/test_osc_compiler $(OSC_DIR)/test_osc_compiler_asan $(OSC_DIR)/oscc \ + $(OSC_DIR)/test_legacy_a64 $(OSC_DIR)/test_legacy_a64_asan $(abspath $(OSC_DIR)/test_osc_model) > $(OSC_DIR)/model.out @tail -1 $(OSC_DIR)/model.out; grep -q '^OSC0B_MODEL_PASS$$' $(OSC_DIR)/model.out $(abspath $(OSC_DIR)/test_osc_model_asan) 05c0b5eed0010001 100000 > $(OSC_DIR)/model_asan.out @@ -77,7 +93,14 @@ test-compiler: $(OSC_DIR)/test_osc_model $(OSC_DIR)/test_osc_model_asan \ @grep "^arena destruction order: " $(OSC_DIR)/compiler.out @grep "^runtime model replay: .* rejected=0$$" $(OSC_DIR)/compiler.out @grep "^runtime model replay: .* rejected=0$$" $(OSC_DIR)/compiler_asan.out - @echo "test-compiler: PASS (OSC-1 slice + OSC-2 contracts + structs + arenas; OSC-2 slice; not a general Omega compiler; no self-hosting.)" + $(abspath $(OSC_DIR)/test_legacy_a64) > $(OSC_DIR)/legacy_a64.out + @tail -1 $(OSC_DIR)/legacy_a64.out; grep -q '^OSC2_LEGACY_A64_PASS$$' $(OSC_DIR)/legacy_a64.out + $(abspath $(OSC_DIR)/test_legacy_a64_asan) quick > $(OSC_DIR)/legacy_a64_asan.out + @tail -1 $(OSC_DIR)/legacy_a64_asan.out; grep -q '^OSC2_LEGACY_A64_PASS$$' $(OSC_DIR)/legacy_a64_asan.out + @grep "^legacy a64 differential: .* mismatches=0$$" $(OSC_DIR)/legacy_a64.out + sh tests/compiler/legacy_a64_callers.sh --selftest > $(OSC_DIR)/legacy_callers.out + @tail -1 $(OSC_DIR)/legacy_callers.out; grep -q '^OSC2_LEGACY_CALLERS_PASS ' $(OSC_DIR)/legacy_callers.out + @echo "test-compiler: PASS (OSC-1 slice + OSC-2 contracts + structs + arenas + legacy AArch64 writer; OSC-2 slice; not a general Omega compiler; no self-hosting.)" osc0b-model-receipt: sh tests/compiler/osc0b_model_receipt.sh diff --git a/src/aarch64_encoder.c b/src/aarch64_encoder.c index 429e1d7a3..d202ab7d2 100644 --- a/src/aarch64_encoder.c +++ b/src/aarch64_encoder.c @@ -1,4 +1,45 @@ +/* Legacy AArch64 instruction writer (OSC-2 item 4, docs/osc/OSC-2-DESIGN.md section 4). + * + * Every emitter validates every field against its exact architectural range BEFORE + * encoding. An in-range call produces the same word as the pre-OSC-2 writer (the + * encoding formulas below are unchanged; their masks are now no-ops). An out-of-range + * field is REFUSED, never trimmed: the emitter writes nothing, prints + * A64_LEGACY_FIELD_OUT_OF_RANGE <emitter> <field>=<value> + * on stderr and aborts the process (SIGABRT). Abort is deliberate: several existing + * callers ignore the int result, so a soft error would silently drop an instruction. + * The -1 result remains only for "buffer full" (unchanged). + * Ranges: registers 0..31 (31 = XZR/SP where the instruction allows it); sf is a C bool; + * imm16 is uint16_t (0..65535); movz/movk shift in {0,16,32,48} (sf=1) or {0,16} (sf=0); + * b imm26 in [-2^25, 2^25); b.cond/cbz/cbnz imm19 in [-2^18, 2^18); cond 0..15; + * adr imm21 in [-2^20, 2^20); ldr/str (unsigned offset) uoff a multiple of 8 (sf=1) or + * 4 (sf=0) with uoff/size <= 4095; ldrb/strb uoff 0..4095; post-index simm9 in [-256, 255]; + * subs imm12 0..4095. + * New code must not use this writer: the set of files that include this header or call + * aarch64_emit_* is frozen (tests/compiler/legacy_a64_allowlist.txt, checked by + * `make test-compiler`). New code uses the validating encoder src/compiler/osc_a64.c. */ #include "aarch64_encoder.h" +#include <stdio.h> +#include <stdlib.h> + +static void refuse(const char *emitter, const char *field, long long value) __attribute__((noreturn)); +static void refuse(const char *emitter, const char *field, long long value) { + fprintf(stderr, "A64_LEGACY_FIELD_OUT_OF_RANGE %s %s=%lld\n", emitter, field, value); + fflush(stderr); + abort(); +} + +#define CHECK_REG(r) do { if ((unsigned)(r) > 31u) refuse(__func__, #r, (long long)(r)); } while (0) +#define CHECK_SIGNED(v, bits) \ + do { if ((long long)(v) < -(1LL << ((bits) - 1)) || (long long)(v) >= (1LL << ((bits) - 1))) \ + refuse(__func__, #v, (long long)(v)); } while (0) +#define CHECK_UMAX(v, max) do { if ((unsigned long long)(v) > (unsigned long long)(max)) refuse(__func__, #v, (long long)(v)); } while (0) +#define CHECK_COND(c) do { if ((int)(c) < 0 || (int)(c) > 15) refuse(__func__, #c, (long long)(int)(c)); } while (0) +#define CHECK_HW_SHIFT(sf, shift) \ + do { if ((shift) % 16u != 0 || (shift) > ((sf) ? 48u : 16u)) refuse(__func__, #shift, (long long)(shift)); } while (0) +#define CHECK_SCALED(sf, uoff) \ + do { unsigned sz_ = (sf) ? 8u : 4u; \ + if ((uoff) % sz_ != 0 || (uoff) / sz_ > 0xFFFu) refuse(__func__, #uoff, (long long)(uoff)); } while (0) +#define CHECK3(rd, rn, rm) do { CHECK_REG(rd); CHECK_REG(rn); CHECK_REG(rm); } while (0) static int emit_u32_le(uint8_t *buf, size_t *pos, size_t max_len, uint32_t insn) { if (*pos + 4 > max_len) return -1; @@ -11,6 +52,7 @@ static int emit_u32_le(uint8_t *buf, size_t *pos, size_t max_len, uint32_t insn) } int aarch64_emit_add_reg(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rd, uint8_t rn, uint8_t rm) { + CHECK3(rd, rn, rm); uint32_t insn = ((uint32_t)sf << 31) | (0x0B << 24) | ((uint32_t)(rm & 0x1f) << 16) | ((uint32_t)(rn & 0x1f) << 5) | @@ -19,6 +61,7 @@ int aarch64_emit_add_reg(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uin } int aarch64_emit_sub_reg(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rd, uint8_t rn, uint8_t rm) { + CHECK3(rd, rn, rm); uint32_t insn = ((uint32_t)sf << 31) | (1U << 30) | (0x0B << 24) | ((uint32_t)(rm & 0x1f) << 16) | ((uint32_t)(rn & 0x1f) << 5) | @@ -27,6 +70,7 @@ int aarch64_emit_sub_reg(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uin } int aarch64_emit_mul_reg(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rd, uint8_t rn, uint8_t rm) { + CHECK3(rd, rn, rm); /* MADD Xd, Xn, Xm, XZR */ uint32_t insn = ((uint32_t)sf << 31) | (0x1B << 24) | ((uint32_t)(rm & 0x1f) << 16) | @@ -37,6 +81,7 @@ int aarch64_emit_mul_reg(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uin } int aarch64_emit_and_reg(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rd, uint8_t rn, uint8_t rm) { + CHECK3(rd, rn, rm); uint32_t insn = ((uint32_t)sf << 31) | (0x0A << 24) | ((uint32_t)(rm & 0x1f) << 16) | ((uint32_t)(rn & 0x1f) << 5) | @@ -45,6 +90,7 @@ int aarch64_emit_and_reg(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uin } int aarch64_emit_orr_reg(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rd, uint8_t rn, uint8_t rm) { + CHECK3(rd, rn, rm); uint32_t insn = ((uint32_t)sf << 31) | (0x2A << 24) | ((uint32_t)(rm & 0x1f) << 16) | ((uint32_t)(rn & 0x1f) << 5) | @@ -53,6 +99,7 @@ int aarch64_emit_orr_reg(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uin } int aarch64_emit_eor_reg(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rd, uint8_t rn, uint8_t rm) { + CHECK3(rd, rn, rm); uint32_t insn = ((uint32_t)sf << 31) | (0x4A << 24) | ((uint32_t)(rm & 0x1f) << 16) | ((uint32_t)(rn & 0x1f) << 5) | @@ -61,10 +108,12 @@ int aarch64_emit_eor_reg(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uin } int aarch64_emit_mov_reg(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rd, uint8_t rm) { + CHECK_REG(rd); CHECK_REG(rm); return aarch64_emit_orr_reg(buf, pos, max_len, sf, rd, REG_XZR, rm); } int aarch64_emit_movz(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rd, uint16_t imm16, uint8_t shift) { + CHECK_REG(rd); CHECK_HW_SHIFT(sf, shift); uint32_t hw = (shift / 16) & 3; uint32_t insn = ((uint32_t)sf << 31) | (0x52800000) | (hw << 21) | ((uint32_t)imm16 << 5) | (rd & 0x1f); @@ -76,26 +125,31 @@ int aarch64_emit_ret(uint8_t *buf, size_t *pos, size_t max_len) { } int aarch64_emit_b(uint8_t *buf, size_t *pos, size_t max_len, int32_t imm26) { + CHECK_SIGNED(imm26, 26); uint32_t insn = 0x14000000 | ((uint32_t)imm26 & 0x03FFFFFF); return emit_u32_le(buf, pos, max_len, insn); } int aarch64_emit_b_cond(uint8_t *buf, size_t *pos, size_t max_len, Aarch64Cond cond, int32_t imm19) { + CHECK_COND(cond); CHECK_SIGNED(imm19, 19); uint32_t insn = 0x54000000 | (((uint32_t)imm19 & 0x7FFFF) << 5) | ((uint32_t)cond & 0x0F); return emit_u32_le(buf, pos, max_len, insn); } int aarch64_emit_cbz(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rt, int32_t imm19) { + CHECK_REG(rt); CHECK_SIGNED(imm19, 19); uint32_t insn = ((uint32_t)sf << 31) | 0x34000000 | (((uint32_t)imm19 & 0x7FFFF) << 5) | (rt & 0x1F); return emit_u32_le(buf, pos, max_len, insn); } int aarch64_emit_cbnz(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rt, int32_t imm19) { + CHECK_REG(rt); CHECK_SIGNED(imm19, 19); uint32_t insn = ((uint32_t)sf << 31) | 0x35000000 | (((uint32_t)imm19 & 0x7FFFF) << 5) | (rt & 0x1F); return emit_u32_le(buf, pos, max_len, insn); } int aarch64_emit_movk(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rd, uint16_t imm16, uint8_t shift) { + CHECK_REG(rd); CHECK_HW_SHIFT(sf, shift); uint32_t hw = (shift / 16) & 3; uint32_t insn = ((uint32_t)sf << 31) | 0x72800000 | (hw << 21) | ((uint32_t)imm16 << 5) | (rd & 0x1f); @@ -103,6 +157,7 @@ int aarch64_emit_movk(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_ } int aarch64_emit_adr(uint8_t *buf, size_t *pos, size_t max_len, uint8_t rd, int32_t imm21) { + CHECK_REG(rd); CHECK_SIGNED(imm21, 21); uint32_t immlo = ((uint32_t)imm21 & 3) << 29; uint32_t immhi = (((uint32_t)imm21 >> 2) & 0x7FFFF) << 5; uint32_t insn = 0x10000000 | immlo | immhi | (rd & 0x1F); @@ -110,6 +165,7 @@ int aarch64_emit_adr(uint8_t *buf, size_t *pos, size_t max_len, uint8_t rd, int3 } int aarch64_emit_ldr_uoff(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rt, uint8_t rn, uint16_t uoff) { + CHECK_REG(rt); CHECK_REG(rn); CHECK_SCALED(sf, uoff); uint32_t scale = sf ? 3 : 2; uint32_t imm12 = ((uint32_t)uoff >> scale) & 0xFFF; uint32_t base = sf ? 0xF9400000 : 0xB9400000; @@ -118,6 +174,7 @@ int aarch64_emit_ldr_uoff(uint8_t *buf, size_t *pos, size_t max_len, bool sf, ui } int aarch64_emit_str_uoff(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rt, uint8_t rn, uint16_t uoff) { + CHECK_REG(rt); CHECK_REG(rn); CHECK_SCALED(sf, uoff); uint32_t scale = sf ? 3 : 2; uint32_t imm12 = ((uint32_t)uoff >> scale) & 0xFFF; uint32_t base = sf ? 0xF9000000 : 0xB9000000; @@ -126,42 +183,50 @@ int aarch64_emit_str_uoff(uint8_t *buf, size_t *pos, size_t max_len, bool sf, ui } int aarch64_emit_ldrb_uoff(uint8_t *buf, size_t *pos, size_t max_len, uint8_t rt, uint8_t rn, uint16_t uoff) { + CHECK_REG(rt); CHECK_REG(rn); CHECK_UMAX(uoff, 0xFFF); uint32_t insn = 0x39400000 | (((uint32_t)uoff & 0xFFF) << 10) | ((uint32_t)(rn & 0x1F) << 5) | (rt & 0x1F); return emit_u32_le(buf, pos, max_len, insn); } int aarch64_emit_strb_uoff(uint8_t *buf, size_t *pos, size_t max_len, uint8_t rt, uint8_t rn, uint16_t uoff) { + CHECK_REG(rt); CHECK_REG(rn); CHECK_UMAX(uoff, 0xFFF); uint32_t insn = 0x39000000 | (((uint32_t)uoff & 0xFFF) << 10) | ((uint32_t)(rn & 0x1F) << 5) | (rt & 0x1F); return emit_u32_le(buf, pos, max_len, insn); } int aarch64_emit_ldr_post(uint8_t *buf, size_t *pos, size_t max_len, uint8_t rt, uint8_t rn, int16_t simm9) { + CHECK_REG(rt); CHECK_REG(rn); CHECK_SIGNED(simm9, 9); uint32_t insn = 0xB8400400 | (((uint32_t)simm9 & 0x1FF) << 12) | ((uint32_t)(rn & 0x1F) << 5) | (rt & 0x1F); return emit_u32_le(buf, pos, max_len, insn); } int aarch64_emit_str_post(uint8_t *buf, size_t *pos, size_t max_len, uint8_t rt, uint8_t rn, int16_t simm9) { + CHECK_REG(rt); CHECK_REG(rn); CHECK_SIGNED(simm9, 9); uint32_t insn = 0xB8000400 | (((uint32_t)simm9 & 0x1FF) << 12) | ((uint32_t)(rn & 0x1F) << 5) | (rt & 0x1F); return emit_u32_le(buf, pos, max_len, insn); } int aarch64_emit_ldr_x_post(uint8_t *buf, size_t *pos, size_t max_len, uint8_t rt, uint8_t rn, int16_t simm9) { + CHECK_REG(rt); CHECK_REG(rn); CHECK_SIGNED(simm9, 9); uint32_t insn = 0xF8400400 | (((uint32_t)simm9 & 0x1FF) << 12) | ((uint32_t)(rn & 0x1F) << 5) | (rt & 0x1F); return emit_u32_le(buf, pos, max_len, insn); } int aarch64_emit_str_x_post(uint8_t *buf, size_t *pos, size_t max_len, uint8_t rt, uint8_t rn, int16_t simm9) { + CHECK_REG(rt); CHECK_REG(rn); CHECK_SIGNED(simm9, 9); uint32_t insn = 0xF8000400 | (((uint32_t)simm9 & 0x1FF) << 12) | ((uint32_t)(rn & 0x1F) << 5) | (rt & 0x1F); return emit_u32_le(buf, pos, max_len, insn); } int aarch64_emit_subs_imm(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rd, uint8_t rn, uint16_t imm12) { + CHECK_REG(rd); CHECK_REG(rn); CHECK_UMAX(imm12, 0xFFF); uint32_t base = sf ? 0xF1000000 : 0x71000000; uint32_t insn = base | (((uint32_t)imm12 & 0xFFF) << 10) | ((uint32_t)(rn & 0x1F) << 5) | (rd & 0x1F); return emit_u32_le(buf, pos, max_len, insn); } int aarch64_emit_subs_reg(uint8_t *buf, size_t *pos, size_t max_len, bool sf, uint8_t rd, uint8_t rn, uint8_t rm) { + CHECK3(rd, rn, rm); uint32_t base = sf ? 0xEB000000 : 0x6B000000; uint32_t insn = base | ((uint32_t)(rm & 0x1F) << 16) | ((uint32_t)(rn & 0x1F) << 5) | (rd & 0x1F); return emit_u32_le(buf, pos, max_len, insn); diff --git a/tests/compiler/legacy_a64_allowlist.txt b/tests/compiler/legacy_a64_allowlist.txt new file mode 100644 index 000000000..9c22d9595 --- /dev/null +++ b/tests/compiler/legacy_a64_allowlist.txt @@ -0,0 +1,21 @@ +# Frozen set of C files that include src/aarch64_encoder.h or call aarch64_emit_* +# (OSC-2 item 4, docs/osc/OSC-2-DESIGN.md section 4). Checked exactly by +# tests/compiler/legacy_a64_callers.sh in `make test-compiler`. +# New code must NOT be added here: it uses the validating encoder src/compiler/osc_a64.c. +# Removing a line when a caller stops using the legacy writer is the only allowed edit. +src/aarch64_encoder.c +src/aarch64_encoder.h +src/omega_exec.c +src/omega_matvec.c +src/omega_matvec_quad.c +src/omega_program.c +src/omega_realize.c +src/omega_realize_synth.c +src/omega_self_host.c +src/polyglot/omx_encoder.c +src/runtime/rx_omega.c +tests/compiler/test_legacy_a64.c +tests/polyglot/test_encoder.c +tests/realize/test_program_realize.c +tests/visor/test_visor_realization.c +tools/omegatool.c diff --git a/tests/compiler/legacy_a64_callers.sh b/tests/compiler/legacy_a64_callers.sh new file mode 100644 index 000000000..ba139096c --- /dev/null +++ b/tests/compiler/legacy_a64_callers.sh @@ -0,0 +1,59 @@ +#!/bin/sh +# OSC-2 item 4: the legacy AArch64 writer (src/aarch64_encoder.c) takes no new callers. +# OSC-2 slice; not a general Omega compiler; no self-hosting. +# Lists every C source/header in the tree (tracked plus untracked, not ignored) that +# includes aarch64_encoder.h or names an aarch64_emit_* function and compares the set +# with tests/compiler/legacy_a64_allowlist.txt. Exit 1 with +# A64_LEGACY_NEW_CALLER <file> a file not on the frozen list uses the writer +# A64_LEGACY_ALLOWLIST_STALE <file> a listed file no longer uses it (drop the line) +# Usage: legacy_a64_callers.sh [--selftest] +# --selftest first proves both refusals fire on a synthetic tree (a new includer, a +# new direct caller, a stale list line), then checks the real tree. +# Last line on success: OSC2_LEGACY_CALLERS_PASS callers=<n> [selftest=3/3] +set -eu +ROOT=$(cd "$(dirname "$0")/../.." && pwd -P) +cd "$ROOT" +LIST=tests/compiler/legacy_a64_allowlist.txt +T=$(mktemp -d) +trap 'rm -rf "$T"' EXIT +PAT='aarch64_encoder\.h|aarch64_emit_[a-z0-9_]+' + +# compare <file-list> <allowlist> <out>: writes the refusal lines to <out>, returns 1 if any +compare() { + : >"$T/found" + while IFS= read -r f; do + [ -f "$f" ] || continue + if grep -qE "$PAT" "$f"; then echo "$f" >>"$T/found"; fi + done <"$1" + LC_ALL=C sort -u "$T/found" >"$T/found.s" + grep -v '^#' "$2" | grep -v '^[[:space:]]*$' | LC_ALL=C sort -u >"$T/allow.s" + LC_ALL=C comm -23 "$T/found.s" "$T/allow.s" | sed 's/^/A64_LEGACY_NEW_CALLER /' >"$3" + LC_ALL=C comm -13 "$T/found.s" "$T/allow.s" | sed 's/^/A64_LEGACY_ALLOWLIST_STALE /' >>"$3" + [ ! -s "$3" ] +} + +ST= +if [ "${1:-}" = --selftest ]; then + mkdir -p "$T/s" + printf '#include "aarch64_encoder.h"\n' >"$T/s/new_include.c" + printf 'int f(void) { return aarch64_emit_ret(0, 0, 0); }\n' >"$T/s/new_call.c" + printf 'int g(void) { return 0; }\n' >"$T/s/clean.c" + printf '%s\n' "$T/s/new_include.c" "$T/s/new_call.c" "$T/s/clean.c" >"$T/s/files" + printf '# test list\n%s\n' "$T/s/clean.c" >"$T/s/allow" + ok=0 + if compare "$T/s/files" "$T/s/allow" "$T/s/out"; then :; fi + grep -qx "A64_LEGACY_NEW_CALLER $T/s/new_include.c" "$T/s/out" && ok=$((ok + 1)) + grep -qx "A64_LEGACY_NEW_CALLER $T/s/new_call.c" "$T/s/out" && ok=$((ok + 1)) + grep -qx "A64_LEGACY_ALLOWLIST_STALE $T/s/clean.c" "$T/s/out" && ok=$((ok + 1)) + [ "$(wc -l <"$T/s/out" | tr -d ' ')" = 3 ] || ok=0 + if [ "$ok" != 3 ]; then cat "$T/s/out"; echo "selftest: refusals did not fire ($ok/3)"; echo "OSC2_LEGACY_CALLERS_FAIL"; exit 1; fi + ST=" selftest=3/3" +fi + +if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then + git ls-files --cached --others --exclude-standard -- '*.c' '*.h' >"$T/files" +else + find . -path ./build -prune -o -type f \( -name '*.c' -o -name '*.h' \) -print | sed 's|^\./||' >"$T/files" +fi +if ! compare "$T/files" "$LIST" "$T/out"; then cat "$T/out"; echo "OSC2_LEGACY_CALLERS_FAIL"; exit 1; fi +echo "OSC2_LEGACY_CALLERS_PASS callers=$(wc -l <"$T/found.s" | tr -d ' ')$ST" diff --git a/tests/compiler/osc2_receipt.sh b/tests/compiler/osc2_receipt.sh index 3f3ede73a..7e32ec9f1 100755 --- a/tests/compiler/osc2_receipt.sh +++ b/tests/compiler/osc2_receipt.sh @@ -1,7 +1,7 @@ #!/bin/sh # OSC-2 compiler slice receipt, one per item (docs/osc/OSC-2-DESIGN.md). # OSC-2 slice; not a general Omega compiler; no self-hosting. -# Usage: tests/compiler/osc2_receipt.sh [ITEM] (ITEM: contracts (default), structs or arenas) +# Usage: tests/compiler/osc2_receipt.sh [ITEM] (ITEM: contracts (default), structs, arenas or encoder) # 1. refuses a dirty tree (any tracked or untracked change): receipts record an exact commit # 2. runs `make test-compiler` into a temporary OUT_DIR (model sweep, back end, # compiler golden/negative/model agreement, contract + struct + arena fuzz, runtime model replay, @@ -11,11 +11,19 @@ # src/language/, tools/omegatool.c, Makefile) are unchanged against the merge # base with origin/main, so the M6/M9/M14 gates cannot have changed; if # PHYSICS_DIR points at the pinned physics checkout it also runs those gates +# ITEM=encoder (OSC-2 item 4) changes src/aarch64_encoder.*: it is left out of that check, +# PHYSICS_DIR is required, each gate log is hashed (build path normalised) and, when +# OSC2_GATE_BASELINE names a directory with base-<m>.gate.log, compared byte for byte; +# it also records the legacy writer differential/refusal lines and the caller allowlist check # 5. writes a content-addressed receipt evidence/OSC-2/receipts/osc2-<ITEM>-<sha256>.json # Never overwrites existing evidence (evidence/OSC-1 is never touched). Timing is not recorded. set -eu ITEM=${1:-${ITEM:-contracts}} -case "$ITEM" in contracts|structs|arenas) ;; *) echo "osc2-receipt: REFUSED: unknown ITEM $ITEM (known: contracts structs arenas)" >&2; exit 2 ;; esac +case "$ITEM" in contracts|structs|arenas|encoder) ;; *) echo "osc2-receipt: REFUSED: unknown ITEM $ITEM (known: contracts structs arenas encoder)" >&2; exit 2 ;; esac +# items are cumulative: the encoder receipt also records the struct and arena evidence +HAS_S=0; HAS_A=0; HAS_E=0 +case "$ITEM" in structs) HAS_S=1 ;; arenas) HAS_S=1; HAS_A=1 ;; encoder) HAS_S=1; HAS_A=1; HAS_E=1 ;; esac +if [ "$HAS_E" = 1 ]; then [ -n "${PHYSICS_DIR:-}" ] && [ -d "$PHYSICS_DIR" ] || { echo "osc2-receipt: REFUSED: ITEM=encoder changes the legacy writer the M6/M9/M14 gates use; PHYSICS_DIR must point at the pinned physics checkout" >&2; exit 2; }; fi ROOT=$(cd "$(dirname "$0")/../.." && pwd -P) cd "$ROOT" EVD=evidence/OSC-2/receipts @@ -45,14 +53,14 @@ FLINE_ASAN=$(grep "^contract fuzz:" "$O/compiler_asan.out" | tail -1) [ -n "$FLINE" ] && [ -n "$FLINE_ASAN" ] || die "no contract fuzz line" SLINE=$(grep "^struct fuzz:" "$O/compiler.out" | tail -1) SLINE_ASAN=$(grep "^struct fuzz:" "$O/compiler_asan.out" | tail -1) -if [ "$ITEM" = structs ] || [ "$ITEM" = arenas ]; then +if [ "$HAS_S" = 1 ]; then case "$SLINE" in *" mismatches=0") ;; *) die "no struct fuzz line with mismatches=0" ;; esac case "$SLINE_ASAN" in *" mismatches=0") ;; *) die "no ASan struct fuzz line with mismatches=0" ;; esac LAYOUT=$(grep "^struct layout:" "$O/compiler.out" | tail -1) DTOR=$(grep "^struct destruction order:" "$O/compiler.out" | tail -1) [ -n "$LAYOUT" ] && [ -n "$DTOR" ] || die "no struct layout / destruction order line" fi -if [ "$ITEM" = arenas ]; then +if [ "$HAS_A" = 1 ]; then ALINE=$(grep "^arena fuzz:" "$O/compiler.out" | tail -1) ALINE_ASAN=$(grep "^arena fuzz:" "$O/compiler_asan.out" | tail -1) case "$ALINE" in *" mismatches=0") ;; *) die "no arena fuzz line with mismatches=0" ;; esac @@ -65,6 +73,19 @@ if [ "$ITEM" = arenas ]; then case "$RLINE_ASAN" in *" rejected=0") ;; *) die "no ASan runtime model replay line with rejected=0" ;; esac n=$(tc ARENA_FULL); [ -n "$n" ] && [ "$n" -gt 0 ] || die "trap ARENA_FULL not observed" fi +if [ "$HAS_E" = 1 ]; then + ELINE=$(grep "^legacy a64 differential:" "$O/legacy_a64.out" | tail -1) + ELINE_ASAN=$(grep "^legacy a64 differential:" "$O/legacy_a64_asan.out" | tail -1) + case "$ELINE" in *" mode=full "*" mismatches=0") ;; *) die "no full legacy a64 differential line with mismatches=0" ;; esac + case "$ELINE_ASAN" in *" mismatches=0") ;; *) die "no ASan legacy a64 differential line with mismatches=0" ;; esac + EREF=$(grep "^legacy a64 refusal:" "$O/legacy_a64.out" | tail -1) + EREF_ASAN=$(grep "^legacy a64 refusal:" "$O/legacy_a64_asan.out" | tail -1) + n=$(echo "$EREF" | sed -n 's/.*cases=\([0-9]*\) named_abort_and_no_write=\([0-9]*\)$/\1 \2/p') + [ -n "$n" ] && [ "${n% *}" = "${n#* }" ] && [ "${n% *}" -gt 0 ] || die "legacy a64 refusal line missing or incomplete" + [ "$EREF_ASAN" = "$EREF" ] || die "ASan legacy a64 refusal line differs" + ECALL=$(grep "^OSC2_LEGACY_CALLERS_PASS " "$O/legacy_callers.out" | tail -1) + [ -n "$ECALL" ] || die "no legacy caller allowlist PASS line" +fi for k in OVERFLOW DIV0 BOUNDS LOOP_BOUND CAST OOM SHIFT REQUIRES ENSURES; do n=$(tc $k); [ -n "$n" ] && [ "$n" -gt 0 ] || die "trap $k not observed in golden corpus" done @@ -81,6 +102,9 @@ NPROG=$(wc -l <"$T/corpus.txt" | tr -d ' ') NNEG=$(ls tests/compiler/neg/*.osc | wc -l | tr -d ' ') CORE="src/omega_*.c src/omega_*.h src/aarch64_* src/language tools/omegatool.c Makefile" +# item 4 changes the legacy writer itself (src/aarch64_encoder.*): it is excluded from the +# unchanged-core check and the M6/M9/M14 gates are run instead (PHYSICS_DIR is required) +[ "$HAS_E" = 1 ] && CORE="src/omega_*.c src/omega_*.h src/aarch64_decoder.* src/aarch64_target.h src/language tools/omegatool.c Makefile" # shellcheck disable=SC2086 if git diff --quiet "$BASE" HEAD -- $CORE; then CORE_UNCHANGED=true; else die "existing Omega core sources changed vs $BASE"; fi @@ -92,19 +116,33 @@ if [ -n "${PHYSICS_DIR:-}" ] && [ -d "$PHYSICS_DIR" ]; then nice make --no-print-directory OUT_DIR=build/osc2-receipt-gates PHYSICS_DIR="$PHYSICS_DIR" "test-$m" >"$T/$m.log" 2>&1 \ || { tail -20 "$T/$m.log"; die "test-$m failed"; } tot=$(grep 'TOTAL GATES' "$T/$m.log" | head -1 | sed 's/.*TOTAL GATES: \([0-9]*\) | PASSED: \([0-9]*\) | FAILED: \([0-9]*\).*/{"total": \1, "passed": \2, "failed": \3}/') + case "$tot" in "{\"total\": "*", \"failed\": 0}") ;; *) die "test-$m: no gate total line with failed=0" ;; esac sed -n "/omegatool --run/,\$p" "$T/$m.log" >build/osc2-receipt-gates/$m.gate.log + # the gate log names the build directory (./build/<dir>/omegatool); normalise it before hashing + sed 's|\./build/[^/ ]*/omegatool|./OUT/omegatool|g' build/osc2-receipt-gates/$m.gate.log >"$T/$m.norm" + gsha=$(sha256sum "$T/$m.norm" | cut -d' ' -f1) + if [ -n "${OSC2_GATE_BASELINE:-}" ]; then + sed 's|\./build/[^/ ]*/omegatool|./OUT/omegatool|g' "$OSC2_GATE_BASELINE/base-$m.gate.log" | cmp -s - "$T/$m.norm" \ + || die "test-$m gate output differs from baseline $OSC2_GATE_BASELINE/base-$m.gate.log" + tot="${tot%\}}, \"gate_log_sha256\": \"$gsha\", \"identical_to_baseline\": true}" + else + tot="${tot%\}}, \"gate_log_sha256\": \"$gsha\"}" + fi G="$G\"$m\": $tot, " done GATES="{${G%, }}" fi SUBX= -if [ "$ITEM" = structs ] || [ "$ITEM" = arenas ]; then +if [ "$HAS_S" = 1 ]; then SUBX="; OSC-2 item 2: unit-level struct declarations (<= 16 structs, 1..16 fields of integer, bool or [T; N], <= 64 cells, declared before use, no recursion) with a fixed layout of 8-byte cells in declaration order (no padding, bound into the IR digest), struct literals initialising every field exactly once in let own, field reads and writes (array fields bounds-checked, TRAP BOUNDS), moves, own / & / &mut parameters with the array borrow rules, deterministic destruction with the same alloc/release events; requires reads fields through any struct parameter, ensures only through a shared & parameter; returning a struct is refused (UNSUPPORTED)" fi -if [ "$ITEM" = arenas ]; then +if [ "$HAS_A" = 1 ]; then SUBX="$SUBX; OSC-2 item 3: arena blocks 'arena r bound K { ... }' (K = 1..64 cells) wired to the OSC-0B region model; 'let x: own [T; N] in r = alloc(v);' and 'let s: own S in r = S { .. };' allocate from the arena (bump allocation, never released one by one); at block end the unique owners declared in the block are released first, then REGION_DESTROY frees the whole arena (nested: inner first); a borrow of an arena object outliving the arena is ARENA_ESCAPE (the model rejection arena-escape), every move into or out of an arena object is ARENA_MOVE (slice restriction stricter than the model), a definite over-capacity allocation is ARENA_CAPACITY, otherwise TRAP ARENA_FULL = 11 at run time; REGION_OPEN / ARENA_ALLOC / REGION_DESTROY are logged in the runtime event log by both engines, and the log of every native run in the test replays through the OSC-0B model (executed runs only, not a proof for all programs)" fi +if [ "$HAS_E" = 1 ]; then + SUBX="$SUBX; OSC-2 item 4: the legacy AArch64 writer src/aarch64_encoder.c (used by omegatool, the realize/matvec/self-host paths, polyglot omx_encoder and rx_omega; not by the OSC compiler slice, whose back end is the validating osc_a64) now validates every field (registers 0..31, imm26/imm19/adr imm21/simm9 signed ranges, imm12 0..4095, cond 0..15, movz/movk shift, ldr/str scaled offsets) and refuses out-of-range input with the named abort A64_LEGACY_FIELD_OUT_OF_RANGE <emitter> <field>=<value> before writing anything, instead of silently masking; in-range words are bit-identical to the old masking formulas (differential test), and new callers outside a frozen allowlist are refused by tests/compiler/legacy_a64_callers.sh" +fi R="$T/receipt.json" { printf '{\n' @@ -128,13 +166,13 @@ R="$T/receipt.json" "$(tc none)" "$(tc OVERFLOW)" "$(tc DIV0)" "$(tc BOUNDS)" "$(tc LOOP_BOUND)" "$(tc CAST)" "$(tc OOM)" "$(tc SHIFT)" "$(tc RUNTIME)" "$(tc REQUIRES)" "$(tc ENSURES)" printf ' "contract_fuzz": "%s; native == interpreter on every run",\n' "$FLINE" printf ' "contract_fuzz_asan": "%s",\n' "$FLINE_ASAN" - if [ "$ITEM" = structs ] || [ "$ITEM" = arenas ]; then + if [ "$HAS_S" = 1 ]; then printf ' "struct_fuzz": "%s; native == interpreter on every run",\n' "$SLINE" printf ' "struct_fuzz_asan": "%s",\n' "$SLINE_ASAN" printf ' "struct_layout": "%s",\n' "$LAYOUT" printf ' "struct_destruction_order": "%s",\n' "$DTOR" fi - if [ "$ITEM" = arenas ]; then + if [ "$HAS_A" = 1 ]; then printf ' "arena_fuzz": "%s; native == interpreter on every run",\n' "$ALINE" printf ' "arena_fuzz_asan": "%s",\n' "$ALINE_ASAN" printf ' "arena_destruction_order": "%s",\n' "$ADTOR" @@ -142,6 +180,12 @@ R="$T/receipt.json" printf ' "runtime_model_replay_asan": "%s",\n' "$RLINE_ASAN" printf ' "arena_full_trap_runs": %s,\n' "$(tc ARENA_FULL)" fi + if [ "$HAS_E" = 1 ]; then + printf ' "legacy_a64_differential": "%s; every emitted word equals the old masking formula; the decoded words were also checked field by field through src/aarch64_decoder (the remainder are repeated edge words already decoded in the sweep)",\n' "$ELINE" + printf ' "legacy_a64_differential_asan": "%s",\n' "$ELINE_ASAN" + printf ' "legacy_a64_refusal": "%s; each out-of-range case aborts (SIGABRT) in a child with the exact named line on stderr and leaves the buffer and position untouched",\n' "$EREF" + printf ' "legacy_a64_callers": "%s; files naming aarch64_encoder.h or an aarch64_emit_ function must be on tests/compiler/legacy_a64_allowlist.txt",\n' "$ECALL" + fi printf ' "determinism": "%s (oscc in separate processes, byte-identical IR digest and machine code)",\n' "$DLINE" printf ' "sanitizers": "all three test binaries also pass under -fsanitize=address,undefined -fno-sanitize-recover=all (model sweep at 10^5)",\n' printf ' "golden_programs": %s,\n' "$NPROG" @@ -157,7 +201,10 @@ R="$T/receipt.json" printf ' "omega_core_sources_unchanged_vs_base": %s,\n' "$CORE_UNCHANGED" printf ' "m6_m9_m14_gates": %s,\n' "$GATES" printf ' "self_host": "no: the OSC-2 compiler slice cannot compile any part of itself (docs/osc/OSC-1-SELF-HOST-STATEMENT.md)",\n' - if [ "$ITEM" = arenas ]; then + if [ "$HAS_E" = 1 ]; then + printf ' "legacy_a64_not_covered": "SP versus XZR meaning of register 31 is not checked (both encode as 31); the decoder is unchanged; the caller scan covers C sources only; a refusal ends the process (abort), it is not a recoverable error; the 280 existing call sites were audited to stay in range, and the M6/M9/M14 gates show byte-identical output, but other generated code paths are covered only by the host suites that ran",\n' + fi + if [ "$HAS_A" = 1 ]; then printf ' "not_covered": "moving arena objects (all arena moves refused: slice restriction, stricter than the model), arena handles as values or parameters, arenas outlasting their block, per-object release inside an arena, struct return values, struct-typed and owner/borrow fields, recursive structs, whole-struct copy or compare, contracts beyond constant folding (no symbolic proof), effects/capabilities, strings, generations, unsafe physical, FFI, Flow IR optimisation, self-hosting (OSC-14); the runtime model replay checks executed runs only and is not a proof for all programs; not a general Omega compiler",\n' elif [ "$ITEM" = structs ]; then printf ' "not_covered": "struct return values, struct-typed and owner/borrow fields, recursive structs, struct literals outside let own initialisers, whole-struct copy or compare, contracts beyond constant folding (no symbolic proof), effects/capabilities, strings, generations, arenas, unsafe physical, FFI, Flow IR optimisation, self-hosting (OSC-14); not a general Omega compiler",\n' diff --git a/tests/compiler/test_legacy_a64.c b/tests/compiler/test_legacy_a64.c new file mode 100644 index 000000000..4194c9a68 --- /dev/null +++ b/tests/compiler/test_legacy_a64.c @@ -0,0 +1,535 @@ +/* + * test_legacy_a64.c -- OSC-2 item 4: the legacy AArch64 writer src/aarch64_encoder.c + * (docs/osc/OSC-2-DESIGN.md section 4). + * OSC-2 slice; not a general Omega compiler; no self-hosting. + * + * (a) differential: for every emitter, in-range fields (exhaustive where the field + * space is <= ~2^24, otherwise seeded random plus every range edge) give the same + * word as the pre-OSC-2 masking formula (copied below as the oracle, old_*), and + * the word decodes through src/aarch64_decoder.c to the same op and fields (the + * fields that decoder reports); + * (b) refusal: every field of every emitter, given values just outside its range and + * extreme values, is refused: the child process dies with SIGABRT, its stderr is + * exactly "A64_LEGACY_FIELD_OUT_OF_RANGE <emitter> <field>=<value>\n", and the + * output buffer and position (shared memory) are untouched. + * Usage: test_legacy_a64 [quick] (quick: random samples only, for the ASan leg) + * Final line: OSC2_LEGACY_A64_PASS or OSC2_LEGACY_A64_FAIL. + */ +#include <signal.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/mman.h> +#include <sys/prctl.h> +#include <sys/resource.h> +#include <sys/wait.h> +#include <unistd.h> + +#include "aarch64_decoder.h" +#include "aarch64_encoder.h" + +static unsigned long n_checks, n_fail, n_words, n_decoded, n_refusal_cases, n_refusal_ok; +static int quick; + +static void check(int cond, const char *fmt, ...) { + n_checks++; + if (cond) return; + n_fail++; + if (n_fail <= 40) { + va_list ap; + va_start(ap, fmt); + fputs("FAIL: ", stdout); + vprintf(fmt, ap); + fputc('\n', stdout); + va_end(ap); + } +} + +static unsigned long long rng = 0x0a64c0de5eed0004ULL; +static unsigned long long rnd(void) { /* splitmix64 */ + unsigned long long z = (rng += 0x9E3779B97F4A7C15ULL); + z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL; + z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL; + return z ^ (z >> 31); +} +/* uniform in [lo, hi], with a 1/8 chance of an edge value */ +static long long pick(long long lo, long long hi) { + unsigned long long r = rnd(); + switch (r & 15) { + case 0: return lo; + case 1: return hi; + default: return lo + (long long)((r >> 4) % (unsigned long long)(hi - lo + 1)); + } +} + +/* ---- oracle: the pre-OSC-2 formulas, verbatim apart from the return ---- */ +static uint32_t old_add_reg(bool sf, uint8_t rd, uint8_t rn, uint8_t rm) { + return ((uint32_t)sf << 31) | (0x0B << 24) | ((uint32_t)(rm & 0x1f) << 16) | ((uint32_t)(rn & 0x1f) << 5) | (rd & 0x1f); +} +static uint32_t old_sub_reg(bool sf, uint8_t rd, uint8_t rn, uint8_t rm) { + return ((uint32_t)sf << 31) | (1U << 30) | (0x0B << 24) | ((uint32_t)(rm & 0x1f) << 16) | ((uint32_t)(rn & 0x1f) << 5) | + (rd & 0x1f); +} +static uint32_t old_mul_reg(bool sf, uint8_t rd, uint8_t rn, uint8_t rm) { + return ((uint32_t)sf << 31) | (0x1B << 24) | ((uint32_t)(rm & 0x1f) << 16) | (0x1f << 10) | ((uint32_t)(rn & 0x1f) << 5) | + (rd & 0x1f); +} +static uint32_t old_and_reg(bool sf, uint8_t rd, uint8_t rn, uint8_t rm) { + return ((uint32_t)sf << 31) | (0x0A << 24) | ((uint32_t)(rm & 0x1f) << 16) | ((uint32_t)(rn & 0x1f) << 5) | (rd & 0x1f); +} +static uint32_t old_orr_reg(bool sf, uint8_t rd, uint8_t rn, uint8_t rm) { + return ((uint32_t)sf << 31) | (0x2A << 24) | ((uint32_t)(rm & 0x1f) << 16) | ((uint32_t)(rn & 0x1f) << 5) | (rd & 0x1f); +} +static uint32_t old_eor_reg(bool sf, uint8_t rd, uint8_t rn, uint8_t rm) { + return ((uint32_t)sf << 31) | (0x4A << 24) | ((uint32_t)(rm & 0x1f) << 16) | ((uint32_t)(rn & 0x1f) << 5) | (rd & 0x1f); +} +static uint32_t old_mov_reg(bool sf, uint8_t rd, uint8_t rm) { return old_orr_reg(sf, rd, REG_XZR, rm); } +static uint32_t old_movz(bool sf, uint8_t rd, uint16_t imm16, uint8_t shift) { + uint32_t hw = (shift / 16) & 3; + return ((uint32_t)sf << 31) | (0x52800000) | (hw << 21) | ((uint32_t)imm16 << 5) | (rd & 0x1f); +} +static uint32_t old_ret(void) { return 0xD65F03C0; } +static uint32_t old_b(int32_t imm26) { return 0x14000000 | ((uint32_t)imm26 & 0x03FFFFFF); } +static uint32_t old_b_cond(Aarch64Cond cond, int32_t imm19) { + return 0x54000000 | (((uint32_t)imm19 & 0x7FFFF) << 5) | ((uint32_t)cond & 0x0F); +} +static uint32_t old_cbz(bool sf, uint8_t rt, int32_t imm19) { + return ((uint32_t)sf << 31) | 0x34000000 | (((uint32_t)imm19 & 0x7FFFF) << 5) | (rt & 0x1F); +} +static uint32_t old_cbnz(bool sf, uint8_t rt, int32_t imm19) { + return ((uint32_t)sf << 31) | 0x35000000 | (((uint32_t)imm19 & 0x7FFFF) << 5) | (rt & 0x1F); +} +static uint32_t old_movk(bool sf, uint8_t rd, uint16_t imm16, uint8_t shift) { + uint32_t hw = (shift / 16) & 3; + return ((uint32_t)sf << 31) | 0x72800000 | (hw << 21) | ((uint32_t)imm16 << 5) | (rd & 0x1f); +} +static uint32_t old_adr(uint8_t rd, int32_t imm21) { + uint32_t immlo = ((uint32_t)imm21 & 3) << 29; + uint32_t immhi = (((uint32_t)imm21 >> 2) & 0x7FFFF) << 5; + return 0x10000000 | immlo | immhi | (rd & 0x1F); +} +static uint32_t old_ldr_uoff(bool sf, uint8_t rt, uint8_t rn, uint16_t uoff) { + uint32_t scale = sf ? 3 : 2; + uint32_t imm12 = ((uint32_t)uoff >> scale) & 0xFFF; + uint32_t base = sf ? 0xF9400000 : 0xB9400000; + return base | (imm12 << 10) | ((uint32_t)(rn & 0x1F) << 5) | (rt & 0x1F); +} +static uint32_t old_str_uoff(bool sf, uint8_t rt, uint8_t rn, uint16_t uoff) { + uint32_t scale = sf ? 3 : 2; + uint32_t imm12 = ((uint32_t)uoff >> scale) & 0xFFF; + uint32_t base = sf ? 0xF9000000 : 0xB9000000; + return base | (imm12 << 10) | ((uint32_t)(rn & 0x1F) << 5) | (rt & 0x1F); +} +static uint32_t old_ldrb_uoff(uint8_t rt, uint8_t rn, uint16_t uoff) { + return 0x39400000 | (((uint32_t)uoff & 0xFFF) << 10) | ((uint32_t)(rn & 0x1F) << 5) | (rt & 0x1F); +} +static uint32_t old_strb_uoff(uint8_t rt, uint8_t rn, uint16_t uoff) { + return 0x39000000 | (((uint32_t)uoff & 0xFFF) << 10) | ((uint32_t)(rn & 0x1F) << 5) | (rt & 0x1F); +} +static uint32_t old_post(uint32_t base, uint8_t rt, uint8_t rn, int16_t simm9) { + return base | (((uint32_t)simm9 & 0x1FF) << 12) | ((uint32_t)(rn & 0x1F) << 5) | (rt & 0x1F); +} +static uint32_t old_subs_imm(bool sf, uint8_t rd, uint8_t rn, uint16_t imm12) { + uint32_t base = sf ? 0xF1000000 : 0x71000000; + return base | (((uint32_t)imm12 & 0xFFF) << 10) | ((uint32_t)(rn & 0x1F) << 5) | (rd & 0x1F); +} +static uint32_t old_subs_reg(bool sf, uint8_t rd, uint8_t rn, uint8_t rm) { + uint32_t base = sf ? 0xEB000000 : 0x6B000000; + return base | ((uint32_t)(rm & 0x1F) << 16) | ((uint32_t)(rn & 0x1F) << 5) | (rd & 0x1F); +} + +/* ---- one emitted word ---- */ +static uint8_t g_buf[8]; +static size_t g_pos; +static uint32_t word_of(int rc, const char *what) { + check(rc == 0 && g_pos == 4, "%s: emitter returned %d pos %zu", what, rc, g_pos); + n_words++; + return (uint32_t)g_buf[0] | (uint32_t)g_buf[1] << 8 | (uint32_t)g_buf[2] << 16 | (uint32_t)g_buf[3] << 24; +} +#define EMIT(call) (g_pos = 0, memset(g_buf, 0, sizeof g_buf), (call)) +#define B g_buf, &g_pos, 4 + +/* expected decoder fields; -1 = not reported by src/aarch64_decoder.c */ +typedef struct { DecodedOp op; int sf, rd, rn, rm, imm16, hw, br, cond; } Want; +static Want want(DecodedOp op) { Want w = {op, -1, -1, -1, -1, -1, -1, -1, -1}; return w; } + +static void dcheck(const char *what, uint32_t w, Want e) { + DecodedInsn d; + int rc = aarch64_decode_instruction(w, &d); + n_decoded++; + check(rc == 0 && d.op == e.op, "%s: %08x decodes as op %d (rc %d), want %d", what, w, (int)d.op, rc, (int)e.op); + if (rc || d.op != e.op) return; + check(e.sf < 0 || (int)d.sf == e.sf, "%s: %08x sf %d want %d", what, w, d.sf, e.sf); + check(e.rd < 0 || d.rd == e.rd, "%s: %08x rd/rt %u want %d", what, w, d.rd, e.rd); + check(e.rn < 0 || d.rn == e.rn, "%s: %08x rn %u want %d", what, w, d.rn, e.rn); + check(e.rm < 0 || d.rm == e.rm, "%s: %08x rm %u want %d", what, w, d.rm, e.rm); + check(e.imm16 < 0 || d.imm16 == e.imm16, "%s: %08x imm16 %u want %d", what, w, d.imm16, e.imm16); + check(e.hw < 0 || d.hw == e.hw, "%s: %08x hw %u want %d", what, w, d.hw, e.hw); + check(e.br < 0 || d.branch_imm == e.br, "%s: %08x branch field %d want %d", what, w, d.branch_imm, e.br); + check(e.cond < 0 || (int)d.cond == e.cond, "%s: %08x cond %d want %d", what, w, (int)d.cond, e.cond); +} + +typedef int (*Emit3)(uint8_t *, size_t *, size_t, bool, uint8_t, uint8_t, uint8_t); +typedef uint32_t (*Old3)(bool, uint8_t, uint8_t, uint8_t); + +static void diff_reg3(const char *name, Emit3 f, Old3 o, DecodedOp op) { + for (int sf = 0; sf < 2; sf++) + for (int rd = 0; rd < 32; rd++) + for (int rn = 0; rn < 32; rn++) + for (int rm = 0; rm < 32; rm++) { /* exhaustive: 65536 words */ + uint32_t w = word_of(EMIT(f(B, sf, rd, rn, rm)), name); + check(w == o(sf, rd, rn, rm), "%s sf=%d rd=%d rn=%d rm=%d: %08x != old %08x", name, sf, rd, rn, rm, w, + o(sf, rd, rn, rm)); + Want e = want(op); + e.sf = sf; e.rd = rd; e.rn = rn; e.rm = rm; + dcheck(name, w, e); + } +} + +static void diff_mov_reg(void) { + for (int sf = 0; sf < 2; sf++) + for (int rd = 0; rd < 32; rd++) + for (int rm = 0; rm < 32; rm++) { + uint32_t w = word_of(EMIT(aarch64_emit_mov_reg(B, sf, rd, rm)), "mov_reg"); + check(w == old_mov_reg(sf, rd, rm), "mov_reg sf=%d rd=%d rm=%d", sf, rd, rm); + Want e = want(DECODED_ORR); + e.sf = sf; e.rd = rd; e.rn = 31; e.rm = rm; + dcheck("mov_reg", w, e); + } +} + +static void diff_movzk(int k) { + const char *name = k ? "movk" : "movz"; + for (int sf = 0; sf < 2; sf++) + for (int hw = 0; hw < (sf ? 4 : 2); hw++) + for (int rd = 0; rd < 32; rd++) { + unsigned step = quick ? 4093u : 1u; /* plain: all 65536 immediates */ + for (unsigned imm = 0; imm <= 0xFFFFu; imm += step) { + uint8_t shift = (uint8_t)(16 * hw); + uint32_t w = word_of(EMIT(k ? aarch64_emit_movk(B, sf, rd, (uint16_t)imm, shift) + : aarch64_emit_movz(B, sf, rd, (uint16_t)imm, shift)), name); + uint32_t ow = k ? old_movk(sf, rd, (uint16_t)imm, shift) : old_movz(sf, rd, (uint16_t)imm, shift); + check(w == ow, "%s sf=%d rd=%d imm=%u shift=%u: %08x != old %08x", name, sf, rd, imm, shift, w, ow); + Want e = want(k ? DECODED_MOVK : DECODED_MOVZ); + e.sf = sf; e.rd = rd; e.imm16 = (int)imm; e.hw = hw; + dcheck(name, w, e); + } + if (quick) { /* the top immediate too */ + uint32_t w = word_of(EMIT(k ? aarch64_emit_movk(B, sf, rd, 0xFFFF, (uint8_t)(16 * hw)) + : aarch64_emit_movz(B, sf, rd, 0xFFFF, (uint8_t)(16 * hw))), name); + check(w == (k ? old_movk(sf, rd, 0xFFFF, (uint8_t)(16 * hw)) : old_movz(sf, rd, 0xFFFF, (uint8_t)(16 * hw))), + "%s top imm", name); + } + } +} + +static void diff_ret(void) { + uint32_t w = word_of(EMIT(aarch64_emit_ret(B)), "ret"); + check(w == old_ret(), "ret %08x", w); + Want e = want(DECODED_RET); + e.rn = 30; + dcheck("ret", w, e); +} + +static void diff_b(void) { + long n = quick ? 20000 : 400000; + for (long i = 0; i < n; i++) { + int32_t imm = (int32_t)pick(-(1LL << 25), (1LL << 25) - 1); + uint32_t w = word_of(EMIT(aarch64_emit_b(B, imm)), "b"); + check(w == old_b(imm), "b imm26=%d: %08x != old %08x", imm, w, old_b(imm)); + Want e = want(DECODED_B); + e.br = (int)((uint32_t)imm & 0x03FFFFFF); + dcheck("b", w, e); + } +} + +static void diff_b_cond(void) { + for (int c = 0; c < 16; c++) { + int32_t lo = -(1 << 18), hi = (1 << 18) - 1, step = quick ? 997 : 1; /* plain: exhaustive, 8M words */ + for (int32_t imm = lo; imm <= hi; imm += step) { + uint32_t w = word_of(EMIT(aarch64_emit_b_cond(B, (Aarch64Cond)c, imm)), "b_cond"); + check(w == old_b_cond((Aarch64Cond)c, imm), "b_cond c=%d imm19=%d", c, imm); + Want e = want(DECODED_B_COND); + e.cond = c; e.br = (int)((uint32_t)imm & 0x7FFFF); + dcheck("b_cond", w, e); + } + uint32_t w = word_of(EMIT(aarch64_emit_b_cond(B, (Aarch64Cond)c, hi)), "b_cond"); + check(w == old_b_cond((Aarch64Cond)c, hi), "b_cond c=%d hi", c); + } +} + +static void diff_cbz(int nz) { + const char *name = nz ? "cbnz" : "cbz"; + long n = quick ? 20000 : 400000; + for (long i = 0; i < n; i++) { + int sf = (int)(rnd() & 1); + uint8_t rt = (uint8_t)pick(0, 31); + int32_t imm = (int32_t)pick(-(1LL << 18), (1LL << 18) - 1); + uint32_t w = word_of(EMIT(nz ? aarch64_emit_cbnz(B, sf, rt, imm) : aarch64_emit_cbz(B, sf, rt, imm)), name); + uint32_t ow = nz ? old_cbnz(sf, rt, imm) : old_cbz(sf, rt, imm); + check(w == ow, "%s sf=%d rt=%u imm19=%d: %08x != old %08x", name, sf, rt, imm, w, ow); + Want e = want(nz ? DECODED_CBNZ : DECODED_CBZ); + e.sf = sf; e.rd = rt; e.br = (int)((uint32_t)imm & 0x7FFFF); + dcheck(name, w, e); + } +} + +static void diff_adr(void) { + long n = quick ? 20000 : 400000; + for (long i = 0; i < n; i++) { + uint8_t rd = (uint8_t)pick(0, 31); + int32_t imm = (int32_t)pick(-(1LL << 20), (1LL << 20) - 1); + uint32_t w = word_of(EMIT(aarch64_emit_adr(B, rd, imm)), "adr"); + check(w == old_adr(rd, imm), "adr rd=%u imm21=%d: %08x != old %08x", rd, imm, w, old_adr(rd, imm)); + /* architectural field check (the decoder reports only rd for ADR) */ + int32_t back = (int32_t)(((w >> 29) & 3) | (((w >> 5) & 0x7FFFF) << 2)); + back = (back ^ (1 << 20)) - (1 << 20); + check(back == imm, "adr imm21 %d reassembles as %d", imm, back); + Want e = want(DECODED_ADR); + e.rd = rd; + dcheck("adr", w, e); + } +} + +static void diff_uoff(int st) { + const char *name = st ? "str_uoff" : "ldr_uoff"; + for (int sf = 0; sf < 2; sf++) { + unsigned sz = sf ? 8u : 4u; + for (int rt = 0; rt < 32; rt++) + for (int rn = 0; rn < 32; rn++) + for (unsigned k = 0; k <= 0xFFFu; k += quick ? 409u : 1u) { /* plain: exhaustive, 8M words */ + uint16_t uoff = (uint16_t)(k * sz); + uint32_t w = word_of(EMIT(st ? aarch64_emit_str_uoff(B, sf, rt, rn, uoff) + : aarch64_emit_ldr_uoff(B, sf, rt, rn, uoff)), name); + uint32_t ow = st ? old_str_uoff(sf, rt, rn, uoff) : old_ldr_uoff(sf, rt, rn, uoff); + check(w == ow, "%s sf=%d rt=%d rn=%d uoff=%u: %08x != old %08x", name, sf, rt, rn, uoff, w, ow); + check(((w >> 10) & 0xFFF) == k, "%s imm12 field", name); + Want e = want(st ? DECODED_STR : DECODED_LDR); + e.sf = sf; e.rd = rt; e.rn = rn; + dcheck(name, w, e); + } + } +} + +static void diff_byte(int st) { + const char *name = st ? "strb_uoff" : "ldrb_uoff"; + for (int rt = 0; rt < 32; rt++) + for (int rn = 0; rn < 32; rn++) + for (unsigned uoff = 0; uoff <= 0xFFFu; uoff += quick ? 409u : 1u) { /* plain: exhaustive, 4M words */ + uint32_t w = word_of(EMIT(st ? aarch64_emit_strb_uoff(B, rt, rn, (uint16_t)uoff) + : aarch64_emit_ldrb_uoff(B, rt, rn, (uint16_t)uoff)), name); + uint32_t ow = st ? old_strb_uoff(rt, rn, (uint16_t)uoff) : old_ldrb_uoff(rt, rn, (uint16_t)uoff); + check(w == ow, "%s rt=%d rn=%d uoff=%u: %08x != old %08x", name, rt, rn, uoff, w, ow); + Want e = want(st ? DECODED_STRB : DECODED_LDRB); + e.rd = rt; e.rn = rn; + dcheck(name, w, e); + } +} + +typedef int (*EmitPost)(uint8_t *, size_t *, size_t, uint8_t, uint8_t, int16_t); +static void diff_post(const char *name, EmitPost f, uint32_t base, DecodedOp op, int sf) { + for (int rt = 0; rt < 32; rt++) + for (int rn = 0; rn < 32; rn++) + for (int s = -256; s <= 255; s += quick ? 37 : 1) { /* plain: exhaustive, 512K words */ + uint32_t w = word_of(EMIT(f(B, rt, rn, (int16_t)s)), name); + uint32_t ow = old_post(base, rt, rn, (int16_t)s); + check(w == ow, "%s rt=%d rn=%d simm9=%d: %08x != old %08x", name, rt, rn, s, w, ow); + Want e = want(op); + e.sf = sf; e.rd = rt; e.rn = rn; + dcheck(name, w, e); + } +} + +static void diff_subs_imm(void) { + for (int sf = 0; sf < 2; sf++) + for (int rd = 0; rd < 32; rd++) + for (int rn = 0; rn < 32; rn++) + for (unsigned imm = 0; imm <= 0xFFFu; imm += quick ? 409u : 1u) { /* plain: exhaustive, 8M words */ + uint32_t w = word_of(EMIT(aarch64_emit_subs_imm(B, sf, rd, rn, (uint16_t)imm)), "subs_imm"); + check(w == old_subs_imm(sf, rd, rn, (uint16_t)imm), "subs_imm sf=%d rd=%d rn=%d imm=%u", sf, rd, rn, imm); + Want e = want(DECODED_SUBS); + e.sf = sf; e.rd = rd; e.rn = rn; + dcheck("subs_imm", w, e); + } +} + +/* ---- refusal path ---- */ +typedef struct { + int id; /* which call */ + long long a, b, c, d; /* arguments */ + const char *expect; /* exact stderr */ +} Refusal; + +static void call(const Refusal *r, uint8_t *buf, size_t *pos) { + switch (r->id) { +#define R3(n, fn) case n: fn(buf, pos, 8, (bool)r->a, (uint8_t)r->b, (uint8_t)r->c, (uint8_t)r->d); break; + R3(0, aarch64_emit_add_reg) R3(1, aarch64_emit_sub_reg) R3(2, aarch64_emit_mul_reg) R3(3, aarch64_emit_and_reg) + R3(4, aarch64_emit_orr_reg) R3(5, aarch64_emit_eor_reg) R3(6, aarch64_emit_subs_reg) +#undef R3 + case 7: aarch64_emit_mov_reg(buf, pos, 8, (bool)r->a, (uint8_t)r->b, (uint8_t)r->c); break; + case 8: aarch64_emit_movz(buf, pos, 8, (bool)r->a, (uint8_t)r->b, (uint16_t)r->c, (uint8_t)r->d); break; + case 9: aarch64_emit_movk(buf, pos, 8, (bool)r->a, (uint8_t)r->b, (uint16_t)r->c, (uint8_t)r->d); break; + case 10: aarch64_emit_b(buf, pos, 8, (int32_t)r->a); break; + case 11: aarch64_emit_b_cond(buf, pos, 8, (Aarch64Cond)r->a, (int32_t)r->b); break; + case 12: aarch64_emit_cbz(buf, pos, 8, (bool)r->a, (uint8_t)r->b, (int32_t)r->c); break; + case 13: aarch64_emit_cbnz(buf, pos, 8, (bool)r->a, (uint8_t)r->b, (int32_t)r->c); break; + case 14: aarch64_emit_adr(buf, pos, 8, (uint8_t)r->a, (int32_t)r->b); break; + case 15: aarch64_emit_ldr_uoff(buf, pos, 8, (bool)r->a, (uint8_t)r->b, (uint8_t)r->c, (uint16_t)r->d); break; + case 16: aarch64_emit_str_uoff(buf, pos, 8, (bool)r->a, (uint8_t)r->b, (uint8_t)r->c, (uint16_t)r->d); break; + case 17: aarch64_emit_ldrb_uoff(buf, pos, 8, (uint8_t)r->a, (uint8_t)r->b, (uint16_t)r->c); break; + case 18: aarch64_emit_strb_uoff(buf, pos, 8, (uint8_t)r->a, (uint8_t)r->b, (uint16_t)r->c); break; + case 19: aarch64_emit_ldr_post(buf, pos, 8, (uint8_t)r->a, (uint8_t)r->b, (int16_t)r->c); break; + case 20: aarch64_emit_str_post(buf, pos, 8, (uint8_t)r->a, (uint8_t)r->b, (int16_t)r->c); break; + case 21: aarch64_emit_ldr_x_post(buf, pos, 8, (uint8_t)r->a, (uint8_t)r->b, (int16_t)r->c); break; + case 22: aarch64_emit_str_x_post(buf, pos, 8, (uint8_t)r->a, (uint8_t)r->b, (int16_t)r->c); break; + case 23: aarch64_emit_subs_imm(buf, pos, 8, (bool)r->a, (uint8_t)r->b, (uint8_t)r->c, (uint16_t)r->d); break; + } +} + +static const Refusal refusals[] = { + {0, 1, 32, 0, 0, "aarch64_emit_add_reg rd=32"}, {0, 1, 0, 255, 0, "aarch64_emit_add_reg rn=255"}, + {0, 0, 0, 0, 33, "aarch64_emit_add_reg rm=33"}, {1, 1, 40, 0, 0, "aarch64_emit_sub_reg rd=40"}, + {1, 1, 0, 32, 0, "aarch64_emit_sub_reg rn=32"}, {1, 1, 0, 0, 32, "aarch64_emit_sub_reg rm=32"}, + {2, 1, 32, 0, 0, "aarch64_emit_mul_reg rd=32"}, {2, 1, 0, 32, 0, "aarch64_emit_mul_reg rn=32"}, + {2, 1, 0, 0, 128, "aarch64_emit_mul_reg rm=128"}, {3, 1, 32, 0, 0, "aarch64_emit_and_reg rd=32"}, + {3, 1, 0, 32, 0, "aarch64_emit_and_reg rn=32"}, {3, 1, 0, 0, 32, "aarch64_emit_and_reg rm=32"}, + {4, 1, 32, 0, 0, "aarch64_emit_orr_reg rd=32"}, {4, 1, 0, 32, 0, "aarch64_emit_orr_reg rn=32"}, + {4, 1, 0, 0, 32, "aarch64_emit_orr_reg rm=32"}, {5, 1, 32, 0, 0, "aarch64_emit_eor_reg rd=32"}, + {5, 1, 0, 32, 0, "aarch64_emit_eor_reg rn=32"}, {5, 1, 0, 0, 32, "aarch64_emit_eor_reg rm=32"}, + {6, 1, 32, 0, 0, "aarch64_emit_subs_reg rd=32"}, {6, 1, 0, 32, 0, "aarch64_emit_subs_reg rn=32"}, + {6, 1, 0, 0, 32, "aarch64_emit_subs_reg rm=32"}, {7, 1, 32, 0, 0, "aarch64_emit_mov_reg rd=32"}, + {7, 1, 0, 32, 0, "aarch64_emit_mov_reg rm=32"}, + {8, 1, 32, 0, 0, "aarch64_emit_movz rd=32"}, {8, 1, 0, 0, 8, "aarch64_emit_movz shift=8"}, + {8, 1, 0, 0, 17, "aarch64_emit_movz shift=17"}, {8, 1, 0, 0, 64, "aarch64_emit_movz shift=64"}, + {8, 0, 0, 0, 32, "aarch64_emit_movz shift=32"}, {8, 0, 0, 0, 48, "aarch64_emit_movz shift=48"}, + {9, 1, 32, 0, 16, "aarch64_emit_movk rd=32"}, {9, 1, 0, 0, 1, "aarch64_emit_movk shift=1"}, + {9, 1, 0, 0, 80, "aarch64_emit_movk shift=80"}, {9, 0, 0, 0, 32, "aarch64_emit_movk shift=32"}, + {10, 1LL << 25, 0, 0, 0, "aarch64_emit_b imm26=33554432"}, + {10, -(1LL << 25) - 1, 0, 0, 0, "aarch64_emit_b imm26=-33554433"}, + {10, 2147483647LL, 0, 0, 0, "aarch64_emit_b imm26=2147483647"}, + {10, -2147483647LL - 1, 0, 0, 0, "aarch64_emit_b imm26=-2147483648"}, + {11, 16, 0, 0, 0, "aarch64_emit_b_cond cond=16"}, {11, -1, 0, 0, 0, "aarch64_emit_b_cond cond=-1"}, + {11, 0, 1 << 18, 0, 0, "aarch64_emit_b_cond imm19=262144"}, + {11, 1, -(1 << 18) - 1, 0, 0, "aarch64_emit_b_cond imm19=-262145"}, + {12, 1, 32, 0, 0, "aarch64_emit_cbz rt=32"}, {12, 1, 0, 1 << 18, 0, "aarch64_emit_cbz imm19=262144"}, + {12, 0, 0, -(1 << 18) - 1, 0, "aarch64_emit_cbz imm19=-262145"}, + {13, 1, 32, 0, 0, "aarch64_emit_cbnz rt=32"}, {13, 1, 0, 1 << 18, 0, "aarch64_emit_cbnz imm19=262144"}, + {13, 0, 0, -(1 << 18) - 1, 0, "aarch64_emit_cbnz imm19=-262145"}, + {14, 32, 0, 0, 0, "aarch64_emit_adr rd=32"}, {14, 0, 1 << 20, 0, 0, "aarch64_emit_adr imm21=1048576"}, + {14, 0, -(1 << 20) - 1, 0, 0, "aarch64_emit_adr imm21=-1048577"}, + {15, 1, 32, 0, 0, "aarch64_emit_ldr_uoff rt=32"}, {15, 1, 0, 32, 0, "aarch64_emit_ldr_uoff rn=32"}, + {15, 1, 0, 0, 4, "aarch64_emit_ldr_uoff uoff=4"}, {15, 1, 0, 0, 32768, "aarch64_emit_ldr_uoff uoff=32768"}, + {15, 0, 0, 0, 2, "aarch64_emit_ldr_uoff uoff=2"}, {15, 0, 0, 0, 16384, "aarch64_emit_ldr_uoff uoff=16384"}, + {15, 1, 0, 0, 65535, "aarch64_emit_ldr_uoff uoff=65535"}, + {16, 1, 32, 0, 0, "aarch64_emit_str_uoff rt=32"}, {16, 1, 0, 32, 0, "aarch64_emit_str_uoff rn=32"}, + {16, 1, 0, 0, 12, "aarch64_emit_str_uoff uoff=12"}, {16, 1, 0, 0, 32768, "aarch64_emit_str_uoff uoff=32768"}, + {16, 0, 0, 0, 6, "aarch64_emit_str_uoff uoff=6"}, {16, 0, 0, 0, 16384, "aarch64_emit_str_uoff uoff=16384"}, + {17, 32, 0, 0, 0, "aarch64_emit_ldrb_uoff rt=32"}, {17, 0, 32, 0, 0, "aarch64_emit_ldrb_uoff rn=32"}, + {17, 0, 0, 4096, 0, "aarch64_emit_ldrb_uoff uoff=4096"}, + {17, 0, 0, 65535, 0, "aarch64_emit_ldrb_uoff uoff=65535"}, + {18, 32, 0, 0, 0, "aarch64_emit_strb_uoff rt=32"}, {18, 0, 32, 0, 0, "aarch64_emit_strb_uoff rn=32"}, + {18, 0, 0, 4096, 0, "aarch64_emit_strb_uoff uoff=4096"}, + {19, 32, 0, 0, 0, "aarch64_emit_ldr_post rt=32"}, {19, 0, 32, 0, 0, "aarch64_emit_ldr_post rn=32"}, + {19, 0, 0, 256, 0, "aarch64_emit_ldr_post simm9=256"}, + {19, 0, 0, -257, 0, "aarch64_emit_ldr_post simm9=-257"}, + {20, 32, 0, 0, 0, "aarch64_emit_str_post rt=32"}, {20, 0, 32, 0, 0, "aarch64_emit_str_post rn=32"}, + {20, 0, 0, 256, 0, "aarch64_emit_str_post simm9=256"}, + {20, 0, 0, -32768, 0, "aarch64_emit_str_post simm9=-32768"}, + {21, 32, 0, 0, 0, "aarch64_emit_ldr_x_post rt=32"}, {21, 0, 32, 0, 0, "aarch64_emit_ldr_x_post rn=32"}, + {21, 0, 0, 512, 0, "aarch64_emit_ldr_x_post simm9=512"}, + {21, 0, 0, -257, 0, "aarch64_emit_ldr_x_post simm9=-257"}, + {22, 32, 0, 0, 0, "aarch64_emit_str_x_post rt=32"}, {22, 0, 32, 0, 0, "aarch64_emit_str_x_post rn=32"}, + {22, 0, 0, 32767, 0, "aarch64_emit_str_x_post simm9=32767"}, + {22, 0, 0, -257, 0, "aarch64_emit_str_x_post simm9=-257"}, + {23, 1, 32, 0, 0, "aarch64_emit_subs_imm rd=32"}, {23, 1, 0, 32, 0, "aarch64_emit_subs_imm rn=32"}, + {23, 1, 0, 0, 4096, "aarch64_emit_subs_imm imm12=4096"}, + {23, 0, 0, 0, 65535, "aarch64_emit_subs_imm imm12=65535"}, +}; + +typedef struct { uint8_t buf[8]; size_t pos; } Shared; + +static void refusal_case(const Refusal *r, Shared *sh) { + char want[160], got[256]; + snprintf(want, sizeof want, "A64_LEGACY_FIELD_OUT_OF_RANGE %s\n", r->expect); + memset(sh->buf, 0xA5, sizeof sh->buf); + sh->pos = 0; + int fd[2]; + if (pipe(fd) != 0) { check(0, "pipe"); return; } + fflush(stdout); + pid_t pid = fork(); + if (pid == 0) { + close(fd[0]); + dup2(fd[1], 2); + struct rlimit nocore = {0, 0}; + setrlimit(RLIMIT_CORE, &nocore); /* the expected abort must not write a core dump */ + prctl(PR_SET_DUMPABLE, 0, 0, 0, 0); + call(r, sh->buf, &sh->pos); + _exit(0); /* reached only if the emitter did not refuse */ + } + close(fd[1]); + size_t n = 0; + ssize_t k; + while (n < sizeof got - 1 && (k = read(fd[0], got + n, sizeof got - 1 - n)) > 0) n += (size_t)k; + got[n] = '\0'; + close(fd[0]); + int st = 0; + waitpid(pid, &st, 0); + n_refusal_cases++; + check(WIFSIGNALED(st) && WTERMSIG(st) == SIGABRT, "refusal %s: child not aborted (status %d)", r->expect, st); + check(strcmp(got, want) == 0, "refusal %s: stderr \"%s\"", r->expect, got); + int untouched = sh->pos == 0; + for (size_t i = 0; i < sizeof sh->buf; i++) untouched &= sh->buf[i] == 0xA5; + check(untouched, "refusal %s: buffer or position written", r->expect); + if (WIFSIGNALED(st) && WTERMSIG(st) == SIGABRT && strcmp(got, want) == 0 && untouched) n_refusal_ok++; +} + +int main(int argc, char **argv) { + quick = argc > 1 && strcmp(argv[1], "quick") == 0; + diff_reg3("add_reg", aarch64_emit_add_reg, old_add_reg, DECODED_ADD); + diff_reg3("sub_reg", aarch64_emit_sub_reg, old_sub_reg, DECODED_SUB); + diff_reg3("mul_reg", aarch64_emit_mul_reg, old_mul_reg, DECODED_MUL); + diff_reg3("and_reg", aarch64_emit_and_reg, old_and_reg, DECODED_AND); + diff_reg3("orr_reg", aarch64_emit_orr_reg, old_orr_reg, DECODED_ORR); + diff_reg3("eor_reg", aarch64_emit_eor_reg, old_eor_reg, DECODED_EOR); + diff_reg3("subs_reg", aarch64_emit_subs_reg, old_subs_reg, DECODED_SUBS); + diff_mov_reg(); + diff_movzk(0); + diff_movzk(1); + diff_ret(); + diff_b(); + diff_b_cond(); + diff_cbz(0); + diff_cbz(1); + diff_adr(); + diff_uoff(0); + diff_uoff(1); + diff_byte(0); + diff_byte(1); + diff_post("ldr_post", aarch64_emit_ldr_post, 0xB8400400, DECODED_LDR, 0); + diff_post("str_post", aarch64_emit_str_post, 0xB8000400, DECODED_STR, 0); + diff_post("ldr_x_post", aarch64_emit_ldr_x_post, 0xF8400400, DECODED_LDR, 1); + diff_post("str_x_post", aarch64_emit_str_x_post, 0xF8000400, DECODED_STR, 1); + diff_subs_imm(); + unsigned long diff_fail = n_fail; + + /* buffer-full is unchanged: -1, nothing written */ + { + uint8_t b[4] = {1, 2, 3, 4}; + size_t p = 1; + check(aarch64_emit_ret(b, &p, 4) == -1 && p == 1 && b[1] == 2, "buffer-full path changed"); + } + + Shared *sh = mmap(NULL, sizeof(Shared), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); + if (sh == MAP_FAILED) { puts("FAIL: mmap"); puts("OSC2_LEGACY_A64_FAIL"); return 1; } + size_t nref = sizeof refusals / sizeof refusals[0]; + for (size_t i = 0; i < nref; i++) refusal_case(&refusals[i], sh); + munmap(sh, sizeof(Shared)); + + printf("legacy a64 differential: emitters=25 mode=%s words=%lu decoded=%lu mismatches=%lu\n", quick ? "quick" : "full", + n_words, n_decoded, diff_fail); + printf("legacy a64 refusal: cases=%lu named_abort_and_no_write=%lu\n", n_refusal_cases, n_refusal_ok); + printf("checks=%lu failures=%lu\n", n_checks, n_fail); + puts(n_fail ? "OSC2_LEGACY_A64_FAIL" : "OSC2_LEGACY_A64_PASS"); + return n_fail ? 1 : 0; +}