Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
security: extend the audit — olmoe loader, tokenizer, server slowloris
KingIcyCreamProjectsfb45de281275
diff --git a/c/olmoe.c b/c/olmoe.cindex cda5913d..84d584e8 100644--- a/c/olmoe.c+++ b/c/olmoe.c@@ -157,20 +157,35 @@ static void softmax_row(float *x, int n) {}/* ---------- caricamento ---------- */+/* config.json arrives from an untrusted mirror: a missing key was a NULL-deref+ * (json_get(...)->num), so require each dimension present + numeric. */+static double req_num(jval *r, const char *k){+ jval *v=json_get(r,k);+ if(!v||v->t!=J_NUM){ fprintf(stderr,"config.json: missing or non-numeric \"%s\"\n",k); exit(1); }+ return v->num;+}static void load_cfg(Cfg *c, const char *snap) {char path[2048]; snprintf(path, sizeof(path), "%s/config.json", snap);FILE *f = fopen(path, "rb"); if(!f){perror(path);exit(1);}fseek(f,0,SEEK_END); long n=ftell(f); fseek(f,0,SEEK_SET);- char *buf = malloc(n+1); if(fread(buf,1,n,f)!=(size_t)n){} buf[n]=0; fclose(f);+ if(n<0 || n>(256L<<20)){ fprintf(stderr,"%s: config.json missing or larger than 256 MB\n",path); exit(1); } /* SEC-9 */+ char *buf = malloc((size_t)n+1); if(!buf){ fprintf(stderr,"OOM reading %s\n",path); exit(1); }+ if(fread(buf,1,(size_t)n,f)!=(size_t)n){ fprintf(stderr,"%s: short read\n",path); exit(1); } buf[n]=0; fclose(f);char *arena=NULL; jval *r = json_parse(buf, &arena);- c->hidden = (int)json_get(r,"hidden_size")->num;- c->n_layers = (int)json_get(r,"num_hidden_layers")->num;- c->n_heads = (int)json_get(r,"num_attention_heads")->num;- c->n_kv_heads= (int)json_get(r,"num_key_value_heads")->num;- c->n_experts = (int)json_get(r,"num_experts")->num;- c->topk = (int)json_get(r,"num_experts_per_tok")->num;- c->inter = (int)json_get(r,"intermediate_size")->num;- c->vocab = (int)json_get(r,"vocab_size")->num;+ c->hidden = (int)req_num(r,"hidden_size");+ c->n_layers = (int)req_num(r,"num_hidden_layers");+ c->n_heads = (int)req_num(r,"num_attention_heads");+ c->n_kv_heads= (int)req_num(r,"num_key_value_heads");+ c->n_experts = (int)req_num(r,"num_experts");+ c->topk = (int)req_num(r,"num_experts_per_tok");+ c->inter = (int)req_num(r,"intermediate_size");+ c->vocab = (int)req_num(r,"vocab_size");+ /* range-check so bad dims can't drive a later malloc(inter*hidden) / div-by-zero */+ if(c->hidden<1||c->hidden>(1<<20) || c->n_heads<1||c->n_heads>(1<<16) ||+ c->inter<1||c->inter>(1<<24) || c->vocab<1||c->vocab>(1<<24) ||+ c->n_layers<1||c->n_layers>4096 || c->n_experts<1||c->n_experts>(1<<20) ||+ c->n_kv_heads<1 || c->topk<1||c->topk>c->n_experts){+ fprintf(stderr,"config.json: dimension out of range\n"); exit(1); }c->head_dim = c->hidden / c->n_heads;jval *th = json_get(r,"rope_theta"); c->theta = th ? (float)th->num : 10000.f;jval *ep = json_get(r,"rms_norm_eps"); c->eps = ep ? (float)ep->num : 1e-5f;@@ -222,6 +237,20 @@ static void load_expert_w(Model *m, const char *name, int8_t *q, float *scale, ist_tensor *t = st_find(&m->S, name);if (t && t->dtype == 3) { /* I8/U8: container colibri */char qs[300]; snprintf(qs, sizeof(qs), "%s.qs", name);+ /* SEC-1 (olmoe): st_init skips its numel==nbytes/esz cross-check for dtype-3+ * (U8/I8) tensors, so a crafted header could declare nbytes >> the config-sized+ * destination and st_read_raw would write past q[O*I]; likewise an oversized .qs+ * would overflow scale[O]. q is malloc(O*I) (int8 = 1 byte/elem) and scale is+ * falloc(O), so require the raw span == O*I bytes and the scale == O elems.+ * Reject, never repair — mirrors glm.c qt_check_fmt. */+ st_tensor *ts = st_find(&m->S, qs);+ int64_t want_w = (int64_t)O * I;+ if (t->nbytes != want_w) {+ fprintf(stderr, "%s: I8 weight bytes %lld != expected %lld [O=%d,I=%d]\n",+ name, (long long)t->nbytes, (long long)want_w, O, I); exit(1); }+ if (!ts || ts->numel != (int64_t)O) {+ fprintf(stderr, "%s: scale elems %lld != expected %d\n",+ qs, (long long)(ts ? ts->numel : -1), O); exit(1); }st_read_raw(&m->S, name, q, 1);st_read_f32(&m->S, qs, scale, 1);return;diff --git a/c/openai_server.py b/c/openai_server.pyindex 94095d63..b49db9b0 100644--- a/c/openai_server.py+++ b/c/openai_server.py@@ -700,6 +700,8 @@ class APIServer(ThreadingHTTPServer):class APIHandler(BaseHTTPRequestHandler):protocol_version = "HTTP/1.1"+ timeout = 30 # per-request socket timeout: a slowloris client that dribbles its+ # request line/body can't pin a worker thread (and a slot) foreverserver_version = "colibri"def log_message(self, fmt, *args):diff --git a/c/tok.h b/c/tok.hindex df8e59c2..e823e375 100644--- a/c/tok.h+++ b/c/tok.h@@ -114,7 +114,9 @@ static void tok_load(Tok *T, const char *path){* mirror non fidato: un id NEGATIVO indicizzerebbe id2str[id] SOTTO l'allocazione* (OOB write) e un added_token privo di "id"/"content" darebbe NULL-deref. */int maxid=0;- for(int i=0;i<vocab->len;i++){ int id=(int)vocab->kids[i]->num;+ for(int i=0;i<vocab->len;i++){+ if(vocab->kids[i]->t!=J_NUM){ fprintf(stderr,"tokenizer.json: non-numeric vocab id at %d\n",i); exit(1); }+ int id=(int)vocab->kids[i]->num;if(id<0){ fprintf(stderr,"tokenizer.json: negative vocab id %d\n",id); exit(1); }if(id>maxid)maxid=id; }if(added) for(int i=0;i<added->len;i++){@@ -123,10 +125,13 @@ static void tok_load(Tok *T, const char *path){int id=(int)ji->num;if(id<0){ fprintf(stderr,"tokenizer.json: negative added id %d\n",id); exit(1); }if(id>maxid)maxid=id; }+ /* an id near INT_MAX would overflow n_ids=maxid+1 (UB) and calloc multi-GB */+ if(maxid > (1<<21)){ fprintf(stderr,"tokenizer.json: implausible max vocab id %d\n",maxid); exit(1); }T->n_ids=maxid+1;T->id2str=calloc(T->n_ids,sizeof(char*));T->id_added=calloc(T->n_ids,sizeof(int));T->id_special=calloc(T->n_ids,sizeof(int));+ if(!T->id2str||!T->id_added||!T->id_special){ fprintf(stderr,"tokenizer.json: OOM sizing %d ids\n",T->n_ids); exit(1); }/* vocab: stringa -> id (capacita' potenza di 2, ~2-3x) */int vc=1; while(vc < vocab->len*2) vc<<=1;@@ -141,6 +146,9 @@ static void tok_load(Tok *T, const char *path){hm_init(&T->merges, mc);for(int i=0;i<merges->len;i++){jval *pr=merges->kids[i];+ if(!pr||pr->t!=J_ARR||pr->len<2||!pr->kids[0]||!pr->kids[1]||+ pr->kids[0]->t!=J_STR||pr->kids[1]->t!=J_STR){+ fprintf(stderr,"tokenizer.json: malformed merge entry %d\n",i); exit(1); }const char *l=pr->kids[0]->str, *r=pr->kids[1]->str;int ll=(int)strlen(l), rl=(int)strlen(r);char *key=malloc(ll+1+rl); memcpy(key,l,ll); key[ll]=0; memcpy(key+ll+1,r,rl);