[Library] Update zlibng (#1255)

* Update zlibng

* Set cmake path more directly in zlibng to hopefully fix an issue with the build on drone

* I'm dumb, missing / in path

* Mackal helps with a dumb gitignore issue

* Adding all the files, not sure what's ignoring them and im tired of looking

* Some tweaks to zlibng build to hopefully get it to build properly. works on msvc now
This commit is contained in:
Alex
2021-02-23 17:00:26 -08:00
committed by GitHub
parent e6dee96266
commit 2957f5084d
184 changed files with 21431 additions and 11703 deletions
+67 -54
View File
@@ -14,60 +14,73 @@
#endif
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataLen) {
uint32_t crc0 = PREFIX(crc32)(0L, NULL, 0);
uint32_t crc1 = crc0;
uint32_t crc2 = crc0;
uint32_t adler0 = PREFIX(adler32)(0L, NULL, 0);
uint32_t adler1 = adler0;
uint32_t adler2 = adler0;
/* Checksum with a buffer of size equal to the first byte in the input. */
uint32_t buffSize = data[0];
uint32_t offset = 0;
uint32_t op[32];
uint32_t crc0 = PREFIX(crc32)(0L, NULL, 0);
uint32_t crc1 = crc0;
uint32_t crc2 = crc0;
uint32_t adler0 = PREFIX(adler32)(0L, NULL, 0);
uint32_t adler1 = adler0;
uint32_t adler2 = adler0;
uint32_t combine1, combine2;
/* Checksum with a buffer of size equal to the first byte in the input. */
uint32_t buffSize = data[0];
uint32_t offset = 0;
uint32_t op[32];
/* Discard inputs larger than 1Mb. */
static size_t kMaxSize = 1024 * 1024;
if (dataLen < 1 || dataLen > kMaxSize)
/* Discard inputs larger than 1Mb. */
static size_t kMaxSize = 1024 * 1024;
if (dataLen < 1 || dataLen > kMaxSize)
return 0;
/* Make sure the buffer has at least a byte. */
if (buffSize == 0)
++buffSize;
/* CRC32 */
PREFIX(crc32_combine_gen)(op, buffSize);
for (offset = 0; offset + buffSize <= dataLen; offset += buffSize) {
uint32_t crc3 = PREFIX(crc32_z)(crc0, data + offset, buffSize);
uint32_t crc4 = PREFIX(crc32_combine_op)(crc1, crc3, op);
crc1 = PREFIX(crc32_z)(crc1, data + offset, buffSize);
assert(crc1 == crc4);
(void)crc1;
(void)crc4;
}
crc1 = PREFIX(crc32_z)(crc1, data + offset, dataLen % buffSize);
crc2 = PREFIX(crc32_z)(crc2, data, dataLen);
assert(crc1 == crc2);
(void)crc1;
(void)crc2;
combine1 = PREFIX(crc32_combine)(crc1, crc2, (z_off_t)dataLen);
combine2 = PREFIX(crc32_combine)(crc1, crc1, (z_off_t)dataLen);
assert(combine1 == combine2);
/* Fast CRC32 combine. */
PREFIX(crc32_combine_gen)(op, (z_off_t)dataLen);
combine1 = PREFIX(crc32_combine_op)(crc1, crc2, op);
combine2 = PREFIX(crc32_combine_op)(crc2, crc1, op);
assert(combine1 == combine2);
combine1 = PREFIX(crc32_combine)(crc1, crc2, (z_off_t)dataLen);
combine2 = PREFIX(crc32_combine_op)(crc2, crc1, op);
assert(combine1 == combine2);
/* Adler32 */
for (offset = 0; offset + buffSize <= dataLen; offset += buffSize)
adler1 = PREFIX(adler32_z)(adler1, data + offset, buffSize);
adler1 = PREFIX(adler32_z)(adler1, data + offset, dataLen % buffSize);
adler2 = PREFIX(adler32_z)(adler2, data, dataLen);
assert(adler1 == adler2);
(void)adler1;
(void)adler2;
combine1 = PREFIX(adler32_combine)(adler1, adler2, (z_off_t)dataLen);
combine2 = PREFIX(adler32_combine)(adler1, adler1, (z_off_t)dataLen);
assert(combine1 == combine2);
(void)combine1;
(void)combine2;
/* This function must return 0. */
return 0;
/* Make sure the buffer has at least a byte. */
if (buffSize == 0)
++buffSize;
/* CRC32 */
PREFIX(crc32_combine_gen)(op, buffSize);
for (offset = 0; offset + buffSize <= dataLen; offset += buffSize) {
uint32_t crc3 = PREFIX(crc32_z)(crc0, data + offset, buffSize);
uint32_t crc4 = PREFIX(crc32_combine_op)(crc1, crc3, op);
crc1 = PREFIX(crc32_z)(crc1, data + offset, buffSize);
assert(crc1 == crc4);
}
crc1 = PREFIX(crc32_z)(crc1, data + offset, dataLen % buffSize);
crc2 = PREFIX(crc32_z)(crc2, data, dataLen);
assert(crc1 == crc2);
assert(PREFIX(crc32_combine)(crc1, crc2, dataLen) ==
PREFIX(crc32_combine)(crc1, crc1, dataLen));
/* Fast CRC32 combine. */
PREFIX(crc32_combine_gen)(op, dataLen);
assert(PREFIX(crc32_combine_op)(crc1, crc2, op) ==
PREFIX(crc32_combine_op)(crc2, crc1, op));
assert(PREFIX(crc32_combine)(crc1, crc2, dataLen) ==
PREFIX(crc32_combine_op)(crc2, crc1, op));
/* Adler32 */
for (offset = 0; offset + buffSize <= dataLen; offset += buffSize)
adler1 = PREFIX(adler32_z)(adler1, data + offset, buffSize);
adler1 = PREFIX(adler32_z)(adler1, data + offset, dataLen % buffSize);
adler2 = PREFIX(adler32_z)(adler2, data, dataLen);
assert(adler1 == adler2);
assert(PREFIX(adler32_combine)(adler1, adler2, dataLen) ==
PREFIX(adler32_combine)(adler1, adler1, dataLen));
/* This function must return 0. */
return 0;
}
+49 -49
View File
@@ -16,72 +16,72 @@
static const uint8_t *data;
static size_t dataLen;
static void check_compress_level(uint8_t *compr, size_t comprLen,
uint8_t *uncompr, size_t uncomprLen,
static void check_compress_level(uint8_t *compr, z_size_t comprLen,
uint8_t *uncompr, z_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, (header >> 8));
put_byte(s, 1, (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);
/* 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);
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. */
z_size_t comprLen = 100 + 2 * PREFIX(compressBound)(size);
z_size_t uncomprLen = (z_size_t)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)
if (size < 1 || size > kMaxSize)
return 0;
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_decompress(compr, comprLen);
free(compr);
free(uncompr);
/* This function must return 0. */
return 0;
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_decompress(compr, comprLen);
free(compr);
free(uncompr);
/* This function must return 0. */
return 0;
}
+62 -63
View File
@@ -24,14 +24,13 @@ static const uint8_t *data;
static size_t dataLen;
static alloc_func zalloc = NULL;
static free_func zfree = NULL;
static size_t dictionaryLen = 0;
static unsigned int dictionaryLen = 0;
static unsigned long dictId; /* Adler32 value of the dictionary */
/* ===========================================================================
* Test deflate() with preset dictionary
*/
void test_dict_deflate(unsigned char **compr, size_t *comprLen)
{
void test_dict_deflate(unsigned char **compr, size_t *comprLen) {
PREFIX3(stream) c_stream; /* compression stream */
int err;
int level = data[0] % 11 - 1; /* [-1..9]
@@ -58,7 +57,7 @@ void test_dict_deflate(unsigned char **compr, size_t *comprLen)
/* deflate would fail for no-compression or for speed levels. */
if (level == 0 || level == 1)
level = -1;
level = -1;
c_stream.zalloc = zalloc;
c_stream.zfree = zfree;
@@ -73,15 +72,15 @@ void test_dict_deflate(unsigned char **compr, size_t *comprLen)
CHECK_ERR(err, "deflateSetDictionary");
/* deflateBound does not provide enough space for low compression levels. */
*comprLen = 100 + 2 * PREFIX(deflateBound)(&c_stream, dataLen);
*comprLen = 100 + 2 * PREFIX(deflateBound)(&c_stream, (unsigned long)dataLen);
*compr = (uint8_t *)calloc(1, *comprLen);
dictId = c_stream.adler;
c_stream.next_out = *compr;
c_stream.avail_out = (unsigned int)(*comprLen);
c_stream.next_in = data;
c_stream.avail_in = dataLen;
c_stream.next_in = (z_const unsigned char *)data;
c_stream.avail_in = (uint32_t)dataLen;
err = PREFIX(deflate)(&c_stream, Z_FINISH);
if (err != Z_STREAM_END) {
@@ -96,75 +95,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;
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.zalloc = zalloc;
d_stream.zfree = zfree;
d_stream.opaque = (void *)0;
d_stream.next_in = compr;
d_stream.avail_in = (unsigned int)comprLen;
d_stream.next_in = compr;
d_stream.avail_in = (unsigned int)comprLen;
err = PREFIX(inflateInit)(&d_stream);
CHECK_ERR(err, "inflateInit");
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;
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);
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)
if (size < 1 || size > kMaxSize)
return 0;
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 = (unsigned int)dataLen;
test_dict_deflate(&compr, &comprLen);
test_dict_inflate(compr, comprLen);
free(compr);
/* This function must return 0. */
return 0;
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;
test_dict_deflate(&compr, &comprLen);
test_dict_inflate(compr, comprLen);
free(compr);
/* This function must return 0. */
return 0;
}
+68 -70
View File
@@ -29,98 +29,96 @@ 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;
PREFIX3(stream) c_stream; /* compression stream */
int err;
unsigned int len = (unsigned int)dataLen;
c_stream.zalloc = zalloc;
c_stream.zfree = zfree;
c_stream.opaque = (void *)0;
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");
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");
c_stream.next_in = (z_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;
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");
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;
*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 */
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.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 */
d_stream.next_in = compr;
d_stream.avail_in = 2; /* just read the zlib header */
err = PREFIX(inflateInit)(&d_stream);
CHECK_ERR(err, "inflateInit");
err = PREFIX(inflateInit)(&d_stream);
CHECK_ERR(err, "inflateInit");
d_stream.next_out = uncompr;
d_stream.avail_out = (unsigned int)uncomprLen;
d_stream.next_out = uncompr;
d_stream.avail_out = (unsigned int)uncomprLen;
err = PREFIX(inflate)(&d_stream, Z_NO_FLUSH);
CHECK_ERR(err, "inflate");
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");
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");
err = PREFIX(inflate)(&d_stream, Z_FINISH);
if (err != Z_STREAM_END) {
fprintf(stderr, "inflate should report Z_STREAM_END\n");
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;
z_size_t comprLen = 100 + 2 * PREFIX(compressBound)(size);
z_size_t uncomprLen = (z_size_t)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)
// 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);
test_flush(compr, &comprLen);
test_sync(compr, comprLen, uncompr, uncomprLen);
free(compr);
free(uncompr);
/* This function must return 0. */
return 0;
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);
free(compr);
free(uncompr);
/* This function must return 0. */
return 0;
}
+85 -87
View File
@@ -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;
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;
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");
err = PREFIX(deflateInit)(&c_stream, Z_BEST_COMPRESSION);
CHECK_ERR(err, "deflateInit");
c_stream.next_out = compr;
c_stream.avail_out = (unsigned int)comprLen;
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);
}
/* 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");
/* 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");
/* 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");
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 */
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.zalloc = zalloc;
d_stream.zfree = zfree;
d_stream.opaque = (void *)0;
d_stream.next_in = compr;
d_stream.avail_in = (unsigned int)comprLen;
d_stream.next_in = compr;
d_stream.avail_in = (unsigned int)comprLen;
err = PREFIX(inflateInit)(&d_stream);
CHECK_ERR(err, "inflateInit");
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");
}
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");
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);
}
if (d_stream.total_out != 2 * uncomprLen + diff) {
fprintf(stderr, "bad large inflate: %" PRIu64 "\n", (uint64_t)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)
if (size < 1 || size > kMaxSize)
return 0;
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);
free(compr);
free(uncompr);
/* This function must return 0. */
return 0;
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);
free(compr);
free(uncompr);
/* This function must return 0. */
return 0;
}
+69 -70
View File
@@ -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;
PREFIX3(stream) c_stream; /* compression stream */
int err;
unsigned long len = (unsigned long)dataLen;
c_stream.zalloc = zalloc;
c_stream.zfree = zfree;
c_stream.opaque = (void *)0;
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");
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.next_in = (z_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");
}
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");
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 */
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.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;
d_stream.next_in = compr;
d_stream.avail_in = 0;
d_stream.next_out = uncompr;
err = PREFIX(inflateInit)(&d_stream);
CHECK_ERR(err, "inflateInit");
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");
}
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");
err = PREFIX(inflateEnd)(&d_stream);
CHECK_ERR(err, "inflateEnd");
if (memcmp(uncompr, data, dataLen)) {
fprintf(stderr, "bad inflate\n");
exit(1);
}
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)
if (size < 1 || size > kMaxSize)
return 0;
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);
free(compr);
free(uncompr);
/* This function must return 0. */
return 0;
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);
free(compr);
free(uncompr);
/* This function must return 0. */
return 0;
}
+87 -92
View File
@@ -12,13 +12,14 @@
* real thing.
*/
/* @(#) $Id$ */
#define _POSIX_SOURCE 1 /* This file needs POSIX for fileno(). */
#define _POSIX_C_SOURCE 200112 /* For snprintf(). */
#include "zbuild.h"
#ifdef ZLIB_COMPAT
# include "zlib.h"
# include "zlib.h"
#else
# include "zlib-ng.h"
# include "zlib-ng.h"
#endif
#include <stdio.h>
#include <assert.h>
@@ -35,7 +36,7 @@
# include <malloc.h>
#endif
#if defined(WIN32) || defined(__CYGWIN__)
#if defined(_WIN32) || defined(__CYGWIN__)
# include <fcntl.h>
# include <io.h>
# define SET_BINARY_MODE(file) setmode(fileno(file), O_BINARY)
@@ -48,8 +49,8 @@
#endif
#if !defined(Z_HAVE_UNISTD_H) && !defined(_LARGEFILE64_SOURCE)
#ifndef WIN32 /* unlink already in stdio.h for WIN32 */
extern int unlink (const char *);
#ifndef _WIN32 /* unlink already in stdio.h for Win32 */
extern int unlink (const char *);
#endif
#endif
@@ -62,23 +63,22 @@
#define BUFLENW (BUFLEN * 3) /* write buffer size */
#define MAX_NAME_LEN 1024
static char *prog;
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[]);
/* ===========================================================================
* Display error message and exit
*/
void error(const char *msg)
{
void error(const char *msg) {
fprintf(stderr, "%s: %s\n", prog, msg);
exit(1);
}
@@ -87,8 +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;
int err;
@@ -121,12 +120,11 @@ 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;
int ifd = fileno(in);
caddr_t buf; /* mmap'ed buffer for the entire input file */
char *buf; /* mmap'ed buffer for the entire input file */
off_t buf_len; /* length of the input file */
struct stat sb;
@@ -136,8 +134,8 @@ int gz_compress_mmap(FILE *in, gzFile out)
if (buf_len <= 0) return Z_ERRNO;
/* Now do the actual mmap: */
buf = mmap((caddr_t) 0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
if (buf == (caddr_t)(-1)) return Z_ERRNO;
buf = mmap((void *)0, buf_len, PROT_READ, MAP_SHARED, ifd, (off_t)0);
if (buf == (char *)(-1)) return Z_ERRNO;
/* Compress the whole file at once: */
len = PREFIX(gzwrite)(out, (char *)buf, (unsigned)buf_len);
@@ -154,8 +152,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;
int err;
@@ -179,10 +176,9 @@ 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;
FILE *in;
gzFile out;
if (strlen(file) + strlen(GZ_SUFFIX) >= sizeof(outfile)) {
@@ -211,11 +207,10 @@ 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;
FILE *out;
FILE *out;
gzFile in;
size_t len = strlen(file);
@@ -252,75 +247,75 @@ void file_uncompress(char *file)
}
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataLen) {
char *inFileName = "/tmp/minigzip_fuzzer.out";
char *outFileName = "/tmp/minigzip_fuzzer.out.gz";
char outmode[20];
FILE *in;
char buf[BUFLEN];
uint32_t offset = 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;
/* Discard inputs larger than 1Mb. */
static size_t kMaxSize = 1024 * 1024;
if (dataLen < 1 || dataLen > kMaxSize)
return 0;
in = fopen(inFileName, "w");
if (fwrite(data, 1, (unsigned)dataLen, in) != dataLen)
error("failed fwrite");
if (fclose(in))
error("failed fclose");
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");
memset(outmode, 0, sizeof(outmode));
snprintf(outmode, sizeof(outmode), "%s", "wb");
/* Compression level: [0..9]. */
outmode[2] = data[0] % 10;
/* 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);
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;
}
if (len == 0)
break;
assert(0 == memcmp(data + offset, buf, len));
offset += len;
}
if (fclose(in))
error("failed fclose");
file_compress(inFileName, outmode);
file_uncompress(outFileName);
/* This function must return 0. */
return 0;
/* 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 (fclose(in))
error("failed fclose");
/* This function must return 0. */
return 0;
}
@@ -5,31 +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);
fprintf(stderr, "Done: %s: (%d bytes)\n", argv[i], (int)n_read);
}
return 0;
return 0;
}