blob: 736cc5228a274fc915c37ecbd061634aed67c2c8 [file] [log] [blame]
Juraj Vijtiukd7989422020-09-15 13:54:00 +02001#include <stdio.h>
2#include <stdlib.h>
3#include <dirent.h>
4#include <unistd.h>
5#include <sys/types.h>
6#include <sys/wait.h>
7#include <sys/stat.h>
8#include<fcntl.h>
9
10int main(int argc, char **argv)
11{
12 DIR *d;
13 struct dirent *dir;
14 pid_t p = 0;
15 int input_fd = 0;
16 int status = 0;
17 int rc = 0;
18 struct stat path_stat;
Juraj Vijtiukd7989422020-09-15 13:54:00 +020019
20 if (argc != 3) {
21 fprintf(stderr, "invalid number of arguments. Call like this ./fuzz_regression_test fuzz_harness corpus_dir\n");
22 return EXIT_FAILURE;
23 }
24
25 d = opendir(argv[2]);
26 if (!d) {
27 fprintf(stderr, "error opening dir %s\n", argv[2]);
28 return EXIT_FAILURE;
29 }
30
31 while ((dir = readdir(d)) != NULL) {
32 stat(dir->d_name, &path_stat);
33 if (!S_ISREG(path_stat.st_mode)) {
34 continue;
35 }
36
37 p = fork();
38 if (p == -1) {
39 fprintf(stderr, "fork failed\n");
40 return EXIT_FAILURE;
41 } else if (p == 0) {
42 input_fd = open(dir->d_name, O_RDONLY);
43 if (input_fd == -1) {
44 fprintf(stderr, "error opening input file %s\n", dir->d_name);
45 return EXIT_FAILURE;
46 }
47
48 dup2(input_fd, STDIN_FILENO);
49 execl(argv[1], argv[1], NULL);
50 return EXIT_SUCCESS;
51 }
52
53 rc = waitpid(p, &status, 0);
54 if (rc == -1) {
55 fprintf(stderr, "waitpid failed\n");
56 return EXIT_FAILURE;
57 }
58
59 if (!WIFEXITED(status)) {
Juraj Vijtiukbef5faf2020-09-16 15:40:02 +020060 fprintf(stderr, "test %s - %s failed\n", argv[1], dir->d_name);
Juraj Vijtiukd7989422020-09-15 13:54:00 +020061 return EXIT_FAILURE;
62 }
63
64 printf("test %s - %s successful\n", argv[1], dir->d_name);
65 }
66
Juraj Vijtiuk4a648322020-11-04 23:43:06 +010067 closedir(d);
68
Juraj Vijtiukd7989422020-09-15 13:54:00 +020069 return EXIT_SUCCESS;
70}