Artifact 0efee131a7c4ab647c8048c0068f7c1ea21ad7a87729dffc8d2aecadf37f9bf1:
- File
test/replay.c
— part of check-in
[3f905be4f2]
at
2026-11-12 21:17:36
on branch trunk
— Every branch of suit's own code is tested, and the gates that keep it so
make cover fails unless every function, line, region and branch in src/ is run by a test. Every system call goes through a wrapper in platform.c that a test build can make fail, so failed-call branches are tested too. The release binary carries none of that code, and a test checks it.
Also: ensure() for invariants, a wider warning set as errors, the clang static analyzer, a second fuzz target for the stamp reader, a replay binary for saved fuzz inputs, a release build that the speed budget now measures, and make ci for the forge's Linux runner: the suite under gcc's sanitizers, then under a pinned Fil-C.
210 unit checks and 214 shell cases. The no-change run measured 0.56 to 0.60 ms over an empty program with the release build.
The Fil-C run and the Linux build have not been run yet. They run first on the forge's runner.
Decisions recorded: the wardrobe is two repositories, libfossil is used through its C API with suit's own file writer, and where each gate runs. (user: dmestas, size: 4193) [annotate] [blame] [check-ins using]
// Replays fuzz inputs through a target without libFuzzer, so the coverage // gate and the Fil-C run can use them. With --mutate N, also runs N // mutations of the inputs from a fixed seed. // // replay [--mutate N] FILE_OR_DIR... #include <dirent.h> #include <fcntl.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> #include <unistd.h> int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); constexpr size_t INPUT_MAX = 2u << 20; constexpr size_t SEEDS_MAX = 4096; static uint8_t *seeds[SEEDS_MAX]; static size_t seed_len[SEEDS_MAX]; static size_t nseeds; static int report = -1; // the real stdout 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 void add_file(const char *path) { FILE *f = fopen(path, "rb"); if (f == nullptr || nseeds == SEEDS_MAX) { dprintf(report, "replay: cannot use %s\n", path); exit(2); } uint8_t *buf = malloc(INPUT_MAX); size_t n = fread(buf, 1, INPUT_MAX, f); fclose(f); seeds[nseeds] = buf; seed_len[nseeds++] = n; } static void add_path(const char *path) { struct stat st; if (stat(path, &st) != 0) return; // a corpus folder that does not exist yet if (!S_ISDIR(st.st_mode)) { add_file(path); return; } DIR *d = opendir(path); struct dirent *de; while ((de = readdir(d)) != nullptr) { if (de->d_name[0] == '.') continue; char child[4096]; snprintf(child, sizeof child, "%s/%s", path, de->d_name); add_path(child); } closedir(d); } static size_t mutate(uint8_t *work, size_t len, size_t cap) { 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] ^= (uint8_t)(1u << (rng() % 8)); break; case 1: if (len > 0) work[at] = (uint8_t)rng(); break; case 2: if (len + 1 < cap) { memmove(work + at + 1, work + at, len - at); work[at] = (uint8_t)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 < cap) { // 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) { // Targets talk to stdout and stderr; keep a handle on the real stdout // for the summary and send both to /dev/null. report = dup(STDOUT_FILENO); int null = open("/dev/null", O_WRONLY); dup2(null, STDOUT_FILENO); dup2(null, STDERR_FILENO); long mutations = 0; int first = 1; if (argc > 2 && strcmp(argv[1], "--mutate") == 0) { mutations = atol(argv[2]); first = 3; } for (int i = first; i < argc; i++) add_path(argv[i]); if (nseeds == 0) { dprintf(report, "replay: no inputs\n"); return 2; } for (size_t i = 0; i < nseeds; i++) LLVMFuzzerTestOneInput(seeds[i], seed_len[i]); dprintf(report, "replay: %zu inputs", nseeds); if (mutations > 0) { uint8_t *work = malloc(64u << 10); for (long m = 0; m < mutations; m++) { size_t s = (size_t)(rng() % nseeds); size_t len = seed_len[s] < (32u << 10) ? seed_len[s] : (32u << 10); memcpy(work, seeds[s], len); len = mutate(work, len, 64u << 10); LLVMFuzzerTestOneInput(work, len); } dprintf(report, ", %ld mutations", mutations); free(work); } dprintf(report, "\n"); return 0; }