diff options
| author | Nathan Moinvaziri <nathan@nathanm.com> | 2019-10-06 10:59:07 -0700 |
|---|---|---|
| committer | Hans Kristian Rosbach <hk-git@circlestorm.org> | 2019-10-06 19:59:07 +0200 |
| commit | 80e6de8a063bd8895d70c3c2dcf792b9b8c7fc48 (patch) | |
| tree | f18469e33271a5bdb338d00757b51779f2963969 /test/fuzz | |
| parent | 503bdef7a29a7078a50d8149e261c4299a82a9da (diff) | |
| download | Project-Tick-80e6de8a063bd8895d70c3c2dcf792b9b8c7fc48.tar.gz Project-Tick-80e6de8a063bd8895d70c3c2dcf792b9b8c7fc48.zip | |
Fixed tab formatting in fuzzers (#414)
* Fixed tab formatting in fuzzers.
* Fixed function argument spacing.
* Fixed whitespace in comments and strings.
Diffstat (limited to 'test/fuzz')
| -rw-r--r-- | test/fuzz/compress_fuzzer.c | 88 | ||||
| -rw-r--r-- | test/fuzz/example_dict_fuzzer.c | 114 | ||||
| -rw-r--r-- | test/fuzz/example_flush_fuzzer.c | 159 | ||||
| -rw-r--r-- | test/fuzz/example_large_fuzzer.c | 192 | ||||
| -rw-r--r-- | test/fuzz/example_small_fuzzer.c | 155 | ||||
| -rw-r--r-- | test/fuzz/minigzip_fuzzer.c | 154 | ||||
| -rw-r--r-- | test/fuzz/standalone_fuzz_target_runner.c | 50 |
7 files changed, 454 insertions, 458 deletions
diff --git a/test/fuzz/compress_fuzzer.c b/test/fuzz/compress_fuzzer.c index e2ccf71772..9c7de09fcb 100644 --- a/test/fuzz/compress_fuzzer.c +++ b/test/fuzz/compress_fuzzer.c @@ -19,69 +19,69 @@ static size_t dataLen; static void check_compress_level(uint8_t *compr, size_t comprLen, uint8_t *uncompr, size_t uncomprLen, int level) { - PREFIX(compress2)(compr, &comprLen, data, dataLen, level); - PREFIX(uncompress)(uncompr, &uncomprLen, compr, comprLen); + PREFIX(compress2)(compr, &comprLen, data, dataLen, level); + PREFIX(uncompress)(uncompr, &uncomprLen, compr, comprLen); - /* Make sure compress + uncompress gives back the input data. */ - assert(dataLen == uncomprLen); - assert(0 == memcmp(data, uncompr, dataLen)); + /* Make sure compress + uncompress gives back the input data. */ + assert(dataLen == uncomprLen); + assert(0 == memcmp(data, uncompr, dataLen)); } #define put_byte(s, i, c) {s[i] = (unsigned char)(c);} static void write_zlib_header(uint8_t *s) { - unsigned level_flags = 0; /* compression level (0..3) */ - unsigned w_bits = 8; /* window size log2(w_size) (8..16) */ - unsigned int header = (Z_DEFLATED + ((w_bits-8)<<4)) << 8; - header |= (level_flags << 6); + unsigned level_flags = 0; /* compression level (0..3) */ + unsigned w_bits = 8; /* window size log2(w_size) (8..16) */ + unsigned int header = (Z_DEFLATED + ((w_bits-8)<<4)) << 8; + header |= (level_flags << 6); - header += 31 - (header % 31); + header += 31 - (header % 31); - /* s is guaranteed to be longer than 2 bytes. */ - put_byte(s, 0, (unsigned char)(header >> 8)); - put_byte(s, 1, (unsigned char)(header & 0xff)); + /* s is guaranteed to be longer than 2 bytes. */ + put_byte(s, 0, (unsigned char)(header >> 8)); + put_byte(s, 1, (unsigned char)(header & 0xff)); } static void check_decompress(uint8_t *compr, size_t comprLen) { - /* We need to write a valid zlib header of size two bytes. Copy the input data - in a larger buffer. Do not modify the input data to avoid libFuzzer error: - fuzz target overwrites its const input. */ - size_t copyLen = dataLen + 2; - uint8_t *copy = (uint8_t *)malloc(copyLen); - memcpy(copy + 2, data, dataLen); - write_zlib_header(copy); - - PREFIX(uncompress)(compr, &comprLen, copy, copyLen); - free(copy); + /* We need to write a valid zlib header of size two bytes. Copy the input data + in a larger buffer. Do not modify the input data to avoid libFuzzer error: + fuzz target overwrites its const input. */ + size_t copyLen = dataLen + 2; + uint8_t *copy = (uint8_t *)malloc(copyLen); + memcpy(copy + 2, data, dataLen); + write_zlib_header(copy); + + PREFIX(uncompress)(compr, &comprLen, copy, copyLen); + free(copy); } int LLVMFuzzerTestOneInput(const uint8_t *d, size_t size) { - /* compressBound does not provide enough space for low compression levels. */ - size_t comprLen = 100 + 2 * PREFIX(compressBound)(size); - size_t uncomprLen = size; - uint8_t *compr, *uncompr; + /* compressBound does not provide enough space for low compression levels. */ + size_t comprLen = 100 + 2 * PREFIX(compressBound)(size); + size_t uncomprLen = size; + uint8_t *compr, *uncompr; - /* Discard inputs larger than 1Mb. */ - static size_t kMaxSize = 1024 * 1024; + /* Discard inputs larger than 1Mb. */ + static size_t kMaxSize = 1024 * 1024; - if (size < 1 || size > kMaxSize) - return 0; + if (size < 1 || size > kMaxSize) + return 0; - data = d; - dataLen = size; - compr = (uint8_t *)calloc(1, comprLen); - uncompr = (uint8_t *)calloc(1, uncomprLen); + data = d; + dataLen = size; + compr = (uint8_t *)calloc(1, comprLen); + uncompr = (uint8_t *)calloc(1, uncomprLen); - check_compress_level(compr, comprLen, uncompr, uncomprLen, 1); - check_compress_level(compr, comprLen, uncompr, uncomprLen, 3); - check_compress_level(compr, comprLen, uncompr, uncomprLen, 6); - check_compress_level(compr, comprLen, uncompr, uncomprLen, 7); + check_compress_level(compr, comprLen, uncompr, uncomprLen, 1); + check_compress_level(compr, comprLen, uncompr, uncomprLen, 3); + check_compress_level(compr, comprLen, uncompr, uncomprLen, 6); + check_compress_level(compr, comprLen, uncompr, uncomprLen, 7); - check_decompress(compr, comprLen); + check_decompress(compr, comprLen); - free(compr); - free(uncompr); + free(compr); + free(uncompr); - /* This function must return 0. */ - return 0; + /* This function must return 0. */ + return 0; } diff --git a/test/fuzz/example_dict_fuzzer.c b/test/fuzz/example_dict_fuzzer.c index b191011737..a9035982fd 100644 --- a/test/fuzz/example_dict_fuzzer.c +++ b/test/fuzz/example_dict_fuzzer.c @@ -96,75 +96,75 @@ void test_dict_deflate(unsigned char **compr, size_t *comprLen) * Test inflate() with a preset dictionary */ void test_dict_inflate(unsigned char *compr, size_t comprLen) { - int err; - PREFIX3(stream) d_stream; /* decompression stream */ - unsigned char *uncompr; - - d_stream.zalloc = zalloc; - d_stream.zfree = zfree; - d_stream.opaque = (void *)0; - - d_stream.next_in = compr; - d_stream.avail_in = (unsigned int)comprLen; - - err = PREFIX(inflateInit)(&d_stream); - CHECK_ERR(err, "inflateInit"); - - uncompr = (uint8_t *)calloc(1, dataLen); - d_stream.next_out = uncompr; - d_stream.avail_out = (unsigned int)dataLen; - - for (;;) { - err = PREFIX(inflate)(&d_stream, Z_NO_FLUSH); - if (err == Z_STREAM_END) - break; - if (err == Z_NEED_DICT) { - if (d_stream.adler != dictId) { - fprintf(stderr, "unexpected dictionary"); - exit(1); - } - err = PREFIX(inflateSetDictionary)( - &d_stream, (const unsigned char *)data, dictionaryLen); + int err; + PREFIX3(stream) d_stream; /* decompression stream */ + unsigned char *uncompr; + + d_stream.zalloc = zalloc; + d_stream.zfree = zfree; + d_stream.opaque = (void *)0; + + d_stream.next_in = compr; + d_stream.avail_in = (unsigned int)comprLen; + + err = PREFIX(inflateInit)(&d_stream); + CHECK_ERR(err, "inflateInit"); + + uncompr = (uint8_t *)calloc(1, dataLen); + d_stream.next_out = uncompr; + d_stream.avail_out = (unsigned int)dataLen; + + for (;;) { + err = PREFIX(inflate)(&d_stream, Z_NO_FLUSH); + if (err == Z_STREAM_END) + break; + if (err == Z_NEED_DICT) { + if (d_stream.adler != dictId) { + fprintf(stderr, "unexpected dictionary"); + exit(1); + } + err = PREFIX(inflateSetDictionary)( + &d_stream, (const unsigned char *)data, dictionaryLen); + } + CHECK_ERR(err, "inflate with dict"); } - CHECK_ERR(err, "inflate with dict"); - } - err = PREFIX(inflateEnd)(&d_stream); - CHECK_ERR(err, "inflateEnd"); + err = PREFIX(inflateEnd)(&d_stream); + CHECK_ERR(err, "inflateEnd"); - if (memcmp(uncompr, data, dataLen)) { - fprintf(stderr, "bad inflate with dict\n"); - exit(1); - } + if (memcmp(uncompr, data, dataLen)) { + fprintf(stderr, "bad inflate with dict\n"); + exit(1); + } - free(uncompr); + free(uncompr); } int LLVMFuzzerTestOneInput(const uint8_t *d, size_t size) { - size_t comprLen = 0; - uint8_t *compr; + size_t comprLen = 0; + uint8_t *compr; - /* Discard inputs larger than 100Kb. */ - static size_t kMaxSize = 100 * 1024; + /* Discard inputs larger than 100Kb. */ + static size_t kMaxSize = 100 * 1024; - if (size < 1 || size > kMaxSize) - return 0; + if (size < 1 || size > kMaxSize) + return 0; - data = d; - dataLen = size; + data = d; + dataLen = size; - /* Set up the contents of the dictionary. The size of the dictionary is - intentionally selected to be of unusual size. To help cover more corner - cases, the size of the dictionary is read from the input data. */ - dictionaryLen = data[0]; - if (dictionaryLen > dataLen) - dictionaryLen = dataLen; + /* Set up the contents of the dictionary. The size of the dictionary is + intentionally selected to be of unusual size. To help cover more corner + cases, the size of the dictionary is read from the input data. */ + dictionaryLen = data[0]; + if (dictionaryLen > dataLen) + dictionaryLen = dataLen; - test_dict_deflate(&compr, &comprLen); - test_dict_inflate(compr, comprLen); + test_dict_deflate(&compr, &comprLen); + test_dict_inflate(compr, comprLen); - free(compr); + free(compr); - /* This function must return 0. */ - return 0; + /* This function must return 0. */ + return 0; } diff --git a/test/fuzz/example_flush_fuzzer.c b/test/fuzz/example_flush_fuzzer.c index f9f2d36466..e53ceeca33 100644 --- a/test/fuzz/example_flush_fuzzer.c +++ b/test/fuzz/example_flush_fuzzer.c @@ -29,98 +29,97 @@ static free_func zfree = NULL; * Test deflate() with full flush */ void test_flush(unsigned char *compr, z_size_t *comprLen) { - PREFIX3(stream) c_stream; /* compression stream */ - int err; - unsigned int len = dataLen; - - c_stream.zalloc = zalloc; - c_stream.zfree = zfree; - c_stream.opaque = (void *)0; - - err = PREFIX(deflateInit)(&c_stream, Z_DEFAULT_COMPRESSION); - CHECK_ERR(err, "deflateInit"); - - c_stream.next_in = (const unsigned char *)data; - c_stream.next_out = compr; - c_stream.avail_in = 3; - c_stream.avail_out = (unsigned int)*comprLen; - err = PREFIX(deflate)(&c_stream, Z_FULL_FLUSH); - CHECK_ERR(err, "deflate flush 1"); - - compr[3]++; /* force an error in first compressed block */ - c_stream.avail_in = len - 3; - - err = PREFIX(deflate)(&c_stream, Z_FINISH); - if (err != Z_STREAM_END) { - CHECK_ERR(err, "deflate flush 2"); - } - err = PREFIX(deflateEnd)(&c_stream); - CHECK_ERR(err, "deflateEnd"); - - *comprLen = (z_size_t)c_stream.total_out; + PREFIX3(stream) c_stream; /* compression stream */ + int err; + unsigned int len = dataLen; + + c_stream.zalloc = zalloc; + c_stream.zfree = zfree; + c_stream.opaque = (void *)0; + + err = PREFIX(deflateInit)(&c_stream, Z_DEFAULT_COMPRESSION); + CHECK_ERR(err, "deflateInit"); + + c_stream.next_in = (const unsigned char *)data; + c_stream.next_out = compr; + c_stream.avail_in = 3; + c_stream.avail_out = (unsigned int)*comprLen; + err = PREFIX(deflate)(&c_stream, Z_FULL_FLUSH); + CHECK_ERR(err, "deflate flush 1"); + + compr[3]++; /* force an error in first compressed block */ + c_stream.avail_in = len - 3; + + err = PREFIX(deflate)(&c_stream, Z_FINISH); + if (err != Z_STREAM_END) { + CHECK_ERR(err, "deflate flush 2"); + } + err = PREFIX(deflateEnd)(&c_stream); + CHECK_ERR(err, "deflateEnd"); + + *comprLen = (z_size_t)c_stream.total_out; } /* =========================================================================== * Test inflateSync() */ -void test_sync(unsigned char *compr, size_t comprLen, unsigned char *uncompr, - size_t uncomprLen) { - int err; - PREFIX3(stream) d_stream; /* decompression stream */ - - d_stream.zalloc = zalloc; - d_stream.zfree = zfree; - d_stream.opaque = (void *)0; - - d_stream.next_in = compr; - d_stream.avail_in = 2; /* just read the zlib header */ - - err = PREFIX(inflateInit)(&d_stream); - CHECK_ERR(err, "inflateInit"); - - d_stream.next_out = uncompr; - d_stream.avail_out = (unsigned int)uncomprLen; - - err = PREFIX(inflate)(&d_stream, Z_NO_FLUSH); - CHECK_ERR(err, "inflate"); - - d_stream.avail_in = (unsigned int)comprLen - 2; /* read all compressed data */ - err = PREFIX(inflateSync)(&d_stream); /* but skip the damaged part */ - CHECK_ERR(err, "inflateSync"); - - err = PREFIX(inflate)(&d_stream, Z_FINISH); - if (err != Z_DATA_ERROR) { - fprintf(stderr, "inflate should report DATA_ERROR\n"); - /* Because of incorrect adler32 */ - exit(1); - } - err = PREFIX(inflateEnd)(&d_stream); - CHECK_ERR(err, "inflateEnd"); +void test_sync(unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen) { + int err; + PREFIX3(stream) d_stream; /* decompression stream */ + + d_stream.zalloc = zalloc; + d_stream.zfree = zfree; + d_stream.opaque = (void *)0; + + d_stream.next_in = compr; + d_stream.avail_in = 2; /* just read the zlib header */ + + err = PREFIX(inflateInit)(&d_stream); + CHECK_ERR(err, "inflateInit"); + + d_stream.next_out = uncompr; + d_stream.avail_out = (unsigned int)uncomprLen; + + err = PREFIX(inflate)(&d_stream, Z_NO_FLUSH); + CHECK_ERR(err, "inflate"); + + d_stream.avail_in = (unsigned int)comprLen - 2; /* read all compressed data */ + err = PREFIX(inflateSync)(&d_stream); /* but skip the damaged part */ + CHECK_ERR(err, "inflateSync"); + + err = PREFIX(inflate)(&d_stream, Z_FINISH); + if (err != Z_DATA_ERROR) { + fprintf(stderr, "inflate should report DATA_ERROR\n"); + /* Because of incorrect adler32 */ + exit(1); + } + err = PREFIX(inflateEnd)(&d_stream); + CHECK_ERR(err, "inflateEnd"); } int LLVMFuzzerTestOneInput(const uint8_t *d, size_t size) { - size_t comprLen = 100 + 2 * PREFIX(compressBound)(size); - size_t uncomprLen = size; - uint8_t *compr, *uncompr; + size_t comprLen = 100 + 2 * PREFIX(compressBound)(size); + size_t uncomprLen = size; + uint8_t *compr, *uncompr; - /* Discard inputs larger than 1Mb. */ - static size_t kMaxSize = 1024 * 1024; + /* Discard inputs larger than 1Mb. */ + static size_t kMaxSize = 1024 * 1024; - // This test requires at least 3 bytes of input data. - if (size <= 3 || size > kMaxSize) - return 0; + // This test requires at least 3 bytes of input data. + if (size <= 3 || size > kMaxSize) + return 0; - data = d; - dataLen = size; - compr = (uint8_t *)calloc(1, comprLen); - uncompr = (uint8_t *)calloc(1, uncomprLen); + data = d; + dataLen = size; + compr = (uint8_t *)calloc(1, comprLen); + uncompr = (uint8_t *)calloc(1, uncomprLen); - test_flush(compr, &comprLen); - test_sync(compr, comprLen, uncompr, uncomprLen); + test_flush(compr, &comprLen); + test_sync(compr, comprLen, uncompr, uncomprLen); - free(compr); - free(uncompr); + free(compr); + free(uncompr); - /* This function must return 0. */ - return 0; + /* This function must return 0. */ + return 0; } diff --git a/test/fuzz/example_large_fuzzer.c b/test/fuzz/example_large_fuzzer.c index 1e8fedc220..4f90a7ff72 100644 --- a/test/fuzz/example_large_fuzzer.c +++ b/test/fuzz/example_large_fuzzer.c @@ -29,115 +29,113 @@ static unsigned int diff; /* =========================================================================== * Test deflate() with large buffers and dynamic change of compression level */ -void test_large_deflate(unsigned char *compr, size_t comprLen, - unsigned char *uncompr, size_t uncomprLen) { - PREFIX3(stream) c_stream; /* compression stream */ - int err; - - c_stream.zalloc = zalloc; - c_stream.zfree = zfree; - c_stream.opaque = (void *)0; - - err = PREFIX(deflateInit)(&c_stream, Z_BEST_COMPRESSION); - CHECK_ERR(err, "deflateInit"); - - c_stream.next_out = compr; - c_stream.avail_out = (unsigned int)comprLen; - - /* At this point, uncompr is still mostly zeroes, so it should compress - * very well: - */ - c_stream.next_in = uncompr; - c_stream.avail_in = (unsigned int)uncomprLen; - err = PREFIX(deflate)(&c_stream, Z_NO_FLUSH); - CHECK_ERR(err, "deflate large 1"); - if (c_stream.avail_in != 0) { - fprintf(stderr, "deflate not greedy\n"); - exit(1); - } - - /* Feed in already compressed data and switch to no compression: */ - PREFIX(deflateParams)(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY); - c_stream.next_in = compr; - diff = (unsigned int)(c_stream.next_out - compr); - c_stream.avail_in = diff; - err = PREFIX(deflate)(&c_stream, Z_NO_FLUSH); - CHECK_ERR(err, "deflate large 2"); - - /* Switch back to compressing mode: */ - PREFIX(deflateParams)(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED); - c_stream.next_in = uncompr; - c_stream.avail_in = (unsigned int)uncomprLen; - err = PREFIX(deflate)(&c_stream, Z_NO_FLUSH); - CHECK_ERR(err, "deflate large 3"); - - err = PREFIX(deflate)(&c_stream, Z_FINISH); - if (err != Z_STREAM_END) { - fprintf(stderr, "deflate large should report Z_STREAM_END\n"); - exit(1); - } - err = PREFIX(deflateEnd)(&c_stream); - CHECK_ERR(err, "deflateEnd"); +void test_large_deflate(unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen) { + PREFIX3(stream) c_stream; /* compression stream */ + int err; + + c_stream.zalloc = zalloc; + c_stream.zfree = zfree; + c_stream.opaque = (void *)0; + + err = PREFIX(deflateInit)(&c_stream, Z_BEST_COMPRESSION); + CHECK_ERR(err, "deflateInit"); + + c_stream.next_out = compr; + c_stream.avail_out = (unsigned int)comprLen; + + /* At this point, uncompr is still mostly zeroes, so it should compress + * very well: + */ + c_stream.next_in = uncompr; + c_stream.avail_in = (unsigned int)uncomprLen; + err = PREFIX(deflate)(&c_stream, Z_NO_FLUSH); + CHECK_ERR(err, "deflate large 1"); + if (c_stream.avail_in != 0) { + fprintf(stderr, "deflate not greedy\n"); + exit(1); + } + + /* Feed in already compressed data and switch to no compression: */ + PREFIX(deflateParams)(&c_stream, Z_NO_COMPRESSION, Z_DEFAULT_STRATEGY); + c_stream.next_in = compr; + diff = (unsigned int)(c_stream.next_out - compr); + c_stream.avail_in = diff; + err = PREFIX(deflate)(&c_stream, Z_NO_FLUSH); + CHECK_ERR(err, "deflate large 2"); + + /* Switch back to compressing mode: */ + PREFIX(deflateParams)(&c_stream, Z_BEST_COMPRESSION, Z_FILTERED); + c_stream.next_in = uncompr; + c_stream.avail_in = (unsigned int)uncomprLen; + err = PREFIX(deflate)(&c_stream, Z_NO_FLUSH); + CHECK_ERR(err, "deflate large 3"); + + err = PREFIX(deflate)(&c_stream, Z_FINISH); + if (err != Z_STREAM_END) { + fprintf(stderr, "deflate large should report Z_STREAM_END\n"); + exit(1); + } + err = PREFIX(deflateEnd)(&c_stream); + CHECK_ERR(err, "deflateEnd"); } /* =========================================================================== * Test inflate() with large buffers */ -void test_large_inflate(unsigned char *compr, size_t comprLen, - unsigned char *uncompr, size_t uncomprLen) { - int err; - PREFIX3(stream) d_stream; /* decompression stream */ - - d_stream.zalloc = zalloc; - d_stream.zfree = zfree; - d_stream.opaque = (void *)0; - - d_stream.next_in = compr; - d_stream.avail_in = (unsigned int)comprLen; - - err = PREFIX(inflateInit)(&d_stream); - CHECK_ERR(err, "inflateInit"); - - for (;;) { - d_stream.next_out = uncompr; /* discard the output */ - d_stream.avail_out = (unsigned int)uncomprLen; - err = PREFIX(inflate)(&d_stream, Z_NO_FLUSH); - if (err == Z_STREAM_END) - break; - CHECK_ERR(err, "large inflate"); - } - - err = PREFIX(inflateEnd)(&d_stream); - CHECK_ERR(err, "inflateEnd"); - - if (d_stream.total_out != 2 * uncomprLen + diff) { - fprintf(stderr, "bad large inflate: %zu\n", d_stream.total_out); - exit(1); - } +void test_large_inflate(unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen) { + int err; + PREFIX3(stream) d_stream; /* decompression stream */ + + d_stream.zalloc = zalloc; + d_stream.zfree = zfree; + d_stream.opaque = (void *)0; + + d_stream.next_in = compr; + d_stream.avail_in = (unsigned int)comprLen; + + err = PREFIX(inflateInit)(&d_stream); + CHECK_ERR(err, "inflateInit"); + + for (;;) { + d_stream.next_out = uncompr; /* discard the output */ + d_stream.avail_out = (unsigned int)uncomprLen; + err = PREFIX(inflate)(&d_stream, Z_NO_FLUSH); + if (err == Z_STREAM_END) + break; + CHECK_ERR(err, "large inflate"); + } + + err = PREFIX(inflateEnd)(&d_stream); + CHECK_ERR(err, "inflateEnd"); + + if (d_stream.total_out != 2 * uncomprLen + diff) { + fprintf(stderr, "bad large inflate: %zu\n", d_stream.total_out); + exit(1); + } } int LLVMFuzzerTestOneInput(const uint8_t *d, size_t size) { - size_t comprLen = 100 + 3 * size; - size_t uncomprLen = comprLen; - uint8_t *compr, *uncompr; + size_t comprLen = 100 + 3 * size; + size_t uncomprLen = comprLen; + uint8_t *compr, *uncompr; - /* Discard inputs larger than 512Kb. */ - static size_t kMaxSize = 512 * 1024; + /* Discard inputs larger than 512Kb. */ + static size_t kMaxSize = 512 * 1024; - if (size < 1 || size > kMaxSize) - return 0; + if (size < 1 || size > kMaxSize) + return 0; - data = d; - dataLen = size; - compr = (uint8_t *)calloc(1, comprLen); - uncompr = (uint8_t *)calloc(1, uncomprLen); + data = d; + dataLen = size; + compr = (uint8_t *)calloc(1, comprLen); + uncompr = (uint8_t *)calloc(1, uncomprLen); - test_large_deflate(compr, comprLen, uncompr, uncomprLen); - test_large_inflate(compr, comprLen, uncompr, uncomprLen); + test_large_deflate(compr, comprLen, uncompr, uncomprLen); + test_large_inflate(compr, comprLen, uncompr, uncomprLen); - free(compr); - free(uncompr); + free(compr); + free(uncompr); - /* This function must return 0. */ - return 0; + /* This function must return 0. */ + return 0; } diff --git a/test/fuzz/example_small_fuzzer.c b/test/fuzz/example_small_fuzzer.c index d965c2db8f..f5caa97eb1 100644 --- a/test/fuzz/example_small_fuzzer.c +++ b/test/fuzz/example_small_fuzzer.c @@ -29,96 +29,95 @@ static free_func zfree = NULL; * Test deflate() with small buffers */ void test_deflate(unsigned char *compr, size_t comprLen) { - PREFIX3(stream) c_stream; /* compression stream */ - int err; - unsigned long len = dataLen; - - c_stream.zalloc = zalloc; - c_stream.zfree = zfree; - c_stream.opaque = (void *)0; - - err = PREFIX(deflateInit)(&c_stream, Z_DEFAULT_COMPRESSION); - CHECK_ERR(err, "deflateInit"); - - c_stream.next_in = (const unsigned char *)data; - c_stream.next_out = compr; - - while (c_stream.total_in != len && c_stream.total_out < comprLen) { - c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */ - err = PREFIX(deflate)(&c_stream, Z_NO_FLUSH); - CHECK_ERR(err, "deflate small 1"); - } - /* Finish the stream, still forcing small buffers: */ - for (;;) { - c_stream.avail_out = 1; - err = PREFIX(deflate)(&c_stream, Z_FINISH); - if (err == Z_STREAM_END) - break; - CHECK_ERR(err, "deflate small 2"); - } - - err = PREFIX(deflateEnd)(&c_stream); - CHECK_ERR(err, "deflateEnd"); + PREFIX3(stream) c_stream; /* compression stream */ + int err; + unsigned long len = dataLen; + + c_stream.zalloc = zalloc; + c_stream.zfree = zfree; + c_stream.opaque = (void *)0; + + err = PREFIX(deflateInit)(&c_stream, Z_DEFAULT_COMPRESSION); + CHECK_ERR(err, "deflateInit"); + + c_stream.next_in = (const unsigned char *)data; + c_stream.next_out = compr; + + while (c_stream.total_in != len && c_stream.total_out < comprLen) { + c_stream.avail_in = c_stream.avail_out = 1; /* force small buffers */ + err = PREFIX(deflate)(&c_stream, Z_NO_FLUSH); + CHECK_ERR(err, "deflate small 1"); + } + /* Finish the stream, still forcing small buffers: */ + for (;;) { + c_stream.avail_out = 1; + err = PREFIX(deflate)(&c_stream, Z_FINISH); + if (err == Z_STREAM_END) + break; + CHECK_ERR(err, "deflate small 2"); + } + + err = PREFIX(deflateEnd)(&c_stream); + CHECK_ERR(err, "deflateEnd"); } /* =========================================================================== * Test inflate() with small buffers */ -void test_inflate(unsigned char *compr, size_t comprLen, unsigned char *uncompr, - size_t uncomprLen) { - int err; - PREFIX3(stream) d_stream; /* decompression stream */ - - d_stream.zalloc = zalloc; - d_stream.zfree = zfree; - d_stream.opaque = (void *)0; - - d_stream.next_in = compr; - d_stream.avail_in = 0; - d_stream.next_out = uncompr; - - err = PREFIX(inflateInit)(&d_stream); - CHECK_ERR(err, "inflateInit"); - - while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) { - d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */ - err = PREFIX(inflate)(&d_stream, Z_NO_FLUSH); - if (err == Z_STREAM_END) - break; - CHECK_ERR(err, "inflate"); - } - - err = PREFIX(inflateEnd)(&d_stream); - CHECK_ERR(err, "inflateEnd"); - - if (memcmp(uncompr, data, dataLen)) { - fprintf(stderr, "bad inflate\n"); - exit(1); - } +void test_inflate(unsigned char *compr, size_t comprLen, unsigned char *uncompr, size_t uncomprLen) { + int err; + PREFIX3(stream) d_stream; /* decompression stream */ + + d_stream.zalloc = zalloc; + d_stream.zfree = zfree; + d_stream.opaque = (void *)0; + + d_stream.next_in = compr; + d_stream.avail_in = 0; + d_stream.next_out = uncompr; + + err = PREFIX(inflateInit)(&d_stream); + CHECK_ERR(err, "inflateInit"); + + while (d_stream.total_out < uncomprLen && d_stream.total_in < comprLen) { + d_stream.avail_in = d_stream.avail_out = 1; /* force small buffers */ + err = PREFIX(inflate)(&d_stream, Z_NO_FLUSH); + if (err == Z_STREAM_END) + break; + CHECK_ERR(err, "inflate"); + } + + err = PREFIX(inflateEnd)(&d_stream); + CHECK_ERR(err, "inflateEnd"); + + if (memcmp(uncompr, data, dataLen)) { + fprintf(stderr, "bad inflate\n"); + exit(1); + } } int LLVMFuzzerTestOneInput(const uint8_t *d, size_t size) { - size_t comprLen = PREFIX(compressBound)(size); - size_t uncomprLen = size; - uint8_t *compr, *uncompr; + size_t comprLen = PREFIX(compressBound)(size); + size_t uncomprLen = size; + uint8_t *compr, *uncompr; - /* Discard inputs larger than 1Mb. */ - static size_t kMaxSize = 1024 * 1024; + /* Discard inputs larger than 1Mb. */ + static size_t kMaxSize = 1024 * 1024; - if (size < 1 || size > kMaxSize) - return 0; + if (size < 1 || size > kMaxSize) + return 0; - data = d; - dataLen = size; - compr = (uint8_t *)calloc(1, comprLen); - uncompr = (uint8_t *)calloc(1, uncomprLen); + data = d; + dataLen = size; + compr = (uint8_t *)calloc(1, comprLen); + uncompr = (uint8_t *)calloc(1, uncomprLen); - test_deflate(compr, comprLen); - test_inflate(compr, comprLen, uncompr, uncomprLen); + test_deflate(compr, comprLen); + test_inflate(compr, comprLen, uncompr, uncomprLen); - free(compr); - free(uncompr); + free(compr); + free(uncompr); - /* This function must return 0. */ - return 0; + /* This function must return 0. */ + return 0; } diff --git a/test/fuzz/minigzip_fuzzer.c b/test/fuzz/minigzip_fuzzer.c index 68ebf8c9ca..1a99fd7aac 100644 --- a/test/fuzz/minigzip_fuzzer.c +++ b/test/fuzz/minigzip_fuzzer.c @@ -65,13 +65,13 @@ static const char *prog = "minigzip_fuzzer"; void error (const char *msg); -void gz_compress (FILE *in, gzFile out); +void gz_compress (FILE *in, gzFile out); #ifdef USE_MMAP -int gz_compress_mmap (FILE *in, gzFile out); +int gz_compress_mmap (FILE *in, gzFile out); #endif -void gz_uncompress (gzFile in, FILE *out); -void file_compress (char *file, char *mode); -void file_uncompress (char *file); +void gz_uncompress (gzFile in, FILE *out); +void file_compress (char *file, char *mode); +void file_uncompress (char *file); int main (int argc, char *argv[]); /* =========================================================================== @@ -87,7 +87,7 @@ void error(const char *msg) * Compress input to output then close both files. */ -void gz_compress(FILE *in, gzFile out) +void gz_compress(FILE *in, gzFile out) { char buf[BUFLEN]; int len; @@ -121,7 +121,7 @@ void gz_compress(FILE *in, gzFile out) /* Try compressing the input file at once using mmap. Return Z_OK if * if success, Z_ERRNO otherwise. */ -int gz_compress_mmap(FILE *in, gzFile out) +int gz_compress_mmap(FILE *in, gzFile out) { int len; int err; @@ -154,7 +154,7 @@ int gz_compress_mmap(FILE *in, gzFile out) /* =========================================================================== * Uncompress input to output then close both files. */ -void gz_uncompress(gzFile in, FILE *out) +void gz_uncompress(gzFile in, FILE *out) { char buf[BUFLENW]; int len; @@ -179,7 +179,7 @@ void gz_uncompress(gzFile in, FILE *out) * Compress the given file: create a corresponding .gz file and remove the * original. */ -void file_compress(char *file, char *mode) +void file_compress(char *file, char *mode) { char outfile[MAX_NAME_LEN]; FILE *in; @@ -211,7 +211,7 @@ void file_compress(char *file, char *mode) /* =========================================================================== * Uncompress the given file and remove the original. */ -void file_uncompress(char *file) +void file_uncompress(char *file) { char buf[MAX_NAME_LEN]; char *infile, *outfile; @@ -252,75 +252,75 @@ void file_uncompress(char *file) } int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataLen) { - char *inFileName = "minigzip_fuzzer.out"; - char *outFileName = "minigzip_fuzzer.out.gz"; - char outmode[20]; - FILE *in; - char buf[BUFLEN]; - uint32_t offset = 0; - - /* Discard inputs larger than 1Mb. */ - static size_t kMaxSize = 1024 * 1024; - if (dataLen < 1 || dataLen > kMaxSize) - return 0; + char *inFileName = "minigzip_fuzzer.out"; + char *outFileName = "minigzip_fuzzer.out.gz"; + char outmode[20]; + FILE *in; + char buf[BUFLEN]; + uint32_t offset = 0; + + /* Discard inputs larger than 1Mb. */ + static size_t kMaxSize = 1024 * 1024; + if (dataLen < 1 || dataLen > kMaxSize) + return 0; + + in = fopen(inFileName, "wb"); + if (fwrite(data, 1, (unsigned)dataLen, in) != dataLen) + error("failed fwrite"); + if (fclose(in)) + error("failed fclose"); + + memset(outmode, 0, sizeof(outmode)); + snprintf(outmode, sizeof(outmode), "%s", "wb"); + + /* Compression level: [0..9]. */ + outmode[2] = data[0] % 10; + + switch (data[0] % 4) { + default: + case 0: + outmode[3] = 0; + break; + case 1: + /* compress with Z_FILTERED */ + outmode[3] = 'f'; + break; + case 2: + /* compress with Z_HUFFMAN_ONLY */ + outmode[3] = 'h'; + break; + case 3: + /* compress with Z_RLE */ + outmode[3] = 'R'; + break; + } - in = fopen(inFileName, "wb"); - if (fwrite(data, 1, (unsigned)dataLen, in) != dataLen) - error("failed fwrite"); - if (fclose(in)) - error("failed fclose"); - - memset(outmode, 0, sizeof(outmode)); - snprintf(outmode, sizeof(outmode), "%s", "wb"); - - /* Compression level: [0..9]. */ - outmode[2] = data[0] % 10; - - switch (data[0] % 4) { - default: - case 0: - outmode[3] = 0; - break; - case 1: - /* compress with Z_FILTERED */ - outmode[3] = 'f'; - break; - case 2: - /* compress with Z_HUFFMAN_ONLY */ - outmode[3] = 'h'; - break; - case 3: - /* compress with Z_RLE */ - outmode[3] = 'R'; - break; - } - - file_compress(inFileName, outmode); - file_uncompress(outFileName); - - /* Check that the uncompressed file matches the input data. */ - in = fopen(inFileName, "rb"); - if (in == NULL) { - perror(inFileName); - exit(1); - } - - memset(buf, 0, sizeof(buf)); - for (;;) { - int len = (int)fread(buf, 1, sizeof(buf), in); - if (ferror(in)) { - perror("fread"); - exit(1); + file_compress(inFileName, outmode); + file_uncompress(outFileName); + + /* Check that the uncompressed file matches the input data. */ + in = fopen(inFileName, "rb"); + if (in == NULL) { + perror(inFileName); + exit(1); + } + + memset(buf, 0, sizeof(buf)); + for (;;) { + int len = (int)fread(buf, 1, sizeof(buf), in); + if (ferror(in)) { + perror("fread"); + exit(1); + } + if (len == 0) + break; + assert(0 == memcmp(data + offset, buf, len)); + offset += len; } - if (len == 0) - break; - assert(0 == memcmp(data + offset, buf, len)); - offset += len; - } - if (fclose(in)) - error("failed fclose"); + if (fclose(in)) + error("failed fclose"); - /* This function must return 0. */ - return 0; + /* This function must return 0. */ + return 0; } diff --git a/test/fuzz/standalone_fuzz_target_runner.c b/test/fuzz/standalone_fuzz_target_runner.c index 91144efb24..a291b48823 100644 --- a/test/fuzz/standalone_fuzz_target_runner.c +++ b/test/fuzz/standalone_fuzz_target_runner.c @@ -5,32 +5,32 @@ extern int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size); int main(int argc, char **argv) { - int i; - fprintf(stderr, "StandaloneFuzzTargetMain: running %d inputs\n", argc - 1); + int i; + fprintf(stderr, "StandaloneFuzzTargetMain: running %d inputs\n", argc - 1); - for (i = 1; i < argc; i++) { - size_t len, n_read, err; - unsigned char *buf; - FILE *f = fopen(argv[i], "rb+"); - if (!f) { - /* Failed to open this file: it may be a directory. */ - fprintf(stderr, "Skipping: %s\n", argv[i]); - continue; + for (i = 1; i < argc; i++) { + size_t len, n_read, err; + unsigned char *buf; + FILE *f = fopen(argv[i], "rb+"); + if (!f) { + /* Failed to open this file: it may be a directory. */ + fprintf(stderr, "Skipping: %s\n", argv[i]); + continue; + } + fprintf(stderr, "Running: %s %s\n", argv[0], argv[i]); + fseek(f, 0, SEEK_END); + len = ftell(f); + fseek(f, 0, SEEK_SET); + buf = (unsigned char *)malloc(len); + n_read = fread(buf, 1, len, f); + assert(n_read == len); + LLVMFuzzerTestOneInput(buf, len); + free(buf); + err = fclose(f); + assert(err == 0); + (void)err; + fprintf(stderr, "Done: %s: (%d bytes)\n", argv[i], (int)n_read); } - fprintf(stderr, "Running: %s %s\n", argv[0], argv[i]); - fseek(f, 0, SEEK_END); - len = ftell(f); - fseek(f, 0, SEEK_SET); - buf = (unsigned char *)malloc(len); - n_read = fread(buf, 1, len, f); - assert(n_read == len); - LLVMFuzzerTestOneInput(buf, len); - free(buf); - err = fclose(f); - assert(err == 0); - (void)err; - fprintf(stderr, "Done: %s: (%d bytes)\n", argv[i], (int)n_read); - } - return 0; + return 0; } |
