diff --git a/tests/fuzz/igzip_checked_inflate_fuzz_test.c b/tests/fuzz/igzip_checked_inflate_fuzz_test.c index a306066..2cd7b7a 100644 --- a/tests/fuzz/igzip_checked_inflate_fuzz_test.c +++ b/tests/fuzz/igzip_checked_inflate_fuzz_test.c @@ -6,71 +6,72 @@ #include #include "igzip_lib.h" -int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size) +int +LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - struct inflate_state state; - z_stream zstate; - size_t out_buf_size = 2 * size; - int zret, iret; - char z_msg_invalid_code_set[] = "invalid code lengths set"; - char z_msg_invalid_dist_set[] = "invalid distances set"; - char z_msg_invalid_lit_len_set[] = "invalid literal/lengths set"; + struct inflate_state state; + z_stream zstate; + size_t out_buf_size = 2 * size; + int zret, iret; + char z_msg_invalid_code_set[] = "invalid code lengths set"; + char z_msg_invalid_dist_set[] = "invalid distances set"; + char z_msg_invalid_lit_len_set[] = "invalid literal/lengths set"; - uint8_t *isal_out_buf = (uint8_t *) malloc(size * 2); - uint8_t *zlib_out_buf = (uint8_t *) malloc(size * 2); + uint8_t *isal_out_buf = (uint8_t *) malloc(size * 2); + uint8_t *zlib_out_buf = (uint8_t *) malloc(size * 2); - assert(NULL != isal_out_buf && NULL != zlib_out_buf); + assert(NULL != isal_out_buf && NULL != zlib_out_buf); - /* Inflate data with isal_inflate */ - memset(&state, 0xff, sizeof(struct inflate_state)); + /* Inflate data with isal_inflate */ + memset(&state, 0xff, sizeof(struct inflate_state)); - isal_inflate_init(&state); - state.next_in = (uint8_t *) data; - state.avail_in = size; - state.next_out = isal_out_buf; - state.avail_out = out_buf_size; + isal_inflate_init(&state); + state.next_in = (uint8_t *) data; + state.avail_in = size; + state.next_out = isal_out_buf; + state.avail_out = out_buf_size; - iret = isal_inflate_stateless(&state); + iret = isal_inflate_stateless(&state); - /* Inflate data with zlib */ - zstate.zalloc = Z_NULL; - zstate.zfree = Z_NULL; - zstate.opaque = Z_NULL; - zstate.avail_in = size; - zstate.next_in = (Bytef *) data; - zstate.avail_out = out_buf_size; - zstate.next_out = zlib_out_buf; - if (inflateInit2(&zstate, -15) != Z_OK) { - free(isal_out_buf); - free(zlib_out_buf); - return -1; - } + /* Inflate data with zlib */ + zstate.zalloc = Z_NULL; + zstate.zfree = Z_NULL; + zstate.opaque = Z_NULL; + zstate.avail_in = size; + zstate.next_in = (Bytef *) data; + zstate.avail_out = out_buf_size; + zstate.next_out = zlib_out_buf; + if (inflateInit2(&zstate, -15) != Z_OK) { + free(isal_out_buf); + free(zlib_out_buf); + return -1; + } - zret = inflate(&zstate, Z_FINISH); + zret = inflate(&zstate, Z_FINISH); - if (zret == Z_STREAM_END) { - /* If zlib finished, assert isal finished with the same answer */ - assert(state.block_state == ISAL_BLOCK_FINISH); - assert(zstate.total_out == state.total_out); - assert(memcmp(isal_out_buf, zlib_out_buf, state.total_out) == 0); - } else if (zret < 0) { - if (zret != Z_BUF_ERROR) - /* If zlib errors, assert isal errors, excluding a few - * cases where zlib is overzealous and when zlib notices - * an error faster than isal */ - assert(iret < 0 || strcmp(zstate.msg, z_msg_invalid_code_set) == 0 - || strcmp(zstate.msg, z_msg_invalid_dist_set) == 0 - || strcmp(zstate.msg, z_msg_invalid_lit_len_set) == 0 - || (iret == ISAL_END_INPUT && zstate.avail_in < 3)); + if (zret == Z_STREAM_END) { + /* If zlib finished, assert isal finished with the same answer */ + assert(state.block_state == ISAL_BLOCK_FINISH); + assert(zstate.total_out == state.total_out); + assert(memcmp(isal_out_buf, zlib_out_buf, state.total_out) == 0); + } else if (zret < 0) { + if (zret != Z_BUF_ERROR) + /* If zlib errors, assert isal errors, excluding a few + * cases where zlib is overzealous and when zlib notices + * an error faster than isal */ + assert(iret < 0 || strcmp(zstate.msg, z_msg_invalid_code_set) == 0 || + strcmp(zstate.msg, z_msg_invalid_dist_set) == 0 || + strcmp(zstate.msg, z_msg_invalid_lit_len_set) == 0 || + (iret == ISAL_END_INPUT && zstate.avail_in < 3)); - } else - /* If zlib did not finish or error, assert isal did not finish - * or that isal found an invalid header since isal notices the - * error faster than zlib */ - assert(iret > 0 || iret == ISAL_INVALID_BLOCK); + } else + /* If zlib did not finish or error, assert isal did not finish + * or that isal found an invalid header since isal notices the + * error faster than zlib */ + assert(iret > 0 || iret == ISAL_INVALID_BLOCK); - inflateEnd(&zstate); - free(isal_out_buf); - free(zlib_out_buf); - return 0; + inflateEnd(&zstate); + free(isal_out_buf); + free(zlib_out_buf); + return 0; } diff --git a/tests/fuzz/igzip_dump_inflate_corpus.c b/tests/fuzz/igzip_dump_inflate_corpus.c index 36771b3..7110e26 100644 --- a/tests/fuzz/igzip_dump_inflate_corpus.c +++ b/tests/fuzz/igzip_dump_inflate_corpus.c @@ -6,35 +6,36 @@ #define DNAME_MAX 256 #define FNAME_MAX (DNAME_MAX + 81) -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { - uint8_t *buf; - int i, len, err; - FILE *fout = NULL; - char fname[FNAME_MAX]; - char dname[DNAME_MAX]; + uint8_t *buf; + int i, len, err; + FILE *fout = NULL; + char fname[FNAME_MAX]; + char dname[DNAME_MAX]; - if (argc != 2) { - fprintf(stderr, "Usage: %s \n", argv[0]); - exit(1); - } - strncpy(dname, argv[1], DNAME_MAX - 1); + if (argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(1); + } + strncpy(dname, argv[1], DNAME_MAX - 1); - for (i = 0; i < sizeof(std_vect_array) / sizeof(struct vect_result); i++) { - buf = std_vect_array[i].vector; - len = std_vect_array[i].vector_length; - err = std_vect_array[i].expected_error; + for (i = 0; i < sizeof(std_vect_array) / sizeof(struct vect_result); i++) { + buf = std_vect_array[i].vector; + len = std_vect_array[i].vector_length; + err = std_vect_array[i].expected_error; - snprintf(fname, FNAME_MAX, "%s/inflate_corp_n%04d_e%04d", dname, i, err); - printf(" writing %s\n", fname); - fout = fopen(fname, "w+"); - if (!fout) { - fprintf(stderr, "Can't open %s for writing\n", fname); - exit(1); - } - fwrite(buf, len, 1, fout); - fclose(fout); - } + snprintf(fname, FNAME_MAX, "%s/inflate_corp_n%04d_e%04d", dname, i, err); + printf(" writing %s\n", fname); + fout = fopen(fname, "w+"); + if (!fout) { + fprintf(stderr, "Can't open %s for writing\n", fname); + exit(1); + } + fwrite(buf, len, 1, fout); + fclose(fout); + } - return 0; + return 0; } diff --git a/tests/fuzz/igzip_fuzz_inflate.c b/tests/fuzz/igzip_fuzz_inflate.c index b28739d..bf23de2 100644 --- a/tests/fuzz/igzip_fuzz_inflate.c +++ b/tests/fuzz/igzip_fuzz_inflate.c @@ -7,35 +7,37 @@ #include "igzip_lib.h" #include "test.h" -extern int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size); +extern int +LLVMFuzzerTestOneInput(const uint8_t *data, size_t size); -int main(int argc, char *argv[]) +int +main(int argc, char *argv[]) { - FILE *in = NULL; - unsigned char *in_buf = NULL; - uint64_t in_file_size; + FILE *in = NULL; + unsigned char *in_buf = NULL; + uint64_t in_file_size; - if (argc != 2) { - fprintf(stderr, "Usage: isal_fuzz_inflate \n"); - exit(1); - } - in = fopen(argv[1], "rb"); - if (!in) { - fprintf(stderr, "Can't open %s for reading\n", argv[1]); - exit(1); - } - in_file_size = get_filesize(in); - in_buf = malloc(in_file_size); + if (argc != 2) { + fprintf(stderr, "Usage: isal_fuzz_inflate \n"); + exit(1); + } + in = fopen(argv[1], "rb"); + if (!in) { + fprintf(stderr, "Can't open %s for reading\n", argv[1]); + exit(1); + } + in_file_size = get_filesize(in); + in_buf = malloc(in_file_size); - if (in_buf == NULL) { - fprintf(stderr, "Failed to malloc input and outputs buffers\n"); - exit(1); - } + if (in_buf == NULL) { + fprintf(stderr, "Failed to malloc input and outputs buffers\n"); + exit(1); + } - if (fread(in_buf, 1, in_file_size, in) != in_file_size) { - fprintf(stderr, "Failed to read from %s\n", argv[1]); - exit(1); - } + if (fread(in_buf, 1, in_file_size, in) != in_file_size) { + fprintf(stderr, "Failed to read from %s\n", argv[1]); + exit(1); + } - return LLVMFuzzerTestOneInput(in_buf, in_file_size); + return LLVMFuzzerTestOneInput(in_buf, in_file_size); } diff --git a/tests/fuzz/igzip_simple_inflate_fuzz_test.c b/tests/fuzz/igzip_simple_inflate_fuzz_test.c index b5f22e8..e222da3 100644 --- a/tests/fuzz/igzip_simple_inflate_fuzz_test.c +++ b/tests/fuzz/igzip_simple_inflate_fuzz_test.c @@ -3,20 +3,21 @@ #include #include "igzip_lib.h" -int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size) +int +LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - struct inflate_state state; - uint8_t *isal_out_buf = (uint8_t *) (malloc(size * 2)); - size_t out_buf_size = 2 * size; + struct inflate_state state; + uint8_t *isal_out_buf = (uint8_t *) (malloc(size * 2)); + size_t out_buf_size = 2 * size; - isal_inflate_init(&state); - state.next_in = (uint8_t *) data; - state.avail_in = size; - state.next_out = isal_out_buf; - state.avail_out = out_buf_size; + isal_inflate_init(&state); + state.next_in = (uint8_t *) data; + state.avail_in = size; + state.next_out = isal_out_buf; + state.avail_out = out_buf_size; - isal_inflate_stateless(&state); + isal_inflate_stateless(&state); - free(isal_out_buf); - return 0; + free(isal_out_buf); + return 0; } diff --git a/tests/fuzz/igzip_simple_round_trip_fuzz_test.c b/tests/fuzz/igzip_simple_round_trip_fuzz_test.c index 83a7a88..7a4771d 100644 --- a/tests/fuzz/igzip_simple_round_trip_fuzz_test.c +++ b/tests/fuzz/igzip_simple_round_trip_fuzz_test.c @@ -11,120 +11,121 @@ #define HEADER_BITS 3 #define LVL_BUF_BITS 3 -#define LEVEL_BIT_MASK ((1< (y)) ? y : x ) +#define MIN(x, y) (((x) > (y)) ? y : x) const int header_size[] = { - 0, //IGZIP_DEFLATE - 10, //IGZIP_GZIP - 0, //IGZIP_GZIP_NO_HDR - 2, //IGZIP_ZLIB - 0, //IGZIP_ZLIB_NO_HDR + 0, // IGZIP_DEFLATE + 10, // IGZIP_GZIP + 0, // IGZIP_GZIP_NO_HDR + 2, // IGZIP_ZLIB + 0, // IGZIP_ZLIB_NO_HDR }; const int trailer_size[] = { - 0, //IGZIP_DEFLATE - 8, //IGZIP_GZIP - 8, //IGZIP_GZIP_NO_HDR - 4, //IGZIP_ZLIB - 4, //IGZIP_ZLIB_NO_HDR + 0, // IGZIP_DEFLATE + 8, // IGZIP_GZIP + 8, // IGZIP_GZIP_NO_HDR + 4, // IGZIP_ZLIB + 4, // IGZIP_ZLIB_NO_HDR }; -int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size) +int +LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - struct inflate_state istate; - struct isal_zstream cstate; - uint8_t *in_data = (uint8_t *) data; - int ret = 1; + struct inflate_state istate; + struct isal_zstream cstate; + uint8_t *in_data = (uint8_t *) data; + int ret = 1; - // Parameter default - int level = 1; - int lev_buf_size = ISAL_DEF_LVL1_DEFAULT; - int wrapper_type = 0; - size_t cmp_buf_size = size + ISAL_DEF_MAX_HDR_SIZE; + // Parameter default + int level = 1; + int lev_buf_size = ISAL_DEF_LVL1_DEFAULT; + int wrapper_type = 0; + size_t cmp_buf_size = size + ISAL_DEF_MAX_HDR_SIZE; - // Parameters are set by one byte of data input - if (size > 1) { - uint8_t in_param = in_data[--size]; - level = MIN(in_param & LEVEL_BIT_MASK, ISAL_DEF_MAX_LEVEL); - in_param >>= LEVEL_BITS; + // Parameters are set by one byte of data input + if (size > 1) { + uint8_t in_param = in_data[--size]; + level = MIN(in_param & LEVEL_BIT_MASK, ISAL_DEF_MAX_LEVEL); + in_param >>= LEVEL_BITS; - wrapper_type = (in_param & HEADER_BIT_MASK) % (IGZIP_ZLIB_NO_HDR + 1); - in_param >>= HEADER_BITS; + wrapper_type = (in_param & HEADER_BIT_MASK) % (IGZIP_ZLIB_NO_HDR + 1); + in_param >>= HEADER_BITS; - switch (level) { - case 0: - lev_buf_size = ISAL_DEF_LVL0_MIN + (in_param) * - (ISAL_DEF_LVL0_EXTRA_LARGE / LEVEL_BIT_MASK); - break; - case 1: - lev_buf_size = ISAL_DEF_LVL1_MIN + (in_param) * - (ISAL_DEF_LVL1_EXTRA_LARGE / LEVEL_BIT_MASK); - break; + switch (level) { + case 0: + lev_buf_size = ISAL_DEF_LVL0_MIN + + (in_param) * (ISAL_DEF_LVL0_EXTRA_LARGE / LEVEL_BIT_MASK); + break; + case 1: + lev_buf_size = ISAL_DEF_LVL1_MIN + + (in_param) * (ISAL_DEF_LVL1_EXTRA_LARGE / LEVEL_BIT_MASK); + break; #ifdef ISAL_DEF_LVL2_MIN - case 2: - lev_buf_size = ISAL_DEF_LVL2_MIN + (in_param) * - (ISAL_DEF_LVL2_EXTRA_LARGE / LEVEL_BIT_MASK); - break; + case 2: + lev_buf_size = ISAL_DEF_LVL2_MIN + + (in_param) * (ISAL_DEF_LVL2_EXTRA_LARGE / LEVEL_BIT_MASK); + break; #endif #ifdef ISAL_DEF_LVL3_MIN - case 3: - lev_buf_size = ISAL_DEF_LVL3_MIN + (in_param) * - (ISAL_DEF_LVL3_EXTRA_LARGE / LEVEL_BIT_MASK); - break; + case 3: + lev_buf_size = ISAL_DEF_LVL3_MIN + + (in_param) * (ISAL_DEF_LVL3_EXTRA_LARGE / LEVEL_BIT_MASK); + break; #endif - } - if (0 == level) - cmp_buf_size = 2 * size + ISAL_DEF_MAX_HDR_SIZE; - else - cmp_buf_size = size + 8 + (TYPE0_HDR_SIZE * (size / TYPE0_MAX_SIZE)); + } + if (0 == level) + cmp_buf_size = 2 * size + ISAL_DEF_MAX_HDR_SIZE; + else + cmp_buf_size = size + 8 + (TYPE0_HDR_SIZE * (size / TYPE0_MAX_SIZE)); - cmp_buf_size += header_size[wrapper_type] + trailer_size[wrapper_type]; - } + cmp_buf_size += header_size[wrapper_type] + trailer_size[wrapper_type]; + } - uint8_t *isal_cmp_buf = (uint8_t *) malloc(cmp_buf_size); - uint8_t *isal_out_buf = (uint8_t *) malloc(size); - uint8_t *isal_lev_buf = (uint8_t *) malloc(lev_buf_size); - assert(NULL != isal_cmp_buf || NULL != isal_out_buf || NULL != isal_lev_buf); + uint8_t *isal_cmp_buf = (uint8_t *) malloc(cmp_buf_size); + uint8_t *isal_out_buf = (uint8_t *) malloc(size); + uint8_t *isal_lev_buf = (uint8_t *) malloc(lev_buf_size); + assert(NULL != isal_cmp_buf || NULL != isal_out_buf || NULL != isal_lev_buf); - isal_deflate_init(&cstate); - cstate.end_of_stream = 1; - cstate.flush = NO_FLUSH; - cstate.next_in = in_data; - cstate.avail_in = size; - cstate.next_out = isal_cmp_buf; - cstate.avail_out = cmp_buf_size; - cstate.level = level; - cstate.level_buf = isal_lev_buf; - cstate.level_buf_size = lev_buf_size; - cstate.gzip_flag = wrapper_type; - ret = isal_deflate_stateless(&cstate); + isal_deflate_init(&cstate); + cstate.end_of_stream = 1; + cstate.flush = NO_FLUSH; + cstate.next_in = in_data; + cstate.avail_in = size; + cstate.next_out = isal_cmp_buf; + cstate.avail_out = cmp_buf_size; + cstate.level = level; + cstate.level_buf = isal_lev_buf; + cstate.level_buf_size = lev_buf_size; + cstate.gzip_flag = wrapper_type; + ret = isal_deflate_stateless(&cstate); - isal_inflate_init(&istate); - istate.next_in = isal_cmp_buf; - istate.avail_in = cstate.total_out; - istate.next_out = isal_out_buf; - istate.avail_out = size; - istate.crc_flag = wrapper_type; - ret |= isal_inflate_stateless(&istate); - ret |= memcmp(isal_out_buf, in_data, size); + isal_inflate_init(&istate); + istate.next_in = isal_cmp_buf; + istate.avail_in = cstate.total_out; + istate.next_out = isal_out_buf; + istate.avail_out = size; + istate.crc_flag = wrapper_type; + ret |= isal_inflate_stateless(&istate); + ret |= memcmp(isal_out_buf, in_data, size); - // Check trailer - uint32_t crc = 0; - int trailer_idx = cstate.total_out - trailer_size[wrapper_type]; + // Check trailer + uint32_t crc = 0; + int trailer_idx = cstate.total_out - trailer_size[wrapper_type]; - if (wrapper_type == IGZIP_GZIP || wrapper_type == IGZIP_GZIP_NO_HDR) - crc = load_le_u32(&isal_cmp_buf[trailer_idx]); - else if (wrapper_type == IGZIP_ZLIB || wrapper_type == IGZIP_ZLIB_NO_HDR) - crc = load_be_u32(&isal_cmp_buf[trailer_idx]); + if (wrapper_type == IGZIP_GZIP || wrapper_type == IGZIP_GZIP_NO_HDR) + crc = load_le_u32(&isal_cmp_buf[trailer_idx]); + else if (wrapper_type == IGZIP_ZLIB || wrapper_type == IGZIP_ZLIB_NO_HDR) + crc = load_be_u32(&isal_cmp_buf[trailer_idx]); - assert(istate.crc == crc); - free(isal_cmp_buf); - free(isal_out_buf); - free(isal_lev_buf); - return ret; + assert(istate.crc == crc); + free(isal_cmp_buf); + free(isal_out_buf); + free(isal_lev_buf); + return ret; }