From 94c45b3329bc8c6c6fb1ab4fd4ed4c11fff7edb8 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Thu, 1 Oct 2026 05:25:23 -0500 Subject: [PATCH 1/2] OSC-2 item 3: arenas wired to the OSC-0B memory model `arena r bound K { ... }` with `let x: own T in r = ...` allocations. Bump-allocated pool slot per arena, destroyed at block end after the block's unique owners (inner arena first). New diagnostics ARENA_ESCAPE, ARENA_MOVE (slice restriction: every arena move refused), ARENA_CAPACITY; new trap ARENA_FULL = 11 in interpreter and native code. Runtime log gains REGION_OPEN / ARENA_ALLOC / REGION_DESTROY; every native run's log in the test is replayed through the OSC-0B model (executed runs, not a proof for all programs). IR ops AOPEN / AALLOC / ADESTROY, encoding version 3 only when arenas are present; existing programs byte-identical. Arena fuzz, destruction-order test, 14 negatives, receipt ITEM=arenas, design doc section 3. OSC-2 slice; not a general Omega compiler; no self-hosting. Co-Authored-By: Drake Stapleton Co-Authored-By: Claude Fable 5.1 Co-Authored-By: Claude Opus 5.5 --- docs/osc/OSC-2-DESIGN.md | 150 ++++++++ mk/compiler.mk | 7 +- src/compiler/osc_cg.c | 19 + src/compiler/osc_check.c | 170 ++++++++- src/compiler/osc_diag.c | 2 +- src/compiler/osc_diag.h | 4 + src/compiler/osc_interp.c | 3 + src/compiler/osc_ir.c | 70 +++- src/compiler/osc_ir.h | 16 +- src/compiler/osc_lex.c | 4 +- src/compiler/osc_lex.h | 2 + src/compiler/osc_lower.c | 23 +- src/compiler/osc_parse.c | 33 ++ src/compiler/osc_parse.h | 9 +- src/compiler/osc_rt.c | 67 +++- src/compiler/osc_rt.h | 43 ++- tests/compiler/neg/ar_arena_as_value.osc | 9 + tests/compiler/neg/ar_capacity_bound_65.osc | 8 + tests/compiler/neg/ar_capacity_bound_zero.osc | 8 + tests/compiler/neg/ar_capacity_static.osc | 11 + tests/compiler/neg/ar_escape_outer_borrow.osc | 11 + .../compiler/neg/ar_escape_return_borrow.osc | 8 + tests/compiler/neg/ar_move_into.osc | 11 + tests/compiler/neg/ar_move_out.osc | 11 + tests/compiler/neg/ar_move_own_param.osc | 11 + tests/compiler/neg/ar_not_an_arena.osc | 7 + tests/compiler/neg/ar_object_after_block.osc | 8 + tests/compiler/neg/ar_shadowing.osc | 12 + tests/compiler/neg/ar_undefined_arena.osc | 6 + tests/compiler/neg/ar_use_after_block.osc | 9 + tests/compiler/osc2_receipt.sh | 41 ++- tests/compiler/progs/arena_basic.osc | 39 ++ tests/compiler/progs/arena_borrows.osc | 29 ++ tests/compiler/progs/arena_dtor.osc | 22 ++ tests/compiler/progs/arena_full.osc | 32 ++ tests/compiler/progs/arena_nested.osc | 31 ++ tests/compiler/progs/arena_structs.osc | 19 + tests/compiler/test_osc_backend.c | 24 ++ tests/compiler/test_osc_compiler.c | 335 +++++++++++++++--- 39 files changed, 1245 insertions(+), 79 deletions(-) create mode 100644 tests/compiler/neg/ar_arena_as_value.osc create mode 100644 tests/compiler/neg/ar_capacity_bound_65.osc create mode 100644 tests/compiler/neg/ar_capacity_bound_zero.osc create mode 100644 tests/compiler/neg/ar_capacity_static.osc create mode 100644 tests/compiler/neg/ar_escape_outer_borrow.osc create mode 100644 tests/compiler/neg/ar_escape_return_borrow.osc create mode 100644 tests/compiler/neg/ar_move_into.osc create mode 100644 tests/compiler/neg/ar_move_out.osc create mode 100644 tests/compiler/neg/ar_move_own_param.osc create mode 100644 tests/compiler/neg/ar_not_an_arena.osc create mode 100644 tests/compiler/neg/ar_object_after_block.osc create mode 100644 tests/compiler/neg/ar_shadowing.osc create mode 100644 tests/compiler/neg/ar_undefined_arena.osc create mode 100644 tests/compiler/neg/ar_use_after_block.osc create mode 100644 tests/compiler/progs/arena_basic.osc create mode 100644 tests/compiler/progs/arena_borrows.osc create mode 100644 tests/compiler/progs/arena_dtor.osc create mode 100644 tests/compiler/progs/arena_full.osc create mode 100644 tests/compiler/progs/arena_nested.osc create mode 100644 tests/compiler/progs/arena_structs.osc diff --git a/docs/osc/OSC-2-DESIGN.md b/docs/osc/OSC-2-DESIGN.md index 584dd1531..4436ee1c9 100644 --- a/docs/osc/OSC-2-DESIGN.md +++ b/docs/osc/OSC-2-DESIGN.md @@ -266,3 +266,153 @@ Existing kinds used by structs: moves, and `&` / `&mut` / own calls with `requires` on fields. Every unit compiles, and native and interpreter outcomes are identical (`struct fuzz: ... mismatches=0`) in the plain and ASan/UBSan runs. The fuzz must hit TRAP BOUNDS and TRAP REQUIRES. + +## 3. Arenas + +### 3.1 Subset statement + +OSC-2 item 3 wires the OSC-0B region rules (model events REGION_OPEN, ALLOC with a region, +REGION_DESTROY) into the compiler. An arena is a bounded block of cells. Objects allocated +in it are never released one by one: the whole arena is destroyed when its block ends. +OSC-2 slice; not a general Omega compiler; no self-hosting. + +### 3.2 Grammar + +``` +stmt := ... | "arena" NAME "bound" INT block (INT = K, 1..64 cells) +let := ... | "let" NAME ":" "own" "[" scalar ";" INT "]" "in" NAME "=" "alloc" "(" expr ")" ";" + | "let" NAME ":" "own" NAME "in" NAME "=" NAME "{" init ("," init)* "}" ";" +``` + +- `arena` is now a keyword. `in` (already a keyword for `for`) also names the arena of an + allocation. +- The arena name is visible only inside its block, so allocating from an arena outside its + block is impossible: the name is undefined there (UNDEFINED_NAME). +- An arena name is not a value. Any other use of it is TYPE_MISMATCH (`arena used as a + value`). It cannot be passed to a function or returned. + +### 3.3 Semantics + +- **Capacity.** K counts 8-byte cells. `[T; N]` uses N cells and a struct uses its cell + count (section 2.3). An allocation is *definite* when it is in the arena's own + straight-line code: same loop depth and same branch depth as the `arena` statement + (nested arena blocks count as straight-line code). The checker adds up definite + allocations; going over K is refused (ARENA_CAPACITY). Every other allocation (inside a + `while` / `for` body or an `if` / `else` branch) is checked at run time: past K the run + traps with TRAP ARENA_FULL (11) in both the interpreter and native code. +- **Runtime.** An arena takes one slot of the 64-slot pool (the lowest free one, like any + allocation; no free slot is TRAP OOM) and bump-allocates its objects inside that slot's + cells. Cells are filled with the initial value at allocation and zeroed at destroy. +- **Destruction order (deterministic).** At the end of an arena block (normal end, or a + `return` from inside it): + 1. the unique owners declared inside the block are released, in reverse declaration + order (OSC-1 rule); + 2. then the arena is destroyed: one REGION_DESTROY frees all its objects at once. + Nested arenas: the inner block ends first, so the inner arena is destroyed before the + outer one. A `return` destroys every enclosing arena, innermost first, interleaved with + the releases of each scope. `tests/compiler/progs/arena_dtor.osc` asserts the exact event + sequence (alloc 1, open 2, arena alloc 3, open 4, arena alloc 5, alloc 6, release 6, + destroy 4, alloc 7, release 7, destroy 2, release 1) in both engines. +- **Borrows.** `&x` and `&mut x` of an arena object follow the OSC-1 lexical rules + (MUTABLE_ALIAS, READ_ONLY_BORROW, `&` / `&mut` parameters). A borrow of an arena object + may not outlive the arena: assigning it to a borrow binding declared outside the arena + block, or returning it, is ARENA_ESCAPE. The model rejects the same situation (a + REGION_DESTROY while a borrow of one of its objects is live) as `arena-escape`. +- **Moves (slice restriction).** Every move that involves an arena object is refused with + ARENA_MOVE: `let y: own .. = x;` with `x` in an arena (`move out of arena`), passing `x` + to an `own` parameter (`move into own parameter`), and `let x: own .. in r = y;` + (`move into arena`). The OSC-0B model allows a move inside a region (the new owner + inherits the region); this slice is stricter than the model and refuses all of them. +- **Model trace.** The checker trace gains REGION_OPEN (at `arena`), ALLOC with the region + id (for `in r`) and REGION_DESTROY (at block end), with per-function region ids 1..16. + Arena objects get no RELEASE event. Golden and fuzz traces replay through the model with + ACCEPT; an ARENA_ESCAPE refusal ends its trace in the refused REGION_DESTROY, which the + model rejects with `arena-escape` (model-agreement test). +- **Runtime event log.** `osc_rt` logs REGION_OPEN (slot, K, region serial), ARENA_ALLOC + (slot, length, object serial) and REGION_DESTROY next to ALLOC / RELEASE. Interpreter and + native code produce identical logs (checked on every differential run). Releasing an arena + slot with RELEASE is a runtime invariant violation (TRAP RUNTIME, never seen for checked + code). +- **IR.** Three instructions are added. `AOPEN dst, K` puts a u64 arena handle in `dst`. + `AALLOC dst, init, h` allocates `dst` (an own reference or struct) from handle `h`. + `ADESTROY h` destroys the arena. The validator enforces a handle discipline: a handle + register is defined once, by AOPEN only, is never a parameter, and is used only as the + handle operand of AALLOC / ADESTROY. Native code calls the runtime through three new + OscRt entry points at offsets 24, 32 and 40 (`arena_open`, `arena_alloc`, + `arena_destroy`). +- **Encoding.** A unit with no arena instruction encodes exactly as before (version byte 1 + without structs, 2 with structs), so every existing program keeps byte-identical IR and + machine code (checked: all 31 earlier golden programs and all 82 earlier negatives give + the same oscc output as origin/main). A unit with arena instructions uses version byte 3, + always writes the struct table, and encodes the new instructions. + +### 3.4 Diagnostics + +New kinds (appended; existing kinds unchanged): + +| Kind | Transition | Cause | +|---|---|---| +| ARENA_ESCAPE | `outer borrow assigned a borrow of an arena object` / `borrow of an arena object returned` / `borrow live at arena end` | a borrow of an arena object outlives the arena | +| ARENA_MOVE | `move out of arena` / `move into own parameter` / `move into arena` | any move of an arena object (slice restriction) | +| ARENA_CAPACITY | `arena bound outside 1..64` / `arena capacity exceeded` | K not in 1..64; definite allocations over K | + +`borrow live at arena end` is a defence in depth: with lexical borrows the two other +ARENA_ESCAPE checks catch every escape first, so no test reaches it. + +Existing kinds used by arenas: + +- **UNDEFINED_NAME:** `allocate in undefined arena` (unknown name, or an arena used after + its block). +- **TYPE_MISMATCH:** `in a non-arena` (`in x` where `x` is not an arena), `arena used as a + value`. +- **REDEFINED_NAME:** an arena name that shadows a visible name (no shadowing, OSC-1 rule). + +New trap: TRAP ARENA_FULL = 11 (`OSC_TRAP_ARENA_FULL`, now `OSC_TRAP_MAX`). + +### 3.5 Limits + +- K is 1..64 cells (one pool slot). An arena uses one of the 64 pool slots while open. +- The static model trace holds 16 arenas per function (the model's region capacity). A function + with more still compiles, but its trace is marked overflow and is not replayed (same rule + as the 64-object and 64-borrow limits). +- Objects are never moved into, out of, or between arenas. +- Arena objects cannot be released early; there is no per-object free. +- Arena names are not values: no arena parameters, returns, or arenas outliving their block. + +### 3.6 Evidence + +- Golden programs: + - `arena_basic.osc`: basic allocation, `return` from inside an arena, unique owners mixed + with arena objects. + - `arena_nested.osc`: nested arenas, allocation into the outer arena from the inner + block, sibling arenas reusing a slot. + - `arena_structs.osc`: struct literals in an arena. + - `arena_borrows.osc`: `&` / `&mut` borrows and parameters on arena objects. + - `arena_full.osc`: allocations in a loop and in a branch, TRAP ARENA_FULL. + - `arena_dtor.osc`: destruction order (event sequence above). +- Negatives in `tests/compiler/neg/ar_*.osc` (14), each asserting kind, object, line, and + transition: escape through an outer borrow, escape by return, move out, move into, move + into an own parameter, static capacity, bound 0, bound 65, undefined arena, `in` a + non-arena, arena shadowing, allocation after the block, arena object used after the + block, arena used as a value. The two ARENA_ESCAPE negatives also pass the + model-agreement check (`ARENA_ESCAPE` maps to the model's `arena-escape`). +- `test_osc_backend` adds the arena validator cases (bad bound, handle type, double + definition, allocation or destroy from a non-handle, handle used as a value or redefined). +- `test_osc_compiler` adds: + - the arena destruction-order event check (`arena destruction order: ...`); + - **arena fuzz:** generated units with nested arenas of random bound, array and struct + allocations into any visible arena, unique owners, borrows passed to `&` / `&mut` + parameters, and allocations in loops and branches. Every unit compiles, native and + interpreter outcomes are identical (`arena fuzz: ... mismatches=0`) in the plain and + ASan/UBSan runs, and the fuzz must hit TRAP ARENA_FULL; + - **runtime model replay:** the runtime event log of every native run in the test + (golden fuzz, expect-run lines, contract, struct and arena fuzz, destruction-order + tests) is replayed through the OSC-0B model. A run that returns replays fully; a run + that traps replays the prefix logged before the trap. Object and region ids are + windowed when a run exceeds 64 objects or 16 regions (a new window re-opens the open + regions and re-allocates the live objects). The line + `runtime model replay: runs=.. events=.. accepted=.. rejected=0` must report + `rejected=0`. **This checks the runs that were executed. It is not a proof that every + program's runtime behaviour satisfies the model.** +- `make osc2-receipt ITEM=arenas` records the arena fuzz lines, the destruction order, the + runtime model replay lines (plain and ASan) and the ARENA_FULL run count. diff --git a/mk/compiler.mk b/mk/compiler.mk index 71abe2b9d..a93f437d7 100644 --- a/mk/compiler.mk +++ b/mk/compiler.mk @@ -72,7 +72,12 @@ test-compiler: $(OSC_DIR)/test_osc_model $(OSC_DIR)/test_osc_model_asan \ @grep "^contract fuzz:" $(OSC_DIR)/compiler.out @grep "^struct fuzz: .* mismatches=0$$" $(OSC_DIR)/compiler.out @grep "^struct fuzz: .* mismatches=0$$" $(OSC_DIR)/compiler_asan.out - @echo "test-compiler: PASS (OSC-1 slice + OSC-2 contracts + structs; OSC-2 slice; not a general Omega compiler; no self-hosting.)" + @grep "^arena fuzz: .* mismatches=0$$" $(OSC_DIR)/compiler.out + @grep "^arena fuzz: .* mismatches=0$$" $(OSC_DIR)/compiler_asan.out + @grep "^arena destruction order: " $(OSC_DIR)/compiler.out + @grep "^runtime model replay: .* rejected=0$$" $(OSC_DIR)/compiler.out + @grep "^runtime model replay: .* rejected=0$$" $(OSC_DIR)/compiler_asan.out + @echo "test-compiler: PASS (OSC-1 slice + OSC-2 contracts + structs + arenas; OSC-2 slice; not a general Omega compiler; no self-hosting.)" osc0b-model-receipt: sh tests/compiler/osc0b_model_receipt.sh diff --git a/src/compiler/osc_cg.c b/src/compiler/osc_cg.c index 0d894b538..c551b4962 100644 --- a/src/compiler/osc_cg.c +++ b/src/compiler/osc_cg.c @@ -366,6 +366,25 @@ static void gen_func(Cg *g, const OscUnit *u, int fi) { mat(g, 2, T[in->a].len); rt_call(g, 8); break; + case OSC_I_AOPEN: /* x0 = rt->arena_open(rt, K) */ + load_rt(g, 0); + mat(g, 1, in->imm); + rt_call(g, 24); + st(g, 0, in->dst); + break; + case OSC_I_AALLOC: /* x0 = rt->arena_alloc(rt, handle, len, init) */ + load_rt(g, 0); + ld(g, 1, in->b); + mat(g, 2, T[in->dst].len); + ld(g, 3, in->a); + rt_call(g, 32); + st(g, 0, in->dst); + break; + case OSC_I_ADESTROY: /* rt->arena_destroy(rt, handle) */ + load_rt(g, 0); + ld(g, 1, in->a); + rt_call(g, 40); + 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 34d68b712..f4406b749 100644 --- a/src/compiler/osc_check.c +++ b/src/compiler/osc_check.c @@ -10,7 +10,7 @@ #include #include -enum { SK_SCALAR = 1, SK_OWNER, SK_BORROW }; +enum { SK_SCALAR = 1, SK_OWNER, SK_BORROW, SK_ARENA /* OSC-2 arenas */ }; typedef struct { char name[64]; @@ -20,6 +20,13 @@ typedef struct { int loop_depth; int obj; /* owner: object index */ int bor; /* borrow binding: current borrow index (-1 none) */ + /* SK_ARENA (OSC-2 section 3): model region id (per function, 1-based), + * bound K in cells, cells taken by definite allocations so far, and the + * conditional depth at the declaration (with loop_depth: an allocation is + * definite when both depths equal the arena's) */ + uint32_t region; + uint16_t cap, used; + int cond_depth; } Sym; typedef struct { @@ -27,6 +34,7 @@ typedef struct { uint16_t n_shared, n_mut; uint32_t move_line; int sym; /* owning symbol or -1 (stand-in / dummy) */ + int arena1; /* OSC-2: arena symbol + 1 for an arena object, 0 for a unique owner */ } Obj; typedef struct { @@ -67,6 +75,8 @@ typedef struct { int term; /* current path has returned */ char sobj[64]; /* statement object (diagnostics) */ 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 */ } C; #define NODE(i) (&c->ast->nodes[(i)]) @@ -86,7 +96,8 @@ static void tr_push(C *c, uint8_t op, const OscModelEvent *ev, uint32_t line, in if (ev) { 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->borrow > OSC_MODEL_MAX_BORROWS || ev->via > OSC_MODEL_MAX_BORROWS || + ev->region > OSC_MODEL_MAX_REGIONS) t->overflow = 1; } if (refused) t->refused = 1; @@ -102,6 +113,16 @@ static void tr_ev(C *c, uint32_t kind, int obj, int obj2, int borrow, int via, u ev.via = via >= 0 ? (uint32_t)via + 1 : 0; tr_push(c, OSC_TR_EVENT, &ev, line, refused); } +/* OSC-2 arenas: REGION_OPEN / REGION_DESTROY of region, ALLOC of obj in region */ +static void tr_region(C *c, uint32_t kind, int obj, uint32_t region, uint32_t line, int refused) +{ + OscModelEvent ev; + memset(&ev, 0, sizeof ev); + ev.kind = kind; + ev.obj = obj >= 0 ? (uint32_t)obj + 1 : 0; + ev.region = region; + 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); } @@ -349,30 +370,59 @@ static int push_scope(C *c, uint32_t line) return 0; } +/* OSC-2 arenas: a borrow of an object of arena symbol as would outlive the + * arena. The refused event is the arena's REGION_DESTROY (the model rejects it + * with arena-escape while that borrow is live). */ +static int arena_escape(C *c, const OscNode *at, int as, const char *who, const char *transition) +{ + Sym *y = &c->sym[as]; + tr_region(c, OSC_EV_REGION_DESTROY, -1, y->region, at->line, 1); + osc_diag_set(c->d, OSC_DIAG_ARENA_ESCAPE, at->line, at->col, y->name, y->line, who, transition, + "a borrow of an object in arena '%s' would outlive the arena (%s)", y->name, transition); + return -1; +} + /* scope-end destruction of the innermost `levels` scopes (without popping). - * Records released owner symbols into the AST release list. */ -static void scope_exit_events(C *c, int levels, uint32_t line, OscNode *rec) + * Records released owner symbols into the AST release list. OSC-2 arenas: + * arena objects are never released one by one; the arena symbol (declared in + * the scope just outside its body) emits REGION_DESTROY, which releases them + * all, and is recorded in the release list (the lowerer emits ADESTROY). Since + * scopes are walked innermost first and symbols newest first, the owners and + * borrows declared inside an arena body end before the arena is destroyed, + * and an inner arena before an outer one. */ +static int scope_exit_events(C *c, int levels, uint32_t line, OscNode *rec) { OscAst *a = c->ast; rec->rel_start = a->nrel; rec->rel_count = 0; for (int lv = 0; lv < levels; lv++) { int d = c->depth - 1 - lv; - for (int i = c->nvis - 1; i >= c->scope_start[d]; i--) { + /* each level visits only its own symbols (inner levels were done) */ + int hi = lv == 0 ? c->nvis : c->scope_start[d + 1]; + for (int i = hi - 1; i >= c->scope_start[d]; i--) { int s = c->vis[i]; Sym *y = &c->sym[s]; if (y->kind == SK_BORROW) { if (y->bor >= 0 && c->bor[y->bor].live) end_bor(c, y->bor, line); } else if (y->kind == SK_OWNER) { Obj *o = &c->obj[y->obj]; - if (!o->moved) { + if (!o->moved && !o->arena1) { tr_ev(c, OSC_EV_RELEASE, y->obj, -1, -1, -1, line, 0); o->moved = 2; /* released (scope gone) */ if (a->nrel < OSC_AST_MAX_REL) { a->rel[a->nrel++] = (int16_t)s; rec->rel_count++; } } + } else if (y->kind == SK_ARENA) { + for (int b = 0; b < c->nbor; b++) + if (c->bor[b].live && c->obj[c->bor[b].obj].arena1 == s + 1) + return arena_escape(c, rec, s, "scope end", "borrow live at arena end"); + tr_region(c, OSC_EV_REGION_DESTROY, -1, y->region, line, 0); + 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++; } } } } + return 0; } static void pop_scope(C *c) @@ -725,6 +775,7 @@ static int contract_name(C *c, const OscNode *n) /* ------------------------------------------------------------ expressions */ static int chk(C *c, int i, OscScalar want); static int chk_call(C *c, int i, OscScalar want, int as_stmt); +static int arena_move_refused(C *c, const OscNode *at, int s, const char *transition); static int want_ok(C *c, const OscNode *n, OscScalar got, OscScalar want) { @@ -744,6 +795,11 @@ static int resolve(C *c, const OscNode *n) 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); + if (s >= 0 && c->sym[s].kind == SK_ARENA) { + osc_diag_set(c->d, OSC_DIAG_TYPE_MISMATCH, n->line, n->col, name, c->sym[s].line, NULL, "arena used as a value", + "'%s' is an arena; it is only named by 'in %s' in an owner declaration", name, name); + return -1; + } return s; } @@ -1212,6 +1268,7 @@ static int chk_call(C *c, int i, OscScalar want, int as_stmt) snprintf(msg, sizeof msg, "'%s' is not an owner of %s", c->sym[s].name, rty(c, pt, tb, sizeof tb)); return tmismatch(c, a, c->sym[s].name, msg); } + if (arena_move_refused(c, a, s, "move into own parameter")) return -1; if (do_move(c, s, -1, a->line, a->col)) return -1; continue; } @@ -1245,6 +1302,59 @@ static int chk_call(C *c, int i, OscScalar want, int as_stmt) } /* ------------------------------------------------------------ statements */ +/* OSC-2 arenas: the arena named by "in NAME" (node an) for an allocation of + * len cells by owner node n; checks the static capacity rule. Returns the + * arena symbol or -1. */ +static int arena_alloc_site(C *c, const OscNode *n, OscNode *an) +{ + char aname[64], oname[64]; + osc_node_name(c->ast, an, aname, sizeof aname); + osc_node_name(c->ast, n, oname, sizeof oname); + int as = lookup(c, aname); + if (as < 0) { + osc_diag_set(c->d, OSC_DIAG_UNDEFINED_NAME, an->line, an->col, aname, 0, oname, "allocate in undefined arena", + "arena '%s' is not defined in an enclosing scope", aname); + return -1; + } + Sym *y = &c->sym[as]; + if (y->kind != SK_ARENA) { + osc_diag_set(c->d, OSC_DIAG_TYPE_MISMATCH, an->line, an->col, aname, y->line, oname, "in a non-arena", + "'%s' is not an arena", aname); + return -1; + } + an->sym = as; + unsigned len = n->ty.len; + if (c->loop_depth == y->loop_depth && c->cond_depth == y->cond_depth) { + /* definite allocation: runs exactly once per arena lifetime */ + if ((unsigned)y->used + len > y->cap) { + osc_diag_set(c->d, OSC_DIAG_ARENA_CAPACITY, n->line, n->col, oname, y->line, y->name, + "arena capacity exceeded", + "'%s' needs %u cell(s) but arena '%s' (bound %u) has %u left", oname, len, y->name, + (unsigned)y->cap, (unsigned)(y->cap - y->used)); + return -1; + } + y->used = (uint16_t)(y->used + len); + } + return as; +} + +/* OSC-2 arenas: refuse moving owner symbol s if it is an arena object (slice + * restriction, stricter than the model, which would let the MOVE inherit the + * region; no trace event is recorded for it). */ +static int arena_move_refused(C *c, const OscNode *at, int s, const char *transition) +{ + Sym *y = &c->sym[s]; + if (y->kind != SK_OWNER || !c->obj[y->obj].arena1) return 0; + const Sym *ar = &c->sym[c->obj[y->obj].arena1 - 1]; + osc_diag_set(c->d, OSC_DIAG_ARENA_MOVE, at->line, at->col, y->name, y->line, ar->name, transition, + "'%s' lives in arena '%s' and cannot be moved (arena objects are never moved in this slice)", + y->name, ar->name); + return -1; +} + +/* arena symbol + 1 of the object borrow b refers to (0 = not an arena object) */ +static int bor_arena1(C *c, int b) { return c->obj[c->bor[b].obj].arena1; } + static int chk_block(C *c, int i, int new_scope); static int chk_stmt(C *c, int i); @@ -1292,6 +1402,7 @@ static int chk_if(C *c, int i) Snap *s0 = snap_take(c); if (!s0) return cap_fail(c, n->line, "checker memory"); int nobj0 = c->nobj; + c->cond_depth++; /* OSC-2 arenas: allocations in a branch are not definite */ int rc = -1; Snap *s1 = NULL; tr_save(c, n->line); @@ -1340,6 +1451,7 @@ static int chk_if(C *c, int i) tr_ev(c, OSC_EV_MOVE, o, -1, -1, -1, c->obj[o].move_line, 0); rc = 0; out: + c->cond_depth--; free(s0); free(s1); return rc; @@ -1431,13 +1543,20 @@ static int chk_stmt(C *c, int i) for (int fi = n->a; fi >= 0; fi = NODE(fi)->next) if (chk(c, NODE(fi)->a, st->fields[NODE(fi)->hi].s) < 0) return -1; } else if (chk(c, n->a, n->ty.elem) < 0) return -1; + int as = -1; + if (n->c >= 0 && (as = arena_alloc_site(c, n, NODE(n->c))) < 0) return -1; int s = declare(c, n, SK_OWNER, &n->ty, 0); if (s < 0) return -1; int o = new_obj(c, s, n->line); if (o < 0) return -1; c->sym[s].obj = o; n->sym = s; - tr_ev(c, OSC_EV_ALLOC, o, -1, -1, -1, n->line, 0); + if (as >= 0) { + c->obj[o].arena1 = as + 1; + tr_region(c, OSC_EV_ALLOC, o, c->sym[as].region, n->line, 0); + } else { + tr_ev(c, OSC_EV_ALLOC, o, -1, -1, -1, n->line, 0); + } return 0; } case ON_LET_MOVE: { @@ -1445,6 +1564,11 @@ static int chk_stmt(C *c, int i) char name[64]; osc_node_name(c->ast, n, name, sizeof name); if (lookup(c, name) >= 0) return declare(c, n, SK_OWNER, &n->ty, 0) < 0 ? -1 : -1; + if (n->c >= 0) { + osc_diag_set(c->d, OSC_DIAG_ARENA_MOVE, n->line, n->col, name, 0, NULL, "move into arena", + "'%s': an owner cannot be moved into an arena (arena objects are created in place)", name); + return -1; + } OscNode *sn = NODE(n->a); int src = resolve(c, sn); if (src < 0) return -1; @@ -1457,6 +1581,7 @@ static int chk_stmt(C *c, int i) } int o = new_obj(c, -1, n->line); if (o < 0) return -1; + if (arena_move_refused(c, sn, src, "move out of arena")) return -1; if (do_move(c, src, o, sn->line, sn->col)) return -1; int s = declare(c, n, SK_OWNER, &n->ty, 0); if (s < 0) return -1; @@ -1526,6 +1651,9 @@ static int chk_stmt(C *c, int i) if (b < 0) return -1; y->bor = b; c->bor[b].sym = s; + int ar1 = bor_arena1(c, b); + if (ar1 && s < ar1 - 1) /* binding declared before the arena: it would outlive the arena */ + return arena_escape(c, v, ar1 - 1, y->name, "outer borrow assigned a borrow of an arena object"); if (src > s) /* declared after the binding: destroyed while the binding is still in scope */ return outlives(c, v, src, b, y->name, "outer borrow assigned a borrow of an inner owner"); return 0; @@ -1570,6 +1698,7 @@ static int chk_stmt(C *c, int i) if (c->sym[s].kind == SK_SCALAR) return tmismatch(c, bn, c->sym[s].name, "only arrays are borrowed"); int b = take_borrow(c, s, bn->mut, bn->line, bn->col); if (b < 0) return -1; + if (bor_arena1(c, b)) return arena_escape(c, bn, bor_arena1(c, b) - 1, "return", "borrow of an arena object returned"); return outlives(c, bn, s, b, "return", "borrow returned from function"); } if (c->fret.s == OSC_T_VOID) { @@ -1583,10 +1712,29 @@ static int chk_stmt(C *c, int i) if (chk(c, n->a, c->fret.s) < 0) return -1; if (chk_return_ensures(c, n)) return -1; } - scope_exit_events(c, c->depth, n->line, n); + if (scope_exit_events(c, c->depth, n->line, n)) return -1; c->term = 1; return 0; } + case ON_ARENA: { /* OSC-2: arena NAME bound K { body } */ + set_sobj_tok(c, n); + if (push_scope(c, n->line)) return -1; + OscType ta = {OSC_T_U64, OSC_REF_NONE, OSC_T_VOID, 0, 0}; + int s = declare(c, n, SK_ARENA, &ta, 0); + if (s < 0) return -1; + Sym *y = &c->sym[s]; + y->region = ++c->nregion; + y->cap = (uint16_t)n->ival; + y->used = 0; + y->cond_depth = c->cond_depth; + n->sym = s; + tr_region(c, OSC_EV_REGION_OPEN, -1, y->region, n->line, 0); + if (chk_block(c, n->b, 1)) return -1; + /* destroy after the body's own scope exit (its owners and borrows end first) */ + if (!c->term && scope_exit_events(c, 1, (uint32_t)NODE(n->b)->ival, n)) return -1; + pop_scope(c); + return 0; + } case ON_CALLSTMT: set_sobj_tok(c, NODE(n->a)); return chk_call(c, n->a, 0, 1) < 0 ? -1 : 0; @@ -1610,7 +1758,7 @@ static int chk_block(C *c, int i, int new_scope) } if (chk_stmt(c, s)) return -1; } - if (!c->term && new_scope) scope_exit_events(c, 1, (uint32_t)n->ival, n); + if (!c->term && new_scope && scope_exit_events(c, 1, (uint32_t)n->ival, n)) return -1; if (new_scope) pop_scope(c); return 0; } @@ -1624,6 +1772,8 @@ static int chk_fn(C *c, int fi) c->fnode = f; c->fret = fn->ty; c->nsym = c->nobj = c->nbor = c->nvis = c->depth = c->loop_depth = c->term = 0; + c->cond_depth = 0; + c->nregion = 0; char fname[64]; osc_node_name(a, fn, fname, sizeof fname); snprintf(c->sobj, sizeof c->sobj, "%s", fname); @@ -1672,7 +1822,7 @@ static int chk_fn(C *c, int fi) "function '%s' returning %s can reach its end without 'return'", fname, tname(fn->ty.s)); return -1; } - scope_exit_events(c, 1, (uint32_t)NODE(fn->b)->ival, fn); + if (scope_exit_events(c, 1, (uint32_t)NODE(fn->b)->ival, fn)) return -1; } pop_scope(c); return 0; diff --git a/src/compiler/osc_diag.c b/src/compiler/osc_diag.c index 0145fd737..4a22c347d 100644 --- a/src/compiler/osc_diag.c +++ b/src/compiler/osc_diag.c @@ -13,7 +13,7 @@ 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"}; + "MISSING_FIELD", "UNDEFINED_TYPE", "RECURSIVE_STRUCT", "ARENA_ESCAPE", "ARENA_MOVE", "ARENA_CAPACITY"}; const char *osc_diag_kind_name(int kind) { diff --git a/src/compiler/osc_diag.h b/src/compiler/osc_diag.h index 8e74e6eb6..93a8b9a82 100644 --- a/src/compiler/osc_diag.h +++ b/src/compiler/osc_diag.h @@ -36,6 +36,10 @@ typedef enum { OSC_DIAG_MISSING_FIELD, /* struct literal leaves a field uninitialised */ OSC_DIAG_UNDEFINED_TYPE, /* struct type name not declared (before use) */ OSC_DIAG_RECURSIVE_STRUCT, /* struct field names the struct itself */ + /* OSC-2 item 3 (arenas, docs/osc/OSC-2-DESIGN.md section 3), appended */ + 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_DIAG__COUNT } OscDiagKind; diff --git a/src/compiler/osc_interp.c b/src/compiler/osc_interp.c index 7529f941e..199ee8071 100644 --- a/src/compiler/osc_interp.c +++ b/src/compiler/osc_interp.c @@ -154,6 +154,9 @@ static uint64_t run_func(Ctx *c, int fi, const uint64_t *args) { case OSC_I_CAST: v[in->dst] = do_cast(c, T[in->dst].s, T[in->a].s, v[in->a]); break; case OSC_I_ALLOC: v[in->dst] = osc_rt_alloc(c->rt, T[in->dst].len, v[in->a]); break; case OSC_I_RELEASE: osc_rt_release(c->rt, v[in->a], T[in->a].len); break; + 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_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 13a4d8980..422333172 100644 --- a/src/compiler/osc_ir.c +++ b/src/compiler/osc_ir.c @@ -89,6 +89,8 @@ static int insn_uses(const OscInsn *in, int16_t *u) { switch (in->op) { case OSC_I_MOV: case OSC_I_UN: case OSC_I_CAST: case OSC_I_ALLOC: case OSC_I_RELEASE: 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_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; @@ -103,6 +105,7 @@ static int insn_def(const OscInsn *in) { switch (in->op) { 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_CALL: return in->dst; /* may be -1 */ default: return -1; } @@ -176,6 +179,23 @@ static int validate_insn(const OscUnit *u, int fi, const OscFunc *f, uint32_t ii NEED(VR(in->a), "RELEASE bad vreg", fi, ii); NEED(type_is_ref(&T[in->a]) && T[in->a].ref == OSC_REF_OWN, "RELEASE of a non-owner", fi, ii); break; + case OSC_I_AOPEN: + NEED(VR(in->dst), "AOPEN bad vreg", fi, ii); + NEED(type_is_scalar(&T[in->dst]) && T[in->dst].s == OSC_T_U64, "AOPEN handle not u64", fi, ii); + NEED(in->imm >= 1 && in->imm <= OSC_MAX_ARRAY_LEN, "AOPEN capacity out of range", fi, ii); + break; + case OSC_I_AALLOC: + NEED(VR(in->dst) && VR(in->a) && VR(in->b), "AALLOC bad vreg", fi, ii); + NEED(type_is_ref(&T[in->dst]) && T[in->dst].ref == OSC_REF_OWN, "AALLOC dst not an owner", fi, ii); + if (T[in->dst].sid) + NEED(type_is_scalar(&T[in->a]) && T[in->a].s == OSC_T_U64, "AALLOC struct init not u64", fi, ii); + else + NEED(type_is_scalar(&T[in->a]) && T[in->a].s == T[in->dst].elem, "AALLOC init type != elem type", fi, ii); + NEED(type_is_scalar(&T[in->b]) && T[in->b].s == OSC_T_U64, "AALLOC handle not u64", fi, ii); + break; + 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_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); @@ -281,6 +301,34 @@ static int validate_func(const OscUnit *u, int fi, char *err, size_t n) { for (uint32_t i = 0; i < f->ninsns; i++) if (validate_insn(u, fi, f, i, err, n)) return -1; + /* OSC-2 arenas: an arena handle vreg is defined by exactly one AOPEN (no + * other definition, not a parameter) and is used only as the handle of + * AALLOC / ADESTROY, so a handle (an address) never reaches a value. */ + uint8_t hk[OSC_MAX_VREGS]; + memset(hk, 0, sizeof hk); + for (uint32_t i = 0; i < f->ninsns; i++) { + const OscInsn *x = &f->insns[i]; + if (x->op != OSC_I_AOPEN) continue; + if (hk[x->dst] || x->dst < f->nparams) return vfail(err, n, "func %d insn %u: AOPEN handle vreg reused", fi, i); + hk[x->dst] = 1; + } + 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_AOPEN && d >= 0 && hk[d]) + return vfail(err, n, "func %d insn %u: arena handle vreg redefined", fi, i); + if (x->op == OSC_I_AALLOC) { + if (!hk[x->b] || hk[x->a]) return vfail(err, n, "func %d insn %u: AALLOC handle misuse", fi, i); + } else if (x->op == OSC_I_ADESTROY) { + if (!hk[x->a]) return vfail(err, n, "func %d insn %u: ADESTROY of a non-handle", fi, i); + } else { + int16_t us[OSC_MAX_PARAMS + 3]; + int k = insn_uses(x, us); + for (int j = 0; j < k; j++) + if (hk[us[j]]) return vfail(err, n, "func %d insn %u: arena handle 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); @@ -404,13 +452,28 @@ static void wtext(W *w, const char *s, size_t cap) { wbytes(w, s, k); } +/* 1 if any function uses an arena instruction (OSC-2 section 3) */ +static bool unit_has_arena(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_AOPEN || op == OSC_I_AALLOC || op == OSC_I_ADESTROY) 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). A unit without structs encodes exactly as version 1. */ + * after the magic) / 3 (OSC-2 arenas: AOPEN/AALLOC/ADESTROY present; the + * struct count byte and table always follow the magic, even when 0). + * A unit without structs or arenas encodes exactly as version 1; one with + * structs but no arenas exactly as version 2. */ uint8_t magic[8] = {'O', 'S', 'C', '1', 'I', 'R', 0, 1}; + bool arena = unit_has_arena(u); if (u->nstructs) magic[7] = 2; + if (arena) magic[7] = 3; wbytes(w, magic, sizeof magic); - if (u->nstructs) { + if (u->nstructs || arena) { w8(w, u->nstructs); for (int k = 0; k < u->nstructs; k++) { const OscStruct *s = &u->structs[k]; @@ -447,6 +510,9 @@ static void encode_unit(const OscUnit *u, W *w) { case OSC_I_BIN: case OSC_I_CMP: w8(w, in->sub); wvr(w, in->dst); wvr(w, in->a); wvr(w, in->b); break; case OSC_I_UN: w8(w, in->sub); wvr(w, in->dst); wvr(w, in->a); break; case OSC_I_RELEASE: wvr(w, in->a); break; + 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_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 4bb895286..50e6e86a6 100644 --- a/src/compiler/osc_ir.h +++ b/src/compiler/osc_ir.h @@ -91,8 +91,16 @@ typedef enum { /* appended for OSC-2 structs (docs/osc/OSC-2-DESIGN.md section 2) */ OSC_I_FLOAD, /* dst = field imm of struct ref a; for an array field, element */ /* b (any integer type, else -1); b out of range: TRAP_BOUNDS */ - OSC_I_FSTORE /* field imm of struct ref a (element b, as FLOAD) = c ; */ + OSC_I_FSTORE, /* field imm of struct ref a (element b, as FLOAD) = c ; */ /* a is REF OWN or REF MUT */ + /* appended for OSC-2 arenas (docs/osc/OSC-2-DESIGN.md section 3) */ + OSC_I_AOPEN, /* dst(u64 handle) = open an arena of imm cells (1..64); */ + /* no free pool slot -> TRAP_OOM */ + 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. */ + /* Arena objects are never RELEASEd individually. */ } OscOp; typedef enum { @@ -131,11 +139,13 @@ typedef enum { /* Appended for OSC-2 contracts (docs/osc/OSC-2-DESIGN.md section 1). * Existing codes are never renumbered. */ OSC_TRAP_REQUIRES = 9, /* a `requires` clause evaluated to false at entry */ - OSC_TRAP_ENSURES = 10 /* an `ensures` clause evaluated to false at return */ + 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 */ } OscTrap; /* Highest trap code; arrays indexed by trap code have OSC_TRAP_MAX + 1 entries. */ -#define OSC_TRAP_MAX OSC_TRAP_ENSURES +#define OSC_TRAP_MAX OSC_TRAP_ARENA_FULL typedef struct { uint8_t op; /* OscOp */ diff --git a/src/compiler/osc_lex.c b/src/compiler/osc_lex.c index e515e7c73..b6600f69d 100644 --- a/src/compiler/osc_lex.c +++ b/src/compiler/osc_lex.c @@ -12,7 +12,7 @@ static const struct { const char *w; int k; } keywords[] = { {"in", OT_IN}, {"return", OT_RETURN}, {"requires", OT_REQUIRES}, {"ensures", OT_ENSURES}, {"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}, + {"i64", OT_I64}, {"bool", OT_BOOL}, {"struct", OT_STRUCT}, {"arena", OT_ARENA}, }; /* Reserved: destruction words (destruction has no surface syntax, III.8) and @@ -30,7 +30,7 @@ static const char *const kind_names[OT__COUNT] = { "ensures", "as", "true", "false", "u8", "u16", "u32", "u64", "i8", "i16", "i32", "i64", "bool", "(", ")", "{", "}", "[", "]", ",", ";", ":", "->", "..", "=", "+", "-", "*", "/", "%", "&", "|", "^", "~", "!", "&&", "||", "<<", ">>", "==", "!=", "<", "<=", ">", ">=", - "struct", ".", + "struct", ".", "arena", }; const char *osc_tok_kind_name(int kind) diff --git a/src/compiler/osc_lex.h b/src/compiler/osc_lex.h index 263458544..3cdee3fc9 100644 --- a/src/compiler/osc_lex.h +++ b/src/compiler/osc_lex.h @@ -27,6 +27,8 @@ typedef enum { OT_ANDAND, OT_OROR, OT_SHL, OT_SHR, OT_EQ, OT_NE, OT_LT, OT_LE, OT_GT, OT_GE, /* OSC-2 item 2 (structs), appended */ OT_STRUCT, OT_DOT, + /* OSC-2 item 3 (arenas), appended */ + OT_ARENA, OT__COUNT } OscTokKind; diff --git a/src/compiler/osc_lower.c b/src/compiler/osc_lower.c index 1ae17e180..5b2ac2944 100644 --- a/src/compiler/osc_lower.c +++ b/src/compiler/osc_lower.c @@ -100,7 +100,9 @@ static void releases(L *l, const OscNode *n) { for (uint32_t k = 0; k < n->rel_count; k++) { int s = l->ast->rel[n->rel_start + k]; - OscInsn *x = emit(l, OSC_I_RELEASE, n->line); + /* 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); x->a = l->vreg[s]; } } @@ -280,6 +282,8 @@ static void stmt(L *l, int i) break; } case ON_LET_ALLOC: { + /* OSC-2 arenas: "in NAME" allocates from that arena's handle */ + int ah = n->c >= 0 ? l->vreg[NODE(n->c)->sym] : -1; if (n->ty.sid) { /* struct literal: field values in source order, then one * allocation (cells zeroed), then one FSTORE per cell */ @@ -288,7 +292,8 @@ static void stmt(L *l, int i) int zero = kconst(l, OSC_T_U64, 0, n->line); int r = newv(l, &n->ty, n->line); l->vreg[n->sym] = (int16_t)r; - OscInsn *x = emit(l, OSC_I_ALLOC, n->line); + OscInsn *x = emit(l, ah >= 0 ? OSC_I_AALLOC : OSC_I_ALLOC, n->line); + if (ah >= 0) x->b = (int16_t)ah; x->dst = (int16_t)r; x->a = (int16_t)zero; int ix = -1, k = 0; @@ -314,7 +319,8 @@ static void stmt(L *l, int i) int v = expr(l, n->a); int r = newv(l, &n->ty, n->line); l->vreg[n->sym] = (int16_t)r; - OscInsn *x = emit(l, OSC_I_ALLOC, n->line); + OscInsn *x = emit(l, ah >= 0 ? OSC_I_AALLOC : OSC_I_ALLOC, n->line); + if (ah >= 0) x->b = (int16_t)ah; x->dst = (int16_t)r; x->a = (int16_t)v; break; @@ -462,6 +468,17 @@ static void stmt(L *l, int i) case ON_CALLSTMT: call(l, n->a, 0); 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); + l->vreg[n->sym] = (int16_t)h; + OscInsn *x = emit(l, OSC_I_AOPEN, n->line); + x->dst = (int16_t)h; + x->imm = n->ival; + block(l, n->b); + if (l->cur >= 0) releases(l, n); + break; + } case ON_BLOCK: block(l, i); break; diff --git a/src/compiler/osc_parse.c b/src/compiler/osc_parse.c index 30080d075..5b90ba7ba 100644 --- a/src/compiler/osc_parse.c +++ b/src/compiler/osc_parse.c @@ -646,6 +646,14 @@ static int parse_stmt(P *p) } adv(p); if (parse_ref_body(p, &N(n)->ty, OSC_REF_OWN)) return -1; + int in_arena = -1; /* OSC-2 arenas: "in NAME" */ + if (at(p, OT_IN)) { + adv(p); + if (!at(p, OT_NAME)) return syntax(p, "an arena name after 'in'"); + in_arena = new_node(p, ON_NAME, cur(p)); + if (in_arena < 0) return -1; + adv(p); + } if (expect(p, OT_ASSIGN)) return -1; if (N(n)->ty.sid && at(p, OT_NAME) && peek_kind(p, 1) == OT_LBRACE) { N(n)->kind = ON_LET_ALLOC; @@ -675,6 +683,7 @@ static int parse_stmt(P *p) } else { return syntax(p, "'alloc' or an owner name"); } + N(n)->c = in_arena; } else if (at(p, OT_AMP)) { N(n)->kind = ON_LET_BORROW; N(n)->mut = (uint8_t)mut; @@ -756,6 +765,30 @@ static int parse_stmt(P *p) adv(p); return syntax(p, "'=', '[' or '(' after a name at statement start"); } + case OT_ARENA: { /* OSC-2 arenas: arena NAME bound K { ... } */ + n = new_node(p, ON_ARENA, t); + if (n < 0) return -1; + adv(p); + if (parse_name_tok(p, n)) return -1; + if (!at(p, OT_BOUND)) return syntax(p, "'bound K' after the arena name"); + adv(p); + if (!at(p, OT_INT)) return syntax(p, "bound INT"); + const OscToken *bt = cur(p); + if (bt->ival == 0 || bt->ival > OSC_ARENA_MAX_CELLS) { + char nm[64]; + osc_node_name(p->ast, N(n), nm, sizeof nm); + osc_diag_set(p->d, OSC_DIAG_ARENA_CAPACITY, bt->line, bt->col, nm, t->line, NULL, + "arena bound outside 1..64", "arena bound %llu outside 1..%d cells", + (unsigned long long)bt->ival, OSC_ARENA_MAX_CELLS); + return -1; + } + N(n)->ival = bt->ival; + adv(p); + int b = parse_block(p); + if (b < 0) return -1; + N(n)->b = b; + return n; + } case OT_IF: return parse_if(p); case OT_WHILE: { diff --git a/src/compiler/osc_parse.h b/src/compiler/osc_parse.h index 27112e84d..d9168f4a0 100644 --- a/src/compiler/osc_parse.h +++ b/src/compiler/osc_parse.h @@ -18,6 +18,7 @@ #define OSC_AST_MAX_NODES 16384 #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) */ typedef enum { ON_NONE = 0, @@ -54,8 +55,14 @@ typedef enum { * flag = 1 for an array field "[e; N]" (ival = N) */ ON_FIELD, /* tok = struct binding; lo = field name token; a = index or -1; * checker: sym = binding, hi = field index */ - ON_FSTORE /* tok = struct binding; lo = field name token; a = index or -1; + ON_FSTORE, /* tok = struct binding; lo = field name token; a = index or -1; * b = value; checker: sym = binding, hi = field index */ + /* 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); + * b = body ON_BLOCK; checker: sym = arena symbol, rel = the + * body's owners then the arena itself (REGION_DESTROY) */ } OscNodeKind; typedef struct { diff --git a/src/compiler/osc_rt.c b/src/compiler/osc_rt.c index 835706714..f5d4913e2 100644 --- a/src/compiler/osc_rt.c +++ b/src/compiler/osc_rt.c @@ -12,6 +12,9 @@ _Static_assert(offsetof(OscRt, alloc) == 0, "OscRt.alloc must be at offset 0"); _Static_assert(offsetof(OscRt, release) == 8, "OscRt.release must be at offset 8"); _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(sizeof(void (*)(void)) == 8, "64-bit function pointers"); #define FNV_OFF 1469598103934665603ULL @@ -22,6 +25,9 @@ void osc_rt_init(OscRt *rt) { rt->alloc = osc_rt_alloc; rt->release = osc_rt_release; rt->trap = osc_rt_trap; + rt->arena_open = osc_rt_arena_open; + rt->arena_alloc = osc_rt_arena_alloc; + rt->arena_destroy = osc_rt_arena_destroy; rt->ev_hash = FNV_OFF; } @@ -31,6 +37,8 @@ void osc_rt_reset(OscRt *rt) { memset(rt->live, 0, sizeof rt->live); memset(rt->slot_len, 0, sizeof rt->slot_len); memset(rt->slot_serial, 0, sizeof rt->slot_serial); + memset(rt->slot_arena, 0, sizeof rt->slot_arena); + memset(rt->arena_top, 0, sizeof rt->arena_top); rt->next_serial = 0; rt->live_count = 0; rt->nev = 0; @@ -39,6 +47,9 @@ void osc_rt_reset(OscRt *rt) { rt->alloc = osc_rt_alloc; rt->release = osc_rt_release; rt->trap = osc_rt_trap; + rt->arena_open = osc_rt_arena_open; + rt->arena_alloc = osc_rt_arena_alloc; + rt->arena_destroy = osc_rt_arena_destroy; } static void log_event(OscRt *rt, uint8_t kind, unsigned slot, uint16_t len, uint32_t serial) { @@ -89,7 +100,7 @@ int osc_rt_is_live_ref(const OscRt *rt, uint64_t addr, uint64_t len) { void osc_rt_release(OscRt *rt, uint64_t addr, uint64_t len) { int s = slot_of(rt, addr, len); - if (s < 0) osc_rt_trap(rt, OSC_TRAP_RUNTIME); + if (s < 0 || rt->slot_arena[s]) osc_rt_trap(rt, OSC_TRAP_RUNTIME); memset(rt->cells[s], 0, sizeof rt->cells[s]); rt->live[s] = 0; rt->live_count--; @@ -98,6 +109,58 @@ void osc_rt_release(OscRt *rt, uint64_t addr, uint64_t len) { * osc_rt_reset find every slot that was ever used. */ } +/* ---- OSC-2 arenas ----------------------------------------------------- */ + +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); + for (unsigned s = 0; s < OSC_RT_SLOTS; s++) { + if (rt->live[s]) continue; + memset(rt->cells[s], 0, sizeof rt->cells[s]); + rt->live[s] = 1; + rt->slot_arena[s] = 1; + rt->arena_top[s] = 0; + rt->slot_len[s] = (uint16_t)cap; + rt->slot_serial[s] = ++rt->next_serial; + rt->live_count++; + log_event(rt, OSC_RT_EV_REGION_OPEN, s, (uint16_t)cap, rt->slot_serial[s]); + return (uint64_t)(uintptr_t)&rt->cells[s][0]; + } + osc_rt_trap(rt, OSC_TRAP_OOM); + return 0; /* not reached */ +} + +/* slot index of an open arena whose handle is h, or -1 */ +static int arena_of(const OscRt *rt, uint64_t h) { + uintptr_t base = (uintptr_t)&rt->cells[0][0]; + uintptr_t a = (uintptr_t)h; + size_t stride = sizeof rt->cells[0]; + if (a < base || a >= base + sizeof rt->cells || (a - base) % stride) return -1; + unsigned s = (unsigned)((a - base) / stride); + return rt->live[s] && rt->slot_arena[s] ? (int)s : -1; +} + +uint64_t osc_rt_arena_alloc(OscRt *rt, uint64_t h, uint64_t len, uint64_t init) { + int s = arena_of(rt, h); + 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); + 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); + return (uint64_t)(uintptr_t)&rt->cells[s][top]; +} + +void osc_rt_arena_destroy(OscRt *rt, uint64_t h) { + int s = arena_of(rt, h); + if (s < 0) osc_rt_trap(rt, OSC_TRAP_RUNTIME); + memset(rt->cells[s], 0, sizeof rt->cells[s]); + rt->live[s] = 0; + rt->slot_arena[s] = 0; + rt->arena_top[s] = 0; + rt->live_count--; + log_event(rt, OSC_RT_EV_REGION_DESTROY, (unsigned)s, rt->slot_len[s], rt->slot_serial[s]); +} + typedef uint64_t (*OscNativeFn)(uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, uint64_t, OscRt *); @@ -119,6 +182,8 @@ int osc_rt_same_outcome(const OscRt *a, const OscRt *b) { if (a->next_serial != b->next_serial || a->live_count != b->live_count) return 0; if (memcmp(a->live, b->live, sizeof a->live) || memcmp(a->slot_len, b->slot_len, sizeof a->slot_len) || 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; 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 || diff --git a/src/compiler/osc_rt.h b/src/compiler/osc_rt.h index cca504dbc..4d6779a29 100644 --- a/src/compiler/osc_rt.h +++ b/src/compiler/osc_rt.h @@ -9,11 +9,15 @@ * takes the lowest-numbered free slot (deterministic). Release zeroes the * cells it owned and frees the slot. Double release or releasing an address * that is not a live slot start is a runtime invariant violation - * (OSC_TRAP_RUNTIME). Every alloc/release is appended to an event log that + * (OSC_TRAP_RUNTIME). An OSC-2 arena occupies one slot of K <= 64 cells and + * bump-allocates inside it (TRAP ARENA_FULL past K); its objects are freed + * together by arena_destroy. Every alloc/release and arena open/alloc/destroy + * is appended to an event log that * tests replay through the II.11 model (src/compiler/model/). * * Native ABI (docs/osc/OSC-1-DESIGN.md "Native ABI"): every compiled function - * receives the OscRt* in x7. The three entry points below are reached with + * receives the OscRt* in x7. The entry points below (alloc, release, trap and, + * for OSC-2 arenas, arena_open, arena_alloc, arena_destroy) are reached with * BLR through the function-pointer fields of OscRt, at the fixed offsets * asserted in osc_rt.c. osc_rt_trap never returns: it records the code and * longjmps back to osc_rt_call_native. @@ -28,7 +32,18 @@ #define OSC_RT_SLOTS 64 #define OSC_RT_EVENTS 4096 -typedef enum { OSC_RT_EV_ALLOC = 1, OSC_RT_EV_RELEASE = 2 } OscRtEventKind; +typedef enum { + OSC_RT_EV_ALLOC = 1, + OSC_RT_EV_RELEASE = 2, + /* OSC-2 arenas (docs/osc/OSC-2-DESIGN.md section 3). For the three arena + * events, slot is the arena's pool slot; REGION_OPEN / REGION_DESTROY carry + * len = capacity K and serial = the region serial; ARENA_ALLOC carries the + * object's len and its own serial. Region and object serials share one + * counter (next_serial). */ + OSC_RT_EV_REGION_OPEN = 3, + OSC_RT_EV_ARENA_ALLOC = 4, + OSC_RT_EV_REGION_DESTROY = 5 +} OscRtEventKind; typedef struct { uint8_t kind; /* OscRtEventKind */ @@ -43,6 +58,10 @@ struct OscRt { uint64_t (*alloc)(OscRt *rt, uint64_t len, uint64_t init); /* offset 0 */ void (*release)(OscRt *rt, uint64_t addr, uint64_t len); /* offset 8 */ void (*trap)(OscRt *rt, uint64_t code); /* offset 16 */ + /* OSC-2 arenas (appended; native code reaches them at these offsets). */ + 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 */ /* Host-side state. */ uint64_t cells[OSC_RT_SLOTS][OSC_MAX_ARRAY_LEN]; uint8_t live[OSC_RT_SLOTS]; @@ -58,6 +77,11 @@ struct OscRt { * those beyond OSC_RT_EVENTS that ev[] cannot hold, so long runs still * compare their complete event history. */ uint64_t ev_hash; + /* OSC-2 arenas: slot_arena[s] = 1 while slot s holds an open arena (its + * slot_len is the capacity K, slot_serial the region serial); arena_top[s] + * is the bump pointer (cells handed out so far). */ + uint8_t slot_arena[OSC_RT_SLOTS]; + uint16_t arena_top[OSC_RT_SLOTS]; }; void osc_rt_init(OscRt *rt); @@ -85,4 +109,17 @@ int osc_rt_same_outcome(const OscRt *a, const OscRt *b); /* 1 if addr is the start of a live slot (used to check REF arguments). */ int osc_rt_is_live_ref(const OscRt *rt, uint64_t addr, uint64_t len); +/* ---- OSC-2 arenas (docs/osc/OSC-2-DESIGN.md section 3) ---------------- + * An arena is one pool slot with capacity cap (1..OSC_MAX_ARRAY_LEN cells), + * opened by osc_rt_arena_open, which returns its handle (the slot's address) + * and logs REGION_OPEN. osc_rt_arena_alloc bump-allocates len cells (all set + * to init) and logs ARENA_ALLOC; if the arena has fewer than len cells left it + * traps OSC_TRAP_ARENA_FULL. Objects are never released one by one: + * osc_rt_arena_destroy zeroes the slot, frees it and logs REGION_DESTROY. A bad + * handle or cap traps OSC_TRAP_RUNTIME (never happens for checked code); no + * free slot for a new arena traps OSC_TRAP_OOM. */ +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); + #endif /* OSC_RT_H */ diff --git a/tests/compiler/neg/ar_arena_as_value.osc b/tests/compiler/neg/ar_arena_as_value.osc new file mode 100644 index 000000000..5b1b571b1 --- /dev/null +++ b/tests/compiler/neg/ar_arena_as_value.osc @@ -0,0 +1,9 @@ +// expect: TYPE_MISMATCH object=a line=6 +// transition: arena used as a value +fn f() -> u64 { + let mut s: u64 = 0; + arena a bound 4 { + s = a; + } + return s; +} diff --git a/tests/compiler/neg/ar_capacity_bound_65.osc b/tests/compiler/neg/ar_capacity_bound_65.osc new file mode 100644 index 000000000..55ab9233e --- /dev/null +++ b/tests/compiler/neg/ar_capacity_bound_65.osc @@ -0,0 +1,8 @@ +// expect: ARENA_CAPACITY object=big line=4 +// transition: arena bound outside 1..64 +fn f() -> u32 { + arena big bound 65 { + let x: own [u32; 1] in big = alloc(2); + } + return 0; +} diff --git a/tests/compiler/neg/ar_capacity_bound_zero.osc b/tests/compiler/neg/ar_capacity_bound_zero.osc new file mode 100644 index 000000000..7ad7c0338 --- /dev/null +++ b/tests/compiler/neg/ar_capacity_bound_zero.osc @@ -0,0 +1,8 @@ +// expect: ARENA_CAPACITY object=a line=4 +// transition: arena bound outside 1..64 +fn f() -> u32 { + arena a bound 0 { + let x: own [u32; 1] in a = alloc(2); + } + return 0; +} diff --git a/tests/compiler/neg/ar_capacity_static.osc b/tests/compiler/neg/ar_capacity_static.osc new file mode 100644 index 000000000..999acebb0 --- /dev/null +++ b/tests/compiler/neg/ar_capacity_static.osc @@ -0,0 +1,11 @@ +// expect: ARENA_CAPACITY object=y line=7 +// transition: arena capacity exceeded +fn f() -> u32 { + let mut s: u32 = 0; + arena a bound 5 { + let x: own [u32; 3] in a = alloc(2); + let y: own [u32; 3] in a = alloc(3); + s = x[0] + y[0]; + } + return s; +} diff --git a/tests/compiler/neg/ar_escape_outer_borrow.osc b/tests/compiler/neg/ar_escape_outer_borrow.osc new file mode 100644 index 000000000..d46625d29 --- /dev/null +++ b/tests/compiler/neg/ar_escape_outer_borrow.osc @@ -0,0 +1,11 @@ +// expect: ARENA_ESCAPE object=a line=8 +// transition: outer borrow assigned a borrow of an arena object +fn f() -> u32 { + let u: own [u32; 2] = alloc(1); + let mut r: &[u32; 2] = &u; + arena a bound 4 { + let x: own [u32; 2] in a = alloc(2); + r = &x; + } + return r[0]; +} diff --git a/tests/compiler/neg/ar_escape_return_borrow.osc b/tests/compiler/neg/ar_escape_return_borrow.osc new file mode 100644 index 000000000..a85d361ef --- /dev/null +++ b/tests/compiler/neg/ar_escape_return_borrow.osc @@ -0,0 +1,8 @@ +// expect: ARENA_ESCAPE object=a line=6 +// transition: borrow of an arena object returned +fn f(k: u32) -> u32 { + arena a bound 4 { + let x: own [u32; 2] in a = alloc(k); + return &x; + } +} diff --git a/tests/compiler/neg/ar_move_into.osc b/tests/compiler/neg/ar_move_into.osc new file mode 100644 index 000000000..042929f79 --- /dev/null +++ b/tests/compiler/neg/ar_move_into.osc @@ -0,0 +1,11 @@ +// expect: ARENA_MOVE object=x line=7 +// transition: move into arena +fn f() -> u32 { + let mut s: u32 = 0; + let u: own [u32; 2] = alloc(1); + arena a bound 4 { + let x: own [u32; 2] in a = u; + s = x[0]; + } + return s; +} diff --git a/tests/compiler/neg/ar_move_out.osc b/tests/compiler/neg/ar_move_out.osc new file mode 100644 index 000000000..11faccc16 --- /dev/null +++ b/tests/compiler/neg/ar_move_out.osc @@ -0,0 +1,11 @@ +// expect: ARENA_MOVE object=x line=7 +// transition: move out of arena +fn f() -> u32 { + let mut s: u32 = 0; + arena a bound 4 { + let x: own [u32; 2] in a = alloc(2); + let y: own [u32; 2] = x; + s = y[0]; + } + return s; +} diff --git a/tests/compiler/neg/ar_move_own_param.osc b/tests/compiler/neg/ar_move_own_param.osc new file mode 100644 index 000000000..850a9359e --- /dev/null +++ b/tests/compiler/neg/ar_move_own_param.osc @@ -0,0 +1,11 @@ +// expect: ARENA_MOVE object=x line=8 +// transition: move into own parameter +fn take(v: own [u32; 2]) -> u32 { return v[0]; } +fn f() -> u32 { + let mut s: u32 = 0; + arena a bound 4 { + let x: own [u32; 2] in a = alloc(2); + s = take(x); + } + return s; +} diff --git a/tests/compiler/neg/ar_not_an_arena.osc b/tests/compiler/neg/ar_not_an_arena.osc new file mode 100644 index 000000000..a5690849d --- /dev/null +++ b/tests/compiler/neg/ar_not_an_arena.osc @@ -0,0 +1,7 @@ +// expect: TYPE_MISMATCH object=k line=5 +// transition: in a non-arena +fn f() -> u32 { + let k: u32 = 3; + let x: own [u32; 2] in k = alloc(2); + return x[0]; +} diff --git a/tests/compiler/neg/ar_object_after_block.osc b/tests/compiler/neg/ar_object_after_block.osc new file mode 100644 index 000000000..c87403464 --- /dev/null +++ b/tests/compiler/neg/ar_object_after_block.osc @@ -0,0 +1,8 @@ +// expect: UNDEFINED_NAME object=x line=7 +// transition: use of undefined name +fn f() -> u32 { + arena a bound 4 { + let x: own [u32; 2] in a = alloc(2); + } + return x[0]; +} diff --git a/tests/compiler/neg/ar_shadowing.osc b/tests/compiler/neg/ar_shadowing.osc new file mode 100644 index 000000000..db322b04b --- /dev/null +++ b/tests/compiler/neg/ar_shadowing.osc @@ -0,0 +1,12 @@ +// expect: REDEFINED_NAME object=a line=6 +// transition: redefine visible name +fn f() -> u32 { + let mut s: u32 = 0; + arena a bound 4 { + arena a bound 4 { + let x: own [u32; 2] in a = alloc(2); + s = x[0]; + } + } + return s; +} diff --git a/tests/compiler/neg/ar_undefined_arena.osc b/tests/compiler/neg/ar_undefined_arena.osc new file mode 100644 index 000000000..6910a767e --- /dev/null +++ b/tests/compiler/neg/ar_undefined_arena.osc @@ -0,0 +1,6 @@ +// expect: UNDEFINED_NAME object=nowhere line=4 +// transition: allocate in undefined arena +fn f() -> u32 { + let x: own [u32; 2] in nowhere = alloc(2); + return x[0]; +} diff --git a/tests/compiler/neg/ar_use_after_block.osc b/tests/compiler/neg/ar_use_after_block.osc new file mode 100644 index 000000000..24a9b5e15 --- /dev/null +++ b/tests/compiler/neg/ar_use_after_block.osc @@ -0,0 +1,9 @@ +// expect: UNDEFINED_NAME object=a line=7 +// transition: allocate in undefined arena +fn f() -> u32 { + arena a bound 4 { + let x: own [u32; 2] in a = alloc(2); + } + let y: own [u32; 2] in a = alloc(1); + return y[0]; +} diff --git a/tests/compiler/osc2_receipt.sh b/tests/compiler/osc2_receipt.sh index e36018465..3f3ede73a 100755 --- a/tests/compiler/osc2_receipt.sh +++ b/tests/compiler/osc2_receipt.sh @@ -1,10 +1,11 @@ #!/bin/sh # OSC-2 compiler slice receipt, one per item (docs/osc/OSC-2-DESIGN.md). # OSC-2 slice; not a general Omega compiler; no self-hosting. -# Usage: tests/compiler/osc2_receipt.sh [ITEM] (ITEM: contracts (default) or structs) +# Usage: tests/compiler/osc2_receipt.sh [ITEM] (ITEM: contracts (default), structs or arenas) # 1. refuses a dirty tree (any tracked or untracked change): receipts record an exact commit # 2. runs `make test-compiler` into a temporary OUT_DIR (model sweep, back end, -# compiler golden/negative/model agreement, contract + struct fuzz, determinism; plain + ASan/UBSan) +# compiler golden/negative/model agreement, contract + struct + arena fuzz, runtime model replay, +# determinism; plain + ASan/UBSan) # 3. compiles every golden program with oscc and records its IR and machine-code digests # 4. checks that the existing Omega core sources (src/omega_*, src/aarch64_*, # src/language/, tools/omegatool.c, Makefile) are unchanged against the merge @@ -14,7 +15,7 @@ # Never overwrites existing evidence (evidence/OSC-1 is never touched). Timing is not recorded. set -eu ITEM=${1:-${ITEM:-contracts}} -case "$ITEM" in contracts|structs) ;; *) echo "osc2-receipt: REFUSED: unknown ITEM $ITEM (known: contracts structs)" >&2; exit 2 ;; esac +case "$ITEM" in contracts|structs|arenas) ;; *) echo "osc2-receipt: REFUSED: unknown ITEM $ITEM (known: contracts structs arenas)" >&2; exit 2 ;; esac ROOT=$(cd "$(dirname "$0")/../.." && pwd -P) cd "$ROOT" EVD=evidence/OSC-2/receipts @@ -44,13 +45,26 @@ FLINE_ASAN=$(grep "^contract fuzz:" "$O/compiler_asan.out" | tail -1) [ -n "$FLINE" ] && [ -n "$FLINE_ASAN" ] || die "no contract fuzz line" SLINE=$(grep "^struct fuzz:" "$O/compiler.out" | tail -1) SLINE_ASAN=$(grep "^struct fuzz:" "$O/compiler_asan.out" | tail -1) -if [ "$ITEM" = structs ]; then +if [ "$ITEM" = structs ] || [ "$ITEM" = arenas ]; then case "$SLINE" in *" mismatches=0") ;; *) die "no struct fuzz line with mismatches=0" ;; esac case "$SLINE_ASAN" in *" mismatches=0") ;; *) die "no ASan struct fuzz line with mismatches=0" ;; esac LAYOUT=$(grep "^struct layout:" "$O/compiler.out" | tail -1) DTOR=$(grep "^struct destruction order:" "$O/compiler.out" | tail -1) [ -n "$LAYOUT" ] && [ -n "$DTOR" ] || die "no struct layout / destruction order line" fi +if [ "$ITEM" = arenas ]; then + ALINE=$(grep "^arena fuzz:" "$O/compiler.out" | tail -1) + ALINE_ASAN=$(grep "^arena fuzz:" "$O/compiler_asan.out" | tail -1) + case "$ALINE" in *" mismatches=0") ;; *) die "no arena fuzz line with mismatches=0" ;; esac + case "$ALINE_ASAN" in *" mismatches=0") ;; *) die "no ASan arena fuzz line with mismatches=0" ;; esac + ADTOR=$(grep "^arena destruction order:" "$O/compiler.out" | tail -1) + [ -n "$ADTOR" ] || die "no arena destruction order line" + RLINE=$(grep "^runtime model replay:" "$O/compiler.out" | tail -1) + RLINE_ASAN=$(grep "^runtime model replay:" "$O/compiler_asan.out" | tail -1) + case "$RLINE" in *" rejected=0") ;; *) die "no runtime model replay line with rejected=0" ;; esac + case "$RLINE_ASAN" in *" rejected=0") ;; *) die "no ASan runtime model replay line with rejected=0" ;; esac + n=$(tc ARENA_FULL); [ -n "$n" ] && [ "$n" -gt 0 ] || die "trap ARENA_FULL not observed" +fi for k in OVERFLOW DIV0 BOUNDS LOOP_BOUND CAST OOM SHIFT REQUIRES ENSURES; do n=$(tc $k); [ -n "$n" ] && [ "$n" -gt 0 ] || die "trap $k not observed in golden corpus" done @@ -85,9 +99,12 @@ if [ -n "${PHYSICS_DIR:-}" ] && [ -d "$PHYSICS_DIR" ]; then fi SUBX= -if [ "$ITEM" = structs ]; then +if [ "$ITEM" = structs ] || [ "$ITEM" = arenas ]; then SUBX="; OSC-2 item 2: unit-level struct declarations (<= 16 structs, 1..16 fields of integer, bool or [T; N], <= 64 cells, declared before use, no recursion) with a fixed layout of 8-byte cells in declaration order (no padding, bound into the IR digest), struct literals initialising every field exactly once in let own, field reads and writes (array fields bounds-checked, TRAP BOUNDS), moves, own / & / &mut parameters with the array borrow rules, deterministic destruction with the same alloc/release events; requires reads fields through any struct parameter, ensures only through a shared & parameter; returning a struct is refused (UNSUPPORTED)" fi +if [ "$ITEM" = arenas ]; then + SUBX="$SUBX; OSC-2 item 3: arena blocks 'arena r bound K { ... }' (K = 1..64 cells) wired to the OSC-0B region model; 'let x: own [T; N] in r = alloc(v);' and 'let s: own S in r = S { .. };' allocate from the arena (bump allocation, never released one by one); at block end the unique owners declared in the block are released first, then REGION_DESTROY frees the whole arena (nested: inner first); a borrow of an arena object outliving the arena is ARENA_ESCAPE (the model rejection arena-escape), every move into or out of an arena object is ARENA_MOVE (slice restriction stricter than the model), a definite over-capacity allocation is ARENA_CAPACITY, otherwise TRAP ARENA_FULL = 11 at run time; REGION_OPEN / ARENA_ALLOC / REGION_DESTROY are logged in the runtime event log by both engines, and the log of every native run in the test replays through the OSC-0B model (executed runs only, not a proof for all programs)" +fi R="$T/receipt.json" { printf '{\n' @@ -111,12 +128,20 @@ R="$T/receipt.json" "$(tc none)" "$(tc OVERFLOW)" "$(tc DIV0)" "$(tc BOUNDS)" "$(tc LOOP_BOUND)" "$(tc CAST)" "$(tc OOM)" "$(tc SHIFT)" "$(tc RUNTIME)" "$(tc REQUIRES)" "$(tc ENSURES)" printf ' "contract_fuzz": "%s; native == interpreter on every run",\n' "$FLINE" printf ' "contract_fuzz_asan": "%s",\n' "$FLINE_ASAN" - if [ "$ITEM" = structs ]; then + if [ "$ITEM" = structs ] || [ "$ITEM" = arenas ]; then printf ' "struct_fuzz": "%s; native == interpreter on every run",\n' "$SLINE" printf ' "struct_fuzz_asan": "%s",\n' "$SLINE_ASAN" printf ' "struct_layout": "%s",\n' "$LAYOUT" printf ' "struct_destruction_order": "%s",\n' "$DTOR" fi + if [ "$ITEM" = arenas ]; then + printf ' "arena_fuzz": "%s; native == interpreter on every run",\n' "$ALINE" + printf ' "arena_fuzz_asan": "%s",\n' "$ALINE_ASAN" + printf ' "arena_destruction_order": "%s",\n' "$ADTOR" + printf ' "runtime_model_replay": "%s; every native run of the test (golden fuzz, expect-run lines, contract, struct and arena fuzz, destruction-order tests) replays its runtime event log through the OSC-0B model, trapping runs as the logged prefix; this checks executed runs, not all programs",\n' "$RLINE" + printf ' "runtime_model_replay_asan": "%s",\n' "$RLINE_ASAN" + printf ' "arena_full_trap_runs": %s,\n' "$(tc ARENA_FULL)" + fi printf ' "determinism": "%s (oscc in separate processes, byte-identical IR digest and machine code)",\n' "$DLINE" printf ' "sanitizers": "all three test binaries also pass under -fsanitize=address,undefined -fno-sanitize-recover=all (model sweep at 10^5)",\n' printf ' "golden_programs": %s,\n' "$NPROG" @@ -132,7 +157,9 @@ R="$T/receipt.json" printf ' "omega_core_sources_unchanged_vs_base": %s,\n' "$CORE_UNCHANGED" printf ' "m6_m9_m14_gates": %s,\n' "$GATES" printf ' "self_host": "no: the OSC-2 compiler slice cannot compile any part of itself (docs/osc/OSC-1-SELF-HOST-STATEMENT.md)",\n' - if [ "$ITEM" = structs ]; then + if [ "$ITEM" = arenas ]; then + printf ' "not_covered": "moving arena objects (all arena moves refused: slice restriction, stricter than the model), arena handles as values or parameters, arenas outlasting their block, per-object release inside an arena, struct return values, struct-typed and owner/borrow fields, recursive structs, whole-struct copy or compare, contracts beyond constant folding (no symbolic proof), effects/capabilities, strings, generations, unsafe physical, FFI, Flow IR optimisation, self-hosting (OSC-14); the runtime model replay checks executed runs only and is not a proof for all programs; not a general Omega compiler",\n' + elif [ "$ITEM" = structs ]; then printf ' "not_covered": "struct return values, struct-typed and owner/borrow fields, recursive structs, struct literals outside let own initialisers, whole-struct copy or compare, contracts beyond constant folding (no symbolic proof), effects/capabilities, strings, generations, arenas, unsafe physical, FFI, Flow IR optimisation, self-hosting (OSC-14); not a general Omega compiler",\n' else printf ' "not_covered": "contracts beyond constant folding (no symbolic proof), effects/capabilities, structs, strings, generations, arenas, unsafe physical, FFI, Flow IR optimisation, self-hosting (OSC-14); not a general Omega compiler",\n' diff --git a/tests/compiler/progs/arena_basic.osc b/tests/compiler/progs/arena_basic.osc new file mode 100644 index 000000000..f563430d0 --- /dev/null +++ b/tests/compiler/progs/arena_basic.osc @@ -0,0 +1,39 @@ +// entry: basic ret_inside mixed +// OSC-2 arenas: allocations from a bounded arena; objects are never released +// one by one, the whole arena is destroyed at the end of its block (also on a +// return from inside the block). Unique owners and arena objects mix. +// expect-run: basic 5 -> 17 +// expect-run: ret_inside 3 -> 13 +// expect-run: ret_inside 0 -> 7 +// expect-run: mixed 4 -> 30 +fn basic(k: u32) -> u32 { + let mut s: u32 = 0; + arena r bound 8 { + let a: own [u32; 3] in r = alloc(k); + let b: own [u32; 5] in r = alloc(2); + a[1] = a[0] + b[4] * 5; + s = a[1] + b[0]; + } + return s; +} + +fn ret_inside(k: u8) -> u32 { + arena r bound 64 { + let a: own [u32; 60] in r = alloc(k as u32); + if k > 0 { return a[59] + 10; } + let b: own [u32; 4] in r = alloc(7); + return b[3]; + } +} + +fn mixed(k: u32) -> u32 { + let u: own [u32; 2] = alloc(k); + let mut s: u32 = 0; + arena r bound 6 { + let a: own [u32; 6] in r = alloc(k * 2); + let v: own [u32; 1] = alloc(10); + s = a[5] + u[1] + v[0]; + } + let w: own [u32; 2] = alloc(8); + return s + w[0]; +} diff --git a/tests/compiler/progs/arena_borrows.osc b/tests/compiler/progs/arena_borrows.osc new file mode 100644 index 000000000..ca389864b --- /dev/null +++ b/tests/compiler/progs/arena_borrows.osc @@ -0,0 +1,29 @@ +// entry: borrows +// OSC-2 arenas: shared and mutable borrows of arena objects inside the block, +// passed to & and &mut parameters; all borrows end before the arena dies. +// expect-run: borrows 2 -> 24 +fn sum4(a: &[u32; 4]) -> u32 { + return a[0] + a[1] + a[2] + a[3]; +} + +fn bump(a: &mut [u32; 4], k: u32) { + for i in 0 .. 4 { a[i] = a[i] + k; } +} + +fn borrows(k: u32) -> u32 { + let mut s: u32 = 0; + arena r bound 8 { + let a: own [u32; 4] in r = alloc(1); + bump(&mut a, k); + { + let m: &mut [u32; 4] = &mut a; + m[0] = m[0] + 5; + let t: &[u32; 4] = &m; + s = sum4(&t); + } + let b: own [u32; 4] in r = alloc(0); + let rb: &[u32; 4] = &a; + s = s + rb[3] - b[0] + 6 - 2; + } + return s; +} diff --git a/tests/compiler/progs/arena_dtor.osc b/tests/compiler/progs/arena_dtor.osc new file mode 100644 index 000000000..1a93c6876 --- /dev/null +++ b/tests/compiler/progs/arena_dtor.osc @@ -0,0 +1,22 @@ +// entry: adtor +// OSC-2 arenas: deterministic destruction order. At the end of an arena block +// the unique owners declared inside it are released first (reverse +// declaration order), then the arena is destroyed (one REGION_DESTROY frees +// all its objects). Nested: inner arena before outer. The event order is +// asserted by test_osc_compiler (arena destruction order). +// expect-run: adtor 1 -> 8 +fn adtor(k: u32) -> u32 { + let mut s: u32 = 0; + let u: own [u32; 1] = alloc(k); + arena outer bound 3 { + let a: own [u32; 1] in outer = alloc(1); + arena inner bound 2 { + let b: own [u32; 2] in inner = alloc(2); + let v: own [u32; 1] = alloc(3); + s = a[0] + b[1] + v[0]; + } + let w: own [u32; 1] = alloc(1); + s = s + w[0]; + } + return s + u[0]; +} diff --git a/tests/compiler/progs/arena_full.osc b/tests/compiler/progs/arena_full.osc new file mode 100644 index 000000000..241ba8713 --- /dev/null +++ b/tests/compiler/progs/arena_full.osc @@ -0,0 +1,32 @@ +// entry: fill cond_fill +// OSC-2 arenas: an allocation whose capacity cannot be decided statically +// (inside a loop or a branch) is checked at run time: TRAP ARENA_FULL. +// expect-run: fill 3 -> 3 +// expect-run: fill 4 -> trap=ARENA_FULL +// expect-run: cond_fill 0 -> 2 +// expect-run: cond_fill 1 -> trap=ARENA_FULL +fn fill(n: u8) -> u32 { + let mut s: u32 = 0; + arena r bound 10 { + let mut i: u8 = 0; + while i < n bound 20 { + let a: own [u32; 3] in r = alloc(i as u32); + s = s + a[0]; + i = i + 1; + } + } + return s; +} + +fn cond_fill(c: u8) -> u32 { + let mut s: u32 = 0; + arena r bound 4 { + let a: own [u32; 2] in r = alloc(1); + if c > 0 { + let b: own [u32; 3] in r = alloc(2); + s = b[0]; + } + s = s + a[0] + 1; + } + return s; +} diff --git a/tests/compiler/progs/arena_nested.osc b/tests/compiler/progs/arena_nested.osc new file mode 100644 index 000000000..121f7d976 --- /dev/null +++ b/tests/compiler/progs/arena_nested.osc @@ -0,0 +1,31 @@ +// entry: nested sibling +// OSC-2 arenas: nested arenas; the inner arena is destroyed first, then the +// outer one. Sibling arenas reuse the runtime slot of a destroyed arena. +// expect-run: nested 3 -> 15 +// expect-run: sibling 2 -> 9 +fn nested(k: u16) -> u16 { + let mut s: u16 = 0; + arena outer bound 4 { + let a: own [u16; 2] in outer = alloc(k); + arena inner bound 4 { + let b: own [u16; 4] in inner = alloc(k * 2); + let c: own [u16; 2] in outer = alloc(3); + s = a[0] + b[3] + c[1] * 2; + } + s = s + 0; + } + return s; +} + +fn sibling(k: u8) -> u8 { + let mut s: u8 = 0; + arena r1 bound 2 { + let a: own [u8; 2] in r1 = alloc(k); + s = a[1]; + } + arena r2 bound 2 { + let b: own [u8; 2] in r2 = alloc(k + 5); + s = s + b[0]; + } + return s; +} diff --git a/tests/compiler/progs/arena_structs.osc b/tests/compiler/progs/arena_structs.osc new file mode 100644 index 000000000..bd7bd83de --- /dev/null +++ b/tests/compiler/progs/arena_structs.osc @@ -0,0 +1,19 @@ +// entry: points +// OSC-2 arenas: struct literals allocated from an arena; a struct uses its +// cell count (Pt: 3 cells) of the arena's bound. +// u8 arithmetic wraps (OSC-1 rule): points 200 -> 96. +// expect-run: points 4 -> 20 +// expect-run: points 200 -> 96 +struct Pt { x: u8, y: u8, w: [u8; 1] } + +fn points(k: u8) -> u8 { + let mut s: u8 = 0; + arena r bound 7 { + let p: own Pt in r = Pt { x: k, y: 1, w: [2; 1] }; + let q: own Pt in r = Pt { y: k * 2, x: 3, w: [0; 1] }; + let t: own [u8; 1] in r = alloc(5); + p.x = p.x + q.y; + s = p.x + p.y + p.w[0] + t[0] + q.x - 3; + } + return s; +} diff --git a/tests/compiler/test_osc_backend.c b/tests/compiler/test_osc_backend.c index f13c6e289..57d306199 100644 --- a/tests/compiler/test_osc_backend.c +++ b/tests/compiler/test_osc_backend.c @@ -910,6 +910,30 @@ static void test_validator(void) { f0->insns[1] = I0(OSC_I_MOV); f0->insns[1].dst = 2; f0->insns[1].a = 3; f0->insns[2] = I0(OSC_I_RELEASE); f0->insns[2].a = 2; f0->insns[3] = I0(OSC_I_RET); f0->insns[3].a = 0; f0->ninsns = 4; f0->blocks[0].count = 4; }); + /* OSC-2 arenas: f0 body = AOPEN h(4); x = AALLOC [i32; 4] init a from h; ADESTROY h; RET a */ +#define ARENA_BODY(f) do { \ + (f)->vtype[2] = TS(OSC_T_U64); (f)->vtype[3] = TR(OSC_REF_OWN, OSC_T_I32, 4); (f)->nvregs = 4; \ + (f)->insns[0] = I0(OSC_I_AOPEN); (f)->insns[0].dst = 2; (f)->insns[0].imm = 4; \ + (f)->insns[1] = I0(OSC_I_AALLOC); (f)->insns[1].dst = 3; (f)->insns[1].a = 0; (f)->insns[1].b = 2; \ + (f)->insns[2] = I0(OSC_I_ADESTROY); (f)->insns[2].a = 2; \ + (f)->insns[3] = I0(OSC_I_RET); (f)->insns[3].a = 0; (f)->ninsns = 4; (f)->blocks[0].count = 4; } while (0) + { + OscUnit *x = base_unit(); + ARENA_BODY(&x->funcs[0]); + char e2[256] = {0}; + check(osc_ir_validate(x, e2, sizeof e2) == 0, "valid arena unit refused: %s", e2); + free(x); + } + CASE("AOPEN bound 0", { ARENA_BODY(f0); f0->insns[0].imm = 0; }); + CASE("AOPEN bound 65", { ARENA_BODY(f0); f0->insns[0].imm = 65; }); + CASE("AOPEN handle not u64", { ARENA_BODY(f0); f0->vtype[2] = TS(OSC_T_U32); }); + CASE("AOPEN twice into one handle", { ARENA_BODY(f0); f0->insns[2] = I0(OSC_I_AOPEN); f0->insns[2].dst = 2; f0->insns[2].imm = 4; }); + CASE("AALLOC from a non-handle", { ARENA_BODY(f0); f0->vtype[1] = TS(OSC_T_U64); f0->insns[1].b = 1; }); + CASE("AALLOC into a shared borrow", { ARENA_BODY(f0); f0->vtype[3] = TR(OSC_REF_SHARED, OSC_T_I32, 4); }); + CASE("ADESTROY of a non-handle", { ARENA_BODY(f0); f0->vtype[1] = TS(OSC_T_U64); f0->insns[2].a = 1; }); + CASE("arena handle used as a value", { ARENA_BODY(f0); f0->ret = TS(OSC_T_U64); f0->insns[3].a = 2; }); + CASE("arena handle redefined by MOV", { ARENA_BODY(f0); f0->vtype[1] = TS(OSC_T_U64); f0->insns[2] = I0(OSC_I_MOV); f0->insns[2].dst = 2; f0->insns[2].a = 1; }); +#undef ARENA_BODY CASE("owner from borrow (MOV)", { f0->vtype[2] = TR(OSC_REF_OWN, OSC_T_I32, 4); f0->vtype[3] = TR(OSC_REF_SHARED, OSC_T_I32, 4); f0->vtype[4] = TR(OSC_REF_OWN, OSC_T_I32, 4); f0->nvregs = 5; diff --git a/tests/compiler/test_osc_compiler.c b/tests/compiler/test_osc_compiler.c index 48eaea074..15f2eb2a6 100644 --- a/tests/compiler/test_osc_compiler.c +++ b/tests/compiler/test_osc_compiler.c @@ -39,7 +39,7 @@ static unsigned long checks, failures; static unsigned long trap_seen[OSC_TRAP_MAX + 1]; -static unsigned long diff_runs, rt_replays, trace_replays, rt_events_replayed; +static unsigned long diff_runs, rt_replays, trace_replays; #define CHECK(cond, ...) \ do { \ @@ -184,70 +184,137 @@ static int replay_trace(const OscTrace *t, uint32_t i0, uint32_t i1, OscModelRej return -1; } -/* runtime pool log -> model ALLOC/RELEASE; windowed ids when > 64 objects */ +/* runtime pool log -> model events, windowed ids: + * ALLOC -> ALLOC(obj, region 0); RELEASE -> RELEASE(obj); + * REGION_OPEN -> REGION_OPEN(region); ARENA_ALLOC -> ALLOC(obj, region of + * that arena slot); REGION_DESTROY -> REGION_DESTROY(region), which releases + * the region's objects in the model. + * When the next object id would exceed 64 (or region id 16), a new window + * starts: a fresh model re-opens the open regions and re-allocates the live + * 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). */ +static unsigned long rr_runs, rr_events, rr_accepted, rr_rejected; static int replay_rt(const OscRt *rt, char *why, size_t n) { static OscModel m; static uint32_t id_of_serial[1 << 16]; static uint32_t serial_of_id[OSC_MODEL_MAX_OBJECTS + 1]; static uint8_t live_id[OSC_MODEL_MAX_OBJECTS + 1]; - uint32_t next = 1; + static int16_t aslot_of_id[OSC_MODEL_MAX_OBJECTS + 1]; /* arena slot, -1 = unique */ + static uint32_t rid_of_slot[OSC_RT_SLOTS]; /* 0 = no open arena */ + uint32_t next = 1, nextr = 1; + OscModelEvent ev; osc_model_init(&m, 0, UINT64_MAX); memset(live_id, 0, sizeof live_id); + memset(rid_of_slot, 0, sizeof rid_of_slot); 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->serial >= (1u << 16)) { snprintf(why, n, "serial too large"); return -1; } - OscModelEvent ev; - memset(&ev, 0, sizeof ev); - if (e->kind == OSC_RT_EV_ALLOC) { - if (next > OSC_MODEL_MAX_OBJECTS) { - /* new window: re-create the live objects with fresh ids */ - uint32_t keep[OSC_MODEL_MAX_OBJECTS], nk = 0; - for (uint32_t k = 1; k <= OSC_MODEL_MAX_OBJECTS; k++) - if (live_id[k]) keep[nk++] = serial_of_id[k]; - osc_model_init(&m, 0, UINT64_MAX); - memset(live_id, 0, sizeof live_id); - next = 1; - for (uint32_t k = 0; k < nk; k++) { - memset(&ev, 0, sizeof ev); - ev.kind = OSC_EV_ALLOC; - ev.obj = next; - if (osc_model_step(&m, &ev, NULL) != OSC_MODEL_ACCEPT) { snprintf(why, n, "re-alloc"); return -1; } - id_of_serial[keep[k]] = next; - serial_of_id[next] = keep[k]; - live_id[next] = 1; - next++; - } - if (next > OSC_MODEL_MAX_OBJECTS) { snprintf(why, n, "64 live objects"); 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) || + (e->kind == OSC_RT_EV_REGION_OPEN && nextr > OSC_MODEL_MAX_REGIONS)) { + /* new window: re-open open regions, re-create live objects */ + uint32_t keep[OSC_MODEL_MAX_OBJECTS], nk = 0; + int16_t keep_slot[OSC_MODEL_MAX_OBJECTS]; + for (uint32_t k = 1; k <= OSC_MODEL_MAX_OBJECTS; k++) + if (live_id[k]) { keep_slot[nk] = aslot_of_id[k]; keep[nk++] = serial_of_id[k]; } + osc_model_init(&m, 0, UINT64_MAX); + memset(live_id, 0, sizeof live_id); + next = 1; + nextr = 1; + for (uint32_t s = 0; s < OSC_RT_SLOTS; s++) { + if (!rid_of_slot[s]) continue; + if (nextr > OSC_MODEL_MAX_REGIONS) { snprintf(why, n, "16 open regions"); return -1; } memset(&ev, 0, sizeof ev); + ev.kind = OSC_EV_REGION_OPEN; + ev.region = nextr; + if (osc_model_step(&m, &ev, NULL) != OSC_MODEL_ACCEPT) { snprintf(why, n, "re-open"); return -1; } + rid_of_slot[s] = nextr++; } + for (uint32_t k = 0; k < nk; k++) { + memset(&ev, 0, sizeof ev); + ev.kind = OSC_EV_ALLOC; + ev.obj = next; + ev.region = keep_slot[k] >= 0 ? rid_of_slot[keep_slot[k]] : 0; + if (osc_model_step(&m, &ev, NULL) != OSC_MODEL_ACCEPT) { snprintf(why, n, "re-alloc"); return -1; } + id_of_serial[keep[k]] = next; + serial_of_id[next] = keep[k]; + aslot_of_id[next] = keep_slot[k]; + live_id[next] = 1; + next++; + } + if (is_alloc && next > OSC_MODEL_MAX_OBJECTS) { snprintf(why, n, "64 live objects"); return -1; } + } + memset(&ev, 0, sizeof ev); + switch (e->kind) { + case OSC_RT_EV_ALLOC: + case OSC_RT_EV_ARENA_ALLOC: ev.kind = OSC_EV_ALLOC; ev.obj = next; + if (e->kind == OSC_RT_EV_ARENA_ALLOC) { + ev.region = rid_of_slot[e->slot]; + if (!ev.region) { snprintf(why, n, "arena alloc into unopened slot %u", e->slot); return -1; } + } id_of_serial[e->serial] = next; serial_of_id[next] = e->serial; + aslot_of_id[next] = e->kind == OSC_RT_EV_ARENA_ALLOC ? (int16_t)e->slot : -1; live_id[next] = 1; next++; - } else { + break; + case OSC_RT_EV_RELEASE: ev.kind = OSC_EV_RELEASE; ev.obj = id_of_serial[e->serial]; if (!ev.obj || !live_id[ev.obj]) { snprintf(why, n, "release of unknown serial %u", e->serial); return -1; } live_id[ev.obj] = 0; + break; + case OSC_RT_EV_REGION_OPEN: + if (rid_of_slot[e->slot]) { snprintf(why, n, "region open on an open arena slot %u", e->slot); return -1; } + ev.kind = OSC_EV_REGION_OPEN; + ev.region = nextr; + rid_of_slot[e->slot] = nextr++; + break; + case OSC_RT_EV_REGION_DESTROY: + ev.kind = OSC_EV_REGION_DESTROY; + ev.region = rid_of_slot[e->slot]; + if (!ev.region) { snprintf(why, n, "destroy of unopened arena slot %u", e->slot); return -1; } + rid_of_slot[e->slot] = 0; + for (uint32_t k = 1; k <= OSC_MODEL_MAX_OBJECTS; k++) + if (live_id[k] && aslot_of_id[k] == (int16_t)e->slot) live_id[k] = 0; + break; + default: + snprintf(why, n, "unknown runtime event kind %u", e->kind); + return -1; } OscModelReject r; if (osc_model_step(&m, &ev, &r) != OSC_MODEL_ACCEPT) { snprintf(why, n, "event %u %s rejected: %s", i, osc_model_event_name(ev.kind), osc_model_reject_name(r)); return -1; } - rt_events_replayed++; + rr_events++; } return 0; } +/* Replay the native run's runtime event log (every native run in this test: + * golden fuzz, expect-run lines, contract / struct / arena fuzz, destruction + * order tests). Non-trapping runs replay fully; trapping runs replay the + * prefix logged before the trap. This checks executed runs only. */ +static int rt_replay_run(const OscRt *rt, const char *ctx) +{ + char why[160]; + rr_runs++; + if (replay_rt(rt, why, sizeof why) == 0) { rr_accepted++; return 0; } + rr_rejected++; + CHECK(0, "%s: runtime event log replay rejected: %s", ctx, why); + return -1; +} + /* ------------------------------------------------------------ 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"}; +static const char *trap_names[OSC_TRAP_MAX + 1] = {"none", "OVERFLOW", "DIV0", "BOUNDS", "LOOP_BOUND", "CAST", "OOM", "SHIFT", "RUNTIME", "REQUIRES", "ENSURES", "ARENA_FULL"}; static uint64_t parse_val(const char *s) { @@ -265,7 +332,7 @@ static unsigned long golden_ok, golden_entries, golden_funcs, traces_skipped_ove static void golden(const char *dir, const char *name, unsigned fuzz) { - char path[1024], ent[512], why[160]; + char path[1024], ent[512]; snprintf(path, sizeof path, "%s/%s", dir, name); size_t len; char *src = read_file(path, &len); @@ -365,12 +432,12 @@ static void golden(const char *dir, const char *name, unsigned fuzz) if (ti == 0) { CHECK(RI->live_count == 0 && RN->live_count == 0, "%s:%s leak: live_count interp %u native %u", name, tok, RI->live_count, RN->live_count); - if (replay_rt(RN, why, sizeof why) == 0) rt_replays++; - else CHECK(0, "%s:%s pool log replay rejected: %s", name, tok, why); } + if (rt_replay_run(RN, name) == 0 && ti == 0) rt_replays++; } - printf(" %-18s %-12s ok=%-5lu ovf=%-4lu div0=%-4lu bnd=%-4lu loop=%-4lu cast=%-4lu oom=%-4lu shift=%-4lu rq=%-4lu en=%-4lu\n", - name, tok, pertrap[0], pertrap[1], pertrap[2], pertrap[3], pertrap[4], pertrap[5], pertrap[6], pertrap[7], pertrap[9], pertrap[10]); + printf(" %-18s %-12s ok=%-5lu ovf=%-4lu div0=%-4lu bnd=%-4lu loop=%-4lu cast=%-4lu oom=%-4lu shift=%-4lu rq=%-4lu en=%-4lu af=%-4lu\n", + name, tok, pertrap[0], pertrap[1], pertrap[2], pertrap[3], pertrap[4], pertrap[5], pertrap[6], pertrap[7], pertrap[9], pertrap[10], + pertrap[11]); } expect_runs(name, src, &nm, &c1); osc_native_unmap(&nm); @@ -439,6 +506,7 @@ static const char *mapped(int kind) case OSC_DIAG_MUTABLE_ALIAS: return "mutable-alias"; 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"; default: return NULL; } } @@ -618,6 +686,7 @@ static void contract_fuzz(unsigned n) osc_rt_reset(RN); int t1 = osc_interp_run_prevalidated(U1, fi, args, 2, RI, &ri); int t2 = osc_rt_call_native(RN, entry, args, 2, &rn); + rt_replay_run(RN, "contract fuzz"); cf_runs++; diff_runs++; int same = t1 == t2 && t1 >= 0 && (t1 != 0 || ri == rn) && osc_rt_same_outcome(RI, RN); @@ -688,13 +757,16 @@ static void struct_layout(void) /* Destruction order of progs/structs_dtor.osc: owners released at scope end in * reverse declaration order, the moved-from struct is not released, in the * interpreter and natively. */ -static void struct_dtor_order(const char *dir) +/* Run entry 0 of / with argument 1 in the interpreter and natively; + * both pool logs must match the expected (kind, serial) sequence exactly. */ +static void dtor_order(const char *dir, const char *file, uint64_t want_ret, unsigned n, const uint8_t *want_kind, + const uint32_t *want_ser) { char path[1024]; - snprintf(path, sizeof path, "%s/structs_dtor.osc", dir); + snprintf(path, sizeof path, "%s/%s", dir, file); size_t len; char *src = read_file(path, &len); - CHECK(src != NULL, "structs_dtor.osc unreadable"); + CHECK(src != NULL, "%s unreadable", file); if (!src) return; OscDiag d; OscCode c; @@ -702,32 +774,53 @@ static void struct_dtor_order(const char *dir) memset(&c, 0, sizeof c); int rc = osc_compile(src, len, U1, &d, NULL); free(src); - CHECK(rc == 0 && osc_cg_compile(U1, &c, err, sizeof err) == 0, "structs_dtor: compile failed"); + CHECK(rc == 0 && osc_cg_compile(U1, &c, err, sizeof err) == 0, "%s: compile failed", file); if (rc) return; OscNative nm; - if (osc_native_map(&nm, c.code, c.len) != 0) { CHECK(0, "structs_dtor: map failed"); osc_cg_free(&c); return; } - static const uint8_t want_kind[8] = {1, 1, 1, 2, 2, 1, 2, 2}; - static const uint32_t want_ser[8] = {1, 2, 3, 3, 2, 4, 4, 1}; + if (osc_native_map(&nm, c.code, c.len) != 0) { CHECK(0, "%s: map failed", file); osc_cg_free(&c); return; } uint64_t args[1] = {1}, ri = 0, rn = 0; osc_rt_reset(RI); osc_rt_reset(RN); int ti = osc_interp_run(U1, 0, args, 1, RI, &ri); int tn = osc_rt_call_native(RN, osc_native_at(&nm, c.entry[0]), args, 1, &rn); - CHECK(ti == 0 && tn == 0 && ri == 14 && rn == 14, "structs_dtor: run %d/%d ret %llu/%llu", ti, tn, + rt_replay_run(RN, file); + CHECK(ti == 0 && tn == 0 && ri == want_ret && rn == want_ret, "%s: run %d/%d ret %llu/%llu", file, ti, tn, (unsigned long long)ri, (unsigned long long)rn); const OscRt *rts[2] = {RI, RN}; for (int r = 0; r < 2; r++) { - CHECK(rts[r]->nev == 8, "structs_dtor: %u pool events, expected 8", rts[r]->nev); - for (unsigned k = 0; k < 8 && k < rts[r]->nev; k++) + CHECK(rts[r]->nev == n, "%s: %u pool events, expected %u", file, rts[r]->nev, n); + for (unsigned k = 0; k < n && k < rts[r]->nev; k++) CHECK(rts[r]->ev[k].kind == want_kind[k] && rts[r]->ev[k].serial == want_ser[k], - "structs_dtor: event %u is kind %u serial %u, expected kind %u serial %u", k, rts[r]->ev[k].kind, + "%s: event %u is kind %u serial %u, expected kind %u serial %u", file, k, rts[r]->ev[k].kind, rts[r]->ev[k].serial, want_kind[k], want_ser[k]); } osc_native_unmap(&nm); osc_cg_free(&c); +} + +static void struct_dtor_order(const char *dir) +{ + static const uint8_t want_kind[8] = {1, 1, 1, 2, 2, 1, 2, 2}; + static const uint32_t want_ser[8] = {1, 2, 3, 3, 2, 4, 4, 1}; + dtor_order(dir, "structs_dtor.osc", 14, 8, want_kind, want_ser); printf("struct destruction order: alloc 1 2 3, release 3 2, alloc 4, release 4 1 (interp == native)\n"); } +/* OSC-2 arenas (arena_dtor.osc): u = ALLOC 1; outer = REGION_OPEN 2; + * a = ARENA_ALLOC 3; inner = REGION_OPEN 4; b = ARENA_ALLOC 5; v = ALLOC 6; + * inner block end: RELEASE v 6, REGION_DESTROY inner 4; w = ALLOC 7; + * outer block end: RELEASE w 7, REGION_DESTROY outer 2; return: RELEASE u 1. + * Unique owners inside an arena block are released before the arena is + * destroyed; the inner arena dies before the outer one. */ +static void arena_dtor_order(const char *dir) +{ + static const uint8_t want_kind[12] = {1, 3, 4, 3, 4, 1, 2, 5, 1, 2, 5, 2}; + static const uint32_t want_ser[12] = {1, 2, 3, 4, 5, 6, 6, 4, 7, 7, 2, 1}; + dtor_order(dir, "arena_dtor.osc", 8, 12, want_kind, want_ser); + printf("arena destruction order: alloc 1, open 2, aalloc 3, open 4, aalloc 5, alloc 6, release 6, destroy 4, " + "alloc 7, release 7, destroy 2, release 1 (interp == native)\n"); +} + /* Struct fuzz: generated units with 1..2 structs of random integer, bool and * [T; N] fields, an entry that builds a struct literal, writes fields * (dynamic array-field indexes reach BOUNDS), moves it, passes it as &mut, @@ -850,6 +943,7 @@ static void struct_fuzz(unsigned n) 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, "struct fuzz"); sf_runs++; diff_runs++; int same = t1 == t2 && t1 >= 0 && (t1 != 0 || ri == rn) && osc_rt_same_outcome(RI, RN); @@ -872,6 +966,150 @@ static void struct_fuzz(unsigned n) CHECK(sf_trap[OSC_TRAP_REQUIRES] > 0, "struct fuzz never hit TRAP REQUIRES"); } +/* OSC-2 arena fuzz: generated units with 1..3 levels of nested arenas of + * random bound (4..31 cells). Statements allocate arrays and struct literals + * into any visible arena, unique owners inside arena blocks, borrows of + * arena objects passed to & / &mut parameters (dynamic indexes reach BOUNDS), + * and allocations inside loops and branches whose capacity is only known at + * run time (TRAP ARENA_FULL). Definite allocations are kept within the bound + * (the generator tracks the static budget), so every unit must compile. + * Interpreter and native outcomes must be identical (0 mismatches); every + * native run's event log replays through the model (rt_replay_run). */ +static unsigned long af_units, af_runs, af_mismatch, af_trap[OSC_TRAP_MAX + 1], af_allocs; +typedef struct { unsigned id, cap, used; } AfArena; + +static void af_alloc(char *src, size_t cap, AfArena *r, int definite, unsigned *nm) +{ + unsigned v = (*nm)++; + int st = sm() % 4 == 0; + unsigned len = st ? 3 : 1 + (unsigned)(sm() % 8); + if (sm() % 3 == 0) len = 4; + int wrap = definite && r->used + len > r->cap; /* would be refused statically: make it dynamic */ + if (wrap) cf_cat(src, cap, " if (a & 3) != 0 {\n"); + if (definite && !wrap) r->used += len; + af_allocs++; + if (st) { + cf_cat(src, cap, " let x%u: own P in r%u = P { x: %s, y: [b & 7; 2] };\n", v, r->id, + sm() % 6 ? "a & 1023" : "a"); + cf_cat(src, cap, " acc = acc + x%u.x + x%u.y[%s];\n", v, v, sm() % 3 ? "1" : "i % 2"); + } else { + cf_cat(src, cap, " let x%u: own [i64; %u] in r%u = alloc(%s);\n", v, len, r->id, sm() % 6 ? "a & 1023" : "a"); + if (len == 4 && sm() % 2) cf_cat(src, cap, " wr4(&mut x%u, i, b);\n acc = acc + rd4(&x%u);\n", v, v); + else cf_cat(src, cap, " acc = acc + x%u[%s];\n", v, sm() % 4 ? "0" : "i & 7"); + } + if (wrap) cf_cat(src, cap, " }\n"); +} + +static void af_body(char *src, size_t cap, AfArena *ar, unsigned nar, unsigned depth, unsigned *nm, unsigned *nmade) +{ + unsigned ns = 2 + (unsigned)(sm() % 4); + for (unsigned k = 0; k < ns; k++) { + AfArena *r = &ar[sm() % nar]; + switch (sm() % 6) { + case 0: + case 1: af_alloc(src, cap, r, 1, nm); break; + case 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); + af_alloc(src, cap, r, 0, nm); + cf_cat(src, cap, " j%u = j%u + 1;\n }\n", j, j); + break; + } + case 3: + cf_cat(src, cap, " if a > b {\n"); + af_alloc(src, cap, r, 0, nm); + cf_cat(src, cap, " }\n"); + break; + case 4: { + unsigned v = (*nm)++; + cf_cat(src, cap, " let w%u: own [i64; 2] = alloc(b & 7);\n acc = acc + w%u[1];\n", v, v); + break; + } + default: + if (depth < 3 && *nmade < 6) { + AfArena *in = &ar[nar]; + in->id = (*nmade)++; + in->cap = 4 + (unsigned)(sm() % 28); + in->used = 0; + cf_cat(src, cap, " arena r%u bound %u {\n", in->id, in->cap); + af_body(src, cap, ar, nar + 1, depth + 1, nm, nmade); + cf_cat(src, cap, " }\n"); + } else { + af_alloc(src, cap, r, 1, nm); + } + } + } +} + +static void arena_fuzz(unsigned n) +{ + static char src[16384]; + sm_state = 0x05C2A3E4Aull; + for (unsigned u = 0; u < n; u++) { + src[0] = 0; + cf_cat(src, sizeof src, + "struct P { x: i64, y: [i64; 2] }\n" + "fn rd4(p: &[i64; 4]) -> i64 { return p[0] + p[1] + p[2] + p[3]; }\n" + "fn wr4(p: &mut [i64; 4], i: u8, b: i64) { p[i & 3] = p[i %% 4] + (b & 15); p[i %% 5] = 1; }\n" + "fn entry(a: i64, b: i64, i: u8) -> i64 {\n let mut acc: i64 = 0;\n"); + AfArena ar[8]; + unsigned nm = 0, nmade = 1; + ar[0].id = 0; + ar[0].cap = 4 + (unsigned)(sm() % 28); + ar[0].used = 0; + cf_cat(src, sizeof src, " arena r0 bound %u {\n", ar[0].cap); + af_body(src, sizeof src, ar, 1, 1, &nm, &nmade); + cf_cat(src, sizeof src, " }\n return acc;\n}\n"); + af_units++; + + OscDiag d; + int rc = osc_compile(src, strlen(src), U1, &d, NULL); + CHECK(rc == 0, "arena 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); + if (rc) continue; + OscCode c; + char err[160]; + memset(&c, 0, sizeof c); + int cg = osc_cg_compile(U1, &c, err, sizeof err); + CHECK(cg == 0, "arena 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, "arena 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, "arena fuzz"); + af_runs++; + diff_runs++; + int same = t1 == t2 && t1 >= 0 && (t1 != 0 || ri == rn) && osc_rt_same_outcome(RI, RN); + CHECK(same, "arena 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) { af_mismatch++; break; } + if (t1 >= 0 && t1 <= OSC_TRAP_MAX) { af_trap[t1]++; trap_seen[t1]++; } + CHECK(t1 != OSC_TRAP_RUNTIME, "arena fuzz unit %u RUNTIME trap", u); + if (t1 == 0) CHECK(RI->live_count == 0 && RN->live_count == 0, "arena fuzz unit %u leak", u); + } + osc_native_unmap(&nm2); + osc_cg_free(&c); + } + printf("arena fuzz: units=%lu allocs=%lu runs=%lu ok=%lu arena_full=%lu bounds=%lu overflow=%lu mismatches=%lu\n", + af_units, af_allocs, af_runs, af_trap[0], af_trap[OSC_TRAP_ARENA_FULL], af_trap[OSC_TRAP_BOUNDS], + af_trap[OSC_TRAP_OVERFLOW], af_mismatch); + CHECK(af_trap[0] > 0, "arena fuzz never returned normally"); + CHECK(af_trap[OSC_TRAP_ARENA_FULL] > 0, "arena fuzz never hit TRAP ARENA_FULL"); +} + int main(int argc, char **argv) { unsigned fuzz = argc > 1 ? (unsigned)strtoul(argv[1], NULL, 10) : 1000; @@ -903,7 +1141,9 @@ int main(int argc, char **argv) contract_fuzz(fuzz / 5 ? fuzz / 5 : 20); struct_layout(); struct_dtor_order(pdir); + arena_dtor_order(pdir); struct_fuzz(fuzz / 8 ? fuzz / 8 : 16); + arena_fuzz(fuzz / 8 ? fuzz / 8 : 16); const char *const *tn = trap_names; printf("trap coverage (runs, interpreter == native):\n"); @@ -912,6 +1152,10 @@ int main(int argc, char **argv) CHECK(trap_seen[8] == 0, "RUNTIME trap observed"); CHECK(trap_seen[OSC_TRAP_REQUIRES] > 0 && trap_seen[OSC_TRAP_ENSURES] > 0, "golden fuzz never hit REQUIRES/ENSURES"); CHECK(traces_skipped_overflow == 0, "%lu golden traces exceeded the model's 64 ids", traces_skipped_overflow); + printf("runtime model replay: runs=%lu events=%lu accepted=%lu rejected=%lu\n", rr_runs, rr_events, rr_accepted, + rr_rejected); + CHECK(rr_runs > 0 && rr_rejected == 0 && rr_accepted == rr_runs, "runtime model replay: %lu of %lu runs rejected", + rr_rejected, rr_runs); for (int i = 0; i < np; i++) free(pv[i]); for (int i = 0; i < nn; i++) free(nv[i]); @@ -922,7 +1166,7 @@ int main(int argc, char **argv) double secs = (double)(t1.tv_sec - t0.tv_sec) + (double)(t1.tv_nsec - t0.tv_nsec) / 1e9; printf("golden=%lu/%d funcs=%lu entries=%lu expect_runs=%lu diff_runs=%lu trace_replays=%lu rt_replays=%lu rt_events=%lu " "neg=%lu/%lu neg_traced=%lu checks=%lu failed=%lu time=%.2fs\n", - golden_ok, np, golden_funcs, golden_entries, expect_total, diff_runs, trace_replays, rt_replays, rt_events_replayed, + golden_ok, np, golden_funcs, golden_entries, expect_total, diff_runs, trace_replays, rt_replays, rr_events, neg_ok, neg_total, neg_traced, checks, failures, secs); if (failures == 0) { printf("OSC1_COMPILER_PASS golden=%lu neg=%lu diff_runs=%lu checks=%lu\n", golden_ok, neg_ok, diff_runs, checks); @@ -972,6 +1216,7 @@ static void expect_runs(const char *name, const char *src, const OscNative *nm, osc_rt_reset(RN); int ti = osc_interp_run(U1, fi, args, na, RI, &ri); int tn = osc_rt_call_native(RN, osc_native_at(nm, code->entry[fi]), args, na, &rn); + rt_replay_run(RN, name); int ok = ti == want_trap && tn == want_trap && (want_trap || (ri == want && rn == want && RI->live_count == 0 && RN->live_count == 0)); CHECK(ok, "%s: expect-run '%.*s': interp trap %d ret %llu, native trap %d ret %llu", name, (int)l, p, From f8bba06d5bb6a8daf0fc90b4d02805f483467a7c Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Thu, 1 Oct 2026 05:38:30 -0500 Subject: [PATCH 2/2] OSC-2 item 3: arenas receipt osc2-arenas-d32a589a78c8238a47ee966e507c2b112d80396455dce0ac293e337e17c3cf58.json Co-Authored-By: Drake Stapleton Co-Authored-By: Claude Fable 5.1 Co-Authored-By: Claude Opus 5.5 --- ...c2b112d80396455dce0ac293e337e17c3cf58.json | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 evidence/OSC-2/receipts/osc2-arenas-d32a589a78c8238a47ee966e507c2b112d80396455dce0ac293e337e17c3cf58.json diff --git a/evidence/OSC-2/receipts/osc2-arenas-d32a589a78c8238a47ee966e507c2b112d80396455dce0ac293e337e17c3cf58.json b/evidence/OSC-2/receipts/osc2-arenas-d32a589a78c8238a47ee966e507c2b112d80396455dce0ac293e337e17c3cf58.json new file mode 100644 index 000000000..5e926b2e6 --- /dev/null +++ b/evidence/OSC-2/receipts/osc2-arenas-d32a589a78c8238a47ee966e507c2b112d80396455dce0ac293e337e17c3cf58.json @@ -0,0 +1,78 @@ +{ + "schema": "omega.osc2.compiler.receipt.v1", + "verdict": "PASS", + "item": "arenas", + "statement": "OSC-2 slice; not a general Omega compiler; no self-hosting.", + "subset": "integer scalars u8 u16 u32 u64 (wrap) and i8 i16 i32 i64 (checked, trap on overflow), bool; let / let mut; arithmetic, bitwise, shift, compare, logical (short-circuit), checked as-casts; if/else; while with static bound N (trap past N) and for over literal ranges; calls to earlier functions in the unit (no recursion, <= 6 params); unique allocation own [T; N] (N <= 64), moves, shared and mutable borrows with lexical lifetimes, bounds-checked indexing, deterministic destruction at scope end; OSC-2 item 1: requires/ensures are pure bool expressions over parameters (and result in ensures; element reads through parameters, ensures only via shared borrows; no calls), refused statically when constant folding decides them false (CONTRACT_VIOLATION), otherwise checked at run time (TRAP REQUIRES = 9 at entry, TRAP ENSURES = 10 at each return before releases; statically true clauses elided); OSC-2 item 2: unit-level struct declarations (<= 16 structs, 1..16 fields of integer, bool or [T; N], <= 64 cells, declared before use, no recursion) with a fixed layout of 8-byte cells in declaration order (no padding, bound into the IR digest), struct literals initialising every field exactly once in let own, field reads and writes (array fields bounds-checked, TRAP BOUNDS), moves, own / & / &mut parameters with the array borrow rules, deterministic destruction with the same alloc/release events; requires reads fields through any struct parameter, ensures only through a shared & parameter; returning a struct is refused (UNSUPPORTED); OSC-2 item 3: arena blocks 'arena r bound K { ... }' (K = 1..64 cells) wired to the OSC-0B region model; 'let x: own [T; N] in r = alloc(v);' and 'let s: own S in r = S { .. };' allocate from the arena (bump allocation, never released one by one); at block end the unique owners declared in the block are released first, then REGION_DESTROY frees the whole arena (nested: inner first); a borrow of an arena object outliving the arena is ARENA_ESCAPE (the model rejection arena-escape), every move into or out of an arena object is ARENA_MOVE (slice restriction stricter than the model), a definite over-capacity allocation is ARENA_CAPACITY, otherwise TRAP ARENA_FULL = 11 at run time; REGION_OPEN / ARENA_ALLOC / REGION_DESTROY are logged in the runtime event log by both engines, and the log of every native run in the test replays through the OSC-0B model (executed runs only, not a proof for all programs)", + "pipeline": "surface text -> lexer/parser -> typed AST -> name/type + ownership/borrow analysis -> typed IR (canonical encoding + sha256) -> AArch64 via src/compiler/osc_a64 (validating encoder, decoder mirror)", + "repo_commit": "94c45b3329bc8c6c6fb1ab4fd4ed4c11fff7edb8", + "base_commit": "845dce4d53f04bb929b83ebc5cf59c064f16d731", + "tree_clean": true, + "host_arch": "aarch64", + "c_compiler": "cc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0", + "osc0b_model": {"seed": "0x05c0b5eed0010001", "sequences": 1000000, "steps": 21905422, "false_accepts": 0, "wrong_name": 0, "false_rejects": 0, "result": "OSC0B_MODEL_PASS"}, + "backend": "units=15 funcs_fuzzed=361 diff_runs=722000 words_roundtrip=13027 enc_random_ok=45779 enc_random_refused=126221 dec_random_words=76497 validator_neg=49; checks=799002 failed=0; native == interpreter on every run", + "compiler": "golden=37/37 funcs=129 entries=92 expect_runs=100 diff_runs=115136 trace_replays=89 rt_replays=70929 rt_events=351214 neg=96/96 neg_traced=18 checks=319518 failed=0", + "golden_trap_coverage": {"none": 73471, "overflow": 3408, "div0": 23, "bounds": 5986, "loop_bound": 2100, "cast": 1499, "oom": 365, "shift": 2805, "runtime": 0, "requires": 1793, "ensures": 2135}, + "contract_fuzz": "contract fuzz: units=200 compiled=174 static_refused=26 runs=11136 ok=882 requires=5435 ensures=4384 overflow=435 other_traps=0 mismatches=0; native == interpreter on every run", + "contract_fuzz_asan": "contract fuzz: units=200 compiled=174 static_refused=26 runs=11136 ok=882 requires=5435 ensures=4384 overflow=435 other_traps=0 mismatches=0", + "struct_fuzz": "struct fuzz: units=125 runs=6000 ok=3805 bounds=1087 requires=155 overflow=224 cast=729 mismatches=0; native == interpreter on every run", + "struct_fuzz_asan": "struct fuzz: units=125 runs=6000 ok=3805 bounds=1087 requires=155 overflow=224 cast=729 mismatches=0", + "struct_layout": "struct layout: L offsets 0 1 4 5 7 cells 8; M cells 64; digest binds layout", + "struct_destruction_order": "struct destruction order: alloc 1 2 3, release 3 2, alloc 4, release 4 1 (interp == native)", + "arena_fuzz": "arena fuzz: units=125 allocs=557 runs=6000 ok=2542 arena_full=2606 bounds=639 overflow=213 mismatches=0; native == interpreter on every run", + "arena_fuzz_asan": "arena fuzz: units=125 allocs=557 runs=6000 ok=2542 arena_full=2606 bounds=639 overflow=213 mismatches=0", + "arena_destruction_order": "arena destruction order: alloc 1, open 2, aalloc 3, open 4, aalloc 5, alloc 6, release 6, destroy 4, alloc 7, release 7, destroy 2, release 1 (interp == native)", + "runtime_model_replay": "runtime model replay: runs=115238 events=351214 accepted=115238 rejected=0; every native run of the test (golden fuzz, expect-run lines, contract, struct and arena fuzz, destruction-order tests) replays its runtime event log through the OSC-0B model, trapping runs as the logged prefix; this checks executed runs, not all programs", + "runtime_model_replay_asan": "runtime model replay: runs=115238 events=351214 accepted=115238 rejected=0", + "arena_full_trap_runs": 4415, + "determinism": "programs=37 identical=37 (oscc in separate processes, byte-identical IR digest and machine code)", + "sanitizers": "all three test binaries also pass under -fsanitize=address,undefined -fno-sanitize-recover=all (model sweep at 10^5)", + "golden_programs": 37, + "negative_programs": 96, + "golden_corpus_sha256": "f8a4be89ebf700e778aa154001148651491fc9b07384d031560f2a63130710eb", + "golden_corpus": [ + {"program": "tests/compiler/progs/arena_basic.osc", "source_sha256": "4d7572fef4094024a1f8e84aa54f29e95c6d34c65bce9bfe9d171a0c6015236d", "ir_sha256": "c1cbf604876cbc775f118dd68b867836197a9fd82ea133aacbea3672b8016f3e", "code_sha256": "3fb18af64f18ec3f52d1c5bc600d13af869282a2e56160a9972a6d7a54e54623"}, + {"program": "tests/compiler/progs/arena_borrows.osc", "source_sha256": "a90474fb7d52cf9d24db60c5752aba7db3dbbb2d1527b51a81410522e05bd96c", "ir_sha256": "b2b2737fd79346c4881c63053eeb7c8a493ece93c733b254986f1b88ac39c4b3", "code_sha256": "69209e13ee4f303a9177c8ff8560291da04c6613e0a6bbb7ff25167270cc2ce8"}, + {"program": "tests/compiler/progs/arena_dtor.osc", "source_sha256": "59bb1cc1a7a3d94852581a8465e290f7fc806f82c0ed0e5d5614f724cd47a2db", "ir_sha256": "0853cf1d750b7aa7f89bedce30bab8abed21b7c03688b3ec1d149ed6a4ff1ecc", "code_sha256": "39e09aa8abe6d0527d1bcdf4769099f5e4fb35783949f436b20b25ff29e33a59"}, + {"program": "tests/compiler/progs/arena_full.osc", "source_sha256": "048b757f8e0997ec6eacc1be7065f10a9621eb8147c3a638faff6883b50db7d6", "ir_sha256": "2095deb65522e3782d66abeb601a02fd4f16d76d05bfec32345931f970c0f3bc", "code_sha256": "c58d9cf04f27ea71370cc0fdfe903113ef4449db1d1e8c265adf4a5f5488f902"}, + {"program": "tests/compiler/progs/arena_nested.osc", "source_sha256": "6fbcb009abcd1e47188ccac0adadf4c2a110e060f44028ef5194e637f6b59659", "ir_sha256": "0d910fe9717796b0f1a3afe70e3ad8af94bb9d43675bbc3568635b0f175ec81d", "code_sha256": "6a45e3becc6c0179bab0eeffb4864795a5cf1d12163ac738272ec10d2e3a008e"}, + {"program": "tests/compiler/progs/arena_structs.osc", "source_sha256": "2ccd4e8a6d4eab0e15f1018545ba4604e54dd2f85b21aed38711c3d34a0cf1a4", "ir_sha256": "f9029fbc626e4a06b12f928b60ec3739c935c0d6afbafe0df9daba7580312bfc", "code_sha256": "c1d5a138f01a4625b2eafc626f3d35f4476189f89fd38d9e777345aa0dee65d1"}, + {"program": "tests/compiler/progs/arrays.osc", "source_sha256": "0d41e301111e2c132ceba7d7272eb42ae0d1b1cb0341c4f76d3fec2ce38a6ee8", "ir_sha256": "3c3b6e50085dd82cde7ec97d2feb580d38b714f8cb4f60db35b9a4f36530df0d", "code_sha256": "e6b45cce88509c39fa0503302691906d79e4189de57244a9bc1036ba15c91ac5"}, + {"program": "tests/compiler/progs/bitrev.osc", "source_sha256": "7a831a9f47434a76b65bdf837fecaf96f4a85d2c31c4682b280301b8aa588d1c", "ir_sha256": "e3aa5e5e5c58fbc686025370e8c87eceb5c4805b2a9d21d55bfbf7bfdba6cc36", "code_sha256": "94674978c5ca8c96ccc0fed847668ab4b73c0dd1f3f7527a613b26cd307c2362"}, + {"program": "tests/compiler/progs/bounds.osc", "source_sha256": "0519478f54c98ca06f3414ff9ac5dc4193ed5b2b43a7a89b2333ef3bb54b3094", "ir_sha256": "7e7bb041550f9a5a4445c0e028650f6e0cf9afcea61819a95e29b92eca13d966", "code_sha256": "07bb15370990980cd1b27be4ee87612133a58463e305e067870b6668ebc9a2d9"}, + {"program": "tests/compiler/progs/calls.osc", "source_sha256": "add5b82ec5ea73b322e9b1620220f9ff73ba9f76aeff031e0654980e2f36bcbc", "ir_sha256": "9d50bea67d3928a457201dfe35541b912b9acfbdaf97e901e7ea2e0f3185524d", "code_sha256": "a6434af2275da292a0a573b355cab439bfb26f85be6b0b0dcdc709edc66d7a94"}, + {"program": "tests/compiler/progs/casts.osc", "source_sha256": "a5093b32ee0d204d20920e5fd8f8c39c8e10b92c89aabb41fb703e7e28637fb3", "ir_sha256": "cc35e86f43ba0ebe29ea6906c0af5e01fa30964c75b0a69b05370c4100ae80b3", "code_sha256": "fdff78f367ea211da5e5058ffe715ea7fb14a1bee2727e69aefd9fda2b27bece"}, + {"program": "tests/compiler/progs/collatz.osc", "source_sha256": "912412fa1ffd990786a055d2b43a35f9fd629fa59299b1181463f3b314cd741f", "ir_sha256": "fdf94380724fa0994a074cbed2cf789c3ed4dfeb14450e648b9144ada4e57edd", "code_sha256": "9b06e959179f262f45ef72015ee1001a026262754804c107b640813131e014bf"}, + {"program": "tests/compiler/progs/contracts_basic.osc", "source_sha256": "8a1370cec47deb9671ef6e4a55a66a94b97d3f34754fa7c64ae741853134bb99", "ir_sha256": "94f2e0f6c9c8c15630725d5105007a7ecdb26212c898219b4d99be340d027ede", "code_sha256": "b7d9a360486ca56847ee83a0dc7731026305a20a9dfd250d2d6bc3197b74a0fc"}, + {"program": "tests/compiler/progs/contracts_ensures.osc", "source_sha256": "4bade10adc6078e9a8cb77b12fd529f0cb733bcf24629018a95402528a0bc3f0", "ir_sha256": "c2a7c5cca6ffd933c90d89d20e0b8dcb5c56fbf831fd9b882ad729de1efefa9f", "code_sha256": "de5746dd6d6c22049987e7bd5bdde6a238fe24785e4c654a852e383a0e657f6a"}, + {"program": "tests/compiler/progs/contracts_requires.osc", "source_sha256": "83f5842fba1f7f027cb655fcff243f1650761afedecb1cd168992a08b7924224", "ir_sha256": "16bc4b6267ee1d4a75cc60d35514baf10cc3f326b3a3e47222ca271587c9dac3", "code_sha256": "599922ff8eff90977309cf593da6cac4f951eb182bdf8a8b156c36ca6c7917ff"}, + {"program": "tests/compiler/progs/factorial.osc", "source_sha256": "3e410e9214a30628b199f7bfe8f99d7e9f439e73af2d279719fa7b523bc74e21", "ir_sha256": "cc36ae8edf9a203d4f82bd1b351b92f2df818427d26239ae8c6542e175674700", "code_sha256": "a3fc5f8c4a5ae8ce6381a0db02a03e391df791a0ce2d6f778face3a30532ecd3"}, + {"program": "tests/compiler/progs/fib.osc", "source_sha256": "9490114a0c756edab0ee7af37252b5883cc7c11b69e54ba5481530b2dcb8e03c", "ir_sha256": "bfe54f4d1502277153becb83faa0fd2975496a1875837fa8e8aa72431aa0ad00", "code_sha256": "d2e25ea89ebd59e49e29303696b7b2bff4ff39d45f62bd5566db609830d153a8"}, + {"program": "tests/compiler/progs/gcd.osc", "source_sha256": "0fad11202f1f6ce6e845b266f24df86cbc0e6160a4772c043a15b60fa54a720f", "ir_sha256": "5d649066a565d7e7ab680ea6b2827306701714da7b23755836814da7cccfa328", "code_sha256": "eb4cbe78da3176853323b2d11b970444c51fb61a33dbc770ec355cc10b94d45d"}, + {"program": "tests/compiler/progs/ifchain.osc", "source_sha256": "89fd46fc610516121dbd38cca232a98abccb55abf1593409ca575ab7e293699e", "ir_sha256": "2323f2753d8e2eb269d65db1b8e9a6006b0e09ed98e0f3f74268143cf870342d", "code_sha256": "f7c3aa9e0ae96c5d1a1c0fbcb44b7504843aac8ed5035b2400d50819cc999e03"}, + {"program": "tests/compiler/progs/isort.osc", "source_sha256": "7dceeaf139addf172152b8861602d02d654e5d2f9fab451d2c38d823ddd1b7db", "ir_sha256": "fe5d0090baeb54d9afd33a9745b7a60136e513aaca0c03f5275d5efab4de3091", "code_sha256": "b99188f630088cd1c48b4e57d48431982eb3671c50fbe5ea42e17db680baf100"}, + {"program": "tests/compiler/progs/loopbound.osc", "source_sha256": "71def740b4af102745e954e5d06582cf28b5e90e633a254a3f248bd1ed8c10c3", "ir_sha256": "d5994fccb2c93ba53d8e3a73222aa54f28f2159509f992167c8e77e779734db2", "code_sha256": "e8b5a7ca1240adbcfc66c6e1e117b01ad692c4faed32381742e0ba9a7313bd83"}, + {"program": "tests/compiler/progs/minmax.osc", "source_sha256": "95c17e1cb5ffc836ef2bcf16ab39246b5c0bbb8acb1c05a6e08b838a12b60b7e", "ir_sha256": "a5ee50c4f631cfda3282c6a6a42842f20986ae4129fa4e22656ea836c8a01543", "code_sha256": "387756318095cc219eec337923bf12440927b254cac189872b17ea617f17ae41"}, + {"program": "tests/compiler/progs/nested.osc", "source_sha256": "8e9aaf037c142ea43dcc4e9013013688eb19f03f24a9e8511058623ced32dc35", "ir_sha256": "94e13f928a06a8d2ddff59030bb412cfcb5e4888570f9b7fc44786c135415c04", "code_sha256": "0eb5b788611a907d360730c08b78c6b0cfbf3a454323b42808f77f8aed300e71"}, + {"program": "tests/compiler/progs/oom.osc", "source_sha256": "1d82ff5262c21e4760b2404dc8fc649863a35ac4a164e4ae6684adcd7cecfc7e", "ir_sha256": "6ce4fcbb5b507668183dd67678a93dad7fe11d7b3f02c2466815712fb300116c", "code_sha256": "5c788e2eef2be25b04013f7bd8378d715756293c36f9509c14e9d5d465e5552a"}, + {"program": "tests/compiler/progs/ops.osc", "source_sha256": "2f68f1cf7d7d4569c90d79e4afc2bbdb9cf1bc2fc39cc8f8e23555c90cdd0223", "ir_sha256": "9742eb592d1cfcd72c622d2ab28fe1dd1fa921f14cd3418662f303be851e1162", "code_sha256": "0c038096a7f33e702937c2979eba15d6fc5a25dd66ba12c5b746df8529e7a959"}, + {"program": "tests/compiler/progs/popcount.osc", "source_sha256": "eab7407d3cfc923d15f52e04e00d96a9e7331b0e2496552b8258e7f88bc65236", "ir_sha256": "15aa9536feb7cbbe9cd03efcf2517321964bb479c0331f45610225c64af186ac", "code_sha256": "04510e1bf63889d5dd0bb7b85eeffb7b016ad87a65a7e0e2209df082f1a9d1db"}, + {"program": "tests/compiler/progs/prefix.osc", "source_sha256": "a4852da0953c8e11e5a9e51d00be81a6cc9e1f9e938cbd9d186f34ccb53cc524", "ir_sha256": "6feaa9af48e493a02a3de461aeae3b88b1c813c9011a462537e88c661704ee63", "code_sha256": "f4c78d8644d86ce7f1edd36ebd6958d5250a34dedde3435587e34dfb2d474362"}, + {"program": "tests/compiler/progs/reborrow.osc", "source_sha256": "caf5e895fcd0660d6ca7c6360228901cb4eee5a7afe128b59334aec904e63feb", "ir_sha256": "34086f590aeaec09669b9656cfca7db57beffb67a59d7e1ba22548336de146b2", "code_sha256": "a6a0a251ed5ddcbc431e38ed73602ee9202fd2d601bd7085ae70ccb5974daf62"}, + {"program": "tests/compiler/progs/scopes.osc", "source_sha256": "9904f63cb89734f2411f04b098640cb0ebe8069a49ae7b1a40c728d434770070", "ir_sha256": "81e996a3c8c6515d57375609aba9ae7c6c5822626b1a8da9cd0943b70db4a1ed", "code_sha256": "e3faba53225b4f3a9398b5d39f6a224b24df08cf9b6a67ca5652a439f0717127"}, + {"program": "tests/compiler/progs/shifts.osc", "source_sha256": "dd33980fb0f3b8d61477b6c197508e3c46d894c0ebc0f99244c01b5e86ee8780", "ir_sha256": "7d2e542f2241ccfacbd6a812c1671733dda94410ede4e3847b43c0955a6f69b3", "code_sha256": "c07f4547cac6e250b1c76021f0955fd530844fcc593a33192592ee0f917ecf59"}, + {"program": "tests/compiler/progs/shortcircuit.osc", "source_sha256": "c3f272b3f6b6d27c9cde9887197e73dd622ea30ff802fb87c250a5e2f704e1d0", "ir_sha256": "863365c4653fb44aac42316b1cde491107ad739d58b7af5b4b55e3a2774c5d41", "code_sha256": "bbd44412068130fdf65d66f718f74c6e0e7e36ee19282f19e5c2f3bbcf66ba2e"}, + {"program": "tests/compiler/progs/sixparams.osc", "source_sha256": "535fead297b7c4b7946c25b0b1f716634b4342701c3ff486dbd8189ed8bc3770", "ir_sha256": "176dee2ab6b66ff28dc04f095964c51f59dcda5518a9696a95a9c588dd78083e", "code_sha256": "c1e8cf802ef21c2b08b08dd266303c715e64ab7323cb5267fe2492de1e54cee8"}, + {"program": "tests/compiler/progs/structs_contracts.osc", "source_sha256": "06573d57946360c9e65090b61696a005ffe3fd3f3c1ca117765d9a3028a6f43a", "ir_sha256": "06b233f9e16e229fc606c8bda44b24e43473a2cd376053341e6ee644e124fe99", "code_sha256": "d52177fe222b3e548cc1df3bb294d524b41bcb093b64e8fc41f153094eacebf0"}, + {"program": "tests/compiler/progs/structs_dtor.osc", "source_sha256": "71f951fae6f1471dc52c5edb43abfa7b7692835e0d9fe8c63ae18a2e44e97ad5", "ir_sha256": "c7b4d22c623f0b1c4d004474c532ca74cbf23911cdcd299ad81843f4ef9a27df", "code_sha256": "3d73cda95a76bd70f7a5e4aa7673a360b2c40423381db88cabadd3bbfe363b0d"}, + {"program": "tests/compiler/progs/structs_fields.osc", "source_sha256": "4da454a3b5633ddec4f39f35c67236b26b87dde922d829ff5eb39df2195b5f2e", "ir_sha256": "865a9a09510cf16363e38345c47544ac1c9b48f7eddec67729f7e1ca40d03461", "code_sha256": "69be8bd0763aec308f5a002820ae496bc86dd1ac93111e532790dab27dd10586"}, + {"program": "tests/compiler/progs/structs_owner.osc", "source_sha256": "babf04cbcfbece1e1c4c4d23c59b97fb7644f639f77d987d6732c613313170cc", "ir_sha256": "11dd47aca0387de3b7fc3f546272b165c9f2d2675626dda03a255a4dd4942807", "code_sha256": "3e1df27680d16d47f1c2568d9e7f3f066ef29eb13e0faa89a16e12c182ec3da7"}, + {"program": "tests/compiler/progs/types.osc", "source_sha256": "ca4d49a4aca05b04e9935dd7cc560a023c4e4cffa36ff84c7755883a90c2b0d5", "ir_sha256": "f623556285814fbd02241fe4f24025bf93811581655beab6073e8d867b42b3a5", "code_sha256": "45c042eeb15cebf30615b7ad341b58a41bd805d83a08c358304b4284785eee35"} + ], + "omega_core_sources_unchanged_vs_base": true, + "m6_m9_m14_gates": {"m6": {"total": 10, "passed": 10, "failed": 0}, "m9": {"total": 10, "passed": 10, "failed": 0}, "m14": {"total": 10, "passed": 10, "failed": 0}}, + "self_host": "no: the OSC-2 compiler slice cannot compile any part of itself (docs/osc/OSC-1-SELF-HOST-STATEMENT.md)", + "not_covered": "moving arena objects (all arena moves refused: slice restriction, stricter than the model), arena handles as values or parameters, arenas outlasting their block, per-object release inside an arena, struct return values, struct-typed and owner/borrow fields, recursive structs, whole-struct copy or compare, contracts beyond constant folding (no symbolic proof), effects/capabilities, strings, generations, unsafe physical, FFI, Flow IR optimisation, self-hosting (OSC-14); the runtime model replay checks executed runs only and is not a proof for all programs; not a general Omega compiler", + "timing_measured": false +}