Skip to content
suit Artifact [77d8723bec]
Login

Artifact [77d8723bec]

Artifact 77d8723bec93de5338375d2d077e46c37f228d8513624cff62e439e5ab6136d7:


// 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 = 65536;

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;
}