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/test_omega_ldst_gb10.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 / test_omega_ldst_gb10.c` 300 lines ยท 16144 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 /* 18 * E1 row 2: general load/store on the GB10 (src/omega_numeric_ldst_gb10.h). 19 * 20 * Modes (one per run): 21 * (none) host tier: spec rules, kernel build/check, host model equals an independent 22 * byte-level oracle on every spec of the table, negative tests. No device. 23 * --dump DIR write each table kernel as DIR/kNNN.bin and its expected listing as DIR/kNNN.lst 24 * (consumed by tools/ldst_nvdisasm_check.sh) 25 * --chip GB10: every spec of the table, chip output compared byte for byte with the host 26 * model. One RESULT line per spec, then a VERDICT line. A byte still equal to the 27 * fill pattern where the model wrote something is counted as "unwritten" (the C3 28 * unwritten-output signature), separately from wrong values. 29 */ 30 #include "omega_numeric.h" 31 #include "omega_numeric_ldst_gb10.h" 32 #include "sha256.h" 33 34 #include 35 #include 36 #include 37 #include 38 39 #define MAX_SPECS 400 40 #define COUNT 1000u 41 #define FILL 0xA5u 42 43 static OmegaLdstSpec g_specs[MAX_SPECS]; 44 static size_t g_nspecs; 45 static int g_pass, g_fail; 46 static void check(int ok, const char *id) { 47 if (ok) { g_pass++; printf("[PASS] %s\n", id); } else { g_fail++; printf("[FAIL] %s\n", id); } 48 } 49 50 static uint64_t rng_state = 0x243f6a8885a308d3ull; 51 static uint32_t rnd(void) { rng_state ^= rng_state << 13; rng_state ^= rng_state >> 7; rng_state ^= rng_state << 17; return (uint32_t)(rng_state >> 16); } 52 53 /* Every valid (ld, st) pair; for each, offsets and strides chosen from a fixed set so negative and 54 * positive offsets, packed and sparse strides, and 24-bit-field extremes are all covered. */ 55 static void build_table(void) { 56 static const int soff[] = { 0, 1, -1, 2, -3, 5 }; /* multiplied by the width */ 57 static const unsigned smul[] = { 1, 2, 3, 5 }; 58 g_nspecs = 0; 59 for (unsigned l = 0; l < OMEGA_LD_COUNT; l++) 60 for (unsigned t = 0; t < OMEGA_ST_COUNT; t++) { 61 size_t lb = omega_ld_bytes(l), sb = omega_st_bytes(t); 62 OmegaLdstSpec probe = { (uint8_t)l, (uint8_t)t, (uint32_t)lb, (uint32_t)sb, 0, 0 }; 63 if (omega_ldst_check_spec(&probe, 1, NULL, 0) != 0) continue; 64 for (unsigned v = 0; v < 6; v++) { 65 OmegaLdstSpec s; 66 s.ld = (uint8_t)l; s.st = (uint8_t)t; 67 s.in_stride = (uint32_t)(lb * smul[(v + l) % 4]); 68 s.out_stride = (uint32_t)(sb * smul[(v + t) % 4]); 69 s.in_off = (int32_t)lb * soff[(v + t) % 6]; 70 s.out_off = (int32_t)sb * soff[(v + l + 1) % 6]; 71 if (g_nspecs < MAX_SPECS) g_specs[g_nspecs++] = s; 72 } 73 } 74 /* 24-bit offset extremes and a large stride (one wide spec each) */ 75 OmegaLdstSpec e1 = { OMEGA_LD_B32, OMEGA_ST_B32, 4, 4, 0, 0 }; 76 e1.in_off = 0; /* extremes of the field are exercised by the encoder tests below; the chip uses the pad */ 77 g_specs[g_nspecs++] = e1; 78 OmegaLdstSpec e2 = { OMEGA_LD_B128, OMEGA_ST_B128, 4096, 64, 16, -16 }; 79 g_specs[g_nspecs++] = e2; 80 OmegaLdstSpec e3 = { OMEGA_LD_S8, OMEGA_ST_B32, 1, 4, 0, 0 }; 81 g_specs[g_nspecs++] = e3; 82 OmegaLdstSpec e4 = { OMEGA_LD_S16, OMEGA_ST_B32, 2, 4, -2, 4 }; 83 g_specs[g_nspecs++] = e4; 84 } 85 86 typedef struct { uint8_t *in, *out; size_t in_len, out_len; } Bufs; 87 88 static int alloc_bufs(const OmegaLdstSpec *s, Bufs *b) { 89 int64_t il, ih, ol, oh; 90 if (omega_ldst_extent(s, COUNT, &il, &ih, &ol, &oh) != 0) return -1; 91 size_t in_need = (size_t)(ih > (int64_t)COUNT * 4 ? ih : (int64_t)COUNT * 4) + OMEGA_LDST_PAD; 92 b->in_len = (in_need + 15) & ~15ull; 93 b->out_len = ((size_t)oh + OMEGA_LDST_PAD + 15) & ~15ull; 94 b->in = aligned_alloc(16, b->in_len); 95 b->out = aligned_alloc(16, b->out_len); 96 if (!b->in || !b->out) return -1; 97 for (size_t i = 0; i < b->in_len; i++) b->in[i] = (uint8_t)rnd(); 98 memset(b->out, FILL, b->out_len); 99 return 0; 100 } 101 102 /* Independent oracle: typed C loads and stores, no shared code with the host model. */ 103 static void oracle(const OmegaLdstSpec *s, const Bufs *b, uint8_t *out) { 104 for (size_t i = 0; i < COUNT; i++) { 105 const uint8_t *src = b->in + OMEGA_LDST_PAD + (int64_t)s->in_off + (int64_t)i * s->in_stride; 106 uint8_t *dst = out + OMEGA_LDST_PAD + (int64_t)s->out_off + (int64_t)i * s->out_stride; 107 uint32_t lo = 0, w[4] = { 0, 0, 0, 0 }; 108 switch (s->ld) { 109 case OMEGA_LD_U8: lo = *src; break; 110 case OMEGA_LD_S8: lo = (uint32_t)(int32_t)(int8_t)*src; break; 111 case OMEGA_LD_U16: { uint16_t v; memcpy(&v, src, 2); lo = v; break; } 112 case OMEGA_LD_S16: { int16_t v; memcpy(&v, src, 2); lo = (uint32_t)(int32_t)v; break; } 113 case OMEGA_LD_B32: memcpy(&lo, src, 4); break; 114 case OMEGA_LD_B64: memcpy(w, src, 8); lo = w[0]; break; 115 case OMEGA_LD_B128: memcpy(w, src, 16); lo = w[0]; break; 116 } 117 if (s->ld <= OMEGA_LD_B32) w[0] = lo; /* B64 and B128 already filled w */ 118 switch (s->st) { 119 case OMEGA_ST_B8: *dst = (uint8_t)w[0]; break; 120 case OMEGA_ST_B16: { uint16_t v = (uint16_t)w[0]; memcpy(dst, &v, 2); break; } 121 case OMEGA_ST_B32: memcpy(dst, &w[0], 4); break; 122 case OMEGA_ST_B64: memcpy(dst, w, 8); break; 123 case OMEGA_ST_B128: memcpy(dst, w, 16); break; 124 } 125 } 126 } 127 128 static int dump(const char *dir) { 129 for (size_t k = 0; k < g_nspecs; k++) { 130 uint8_t code[OMEGA_LDST_CODE_BYTES]; 131 static char lst[8192]; 132 size_t cl = 0; 133 char path[512]; 134 if (omega_ldst_build_kernel(&g_specs[k], code, sizeof code, &cl) != 0 || omega_ldst_listing(&g_specs[k], lst, sizeof lst) != 0) return 1; 135 snprintf(path, sizeof path, "%s/k%03zu.bin", dir, k); 136 FILE *f = fopen(path, "wb"); if (!f) return 1; fwrite(code, 1, cl, f); fclose(f); 137 snprintf(path, sizeof path, "%s/k%03zu.lst", dir, k); 138 f = fopen(path, "w"); if (!f) return 1; fputs(lst, f); fclose(f); 139 } 140 printf("dumped %zu kernels\n", g_nspecs); 141 return 0; 142 } 143 144 static int host_tier(void) { 145 char why[200], d[96]; 146 int all_check = 1, all_model = 1, all_build = 1; 147 for (size_t k = 0; k < g_nspecs; k++) { 148 const OmegaLdstSpec *s = &g_specs[k]; 149 uint8_t code[OMEGA_LDST_CODE_BYTES]; 150 size_t cl = 0; 151 if (omega_ldst_check_spec(s, COUNT, why, sizeof why) != 0) { all_check = 0; printf(" spec %zu (%s) refused: %s\n", k, omega_ldst_describe(s, d, sizeof d), why); continue; } 152 if (omega_ldst_build_kernel(s, code, sizeof code, &cl) != 0 || omega_ldst_check_kernel(s, code, cl, why, sizeof why) != 0) { all_build = 0; printf(" spec %zu build/check: %s\n", k, why); } 153 Bufs b; 154 if (alloc_bufs(s, &b) != 0) { all_model = 0; continue; } 155 uint8_t *want = malloc(b.out_len); 156 memcpy(want, b.out, b.out_len); 157 oracle(s, &b, want); 158 if (omega_ldst_host_run(s, b.in, b.in_len, b.out, b.out_len, OMEGA_LDST_PAD, COUNT) != 0 || memcmp(want, b.out, b.out_len) != 0) { 159 all_model = 0; printf(" spec %zu (%s): host model != oracle\n", k, omega_ldst_describe(s, d, sizeof d)); 160 } 161 free(want); free(b.in); free(b.out); 162 } 163 char id[96]; 164 snprintf(id, sizeof id, "LDST_TABLE_SPECS_VALID (%zu specs)", g_nspecs); check(all_check, id); 165 check(all_build, "LDST_KERNELS_BUILD_AND_PASS_STRUCTURAL_CHECK"); 166 check(all_model, "LDST_HOST_MODEL_EQUALS_BYTE_ORACLE"); 167 168 /* every (ld, st) pair of the contract appears in the table */ 169 int seen[OMEGA_LD_COUNT][OMEGA_ST_COUNT] = { { 0 } }, miss = 0; 170 for (size_t k = 0; k < g_nspecs; k++) seen[g_specs[k].ld][g_specs[k].st] = 1; 171 for (unsigned l = 0; l < OMEGA_LD_COUNT; l++) for (unsigned t = 0; t < OMEGA_ST_COUNT; t++) { 172 OmegaLdstSpec p = { (uint8_t)l, (uint8_t)t, (uint32_t)omega_ld_bytes(l), (uint32_t)omega_st_bytes(t), 0, 0 }; 173 if (omega_ldst_check_spec(&p, 1, NULL, 0) == 0 && !seen[l][t]) miss++; 174 } 175 check(miss == 0, "LDST_TABLE_COVERS_EVERY_VALID_LOAD_STORE_PAIR"); 176 177 /* offset field extremes: the encoder carries the full signed 24-bit range */ 178 OmegaLdstSpec x = { OMEGA_LD_B32, OMEGA_ST_B32, 4, 4, 0x7ffffc, -0x800000 }; 179 uint8_t code[OMEGA_LDST_CODE_BYTES]; 180 size_t cl = 0; 181 check(omega_ldst_build_kernel(&x, code, sizeof code, &cl) == 0 && omega_ldst_check_kernel(&x, code, cl, why, sizeof why) == 0, "LDST_OFFSET_24BIT_EXTREMES_ENCODE"); 182 183 /* negative: each refusal rule */ 184 struct { OmegaLdstSpec s; size_t n; const char *id; } neg[] = { 185 { { OMEGA_LD_B32, OMEGA_ST_B64, 4, 8, 0, 0 }, 4, "NEG_STORE_WIDER_THAN_LOAD_REFUSED" }, 186 { { OMEGA_LD_U8, OMEGA_ST_B64, 1, 8, 0, 0 }, 4, "NEG_NARROW_LOAD_WIDE_STORE_REFUSED" }, 187 { { OMEGA_LD_B32, OMEGA_ST_B32, 2, 4, 0, 0 }, 4, "NEG_STRIDE_BELOW_WIDTH_REFUSED" }, 188 { { OMEGA_LD_B32, OMEGA_ST_B32, 6, 4, 0, 0 }, 4, "NEG_STRIDE_NOT_MULTIPLE_REFUSED" }, 189 { { OMEGA_LD_B32, OMEGA_ST_B32, 4, 4, 2, 0 }, 4, "NEG_INPUT_OFFSET_MISALIGNED_REFUSED" }, 190 { { OMEGA_LD_B32, OMEGA_ST_B32, 4, 4, 0, 6 }, 4, "NEG_OUTPUT_OFFSET_MISALIGNED_REFUSED" }, 191 { { OMEGA_LD_B32, OMEGA_ST_B32, 4, 4, 0x800000, 0 }, 4, "NEG_OFFSET_OUTSIDE_24BIT_REFUSED" }, 192 { { OMEGA_LD_B32, OMEGA_ST_B32, 4, 4, -0x800004, 0 }, 4, "NEG_NEGATIVE_OFFSET_OUTSIDE_24BIT_REFUSED" }, 193 { { OMEGA_LD_B32, OMEGA_ST_B32, 0, 4, 0, 0 }, 4, "NEG_ZERO_STRIDE_REFUSED" }, 194 { { OMEGA_LD_B32, OMEGA_ST_B32, 1u << 24, 4, 0, 0 }, 4, "NEG_STRIDE_2_POW_24_REFUSED" }, 195 { { 9, OMEGA_ST_B32, 4, 4, 0, 0 }, 4, "NEG_UNKNOWN_LOAD_KIND_REFUSED" }, 196 { { OMEGA_LD_B32, 9, 4, 4, 0, 0 }, 4, "NEG_UNKNOWN_STORE_KIND_REFUSED" }, 197 { { OMEGA_LD_B32, OMEGA_ST_B32, 4, 4, 0, 0 }, 0, "NEG_ZERO_COUNT_REFUSED" }, 198 }; 199 for (size_t i = 0; i < sizeof neg / sizeof neg[0]; i++) check(omega_ldst_check_spec(&neg[i].s, neg[i].n, why, sizeof why) != 0, neg[i].id); 200 201 /* negative: a mutated kernel word is caught, each patched field */ 202 OmegaLdstSpec ok = { OMEGA_LD_B64, OMEGA_ST_B64, 16, 24, 8, -16 }; 203 uint8_t good[OMEGA_LDST_CODE_BYTES]; 204 omega_ldst_build_kernel(&ok, good, sizeof good, &cl); 205 struct { size_t byte; uint8_t xor; const char *id; } mut[] = { 206 { 16 * 13 + 5, 0x02, "NEG_LDG_OFFSET_BIT_FLIPPED_CAUGHT" }, 207 { 16 * 13 + 9, 0x02, "NEG_LDG_SIZE_FIELD_CHANGED_CAUGHT" }, 208 { 16 * 13 + 2, 0x04, "NEG_LDG_DEST_REGISTER_CHANGED_CAUGHT" }, 209 { 16 * 18 + 9, 0x04, "NEG_STG_SIZE_FIELD_CHANGED_CAUGHT" }, 210 { 16 * 18 + 5, 0x01, "NEG_STG_OFFSET_BIT_FLIPPED_CAUGHT" }, 211 { 16 * 12 + 4, 0x01, "NEG_INPUT_STRIDE_WORD_CHANGED_CAUGHT" }, 212 { 16 * 16 + 4, 0x01, "NEG_OUTPUT_STRIDE_WORD_CHANGED_CAUGHT" }, 213 { 16 * 17 + 15, 0x10, "NEG_IADD3_CONTROL_WORD_CHANGED_CAUGHT" }, 214 { 16 * 5 + 1, 0x01, "NEG_PROLOGUE_WORD_CHANGED_CAUGHT" }, 215 { 16 * 20 + 0, 0x01, "NEG_EPILOGUE_WORD_CHANGED_CAUGHT" }, 216 }; 217 for (size_t i = 0; i < sizeof mut / sizeof mut[0]; i++) { 218 uint8_t m[OMEGA_LDST_CODE_BYTES]; 219 memcpy(m, good, sizeof m); 220 m[mut[i].byte] ^= mut[i].xor; 221 check(omega_ldst_check_kernel(&ok, m, sizeof m, why, sizeof why) != 0, mut[i].id); 222 } 223 check(omega_ldst_check_kernel(&ok, good, sizeof good - 16, why, sizeof why) != 0, "NEG_SHORT_KERNEL_CAUGHT"); 224 225 /* negative: host model refuses an access outside the buffer */ 226 uint8_t tiny_in[64] = { 0 }, tiny_out[64] = { 0 }; 227 OmegaLdstSpec small = { OMEGA_LD_B32, OMEGA_ST_B32, 4, 4, 0, 0 }; 228 check(omega_ldst_host_run(&small, tiny_in, sizeof tiny_in, tiny_out, sizeof tiny_out, 0, 16) == 0, "LDST_HOST_RUN_INSIDE_BUFFER_OK"); 229 check(omega_ldst_host_run(&small, tiny_in, sizeof tiny_in, tiny_out, sizeof tiny_out, 0, 17) != 0, "NEG_HOST_RUN_PAST_END_REFUSED"); 230 OmegaLdstSpec neg_off = { OMEGA_LD_B32, OMEGA_ST_B32, 4, 4, -4, 0 }; 231 check(omega_ldst_host_run(&neg_off, tiny_in, sizeof tiny_in, tiny_out, sizeof tiny_out, 0, 4) != 0, "NEG_HOST_RUN_BEFORE_START_REFUSED"); 232 check(omega_ldst_gb10_run(&small, tiny_in, sizeof tiny_in, tiny_out, sizeof tiny_out, 0, 17) != 0, "NEG_GB10_RUN_PAST_END_REFUSED_BEFORE_DEVICE"); 233 234 printf("Gate LDST host: PASSED=%d FAILED=%d\n", g_pass, g_fail); 235 printf("VERDICT %s\n", g_fail ? "HOST_REGRESSION" : "PASS_HOST"); 236 return g_fail ? 1 : 0; 237 } 238 239 /* first..last (inclusive) specs, each run `repeats` times; omega_ldst_gb10_set_spec_id labels any OMEGA_DEVERR line. */ 240 static int chip(size_t first, size_t last, unsigned repeats) { 241 int bad_specs = 0; 242 for (size_t k = first; k <= last && k < g_nspecs; k++) 243 for (unsigned rep = 0; rep < repeats; rep++) { 244 const OmegaLdstSpec *s = &g_specs[k]; 245 Bufs b; 246 char d[96]; 247 if (alloc_bufs(s, &b) != 0) return 2; 248 uint8_t *want = malloc(b.out_len), *got = malloc(b.out_len); 249 memcpy(want, b.out, b.out_len); 250 memcpy(got, b.out, b.out_len); 251 if (omega_ldst_host_run(s, b.in, b.in_len, want, b.out_len, OMEGA_LDST_PAD, COUNT) != 0) return 2; 252 omega_ldst_gb10_set_spec_id((int)k); 253 int rc = omega_ldst_gb10_run(s, b.in, b.in_len, got, b.out_len, OMEGA_LDST_PAD, COUNT); 254 uint64_t mism = 0, unwritten = 0; 255 if (rc == 0) 256 for (size_t i = 0; i < b.out_len; i++) 257 if (got[i] != want[i]) { mism++; if (got[i] == FILL && want[i] != FILL) unwritten++; } 258 int ok = rc == 0 && mism == 0; 259 if (!ok) bad_specs++; 260 printf("RESULT chip spec=%zu %s count=%u rc=%d mismatches=%" PRIu64 " unwritten=%" PRIu64 " verdict=%s\n", k, 261 omega_ldst_describe(s, d, sizeof d), COUNT, rc, mism, unwritten, ok ? "PASS" : "FAIL"); 262 fflush(stdout); 263 free(want); free(got); free(b.in); free(b.out); 264 } 265 printf("VERDICT %s\n", bad_specs ? "FAIL" : "PASS"); 266 return bad_specs ? 1 : 0; 267 } 268 269 /* whole token must be decimal digits; false otherwise */ 270 static int parse_num(const char *s, unsigned long *out) { 271 char *end; 272 if (!*s || *s < '0' || *s > '9') return 0; 273 *out = strtoul(s, &end, 10); 274 return *end == '\0'; 275 } 276 277 /* --chip [--spec N] [--repeats R]: all specs once by default; with --spec only spec N, R times (default 1). */ 278 int main(int argc, char **argv) { 279 build_table(); 280 if (argc >= 3 && !strcmp(argv[1], "--dump")) return dump(argv[2]); 281 if (argc >= 2 && !strcmp(argv[1], "--chip")) { 282 size_t first = 0, last = g_nspecs ? g_nspecs - 1 : 0; 283 unsigned repeats = 1; 284 for (int i = 2; i < argc; i++) { 285 if (!strcmp(argv[i], "--spec") && i + 1 < argc) { 286 unsigned long v; 287 if (!parse_num(argv[++i], &v)) { fprintf(stderr, "--spec needs a whole number, got '%s'\n", argv[i]); return 2; } 288 first = last = (size_t)v; 289 if (first >= g_nspecs) { fprintf(stderr, "--spec %zu out of range (0..%zu)\n", first, g_nspecs - 1); return 2; } 290 } else if (!strcmp(argv[i], "--repeats") && i + 1 < argc) { 291 unsigned long v; 292 if (!parse_num(argv[++i], &v)) { fprintf(stderr, "--repeats needs a whole number, got '%s'\n", argv[i]); return 2; } 293 repeats = (unsigned)v; 294 if (repeats == 0) { fprintf(stderr, "--repeats must be >= 1\n"); return 2; } 295 } else { fprintf(stderr, "unknown chip argument: %s\n", argv[i]); return 2; } 296 } 297 return chip(first, last, repeats); 298 } 299 return host_tier(); 300 }