interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / docs / osc / OSC-EXT-BYTES-DESIGN.md

119 lines · 6953 bytes Raw Blame History Rendered
1# OSC external byte slice: design spec (not implemented)
2
3**Status**: implemented on branch osh/osc-ext-bytes (arch#158).
4**Written by**: Lane 42, migration seam 1 (CRB1 crumb reader), 2026-10-01.
5**Why**: prerequisite for an Omega-written reader of crumb lesson files. The reader contract is
6aien-protocols `specs/crumb-visible/CRUMB_READER_CONTRACT.md` 1.0.0 (reader = pure function of a
7caller-owned, read-only byte buffer given as pointer + length). OSC today has no way to receive
8such a buffer: arrays come only from its own pool (`osc_rt.h` cells, `OSC_MAX_ARRAY_LEN` 64 in
9`osc_ir.h`), and a CRB1 record can be thousands of bytes.
10
11## 1. Feature
12
13Two new parameter types, usable only as function parameters:
14
15| Type | Meaning | Passed as |
16|---|---|---|
17| `bytes` | read-only borrowed external byte slice | two consecutive parameter registers: pointer, then length (u64) |
18| `cells` | caller-supplied writable u64 output slice | two consecutive parameter registers: pointer, then length in cells (u64) |
19
20`bytes` is the required minimum ("read a caller-supplied byte buffer as an external input").
21`cells` is the matching output side: a CRB1 record decodes to up to 64 x 16 lane values, more
22than one OSC array holds, so results go to a caller buffer (or are reduced to a digest).
23
24## 2. Grammar (additions)
25
26```
27param += NAME ':' 'bytes' | NAME ':' 'cells'
28expr += NAME '.' 'len' (u64; NAME is a bytes or cells parameter)
29 | NAME '[' expr ']' (bytes: u8 load zero-extended to u64; cells: u64 load)
30stmt += NAME '[' expr ']' '=' expr ';' (cells only: u64 store)
31```
32
33## 3. Semantics
34
35- **Borrow for one call.** The caller owns both buffers. They are valid from entry to return of
36 the outermost compiled function and never afterwards. The runtime keeps no reference.
37- **Bounds.** Every `b[i]` and `c[i]` checks `i < len` (unsigned) before the access and traps
38 `OSC_TRAP_BOUNDS` (3) otherwise. No unchecked form exists.
39- **Read only input.** A `bytes` parameter is never written; there is no syntax for it.
40- **Empty slices.** `len = 0` is valid; the pointer may then be null and every index traps.
41- **No aliasing.** The caller guarantees the `bytes` and `cells` ranges do not overlap. The host
42 harness checks this before the call and refuses overlapping buffers (it never calls in).
43- **Passing down.** A slice parameter may be passed whole to another compiled function's slice
44 parameter (same type). It uses two of the six parameter registers (x0..x5; x7 stays the
45 runtime context).
46- **Determinism.** Loads and stores have no side effects beyond the cells written; the result of
47 a call is a function of the input bytes and the arguments only.
48
49## 4. Static refusals (named, at compile time)
50
51- `SLICE_ESCAPE`: a slice is returned, stored in a local, struct field, array element, pool, arena
52 or any durable state.
53- `SLICE_VALUE`: a slice is used as a value (arithmetic, comparison, cast), other than
54 `.len` and indexing.
55- `SLICE_WRITE`: an assignment to an element of a `bytes` parameter.
56- `SLICE_PARAMS`: a function whose parameters need more than six registers once each slice counts
57 as two.
58
59## 5. Native ABI (AArch64)
60
61Pointer in `xN`, length in `xN+1`, in declaration order. Element access lowers to
62`CMP idx, len` + `B.HS trap_bounds` + `LDRB`/`LDR`/`STR` with register offset (cells scaled by 8).
63Native code is entered through `osc_rt_call_native` (`osc_rt.h`) and the interpreter through
64`osc_interp_run` (`osc_interp.h`); both take the same pointer and length, so the native vs
65interpreter differential covers slices unchanged.
66
67## 6. Encoding and versioning
68
69A compiled unit's canonical encoding gains two type tags. Per the repository rule (magic +
70version; old versions refused, never reinterpreted) this is a unit-encoding version bump; a unit
71using slices is refused by a compiler or loader that does not know the new version.
72
73## 7. Acceptance (for the implementing lane)
74
751. Compiler tests: in-range loads; `i = len` traps BOUNDS; `len = 0` traps on any index; each
76 named refusal fires; native and interpreter agree on every test.
772. Host harness under ASan/UBSan: a buffer of exactly `len` bytes (no slack) with guard pages or
78 ASan redzones shows no out-of-range access.
793. Then, as separate work (not part of this feature): a CRB1 reader written in `.osc` behind the
80 same entry shape as `cl_crumb_decode` passes
81 `CRUMB_VISIBLE_V1_CONFORMANCE impl=osc pass=171 fail=0` on the shared corpus with the
82 **contract** codes (aien-protocols specs/crumb-visible, vendored in `tests/crumbline/crb1`),
83 not the current C codes: the C reader has 16 recorded code differences
84 (`tests/crumbline/crb1_findings.txt`). Contract section 6 also requires a UTF-8 validity check
85 (invalid UTF-8 is LANE, valid non-digit is NONCANONICAL), which the `.osc` reader must code.
86
87## 8. Not in scope
88
89Writing the Omega reader; any change to `src/runtime`; strings, allocation from slices, pointer
90arithmetic, mutable input, slices in structs; any change to the crumb format.
91
92## 9. Design gaps found while implementing
93
941. No byte load or store in the assembler. Added `OSC_A64_LDRB_REG` (register offset,
95 `0x38606800 | Rm<<16 | Rn<<5 | Rt`) with its decoder entry, so the self-check in `osc_cg.c`
96 passes. The encoding was cross-checked against GNU `as`/`objdump`.
972. `bounds()` compared against an immediate. Added `bounds_r()` in `osc_cg.c`: an unsigned
98 register compare (`cmp` then `b.hs` to the BOUNDS trap). Negative signed indices are huge
99 unsigned values, so they trap too. An empty slice never touches its pointer.
1003. The interpreter refuses addresses outside its pool. Slice access is its own checked path
101 (`sload`/`sstore` in `osc_interp.c`: `idx >= len` traps, then reads or writes through the
102 caller pointer). The pool check is unchanged.
103
104Smaller gaps closed on the way: there was no unit loader, so `osc_ir_encoding_version` gives the
105"unknown version refused" rule (plain units stay version 1 to 4, slice units are 5); there was no
106host entry, so `osc_ir_slice_args_ok` refuses overlapping, NULL-with-length or wrapping buffers
107before the call; `b[i]` is typed u64 (u8 zero-extended).
108
109### Review fixes (PR #335 hostile review)
110
111- Native entry was unchecked: `osc_native_call` (osc_native.c) runs `osc_ir_slice_args_ok` and
112 refuses (returns -1, never calls in) before `osc_rt_call_native`, which stays the raw entry.
113 The slice tests' native path goes through it.
114- Static aliasing: passing one `cells` slice to two parameters of one call is refused at compile
115 time as `SLICE_ALIAS` (a cells passed as `bytes` is already a type mismatch). `bytes` twice is
116 harmless and allowed.
117- `osc_ir_slice_args_ok` also refuses a `cells` pointer that is not 8-byte aligned (both paths).
118- `ensures result <= b.len` is now accepted: a slice length is immutable, so contracts may read
119 `.len`. Reading `cells` elements in `ensures` stays refused.