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/aien-protocols-mv/blob/main/specs/microjob/tools/mjb_verify.c # aien-protocols-mv Canonical specifications, wire protocols, and schemas for the AIEN sovereign ecosystem Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-protocols-mv / specs / microjob / tools / mjb_verify.c` 819 lines ยท 40571 bytes Raw Blame History 1 /* mjb_verify.c: reference checker for MJB1, the typed bounded microjob DAG. 2 * Spec: ../SPEC.md (Draft v0.1, contract 0.1.0, wire version 1). 3 * 4 * Self-contained C99: its own SHA-256 (FIPS 180-4), libc only, no heap 5 * (static buffers), no clock. mjb_check() is a pure function of one 6 * read-only buffer. It pins the static document rules of SPEC.md section 4.4 7 * with vectors; it is not linked into any runtime. Receipt rules (section 5) 8 * are stateful and live in the Rust ledger (crates/aien-microjob). 9 * 10 * Usage: 11 * mjb_verify FILE print one verdict line 12 * mjb_verify --corpus DIR check every DIR/expected.txt line, print 13 * MJB1_CONFORMANCE impl=c pass=N fail=M 14 * mjb_verify --make-vectors DIR write the vector files and DIR/expected.txt 15 * 16 * Verdict line (the format expected.txt pins): 17 * ok nodes=N incarnation=I ceiling=C objective=H64 tenant=H64 digest=H64 18 * order=i,j,... (one line; canonical Kahn order, lowest index first) 19 * refuse CODE (CODE is a name from SPEC.md section 6) 20 * 21 * Exit status: 0 on accept / all corpus lines agree, 1 otherwise, 2 on usage 22 * or I/O error. 23 * 24 * Mutation hooks: building with -DMJB_MUTANT=K disables check K (the CHK_* 25 * numbers below). tools/mutate-verifier.sh builds every mutant and requires 26 * the vectors to fail each one, which shows every check is exercised. 27 */ 28 #define _POSIX_C_SOURCE 200809L /* open, fdopen, close under -std=c99 */ 29 #include 30 #include 31 #include 32 #include 33 #include 34 #include 35 36 #ifndef MJB_MUTANT 37 #define MJB_MUTANT 0 38 #endif 39 #define CHECK(k) (MJB_MUTANT != (k)) 40 41 enum { 42 CHK_MIN_LEN = 1, CHK_MAGIC, CHK_DOWNGRADE, CHK_VERSION, CHK_COUNT, CHK_EXACT_LEN, 43 CHK_RES0, CHK_CEILING, CHK_OBJECTIVE, CHK_TENANT, CHK_SUBJECT, CHK_APPROVAL, 44 CHK_INCARNATION, CHK_JOB, CHK_IDEM, CHK_GEN, CHK_RES1, CHK_DEP_SLOTS, 45 CHK_OPERAND_SLOTS, CHK_OPERAND_PAD, CHK_DIMS, CHK_E_KIND_OP, CHK_E_PROFILE, 46 CHK_E_EFFECT, CHK_E_VERIFIER, CHK_E_RETRY, CHK_E_ATTEMPTS, CHK_E_CANCEL, 47 CHK_E_PRECISION, CHK_E_SUBSTRATE, CHK_E_DEPN, CHK_E_OPN, CHK_E_RTYPE, CHK_E_RANK, 48 CHK_E_PURE, CHK_E_OPKIND, CHK_TENANT_MATCH, CHK_CAPABILITY, CHK_EFFECT_CEIL, 49 CHK_VERIFIER_ZERO, CHK_VERIFIER_REF, CHK_RECOMPUTE_REF, CHK_RECOMPUTE_PURE, 50 CHK_IMPURE_NEURAL, CHK_IMPURE_EFFECT, CHK_COMPUTE_TOKENS, CHK_NEURAL_TOKENS, 51 CHK_EXPANSION, CHK_RETRY_NEVER, CHK_RETRY_EFFECT, CHK_ZERO_CAP, CHK_NODE_EXPIRY, 52 CHK_OPERAND_MISSING, CHK_DUP, CHK_DEP_RANGE, CHK_STALE, CHK_CYCLE, CHK_BUDGET, 53 CHK_LAST 54 }; 55 56 /* ---- refusal codes (spec section 6, document part) ------------------------ */ 57 enum { 58 MJB_OK = 0, MJB_BAD_LENGTH, MJB_BAD_MAGIC, MJB_DOWNGRADE, MJB_BAD_VERSION, 59 MJB_BAD_COUNT, MJB_RESERVED_NONZERO, MJB_BAD_ENUM, MJB_NO_OBJECTIVE, MJB_NO_TENANT, 60 MJB_NO_SUBJECT, MJB_NO_APPROVAL, MJB_BAD_EXPIRY, MJB_NO_JOB, MJB_CROSS_TENANT, 61 MJB_NO_CAPABILITY, MJB_UNAUTHORIZED_EFFECT, MJB_NO_VERIFIER, MJB_IMPURE, 62 MJB_TOKEN_PROFILE, MJB_NEEDS_EXPANSION, MJB_BAD_RETRY, MJB_ZERO_CAP, 63 MJB_MISSING_OPERAND, MJB_DUPLICATE_JOB, MJB_MISSING_DEP, MJB_STALE_GENERATION, 64 MJB_CYCLE, MJB_OVER_BUDGET, MJB_CODE_COUNT 65 }; 66 static const char *const code_names[MJB_CODE_COUNT] = { 67 "OK", "MJB_BAD_LENGTH", "MJB_BAD_MAGIC", "MJB_DOWNGRADE", "MJB_BAD_VERSION", 68 "MJB_BAD_COUNT", "MJB_RESERVED_NONZERO", "MJB_BAD_ENUM", "MJB_NO_OBJECTIVE", 69 "MJB_NO_TENANT", "MJB_NO_SUBJECT", "MJB_NO_APPROVAL", "MJB_BAD_EXPIRY", "MJB_NO_JOB", 70 "MJB_CROSS_TENANT", "MJB_NO_CAPABILITY", "MJB_UNAUTHORIZED_EFFECT", "MJB_NO_VERIFIER", 71 "MJB_IMPURE", "MJB_TOKEN_PROFILE", "MJB_NEEDS_EXPANSION", "MJB_BAD_RETRY", 72 "MJB_ZERO_CAP", "MJB_MISSING_OPERAND", "MJB_DUPLICATE_JOB", "MJB_MISSING_DEP", 73 "MJB_STALE_GENERATION", "MJB_CYCLE", "MJB_OVER_BUDGET" 74 }; 75 76 /* ---- SHA-256 ------------------------------------------------------------ */ 77 typedef struct { uint32_t h[8]; uint64_t n; uint8_t b[64]; uint32_t used; } sha256; 78 79 static const uint32_t K256[64] = { 80 0x428a2f98,0x71374491,0xb5c0fbcf,0xe9b5dba5,0x3956c25b,0x59f111f1,0x923f82a4,0xab1c5ed5, 81 0xd807aa98,0x12835b01,0x243185be,0x550c7dc3,0x72be5d74,0x80deb1fe,0x9bdc06a7,0xc19bf174, 82 0xe49b69c1,0xefbe4786,0x0fc19dc6,0x240ca1cc,0x2de92c6f,0x4a7484aa,0x5cb0a9dc,0x76f988da, 83 0x983e5152,0xa831c66d,0xb00327c8,0xbf597fc7,0xc6e00bf3,0xd5a79147,0x06ca6351,0x14292967, 84 0x27b70a85,0x2e1b2138,0x4d2c6dfc,0x53380d13,0x650a7354,0x766a0abb,0x81c2c92e,0x92722c85, 85 0xa2bfe8a1,0xa81a664b,0xc24b8b70,0xc76c51a3,0xd192e819,0xd6990624,0xf40e3585,0x106aa070, 86 0x19a4c116,0x1e376c08,0x2748774c,0x34b0bcb5,0x391c0cb3,0x4ed8aa4a,0x5b9cca4f,0x682e6ff3, 87 0x748f82ee,0x78a5636f,0x84c87814,0x8cc70208,0x90befffa,0xa4506ceb,0xbef9a3f7,0xc67178f2 88 }; 89 #define ROR(x, r) (((x) >> (r)) | ((x) << (32 - (r)))) 90 91 static void sha_block(sha256 *s, const uint8_t *p) { 92 uint32_t w[64], a, b, c, d, e, f, g, h; 93 for (int i = 0; i < 16; i++) 94 w[i] = (uint32_t)p[4*i] << 24 | (uint32_t)p[4*i+1] << 16 | (uint32_t)p[4*i+2] << 8 | p[4*i+3]; 95 for (int i = 16; i < 64; i++) { 96 uint32_t s0 = ROR(w[i-15], 7) ^ ROR(w[i-15], 18) ^ (w[i-15] >> 3); 97 uint32_t s1 = ROR(w[i-2], 17) ^ ROR(w[i-2], 19) ^ (w[i-2] >> 10); 98 w[i] = w[i-16] + s0 + w[i-7] + s1; 99 } 100 a = s->h[0]; b = s->h[1]; c = s->h[2]; d = s->h[3]; 101 e = s->h[4]; f = s->h[5]; g = s->h[6]; h = s->h[7]; 102 for (int i = 0; i < 64; i++) { 103 uint32_t t1 = h + (ROR(e, 6) ^ ROR(e, 11) ^ ROR(e, 25)) + ((e & f) ^ (~e & g)) + K256[i] + w[i]; 104 uint32_t t2 = (ROR(a, 2) ^ ROR(a, 13) ^ ROR(a, 22)) + ((a & b) ^ (a & c) ^ (b & c)); 105 h = g; g = f; f = e; e = d + t1; d = c; c = b; b = a; a = t1 + t2; 106 } 107 s->h[0] += a; s->h[1] += b; s->h[2] += c; s->h[3] += d; 108 s->h[4] += e; s->h[5] += f; s->h[6] += g; s->h[7] += h; 109 } 110 static void sha_init(sha256 *s) { 111 static const uint32_t iv[8] = { 0x6a09e667,0xbb67ae85,0x3c6ef372,0xa54ff53a, 112 0x510e527f,0x9b05688c,0x1f83d9ab,0x5be0cd19 }; 113 memcpy(s->h, iv, sizeof iv); s->n = 0; s->used = 0; 114 } 115 static void sha_update(sha256 *s, const void *data, size_t len) { 116 const uint8_t *p = data; 117 s->n += len; 118 while (len) { 119 uint32_t k = 64 - s->used; 120 if (k > len) k = (uint32_t)len; 121 memcpy(s->b + s->used, p, k); 122 s->used += k; p += k; len -= k; 123 if (s->used == 64) { sha_block(s, s->b); s->used = 0; } 124 } 125 } 126 static void sha_final(sha256 *s, uint8_t out[32]) { 127 uint64_t bits = s->n * 8; 128 uint8_t pad = 0x80, z = 0, l[8]; 129 sha_update(s, &pad, 1); 130 while (s->used != 56) sha_update(s, &z, 1); 131 for (int i = 0; i < 8; i++) l[i] = (uint8_t)(bits >> (56 - 8 * i)); 132 sha_update(s, l, 8); 133 for (int i = 0; i < 8; i++) { 134 out[4*i] = (uint8_t)(s->h[i] >> 24); out[4*i+1] = (uint8_t)(s->h[i] >> 16); 135 out[4*i+2] = (uint8_t)(s->h[i] >> 8); out[4*i+3] = (uint8_t)s->h[i]; 136 } 137 } 138 static void sha_once(const void *p, size_t n, uint8_t out[32]) { 139 sha256 s; sha_init(&s); sha_update(&s, p, n); sha_final(&s, out); 140 } 141 142 /* ---- big-endian helpers -------------------------------------------------- */ 143 static uint16_t g16(const uint8_t *p) { return (uint16_t)(p[0] << 8 | p[1]); } 144 static uint32_t g32(const uint8_t *p) { return (uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 | p[3]; } 145 static uint64_t g64(const uint8_t *p) { return (uint64_t)g32(p) << 32 | g32(p + 4); } 146 static void p16(uint8_t *p, uint16_t v) { p[0] = (uint8_t)(v >> 8); p[1] = (uint8_t)v; } 147 static void p32(uint8_t *p, uint32_t v) { p[0] = (uint8_t)(v >> 24); p[1] = (uint8_t)(v >> 16); p[2] = (uint8_t)(v >> 8); p[3] = (uint8_t)v; } 148 static void p64(uint8_t *p, uint64_t v) { p32(p, (uint32_t)(v >> 32)); p32(p + 4, (uint32_t)v); } 149 static int all_zero(const uint8_t *p, size_t n) { while (n--) if (*p++) return 0; return 1; } 150 151 /* ---- the format (spec section 4) ------------------------------------------ */ 152 #define HDR 192u 153 #define NODE 448u 154 #define MAXN 64u 155 #define MAXDEP 8u 156 #define MAXOPS 4u 157 #define DEPLEN 6u 158 #define OPLEN 40u 159 #define MAXDOC (HDR + MAXN * NODE) 160 161 #define H_VERSION 4 162 #define H_CEILING 5 163 #define H_COUNT 6 164 #define H_OBJECTIVE 8 165 #define H_TENANT 40 166 #define H_SUBJECT 72 167 #define H_APPROVAL 104 168 #define H_INCARNATION 136 169 #define H_RES0 140 170 #define H_EXPIRES 144 171 #define H_B_TIME 152 172 #define H_B_FUEL 160 173 #define H_B_MEMORY 168 174 #define H_B_WORK 176 175 #define H_B_IN 184 176 #define H_B_OUT 188 177 178 #define N_JOB 0 179 #define N_IDEM 16 180 #define N_GEN 32 181 #define N_KIND 36 182 #define N_OP 37 183 #define N_PROFILE 38 184 #define N_EFFECT 39 185 #define N_IN 40 186 #define N_OUT 42 187 #define N_REQ_IN 44 188 #define N_REQ_OUT 46 189 #define N_VERIFIER 48 190 #define N_RETRY 49 191 #define N_ATTEMPTS 50 192 #define N_CANCEL 51 193 #define N_PRECISION 52 194 #define N_SUBSTRATE 53 195 #define N_DEPS_N 54 196 #define N_OPS_N 55 197 #define N_RTYPE 56 198 #define N_RANK 57 199 #define N_PURE 58 200 #define N_RES1 59 201 #define N_TIME 60 202 #define N_FUEL 64 203 #define N_MEMORY 72 204 #define N_WORK 80 205 #define N_EXPIRES 88 206 #define N_DIMS 96 207 #define N_CAP 112 208 #define N_TENANT 144 209 #define N_MODEL 176 210 #define N_VREF 208 211 #define N_DEPS 240 212 #define N_OPERANDS 288 213 214 static const uint16_t cap_in[5] = { 0, 64, 128, 256, 512 }; 215 static const uint16_t cap_out[5] = { 0, 16, 32, 64, 128 }; 216 217 /* Rules 10 to 23 for one node record. Loops are clamped to the slot counts so 218 * that every mutant stays memory-safe. */ 219 static int check_node(const uint8_t *n, const uint8_t *h) { 220 unsigned depn = n[N_DEPS_N], opn = n[N_OPS_N], rank = n[N_RANK]; 221 unsigned dlim = depn < MAXDEP ? depn : MAXDEP, olim = opn < MAXOPS ? opn : MAXOPS; 222 unsigned k = n[N_KIND], op = n[N_OP], prof = n[N_PROFILE], vk = n[N_VERIFIER], s; 223 unsigned pidx = prof <= 4 ? prof : 0; 224 int pure = n[N_PURE] == 1, vz = all_zero(n + N_VREF, 32), mz = all_zero(n + N_MODEL, 32); 225 uint16_t tin = g16(n + N_IN), tout = g16(n + N_OUT), rin = g16(n + N_REQ_IN), rout = g16(n + N_REQ_OUT); 226 uint64_t exp = g64(n + N_EXPIRES); 227 228 if (CHECK(CHK_JOB) && all_zero(n + N_JOB, 16)) return MJB_NO_JOB; 229 if (CHECK(CHK_IDEM) && all_zero(n + N_IDEM, 16)) return MJB_NO_JOB; 230 if (CHECK(CHK_GEN) && g32(n + N_GEN) == 0) return MJB_NO_JOB; 231 232 if (CHECK(CHK_RES1) && n[N_RES1]) return MJB_RESERVED_NONZERO; 233 if (CHECK(CHK_DEP_SLOTS)) 234 for (s = dlim; s < MAXDEP; s++) if (!all_zero(n + N_DEPS + DEPLEN * s, DEPLEN)) return MJB_RESERVED_NONZERO; 235 if (CHECK(CHK_OPERAND_SLOTS)) 236 for (s = olim; s < MAXOPS; s++) if (!all_zero(n + N_OPERANDS + OPLEN * s, OPLEN)) return MJB_RESERVED_NONZERO; 237 if (CHECK(CHK_OPERAND_PAD)) 238 for (s = 0; s < olim; s++) if (!all_zero(n + N_OPERANDS + OPLEN * s + 1, 3)) return MJB_RESERVED_NONZERO; 239 if (CHECK(CHK_DIMS)) 240 for (s = rank < 4 ? rank : 4; s < 4; s++) if (g32(n + N_DIMS + 4 * s)) return MJB_RESERVED_NONZERO; 241 242 if (CHECK(CHK_E_KIND_OP) && !((k == 1 && op >= 1 && op <= 6) || (k == 2 && op >= 1 && op <= 4))) return MJB_BAD_ENUM; 243 if (CHECK(CHK_E_PROFILE) && prof > 4) return MJB_BAD_ENUM; 244 if (CHECK(CHK_E_EFFECT) && n[N_EFFECT] > 2) return MJB_BAD_ENUM; 245 if (CHECK(CHK_E_VERIFIER) && vk > 3) return MJB_BAD_ENUM; 246 if (CHECK(CHK_E_RETRY) && (n[N_RETRY] < 1 || n[N_RETRY] > 3)) return MJB_BAD_ENUM; 247 if (CHECK(CHK_E_ATTEMPTS) && (n[N_ATTEMPTS] < 1 || n[N_ATTEMPTS] > 8)) return MJB_BAD_ENUM; 248 if (CHECK(CHK_E_CANCEL) && (n[N_CANCEL] < 1 || n[N_CANCEL] > 2)) return MJB_BAD_ENUM; 249 if (CHECK(CHK_E_PRECISION) && n[N_PRECISION] > 6) return MJB_BAD_ENUM; 250 if (CHECK(CHK_E_SUBSTRATE) && (n[N_SUBSTRATE] == 0 || (n[N_SUBSTRATE] & 0xF8))) return MJB_BAD_ENUM; 251 if (CHECK(CHK_E_DEPN) && depn > MAXDEP) return MJB_BAD_ENUM; 252 if (CHECK(CHK_E_OPN) && opn > MAXOPS) return MJB_BAD_ENUM; 253 if (CHECK(CHK_E_RTYPE) && (n[N_RTYPE] < 1 || n[N_RTYPE] > 7)) return MJB_BAD_ENUM; 254 if (CHECK(CHK_E_RANK) && rank > 4) return MJB_BAD_ENUM; 255 if (CHECK(CHK_E_PURE) && n[N_PURE] > 1) return MJB_BAD_ENUM; 256 if (CHECK(CHK_E_OPKIND)) 257 for (s = 0; s < olim; s++) { 258 uint8_t ok = n[N_OPERANDS + OPLEN * s]; 259 if (ok < 1 || ok > 5) return MJB_BAD_ENUM; 260 } 261 262 if (CHECK(CHK_TENANT_MATCH) && memcmp(n + N_TENANT, h + H_TENANT, 32) != 0) return MJB_CROSS_TENANT; 263 if (CHECK(CHK_CAPABILITY) && all_zero(n + N_CAP, 32)) return MJB_NO_CAPABILITY; 264 if (CHECK(CHK_EFFECT_CEIL) && n[N_EFFECT] > h[H_CEILING]) return MJB_UNAUTHORIZED_EFFECT; 265 266 if (CHECK(CHK_VERIFIER_ZERO) && vk == 0) return MJB_NO_VERIFIER; 267 if (CHECK(CHK_VERIFIER_REF) && vk != 1 && vz) return MJB_NO_VERIFIER; 268 if (CHECK(CHK_RECOMPUTE_REF) && vk == 1 && !vz) return MJB_NO_VERIFIER; 269 if (CHECK(CHK_RECOMPUTE_PURE) && vk == 1 && !pure) return MJB_NO_VERIFIER; 270 271 if (CHECK(CHK_IMPURE_NEURAL) && pure && k == 2) return MJB_IMPURE; 272 if (CHECK(CHK_IMPURE_EFFECT) && pure && n[N_EFFECT] == 2) return MJB_IMPURE; 273 274 if (k == 1) { 275 if (CHECK(CHK_COMPUTE_TOKENS) && (prof != 0 || tin || tout || rin || rout || !mz)) return MJB_TOKEN_PROFILE; 276 } else { 277 if (CHECK(CHK_NEURAL_TOKENS) && (prof == 0 || tin == 0 || tout == 0 || tin > cap_in[pidx] || tout > cap_out[pidx] || mz)) 278 return MJB_TOKEN_PROFILE; 279 } 280 if (CHECK(CHK_EXPANSION) && (rin > tin || rout > tout)) return MJB_NEEDS_EXPANSION; 281 282 if (CHECK(CHK_RETRY_NEVER) && n[N_RETRY] == 1 && n[N_ATTEMPTS] > 1) return MJB_BAD_RETRY; 283 if (CHECK(CHK_RETRY_EFFECT) && n[N_EFFECT] == 2 && n[N_RETRY] == 2) return MJB_BAD_RETRY; 284 285 if (CHECK(CHK_ZERO_CAP) && (g32(n + N_TIME) == 0 || g64(n + N_FUEL) == 0 || g64(n + N_MEMORY) == 0 || g64(n + N_WORK) == 0)) 286 return MJB_ZERO_CAP; 287 if (CHECK(CHK_NODE_EXPIRY) && (exp == 0 || exp > g64(h + H_EXPIRES))) return MJB_BAD_EXPIRY; 288 if (CHECK(CHK_OPERAND_MISSING)) 289 for (s = 0; s < olim; s++) { 290 const uint8_t *o = n + N_OPERANDS + OPLEN * s; 291 if (g32(o + 4) == 0 || all_zero(o + 8, 32)) return MJB_MISSING_OPERAND; 292 } 293 return MJB_OK; 294 } 295 296 /* Exact sum: *over is set when the total does not fit in 64 bits. */ 297 static uint64_t add_exact(uint64_t a, uint64_t b, int *over) { 298 if (a > UINT64_MAX - b) *over = 1; 299 return a + b; 300 } 301 302 /* Kahn's algorithm, lowest index first. Returns the number of nodes emitted. */ 303 static unsigned kahn(const uint8_t *b, unsigned count, unsigned *order) { 304 unsigned indeg[MAXN], emitted[MAXN] = { 0 }, done = 0, i, s; 305 for (i = 0; i < count; i++) { 306 unsigned d = b[HDR + NODE * i + N_DEPS_N]; 307 indeg[i] = d < MAXDEP ? d : MAXDEP; 308 } 309 while (done < count) { 310 unsigned next = count; 311 for (i = 0; i < count; i++) if (!emitted[i] && indeg[i] == 0) { next = i; break; } 312 if (next == count) break; 313 emitted[next] = 1; 314 order[done++] = next; 315 for (i = 0; i < count; i++) { 316 const uint8_t *n = b + HDR + NODE * i; 317 unsigned d = n[N_DEPS_N] < MAXDEP ? n[N_DEPS_N] : MAXDEP; 318 for (s = 0; s < d; s++) if (g16(n + N_DEPS + DEPLEN * s) == next) indeg[i]--; 319 } 320 } 321 return done; 322 } 323 324 static int mjb_check(const uint8_t *b, size_t len, unsigned *order, unsigned *count_out) { 325 unsigned count, i, j, s; 326 int rc, over = 0; 327 uint64_t t = 0, fu = 0, wk = 0, ti = 0, to = 0; 328 *count_out = 0; 329 if (CHECK(CHK_MIN_LEN) && len < HDR) return MJB_BAD_LENGTH; 330 if (CHECK(CHK_MAGIC) && memcmp(b, "MJB1", 4) != 0) return MJB_BAD_MAGIC; 331 if (CHECK(CHK_DOWNGRADE) && b[H_VERSION] == 0) return MJB_DOWNGRADE; 332 if (CHECK(CHK_VERSION) && b[H_VERSION] > 1) return MJB_BAD_VERSION; 333 count = g16(b + H_COUNT); 334 if (CHECK(CHK_COUNT) && (count == 0 || count > MAXN)) return MJB_BAD_COUNT; 335 if (CHECK(CHK_EXACT_LEN) && len != HDR + (size_t)count * NODE) return MJB_BAD_LENGTH; 336 if (count > MAXN) return MJB_BAD_COUNT; /* array bound for mutants, not a rule of its own */ 337 if (CHECK(CHK_RES0) && !all_zero(b + H_RES0, 4)) return MJB_RESERVED_NONZERO; 338 if (CHECK(CHK_CEILING) && b[H_CEILING] > 2) return MJB_BAD_ENUM; 339 if (CHECK(CHK_OBJECTIVE) && all_zero(b + H_OBJECTIVE, 32)) return MJB_NO_OBJECTIVE; 340 if (CHECK(CHK_TENANT) && all_zero(b + H_TENANT, 32)) return MJB_NO_TENANT; 341 if (CHECK(CHK_SUBJECT) && all_zero(b + H_SUBJECT, 32)) return MJB_NO_SUBJECT; 342 if (CHECK(CHK_APPROVAL) && all_zero(b + H_APPROVAL, 32)) return MJB_NO_APPROVAL; 343 if (CHECK(CHK_INCARNATION) && g32(b + H_INCARNATION) == 0) return MJB_NO_OBJECTIVE; 344 /* A header expires_at of 0 needs no rule of its own: every node then fails 345 * rule 22 (node expiry above the header), with the same code. */ 346 347 for (i = 0; i < count; i++) 348 if ((rc = check_node(b + HDR + NODE * i, b)) != MJB_OK) return rc; 349 350 if (CHECK(CHK_DUP)) 351 for (i = 0; i < count; i++) 352 for (j = i + 1; j < count; j++) { 353 const uint8_t *x = b + HDR + NODE * i, *y = b + HDR + NODE * j; 354 if (!memcmp(x + N_JOB, y + N_JOB, 16) || !memcmp(x + N_IDEM, y + N_IDEM, 16)) return MJB_DUPLICATE_JOB; 355 } 356 if (CHECK(CHK_DEP_RANGE)) 357 for (i = 0; i < count; i++) { 358 const uint8_t *n = b + HDR + NODE * i; 359 unsigned d = n[N_DEPS_N] < MAXDEP ? n[N_DEPS_N] : MAXDEP; 360 for (s = 0; s < d; s++) { 361 unsigned idx = g16(n + N_DEPS + DEPLEN * s); 362 if (idx == i || idx >= count) return MJB_MISSING_DEP; 363 } 364 } 365 if (CHECK(CHK_STALE)) 366 for (i = 0; i < count; i++) { 367 const uint8_t *n = b + HDR + NODE * i; 368 unsigned d = n[N_DEPS_N] < MAXDEP ? n[N_DEPS_N] : MAXDEP; 369 for (s = 0; s < d; s++) { 370 unsigned idx = g16(n + N_DEPS + DEPLEN * s); 371 if (idx < count && g32(n + N_DEPS + DEPLEN * s + 2) != g32(b + HDR + NODE * idx + N_GEN)) 372 return MJB_STALE_GENERATION; 373 } 374 } 375 if (CHECK(CHK_CYCLE) && kahn(b, count, order) != count) return MJB_CYCLE; 376 if (CHECK(CHK_BUDGET)) { 377 for (i = 0; i < count; i++) { 378 const uint8_t *n = b + HDR + NODE * i; 379 t = add_exact(t, g32(n + N_TIME), &over); 380 fu = add_exact(fu, g64(n + N_FUEL), &over); 381 wk = add_exact(wk, g64(n + N_WORK), &over); 382 ti += g16(n + N_IN); 383 to += g16(n + N_OUT); 384 if (g64(n + N_MEMORY) > g64(b + H_B_MEMORY)) over = 1; 385 } 386 if (over || t > g64(b + H_B_TIME) || fu > g64(b + H_B_FUEL) || wk > g64(b + H_B_WORK) 387 || ti > g32(b + H_B_IN) || to > g32(b + H_B_OUT)) 388 return MJB_OVER_BUDGET; 389 } 390 kahn(b, count, order); 391 *count_out = count; 392 return MJB_OK; 393 } 394 395 static void hex(char *out, const uint8_t *p, size_t n) { 396 static const char H[] = "`0123456789abcdef`"; 397 for (size_t i = 0; i < n; i++) { out[2*i] = H[p[i] >> 4]; out[2*i+1] = H[p[i] & 15]; } 398 out[2*n] = 0; 399 } 400 401 static void ok_line(const uint8_t *b, size_t len, const char *order, char *line, size_t cap) { 402 char obj[65], ten[65], dig[65]; 403 uint8_t d[32]; 404 sha_once(b, len, d); 405 hex(obj, b + H_OBJECTIVE, 32); 406 hex(ten, b + H_TENANT, 32); 407 hex(dig, d, 32); 408 snprintf(line, cap, "ok nodes=%u incarnation=%lu ceiling=%u objective=%s tenant=%s digest=%s order=%s", 409 (unsigned)g16(b + H_COUNT), (unsigned long)g32(b + H_INCARNATION), (unsigned)b[H_CEILING], obj, ten, dig, order); 410 } 411 412 static void order_text(const unsigned *order, unsigned count, char *out, size_t cap) { 413 size_t at = 0; 414 out[0] = 0; 415 for (unsigned i = 0; i < count && at + 8 < cap; i++) 416 at += (size_t)snprintf(out + at, cap - at, i ? ",%u" : "%u", order[i]); 417 } 418 419 static void verdict_line(const uint8_t *b, size_t len, char *line, size_t cap) { 420 unsigned order[MAXN], count; 421 char ord[512]; 422 int code = mjb_check(b, len, order, &count); 423 if (code != MJB_OK) { snprintf(line, cap, "refuse %s", code_names[code]); return; } 424 order_text(order, count, ord, sizeof ord); 425 ok_line(b, len, ord, line, cap); 426 } 427 428 /* ---- file I/O (outside the checker) ---------------------------------------- */ 429 #define FILE_CAP 65536u 430 static uint8_t file_buf[FILE_CAP]; 431 432 /* Reads the whole file into file_buf (zeroed first). Returns 0, or -1 on I/O 433 * error or a file larger than FILE_CAP (no vector is that large). */ 434 static int slurp(const char *path, size_t *n) { 435 FILE *f = fopen(path, "rb"); 436 size_t k; 437 memset(file_buf, 0, sizeof file_buf); 438 if (!f) return -1; 439 k = fread(file_buf, 1, FILE_CAP, f); 440 if (k == FILE_CAP && fgetc(f) != EOF) { fclose(f); return -1; } 441 fclose(f); 442 *n = k; 443 return 0; 444 } 445 446 static void rstrip(char *s) { size_t n = strlen(s); while (n && (s[n-1] == '\n' || s[n-1] == '\r')) s[--n] = 0; } 447 static int plain_name(const char *s) { return s[0] && s[0] != '.' && !strchr(s, '/'); } 448 449 static FILE *create_file(const char *path, const char *mode) { 450 int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); 451 FILE *f; 452 if (fd < 0) return NULL; 453 if (!(f = fdopen(fd, mode))) close(fd); 454 return f; 455 } 456 457 /* The corpus walk opens only names that readdir reports in DIR; expected.txt 458 * names are lookup keys, never paths. An expected name with no file in DIR is 459 * a failure ("missing"), as is a duplicate expected name. */ 460 #define CORPUS_CAP 512u 461 struct expect { char name[256]; char want[1024]; int seen; }; 462 static struct expect expects[CORPUS_CAP]; 463 464 static int corpus(const char *dir) { 465 char path[4096], line[1024], got[1024]; 466 unsigned pass = 0, fail = 0, count = 0, i; 467 size_t n; 468 FILE *f; 469 DIR *d; 470 struct dirent *e; 471 snprintf(path, sizeof path, "%s/expected.txt", dir); 472 if (!(f = fopen(path, "r"))) { fprintf(stderr, "cannot read %s\n", path); return 2; } 473 while (fgets(line, sizeof line, f)) { 474 char *sp; 475 rstrip(line); 476 if (!line[0] || line[0] == '#') continue; 477 if (!(sp = strchr(line, ' '))) { fprintf(stderr, "bad line: %s\n", line); fail++; continue; } 478 *sp = 0; 479 if (!plain_name(line) || strlen(line) >= sizeof expects[0].name) { fprintf(stderr, "not a plain file name: %s\n", line); fail++; continue; } 480 for (i = 0; i < count && strcmp(expects[i].name, line) != 0; i++) ; 481 if (i < count) { fprintf(stderr, "duplicate name: %s\n", line); fail++; continue; } 482 if (count == CORPUS_CAP) { fprintf(stderr, "too many lines: %s\n", line); fail++; continue; } 483 snprintf(expects[count].name, sizeof expects[count].name, "%s", line); 484 snprintf(expects[count].want, sizeof expects[count].want, "%s", sp + 1); 485 expects[count].seen = 0; 486 count++; 487 } 488 fclose(f); 489 if (!(d = opendir(dir))) { fprintf(stderr, "cannot read %s\n", dir); return 2; } 490 while ((e = readdir(d))) { 491 for (i = 0; i < count && strcmp(expects[i].name, e->d_name) != 0; i++) ; 492 if (i == count) continue; 493 expects[i].seen = 1; 494 snprintf(path, sizeof path, "%s/%s", dir, e->d_name); 495 if (slurp(path, &n)) { fprintf(stderr, "cannot read %s\n", path); fail++; continue; } 496 verdict_line(file_buf, n, got, sizeof got); 497 if (strcmp(got, expects[i].want) != 0) { fail++; printf("MISMATCH %s\n expected: %s\n got: %s\n", e->d_name, expects[i].want, got); } 498 else pass++; 499 } 500 closedir(d); 501 for (i = 0; i < count; i++) 502 if (!expects[i].seen) { fprintf(stderr, "missing: %s\n", expects[i].name); fail++; } 503 printf("MJB1_CONFORMANCE impl=c pass=%u fail=%u\n", pass, fail); 504 return fail || !pass ? 1 : 0; 505 } 506 507 /* ---- vector generator -------------------------------------------------------- */ 508 /* Every refused vector starts from one well-formed document and changes one 509 * thing. The expected line of an accepted vector takes its order from the 510 * generator's intent, not from mjb_check, so the checker is tested against it. */ 511 static uint8_t doc[MAXDOC + 64]; 512 static unsigned doc_n; 513 static FILE *gen_expected; 514 static int gen_fail; 515 static const char *gen_dir; 516 517 static void fill(uint8_t *p, size_t n, uint8_t seed) { for (size_t i = 0; i < n; i++) p[i] = (uint8_t)(seed + 7 * i); } 518 static uint8_t *nd(unsigned i) { return doc + HDR + NODE * i; } 519 static size_t doc_len(void) { return HDR + (size_t)doc_n * NODE; } 520 521 static void header(unsigned count, uint8_t ceiling) { 522 memset(doc, 0, sizeof doc); 523 doc_n = count; 524 memcpy(doc, "MJB1", 4); 525 doc[H_VERSION] = 1; 526 doc[H_CEILING] = ceiling; 527 p16(doc + H_COUNT, (uint16_t)count); 528 fill(doc + H_OBJECTIVE, 32, 0x0B); 529 fill(doc + H_TENANT, 32, 0x7E); 530 fill(doc + H_SUBJECT, 32, 0x5B); 531 fill(doc + H_APPROVAL, 32, 0xA9); 532 p32(doc + H_INCARNATION, 1); 533 p64(doc + H_EXPIRES, 2000000000u); 534 p64(doc + H_B_TIME, 10000); 535 p64(doc + H_B_FUEL, `1000000`); 536 p64(doc + H_B_MEMORY, (uint64_t)1 << 30); 537 p64(doc + H_B_WORK, `1000000`); 538 p32(doc + H_B_IN, 4096); 539 p32(doc + H_B_OUT, 1024); 540 } 541 542 static void common(unsigned i) { 543 uint8_t *n = nd(i); 544 fill(n + N_JOB, 14, 0x4A); p16(n + N_JOB + 14, (uint16_t)(i + 1)); 545 fill(n + N_IDEM, 14, 0x1D); p16(n + N_IDEM + 14, (uint16_t)(i + 1)); 546 p32(n + N_GEN, 1); 547 n[N_CANCEL] = 1; 548 p32(n + N_TIME, 100); 549 p64(n + N_FUEL, 1000); 550 p64(n + N_MEMORY, (uint64_t)1 << 20); 551 p64(n + N_WORK, 1000); 552 p64(n + N_EXPIRES, 1999999999u); 553 fill(n + N_CAP, 32, 0xCA); 554 memcpy(n + N_TENANT, doc + H_TENANT, 32); 555 } 556 557 static void compute(unsigned i, uint8_t op) { 558 uint8_t *n = nd(i); 559 common(i); 560 n[N_KIND] = 1; n[N_OP] = op; n[N_PROFILE] = 0; n[N_EFFECT] = 1; 561 n[N_VERIFIER] = 1; n[N_RETRY] = 2; n[N_ATTEMPTS] = 3; 562 n[N_PRECISION] = 0; n[N_SUBSTRATE] = 1; n[N_RTYPE] = 2; n[N_PURE] = 1; 563 } 564 565 static void neural(unsigned i, uint8_t op, uint8_t prof, uint16_t tin, uint16_t tout, uint16_t rin, uint16_t rout) { 566 uint8_t *n = nd(i); 567 common(i); 568 n[N_KIND] = 2; n[N_OP] = op; n[N_PROFILE] = prof; n[N_EFFECT] = 0; 569 p16(n + N_IN, tin); p16(n + N_OUT, tout); p16(n + N_REQ_IN, rin); p16(n + N_REQ_OUT, rout); 570 n[N_VERIFIER] = 2; fill(n + N_VREF, 32, 0x5E); 571 n[N_RETRY] = 2; n[N_ATTEMPTS] = 2; 572 n[N_PRECISION] = 2; n[N_SUBSTRATE] = 3; n[N_RTYPE] = 6; n[N_PURE] = 0; 573 fill(n + N_MODEL, 32, 0x3D); 574 } 575 576 static void dep(unsigned i, unsigned on) { 577 uint8_t *n = nd(i); 578 unsigned s = n[N_DEPS_N]++; 579 p16(n + N_DEPS + DEPLEN * s, (uint16_t)on); 580 p32(n + N_DEPS + DEPLEN * s + 2, g32(nd(on) + N_GEN)); 581 } 582 583 static void operand(unsigned i, uint8_t kind, uint8_t seed) { 584 uint8_t *n = nd(i); 585 unsigned s = n[N_OPS_N]++; 586 uint8_t *o = n + N_OPERANDS + OPLEN * s; 587 o[0] = kind; 588 p32(o + 4, 1); 589 fill(o + 8, 32, seed); 590 } 591 592 /* compute(ARITH, 1 CONTENT operand) -> compute(REDUCE) -> neural ROUTE 64/16 */ 593 static void base3(void) { 594 header(3, 1); 595 compute(0, 1); operand(0, 3, 0x21); 596 compute(1, 3); dep(1, 0); 597 neural(2, 1, 1, 64, 16, 40, 8); dep(2, 1); operand(2, 1, 0x31); 598 } 599 600 static void emit(const char *name, size_t n, const char *expect) { 601 char path[4096]; 602 FILE *f; 603 snprintf(path, sizeof path, "%s/%s", gen_dir, name); 604 if (!(f = create_file(path, "wb")) || fwrite(doc, 1, n, f) != n || fclose(f) != 0) { 605 fprintf(stderr, "cannot write %s\n", path); 606 gen_fail = 1; 607 return; 608 } 609 fprintf(gen_expected, "%s %s\n", name, expect); 610 } 611 612 static void ok(const char *name, const char *order) { 613 char line[1024]; 614 ok_line(doc, doc_len(), order, line, sizeof line); 615 emit(name, doc_len(), line); 616 } 617 618 static void bad_len(const char *name, size_t n, const char *code) { 619 char line[128]; 620 snprintf(line, sizeof line, "refuse %s", code); 621 emit(name, n, line); 622 } 623 static void bad(const char *name, const char *code) { bad_len(name, doc_len(), code); } 624 625 static int make_vectors(const char *dir) { 626 char path[4096]; 627 unsigned i; 628 gen_dir = dir; 629 snprintf(path, sizeof path, "%s/expected.txt", dir); 630 if (!(gen_expected = create_file(path, "w"))) { fprintf(stderr, "cannot write %s\n", path); return 2; } 631 fprintf(gen_expected, 632 "# MJB1 document vectors. Spec: ../SPEC.md (Draft v0.1, wire version 1).\n" 633 "# Line: , verdict line as printed by tools/mjb_verify.c:\n" 634 "# ok nodes=N incarnation=I ceiling=C objective=H64 tenant=H64 digest=H64 order=i,j,...\n" 635 "# refuse \n" 636 "# Generated by tools/mjb_verify.c --make-vectors; do not edit by hand.\n"); 637 638 /* ---- accepted ---- */ 639 base3(); 640 ok("ok-base3.mjb", "0,1,2"); 641 642 /* 11 nodes: two independent sums, reduce, mean, compare, two neural 643 * routes, an independent numeric check, a summary, a compile step and 644 * one EFFECT node under an EFFECT ceiling. */ 645 header(11, 2); 646 compute(0, 1); operand(0, 3, 0x41); 647 compute(1, 1); operand(1, 3, 0x42); 648 compute(2, 3); dep(2, 0); dep(2, 1); 649 compute(3, 1); dep(3, 2); 650 compute(4, 4); dep(4, 3); operand(4, 3, 0x43); 651 neural(5, 1, 1, 64, 16, 48, 4); operand(5, 3, 0x44); 652 neural(6, 1, 2, 128, 32, 100, 4); operand(6, 1, 0x45); 653 compute(7, 6); dep(7, 3); nd(7)[N_VERIFIER] = 2; fill(nd(7) + N_VREF, 32, 0x77); 654 neural(8, 4, 3, 256, 64, 200, 60); dep(8, 4); dep(8, 5); dep(8, 6); operand(8, 5, 0x46); 655 compute(9, 5); dep(9, 8); operand(9, 4, 0x47); 656 compute(10, 1); dep(10, 4); dep(10, 7); operand(10, 2, 0x48); 657 nd(10)[N_EFFECT] = 2; nd(10)[N_PURE] = 0; nd(10)[N_VERIFIER] = 3; fill(nd(10) + N_VREF, 32, 0x99); 658 nd(10)[N_RETRY] = 3; nd(10)[N_ATTEMPTS] = 2; nd(10)[N_CANCEL] = 2; 659 ok("ok-sample-11.mjb", "0,1,2,3,4,5,6,7,8,9,10"); 660 661 header(64, 1); 662 for (i = 0; i < 64; i++) { compute(i, 1); if (i) dep(i, i - 1); else operand(0, 3, 0x51); } 663 { 664 char ord[512]; 665 unsigned o[64]; 666 for (i = 0; i < 64; i++) o[i] = i; 667 order_text(o, 64, ord, sizeof ord); 668 ok("ok-chain-64.mjb", ord); 669 } 670 671 header(4, 1); 672 compute(2, 1); operand(2, 3, 0x61); 673 compute(3, 1); dep(3, 2); 674 compute(0, 1); dep(0, 3); 675 compute(1, 1); dep(1, 3); 676 ok("ok-reverse-order.mjb", "2,3,0,1"); 677 678 header(9, 1); 679 for (i = 0; i < 8; i++) { compute(i, 1); operand(i, 3, (uint8_t)(0x70 + i)); } 680 compute(8, 2); 681 for (i = 0; i < 8; i++) dep(8, i); 682 operand(8, 1, 0x81); operand(8, 2, 0x82); operand(8, 4, 0x83); operand(8, 5, 0x84); 683 nd(8)[N_RTYPE] = 4; nd(8)[N_RANK] = 4; nd(8)[N_PRECISION] = 1; 684 p32(nd(8) + N_DIMS, 2); p32(nd(8) + N_DIMS + 4, 3); p32(nd(8) + N_DIMS + 8, 4); p32(nd(8) + N_DIMS + 12, 5); 685 ok("ok-fanin-8-tensor.mjb", "0,1,2,3,4,5,6,7,8"); 686 687 header(1, 0); 688 neural(0, 2, 1, 64, 16, 64, 16); operand(0, 3, 0x91); 689 ok("ok-minimal-exact-64-16.mjb", "0"); 690 691 header(1, 0); 692 neural(0, 3, 4, 512, 128, 300, 100); operand(0, 3, 0x92); 693 ok("ok-specialist-512-128.mjb", "0"); 694 695 header(1, 0); 696 neural(0, 1, 2, 40, 10, 40, 10); operand(0, 3, 0x93); 697 ok("ok-optimizer-smaller-caps.mjb", "0"); 698 699 /* ---- refused: header, in check order ---- */ 700 base3(); bad_len("bad-length-short.mjb", 100, "MJB_BAD_LENGTH"); 701 base3(); bad_len("bad-length-empty.mjb", 0, "MJB_BAD_LENGTH"); 702 base3(); doc[3] = '2'; bad("bad-magic.mjb", "MJB_BAD_MAGIC"); 703 base3(); doc[H_VERSION] = 0; bad("bad-version-downgrade.mjb", "MJB_DOWNGRADE"); 704 base3(); doc[H_VERSION] = 2; bad("bad-version-unknown.mjb", "MJB_BAD_VERSION"); 705 base3(); p16(doc + H_COUNT, 0); bad_len("bad-count-zero.mjb", HDR, "MJB_BAD_COUNT"); 706 base3(); p16(doc + H_COUNT, 65); bad_len("bad-count-65.mjb", HDR, "MJB_BAD_COUNT"); 707 base3(); bad_len("bad-length-mismatch.mjb", doc_len() - 1, "MJB_BAD_LENGTH"); 708 base3(); bad_len("bad-length-trailing.mjb", doc_len() + 1, "MJB_BAD_LENGTH"); 709 base3(); doc[H_RES0 + 1] = 1; bad("bad-reserved0.mjb", "MJB_RESERVED_NONZERO"); 710 base3(); doc[H_CEILING] = 3; bad("bad-ceiling-enum.mjb", "MJB_BAD_ENUM"); 711 base3(); memset(doc + H_OBJECTIVE, 0, 32); bad("bad-no-objective.mjb", "MJB_NO_OBJECTIVE"); 712 base3(); memset(doc + H_TENANT, 0, 32); bad("bad-no-tenant.mjb", "MJB_NO_TENANT"); 713 base3(); memset(doc + H_SUBJECT, 0, 32); bad("bad-no-subject.mjb", "MJB_NO_SUBJECT"); 714 base3(); memset(doc + H_APPROVAL, 0, 32); bad("bad-no-approval.mjb", "MJB_NO_APPROVAL"); 715 base3(); p32(doc + H_INCARNATION, 0); bad("bad-no-incarnation.mjb", "MJB_NO_OBJECTIVE"); 716 base3(); p64(doc + H_EXPIRES, 0); bad("bad-header-expiry-zero.mjb", "MJB_BAD_EXPIRY"); 717 718 /* ---- refused: one node rule each ---- */ 719 base3(); memset(nd(1) + N_JOB, 0, 16); bad("bad-no-job-id.mjb", "MJB_NO_JOB"); 720 base3(); memset(nd(1) + N_IDEM, 0, 16); bad("bad-no-idempotence-key.mjb", "MJB_NO_JOB"); 721 base3(); p32(nd(0) + N_GEN, 0); bad("bad-no-generation.mjb", "MJB_NO_JOB"); 722 base3(); nd(1)[N_RES1] = 1; bad("bad-reserved1.mjb", "MJB_RESERVED_NONZERO"); 723 base3(); nd(0)[N_DEPS + 1] = 1; bad("bad-reserved-unused-dep.mjb", "MJB_RESERVED_NONZERO"); 724 base3(); nd(1)[N_DEPS + DEPLEN + 5] = 1; bad("bad-reserved-unused-dep-generation.mjb", "MJB_RESERVED_NONZERO"); 725 base3(); nd(1)[N_OPERANDS + 20] = 1; bad("bad-reserved-unused-operand.mjb", "MJB_RESERVED_NONZERO"); 726 base3(); nd(0)[N_OPERANDS + 2] = 1; bad("bad-reserved-operand-pad.mjb", "MJB_RESERVED_NONZERO"); 727 base3(); p32(nd(0) + N_DIMS, 1); bad("bad-reserved-dims.mjb", "MJB_RESERVED_NONZERO"); 728 base3(); nd(0)[N_KIND] = 3; bad("bad-enum-kind.mjb", "MJB_BAD_ENUM"); 729 base3(); nd(0)[N_OP] = 7; bad("bad-enum-op-compute.mjb", "MJB_BAD_ENUM"); 730 base3(); nd(0)[N_OP] = 0; bad("bad-enum-op-zero.mjb", "MJB_BAD_ENUM"); 731 base3(); nd(2)[N_OP] = 5; bad("bad-enum-op-neural.mjb", "MJB_BAD_ENUM"); 732 base3(); nd(2)[N_PROFILE] = 5; bad("bad-enum-profile.mjb", "MJB_BAD_ENUM"); 733 base3(); nd(0)[N_EFFECT] = 3; bad("bad-enum-effect.mjb", "MJB_BAD_ENUM"); 734 base3(); nd(0)[N_VERIFIER] = 4; bad("bad-enum-verifier.mjb", "MJB_BAD_ENUM"); 735 base3(); nd(0)[N_RETRY] = 0; bad("bad-enum-retry-zero.mjb", "MJB_BAD_ENUM"); 736 base3(); nd(0)[N_RETRY] = 4; bad("bad-enum-retry-four.mjb", "MJB_BAD_ENUM"); 737 base3(); nd(0)[N_ATTEMPTS] = 0; bad("bad-enum-attempts-zero.mjb", "MJB_BAD_ENUM"); 738 base3(); nd(0)[N_ATTEMPTS] = 9; bad("bad-enum-attempts-nine.mjb", "MJB_BAD_ENUM"); 739 base3(); nd(0)[N_CANCEL] = 0; bad("bad-enum-cancel-zero.mjb", "MJB_BAD_ENUM"); 740 base3(); nd(0)[N_CANCEL] = 3; bad("bad-enum-cancel-three.mjb", "MJB_BAD_ENUM"); 741 base3(); nd(0)[N_PRECISION] = 7; bad("bad-enum-precision.mjb", "MJB_BAD_ENUM"); 742 base3(); nd(0)[N_SUBSTRATE] = 0; bad("bad-enum-substrate-zero.mjb", "MJB_BAD_ENUM"); 743 base3(); nd(0)[N_SUBSTRATE] = 0x09; bad("bad-enum-substrate-high.mjb", "MJB_BAD_ENUM"); 744 base3(); nd(0)[N_DEPS_N] = 9; bad("bad-enum-dep-count.mjb", "MJB_BAD_ENUM"); 745 base3(); operand(0, 3, 0x22); operand(0, 3, 0x23); operand(0, 3, 0x24); nd(0)[N_OPS_N] = 5; 746 bad("bad-enum-operand-count.mjb", "MJB_BAD_ENUM"); 747 base3(); nd(0)[N_RTYPE] = 0; bad("bad-enum-result-type-zero.mjb", "MJB_BAD_ENUM"); 748 base3(); nd(0)[N_RTYPE] = 8; bad("bad-enum-result-type-eight.mjb", "MJB_BAD_ENUM"); 749 base3(); nd(0)[N_RANK] = 5; bad("bad-enum-rank.mjb", "MJB_BAD_ENUM"); 750 base3(); nd(0)[N_PURE] = 2; bad("bad-enum-pure.mjb", "MJB_BAD_ENUM"); 751 base3(); nd(0)[N_OPERANDS] = 6; bad("bad-enum-operand-kind-six.mjb", "MJB_BAD_ENUM"); 752 base3(); nd(0)[N_OPERANDS] = 0; bad("bad-enum-operand-kind-zero.mjb", "MJB_BAD_ENUM"); 753 base3(); nd(1)[N_TENANT + 31] ^= 1; bad("bad-cross-tenant.mjb", "MJB_CROSS_TENANT"); 754 base3(); memset(nd(1) + N_CAP, 0, 32); bad("bad-no-capability.mjb", "MJB_NO_CAPABILITY"); 755 base3(); nd(0)[N_EFFECT] = 2; bad("bad-unauthorized-effect.mjb", "MJB_UNAUTHORIZED_EFFECT"); 756 base3(); doc[H_CEILING] = 0; bad("bad-unauthorized-compute-under-none.mjb", "MJB_UNAUTHORIZED_EFFECT"); 757 base3(); nd(2)[N_VERIFIER] = 0; bad("bad-no-verifier-kind-zero.mjb", "MJB_NO_VERIFIER"); 758 base3(); nd(0)[N_VERIFIER] = 0; bad("bad-no-verifier-kind-zero-no-ref.mjb", "MJB_NO_VERIFIER"); 759 base3(); memset(nd(2) + N_VREF, 0, 32); bad("bad-no-verifier-checker-no-ref.mjb", "MJB_NO_VERIFIER"); 760 base3(); fill(nd(0) + N_VREF, 32, 0x55); bad("bad-no-verifier-recompute-with-ref.mjb", "MJB_NO_VERIFIER"); 761 base3(); nd(0)[N_PURE] = 0; bad("bad-no-verifier-recompute-impure.mjb", "MJB_NO_VERIFIER"); 762 base3(); nd(2)[N_PURE] = 1; bad("bad-impure-neural.mjb", "MJB_IMPURE"); 763 base3(); doc[H_CEILING] = 2; nd(0)[N_EFFECT] = 2; bad("bad-impure-effect.mjb", "MJB_IMPURE"); 764 base3(); p16(nd(0) + N_IN, 10); bad("bad-token-compute-with-tokens.mjb", "MJB_TOKEN_PROFILE"); 765 base3(); nd(0)[N_PROFILE] = 1; bad("bad-token-compute-with-profile.mjb", "MJB_TOKEN_PROFILE"); 766 base3(); fill(nd(0) + N_MODEL, 32, 0x3D); bad("bad-token-compute-with-model.mjb", "MJB_TOKEN_PROFILE"); 767 base3(); p16(nd(0) + N_REQ_IN, 5); bad("bad-token-compute-required.mjb", "MJB_TOKEN_PROFILE"); 768 base3(); nd(2)[N_PROFILE] = 0; bad("bad-token-neural-zero-profile.mjb", "MJB_TOKEN_PROFILE"); 769 base3(); p16(nd(2) + N_IN, 65); bad("bad-token-neural-over-profile-input.mjb", "MJB_TOKEN_PROFILE"); 770 base3(); p16(nd(2) + N_OUT, 17); bad("bad-token-neural-over-profile-output.mjb", "MJB_TOKEN_PROFILE"); 771 base3(); p16(nd(2) + N_OUT, 0); p16(nd(2) + N_REQ_OUT, 0); bad("bad-token-neural-zero-cap.mjb", "MJB_TOKEN_PROFILE"); 772 base3(); memset(nd(2) + N_MODEL, 0, 32); bad("bad-token-neural-no-model.mjb", "MJB_TOKEN_PROFILE"); 773 base3(); p16(nd(2) + N_REQ_IN, 90); bad("bad-needs-expansion-64-16-input.mjb", "MJB_NEEDS_EXPANSION"); 774 base3(); p16(nd(2) + N_REQ_OUT, 20); bad("bad-needs-expansion-64-16-output.mjb", "MJB_NEEDS_EXPANSION"); 775 base3(); nd(0)[N_RETRY] = 1; nd(0)[N_ATTEMPTS] = 2; bad("bad-retry-never-multi.mjb", "MJB_BAD_RETRY"); 776 base3(); doc[H_CEILING] = 2; nd(2)[N_EFFECT] = 2; bad("bad-retry-effect-idempotent.mjb", "MJB_BAD_RETRY"); 777 base3(); p32(nd(1) + N_TIME, 0); bad("bad-zero-cap-time.mjb", "MJB_ZERO_CAP"); 778 base3(); p64(nd(1) + N_FUEL, 0); bad("bad-zero-cap-fuel.mjb", "MJB_ZERO_CAP"); 779 base3(); p64(nd(1) + N_MEMORY, 0); bad("bad-zero-cap-memory.mjb", "MJB_ZERO_CAP"); 780 base3(); p64(nd(1) + N_WORK, 0); bad("bad-zero-cap-work.mjb", "MJB_ZERO_CAP"); 781 base3(); p64(nd(1) + N_EXPIRES, 0); bad("bad-node-expiry-zero.mjb", "MJB_BAD_EXPIRY"); 782 base3(); p64(nd(1) + N_EXPIRES, 2000000001u); bad("bad-node-expiry-after-header.mjb", "MJB_BAD_EXPIRY"); 783 base3(); memset(nd(0) + N_OPERANDS + 8, 0, 32); bad("bad-missing-operand-digest.mjb", "MJB_MISSING_OPERAND"); 784 base3(); p32(nd(0) + N_OPERANDS + 4, 0); bad("bad-missing-operand-generation.mjb", "MJB_MISSING_OPERAND"); 785 786 /* ---- refused: whole graph ---- */ 787 base3(); memcpy(nd(2) + N_JOB, nd(1) + N_JOB, 16); bad("bad-duplicate-job.mjb", "MJB_DUPLICATE_JOB"); 788 base3(); memcpy(nd(2) + N_IDEM, nd(1) + N_IDEM, 16); bad("bad-duplicate-idempotence-key.mjb", "MJB_DUPLICATE_JOB"); 789 base3(); p16(nd(1) + N_DEPS, 1); bad("bad-missing-dep-self.mjb", "MJB_MISSING_DEP"); 790 base3(); p16(nd(1) + N_DEPS, 3); bad("bad-missing-dep-out-of-range.mjb", "MJB_MISSING_DEP"); 791 base3(); p32(nd(0) + N_GEN, 2); bad("bad-stale-generation.mjb", "MJB_STALE_GENERATION"); 792 base3(); dep(0, 1); bad("bad-cycle-two.mjb", "MJB_CYCLE"); 793 base3(); dep(0, 2); bad("bad-cycle-three.mjb", "MJB_CYCLE"); 794 base3(); p64(doc + H_B_TIME, 299); bad("bad-over-budget-time.mjb", "MJB_OVER_BUDGET"); 795 base3(); p64(doc + H_B_FUEL, 2999); bad("bad-over-budget-fuel.mjb", "MJB_OVER_BUDGET"); 796 base3(); p64(doc + H_B_WORK, 2999); bad("bad-over-budget-work.mjb", "MJB_OVER_BUDGET"); 797 base3(); p32(doc + H_B_IN, 63); bad("bad-over-budget-input-tokens.mjb", "MJB_OVER_BUDGET"); 798 base3(); p32(doc + H_B_OUT, 15); bad("bad-over-budget-output-tokens.mjb", "MJB_OVER_BUDGET"); 799 base3(); p64(doc + H_B_MEMORY, ((uint64_t)1 << 20) - 1); bad("bad-over-budget-memory.mjb", "MJB_OVER_BUDGET"); 800 base3(); p64(doc + H_B_FUEL, UINT64_MAX); p64(nd(0) + N_FUEL, UINT64_MAX); bad("bad-over-budget-overflow.mjb", "MJB_OVER_BUDGET"); 801 802 if (fclose(gen_expected) != 0) gen_fail = 1; 803 return gen_fail ? 2 : 0; 804 } 805 806 int main(int argc, char **argv) { 807 char out[1024]; 808 size_t n; 809 if (argc == 2 && argv[1][0] != '-') { 810 if (slurp(argv[1], &n)) { fprintf(stderr, "cannot read %s\n", argv[1]); return 2; } 811 verdict_line(file_buf, n, out, sizeof out); 812 puts(out); 813 return strncmp(out, "ok ", 3) == 0 ? 0 : 1; 814 } 815 if (argc == 3 && strcmp(argv[1], "--corpus") == 0) return corpus(argv[2]); 816 if (argc == 3 && strcmp(argv[1], "--make-vectors") == 0) return make_vectors(argv[2]); 817 fprintf(stderr, "usage: mjb_verify FILE | --corpus DIR | --make-vectors DIR\n"); 818 return 2; 819 }