2017-06-09 16:27:37 +02:00
|
|
|
/*
|
2018-02-22 10:03:37 +01:00
|
|
|
* Copyright(c) 2017-2018 Tim Ruehsen
|
2017-06-09 16:27:37 +02:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* This file is part of libpsl.
|
|
|
|
*/
|
|
|
|
|
2018-12-08 23:27:28 +01:00
|
|
|
#include <config.h>
|
2017-06-09 16:27:37 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2018-04-20 08:42:30 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
2017-06-09 16:27:37 +02:00
|
|
|
#include <unistd.h>
|
2018-04-20 08:42:30 +02:00
|
|
|
#endif
|
|
|
|
|
2017-06-09 16:27:37 +02:00
|
|
|
#include <stdlib.h>
|
2018-04-20 08:42:30 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_STDINT_H
|
2017-06-09 16:27:37 +02:00
|
|
|
#include <stdint.h>
|
2018-04-20 08:42:30 +02:00
|
|
|
#endif
|
|
|
|
|
2017-06-09 16:27:37 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2019-04-04 10:11:44 +02:00
|
|
|
#if defined (_MSC_VER) && ! defined (ssize_t)
|
|
|
|
#include <basetsd.h>
|
|
|
|
typedef SSIZE_T ssize_t;
|
|
|
|
#endif
|
|
|
|
|
2017-06-09 16:27:37 +02:00
|
|
|
#include "fuzzer.h"
|
|
|
|
|
2019-06-04 21:31:43 +02:00
|
|
|
#if defined (TEST_RUN) && defined (HAVE_FMEMOPEN) && defined (HAVE_DIRENT_H)
|
|
|
|
|
|
|
|
#include <dirent.h>
|
2017-06-09 16:27:37 +02:00
|
|
|
|
|
|
|
static void test_all_from(const char *dirname)
|
|
|
|
{
|
|
|
|
DIR *dirp;
|
|
|
|
struct dirent *dp;
|
|
|
|
|
|
|
|
if ((dirp = opendir(dirname))) {
|
|
|
|
while ((dp = readdir(dirp))) {
|
2019-06-02 13:07:35 +02:00
|
|
|
char fname[1024];
|
2018-12-08 23:38:45 +01:00
|
|
|
int fd;
|
|
|
|
struct stat st;
|
|
|
|
uint8_t *data;
|
|
|
|
ssize_t n;
|
|
|
|
|
2017-06-09 16:27:37 +02:00
|
|
|
if (*dp->d_name == '.') continue;
|
|
|
|
|
2019-06-02 13:07:35 +02:00
|
|
|
snprintf(fname, sizeof(fname), "%s/%s", dirname, dp->d_name);
|
2017-06-09 16:27:37 +02:00
|
|
|
|
|
|
|
if ((fd = open(fname, O_RDONLY)) == -1) {
|
|
|
|
fprintf(stderr, "Failed to open %s (%d)\n", fname, errno);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fstat(fd, &st) != 0) {
|
|
|
|
fprintf(stderr, "Failed to stat %d (%d)\n", fd, errno);
|
|
|
|
close(fd);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-12-08 23:38:45 +01:00
|
|
|
data = malloc(st.st_size);
|
2017-06-09 16:27:37 +02:00
|
|
|
if ((n = read(fd, data, st.st_size)) == st.st_size) {
|
2019-06-02 13:07:35 +02:00
|
|
|
printf("testing %d bytes from '%s'\n", (int) n, fname);
|
2017-06-09 16:27:37 +02:00
|
|
|
LLVMFuzzerTestOneInput(data, st.st_size);
|
|
|
|
} else
|
2018-12-08 23:38:45 +01:00
|
|
|
fprintf(stderr, "Failed to read %d bytes from %s (%d), got %d\n", (int) st.st_size, fname, errno, (int) n);
|
2017-06-09 16:27:37 +02:00
|
|
|
|
|
|
|
free(data);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
/* if VALGRIND testing is enabled, we have to call ourselves with valgrind checking */
|
|
|
|
if (argc == 1) {
|
|
|
|
const char *valgrind = getenv("TESTS_VALGRIND");
|
|
|
|
|
|
|
|
if (valgrind && *valgrind) {
|
2019-06-02 13:07:35 +02:00
|
|
|
char cmd[1024];
|
2017-06-09 16:27:37 +02:00
|
|
|
|
2019-06-02 13:07:35 +02:00
|
|
|
snprintf(cmd, sizeof(cmd), "TESTS_VALGRIND="" %s %s", valgrind, argv[0]);
|
2017-06-09 16:27:37 +02:00
|
|
|
return system(cmd) != 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-02 13:07:35 +02:00
|
|
|
const char *target = strrchr(argv[0], '/');
|
2017-06-09 16:27:37 +02:00
|
|
|
target = target ? target + 1 : argv[0];
|
|
|
|
|
2019-06-02 13:07:35 +02:00
|
|
|
char corporadir[1024];
|
|
|
|
snprintf(corporadir, sizeof(corporadir), SRCDIR "/%s.in", target);
|
2017-06-09 16:27:37 +02:00
|
|
|
|
|
|
|
test_all_from(corporadir);
|
|
|
|
|
2019-06-02 13:07:35 +02:00
|
|
|
snprintf(corporadir, sizeof(corporadir), SRCDIR "/%s.repro", target);
|
2017-06-09 16:27:37 +02:00
|
|
|
|
|
|
|
test_all_from(corporadir);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-04 21:31:43 +02:00
|
|
|
#else /* TEST_RUN && HAVE_FMEMOPEN && HAVE_DIRENT_H */
|
2017-06-09 16:27:37 +02:00
|
|
|
|
|
|
|
#ifndef __AFL_LOOP
|
|
|
|
static int __AFL_LOOP(int n)
|
|
|
|
{
|
|
|
|
static int first = 1;
|
|
|
|
|
|
|
|
if (first) {
|
|
|
|
first = 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2018-04-20 08:42:30 +02:00
|
|
|
#ifdef HAVE_FMEMOPEN
|
2017-06-09 16:27:37 +02:00
|
|
|
int ret;
|
|
|
|
unsigned char buf[64 * 1024];
|
|
|
|
|
2017-07-13 16:30:52 +02:00
|
|
|
while (__AFL_LOOP(10000)) { /* only works with afl-clang-fast */
|
2017-06-09 16:27:37 +02:00
|
|
|
ret = fread(buf, 1, sizeof(buf), stdin);
|
|
|
|
if (ret < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
LLVMFuzzerTestOneInput(buf, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2018-04-20 08:42:30 +02:00
|
|
|
#else
|
|
|
|
exit (77);
|
|
|
|
#endif
|
2017-06-09 16:27:37 +02:00
|
|
|
}
|
|
|
|
|
2019-06-04 21:31:43 +02:00
|
|
|
#endif /* TEST_RUN && HAVE_FMEMOPEN && HAVE_DIRENT_H */
|