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/tests/checker2/test_c2_ir.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 / tests / checker2 / test_c2_ir.c` 1098 lines ยท 42843 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 /* test_c2_ir: checker2's decoder and acceptance rules (omega#446). 18 * 19 * Usage: test_c2_ir (tests/checker2/differential.sh fills it) 20 * 21 * 1. Every unit in the dir: strict decode, ACCEPT, and checker2's own re-encoding is byte-identical. 22 * 2. Robustness: truncated prefixes are refused; deterministic bit flips never crash (run under 23 * ASan/UBSan by the make target). 24 * 3. Planted defects: each named defect is applied to the first unit (sorted by name) where it 25 * fits, and checker2 must refuse the mutated unit with the expected rule, and refuse its bytes. 26 * A defect that fits no unit fails the test (no silent skips). 27 * 4. Hand-built units for the control-flow defects no producer output contains. */ 28 #include 29 #include 30 #include 31 #include 32 33 #include "c2_ir.h" 34 35 static int fails, checks; 36 #define CHECK(cond, ...) \ 37 do { \ 38 checks++; \ 39 if (!(cond)) { \ 40 fails++; \ 41 fprintf(stderr, "FAIL %s:%d: ", __FILE__, __LINE__); \ 42 fprintf(stderr, __VA_ARGS__); \ 43 fputc('\n', stderr); \ 44 } \ 45 } while (0) 46 47 typedef struct { char *name; uint8_t *b; size_t n; } Unit; 48 static Unit corpus[512]; 49 static int ncorpus; 50 51 static int load(const char *dir) 52 { 53 struct dirent **ents; 54 int n = scandir(dir, &ents, NULL, alphasort); 55 if (n < 0) { perror(dir); return -1; } 56 for (int i = 0; i < n; i++) { 57 size_t l = strlen(ents[i]->d_name); 58 if (l > 3 && strcmp(ents[i]->d_name + l - 3, ".ir") == 0 && ncorpus < 512) { 59 char path[4096]; 60 snprintf(path, sizeof path, "%s/%s", dir, ents[i]->d_name); 61 FILE *f = fopen(path, "rb"); 62 if (f) { 63 fseek(f, 0, SEEK_END); 64 long sz = ftell(f); 65 fseek(f, 0, SEEK_SET); 66 Unit *u = &corpus[ncorpus]; 67 u->b = malloc((size_t)sz + 1); 68 u->n = fread(u->b, 1, (size_t)sz, f); 69 u->name = strdup(ents[i]->d_name); 70 fclose(f); 71 ncorpus++; 72 } 73 } 74 free(ents[i]); 75 } 76 free(ents); 77 return 0; 78 } 79 80 /* ------------------------------------------------------------------ mutation helpers */ 81 82 static int isval(uint8_t s) { return s >= S_BOOL && s <= S_I64; } 83 static int isint(uint8_t s) { return s >= S_U8 && s <= S_I64; } 84 static unsigned widthof(uint8_t s) 85 { 86 static const unsigned w[] = {0, 1, 8, 16, 32, 64, 8, 16, 32, 64}; 87 return s <= S_I64 ? w[s] : 0; 88 } 89 90 /* first register >= from with a value type, matching want (0 any value, else that scalar) and 91 * differing from not (0 = no restriction); int_only restricts to integers. -1 if none. */ 92 static int reg_val(const C2Func *f, int from, uint8_t want, uint8_t not, int int_only) 93 { 94 for (int r = from; r < f->nvregs; r++) { 95 uint8_t s = f->vt[r].s; 96 if (!isval(s) || (want && s != want) || (not && s == not) || (int_only && !isint(s))) continue; 97 if (r > 0 && r - 1 < f->nparams && (f->vt[r - 1].s == S_BYTES || f->vt[r - 1].s == S_CELLS)) continue; 98 return r; 99 } 100 return -1; 101 } 102 static int reg_ref(const C2Func *f, int from) 103 { 104 for (int r = from; r < f->nvregs; r++) 105 if (f->vt[r].s == S_REF) return r; 106 return -1; 107 } 108 static int newreg(C2Func *f, C2Type t) 109 { 110 C2Type *nv = realloc(f->vt, (f->nvregs + 1u) * sizeof *nv); 111 if (!nv) abort(); 112 f->vt = nv; 113 f->vt[f->nvregs] = t; 114 return f->nvregs++; 115 } 116 static void ins(C2Func *f, uint32_t pos, C2Insn in) 117 { 118 C2Insn *ni = realloc(f->in, (f->ninsns + 1u) * sizeof *ni); 119 if (!ni) abort(); 120 f->in = ni; 121 memmove(&f->in[pos + 1], &f->in[pos], (f->ninsns - pos) * sizeof *f->in); 122 f->in[pos] = in; 123 f->ninsns++; 124 for (int b = 0; b < f->nblocks; b++) { 125 C2Block *k = &f->blk[b]; 126 if (pos >= k->first && pos < k->first + k->count) k->count++; 127 else if (k->first > pos) k->first++; 128 } 129 } 130 static void del(C2Func *f, uint32_t pos) 131 { 132 memmove(&f->in[pos], &f->in[pos + 1], (f->ninsns - pos - 1) * sizeof *f->in); 133 f->ninsns--; 134 for (int b = 0; b < f->nblocks; b++) { 135 C2Block *k = &f->blk[b]; 136 if (pos >= k->first && pos < k->first + k->count) k->count--; 137 else if (k->first > pos) k->first--; 138 } 139 } 140 static C2Insn mk(uint8_t op, int16_t a) 141 { 142 C2Insn in; 143 memset(&in, 0, sizeof in); 144 in.op = op; 145 in.dst = in.b = in.c = -1; 146 for (int k = 0; k < C2_MAX_PARAMS; k++) in.args[k] = -1; 147 in.a = a; 148 return in; 149 } 150 static int block_of(const C2Func *f, uint32_t i) 151 { 152 for (int b = 0; b < f->nblocks; b++) 153 if (i >= f->blk[b].first && i < f->blk[b].first + f->blk[b].count) return b; 154 return -1; 155 } 156 static int block_ends_ret(const C2Func *f, int b) { return f->in[f->blk[b].first + f->blk[b].count - 1].op == C2_RET; } 157 static int single_def_const(const C2Func *f, int r, uint32_t *at) 158 { 159 int n = r < f->nparams ? 1 : 0; 160 for (uint32_t i = 0; i < f->ninsns; i++) { 161 const C2Insn *in = &f->in[i]; 162 int d = -1; 163 switch (in->op) { 164 case C2_BR: case C2_CBR: case C2_RET: case C2_TRAP: case C2_STORE: case C2_FSTORE: case C2_RELEASE: 165 case C2_ADESTROY: case C2_PCLOSE: case C2_HFREE: case C2_HSTORE: case C2_SSTORE: break; 166 default: d = in->dst; 167 } 168 if (d == r) { n++; *at = i; } 169 } 170 return n == 1 && f->in[*at].op == C2_CONST; 171 } 172 /* a CBR closing a loop header: some block at or after it branches back to it */ 173 static int is_loop_header(const C2Func *f, int h) 174 { 175 for (int b = h; b < f->nblocks; b++) { 176 const C2Insn *t = &f->in[f->blk[b].first + f->blk[b].count - 1]; 177 if ((t->op == C2_BR && t->t == h) || (t->op == C2_CBR && (t->t == h || t->f == h))) return 1; 178 } 179 return 0; 180 } 181 182 #define EACH_INSN(op_) \ 183 for (int fi = 0; fi < u->nfuncs; fi++) \ 184 for (uint32_t i = 0; i < u->fn[fi].ninsns; i++) \ 185 if (u->fn[fi].in[i].op == (op_)) 186 #define FN (&u->fn[fi]) 187 #define IN (&u->fn[fi].in[i]) 188 189 /* ------------------------------------------------------------------ the defects */ 190 191 static int d_const_noncanon(C2Unit *u) 192 { 193 EACH_INSN(C2_CONST) { 194 uint8_t s = FN->vt[IN->dst].s; 195 if (s == S_BOOL) { IN->imm = 2; return 1; } 196 if (widthof(s) && widthof(s) < 64) { IN->imm = 1ull << widthof(s); return 1; } 197 } 198 return 0; 199 } 200 static int d_const_into_ref(C2Unit *u) 201 { 202 EACH_INSN(C2_CONST) { int r = reg_ref(FN, FN->nparams); if (r >= 0) { IN->dst = (int16_t)r; return 1; } } 203 return 0; 204 } 205 static int d_mov_scalar_mismatch(C2Unit *u) 206 { 207 EACH_INSN(C2_MOV) { 208 uint8_t s = FN->vt[IN->dst].s; 209 if (!isval(s)) continue; 210 int r = reg_val(FN, 0, 0, s, 0); 211 if (r >= 0) { IN->a = (int16_t)r; return 1; } 212 } 213 return 0; 214 } 215 static int d_mov_ref_from_scalar(C2Unit *u) 216 { 217 EACH_INSN(C2_MOV) { 218 if (FN->vt[IN->dst].s != S_REF) continue; 219 int r = reg_val(FN, 0, 0, 0, 0); 220 if (r >= 0) { IN->a = (int16_t)r; return 1; } 221 } 222 return 0; 223 } 224 static int d_mov_kind_widen(C2Unit *u) /* shared borrow into a mut or own destination */ 225 { 226 EACH_INSN(C2_MOV) { 227 const C2Type *td = &FN->vt[IN->dst]; 228 if (td->s != S_REF || td->kind == K_SHARED) continue; 229 for (int r = 0; r < FN->nvregs; r++) { 230 const C2Type *t = &FN->vt[r]; 231 if (t->s == S_REF && t->kind == K_SHARED && t->elem == td->elem && t->len == td->len && t->sid == td->sid) { 232 IN->a = (int16_t)r; 233 return 1; 234 } 235 } 236 } 237 return 0; 238 } 239 static int d_bin_subop(C2Unit *u) { EACH_INSN(C2_BIN) { IN->sub = B_LIMIT; return 1; } return 0; } 240 static int d_bin_mismatch(C2Unit *u) 241 { 242 EACH_INSN(C2_BIN) { 243 if (IN->sub == B_SHL || IN->sub == B_SHR) continue; 244 uint8_t s = FN->vt[IN->dst].s; 245 int r = reg_val(FN, 0, 0, s, 1); 246 if (isint(s) && r >= 0) { IN->b = (int16_t)r; return 1; } 247 } 248 return 0; 249 } 250 static int d_shift_bool(C2Unit *u) 251 { 252 EACH_INSN(C2_BIN) { 253 if (IN->sub != B_SHL && IN->sub != B_SHR) continue; 254 int r = reg_val(FN, 0, S_BOOL, 0, 0); 255 if (r >= 0) { IN->b = (int16_t)r; return 1; } 256 } 257 return 0; 258 } 259 static int d_bin_bool_arith(C2Unit *u) 260 { 261 EACH_INSN(C2_BIN) { if (FN->vt[IN->dst].s == S_BOOL) { IN->sub = B_ADD; return 1; } } 262 EACH_INSN(C2_CMP) { /* no bool BIN in the corpus: make one from a comparison result */ 263 int16_t d = IN->dst; 264 for (uint32_t j = i + 1; j < FN->ninsns; j++) 265 if (FN->in[j].op == C2_BIN && isint(FN->vt[FN->in[j].dst].s) && FN->in[j].sub == B_ADD) { 266 FN->in[j].dst = FN->in[j].a = FN->in[j].b = d; 267 return 1; 268 } 269 } 270 return 0; 271 } 272 static int d_un_subop(C2Unit *u) { EACH_INSN(C2_UN) { IN->sub = U_LIMIT; return 1; } return 0; } 273 static int d_un_lnot_int(C2Unit *u) 274 { 275 EACH_INSN(C2_UN) { if (IN->sub != U_LNOT) { IN->sub = U_LNOT; return 1; } } 276 return 0; 277 } 278 static int d_un_neg_bool(C2Unit *u) 279 { 280 EACH_INSN(C2_UN) { if (IN->sub == U_LNOT) { IN->sub = U_NEG; return 1; } } 281 return 0; 282 } 283 static int d_cmp_nonbool(C2Unit *u) 284 { 285 EACH_INSN(C2_CMP) { int r = reg_val(FN, FN->nparams, 0, S_BOOL, 1); if (r >= 0) { IN->dst = (int16_t)r; return 1; } } 286 return 0; 287 } 288 static int d_cmp_subop(C2Unit *u) { EACH_INSN(C2_CMP) { IN->sub = CC_LIMIT; return 1; } return 0; } 289 static int d_cmp_mixed(C2Unit *u) 290 { 291 EACH_INSN(C2_CMP) { 292 int r = reg_val(FN, 0, 0, FN->vt[IN->a].s, 0); 293 if (r >= 0) { IN->b = (int16_t)r; return 1; } 294 } 295 return 0; 296 } 297 static int d_cast_bool(C2Unit *u) 298 { 299 EACH_INSN(C2_CAST) { int r = reg_val(FN, 0, S_BOOL, 0, 0); if (r >= 0) { IN->a = (int16_t)r; return 1; } } 300 return 0; 301 } 302 static int d_alloc_nonowner(C2Unit *u) 303 { 304 EACH_INSN(C2_ALLOC) { int r = reg_val(FN, FN->nparams, 0, 0, 0); if (r >= 0) { IN->dst = (int16_t)r; return 1; } } 305 return 0; 306 } 307 static int d_alloc_init(C2Unit *u) 308 { 309 EACH_INSN(C2_ALLOC) { 310 const C2Type *t = &FN->vt[IN->dst]; 311 uint8_t want = t->sid ? S_U64 : t->elem; 312 int r = reg_val(FN, 0, 0, want, 0); 313 if (r >= 0) { IN->a = (int16_t)r; return 1; } 314 } 315 return 0; 316 } 317 static int d_release_scalar(C2Unit *u) 318 { 319 EACH_INSN(C2_RELEASE) { int r = reg_val(FN, 0, 0, 0, 0); if (r >= 0) { IN->a = (int16_t)r; return 1; } } 320 return 0; 321 } 322 static int d_load_index_bool(C2Unit *u) 323 { 324 EACH_INSN(C2_LOAD) { int r = reg_val(FN, 0, S_BOOL, 0, 0); if (r >= 0) { IN->b = (int16_t)r; return 1; } } 325 return 0; 326 } 327 static int d_load_nonarray(C2Unit *u) 328 { 329 EACH_INSN(C2_LOAD) { int r = reg_val(FN, 0, 0, 0, 0); if (r >= 0) { IN->a = (int16_t)r; return 1; } } 330 return 0; 331 } 332 static int d_load_elem(C2Unit *u) 333 { 334 EACH_INSN(C2_LOAD) { 335 int r = reg_val(FN, FN->nparams, 0, FN->vt[IN->a].elem, 0); 336 if (r >= 0) { IN->dst = (int16_t)r; return 1; } 337 } 338 return 0; 339 } 340 static int d_store_shared(C2Unit *u) 341 { 342 EACH_INSN(C2_STORE) { if (FN->vt[IN->a].kind == K_MUT) { FN->vt[IN->a].kind = K_SHARED; return 1; } } 343 return 0; 344 } 345 static int d_fload_field_range(C2Unit *u) 346 { 347 EACH_INSN(C2_FLOAD) { IN->imm = u->st[FN->vt[IN->a].sid - 1].nfields; return 1; } 348 return 0; 349 } 350 static int d_fload_type(C2Unit *u) 351 { 352 EACH_INSN(C2_FLOAD) { 353 uint8_t s = u->st[FN->vt[IN->a].sid - 1].f[IN->imm].s; 354 int r = reg_val(FN, FN->nparams, 0, s, 0); 355 if (r >= 0) { IN->dst = (int16_t)r; return 1; } 356 } 357 return 0; 358 } 359 static int d_fstore_elem_index(C2Unit *u) 360 { 361 EACH_INSN(C2_FSTORE) { 362 if (IN->b != -1) continue; 363 int r = reg_val(FN, 0, 0, 0, 1); 364 if (r >= 0) { IN->b = (int16_t)r; return 1; } 365 } 366 return 0; 367 } 368 static int d_fstore_shared(C2Unit *u) 369 { 370 EACH_INSN(C2_FSTORE) { if (FN->vt[IN->a].kind == K_MUT) { FN->vt[IN->a].kind = K_SHARED; return 1; } } 371 return 0; 372 } 373 static int d_arena_cap0(C2Unit *u) { EACH_INSN(C2_AOPEN) { IN->imm = 0; return 1; } return 0; } 374 static int d_arena_cap65(C2Unit *u) { EACH_INSN(C2_AOPEN) { IN->imm = C2_MAX_ARRAY + 1; return 1; } return 0; } 375 static int d_pool_cap17(C2Unit *u) { EACH_INSN(C2_POPEN) { IN->nargs = C2_POOL_SLOTS + 1; return 1; } return 0; } 376 static int d_pool_cap0(C2Unit *u) { EACH_INSN(C2_POPEN) { IN->nargs = 0; return 1; } return 0; } 377 static int d_pool_elem_ref(C2Unit *u) { EACH_INSN(C2_POPEN) { IN->sub = S_REF; return 1; } return 0; } 378 static int d_trap0(C2Unit *u) { EACH_INSN(C2_TRAP) { IN->imm = 0; return 1; } return 0; } 379 static int d_trap15(C2Unit *u) { EACH_INSN(C2_TRAP) { IN->imm = C2_TRAP_MAX + 1; return 1; } return 0; } 380 static int d_br_range(C2Unit *u) { EACH_INSN(C2_BR) { IN->t = FN->nblocks; return 1; } return 0; } 381 static int d_cbr_range(C2Unit *u) { EACH_INSN(C2_CBR) { IN->f = FN->nblocks; return 1; } return 0; } 382 static int d_cbr_nonbool(C2Unit *u) 383 { 384 EACH_INSN(C2_CBR) { int r = reg_val(FN, 0, 0, 0, 1); if (r >= 0) { IN->a = (int16_t)r; return 1; } } 385 return 0; 386 } 387 static int d_ret_type(C2Unit *u) 388 { 389 EACH_INSN(C2_RET) { 390 if (IN->a < 0) continue; 391 int r = reg_val(FN, 0, 0, FN->ret.s, 0); 392 if (r >= 0) { IN->a = (int16_t)r; return 1; } 393 } 394 return 0; 395 } 396 static int d_ret_void_value(C2Unit *u) 397 { 398 EACH_INSN(C2_RET) { if (IN->a < 0 && FN->nvregs > 0) { IN->a = 0; return 1; } } 399 return 0; 400 } 401 static int d_call_self(C2Unit *u) { EACH_INSN(C2_CALL) { IN->callee = (uint16_t)fi; return 1; } return 0; } 402 static int d_call_arity(C2Unit *u) 403 { 404 EACH_INSN(C2_CALL) { if (IN->nargs > 0) { IN->nargs--; return 1; } } 405 return 0; 406 } 407 static int d_call_arg_type(C2Unit *u) 408 { 409 EACH_INSN(C2_CALL) { 410 const C2Func *g = &u->fn[IN->callee]; 411 for (int k = 0; k < IN->nargs; k++) { 412 if (!isval(g->vt[k].s)) continue; 413 if (k > 0 && (g->vt[k - 1].s == S_BYTES || g->vt[k - 1].s == S_CELLS)) continue; 414 int r = reg_val(FN, 0, 0, g->vt[k].s, 0); 415 if (r >= 0) { IN->args[k] = (int16_t)r; return 1; } 416 } 417 } 418 return 0; 419 } 420 static int d_call_ref_kind(C2Unit *u) /* shared borrow passed to a mut parameter */ 421 { 422 EACH_INSN(C2_CALL) { 423 const C2Func *g = &u->fn[IN->callee]; 424 for (int k = 0; k < IN->nargs; k++) { 425 const C2Type *p = &g->vt[k]; 426 if (p->s != S_REF || p->kind != K_MUT) continue; 427 for (int r = 0; r < FN->nvregs; r++) { 428 const C2Type *t = &FN->vt[r]; 429 if (t->s == S_REF && t->kind == K_SHARED && t->elem == p->elem && t->len == p->len && t->sid == p->sid) { 430 IN->args[k] = (int16_t)r; 431 return 1; 432 } 433 } 434 } 435 } 436 return 0; 437 } 438 static int d_call_ret_type(C2Unit *u) 439 { 440 EACH_INSN(C2_CALL) { 441 if (IN->dst < 0) continue; 442 int r = reg_val(FN, FN->nparams, 0, FN->vt[IN->dst].s, 0); 443 if (r >= 0) { IN->dst = (int16_t)r; return 1; } 444 } 445 return 0; 446 } 447 static int d_call_alias(C2Unit *u) /* one register bound to two parameters, one of them writable */ 448 { 449 EACH_INSN(C2_CALL) { 450 const C2Func *g = &u->fn[IN->callee]; 451 for (int k = 0; k < IN->nargs; k++) 452 for (int j = 0; j < IN->nargs; j++) { 453 const C2Type *pk = &g->vt[k], *pj = &g->vt[j]; 454 if (j == k || pk->s != S_REF || pj->s != S_REF || pk->kind != K_MUT) continue; 455 if (pk->elem != pj->elem || pk->len != pj->len || pk->sid != pj->sid) continue; 456 IN->args[j] = IN->args[k]; 457 return 1; 458 } 459 } 460 return 0; 461 } 462 static int d_term_missing(C2Unit *u) 463 { 464 EACH_INSN(C2_BR) { IN->op = C2_TRAP; IN->imm = 1; return 1; } 465 return 0; 466 } 467 static int d_term_inside(C2Unit *u) 468 { 469 for (int fi = 0; fi < u->nfuncs; fi++) 470 for (int b = 0; b < FN->nblocks; b++) 471 if (FN->blk[b].count >= 2) { 472 C2Insn *in = &FN->in[FN->blk[b].first]; 473 *in = mk(C2_BR, -1); 474 in->t = 0; 475 return 1; 476 } 477 return 0; 478 } 479 static int d_block_gap(C2Unit *u) 480 { 481 for (int fi = 0; fi < u->nfuncs; fi++) 482 if (FN->nblocks >= 2 && FN->blk[1].count >= 2) { FN->blk[1].first++; FN->blk[1].count--; return 1; } 483 return 0; 484 } 485 static int d_undefined_use(C2Unit *u) 486 { 487 EACH_INSN(C2_BIN) { IN->a = (int16_t)newreg(FN, FN->vt[IN->a]); return 1; } 488 return 0; 489 } 490 static int d_operand_range(C2Unit *u) { EACH_INSN(C2_BIN) { IN->a = (int16_t)FN->nvregs; return 1; } return 0; } 491 static int slice_param(const C2Func *f, uint8_t s) 492 { 493 for (int r = 0; r + 1 < f->nparams; r++) 494 if (f->vt[r].s == s || (!s && (f->vt[r].s == S_BYTES || f->vt[r].s == S_CELLS))) return r; 495 return -1; 496 } 497 static int d_slice_len_write(C2Unit *u) 498 { 499 EACH_INSN(C2_CONST) { 500 int p = slice_param(FN, 0); 501 if (p >= 0 && FN->vt[IN->dst].s == S_U64) { IN->dst = (int16_t)(p + 1); return 1; } 502 } 503 return 0; 504 } 505 static int d_slice_ptr_write(C2Unit *u) 506 { 507 EACH_INSN(C2_CONST) { int p = slice_param(FN, 0); if (p >= 0) { IN->dst = (int16_t)p; return 1; } } 508 return 0; 509 } 510 static int d_sstore_bytes(C2Unit *u) 511 { 512 EACH_INSN(C2_SSTORE) { FN->vt[IN->a].s = S_BYTES; return 1; } 513 return 0; 514 } 515 static int d_slice_len_type(C2Unit *u) 516 { 517 for (int fi = 0; fi < u->nfuncs; fi++) { int p = slice_param(FN, 0); if (p >= 0) { FN->vt[p + 1].s = S_U32; return 1; } } 518 return 0; 519 } 520 static int d_slice_nonparam(C2Unit *u) 521 { 522 for (int fi = 0; fi < u->nfuncs; fi++) { int p = slice_param(FN, 0); if (p >= 0) { FN->nparams = (uint8_t)p; return 1; } } 523 return 0; 524 } 525 static int d_double_release(C2Unit *u) { EACH_INSN(C2_RELEASE) { ins(FN, i + 1, *IN); return 1; } return 0; } 526 static int d_use_after_release(C2Unit *u) 527 { 528 EACH_INSN(C2_LOAD) { 529 int16_t a = IN->a; 530 if (FN->vt[a].kind != K_OWN || a < FN->nparams) continue; 531 ins(FN, i, mk(C2_RELEASE, a)); 532 return 1; 533 } 534 return 0; 535 } 536 static int d_leak(C2Unit *u) 537 { 538 EACH_INSN(C2_RELEASE) { 539 if (!block_ends_ret(FN, block_of(FN, i))) continue; 540 del(FN, i); 541 return 1; 542 } 543 return 0; 544 } 545 static int d_owner_overwrite(C2Unit *u) { EACH_INSN(C2_ALLOC) { ins(FN, i + 1, *IN); return 1; } return 0; } 546 static int d_release_arena_obj(C2Unit *u) { EACH_INSN(C2_AALLOC) { ins(FN, i + 1, mk(C2_RELEASE, IN->dst)); return 1; } return 0; } 547 static int d_arena_alloc_after_destroy(C2Unit *u) { EACH_INSN(C2_AALLOC) { ins(FN, i, mk(C2_ADESTROY, IN->b)); return 1; } return 0; } 548 static int d_arena_double_destroy(C2Unit *u) { EACH_INSN(C2_ADESTROY) { ins(FN, i + 1, *IN); return 1; } return 0; } 549 static int d_pool_double_close(C2Unit *u) { EACH_INSN(C2_PCLOSE) { ins(FN, i + 1, *IN); return 1; } return 0; } 550 static int d_pool_use_after_close(C2Unit *u) { EACH_INSN(C2_HALLOC) { ins(FN, i, mk(C2_PCLOSE, IN->b)); return 1; } return 0; } 551 static int d_arena_leak(C2Unit *u) 552 { 553 EACH_INSN(C2_ADESTROY) { if (block_ends_ret(FN, block_of(FN, i))) { del(FN, i); return 1; } } 554 return 0; 555 } 556 static int d_pool_leak(C2Unit *u) 557 { 558 EACH_INSN(C2_PCLOSE) { if (block_ends_ret(FN, block_of(FN, i))) { del(FN, i); return 1; } } 559 return 0; 560 } 561 static int d_handle_as_data(C2Unit *u) 562 { 563 EACH_INSN(C2_AOPEN) { 564 int16_t h = IN->dst; 565 for (uint32_t j = 0; j < FN->ninsns; j++) { 566 C2Insn *o = &FN->in[j]; 567 if (o->op == C2_BIN && FN->vt[o->a].s == S_U64) { o->a = h; return 1; } 568 if (o->op == C2_CMP && FN->vt[o->a].s == S_U64) { o->a = h; return 1; } 569 } 570 C2Insn c = mk(C2_CMP, h); /* no u64 arithmetic nearby: compare the handle with itself */ 571 c.sub = CC_EQ; 572 c.b = h; 573 c.dst = (int16_t)newreg(FN, (C2Type){.s = S_BOOL}); 574 ins(FN, i + 1, c); 575 return 1; 576 } 577 return 0; 578 } 579 static int d_handle_forged(C2Unit *u) 580 { 581 EACH_INSN(C2_AALLOC) { IN->b = (int16_t)newreg(FN, (C2Type){.s = S_U64}); return 1; } 582 return 0; 583 } 584 static int d_pool_value_type(C2Unit *u) 585 { 586 EACH_INSN(C2_HLOAD) { 587 int r = newreg(FN, (C2Type){.s = FN->vt[IN->dst].s == S_U8 ? S_U16 : S_U8}); 588 if (r >= 0) { IN->dst = (int16_t)r; return 1; } 589 } 590 return 0; 591 } 592 /* the CMP feeding the CBR that ends a loop header block, with its bound K a single-def CONST */ 593 static int loop_guard(C2Func *f, uint32_t *cmp_at, uint32_t *k_at, int *hdr) 594 { 595 for (int h = 0; h < f->nblocks; h++) { 596 if (!is_loop_header(f, h)) continue; 597 for (int g = h; g < f->nblocks; g++) { 598 C2Insn *t = &f->in[f->blk[g].first + f->blk[g].count - 1]; 599 if (t->op != C2_CBR) continue; 600 for (uint32_t i = f->blk[g].first; i < f->blk[g].first + f->blk[g].count; i++) { 601 C2Insn *c = &f->in[i]; 602 if (c->op == C2_CMP && c->dst == t->a && (c->sub == CC_GE || c->sub == CC_LT) && 603 single_def_const(f, c->b, k_at)) { 604 *cmp_at = i; 605 *hdr = g; 606 return 1; 607 } 608 } 609 } 610 } 611 return 0; 612 } 613 static int d_loop_bound_huge(C2Unit *u) 614 { 615 for (int fi = 0; fi < u->nfuncs; fi++) { 616 uint32_t c, k; 617 int g; 618 if (!loop_guard(FN, &c, &k, &g)) continue; 619 uint8_t s = FN->vt[FN->in[c].b].s; 620 if (widthof(s) < 32) continue; 621 FN->in[k].imm = `2000000`; 622 return 1; 623 } 624 return 0; 625 } 626 static int d_loop_counter_down(C2Unit *u) 627 { 628 for (int fi = 0; fi < u->nfuncs; fi++) { 629 uint32_t c, k; 630 int g; 631 if (!loop_guard(FN, &c, &k, &g)) continue; 632 int16_t ctr = FN->in[c].a; 633 for (uint32_t i = 0; i < FN->ninsns; i++) 634 if (IN->op == C2_BIN && IN->dst == ctr && IN->a == ctr && IN->sub == B_ADD) { IN->sub = B_SUB; return 1; } 635 } 636 return 0; 637 } 638 static int d_loop_guard_swapped(C2Unit *u) 639 { 640 for (int fi = 0; fi < u->nfuncs; fi++) { 641 uint32_t c, k; 642 int g; 643 if (!loop_guard(FN, &c, &k, &g)) continue; 644 C2Insn *t = &FN->in[FN->blk[g].first + FN->blk[g].count - 1]; 645 uint16_t x = t->t; 646 t->t = t->f; 647 t->f = x; 648 return 1; 649 } 650 return 0; 651 } 652 static int d_struct_offset(C2Unit *u) 653 { 654 for (int s = 0; s < u->nstructs; s++) if (u->st[s].nfields >= 2) { u->st[s].f[1].off++; return 1; } 655 return 0; 656 } 657 static int d_struct_cells(C2Unit *u) { if (u->nstructs) { u->st[0].ncells++; return 1; } return 0; } 658 static int d_struct_dup_field(C2Unit *u) 659 { 660 for (int s = 0; s < u->nstructs; s++) 661 if (u->st[s].nfields >= 2) { strcpy(u->st[s].f[1].name, u->st[s].f[0].name); return 1; } 662 return 0; 663 } 664 static int d_struct_field_ref(C2Unit *u) { if (u->nstructs) { u->st[0].f[0].s = S_REF; return 1; } return 0; } 665 static int d_func_dup_name(C2Unit *u) { if (u->nfuncs >= 2) { strcpy(u->fn[1].name, u->fn[0].name); return 1; } return 0; } 666 static int d_func_ret_ref(C2Unit *u) 667 { 668 for (int fi = 0; fi < u->nfuncs; fi++) { 669 int r = reg_ref(FN, 0); 670 if (r >= 0) { FN->ret = FN->vt[r]; return 1; } 671 } 672 return 0; 673 } 674 static int d_vreg_kind(C2Unit *u) 675 { 676 for (int fi = 0; fi < u->nfuncs; fi++) { int r = reg_ref(FN, 0); if (r >= 0) { FN->vt[r].kind = 4; return 1; } } 677 return 0; 678 } 679 static int d_vreg_len0(C2Unit *u) 680 { 681 for (int fi = 0; fi < u->nfuncs; fi++) 682 for (int r = 0; r < FN->nvregs; r++) 683 if (FN->vt[r].s == S_REF && !FN->vt[r].sid) { FN->vt[r].len = 0; return 1; } 684 return 0; 685 } 686 static int d_vreg_sid(C2Unit *u) 687 { 688 for (int fi = 0; fi < u->nfuncs; fi++) 689 for (int r = 0; r < FN->nvregs; r++) 690 if (FN->vt[r].s == S_REF && FN->vt[r].sid) { FN->vt[r].sid = (uint8_t)(u->nstructs + 1); return 1; } 691 return 0; 692 } 693 static int d_params_over(C2Unit *u) { u->fn[0].nparams = C2_MAX_PARAMS + 1; return u->fn[0].nvregs > C2_MAX_PARAMS; } 694 static int d_borrow_outlives(C2Unit *u) 695 { 696 EACH_INSN(C2_MOV) { 697 int16_t b = IN->dst, o = IN->a; 698 if (FN->vt[b].s != S_REF || FN->vt[b].kind == K_OWN || FN->vt[o].kind != K_OWN || o < FN->nparams) continue; 699 int arena = 0; 700 for (uint32_t j = 0; j < FN->ninsns; j++) arena |= FN->in[j].op == C2_AALLOC && FN->in[j].dst == o; 701 if (arena) continue; 702 for (uint32_t j = i + 1; j < FN->ninsns && block_of(FN, j) == block_of(FN, i); j++) { 703 uint8_t op = FN->in[j].op; 704 if ((op == C2_LOAD || op == C2_STORE || op == C2_FLOAD || op == C2_FSTORE) && FN->in[j].a == b) { 705 ins(FN, j, mk(C2_RELEASE, o)); 706 return 1; 707 } 708 } 709 } 710 return 0; 711 } 712 static int d_use_after_move(C2Unit *u) 713 { 714 EACH_INSN(C2_CALL) { 715 const C2Func *g = &u->fn[IN->callee]; 716 for (int k = 0; k < IN->nargs; k++) 717 if (g->vt[k].s == S_REF && g->vt[k].kind == K_OWN) { ins(FN, i + 1, *IN); return 1; } 718 } 719 return 0; 720 } 721 static int d_mov_after_move(C2Unit *u) /* own -> own move, then the moved-from owner moved again */ 722 { 723 EACH_INSN(C2_MOV) { 724 if (FN->vt[IN->dst].kind != K_OWN || FN->vt[IN->a].s != S_REF || FN->vt[IN->a].kind != K_OWN) continue; 725 C2Insn again = *IN; 726 again.dst = (int16_t)newreg(FN, FN->vt[IN->dst]); 727 ins(FN, i + 1, again); 728 return 1; 729 } 730 return 0; 731 } 732 733 static int d_alloc_into_borrow(C2Unit *u) 734 { 735 EACH_INSN(C2_ALLOC) { 736 C2Type t = FN->vt[IN->dst]; 737 t.kind = K_MUT; 738 IN->dst = (int16_t)newreg(FN, t); 739 return 1; 740 } 741 return 0; 742 } 743 /* a table slot past nfields holding a plausible field: the field index must still be refused */ 744 static int d_field_stale_slot(C2Unit *u) 745 { 746 EACH_INSN(C2_FLOAD) { 747 C2Struct *s = &u->st[FN->vt[IN->a].sid - 1]; 748 if (s->nfields >= C2_MAX_FIELDS) continue; 749 s->f[s->nfields] = s->f[IN->imm]; 750 IN->imm = s->nfields; 751 return 1; 752 } 753 return 0; 754 } 755 /* a scalar register whose unused element byte happens to match: indexing it must be refused */ 756 static int d_index_scalar_stale_elem(C2Unit *u) 757 { 758 EACH_INSN(C2_LOAD) { 759 if (FN->vt[IN->a].sid) continue; 760 int r = reg_val(FN, 0, S_U64, 0, 0); 761 if (r < 0) continue; 762 FN->vt[r].elem = FN->vt[IN->a].elem; 763 IN->a = (int16_t)r; 764 return 1; 765 } 766 return 0; 767 } 768 static int d_sload_non_slice(C2Unit *u) 769 { 770 EACH_INSN(C2_SLOAD) { 771 int r = reg_val(FN, 0, S_U64, 0, 0); 772 IN->a = (int16_t)(r >= 0 ? r : newreg(FN, (C2Type){.s = S_U64})); 773 return 1; 774 } 775 return 0; 776 } 777 778 /* hand-built function bodies replacing fn[0] (control-flow shapes no producer emits) */ 779 static void replace_fn0(C2Unit *u, int nblocks, const C2Insn *code, int ncode, const int *counts) 780 { 781 C2Func *f = &u->fn[0]; 782 free(f->in); 783 free(f->blk); 784 f->in = calloc((size_t)ncode, sizeof *f->in); 785 f->blk = calloc((size_t)nblocks, sizeof *f->blk); 786 memcpy(f->in, code, (size_t)ncode * sizeof *code); 787 f->ninsns = (uint32_t)ncode; 788 f->nblocks = (uint16_t)nblocks; 789 uint32_t at = 0; 790 for (int b = 0; b < nblocks; b++) { f->blk[b].first = at; f->blk[b].count = (uint32_t)counts[b]; at += (uint32_t)counts[b]; } 791 f->ret.s = S_VOID; 792 } 793 static int d_irreducible(C2Unit *u) 794 { 795 /* v_bool <- param or const; B0: CBR c B1 B2; B1: BR B2; B2: BR B1 (two entries into one cycle) */ 796 C2Func *f = &u->fn[0]; 797 int c = reg_val(f, 0, S_BOOL, 0, 0); 798 if (c < 0 || c >= f->nparams) return 0; 799 C2Insn code[3] = {mk(C2_CBR, (int16_t)c), mk(C2_BR, -1), mk(C2_BR, -1)}; 800 code[0].t = 1; code[0].f = 2; code[1].t = 2; code[2].t = 1; 801 int counts[3] = {1, 1, 1}; 802 replace_fn0(u, 3, code, 3, counts); 803 return 1; 804 } 805 static int d_endless_loop(C2Unit *u) 806 { 807 C2Insn code[2] = {mk(C2_BR, -1), mk(C2_BR, -1)}; 808 code[0].t = 1; code[1].t = 1; 809 int counts[2] = {1, 1}; 810 if (u->fn[0].ret.s != S_VOID) return 0; 811 replace_fn0(u, 2, code, 2, counts); 812 return 1; 813 } 814 /* omega#465: fn0 rebuilt as straight code over new registers, so only its ownership can be wrong */ 815 static int fn0_owns_param(const C2Unit *u) 816 { 817 const C2Func *f = &u->fn[0]; 818 for (int r = 0; r < f->nparams; r++) 819 if (f->vt[r].s == S_REF && f->vt[r].kind == K_OWN) return 1; 820 return 0; 821 } 822 static int d_leak_one_path(C2Unit *u) 823 { 824 /* B0: x=0; y=1; c=(x==y); a=alloc[4](x); CBR c B1 B2 B1: RELEASE a; BR B2 B2: RET -- the 825 * false edge reaches the return still owning a */ 826 C2Func *f = &u->fn[0]; 827 if (fn0_owns_param(u)) return 0; 828 int x = newreg(f, (C2Type){.s = S_U32}), y = newreg(f, (C2Type){.s = S_U32}); 829 int c = newreg(f, (C2Type){.s = S_BOOL}); 830 int a = newreg(f, (C2Type){.s = S_REF, .kind = K_OWN, .elem = S_U32, .len = 4}); 831 C2Insn code[8] = {mk(C2_CONST, -1), mk(C2_CONST, -1), mk(C2_CMP, (int16_t)x), mk(C2_ALLOC, (int16_t)x), 832 mk(C2_CBR, (int16_t)c), mk(C2_RELEASE, (int16_t)a), mk(C2_BR, -1), mk(C2_RET, -1)}; 833 code[0].dst = (int16_t)x; 834 code[1].dst = (int16_t)y; code[1].imm = 1; 835 code[2].dst = (int16_t)c; code[2].b = (int16_t)y; code[2].sub = CC_EQ; 836 code[3].dst = (int16_t)a; 837 code[4].t = 1; code[4].f = 2; 838 code[6].t = 2; 839 int counts[3] = {5, 2, 1}; 840 replace_fn0(u, 3, code, 8, counts); 841 return 1; 842 } 843 static int d_mut_borrow_copied(C2Unit *u) 844 { 845 /* x=0; a=alloc[4](x); d=mut a; e=mut d; d[x]=x; e[x]=x; RELEASE a; RET -- d and e are two 846 * live writers of one array when d is written */ 847 C2Func *f = &u->fn[0]; 848 if (fn0_owns_param(u)) return 0; 849 int x = newreg(f, (C2Type){.s = S_U32}); 850 int a = newreg(f, (C2Type){.s = S_REF, .kind = K_OWN, .elem = S_U32, .len = 4}); 851 int d = newreg(f, (C2Type){.s = S_REF, .kind = K_MUT, .elem = S_U32, .len = 4}); 852 int e = newreg(f, (C2Type){.s = S_REF, .kind = K_MUT, .elem = S_U32, .len = 4}); 853 C2Insn code[8] = {mk(C2_CONST, -1), mk(C2_ALLOC, (int16_t)x), mk(C2_MOV, (int16_t)a), mk(C2_MOV, (int16_t)d), 854 mk(C2_STORE, (int16_t)d), mk(C2_STORE, (int16_t)e), mk(C2_RELEASE, (int16_t)a), mk(C2_RET, -1)}; 855 code[0].dst = (int16_t)x; 856 code[1].dst = (int16_t)a; 857 code[2].dst = (int16_t)d; 858 code[3].dst = (int16_t)e; 859 code[4].b = code[4].c = (int16_t)x; 860 code[5].b = code[5].c = (int16_t)x; 861 int counts[1] = {8}; 862 replace_fn0(u, 1, code, 8, counts); 863 return 1; 864 } 865 static int d_overwrite_one_path(C2Unit *u) 866 { 867 /* as d_leak_one_path, but B2: a=alloc[4](x); RELEASE a; RET -- on the false edge the second 868 * allocation overwrites a while it still owns the first */ 869 if (!d_leak_one_path(u)) return 0; 870 C2Func *f = &u->fn[0]; 871 int a = f->in[3].dst; 872 C2Insn al = f->in[3]; 873 ins(f, 7, mk(C2_RELEASE, (int16_t)a)); 874 ins(f, 7, al); 875 return 1; 876 } 877 878 typedef struct { const char *name, *rule; int (*apply)(C2Unit *); } Defect; 879 static const Defect defects[] = { 880 {"leak-on-one-path", "C2-OWN-LEAK", d_leak_one_path}, 881 {"mut-borrow-copied", "C2-BORROW-MUT-ALIAS", d_mut_borrow_copied}, 882 {"overwrite-on-one-path", "C2-OWN-LEAK", d_overwrite_one_path}, 883 {"const-noncanonical", "C2-CONST-CANONICAL", d_const_noncanon}, 884 {"const-into-reference", "C2-TYPE-CONST", d_const_into_ref}, 885 {"mov-scalar-mismatch", "C2-TYPE-MOV", d_mov_scalar_mismatch}, 886 {"mov-reference-from-scalar", "C2-TYPE-MOV", d_mov_ref_from_scalar}, 887 {"mov-shared-widened", "C2-TYPE-MOV", d_mov_kind_widen}, 888 {"bin-subop", "C2-DECODE-SUBOP", d_bin_subop}, 889 {"bin-operand-mismatch", "C2-TYPE-BIN", d_bin_mismatch}, 890 {"shift-by-bool", "C2-TYPE-BIN", d_shift_bool}, 891 {"bool-arithmetic", "C2-TYPE-BIN", d_bin_bool_arith}, 892 {"un-subop", "C2-DECODE-SUBOP", d_un_subop}, 893 {"lnot-on-int", "C2-TYPE-UN", d_un_lnot_int}, 894 {"neg-on-bool", "C2-TYPE-UN", d_un_neg_bool}, 895 {"cmp-result-not-bool", "C2-TYPE-CMP", d_cmp_nonbool}, 896 {"cmp-subop", "C2-DECODE-SUBOP", d_cmp_subop}, 897 {"cmp-mixed-types", "C2-TYPE-CMP", d_cmp_mixed}, 898 {"cast-from-bool", "C2-TYPE-CAST", d_cast_bool}, 899 {"alloc-into-scalar", "C2-TYPE-ALLOC", d_alloc_nonowner}, 900 {"alloc-init-type", "C2-TYPE-ALLOC", d_alloc_init}, 901 {"release-scalar", "C2-TYPE-RELEASE", d_release_scalar}, 902 {"index-by-bool", "C2-TYPE-INDEX", d_load_index_bool}, 903 {"index-non-array", "C2-TYPE-INDEX", d_load_nonarray}, 904 {"load-element-type", "C2-TYPE-INDEX", d_load_elem}, 905 {"store-through-shared", "C2-READ-ONLY", d_store_shared}, 906 {"field-out-of-range", "C2-TYPE-FIELD", d_fload_field_range}, 907 {"field-load-type", "C2-TYPE-FIELD", d_fload_type}, 908 {"field-scalar-indexed", "C2-TYPE-FIELD", d_fstore_elem_index}, 909 {"field-store-through-shared", "C2-READ-ONLY", d_fstore_shared}, 910 {"arena-capacity-0", "C2-ARENA-CAPACITY", d_arena_cap0}, 911 {"arena-capacity-65", "C2-ARENA-CAPACITY", d_arena_cap65}, 912 {"pool-slots-17", "C2-POOL-CAPACITY", d_pool_cap17}, 913 {"pool-slots-0", "C2-POOL-CAPACITY", d_pool_cap0}, 914 {"pool-of-references", "C2-TYPE-POOL", d_pool_elem_ref}, 915 {"trap-code-0", "C2-TRAP-CODE", d_trap0}, 916 {"trap-code-15", "C2-TRAP-CODE", d_trap15}, 917 {"branch-out-of-range", "C2-CFG-TARGET", d_br_range}, 918 {"cond-branch-out-of-range", "C2-CFG-TARGET", d_cbr_range}, 919 {"branch-on-int", "C2-TYPE-COND", d_cbr_nonbool}, 920 {"return-type", "C2-TYPE-RET", d_ret_type}, 921 {"void-returns-value", "C2-TYPE-RET", d_ret_void_value}, 922 {"recursive-call", "C2-CALL-ORDER", d_call_self}, 923 {"call-arity", "C2-CALL-ARITY", d_call_arity}, 924 {"call-argument-type", "C2-TYPE-CALL", d_call_arg_type}, 925 {"call-shared-to-mut", "C2-TYPE-CALL", d_call_ref_kind}, 926 {"call-result-type", "C2-TYPE-CALL", d_call_ret_type}, 927 {"call-aliased-mut", "C2-CALL-ALIAS", d_call_alias}, 928 {"block-without-terminator", "C2-CFG-TERMINATOR", d_term_missing}, 929 {"terminator-mid-block", "C2-CFG-TERMINATOR", d_term_inside}, 930 {"block-layout-gap", "C2-CFG-LAYOUT", d_block_gap}, 931 {"read-before-write", "C2-UNDEFINED-USE", d_undefined_use}, 932 {"operand-out-of-range", "C2-OPERAND-RANGE", d_operand_range}, 933 {"slice-length-written", "C2-SLICE-WRITE", d_slice_len_write}, 934 {"slice-pointer-written", "C2-SLICE-WRITE", d_slice_ptr_write}, 935 {"store-into-bytes-slice", "C2-SLICE-WRITE", d_sstore_bytes}, 936 {"slice-length-not-u64", "C2-SLICE-PARAM", d_slice_len_type}, 937 {"slice-not-a-parameter", "C2-SLICE-PARAM", d_slice_nonparam}, 938 {"double-release", "C2-OWN-DOUBLE-RELEASE", d_double_release}, 939 {"use-after-release", "C2-OWN-USE-DEAD", d_use_after_release}, 940 {"leak-at-return", "C2-OWN-LEAK", d_leak}, 941 {"owner-overwritten", "C2-OWN-LEAK", d_owner_overwrite}, 942 {"release-arena-object", "C2-OWN-RELEASE-ARENA", d_release_arena_obj}, 943 {"alloc-from-destroyed-arena", "C2-ARENA-DEAD", d_arena_alloc_after_destroy}, 944 {"arena-destroyed-twice", "C2-HANDLE-DOUBLE-CLOSE", d_arena_double_destroy}, 945 {"pool-closed-twice", "C2-HANDLE-DOUBLE-CLOSE", d_pool_double_close}, 946 {"pool-use-after-close", "C2-HANDLE-DEAD", d_pool_use_after_close}, 947 {"arena-never-destroyed", "C2-HANDLE-LEAK", d_arena_leak}, 948 {"pool-never-closed", "C2-HANDLE-LEAK", d_pool_leak}, 949 {"handle-used-as-data", "C2-HANDLE-DISCIPLINE", d_handle_as_data}, 950 {"forged-arena-handle", "C2-HANDLE-DISCIPLINE", d_handle_forged}, 951 {"pool-value-type", "C2-TYPE-POOL", d_pool_value_type}, 952 {"loop-bound-too-large", "C2-LOOP-UNBOUNDED", d_loop_bound_huge}, 953 {"loop-counter-decrements", "C2-LOOP-UNBOUNDED", d_loop_counter_down}, 954 {"loop-guard-inverted", "C2-LOOP-UNBOUNDED", d_loop_guard_swapped}, 955 {"struct-field-offset", "C2-STRUCT-LAYOUT", d_struct_offset}, 956 {"struct-cell-count", "C2-STRUCT-LAYOUT", d_struct_cells}, 957 {"struct-duplicate-field", "C2-STRUCT-NAME", d_struct_dup_field}, 958 {"struct-field-reference", "C2-STRUCT-FIELD-TYPE", d_struct_field_ref}, 959 {"duplicate-function", "C2-FUNC-NAME", d_func_dup_name}, 960 {"function-returns-reference", "C2-FUNC-RETURN", d_func_ret_ref}, 961 {"reference-kind-4", "C2-TYPE-INVALID", d_vreg_kind}, 962 {"array-length-0", "C2-TYPE-INVALID", d_vreg_len0}, 963 {"struct-id-out-of-range", "C2-TYPE-INVALID", d_vreg_sid}, 964 {"seven-parameters", "C2-FUNC-PARAMS", d_params_over}, 965 {"borrow-outlives-owner", "C2-BORROW-OUTLIVES-OWNER", d_borrow_outlives}, 966 {"use-after-move-into-call", "C2-OWN-USE-DEAD", d_use_after_move}, 967 {"move-after-move", "C2-OWN-USE-DEAD", d_mov_after_move}, 968 {"irreducible-cycle", "C2-CFG-IRREDUCIBLE", d_irreducible}, 969 {"endless-loop", "C2-LOOP-UNBOUNDED", d_endless_loop}, 970 {"alloc-into-borrow", "C2-TYPE-ALLOC", d_alloc_into_borrow}, 971 {"field-index-past-table", "C2-TYPE-FIELD", d_field_stale_slot}, 972 {"index-scalar-register", "C2-TYPE-INDEX", d_index_scalar_stale_elem}, 973 {"slice-load-from-scalar", "C2-TYPE-SLICE", d_sload_non_slice}, 974 }; 975 #define NDEFECTS ((int)(sizeof defects / sizeof defects[0])) 976 977 /* ------------------------------------------------------------------ byte-level defects */ 978 979 static int bytes_reject(const uint8_t *b, size_t n, const char *want_rule, const char *what) 980 { 981 C2Verdict v; 982 int rc = c2_check_bytes(b, n, &v); 983 CHECK(rc != 0, "%s: bytes accepted", what); 984 if (rc != 0 && want_rule) CHECK(strcmp(v.rule, want_rule) == 0, "%s: rule %s, want %s", what, v.rule, want_rule); 985 return rc != 0; 986 } 987 988 static int byte_defects(void) 989 { 990 int planted = 0; 991 const Unit *s = NULL, *plain = NULL; 992 for (int i = 0; i < ncorpus; i++) { 993 if (!s && corpus[i].b[7] >= 2) s = &corpus[i]; 994 if (!plain && corpus[i].b[7] == 1) plain = &corpus[i]; 995 } 996 CHECK(s && plain, "corpus needs a version-1 and a version>=2 unit"); 997 if (!s || !plain) return 0; 998 uint8_t *b = malloc(s->n + 1); 999 memcpy(b, s->b, s->n); 1000 b[0] ^= 1; 1001 planted += bytes_reject(b, s->n, "C2-DECODE-MAGIC", "bad-magic"); 1002 memcpy(b, s->b, s->n); 1003 b[7] = 6; 1004 planted += bytes_reject(b, s->n, "C2-DECODE-VERSION", "unknown-version"); 1005 memcpy(b, s->b, s->n); 1006 b[s->n] = 0; 1007 planted += bytes_reject(b, s->n + 1, "C2-DECODE-TRAILING", "trailing-byte"); 1008 planted += bytes_reject(s->b, s->n - 1, NULL, "truncated-by-one"); 1009 free(b); 1010 /* a version-1 unit relabelled as version 2 needs a struct table it does not have */ 1011 b = malloc(plain->n + 1); 1012 memcpy(b, plain->b, 7); 1013 b[7] = 2; 1014 b[8] = 0; /* empty struct table: well formed but the canonical version for no structs is 1 */ 1015 memcpy(b + 9, plain->b + 8, plain->n - 8); 1016 planted += bytes_reject(b, plain->n + 1, "C2-DECODE-VERSION-NONCANONICAL", "version-not-canonical"); 1017 free(b); 1018 return planted; 1019 } 1020 1021 /* ------------------------------------------------------------------ main */ 1022 1023 int main(int argc, char **argv) 1024 { 1025 if (argc != 2) { fprintf(stderr, "usage: test_c2_ir \n"); return 2; } 1026 if (load(argv[1]) || ncorpus == 0) { fprintf(stderr, "test_c2_ir: no .ir files in %s\n", argv[1]); return 2; } 1027 static uint8_t enc[1 << 20]; 1028 C2Unit *u = malloc(sizeof *u); 1029 C2Verdict v; 1030 1031 /* 1. accept and round-trip */ 1032 int accepted = 0; 1033 for (int i = 0; i < ncorpus; i++) { 1034 int d = c2_decode(corpus[i].b, corpus[i].n, u, &v); 1035 CHECK(d == 0, "%s: decode refused: %s %s", corpus[i].name, v.rule, v.detail); 1036 if (d == 0) { 1037 int c = c2_check_unit(u, &v); 1038 CHECK(c == 0, "%s: rejected: %s f%d i%d %s", corpus[i].name, v.rule, v.func, v.insn, v.detail); 1039 size_t n = c2_encode(u, enc, sizeof enc); 1040 CHECK(n == corpus[i].n && memcmp(enc, corpus[i].b, n) == 0, "%s: re-encoding differs", corpus[i].name); 1041 accepted += c == 0; 1042 } 1043 c2_unit_free(u); 1044 } 1045 CHECK(accepted == ncorpus, "accepted %d of %d", accepted, ncorpus); 1046 1047 /* 2. robustness: truncations refused; bit flips never crash */ 1048 int truncs = 0, flips = 0, flip_rejects = 0; 1049 for (int i = 0; i < ncorpus; i++) { 1050 size_t n = corpus[i].n; 1051 for (size_t k = 0; k < 72; k++) { 1052 size_t cut = k < 64 ? n * k / 64 : n - (72 - k); 1053 if (cut >= n) continue; 1054 truncs++; 1055 CHECK(c2_check_bytes(corpus[i].b, cut, &v) != 0, "%s: prefix of %zu bytes accepted", corpus[i].name, cut); 1056 } 1057 uint8_t *b = malloc(n); 1058 for (int k = 0; k < 96; k++) { 1059 memcpy(b, corpus[i].b, n); 1060 size_t pos = (size_t)(((uint64_t)k * 2654435761u + (uint64_t)i * 40503u) % n); 1061 b[pos] ^= (uint8_t)(1u << (k & 7)); 1062 flips++; 1063 flip_rejects += c2_check_bytes(b, n, &v) != 0; 1064 } 1065 free(b); 1066 } 1067 1068 /* 3. planted defects */ 1069 int planted = 0; 1070 for (int d = 0; d < NDEFECTS; d++) { 1071 int applied = 0; 1072 for (int i = 0; i < ncorpus && !applied; i++) { 1073 if (c2_decode(corpus[i].b, corpus[i].n, u, &v)) { c2_unit_free(u); continue; } 1074 if (defects[d].apply(u)) { 1075 applied = 1; 1076 int c = c2_check_unit(u, &v); 1077 CHECK(c != 0, "defect %s on %s: accepted", defects[d].name, corpus[i].name); 1078 if (c != 0) 1079 CHECK(strcmp(v.rule, defects[d].rule) == 0, "defect %s on %s: rule %s (%s), want %s", defects[d].name, 1080 corpus[i].name, v.rule, v.detail, defects[d].rule); 1081 int ok = c != 0 && strcmp(v.rule, defects[d].rule) == 0; 1082 size_t n = c2_encode(u, enc, sizeof enc); 1083 if (n) CHECK(c2_check_bytes(enc, n, &v) != 0, "defect %s on %s: its bytes are accepted", defects[d].name, corpus[i].name); 1084 planted += ok; 1085 } 1086 c2_unit_free(u); 1087 } 1088 CHECK(applied, "defect %s fits no corpus unit", defects[d].name); 1089 } 1090 planted += byte_defects(); 1091 CHECK(planted >= 50, "gate needs >= 50 planted defects refused, got %d", planted); 1092 free(u); 1093 1094 printf("test_c2_ir: %d units accepted and re-encoded identically, %d truncations refused, %d bit flips (%d refused), " 1095 "%d planted defects refused; %d checks, %d failed\n", 1096 accepted, truncs, flips, flip_rejects, planted, checks, fails); 1097 return fails ? 1 : 0; 1098 }