/* * M0 ABI spike: hand-written C in the exact shape louise will emit, used to * validate runtime ABI v0 before the compiler exists (docs/runtime-abi.md). * * Conventions mirrored from the ABI: * - every function takes tl_ctx* first; parameters and results are owned; * - TL_ENTER at entry and loop back-edges, bailing to a label that drops * owned locals; * - TL_YIELDING checked after every call that may yield. * * Exercised: RC + iterative drop of long lists on fiber stacks, Perceus * in-place reuse, `final ctl` unwinding through deep recursion, panics, * preemption of 1000 actors, and kill-by-unwinding. The run fails unless * every heap block is released at exit. * * Evidence vectors are not in ABI v0, so the `fail` handler's marker is * passed as an explicit parameter here. */ #define _POSIX_C_SOURCE 200809L #define TL_CHECKED /* as debug builds of generated code are (issue #217) */ #include "louise_rt.h" #include #include #include #include #include #define CHECK(cond) \ do { \ if (!(cond)) { \ fprintf(stderr, "spike: check failed at line %d: %s\n", __LINE__, #cond); \ exit(2); \ } \ } while (0) /* type List { Nil Cons(a, List) } -- Nil is nullary, so a small int. */ #define NIL TL_UNIT #define TAG_CONS 1 static tl_value cons(tl_ctx *ctx, tl_value hd, tl_value tl) { tl_block *b = tl_alloc(ctx, TAG_CONS, 2, 0); b->fields[0] = hd; b->fields[1] = tl; return tl_from_block(b); } /* fn range(lo: Int, hi: Int) -> List (built back to front, tail loop) */ static tl_value range(tl_ctx *ctx, tl_value lo, tl_value hi) { tl_value acc = NIL; loop: TL_ENTER(ctx, bail); if (tl_int_get(hi) < tl_int_get(lo)) return acc; acc = cons(ctx, hi, acc); hi = tl_int_sub(ctx, hi, tl_int(1)); if (TL_YIELDING(ctx)) goto bail; goto loop; bail: tl_drop(ctx, acc); return TL_UNIT; } /* fn sum(xs: List) -> Int (consumes xs; unique cells freed in place) */ static tl_value sum(tl_ctx *ctx, tl_value xs) { tl_value acc = tl_int(0); loop: TL_ENTER(ctx, bail); if (tl_is_int(xs)) return acc; /* Nil */ { tl_block *c = tl_as_block(xs); tl_value hd = c->fields[0], tl = c->fields[1]; if (tl_is_unique(xs)) { tl_free(ctx, c); /* children moved out */ } else { tl_dup(hd); tl_dup(tl); tl_drop(ctx, xs); } acc = tl_int_add(ctx, acc, hd); xs = tl; if (TL_YIELDING(ctx)) goto bail; } goto loop; bail: tl_drop(ctx, xs); return TL_UNIT; } /* fn incr_all(xs: List) -> List (FBIP: reuses unique cells) * Written with an explicit reversal so it runs in constant stack. */ static tl_value reverse(tl_ctx *ctx, tl_value xs) { tl_value acc = NIL; loop: TL_ENTER(ctx, bail); if (tl_is_int(xs)) return acc; { tl_block *c = tl_as_block(xs); tl_value hd = c->fields[0], tl = c->fields[1]; if (tl_is_unique(xs)) { /* reuse c for the new cell */ c->fields[1] = acc; acc = xs; } else { tl_dup(hd); tl_dup(tl); tl_drop(ctx, xs); acc = cons(ctx, hd, acc); } xs = tl; } goto loop; bail: tl_drop(ctx, xs); tl_drop(ctx, acc); return TL_UNIT; } static int64_t g_reused_cells; static tl_value incr_all(tl_ctx *ctx, tl_value xs) { tl_value acc = NIL; loop: TL_ENTER(ctx, bail); if (tl_is_int(xs)) return reverse(ctx, acc); { tl_block *c = tl_as_block(xs); tl_value hd = c->fields[0], tl = c->fields[1]; tl_value next = tl_int_add(ctx, hd, tl_int(1)); if (TL_YIELDING(ctx)) goto bail; if (tl_is_unique(xs)) { c->fields[0] = next; c->fields[1] = acc; acc = xs; g_reused_cells++; } else { tl_dup(tl); tl_drop(ctx, xs); acc = cons(ctx, next, acc); } xs = tl; } goto loop; bail: tl_drop(ctx, xs); tl_drop(ctx, acc); return TL_UNIT; } /* effect fail { final ctl fail(reason: String) -> Never } * * fn deep(n: Int, held: List) -> Int / * Each frame owns a fresh list across the recursive call; at depth 0 it * performs fail, which must unwind and release every frame's list. */ static tl_value deep(tl_ctx *ctx, uint64_t fail_marker, tl_value n, tl_value held) { tl_value mine = NIL, r; TL_ENTER(ctx, bail); if (tl_int_get(n) == 0) { tl_drop(ctx, held); return tl_yield_final(ctx, fail_marker, tl_string_from(ctx, "boom", 4)); } mine = range(ctx, tl_int(1), tl_int(10)); if (TL_YIELDING(ctx)) goto bail; r = deep(ctx, fail_marker, tl_int_sub(ctx, n, tl_int(1)), held); held = NIL; /* moved into the call */ if (TL_YIELDING(ctx)) goto bail; return tl_int_add(ctx, r, sum(ctx, mine)); bail: tl_drop(ctx, mine); tl_drop(ctx, held); return TL_UNIT; } /* handle { deep(n, range(1, 100)) } with { fail(reason) -> -1 } */ static tl_value with_fail_handler(tl_ctx *ctx, tl_value n) { uint64_t m = tl_fresh_marker(ctx); tl_value held = range(ctx, tl_int(1), tl_int(100)); if (TL_YIELDING(ctx)) return TL_UNIT; tl_value r = deep(ctx, m, n, held); tl_value reason; if (tl_catch_final(ctx, m, &reason)) { CHECK(tl_string_len(reason) == 4); tl_drop(ctx, reason); return tl_int(-1); } if (TL_YIELDING(ctx)) return TL_UNIT; /* someone else's unwind */ return r; } /* Actor bodies: closures called as code(ctx, self). */ /* fn worker() -> Never -- holds a list and loops forever until killed. */ static tl_value worker(tl_ctx *ctx, tl_value self) { tl_drop(ctx, self); tl_value held = range(ctx, tl_int(1), tl_int(1000)); if (TL_YIELDING(ctx)) return TL_UNIT; loop: TL_ENTER(ctx, bail); { tl_value s = sum(ctx, range(ctx, tl_int(1), tl_int(50))); if (TL_YIELDING(ctx)) goto bail; CHECK(tl_int_get(s) == 1275); } goto loop; bail: tl_drop(ctx, held); return TL_UNIT; } /* fn divide() -> Int -- panics with "division by zero". (Overflow no * longer panics: it promotes to a big integer, ABI version 2.) */ static tl_value divider(tl_ctx *ctx, tl_value self) { tl_drop(ctx, self); tl_value held = range(ctx, tl_int(1), tl_int(100)); if (TL_YIELDING(ctx)) return TL_UNIT; tl_value r = tl_int_div(ctx, tl_int(1), tl_int(0)); if (TL_YIELDING(ctx)) { tl_drop(ctx, held); return TL_UNIT; } tl_drop(ctx, held); return r; } static tl_value spawn_fn(tl_ctx *ctx, tl_value (*code)(tl_ctx *, tl_value)) { return tl_spawn(ctx, tl_closure_alloc(ctx, (tl_closure_fn)code, (tl_closure_fn)tl_borrowed0, 0)); } static tl_value root(tl_ctx *ctx) { /* 1. A million-element list: sum consumes it, a second one is dropped * whole (iterative drop on a 256 KiB fiber stack), in steps: the * drop frees 4,096 cells and the actor's safe points the rest * (issue #158), so it yields until they are all freed. */ tl_value s = sum(ctx, range(ctx, tl_int(1), tl_int(1000000))); CHECK(tl_int_get(s) == 500000500000); tl_drop(ctx, range(ctx, tl_int(1), tl_int(1000000))); CHECK(tl_stats_live_blocks() > 0); CHECK(ctx->reductions == 0); /* the next function entry yields */ int steps = 0; while (tl_stats_live_blocks() != 0) { tl_preempt(ctx); steps++; } CHECK(steps > 100); /* 2. FBIP: a unique list is updated in place; a shared one is copied. */ tl_value xs = range(ctx, tl_int(1), tl_int(1000)); xs = incr_all(ctx, xs); CHECK(g_reused_cells == 1000); tl_value shared = tl_dup(xs); tl_value ys = incr_all(ctx, shared); CHECK(g_reused_cells == 1000); CHECK(tl_int_get(sum(ctx, xs)) == 501500); CHECK(tl_int_get(sum(ctx, ys)) == 502500); CHECK(tl_stats_live_blocks() == 0); /* 3. final ctl: unwind 500 frames, each owning a list. */ tl_value r = with_fail_handler(ctx, tl_int(500)); CHECK(!TL_YIELDING(ctx)); CHECK(tl_int_get(r) == -1); CHECK(tl_stats_live_blocks() == 0); /* 4. Overflow promotes to a big integer, a block; a result that fits * is a small int again (ADR 0014). */ tl_value big = tl_int_add(ctx, tl_int(TL_INT_MAX), tl_int(1)); CHECK(tl_is_ptr(big) && tl_as_block(big)->h.tag == TL_TAG_BIGINT); CHECK(tl_int_sub(ctx, big, tl_int(1)) == tl_int(TL_INT_MAX)); tl_drop(ctx, big); CHECK(tl_stats_live_blocks() == 0); /* 5. A count that would overflow sticks (issue #217): the inline * tl_dup and tl_drop leave it, and the block is never freed. */ tl_value held = cons(ctx, tl_int(1), TL_UNIT); tl_as_block(held)->h.rc = TL_RC_STUCK - 1; tl_dup(held); CHECK(tl_as_block(held)->h.rc == TL_RC_STUCK); tl_dup(held); for (int i = 0; i < 3; i++) tl_drop(ctx, held); CHECK(tl_as_block(held)->h.rc == TL_RC_STUCK); tl_as_block(held)->h.rc = 1; tl_drop(ctx, held); CHECK(tl_stats_live_blocks() == 0); /* 6. Checked builds: a call of an extern marked as never unwinding * returns as usual, and if it panics after all, the program aborts * instead of running on without a yield check (issue #217). */ CHECK(TL_NO_UNWIND("tl_int_add", tl_int_add(ctx, tl_int(1), tl_int(2))) == tl_int(3)); fflush(NULL); pid_t child = fork(); if (child == 0) { TL_NO_UNWIND("tl_spike_marked_wrong", tl_panic_str(ctx, "boom")); _exit(0); } int status; CHECK(waitpid(child, &status, 0) == child); CHECK(WIFSIGNALED(status) && WTERMSIG(status) == SIGABRT); /* 7. Checked builds poison the argument slots at each slice: a slot * stored before a suspension is not there after it, and a callee * that copies it aborts (issue #217). */ tl_checked(); ctx->xargs[0] = tl_int(5); CHECK(TL_XARG(ctx, 0) == tl_int(5)); tl_preempt(ctx); CHECK(ctx->xargs[0] == TL_XARGS_POISON); fflush(NULL); child = fork(); if (child == 0) { (void)TL_XARG(ctx, 0); _exit(0); } CHECK(waitpid(child, &status, 0) == child); CHECK(WIFSIGNALED(status) && WTERMSIG(status) == SIGABRT); /* 8. Actors: 1000 preempted workers plus one that panics. */ enum { WORKERS = 1000 }; static tl_value pids[WORKERS]; for (int i = 0; i < WORKERS; i++) pids[i] = spawn_fn(ctx, worker); spawn_fn(ctx, divider); for (int round = 0; round < 5; round++) tl_preempt(ctx); /* let them run */ CHECK(tl_actor_count() == 1 + WORKERS); /* divider gone */ for (int i = 0; i < WORKERS; i += 2) tl_kill(ctx, pids[i]); while (tl_actor_count() > 1 + WORKERS / 2) tl_preempt(ctx); printf("spike: lists, reuse, final ctl, %d actors ok; live blocks with %d workers running: %lld\n", WORKERS, WORKERS / 2, (long long)tl_stats_live_blocks()); /* Returning kills the remaining workers; main() checks they release everything. */ return TL_UNIT; } int main(void) { tl_abi_check(TL_ABI_VERSION); int code = tl_main(root); CHECK(code == 0); CHECK(tl_actor_count() == 0); CHECK(tl_stats_live_blocks() == 0); printf("spike: ok (all actors exited, 0 live blocks)\n"); return 0; }