interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / tests / model / world_diff.c

762 lines · 30652 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * world_diff.c -- differential harness: the lifecycle model against the real
19 * rx_world (hardening lane LG). Host only; no graphics chip.
20 *
21 * The real World is built with the model's tree (P -> C1, C2; C1 -> G), the
22 * model's budget (2 slots, 8 memory) and needs. Every reaction function is a
23 * gate: it parks inside the function until the harness says "finish" (commit
24 * a new, strictly increasing value) or "fail" (return an error). After every
25 * operation the harness waits until the World is settled (every admitted
26 * activation is parked inside its function, nothing pending) and compares
27 * what the World shows with what the AS_BUILT model predicts.
28 *
29 * Observed through rx_world.h only: rx_world_footprint, rx_world_crumb,
30 * rx_world_verify_crumbs, the RxStats block and each RxReaction's state,
31 * rearm, parked, yield_left and commits fields (declared in rx_world.h, read
32 * under the world lock). No src/runtime code is changed or shimmed.
33 *
34 * Parts:
35 * 1. Exhaustive differential: every operation sequence of the AS_BUILT model
36 * up to --depth (default 6), outside publications, finishes, failures
37 * and clock ticks, replayed on a fresh World. Expect 0 mismatches.
38 * 2. Harness mutants: the same comparison against the model with a leak and
39 * a double-commit fault switched on must find a mismatch (KILLED).
40 * 3. Real-side checks on every settled step and at the end of each run:
41 * budget held == budget of the activations in their functions, zero at
42 * quiescence; no two commits of one reaction answer the same wake;
43 * rx_world_verify_crumbs == 0. Each checker has a shim mutation that
44 * must trip it.
45 * 4. Directed replays of the model's counterexamples for the two known
46 * gaps (I2 cancel, I4 deadline) on the real World, reported as FINDINGS.
47 * --stress: concurrent publishers, clock ticks and observers against free
48 * running reactions (the ThreadSanitizer workload).
49 * Output: human lines plus "RESULT key=value" lines for the receipt.
50 */
51#include "runtime/rx_world.h"
52#include "world_explore.h"
53#include "world_model.h"
54
55#include <errno.h>
56#include <pthread.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <time.h>
61
62/* ---- gate ---------------------------------------------------------------- */
63
64typedef struct {
65 pthread_mutex_t mu;
66 pthread_cond_t cv;
67 int in_fn[WM_N];
68 int n_in_fn;
69 int cmd[WM_N]; /* 1 finish, 2 fail */
70 int drain; /* functions return at once (commit) */
71 int free_run; /* --stress: never park */
72} Gate;
73
74static Gate g = { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, {0}, 0, {0}, 0, 0 };
75static uint64_t g_value; /* atomic; strictly increasing outputs */
76static RxObjRef g_out[WM_N];
77static uint32_t g_idx[WM_N] = { 0, 1, 2, 3 };
78
79static int fn_gate(RxCtx *ctx) {
80 uint32_t i = *(const uint32_t *)ctx->user;
81 int c = 1;
82 pthread_mutex_lock(&g.mu);
83 if (g.free_run) {
84 uint64_t v = __atomic_add_fetch(&g_value, 1, __ATOMIC_SEQ_CST);
85 c = (v % 7u == 0) ? 2 : 1;
86 } else if (!g.drain) {
87 g.in_fn[i] = 1;
88 g.n_in_fn++;
89 while (!g.cmd[i] && !g.drain) pthread_cond_wait(&g.cv, &g.mu);
90 if (g.cmd[i]) c = g.cmd[i];
91 g.cmd[i] = 0;
92 g.in_fn[i] = 0;
93 g.n_in_fn--;
94 }
95 pthread_mutex_unlock(&g.mu);
96 if (c == 2) return -1;
97 uint64_t v = __atomic_add_fetch(&g_value, 1, __ATOMIC_SEQ_CST);
98 ctx->n_out = 1;
99 ctx->out[0].obj = g_out[i];
100 ctx->out[0].field = 0;
101 ctx->out[0].value = v;
102 return 0;
103}
104
105/* ---- authority stand-in: every reference is valid ------------------------ */
106
107static int auth_ok(const void *c, RxCapRef ref, uint32_t subject, uint64_t resource,
108 uint32_t rights, RxCapEntry *out) {
109 (void)c; (void)ref; (void)subject; (void)resource; (void)rights;
110 if (out) memset(out, 0, sizeof *out);
111 return RX_CAP_OK;
112}
113static int auth_inspect(const void *c, RxCapRef ref, RxCapEntry *out) {
114 (void)c; (void)ref; (void)out;
115 return RX_CAP_ERR_STATE;
116}
117static int g_auth_ctx;
118
119/* ---- real World ---------------------------------------------------------- */
120
121typedef struct {
122 RxWorld *w;
123 RxObjRef e;
124 uint8_t tick;
125 uint32_t slots;
126 uint64_t mem;
127} Real;
128
129static void set_budget(Real *R) {
130 RxResourceBudget b;
131 memset(&b, 0, sizeof b);
132 b.slots = R->slots;
133 b.memory_bytes = R->mem;
134 b.energy_budget = UINT64_MAX;
135 b.offered_locality = UINT32_MAX;
136 b.offered_accel = UINT32_MAX;
137 b.compute_mask = UINT32_MAX;
138 b.logical_tick = R->tick;
139 rx_world_set_resources(R->w, &b);
140}
141
142static int real_start(Real *R, uint32_t workers, uint32_t slots, uint64_t mem) {
143 memset(R, 0, sizeof *R);
144 R->slots = slots;
145 R->mem = mem;
146 R->w = calloc(1, sizeof *R->w);
147 if (!R->w) return -1;
148 if (rx_world_init_with_auth(R->w, NULL, &g_auth_ctx, auth_ok, auth_inspect, workers,
149 1u << 14) != RX_OK) {
150 free(R->w);
151 return -1;
152 }
153 R->w->external_subject = 100;
154 set_budget(R);
155 if (rx_world_create(R->w, 1, RX_PERSIST_RESIDENT, 1, NULL, &R->e) != RX_OK) return -1;
156 for (uint32_t i = 0; i < WM_N; i++)
157 if (rx_world_create(R->w, 1, RX_PERSIST_RESIDENT, 1, NULL, &g_out[i]) != RX_OK) return -1;
158 for (uint32_t i = 0; i < WM_N; i++) {
159 RxReactionDesc d;
160 memset(&d, 0, sizeof d);
161 d.name = wm_name[i];
162 d.faculty = i + 1;
163 d.subject = 1;
164 d.priority = RX_PRIO_FOREGROUND;
165 d.need.memory_bytes = wm_mem[i];
166 d.need.deadline = wm_deadline[i];
167 d.n_triggers = 1;
168 d.triggers[0].obj = wm_parent[i] < 0 ? R->e : g_out[wm_parent[i]];
169 d.triggers[0].mask = RX_FIELD(0);
170 d.n_writes = 1;
171 d.writes[0].obj = g_out[i];
172 d.writes[0].mask = RX_FIELD(0);
173 d.n_caps = 1;
174 d.caps[0].ref = (RxCapRef){ 1, 1 };
175 d.caps[0].resource = 1;
176 d.caps[0].rights = RX_RIGHT_READ | RX_RIGHT_WRITE;
177 d.fn = fn_gate;
178 d.user = &g_idx[i];
179 uint32_t rid = UINT32_MAX;
180 if (rx_world_add_reaction(R->w, &d, &rid) != RX_OK || rid != i) return -1;
181 }
182 return 0;
183}
184
185static uint64_t g_ext_value;
186
187static int real_ext(Real *R) {
188 RxMutation m = { R->e, 0, ++g_ext_value };
189 return rx_world_publish_external(R->w, (RxCapRef){ 1, 1 }, &m, 1) > 0 ? 0 : -1;
190}
191
192static int real_release(uint32_t r, int how) {
193 pthread_mutex_lock(&g.mu);
194 int ok = g.in_fn[r];
195 if (ok) g.cmd[r] = how;
196 pthread_cond_broadcast(&g.cv);
197 pthread_mutex_unlock(&g.mu);
198 return ok ? 0 : -1;
199}
200
201/* ---- observation ----------------------------------------------------------- */
202
203enum { CL_IDLE = 0, CL_WAITING = 1, CL_RUN = 2, CL_PARKED = 3, CL_OTHER = 9 };
204typedef struct {
205 uint8_t cls[WM_N], rearm[WM_N], yield[WM_N];
206 uint32_t commits[WM_N];
207 uint32_t used_slots, in_flight;
208 uint64_t used_mem, overdue, invalidations, failed;
209 int in_fn[WM_N];
210} Obs;
211
212static const char *cls_name(int c) {
213 switch (c) {
214 case CL_IDLE: return "IDLE"; case CL_WAITING: return "WAITING";
215 case CL_RUN: return "RUN"; case CL_PARKED: return "PARKED"; default: return "OTHER";
216 }
217}
218
219static void model_obs(const WmState *s, Obs *o) {
220 memset(o, 0, sizeof *o);
221 for (uint32_t r = 0; r < WM_N; r++) {
222 o->cls[r] = s->st[r] == WM_RUN ? CL_RUN : s->st[r] == WM_WAITING ? CL_WAITING
223 : s->st[r] == WM_IDLE ? (s->parked[r] ? CL_PARKED : CL_IDLE) : CL_OTHER;
224 o->rearm[r] = s->rearm[r];
225 o->yield[r] = s->yield[r];
226 o->commits[r] = s->commits[r];
227 o->in_fn[r] = s->st[r] == WM_RUN;
228 }
229 o->used_slots = s->used_slots;
230 o->used_mem = s->used_mem;
231 o->in_flight = s->in_flight;
232 o->overdue = s->overdue;
233 o->invalidations = s->invalidations;
234 o->failed = s->failures;
235}
236
237static int g_real_leak_shim; /* checker self-test: pretend one slot leaked */
238
239/* Wait until settled, then capture. Lock order: world, then gate (the gate
240 * never takes the world lock), so the two counts are one consistent cut. */
241static int settle(Real *R, Obs *o) {
242 struct timespec t0, now;
243 clock_gettime(CLOCK_MONOTONIC, &t0);
244 for (;;) {
245 RxWorld *w = R->w;
246 pthread_mutex_lock(&w->mu);
247 pthread_mutex_lock(&g.mu);
248 int pending = 0;
249 for (uint32_t r = 0; r < WM_N; r++) pending |= g.cmd[r];
250 int ok = !pending && w->in_flight == (uint32_t)g.n_in_fn && w->deferred_len == 0;
251 if (ok) {
252 memset(o, 0, sizeof *o);
253 for (uint32_t r = 0; r < WM_N; r++) {
254 const RxReaction *x = &w->reactions[r];
255 o->cls[r] = x->state == RX_DORMANT ? (x->parked ? CL_PARKED : CL_IDLE)
256 : x->state == RX_BLOCKED_RESOURCE ? CL_WAITING
257 : x->state == RX_RUNNING ? CL_RUN : CL_OTHER;
258 o->rearm[r] = x->rearm;
259 o->yield[r] = (uint8_t)x->yield_left;
260 o->commits[r] = (uint32_t)x->commits;
261 o->in_fn[r] = g.in_fn[r];
262 }
263 o->used_slots = w->used_slots + (g_real_leak_shim ? 1u : 0u);
264 o->used_mem = w->used_memory;
265 o->in_flight = w->in_flight;
266 o->overdue = w->stats.deadline_overdue;
267 o->invalidations = w->stats.invalidations;
268 o->failed = w->stats.failed;
269 }
270 pthread_mutex_unlock(&g.mu);
271 pthread_mutex_unlock(&w->mu);
272 if (ok) return 0;
273 clock_gettime(CLOCK_MONOTONIC, &now);
274 if ((now.tv_sec - t0.tv_sec) * 1000000000L + (now.tv_nsec - t0.tv_nsec) > 3000000000L)
275 return -1;
276 struct timespec ts = { 0, 20000 };
277 nanosleep(&ts, NULL);
278 }
279}
280
281/* Real-side I1 on one settled cut: held budget == the activations parked in
282 * their functions (each holds exactly its need). */
283static int real_leak_check(const Obs *o) {
284 uint32_t n = 0;
285 uint64_t mem = 0;
286 for (uint32_t r = 0; r < WM_N; r++)
287 if (o->in_fn[r]) { n++; mem += wm_mem[r]; }
288 return o->used_slots == n && o->used_mem == mem && o->in_flight == n ? 0 : -1;
289}
290
291static int obs_diff(const Obs *m, const Obs *r, char *buf, size_t n) {
292 for (uint32_t i = 0; i < WM_N; i++) {
293 if (m->cls[i] != r->cls[i])
294 return snprintf(buf, n, "%s state model=%s real=%s", wm_name[i],
295 cls_name(m->cls[i]), cls_name(r->cls[i])), 1;
296 if (m->rearm[i] != r->rearm[i])
297 return snprintf(buf, n, "%s rearm model=%u real=%u", wm_name[i], m->rearm[i], r->rearm[i]), 1;
298 if (m->yield[i] != r->yield[i])
299 return snprintf(buf, n, "%s yield model=%u real=%u", wm_name[i], m->yield[i], r->yield[i]), 1;
300 if (m->commits[i] != r->commits[i])
301 return snprintf(buf, n, "%s commits model=%u real=%u", wm_name[i], m->commits[i], r->commits[i]), 1;
302 }
303#define D(f) if (m->f != r->f) return snprintf(buf, n, #f " model=%llu real=%llu", \
304 (unsigned long long)m->f, (unsigned long long)r->f), 1;
305 D(used_slots) D(used_mem) D(in_flight) D(overdue) D(invalidations) D(failed)
306#undef D
307 return 0;
308}
309
310/* Real-side I3 over the crumb log: no two COMMIT crumbs of one reaction share
311 * a wake cause. `dup_shim` appends a copy of the first commit (self-test). */
312static int commit_unique_check(RxWorld *w, int dup_shim, uint64_t *n_commits) {
313 uint64_t n = w->n_crumbs;
314 uint64_t cap = n + 1;
315 uint32_t *rx = malloc(sizeof(uint32_t) * cap);
316 uint64_t *cause = malloc(sizeof(uint64_t) * cap);
317 if (!rx || !cause) { free(rx); free(cause); return -1; }
318 uint64_t k = 0;
319 for (uint64_t id = 1; id <= n; id++) {
320 const RxCrumb *c = rx_world_crumb(w, id);
321 if (c && c->kind == RX_CRUMB_COMMIT) { rx[k] = c->reaction; cause[k] = c->wake_cause; k++; }
322 }
323 if (dup_shim && k) { rx[k] = rx[0]; cause[k] = cause[0]; k++; }
324 int bad = 0;
325 for (uint64_t i = 0; i < k && !bad; i++)
326 for (uint64_t j = i + 1; j < k; j++)
327 if (rx[i] == rx[j] && cause[i] == cause[j]) { bad = 1; break; }
328 if (n_commits) *n_commits = k;
329 free(rx);
330 free(cause);
331 return bad ? -1 : 0;
332}
333
334static void gate_reset(void) {
335 pthread_mutex_lock(&g.mu);
336 memset(g.in_fn, 0, sizeof g.in_fn);
337 memset(g.cmd, 0, sizeof g.cmd);
338 g.n_in_fn = 0;
339 g.drain = 0;
340 pthread_mutex_unlock(&g.mu);
341}
342
343/* Drain, wait quiescent, run end-of-run checks, destroy. */
344typedef struct { int quiesce_fail, leak_fail, dup_fail, verify_fail; } EndCheck;
345static void real_finish(Real *R, EndCheck *ec) {
346 pthread_mutex_lock(&g.mu);
347 g.drain = 1;
348 pthread_cond_broadcast(&g.cv);
349 pthread_mutex_unlock(&g.mu);
350 if (rx_world_wait_quiescent(R->w, 5000) != RX_OK) ec->quiesce_fail++;
351 RxFootprint f;
352 rx_world_footprint(R->w, &f);
353 if (f.used_slots || f.used_memory || f.used_energy || f.in_flight) ec->leak_fail++;
354 if (commit_unique_check(R->w, 0, NULL) != 0) ec->dup_fail++;
355 uint64_t checked = 0;
356 if (rx_world_verify_crumbs(R->w, &checked) != 0) ec->verify_fail++;
357 rx_world_destroy(R->w);
358 free(R->w);
359 R->w = NULL;
360 gate_reset();
361}
362
363/* ---- one replay ------------------------------------------------------------ */
364
365typedef struct {
366 uint64_t runs, steps, mismatches, real_leak, harness_err;
367 EndCheck end;
368 char first[512];
369} DiffStats;
370
371static void trace_str(const WmOp *t, int n, char *buf, size_t cap) {
372 size_t at = 0;
373 buf[0] = 0;
374 for (int i = 0; i < n && at + 24 < cap; i++) {
375 char b[32];
376 wm_op_str(t[i], b, sizeof b);
377 at += (size_t)snprintf(buf + at, cap - at, "%s%s", i ? " -> " : "", b);
378 }
379}
380
381/* Returns 1 on a model/real mismatch. */
382static int replay(const WmProfile *p, const WmOp *t, int n, DiffStats *ds) {
383 Real R;
384 gate_reset();
385 if (real_start(&R, WM_N, WM_SLOTS, WM_MEM) != 0) { ds->harness_err++; return 0; }
386 WmState s;
387 wm_init(&s);
388 int mismatch = 0;
389 Obs mo, ro;
390 char why[200] = "";
391 for (int i = 0; i < n && !mismatch; i++) {
392 wm_step(p, &s, t[i]);
393 int rc = 0;
394 switch (t[i].kind) {
395 case OP_EXT: rc = real_ext(&R); break;
396 case OP_TICK: R.tick++; set_budget(&R); break;
397 case OP_FIN: rc = real_release(t[i].r, 1); break;
398 case OP_FAIL: rc = real_release(t[i].r, 2); break;
399 default: break;
400 }
401 if (rc != 0 || settle(&R, &ro) != 0) {
402 mismatch = 1;
403 snprintf(why, sizeof why, "step %d: real World did not accept/settle (rc=%d)", i + 1, rc);
404 break;
405 }
406 ds->steps++;
407 if (real_leak_check(&ro) != 0) ds->real_leak++;
408 model_obs(&s, &mo);
409 if (obs_diff(&mo, &ro, why, sizeof why)) {
410 mismatch = 1;
411 char w2[200];
412 snprintf(w2, sizeof w2, "after step %d: %s", i + 1, why);
413 memcpy(why, w2, sizeof why);
414 }
415 }
416 if (mismatch && !ds->first[0]) {
417 char tb[300];
418 trace_str(t, n, tb, sizeof tb);
419 snprintf(ds->first, sizeof ds->first, "[%s] %s", tb, why);
420 }
421 if (mismatch) ds->mismatches++;
422 ds->runs++;
423 real_finish(&R, &ds->end);
424 return mismatch;
425}
426
427/* Every sequence of the AS_BUILT model up to `depth` (no CANCEL: rx_world has
428 * no cancel entry point; see the directed replay). Replays leaves only;
429 * every prefix is compared along the way. */
430static int g_stop_on_first;
431static void enumerate(const WmProfile *p, WmState *s, WmOp *t, int at, int depth, DiffStats *ds) {
432 if (g_stop_on_first && ds->mismatches) return;
433 WmOp ops[16];
434 int nops = wm_all_ops(ops);
435 int any = 0;
436 if (at < depth) {
437 for (int o = 0; o < nops; o++) {
438 if (ops[o].kind == OP_CANCEL || !wm_enabled(p, s, ops[o])) continue;
439 any = 1;
440 WmState c = *s;
441 wm_step(p, &c, ops[o]);
442 t[at] = ops[o];
443 enumerate(p, &c, t, at + 1, depth, ds);
444 }
445 }
446 if (!any && at > 0) replay(p, t, at, ds);
447}
448
449/* ---- directed replays of the model's counterexamples ---------------------- */
450
451static int directed_cancel(void) {
452 WxResult xr;
453 wx_explore(&wm_as_built, &xr);
454 int b = wx_bit_index(V_CANCEL_CHILD);
455 if (xr.trace_len[b] <= 0) { printf("RESULT real.I2.directed=NOT_RUN\n"); return -1; }
456 WmOp *t = xr.trace[b];
457 int n = xr.trace_len[b];
458 char tb[300];
459 trace_str(t, n, tb, sizeof tb);
460 Real R;
461 gate_reset();
462 if (real_start(&R, WM_N, WM_SLOTS, WM_MEM) != 0) return -1;
463 WmState spec;
464 wm_init(&spec);
465 Obs ro;
466 int remove_rc = 0, removed_busy = 0, still = 0;
467 uint32_t target = 0;
468 char who[64] = "";
469 for (int i = 0; i < n; i++) {
470 wm_step(&wm_spec, &spec, t[i]);
471 switch (t[i].kind) {
472 case OP_EXT: real_ext(&R); break;
473 case OP_TICK: R.tick++; set_budget(&R); break;
474 case OP_FIN: real_release(t[i].r, 1); break;
475 case OP_FAIL: real_release(t[i].r, 2); break;
476 case OP_CANCEL:
477 /* No cancel entry point. The nearest public call, removal, is
478 * tried on every busy member of the subtree. */
479 target = t[i].r;
480 settle(&R, &ro);
481 for (uint32_t x = 0; x < WM_N; x++)
482 if ((x == target || wm_is_descendant(x, target)) && ro.cls[x] != CL_IDLE) {
483 remove_rc = rx_world_remove_reaction(R.w, x);
484 if (remove_rc == RX_ERR_BUSY) removed_busy++;
485 }
486 break;
487 }
488 settle(&R, &ro);
489 }
490 uint32_t child = WM_N;
491 for (uint32_t x = 0; x < WM_N; x++)
492 if (wm_is_descendant(x, target) && ro.cls[x] != CL_IDLE) {
493 still++;
494 size_t l = strlen(who);
495 snprintf(who + l, sizeof who - l, "%s%s=%s", l ? "," : "", wm_name[x], cls_name(ro.cls[x]));
496 if (child == WM_N && ro.cls[x] == CL_RUN) child = x;
497 }
498 uint32_t commits_before = child < WM_N ? ro.commits[child] : 0, commits_after = 0;
499 if (child < WM_N) {
500 real_release(child, 1);
501 settle(&R, &ro);
502 commits_after = ro.commits[child];
503 }
504 int spec_idle = 1;
505 for (uint32_t x = 0; x < WM_N; x++)
506 if ((x == target || wm_is_descendant(x, target)) && spec.st[x] != WM_IDLE) spec_idle = 0;
507 printf("FINDING I2 cancel does not reach children (KNOWN_FAIL)\n");
508 printf(" trace: %s\n", tb);
509 printf(" SPEC model after cancel(%s): whole subtree idle=%s, budget slots held=%u\n",
510 wm_name[target], spec_idle ? "yes" : "no", spec.used_slots);
511 printf(" real rx_world: no cancel entry point (RX_CANCELLED is never entered);"
512 " rx_world_remove_reaction on busy members returned %d (%d x RX_ERR_BUSY)\n",
513 remove_rc, removed_busy);
514 printf(" real rx_world after the cancel: children still in flight: %s\n", who);
515 if (child < WM_N)
516 printf(" continuation FIN(%s): real commits %u -> %u (child published after its parent was cancelled)\n",
517 wm_name[child], commits_before, commits_after);
518 EndCheck ec = { 0, 0, 0, 0 };
519 real_finish(&R, &ec);
520 int reproduced = still > 0 && spec_idle;
521 printf("RESULT real.I2.directed=%s\n", reproduced ? "KNOWN_FAIL" : "NOT_REPRODUCED");
522 printf("RESULT real.I2.counterexample=%s\n", tb);
523 printf("RESULT real.I2.children_in_flight_after_cancel=%s\n", who[0] ? who : "none");
524 printf("RESULT real.I2.child_commit_after_cancel=%s\n",
525 child < WM_N && commits_after > commits_before ? "YES" : "NO");
526 return reproduced ? 0 : -1;
527}
528
529static int directed_deadline(void) {
530 WxResult xr;
531 wx_explore(&wm_as_built, &xr);
532 int b = wx_bit_index(V_DEADLINE);
533 if (xr.trace_len[b] <= 0) { printf("RESULT real.I4.directed=NOT_RUN\n"); return -1; }
534 WmOp *t = xr.trace[b];
535 int n = xr.trace_len[b];
536 char tb[300];
537 trace_str(t, n, tb, sizeof tb);
538 Real R;
539 gate_reset();
540 if (real_start(&R, WM_N, WM_SLOTS, WM_MEM) != 0) return -1;
541 WmState spec;
542 wm_init(&spec);
543 Obs ro;
544 for (int i = 0; i < n; i++) {
545 wm_step(&wm_spec, &spec, t[i]);
546 switch (t[i].kind) {
547 case OP_EXT: real_ext(&R); break;
548 case OP_TICK: R.tick++; set_budget(&R); break;
549 case OP_FIN: real_release(t[i].r, 1); break;
550 case OP_FAIL: real_release(t[i].r, 2); break;
551 default: break;
552 }
553 settle(&R, &ro);
554 }
555 uint32_t late = WM_N;
556 for (uint32_t x = 0; x < WM_N; x++)
557 if (ro.cls[x] == CL_RUN && wm_deadline[x] && R.tick > wm_deadline[x]) { late = x; break; }
558 uint64_t real_overdue = ro.overdue;
559 uint32_t c0 = late < WM_N ? ro.commits[late] : 0, c1 = 0;
560 if (late < WM_N) {
561 real_release(late, 1);
562 settle(&R, &ro);
563 c1 = ro.commits[late];
564 }
565 printf("FINDING I4 deadline overrun is not surfaced while running, and never enforced (KNOWN_FAIL)\n");
566 printf(" trace: %s\n", tb);
567 if (late < WM_N)
568 printf(" %s (deadline %u) still running at tick %u: SPEC surfaced overruns=%u, real deadline_overdue=%llu\n",
569 wm_name[late], wm_deadline[late], R.tick, spec.overdue, (unsigned long long)real_overdue);
570 printf(" continuation FIN: real commits %u -> %u (late work still publishes)\n", c0, c1);
571 EndCheck ec = { 0, 0, 0, 0 };
572 real_finish(&R, &ec);
573 int reproduced = late < WM_N && spec.overdue > real_overdue;
574 printf("RESULT real.I4.directed=%s\n", reproduced ? "KNOWN_FAIL" : "NOT_REPRODUCED");
575 printf("RESULT real.I4.counterexample=%s\n", tb);
576 printf("RESULT real.I4.spec_overdue=%u\n", spec.overdue);
577 printf("RESULT real.I4.real_deadline_overdue=%llu\n", (unsigned long long)real_overdue);
578 printf("RESULT real.I4.late_commit_published=%s\n", c1 > c0 ? "YES" : "NO");
579 return reproduced ? 0 : -1;
580}
581
582/* ---- checker self-tests (shim mutations) ---------------------------------- */
583
584static int self_tests(void) {
585 int bad = 0;
586 /* Leak checker: one settled run with the observation shim on must trip. */
587 WmOp t[2] = { { OP_EXT, 0 }, { OP_FIN, WM_P } };
588 DiffStats ds;
589 memset(&ds, 0, sizeof ds);
590 g_real_leak_shim = 1;
591 replay(&wm_as_built, t, 2, &ds);
592 g_real_leak_shim = 0;
593 int leak_killed = ds.real_leak > 0;
594 printf("RESULT shim.real_leak_check=%s\n", leak_killed ? "KILLED" : "SURVIVED");
595 bad += !leak_killed;
596 /* Duplicate-commit checker: a copied COMMIT crumb must trip it. */
597 Real R;
598 gate_reset();
599 int dup_killed = 0, clean_ok = 0;
600 if (real_start(&R, WM_N, WM_SLOTS, WM_MEM) == 0) {
601 Obs ro;
602 real_ext(&R);
603 settle(&R, &ro);
604 real_release(WM_P, 1);
605 settle(&R, &ro);
606 uint64_t k = 0;
607 clean_ok = commit_unique_check(R.w, 0, &k) == 0 && k > 0;
608 dup_killed = commit_unique_check(R.w, 1, NULL) != 0;
609 EndCheck ec = { 0, 0, 0, 0 };
610 real_finish(&R, &ec);
611 }
612 printf("RESULT shim.commit_unique_check=%s\n", dup_killed && clean_ok ? "KILLED" : "SURVIVED");
613 bad += !(dup_killed && clean_ok);
614 return bad;
615}
616
617/* ---- stress (ThreadSanitizer workload) ------------------------------------ */
618
619typedef struct { Real *R; int iters; int stop; } StressCtx;
620
621static void *stress_pub(void *a) {
622 StressCtx *c = a;
623 for (int i = 0; i < c->iters; i++) {
624 RxMutation m = { c->R->e, 0, __atomic_add_fetch(&g_ext_value, 1, __ATOMIC_SEQ_CST) };
625 rx_world_publish_external(c->R->w, (RxCapRef){ 1, 1 }, &m, 1);
626 }
627 return NULL;
628}
629static void *stress_tick(void *a) {
630 StressCtx *c = a;
631 for (int i = 0; i < 200; i++) {
632 RxResourceBudget b;
633 memset(&b, 0, sizeof b);
634 b.slots = 3; b.memory_bytes = 16; b.energy_budget = UINT64_MAX;
635 b.offered_locality = b.offered_accel = b.compute_mask = UINT32_MAX;
636 b.logical_tick = (uint64_t)i;
637 rx_world_set_resources(c->R->w, &b);
638 }
639 return NULL;
640}
641static void *stress_observe(void *a) {
642 StressCtx *c = a;
643 while (!__atomic_load_n(&c->stop, __ATOMIC_SEQ_CST)) {
644 RxFootprint f;
645 RxObject o;
646 uint8_t d[32];
647 rx_world_footprint(c->R->w, &f);
648 rx_world_read(c->R->w, c->R->e, &o);
649 rx_world_digest(c->R->w, d);
650 }
651 return NULL;
652}
653
654static int stress(int iters) {
655 Real R;
656 gate_reset();
657 pthread_mutex_lock(&g.mu);
658 g.free_run = 1;
659 pthread_mutex_unlock(&g.mu);
660 if (real_start(&R, 8, 3, 16) != 0) { printf("RESULT stress=ERROR\n"); return 1; }
661 StressCtx c = { &R, iters, 0 };
662 pthread_t pub[3], tick, obs;
663 for (int i = 0; i < 3; i++) pthread_create(&pub[i], NULL, stress_pub, &c);
664 pthread_create(&tick, NULL, stress_tick, &c);
665 pthread_create(&obs, NULL, stress_observe, &c);
666 for (int i = 0; i < 3; i++) pthread_join(pub[i], NULL);
667 pthread_join(tick, NULL);
668 int q = rx_world_wait_quiescent(R.w, 20000);
669 __atomic_store_n(&c.stop, 1, __ATOMIC_SEQ_CST);
670 pthread_join(obs, NULL);
671 RxFootprint f;
672 rx_world_footprint(R.w, &f);
673 uint64_t commits = 0, checked = 0;
674 int dup = commit_unique_check(R.w, 0, &commits);
675 int ver = rx_world_verify_crumbs(R.w, &checked);
676 pthread_mutex_lock(&R.w->mu);
677 RxStats st = R.w->stats;
678 pthread_mutex_unlock(&R.w->mu);
679 int ok = q == RX_OK && !f.used_slots && !f.used_memory && !f.in_flight && dup == 0 && ver == 0;
680 printf("[*] stress: %d x 3 publications, 200 clock ticks, observer thread: quiescent=%s "
681 "commits=%llu failed=%llu invalidations=%llu crumbs_verified=%llu held_at_rest=%u/%llu\n",
682 iters, q == RX_OK ? "yes" : "NO", (unsigned long long)commits,
683 (unsigned long long)st.failed, (unsigned long long)st.invalidations,
684 (unsigned long long)checked, f.used_slots, (unsigned long long)f.used_memory);
685 printf("RESULT stress.publications=%d\n", iters * 3);
686 printf("RESULT stress.commits=%llu\n", (unsigned long long)commits);
687 printf("RESULT stress.no_leak_at_rest=%s\n", !f.used_slots && !f.used_memory && !f.in_flight ? "PASS" : "FAIL");
688 printf("RESULT stress.commit_unique=%s\n", dup == 0 ? "PASS" : "FAIL");
689 printf("RESULT stress.crumbs_verify=%s\n", ver == 0 ? "PASS" : "FAIL");
690 printf("RESULT stress=%s\n", ok ? "PASS" : "FAIL");
691 EndCheck ec = { 0, 0, 0, 0 };
692 real_finish(&R, &ec);
693 pthread_mutex_lock(&g.mu);
694 g.free_run = 0;
695 pthread_mutex_unlock(&g.mu);
696 return ok ? 0 : 1;
697}
698
699/* ---- main ------------------------------------------------------------------ */
700
701int main(int argc, char **argv) {
702 int depth = 6, do_stress = 0, stress_iters = 3000, only_stress = 0;
703 for (int i = 1; i < argc; i++) {
704 if (!strcmp(argv[i], "--depth") && i + 1 < argc) depth = atoi(argv[++i]);
705 else if (!strcmp(argv[i], "--stress")) do_stress = 1;
706 else if (!strcmp(argv[i], "--stress-only")) do_stress = only_stress = 1;
707 else if (!strcmp(argv[i], "--stress-iters") && i + 1 < argc) stress_iters = atoi(argv[++i]);
708 else { fprintf(stderr, "usage: %s [--depth N] [--stress|--stress-only] [--stress-iters N]\n", argv[0]); return 2; }
709 }
710 int bad = 0;
711 if (!only_stress) {
712 WmOp t[64];
713 WmState s;
714 DiffStats ds;
715 memset(&ds, 0, sizeof ds);
716 wm_init(&s);
717 enumerate(&wm_as_built, &s, t, 0, depth, &ds);
718 printf("[*] differential AS_BUILT vs rx_world, depth %d: %llu runs, %llu settled steps, "
719 "%llu mismatches\n", depth, (unsigned long long)ds.runs,
720 (unsigned long long)ds.steps, (unsigned long long)ds.mismatches);
721 if (ds.first[0]) printf(" first mismatch: %s\n", ds.first);
722 printf("RESULT diff.depth=%d\n", depth);
723 printf("RESULT diff.runs=%llu\n", (unsigned long long)ds.runs);
724 printf("RESULT diff.steps=%llu\n", (unsigned long long)ds.steps);
725 printf("RESULT diff.mismatches=%llu\n", (unsigned long long)ds.mismatches);
726 printf("RESULT real.I1.settled_leak_violations=%llu\n", (unsigned long long)ds.real_leak);
727 printf("RESULT real.I1.quiescent_leak_violations=%d\n", ds.end.leak_fail);
728 printf("RESULT real.I3.duplicate_commit_runs=%d\n", ds.end.dup_fail);
729 printf("RESULT real.crumbs_verify_failures=%d\n", ds.end.verify_fail);
730 printf("RESULT real.quiesce_failures=%d\n", ds.end.quiesce_fail);
731 printf("RESULT diff.harness_errors=%llu\n", (unsigned long long)ds.harness_err);
732 int diff_ok = ds.runs > 0 && ds.mismatches == 0 && ds.real_leak == 0 && ds.end.leak_fail == 0 &&
733 ds.end.dup_fail == 0 && ds.end.verify_fail == 0 && ds.end.quiesce_fail == 0 &&
734 ds.harness_err == 0;
735 printf("RESULT diff.verdict=%s\n", diff_ok ? "PASS" : "FAIL");
736 bad += !diff_ok;
737
738 /* Harness mutants: AS_BUILT plus one fault; the comparison must notice. */
739 const WmProfile mut_leak = { "DIFF_MUT_LEAK_ON_FAIL", CANCEL_NONE, 0, 1, 0 };
740 const WmProfile mut_double = { "DIFF_MUT_COMMIT_KEEPS_ACTIVATION", CANCEL_NONE, 0, 0, 1 };
741 const WmProfile *muts[2] = { &mut_leak, &mut_double };
742 g_stop_on_first = 1;
743 for (int m = 0; m < 2; m++) {
744 DiffStats dm;
745 memset(&dm, 0, sizeof dm);
746 wm_init(&s);
747 enumerate(muts[m], &s, t, 0, depth < 4 ? depth : 4, &dm);
748 int killed = dm.mismatches > 0;
749 printf("RESULT diff.%s=%s\n", muts[m]->name, killed ? "KILLED" : "SURVIVED");
750 if (killed) printf(" %s killed: %s\n", muts[m]->name, dm.first);
751 bad += !killed;
752 }
753 g_stop_on_first = 0;
754 bad += self_tests();
755 if (directed_cancel() != 0) bad++;
756 if (directed_deadline() != 0) bad++;
757 }
758 if (do_stress) bad += stress(stress_iters);
759 printf("RESULT world_diff.verdict=%s\n", bad ? "FAIL" : "PASS");
760 printf("WORLD DIFF: %s\n", bad ? "FAIL" : "PASS");
761 return bad ? 1 : 0;
762}