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#166 (code only)
Apply it
git checkout -b patch-5831afb69b7efa5f 3108fc2f5d499a76d6ed72f53681c3e2ba9281a4 curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/5831afb69b7efa5f.patch | git am
Provenance
github-import 1
aien-dev/omega-mv
166
92b57752c1d55ba82f03f931076d3a60bd42a325
{"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":166,"url":"https://github.com/aien-dev/omega-mv/pull/166","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:35Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/docs/osc/OSC-3-DESIGN.md b/docs/osc/OSC-3-DESIGN.md index b8f194562..7b3f23d08 100644 --- a/docs/osc/OSC-3-DESIGN.md +++ b/docs/osc/OSC-3-DESIGN.md @@ -55,6 +55,136 @@ has its own post-merge result (the 2026-10-01 audit found the compiler suites never ran after merge). An earlier draft of this item added a separate `compiler-main.yml` for this; it was dropped as redundant after #154. +## Item 2: versioned handles + +The OSC-0B model (`src/compiler/model/osc_model.h`) gives slots u64 +generations with `SLOT_ALLOC` / `SLOT_FREE` / `HANDLE_USE` events: a use or +free with the wrong generation is `STALE_GENERATION`, and a slot freed at +`gen_max` is retired, never wrapped (`GENERATION_WRAP`). Item 2 compiles that +discipline. + +### Subset + +- A pool is a fixed-capacity block of K slots (1..16) of one scalar type, + local to one function. It is declared as a block statement and closed at + the end of that block (also on a `return` from inside it), like an arena. +- A handle is a (slot, u64 generation) pair of one pool. Handles are + copyable values and never owners: copying a handle does not move + anything, a handle going out of scope frees nothing, and closing the pool + ends every slot whatever handles exist. +- Pools and handles do not cross function boundaries (parameters, returns), + are not struct fields or array elements, and are never values in + arithmetic, comparisons or `return`. +- The subset has no durable encoder: no handle (and no borrow) can be + written to durable state. The model's `LIVE_HANDLE_DURABLE` refusal is + therefore unreachable from compiled code; a durable writer for handles + must name that refusal before it exists. + +### Grammar + +``` +stmt += 'pool' NAME ':' '[' scalar ';' INT ']' ( 'gen' INT )? block + | 'let' 'mut'? NAME ':' 'handle' NAME '=' hexpr ';' + | NAME '.' 'free' '(' NAME ')' ';' + | NAME '[' NAME ']' '=' expr ';' (pool write) + | NAME '=' hexpr ';' (handle rebind, NAME mut) +hexpr := NAME '.' 'alloc' '(' expr ')' | NAME (a handle of the same pool) +expr += NAME '[' NAME ']' (pool read) +``` + +`gen INT` declares the generation base (default 0): every slot starts at +that generation. It exists so tests can reach exhaustion with a few +allocations; the base is part of the unit's canonical encoding. + +### Semantics + +- `p.alloc(e)` takes the lowest free slot, stores `e`, and yields + (slot, current generation of that slot). +- `p.free(h)` checks h, then advances the slot's generation; at + 2^64 - 1 the slot is retired instead and is never allocated again. +- `p[h]` and `p[h] = e` check h: the slot must be live and its generation + must equal h's. +- Run-time traps (identical in the interpreter and native AArch64, which + share the runtime through fixed function-pointer offsets 48..96): + `STALE` (wrong generation or slot not live, on use or free), + `RETIRED` (alloc with no free slot while some slot is retired) and + `POOL_FULL` (alloc with every slot live). +- Every pool operation is logged (`POOL_OPEN`, `SLOT_ALLOC`, `SLOT_FREE`, + `HANDLE_USE` with rights 1 = read / 2 = write, `POOL_CLOSE`) with its + generation folded into the event hash. The test harness replays each + native run's log through `osc_model` (one model per pool, base = declared + base, max = 2^64 - 1) and requires rejected = 0; for a trapping run the + prefix replays clean and the trap itself is confirmed by the model + (`STALE` as `STALE_GENERATION`, `RETIRED` as `GENERATION_WRAP` on every + slot, `POOL_FULL` as `PROTOCOL` on every slot). + +### Static refusals (named) + +The checker tracks a provenance per allocation; copies share it. A +provenance is freed when every path freed it (branch merge intersects). + +| Diagnostic | Transition | When | +|---|---|---| +| `STALE_HANDLE` | read / write after free | use of a handle whose slot was freed on every path | +| `STALE_HANDLE` | ... via a copy | the same through a copy made before the free | +| `STALE_HANDLE` | free after free | double free (also via a copy) | +| `GENERATION_EXHAUSTED` | retired slot reused | definite allocations exceed K x (2^64 - base) | +| `POOL_CAPACITY` | pool full | definite live allocations exceed K | +| `POOL_CAPACITY` | pool size outside 1..16 | K outside 1..16 | +| `WRONG_POOL` | handle of another pool | handle of pool p used with pool q, or `q.alloc` bound as a handle of p | +| `TYPE_MISMATCH` | handle / pool used as a value; pool index not a handle | type confusion | +| `UNSUPPORTED` | handle crosses a function boundary | pool or handle parameter or return type | +| `IMMUTABLE_ASSIGN` | | rebinding a handle declared without `mut` | + +"Definite" means at the pool's own loop and branch depth. Allocations inside +loops or branches are not counted; once a free happens behind a branch or +loop, the live count is no longer tracked statically (the generation count +still is). What the checker cannot decide is checked at run time (the traps +above). + +### Limits + +- 16 slots per pool, 8 pools open at once per run, scalar elements only. +- No handle parameters or returns, no handles in structs or arrays, no + durable encoding (see Subset). +- The model replay has 64 handle ids; the harness re-windows the model + (fresh model seeded with the live slots and their generations) past that. +- Serial counters (audit finding): the runtime's object/region serial + (`next_serial`) and the pool instance serial (`pool_serial`) stay `uint32_t` + (they are hashed into the event log as 4 bytes, so widening them would + change every logged run digest). Instead they never wrap: an allocation, + arena open, arena allocation or pool open with its counter at `UINT32_MAX` + traps `RUNTIME` before any state changes, identically in the interpreter and + native code (both call the same runtime). The handle fuzz leg checks all + four sites directly. Handle generations themselves are `u64` and retire at + 2^64 - 1 (above). + +### Evidence + +Measured on the Spark, single core, `nice` (full counts = `make +test-compiler-full`, identical plain and under ASan/UBSan): + +``` +osc3 handles: units=125 compiled=102 static_refused=23 runs=4896 ok=2705 stale_traps=1616 pool_full=530 retired=45 overflow=0 mismatches=0 +osc3 handles: model replay pool_events=48807 stale_confirmed=1616 wrap_confirmed=45 full_confirmed=530 windows=0 (handle fuzz runs only) +osc3 handles: serial counters at UINT32_MAX trap RUNTIME, never wrap: 4 of 4 +runtime model replay: ... rejected=0 +``` + +- Every run is executed by the interpreter and natively; outcomes (return + value or trap, runtime state, event log with generations) are identical. +- Every native run's event log replays through `osc_model`; every STALE, + RETIRED and POOL_FULL trap is confirmed by the model on the replayed + prefix (counts equal the trap counts above). The `cycle` golden (100 + allocations from one slot) exercises the model re-window. +- Static refusals in the fuzz are only `STALE_HANDLE` (any other refusal + fails the leg). +- Goldens: `handles_basic`, `handles_stale`, `handles_exhaust` (13 + `expect-run` lines). Negatives: 15 `h_*` programs, one per refusal above. +- OSC-1/OSC-2 golden programs compile byte-identically (IR and code digests + checked by the item 2 receipt against the OSC-2 baseline). +- The exact receipt is under `evidence/OSC-3/receipts/osc3-handles-*.json`. + ## Generation width: remaining 32-bit sites and fix plan ADR OMEGA-SYSTEMS-CORE-0000 decision 1 fixes capability and object generations @@ -72,12 +202,12 @@ truncated generation can make a stale handle look live again. | `src/runtime/rx_graph.c:517, :529` crumb/graph hash writes object generations with `put32` | u32 on the wire | runtime lane | `put64` together with a crumb/graph record version bump (old records refused, not reinterpreted), in the identity-break sequence (item 5) | | `src/runtime/rx_jspace.c:1406, :1413, :1414` J-Space body writes branch/slot generations with `put32` | u32 on the wire | runtime lane | `put64` with a J-Space record version bump, same rule | | `src/omega_accelerator.h:67, :87` `uint32_t capability_generation` | u32 | accelerator owner (not OSC) | widen to u64, retire at max; ADR decision 1 names this file | -| `src/compiler/osc_rt.h:69-70` `slot_serial[]`, `next_serial` (`++next_serial`, no overflow check) | u32 | OSC (this lane) | item 2: handle generations are u64 with retire-at-max; the region serial is widened or traps on wrap (stated in the item 2 section) | +| `src/compiler/osc_rt.h` `slot_serial[]`, `next_serial`, `pool_serial` | u32, never wraps (item 2) | OSC (this lane) | done in item 2: handle generations are u64 with retire-at-max; the serials stay u32 (hashed into the event log as 4 bytes) and an allocation, arena open, arena allocation or pool open with its counter at UINT32_MAX traps RUNTIME before any state changes (item 2, Limits) | | AIENOS ADR 0013 (`index u32; generation u32`, retire at u32::MAX) | u32 | aienos lane | request only, see item 5 (`docs/osc/OSC-3-ADR0013-REQUEST.md`); not edited from this repository | Only the `osc_rt` row is inside this lane's files. The others are listed so the owning lanes can schedule them; each is a versioned change with old records -refused, never reinterpreted. None is applied by OSC-3. +refused, never reinterpreted. Only the `osc_rt` row is applied by OSC-3 (item 2). ## Slice order after item 4 diff --git a/evidence/OSC-3/receipts/osc3-handles-9b03e5e2eb542171e7f0d2c26ce8a2fbbd472708b31312896e998b6440ac6fa5.json b/evidence/OSC-3/receipts/osc3-handles-9b03e5e2eb542171e7f0d2c26ce8a2fbbd472708b31312896e998b6440ac6fa5.json new file mode 100644 index 000000000..00d5c1485 --- /dev/null +++ b/evidence/OSC-3/receipts/osc3-handles-9b03e5e2eb542171e7f0d2c26ce8a2fbbd472708b31312896e998b6440ac6fa5.json @@ -0,0 +1,86 @@ +{ + "schema": "omega.osc3.compiler.receipt.v1", + "verdict": "PASS", + "item": "handles", + "statement": "OSC-3 slice; not a general Omega compiler; no self-hosting.", + "design": "docs/osc/OSC-3-DESIGN.md", + "repo_commit": "bfd55d4bfba3a33f9ef40d82e4ed3fada6c5cc55", + "base_commit": "38a6dea0882b310e736da07da496984a5add16bc", + "tree_clean": true, + "host_arch": "aarch64", + "c_compiler": "cc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0", + "test_compiler_full": "PASS", + "test_compiler_quick": {"result": "PASS", "wall_seconds": 27, "budget_seconds": 60, "note": "nice, one make job; smoke counts, not evidence"}, + "osc0b_model": "OSC0B_MODEL_SEED=05c0b5eed0010001 SEQUENCES=1000000 STEPS=21905422 FALSE_ACCEPTS=0 WRONG_NAME=0 FALSE_REJECTS=0", + "compiler": "golden=40/40 funcs=137 entries=100 expect_runs=113 diff_runs=128032 trace_replays=97 rt_replays=74776 rt_events=705521 neg=111/111 neg_traced=23 checks=350342 failed=0", + "compiler_asan": "golden=40/40 funcs=137 entries=100 expect_runs=113 diff_runs=128032 trace_replays=97 rt_replays=74776 rt_events=705521 neg=111/111 neg_traced=23 checks=350342 failed=0", + "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", + "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", + "struct_fuzz_asan": "struct fuzz: units=125 runs=6000 ok=3805 bounds=1087 requires=155 overflow=224 cast=729 mismatches=0", + "arena_fuzz": "arena fuzz: units=125 allocs=557 runs=6000 ok=2542 arena_full=2606 bounds=639 overflow=213 mismatches=0", + "arena_fuzz_asan": "arena fuzz: units=125 allocs=557 runs=6000 ok=2542 arena_full=2606 bounds=639 overflow=213 mismatches=0", + "runtime_model_replay": "runtime model replay: runs=128147 events=705521 accepted=128147 rejected=0", + "runtime_model_replay_asan": "runtime model replay: runs=128147 events=705521 accepted=128147 rejected=0", + "legacy_a64_differential": "legacy a64 differential: emitters=25 mode=full words=71266833 decoded=71266817 mismatches=0", + "item_evidence": [ + "plain: osc3 handles: units=125 compiled=102 static_refused=23 runs=4896 ok=2705 stale_traps=1616 pool_full=530 retired=45 overflow=0 mismatches=0", + "plain: osc3 handles: model replay pool_events=48807 stale_confirmed=1616 wrap_confirmed=45 full_confirmed=530 windows=0 (handle fuzz runs only); all runs re-windows=1099", + "plain: osc3 handles: serial counters at UINT32_MAX trap RUNTIME, never wrap: 4 of 4", + "asan: osc3 handles: units=125 compiled=102 static_refused=23 runs=4896 ok=2705 stale_traps=1616 pool_full=530 retired=45 overflow=0 mismatches=0", + "asan: osc3 handles: model replay pool_events=48807 stale_confirmed=1616 wrap_confirmed=45 full_confirmed=530 windows=0 (handle fuzz runs only); all runs re-windows=1099", + "asan: osc3 handles: serial counters at UINT32_MAX trap RUNTIME, never wrap: 4 of 4" + ], + "determinism": "programs=40 identical=40", + "golden_programs": 40, + "negative_programs": 111, + "osc2_baseline_receipt": "evidence/OSC-2/receipts/osc2-encoder-cb452b9889ad972086f304448645168791c52ec7a33eff284a306da237eca1c9.json", + "osc2_programs_identical": "37 of 37", + "osc2_programs_changed": [], + "golden_corpus_sha256": "5868ebfa8e9105bb701f7163e5eb6138d8f98191ad4854cb0af9ee27f3f4f69c", + "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/handles_basic.osc", "source_sha256": "ec4262e413dc353c2d93edfa8c4ae27246ff143724b6f872bf8b5181aa2652a1", "ir_sha256": "8e0b9bc0345de5061f67a07795953d77b1104d797f1afafde65b28173f1e7e58", "code_sha256": "1a5c0e615cfa6227e0f0c99b19c6018c9e607fdf6a382667c15a39d30be11990"}, + {"program": "tests/compiler/progs/handles_exhaust.osc", "source_sha256": "b3e5c3eedcae3889ccd638a7ad2ad9c0f35e5ec6ba729de726cc0d332bc57708", "ir_sha256": "633d35faff6c5f6e73fbedc4633116650c197e68e0441097d83be8928f76434e", "code_sha256": "b238148e76054dfb07ba9aeaaf9fe92c1ffcad9759cb77a2222e318031bf832f"}, + {"program": "tests/compiler/progs/handles_stale.osc", "source_sha256": "f3356bbaefc37933399e0d24c58b80b85ec207138b11c38e65c8ba8f4c7e180f", "ir_sha256": "3dd7da141b54dd7cf647eb265b89ba8532bc07259b3e2dc6e077107307c01663", "code_sha256": "8ceb40b62bc65b1dd5450c36729b27ddeb3430038eb3efe6edaa7934e85e7a58"}, + {"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"} + ], + "m6_m9_m14_gates": {"m6": {"total": 10, "passed": 10, "failed": 0, "gate_log_sha256": "7fcc69abb701878b6a400b9ebdceb83bcb98496c8b884b01a2b7587d5bd8b178", "identical_to_osc2_baseline": true}, "m9": {"total": 10, "passed": 10, "failed": 0, "gate_log_sha256": "4d8a42eab694e06c2ee49accf9e7325a59912f80d74358bdabbfd57f2301f184", "identical_to_osc2_baseline": true}, "m14": {"total": 10, "passed": 10, "failed": 0, "gate_log_sha256": "5e96e93e2d5b381917fb145f1f6d4757f0fd311361d39bc6f24bb82ec8d17e35", "identical_to_osc2_baseline": true}}, + "self_host": "no: the OSC-3 compiler slice cannot compile any part of itself (docs/osc/OSC-1-SELF-HOST-STATEMENT.md)", + "timing_measured": "quick wall time only" +} diff --git a/src/compiler/osc_cg.c b/src/compiler/osc_cg.c index c551b4962..54b1f81ca 100644 --- a/src/compiler/osc_cg.c +++ b/src/compiler/osc_cg.c @@ -385,6 +385,44 @@ static void gen_func(Cg *g, const OscUnit *u, int fi) { ld(g, 1, in->a); rt_call(g, 40); break; + /* OSC-3 item 2 pools: calls into the shared runtime */ + case OSC_I_POPEN: /* x0 = rt->pool_open(rt, K, base) */ + load_rt(g, 0); + mat(g, 1, in->nargs); + mat(g, 2, in->imm); + rt_call(g, 48); + st(g, 0, in->dst); + break; + case OSC_I_PCLOSE: /* rt->pool_close(rt, pool) */ + load_rt(g, 0); + ld(g, 1, in->a); + rt_call(g, 56); + break; + case OSC_I_HALLOC: /* x0 = rt->h_alloc(rt, pool, init) */ + load_rt(g, 0); + ld(g, 1, in->b); + ld(g, 2, in->a); + rt_call(g, 64); + st(g, 0, in->dst); + break; + case OSC_I_HGEN: /* x0 = rt->h_gen(rt, pool, slot) */ + load_rt(g, 0); + ld(g, 1, in->a); + ld(g, 2, in->b); + rt_call(g, 72); + st(g, 0, in->dst); + break; + case OSC_I_HFREE: /* rt->h_free(rt, pool, slot, gen) */ + case OSC_I_HLOAD: /* x0 = rt->h_load(rt, pool, slot, gen) */ + case OSC_I_HSTORE: /* rt->h_store(rt, pool, slot, gen, value) */ + load_rt(g, 0); + ld(g, 1, in->a); + ld(g, 2, in->b); + ld(g, 3, in->c); + if (in->op == OSC_I_HSTORE) ld(g, 4, in->args[0]); + rt_call(g, in->op == OSC_I_HFREE ? 80 : in->op == OSC_I_HLOAD ? 88 : 96); + if (in->op == OSC_I_HLOAD) st(g, 0, in->dst); + break; case OSC_I_LOAD: case OSC_I_STORE: ld(g, 9, in->a); diff --git a/src/compiler/osc_check.c b/src/compiler/osc_check.c index f4406b749..ae92a6c54 100644 --- a/src/compiler/osc_check.c +++ b/src/compiler/osc_check.c @@ -10,7 +10,11 @@ #include <stdlib.h> #include <string.h> -enum { SK_SCALAR = 1, SK_OWNER, SK_BORROW, SK_ARENA /* OSC-2 arenas */ }; +enum { SK_SCALAR = 1, SK_OWNER, SK_BORROW, SK_ARENA /* OSC-2 arenas */, + SK_POOL, SK_HANDLE /* OSC-3 item 2: versioned handles */ }; + +/* OSC-3 item 2: static provenances (one per checked `P.alloc` evaluation) */ +#define MAX_PROV 1024 typedef struct { char name[64]; @@ -27,8 +31,24 @@ typedef struct { uint32_t region; uint16_t cap, used; int cond_depth; + /* OSC-3 item 2. SK_POOL: cap = K slots, gbase = declared generation base, + * dallocs = definite allocations so far, dlive = definite live slots (a + * lower bound; valid while cap_off == 0), cap_off = 1 once a free that is + * not definite was seen. SK_HANDLE: pool = its pool symbol, prov = the + * static provenance it holds (-1 unknown). */ + uint64_t gbase, dallocs; + uint16_t dlive; + uint8_t cap_off; + int pool, prov; } Sym; +/* OSC-3 item 2: handle state saved around branches and loop bodies */ +typedef struct { + uint8_t pfreed[MAX_PROV]; + int hprov[OSC_CHECK_MAX_SYMS]; + int nprov, nsym; +} HSave; + typedef struct { uint8_t moved; uint16_t n_shared, n_mut; @@ -77,6 +97,14 @@ typedef struct { int contract; /* 0 body; 1 checking a requires clause; 2 an ensures clause (OSC-2) */ int cond_depth; /* OSC-2 arenas: nesting of if/else branches */ uint32_t nregion; /* OSC-2 arenas: model region ids handed out in this function */ + /* OSC-3 item 2: provenances of this function. pfreed = freed on every path + * reaching the current point; pmslot / pmhnd = model slot and handle ids + * (fresh per provenance); pfree_sym = handle symbol named by the free. */ + int nprov; + uint8_t pfreed[MAX_PROV]; + uint32_t pfree_line[MAX_PROV], pmslot[MAX_PROV], pmhnd[MAX_PROV]; + int pfree_sym[MAX_PROV]; + uint32_t nmslot, nmhnd; } C; #define NODE(i) (&c->ast->nodes[(i)]) @@ -97,7 +125,8 @@ static void tr_push(C *c, uint8_t op, const OscModelEvent *ev, uint32_t line, in e->ev = *ev; if (ev->obj > OSC_MODEL_MAX_OBJECTS || ev->obj2 > OSC_MODEL_MAX_OBJECTS || ev->borrow > OSC_MODEL_MAX_BORROWS || ev->via > OSC_MODEL_MAX_BORROWS || - ev->region > OSC_MODEL_MAX_REGIONS) + ev->region > OSC_MODEL_MAX_REGIONS || ev->slot > OSC_MODEL_MAX_SLOTS || + ev->handle > OSC_MODEL_MAX_HANDLES) t->overflow = 1; } if (refused) t->refused = 1; @@ -123,6 +152,18 @@ static void tr_region(C *c, uint32_t kind, int obj, uint32_t region, uint32_t li ev.region = region; tr_push(c, OSC_TR_EVENT, &ev, line, refused); } +/* OSC-3 item 2: SLOT_ALLOC (slot, handle, rights) / SLOT_FREE (handle) / + * HANDLE_USE (handle, rights) */ +static void tr_slot(C *c, uint32_t kind, uint32_t slot, uint32_t handle, uint64_t rights, uint32_t line, int refused) +{ + OscModelEvent ev; + memset(&ev, 0, sizeof ev); + ev.kind = kind; + ev.slot = slot; + ev.handle = handle; + ev.rights = rights; + tr_push(c, OSC_TR_EVENT, &ev, line, refused); +} static void tr_save(C *c, uint32_t line) { tr_push(c, OSC_TR_SAVE, NULL, line, 0); } static void tr_restore(C *c, uint32_t line) { tr_push(c, OSC_TR_RESTORE, NULL, line, 0); } @@ -419,6 +460,10 @@ static int scope_exit_events(C *c, int levels, uint32_t line, OscNode *rec) for (int o = 0; o < c->nobj; o++) if (c->obj[o].arena1 == s + 1 && !c->obj[o].moved) c->obj[o].moved = 2; if (a->nrel < OSC_AST_MAX_REL) { a->rel[a->nrel++] = (int16_t)s; rec->rel_count++; } + } else if (y->kind == SK_POOL) { + /* OSC-3 item 2: the pool closes (PCLOSE); its handles are + * values declared inside its scope, so none outlives it */ + if (a->nrel < OSC_AST_MAX_REL) { a->rel[a->nrel++] = (int16_t)s; rec->rel_count++; } } } } @@ -444,6 +489,37 @@ static Snap *snap_take(C *c) s->nsym = c->nsym; return s; } +/* OSC-3 item 2: handle state (freed provenances, provenance per handle) */ +static HSave *hs_take(C *c) +{ + HSave *s = malloc(sizeof *s); + if (!s) return NULL; + memcpy(s->pfreed, c->pfreed, (size_t)c->nprov); + for (int i = 0; i < c->nsym; i++) s->hprov[i] = c->sym[i].prov; + s->nprov = c->nprov; + s->nsym = c->nsym; + return s; +} +static void hs_restore(C *c, const HSave *s) +{ + memcpy(c->pfreed, s->pfreed, (size_t)s->nprov); + for (int i = 0; i < s->nsym; i++) c->sym[i].prov = s->hprov[i]; +} +/* after a branch or loop: a handle whose provenance differs from the one it + * had before (assigned on some path) becomes unknown; the model trace was + * restored at OSC_TR_RESTORE, so provenances freed on every surviving path + * since `before` are re-emitted as SLOT_FREE */ +static void hs_settle(C *c, const HSave *before, const HSave *other, uint32_t line) +{ + for (int i = 0; i < before->nsym; i++) { + if (c->sym[i].kind != SK_HANDLE) continue; + if (c->sym[i].prov != before->hprov[i] || (other && other->hprov[i] != before->hprov[i])) c->sym[i].prov = -1; + } + for (int p = 0; p < before->nprov; p++) + if (c->pfreed[p] && !before->pfreed[p] && c->pmhnd[p]) + tr_slot(c, OSC_EV_SLOT_FREE, 0, c->pmhnd[p], 0, line, 0); +} + /* restore state of the entities that existed at the snapshot; later ones stay * allocated (ids are single assignment) but are dead */ static void snap_restore(C *c, const Snap *s) @@ -800,9 +876,171 @@ static int resolve(C *c, const OscNode *n) "'%s' is an arena; it is only named by 'in %s' in an owner declaration", name, name); return -1; } + if (s >= 0 && c->sym[s].kind == SK_POOL) { /* OSC-3 item 2 */ + osc_diag_set(c->d, OSC_DIAG_TYPE_MISMATCH, n->line, n->col, name, c->sym[s].line, NULL, "pool used as a value", + "'%s' is a pool; it is only used as '%s.alloc(e)', '%s.free(h)', '%s[h]'", name, name, name, name); + return -1; + } + if (s >= 0 && c->sym[s].kind == SK_HANDLE) { + osc_diag_set(c->d, OSC_DIAG_TYPE_MISMATCH, n->line, n->col, name, c->sym[s].line, NULL, "handle used as a value", + "'%s' is a handle; it is only used as a pool index, freed, or copied into another handle " + "binding (no arithmetic, casts, arguments, returns or storage)", name); + return -1; + } + return s; +} + +/* ------------------------------------------------------------ OSC-3 item 2: versioned handles */ +/* pool symbol named by n (no diagnostic), or -1 */ +static int pool_named(C *c, const OscNode *n) +{ + char name[64]; + osc_node_name(c->ast, n, name, sizeof name); + int s = lookup(c, name); + return s >= 0 && c->sym[s].kind == SK_POOL ? s : -1; +} + +/* the pool for `NAME.alloc` / `NAME.free` / a handle type: must be a pool */ +static int pool_sym(C *c, const OscNode *n) +{ + char name[64]; + osc_node_name(c->ast, n, name, sizeof name); + int s = lookup(c, name); + if (s < 0) { + osc_diag_set(c->d, OSC_DIAG_UNDEFINED_NAME, n->line, n->col, name, 0, NULL, "use of undefined name", + "'%s' is not defined in an enclosing scope", name); + return -1; + } + if (c->sym[s].kind != SK_POOL) { + osc_diag_set(c->d, OSC_DIAG_TYPE_MISMATCH, n->line, n->col, name, c->sym[s].line, NULL, "not a pool", + "'%s' is not a pool", name); + return -1; + } return s; } +/* handle operand: an ON_NAME naming a handle of pool ps. Returns its symbol. */ +static int handle_operand(C *c, const OscNode *hn, int ps, const char *what) +{ + Sym *p = &c->sym[ps]; + char name[64]; + if (hn->kind != ON_NAME) { + osc_diag_set(c->d, OSC_DIAG_TYPE_MISMATCH, hn->line, hn->col, p->name, p->line, NULL, "pool index not a handle", + "%s of pool '%s' takes a handle binding of that pool", what, p->name); + return -1; + } + osc_node_name(c->ast, hn, name, sizeof name); + int s = lookup(c, name); + if (s < 0) { + osc_diag_set(c->d, OSC_DIAG_UNDEFINED_NAME, hn->line, hn->col, name, 0, NULL, "use of undefined name", + "'%s' is not defined in an enclosing scope", name); + return -1; + } + if (c->sym[s].kind != SK_HANDLE) { + osc_diag_set(c->d, OSC_DIAG_TYPE_MISMATCH, hn->line, hn->col, name, c->sym[s].line, p->name, + "pool index not a handle", "%s of pool '%s' takes a handle of that pool; '%s' is not a handle", + what, p->name, name); + return -1; + } + if (c->sym[s].pool != ps) { + osc_diag_set(c->d, OSC_DIAG_WRONG_POOL, hn->line, hn->col, name, c->sym[s].line, p->name, "handle of another pool", + "'%s' is a handle of pool '%s', used with pool '%s'", name, c->sym[c->sym[s].pool].name, p->name); + return -1; + } + return s; +} + +/* use of handle hs (read wr = 0 / write wr = 1): refused when its provenance + * was freed on every path here. The refused event is the model's HANDLE_USE + * (stale-generation). */ +static int handle_use(C *c, const OscNode *at, int hs, int wr) +{ + Sym *h = &c->sym[hs]; + int pv = h->prov; + if (pv < 0) return 0; + if (c->pfreed[pv]) { + int copy = c->pfree_sym[pv] != hs; + char tr[64]; + snprintf(tr, sizeof tr, "%s after free%s", wr ? "write" : "read", copy ? " via a copy" : ""); + tr_slot(c, OSC_EV_HANDLE_USE, 0, c->pmhnd[pv], wr ? 2 : 1, at->line, 1); + osc_diag_set(c->d, OSC_DIAG_STALE_HANDLE, at->line, at->col, h->name, c->pfree_line[pv], + copy ? c->sym[c->pfree_sym[pv]].name : NULL, tr, + "'%s' is stale: its slot was freed at line %u%s%s (generation mismatch)", h->name, + c->pfree_line[pv], copy ? " through " : "", copy ? c->sym[c->pfree_sym[pv]].name : ""); + return -1; + } + tr_slot(c, OSC_EV_HANDLE_USE, 0, c->pmhnd[pv], wr ? 2 : 1, at->line, 0); + return 0; +} + +/* `P.alloc(e)` (ON_PALLOC an) for a handle of pool ps: returns the new + * provenance. Definite allocations (same loop and branch depth as the pool) + * are counted against K live slots and the generation budget. */ +static int chk(C *c, int i, OscScalar want); +static int handle_alloc(C *c, OscNode *an, int ps) +{ + int as = pool_sym(c, an); + if (as < 0) return -1; + Sym *p = &c->sym[ps]; + if (as != ps) { + osc_diag_set(c->d, OSC_DIAG_WRONG_POOL, an->line, an->col, c->sym[as].name, c->sym[as].line, p->name, + "handle of another pool", "'%s.alloc' makes a handle of pool '%s', bound as a handle of pool '%s'", + c->sym[as].name, c->sym[as].name, p->name); + return -1; + } + an->sym = ps; + if (chk(c, an->a, p->ty.s) < 0) return -1; + if (c->loop_depth == p->loop_depth && c->cond_depth == p->cond_depth) { + if (!p->cap_off && p->dlive + 1u > p->cap) { + osc_diag_set(c->d, OSC_DIAG_POOL_CAPACITY, an->line, an->col, p->name, p->line, NULL, "pool full", + "allocation needs %u live slots, pool '%s' has %u", p->dlive + 1u, p->name, p->cap); + return -1; + } + /* each slot serves generations gbase..UINT64_MAX: K * (2^64 - gbase) allocations in all */ + uint64_t per = UINT64_MAX - p->gbase; /* + 1 */ + if (per < (1ull << 32) && p->dallocs + 1 > (uint64_t)p->cap * (per + 1)) { + osc_diag_set(c->d, OSC_DIAG_GENERATION_EXHAUSTED, an->line, an->col, p->name, p->line, NULL, + "retired slot reused", + "allocation %llu from pool '%s' exceeds its generation budget (%u slots x %llu generations " + "from base %llu); every slot is live or retired", + (unsigned long long)(p->dallocs + 1), p->name, p->cap, (unsigned long long)(per + 1), + (unsigned long long)p->gbase); + return -1; + } + p->dallocs++; + if (!p->cap_off) p->dlive++; + } + if (c->nprov >= MAX_PROV) return cap_fail(c, an->line, "handle provenance capacity"); + int pv = c->nprov++; + c->pfreed[pv] = 0; + c->pfree_line[pv] = 0; + c->pfree_sym[pv] = -1; + c->pmslot[pv] = ++c->nmslot; + c->pmhnd[pv] = ++c->nmhnd; + tr_slot(c, OSC_EV_SLOT_ALLOC, c->pmslot[pv], c->pmhnd[pv], 3, an->line, 0); + return pv; +} + +/* value of a handle binding (let or assign): P.alloc(e) or a handle of P. + * Returns the provenance (-1 unknown) or -2 on error. */ +static int handle_value(C *c, OscNode *v, int ps, const char *target) +{ + if (v->kind == ON_PALLOC) { + int pv = handle_alloc(c, v, ps); + return pv < 0 ? -2 : pv; + } + if (v->kind == ON_NAME) { + int s = handle_operand(c, v, ps, "a handle binding"); + if (s < 0) return -2; + v->sym = s; + return c->sym[s].prov; + } + char msg[200]; + snprintf(msg, sizeof msg, "handle '%s' takes '%s.alloc(e)' or another handle of pool '%s'", target, + c->sym[ps].name, c->sym[ps].name); + return tmismatch(c, v, target, msg) ? -2 : -2; +} + /* "[u8; 4]" for an array ref type, the struct name for a struct ref type */ static const char *rty(C *c, const OscType *t, char *buf, size_t cap) { @@ -922,8 +1160,21 @@ static int chk(C *c, int i, OscScalar want) t = c->sym[s].ty.s; break; } + case ON_HLOAD: case ON_INDEX: { if (contract_name(c, n)) return -1; + int ps = pool_named(c, n); + if (ps >= 0) { /* OSC-3 item 2: P[h] reads the slot's value */ + int hs = handle_operand(c, NODE(n->a), ps, "a read"); + if (hs < 0) return -1; + n->kind = ON_HLOAD; + n->sym = ps; + n->sym2 = hs; + NODE(n->a)->sym = hs; + if (handle_use(c, n, hs, 0)) return -1; + t = c->sym[ps].ty.s; + break; + } int s = resolve(c, n); if (s < 0) return -1; n->sym = s; @@ -1087,6 +1338,8 @@ static int chk(C *c, int i, OscScalar want) t = T; break; } + case ON_PALLOC: /* OSC-3 item 2 */ + return tmismatch(c, n, NULL, "'P.alloc(e)' makes a handle; bind it with 'let NAME: handle P = P.alloc(e);'"); default: return tmismatch(c, n, NULL, "unexpected expression"); } @@ -1405,13 +1658,17 @@ static int chk_if(C *c, int i) c->cond_depth++; /* OSC-2 arenas: allocations in a branch are not definite */ int rc = -1; Snap *s1 = NULL; + HSave *h0 = hs_take(c), *h1 = NULL; /* OSC-3 item 2 */ + if (!h0) { cap_fail(c, n->line, "checker memory"); goto out; } tr_save(c, n->line); if (chk_block(c, n->b, 1)) goto out; int t1 = c->term; s1 = snap_take(c); - if (!s1) { cap_fail(c, n->line, "checker memory"); goto out; } + h1 = hs_take(c); + if (!s1 || !h1) { cap_fail(c, n->line, "checker memory"); goto out; } tr_restore(c, n->line); snap_restore(c, s0); + hs_restore(c, h0); c->term = 0; int t2 = 0; if (n->c >= 0) { @@ -1429,7 +1686,10 @@ static int chk_if(C *c, int i) } if (t2) { snap_restore(c, s1); + hs_restore(c, h1); } else if (!t1) { + /* OSC-3 item 2: freed only if freed on both paths */ + for (int p = 0; p < h0->nprov; p++) c->pfreed[p] = (uint8_t)(c->pfreed[p] && h1->pfreed[p]); for (int o = 0; o < nobj0; o++) { int m1 = s1->obj[o].moved == 1, m2 = c->obj[o].moved == 1; if (m1 != m2) { @@ -1449,11 +1709,14 @@ static int chk_if(C *c, int i) for (int o = 0; o < nobj0; o++) if (c->obj[o].moved == 1 && s0->obj[o].moved != 1) tr_ev(c, OSC_EV_MOVE, o, -1, -1, -1, c->obj[o].move_line, 0); + hs_settle(c, h0, t1 || t2 ? NULL : h1, n->line); rc = 0; out: c->cond_depth--; free(s0); free(s1); + free(h0); + free(h1); return rc; } @@ -1461,6 +1724,8 @@ static int chk_loop_body(C *c, OscNode *n, int is_for) { Snap *s0 = snap_take(c); if (!s0) return cap_fail(c, n->line, "checker memory"); + HSave *h0 = hs_take(c); /* OSC-3 item 2 */ + if (!h0) { free(s0); return cap_fail(c, n->line, "checker memory"); } int rc = -1; tr_save(c, n->line); c->loop_depth++; @@ -1480,10 +1745,20 @@ static int chk_loop_body(C *c, OscNode *n, int is_for) c->loop_depth--; tr_restore(c, n->line); snap_restore(c, s0); + { + /* OSC-3 item 2: frees in the body are not definite after the loop; + * handles assigned in the body become unknown */ + int keep[OSC_CHECK_MAX_SYMS]; + for (int k = 0; k < h0->nsym; k++) keep[k] = c->sym[k].prov; + hs_restore(c, h0); + for (int k = 0; k < h0->nsym; k++) c->sym[k].prov = keep[k]; + hs_settle(c, h0, NULL, n->line); + } c->term = 0; rc = 0; out: free(s0); + free(h0); return rc; } @@ -1610,6 +1885,27 @@ static int chk_stmt(C *c, int i) } case ON_ASSIGN: { set_sobj_tok(c, n); + { + /* OSC-3 item 2: h = P.alloc(e) / h = h2 */ + char hn[64]; + osc_node_name(c->ast, n, hn, sizeof hn); + int hs = lookup(c, hn); + if (hs >= 0 && c->sym[hs].kind == SK_HANDLE) { + Sym *y = &c->sym[hs]; + if (!y->mut) { + osc_diag_set(c->d, OSC_DIAG_IMMUTABLE_ASSIGN, n->line, n->col, y->name, y->line, NULL, + "assign to immutable binding", "'%s' is not assignable (declare it with 'let mut')", + y->name); + return -1; + } + if (NODE(n->a)->kind == ON_BORROW) return tmismatch(c, NODE(n->a), y->name, "a borrow cannot be assigned to a handle"); + int pv = handle_value(c, NODE(n->a), y->pool, y->name); + if (pv == -2) return -1; + n->sym = hs; + y->prov = pv; + return 0; + } + } int s = resolve(c, n); if (s < 0) return -1; n->sym = s; @@ -1658,8 +1954,20 @@ static int chk_stmt(C *c, int i) return outlives(c, v, src, b, y->name, "outer borrow assigned a borrow of an inner owner"); return 0; } + case ON_HSTORE: case ON_STORE: { set_sobj_tok(c, n); + int ps = pool_named(c, n); + if (ps >= 0) { /* OSC-3 item 2: P[h] = e writes the slot's value */ + int hs = handle_operand(c, NODE(n->a), ps, "a write"); + if (hs < 0) return -1; + n->kind = ON_HSTORE; + n->sym = ps; + n->sym2 = hs; + NODE(n->a)->sym = hs; + if (chk(c, n->b, c->sym[ps].ty.s) < 0) return -1; + return handle_use(c, n, hs, 1); + } int s = resolve(c, n); if (s < 0) return -1; n->sym = s; @@ -1735,6 +2043,75 @@ static int chk_stmt(C *c, int i) pop_scope(c); return 0; } + case ON_POOL: { /* OSC-3 item 2: pool NAME: [T; K] [gen B] { body } */ + set_sobj_tok(c, n); + if (push_scope(c, n->line)) return -1; + OscType tp = {n->ty.s, OSC_REF_NONE, OSC_T_VOID, 0, 0}; + int s = declare(c, n, SK_POOL, &tp, 0); + if (s < 0) return -1; + Sym *y = &c->sym[s]; + y->cap = (uint16_t)n->ival; + y->gbase = (uint64_t)n->lo; + y->cond_depth = c->cond_depth; + y->dallocs = 0; + y->dlive = 0; + y->cap_off = 0; + n->sym = s; + if (chk_block(c, n->b, 1)) return -1; + if (!c->term && scope_exit_events(c, 1, (uint32_t)NODE(n->b)->ival, n)) return -1; + pop_scope(c); + return 0; + } + case ON_LET_HANDLE: { /* let [mut] h: handle P = P.alloc(e) | h2; */ + set_sobj_tok(c, n); + char name[64]; + osc_node_name(c->ast, n, name, sizeof name); + if (lookup(c, name) >= 0) return declare(c, n, SK_HANDLE, &n->ty, n->mut) < 0 ? -1 : -1; + int ps = pool_sym(c, NODE(n->c)); + if (ps < 0) return -1; + int pv = handle_value(c, NODE(n->a), ps, name); + if (pv == -2) return -1; + OscType th = {OSC_T_U64, OSC_REF_NONE, OSC_T_VOID, 0, 0}; + n->ty = th; + int s = declare(c, n, SK_HANDLE, &th, n->mut); + if (s < 0) return -1; + c->sym[s].pool = ps; + c->sym[s].prov = pv; + n->sym = s; + n->sym2 = ps; + return 0; + } + case ON_PFREE: { /* P.free(h); */ + set_sobj_tok(c, n); + int ps = pool_sym(c, n); + if (ps < 0) return -1; + int hs = handle_operand(c, NODE(n->a), ps, "a free"); + if (hs < 0) return -1; + n->sym = ps; + n->sym2 = hs; + NODE(n->a)->sym = hs; + Sym *p = &c->sym[ps], *h = &c->sym[hs]; + if (c->loop_depth == p->loop_depth && c->cond_depth == p->cond_depth) { + if (p->dlive) p->dlive--; + } else { + p->cap_off = 1; + } + int pv = h->prov; + if (pv < 0) return 0; + if (c->pfreed[pv]) { + int copy = c->pfree_sym[pv] != hs; + tr_slot(c, OSC_EV_SLOT_FREE, 0, c->pmhnd[pv], 0, n->line, 1); + osc_diag_set(c->d, OSC_DIAG_STALE_HANDLE, n->line, n->col, h->name, c->pfree_line[pv], + copy ? c->sym[c->pfree_sym[pv]].name : NULL, copy ? "free after free via a copy" : "free after free", + "'%s' is stale: its slot was already freed at line %u (double free)", h->name, c->pfree_line[pv]); + return -1; + } + tr_slot(c, OSC_EV_SLOT_FREE, 0, c->pmhnd[pv], 0, n->line, 0); + c->pfreed[pv] = 1; + c->pfree_line[pv] = n->line; + c->pfree_sym[pv] = hs; + return 0; + } case ON_CALLSTMT: set_sobj_tok(c, NODE(n->a)); return chk_call(c, n->a, 0, 1) < 0 ? -1 : 0; @@ -1774,6 +2151,8 @@ static int chk_fn(C *c, int fi) c->nsym = c->nobj = c->nbor = c->nvis = c->depth = c->loop_depth = c->term = 0; c->cond_depth = 0; c->nregion = 0; + c->nprov = 0; /* OSC-3 item 2 */ + c->nmslot = c->nmhnd = 0; char fname[64]; osc_node_name(a, fn, fname, sizeof fname); snprintf(c->sobj, sizeof c->sobj, "%s", fname); diff --git a/src/compiler/osc_diag.c b/src/compiler/osc_diag.c index 4a22c347d..0d9a017b5 100644 --- a/src/compiler/osc_diag.c +++ b/src/compiler/osc_diag.c @@ -13,7 +13,8 @@ static const char *const kind_names[OSC_DIAG__COUNT] = { "AMBIGUOUS_WIDTH", "OVERFLOW_UNSAFE", "UNBOUNDED_LOOP", "MISSING_RETURN", "USE_AFTER_MOVE", "CONDITIONAL_MOVE", "MUTABLE_ALIAS", "BORROW_OUTLIVES_OWNER", "STATIC_OUT_OF_BOUNDS", "IMMUTABLE_ASSIGN", "READ_ONLY_BORROW", "CONTRACT_INVALID", "CONTRACT_VIOLATION", "UNKNOWN_FIELD", "DUPLICATE_FIELD", - "MISSING_FIELD", "UNDEFINED_TYPE", "RECURSIVE_STRUCT", "ARENA_ESCAPE", "ARENA_MOVE", "ARENA_CAPACITY"}; + "MISSING_FIELD", "UNDEFINED_TYPE", "RECURSIVE_STRUCT", "ARENA_ESCAPE", "ARENA_MOVE", "ARENA_CAPACITY", + "STALE_HANDLE", "WRONG_POOL", "POOL_CAPACITY", "GENERATION_EXHAUSTED"}; const char *osc_diag_kind_name(int kind) { diff --git a/src/compiler/osc_diag.h b/src/compiler/osc_diag.h index 93a8b9a82..19b9683d2 100644 --- a/src/compiler/osc_diag.h +++ b/src/compiler/osc_diag.h @@ -40,6 +40,11 @@ typedef enum { OSC_DIAG_ARENA_ESCAPE, /* a borrow of an arena object would outlive the arena */ OSC_DIAG_ARENA_MOVE, /* an arena object is moved (out of the arena, or into own param) */ OSC_DIAG_ARENA_CAPACITY, /* arena bound out of range, or allocations statically exceed it */ + /* OSC-3 item 2 (versioned handles, docs/osc/OSC-3-DESIGN.md "Item 2"), appended */ + OSC_DIAG_STALE_HANDLE, /* a handle is used or freed after its slot was freed (also via a copy) */ + OSC_DIAG_WRONG_POOL, /* a handle of one pool is used with another pool (or copied across pools) */ + OSC_DIAG_POOL_CAPACITY, /* pool K out of 1..16, or definite allocations exceed K live slots */ + OSC_DIAG_GENERATION_EXHAUSTED, /* definite alloc/free cycles reuse a retired slot (generation would wrap) */ OSC_DIAG__COUNT } OscDiagKind; diff --git a/src/compiler/osc_interp.c b/src/compiler/osc_interp.c index 199ee8071..c9f4aebc9 100644 --- a/src/compiler/osc_interp.c +++ b/src/compiler/osc_interp.c @@ -157,6 +157,13 @@ static uint64_t run_func(Ctx *c, int fi, const uint64_t *args) { case OSC_I_AOPEN: v[in->dst] = osc_rt_arena_open(c->rt, in->imm); break; case OSC_I_AALLOC: v[in->dst] = osc_rt_arena_alloc(c->rt, v[in->b], T[in->dst].len, v[in->a]); break; case OSC_I_ADESTROY: osc_rt_arena_destroy(c->rt, v[in->a]); break; + case OSC_I_POPEN: v[in->dst] = osc_rt_pool_open(c->rt, in->nargs, in->imm); break; + case OSC_I_PCLOSE: osc_rt_pool_close(c->rt, v[in->a]); break; + case OSC_I_HALLOC: v[in->dst] = osc_rt_h_alloc(c->rt, v[in->b], v[in->a]); break; + case OSC_I_HGEN: v[in->dst] = osc_rt_h_gen(c->rt, v[in->a], v[in->b]); break; + case OSC_I_HFREE: osc_rt_h_free(c->rt, v[in->a], v[in->b], v[in->c]); break; + case OSC_I_HLOAD: v[in->dst] = osc_rt_h_load(c->rt, v[in->a], v[in->b], v[in->c]); break; + case OSC_I_HSTORE: osc_rt_h_store(c->rt, v[in->a], v[in->b], v[in->c], v[in->args[0]]); break; case OSC_I_LOAD: v[in->dst] = *elem(c, &T[in->a], v[in->a], T[in->b].s, v[in->b]); break; case OSC_I_STORE: *elem(c, &T[in->a], v[in->a], T[in->b].s, v[in->b]) = v[in->c]; break; case OSC_I_FLOAD: diff --git a/src/compiler/osc_ir.c b/src/compiler/osc_ir.c index 422333172..123c7e190 100644 --- a/src/compiler/osc_ir.c +++ b/src/compiler/osc_ir.c @@ -91,6 +91,10 @@ static int insn_uses(const OscInsn *in, int16_t *u) { case OSC_I_CBR: u[k++] = in->a; break; case OSC_I_AALLOC: u[k++] = in->a; u[k++] = in->b; break; case OSC_I_ADESTROY: u[k++] = in->a; break; + case OSC_I_PCLOSE: u[k++] = in->a; break; + case OSC_I_HALLOC: case OSC_I_HGEN: u[k++] = in->a; u[k++] = in->b; break; + case OSC_I_HFREE: case OSC_I_HLOAD: u[k++] = in->a; u[k++] = in->b; u[k++] = in->c; break; + case OSC_I_HSTORE: u[k++] = in->a; u[k++] = in->b; u[k++] = in->c; u[k++] = in->args[0]; break; case OSC_I_BIN: case OSC_I_CMP: case OSC_I_LOAD: u[k++] = in->a; u[k++] = in->b; break; case OSC_I_STORE: u[k++] = in->a; u[k++] = in->b; u[k++] = in->c; break; case OSC_I_FLOAD: u[k++] = in->a; if (in->b >= 0) u[k++] = in->b; break; @@ -106,6 +110,7 @@ static int insn_def(const OscInsn *in) { case OSC_I_CONST: case OSC_I_MOV: case OSC_I_BIN: case OSC_I_UN: case OSC_I_CMP: case OSC_I_CAST: case OSC_I_ALLOC: case OSC_I_LOAD: case OSC_I_FLOAD: return in->dst; case OSC_I_AOPEN: case OSC_I_AALLOC: return in->dst; + case OSC_I_POPEN: case OSC_I_HALLOC: case OSC_I_HGEN: case OSC_I_HLOAD: return in->dst; case OSC_I_CALL: return in->dst; /* may be -1 */ default: return -1; } @@ -196,6 +201,34 @@ static int validate_insn(const OscUnit *u, int fi, const OscFunc *f, uint32_t ii case OSC_I_ADESTROY: NEED(VR(in->a) && type_is_scalar(&T[in->a]) && T[in->a].s == OSC_T_U64, "ADESTROY handle not u64", fi, ii); break; + case OSC_I_POPEN: + NEED(VR(in->dst) && type_is_scalar(&T[in->dst]) && T[in->dst].s == OSC_T_U64, "POPEN pool not u64", fi, ii); + NEED(in->nargs >= 1 && in->nargs <= OSC_POOL_SLOTS, "POPEN slot count out of range", fi, ii); + NEED(is_value_scalar((OscScalar)in->sub), "POPEN element type not scalar", fi, ii); + break; + case OSC_I_PCLOSE: + NEED(VR(in->a) && type_is_scalar(&T[in->a]) && T[in->a].s == OSC_T_U64, "PCLOSE pool not u64", fi, ii); + break; + case OSC_I_HALLOC: + case OSC_I_HGEN: + case OSC_I_HFREE: + case OSC_I_HLOAD: + case OSC_I_HSTORE: { + int16_t us[OSC_MAX_PARAMS + 3]; + int k = insn_uses(in, us); + for (int j = 0; j < k; j++) NEED(VR(us[j]) && type_is_scalar(&T[us[j]]), "handle op bad operand", fi, ii); + int d = insn_def(in); + if (in->op == OSC_I_HALLOC || in->op == OSC_I_HGEN || in->op == OSC_I_HLOAD) + NEED(VR(d) && type_is_scalar(&T[d]), "handle op bad dst", fi, ii); + if (in->op == OSC_I_HALLOC || in->op == OSC_I_HGEN) NEED(T[d].s == OSC_T_U64, "handle slot/gen not u64", fi, ii); + if (in->op == OSC_I_HSTORE) NEED(in->nargs == 1, "HSTORE needs one value", fi, ii); + /* slot and gen are u64; the pool operand and the element types are + * checked against the defining POPEN by the pool discipline */ + if (in->op == OSC_I_HALLOC) break; + NEED(T[in->b].s == OSC_T_U64, "handle slot not u64", fi, ii); + if (in->op != OSC_I_HGEN) NEED(T[in->c].s == OSC_T_U64, "handle gen not u64", fi, ii); + break; + } case OSC_I_LOAD: NEED(VR(in->dst) && VR(in->a) && VR(in->b), "LOAD bad vreg", fi, ii); NEED(type_is_array_ref(&T[in->a]), "LOAD base not an array ref", fi, ii); @@ -329,6 +362,50 @@ static int validate_func(const OscUnit *u, int fi, char *err, size_t n) { } } + /* OSC-3 item 2: a pool vreg is defined by exactly one POPEN (not a + * parameter, never redefined) and is used only as the pool operand of + * HALLOC / HGEN / HFREE / HLOAD / HSTORE / PCLOSE. Element types of + * HALLOC init, HLOAD dst and HSTORE value match the POPEN element type. */ + uint8_t pk[OSC_MAX_VREGS]; + memset(pk, 0, sizeof pk); + for (uint32_t i = 0; i < f->ninsns; i++) { + const OscInsn *x = &f->insns[i]; + if (x->op != OSC_I_POPEN) continue; + if (pk[x->dst] || hk[x->dst] || x->dst < f->nparams) + return vfail(err, n, "func %d insn %u: POPEN pool vreg reused", fi, i); + pk[x->dst] = (uint8_t)x->sub; + } + for (uint32_t i = 0; i < f->ninsns; i++) { + const OscInsn *x = &f->insns[i]; + int d = insn_def(x); + if (x->op != OSC_I_POPEN && d >= 0 && pk[d]) + return vfail(err, n, "func %d insn %u: pool vreg redefined", fi, i); + switch (x->op) { + case OSC_I_PCLOSE: + if (!pk[x->a]) return vfail(err, n, "func %d insn %u: PCLOSE of a non-pool", fi, i); + break; + case OSC_I_HALLOC: + if (!pk[x->b] || pk[x->a]) return vfail(err, n, "func %d insn %u: HALLOC pool misuse", fi, i); + if (f->vtype[x->a].s != (OscScalar)pk[x->b]) + return vfail(err, n, "func %d insn %u: HALLOC init type != pool element type", fi, i); + break; + case OSC_I_HGEN: case OSC_I_HFREE: case OSC_I_HLOAD: case OSC_I_HSTORE: + if (!pk[x->a] || pk[x->b] || (x->op != OSC_I_HGEN && pk[x->c])) + return vfail(err, n, "func %d insn %u: handle pool misuse", fi, i); + if (x->op == OSC_I_HLOAD && f->vtype[x->dst].s != (OscScalar)pk[x->a]) + return vfail(err, n, "func %d insn %u: HLOAD type != pool element type", fi, i); + if (x->op == OSC_I_HSTORE && (pk[x->args[0]] || f->vtype[x->args[0]].s != (OscScalar)pk[x->a])) + return vfail(err, n, "func %d insn %u: HSTORE value type != pool element type", fi, i); + break; + default: { + int16_t us[OSC_MAX_PARAMS + 3]; + int k = insn_uses(x, us); + for (int j = 0; j < k; j++) + if (pk[us[j]]) return vfail(err, n, "func %d insn %u: pool used as a value", fi, i); + } + } + } + /* definite assignment: every use reads a vreg defined on every path */ int nb = f->nblocks; uint64_t *in = malloc(sizeof(uint64_t) * BSW * nb), *out = malloc(sizeof(uint64_t) * BSW * nb); @@ -462,6 +539,16 @@ static bool unit_has_arena(const OscUnit *u) { return false; } +/* 1 if any function uses a pool instruction (OSC-3 item 2) */ +static bool unit_has_pool(const OscUnit *u) { + for (int fi = 0; fi < u->nfuncs; fi++) + for (uint32_t i = 0; i < u->funcs[fi].ninsns; i++) { + uint8_t op = u->funcs[fi].insns[i].op; + if (op >= OSC_I_POPEN && op <= OSC_I_HSTORE) return true; + } + return false; +} + static void encode_unit(const OscUnit *u, W *w) { /* format version 1 (OSC-1, no structs) / 2 (OSC-2 structs: struct table * after the magic) / 3 (OSC-2 arenas: AOPEN/AALLOC/ADESTROY present; the @@ -472,8 +559,10 @@ static void encode_unit(const OscUnit *u, W *w) { bool arena = unit_has_arena(u); if (u->nstructs) magic[7] = 2; if (arena) magic[7] = 3; + bool pool = unit_has_pool(u); /* OSC-3 item 2: version 4, same layout as 3 */ + if (pool) magic[7] = 4; wbytes(w, magic, sizeof magic); - if (u->nstructs || arena) { + if (u->nstructs || arena || pool) { w8(w, u->nstructs); for (int k = 0; k < u->nstructs; k++) { const OscStruct *s = &u->structs[k]; @@ -513,6 +602,12 @@ static void encode_unit(const OscUnit *u, W *w) { case OSC_I_AOPEN: wvr(w, in->dst); w8(w, in->imm); break; case OSC_I_AALLOC: wvr(w, in->dst); wvr(w, in->a); wvr(w, in->b); break; case OSC_I_ADESTROY: wvr(w, in->a); break; + case OSC_I_POPEN: wvr(w, in->dst); w8(w, in->sub); w8(w, in->nargs); w64(w, in->imm); break; + case OSC_I_PCLOSE: wvr(w, in->a); break; + case OSC_I_HALLOC: case OSC_I_HGEN: wvr(w, in->dst); wvr(w, in->a); wvr(w, in->b); break; + case OSC_I_HFREE: wvr(w, in->a); wvr(w, in->b); wvr(w, in->c); break; + case OSC_I_HLOAD: wvr(w, in->dst); wvr(w, in->a); wvr(w, in->b); wvr(w, in->c); break; + case OSC_I_HSTORE: wvr(w, in->a); wvr(w, in->b); wvr(w, in->c); wvr(w, in->args[0]); break; case OSC_I_LOAD: wvr(w, in->dst); wvr(w, in->a); wvr(w, in->b); break; case OSC_I_STORE: wvr(w, in->a); wvr(w, in->b); wvr(w, in->c); break; case OSC_I_FLOAD: wvr(w, in->dst); wvr(w, in->a); wvr(w, in->b); w8(w, in->imm); break; diff --git a/src/compiler/osc_ir.h b/src/compiler/osc_ir.h index 50e6e86a6..169980b36 100644 --- a/src/compiler/osc_ir.h +++ b/src/compiler/osc_ir.h @@ -99,8 +99,21 @@ typedef enum { OSC_I_AALLOC, /* dst(REF OWN, elem, len) = bump allocation of len cells from */ /* arena handle b (u64, defined by AOPEN), every cell = a */ /* (as ALLOC); arena too full -> TRAP_ARENA_FULL */ - OSC_I_ADESTROY /* destroy arena handle a: zero all its cells, free its slot. */ + OSC_I_ADESTROY, /* destroy arena handle a: zero all its cells, free its slot. */ /* Arena objects are never RELEASEd individually. */ + /* appended for OSC-3 item 2 versioned handles (docs/osc/OSC-3-DESIGN.md) */ + OSC_I_POPEN, /* dst(u64 pool id) = open a pool of nargs slots (1..16) of */ + /* element scalar sub, every slot free at generation imm; */ + /* no free pool table entry -> TRAP_OOM */ + OSC_I_PCLOSE, /* close pool a: zero its slots, free its table entry */ + OSC_I_HALLOC, /* dst(u64 slot) = lowest free slot of pool b, value = a (elem */ + /* type); no free slot: TRAP_RETIRED if a slot is retired, */ + /* else TRAP_POOL_FULL */ + OSC_I_HGEN, /* dst(u64) = current generation of slot b of pool a */ + OSC_I_HFREE, /* free handle (slot b, gen c) of pool a: stale -> TRAP_STALE; */ + /* gen == 2^64-1 retires the slot, else gen + 1 */ + OSC_I_HLOAD, /* dst(elem) = value of handle (b, c) of pool a; stale -> STALE */ + OSC_I_HSTORE /* value of handle (b, c) of pool a = args[0] (nargs = 1) */ } OscOp; typedef enum { @@ -141,11 +154,17 @@ typedef enum { OSC_TRAP_REQUIRES = 9, /* a `requires` clause evaluated to false at entry */ OSC_TRAP_ENSURES = 10, /* an `ensures` clause evaluated to false at return */ /* Appended for OSC-2 arenas (section 3). */ - OSC_TRAP_ARENA_FULL = 11 /* an arena allocation exceeded the arena's capacity */ + OSC_TRAP_ARENA_FULL = 11, /* an arena allocation exceeded the arena's capacity */ + /* appended for OSC-3 item 2 versioned handles */ + OSC_TRAP_STALE = 12, /* a handle whose generation no longer matches its slot */ + OSC_TRAP_POOL_FULL = 13, /* pool alloc with every slot live */ + OSC_TRAP_RETIRED = 14 /* pool alloc with no free slot and at least one slot */ + /* retired at the maximum generation (never wrapped) */ } OscTrap; /* Highest trap code; arrays indexed by trap code have OSC_TRAP_MAX + 1 entries. */ -#define OSC_TRAP_MAX OSC_TRAP_ARENA_FULL +#define OSC_TRAP_MAX OSC_TRAP_RETIRED +#define OSC_POOL_SLOTS 16 /* OSC-3 item 2: slots per pool */ typedef struct { uint8_t op; /* OscOp */ diff --git a/src/compiler/osc_lex.c b/src/compiler/osc_lex.c index b6600f69d..abadc0b83 100644 --- a/src/compiler/osc_lex.c +++ b/src/compiler/osc_lex.c @@ -13,6 +13,7 @@ static const struct { const char *w; int k; } keywords[] = { {"as", OT_AS}, {"true", OT_TRUE}, {"false", OT_FALSE}, {"u8", OT_U8}, {"u16", OT_U16}, {"u32", OT_U32}, {"u64", OT_U64}, {"i8", OT_I8}, {"i16", OT_I16}, {"i32", OT_I32}, {"i64", OT_I64}, {"bool", OT_BOOL}, {"struct", OT_STRUCT}, {"arena", OT_ARENA}, + {"pool", OT_POOL}, {"handle", OT_HANDLE}, }; /* Reserved: destruction words (destruction has no surface syntax, III.8) and @@ -30,7 +31,7 @@ static const char *const kind_names[OT__COUNT] = { "ensures", "as", "true", "false", "u8", "u16", "u32", "u64", "i8", "i16", "i32", "i64", "bool", "(", ")", "{", "}", "[", "]", ",", ";", ":", "->", "..", "=", "+", "-", "*", "/", "%", "&", "|", "^", "~", "!", "&&", "||", "<<", ">>", "==", "!=", "<", "<=", ">", ">=", - "struct", ".", "arena", + "struct", ".", "arena", "pool", "handle", }; const char *osc_tok_kind_name(int kind) diff --git a/src/compiler/osc_lex.h b/src/compiler/osc_lex.h index 3cdee3fc9..c74854656 100644 --- a/src/compiler/osc_lex.h +++ b/src/compiler/osc_lex.h @@ -29,6 +29,8 @@ typedef enum { OT_STRUCT, OT_DOT, /* OSC-2 item 3 (arenas), appended */ OT_ARENA, + /* OSC-3 item 2 (versioned handles), appended */ + OT_POOL, OT_HANDLE, OT__COUNT } OscTokKind; diff --git a/src/compiler/osc_lower.c b/src/compiler/osc_lower.c index 5b2ac2944..3eede7cc2 100644 --- a/src/compiler/osc_lower.c +++ b/src/compiler/osc_lower.c @@ -23,6 +23,11 @@ typedef struct { int16_t vreg[OSC_CHECK_MAX_SYMS]; int failed; int fi; /* function being lowered */ + /* OSC-3 item 2: symbol kinds the lowerer must tell apart (reset per + * function). A pool symbol's vreg is its u64 pool id; a handle symbol's + * vreg r is its slot and r + 1 its generation. */ + uint8_t ispool[OSC_CHECK_MAX_SYMS]; + uint8_t ishandle[OSC_CHECK_MAX_SYMS]; } L; #define NODE(i) (&l->ast->nodes[(i)]) @@ -102,7 +107,7 @@ static void releases(L *l, const OscNode *n) int s = l->ast->rel[n->rel_start + k]; /* OSC-2 arenas: an arena symbol's vreg is its u64 handle (owners are REFs) */ int arena = l->f->vtype[l->vreg[s]].s != OSC_T_REF; - OscInsn *x = emit(l, arena ? OSC_I_ADESTROY : OSC_I_RELEASE, n->line); + OscInsn *x = emit(l, l->ispool[s] ? OSC_I_PCLOSE : arena ? OSC_I_ADESTROY : OSC_I_RELEASE, n->line); x->a = l->vreg[s]; } } @@ -169,6 +174,15 @@ static int expr(L *l, int i) case ON_BOOL: return kconst(l, OSC_T_BOOL, n->ival ? 1 : 0, n->line); case ON_INT: return kconst(l, n->ty.s, n->cval, n->line); case ON_NAME: return l->vreg[n->sym]; + case ON_HLOAD: { /* OSC-3 item 2: P[h] */ + int r = newvs(l, n->ty.s, n->line); + OscInsn *x = emit(l, OSC_I_HLOAD, n->line); + x->dst = (int16_t)r; + x->a = l->vreg[n->sym]; + x->b = l->vreg[n->sym2]; + x->c = (int16_t)(l->vreg[n->sym2] + 1); + return r; + } case ON_INDEX: { int ix = expr(l, n->a); int r = newvs(l, n->ty.s, n->line); @@ -266,6 +280,45 @@ static void ensures_check(L *l, int v) contract_check(l, a->ensn[l->fi], OSC_TRAP_ENSURES); } +/* OSC-3 item 2: the (slot, gen) vregs of handle value i (ON_PALLOC allocates + * from its pool: HALLOC then HGEN; ON_NAME is a copy of another handle). + * Returns the slot vreg; *gen gets the generation vreg. */ +static int hval(L *l, int i, int *gen) +{ + const OscNode *v = NODE(i); + if (v->kind == ON_PALLOC) { + int init = expr(l, v->a); + int pool = l->vreg[v->sym]; + int s = newvs(l, OSC_T_U64, v->line); + int g = newvs(l, OSC_T_U64, v->line); + OscInsn *x = emit(l, OSC_I_HALLOC, v->line); + x->dst = (int16_t)s; + x->a = (int16_t)init; + x->b = (int16_t)pool; + x = emit(l, OSC_I_HGEN, v->line); + x->dst = (int16_t)g; + x->a = (int16_t)pool; + x->b = (int16_t)s; + *gen = g; + return s; + } + int r = l->vreg[v->sym]; + *gen = r + 1; + return r; +} + +/* bind handle symbol s (vregs r, r + 1) to the value of node i */ +static void hbind(L *l, int s, int i, uint32_t line) +{ + int g, v = hval(l, i, &g); + OscInsn *x = emit(l, OSC_I_MOV, line); + x->dst = l->vreg[s]; + x->a = (int16_t)v; + x = emit(l, OSC_I_MOV, line); + x->dst = (int16_t)(l->vreg[s] + 1); + x->a = (int16_t)g; +} + static void block(L *l, int i); static void stmt(L *l, int i) @@ -335,6 +388,7 @@ static void stmt(L *l, int i) break; } case ON_ASSIGN: { + if (l->ishandle[n->sym]) { hbind(l, n->sym, n->a, n->line); break; } const OscNode *v = NODE(n->a); int src = v->kind == ON_BORROW ? l->vreg[n->sym2] : expr(l, n->a); OscInsn *x = emit(l, OSC_I_MOV, n->line); @@ -468,6 +522,45 @@ static void stmt(L *l, int i) case ON_CALLSTMT: call(l, n->a, 0); break; + case ON_POOL: { /* OSC-3 item 2: POPEN, body, then (if live) PCLOSE */ + int p = newvs(l, OSC_T_U64, n->line); + l->vreg[n->sym] = (int16_t)p; + l->ispool[n->sym] = 1; + OscInsn *x = emit(l, OSC_I_POPEN, n->line); + x->dst = (int16_t)p; + x->sub = (uint8_t)n->ty.s; + x->nargs = (uint8_t)n->ival; + x->imm = (uint64_t)n->lo; + block(l, n->b); + if (l->cur >= 0) releases(l, n); + break; + } + case ON_LET_HANDLE: { + int r = newvs(l, OSC_T_U64, n->line); + int g = newvs(l, OSC_T_U64, n->line); + if (g != r + 1) { lcap(l, n->line, "vreg limit"); break; } + l->vreg[n->sym] = (int16_t)r; + l->ishandle[n->sym] = 1; + hbind(l, n->sym, n->a, n->line); + break; + } + case ON_PFREE: { + OscInsn *x = emit(l, OSC_I_HFREE, n->line); + x->a = l->vreg[n->sym]; + x->b = l->vreg[n->sym2]; + x->c = (int16_t)(l->vreg[n->sym2] + 1); + break; + } + case ON_HSTORE: { + int v = expr(l, n->b); + OscInsn *x = emit(l, OSC_I_HSTORE, n->line); + x->a = l->vreg[n->sym]; + x->b = l->vreg[n->sym2]; + x->c = (int16_t)(l->vreg[n->sym2] + 1); + x->args[0] = (int16_t)v; + x->nargs = 1; + break; + } case ON_ARENA: { /* OSC-2: AOPEN, body, then (if live) the body's releases were * emitted by block(); the arena's own list destroys it */ int h = newvs(l, OSC_T_U64, n->line); @@ -509,6 +602,8 @@ static int lower_fn(L *l, int fi) f->ret = fn->ty; l->nins = 0; l->nblk = 0; + memset(l->ispool, 0, sizeof l->ispool); + memset(l->ishandle, 0, sizeof l->ishandle); for (int p = fn->a; p >= 0; p = NODE(p)->next) { const OscNode *pn = NODE(p); int r = newv(l, &pn->ty, pn->line); diff --git a/src/compiler/osc_parse.c b/src/compiler/osc_parse.c index 5b90ba7ba..19440785e 100644 --- a/src/compiler/osc_parse.c +++ b/src/compiler/osc_parse.c @@ -199,6 +199,17 @@ static int parse_ref_body(P *p, OscType *ty, OscRefKind rk) return parse_arr_body(p, ty, rk); } +/* OSC-3 item 2: pools and handles are function-local (never parameters or + * results) */ +static int local_only(P *p) +{ + const OscToken *t = cur(p); + osc_diag_set(p->d, OSC_DIAG_UNSUPPORTED, t->line, t->col, at(p, OT_HANDLE) ? "handle" : "pool", 0, NULL, + "handle crosses a function boundary", + "pools and handles are function-local: no handle or pool parameters or return types"); + return -1; +} + /* ptype: scalar | own[..] | &[..] | &mut[..] | own S | &S | &mut S */ static int parse_ptype(P *p, OscType *ty) { @@ -209,6 +220,7 @@ static int parse_ptype(P *p, OscType *ty) return parse_ref_body(p, ty, OSC_REF_SHARED); } if (at(p, OT_NAME) && find_struct(p, cur(p)) >= 0) return struct_by_value(p, cur(p), "struct by value"); + if (at(p, OT_HANDLE) || at(p, OT_POOL)) return local_only(p); return parse_scalar(p, ty); } @@ -481,6 +493,19 @@ static int parse_primary(P *p) return n; case OT_NAME: if (peek_kind(p, 1) == OT_LPAREN) return parse_call(p); + if (peek_kind(p, 1) == OT_DOT && peek_kind(p, 2) == OT_ALLOC) { /* OSC-3 item 2: P.alloc(e) */ + n = new_node(p, ON_PALLOC, t); + if (n < 0) return -1; + adv(p); adv(p); adv(p); + if (expect(p, OT_LPAREN)) return -1; + if (enter(p)) return -1; + int e0 = parse_expr(p); + leave(p); + if (e0 < 0) return -1; + N(n)->a = e0; + if (expect(p, OT_RPAREN)) return -1; + return n; + } if (peek_kind(p, 1) == OT_DOT) return parse_field_ref(p, ON_FIELD); if (peek_kind(p, 1) == OT_LBRACK) { n = new_node(p, ON_INDEX, t); @@ -637,6 +662,22 @@ static int parse_stmt(P *p) if (n < 0) return -1; if (parse_name_tok(p, n)) return -1; if (expect(p, OT_COLON)) return -1; + if (at(p, OT_HANDLE)) { /* OSC-3 item 2: let [mut] h: handle P = P.alloc(e) | h2; */ + N(n)->kind = ON_LET_HANDLE; + N(n)->mut = (uint8_t)mut; + adv(p); + if (!at(p, OT_NAME)) return syntax(p, "a pool name after 'handle'"); + int pn = new_node(p, ON_NAME, cur(p)); + if (pn < 0) return -1; + adv(p); + N(n)->c = pn; + if (expect(p, OT_ASSIGN)) return -1; + int e = parse_expr(p); + if (e < 0) return -1; + N(n)->a = e; + if (expect(p, OT_SEMI)) return -1; + return n; + } if (at(p, OT_OWN)) { if (mut) { const OscToken *mt = &p->ast->toks[N(n)->tok - 1]; @@ -719,6 +760,19 @@ static int parse_stmt(P *p) if (expect(p, OT_SEMI)) return -1; return n; } + if (k1 == OT_DOT && peek_kind(p, 2) == OT_RESERVED && p->ast->toks[p->pos + 2].len == 4 && + memcmp(p->ast->src + p->ast->toks[p->pos + 2].off, "free", 4) == 0) { /* OSC-3 item 2: P.free(h); */ + n = new_node(p, ON_PFREE, t); + if (n < 0) return -1; + adv(p); adv(p); adv(p); + if (expect(p, OT_LPAREN)) return -1; + int h = parse_expr(p); + if (h < 0) return -1; + N(n)->a = h; + if (expect(p, OT_RPAREN)) return -1; + if (expect(p, OT_SEMI)) return -1; + return n; + } if (k1 == OT_DOT) { n = parse_field_ref(p, ON_FSTORE); if (n < 0) return -1; @@ -765,6 +819,40 @@ static int parse_stmt(P *p) adv(p); return syntax(p, "'=', '[' or '(' after a name at statement start"); } + case OT_POOL: { /* OSC-3 item 2: pool NAME: [T; K] [gen B] { ... } */ + n = new_node(p, ON_POOL, t); + if (n < 0) return -1; + adv(p); + if (parse_name_tok(p, n)) return -1; + if (expect(p, OT_COLON)) return -1; + if (expect(p, OT_LBRACK)) return -1; + if (parse_scalar(p, &N(n)->ty)) return -1; + if (expect(p, OT_SEMI)) return -1; + if (!at(p, OT_INT)) return syntax(p, "a slot count INT"); + const OscToken *kt = cur(p); + if (kt->ival == 0 || kt->ival > OSC_POOL_MAX_SLOTS) { + char nm[64]; + osc_node_name(p->ast, N(n), nm, sizeof nm); + osc_diag_set(p->d, OSC_DIAG_POOL_CAPACITY, kt->line, kt->col, nm, t->line, NULL, + "pool size outside 1..16", "pool size %llu outside 1..%d slots", + (unsigned long long)kt->ival, OSC_POOL_MAX_SLOTS); + return -1; + } + N(n)->ival = kt->ival; + adv(p); + if (expect(p, OT_RBRACK)) return -1; + N(n)->lo = 0; + if (at(p, OT_NAME) && tok_eq(p, cur(p), "gen")) { /* declared generation base */ + adv(p); + if (!at(p, OT_INT)) return syntax(p, "gen INT (the generation base)"); + N(n)->lo = (int64_t)cur(p)->ival; + adv(p); + } + int b = parse_block(p); + if (b < 0) return -1; + N(n)->b = b; + return n; + } case OT_ARENA: { /* OSC-2 arenas: arena NAME bound K { ... } */ n = new_node(p, ON_ARENA, t); if (n < 0) return -1; @@ -985,6 +1073,7 @@ static int parse_fn(P *p) if (a->toks[q].kind == OT_NAME && find_struct(p, &a->toks[q]) >= 0) return struct_by_value(p, &a->toks[q], "struct return type"); } + if (at(p, OT_HANDLE) || at(p, OT_POOL)) return local_only(p); if (at(p, OT_OWN) || at(p, OT_AMP) || at(p, OT_LBRACK)) { const OscToken *t = cur(p); osc_diag_set(p->d, OSC_DIAG_UNSUPPORTED, t->line, t->col, fname, N(f)->line, NULL, diff --git a/src/compiler/osc_parse.h b/src/compiler/osc_parse.h index d9168f4a0..7936f9d05 100644 --- a/src/compiler/osc_parse.h +++ b/src/compiler/osc_parse.h @@ -19,6 +19,7 @@ #define OSC_AST_MAX_DEPTH 64 /* nesting of blocks + expressions */ #define OSC_AST_MAX_REL 16384 /* release-list entries (checker) */ #define OSC_ARENA_MAX_CELLS OSC_MAX_ARRAY_LEN /* arena bound K: 1..64 cells (one pool slot) */ +#define OSC_POOL_MAX_SLOTS 16 /* pool K: 1..16 slots (OSC-0B model slot capacity) */ typedef enum { ON_NONE = 0, @@ -60,9 +61,23 @@ typedef enum { /* OSC-2 arenas (docs/osc/OSC-2-DESIGN.md section 3). An arena allocation * is an ON_LET_ALLOC (or, refused by the checker, ON_LET_MOVE) whose c is * an ON_NAME naming the arena ("in NAME"); c = -1 otherwise. */ - ON_ARENA /* tok = arena name; ival = bound K (1..OSC_ARENA_MAX_CELLS); + ON_ARENA, /* tok = arena name; ival = bound K (1..OSC_ARENA_MAX_CELLS); * b = body ON_BLOCK; checker: sym = arena symbol, rel = the * body's owners then the arena itself (REGION_DESTROY) */ + /* OSC-3 item 2: versioned handles (docs/osc/OSC-3-DESIGN.md "Item 2"). + * Appended. */ + ON_POOL, /* tok = pool name; ty.s = element scalar; ival = K slots + * (1..OSC_POOL_MAX_SLOTS); lo = declared generation base + * (u64 bits); b = body ON_BLOCK; checker: sym = pool symbol, + * rel = the body's owners then the pool itself (PCLOSE) */ + ON_LET_HANDLE, /* mut; tok = handle name; c = ON_NAME of the declared pool; + * a = ON_PALLOC or ON_NAME (copy of a handle) */ + ON_PALLOC, /* tok = pool name; a = initial value expr ("p.alloc(e)") */ + ON_PFREE, /* tok = pool name; a = handle expr ("p.free(h);") */ + ON_HLOAD, /* checker rewrite of ON_INDEX on a pool: tok = pool; a = handle; + * sym = pool symbol, sym2 = handle symbol */ + ON_HSTORE /* checker rewrite of ON_STORE on a pool: tok = pool; a = handle; + * b = value; sym = pool symbol, sym2 = handle symbol */ } OscNodeKind; typedef struct { diff --git a/src/compiler/osc_rt.c b/src/compiler/osc_rt.c index f5d4913e2..b10731e29 100644 --- a/src/compiler/osc_rt.c +++ b/src/compiler/osc_rt.c @@ -15,11 +15,28 @@ _Static_assert(offsetof(OscRt, trap) == 16, "OscRt.trap must be at offset 16"); _Static_assert(offsetof(OscRt, arena_open) == 24, "OscRt.arena_open must be at offset 24"); _Static_assert(offsetof(OscRt, arena_alloc) == 32, "OscRt.arena_alloc must be at offset 32"); _Static_assert(offsetof(OscRt, arena_destroy) == 40, "OscRt.arena_destroy must be at offset 40"); +_Static_assert(offsetof(OscRt, pool_open) == 48, "OscRt.pool_open must be at offset 48"); +_Static_assert(offsetof(OscRt, pool_close) == 56, "OscRt.pool_close must be at offset 56"); +_Static_assert(offsetof(OscRt, h_alloc) == 64, "OscRt.h_alloc must be at offset 64"); +_Static_assert(offsetof(OscRt, h_gen) == 72, "OscRt.h_gen must be at offset 72"); +_Static_assert(offsetof(OscRt, h_free) == 80, "OscRt.h_free must be at offset 80"); +_Static_assert(offsetof(OscRt, h_load) == 88, "OscRt.h_load must be at offset 88"); +_Static_assert(offsetof(OscRt, h_store) == 96, "OscRt.h_store must be at offset 96"); _Static_assert(sizeof(void (*)(void)) == 8, "64-bit function pointers"); #define FNV_OFF 1469598103934665603ULL #define FNV_PRIME 1099511628211ULL +static void set_pool_fns(OscRt *rt) { + rt->pool_open = osc_rt_pool_open; + rt->pool_close = osc_rt_pool_close; + rt->h_alloc = osc_rt_h_alloc; + rt->h_gen = osc_rt_h_gen; + rt->h_free = osc_rt_h_free; + rt->h_load = osc_rt_h_load; + rt->h_store = osc_rt_h_store; +} + void osc_rt_init(OscRt *rt) { memset(rt, 0, sizeof *rt); rt->alloc = osc_rt_alloc; @@ -29,6 +46,7 @@ void osc_rt_init(OscRt *rt) { rt->arena_alloc = osc_rt_arena_alloc; rt->arena_destroy = osc_rt_arena_destroy; rt->ev_hash = FNV_OFF; + set_pool_fns(rt); } void osc_rt_reset(OscRt *rt) { @@ -50,15 +68,28 @@ void osc_rt_reset(OscRt *rt) { rt->arena_open = osc_rt_arena_open; rt->arena_alloc = osc_rt_arena_alloc; rt->arena_destroy = osc_rt_arena_destroy; + memset(rt->pools, 0, sizeof rt->pools); + rt->pool_serial = 0; + rt->trap_op = rt->trap_pool = rt->trap_slot = rt->trap_rights = 0; + rt->trap_gen = 0; + set_pool_fns(rt); } -static void log_event(OscRt *rt, uint8_t kind, unsigned slot, uint16_t len, uint32_t serial) { +static void log_event_g(OscRt *rt, uint8_t kind, unsigned slot, uint16_t len, uint32_t serial, uint64_t gen) { OscRtEvent e = {kind, (uint8_t)slot, len, serial}; - if (rt->nev < OSC_RT_EVENTS) rt->ev[rt->nev] = e; + if (rt->nev < OSC_RT_EVENTS) { rt->ev[rt->nev] = e; rt->ev_gen[rt->nev] = gen; } if (rt->nev != UINT32_MAX) rt->nev++; uint8_t b[8] = {kind, (uint8_t)slot, (uint8_t)len, (uint8_t)(len >> 8), (uint8_t)serial, (uint8_t)(serial >> 8), (uint8_t)(serial >> 16), (uint8_t)(serial >> 24)}; for (unsigned i = 0; i < 8; i++) { rt->ev_hash ^= b[i]; rt->ev_hash *= FNV_PRIME; } + /* OSC-3 item 2: pool events also hash their u64 generation (older event + * kinds hash exactly as before) */ + if (kind >= OSC_RT_EV_POOL_OPEN) + for (unsigned i = 0; i < 8; i++) { rt->ev_hash ^= (uint8_t)(gen >> (8 * i)); rt->ev_hash *= FNV_PRIME; } +} + +static void log_event(OscRt *rt, uint8_t kind, unsigned slot, uint16_t len, uint32_t serial) { + log_event_g(rt, kind, slot, len, serial, 0); } void osc_rt_trap(OscRt *rt, uint64_t code) { @@ -68,6 +99,7 @@ void osc_rt_trap(OscRt *rt, uint64_t code) { uint64_t osc_rt_alloc(OscRt *rt, uint64_t len, uint64_t init) { if (len < 1 || len > OSC_MAX_ARRAY_LEN) osc_rt_trap(rt, OSC_TRAP_RUNTIME); + if (rt->next_serial == UINT32_MAX) osc_rt_trap(rt, OSC_TRAP_RUNTIME); /* serial exhausted: never wrap */ for (unsigned s = 0; s < OSC_RT_SLOTS; s++) { if (rt->live[s]) continue; for (unsigned i = 0; i < len; i++) rt->cells[s][i] = init; @@ -113,6 +145,7 @@ void osc_rt_release(OscRt *rt, uint64_t addr, uint64_t len) { uint64_t osc_rt_arena_open(OscRt *rt, uint64_t cap) { if (cap < 1 || cap > OSC_MAX_ARRAY_LEN) osc_rt_trap(rt, OSC_TRAP_RUNTIME); + if (rt->next_serial == UINT32_MAX) osc_rt_trap(rt, OSC_TRAP_RUNTIME); /* serial exhausted: never wrap */ for (unsigned s = 0; s < OSC_RT_SLOTS; s++) { if (rt->live[s]) continue; memset(rt->cells[s], 0, sizeof rt->cells[s]); @@ -144,6 +177,7 @@ uint64_t osc_rt_arena_alloc(OscRt *rt, uint64_t h, uint64_t len, uint64_t init) if (s < 0 || len < 1 || len > OSC_MAX_ARRAY_LEN) osc_rt_trap(rt, OSC_TRAP_RUNTIME); unsigned top = rt->arena_top[s]; if (len > (uint64_t)(rt->slot_len[s] - top)) osc_rt_trap(rt, OSC_TRAP_ARENA_FULL); + if (rt->next_serial == UINT32_MAX) osc_rt_trap(rt, OSC_TRAP_RUNTIME); /* serial exhausted: never wrap */ for (unsigned i = 0; i < len; i++) rt->cells[s][top + i] = init; rt->arena_top[s] = (uint16_t)(top + len); log_event(rt, OSC_RT_EV_ARENA_ALLOC, (unsigned)s, (uint16_t)len, ++rt->next_serial); @@ -161,6 +195,105 @@ void osc_rt_arena_destroy(OscRt *rt, uint64_t h) { log_event(rt, OSC_RT_EV_REGION_DESTROY, (unsigned)s, rt->slot_len[s], rt->slot_serial[s]); } +/* ---- OSC-3 item 2 versioned handles ----------------------------------- */ + +/* the open pool with id p (index + 1), else trap RUNTIME */ +static OscRtPool *pool_of(OscRt *rt, uint64_t p) { + if (p < 1 || p > OSC_RT_POOLS || !rt->pools[p - 1].open) osc_rt_trap(rt, OSC_TRAP_RUNTIME); + return &rt->pools[p - 1]; +} + +static void pool_trap(OscRt *rt, uint64_t p, int code, int op, uint64_t slot, uint64_t gen, int rights) { + rt->trap_op = (uint8_t)op; + rt->trap_pool = (uint8_t)(p - 1); + rt->trap_slot = (uint8_t)slot; + rt->trap_gen = gen; + rt->trap_rights = (uint8_t)rights; + osc_rt_trap(rt, (uint64_t)code); +} + +uint64_t osc_rt_pool_open(OscRt *rt, uint64_t k, uint64_t base) { + if (k < 1 || k > OSC_POOL_SLOTS) osc_rt_trap(rt, OSC_TRAP_RUNTIME); + if (rt->pool_serial == UINT32_MAX) osc_rt_trap(rt, OSC_TRAP_RUNTIME); /* serial exhausted: never wrap */ + for (unsigned i = 0; i < OSC_RT_POOLS; i++) { + OscRtPool *q = &rt->pools[i]; + if (q->open) continue; + memset(q, 0, sizeof *q); + q->open = 1; + q->k = (uint8_t)k; + q->base = base; + q->serial = ++rt->pool_serial; + for (unsigned s = 0; s < k; s++) q->gen[s] = base; + log_event_g(rt, OSC_RT_EV_POOL_OPEN, i, (uint16_t)k, q->serial, base); + return i + 1; + } + osc_rt_trap(rt, OSC_TRAP_OOM); + return 0; /* not reached */ +} + +void osc_rt_pool_close(OscRt *rt, uint64_t p) { + OscRtPool *q = pool_of(rt, p); + log_event_g(rt, OSC_RT_EV_POOL_CLOSE, (unsigned)(p - 1), q->k, q->serial, q->base); + q->open = 0; + memset(q->state, 0, sizeof q->state); + memset(q->val, 0, sizeof q->val); +} + +uint64_t osc_rt_h_alloc(OscRt *rt, uint64_t p, uint64_t init) { + OscRtPool *q = pool_of(rt, p); + int retired = -1; + for (unsigned s = 0; s < q->k; s++) { + if (q->state[s] == OSC_RT_SLOT_RETIRED && retired < 0) retired = (int)s; + if (q->state[s] != OSC_RT_SLOT_FREE) continue; + q->state[s] = OSC_RT_SLOT_LIVE; + q->val[s] = init; + log_event_g(rt, OSC_RT_EV_SLOT_ALLOC, (unsigned)(p - 1), (uint16_t)s, q->serial, q->gen[s]); + return s; + } + if (retired >= 0) pool_trap(rt, p, OSC_TRAP_RETIRED, 3, (uint64_t)retired, q->gen[retired], 0); + pool_trap(rt, p, OSC_TRAP_POOL_FULL, 3, 0, 0, 0); + return 0; /* not reached */ +} + +uint64_t osc_rt_h_gen(OscRt *rt, uint64_t p, uint64_t slot) { + OscRtPool *q = pool_of(rt, p); + if (slot >= q->k) osc_rt_trap(rt, OSC_TRAP_RUNTIME); + return q->gen[slot]; +} + +/* the pool of a handle access; traps STALE unless (slot, gen) is live */ +static OscRtPool *checked(OscRt *rt, uint64_t p, uint64_t slot, uint64_t gen, int op, int rights) { + OscRtPool *q = pool_of(rt, p); + if (slot >= q->k) osc_rt_trap(rt, OSC_TRAP_RUNTIME); + if (q->state[slot] != OSC_RT_SLOT_LIVE || q->gen[slot] != gen) + pool_trap(rt, p, OSC_TRAP_STALE, op, slot, gen, rights); + return q; +} + +void osc_rt_h_free(OscRt *rt, uint64_t p, uint64_t slot, uint64_t gen) { + OscRtPool *q = checked(rt, p, slot, gen, 2, 0); + log_event_g(rt, OSC_RT_EV_SLOT_FREE, (unsigned)(p - 1), (uint16_t)slot, q->serial, gen); + q->val[slot] = 0; + if (gen == UINT64_MAX) { + q->state[slot] = OSC_RT_SLOT_RETIRED; /* never wrapped */ + } else { + q->state[slot] = OSC_RT_SLOT_FREE; + q->gen[slot] = gen + 1; + } +} + +uint64_t osc_rt_h_load(OscRt *rt, uint64_t p, uint64_t slot, uint64_t gen) { + OscRtPool *q = checked(rt, p, slot, gen, 1, 1); + log_event_g(rt, OSC_RT_EV_HANDLE_USE, (unsigned)(p - 1), (uint16_t)(slot | 1u << 8), q->serial, gen); + return q->val[slot]; +} + +void osc_rt_h_store(OscRt *rt, uint64_t p, uint64_t slot, uint64_t gen, uint64_t v) { + OscRtPool *q = checked(rt, p, slot, gen, 1, 2); + log_event_g(rt, OSC_RT_EV_HANDLE_USE, (unsigned)(p - 1), (uint16_t)(slot | 2u << 8), q->serial, gen); + q->val[slot] = v; +} + typedef uint64_t (*OscNativeFn)(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, OscRt *); @@ -184,10 +317,19 @@ int osc_rt_same_outcome(const OscRt *a, const OscRt *b) { memcmp(a->slot_serial, b->slot_serial, sizeof a->slot_serial)) return 0; if (memcmp(a->slot_arena, b->slot_arena, sizeof a->slot_arena) || memcmp(a->arena_top, b->arena_top, sizeof a->arena_top)) return 0; + if (a->pool_serial != b->pool_serial || a->trap_op != b->trap_op || a->trap_pool != b->trap_pool || + a->trap_slot != b->trap_slot || a->trap_rights != b->trap_rights || a->trap_gen != b->trap_gen) return 0; + for (unsigned i = 0; i < OSC_RT_POOLS; i++) { + const OscRtPool *p = &a->pools[i], *q = &b->pools[i]; + if (p->open != q->open || p->k != q->k || p->serial != q->serial || p->base != q->base || + memcmp(p->state, q->state, sizeof p->state) || memcmp(p->gen, q->gen, sizeof p->gen) || + memcmp(p->val, q->val, sizeof p->val)) return 0; + } uint32_t n = a->nev < OSC_RT_EVENTS ? a->nev : OSC_RT_EVENTS; for (uint32_t i = 0; i < n; i++) if (a->ev[i].kind != b->ev[i].kind || a->ev[i].slot != b->ev[i].slot || - a->ev[i].len != b->ev[i].len || a->ev[i].serial != b->ev[i].serial) return 0; + a->ev[i].len != b->ev[i].len || a->ev[i].serial != b->ev[i].serial || + (a->ev[i].kind >= OSC_RT_EV_POOL_OPEN && a->ev_gen[i] != b->ev_gen[i])) return 0; for (unsigned s = 0; s < OSC_RT_SLOTS; s++) if ((a->slot_serial[s] || a->live[s]) && memcmp(a->cells[s], b->cells[s], sizeof a->cells[s])) return 0; return 1; diff --git a/src/compiler/osc_rt.h b/src/compiler/osc_rt.h index 4d6779a29..aea0e68b6 100644 --- a/src/compiler/osc_rt.h +++ b/src/compiler/osc_rt.h @@ -42,7 +42,20 @@ typedef enum { * counter (next_serial). */ OSC_RT_EV_REGION_OPEN = 3, OSC_RT_EV_ARENA_ALLOC = 4, - OSC_RT_EV_REGION_DESTROY = 5 + OSC_RT_EV_REGION_DESTROY = 5, + /* OSC-3 item 2 versioned handles (docs/osc/OSC-3-DESIGN.md "Item 2"). For + * the five pool events, slot = the pool table index, serial = the pool + * instance serial (pool_serial, its own counter), and the u64 generation + * is in ev_gen[] at the same index. POOL_OPEN / POOL_CLOSE: len = K, + * gen = the declared generation base. SLOT_ALLOC / SLOT_FREE: len = the + * pool slot (0-based), gen = the slot generation (SLOT_FREE: the + * generation being freed). HANDLE_USE: len = slot | rights << 8 (rights + * 1 = read, 2 = write), gen = the handle generation. */ + OSC_RT_EV_POOL_OPEN = 6, + OSC_RT_EV_SLOT_ALLOC = 7, + OSC_RT_EV_SLOT_FREE = 8, + OSC_RT_EV_HANDLE_USE = 9, + OSC_RT_EV_POOL_CLOSE = 10 } OscRtEventKind; typedef struct { @@ -52,6 +65,20 @@ typedef struct { uint32_t serial; /* allocation serial number (object identity, 1-based) */ } OscRtEvent; +/* OSC-3 item 2 pool slot states */ +enum { OSC_RT_SLOT_FREE = 0, OSC_RT_SLOT_LIVE = 1, OSC_RT_SLOT_RETIRED = 2 }; +#define OSC_RT_POOLS 8 + +typedef struct { + uint8_t open; + uint8_t k; /* slots 1..OSC_POOL_SLOTS */ + uint8_t state[OSC_POOL_SLOTS]; /* OSC_RT_SLOT_* */ + uint32_t serial; /* pool instance serial (1-based) */ + uint64_t base; /* declared generation base */ + uint64_t gen[OSC_POOL_SLOTS]; + uint64_t val[OSC_POOL_SLOTS]; +} OscRtPool; + typedef struct OscRt OscRt; struct OscRt { /* Fixed-offset fields read by native code. Do not reorder. */ @@ -62,6 +89,14 @@ struct OscRt { uint64_t (*arena_open)(OscRt *rt, uint64_t cap); /* offset 24 */ uint64_t (*arena_alloc)(OscRt *rt, uint64_t h, uint64_t len, uint64_t init); /* offset 32 */ void (*arena_destroy)(OscRt *rt, uint64_t h); /* offset 40 */ + /* OSC-3 item 2 pools (appended; native code reaches them at these offsets). */ + uint64_t (*pool_open)(OscRt *rt, uint64_t k, uint64_t base); /* offset 48 */ + void (*pool_close)(OscRt *rt, uint64_t p); /* offset 56 */ + uint64_t (*h_alloc)(OscRt *rt, uint64_t p, uint64_t init); /* offset 64 */ + uint64_t (*h_gen)(OscRt *rt, uint64_t p, uint64_t slot); /* offset 72 */ + void (*h_free)(OscRt *rt, uint64_t p, uint64_t slot, uint64_t gen); /* offset 80 */ + uint64_t (*h_load)(OscRt *rt, uint64_t p, uint64_t slot, uint64_t gen); /* offset 88 */ + void (*h_store)(OscRt *rt, uint64_t p, uint64_t slot, uint64_t gen, uint64_t v); /* offset 96 */ /* Host-side state. */ uint64_t cells[OSC_RT_SLOTS][OSC_MAX_ARRAY_LEN]; uint8_t live[OSC_RT_SLOTS]; @@ -82,6 +117,17 @@ struct OscRt { * is the bump pointer (cells handed out so far). */ uint8_t slot_arena[OSC_RT_SLOTS]; uint16_t arena_top[OSC_RT_SLOTS]; + /* OSC-3 item 2 pools: OSC_RT_POOLS table entries; pool id = index + 1. */ + OscRtPool pools[OSC_RT_POOLS]; + uint32_t pool_serial; + uint64_t ev_gen[OSC_RT_EVENTS]; + /* The refused operation of a STALE / RETIRED / POOL_FULL trap (not logged: + * the log holds the accepted prefix). trap_op: 1 = use, 2 = free, + * 3 = alloc; trap_pool: pool index; trap_slot / trap_gen: the stale + * handle (STALE) or the lowest retired slot (RETIRED); trap_rights: the + * use's rights (1 read, 2 write). Zero when the last run did not trap so. */ + uint8_t trap_op, trap_pool, trap_slot, trap_rights; + uint64_t trap_gen; }; void osc_rt_init(OscRt *rt); @@ -122,4 +168,22 @@ uint64_t osc_rt_arena_open(OscRt *rt, uint64_t cap); uint64_t osc_rt_arena_alloc(OscRt *rt, uint64_t h, uint64_t len, uint64_t init); void osc_rt_arena_destroy(OscRt *rt, uint64_t h); +/* ---- OSC-3 item 2 versioned handles (docs/osc/OSC-3-DESIGN.md) ---------- + * A pool holds k (1..16) slots; each slot is free, live or retired and has + * a u64 generation (initially the declared base). A handle is (slot, gen). + * osc_rt_h_alloc takes the lowest free slot (POOL_FULL if every slot is live, + * RETIRED if none is free and at least one is retired). osc_rt_h_free + * retires a slot freed at generation UINT64_MAX, else bumps its generation; + * generations never wrap. Use or free through a handle whose slot is not live + * at that generation traps OSC_TRAP_STALE. A bad pool id or slot traps + * OSC_TRAP_RUNTIME (never happens for checked code); no free pool table entry + * traps OSC_TRAP_OOM. Pools do not touch the array slots or live_count. */ +uint64_t osc_rt_pool_open(OscRt *rt, uint64_t k, uint64_t base); +void osc_rt_pool_close(OscRt *rt, uint64_t p); +uint64_t osc_rt_h_alloc(OscRt *rt, uint64_t p, uint64_t init); +uint64_t osc_rt_h_gen(OscRt *rt, uint64_t p, uint64_t slot); +void osc_rt_h_free(OscRt *rt, uint64_t p, uint64_t slot, uint64_t gen); +uint64_t osc_rt_h_load(OscRt *rt, uint64_t p, uint64_t slot, uint64_t gen); +void osc_rt_h_store(OscRt *rt, uint64_t p, uint64_t slot, uint64_t gen, uint64_t v); + #endif /* OSC_RT_H */ diff --git a/tests/compiler/neg/h_copy_after_free.osc b/tests/compiler/neg/h_copy_after_free.osc new file mode 100644 index 000000000..1c717a8d0 --- /dev/null +++ b/tests/compiler/neg/h_copy_after_free.osc @@ -0,0 +1,12 @@ +// expect: STALE_HANDLE object=c line=9 +// transition: read after free via a copy +fn f() -> i64 { + let mut s: i64 = 0; + pool p: [i64; 2] { + let h: handle p = p.alloc(1); + let c: handle p = h; + p.free(h); + s = p[c]; + } + return s; +} diff --git a/tests/compiler/neg/h_copy_double_free.osc b/tests/compiler/neg/h_copy_double_free.osc new file mode 100644 index 000000000..f1cd0b76c --- /dev/null +++ b/tests/compiler/neg/h_copy_double_free.osc @@ -0,0 +1,11 @@ +// expect: STALE_HANDLE object=c line=8 +// transition: free after free via a copy +fn f() -> i64 { + pool p: [i64; 2] { + let h: handle p = p.alloc(1); + let c: handle p = h; + p.free(h); + p.free(c); + } + return 0; +} diff --git a/tests/compiler/neg/h_double_free.osc b/tests/compiler/neg/h_double_free.osc new file mode 100644 index 000000000..6d3a90c90 --- /dev/null +++ b/tests/compiler/neg/h_double_free.osc @@ -0,0 +1,10 @@ +// expect: STALE_HANDLE object=h line=7 +// transition: free after free +fn f() -> i64 { + pool p: [i64; 2] { + let h: handle p = p.alloc(1); + p.free(h); + p.free(h); + } + return 0; +} diff --git a/tests/compiler/neg/h_handle_as_value.osc b/tests/compiler/neg/h_handle_as_value.osc new file mode 100644 index 000000000..a70bdb698 --- /dev/null +++ b/tests/compiler/neg/h_handle_as_value.osc @@ -0,0 +1,10 @@ +// expect: TYPE_MISMATCH object=h line=7 +// transition: handle used as a value +fn f() -> i64 { + let mut s: i64 = 0; + pool p: [i64; 2] { + let h: handle p = p.alloc(1); + s = h; + } + return s; +} diff --git a/tests/compiler/neg/h_handle_param.osc b/tests/compiler/neg/h_handle_param.osc new file mode 100644 index 000000000..578334e55 --- /dev/null +++ b/tests/compiler/neg/h_handle_param.osc @@ -0,0 +1,5 @@ +// expect: UNSUPPORTED object=handle line=3 +// transition: handle crosses a function boundary +fn f(h: handle p) -> i64 { + return 0; +} diff --git a/tests/compiler/neg/h_immutable_handle.osc b/tests/compiler/neg/h_immutable_handle.osc new file mode 100644 index 000000000..748c4a58d --- /dev/null +++ b/tests/compiler/neg/h_immutable_handle.osc @@ -0,0 +1,10 @@ +// expect: IMMUTABLE_ASSIGN object=h line=7 +fn f() -> i64 { + pool p: [i64; 2] { + let h: handle p = p.alloc(1); + p.free(h); + let g: handle p = p.alloc(2); + h = g; + } + return 0; +} diff --git a/tests/compiler/neg/h_not_a_handle.osc b/tests/compiler/neg/h_not_a_handle.osc new file mode 100644 index 000000000..fce2890b6 --- /dev/null +++ b/tests/compiler/neg/h_not_a_handle.osc @@ -0,0 +1,10 @@ +// expect: TYPE_MISMATCH object=i line=7 +// transition: pool index not a handle +fn f() -> i64 { + let mut s: i64 = 0; + let i: i64 = 0; + pool p: [i64; 2] { + s = p[i]; + } + return s; +} diff --git a/tests/compiler/neg/h_pool_as_value.osc b/tests/compiler/neg/h_pool_as_value.osc new file mode 100644 index 000000000..4735fed23 --- /dev/null +++ b/tests/compiler/neg/h_pool_as_value.osc @@ -0,0 +1,9 @@ +// expect: TYPE_MISMATCH object=p line=6 +// transition: pool used as a value +fn f() -> i64 { + let mut s: i64 = 0; + pool p: [i64; 2] { + s = p; + } + return s; +} diff --git a/tests/compiler/neg/h_pool_full.osc b/tests/compiler/neg/h_pool_full.osc new file mode 100644 index 000000000..e158e36e2 --- /dev/null +++ b/tests/compiler/neg/h_pool_full.osc @@ -0,0 +1,11 @@ +// expect: POOL_CAPACITY object=p line=7 +// transition: pool full +fn f() -> i64 { + let mut s: i64 = 0; + pool p: [i64; 1] { + let a: handle p = p.alloc(1); + let b: handle p = p.alloc(2); + s = p[a] + p[b]; + } + return s; +} diff --git a/tests/compiler/neg/h_pool_size.osc b/tests/compiler/neg/h_pool_size.osc new file mode 100644 index 000000000..4f8e66ebd --- /dev/null +++ b/tests/compiler/neg/h_pool_size.osc @@ -0,0 +1,7 @@ +// expect: POOL_CAPACITY object=p line=4 +// transition: pool size outside 1..16 +fn f() -> i64 { + pool p: [i64; 17] { + } + return 0; +} diff --git a/tests/compiler/neg/h_read_after_free.osc b/tests/compiler/neg/h_read_after_free.osc new file mode 100644 index 000000000..67adf5b4e --- /dev/null +++ b/tests/compiler/neg/h_read_after_free.osc @@ -0,0 +1,11 @@ +// expect: STALE_HANDLE object=h line=8 +// transition: read after free +fn f() -> i64 { + let mut s: i64 = 0; + pool p: [i64; 2] { + let h: handle p = p.alloc(1); + p.free(h); + s = p[h]; + } + return s; +} diff --git a/tests/compiler/neg/h_retired_reuse.osc b/tests/compiler/neg/h_retired_reuse.osc new file mode 100644 index 000000000..775a48be3 --- /dev/null +++ b/tests/compiler/neg/h_retired_reuse.osc @@ -0,0 +1,10 @@ +// expect: GENERATION_EXHAUSTED object=p line=7 +// transition: retired slot reused +fn f() -> i64 { + pool p: [i64; 1] gen 18446744073709551615 { + let mut h: handle p = p.alloc(1); + p.free(h); + h = p.alloc(2); + } + return 0; +} diff --git a/tests/compiler/neg/h_write_after_free.osc b/tests/compiler/neg/h_write_after_free.osc new file mode 100644 index 000000000..622395810 --- /dev/null +++ b/tests/compiler/neg/h_write_after_free.osc @@ -0,0 +1,11 @@ +// expect: STALE_HANDLE object=h line=8 +// transition: write after free +fn f() -> i64 { + let s: i64 = 0; + pool p: [i64; 2] { + let h: handle p = p.alloc(1); + p.free(h); + p[h] = 3; + } + return s; +} diff --git a/tests/compiler/neg/h_wrong_pool.osc b/tests/compiler/neg/h_wrong_pool.osc new file mode 100644 index 000000000..554390f29 --- /dev/null +++ b/tests/compiler/neg/h_wrong_pool.osc @@ -0,0 +1,13 @@ +// expect: WRONG_POOL object=h line=9 +// transition: handle of another pool +fn f() -> i64 { + let mut s: i64 = 0; + pool p: [i64; 2] { + pool q: [i64; 2] { + let h: handle p = p.alloc(1); + let g: handle q = q.alloc(2); + s = q[h] + q[g]; + } + } + return s; +} diff --git a/tests/compiler/neg/h_wrong_pool_alloc.osc b/tests/compiler/neg/h_wrong_pool_alloc.osc new file mode 100644 index 000000000..e69d11164 --- /dev/null +++ b/tests/compiler/neg/h_wrong_pool_alloc.osc @@ -0,0 +1,12 @@ +// expect: WRONG_POOL object=q line=7 +// transition: handle of another pool +fn f() -> i64 { + let mut s: i64 = 0; + pool p: [i64; 2] { + pool q: [i64; 2] { + let h: handle p = q.alloc(1); + s = p[h]; + } + } + return s; +} diff --git a/tests/compiler/progs/handles_basic.osc b/tests/compiler/progs/handles_basic.osc new file mode 100644 index 000000000..484f94913 --- /dev/null +++ b/tests/compiler/progs/handles_basic.osc @@ -0,0 +1,31 @@ +// entry: basic reuse +// OSC-3 item 2: versioned handles. A handle is a copyable (slot, generation) +// value of one function-local pool; it never owns its slot. Freeing bumps the +// slot generation, so the slot is reused by the next allocation. +// expect-run: basic 5 -> 23 +// expect-run: reuse 2 -> 8 +fn basic(k: i64) -> i64 { + let mut s: i64 = 0; + pool p: [i64; 2] { + let a: handle p = p.alloc(k); + let b: handle p = p.alloc(10); + p[a] = p[a] + p[b]; + s = p[a] + 8; + p.free(b); + p.free(a); + } + return s; +} + +fn reuse(k: i64) -> i64 { + let mut s: i64 = 0; + pool p: [i64; 1] { + let mut h: handle p = p.alloc(k); + let c: handle p = h; + s = p[c]; + p.free(h); + h = p.alloc(k * 3); + s = s + p[h]; + } + return s; +} diff --git a/tests/compiler/progs/handles_exhaust.osc b/tests/compiler/progs/handles_exhaust.osc new file mode 100644 index 000000000..ddc8e5b36 --- /dev/null +++ b/tests/compiler/progs/handles_exhaust.osc @@ -0,0 +1,51 @@ +// entry: retire full cycle +// OSC-3 item 2: a declared generation base reaches exhaustion in a test. A slot +// freed at generation 2^64 - 1 is retired, never wrapped; allocating when every +// slot is live or retired traps RETIRED (some slot retired) or POOL_FULL. +// expect-run: retire 2 -> 3 +// expect-run: retire 3 -> trap=RETIRED +// expect-run: full 3 -> 3 +// expect-run: full 4 -> trap=POOL_FULL +// expect-run: cycle 100 -> 4950 +// (cycle: 100 allocations from one slot; the model replay re-windows past +// its 64 handle ids) +fn retire(n: u8) -> i64 { + let mut s: i64 = 0; + pool p: [i64; 1] gen 18446744073709551614 { + let mut i: u8 = 0; + while i < n bound 8 { + let h: handle p = p.alloc(i as i64); + s = s + p[h] + 1; + p.free(h); + i = i + 1; + } + } + return s; +} + +fn full(n: u8) -> i64 { + let mut s: i64 = 0; + pool p: [i64; 3] { + let mut i: u8 = 0; + while i < n bound 8 { + let h: handle p = p.alloc(i as i64); + s = s + p[h]; + i = i + 1; + } + } + return s; +} + +fn cycle(n: u8) -> i64 { + let mut s: i64 = 0; + pool p: [i64; 1] { + let mut i: u8 = 0; + while i < n bound 200 { + let h: handle p = p.alloc(i as i64); + s = s + p[h]; + p.free(h); + i = i + 1; + } + } + return s; +} diff --git a/tests/compiler/progs/handles_stale.osc b/tests/compiler/progs/handles_stale.osc new file mode 100644 index 000000000..8dd4dc0a0 --- /dev/null +++ b/tests/compiler/progs/handles_stale.osc @@ -0,0 +1,43 @@ +// entry: stale dfree aba +// OSC-3 item 2: a free the checker cannot decide (behind a branch) leaves the +// handle and its copies checked at run time: a use or free whose generation +// no longer matches the slot traps STALE, also after the slot was reused. +// expect-run: stale 0 -> 7 +// expect-run: stale 1 -> trap=STALE +// expect-run: dfree 0 -> 1 +// expect-run: dfree 1 -> trap=STALE +// expect-run: aba 0 -> 10 +// expect-run: aba 1 -> trap=STALE +fn stale(k: u8) -> i64 { + let mut s: i64 = 0; + pool p: [i64; 2] { + let h: handle p = p.alloc(7); + let c: handle p = h; + if k > 0 { p.free(h); } + s = p[c]; + } + return s; +} + +fn dfree(k: u8) -> i64 { + pool p: [i64; 1] { + let h: handle p = p.alloc(5); + if k == 1 { p.free(h); } + p.free(h); + } + return 1; +} + +fn aba(k: u8) -> i64 { + let mut s: i64 = 0; + pool p: [i64; 1] { + let mut h: handle p = p.alloc(5); + let old: handle p = h; + if k > 0 { + p.free(h); + h = p.alloc(9); + } + s = p[h] + p[old]; + } + return s; +} diff --git a/tests/compiler/test_osc_compiler.c b/tests/compiler/test_osc_compiler.c index 15f2eb2a6..d3037b560 100644 --- a/tests/compiler/test_osc_compiler.c +++ b/tests/compiler/test_osc_compiler.c @@ -194,6 +194,159 @@ static int replay_trace(const OscTrace *t, uint32_t i0, uint32_t i1, OscModelRej * objects (with their regions) under fresh ids. Every event of the log is * replayed; a trapping run's log is a prefix and replays the same way. * Returns 0 (all accepted) or -1 (why says which event and reason). */ +/* OSC-3 item 2: runtime pool events -> OSC-0B slot/handle model events. One + * model per open pool (osc_model_init(gen_base = the declared base, + * gen_max = UINT64_MAX)); model slot = pool slot + 1. Each SLOT_ALLOC mints a + * fresh model handle (rights 3); HANDLE_USE and SLOT_FREE name the live + * handle of the slot. Every logged generation must equal the model slot's + * generation. Past 64 handles a fresh model is advanced to the same slot + * generations (ALLOC with no handle + explicit SLOT_FREE per generation step) + * and the live slots re-minted. A STALE / RETIRED / POOL_FULL trap is then + * confirmed: the refused operation replays as a model rejection + * (STALE_GENERATION / GENERATION_WRAP / every slot live). */ +static unsigned long rp_events, rp_stale_confirmed, rp_wrap_confirmed, rp_full_confirmed, rp_windows; +typedef struct { + OscModel m; + uint64_t base; + uint8_t k, open; + uint32_t next; /* next model handle id */ + uint32_t hid[OSC_POOL_SLOTS]; /* live handle of each slot (0 = none) */ + uint32_t mslot[OSC_MODEL_MAX_HANDLES + 1]; /* minted handle -> pool slot + 1 */ + uint64_t mgen[OSC_MODEL_MAX_HANDLES + 1]; /* minted handle -> generation */ +} RpPool; +static RpPool rp[OSC_RT_POOLS]; + +static int rp_step(OscModel *m, uint32_t kind, uint32_t slot, uint32_t handle, uint64_t gen, uint64_t rights, + OscModelReject *r) +{ + OscModelEvent ev; + memset(&ev, 0, sizeof ev); + ev.kind = kind; + ev.slot = slot; + ev.handle = handle; + ev.gen = gen; + ev.rights = rights; + return osc_model_step(m, &ev, r) == OSC_MODEL_ACCEPT ? 0 : -1; +} + +/* fresh model whose slots match q's: free/live at the same generation, or retired */ +static int rp_rewindow(RpPool *q, char *why, size_t n) +{ + OscModel old = q->m; + rp_windows++; + osc_model_init(&q->m, q->base, UINT64_MAX); + q->next = 1; + memset(q->mslot, 0, sizeof q->mslot); + for (uint32_t s = 1; s <= q->k; s++) { + const OscMSlot *o = &old.slot[s]; + for (uint64_t g = q->base; g != o->gen || o->state == 2 /* retired: also free gen_max */; g++) { + if (rp_step(&q->m, OSC_EV_SLOT_ALLOC, s, 0, 0, 0, NULL) || + rp_step(&q->m, OSC_EV_SLOT_FREE, s, 0, g, 0, NULL)) { snprintf(why, n, "re-window advance"); return -1; } + if (g == UINT64_MAX) break; + } + q->hid[s - 1] = 0; + if (o->state == 1) { /* live: re-mint */ + uint32_t h = q->next++; + if (rp_step(&q->m, OSC_EV_SLOT_ALLOC, s, h, 0, 3, NULL)) { snprintf(why, n, "re-window mint"); return -1; } + q->hid[s - 1] = h; + q->mslot[h] = s; + q->mgen[h] = q->m.slot[s].gen; + } + if (q->m.slot[s].state != o->state || q->m.slot[s].gen != o->gen) { snprintf(why, n, "re-window state"); return -1; } + } + return 0; +} + +static int rp_event(const OscRt *rt, uint32_t i, char *why, size_t n) +{ + const OscRtEvent *e = &rt->ev[i]; + uint64_t gen = rt->ev_gen[i]; + if (e->slot >= OSC_RT_POOLS) { snprintf(why, n, "pool event on bad pool %u", e->slot); return -1; } + RpPool *q = &rp[e->slot]; + unsigned ps = e->len & 0xff, rights = e->len >> 8; + OscModelReject r = OSC_REJ_NONE; + rp_events++; + switch (e->kind) { + case OSC_RT_EV_POOL_OPEN: + if (q->open || e->len < 1 || e->len > OSC_POOL_SLOTS) { snprintf(why, n, "bad pool open"); return -1; } + memset(q, 0, sizeof *q); + q->open = 1; + q->k = (uint8_t)e->len; + q->base = gen; + q->next = 1; + osc_model_init(&q->m, gen, UINT64_MAX); + return 0; + case OSC_RT_EV_POOL_CLOSE: + if (!q->open) { snprintf(why, n, "close of a closed pool"); return -1; } + q->open = 0; + return 0; + case OSC_RT_EV_SLOT_ALLOC: + if (!q->open || ps >= q->k) { snprintf(why, n, "bad slot alloc"); return -1; } + if (q->next > OSC_MODEL_MAX_HANDLES && rp_rewindow(q, why, n)) return -1; + if (q->m.slot[ps + 1].gen != gen) { snprintf(why, n, "alloc generation %llu != model %llu", (unsigned long long)gen, (unsigned long long)q->m.slot[ps + 1].gen); return -1; } + if (rp_step(&q->m, OSC_EV_SLOT_ALLOC, ps + 1, q->next, 0, 3, &r)) break; + q->hid[ps] = q->next; + q->mslot[q->next] = ps + 1; + q->mgen[q->next] = gen; + q->next++; + return 0; + case OSC_RT_EV_SLOT_FREE: + case OSC_RT_EV_HANDLE_USE: + if (!q->open || ps >= q->k || !q->hid[ps]) { snprintf(why, n, "pool event on a slot with no handle"); return -1; } + if (q->mgen[q->hid[ps]] != gen) { snprintf(why, n, "handle generation %llu != minted %llu", (unsigned long long)gen, (unsigned long long)q->mgen[q->hid[ps]]); return -1; } + if (e->kind == OSC_RT_EV_SLOT_FREE) { + if (rp_step(&q->m, OSC_EV_SLOT_FREE, 0, q->hid[ps], 0, 0, &r)) break; + q->hid[ps] = 0; + } else { + if (rights < 1 || rights > 2) { snprintf(why, n, "bad use rights"); return -1; } + if (rp_step(&q->m, OSC_EV_HANDLE_USE, 0, q->hid[ps], 0, rights, &r)) break; + } + return 0; + default: + snprintf(why, n, "unknown pool event %u", e->kind); + return -1; + } + snprintf(why, n, "pool event %u rejected: %s", i, osc_model_reject_name(r)); + return -1; +} + +/* the refused operation of a pool trap is rejected by the model */ +static int rp_trap(const OscRt *rt, char *why, size_t n) +{ + unsigned code = rt->trap_code; + if (code != OSC_TRAP_STALE && code != OSC_TRAP_RETIRED && code != OSC_TRAP_POOL_FULL) return 0; + if (rt->nev > OSC_RT_EVENTS) return 0; /* log prefix not complete: nothing to confirm */ + if (rt->trap_pool >= OSC_RT_POOLS || !rp[rt->trap_pool].open) { snprintf(why, n, "pool trap on unopened pool"); return -1; } + RpPool *q = &rp[rt->trap_pool]; + OscModel m = q->m; + OscModelReject r = OSC_REJ_NONE; + if (code == OSC_TRAP_STALE) { + uint32_t s = rt->trap_slot + 1u, h = 0; + if (s > q->k) { snprintf(why, n, "stale trap on bad slot"); return -1; } + for (uint32_t k = 1; k < q->next && k <= OSC_MODEL_MAX_HANDLES; k++) + if (q->mslot[k] == s && q->mgen[k] == rt->trap_gen) h = k; + int rc; + if (h && rt->trap_op == 1) rc = rp_step(&m, OSC_EV_HANDLE_USE, 0, h, 0, rt->trap_rights, &r); + else if (h) rc = rp_step(&m, OSC_EV_SLOT_FREE, 0, h, 0, 0, &r); + else rc = rp_step(&m, OSC_EV_SLOT_FREE, s, 0, rt->trap_gen, 0, &r); + if (rc == 0 || r != OSC_REJ_STALE_GENERATION) { snprintf(why, n, "stale trap not a model STALE_GENERATION (%s)", osc_model_reject_name(r)); return -1; } + rp_stale_confirmed++; + return 0; + } + /* RETIRED / POOL_FULL: no slot of the pool can be allocated */ + unsigned wrap = 0; + for (uint32_t s = 1; s <= q->k; s++) { + OscModel t = m; + if (rp_step(&t, OSC_EV_SLOT_ALLOC, s, 0, 0, 0, &r) == 0) { snprintf(why, n, "pool trap but model slot %u is free", s); return -1; } + if (r == OSC_REJ_GENERATION_WRAP) wrap++; + else if (r != OSC_REJ_PROTOCOL) { snprintf(why, n, "pool trap: unexpected %s", osc_model_reject_name(r)); return -1; } + } + if ((code == OSC_TRAP_RETIRED) != (wrap > 0)) { snprintf(why, n, "pool trap kind disagrees with the model"); return -1; } + if (code == OSC_TRAP_RETIRED) rp_wrap_confirmed++; + else rp_full_confirmed++; + return 0; +} + static unsigned long rr_runs, rr_events, rr_accepted, rr_rejected; static int replay_rt(const OscRt *rt, char *why, size_t n) { @@ -208,9 +361,15 @@ static int replay_rt(const OscRt *rt, char *why, size_t n) osc_model_init(&m, 0, UINT64_MAX); memset(live_id, 0, sizeof live_id); memset(rid_of_slot, 0, sizeof rid_of_slot); + memset(rp, 0, sizeof rp); uint32_t nev = rt->nev < OSC_RT_EVENTS ? rt->nev : OSC_RT_EVENTS; for (uint32_t i = 0; i < nev; i++) { const OscRtEvent *e = &rt->ev[i]; + if (e->kind >= OSC_RT_EV_POOL_OPEN) { /* OSC-3 item 2 */ + if (rp_event(rt, i, why, n)) return -1; + rr_events++; + continue; + } if (e->serial >= (1u << 16)) { snprintf(why, n, "serial too large"); return -1; } int is_alloc = e->kind == OSC_RT_EV_ALLOC || e->kind == OSC_RT_EV_ARENA_ALLOC; if ((is_alloc && next > OSC_MODEL_MAX_OBJECTS) || @@ -294,6 +453,7 @@ static int replay_rt(const OscRt *rt, char *why, size_t n) } rr_events++; } + if (rp_trap(rt, why, n)) return -1; return 0; } @@ -314,7 +474,7 @@ static int rt_replay_run(const OscRt *rt, const char *ctx) /* ------------------------------------------------------------ golden */ static OscUnit *U1, *U2; static unsigned long expect_total; -static const char *trap_names[OSC_TRAP_MAX + 1] = {"none", "OVERFLOW", "DIV0", "BOUNDS", "LOOP_BOUND", "CAST", "OOM", "SHIFT", "RUNTIME", "REQUIRES", "ENSURES", "ARENA_FULL"}; +static const char *trap_names[OSC_TRAP_MAX + 1] = {"none", "OVERFLOW", "DIV0", "BOUNDS", "LOOP_BOUND", "CAST", "OOM", "SHIFT", "RUNTIME", "REQUIRES", "ENSURES", "ARENA_FULL", "STALE", "POOL_FULL", "RETIRED"}; static uint64_t parse_val(const char *s) { @@ -507,6 +667,7 @@ static const char *mapped(int kind) case OSC_DIAG_BORROW_OUTLIVES_OWNER: return "borrow-outlives-owner"; case OSC_DIAG_READ_ONLY_BORROW: return "forged-rights"; case OSC_DIAG_ARENA_ESCAPE: return "arena-escape"; + case OSC_DIAG_STALE_HANDLE: return "stale-generation"; /* OSC-3 item 2 */ default: return NULL; } } @@ -1110,6 +1271,202 @@ static void arena_fuzz(unsigned n) CHECK(af_trap[OSC_TRAP_ARENA_FULL] > 0, "arena fuzz never hit TRAP ARENA_FULL"); } +/* ------------------------------------------------------------ OSC-3 item 2 handle fuzz */ +/* Generated units: one pool (K = 1..4 slots of i64, generation base 0, small, + * or within 3 of 2^64 - 1) with 1..3 mutable handles (the first allocated, + * the others copies), then random reads, writes, conditional frees, + * re-allocations (leaking the old slot), free + re-allocate pairs, copies, + * bounded loops of alloc/free cycles and a nested second pool. Frees behind a + * dynamic condition make later uses stale at run time only (TRAP STALE); + * leaks fill the pool (TRAP POOL_FULL); near-maximum bases retire slots + * (TRAP RETIRED). The generator keeps definite allocations within the static + * K and generation budgets, so a unit is either compiled or refused with + * STALE_HANDLE (a copy made stale by a definite free, a static use after + * free); any other refusal fails. Interpreter and native outcomes must be + * identical; every native run replays through the OSC-0B model. */ +static unsigned long hf_units, hf_compiled, hf_refused, hf_runs, hf_mismatch, hf_trap[OSC_TRAP_MAX + 1]; +typedef struct { unsigned k, nh, used, off; uint64_t base, dallocs; } HfPool; + +static int hf_budget(const HfPool *p) +{ + uint64_t per = UINT64_MAX - p->base; + if (!p->off && p->used + 1 > p->k) return 0; + if (per < 8 && p->dallocs + 1 > (uint64_t)p->k * (per + 1)) return 0; + return 1; +} + +/* one statement; definite = at the pool's own depth */ +static void hf_stmt(char *src, size_t cap, HfPool *p, int definite, unsigned depth, unsigned *nm) +{ + unsigned h = (unsigned)(sm() % p->nh), h2 = (unsigned)(sm() % p->nh); + switch (sm() % 9) { + case 0: case 1: + cf_cat(src, cap, " acc = acc + p0[h%u];\n", h); + break; + case 2: + cf_cat(src, cap, " p0[h%u] = (acc & 1023) + (b & 7);\n", h); + break; + case 3: + cf_cat(src, cap, " if ((a >> %u) & 3) == 0 {\n p0.free(h%u);\n }\n", (unsigned)(sm() % 40), h); + p->off = 1; + break; + case 4: /* re-allocate, leaking the old slot */ + if (definite && !hf_budget(p)) { + cf_cat(src, cap, " if (b & %u) == 1 {\n h%u = p0.alloc(a & 255);\n }\n", 1u + (unsigned)(sm() % 7), h); + } else { + cf_cat(src, cap, " h%u = p0.alloc(a & 255);\n", h); + if (definite) { p->used++; p->dallocs++; } + } + break; + case 5: /* free + re-allocate */ + if (definite && !(p->dallocs + 1 <= (uint64_t)p->k * ((UINT64_MAX - p->base) < 8 ? (UINT64_MAX - p->base) + 1 : 64))) { + cf_cat(src, cap, " acc = acc + p0[h%u];\n", h); + } else { + cf_cat(src, cap, " p0.free(h%u);\n h%u = p0.alloc(i as i64);\n", h, h); + if (definite) p->dallocs++; + else p->off = 1; + } + break; + case 6: + if (h != h2) cf_cat(src, cap, " h%u = h%u;\n", h2, h); + else cf_cat(src, cap, " acc = acc + p0[h%u];\n", h); + break; + case 7: + if (depth < 2) { + unsigned j = (*nm)++; + cf_cat(src, cap, " let mut j%u: u8 = 0;\n while j%u < (i %% 6) bound 6 {\n", j, j); + unsigned ns = 1 + (unsigned)(sm() % 3); + for (unsigned s = 0; s < ns; s++) hf_stmt(src, cap, p, 0, depth + 1, nm); + cf_cat(src, cap, " j%u = j%u + 1;\n }\n", j, j); + } else { + cf_cat(src, cap, " acc = acc + p0[h%u];\n", h); + } + break; + default: + if (depth == 0 && sm() % 2) { + unsigned q = (*nm)++; + cf_cat(src, cap, " pool q%u: [u8; %u] {\n let g%u: handle q%u = q%u.alloc((a & 7) as u8);\n" + " q%u[g%u] = q%u[g%u] + 1;\n acc = acc + (q%u[g%u] as i64);\n }\n", + q, 1u + (unsigned)(sm() % 3), q, q, q, q, q, q, q, q, q); + } else { + cf_cat(src, cap, " acc = acc + p0[h%u];\n", h); + } + } +} + +static void handle_fuzz(unsigned n) +{ + static char src[16384]; + const unsigned long ev0 = rp_events, st0 = rp_stale_confirmed, wr0 = rp_wrap_confirmed, fu0 = rp_full_confirmed, wi0 = rp_windows; + sm_state = 0x05C3A2B4ull; + for (unsigned u = 0; u < n; u++) { + HfPool p; + memset(&p, 0, sizeof p); + p.k = 1 + (unsigned)(sm() % 4); + p.nh = 1 + (unsigned)(sm() % 3); + switch (sm() % 3) { + case 0: p.base = 0; break; + case 1: p.base = sm() % 1000; break; + default: p.base = UINT64_MAX - sm() % 4; + } + src[0] = 0; + cf_cat(src, sizeof src, "fn entry(a: i64, b: i64, i: u8) -> i64 {\n let mut acc: i64 = 0;\n" + " pool p0: [i64; %u] gen %llu {\n let mut h0: handle p0 = p0.alloc(a & 1023);\n", + p.k, (unsigned long long)p.base); + p.used = 1; + p.dallocs = 1; + for (unsigned h = 1; h < p.nh; h++) cf_cat(src, sizeof src, " let mut h%u: handle p0 = h0;\n", h); + unsigned nm = 0, ns = 3 + (unsigned)(sm() % 6); + for (unsigned s = 0; s < ns; s++) hf_stmt(src, sizeof src, &p, 1, 0, &nm); + cf_cat(src, sizeof src, " acc = acc + p0[h0];\n }\n return acc;\n}\n"); + hf_units++; + + OscDiag d; + int rc = osc_compile(src, strlen(src), U1, &d, NULL); + if (rc) { + CHECK(d.kind == OSC_DIAG_STALE_HANDLE, "handle fuzz unit %u refused %s line %u object=%s: %s\n%s", u, + osc_diag_kind_name(d.kind), d.line, d.object, d.message, src); + hf_refused++; + continue; + } + hf_compiled++; + OscCode c; + char err[160]; + memset(&c, 0, sizeof c); + int cg = osc_cg_compile(U1, &c, err, sizeof err); + CHECK(cg == 0, "handle fuzz unit %u codegen refused: %s", u, err); + if (cg) continue; + OscNative nm2; + int mr = osc_native_map(&nm2, c.code, c.len); + CHECK(mr == 0, "handle fuzz unit %u native map failed (%d)", u, mr); + if (mr) { osc_cg_free(&c); continue; } + int fi = U1->nfuncs - 1; + void *entry = osc_native_at(&nm2, c.entry[fi]); + for (unsigned it = 0; it < 48; it++) { + uint64_t args[OSC_MAX_PARAMS] = {0}, ri = 0, rn = 0; + args[0] = gen_arg(OSC_T_I64); + args[1] = gen_arg(OSC_T_I64); + args[2] = it & 1 ? (uint64_t)(it % 6) : gen_arg(OSC_T_U8); + osc_rt_reset(RI); + osc_rt_reset(RN); + int t1 = osc_interp_run_prevalidated(U1, fi, args, 3, RI, &ri); + int t2 = osc_rt_call_native(RN, entry, args, 3, &rn); + rt_replay_run(RN, "handle fuzz"); + hf_runs++; + diff_runs++; + int same = t1 == t2 && t1 >= 0 && (t1 != 0 || ri == rn) && osc_rt_same_outcome(RI, RN); + CHECK(same, "handle fuzz unit %u args %llu %llu %llu: interp trap %d ret %llu vs native trap %d ret %llu\n%s", + u, (unsigned long long)args[0], (unsigned long long)args[1], (unsigned long long)args[2], t1, + (unsigned long long)ri, t2, (unsigned long long)rn, src); + if (!same) { hf_mismatch++; break; } + if (t1 >= 0 && t1 <= OSC_TRAP_MAX) { hf_trap[t1]++; trap_seen[t1]++; } + CHECK(t1 != OSC_TRAP_RUNTIME, "handle fuzz unit %u RUNTIME trap\n%s", u, src); + } + osc_native_unmap(&nm2); + osc_cg_free(&c); + } + printf("osc3 handles: units=%lu compiled=%lu static_refused=%lu runs=%lu ok=%lu stale_traps=%lu pool_full=%lu " + "retired=%lu overflow=%lu mismatches=%lu\n", + hf_units, hf_compiled, hf_refused, hf_runs, hf_trap[0], hf_trap[OSC_TRAP_STALE], hf_trap[OSC_TRAP_POOL_FULL], + hf_trap[OSC_TRAP_RETIRED], hf_trap[OSC_TRAP_OVERFLOW], hf_mismatch); + printf("osc3 handles: model replay pool_events=%lu stale_confirmed=%lu wrap_confirmed=%lu full_confirmed=%lu " + "windows=%lu (handle fuzz runs only); all runs re-windows=%lu\n", rp_events - ev0, rp_stale_confirmed - st0, rp_wrap_confirmed - wr0, + rp_full_confirmed - fu0, rp_windows - wi0, rp_windows); + CHECK(hf_trap[0] > 0, "handle fuzz never returned normally"); + CHECK(hf_trap[OSC_TRAP_STALE] > 0, "handle fuzz never hit TRAP STALE"); + CHECK(hf_trap[OSC_TRAP_POOL_FULL] > 0, "handle fuzz never hit TRAP POOL_FULL"); + CHECK(hf_trap[OSC_TRAP_RETIRED] > 0, "handle fuzz never hit TRAP RETIRED"); + CHECK(rp_stale_confirmed - st0 == hf_trap[OSC_TRAP_STALE], "stale traps %lu, model-confirmed %lu", + hf_trap[OSC_TRAP_STALE], rp_stale_confirmed - st0); + CHECK(rp_wrap_confirmed - wr0 == hf_trap[OSC_TRAP_RETIRED], "retired traps %lu, model-confirmed %lu", + hf_trap[OSC_TRAP_RETIRED], rp_wrap_confirmed - wr0); + CHECK(rp_full_confirmed - fu0 == hf_trap[OSC_TRAP_POOL_FULL], "pool_full traps %lu, model-confirmed %lu", + hf_trap[OSC_TRAP_POOL_FULL], rp_full_confirmed - fu0); + CHECK(hf_compiled > hf_refused, "handle fuzz mostly refused (%lu of %lu)", hf_refused, hf_units); + /* serial counters never wrap: at UINT32_MAX the next allocation traps RUNTIME, before any state changes */ + unsigned long sw_traps = 0; + for (int w = 0; w < 4; w++) { + osc_rt_reset(RI); + volatile uint64_t ah = 0; + if (w == 2 && setjmp(RI->jb) == 0) ah = osc_rt_arena_open(RI, 4); + if (w == 3) RI->pool_serial = UINT32_MAX; + else RI->next_serial = UINT32_MAX; + volatile unsigned live = RI->live_count; + if (setjmp(RI->jb) == 0) { + if (w == 0) osc_rt_alloc(RI, 1, 0); + else if (w == 1) osc_rt_arena_open(RI, 4); + else if (w == 2) osc_rt_arena_alloc(RI, ah, 1, 0); + else osc_rt_pool_open(RI, 2, 0); + CHECK(0, "serial counter %d wrapped without a trap", w); + } else { + CHECK(RI->trap_code == OSC_TRAP_RUNTIME && RI->live_count == live, "serial wrap %d trapped %u", w, + RI->trap_code); + sw_traps++; + } + } + printf("osc3 handles: serial counters at UINT32_MAX trap RUNTIME, never wrap: %lu of 4\n", sw_traps); +} + int main(int argc, char **argv) { unsigned fuzz = argc > 1 ? (unsigned)strtoul(argv[1], NULL, 10) : 1000; @@ -1144,6 +1501,7 @@ int main(int argc, char **argv) arena_dtor_order(pdir); struct_fuzz(fuzz / 8 ? fuzz / 8 : 16); arena_fuzz(fuzz / 8 ? fuzz / 8 : 16); + handle_fuzz(fuzz / 8 ? fuzz / 8 : 16); const char *const *tn = trap_names; printf("trap coverage (runs, interpreter == native):\n");