Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/src/checker2/c2_blast.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / src / checker2 / c2_blast.c` 439 lines ยท 16739 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 /* c2_blast.c: symbolic execution of one IR function into CNF (see c2_blast.h). */ 18 #include 19 #include 20 #include 21 #include 22 23 #include "c2_blast.h" 24 25 #define T 1 26 #define F (-1) 27 #define MAXW 128 28 29 typedef struct { 30 C2Cnf *cnf; 31 int nvars; 32 int oom; 33 } Bl; 34 35 static int newvar(Bl *b) { return ++b->nvars; } 36 static void clause(Bl *b, int n, ...) 37 { 38 int32_t l[4]; 39 va_list ap; 40 va_start(ap, n); 41 for (int k = 0; k < n; k++) l[k] = va_arg(ap, int); 42 va_end(ap); 43 if (c2_cnf_add(b->cnf, l, n)) b->oom = 1; 44 } 45 46 static int AND(Bl *b, int x, int y) 47 { 48 if (x == F || y == F || x == -y) return F; 49 if (x == T) return y; 50 if (y == T || x == y) return x; 51 int v = newvar(b); 52 clause(b, 2, -v, x); 53 clause(b, 2, -v, y); 54 clause(b, 3, v, -x, -y); 55 return v; 56 } 57 static int OR(Bl *b, int x, int y) { return -AND(b, -x, -y); } 58 static int XOR(Bl *b, int x, int y) 59 { 60 if (x == F) return y; 61 if (y == F) return x; 62 if (x == T) return -y; 63 if (y == T) return -x; 64 if (x == y) return F; 65 if (x == -y) return T; 66 int v = newvar(b); 67 clause(b, 3, -v, x, y); 68 clause(b, 3, -v, -x, -y); 69 clause(b, 3, v, -x, y); 70 clause(b, 3, v, x, -y); 71 return v; 72 } 73 static int MUX(Bl *b, int s, int x, int y) /* s ? x : y */ 74 { 75 if (s == T || x == y) return x; 76 if (s == F) return y; 77 return OR(b, AND(b, s, x), AND(b, -s, y)); 78 } 79 80 /* ------------------------------------------------------------------ bit vectors */ 81 82 static void add(Bl *b, const int *x, const int *y, int cin, int w, int *out, int *cout) 83 { 84 int c = cin; /* out may alias x or y: read bit i of both before writing out[i] */ 85 for (int i = 0; i < w; i++) { 86 int p = XOR(b, x[i], y[i]), g = AND(b, x[i], y[i]); 87 out[i] = XOR(b, p, c); 88 c = OR(b, g, AND(b, c, p)); 89 } 90 if (cout) *cout = c; 91 } 92 static void neg_bits(const int *x, int w, int *o) { for (int i = 0; i < w; i++) o[i] = -x[i]; } 93 static int eq(Bl *b, const int *x, const int *y, int w) 94 { 95 int r = T; 96 for (int i = 0; i < w; i++) r = AND(b, r, -XOR(b, x[i], y[i])); 97 return r; 98 } 99 static int ult(Bl *b, const int *x, const int *y, int w) /* x < y unsigned: no carry out of x + ~y + 1 */ 100 { 101 int ny[MAXW], s[MAXW], c; 102 neg_bits(y, w, ny); 103 add(b, x, ny, T, w, s, &c); 104 return -c; 105 } 106 static int slt(Bl *b, const int *x, const int *y, int w) 107 { 108 int fx[MAXW], fy[MAXW]; 109 memcpy(fx, x, (size_t)w * sizeof *x); 110 memcpy(fy, y, (size_t)w * sizeof *y); 111 fx[w - 1] = -x[w - 1]; 112 fy[w - 1] = -y[w - 1]; 113 return ult(b, fx, fy, w); 114 } 115 static void mul(Bl *b, const int *x, const int *y, int w, int *out) 116 { 117 int acc[MAXW], part[MAXW]; 118 for (int i = 0; i < w; i++) acc[i] = F; 119 for (int i = 0; i < w; i++) { 120 for (int k = 0; k < w; k++) part[k] = k < i ? F : AND(b, y[i], x[k - i]); 121 add(b, acc, part, F, w, acc, NULL); 122 } 123 memcpy(out, acc, (size_t)w * sizeof *acc); 124 } 125 static int all_equal(Bl *b, const int *x, int lo, int hi) /* x[lo..hi] all equal */ 126 { 127 int r = T; 128 for (int i = lo + 1; i <= hi; i++) r = AND(b, r, -XOR(b, x[i], x[lo])); 129 return r; 130 } 131 132 /* ------------------------------------------------------------------ types */ 133 134 static int width(uint8_t s) 135 { 136 static const int w[] = {0, 1, 8, 16, 32, 64, 8, 16, 32, 64}; 137 return s <= S_I64 ? w[s] : 0; 138 } 139 static int is_signed(uint8_t s) { return s >= S_I8 && s <= S_I64; } 140 static int log2w(int w) { int k = 0; while ((1 << k) < w) k++; return k; } 141 142 /* ------------------------------------------------------------------ symbolic execution */ 143 144 typedef struct { 145 int block; 146 int guard; 147 int **reg; /* nvregs pointers into the value pool (values are immutable once built) */ 148 } St; 149 150 typedef struct { 151 Bl b; 152 const C2Unit *u; 153 const C2Func *f; 154 C2BlastInfo *info; 155 int **pool; /* every allocated value, freed at the end */ 156 long npool, cappool; 157 int32_t *bad; 158 int nbad, capbad; 159 long steps; 160 } Ex; 161 162 static int *val(Ex *x, int w) 163 { 164 if (x->npool == x->cappool) { 165 x->cappool = x->cappool ? x->cappool * 2 : 1024; 166 int **np = realloc(x->pool, (size_t)x->cappool * sizeof *np); 167 if (!np) { x->b.oom = 1; return NULL; } 168 x->pool = np; 169 } 170 int *v = calloc((size_t)(w ? w : 1), sizeof *v); 171 if (!v) { x->b.oom = 1; return NULL; } 172 x->pool[x->npool++] = v; 173 return v; 174 } 175 176 static int refuse(Ex *x, const char *rule, const char *fmt, ...) 177 { 178 x->info->rule = rule; 179 va_list ap; 180 va_start(ap, fmt); 181 vsnprintf(x->info->detail, sizeof x->info->detail, fmt, ap); 182 va_end(ap); 183 return -1; 184 } 185 186 /* a trap can fire on this path when cond holds: record it, return the guard of the run going on */ 187 static int trap_if(Ex *x, int g, int cond) 188 { 189 int t = AND(&x->b, g, cond); 190 if (t != F) { 191 if (x->nbad == x->capbad) { 192 x->capbad = x->capbad ? x->capbad * 2 : 64; 193 int32_t *nb = realloc(x->bad, (size_t)x->capbad * sizeof *nb); 194 if (!nb) { x->b.oom = 1; return F; } 195 x->bad = nb; 196 } 197 x->bad[x->nbad++] = t; 198 } 199 return AND(&x->b, g, -cond); 200 } 201 202 /* run one path to its end; forks are pushed on the stack */ 203 static int run(Ex *x, St *s, St *stack, int *sp) 204 { 205 const C2Func *f = x->f; 206 Bl *b = &x->b; 207 for (;;) { 208 const C2Block *bk = &f->blk[s->block]; 209 for (uint32_t i = bk->first; i < bk->first + bk->count; i++) { 210 const C2Insn *in = &f->in[i]; 211 if (++x->steps > C2_BLAST_MAX_STEPS) return refuse(x, "BLAST-TOO-LARGE", "more than %ld steps", C2_BLAST_MAX_STEPS); 212 if (b->oom) return refuse(x, "BLAST-RESOURCE", "out of memory"); 213 const C2Type *td = in->dst >= 0 ? &f->vt[in->dst] : NULL; 214 const C2Type *ta = in->a >= 0 ? &f->vt[in->a] : NULL; 215 int w = td ? width(td->s) : 0; 216 int *r = NULL; 217 switch (in->op) { 218 case C2_CONST: 219 r = val(x, w); 220 for (int k = 0; r && k < w; k++) r[k] = (in->imm >> k) & 1 ? T : F; 221 break; 222 case C2_MOV: 223 s->reg[in->dst] = s->reg[in->a]; 224 continue; 225 case C2_BIN: { 226 const int *A = s->reg[in->a], *B = s->reg[in->b]; 227 int sg = is_signed(td->s); 228 r = val(x, w); 229 if (!r) break; 230 switch (in->sub) { 231 case B_ADD: case B_SUB: { 232 int nb[MAXW]; 233 if (in->sub == B_SUB) neg_bits(B, w, nb); else memcpy(nb, B, (size_t)w * sizeof *nb); 234 add(b, A, nb, in->sub == B_SUB ? T : F, w, r, NULL); 235 if (sg) { /* overflow: operands (after negation for SUB) agree in sign, result differs */ 236 int same = in->sub == B_ADD ? -XOR(b, A[w - 1], B[w - 1]) : XOR(b, A[w - 1], B[w - 1]); 237 s->guard = trap_if(x, s->guard, AND(b, same, XOR(b, r[w - 1], A[w - 1]))); /* GUARD:add-sub-overflow */ 238 } 239 break; 240 } 241 case B_MUL: 242 if (!sg) { mul(b, A, B, w, r); break; } 243 { 244 int ax[MAXW], bx[MAXW], p[MAXW]; 245 for (int k = 0; k < 2 * w; k++) { ax[k] = A[k < w ? k : w - 1]; bx[k] = B[k < w ? k : w - 1]; } 246 mul(b, ax, bx, 2 * w, p); 247 memcpy(r, p, (size_t)w * sizeof *r); 248 s->guard = trap_if(x, s->guard, -all_equal(b, p, w - 1, 2 * w - 1)); /* GUARD:mul-overflow */ 249 } 250 break; 251 case B_AND: for (int k = 0; k < w; k++) r[k] = AND(b, A[k], B[k]); break; 252 case B_OR: for (int k = 0; k < w; k++) r[k] = OR(b, A[k], B[k]); break; 253 case B_XOR: for (int k = 0; k < w; k++) r[k] = XOR(b, A[k], B[k]); break; 254 case B_SHL: case B_SHR: { 255 if (in->sub == B_SHR && sg) return refuse(x, "BLAST-UNSUPPORTED", "SHR on a signed operand (insn %u)", i); 256 int wb = width(f->vt[in->b].s), lg = log2w(w); 257 int big = F; /* amount >= w, or negative (sign bit of a signed amount) */ 258 for (int k = lg; k < wb; k++) big = OR(b, big, B[k]); 259 s->guard = trap_if(x, s->guard, big); /* GUARD:shift-range */ 260 int cur[MAXW], nxt[MAXW]; 261 memcpy(cur, A, (size_t)w * sizeof *cur); 262 for (int j = 0; j < lg && j < wb; j++) { 263 int sh = 1 << j; 264 for (int k = 0; k < w; k++) { 265 int from = in->sub == B_SHL ? k - sh : k + sh; 266 int moved = from >= 0 && from < w ? cur[from] : F; 267 nxt[k] = MUX(b, B[j], moved, cur[k]); 268 } 269 memcpy(cur, nxt, (size_t)w * sizeof *cur); 270 } 271 memcpy(r, cur, (size_t)w * sizeof *r); 272 if (in->sub == B_SHL && sg) { /* a << k representable iff a[w-1-k .. w-1] all equal */ 273 int ovf = F; 274 for (int k = 1; k < w; k++) { 275 int amt = T; 276 for (int j = 0; j < lg; j++) amt = AND(b, amt, (k >> j) & 1 ? B[j] : -B[j]); 277 ovf = OR(b, ovf, AND(b, amt, -all_equal(b, A, w - 1 - k, w - 1))); 278 } 279 s->guard = trap_if(x, s->guard, ovf); /* GUARD:shl-overflow */ 280 } 281 break; 282 } 283 default: return refuse(x, "BLAST-UNSUPPORTED", "binary op %u (insn %u)", in->sub, i); 284 } 285 break; 286 } 287 case C2_UN: { 288 const int *A = s->reg[in->a]; 289 r = val(x, w); 290 if (!r) break; 291 if (in->sub == U_NEG) { 292 int zero[MAXW], na[MAXW]; 293 for (int k = 0; k < w; k++) zero[k] = F; 294 neg_bits(A, w, na); 295 add(b, zero, na, T, w, r, NULL); 296 if (is_signed(td->s)) { /* -MIN is not representable */ 297 int min = A[w - 1]; 298 for (int k = 0; k < w - 1; k++) min = AND(b, min, -A[k]); 299 s->guard = trap_if(x, s->guard, min); /* GUARD:neg-overflow */ 300 } 301 } else if (in->sub == U_BNOT) neg_bits(A, w, r); 302 else r[0] = -A[0]; 303 break; 304 } 305 case C2_CMP: { 306 const int *A = s->reg[in->a], *B = s->reg[in->b]; 307 int wa = width(ta->s), sg = is_signed(ta->s); 308 int lt = sg ? slt(b, A, B, wa) : ult(b, A, B, wa); 309 int gt = sg ? slt(b, B, A, wa) : ult(b, B, A, wa); 310 int e = eq(b, A, B, wa), c; 311 switch (in->sub) { 312 case CC_EQ: c = e; break; 313 case CC_NE: c = -e; break; 314 case CC_LT: c = lt; break; 315 case CC_LE: c = -gt; break; 316 case CC_GT: c = gt; break; 317 default: c = -lt; break; /* CC_GE */ 318 } 319 r = val(x, 1); 320 if (r) r[0] = c; 321 break; 322 } 323 case C2_CAST: { 324 const int *A = s->reg[in->a]; 325 int ws = width(ta->s), wt = w, W = (ws > wt ? ws : wt) + 1; 326 int ext[MAXW + 1]; 327 for (int k = 0; k < W; k++) ext[k] = k < ws ? A[k] : (is_signed(ta->s) ? A[ws - 1] : F); 328 int fill = is_signed(td->s) ? ext[wt - 1] : F, fits = T; 329 for (int k = wt; k < W; k++) fits = AND(b, fits, -XOR(b, ext[k], fill)); 330 s->guard = trap_if(x, s->guard, -fits); /* GUARD:cast-range */ 331 r = val(x, wt); 332 if (r) memcpy(r, ext, (size_t)wt * sizeof *r); 333 break; 334 } 335 case C2_TRAP: 336 if (in->imm != 9) (void)trap_if(x, s->guard, T); /* 9 = REQUIRES: the caller's fault; GUARD:trap-insn */ 337 return 0; 338 case C2_RET: 339 return 0; 340 case C2_BR: 341 s->block = in->t; 342 goto next_block; 343 case C2_CBR: { 344 int c = s->reg[in->a][0]; 345 int gt = AND(b, s->guard, c), gf = AND(b, s->guard, -c); 346 if (gt != F && gf != F) { 347 if (x->info->paths >= C2_BLAST_MAX_PATHS) return refuse(x, "BLAST-TOO-LARGE", "more than %d paths", C2_BLAST_MAX_PATHS); 348 St *o = &stack[(*sp)++]; 349 o->block = in->f; 350 o->guard = gf; 351 o->reg = malloc((size_t)f->nvregs * sizeof *o->reg); 352 if (!o->reg) return refuse(x, "BLAST-RESOURCE", "out of memory"); 353 memcpy(o->reg, s->reg, (size_t)f->nvregs * sizeof *o->reg); 354 x->info->paths++; 355 } 356 if (gt == F && gf == F) return 0; 357 s->block = gt != F ? in->t : in->f; 358 s->guard = gt != F ? gt : gf; 359 goto next_block; 360 } 361 default: 362 return refuse(x, "BLAST-UNSUPPORTED", "opcode %u (insn %u)", in->op, i); 363 } 364 if (!r) return refuse(x, "BLAST-RESOURCE", "out of memory"); 365 s->reg[in->dst] = r; 366 if (s->guard == F) return 0; /* every run of this path has trapped */ 367 } 368 return refuse(x, "BLAST-UNSUPPORTED", "block %d falls through", s->block); 369 next_block:; 370 } 371 } 372 373 int c2_func_index(const C2Unit *u, const char *name) 374 { 375 for (int i = 0; i < u->nfuncs; i++) 376 if (strcmp(u->fn[i].name, name) == 0) return i; 377 return -1; 378 } 379 380 int c2_blast_notrap(const C2Unit *u, int fi, C2Cnf *cnf, C2BlastInfo *info) 381 { 382 memset(cnf, 0, sizeof *cnf); 383 memset(info, 0, sizeof *info); 384 info->rule = "BLAST-OK"; 385 C2Verdict v; 386 if (c2_check_unit(u, &v)) { 387 info->rule = "BLAST-UNIT-REJECTED"; 388 snprintf(info->detail, sizeof info->detail, "%s", v.rule); 389 return -1; 390 } 391 if (fi < 0 || fi >= u->nfuncs) { info->rule = "BLAST-NO-FUNCTION"; return -1; } 392 const C2Func *f = &u->fn[fi]; 393 Ex x; 394 memset(&x, 0, sizeof x); 395 x.b.cnf = cnf; 396 x.u = u; 397 x.f = f; 398 x.info = info; 399 int rc = -1; 400 St *stack = calloc(C2_BLAST_MAX_PATHS + 1, sizeof *stack); 401 St s = {0, T, calloc((size_t)f->nvregs + 1, sizeof(int *))}; 402 int sp = 0; 403 if (!stack || !s.reg) { refuse(&x, "BLAST-RESOURCE", "out of memory"); goto out; } 404 for (int r = 0; r < f->nvregs; r++) 405 if (!(f->vt[r].s >= S_BOOL && f->vt[r].s <= S_I64)) { refuse(&x, "BLAST-UNSUPPORTED", "register v%d is not a scalar", r); goto out; } /* GUARD:blast-scalar-only */ 406 /* variable 1 is TRUE; argument bits are variables 2..inputs+1 */ 407 x.b.nvars = 1; 408 clause(&x.b, 1, T); 409 for (int r = 0; r < f->nparams; r++) { 410 int w = width(f->vt[r].s); 411 int *p = val(&x, w); 412 if (!p) { refuse(&x, "BLAST-RESOURCE", "out of memory"); goto out; } 413 for (int k = 0; k < w; k++) p[k] = newvar(&x.b); 414 s.reg[r] = p; 415 info->inputs += w; 416 } 417 info->paths = 1; 418 if (run(&x, &s, stack, &sp)) goto out; 419 while (sp > 0) { 420 St o = stack[--sp]; 421 int e = run(&x, &o, stack, &sp); 422 free(o.reg); 423 if (e) goto out; 424 } 425 /* some trap fires: OR of all trap sites (no site: the empty clause, unsatisfiable at once) */ 426 info->trap_sites = x.nbad; 427 if (c2_cnf_add(cnf, x.bad, x.nbad) || x.b.oom) { refuse(&x, "BLAST-RESOURCE", "out of memory"); goto out; } 428 cnf->nvars = x.b.nvars; 429 rc = 0; 430 out: 431 while (sp > 0) free(stack[--sp].reg); 432 free(stack); 433 free(s.reg); 434 for (long i = 0; i < x.npool; i++) free(x.pool[i]); 435 free(x.pool); 436 free(x.bad); 437 if (rc) c2_cnf_free(cnf); 438 return rc; 439 }