omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
GitHub aien-dev/omega-mv#175 (code only)
Fetch it
curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/5114f07256863f82.bundle -o 5114f07256863f82.bundle git fetch 5114f07256863f82.bundle refs/pull/175/head
Provenance
github-import 1
aien-dev/omega-mv
175
2ee96c66d9023eb76e3326190d685d8f5866501f
{"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":175,"url":"https://github.com/aien-dev/omega-mv/pull/175","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:39Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/docs/osc/OSC-1-DESIGN.md b/docs/osc/OSC-1-DESIGN.md index 9be6cf84d..e93b500d4 100644 --- a/docs/osc/OSC-1-DESIGN.md +++ b/docs/osc/OSC-1-DESIGN.md @@ -127,7 +127,7 @@ argument for the duration of a call) holds a borrow of one or more owners. a move, any use of `a` (read, index, borrow, move) is refused: **use after move**. A move inside a loop body of an owner declared outside that loop is refused (use after move on the next iteration). A move on only some paths of - an `if` is refused (`conditional move`): OSC-1 has no drop flags. + an `if` is refused (`conditional move`): OSC-1 has no drop flags (superseded by OSC-3 item 3, drop flags). - Shared borrow `&a`: allowed while `a` is owned and not mutably borrowed. - Mutable borrow `&mut a`: `a` must be owned and not borrowed at all; through a borrow binding `r: &mut`, `&mut r` is a reborrow of the same target. diff --git a/docs/osc/OSC-3-DESIGN.md b/docs/osc/OSC-3-DESIGN.md index 7b3f23d08..abafc0c9f 100644 --- a/docs/osc/OSC-3-DESIGN.md +++ b/docs/osc/OSC-3-DESIGN.md @@ -185,6 +185,106 @@ runtime model replay: ... rejected=0 checked by the item 2 receipt against the OSC-2 baseline). - The exact receipt is under `evidence/OSC-3/receipts/osc3-handles-*.json`. +## Item 3: drop flags (conditional moves) + +OSC-1 and OSC-2 refused any move of an owner inside an `if` +(`CONDITIONAL_MOVE`, OSC-1-DESIGN "no drop flags"). Item 3 accepts it: an +owner that is moved on some paths and not on others gets a hidden drop flag, +and its scope-end release runs only when the flag says live. This supersedes +the OSC-1 rule; `CONDITIONAL_MOVE` keeps its number but is no longer emitted. + +### Subset + +- Owners are unique array owners (`own [T; N]`) and unique struct owners + (`own S`), declared by `let` (alloc, struct literal or let-move) or as an + `own` parameter. Arena owners (`in r`) are unchanged: they cannot be moved + (`ARENA_MOVE`), so they never need a flag. Handles (item 2) own nothing + and are out of scope. +- A move is a let-move (`let b: own T = a;`) or passing the owner as an + `own` argument. Either may now sit inside one branch of `if` / `else` / + `else if`, at any nesting depth. +- After an `if`, each owner declared before it is in one of three states: + live (moved on no path that falls through), moved (moved on every path + that falls through) or maybe-moved (moved on some). A branch that ends in + `return` does not count: its state never reaches the join. +- Only maybe-moved owners get a flag. Programs without a conditional move + compile to byte-identical IR and code (no `OSC3_CODE_CHANGE_OK`). + +### Grammar + +No new syntax. The grammar of OSC-2 is unchanged; programs that OSC-2 +refused with `CONDITIONAL_MOVE` are now accepted when the rules below hold. + +### Semantics + +- Merge rule at the end of an `if` (both arms falling through): equal + states stay; any difference (live vs moved, live vs maybe, moved vs maybe) + becomes maybe-moved. If one arm returns, the other arm's state is taken. +- Flag: one `bool` virtual register per flagged declaration, allocated by + the lowerer when the checker marks the declaration (`OscNode.dflag`). It + is set to 1 (`CONST 1`) right after the declaration (for an `own` + parameter: first thing in the entry block), and set to 0 (`CONST 0`) at + every move of that owner (before the `CALL` for an own argument, before + the new binding for a let-move), on every path. +- Release: every scope-end release of a flagged owner (block end and every + `return` it is live or maybe-moved at) becomes + `CBR flag -> release, join; release: RELEASE owner; BR join`. Owners that + are definitely moved are not released (as in OSC-2); live owners without + a flag are released unconditionally (as in OSC-2). Release order stays + reverse declaration order; a skipped release simply drops out. +- Loop-local owners: an owner declared inside a loop body may be moved on + one path of that body; its declaration sets the flag again each + iteration, so each iteration's object is released at most once. +- Frame layout: the flag is an ordinary vreg, so it has the ordinary stack + slot of the native frame, `[sp, #8*(v+1)]` (`osc_cg.c slot()`), holding + 0 or 1 in a 64-bit cell; non-parameter slots are zeroed by the prologue + and the declaration's `CONST 1` sets it. No new IR op, no interpreter or + backend change: `CONST`, `CBR`, `BR` and `RELEASE` already exist, so both + engines run the same IR and agree by construction. +- Static model trace: around the `if` the checker emits `SAVE`/`RESTORE` + as before. A definitely moved owner gets its `MOVE obj -> 0` re-emitted + after the `if` (unchanged). A maybe-moved owner emits nothing after the + `if`: on the straight-line trace it stays live and its guarded scope-end + `RELEASE` replays as a plain release (the trace follows the path on which + the flag is set). Each run's runtime log replays exactly (a release that + did not run is simply absent). +- Soundness argument: the flag equals "this owner's object has not been + moved" on every executed path, because it is set at the (re)declaration + and cleared at each move, and every release of a flagged owner tests it. + So no path releases twice (no `RUNTIME` double-release trap) and no path + leaks (live count 0 after a normal return); both are checked on every + fuzz run in both engines. + +### Static refusals (named) + +| Diagnostic | Transition | When | +|---|---|---| +| `USE_AFTER_MOVE` | use after maybe-move | read, write, field access or call use of a maybe-moved owner ("used after it may have been moved at line N (moved on some path of an 'if')") | +| `USE_AFTER_MOVE` | use after maybe-move | borrow (`&` / `&mut`) of a maybe-moved owner | +| `USE_AFTER_MOVE` | use after maybe-move | moving a maybe-moved owner again (double move across branches / two ifs) | +| `USE_AFTER_MOVE` | use after move (move inside loop) | an owner declared outside a loop moved inside it, also inside an `if` in the loop (unchanged from OSC-1) | +| `IMMUTABLE_ASSIGN` | assign to immutable binding | re-assigning an owner, maybe-moved or not ("owners are never reassigned"); item 3 does not add a set-live rule | + +For the model the maybe-move refusals are use-after-move: the checker emits +an accepted `MOVE obj -> 0` (the path that moved it) right before the refused +event, so the negative harness replays them as `use-after-move`. + +### Limits + +- Flags exist only for owners moved inside `if` arms; moves of an outer + owner inside a loop stay refused (no re-initialisation rule). +- No reassignment of owners (refused), so a flag only goes live -> moved + within one declaration's lifetime. +- One flag per declaration (up to the checker's symbol limit); flags are not + packed into bits. +- The static trace checks the path on which each maybe-moved owner is still + live; the moved path is checked by every executed run's runtime replay + (fuzz and goldens), not statically. + +### Evidence + +EVIDENCE_PLACEHOLDER + ## Generation width: remaining 32-bit sites and fix plan ADR OMEGA-SYSTEMS-CORE-0000 decision 1 fixes capability and object generations diff --git a/src/compiler/osc_check.c b/src/compiler/osc_check.c index ae92a6c54..1a9b7dd27 100644 --- a/src/compiler/osc_check.c +++ b/src/compiler/osc_check.c @@ -40,6 +40,7 @@ typedef struct { uint16_t dlive; uint8_t cap_off; int pool, prov; + int decl; /* OSC-3 item 3: declaring AST node (owners: carries the drop-flag mark) */ } Sym; /* OSC-3 item 2: handle state saved around branches and loop bodies */ @@ -49,8 +50,13 @@ typedef struct { int nprov, nsym; } HSave; +/* Obj.moved. OSC-3 item 3 (drop flags) adds OM_MAYBE: moved on some but not + * all paths that reach the current point. Such an owner cannot be used, + * borrowed or moved; at scope end its release is guarded by a drop flag. */ +enum { OM_LIVE = 0, OM_MOVED = 1, OM_GONE = 2 /* released (scope gone) */, OM_MAYBE = 3 }; + typedef struct { - uint8_t moved; + uint8_t moved; /* OM_* */ uint16_t n_shared, n_mut; uint32_t move_line; int sym; /* owning symbol or -1 (stand-in / dummy) */ @@ -248,6 +254,17 @@ static int diag_own(C *c, OscDiagKind k, uint32_t line, uint32_t col, int s, con return -1; } +/* OSC-3 item 3: a maybe-moved owner (OM_MAYBE) is refused exactly like a + * moved one. Its trace records the path on which it was moved (MOVE obj -> 0, + * accepted) before the refused event, so the refusal replays through the + * model as use-after-move. Returns 1 for a maybe-moved owner. */ +static int maybe_path(C *c, const Sym *y) +{ + if (c->obj[y->obj].moved != OM_MAYBE) return 0; + tr_ev(c, OSC_EV_MOVE, y->obj, -1, -1, -1, c->obj[y->obj].move_line, 0); + return 1; +} + /* direct or via-borrow access of symbol s (owner or borrow binding). wr: write. */ static int access(C *c, int s, int wr, uint32_t line, uint32_t col) { @@ -257,9 +274,10 @@ static int access(C *c, int s, int wr, uint32_t line, uint32_t col) Obj *o = &c->obj[y->obj]; uint32_t k = wr ? OSC_EV_USE_WRITE : OSC_EV_USE_READ; if (o->moved) { + int mb = maybe_path(c, y); tr_ev(c, k, y->obj, -1, -1, -1, line, 1); - snprintf(msg, sizeof msg, "'%s' used after it was moved at line %u", y->name, o->move_line); - return diag_own(c, OSC_DIAG_USE_AFTER_MOVE, line, col, s, NULL, "use after move", msg); + snprintf(msg, sizeof msg, mb ? "'%s' used after it may have been moved at line %u (moved on some path of an 'if')" : "'%s' used after it was moved at line %u", y->name, o->move_line); + return diag_own(c, OSC_DIAG_USE_AFTER_MOVE, line, col, s, NULL, mb ? "use after maybe-move" : "use after move", msg); } if (o->n_mut || (wr && o->n_shared)) { conflict_name(c, y->obj, -1, !wr, other, sizeof other); @@ -300,9 +318,10 @@ static int take_borrow(C *c, int s, int mut, uint32_t line, uint32_t col) if (y->kind == SK_OWNER) { Obj *o = &c->obj[y->obj]; if (o->moved) { + int mb = maybe_path(c, y); tr_ev(c, k, y->obj, -1, c->nbor, -1, line, 1); - snprintf(msg, sizeof msg, "borrow of '%s' after it was moved at line %u", y->name, o->move_line); - return diag_own(c, OSC_DIAG_USE_AFTER_MOVE, line, col, s, NULL, "use after move", msg); + snprintf(msg, sizeof msg, mb ? "borrow of '%s' after it may have been moved at line %u (moved on some path of an 'if')" : "borrow of '%s' after it was moved at line %u", y->name, o->move_line); + return diag_own(c, OSC_DIAG_USE_AFTER_MOVE, line, col, s, NULL, mb ? "use after maybe-move" : "use after move", msg); } if (o->n_mut || (mut && o->n_shared)) { conflict_name(c, y->obj, -1, !mut, other, sizeof other); @@ -344,9 +363,10 @@ static int do_move(C *c, int s, int dest, uint32_t line, uint32_t col) Obj *o = &c->obj[y->obj]; char other[64], msg[160]; if (o->moved) { + int mb = maybe_path(c, y); tr_ev(c, OSC_EV_MOVE, y->obj, dest, -1, -1, line, 1); - snprintf(msg, sizeof msg, "'%s' moved again after it was moved at line %u", y->name, o->move_line); - return diag_own(c, OSC_DIAG_USE_AFTER_MOVE, line, col, s, NULL, "use after move", msg); + snprintf(msg, sizeof msg, mb ? "'%s' moved again after it may have been moved at line %u (moved on some path of an 'if')" : "'%s' moved again after it was moved at line %u", y->name, o->move_line); + return diag_own(c, OSC_DIAG_USE_AFTER_MOVE, line, col, s, NULL, mb ? "use after maybe-move" : "use after move", msg); } if (o->n_shared || o->n_mut) { conflict_name(c, y->obj, -1, 0, other, sizeof other); @@ -400,6 +420,7 @@ static int declare(C *c, const OscNode *n, uint8_t kind, const OscType *ty, int y->ty = *ty; y->loop_depth = c->loop_depth; y->obj = y->bor = -1; + y->decl = (int)(n - c->ast->nodes); c->vis[c->nvis++] = s; return s; } @@ -447,7 +468,9 @@ static int scope_exit_events(C *c, int levels, uint32_t line, OscNode *rec) 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 && !o->arena1) { + /* OSC-3 item 3: a maybe-moved owner is listed too; the lowerer + * guards its release with the owner's drop flag */ + if ((o->moved == OM_LIVE || o->moved == OM_MAYBE) && !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++; } @@ -1690,18 +1713,19 @@ static int chk_if(C *c, int i) } else if (!t1) { /* OSC-3 item 2: freed only if freed on both paths */ for (int p = 0; p < h0->nprov; p++) c->pfreed[p] = (uint8_t)(c->pfreed[p] && h1->pfreed[p]); + /* OSC-3 item 3 (drop flags): an owner moved on one path and not (or + * only maybe) on the other is maybe-moved after the `if`. Its + * declaration gets a drop flag: set at the declaration, cleared at + * every move, tested by its scope-end release. Both paths moved: + * definitely moved (no flag). Neither: unchanged. Pre-existing + * objects are never OM_GONE on a path that falls through. */ for (int o = 0; o < nobj0; o++) { - int m1 = s1->obj[o].moved == 1, m2 = c->obj[o].moved == 1; - if (m1 != m2) { - int sym = c->obj[o].sym; - uint32_t ml = m1 ? s1->obj[o].move_line : c->obj[o].move_line; - char msg[320]; - snprintf(msg, sizeof msg, "'%s' is moved on only one path of the 'if' at line %u (no drop flags)", - sym >= 0 ? c->sym[sym].name : "?", n->line); - osc_diag_set(c->d, OSC_DIAG_CONDITIONAL_MOVE, ml, 0, sym >= 0 ? c->sym[sym].name : "?", - sym >= 0 ? c->sym[sym].line : 0, "if", "conditional move", "%s", msg); - goto out; - } + int m1 = s1->obj[o].moved, m2 = c->obj[o].moved; + if (m1 == m2) continue; + if (m2 == OM_LIVE || (m2 == OM_MOVED && m1 == OM_MAYBE)) c->obj[o].move_line = s1->obj[o].move_line; + c->obj[o].moved = OM_MAYBE; + int sym = c->obj[o].sym; + if (sym >= 0) NODE(c->sym[sym].decl)->dflag = 1; } } c->term = 0; diff --git a/src/compiler/osc_lower.c b/src/compiler/osc_lower.c index 3eede7cc2..c90b8d7d2 100644 --- a/src/compiler/osc_lower.c +++ b/src/compiler/osc_lower.c @@ -28,6 +28,11 @@ typedef struct { * vreg r is its slot and r + 1 its generation. */ uint8_t ispool[OSC_CHECK_MAX_SYMS]; uint8_t ishandle[OSC_CHECK_MAX_SYMS]; + /* OSC-3 item 3: drop-flag vreg (bool) of an owner symbol whose + * declaration the checker marked (dflag), -1 otherwise. 1 = live, + * 0 = moved. Set at the declaration, cleared at every move, tested by + * the owner's scope-end release (CBR around RELEASE). */ + int16_t dfv[OSC_CHECK_MAX_SYMS]; } L; #define NODE(i) (&l->ast->nodes[(i)]) @@ -101,14 +106,43 @@ static int kconst(L *l, OscScalar s, uint64_t v, uint32_t line) return r; } +/* OSC-3 item 3 drop flags: write imm (1 live / 0 moved) into owner s's flag */ +static void dflag_set(L *l, int s, uint64_t imm, uint32_t line) +{ + if (s < 0 || l->dfv[s] < 0) return; + OscInsn *x = emit(l, OSC_I_CONST, line); + x->dst = l->dfv[s]; + x->imm = imm; +} +/* owner s just declared by node n: give it a flag (set live) if marked */ +static void dflag_decl(L *l, int s, const OscNode *n) +{ + if (!n->dflag) return; + l->dfv[s] = (int16_t)newvs(l, OSC_T_BOOL, n->line); + dflag_set(l, s, 1, n->line); +} + static void releases(L *l, const OscNode *n) { + if (l->cur < 0) return; /* dead code: nothing to release (emit would drop it) */ for (uint32_t k = 0; k < n->rel_count; k++) { int s = l->ast->rel[n->rel_start + k]; /* OSC-2 arenas: an arena symbol's vreg is its u64 handle (owners are REFs) */ int arena = l->f->vtype[l->vreg[s]].s != OSC_T_REF; + int guard = !l->ispool[s] && !arena && l->dfv[s] >= 0 ? l->dfv[s] : -1; + int rb = -1, jb = -1; + if (guard >= 0) { /* OSC-3 item 3: release only if the drop flag says live */ + rb = newblk(l, n->line); + jb = newblk(l, n->line); + cbr(l, guard, rb, jb, n->line); + l->cur = rb; + } OscInsn *x = emit(l, l->ispool[s] ? OSC_I_PCLOSE : arena ? OSC_I_ADESTROY : OSC_I_RELEASE, n->line); x->a = l->vreg[s]; + if (guard >= 0) { + br(l, jb, n->line); + l->cur = jb; + } } } @@ -154,6 +188,12 @@ static int call(L *l, int i, int want_value) args[na++] = l->vreg[an->sym]; /* own argument: moves the owner */ else args[na++] = expr(l, a); } + /* OSC-3 item 3: an owner passed to an own parameter is moved here */ + for (int a = n->a; a >= 0; a = NODE(a)->next) { + const OscNode *an = NODE(a); + if (an->kind == ON_NAME && an->sym >= 0 && an->sym < OSC_CHECK_MAX_SYMS && l->dfv[an->sym] >= 0) + dflag_set(l, an->sym, 0, n->line); + } const OscNode *g = NODE(l->ast->fns[n->sym]); int dst = -1; if (g->ty.s != OSC_T_VOID && want_value) dst = newvs(l, g->ty.s, n->line); @@ -367,6 +407,7 @@ static void stmt(L *l, int i) x->imm = (uint64_t)fn->hi; } } + dflag_decl(l, n->sym, n); /* OSC-3 item 3 */ break; } int v = expr(l, n->a); @@ -376,6 +417,7 @@ static void stmt(L *l, int i) if (ah >= 0) x->b = (int16_t)ah; x->dst = (int16_t)r; x->a = (int16_t)v; + dflag_decl(l, n->sym, n); /* OSC-3 item 3 */ break; } case ON_LET_MOVE: @@ -385,6 +427,10 @@ static void stmt(L *l, int i) OscInsn *x = emit(l, OSC_I_MOV, n->line); x->dst = (int16_t)r; x->a = l->vreg[n->sym2]; + if (n->kind == ON_LET_MOVE) { /* OSC-3 item 3: the source is moved; the new owner starts live */ + dflag_set(l, n->sym2, 0, n->line); + dflag_decl(l, n->sym, n); + } break; } case ON_ASSIGN: { @@ -604,6 +650,7 @@ static int lower_fn(L *l, int fi) l->nblk = 0; memset(l->ispool, 0, sizeof l->ispool); memset(l->ishandle, 0, sizeof l->ishandle); + memset(l->dfv, 0xff, sizeof l->dfv); /* OSC-3 item 3: no drop flags */ for (int p = fn->a; p >= 0; p = NODE(p)->next) { const OscNode *pn = NODE(p); int r = newv(l, &pn->ty, pn->line); @@ -611,6 +658,9 @@ static int lower_fn(L *l, int fi) f->nparams++; } l->cur = newblk(l, fn->line); + /* OSC-3 item 3: own parameters that may be moved on only some paths get + * their drop flag (vregs after the parameters), set live at entry */ + for (int p = fn->a; p >= 0; p = NODE(p)->next) dflag_decl(l, NODE(p)->sym, NODE(p)); l->fi = fi; if (a->reqn[fi] >= 0 && !a->req_elide[fi]) contract_check(l, a->reqn[fi], OSC_TRAP_REQUIRES); /* body block inlined (same emission as block()) so a void function's diff --git a/src/compiler/osc_parse.h b/src/compiler/osc_parse.h index 7936f9d05..12ca3ff8c 100644 --- a/src/compiler/osc_parse.h +++ b/src/compiler/osc_parse.h @@ -92,6 +92,9 @@ typedef struct { OscType ty; /* declared type (decls, params, casts, fn ret) / result type (exprs, checker) */ uint8_t mut; uint8_t flag; + uint8_t dflag; /* OSC-3 item 3, checker: an owner declaration (ON_LET_ALLOC, + * ON_LET_MOVE, own parameter) whose owner may be moved on + * only some paths; the lowerer gives it a drop flag */ /* checker annotations */ uint8_t is_const; /* expression folded to cval (already canonical for ty) */ uint64_t cval; diff --git a/tests/compiler/neg/df_borrow_after_maybe_move.osc b/tests/compiler/neg/df_borrow_after_maybe_move.osc new file mode 100644 index 000000000..eca43bb34 --- /dev/null +++ b/tests/compiler/neg/df_borrow_after_maybe_move.osc @@ -0,0 +1,8 @@ +// expect: USE_AFTER_MOVE object=a line=7 +// transition: use after maybe-move +fn rd(x: &[u8; 2]) -> u8 { return x[0]; } +fn f(c: bool) -> u8 { + let a: own [u8; 2] = alloc(3); + if c { } else { let b: own [u8; 2] = a; } + return rd(&a); +} diff --git a/tests/compiler/neg/df_double_move_branches.osc b/tests/compiler/neg/df_double_move_branches.osc new file mode 100644 index 000000000..03a0a6e14 --- /dev/null +++ b/tests/compiler/neg/df_double_move_branches.osc @@ -0,0 +1,9 @@ +// expect: USE_AFTER_MOVE object=a line=7 +// transition: use after maybe-move +fn take(x: own [u8; 2]) -> u8 { return x[0]; } +fn f(k: u8) -> u8 { + let a: own [u8; 2] = alloc(3); + if k > 1 { let m: own [u8; 2] = a; } + if k > 5 { return take(a); } + return 0; +} diff --git a/tests/compiler/neg/df_move_in_loop_if.osc b/tests/compiler/neg/df_move_in_loop_if.osc new file mode 100644 index 000000000..422adf074 --- /dev/null +++ b/tests/compiler/neg/df_move_in_loop_if.osc @@ -0,0 +1,10 @@ +// expect: USE_AFTER_MOVE object=a line=7 +// transition: move inside loop +fn take(x: own [u8; 2]) -> u8 { return x[0]; } +fn f() -> u8 { + let a: own [u8; 2] = alloc(3); + for i in 0 .. 3 { + if i == 1 { let s: u8 = take(a); } + } + return 0; +} diff --git a/tests/compiler/neg/df_nested_maybe_move.osc b/tests/compiler/neg/df_nested_maybe_move.osc new file mode 100644 index 000000000..cc4c66e03 --- /dev/null +++ b/tests/compiler/neg/df_nested_maybe_move.osc @@ -0,0 +1,12 @@ +// expect: USE_AFTER_MOVE object=p line=11 +// transition: use after maybe-move +struct P { x: u32, y: u32 } +fn eat(p: own P) -> u32 { return p.x; } +fn f(k: u32) -> u32 { + let p: own P = P { x: 1, y: 2 }; + let mut r: u32 = 0; + if k > 1 { + if k > 4 { r = eat(p); } else { r = 2; } + } else { r = p.y; } + return r + p.x; +} diff --git a/tests/compiler/neg/df_reassign_maybe_moved.osc b/tests/compiler/neg/df_reassign_maybe_moved.osc new file mode 100644 index 000000000..4aaef56ac --- /dev/null +++ b/tests/compiler/neg/df_reassign_maybe_moved.osc @@ -0,0 +1,10 @@ +// expect: IMMUTABLE_ASSIGN object=a line=8 +// re-assigning a maybe-moved owner is refused (owners are never reassigned). +fn take(x: own [u8; 2]) -> u8 { return x[0]; } +fn f(c: bool) -> u8 { + let a: own [u8; 2] = alloc(3); + let b: own [u8; 2] = alloc(4); + if c { let s: u8 = take(a); } + a = b; + return 0; +} diff --git a/tests/compiler/neg/conditional_move.osc b/tests/compiler/neg/df_use_after_maybe_move.osc similarity index 59% rename from tests/compiler/neg/conditional_move.osc rename to tests/compiler/neg/df_use_after_maybe_move.osc index 70820b990..1d0f63232 100644 --- a/tests/compiler/neg/conditional_move.osc +++ b/tests/compiler/neg/df_use_after_maybe_move.osc @@ -1,9 +1,8 @@ -// expect: CONDITIONAL_MOVE object=a line=7 -// transition: conditional move +// expect: USE_AFTER_MOVE object=a line=7 +// transition: use after maybe-move fn take(x: own [u8; 2]) -> u8 { return x[0]; } - fn f(c: bool) -> u8 { let a: own [u8; 2] = alloc(3); if c { let s: u8 = take(a); } - return 1; + return a[1]; } diff --git a/tests/compiler/progs/df_both.osc b/tests/compiler/progs/df_both.osc new file mode 100644 index 000000000..d2a73b5e8 --- /dev/null +++ b/tests/compiler/progs/df_both.osc @@ -0,0 +1,15 @@ +// entry: f +// OSC-3 item 3 (drop flags): an owner moved on both paths is definitely +// moved after the `if`: no drop flag, no release (code as in OSC-2). +fn take(x: own [u16; 3]) -> u16 { return x[0] + x[2]; } + +fn f(k: u16) -> u16 { + let a: own [u16; 3] = alloc(k & 255); + let b: own [u16; 3] = alloc(2); + let mut r: u16 = 0; + if k > 100 { r = take(a); } else { let m: own [u16; 3] = a; r = m[1] + 1; } + return r + take(b); +} +// expect-run: f 200 -> 404 +// expect-run: f 7 -> 12 +// expect-run: f 300 -> 92 diff --git a/tests/compiler/progs/df_conditional_move.osc b/tests/compiler/progs/df_conditional_move.osc new file mode 100644 index 000000000..bcba38b57 --- /dev/null +++ b/tests/compiler/progs/df_conditional_move.osc @@ -0,0 +1,13 @@ +// entry: f +// Was neg/conditional_move.osc (CONDITIONAL_MOVE, OSC-1/OSC-2). OSC-3 item 3 +// accepts it: `a` gets a drop flag and is released at the return only when +// the branch did not move it. +fn take(x: own [u8; 2]) -> u8 { return x[0]; } + +fn f(c: bool) -> u8 { + let a: own [u8; 2] = alloc(3); + if c { let s: u8 = take(a); } + return 1; +} +// expect-run: f 1 -> 1 +// expect-run: f 0 -> 1 diff --git a/tests/compiler/progs/df_else.osc b/tests/compiler/progs/df_else.osc new file mode 100644 index 000000000..31b67d33e --- /dev/null +++ b/tests/compiler/progs/df_else.osc @@ -0,0 +1,18 @@ +// entry: f +// OSC-3 item 3 (drop flags): a struct owner moved in the else-branch only +// (by a let-move into a branch-local owner, released at the branch end). +struct P { x: u32, y: u32 } + +fn f(k: u32) -> u32 { + let p: own P = P { x: k & 255, y: 3 }; + let mut r: u32 = 1; + if k < 10 { + r = r + p.x; + } else { + let q: own P = p; + r = q.y * 2; + } + return r; +} +// expect-run: f 4 -> 5 +// expect-run: f 12 -> 6 diff --git a/tests/compiler/progs/df_nested.osc b/tests/compiler/progs/df_nested.osc new file mode 100644 index 000000000..f06e62df9 --- /dev/null +++ b/tests/compiler/progs/df_nested.osc @@ -0,0 +1,34 @@ +// entry: f g +// OSC-3 item 3 (drop flags): moves at different depths of nested ifs and an +// else-if chain; returns inside branches while another owner is maybe-moved +// (the return's release of that owner is guarded by its flag). +fn take(x: own [u32; 2]) -> u32 { return x[0] + x[1]; } + +fn f(k: u32) -> u32 { + let a: own [u32; 2] = alloc(1); + let b: own [u32; 2] = alloc(2); + let mut r: u32 = 0; + if k > 2 { + if k > 5 { r = take(a); } else { r = take(b); } + } else if k == 1 { + r = take(a) + take(b); + } + return r + 100; +} + +fn g(k: u32) -> u32 { + let a: own [u32; 2] = alloc(k & 7); + let b: own [u32; 2] = alloc(3); + if k > 4 { let m: own [u32; 2] = a; b[0] = m[0]; } + if k > 6 { return b[0] + 1; } + if k == 2 { let n: own [u32; 2] = b; return n[1]; } + return b[1] + 10; +} +// expect-run: f 7 -> 102 +// expect-run: f 4 -> 104 +// expect-run: f 1 -> 106 +// expect-run: f 0 -> 100 +// expect-run: g 7 -> 8 +// expect-run: g 5 -> 13 +// expect-run: g 2 -> 3 +// expect-run: g 0 -> 13 diff --git a/tests/compiler/progs/df_order.osc b/tests/compiler/progs/df_order.osc new file mode 100644 index 000000000..3b769724d --- /dev/null +++ b/tests/compiler/progs/df_order.osc @@ -0,0 +1,24 @@ +// entry: order +// OSC-3 item 3 (drop flags): release order with conditional moves, asserted +// by test_osc_compiler for k = 0, 1, 3 in the interpreter and natively: +// k=0: alloc 1 2 3, release 2 (q), alloc 4 5, release 5 4 3 1 (p skipped) +// k=1: alloc 1 2 3, release 1 (eata), alloc 4 5, release 5 4 3 2 (a skipped) +// k=3: alloc 1 2 3, release 1, alloc 4, release 4 (eata), alloc 5, release 5 3 2 +struct P { x: u32, y: u32 } + +fn eata(a: own [u32; 2]) -> u32 { return a[0] + a[1]; } + +fn order(k: u32) -> u32 { + let a: own [u32; 2] = alloc(k); + let p: own P = P { x: k, y: 2 }; + let b: own [u32; 2] = alloc(5); + let mut r: u32 = 0; + if k > 0 { r = eata(a); } else { let q: own P = p; r = q.x + q.y; } + let c: own [u32; 2] = alloc(7); + if k > 1 { if k > 2 { r = r + eata(c); } } + let d: own [u32; 2] = alloc(1); + return r + b[0] + d[0]; +} +// expect-run: order 0 -> 8 +// expect-run: order 1 -> 8 +// expect-run: order 3 -> 26 diff --git a/tests/compiler/progs/df_struct.osc b/tests/compiler/progs/df_struct.osc new file mode 100644 index 000000000..88cf513d4 --- /dev/null +++ b/tests/compiler/progs/df_struct.osc @@ -0,0 +1,40 @@ +// entry: f h +// OSC-3 item 3 (drop flags): struct owners, an own parameter moved on one +// path, and an owner declared inside a loop body and moved on one path (its +// flag is set again by each iteration's declaration). +struct P { x: u32, v: [u32; 2] } + +fn eat(p: own P) -> u32 { return p.x + p.v[1]; } + +fn eat2(a: own [u32; 2]) -> u32 { return a[0] + a[1]; } + +fn pass(p: own P, k: u32) -> u32 { + if k > 3 { return eat(p) + 1; } + let mut r: u32 = p.x; + if k == 1 { r = r + eat(p); } + return r; +} + +fn f(k: u32) -> u32 { + let p: own P = P { x: k & 7, v: [1; 2] }; + let q: own P = P { x: 2, v: [k & 3; 2] }; + let mut r: u32 = pass(q, k); + if k > 8 { r = r + eat(p); } + return r; +} + +fn h(n: u32) -> u32 { + let mut s: u32 = 0; + for i in 0 .. 6 { + let a: own [u32; 2] = alloc(i as u32); + if (i as u32) < n { s = s + eat2(a); } + } + return s; +} +// expect-run: f 0 -> 2 +// expect-run: f 1 -> 5 +// expect-run: f 5 -> 4 +// expect-run: f 9 -> 6 +// expect-run: h 3 -> 6 +// expect-run: h 0 -> 0 +// expect-run: h 10 -> 30 diff --git a/tests/compiler/progs/df_then.osc b/tests/compiler/progs/df_then.osc new file mode 100644 index 000000000..e547e87a5 --- /dev/null +++ b/tests/compiler/progs/df_then.osc @@ -0,0 +1,31 @@ +// entry: f g +// OSC-3 item 3 (drop flags): an owner moved in the then-branch only. After +// the `if` it is maybe-moved: its drop flag is set at the declaration and +// cleared by the move, and its scope-end release runs only when the flag +// says live. A then-branch that returns leaves the owner definitely live +// after the `if` (no flag). +fn take(x: own [u8; 2]) -> u8 { return x[0] + x[1]; } + +fn f(k: u8) -> u8 { + let a: own [u8; 2] = alloc(k & 15); + let b: own [u8; 2] = alloc(4); + let mut r: u8 = 0; + if k > 3 { r = take(a); } + return r + b[0]; +} + +fn g(k: u8) -> u8 { + let a: own [u8; 2] = alloc(1); + if k == 1 { + let m: own [u8; 2] = a; + m[0] = 9; + return m[0] + m[1]; + } + a[1] = k & 7; + return a[0] + a[1]; +} +// expect-run: f 5 -> 14 +// expect-run: f 2 -> 4 +// expect-run: f 20 -> 12 +// expect-run: g 1 -> 10 +// expect-run: g 5 -> 6 diff --git a/tests/compiler/test_osc_compiler.c b/tests/compiler/test_osc_compiler.c index d3037b560..308f4829a 100644 --- a/tests/compiler/test_osc_compiler.c +++ b/tests/compiler/test_osc_compiler.c @@ -918,10 +918,30 @@ 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. */ -/* Run entry 0 of <dir>/<file> 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) +/* Index of the function named `name` that takes exactly `nargs` parameters, + * all scalar, or -1. The harnesses below pass raw integers as arguments; a + * ref parameter (own / borrow) would receive an integer where native code + * expects an object address and dereference it (segfault), so such a + * function is never a valid harness entry. */ +static int scalar_entry(const OscUnit *u, const char *name, unsigned nargs) +{ + for (int k = 0; k < u->nfuncs; k++) { + const OscFunc *f = &u->funcs[k]; + if (strcmp(f->name, name) != 0) continue; + if ((unsigned)f->nparams != nargs) return -1; + for (unsigned p = 0; p < nargs; p++) + if (f->vtype[p].s == OSC_T_REF || f->vtype[p].s == OSC_T_VOID) return -1; + return k; + } + return -1; +} + +/* Run function `entry` of <dir>/<file> with argument arg in the interpreter and + * natively; both pool logs must match the expected (kind, serial) sequence + * exactly. The entry is looked up by name: a program may define helpers before + * it (no forward references), so function 0 is not the entry in general. */ +static void dtor_order_k(const char *dir, const char *file, const char *entry, uint64_t arg, uint64_t want_ret, + unsigned n, const uint8_t *want_kind, const uint32_t *want_ser) { char path[1024]; snprintf(path, sizeof path, "%s/%s", dir, file); @@ -935,15 +955,21 @@ static void dtor_order(const char *dir, const char *file, uint64_t want_ret, uns 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, "%s: compile failed", file); + CHECK(rc == 0, "%s: compile failed", file); if (rc) return; + int fi = scalar_entry(U1, entry, 1); + CHECK(fi >= 0, "%s: no function '%s' taking one scalar argument", file, entry); + if (fi < 0) return; + int cg = osc_cg_compile(U1, &c, err, sizeof err); + CHECK(cg == 0, "%s: codegen refused: %s", file, err); + if (cg) return; OscNative nm; 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; + uint64_t args[1] = {arg}, 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); + int ti = osc_interp_run(U1, fi, args, 1, RI, &ri); + int tn = osc_rt_call_native(RN, osc_native_at(&nm, c.entry[fi]), args, 1, &rn); 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); @@ -959,11 +985,62 @@ static void dtor_order(const char *dir, const char *file, uint64_t want_ret, uns osc_cg_free(&c); } +static void dtor_order(const char *dir, const char *file, const char *entry, uint64_t want_ret, unsigned n, + const uint8_t *want_kind, const uint32_t *want_ser) +{ + dtor_order_k(dir, file, entry, 1, want_ret, n, want_kind, want_ser); +} + +/* OSC-3 item 3 (drop flags): progs/df_order.osc release order for k = 0, 1, 3 + * (see the program header). Each engine's pool log is printed as A<serial> + * (alloc) / R<serial> (release) after dtor_order_k checked it exactly. */ +static void df_fmt_log(const OscRt *rt, char *out, size_t cap) +{ + out[0] = 0; + for (unsigned k = 0; k < rt->nev; k++) { + size_t l = strlen(out); + snprintf(out + l, cap - l, "%s%c%u", k ? " " : "", rt->ev[k].kind == 1 ? 'A' : rt->ev[k].kind == 2 ? 'R' : '?', + rt->ev[k].serial); + } +} + +static void df_dtor_order(const char *dir) +{ + static const uint8_t kind0[10] = {1, 1, 1, 2, 1, 1, 2, 2, 2, 2}, kind3[10] = {1, 1, 1, 2, 1, 2, 1, 2, 2, 2}; + static const uint32_t ser0[10] = {1, 2, 3, 2, 4, 5, 5, 4, 3, 1}, ser1[10] = {1, 2, 3, 1, 4, 5, 5, 4, 3, 2}, + ser3[10] = {1, 2, 3, 1, 4, 4, 5, 5, 3, 2}; + static const uint64_t ks[3] = {0, 1, 3}, rets[3] = {8, 8, 26}; + const uint32_t *sers[3] = {ser0, ser1, ser3}; + const uint8_t *kinds[3] = {kind0, kind0, kind3}; + for (int t = 0; t < 3; t++) { + unsigned long f0 = failures; + dtor_order_k(dir, "df_order.osc", "order", ks[t], rets[t], 10, kinds[t], sers[t]); + char li[128], ln[128]; + df_fmt_log(RI, li, sizeof li); + df_fmt_log(RN, ln, sizeof ln); + printf("osc3 dropflags: order k=%llu interp=[%s] native=[%s] %s\n", (unsigned long long)ks[t], li, ln, + failures == f0 && strcmp(li, ln) == 0 ? "match" : "MISMATCH"); + CHECK(strcmp(li, ln) == 0, "df_order k=%llu: engines disagree", (unsigned long long)ks[t]); + } + /* Regression (forge run lane30-dropflags-172347, test_osc_compiler segfault): + * df_order.osc defines its helper eata(a: own [u32; 2]) before the entry + * order(k: u32). The harness used to run function 0, so natively eata + * received k (0, 1, 3) as an array address and loaded through it. The + * entry is now resolved by name and must take only scalars. Counterexamples: + * running function 0 again crashes the run above; dropping the ref-param + * test in scalar_entry makes the eata check below fail. */ + int fo = scalar_entry(U1, "order", 1), fe = scalar_entry(U1, "eata", 1); + CHECK(fo > 0, "df_order.osc: entry 'order' must not be function 0 (got %d); the regression would be vacuous", fo); + CHECK(fe < 0, "scalar_entry accepted 'eata' (own parameter) as a raw-argument entry (got %d)", fe); + printf("osc3 dropflags: harness entry by name: order=%d (not function 0), own-param helper eata refused=%s\n", fo, + fe < 0 ? "yes" : "NO"); +} + 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); + dtor_order(dir, "structs_dtor.osc", "dtor", 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"); } @@ -977,7 +1054,7 @@ 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); + dtor_order(dir, "arena_dtor.osc", "adtor", 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"); } @@ -1467,6 +1544,225 @@ static void handle_fuzz(unsigned n) printf("osc3 handles: serial counters at UINT32_MAX trap RUNTIME, never wrap: %lu of 4\n", sw_traps); } +/* OSC-3 item 3 (drop flags) fuzz: generated units whose entry declares + * array ([u32; 3]) and struct (S) owners and runs a random statement tree + * of depth <= 3: if / else / else-if with conditions on the arguments, + * moves by own call and by let-move, reads and writes, branch-local owners, + * early returns, loop-local owners moved on one path, and calls of an own + * parameter helper that moves its parameter on some paths. The generator + * mirrors the checker's merge rule (live / moved / maybe) and only touches + * owners that are live on the current path, so every unit must compile. + * Each unit: static trace replay per function (no refused event, model + * accepts), then interpreter vs native on 24 argument triples with the + * runtime pool log replayed through the model, no RUNTIME trap, no leak. */ +static unsigned long df_units, df_runs, df_mismatch, df_maybe_units, df_rej, df_trace_skip, df_traced, + df_trap[OSC_TRAP_MAX + 1]; +typedef struct { char name[8]; uint8_t st, isst; } DfOwn; /* st: 0 live, 1 moved, 3 maybe */ +static DfOwn df_o[64]; +static unsigned df_no, df_id, df_maybe; +static char df_src[16384]; +#define DFC(...) cf_cat(df_src, sizeof df_src, __VA_ARGS__) + +static void df_ind(unsigned d) { for (unsigned k = 0; k <= d; k++) DFC(" "); } +static void df_cond(void) +{ + static const char *v[] = {"a", "b", "c"}; + const char *x = v[sm() % 3]; + switch (sm() % 3) { + case 0: DFC("%s > %u", x, (unsigned)(sm() % 16)); break; + case 1: DFC("(%s & %u) == %u", x, 1 + (unsigned)(sm() % 7), (unsigned)(sm() % 4)); break; + default: DFC("%s < %u", x, (unsigned)(sm() % 16)); break; + } +} +/* a random owner with state 0 (live) at index >= lo, or -1 */ +static int df_pick(unsigned lo) +{ + int c[64], k = 0; + for (unsigned i = lo; i < df_no; i++) + if (df_o[i].st == 0) c[k++] = (int)i; + return k ? c[sm() % (unsigned)k] : -1; +} +static unsigned df_new(const char *pfx, int isst) +{ + unsigned i = df_no++; + snprintf(df_o[i].name, sizeof df_o[i].name, "%s%u", pfx, df_id++); + df_o[i].st = 0; + df_o[i].isst = (uint8_t)isst; + return i; +} +static void df_decl(unsigned d, int isst, const char *pfx) +{ + unsigned i = df_new(pfx, isst); + df_ind(d); + if (isst) DFC("let %s: own S = S { x: (b & 31) as u32, v: [(c & 3) as u32; 2] };\n", df_o[i].name); + else DFC("let %s: own [u32; 3] = alloc((a & 15) as u32);\n", df_o[i].name); +} + +/* one block body at depth d; lo = first owner that may be moved (owners + * declared outside an enclosing loop stay unmoved); returns 1 if the block + * ends in a return. Branch-local owners are dropped from df_o at the end. */ +static int df_block(unsigned d, unsigned lo, int inloop) +{ + unsigned base = df_no, ns = 1 + (unsigned)(sm() % 3); + for (unsigned s = 0; s < ns; s++) { + unsigned k = (unsigned)(sm() % 16); + int o; + if (k < 4 && (o = df_pick(lo)) >= 0) { /* move by own call */ + df_ind(d); + if (df_o[o].isst && sm() % 2) DFC("acc = acc + pm(%s, a);\n", df_o[o].name); + else DFC("acc = acc + %s(%s);\n", df_o[o].isst ? "es" : "ea", df_o[o].name); + df_o[o].st = 1; + } else if (k < 6 && (o = df_pick(lo)) >= 0) { /* let-move */ + int isst = df_o[o].isst; + df_o[o].st = 1; + unsigned m = df_new("m", isst); + df_ind(d); + DFC("let %s: own %s = %s;\n", df_o[m].name, isst ? "S" : "[u32; 3]", df_o[o].name); + df_ind(d); + DFC("acc = acc + (%s%s as u64);\n", df_o[m].name, isst ? ".x" : "[1]"); + } else if (k < 8 && (o = df_pick(0)) >= 0) { /* read and write */ + df_ind(d); + if (df_o[o].isst) DFC("%s.v[1] = %s.x + ((b & 7) as u32);\n", df_o[o].name, df_o[o].name); + else DFC("%s[2] = %s[0] + ((b & 7) as u32);\n", df_o[o].name, df_o[o].name); + df_ind(d); + DFC("acc = acc + (%s%s as u64);\n", df_o[o].name, df_o[o].isst ? ".v[1]" : "[2]"); + } else if (k < 12 && d < 3) { /* if / else */ + uint8_t s0[64], s1[64]; + for (unsigned i = 0; i < df_no; i++) s0[i] = df_o[i].st; + df_ind(d); + DFC("if "); + df_cond(); + DFC(" {\n"); + int t1 = df_block(d + 1, lo, inloop); + df_ind(d); + DFC("}"); + for (unsigned i = 0; i < df_no; i++) { s1[i] = df_o[i].st; df_o[i].st = s0[i]; } + int t2 = 0; + if (sm() % 2) { + DFC(" else {\n"); + t2 = df_block(d + 1, lo, inloop); + df_ind(d); + DFC("}"); + } + DFC("\n"); + if (t1 && t2) { df_no = base; return 1; } + if (t2) for (unsigned i = 0; i < df_no; i++) df_o[i].st = s1[i]; + else if (!t1) + for (unsigned i = 0; i < df_no; i++) + if (s1[i] != df_o[i].st) { df_o[i].st = 3; df_maybe = 1; } + } else if (k < 13) { /* branch-local or block owner */ + df_decl(d, (int)(sm() % 2), "t"); + } else if (k < 14 && d < 2 && !inloop) { /* loop-local owner moved on one path */ + unsigned iv = df_id++, lb = df_no; + df_ind(d); + DFC("for i%u in 0 .. 3 {\n", iv); + unsigned w = df_new("w", 0); + df_ind(d + 1); + DFC("let %s: own [u32; 3] = alloc((i%u as u32) + ((c & 3) as u32));\n", df_o[w].name, iv); + df_block(d + 1, lb, 1); + df_no = lb; + df_ind(d); + DFC("}\n"); + } else if (k < 15 && !inloop && sm() % 2) { /* early return */ + df_ind(d); + DFC("return acc + %u;\n", (unsigned)(sm() % 50)); + df_no = base; + return 1; + } + } + df_no = base; + return 0; +} + +static void dropflags_fuzz(unsigned n) +{ + sm_state = 0xD50F1A65ull; + unsigned long rej0 = rr_rejected; + for (unsigned u = 0; u < n; u++) { + df_src[0] = 0; + df_no = df_id = df_maybe = 0; + DFC("struct S { x: u32, v: [u32; 2] }\n"); + DFC("fn es(p: own S) -> u64 { return (p.x as u64) + (p.v[1] as u64); }\n"); + DFC("fn ea(x: own [u32; 3]) -> u64 { return (x[0] as u64) + (x[2] as u64); }\n"); + DFC("fn pm(p: own S, k: u64) -> u64 {\n if k > %u { return es(p) + 1; }\n" + " let mut r: u64 = p.x as u64;\n if (k & 1) == 1 { r = r + es(p); }", + (unsigned)(sm() % 16)); + if (sm() % 2) DFC(" else { let q: own S = p; r = r + (q.v[0] as u64); }\n"); + else DFC("\n"); + DFC(" return r;\n}\n"); + DFC("fn entry(a: u64, b: u64, c: u64) -> u64 {\n let mut acc: u64 = 0;\n"); + unsigned no = 2 + (unsigned)(sm() % 4); + for (unsigned i = 0; i < no; i++) df_decl(0, (int)(sm() % 2), "o"); + unsigned keep = df_no; + if (!df_block(0, 0, 0)) DFC(" return acc;\n"); + df_no = keep; + DFC("}\n"); + df_units++; + if (df_maybe) df_maybe_units++; + + OscDiag d; + int rc = osc_compile(df_src, strlen(df_src), U1, &d, TR); + CHECK(rc == 0, "dropflags 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, df_src); + if (rc) continue; + CHECK(!TR->refused, "dropflags fuzz unit %u: trace has a refused event", u); + if (!TR->overflow) { + uint32_t i = 0; + while (i < TR->n) { + uint32_t j = i; + while (j < TR->n && TR->e[j].func == TR->e[i].func) j++; + OscModelReject r; + int bad = replay_trace(TR, i, j, &r); + CHECK(bad < 0, "dropflags fuzz unit %u: trace rejected at event %d: %s\n%s", u, bad, + osc_model_reject_name(r), df_src); + trace_replays++; + df_traced++; + i = j; + } + } else df_trace_skip++; + OscCode c; + char err[160]; + memset(&c, 0, sizeof c); + int cg = osc_cg_compile(U1, &c, err, sizeof err); + CHECK(cg == 0, "dropflags fuzz unit %u codegen refused: %s", u, err); + if (cg) continue; + OscNative nm; + int mr = osc_native_map(&nm, c.code, c.len); + CHECK(mr == 0, "dropflags 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(&nm, c.entry[fi]); + for (unsigned it = 0; it < 24; it++) { + uint64_t args[OSC_MAX_PARAMS] = {0}, ri = 0, rn = 0; + for (int k = 0; k < 3; k++) args[k] = it < 16 ? sm() % 20 : gen_arg(OSC_T_U64); + 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); + if (rt_replay_run(RN, "dropflags fuzz") != 0) df_rej++; + df_runs++; + diff_runs++; + int same = t1 == t2 && t1 >= 0 && (t1 != 0 || ri == rn) && osc_rt_same_outcome(RI, RN); + CHECK(same, "dropflags 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, df_src); + if (!same) { df_mismatch++; break; } + if (t1 >= 0 && t1 <= OSC_TRAP_MAX) df_trap[t1]++; + CHECK(t1 != OSC_TRAP_RUNTIME, "dropflags fuzz unit %u RUNTIME trap (double release)\n%s", u, df_src); + if (t1 == 0) CHECK(RI->live_count == 0 && RN->live_count == 0, "dropflags fuzz unit %u leak\n%s", u, df_src); + } + osc_native_unmap(&nm); + osc_cg_free(&c); + } + printf("osc3 dropflags: fuzz units=%lu runs=%lu mismatches=%lu model_rejected=%lu maybe_units=%lu ok=%lu " + "traces=%lu trace_skipped=%lu\n", df_units, df_runs, df_mismatch, rr_rejected - rej0, df_maybe_units, df_trap[0], + df_traced, df_trace_skip); + CHECK(df_rej == 0 && rr_rejected == rej0, "dropflags fuzz: %lu runs rejected by the model", df_rej); + CHECK(df_maybe_units > 0, "dropflags fuzz never generated a maybe-moved owner"); + CHECK(df_trap[0] > 0, "dropflags fuzz never returned normally"); +} +#undef DFC + int main(int argc, char **argv) { unsigned fuzz = argc > 1 ? (unsigned)strtoul(argv[1], NULL, 10) : 1000; @@ -1502,6 +1798,8 @@ int main(int argc, char **argv) struct_fuzz(fuzz / 8 ? fuzz / 8 : 16); arena_fuzz(fuzz / 8 ? fuzz / 8 : 16); handle_fuzz(fuzz / 8 ? fuzz / 8 : 16); + df_dtor_order(pdir); + dropflags_fuzz(fuzz / 8 ? fuzz / 8 : 16); const char *const *tn = trap_names; printf("trap coverage (runs, interpreter == native):\n");