Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
harden config, grammar and model-dir file handling
JustVuggf7b5b307d5c2
diff --git a/c/Makefile b/c/Makefileindex d883728c..c539ce46 100644--- a/c/Makefile+++ b/c/Makefile@@ -1912,6 +1912,9 @@ tests/test_tier$(EXE): tests/test_tier.ctests/test_grammar$(EXE): tests/test_grammar.c$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)+tests/test_own_file$(EXE): tests/test_own_file.c+ $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)+# RoPE inv_freq precompute must stay byte-identical to the old powf-per-position# path; drives the real rope_interleave (includes colibri.c) vs a reference.# -ffp-contract=off: the in-test reference is a DIFFERENT loop body than thediff --git a/c/colibri.c b/c/colibri.cindex 1b2b8978..d4212aaf 100644--- a/c/colibri.c+++ b/c/colibri.c@@ -1875,6 +1875,11 @@ static void load_cfg(Cfg *c, const char *snap){if(c->topk>c->n_experts){fprintf(stderr,"config: num_experts_per_tok=%d exceeds n_routed_experts=%d\n",c->topk,c->n_experts); exit(1); }+ /* the DSA indexer ropes the first qk_rope_head_dim floats of each of its rows, which+ * are index_head_dim long (GLM-5.2: 64 of 128) */+ if(c->index_hd>0 && c->index_hd<c->qk_rope){+ fprintf(stderr,"config: index_head_dim=%d is shorter than qk_rope_head_dim=%d, which the "+ "indexer ropes in each of its rows\n",c->index_hd,c->qk_rope); exit(1); }#undef CKRfree(ar);}diff --git a/c/deepseek_v4.c b/c/deepseek_v4.cindex dc3190c7..b2b09e9f 100644--- a/c/deepseek_v4.c+++ b/c/deepseek_v4.c@@ -14517,6 +14517,7 @@ static int v4_ckpt_slot_count(void) {#include <sys/stat.h>#define v4_ckpt_mkdir(p) mkdir((p), 0755)#endif+#include "own_file.h" /* the checkpoints sit in a downloaded model dir */static void v4_ckpt_slot_free(V4PrefixCkpt *slot);static char v4_ckpt_dir[1024];static uint32_t v4_ckpt_fingerprint;@@ -14536,6 +14537,13 @@ static void v4_ckpt_disk_init(ColiV4Session *session) {const char *model = coli_v4_engine_target_model_dir(session->engine);if (!model || !*model) { v4_ckpt_dir[0] = '-'; return; }snprintf(v4_ckpt_dir, sizeof(v4_ckpt_dir), "%s/.coli_ckpt", model);+ /* The engine makes this directory itself; one that is a link came with the+ * model, and the checkpoints would land wherever it points. */+ if (coli_own_is_link(v4_ckpt_dir)) {+ fprintf(stderr, "v4_ckpt: %s is a link, not the engine's directory: "+ "checkpoints stay in memory\n", v4_ckpt_dir);+ v4_ckpt_dir[0] = '-'; return;+ }const ColiDeepSeekV4Config *c = &session->config;uint32_t h = 2166136261u;int fields[] = {c->num_hidden_layers, c->hidden_size, c->head_dim,@@ -14560,10 +14568,10 @@ static void v4_ckpt_disk_write(int i) {char path[1200], tmp[1240];v4_ckpt_disk_path(path, sizeof(path), i);snprintf(tmp, sizeof(tmp), "%s.tmp", path);- FILE *f = fopen(tmp, "wb");+ FILE *f = coli_own_fopen(tmp, "wb");if (!f) {v4_ckpt_mkdir(v4_ckpt_dir);- f = fopen(tmp, "wb");+ f = coli_own_fopen(tmp, "wb");if (!f) return;}static const char magic[9] = "COLIV4CK";@@ -14589,7 +14597,7 @@ static void v4_ckpt_disk_load(void) {if (v4_ckpt_slots[i].ids) continue;char path[1200];v4_ckpt_disk_path(path, sizeof(path), i);- FILE *f = fopen(path, "rb");+ FILE *f = coli_own_fopen(path, "rb");if (!f) continue;char magic[8]; uint32_t fp = 0; int32_t head[4];V4PrefixCkpt *slot = &v4_ckpt_slots[i];diff --git a/c/deepseek_v41.c b/c/deepseek_v41.cindex 7d1a11bb..4368bd56 100644--- a/c/deepseek_v41.c+++ b/c/deepseek_v41.c@@ -224,6 +224,13 @@ static void cfg_load(Cfg *c, const char *snap) {if (c->n_layers < 1 || c->n_layers > V41_MAX_LAYERS) {fprintf(stderr, "[cfg] n_layers %d out of range\n", c->n_layers); exit(1); }+ /* The rope turns the last rope_head_dim floats of every attention row (head_dim long)+ * and of every index query and key (index_head_dim long): it must fit both, in pairs+ * (the release: 64 of 512 and of 128). */+ if (c->head_dim < 1 || c->rope_dim < 0 || (c->rope_dim & 1) || c->rope_dim > c->head_dim ||+ c->index_head_dim < 0 || (c->index_head_dim > 0 && c->rope_dim > c->index_head_dim)) {+ fprintf(stderr, "[cfg] rope_head_dim %d does not fit head_dim %d and index_head_dim %d\n",+ c->rope_dim, c->head_dim, c->index_head_dim); exit(1); }int ratios[V41_MAX_LAYERS] = {0};int nr = jints(t, "compress_ratios", ratios, V41_MAX_LAYERS);if (nr < c->n_layers) {diff --git a/c/grammar.h b/c/grammar.hindex be6ba6af..65512cb5 100644--- a/c/grammar.h+++ b/c/grammar.h@@ -40,6 +40,11 @@#define GR_MAX_RULES 1024#define GR_MAX_STACKS 64 /* ambiguita' massima seguita in parallelo */#define GR_MAX_DEPTH 64 /* profondita' massima di uno stack del PDA */+/* A frame keeps its alternate and its symbol in 16 bits (GrFrame): a rule with more+ * alternates, or an alternate with more symbols, is refused when the grammar is read,+ * not wrapped to a negative index when it is walked. */+#define GR_MAX_ALTS 32767+#define GR_MAX_SYMS 32767typedef struct { uint8_t bits[32]; } GrCls; /* insieme di byte ammessi */enum { GR_CLS = 0, GR_REF = 1 };@@ -57,6 +62,7 @@ typedef struct { Grammar *G; GrStack st[GR_MAX_STACKS]; int n; int alive; } GrStstatic int gr__alt_new(Grammar *G, int ri){GrRule *R=&G->r[ri];+ if(R->n>=GR_MAX_ALTS){ snprintf(G->err,sizeof G->err,"a rule has more than %d alternates",GR_MAX_ALTS); return -1; }if(R->n==R->cap){ int nc=R->cap?R->cap*2:4;GrAlt *na=(GrAlt*)realloc(R->a,(size_t)nc*sizeof(GrAlt)); if(!na) return -1;R->a=na; R->cap=nc; }@@ -65,6 +71,7 @@ static int gr__alt_new(Grammar *G, int ri){}static int gr__push(Grammar *G, int ri, int ai, const GrSym *sy){GrAlt *A=&G->r[ri].a[ai];+ if(A->n>=GR_MAX_SYMS){ snprintf(G->err,sizeof G->err,"an alternate has more than %d symbols",GR_MAX_SYMS); return -1; }if(A->n==A->cap){ int nc=A->cap?A->cap*2:8;GrSym *ns=(GrSym*)realloc(A->s,(size_t)nc*sizeof(GrSym)); if(!ns) return -1;A->s=ns; A->cap=nc; }@@ -127,7 +134,7 @@ static int gr__lit(Grammar *G, int ri, int ai, const char **pp){if(b<0){ snprintf(G->err,sizeof G->err,"invalid escape in literal"); return -1; } }else b=(unsigned char)*p++;GrSym s; memset(&s,0,sizeof s); s.t=GR_CLS; s.c.bits[b>>3]|=(uint8_t)(1u<<(b&7));- if(gr__push(G,ri,ai,&s)){ snprintf(G->err,sizeof G->err,"out of memory"); return -1; }+ if(gr__push(G,ri,ai,&s)){ if(!G->err[0]) snprintf(G->err,sizeof G->err,"out of memory"); return -1; }}if(*p!='"'){ snprintf(G->err,sizeof G->err,"unterminated literal"); return -1; }*pp=p+1; return 0;@@ -154,7 +161,7 @@ static int gr__cls(Grammar *G, int ri, int ai, const char **pp){if(*p!=']'){ snprintf(G->err,sizeof G->err,"unterminated character class"); return -1; }if(neg) for(int i=0;i<32;i++) s.c.bits[i]=(uint8_t)~s.c.bits[i];*pp=p+1;- if(gr__push(G,ri,ai,&s)){ snprintf(G->err,sizeof G->err,"out of memory"); return -1; }+ if(gr__push(G,ri,ai,&s)){ if(!G->err[0]) snprintf(G->err,sizeof G->err,"out of memory"); return -1; }return 0;}/* postfisso ? * + sull'ITEM appena letto (simboli [n0, n) dell'alternate corrente).@@ -179,14 +186,14 @@ static int gr__postfix(Grammar *G, int ri, int ai, int n0, char op){if(gr__push(G,ri,ai,&R)) goto full; /* l'item nell'alternate diventa R */return 0;full:- snprintf(G->err,sizeof G->err,"grammar is too large");+ if(!G->err[0]) snprintf(G->err,sizeof G->err,"grammar is too large");return -1;}static int gr__alts(Grammar *G, int ri, const char **pp, int depth, int in_group){if(depth>32){ snprintf(G->err,sizeof G->err,"groups are nested too deeply"); return -1; }const char *p=*pp;int ai=gr__alt_new(G,ri);- if(ai<0){ snprintf(G->err,sizeof G->err,"out of memory"); return -1; }+ if(ai<0){ if(!G->err[0]) snprintf(G->err,sizeof G->err,"out of memory"); return -1; }for(;;){p=gr__ws(p);if(!*p){@@ -200,7 +207,7 @@ static int gr__alts(Grammar *G, int ri, const char **pp, int depth, int in_groupif(*p=='|'){p++;ai=gr__alt_new(G,ri);- if(ai<0){ snprintf(G->err,sizeof G->err,"out of memory"); return -1; }+ if(ai<0){ if(!G->err[0]) snprintf(G->err,sizeof G->err,"out of memory"); return -1; }continue;}int n0=G->r[ri].a[ai].n;@@ -217,7 +224,7 @@ static int gr__alts(Grammar *G, int ri, const char **pp, int depth, int in_groupif(*p!=')'){ snprintf(G->err,sizeof G->err,"missing ')'"); return -1; }p++;GrSym s; memset(&s,0,sizeof s); s.t=GR_REF; s.ref=(int16_t)gi;- if(gr__push(G,ri,ai,&s)){ snprintf(G->err,sizeof G->err,"out of memory"); return -1; }+ if(gr__push(G,ri,ai,&s)){ if(!G->err[0]) snprintf(G->err,sizeof G->err,"out of memory"); return -1; }} else if(gr__idch(*p)){int nl=gr__idlen(p);const char *after=gr__ws(p+nl);@@ -226,7 +233,7 @@ static int gr__alts(Grammar *G, int ri, const char **pp, int depth, int in_groupif(ref<0){ snprintf(G->err,sizeof G->err,"too many rules"); return -1; }p+=nl;GrSym s; memset(&s,0,sizeof s); s.t=GR_REF; s.ref=(int16_t)ref;- if(gr__push(G,ri,ai,&s)){ snprintf(G->err,sizeof G->err,"out of memory"); return -1; }+ if(gr__push(G,ri,ai,&s)){ if(!G->err[0]) snprintf(G->err,sizeof G->err,"out of memory"); return -1; }} else {snprintf(G->err,sizeof G->err,"unexpected character '%c'",*p); return -1;}@@ -236,8 +243,8 @@ static int gr__alts(Grammar *G, int ri, const char **pp, int depth, int in_group*pp=p;return 0;}-/* parse del testo GBNF. 0 = ok; -1 = errore (messaggio in G->err). */-static int gr_parse(Grammar *G, const char *src){+static void gr_free(Grammar *G);+static int gr__parse(Grammar *G, const char *src){memset(G,0,sizeof *G); G->root=-1;const char *p=src;for(;;){@@ -261,6 +268,19 @@ static int gr_parse(Grammar *G, const char *src){if(G->root<0){ snprintf(G->err,sizeof G->err,"missing 'root' rule"); return -1; }return 0;}++/* parse del testo GBNF. 0 = ok; -1 = errore (messaggio in G->err), e quello che il parse+ * aveva costruito liberato: una grammatica rifiutata non resta in memoria (il motore ne+ * legge una per richiesta). */+static int gr_parse(Grammar *G, const char *src){+ if(!gr__parse(G,src)) return 0;+ char err[sizeof G->err];+ memcpy(err,G->err,sizeof err);+ gr_free(G);+ memcpy(G->err,err,sizeof err);+ return -1;+}+static void gr_free(Grammar *G){for(int i=0;i<G->n;i++){for(int a=0;a<G->r[i].n;a++) free(G->r[i].a[a].s);diff --git a/c/kimi_k3.c b/c/kimi_k3.cindex ccb1cc34..bc50ba86 100644--- a/c/kimi_k3.c+++ b/c/kimi_k3.c@@ -905,6 +905,11 @@ static void load_cfg(Cfg *c, const char *snap){if(v>=1&&v<=c->n_layers) c->idx_type[v-1]=1; }}}+ /* the DSA indexer ropes the first qk_rope_head_dim floats of each of its index_hd-long+ * rows (dsa_rope) */+ if(c->index_hd > 0 && c->index_hd < c->qk_rope){+ fprintf(stderr,"config.json: index_hd=%d is shorter than qk_rope_head_dim=%d, which the "+ "indexer ropes in each of its rows\n",c->index_hd,c->qk_rope); exit(1); }jval *b=json_get(root,"bos_token_id"); if(!b) b=json_get(tc,"bos_token_id");c->bos = b&&b->t==J_NUM ? (int)b->num : -1;jval *e=json_get(root,"eos_token_id"); if(!e) e=json_get(tc,"eos_token_id");diff --git a/c/kv_persist.h b/c/kv_persist.hindex 2ad5ffb7..e2137a6d 100644--- a/c/kv_persist.h+++ b/c/kv_persist.h@@ -5,6 +5,8 @@#ifndef KV_PERSIST_H#define KV_PERSIST_H+#include "own_file.h" /* the cache file may sit in a downloaded model dir */+static int g_kvsave=1;#define KV_MAGIC "COLIKV1\0" /* v1: righe Lc/Rc f32 */#define KV_MAGIC2 "COLIKV2\0" /* v2 (KV8): righe fp8 e4m3 + scala f32 per riga */@@ -31,19 +33,24 @@ static int64_t kv_rec_bytes(Model *m){static int kv_disk_open(Model *m){KVState *k=m->kv;if(k->disk_fp) return 1;- k->disk_fp=fopen(k->disk_path,"r+b");+ k->disk_fp=coli_own_fopen(k->disk_path,"r+b");if(k->disk_fp){ char mg[8]; /* formato del file != formato attivo -> riscrivi */if(fread(mg,1,8,k->disk_fp)!=8 || memcmp(mg,kv_active_magic(),8)){fclose(k->disk_fp); k->disk_fp=NULL; k->disk_nrec=0; }}if(!k->disk_fp){- k->disk_fp=fopen(k->disk_path,"wb");- if(!k->disk_fp) return 0;+ k->disk_fp=coli_own_fopen(k->disk_path,"wb");+ if(!k->disk_fp){+ static int said;+ if(!said++) fprintf(stderr,"[KV] cannot write %s (%s): conversations will not reopen warm\n",+ k->disk_path,strerror(errno));+ return 0;+ }int32_t h[8]; kv_hdr(m,h,0);fwrite(kv_active_magic(),1,8,k->disk_fp); fwrite(h,4,8,k->disk_fp);fflush(k->disk_fp);fclose(k->disk_fp);- k->disk_fp=fopen(k->disk_path,"r+b");+ k->disk_fp=coli_own_fopen(k->disk_path,"r+b");if(!k->disk_fp) return 0;}return 1;@@ -58,7 +65,7 @@ static void kv_disk_truncate(Model *m, int nrec){* record MAI scritti — il load successivo li leggerebbe come spazzatura. */if(nrec>k->disk_nrec) nrec=k->disk_nrec;if(k->disk_fp){ fclose(k->disk_fp); k->disk_fp=NULL; }- FILE *f=fopen(k->disk_path,"r+b");+ FILE *f=coli_own_fopen(k->disk_path,"r+b");if(!f){ k->disk_nrec=0; return; }k->disk_nrec=nrec;int32_t nr=nrec; fseek(f,8+6*4,SEEK_SET); fwrite(&nr,4,1,f);@@ -138,7 +145,7 @@ static int kv_disk_load(Model *m, int *hist, int maxctx){if(!g_kvsave) return 0;KVState *k=m->kv;Cfg *c=&m->c;- FILE *f=fopen(k->disk_path,"rb"); if(!f) return 0;+ FILE *f=coli_own_fopen(k->disk_path,"rb"); if(!f) return 0;char mg[8]; int32_t h[8], w[8]; kv_hdr(m,w,0);int dt=-1; /* dtype del FILE: 0=f32 (v1), 1=fp8 (v2), 2=PolarQuant (v3) */if(fread(mg,1,8,f)==8){diff --git a/c/olmoe.c b/c/olmoe.cindex cd3fb557..9da56403 100644--- a/c/olmoe.c+++ b/c/olmoe.c@@ -2454,7 +2454,7 @@ int main(int argc, char **argv) {snprintf(pinpath, sizeof(pinpath), "%s/hot_pinned.bin", snap);FILE *pinf_chk = fopen(pinpath, "rb");if (!pinf_chk) {- FILE *pinf_save = fopen(pinpath, "wb");+ FILE *pinf_save = coli_own_fopen(pinpath, "wb"); /* never through a planted link */if (pinf_save) {size_t expected_size = (size_t)m.c.n_layers * m.c.n_experts;fwrite(m.is_pinned, 1, expected_size, pinf_save);diff --git a/c/own_file.h b/c/own_file.hnew file mode 100644index 00000000..c27e52e5--- /dev/null+++ b/c/own_file.h@@ -0,0 +1,82 @@+/* own_file.h -- opening the files the engine keeps beside a model.+ *+ * The engines write their own state into the model's directory: the KV cache+ * (.coli_kv), the expert history (.coli_usage), prefix checkpoints (.coli_ckpt/),+ * pinning tables. That directory usually comes from a download, so anything at+ * those names may have been planted by whoever packed the model. A symlink+ * named .coli_kv made the engine write its KV cache, whose bytes follow the+ * weights, to wherever the link pointed.+ *+ * coli_own_fopen() opens such a file only when the name is a regular file of its+ * own or does not exist yet (then it creates one): it never follows a symlink in+ * the last component, and refuses a FIFO, a device or a directory. "w" modes+ * truncate after those checks, never before. The modes are fopen's: "rb", "r+b",+ * "wb", "w", "w+b". A refused name fails like a failed fopen (NULL, errno set:+ * ELOOP for a link, EINVAL for anything else that is not a regular file).+ *+ * coli_own_is_link() says whether a path's last component is a symlink (or, on+ * Windows, any reparse point), for directories the engine creates and writes in.+ *+ * Windows has no O_NOFOLLOW: there the name is checked for a reparse point just+ * before the open. Creating a symlink there needs a privilege or developer+ * mode, and archive tools leave them out unless asked. */+#ifndef OWN_FILE_H+#define OWN_FILE_H++#include <errno.h>+#include <stdio.h>+#include <string.h>++#ifdef _WIN32+#ifndef WIN32_LEAN_AND_MEAN+#define WIN32_LEAN_AND_MEAN+#endif+#include <windows.h>++static inline int coli_own_is_link(const char *path){+ DWORD a = GetFileAttributesA(path);+ return a != INVALID_FILE_ATTRIBUTES && (a & FILE_ATTRIBUTE_REPARSE_POINT);+}++static inline FILE *coli_own_fopen(const char *path, const char *mode){+ DWORD a = GetFileAttributesA(path);+ if(a != INVALID_FILE_ATTRIBUTES){+ if(a & FILE_ATTRIBUTE_REPARSE_POINT){ errno = ELOOP; return NULL; }+ if(a & FILE_ATTRIBUTE_DIRECTORY){ errno = EINVAL; return NULL; }+ }+ return fopen(path, mode);+}+#else+#include <fcntl.h>+#include <sys/stat.h>+#include <unistd.h>++static inline int coli_own_is_link(const char *path){+ struct stat st;+ return lstat(path, &st) == 0 && S_ISLNK(st.st_mode);+}++static inline FILE *coli_own_fopen(const char *path, const char *mode){+ int rw = strchr(mode, '+') != NULL, fl;+ if(mode[0] == 'r') fl = rw ? O_RDWR : O_RDONLY;+ else if(mode[0] == 'w') fl = (rw ? O_RDWR : O_WRONLY) | O_CREAT;+ else { errno = EINVAL; return NULL; }+#ifdef O_CLOEXEC+ fl |= O_CLOEXEC;+#endif+ /* O_NONBLOCK so a planted FIFO cannot hang the open; cleared once the+ * descriptor is known to be a regular file. */+ int fd = open(path, fl | O_NOFOLLOW | O_NONBLOCK, 0666);+ if(fd < 0) return NULL;+ struct stat st;+ if(fstat(fd, &st) != 0 || !S_ISREG(st.st_mode)){ close(fd); errno = EINVAL; return NULL; }+ int cur = fcntl(fd, F_GETFL);+ if(cur != -1) fcntl(fd, F_SETFL, cur & ~O_NONBLOCK);+ if(mode[0] == 'w' && ftruncate(fd, 0) != 0){ int e = errno; close(fd); errno = e; return NULL; }+ FILE *f = fdopen(fd, mode);+ if(!f){ int e = errno; close(fd); errno = e; }+ return f;+}+#endif++#endif /* OWN_FILE_H */diff --git a/c/route_trace.h b/c/route_trace.hindex 8e0efabd..3fc1406f 100644--- a/c/route_trace.h+++ b/c/route_trace.h@@ -55,6 +55,7 @@#include <string.h>#include <stdint.h>#include "compat.h" /* rename() -> MoveFileEx on Windows; see above */+#include "own_file.h" /* the history may sit in a downloaded model dir */#define RT_FORMAT_VERSION 1#define RT_IKU1_MAGIC 0x31554B49u /* "IKU1" — inkling.c's usage_save/pins_load */@@ -360,7 +361,10 @@ static int rt_save(const char *path, int quiet){}return 0;}- FILE *f = fopen(tmp, "w");+ /* Never through a link planted in the model dir. The temp name is ours, so whatever+ * else sits there is removed (the link, never its target) and the open retried. */+ FILE *f = coli_own_fopen(tmp, "w");+ if(!f && (errno == ELOOP || errno == EINVAL) && remove(tmp) == 0) f = coli_own_fopen(tmp, "w");if(!f){ if(!quiet) perror(tmp); return 0; }/* An all-zero history stays a ZERO-BYTE file, the way it was before this header* existed. PIN=auto decides whether a history is usable by testing the file SIZE anddiff --git a/c/tests/test_grammar.c b/c/tests/test_grammar.cindex 52073e74..9dd6bd78 100644--- a/c/tests/test_grammar.c+++ b/c/tests/test_grammar.c@@ -171,6 +171,36 @@ int main(void){CHECK(gr_admissible(&S,m,&end)==0 && end==1);gr_free(&G);+ /* A frame keeps its alternate and its symbol in 16 bits: one past 32767 of either+ * is refused when the grammar is read (it used to wrap to a negative index and read+ * before the alternates); at the limit the grammar still parses and walks. */+ {+ static char big[140000];+ int n = snprintf(big, sizeof big, "root ::= ");+ for (int i = 0; i < 32768; i++) big[n++] = '|';+ big[n] = 0;+ CHECK(gr_parse(&G, big) != 0 && strstr(G.err, "alternates"));+ gr_free(&G);+ n = snprintf(big, sizeof big, "root ::= ");+ for (int i = 0; i < 32768; i++) { big[n++] = '"'; big[n++] = 'a'; big[n++] = '"'; big[n++] = ' '; }+ big[n] = 0;+ CHECK(gr_parse(&G, big) != 0 && strstr(G.err, "symbols"));+ gr_free(&G);+ n = snprintf(big, sizeof big, "root ::= \"y\"");+ for (int i = 1; i < 32767; i++) { big[n++] = '|'; big[n++] = '"'; big[n++] = 'x'; big[n++] = '"'; }+ big[n] = 0;+ CHECK(gr_parse(&G, big) == 0);+ gr_state_init(&S, &G); /* past GR_MAX_STACKS the walker turns itself off */+ gr_free(&G);+ n = snprintf(big, sizeof big, "root ::= ");+ for (int i = 0; i < 32767; i++) { big[n++] = '"'; big[n++] = 'a'; big[n++] = '"'; big[n++] = ' '; }+ big[n] = 0;+ CHECK(gr_parse(&G, big) == 0);+ gr_state_init(&S, &G);+ CHECK(S.alive && feed(&S, "aaaa") == 4);+ gr_free(&G);+ }+puts("test_grammar: ok");return 0;}diff --git a/c/tests/test_own_file.c b/c/tests/test_own_file.cnew file mode 100644index 00000000..f02170e6--- /dev/null+++ b/c/tests/test_own_file.c@@ -0,0 +1,149 @@+/* own_file.h: the engine's files beside a model are opened only as regular files+ * of their own. A symlink planted under one of their names (a .coli_kv pointing+ * at a file elsewhere) must be refused without touching its target. */+#include <errno.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/stat.h>++#include "../own_file.h"++#ifdef _WIN32+#include <direct.h>+#include <process.h>+#define mkdir_(p) _mkdir(p)+#define pid_() _getpid()+#else+#include <unistd.h>+#define mkdir_(p) mkdir((p), 0700)+#define pid_() getpid()+#endif++#define CHECK(condition) do { \+ if (!(condition)) { \+ fprintf(stderr, "%s:%d: check failed: %s\n", __FILE__, __LINE__, #condition); \+ return 1; \+ } \+} while (0)++static char dir[512];++static const char *at(const char *name){+ static char path[4][600]; static int k;+ k = (k + 1) & 3;+ snprintf(path[k], sizeof path[k], "%s/%s", dir, name);+ return path[k];+}++static int put(const char *path, const char *text){+ FILE *f = fopen(path, "wb");+ if(!f) return -1;+ fputs(text, f);+ return fclose(f);+}++/* the file's bytes, or "" when it cannot be read */+static const char *get(const char *path){+ static char buf[256];+ FILE *f = fopen(path, "rb");+ buf[0] = 0;+ if(f){ size_t n = fread(buf, 1, sizeof buf - 1, f); buf[n] = 0; fclose(f); }+ return buf;+}++int main(void){+ const char *base = getenv("TMPDIR");+#ifdef _WIN32+ if(!base) base = getenv("TEMP");+#endif+ if(!base) base = "/tmp";+ snprintf(dir, sizeof dir, "%s/own_file_%ld", base, (long)pid_());+ CHECK(mkdir_(dir) == 0);+ FILE *f;++ /* a name that does not exist is created; "wb" truncates a file of its own */+ CHECK((f = coli_own_fopen(at(".coli_kv"), "wb")) != NULL);+ CHECK(fputs("first and longer", f) >= 0 && fclose(f) == 0);+ CHECK(!strcmp(get(at(".coli_kv")), "first and longer"));+ CHECK((f = coli_own_fopen(at(".coli_kv"), "wb")) != NULL);+ CHECK(fputs("second", f) >= 0 && fclose(f) == 0);+ CHECK(!strcmp(get(at(".coli_kv")), "second"));++ /* "r+b" keeps the bytes and writes in place; "rb" reads */+ CHECK((f = coli_own_fopen(at(".coli_kv"), "r+b")) != NULL);+ CHECK(fseek(f, 0, SEEK_SET) == 0 && fputc('S', f) == 'S' && fclose(f) == 0);+ CHECK(!strcmp(get(at(".coli_kv")), "Second"));+ CHECK((f = coli_own_fopen(at(".coli_kv"), "rb")) != NULL);+ char line[16] = {0};+ CHECK(fread(line, 1, sizeof line - 1, f) == 6 && !strcmp(line, "Second"));+ fclose(f);++ /* "w" (text) as the history uses it */+ CHECK((f = coli_own_fopen(at(".coli_usage.tmp"), "w")) != NULL);+ CHECK(fprintf(f, "0 1 2\n") > 0 && fclose(f) == 0);++ /* a missing file is not created by a read mode */+ errno = 0;+ CHECK(coli_own_fopen(at("absent"), "rb") == NULL && errno == ENOENT);+ CHECK(coli_own_fopen(at("absent"), "r+b") == NULL);+ CHECK(!coli_own_is_link(at("absent")));++ /* a directory under the name is refused in every mode */+ CHECK(mkdir_(at("sub")) == 0);+ CHECK(coli_own_fopen(at("sub"), "rb") == NULL);+ CHECK(coli_own_fopen(at("sub"), "wb") == NULL);+ CHECK(!coli_own_is_link(at("sub")));++ /* an unknown mode is refused */+ errno = 0;+ CHECK(coli_own_fopen(at(".coli_kv"), "ab") == NULL && errno == EINVAL);++#ifndef _WIN32+ /* a planted symlink is never followed: every mode fails, the target keeps its+ * bytes, and a dangling link creates nothing at its target */+ CHECK(put(at("victim"), "keep me") == 0);+ CHECK(symlink(at("victim"), at("link")) == 0);+ CHECK(coli_own_is_link(at("link")));+ const char *modes[] = {"rb", "r+b", "wb", "w", "w+b"};+ for(size_t i = 0; i < sizeof modes / sizeof *modes; i++){+ errno = 0;+ CHECK(coli_own_fopen(at("link"), modes[i]) == NULL && errno == ELOOP);+ }+ CHECK(!strcmp(get(at("victim")), "keep me"));+ CHECK(symlink(at("not-yet"), at("dangling")) == 0);+ CHECK(coli_own_fopen(at("dangling"), "wb") == NULL);+ struct stat st;+ CHECK(stat(at("not-yet"), &st) != 0 && errno == ENOENT);++ /* a symlinked directory is reported, and a link to a directory refused */+ CHECK(symlink(at("sub"), at("sublink")) == 0);+ CHECK(coli_own_is_link(at("sublink")));+ CHECK(coli_own_fopen(at("sublink"), "wb") == NULL);++ /* a FIFO under the name is refused at once, in a write mode too (no reader) */+ CHECK(mkfifo(at("fifo"), 0600) == 0);+ CHECK(coli_own_fopen(at("fifo"), "rb") == NULL && errno == EINVAL);+ CHECK(coli_own_fopen(at("fifo"), "wb") == NULL);+ CHECK(coli_own_fopen(at("fifo"), "r+b") == NULL);++ /* the descriptor is blocking again once accepted */+ CHECK((f = coli_own_fopen(at(".coli_kv"), "rb")) != NULL);+ {+ int fl = fcntl(fileno(f), F_GETFL);+ CHECK(fl != -1 && !(fl & O_NONBLOCK));+ }+ fclose(f);++ unlink(at("fifo")); unlink(at("sublink")); unlink(at("dangling"));+ unlink(at("link")); unlink(at("victim"));+#endif+ remove(at(".coli_usage.tmp")); remove(at(".coli_kv"));+#ifdef _WIN32+ _rmdir(at("sub")); _rmdir(dir);+#else+ rmdir(at("sub")); rmdir(dir);+#endif+ printf("own_file: OK\n");+ return 0;+}