2018-07-03 15:45:11 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
|
|
|
*/
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdint.h>
|
2018-07-03 15:45:11 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2020-03-09 16:17:26 +01:00
|
|
|
#include <sys/stat.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <unistd.h>
|
2018-07-03 15:45:11 +02:00
|
|
|
|
|
|
|
#include "fuzz.h"
|
|
|
|
|
|
|
|
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
static void
|
2020-02-14 08:14:03 +01:00
|
|
|
test_all_from(const char *dirname) {
|
|
|
|
DIR *dirp;
|
2018-07-03 15:45:11 +02:00
|
|
|
struct dirent *dp;
|
|
|
|
|
|
|
|
dirp = opendir(dirname);
|
|
|
|
if (dirp == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((dp = readdir(dirp)) != NULL) {
|
2020-02-14 08:14:03 +01:00
|
|
|
char filename[strlen(dirname) + strlen(dp->d_name) + 2];
|
|
|
|
int fd;
|
2018-07-03 15:45:11 +02:00
|
|
|
struct stat st;
|
2020-02-14 08:14:03 +01:00
|
|
|
char *data;
|
|
|
|
ssize_t n;
|
2018-07-03 15:45:11 +02:00
|
|
|
|
|
|
|
if (dp->d_name[0] == '.') {
|
|
|
|
continue;
|
|
|
|
}
|
2020-02-12 13:59:18 +01:00
|
|
|
snprintf(filename, sizeof(filename), "%s/%s", dirname,
|
|
|
|
dp->d_name);
|
2018-07-03 15:45:11 +02:00
|
|
|
|
|
|
|
if ((fd = open(filename, O_RDONLY)) == -1) {
|
|
|
|
fprintf(stderr, "Failed to open %s: %s\n", filename,
|
|
|
|
strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fstat(fd, &st) != 0) {
|
|
|
|
fprintf(stderr, "Failed to stat %s: %s\n", filename,
|
|
|
|
strerror(errno));
|
|
|
|
goto closefd;
|
|
|
|
}
|
|
|
|
|
|
|
|
data = malloc(st.st_size);
|
|
|
|
n = read(fd, data, st.st_size);
|
|
|
|
if (n == st.st_size) {
|
2020-02-12 13:59:18 +01:00
|
|
|
printf("testing %zd bytes from %s\n", n, filename);
|
2018-07-03 15:45:11 +02:00
|
|
|
fflush(stdout);
|
|
|
|
LLVMFuzzerTestOneInput((const uint8_t *)data, n);
|
|
|
|
fflush(stderr);
|
|
|
|
} else {
|
|
|
|
if (n < 0) {
|
|
|
|
fprintf(stderr,
|
2020-02-12 13:59:18 +01:00
|
|
|
"Failed to read %zd bytes from %s: "
|
|
|
|
"%s\n",
|
|
|
|
(ssize_t)st.st_size, filename,
|
2018-07-03 15:45:11 +02:00
|
|
|
strerror(errno));
|
|
|
|
} else {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Failed to read %zd bytes from %s"
|
|
|
|
", got %zd\n",
|
2020-02-12 13:59:18 +01:00
|
|
|
(ssize_t)st.st_size, filename, n);
|
2018-07-03 15:45:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
free(data);
|
|
|
|
closefd:
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
closedir(dirp);
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
int
|
2020-02-14 08:14:03 +01:00
|
|
|
main(int argc, char **argv) {
|
|
|
|
char corpusdir[PATH_MAX];
|
2018-07-03 15:45:11 +02:00
|
|
|
const char *target = strrchr(argv[0], '/');
|
|
|
|
|
2020-07-31 13:53:38 +02:00
|
|
|
(void)LLVMFuzzerInitialize(&argc, &argv);
|
|
|
|
|
2018-07-03 15:45:11 +02:00
|
|
|
UNUSED(argc);
|
|
|
|
UNUSED(argv);
|
|
|
|
|
2020-08-13 12:18:57 +10:00
|
|
|
if (argc != 1) {
|
|
|
|
debug = true;
|
|
|
|
}
|
|
|
|
|
2020-01-30 17:57:25 +11:00
|
|
|
target = (target != NULL) ? target + 1 : argv[0];
|
2018-07-03 15:45:11 +02:00
|
|
|
if (strncmp(target, "lt-", 3) == 0) {
|
|
|
|
target += 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(corpusdir, sizeof(corpusdir), FUZZDIR "/%s.in", target);
|
|
|
|
|
|
|
|
test_all_from(corpusdir);
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
return (0);
|
2018-07-03 15:45:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif __AFL_COMPILER
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
int
|
2020-02-14 08:14:03 +01:00
|
|
|
main(int argc, char **argv) {
|
|
|
|
int ret;
|
2018-07-03 15:45:11 +02:00
|
|
|
unsigned char buf[64 * 1024];
|
|
|
|
|
2020-07-31 13:53:38 +02:00
|
|
|
(void)LLVMFuzzerInitialize(&argc, &argv);
|
2018-07-03 15:45:11 +02:00
|
|
|
|
|
|
|
#ifdef __AFL_LOOP
|
2020-02-13 21:48:23 +01:00
|
|
|
while (__AFL_LOOP(10000)) { /* only works with afl-clang-fast */
|
|
|
|
#else /* ifdef __AFL_LOOP */
|
2018-07-03 15:45:11 +02:00
|
|
|
{
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* ifdef __AFL_LOOP */
|
2018-07-03 15:45:11 +02:00
|
|
|
ret = fread(buf, 1, sizeof(buf), stdin);
|
|
|
|
if (ret < 0) {
|
2020-02-13 21:48:23 +01:00
|
|
|
return (0);
|
2018-07-03 15:45:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LLVMFuzzerTestOneInput(buf, ret);
|
|
|
|
}
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
return (0);
|
2018-07-03 15:45:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION */
|