Artifact c6b31b0c7726bb100e3483c09e845ff21b6e51405fe35937986cc8c0b30bf4ae:
- File
test/fuzz.c
— part of check-in
[6d70dcc76a]
at
2026-11-12 19:36:41
on branch trunk
— Phase 1: the config parser, the no-change run and the speed bench
suit check parses suit.conf strictly and prints a canonical dump. Any unknown kind or key, duplicate, cycle or dangling name rejects the whole file with its line number.
suit run compares a stamp against the files it names and exits without writing, allocating or printing when nothing changed.
suit bench times suit run against an empty program built the same way and against /usr/bin/true, interleaved, and fails the build when the no-change run costs more than 1 ms over the empty program or 1 MiB more memory. It measured 0.65 to 0.94 ms over, quiet and under load. A system binary starts 0.6 to 1.5 ms faster than any built binary on macOS, so it is shown for information and is not the baseline.
94 unit checks, 100 shell cases and a libFuzzer target for the parser. (user: dmestas, size: 3874) [annotate] [blame] [check-ins using]
// Parser fuzz target. Built two ways: as a libFuzzer target, or with // -DMUTATE_MAIN as a fixed-seed mutation loop over the seed corpus. #include "conf.h" #include <assert.h> #include <stdio.h> #include <stdlib.h> static Conf conf; int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size); int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size) { static FILE *sink; if (sink == nullptr) { sink = fopen("/dev/null", "w"); assert(sink != nullptr); } ConfError e; if (conf_parse(&conf, (const char *)data, size, &e)) { assert(conf.nsections <= CONF_SECTION_MAX); assert(conf.nentries <= CONF_ENTRY_MAX); conf_dump(&conf, sink); } else { assert(e.msg[0] != '\0'); } return 0; } #ifdef MUTATE_MAIN #include <dirent.h> #include <string.h> constexpr size_t SEED_MAX = 64; constexpr size_t INPUT_MAX = 16384; static unsigned char seeds[SEED_MAX][INPUT_MAX]; static size_t seed_len[SEED_MAX]; static unsigned char work[INPUT_MAX]; static uint64_t rng_state = 0x9E3779B97F4A7C15u; static uint64_t rng(void) { rng_state ^= rng_state << 13; rng_state ^= rng_state >> 7; rng_state ^= rng_state << 17; return rng_state; } static size_t load_seeds(const char *dir) { DIR *d = opendir(dir); if (d == nullptr) { perror(dir); exit(2); } size_t n = 0; struct dirent *de; while ((de = readdir(d)) != nullptr && n < SEED_MAX) { if (de->d_name[0] == '.') continue; char path[1024]; snprintf(path, sizeof path, "%s/%s", dir, de->d_name); FILE *f = fopen(path, "rb"); if (f == nullptr) continue; seed_len[n] = fread(seeds[n], 1, INPUT_MAX, f); fclose(f); n++; } closedir(d); return n; } static size_t mutate(size_t len) { static const char dict[] = "[]#= \t\n\r*/.:@~-_xyz0123456789"; int rounds = 1 + (int)(rng() % 6); for (int r = 0; r < rounds; r++) { size_t at = len > 0 ? (size_t)(rng() % len) : 0; switch (rng() % 5) { case 0: if (len > 0) work[at] ^= (unsigned char)(1u << (rng() % 8)); break; case 1: if (len > 0) work[at] = (unsigned char)rng(); break; case 2: if (len < INPUT_MAX) { memmove(work + at + 1, work + at, len - at); work[at] = (unsigned char)dict[rng() % (sizeof dict - 1)]; len++; } break; case 3: if (len > 0) { memmove(work + at, work + at + 1, len - at - 1); len--; } break; default: if (len > 8 && len * 2 < INPUT_MAX) { // duplicate a slice size_t n = 1 + (size_t)(rng() % 64); if (at + n > len) n = len - at; memmove(work + at + n, work + at, len - at); len += n; } } } return len; } int main(int argc, char **argv) { if (argc != 3) { fprintf(stderr, "usage: mutate CORPUS_DIR ITERATIONS\n"); return 2; } size_t nseeds = load_seeds(argv[1]); long iterations = atol(argv[2]); if (nseeds == 0 || iterations < 1) { fprintf(stderr, "mutate: need seeds and a positive iteration count\n"); return 2; } long runs = 0; for (size_t s = 0; s < nseeds; s++) { // each seed unmodified first LLVMFuzzerTestOneInput(seeds[s], seed_len[s]); runs++; } for (; runs < iterations; runs++) { size_t s = (size_t)(rng() % nseeds); memcpy(work, seeds[s], seed_len[s]); size_t len = mutate(seed_len[s]); LLVMFuzzerTestOneInput(work, len); } printf("fuzz: %ld executions from %zu seeds, no failures\n", runs, nseeds); return 0; } #endif