From 4b7c09e0379485bcaaa6070c8d58274048881588 Mon Sep 17 00:00:00 2001 From: sebpop Date: Wed, 14 Nov 2018 16:00:16 -0600 Subject: [PATCH] [zlib] add test for fast crc32 (#1954) Add a check for the recently added routines to combine crc32 checksums. This change adds more code coverage, however the checksum_fuzzer still does not pass the complexity check: (the threshold is at 100) oss-fuzz$ build/out/zlib/checksum_fuzzer -runs=10 2>&1 | grep -Po "INFO: Loaded [[:digit:]]+ module.*\(.*(counters|guards)\):[[:space:]]+\K[[:digit:]]+" 92 --- projects/zlib/checksum_fuzzer.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/projects/zlib/checksum_fuzzer.c b/projects/zlib/checksum_fuzzer.c index 4664e05e7..b14ba6fc3 100644 --- a/projects/zlib/checksum_fuzzer.c +++ b/projects/zlib/checksum_fuzzer.c @@ -8,13 +8,16 @@ #include "zlib.h" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataLen) { - uint32_t crc1 = crc32(0L, NULL, 0); - uint32_t crc2 = crc32(0L, NULL, 0); - uint32_t adler1 = adler32(0L, NULL, 0); - uint32_t adler2 = adler32(0L, NULL, 0);; + uint32_t crc0 = crc32(0L, NULL, 0); + uint32_t crc1 = crc0; + uint32_t crc2 = crc0; + uint32_t adler0 = 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; + z_crc_t op[32]; /* Discard inputs larger than 1Mb. */ static size_t kMaxSize = 1024 * 1024; @@ -26,22 +29,34 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t dataLen) { ++buffSize; /* CRC32 */ - for (offset = 0; offset + buffSize <= dataLen; offset += buffSize) + crc32_combine_gen(op, buffSize); + for (offset = 0; offset + buffSize <= dataLen; offset += buffSize) { + uint32_t crc3 = crc32_z(crc0, data + offset, buffSize); + uint32_t crc4 = crc32_combine_op(crc1, crc3, op); crc1 = crc32_z(crc1, data + offset, buffSize); + assert(crc1 == crc4); + } crc1 = crc32_z(crc1, data + offset, dataLen % buffSize); - crc2 = crc32_z(crc2, data, dataLen); + crc2 = crc32(crc2, data, (uint32_t) dataLen); assert(crc1 == crc2); assert(crc32_combine(crc1, crc2, dataLen) == crc32_combine(crc1, crc1, dataLen)); + /* Fast CRC32 combine. */ + crc32_combine_gen(op, dataLen); + assert(crc32_combine_op(crc1, crc2, op) == + crc32_combine_op(crc2, crc1, op)); + assert(crc32_combine(crc1, crc2, dataLen) == + crc32_combine_op(crc2, crc1, op)); + /* Adler32 */ for (offset = 0; offset + buffSize <= dataLen; offset += buffSize) adler1 = adler32_z(adler1, data + offset, buffSize); adler1 = adler32_z(adler1, data + offset, dataLen % buffSize); - adler2 = adler32_z(adler2, data, dataLen); + adler2 = adler32(adler2, data, (uint32_t) dataLen); assert(adler1 == adler2); assert(adler32_combine(adler1, adler2, dataLen) ==