Make the fuzz harness print status messages

This commit is contained in:
Nigel Tao committed 2018-05-03 09:03:48 +10:00
1 parent 2a97a1ad29
commit db308f1045
3 files changed
+42 -15

No files matched your search

+20 -11
View File
@@ -26,13 +26,9 @@ void intentional_segfault() {
*intentional_segfault_ptr = 0;
}
void fuzz(wuffs_base__io_reader src_reader, uint32_t hash);
const char* fuzz(wuffs_base__io_reader src_reader, uint32_t hash);
#ifdef __cplusplus
extern "C" {
#endif
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
static const char* llvmFuzzerTestOneInput(const uint8_t* data, size_t size) {
// Hash input as per https://en.wikipedia.org/wiki/Jenkins_hash_function
size_t i = 0;
uint32_t hash = 0;
@@ -50,7 +46,15 @@ int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
.wi = size,
.ri = 0,
.closed = true};
fuzz(wuffs_base__io_buffer__reader(&src), hash);
return fuzz(wuffs_base__io_buffer__reader(&src), hash);
}
#ifdef __cplusplus
extern "C" {
#endif
int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
llvmFuzzerTestOneInput(data, size);
return 0;
}
@@ -69,33 +73,38 @@ int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
int main(int argc, char** argv) {
int i;
for (i = 1; i < argc; i++) {
printf("Processing %s\n", argv[i]);
printf("%-50s", argv[i]);
struct stat z;
int fd = open(argv[i], O_RDONLY, 0);
if (fd == -1) {
printf("\n");
fprintf(stderr, "FAIL: open: %s\n", strerror(errno));
return 1;
}
if (fstat(fd, &z)) {
printf("\n");
fprintf(stderr, "FAIL: fstat: %s\n", strerror(errno));
return 1;
}
if ((z.st_size < 0) || (0x7FFFFFFF < z.st_size)) {
printf("\n");
fprintf(stderr, "FAIL: file size out of bounds");
return 1;
}
size_t n = z.st_size;
void* data = mmap(NULL, n, PROT_READ, MAP_SHARED, fd, 0);
if (data == MAP_FAILED) {
printf("\n");
fprintf(stderr, "FAIL: mmap: %s\n", strerror(errno));
return 1;
}
int fuzz = LLVMFuzzerTestOneInput((const uint8_t*)(data), n);
if (fuzz) {
return fuzz;
const char* msg = llvmFuzzerTestOneInput((const uint8_t*)(data), n);
if (!msg) {
msg = "(null)";
}
printf(" %s\n", msg);
if (munmap(data, n)) {
fprintf(stderr, "FAIL: mmap: %s\n", strerror(errno));
+18 -3
View File
@@ -38,7 +38,8 @@ It should print "PASS", amongst other information, and exit(0).
#include "../../../gen/c/std/gif.c"
#include "../fuzzlib/fuzzlib.c"
void fuzz(wuffs_base__io_reader src_reader, uint32_t hash) {
const char* fuzz(wuffs_base__io_reader src_reader, uint32_t hash) {
const char* ret = NULL;
void* pixbuf = NULL;
// Use a {} code block so that "goto exit" doesn't trigger "jump bypasses
@@ -50,23 +51,31 @@ void fuzz(wuffs_base__io_reader src_reader, uint32_t hash) {
wuffs_base__image_config ic = {{0}};
s = wuffs_gif__decoder__decode_config(&dec, &ic, src_reader);
if (s || !wuffs_base__image_config__is_valid(&ic)) {
if (s) {
ret = wuffs_gif__status__string(s);
goto exit;
}
if (!wuffs_base__image_config__is_valid(&ic)) {
ret = "invalid image_config";
goto exit;
}
size_t pixbuf_size = wuffs_base__image_config__pixbuf_size(&ic);
// Don't try to allocate more than 64 MiB.
if (pixbuf_size > 64 * 1024 * 1024) {
ret = "image too large";
goto exit;
}
pixbuf = malloc(pixbuf_size);
if (!pixbuf) {
ret = "out of memory";
goto exit;
}
wuffs_base__io_buffer dst = {.ptr = (uint8_t*)(pixbuf), .len = pixbuf_size};
wuffs_base__io_writer dst_writer = wuffs_base__io_buffer__writer(&dst);
bool seen_ok = false;
while (true) {
// TODO: handle the frame rect being larger than the image rect. The
// GIF89a spec doesn't disallow this and the Wuffs code tolerates it, in
@@ -76,8 +85,13 @@ void fuzz(wuffs_base__io_reader src_reader, uint32_t hash) {
dst.wi = 0;
s = wuffs_gif__decoder__decode_frame(&dec, dst_writer, src_reader);
if (s) {
break;
if ((s == WUFFS_GIF__SUSPENSION_END_OF_DATA) && seen_ok) {
s = WUFFS_GIF__STATUS_OK;
}
ret = wuffs_gif__status__string(s);
goto exit;
}
seen_ok = true;
}
}
@@ -85,4 +99,5 @@ exit:
if (pixbuf) {
free(pixbuf);
}
return ret;
}
+4 -1
View File
@@ -40,7 +40,8 @@ It should print "PASS", amongst other information, and exit(0).
#include "../../../gen/c/std/zlib.c"
#include "../fuzzlib/fuzzlib.c"
void fuzz(wuffs_base__io_reader src_reader, uint32_t hash) {
const char* fuzz(wuffs_base__io_reader src_reader, uint32_t hash) {
const char* ret = NULL;
wuffs_zlib__status s;
wuffs_zlib__decoder dec;
wuffs_zlib__decoder__initialize(&dec, WUFFS_VERSION, 0);
@@ -59,6 +60,7 @@ void fuzz(wuffs_base__io_reader src_reader, uint32_t hash) {
dst.wi = 0;
s = wuffs_zlib__decoder__decode(&dec, dst_writer, src_reader);
if (s != WUFFS_ZLIB__SUSPENSION_SHORT_WRITE) {
ret = wuffs_zlib__status__string(s);
break;
}
if (dst.wi == 0) {
@@ -66,4 +68,5 @@ void fuzz(wuffs_base__io_reader src_reader, uint32_t hash) {
intentional_segfault();
}
}
return ret;
}