Artifact 7b4f32bc89d169c1537be58a18a341572869abc46aff3be032e55cf4b78f802a:
- File
test/fuzz/fuzz_chat.c
— part of check-in
[55cff08c2c]
at
2026-11-19 00:56:53
on branch stage-4
— Add the Chat Completions adapter for OpenAI, OpenRouter and compatibles
A chunk decoder following Pi: text, reasoning from reasoning_content, reasoning or reasoning_text kept under its field, OpenRouter reasoning_details verbatim, tool calls by index, usage, finish_reason, and in-stream errors. Requests follow Pi too: system or developer role, tools as functions, tool messages, stream_options, store and reasoning by dialect. The retry loop and header assembly move into a shared module both adapters use. Recorded streams, a fuzz target and fake server cases cover it, including a conversation moving between the two APIs in both directions. (user: dmestas, size: 2880) [annotate] [blame] [check-ins using] [more...]
/* libFuzzer target: SSE framer plus Chat Completions decoder. * Properties checked on every input: * - no crash, leak or undefined behaviour (sanitizers); * - the outcome and both forms of the message do not depend on chunking; * - a decoded message's native form is a JSON object and its neutral form * a JSON array. */ #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "openai_chat.h" #include "sse.h" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); static sqlite3 *db; static void require_json(const char *sql, const char *value) { sqlite3_stmt *st; if (sqlite3_prepare_v2(db, sql, -1, &st, NULL) != SQLITE_OK) { abort(); } sqlite3_bind_text(st, 1, value, -1, SQLITE_STATIC); if (sqlite3_step(st) != SQLITE_ROW || sqlite3_column_int(st, 0) != 1) { fprintf(stderr, "bad decoded JSON: %s\n", value); abort(); } sqlite3_finalize(st); } static char *decode(const char *bytes, size_t n, size_t chunk) { pic_chat c; pic_sse p; char *native = NULL, *neutral = NULL, *out; size_t cap; int rc = 0; if (pic_chat_init(&c, db) != 0 || pic_sse_init(&p, 4096, 1u << 16, pic_chat_event, &c) != 0) { abort(); } for (size_t i = 0; i < n && rc == 0; i += chunk) { rc = pic_sse_feed(&p, bytes + i, n - i < chunk ? n - i : chunk); } if (rc == 0) { rc = pic_sse_pending(&p) ? -1 : pic_chat_finish(&c); } if (rc == 0) { native = pic_chat_native(&c); neutral = pic_chat_neutral(&c); if (!native || !neutral) { abort(); } require_json("SELECT json_valid(?1) AND json_type(?1) = 'object'", native); require_json("SELECT json_valid(?1) AND json_type(?1) = 'array'", neutral); } cap = 64 + strlen(c.err) + (native ? strlen(native) : 0) + (neutral ? strlen(neutral) : 0); out = malloc(cap); if (!out) { abort(); } (void)snprintf(out, cap, "%d|%s|%s|%s|%s", rc, rc == 0 ? "" : c.err, native ? native : "", neutral ? neutral : "", rc == 0 ? pic_chat_stop(&c) : ""); free(native); free(neutral); pic_sse_free(&p); pic_chat_free(&c); return out; } int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { char *whole, *split; if (!db && sqlite3_open(":memory:", &db) != SQLITE_OK) { abort(); } if (size == 0) { return 0; } /* The first byte picks a chunk size; the rest is the stream. */ whole = decode((const char *)data + 1, size - 1, size); split = decode((const char *)data + 1, size - 1, (size_t)(data[0] % 16) + 1); if (strcmp(whole, split) != 0) { fprintf(stderr, "chunking changed the result\nwhole: %s\nsplit: %s\n", whole, split); abort(); } free(whole); free(split); return 0; }