Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
security: carry the published advisory fixes to glm53, qwen36 and deepseek_v41
Marcello Costagliola5697a1b0de1d
diff --git a/.github/workflows/check.yml b/.github/workflows/check.ymlindex c25acd06..f8d6178a 100644--- a/.github/workflows/check.yml+++ b/.github/workflows/check.yml@@ -130,6 +130,7 @@ jobs:python c/tools/make_edge_tiny_tokenizer.py --vocab-size 281 /tmp/glm53_stream-i4cd c && COLI_GLM53_FIXTURE=/tmp/glm53_stream-i4 python -m unittest -v tests.test_glm53_dashboardCOLI_GLM53_FIXTURE=/tmp/glm53_stream-i4 python -m unittest -v tests.test_glm53_context_exceeded+ COLI_GLM53_FIXTURE=/tmp/glm53_stream-i4 python -m unittest -v tests.test_glm53_hostile_weights# The two stdlib-only oracles used to be argparse scripts that `make# test-python` collected as zero tests (#1700). They run here, on the# fixtures generated above, token-exact against transformers in f32.diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex efadaa1b..4d0c77f0 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -572,6 +572,12 @@ jobs:fiecho "sanitizers clean ($fx):"; grep -E "Matching tokens" san.log || truedone+ # Same sanitized binary on hostile weights: an all-NaN router row and an+ # oversized e_score_correction_bias must degrade or be refused, never+ # reach the heap.+ ASAN_OPTIONS=detect_leaks=0 QWEN36_TINY_C=qwen36_tiny_c \+ QWEN36_TINY_REF=qwen36_tiny/ref_full.json \+ python3 -m unittest -v tests.test_qwen36_hostile_weights- name: KV prefix reuse is token-identical (serve, several turns)run: |cd cdiff --git a/c/Makefile b/c/Makefileindex bfa43574..be9706e1 100644--- a/c/Makefile+++ b/c/Makefile@@ -1440,6 +1440,9 @@ tests/test_qwen38_config$(EXE): tests/test_qwen38_config.c sse41_kernels.h qwen3tests/test_dsv41_serve_budget$(EXE): tests/test_dsv41_serve_budget.c sse41_kernels.h deepseek_v41.c cli_args.h st.h json.h tok.h tok_unicode.h tok_unicode_deepseek.h compat.h omp_tune.h kv_prefix.h pin_pool.h quant.h fp8_format.h idot.h sparse_attn.h hyper_connections.h serve_codec.h serve_poll.h$(CC) $(NOCUDA_CFLAGS) $< -o $@ $(NOCUDA_LDFLAGS)+tests/test_dsv41_untrusted_load$(EXE): tests/test_dsv41_untrusted_load.c sse41_kernels.h deepseek_v41.c cli_args.h st.h json.h tok.h tok_unicode.h tok_unicode_deepseek.h compat.h omp_tune.h kv_prefix.h pin_pool.h quant.h fp8_format.h idot.h sparse_attn.h hyper_connections.h serve_codec.h serve_poll.h+ $(CC) $(NOCUDA_CFLAGS) $< -o $@ $(NOCUDA_LDFLAGS)+tests/test_qwen38_idot$(EXE): tests/test_qwen38_idot.c sse41_kernels.h qwen38.c qwen38_core.h kv_prefix.h qwen38_nfc.h qwen38_nfc_tables.h st.h json.h compat.h quant.h fp8_format.h idot.h route_trace.h tok_unicode.h tok_unicode_deepseek.h tok_unicode_o200k.h$(CC) $(NOCUDA_CFLAGS) $< -o $@ $(NOCUDA_LDFLAGS)diff --git a/c/deepseek_v41.c b/c/deepseek_v41.cindex d4e2df1d..0948f9f7 100644--- a/c/deepseek_v41.c+++ b/c/deepseek_v41.c@@ -347,8 +347,11 @@ static void wf_load(shards *S, WF *w, const char *name, int64_t n) {w->n = n;w->w = xmalloc((size_t)n * sizeof(float), name);/* st_read_f32 widens bf16/f16 as well, so a checkpoint that stores one of these- * small tensors in bf16 rather than f32 still loads. */- if (st_read_f32(S, name, w->w, 0) != n) {+ * small tensors in bf16 rather than f32 still loads.+ * SEC: capped, because `n` comes from config.json and the element count from the+ * file. Uncapped, a tensor longer than `n` was copied over the heap first and+ * refused second, by the count check below, after the damage. */+ if (st_read_f32_cap(S, name, w->w, n, 0) != n) {fprintf(stderr, "%s: expected %lld floats\n", name, (long long)n); exit(1); }}@@ -546,6 +549,15 @@ static void engram_load_sidecar(Engram *e, const char *snap) {jval *multipliers = json_get(root, "multipliers");if (!primes || !offsets || !multipliers) {fprintf(stderr, "[engram] sidecar lacks primes/offsets/multipliers\n"); exit(1); }+ /* SEC: the loop below indexes all three by table, up to the length of layer_ids --+ * a different number, chosen by the same file. A shorter array was read past its+ * end, and a key that is not an array has no kids at all. */+ const jval *per_table[] = { primes, offsets, multipliers };+ const char *per_table_name[] = { "primes", "offsets", "multipliers" };+ for (int k = 0; k < 3; k++)+ if (per_table[k]->t != J_ARR || per_table[k]->len < e->n_layers) {+ fprintf(stderr, "[engram] %s must be an array with one entry per table (%d)\n",+ per_table_name[k], e->n_layers); exit(1); }for (int layer = 0; layer < e->n_layers; layer++) {jval *rows = primes->kids[layer];for (int n = 0; n < rows->len && n < V41_MAX_NGRAM; n++)diff --git a/c/glm53.c b/c/glm53.cindex 3b8b1b13..a5cbecf1 100644--- a/c/glm53.c+++ b/c/glm53.c@@ -1804,8 +1804,10 @@ static void ffn_layer(GModel *m, const GLayer *l, int index, const float *x,float choice = score[e] + (l->rbias ? l->rbias[e] : 0.0f);if (!used && choice > value) { value = choice; best = e; }}- mine[k] = best;- mine_w[k] = score[best];+ /* SEC: all-NaN scores leave best at -1, and score[-1] is the very+ * next read. See rt_router_pick in route_trace.h. */+ mine[k] = rt_router_pick(best, k, c->n_experts, index);+ mine_w[k] = score[mine[k]];total += mine_w[k];}for (int k = 0; k < topk; k++)diff --git a/c/qwen36.c b/c/qwen36.cindex 0cdb9923..8c9b821a 100644--- a/c/qwen36.c+++ b/c/qwen36.c@@ -1807,8 +1807,9 @@ static void model_init_range(Model *m, const char *snap, int cap, int bits,} else { l->qn = NULL; l->kn = NULL; }/* router correction bias (optional) */snprintf(nm,sizeof(nm),"model.layers.%d.mlp.gate.e_score_correction_bias", ai);- if (c->n_experts && st_has(&m->S, nm)) { l->gate_bias = falloc(c->n_experts); st_read_f32(&m->S, nm, l->gate_bias, 0); }- else l->gate_bias = NULL;+ /* SEC: through load_t_n, which refuses a length other than n_experts. The+ * buffer used to be n_experts floats and the copy as long as the file said. */+ l->gate_bias = c->n_experts && st_has(&m->S, nm) ? load_t_n(m, nm, c->n_experts) : NULL;/* shared expert (dense, int8-during-load). A dense model's MLP is the same* SwiGLU under mlp.{gate,up,down}_proj, with no gate in front of it. */const char *shp = c->n_experts ? "mlp.shared_expert." : "mlp.";@@ -2698,6 +2699,23 @@ static void moe(Model *m, Layer *l, int layer, float *x, int S, float *out) {m->route.agree_hit += (uint64_t)K; m->route.agree_tot += (uint64_t)K; m->route.kl_n++;}}+ /* SEC: an all-NaN router row (a corrupt tile, an fp overflow) leaves best at+ * -1 above -- NaN > bv is false for every expert -- and route_select pads+ * with -1 when fewer than K experts rank. Every consumer below takes the id+ * as an index and a file offset: expert_get() went looking for experts.-1.+ * Same degradation as rt_router_pick in route_trace.h, which this engine+ * does not include: the slot's own index, in range because topk <=+ * n_experts is a config check, at weight 0 so the slot adds nothing. */+ for (int kk = 0; kk < K; kk++) {+ if (idx[kk] >= 0) continue;+ static int warned;+ if (!warned) {+ warned = 1;+ fprintf(stderr, "[router] non-finite logits at layer %d, or fewer than top-k "+ "experts eligible: selection degraded\n", layer);+ }+ idx[kk] = kk; val[kk] = 0.f;+ }if (m->resident_collecting) {for (int kk = 0; kk < K; kk++) if (idx[kk] >= 0) m->seen[(int64_t)layer * E + idx[kk]] = 1;}diff --git a/c/tests/safetensors_edit.py b/c/tests/safetensors_edit.pynew file mode 100644index 00000000..67ccc3d5--- /dev/null+++ b/c/tests/safetensors_edit.py@@ -0,0 +1,59 @@+"""Stdlib edits of a safetensors container, for tests that need a hostile one.++A model directory is as attacker-chosen as its config.json: whoever ships it picks+every byte of every tensor and every length in every header. These helpers copy a+known-good tiny fixture and change exactly one thing, so a test can ask what the+engine does with that one thing.+"""+import json+import shutil+import struct+from pathlib import Path++_NAN = {"F32": struct.pack("<I", 0x7FC00000), "F16": struct.pack("<H", 0x7E00),+ "BF16": struct.pack("<H", 0x7FC0)}+++def _read_header(path):+ with open(path, "rb") as handle:+ size = struct.unpack("<Q", handle.read(8))[0]+ return json.loads(handle.read(size)), 8 + size+++def _shard_of(directory, name):+ for shard in sorted(Path(directory).glob("*.safetensors")):+ header, start = _read_header(shard)+ if name in header:+ return shard, header, start+ raise KeyError(f"{name} is in no shard of {directory}")+++def copy_fixture(source, destination):+ shutil.copytree(source, destination)+ return Path(destination)+++def fill_nan(directory, name):+ """Overwrite every element of tensor `name` with a quiet NaN of its dtype."""+ shard, header, start = _shard_of(directory, name)+ entry = header[name]+ pattern = _NAN[entry["dtype"]]+ first, last = entry["data_offsets"]+ with open(shard, "r+b") as handle:+ handle.seek(start + first)+ handle.write(pattern * ((last - first) // len(pattern)))+++def add_f32(directory, beside, name, count, value=0x41414141):+ """Append an F32 tensor `name` of `count` elements to the shard holding `beside`.++ The default fill is 0x41414141, 'AAAA': the bytes a heap overflow would spray.+ """+ shard, header, start = _shard_of(directory, beside)+ data = shard.read_bytes()[start:]+ header[name] = {"dtype": "F32", "shape": [count],+ "data_offsets": [len(data), len(data) + 4 * count]}+ encoded = json.dumps(header, separators=(",", ":")).encode()+ encoded += b" " * (-len(encoded) % 8)+ shard.write_bytes(struct.pack("<Q", len(encoded)) + encoded + data+ + struct.pack("<I", value) * count)diff --git a/c/tests/test_dsv41_untrusted_load.c b/c/tests/test_dsv41_untrusted_load.cnew file mode 100644index 00000000..162dc988--- /dev/null+++ b/c/tests/test_dsv41_untrusted_load.c@@ -0,0 +1,197 @@+/* deepseek_v41.c reads two things from the snapshot directory that are as+ * attacker-chosen as the weights themselves, and both trusted the file for a size+ * nothing had checked:+ *+ * - wf_load() allocated `n` floats and let st_read_f32 write as many as the TENSOR+ * declares, comparing the count only after the copy. A norm.weight with more+ * elements than the config implies overran the heap with file bytes, and the+ * check then reported a write it could no longer undo.+ * - engram_load_sidecar() walked primes/offsets/multipliers by the length of+ * layer_ids -- a different, equally file-chosen number -- and dereferenced+ * whatever sat past the end of a shorter array, or a NULL kids pointer when+ * the key was not an array at all.+ *+ * Both refusals exit(1) in place, so those cases run in a forked child with stderr+ * captured (the idiom of tests/test_dup_name_refusal.c). Under `make test-asan` the+ * unfixed loader is a heap-buffer-overflow report instead of the refusal message+ * these checks ask for; in a plain build it is the wrong message, or a crash. */+#define main dsv41_main_unused+#include "../deepseek_v41.c"+#undef main+#ifndef _WIN32+#include <sys/wait.h>+#endif++static int g_fails;+static void check(int ok, const char *what) {+ if (!ok) { printf("FAIL: %s\n", what); g_fails++; }+}++#ifndef _WIN32+static char g_dir[512];+static int64_t g_want;++/* one F32 tensor holding 1, 2, ..., n */+static void write_f32_shard(const char *dir, const char *name, int n) {+ char path[600], hdr[256];+ snprintf(path, sizeof(path), "%s/model.safetensors", dir);+ int hl = snprintf(hdr, sizeof(hdr),+ "{\"%s\":{\"dtype\":\"F32\",\"shape\":[%d],\"data_offsets\":[0,%d]}}", name, n, n * 4);+ uint64_t hlen = (uint64_t)hl;+ FILE *f = fopen(path, "wb");+ if (!f) { perror(path); exit(1); }+ fwrite(&hlen, 8, 1, f);+ fwrite(hdr, 1, (size_t)hl, f);+ for (int i = 0; i < n; i++) { float v = (float)(i + 1); fwrite(&v, 4, 1, f); }+ fclose(f);+}++static void write_sidecar(const char *dir, const char *json) {+ char path[600];+ snprintf(path, sizeof(path), "%s/dsv41_engram.json", dir);+ FILE *f = fopen(path, "wb");+ if (!f) { perror(path); exit(1); }+ fputs(json, f);+ fclose(f);+}++static void run_forked(void (*fn)(void), int *exit_code, char *err, size_t err_sz) {+ int pipefd[2];+ if (pipe(pipefd) != 0) { *exit_code = -1; err[0] = 0; return; }+ fflush(stdout); /* else a child that exit()s flushes the parent's FAIL lines again */+ pid_t pid = fork();+ if (pid < 0) { *exit_code = -1; err[0] = 0; return; }+ if (pid == 0) {+ dup2(pipefd[1], 2); close(pipefd[0]); close(pipefd[1]);+ fn();+ _exit(42); /* reaching here means fn() did NOT refuse */+ }+ close(pipefd[1]);+ size_t off = 0; ssize_t n;+ while (off < err_sz - 1 && (n = read(pipefd[0], err + off, err_sz - 1 - off)) > 0) off += (size_t)n;+ err[off] = 0;+ close(pipefd[0]);+ int status = 0; waitpid(pid, &status, 0);+ *exit_code = WIFEXITED(status) ? WEXITSTATUS(status) : -1;+}++static void child_wf_load(void) {+ shards S; st_init(&S, g_dir);+ WF w; wf_load(&S, &w, "norm.weight", g_want);+}++static Engram g_engram;+static void child_engram(void) {+ memset(&g_engram, 0, sizeof(g_engram));+ engram_load_sidecar(&g_engram, g_dir);+}++static void test_wf_load(void) {+ char dir[] = "test_dsv41_untrusted_wf_XXXXXX";+ if (!mkdtemp(dir)) { perror("mkdtemp"); check(0, "wf_load: mkdtemp"); return; }+ snprintf(g_dir, sizeof(g_dir), "%s", dir);+ int code; char err[8192];++ /* longer than the config says: refused before a single float is written */+ write_f32_shard(dir, "norm.weight", 64);+ g_want = 4;+ run_forked(child_wf_load, &code, err, sizeof(err));+ check(code == 1, "wf_load: 64-element tensor into 4 floats exits(1)");+ check(strstr(err, "exceeds destination capacity") != NULL,+ "wf_load: refused by the capacity check, before the copy");+ check(strstr(err, "AddressSanitizer") == NULL, "wf_load: no heap overflow on the way out");+ if (g_fails) printf("--- child stderr ---\n%s\n", err);++ /* shorter than the config says: still refused, by the count check after the read */+ write_f32_shard(dir, "norm.weight", 2);+ run_forked(child_wf_load, &code, err, sizeof(err));+ check(code == 1, "wf_load: 2-element tensor for 4 floats exits(1)");+ check(strstr(err, "expected 4 floats") != NULL, "wf_load: short tensor names the expected count");++ /* exact: loads, and the values are the file's */+ write_f32_shard(dir, "norm.weight", 4);+ shards S; st_init(&S, dir);+ WF w; wf_load(&S, &w, "norm.weight", 4);+ check(w.n == 4 && w.w[0] == 1.f && w.w[3] == 4.f, "wf_load: exact tensor loads its values");+ free(w.w);++ char path[600];+ snprintf(path, sizeof(path), "%s/model.safetensors", dir); unlink(path);+ rmdir(dir);+}++/* two tables; every per-table array has two entries */+#define ENGRAM_OK \+ "{\"max_ngram_size\":3,\"n_heads\":2,\"head_dim\":32,\"pad_id\":0,\"layer_ids\":[1,2]," \+ "\"primes\":[[[5,7],[11,13]],[[17,19],[23,29]]]," \+ "\"offsets\":[[0,5,12,23],[0,17,36,59]]," \+ "\"multipliers\":[[\"3\",\"5\",\"7\"],[\"11\",\"13\",\"17\"]]," \+ "\"token_map\":[0,1,2]}"++static void expect_engram_refused(const char *dir, const char *json, const char *key, const char *what) {+ int code; char err[8192];+ write_sidecar(dir, json);+ run_forked(child_engram, &code, err, sizeof(err));+ char label[160];+ snprintf(label, sizeof(label), "engram: %s exits(1)", what);+ check(code == 1, label);+ snprintf(label, sizeof(label), "engram: %s is refused by name", what);+ int named = strstr(err, "[engram]") != NULL && strstr(err, key) != NULL;+ check(named, label);+ if (code != 1 || !named) printf("--- child stderr (exit %d) ---\n%s\n", code, err);+}++static void test_engram_sidecar(void) {+ char dir[] = "test_dsv41_untrusted_engram_XXXXXX";+ if (!mkdtemp(dir)) { perror("mkdtemp"); check(0, "engram: mkdtemp"); return; }+ snprintf(g_dir, sizeof(g_dir), "%s", dir);++ write_sidecar(dir, ENGRAM_OK);+ child_engram();+ check(g_engram.active == 1 && g_engram.n_layers == 2, "engram: well-formed sidecar loads both tables");+ check(g_engram.primes[1][1][1] == 29 && g_engram.offsets[1][3] == 59 &&+ g_engram.multipliers[1][2] == 17 && g_engram.token_map_len == 3,+ "engram: well-formed sidecar keeps its values");+ free(g_engram.token_map);++ expect_engram_refused(dir,+ "{\"max_ngram_size\":3,\"n_heads\":2,\"head_dim\":32,\"layer_ids\":[1,2],"+ "\"primes\":[[[5,7],[11,13]]],\"offsets\":[[0,5,12,23],[0,17,36,59]],"+ "\"multipliers\":[[\"3\",\"5\",\"7\"],[\"11\",\"13\",\"17\"]],\"token_map\":[0]}",+ "primes", "primes shorter than layer_ids");+ expect_engram_refused(dir,+ "{\"max_ngram_size\":3,\"n_heads\":2,\"head_dim\":32,\"layer_ids\":[1,2],"+ "\"primes\":[[[5,7],[11,13]],[[17,19],[23,29]]],\"offsets\":7,"+ "\"multipliers\":[[\"3\",\"5\",\"7\"],[\"11\",\"13\",\"17\"]],\"token_map\":[0]}",+ "offsets", "offsets that is a number");+ /* an object has kids and a len, so only the type check stands between it and+ * a table read out of a map */+ expect_engram_refused(dir,+ "{\"max_ngram_size\":3,\"n_heads\":2,\"head_dim\":32,\"layer_ids\":[1,2],"+ "\"primes\":[[[5,7],[11,13]],[[17,19],[23,29]]],"+ "\"offsets\":{\"a\":[0,5,12,23],\"b\":[0,17,36,59]},"+ "\"multipliers\":[[\"3\",\"5\",\"7\"],[\"11\",\"13\",\"17\"]],\"token_map\":[0]}",+ "offsets", "offsets that is an object");+ expect_engram_refused(dir,+ "{\"max_ngram_size\":3,\"n_heads\":2,\"head_dim\":32,\"layer_ids\":[1,2],"+ "\"primes\":[[[5,7],[11,13]],[[17,19],[23,29]]],\"offsets\":[[0,5,12,23],[0,17,36,59]],"+ "\"multipliers\":[[\"3\",\"5\",\"7\"]],\"token_map\":[0]}",+ "multipliers", "multipliers shorter than layer_ids");++ char path[600];+ snprintf(path, sizeof(path), "%s/dsv41_engram.json", dir); unlink(path);+ rmdir(dir);+}+#endif++int main(void) {+#ifndef _WIN32+ test_wf_load();+ test_engram_sidecar();+#else+ printf("dsv41 untrusted load: skipped on Windows (no fork)\n");+#endif+ if (g_fails) { printf("%d check(s) failed\n", g_fails); return 1; }+ printf("dsv41 untrusted load: wf_load capacity + engram sidecar shape -- ok\n");+ return 0;+}diff --git a/c/tests/test_glm53_hostile_weights.py b/c/tests/test_glm53_hostile_weights.pynew file mode 100644index 00000000..8d854377--- /dev/null+++ b/c/tests/test_glm53_hostile_weights.py@@ -0,0 +1,85 @@+"""glm53 on a container whose router weights were chosen by someone else.++An all-NaN router row: NaN > best is false for every expert, so the top-k pick+stayed at -1, and the gate weight was read as score[-1] before the id reached the+expert cache. GHSA-5xpg-vw35-2687 fixed this shape in inkling, kimi_k3 and olmoe+through rt_router_pick() in route_trace.h, which glm53 already includes.++Runs in the GLM-5.3 oracle job of check.yml, on the streaming fixture it builds+(the same one test_glm53_context_exceeded uses):++ COLI_GLM53_FIXTURE=/tmp/glm53_stream-i4 python -m unittest -v tests.test_glm53_hostile_weights+"""+import os+import subprocess+import sys+import tempfile+import unittest+from pathlib import Path++sys.path.insert(0, str(Path(__file__).resolve().parent))+from safetensors_edit import copy_fixture, fill_nan # noqa: E402++HERE = Path(__file__).resolve().parent.parent+BINARY = next((path for path in (HERE / "glm53.exe", HERE / "glm53") if path.exists()), None)+FIXTURE = Path(os.environ.get("COLI_GLM53_FIXTURE", ""))+ROUTER = "model.language_model.layers.3.mlp.gate.weight" # the fixture's one MoE layer+SANITIZER = ("ERROR: AddressSanitizer", "runtime error:")+++def serve_turn(snapshot, prompt=b"abc"):+ """One SERVE turn; return (the line that ends it, the engine's stderr)."""+ environment = {**os.environ, "SNAP": str(snapshot), "SERVE": "1",+ "GLM53_BITS": "32", "GLM53_MAXT": "64"}+ with tempfile.TemporaryFile() as errors:+ process = subprocess.Popen([str(BINARY)], env=environment, stdin=subprocess.PIPE,+ stdout=subprocess.PIPE, stderr=errors, bufsize=0)+ ending = None+ try:+ while b"READY" not in (line := process.stdout.readline()):+ if not line:+ break+ else:+ process.stdin.write(f"SUBMIT 7 0 {len(prompt)} 4 0 1\n".encode() + prompt + b"\n")+ process.stdin.flush()+ for _ in range(400):+ line = process.stdout.readline()+ if not line:+ break+ text = line.decode("latin-1").rstrip("\n")+ if text.startswith("DATA "):+ process.stdout.read(int(text.split()[2]))+ process.stdout.readline()+ elif text.startswith(("ERROR ", "DONE ")):+ ending = text+ break+ finally:+ process.stdin.close()+ try:+ process.wait(60)+ except subprocess.TimeoutExpired:+ process.kill()+ process.wait()+ process.stdout.close()+ errors.seek(0)+ return ending, errors.read().decode("utf-8", "replace")++[email protected](BINARY, "glm53 engine is not built")[email protected](FIXTURE.name and (FIXTURE / "config.json").is_file(),+ "COLI_GLM53_FIXTURE not set to a GLM-5.3-Flash container")+class Glm53HostileWeightsTest(unittest.TestCase):+ def test_an_all_nan_router_degrades_instead_of_indexing_minus_one(self):+ with tempfile.TemporaryDirectory() as scratch:+ snapshot = copy_fixture(FIXTURE, Path(scratch) / "model")+ fill_nan(snapshot, ROUTER)+ ending, stderr = serve_turn(snapshot)+ for marker in SANITIZER:+ self.assertNotIn(marker, stderr, stderr[-4000:])+ self.assertIsNotNone(ending, "the engine died mid-turn:\n" + stderr[-4000:])+ self.assertTrue(ending.startswith("DONE 7 "), ending)+ self.assertIn("[router] non-finite logits", stderr)+++if __name__ == "__main__":+ unittest.main()diff --git a/c/tests/test_qwen36_hostile_weights.py b/c/tests/test_qwen36_hostile_weights.pynew file mode 100644index 00000000..629bfdb5--- /dev/null+++ b/c/tests/test_qwen36_hostile_weights.py@@ -0,0 +1,87 @@+"""qwen36 on a container whose weights were chosen by someone else.++Two shapes the published advisories fixed in other engines and qwen36 never got:++ - an all-NaN router row. NaN > best is false for every expert, so the top-k pick+ stayed at -1 -- and route_select pads with -1 by design when fewer than K are+ eligible -- and expert_get() took -1 as an index and as a file offset+ (GHSA-5xpg-vw35-2687 in inkling, kimi_k3 and olmoe).+ - an e_score_correction_bias longer than n_experts. The buffer was n_experts+ floats; the copy was as long as the file said (GHSA-pmq2-6f2p-hjvf in inkling).++Needs the converted tiny container and its reference, which take PyTorch to build:+the Qwen3.6 tiny job of ci.yml makes them and runs this after its sanitizer build,+so a heap error is a diagnostic here and not a matter of luck.++ QWEN36_TINY_C=qwen36_tiny_c QWEN36_TINY_REF=qwen36_tiny/ref_full.json \+ python3 -m unittest -v tests.test_qwen36_hostile_weights+"""+import os+import subprocess+import sys+import tempfile+import unittest+from pathlib import Path++sys.path.insert(0, str(Path(__file__).resolve().parent))+from safetensors_edit import add_f32, copy_fixture, fill_nan # noqa: E402++HERE = Path(__file__).resolve().parent.parent+ENGINE = next((p for p in (HERE / "qwen36.exe", HERE / "qwen36") if p.exists()), None)+FIXTURE = Path(os.environ.get("QWEN36_TINY_C", ""))+REFERENCE = Path(os.environ.get("QWEN36_TINY_REF", ""))+SANITIZER = ("ERROR: AddressSanitizer", "runtime error:")+++def run_engine(snapshot, **extra):+ environment = {**os.environ, "SNAP": str(snapshot), "COLI_DENSE_I8": "0", **extra}+ return subprocess.run([str(ENGINE), "1", "8", str(REFERENCE)], env=environment,+ capture_output=True, text=True, errors="replace", timeout=600)++[email protected](ENGINE, "qwen36 engine is not built")[email protected](FIXTURE.name and (FIXTURE / "config.json").is_file()+ and REFERENCE.is_file(),+ "QWEN36_TINY_C / QWEN36_TINY_REF not set to the converted tiny fixture")+class Qwen36HostileWeightsTest(unittest.TestCase):+ def setUp(self):+ self.scratch = tempfile.TemporaryDirectory()+ self.snapshot = copy_fixture(FIXTURE, Path(self.scratch.name) / "model")++ def tearDown(self):+ self.scratch.cleanup()++ def assertNoSanitizerReport(self, run):+ for marker in SANITIZER:+ self.assertNotIn(marker, run.stderr, run.stderr[-4000:])++ def test_an_all_nan_router_degrades_instead_of_indexing_minus_one(self):+ fill_nan(self.snapshot, "model.layers.0.mlp.gate.weight")+ run = run_engine(self.snapshot)+ self.assertNoSanitizerReport(run)+ # The tokens are wrong -- the router is garbage -- so the reference+ # comparison fails, but the process must finish rather than die on a signal.+ self.assertGreaterEqual(run.returncode, 0, run.stderr[-4000:])+ self.assertIn("[router] non-finite logits", run.stderr)++ def test_the_cache_route_pads_are_not_passed_on_as_minus_one(self):+ # route_select ranks nothing on a NaN row and pads all K slots with -1,+ # a different producer of the same id than the plain loop above.+ fill_nan(self.snapshot, "model.layers.0.mlp.gate.weight")+ run = run_engine(self.snapshot, CACHE_ROUTE="1")+ self.assertNoSanitizerReport(run)+ self.assertGreaterEqual(run.returncode, 0, run.stderr[-4000:])+ self.assertIn("[router] non-finite logits", run.stderr)++ def test_a_correction_bias_longer_than_n_experts_is_refused(self):+ add_f32(self.snapshot, "model.layers.0.mlp.gate.weight",+ "model.layers.0.mlp.gate.e_score_correction_bias", 4096)+ run = run_engine(self.snapshot)+ self.assertNoSanitizerReport(run)+ self.assertEqual(run.returncode, 1, run.stderr[-4000:])+ self.assertIn("e_score_correction_bias", run.stderr)+ self.assertIn("refusing", run.stderr)+++if __name__ == "__main__":+ unittest.main()