diff options
author | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2020-01-24 22:53:50 +0100 |
---|---|---|
committer | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2020-01-25 08:51:08 +0100 |
commit | c5417511c3dfd8d048ff84f6cf36d953949471f7 (patch) | |
tree | 27a448f2df9b5a295d424f1f62d3842bf6c2944b /fuzz | |
parent | 920805c9ffeede539f061287e75027d0f6f0151a (diff) | |
download | gnutls-c5417511c3dfd8d048ff84f6cf36d953949471f7.tar.gz |
fuzzers: when provided with a parameter they will run on a single file
Signed-off-by: Nikos Mavrogiannopoulos <nmav@gnutls.org>
Diffstat (limited to 'fuzz')
-rw-r--r-- | fuzz/main.c | 64 |
1 files changed, 39 insertions, 25 deletions
diff --git a/fuzz/main.c b/fuzz/main.c index cdeba17238..b2223d0fc7 100644 --- a/fuzz/main.c +++ b/fuzz/main.c @@ -43,6 +43,41 @@ # define SLASH '/' #endif +static int test_single_file(const char *fname) +{ + int fd, ret; + struct stat st; + uint8_t *data; + ssize_t n; + + if ((fd = open(fname, O_RDONLY)) == -1) { + fprintf(stderr, "Failed to open %s (%d)\n", fname, errno); + return -1; + } + + if (fstat(fd, &st) != 0) { + fprintf(stderr, "Failed to stat %d (%d)\n", fd, errno); + close(fd); + return -1; + } + + data = malloc(st.st_size); + if ((n = read(fd, data, st.st_size)) == st.st_size) { + printf("testing %llu bytes from '%s'\n", (unsigned long long) st.st_size, fname); + fflush(stdout); + LLVMFuzzerTestOneInput(data, st.st_size); + fflush(stderr); + ret = 0; + } else { + fprintf(stderr, "Failed to read %llu bytes from %s (%d), got %zd\n", (unsigned long long) st.st_size, fname, errno, n); + ret = -1; + } + + free(data); + close(fd); + return ret; +} + static int test_all_from(const char *dirname) { DIR *dirp; @@ -55,31 +90,8 @@ static int test_all_from(const char *dirname) char fname[strlen(dirname) + strlen(dp->d_name) + 2]; snprintf(fname, sizeof(fname), "%s/%s", dirname, dp->d_name); - int fd; - if ((fd = open(fname, O_RDONLY)) == -1) { - fprintf(stderr, "Failed to open %s (%d)\n", fname, errno); - continue; - } - - struct stat st; - if (fstat(fd, &st) != 0) { - fprintf(stderr, "Failed to stat %d (%d)\n", fd, errno); - close(fd); + if (test_single_file(fname) < 0) continue; - } - - uint8_t *data = malloc(st.st_size); - ssize_t n; - if ((n = read(fd, data, st.st_size)) == st.st_size) { - printf("testing %llu bytes from '%s'\n", (unsigned long long) st.st_size, fname); - fflush(stdout); - LLVMFuzzerTestOneInput(data, st.st_size); - fflush(stderr); - } else - fprintf(stderr, "Failed to read %llu bytes from %s (%d), got %zd\n", (unsigned long long) st.st_size, fname, errno, n); - - free(data); - close(fd); } closedir(dirp); return 0; @@ -108,7 +120,9 @@ int main(int argc, char **argv) target_len -= 4; // ignore .exe #endif - { + if (argc > 1) { /* test a single file */ + test_single_file(argv[1]); + } else { /* test the target directory */ int rc; char corporadir[sizeof(SRCDIR) + 1 + target_len + 8]; snprintf(corporadir, sizeof(corporadir), SRCDIR "/%.*s.in", (int) target_len, target); |