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
omega-mv / tests / runtime / rx_deadline_cancel.c
| 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 | * HD-09 resource contract v0, first enforcement cut (aien-architecture |
| 19 | * docs/hardening/resource-contract-v0.md section 5): a reaction that declared |
| 20 | * a deadline and whose function returns after the World's logical tick has |
| 21 | * passed it is cancelled before publishing. |
| 22 | * |
| 23 | * Two reactions, both triggered by one outside object E, run in their own |
| 24 | * functions at tick 0 and are held there by a gate: |
| 25 | * D deadline 5, memory 64, energy 3, writes OD field 0 |
| 26 | * H no deadline, memory 32, energy 5, writes OH field 0 (the holder: it |
| 27 | * keeps its charge while D ends, so a missing or a double refund of D's |
| 28 | * charge is visible instead of being hidden by the clamp at zero) |
| 29 | * |
| 30 | * late tick moved to 6 while D is held, then D released: |
| 31 | * D has zero commits, OD field 0 and version unchanged, no COMMIT |
| 32 | * crumb of D anywhere, D's last crumb is CANCELLED with reason |
| 33 | * RX_ERR_DEADLINE, deadline_cancelled == 1; right after D ends |
| 34 | * the footprint is exactly H's charge (slots 1, memory 32, |
| 35 | * energy 5); after H ends it is back to the pre-admit zero; |
| 36 | * rx_world_verify_crumbs returns 0. |
| 37 | * control tick moved only to 5 (the comparison is strictly greater): |
| 38 | * D commits exactly once, OD holds its value, no CANCELLED crumb, |
| 39 | * deadline_cancelled == 0, same footprint checks, chain verifies. |
| 40 | * |
| 41 | * Mutations that must make this test fail (none is applied by the build; |
| 42 | * tests/runtime/rx_deadline_cancel_mutants.sh applies M1-M4 to a temp copy |
| 43 | * and requires every one to fail). A second uncharge is not claimed: uncharge |
| 44 | * returns early once the reaction no longer holds a charge, so it is not |
| 45 | * observable. |
| 46 | * M1 `>` changed to `>=` in the run_one check: control cancels -> FAIL |
| 47 | * (control: commits 0, CANCELLED crumb present). |
| 48 | * M2 the run_one check deleted: late commits -> FAIL (late: commits 1, |
| 49 | * OD changed, no CANCELLED crumb). |
| 50 | * M3 cancel, but stage/commit the proposed writes anyway: late -> FAIL |
| 51 | * (OD field/version changed, COMMIT crumb of D present). |
| 52 | * M4 cancel without end_activation's refund (charge marked released first): |
| 53 | * late -> FAIL (footprint after D ends is not exactly H's charge). |
| 54 | * |
| 55 | * Host only, CPU only. Exit 0 = PASS. |
| 56 | */ |
| 57 | #include "runtime/rx_caproot.h" |
| 58 | #include "runtime/rx_world.h" |
| 59 | |
| 60 | #include <pthread.h> |
| 61 | #include <stdint.h> |
| 62 | #include <stdio.h> |
| 63 | #include <stdlib.h> |
| 64 | #include <string.h> |
| 65 | #include <time.h> |
| 66 | |
| 67 | enum { RD = 0, RH = 1, NR = 2 }; |
| 68 | |
| 69 | static const uint64_t k_mem[NR] = { 64, 32 }; |
| 70 | static const uint64_t k_energy[NR] = { 3, 5 }; |
| 71 | static const uint64_t k_value[NR] = { 0xD0D0u, 0x4040u }; |
| 72 | |
| 73 | /* ---- gate: each function parks until released --------------------------- */ |
| 74 | |
| 75 | static struct { |
| 76 | pthread_mutex_t mu; |
| 77 | pthread_cond_t cv; |
| 78 | int in_fn[NR]; |
| 79 | int go[NR]; |
| 80 | } g = { PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER, {0}, {0} }; |
| 81 | |
| 82 | static RxObjRef g_out[NR]; |
| 83 | static uint32_t g_idx[NR] = { RD, RH }; |
| 84 | |
| 85 | static int fn_gate(RxCtx *ctx) { |
| 86 | uint32_t i = *(const uint32_t *)ctx->user; |
| 87 | pthread_mutex_lock(&g.mu); |
| 88 | g.in_fn[i] = 1; |
| 89 | pthread_cond_broadcast(&g.cv); |
| 90 | while (!g.go[i]) pthread_cond_wait(&g.cv, &g.mu); |
| 91 | g.go[i] = 0; |
| 92 | g.in_fn[i] = 0; |
| 93 | pthread_mutex_unlock(&g.mu); |
| 94 | ctx->n_out = 1; |
| 95 | ctx->out[0].obj = g_out[i]; |
| 96 | ctx->out[0].field = 0; |
| 97 | ctx->out[0].value = k_value[i]; |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static void release(uint32_t i) { |
| 102 | pthread_mutex_lock(&g.mu); |
| 103 | g.go[i] = 1; |
| 104 | pthread_cond_broadcast(&g.cv); |
| 105 | pthread_mutex_unlock(&g.mu); |
| 106 | } |
| 107 | |
| 108 | static uint64_t ms_since(const struct timespec *t0) { |
| 109 | struct timespec now; |
| 110 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 111 | return (uint64_t)((now.tv_sec - t0->tv_sec) * 1000L + (now.tv_nsec - t0->tv_nsec) / 1000000L); |
| 112 | } |
| 113 | |
| 114 | /* Bounded wait (5 s) until both functions are parked in the gate. */ |
| 115 | static int wait_both_in_fn(void) { |
| 116 | struct timespec t0; |
| 117 | clock_gettime(CLOCK_MONOTONIC, &t0); |
| 118 | for (;;) { |
| 119 | pthread_mutex_lock(&g.mu); |
| 120 | int ok = g.in_fn[RD] && g.in_fn[RH]; |
| 121 | pthread_mutex_unlock(&g.mu); |
| 122 | if (ok) return 0; |
| 123 | if (ms_since(&t0) > 5000) return -1; |
| 124 | struct timespec ts = { 0, 100000 }; |
| 125 | nanosleep(&ts, NULL); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /* Bounded wait (5 s) until D's activation has ended (DORMANT, H still in flight). */ |
| 130 | static int wait_d_ended(RxWorld *w) { |
| 131 | struct timespec t0; |
| 132 | clock_gettime(CLOCK_MONOTONIC, &t0); |
| 133 | for (;;) { |
| 134 | pthread_mutex_lock(&w->mu); |
| 135 | int ok = w->reactions[RD].state == RX_DORMANT && w->in_flight == 1; |
| 136 | pthread_mutex_unlock(&w->mu); |
| 137 | if (ok) return 0; |
| 138 | if (ms_since(&t0) > 5000) return -1; |
| 139 | struct timespec ts = { 0, 100000 }; |
| 140 | nanosleep(&ts, NULL); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /* ---- authority stand-in: every reference is valid ------------------------ */ |
| 145 | |
| 146 | static int g_auth_ctx; |
| 147 | static int auth_ok(const void *c, RxCapRef ref, uint32_t subject, uint64_t resource, |
| 148 | uint32_t rights, RxCapEntry *out) { |
| 149 | (void)c; (void)ref; (void)subject; (void)resource; (void)rights; |
| 150 | if (out) memset(out, 0, sizeof *out); |
| 151 | return RX_CAP_OK; |
| 152 | } |
| 153 | static int auth_inspect(const void *c, RxCapRef ref, RxCapEntry *out) { |
| 154 | (void)c; (void)ref; (void)out; |
| 155 | return RX_CAP_ERR_STATE; |
| 156 | } |
| 157 | |
| 158 | /* ---- checks ----------------------------------------------------------------- */ |
| 159 | |
| 160 | static int g_fail; |
| 161 | #define CHECK(cond, ...) do { \ |
| 162 | if (cond) { printf(" ok "); printf(__VA_ARGS__); printf("\n"); } \ |
| 163 | else { printf(" FAIL "); printf(__VA_ARGS__); printf("\n"); g_fail++; } \ |
| 164 | } while (0) |
| 165 | |
| 166 | static void budget(RxResourceBudget *b, uint64_t tick) { |
| 167 | memset(b, 0, sizeof *b); |
| 168 | b->slots = 4; |
| 169 | b->memory_bytes = 1024; |
| 170 | b->energy_budget = 1024; |
| 171 | b->offered_locality = UINT32_MAX; |
| 172 | b->offered_accel = UINT32_MAX; |
| 173 | b->compute_mask = UINT32_MAX; |
| 174 | b->logical_tick = tick; |
| 175 | } |
| 176 | |
| 177 | static void footprint_is(RxWorld *w, uint32_t slots, uint64_t mem, uint64_t energy, |
| 178 | const char *when) { |
| 179 | RxFootprint fp; |
| 180 | rx_world_footprint(w, &fp); |
| 181 | CHECK(fp.used_slots == slots && fp.used_memory == mem && fp.used_energy == energy, |
| 182 | "%s: used_slots=%u used_memory=%llu used_energy=%llu (want %u/%llu/%llu)", when, |
| 183 | fp.used_slots, (unsigned long long)fp.used_memory, (unsigned long long)fp.used_energy, |
| 184 | slots, (unsigned long long)mem, (unsigned long long)energy); |
| 185 | } |
| 186 | |
| 187 | /* One run. final_tick 6 = late case, 5 = control case. */ |
| 188 | static int run_case(uint64_t final_tick) { |
| 189 | const int late = final_tick > 5; |
| 190 | printf("%s: deadline 5, tick 0 -> %llu while D is held\n", late ? "late" : "control", |
| 191 | (unsigned long long)final_tick); |
| 192 | memset(g.in_fn, 0, sizeof g.in_fn); |
| 193 | memset(g.go, 0, sizeof g.go); |
| 194 | |
| 195 | RxWorld *w = calloc(1, sizeof *w); |
| 196 | if (!w) return -1; |
| 197 | if (rx_world_init_with_auth(w, NULL, &g_auth_ctx, auth_ok, auth_inspect, 2, 1u << 12) != RX_OK) { |
| 198 | free(w); |
| 199 | return -1; |
| 200 | } |
| 201 | w->external_subject = 100; |
| 202 | RxResourceBudget b; |
| 203 | budget(&b, 0); |
| 204 | rx_world_set_resources(w, &b); |
| 205 | |
| 206 | RxObjRef e; |
| 207 | int rc = rx_world_create(w, 1, RX_PERSIST_RESIDENT, 1, NULL, &e); |
| 208 | for (uint32_t i = 0; i < NR && rc == RX_OK; i++) |
| 209 | rc = rx_world_create(w, 1, RX_PERSIST_RESIDENT, 1, NULL, &g_out[i]); |
| 210 | for (uint32_t i = 0; i < NR && rc == RX_OK; i++) { |
| 211 | RxReactionDesc d; |
| 212 | memset(&d, 0, sizeof d); |
| 213 | d.name = i == RD ? "D" : "H"; |
| 214 | d.faculty = i + 1; |
| 215 | d.subject = 1; |
| 216 | d.priority = RX_PRIO_FOREGROUND; |
| 217 | d.need.memory_bytes = k_mem[i]; |
| 218 | d.need.energy_cost = k_energy[i]; |
| 219 | d.need.deadline = i == RD ? 5 : 0; |
| 220 | d.n_triggers = 1; |
| 221 | d.triggers[0].obj = e; |
| 222 | d.triggers[0].mask = RX_FIELD(0); |
| 223 | d.n_writes = 1; |
| 224 | d.writes[0].obj = g_out[i]; |
| 225 | d.writes[0].mask = RX_FIELD(0); |
| 226 | d.n_caps = 1; |
| 227 | d.caps[0].ref = (RxCapRef){ 1, 1 }; |
| 228 | d.caps[0].resource = 1; |
| 229 | d.caps[0].rights = RX_RIGHT_READ | RX_RIGHT_WRITE; |
| 230 | d.fn = fn_gate; |
| 231 | d.user = &g_idx[i]; |
| 232 | uint32_t rid = UINT32_MAX; |
| 233 | rc = rx_world_add_reaction(w, &d, &rid); |
| 234 | if (rc == RX_OK && rid != i) rc = RX_ERR_ARG; |
| 235 | } |
| 236 | if (rc != RX_OK) { |
| 237 | printf(" FAIL setup rc=%d\n", rc); |
| 238 | g_fail++; |
| 239 | rx_world_destroy(w); |
| 240 | free(w); |
| 241 | return -1; |
| 242 | } |
| 243 | |
| 244 | RxObject od0; |
| 245 | memset(&od0, 0, sizeof od0); |
| 246 | rx_world_read(w, g_out[RD], &od0); |
| 247 | footprint_is(w, 0, 0, 0, "before admission"); |
| 248 | |
| 249 | RxMutation m = { e, 0, 1 }; |
| 250 | int64_t ext = rx_world_publish_external(w, (RxCapRef){ 1, 1 }, &m, 1); |
| 251 | CHECK(ext > 0, "outside publication at tick 0 (crumb %lld)", (long long)ext); |
| 252 | int parked = wait_both_in_fn(); |
| 253 | CHECK(parked == 0, "D and H are both inside their functions"); |
| 254 | if (parked != 0) { |
| 255 | release(RD); |
| 256 | release(RH); |
| 257 | rx_world_wait_quiescent(w, 5000); |
| 258 | rx_world_destroy(w); |
| 259 | free(w); |
| 260 | return -1; |
| 261 | } |
| 262 | footprint_is(w, 2, k_mem[RD] + k_mem[RH], k_energy[RD] + k_energy[RH], "both running"); |
| 263 | |
| 264 | budget(&b, final_tick); |
| 265 | rx_world_set_resources(w, &b); |
| 266 | release(RD); |
| 267 | CHECK(wait_d_ended(w) == 0, "D's activation ended while H is still held"); |
| 268 | /* Exactly H's charge: D refunded once, not zero times, not twice. */ |
| 269 | footprint_is(w, 1, k_mem[RH], k_energy[RH], "after D ended, H still held"); |
| 270 | |
| 271 | release(RH); |
| 272 | CHECK(rx_world_wait_quiescent(w, 5000) == 0, "World quiescent after H released"); |
| 273 | footprint_is(w, 0, 0, 0, "after quiescence (pre-admission values)"); |
| 274 | |
| 275 | RxObject od; |
| 276 | memset(&od, 0, sizeof od); |
| 277 | rx_world_read(w, g_out[RD], &od); |
| 278 | RxObject oh; |
| 279 | memset(&oh, 0, sizeof oh); |
| 280 | rx_world_read(w, g_out[RH], &oh); |
| 281 | uint64_t d_commits, h_commits, cancelled, d_commit_crumbs = 0, cancel_crumbs = 0; |
| 282 | const RxCrumb *d_last = NULL; |
| 283 | pthread_mutex_lock(&w->mu); |
| 284 | d_commits = w->reactions[RD].commits; |
| 285 | h_commits = w->reactions[RH].commits; |
| 286 | cancelled = w->stats.deadline_cancelled; |
| 287 | for (uint64_t i = 0; i < w->n_crumbs; i++) { |
| 288 | const RxCrumb *c = &w->crumbs[i]; |
| 289 | if (c->reaction != RD) continue; |
| 290 | d_last = c; |
| 291 | if (c->kind == RX_CRUMB_COMMIT) d_commit_crumbs++; |
| 292 | if (c->kind == RX_CRUMB_CANCELLED) cancel_crumbs++; |
| 293 | } |
| 294 | int d_last_kind = d_last ? (int)d_last->kind : -1; |
| 295 | int d_last_reason = d_last ? d_last->reason : 0; |
| 296 | pthread_mutex_unlock(&w->mu); |
| 297 | |
| 298 | CHECK(h_commits == 1 && oh.field[0] == k_value[RH], "H (no deadline) committed once"); |
| 299 | if (late) { |
| 300 | CHECK(d_commits == 0, "D commits == 0 (got %llu)", (unsigned long long)d_commits); |
| 301 | CHECK(d_commit_crumbs == 0, "no COMMIT crumb of D (got %llu)", |
| 302 | (unsigned long long)d_commit_crumbs); |
| 303 | CHECK(od.field[0] == od0.field[0] && od.version == od0.version && |
| 304 | od.field_version[0] == od0.field_version[0], |
| 305 | "OD unchanged: field0 %llu -> %llu, version %llu -> %llu", |
| 306 | (unsigned long long)od0.field[0], (unsigned long long)od.field[0], |
| 307 | (unsigned long long)od0.version, (unsigned long long)od.version); |
| 308 | CHECK(d_last_kind == RX_CRUMB_CANCELLED && d_last_reason == RX_ERR_DEADLINE, |
| 309 | "D's last crumb is CANCELLED/RX_ERR_DEADLINE (got kind %d reason %d)", |
| 310 | d_last_kind, d_last_reason); |
| 311 | CHECK(cancel_crumbs == 1, "exactly one CANCELLED crumb of D (got %llu)", |
| 312 | (unsigned long long)cancel_crumbs); |
| 313 | CHECK(cancelled == 1, "stats.deadline_cancelled == 1 (got %llu)", |
| 314 | (unsigned long long)cancelled); |
| 315 | } else { |
| 316 | CHECK(d_commits == 1, "D commits == 1 (got %llu)", (unsigned long long)d_commits); |
| 317 | CHECK(od.field[0] == k_value[RD], "OD holds D's value"); |
| 318 | CHECK(cancel_crumbs == 0 && d_last_kind == RX_CRUMB_COMMIT, |
| 319 | "no CANCELLED crumb of D; last is COMMIT (kind %d)", d_last_kind); |
| 320 | CHECK(cancelled == 0, "stats.deadline_cancelled == 0 (got %llu)", |
| 321 | (unsigned long long)cancelled); |
| 322 | } |
| 323 | uint64_t checked = 0; |
| 324 | int vr = rx_world_verify_crumbs(w, &checked); |
| 325 | CHECK(vr == 0, "crumb chain verifies (rc %d, %llu crumbs)", vr, (unsigned long long)checked); |
| 326 | |
| 327 | rx_world_destroy(w); |
| 328 | free(w); |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | int main(void) { |
| 333 | run_case(6); |
| 334 | run_case(5); |
| 335 | if (g_fail) { |
| 336 | printf("RX_DEADLINE_CANCEL: FAIL (%d checks)\n", g_fail); |
| 337 | return 1; |
| 338 | } |
| 339 | printf("RX_DEADLINE_CANCEL: PASS\n"); |
| 340 | return 0; |
| 341 | } |