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
omega-mv / om / codec / README.md
| 1 | # om/codec: the graph codec, canonical form and validator in Omega |
| 2 | |
| 3 | `codec.om` ports `src/omega_codec.c`, `src/omega_canonical.c` and `src/omega_validate.c` (with |
| 4 | `omega_codec.h`, `omega_canonical.h`, `omega_validate.h`, `omega_types.h`) to seeded Omega, for |
| 5 | aien-dev/omega-mv#726. It also ports the parts of `src/omega_core.c` that those files call: the |
| 6 | effect payload decode (`omega_effect_read`) and the four builders that `omega_graph_parse_text` |
| 7 | uses. No C file changes. Nothing is linked into the product yet. |
| 8 | |
| 9 | | file | role | |
| 10 | |---|---| |
| 11 | | `codec.om` | the library (12 exported functions) and, after the `note ==== parity harness` line, a program-mode harness | |
| 12 | | `parity/corpus.py` | deterministic corpus generator (wire graphs, text inputs, hex-id records) | |
| 13 | | `parity/expected.txt` | sha256 of each output stream as the C oracle wrote it | |
| 14 | | `parity/run.sh` | compiles `codec.om`, regenerates the corpus, compares every stream with `expected.txt`; `--mutants` adds 21 mutants | |
| 15 | | `parity/unit.py` | cuts the function-mode unit of one exported function (for omega-bridge ABI v1) | |
| 16 | |
| 17 | ## What the C files contain (and where the task text differs) |
| 18 | |
| 19 | The task text speaks of "programs/ops/operands" and "decode bytes into a program record". The C has |
| 20 | no program or op list. It has an `OmegaGraph` of up to 256 semantic objects. Each object has a kind, |
| 21 | up to 32 attributes (key and value), 64 relations (kind and target id), 32 constraints and a |
| 22 | 1024-byte payload. The payload bytes are read through host structs (`TypePayload`, `ValuePayload`, |
| 23 | `OperationPayload`, `ApplyPayload`). The port follows the files: |
| 24 | |
| 25 | - `omega_graph_deserialize_binary`: the `OMGG` wire format becomes a graph, and every object gets |
| 26 | its semantic id. |
| 27 | - `omega_graph_serialize_binary`, `omega_canonical_encode`, `omega_compute_semantic_id`: the |
| 28 | canonical encoding with sorted attributes, relations and constraints, and its sha256. |
| 29 | - `omega_validate_graph` and `omega_validate_object`: the C returns only `-1` for every failure. |
| 30 | What tells the failures apart is the message the C writes into `err_msg`, so the port writes the |
| 31 | same message byte for byte. |
| 32 | - `omega_graph_format_text` and `omega_graph_parse_text`. Both are in `omega_codec.c`, though the |
| 33 | task text does not name them. |
| 34 | - `omega_hex_semantic_id`, `omega_parse_hex_semantic_id`, `omega_compare_semantic_id`, |
| 35 | `omega_canonical_object_version`, `omega_canonical_check_header`. |
| 36 | |
| 37 | **The task corpus does not exist.** The task asked for every `.omega`/`.osc`/`.bin` file under |
| 38 | `tests/` and `examples/` that the C decoder accepts: |
| 39 | - There is no `examples/` directory. |
| 40 | - `tests/` has 248 such files (246 `.osc` source texts and 2 unrelated `.bin`). The C decoder |
| 41 | accepts none of them, because none starts with `OMGG`. The port refuses the same 248, and |
| 42 | `run.sh` reports this. |
| 43 | |
| 44 | The accepted graphs therefore come from two sources: |
| 45 | - **The product's own builders.** These are the `omegatool --dump-test-vectors` graphs, the |
| 46 | physics law graph, and one graph that uses every builder. `corpus.py` rebuilds them, and |
| 47 | `--check-product` checks that they are byte-equal to what the C builders write. |
| 48 | - **Generated graphs** (below). |
| 49 | |
| 50 | ## Design: a graph is a wire image plus an index |
| 51 | |
| 52 | The C `OmegaGraph` is 6,648,836 bytes of fixed records. Omega has no structs that could be passed |
| 53 | across the ABI, no heap, and no globals in function mode. So the port never builds the struct. |
| 54 | Instead, a graph is two arrays: |
| 55 | |
| 56 | - **W** (`uchar`, 2,097,152 bytes) is the `OMGG` wire image the graph was decoded from. |
| 57 | - **X** (`ulong`, 20,544) is an index. `X[0]` is the object count. Object `j` has 80 cells at |
| 58 | `64 + 80 j`: |
| 59 | - cbuf offset, clen, kind, attribute count, relation count, offset of the first relation, |
| 60 | constraint count, payload length, payload offset, `has_id`; |
| 61 | - the id as 4 big-endian words; |
| 62 | - 32 attribute key offsets (each key length is the byte just before its key); |
| 63 | - 32 constraint record offsets. |
| 64 | |
| 65 | Every field of the C object is a function of W and X. Relations are fixed-size records, so one |
| 66 | offset locates all of them. `cparse` uses the C builders' record layout to write the objects it |
| 67 | builds into W as an `OMGG` image. Then it indexes W with the same decoder. A builder object has no |
| 68 | attributes, so its canonical encoding is exactly its record, and re-decoding it gives the same |
| 69 | object. |
| 70 | |
| 71 | `sbyte` returns any byte of the C struct image of object `j`, using the aarch64 LP64 layout measured |
| 72 | with `offsetof`: |
| 73 | - `OmegaObject` is 25,972 bytes, with the payload at offset 24,948; |
| 74 | - multi-byte fields are host little-endian; |
| 75 | - padding is zero; |
| 76 | - objects at or past the count are zero, because the C memsets the graph. |
| 77 | |
| 78 | The C reads past an object's 1024-byte payload in two places, and the port reproduces both: |
| 79 | - `format_text` folds `bytes[byte_len]` of a `KIND_VALUE`, with a `byte_len` of up to 65,535. |
| 80 | - The cycle walk reads `operands[operand_count]` through `ApplyPayload` for any `KIND_OPERATION` |
| 81 | whose payload is at least 161 bytes, with an `operand_count` of up to 255. |
| 82 | |
| 83 | C behaviours the port keeps: |
| 84 | - The decoded attribute key is a C string, so the canonical form and the sort use the key up to its |
| 85 | first NUL, not `klen`. |
| 86 | - An object whose canonical form exceeds 4,096 bytes gets no id (`has_id` 0, id zero). It is |
| 87 | invisible to id lookups, and `serialize` fails on it. |
| 88 | - `format_text` keeps the stale `w` when an operation's payload is shorter than an `ApplyPayload`: |
| 89 | it advances `pos` without writing. The C's `w >= max_len - pos` check runs at equality. |
| 90 | - `parse_text` runs `strstr`/`strchr` over the whole rest of the string, not just the current line. |
| 91 | It re-assigns pointers on every matching line. When the graph is full, builders return NULL, so |
| 92 | `val_a` can stay NULL and `op_add` can become NULL. |
| 93 | - The product compiles with `-D_GNU_SOURCE` against glibc 2.39, so `strtoul` binds to |
| 94 | `__isoc23_strtoul` (checked with `nm`) and accepts `0b`/`0B` binary. Overflow gives `ULONG_MAX` |
| 95 | whatever the sign. |
| 96 | - glibc aarch64 `memcmp` returns -1/0/1 (measured), and so does `cidcmp`. |
| 97 | |
| 98 | The cycle walk does not recurse, because function mode forbids call cycles. It keeps an explicit |
| 99 | stack of frames (object, phase, step, next j) and visits objects in the C's order. That order |
| 100 | includes duplicate objects with equal ids. Cycles are reachable only through the operand read past |
| 101 | the payload, when it lands on an id stored in the next object's attribute value. The corpus builds |
| 102 | such cycles on purpose. |
| 103 | |
| 104 | ## Oracle |
| 105 | |
| 106 | - **Source.** omega-mv `acb3f15c`, sha256: |
| 107 | - `40dbd8d3…e07a` omega_codec.c |
| 108 | - `a15848c3…65c0` omega_canonical.c |
| 109 | - `5d2472ec…c3ec` omega_validate.c |
| 110 | - `e00b7830…4752` omega_core.c |
| 111 | - `ea8ae1c8…ed15` sha256.c |
| 112 | - `61d35344…392c` omega_om_units.inc |
| 113 | - `c058a742…395c` omega_types.h |
| 114 | - **Build.** gcc 13.3.0 with the Makefile CFLAGS `-std=gnu11 -Wall -Wextra -Werror -D_GNU_SOURCE |
| 115 | -O2 -Isrc`. The `.text` of each of the four codec objects is byte-identical to a `make` build of |
| 116 | the same sources (`omega-blake3-abi/build`). |
| 117 | - **Driver.** It lives outside the tree, in `/tmp/turx/CodecPort-oracle/`: `oracle.c` sha256 |
| 118 | `1b0eed89…fa26` and `oracle.sh` `eb3d5918…417c`. One command rebuilds the C from a checkout's |
| 119 | `src/`, regenerates the corpus with this `corpus.py`, writes the streams and compares their |
| 120 | sha256 with `parity/expected.txt`: |
| 121 | `sh /tmp/turx/CodecPort-oracle/oracle.sh CHECKOUT`. |
| 122 | - **Memory past the graph.** The driver puts the graph in a zeroed allocation with 128 KiB of |
| 123 | slack after `OmegaGraph`, so the C's reads past the last object see zeros. |
| 124 | - **sha256.** The ids come from the product's `sha256.c`, which is itself the `om/sha256` unit. The |
| 125 | port uses its own SHA-256 (the FIPS code of `tools/zero-legacy/sha256.om`). The driver also |
| 126 | checks every serialized graph's sha256 against Python's hashlib. |
| 127 | |
| 128 | ## Parity (pinned omc `26540e6d…8991`) |
| 129 | |
| 130 | `codec.om` sha256 is `c8e93310…a2e8`. It compiles to a 95,048-byte program, the same bytes as under |
| 131 | the previous pins `3ad9f10f…3194`, `b4f45121…9b55`, `be8c5130…448a`, `24caacf4…abb7`, `90c8884c…3983`, `469532a5…79bc` and `62775d2a…712b`. The corpus is |
| 132 | generated on every run: |
| 133 | - `decode.pack`: 4,564 wire inputs, 13.6 MB; |
| 134 | - `text.pack`: 1,612 text inputs; |
| 135 | - `hexid.pack`: 6,000 records. |
| 136 | |
| 137 | | stream | what it records | lines | result | |
| 138 | |---|---|---|---| |
| 139 | | decode | rc, count, `has_id` and id of every object | 4,564 (2,908 decoded) | equal to C | |
| 140 | | validate | rc and message (23 distinct message forms seen, including 8 cycles) | 2,908 | equal | |
| 141 | | serialize | rc, length, bytes, sha256 at max_len 1,049,606; rc and length at 8,192 | 2,908 | equal | |
| 142 | | format | rc and text at cap 4,096 and at cap 200 | 2,908 | equal | |
| 143 | | parse | parse_text rc, ids, then the validate, serialize and format lines of the graph it built | 6,448 | equal | |
| 144 | | hexid | parse_hex rc and output bytes, hex of an id, compare of two ids and of an id with itself | 6,000 | equal | |
| 145 | | header | object_version and check_header for all 65,536 (version, kind) pairs | 65,536 | equal | |
| 146 | |
| 147 | The decode inputs are: |
| 148 | - the 3 product vectors; |
| 149 | - 57 edge cases; |
| 150 | - the 12 cycle graphs, built through the operand read past the payload; |
| 151 | - 21 overread graphs, whose `format_text` reads past the payload, one up to the end of a 256-object |
| 152 | graph; |
| 153 | - 353 targeted graphs: every validate branch at its boundaries, a walk 200 deep, and a walk over 120 |
| 154 | duplicates; |
| 155 | - 18 boundary graphs: serialize totals 8,186 to 8,199, and format where the stale length equals the |
| 156 | room left; |
| 157 | - 300 clean graphs and 700 structured random graphs; |
| 158 | - 2,000 single-byte mutants and 300 truncations of valid graphs; |
| 159 | - 500 random byte strings, and 300 random strings behind an `OMGG` header. |
| 160 | |
| 161 | The text inputs cover: |
| 162 | - number forms: decimal, hex, octal, binary, sign, overflow, and `0x`/`0b` with no digits; |
| 163 | - C-locale whitespace, NUL inside the text, CRLF; |
| 164 | - graphs of 256 objects and more; |
| 165 | - random bytes. |
| 166 | |
| 167 | **Mutants.** `run.sh --mutants` makes 21 single edits. Each one must change the stream it names, |
| 168 | and it must run to completion: a trap does not count as killed. All 21 are killed: |
| 169 | - attribute sort, key past its NUL, relation order, constraint length tie; |
| 170 | - klen 64, effect padding, id limit, unsigned width, value byte rounding; |
| 171 | - operand window offset, struct value offset, serialize length check, stale line length, value last |
| 172 | 8 bytes; |
| 173 | - binary prefix, strtoul overflow, line end, negative hex pair, memcmp sign, v1 effect code, a |
| 174 | SHA-256 constant. |
| 175 | |
| 176 | Reproduce from a fresh clone; the default omc is the checkout's `seed/out/omc`, which is the pinned one: |
| 177 | |
| 178 | sh om/codec/parity/run.sh --mutants |
| 179 | |
| 180 | ## ABI note (bindable functions) |
| 181 | |
| 182 | The pinned omc refuses `const` in function mode (error 9), so array lengths are written as numbers. |
| 183 | `parity/unit.py` cuts each exported function with its helpers. Every one of the 12 units compiles |
| 184 | in function mode. The pinned `native.om` (omega-bridge `012860db`, sha256 `2c2c53f6…3940`) accepts |
| 185 | them all, and its generated C compiles with `-Wall -Wextra -Werror`. Signatures: |
| 186 | |
| 187 | - `omega_cdecode(r, w[2097152 B], n, x[20544])` |
| 188 | - `omega_cserial(r, w, x, mx, o[1049606 B])` |
| 189 | - `omega_cvalid(r, w, x, e[256 B])` |
| 190 | - `omega_cvobject(r, w, x, j, e[256 B])` |
| 191 | - `omega_cformat(r, w, x, cap, t[65536 B])` |
| 192 | - `omega_cparse(r, s[262144 B], n, w, x)` |
| 193 | - `omega_ccanon(r, w, x, j, cb[4096 B])` |
| 194 | - `omega_chexparse(r, h[65 B], o[32 B])` |
| 195 | - `omega_chexid(r, a[32 B], o[65 B])` |
| 196 | - `omega_cidcmp(r, a[32 B], b[32 B])` |
| 197 | - `omega_cobjver(r, k)` |
| 198 | - `omega_cchkhdr(r, v, k)` |
| 199 | |
| 200 | There is also a check run outside the tree. A `/tmp` driver (`abi.c` sha256 `e2f31ab1…6a6f`) ran |
| 201 | all 7 streams through the generated bindings, and the output was byte-identical to the C oracle's. |
| 202 | |
| 203 | ## Limits |
| 204 | |
| 205 | 1. **WCAP.** `cdecode` returns `-2` instead of continuing when it would read a wire byte at or past |
| 206 | 2,097,152. The C has no such limit. `run.sh` shows a 2,097,174-byte graph that the C decodes |
| 207 | (rc 0) and the port refuses (rc -2). |
| 208 | Every graph that `serialize` can write is at most 1,049,606 bytes, and the product's callers use |
| 209 | 4,096 to 8,192-byte buffers. |
| 210 | 2. **SCAP.** `cparse` returns `-2` when the input is longer than 262,144 bytes. The C has no limit. |
| 211 | The string ends at its first NUL or at `n`. |
| 212 | 3. **TCAP.** `cformat` returns `-1` for `cap` > 65,536, which the C would format. The product's |
| 213 | caller uses 4,096. |
| 214 | 4. **The error buffer is 256 bytes, and there is no length parameter.** The C callers pass 256 |
| 215 | (`omega_verify.c`, `omegatool.c`, `visor_verify.c`) or 160 (`visor_effect_request.c`). The |
| 216 | longest message is 99 bytes (a dangling-relation message with its 64 hex digits), so the C's |
| 217 | snprintf never truncates for these callers. A caller that passes fewer than 100 bytes would see |
| 218 | truncation in the C and not here. An adapter for a 160-byte caller copies the message out. |
| 219 | 5. **Reads past the object.** Inside the graph, the port reproduces the C struct image exactly. |
| 220 | Past the end of `OmegaGraph`, the C reads memory it does not own (undefined behaviour), and the |
| 221 | port returns zero there. That matches the oracle only because the oracle's graph sits in zeroed |
| 222 | slack. Another caller's memory past the graph could differ. |
| 223 | 6. **`omega_validate_object`.** The C also takes an object that is not in the graph. `cvobject` |
| 224 | validates X's object `j`. |
| 225 | 7. **Arguments.** `j` must be below 256 (`ccanon`, `cvobject`), or the call traps 3. W and X must |
| 226 | come from `cdecode` or `cparse`: an arbitrary index can trap (out of range). |
| 227 | 8. **`ccanon` on failure.** When the encoding exceeds 4,096 bytes, `ccanon` writes nothing. The C |
| 228 | writes a prefix before it fails. No caller reads the buffer after a failure. `cserial` writes the |
| 229 | same prefix as the C. |
| 230 | 9. **Unreachable C paths are not ported:** |
| 231 | - NULL-argument checks and their messages (`Null graph pointer`, `Null object pointer`); |
| 232 | - `Graph object count %u exceeds maximum` (X never holds more than 256); |
| 233 | - the C's `omega_graph_find_object*` and the other builders in `omega_core.c`, which are out of |
| 234 | scope. |
| 235 | |
| 236 | The effect-refusal message in validate is ported but not covered: a decoded graph's effects have |
| 237 | already passed the same check, and parse_text builds no effects. |
| 238 | 10. **Platform.** Parity holds for the product's platform: aarch64 LP64 struct offsets, glibc 2.39 |
| 239 | strtoul/strtol/memcmp, and the C locale. It is not claimed for another libc or ABI. |
| 240 | |
| 241 | ## NOT_RUN |
| 242 | |
| 243 | - No C adapter, no change to `src/`, no unit in `om/units.sh` or `src/omega_om_units.inc`. |
| 244 | - No deletion of the C objects from `libomega_gpu.a` (that goes through omega-bridge #8). |
| 245 | - No `make test`, no gate suite, no GPU. |