summaryrefslogtreecommitdiff
path: root/test/fuzz/standalone_fuzz_target_runner.c
blob: a291b488234090bd50cc976e53c607d82700a067 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

extern int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size);

int main(int argc, char **argv) {
    int i;
    fprintf(stderr, "StandaloneFuzzTargetMain: running %d inputs\n", argc - 1);

    for (i = 1; i < argc; i++) {
        size_t len, n_read, err;
        unsigned char *buf;
        FILE *f = fopen(argv[i], "rb+");
        if (!f) {
            /* Failed to open this file: it may be a directory. */
            fprintf(stderr, "Skipping: %s\n", argv[i]);
            continue;
        }
        fprintf(stderr, "Running: %s %s\n", argv[0], argv[i]);
        fseek(f, 0, SEEK_END);
        len = ftell(f);
        fseek(f, 0, SEEK_SET);
        buf = (unsigned char *)malloc(len);
        n_read = fread(buf, 1, len, f);
        assert(n_read == len);
        LLVMFuzzerTestOneInput(buf, len);
        free(buf);
        err = fclose(f);
        assert(err == 0);
        (void)err;
        fprintf(stderr, "Done:    %s: (%d bytes)\n", argv[i], (int)n_read);
    }

    return 0;
}