fuzz: support running single test at a time
Support testing single input file at a time.
Signed-off-by: Dmitry Baryshkov <dbaryshkov@gmail.com>
diff --git a/fuzz/main.c b/fuzz/main.c
index 1521056..96a38cf 100644
--- a/fuzz/main.c
+++ b/fuzz/main.c
@@ -39,6 +39,41 @@
#include <dirent.h>
+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 void test_all_from(const char *dirname)
{
DIR *dirp;
@@ -103,6 +138,9 @@
}
}
+ if (argc > 1)
+ return test_single_file(argv[1]);
+
const char *target = strrchr(argv[0], '/');
target = target ? target + 1 : argv[0];