Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
fix(qwen36): refuse loudly on a failed encode-buffer realloc
Roman Dolgovd54804268a29
diff --git a/c/Makefile b/c/Makefileindex 6cfa5cf7..a523e42b 100644--- a/c/Makefile+++ b/c/Makefile@@ -1155,6 +1155,10 @@ tests/test_qwen36_tok_merges$(EXE): tests/test_qwen36_tok_merges.c qwen36.c expetests/test_qwen36_tok_truncated$(EXE): tests/test_qwen36_tok_truncated.c qwen36.c expert_ffn.h qwen36_tier.h st.h json.h compat.h $(QWEN36_TIER_SRC) $(CUDA_OBJ)$(CC) $(QWEN36_CFLAGS) $< $(QWEN36_TIER_SRC) $(CUDA_OBJ) -o $@ $(QWEN36_LDFLAGS)+# push_id / bpe_piece must refuse loudly, not crash, on a failed realloc during growth.+tests/test_qwen36_encode_oom$(EXE): tests/test_qwen36_encode_oom.c qwen36.c expert_ffn.h qwen36_tier.h st.h json.h compat.h $(QWEN36_TIER_SRC) $(CUDA_OBJ)+ $(CC) $(QWEN36_CFLAGS) $< $(QWEN36_TIER_SRC) $(CUDA_OBJ) -o $@ $(QWEN36_LDFLAGS)+# Reproducible local timing evidence; intentionally not a noisy CI perf gate.tests/bench_qwen36_dense_batch$(EXE): tests/bench_qwen36_dense_batch.c qwen36.c qwen36_tier.h st.h json.h compat.h $(QWEN36_TIER_SRC) $(CUDA_OBJ)$(CC) $(QWEN36_CFLAGS) $< $(QWEN36_TIER_SRC) $(CUDA_OBJ) -o $@ $(QWEN36_LDFLAGS)diff --git a/c/qwen36.c b/c/qwen36.cindex 3a4eb071..d1baa099 100644--- a/c/qwen36.c+++ b/c/qwen36.c@@ -186,7 +186,15 @@ static void build_byte_sym(void){g_unmap[cp]=(short)b; /* reverse: mapped codepoint -> original byte */}}-static void push_id(int **ids,int *n,int *cap,int v){ if(*n==*cap){*cap*=2; *ids=realloc(*ids,*cap*sizeof(int));} (*ids)[(*n)++]=v; }+static void push_id(int **ids,int *n,int *cap,int v){+ if(*n==*cap){+ *cap*=2;+ int *tmp=realloc(*ids,*cap*sizeof(int));+ if(!tmp){ fprintf(stderr,"qwen36: OOM reallocating token id buffer (%d entries)\n",*cap); exit(1); }+ *ids=tmp;+ }+ (*ids)[(*n)++]=v;+}static int try_special(const char *s,int i,int n,int *id_out){int best_len=0,best_id=-1;@@ -234,7 +242,13 @@ static void bpe_piece(const char *piece,int len,int **ids,int *n,int *cap){for(int b=0;b<len;b++){const char *sym=byte_sym_utf8[(unsigned char)piece[b]];int sl=(int)strlen(sym); char *d=malloc(sl+1); memcpy(d,sym,sl); d[sl]=0;- if(sc==scap){scap*=2; syms=realloc(syms,scap*sizeof(char*));} syms[sc++]=d;+ if(sc==scap){+ scap*=2;+ char **tmp=realloc(syms,scap*sizeof(char*));+ if(!tmp){ fprintf(stderr,"qwen36: OOM reallocating BPE symbol buffer (%d entries)\n",scap); exit(1); }+ syms=tmp;+ }+ syms[sc++]=d;}while(sc>1){int best=-1,besti=-1;diff --git a/c/tests/test_qwen36_encode_oom.c b/c/tests/test_qwen36_encode_oom.cnew file mode 100644index 00000000..1849bd67--- /dev/null+++ b/c/tests/test_qwen36_encode_oom.c@@ -0,0 +1,147 @@+/* push_id and bpe_piece's growth reallocs were unchecked: a failed realloc left the+ * buffer NULL and the very next store wrote through it. Injects a real allocation+ * failure (not just a code read) and asserts the engine refuses loudly instead of+ * crashing on a NULL write, mirroring tests/test_798_guards.c's shadow-realloc+ * technique -- qwen38.c's own push_id/bpe_piece already guard this exact pattern+ * (q38_encode_realloc / q38_encode_oom); qwen36.c never got the equivalent fix. */+#ifndef _GNU_SOURCE+#define _GNU_SOURCE+#endif+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#ifndef _WIN32+#include <unistd.h>+#include <sys/wait.h>+#endif++#ifndef _WIN32+/* Forward-declare the shadow under its own name before the macro below hijacks every+ * "realloc" token that follows, mirroring tests/test_798_guards.c's technique. */+static void *test_realloc_seam(void *p, size_t n);+#define realloc test_realloc_seam+#endif++#define main qwen36_main_unused+#include "../qwen36.c"+#undef main++#ifndef _WIN32+#undef realloc++static long g_realloc_n = 0, g_realloc_fail_at = -1;++static void *test_realloc_seam(void *p, size_t n){+ g_realloc_n++;+ if (g_realloc_n == g_realloc_fail_at) return NULL;+ return realloc(p, n);+}+static void reset_seam(void){ g_realloc_n = 0; g_realloc_fail_at = -1; }+#endif++static int g_nfails = 0;+static void check(int cond, const char *what){+ if (!cond) { printf("FAIL: %s\n", what); g_nfails++; }+}++#ifndef _WIN32+/* fork/pipe/waitpid harness, mirrors tests/test_798_guards.c's inline idiom */+static void run_forked(void (*fn)(void), int *exit_code, char *errbuf, size_t errbuf_sz) {+ int pipefd[2];+ if (pipe(pipefd) != 0) { *exit_code = -1; errbuf[0] = 0; return; }+ pid_t pid = fork();+ if (pid < 0) { *exit_code = -1; errbuf[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 -- a bug, not a crash */+ }+ close(pipefd[1]);+ size_t off = 0; ssize_t n;+ while (off < errbuf_sz - 1 && (n = read(pipefd[0], errbuf + off, errbuf_sz - 1 - off)) > 0) off += (size_t)n;+ errbuf[off] = 0;+ close(pipefd[0]);+ int status = 0; waitpid(pid, &status, 0);+ *exit_code = WIFEXITED(status) ? WEXITSTATUS(status) : -1;+}++/* push_id: cap starts at 2, so the 3rd push (n==cap) is the realloc growth call --+ * the first and only realloc in a fresh child, so ordinal 1 targets it precisely. */+static void fn_push_id_grow(void) {+ int *ids = malloc(2 * sizeof(int));+ int n = 0, cap = 2;+ push_id(&ids, &n, &cap, 10);+ push_id(&ids, &n, &cap, 20);+ push_id(&ids, &n, &cap, 30); /* n==cap(2) here: triggers the realloc */+ free(ids);+}++/* bpe_piece: scap starts at 16, so a 17-byte piece triggers exactly one realloc (the+ * byte-symbol table growth) before the merge loop is ever reached -- an empty+ * (never-loaded) merge table is never consulted on the failing path. */+static void fn_bpe_piece_grow(void) {+ build_byte_sym();+ int cap = 16, n = 0; int *ids = malloc((size_t)cap * sizeof(int));+ const char *piece = "abcdefghijklmnopq"; /* 17 bytes */+ bpe_piece(piece, (int)strlen(piece), &ids, &n, &cap);+ free(ids);+}++static void test_push_id_oom(void) {+ reset_seam();+ g_realloc_fail_at = 1;+ int exit_code; char err[512];+ run_forked(fn_push_id_grow, &exit_code, err, sizeof(err));+ check(exit_code == 1, "push_id: exits(1) on injected realloc failure");+ check(strstr(err, "OOM reallocating token id buffer") != NULL,+ "push_id: message names the buffer");+ reset_seam();+}++static void test_bpe_piece_oom(void) {+ reset_seam();+ g_realloc_fail_at = 1;+ int exit_code; char err[512];+ run_forked(fn_bpe_piece_grow, &exit_code, err, sizeof(err));+ check(exit_code == 1, "bpe_piece: exits(1) on injected realloc failure");+ check(strstr(err, "OOM reallocating BPE symbol buffer") != NULL,+ "bpe_piece: message names the buffer");+ reset_seam();+}+#endif++/* ---- controls: same growth paths, no injection -- must succeed normally ---- */+static void test_push_id_control(void) {+ int *ids = malloc(2 * sizeof(int));+ int n = 0, cap = 2;+ push_id(&ids, &n, &cap, 10);+ push_id(&ids, &n, &cap, 20);+ push_id(&ids, &n, &cap, 30);+ check(n == 3 && cap >= 3 && ids[0] == 10 && ids[1] == 20 && ids[2] == 30,+ "push_id control: grows and stores correctly without injection");+ free(ids);+}++static void test_bpe_piece_control(void) {+ build_byte_sym();+ int cap = 16, n = 0; int *ids = malloc((size_t)cap * sizeof(int));+ const char *piece = "abcdefghijklmnopq";+ bpe_piece(piece, (int)strlen(piece), &ids, &n, &cap);+ check(n == 17, "bpe_piece control: one id per byte with no merge table loaded");+ free(ids);+}++int main(void) {+#ifndef _WIN32+ test_push_id_oom();+ test_bpe_piece_oom();+#else+ printf("qwen36 encode allocation-failure injection: skipped on Windows (no fork)\n");+#endif+ test_push_id_control();+ test_bpe_piece_control();++ if (g_nfails) { printf("%d check(s) failed\n", g_nfails); return 1; }+ printf("OK\n");+ return 0;+}