libtpms/tests/fuzz-main.c
Marc-André Lureau b214dabfc8 fuzz: better oss-fuzz integration
There were a few issues with the oss-fuzz integration from commit
8373f09854 ("build-sys: add oss-fuzz
support").

When building on OSS-Fuzz, the projects should use the provided CFLAGS
and CXXFLAGS and don't append any extra sanitization / fuzzing flags.

$LIB_FUZZING_ENGINE is defined to set the library to link to, and it
is a c++ library, so we should build fuzzer with c++...

Now --enable-fuzzer is only used for -fsanitize=fuzzer.

Add a tests/fuzz-main.c as fallback, to run the corpus on other builds.

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
2019-04-01 11:22:03 -04:00

51 lines
1.1 KiB
C

#include <stdlib.h>
#include <stdio.h>
#define MIN_NUMBER_OF_RUNS 4
#define EXIT_TEST_SKIP 77
extern int LLVMFuzzerTestOneInput (const unsigned char *data, size_t size);
int main(int argc, char **argv)
{
int i, j;
for (i = 1; i < argc; i++) {
char *name = argv[i];
ssize_t size;
FILE *f = fopen(name, "rb");
char *buf;
fprintf(stdout, "%s...\n", name);
if (f == NULL) {
perror("fopen() failed");
continue;
}
fseek(f, 0, SEEK_END);
size = ftell(f);
if (size < 0) {
fclose(f);
perror("ftell() failed");
continue;
}
fseek(f, 0, SEEK_SET);
buf = malloc(size + 1);
if (fread(buf, 1, size, f) != (size_t)size) {
fclose(f);
perror("fread() failed");
continue;
}
fclose(f);
buf[size] = 0;
for (j = 0; j < MIN_NUMBER_OF_RUNS; j++) {
if (LLVMFuzzerTestOneInput((void *)buf, size) == EXIT_TEST_SKIP) {
return EXIT_TEST_SKIP;
}
}
free(buf);
}
return EXIT_SUCCESS;
}