diff --git a/Makefile b/Makefile index 34a0c95..8c35f18 100644 --- a/Makefile +++ b/Makefile @@ -47,7 +47,7 @@ PLUGIN = plugins/clock/clock.so plugins/lcm/lcm.so plugins/cps/cps.so \ plugins/cache/cache.so TESTS = tests/test_buf tests/test_tool tests/test_session tests/test_conv \ tests/test_transport tests/test_agent tests/test_openai \ - tests/test_theme tests/test_plugin tests/test_skills tests/test_telegram tests/test_delegate tests/test_builtin tests/test_ascii tests/test_ack tests/test_i18n tests/test_config tests/test_focus tests/test_sse tests/test_anthropic tests/test_cli tests/test_compactor tests/test_secrets tests/test_mcpclient tests/test_kids tests/test_lifecycle tests/test_md tests/test_tgmd tests/test_providers tests/test_b64 tests/test_pause tests/test_swarm tests/test_provstate tests/test_usage tests/test_agents tests/test_confblock tests/test_jobs tests/test_engine tests/test_self + tests/test_theme tests/test_plugin tests/test_skills tests/test_telegram tests/test_delegate tests/test_builtin tests/test_ascii tests/test_ack tests/test_i18n tests/test_config tests/test_focus tests/test_sse tests/test_anthropic tests/test_cli tests/test_compactor tests/test_secrets tests/test_mcpclient tests/test_kids tests/test_lifecycle tests/test_md tests/test_tgmd tests/test_providers tests/test_b64 tests/test_pause tests/test_swarm tests/test_provstate tests/test_usage tests/test_agents tests/test_confblock tests/test_jobs tests/test_engine tests/test_self tests/test_fuzz RPATH = -Wl,-rpath,'$$ORIGIN' PLUGIN_RPATH = -Wl,-rpath,'$$ORIGIN/../..' @@ -257,6 +257,10 @@ tests/test_junction: tests/test_junction.c $(LIB) plugins/junction/junction.so $(CC) $(CFLAGS) -o $@ tests/test_junction.c -L. -lsplinter $(TEST_RPATH) $(LIBS) tests/test_self: tests/test_self.c $(LIB) plugins/self/self.so $(CC) $(CFLAGS) -o $@ tests/test_self.c -L. -lsplinter $(TEST_RPATH) $(LIBS) + +tests/test_fuzz: tests/test_fuzz.c $(LIB) + $(CC) $(CFLAGS) -o $@ tests/test_fuzz.c -L. -lsplinter $(TEST_RPATH) $(LIBS) + tests/test_usage: tests/test_usage.c $(LIB) $(CC) $(CFLAGS) -o $@ tests/test_usage.c -L. -lsplinter $(TEST_RPATH) $(LIBS) tests/test_jobs: tests/test_jobs.c $(LIB) $(PLUGIN) diff --git a/tests/test_fuzz.c b/tests/test_fuzz.c new file mode 100644 index 0000000..6d928c9 --- /dev/null +++ b/tests/test_fuzz.c @@ -0,0 +1,191 @@ +/* + * 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); +} + +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); + + 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; +}