/* * splinter -- deterministic fuzz over the parsers that read other people's text. * * Copyright (c) 2026 Konrad Lother * SPDX-License-Identifier: BSD-3-Clause * * The markdown renderer, its chunk cursor, the SSE reader and the JSON parser * all take text from a model or a provider. A fixed seed makes this a normal * test: the same inputs every run, no flakes, and ASAN turns the interesting * case (an out-of-bounds read) into a failure instead of a crash somewhere * else an hour later. * * The cursor carries one property beyond "does not crash": it must finish. A * piece that does not advance the position would loop forever in the Telegram * sender, so the loop has a hard cap that is generous but finite. */ #include #include #include #include "splinter.h" #include "mcp.h" #include "tgmd.h" static int failures; #define CHECK(cond) do { \ if (!(cond)) { \ fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, \ #cond); \ failures++; \ } \ } while (0) /* xorshift64*, fixed seed: reproducible on every machine */ static unsigned long long rng_state = 0x9e3779b97f4a7c15ULL; static unsigned long long rnd(void) { unsigned long long x = rng_state; x ^= x >> 12; x ^= x << 25; x ^= x >> 27; rng_state = x; return x * 0x2545f4914f6cdd1dULL; } /* * An alphabet that pushes on the parser's edges: the tags it knows, the * characters it escapes, attributes, partial tags, a NUL byte can not be * represented in a C string, so control characters stand in for the rest. */ static const char *const atoms[] = { "<", ">", "&", "\"", "'", "/", "=", "\n", "\t", " ", "a", "b", "", "", "", "", "", "", "", "", "
", "
", "
", "
", "", "&", "<", """, "&#x", "\x01", "\x7f", "\xc3\xa4", "```", "---", "| x | y |", "*", "_", "~", "\r\n", }; static void make_text(char *buf, size_t cap) { size_t at = 0; while (at + 8 < cap) { const char *a = atoms[rnd() % (sizeof(atoms) / sizeof(atoms[0]))]; size_t n = strlen(a); if (at + n + 1 >= cap) break; memcpy(buf + at, a, n); at += n; } buf[at] = '\0'; } /* every piece must fit the budget (tag overhead included) and the loop must end */ static void check_cursor(const char *html, size_t limit) { struct sp_tgmd_cursor c; struct sp_buf piece; size_t iter = 0, cap = strlen(html) * 2 + 1000; int rc; if (sp_buf_init(&piece) != 0) return; CHECK(sp_tgmd_cursor_init(&c, html) == 0); for (;;) { sp_buf_clear(&piece); rc = sp_tgmd_next(&c, limit, &piece); if (rc <= 0) break; CHECK(piece.len > 0); CHECK(piece.len <= limit + 64); /* the tag overhead is bounded */ if (++iter > cap) { CHECK(0 && "tgmd cursor did not finish"); break; } } sp_tgmd_cursor_free(&c); sp_buf_free(&piece); } /* * A parser that recurses without a limit is a crash reachable from the * network: the OpenAI gateway and the A2A server hand it a request body of up * to 4 MiB, and 200k nested arrays overflowed the stack and killed the * process. A real document nests four or five deep, so a limit far above that * costs nothing and takes the crash away. */ static void test_json_depth(void) { size_t n = 200000, i; char *deep = malloc(2 * n + 1); mcp_json *v = NULL; CHECK(deep != NULL); if (deep == NULL) return; for (i = 0; i < n; i++) deep[i] = '['; for (i = 0; i < n; i++) deep[n + i] = ']'; deep[2 * n] = '\0'; CHECK(mcp_parse(deep) == NULL); CHECK(mcp_parse_exact(deep) == NULL); free(deep); /* a document of an ordinary shape still parses */ v = mcp_parse("{\"a\":[1,{\"b\":[2,3]}],\"c\":\"d\"}"); CHECK(v != NULL); CHECK(v != NULL && mcp_arg_num(mcp_arg(v, "c"), NULL, -1) == -1); if (v != NULL) mcp_json_free(v); /* and one just inside the limit */ { char ok[160]; size_t k = 60; for (i = 0; i < k; i++) ok[i] = '['; for (i = 0; i < k; i++) ok[k + i] = ']'; ok[2 * k] = '\0'; v = mcp_parse(ok); CHECK(v != NULL); if (v != NULL) mcp_json_free(v); } } int main(void) { char md[512]; struct sp_buf out, err; unsigned i; CHECK(sp_buf_init(&out) == 0); CHECK(sp_buf_init(&err) == 0); test_json_depth(); for (i = 0; i < 4000; i++) { struct sp_sse *sse; make_text(md, sizeof(md)); /* the renderer is deterministic */ sp_buf_clear(&out); CHECK(sp_tgmd_render(md, &out) == 0); { struct sp_buf again; CHECK(sp_buf_init(&again) == 0); CHECK(sp_tgmd_render(md, &again) == 0); CHECK(again.len == out.len && (out.len == 0 || memcmp(again.p, out.p, out.len) == 0)); sp_buf_free(&again); } if (out.p != NULL && out.len > 0) check_cursor(out.p, 8 + (size_t)(rnd() % 200)); /* the SSE reader: split the same text into lines */ sse = sp_sse_new(); CHECK(sse != NULL); if (sse != NULL) { char *copy = strdup(md), *line, *save = NULL; CHECK(copy != NULL); if (copy != NULL) { for (line = strtok_r(copy, "\n", &save); line != NULL; line = strtok_r(NULL, "\n", &save)) { struct sp_buf delta; if (sp_buf_init(&delta) != 0) continue; sp_buf_clear(&err); (void)sp_sse_line(sse, line, &delta, &err); sp_buf_free(&delta); } free(copy); } { struct sp_reply rep; memset(&rep, 0, sizeof(rep)); sp_buf_clear(&err); (void)sp_sse_finish(sse, &rep, &err); sp_reply_free(&rep); } sp_sse_free(sse); } /* the JSON parser: random text must parse or fail, never crash */ { mcp_json *v = mcp_parse(md); if (v != NULL) mcp_json_free(v); v = mcp_parse_exact(md); if (v != NULL) mcp_json_free(v); } } sp_buf_free(&out); sp_buf_free(&err); if (failures == 0) printf("test_fuzz: ok (4000 inputs)\n"); return failures == 0 ? 0 : 1; }