tobogganSe connecter

louise

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

337 lignes · 11123 octetsBrut · Historique
1/*
2 * M0 ABI spike: hand-written C in the exact shape louise will emit, used to
3 * validate runtime ABI v0 before the compiler exists (docs/runtime-abi.md).
4 *
5 * Conventions mirrored from the ABI:
6 * - every function takes tl_ctx* first; parameters and results are owned;
7 * - TL_ENTER at entry and loop back-edges, bailing to a label that drops
8 * owned locals;
9 * - TL_YIELDING checked after every call that may yield.
10 *
11 * Exercised: RC + iterative drop of long lists on fiber stacks, Perceus
12 * in-place reuse, `final ctl` unwinding through deep recursion, panics,
13 * preemption of 1000 actors, and kill-by-unwinding. The run fails unless
14 * every heap block is released at exit.
15 *
16 * Evidence vectors are not in ABI v0, so the `fail` handler's marker is
17 * passed as an explicit parameter here.
18 */
19#define _POSIX_C_SOURCE 200809L
20#define TL_CHECKED /* as debug builds of generated code are (issue #217) */
21#include "louise_rt.h"
22
23#include <signal.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <sys/wait.h>
27#include <unistd.h>
28
29#define CHECK(cond) \
30 do { \
31 if (!(cond)) { \
32 fprintf(stderr, "spike: check failed at line %d: %s\n", __LINE__, #cond); \
33 exit(2); \
34 } \
35 } while (0)
36
37/* type List<a> { Nil Cons(a, List<a>) } -- Nil is nullary, so a small int. */
38#define NIL TL_UNIT
39#define TAG_CONS 1
40
41static tl_value cons(tl_ctx *ctx, tl_value hd, tl_value tl) {
42 tl_block *b = tl_alloc(ctx, TAG_CONS, 2, 0);
43 b->fields[0] = hd;
44 b->fields[1] = tl;
45 return tl_from_block(b);
46}
47
48/* fn range(lo: Int, hi: Int) -> List<Int> (built back to front, tail loop) */
49static tl_value range(tl_ctx *ctx, tl_value lo, tl_value hi) {
50 tl_value acc = NIL;
51loop:
52 TL_ENTER(ctx, bail);
53 if (tl_int_get(hi) < tl_int_get(lo)) return acc;
54 acc = cons(ctx, hi, acc);
55 hi = tl_int_sub(ctx, hi, tl_int(1));
56 if (TL_YIELDING(ctx)) goto bail;
57 goto loop;
58bail:
59 tl_drop(ctx, acc);
60 return TL_UNIT;
61}
62
63/* fn sum(xs: List<Int>) -> Int (consumes xs; unique cells freed in place) */
64static tl_value sum(tl_ctx *ctx, tl_value xs) {
65 tl_value acc = tl_int(0);
66loop:
67 TL_ENTER(ctx, bail);
68 if (tl_is_int(xs)) return acc; /* Nil */
69 {
70 tl_block *c = tl_as_block(xs);
71 tl_value hd = c->fields[0], tl = c->fields[1];
72 if (tl_is_unique(xs)) {
73 tl_free(ctx, c); /* children moved out */
74 } else {
75 tl_dup(hd);
76 tl_dup(tl);
77 tl_drop(ctx, xs);
78 }
79 acc = tl_int_add(ctx, acc, hd);
80 xs = tl;
81 if (TL_YIELDING(ctx)) goto bail;
82 }
83 goto loop;
84bail:
85 tl_drop(ctx, xs);
86 return TL_UNIT;
87}
88
89/* fn incr_all(xs: List<Int>) -> List<Int> (FBIP: reuses unique cells)
90 * Written with an explicit reversal so it runs in constant stack. */
91static tl_value reverse(tl_ctx *ctx, tl_value xs) {
92 tl_value acc = NIL;
93loop:
94 TL_ENTER(ctx, bail);
95 if (tl_is_int(xs)) return acc;
96 {
97 tl_block *c = tl_as_block(xs);
98 tl_value hd = c->fields[0], tl = c->fields[1];
99 if (tl_is_unique(xs)) { /* reuse c for the new cell */
100 c->fields[1] = acc;
101 acc = xs;
102 } else {
103 tl_dup(hd);
104 tl_dup(tl);
105 tl_drop(ctx, xs);
106 acc = cons(ctx, hd, acc);
107 }
108 xs = tl;
109 }
110 goto loop;
111bail:
112 tl_drop(ctx, xs);
113 tl_drop(ctx, acc);
114 return TL_UNIT;
115}
116
117static int64_t g_reused_cells;
118
119static tl_value incr_all(tl_ctx *ctx, tl_value xs) {
120 tl_value acc = NIL;
121loop:
122 TL_ENTER(ctx, bail);
123 if (tl_is_int(xs)) return reverse(ctx, acc);
124 {
125 tl_block *c = tl_as_block(xs);
126 tl_value hd = c->fields[0], tl = c->fields[1];
127 tl_value next = tl_int_add(ctx, hd, tl_int(1));
128 if (TL_YIELDING(ctx)) goto bail;
129 if (tl_is_unique(xs)) {
130 c->fields[0] = next;
131 c->fields[1] = acc;
132 acc = xs;
133 g_reused_cells++;
134 } else {
135 tl_dup(tl);
136 tl_drop(ctx, xs);
137 acc = cons(ctx, next, acc);
138 }
139 xs = tl;
140 }
141 goto loop;
142bail:
143 tl_drop(ctx, xs);
144 tl_drop(ctx, acc);
145 return TL_UNIT;
146}
147
148/* effect fail { final ctl fail(reason: String) -> Never }
149 *
150 * fn deep(n: Int, held: List<Int>) -> Int / <fail>
151 * Each frame owns a fresh list across the recursive call; at depth 0 it
152 * performs fail, which must unwind and release every frame's list. */
153static tl_value deep(tl_ctx *ctx, uint64_t fail_marker, tl_value n, tl_value held) {
154 tl_value mine = NIL, r;
155 TL_ENTER(ctx, bail);
156 if (tl_int_get(n) == 0) {
157 tl_drop(ctx, held);
158 return tl_yield_final(ctx, fail_marker, tl_string_from(ctx, "boom", 4));
159 }
160 mine = range(ctx, tl_int(1), tl_int(10));
161 if (TL_YIELDING(ctx)) goto bail;
162 r = deep(ctx, fail_marker, tl_int_sub(ctx, n, tl_int(1)), held);
163 held = NIL; /* moved into the call */
164 if (TL_YIELDING(ctx)) goto bail;
165 return tl_int_add(ctx, r, sum(ctx, mine));
166bail:
167 tl_drop(ctx, mine);
168 tl_drop(ctx, held);
169 return TL_UNIT;
170}
171
172/* handle { deep(n, range(1, 100)) } with { fail(reason) -> -1 } */
173static tl_value with_fail_handler(tl_ctx *ctx, tl_value n) {
174 uint64_t m = tl_fresh_marker(ctx);
175 tl_value held = range(ctx, tl_int(1), tl_int(100));
176 if (TL_YIELDING(ctx)) return TL_UNIT;
177 tl_value r = deep(ctx, m, n, held);
178 tl_value reason;
179 if (tl_catch_final(ctx, m, &reason)) {
180 CHECK(tl_string_len(reason) == 4);
181 tl_drop(ctx, reason);
182 return tl_int(-1);
183 }
184 if (TL_YIELDING(ctx)) return TL_UNIT; /* someone else's unwind */
185 return r;
186}
187
188/* Actor bodies: closures called as code(ctx, self). */
189
190/* fn worker() -> Never -- holds a list and loops forever until killed. */
191static tl_value worker(tl_ctx *ctx, tl_value self) {
192 tl_drop(ctx, self);
193 tl_value held = range(ctx, tl_int(1), tl_int(1000));
194 if (TL_YIELDING(ctx)) return TL_UNIT;
195loop:
196 TL_ENTER(ctx, bail);
197 {
198 tl_value s = sum(ctx, range(ctx, tl_int(1), tl_int(50)));
199 if (TL_YIELDING(ctx)) goto bail;
200 CHECK(tl_int_get(s) == 1275);
201 }
202 goto loop;
203bail:
204 tl_drop(ctx, held);
205 return TL_UNIT;
206}
207
208/* fn divide() -> Int -- panics with "division by zero". (Overflow no
209 * longer panics: it promotes to a big integer, ABI version 2.) */
210static tl_value divider(tl_ctx *ctx, tl_value self) {
211 tl_drop(ctx, self);
212 tl_value held = range(ctx, tl_int(1), tl_int(100));
213 if (TL_YIELDING(ctx)) return TL_UNIT;
214 tl_value r = tl_int_div(ctx, tl_int(1), tl_int(0));
215 if (TL_YIELDING(ctx)) {
216 tl_drop(ctx, held);
217 return TL_UNIT;
218 }
219 tl_drop(ctx, held);
220 return r;
221}
222
223static tl_value spawn_fn(tl_ctx *ctx, tl_value (*code)(tl_ctx *, tl_value)) {
224 return tl_spawn(ctx, tl_closure_alloc(ctx, (tl_closure_fn)code, (tl_closure_fn)tl_borrowed0, 0));
225}
226
227static tl_value root(tl_ctx *ctx) {
228 /* 1. A million-element list: sum consumes it, a second one is dropped
229 * whole (iterative drop on a 256 KiB fiber stack), in steps: the
230 * drop frees 4,096 cells and the actor's safe points the rest
231 * (issue #158), so it yields until they are all freed. */
232 tl_value s = sum(ctx, range(ctx, tl_int(1), tl_int(1000000)));
233 CHECK(tl_int_get(s) == 500000500000);
234 tl_drop(ctx, range(ctx, tl_int(1), tl_int(1000000)));
235 CHECK(tl_stats_live_blocks() > 0);
236 CHECK(ctx->reductions == 0); /* the next function entry yields */
237 int steps = 0;
238 while (tl_stats_live_blocks() != 0) {
239 tl_preempt(ctx);
240 steps++;
241 }
242 CHECK(steps > 100);
243
244 /* 2. FBIP: a unique list is updated in place; a shared one is copied. */
245 tl_value xs = range(ctx, tl_int(1), tl_int(1000));
246 xs = incr_all(ctx, xs);
247 CHECK(g_reused_cells == 1000);
248 tl_value shared = tl_dup(xs);
249 tl_value ys = incr_all(ctx, shared);
250 CHECK(g_reused_cells == 1000);
251 CHECK(tl_int_get(sum(ctx, xs)) == 501500);
252 CHECK(tl_int_get(sum(ctx, ys)) == 502500);
253 CHECK(tl_stats_live_blocks() == 0);
254
255 /* 3. final ctl: unwind 500 frames, each owning a list. */
256 tl_value r = with_fail_handler(ctx, tl_int(500));
257 CHECK(!TL_YIELDING(ctx));
258 CHECK(tl_int_get(r) == -1);
259 CHECK(tl_stats_live_blocks() == 0);
260
261 /* 4. Overflow promotes to a big integer, a block; a result that fits
262 * is a small int again (ADR 0014). */
263 tl_value big = tl_int_add(ctx, tl_int(TL_INT_MAX), tl_int(1));
264 CHECK(tl_is_ptr(big) && tl_as_block(big)->h.tag == TL_TAG_BIGINT);
265 CHECK(tl_int_sub(ctx, big, tl_int(1)) == tl_int(TL_INT_MAX));
266 tl_drop(ctx, big);
267 CHECK(tl_stats_live_blocks() == 0);
268
269 /* 5. A count that would overflow sticks (issue #217): the inline
270 * tl_dup and tl_drop leave it, and the block is never freed. */
271 tl_value held = cons(ctx, tl_int(1), TL_UNIT);
272 tl_as_block(held)->h.rc = TL_RC_STUCK - 1;
273 tl_dup(held);
274 CHECK(tl_as_block(held)->h.rc == TL_RC_STUCK);
275 tl_dup(held);
276 for (int i = 0; i < 3; i++) tl_drop(ctx, held);
277 CHECK(tl_as_block(held)->h.rc == TL_RC_STUCK);
278 tl_as_block(held)->h.rc = 1;
279 tl_drop(ctx, held);
280 CHECK(tl_stats_live_blocks() == 0);
281
282 /* 6. Checked builds: a call of an extern marked as never unwinding
283 * returns as usual, and if it panics after all, the program aborts
284 * instead of running on without a yield check (issue #217). */
285 CHECK(TL_NO_UNWIND("tl_int_add", tl_int_add(ctx, tl_int(1), tl_int(2))) == tl_int(3));
286 fflush(NULL);
287 pid_t child = fork();
288 if (child == 0) {
289 TL_NO_UNWIND("tl_spike_marked_wrong", tl_panic_str(ctx, "boom"));
290 _exit(0);
291 }
292 int status;
293 CHECK(waitpid(child, &status, 0) == child);
294 CHECK(WIFSIGNALED(status) && WTERMSIG(status) == SIGABRT);
295
296 /* 7. Checked builds poison the argument slots at each slice: a slot
297 * stored before a suspension is not there after it, and a callee
298 * that copies it aborts (issue #217). */
299 tl_checked();
300 ctx->xargs[0] = tl_int(5);
301 CHECK(TL_XARG(ctx, 0) == tl_int(5));
302 tl_preempt(ctx);
303 CHECK(ctx->xargs[0] == TL_XARGS_POISON);
304 fflush(NULL);
305 child = fork();
306 if (child == 0) {
307 (void)TL_XARG(ctx, 0);
308 _exit(0);
309 }
310 CHECK(waitpid(child, &status, 0) == child);
311 CHECK(WIFSIGNALED(status) && WTERMSIG(status) == SIGABRT);
312
313 /* 8. Actors: 1000 preempted workers plus one that panics. */
314 enum { WORKERS = 1000 };
315 static tl_value pids[WORKERS];
316 for (int i = 0; i < WORKERS; i++) pids[i] = spawn_fn(ctx, worker);
317 spawn_fn(ctx, divider);
318 for (int round = 0; round < 5; round++) tl_preempt(ctx); /* let them run */
319 CHECK(tl_actor_count() == 1 + WORKERS); /* divider gone */
320 for (int i = 0; i < WORKERS; i += 2) tl_kill(ctx, pids[i]);
321 while (tl_actor_count() > 1 + WORKERS / 2) tl_preempt(ctx);
322
323 printf("spike: lists, reuse, final ctl, %d actors ok; live blocks with %d workers running: %lld\n",
324 WORKERS, WORKERS / 2, (long long)tl_stats_live_blocks());
325 /* Returning kills the remaining workers; main() checks they release everything. */
326 return TL_UNIT;
327}
328
329int main(void) {
330 tl_abi_check(TL_ABI_VERSION);
331 int code = tl_main(root);
332 CHECK(code == 0);
333 CHECK(tl_actor_count() == 0);
334 CHECK(tl_stats_live_blocks() == 0);
335 printf("spike: ok (all actors exited, 0 live blocks)\n");
336 return 0;
337}