tests: reformat using new code style

Signed-off-by: Marcel Cornu <marcel.d.cornu@intel.com>
This commit is contained in:
Marcel Cornu 2024-04-19 17:10:01 +01:00 committed by Pablo de Lara
parent ae951677ab
commit 9ab5a9e579
5 changed files with 217 additions and 211 deletions

View File

@ -6,71 +6,72 @@
#include <assert.h> #include <assert.h>
#include "igzip_lib.h" #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; struct inflate_state state;
z_stream zstate; z_stream zstate;
size_t out_buf_size = 2 * size; size_t out_buf_size = 2 * size;
int zret, iret; int zret, iret;
char z_msg_invalid_code_set[] = "invalid code lengths set"; char z_msg_invalid_code_set[] = "invalid code lengths set";
char z_msg_invalid_dist_set[] = "invalid distances set"; char z_msg_invalid_dist_set[] = "invalid distances set";
char z_msg_invalid_lit_len_set[] = "invalid literal/lengths set"; char z_msg_invalid_lit_len_set[] = "invalid literal/lengths set";
uint8_t *isal_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); 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 */ /* Inflate data with isal_inflate */
memset(&state, 0xff, sizeof(struct inflate_state)); memset(&state, 0xff, sizeof(struct inflate_state));
isal_inflate_init(&state); isal_inflate_init(&state);
state.next_in = (uint8_t *) data; state.next_in = (uint8_t *) data;
state.avail_in = size; state.avail_in = size;
state.next_out = isal_out_buf; state.next_out = isal_out_buf;
state.avail_out = out_buf_size; state.avail_out = out_buf_size;
iret = isal_inflate_stateless(&state); iret = isal_inflate_stateless(&state);
/* Inflate data with zlib */ /* Inflate data with zlib */
zstate.zalloc = Z_NULL; zstate.zalloc = Z_NULL;
zstate.zfree = Z_NULL; zstate.zfree = Z_NULL;
zstate.opaque = Z_NULL; zstate.opaque = Z_NULL;
zstate.avail_in = size; zstate.avail_in = size;
zstate.next_in = (Bytef *) data; zstate.next_in = (Bytef *) data;
zstate.avail_out = out_buf_size; zstate.avail_out = out_buf_size;
zstate.next_out = zlib_out_buf; zstate.next_out = zlib_out_buf;
if (inflateInit2(&zstate, -15) != Z_OK) { if (inflateInit2(&zstate, -15) != Z_OK) {
free(isal_out_buf); free(isal_out_buf);
free(zlib_out_buf); free(zlib_out_buf);
return -1; return -1;
} }
zret = inflate(&zstate, Z_FINISH); zret = inflate(&zstate, Z_FINISH);
if (zret == Z_STREAM_END) { if (zret == Z_STREAM_END) {
/* If zlib finished, assert isal finished with the same answer */ /* If zlib finished, assert isal finished with the same answer */
assert(state.block_state == ISAL_BLOCK_FINISH); assert(state.block_state == ISAL_BLOCK_FINISH);
assert(zstate.total_out == state.total_out); assert(zstate.total_out == state.total_out);
assert(memcmp(isal_out_buf, zlib_out_buf, state.total_out) == 0); assert(memcmp(isal_out_buf, zlib_out_buf, state.total_out) == 0);
} else if (zret < 0) { } else if (zret < 0) {
if (zret != Z_BUF_ERROR) if (zret != Z_BUF_ERROR)
/* If zlib errors, assert isal errors, excluding a few /* If zlib errors, assert isal errors, excluding a few
* cases where zlib is overzealous and when zlib notices * cases where zlib is overzealous and when zlib notices
* an error faster than isal */ * an error faster than isal */
assert(iret < 0 || strcmp(zstate.msg, z_msg_invalid_code_set) == 0 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_dist_set) == 0 ||
|| strcmp(zstate.msg, z_msg_invalid_lit_len_set) == 0 strcmp(zstate.msg, z_msg_invalid_lit_len_set) == 0 ||
|| (iret == ISAL_END_INPUT && zstate.avail_in < 3)); (iret == ISAL_END_INPUT && zstate.avail_in < 3));
} else } else
/* If zlib did not finish or error, assert isal did not finish /* If zlib did not finish or error, assert isal did not finish
* or that isal found an invalid header since isal notices the * or that isal found an invalid header since isal notices the
* error faster than zlib */ * error faster than zlib */
assert(iret > 0 || iret == ISAL_INVALID_BLOCK); assert(iret > 0 || iret == ISAL_INVALID_BLOCK);
inflateEnd(&zstate); inflateEnd(&zstate);
free(isal_out_buf); free(isal_out_buf);
free(zlib_out_buf); free(zlib_out_buf);
return 0; return 0;
} }

View File

@ -6,35 +6,36 @@
#define DNAME_MAX 256 #define DNAME_MAX 256
#define FNAME_MAX (DNAME_MAX + 81) #define FNAME_MAX (DNAME_MAX + 81)
int main(int argc, char *argv[]) int
main(int argc, char *argv[])
{ {
uint8_t *buf; uint8_t *buf;
int i, len, err; int i, len, err;
FILE *fout = NULL; FILE *fout = NULL;
char fname[FNAME_MAX]; char fname[FNAME_MAX];
char dname[DNAME_MAX]; char dname[DNAME_MAX];
if (argc != 2) { if (argc != 2) {
fprintf(stderr, "Usage: %s <outdir>\n", argv[0]); fprintf(stderr, "Usage: %s <outdir>\n", argv[0]);
exit(1); exit(1);
} }
strncpy(dname, argv[1], DNAME_MAX - 1); strncpy(dname, argv[1], DNAME_MAX - 1);
for (i = 0; i < sizeof(std_vect_array) / sizeof(struct vect_result); i++) { for (i = 0; i < sizeof(std_vect_array) / sizeof(struct vect_result); i++) {
buf = std_vect_array[i].vector; buf = std_vect_array[i].vector;
len = std_vect_array[i].vector_length; len = std_vect_array[i].vector_length;
err = std_vect_array[i].expected_error; err = std_vect_array[i].expected_error;
snprintf(fname, FNAME_MAX, "%s/inflate_corp_n%04d_e%04d", dname, i, err); snprintf(fname, FNAME_MAX, "%s/inflate_corp_n%04d_e%04d", dname, i, err);
printf(" writing %s\n", fname); printf(" writing %s\n", fname);
fout = fopen(fname, "w+"); fout = fopen(fname, "w+");
if (!fout) { if (!fout) {
fprintf(stderr, "Can't open %s for writing\n", fname); fprintf(stderr, "Can't open %s for writing\n", fname);
exit(1); exit(1);
} }
fwrite(buf, len, 1, fout); fwrite(buf, len, 1, fout);
fclose(fout); fclose(fout);
} }
return 0; return 0;
} }

View File

@ -7,35 +7,37 @@
#include "igzip_lib.h" #include "igzip_lib.h"
#include "test.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; FILE *in = NULL;
unsigned char *in_buf = NULL; unsigned char *in_buf = NULL;
uint64_t in_file_size; uint64_t in_file_size;
if (argc != 2) { if (argc != 2) {
fprintf(stderr, "Usage: isal_fuzz_inflate <infile>\n"); fprintf(stderr, "Usage: isal_fuzz_inflate <infile>\n");
exit(1); exit(1);
} }
in = fopen(argv[1], "rb"); in = fopen(argv[1], "rb");
if (!in) { if (!in) {
fprintf(stderr, "Can't open %s for reading\n", argv[1]); fprintf(stderr, "Can't open %s for reading\n", argv[1]);
exit(1); exit(1);
} }
in_file_size = get_filesize(in); in_file_size = get_filesize(in);
in_buf = malloc(in_file_size); in_buf = malloc(in_file_size);
if (in_buf == NULL) { if (in_buf == NULL) {
fprintf(stderr, "Failed to malloc input and outputs buffers\n"); fprintf(stderr, "Failed to malloc input and outputs buffers\n");
exit(1); exit(1);
} }
if (fread(in_buf, 1, in_file_size, in) != in_file_size) { if (fread(in_buf, 1, in_file_size, in) != in_file_size) {
fprintf(stderr, "Failed to read from %s\n", argv[1]); fprintf(stderr, "Failed to read from %s\n", argv[1]);
exit(1); exit(1);
} }
return LLVMFuzzerTestOneInput(in_buf, in_file_size); return LLVMFuzzerTestOneInput(in_buf, in_file_size);
} }

View File

@ -3,20 +3,21 @@
#include <stddef.h> #include <stddef.h>
#include "igzip_lib.h" #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; struct inflate_state state;
uint8_t *isal_out_buf = (uint8_t *) (malloc(size * 2)); uint8_t *isal_out_buf = (uint8_t *) (malloc(size * 2));
size_t out_buf_size = 2 * size; size_t out_buf_size = 2 * size;
isal_inflate_init(&state); isal_inflate_init(&state);
state.next_in = (uint8_t *) data; state.next_in = (uint8_t *) data;
state.avail_in = size; state.avail_in = size;
state.next_out = isal_out_buf; state.next_out = isal_out_buf;
state.avail_out = out_buf_size; state.avail_out = out_buf_size;
isal_inflate_stateless(&state); isal_inflate_stateless(&state);
free(isal_out_buf); free(isal_out_buf);
return 0; return 0;
} }

View File

@ -11,120 +11,121 @@
#define HEADER_BITS 3 #define HEADER_BITS 3
#define LVL_BUF_BITS 3 #define LVL_BUF_BITS 3
#define LEVEL_BIT_MASK ((1<<LEVEL_BITS) - 1) #define LEVEL_BIT_MASK ((1 << LEVEL_BITS) - 1)
#define HEADER_BIT_MASK ((1<<HEADER_BITS) - 1) #define HEADER_BIT_MASK ((1 << HEADER_BITS) - 1)
#define TYPE0_HDR_SIZE 5 #define TYPE0_HDR_SIZE 5
#define TYPE0_MAX_SIZE 65535 #define TYPE0_MAX_SIZE 65535
#define MIN(x,y) (((x) > (y)) ? y : x ) #define MIN(x, y) (((x) > (y)) ? y : x)
const int header_size[] = { const int header_size[] = {
0, //IGZIP_DEFLATE 0, // IGZIP_DEFLATE
10, //IGZIP_GZIP 10, // IGZIP_GZIP
0, //IGZIP_GZIP_NO_HDR 0, // IGZIP_GZIP_NO_HDR
2, //IGZIP_ZLIB 2, // IGZIP_ZLIB
0, //IGZIP_ZLIB_NO_HDR 0, // IGZIP_ZLIB_NO_HDR
}; };
const int trailer_size[] = { const int trailer_size[] = {
0, //IGZIP_DEFLATE 0, // IGZIP_DEFLATE
8, //IGZIP_GZIP 8, // IGZIP_GZIP
8, //IGZIP_GZIP_NO_HDR 8, // IGZIP_GZIP_NO_HDR
4, //IGZIP_ZLIB 4, // IGZIP_ZLIB
4, //IGZIP_ZLIB_NO_HDR 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 inflate_state istate;
struct isal_zstream cstate; struct isal_zstream cstate;
uint8_t *in_data = (uint8_t *) data; uint8_t *in_data = (uint8_t *) data;
int ret = 1; int ret = 1;
// Parameter default // Parameter default
int level = 1; int level = 1;
int lev_buf_size = ISAL_DEF_LVL1_DEFAULT; int lev_buf_size = ISAL_DEF_LVL1_DEFAULT;
int wrapper_type = 0; int wrapper_type = 0;
size_t cmp_buf_size = size + ISAL_DEF_MAX_HDR_SIZE; size_t cmp_buf_size = size + ISAL_DEF_MAX_HDR_SIZE;
// Parameters are set by one byte of data input // Parameters are set by one byte of data input
if (size > 1) { if (size > 1) {
uint8_t in_param = in_data[--size]; uint8_t in_param = in_data[--size];
level = MIN(in_param & LEVEL_BIT_MASK, ISAL_DEF_MAX_LEVEL); level = MIN(in_param & LEVEL_BIT_MASK, ISAL_DEF_MAX_LEVEL);
in_param >>= LEVEL_BITS; in_param >>= LEVEL_BITS;
wrapper_type = (in_param & HEADER_BIT_MASK) % (IGZIP_ZLIB_NO_HDR + 1); wrapper_type = (in_param & HEADER_BIT_MASK) % (IGZIP_ZLIB_NO_HDR + 1);
in_param >>= HEADER_BITS; in_param >>= HEADER_BITS;
switch (level) { switch (level) {
case 0: case 0:
lev_buf_size = ISAL_DEF_LVL0_MIN + (in_param) * lev_buf_size = ISAL_DEF_LVL0_MIN +
(ISAL_DEF_LVL0_EXTRA_LARGE / LEVEL_BIT_MASK); (in_param) * (ISAL_DEF_LVL0_EXTRA_LARGE / LEVEL_BIT_MASK);
break; break;
case 1: case 1:
lev_buf_size = ISAL_DEF_LVL1_MIN + (in_param) * lev_buf_size = ISAL_DEF_LVL1_MIN +
(ISAL_DEF_LVL1_EXTRA_LARGE / LEVEL_BIT_MASK); (in_param) * (ISAL_DEF_LVL1_EXTRA_LARGE / LEVEL_BIT_MASK);
break; break;
#ifdef ISAL_DEF_LVL2_MIN #ifdef ISAL_DEF_LVL2_MIN
case 2: case 2:
lev_buf_size = ISAL_DEF_LVL2_MIN + (in_param) * lev_buf_size = ISAL_DEF_LVL2_MIN +
(ISAL_DEF_LVL2_EXTRA_LARGE / LEVEL_BIT_MASK); (in_param) * (ISAL_DEF_LVL2_EXTRA_LARGE / LEVEL_BIT_MASK);
break; break;
#endif #endif
#ifdef ISAL_DEF_LVL3_MIN #ifdef ISAL_DEF_LVL3_MIN
case 3: case 3:
lev_buf_size = ISAL_DEF_LVL3_MIN + (in_param) * lev_buf_size = ISAL_DEF_LVL3_MIN +
(ISAL_DEF_LVL3_EXTRA_LARGE / LEVEL_BIT_MASK); (in_param) * (ISAL_DEF_LVL3_EXTRA_LARGE / LEVEL_BIT_MASK);
break; break;
#endif #endif
} }
if (0 == level) if (0 == level)
cmp_buf_size = 2 * size + ISAL_DEF_MAX_HDR_SIZE; cmp_buf_size = 2 * size + ISAL_DEF_MAX_HDR_SIZE;
else else
cmp_buf_size = size + 8 + (TYPE0_HDR_SIZE * (size / TYPE0_MAX_SIZE)); 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_cmp_buf = (uint8_t *) malloc(cmp_buf_size);
uint8_t *isal_out_buf = (uint8_t *) malloc(size); uint8_t *isal_out_buf = (uint8_t *) malloc(size);
uint8_t *isal_lev_buf = (uint8_t *) malloc(lev_buf_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); assert(NULL != isal_cmp_buf || NULL != isal_out_buf || NULL != isal_lev_buf);
isal_deflate_init(&cstate); isal_deflate_init(&cstate);
cstate.end_of_stream = 1; cstate.end_of_stream = 1;
cstate.flush = NO_FLUSH; cstate.flush = NO_FLUSH;
cstate.next_in = in_data; cstate.next_in = in_data;
cstate.avail_in = size; cstate.avail_in = size;
cstate.next_out = isal_cmp_buf; cstate.next_out = isal_cmp_buf;
cstate.avail_out = cmp_buf_size; cstate.avail_out = cmp_buf_size;
cstate.level = level; cstate.level = level;
cstate.level_buf = isal_lev_buf; cstate.level_buf = isal_lev_buf;
cstate.level_buf_size = lev_buf_size; cstate.level_buf_size = lev_buf_size;
cstate.gzip_flag = wrapper_type; cstate.gzip_flag = wrapper_type;
ret = isal_deflate_stateless(&cstate); ret = isal_deflate_stateless(&cstate);
isal_inflate_init(&istate); isal_inflate_init(&istate);
istate.next_in = isal_cmp_buf; istate.next_in = isal_cmp_buf;
istate.avail_in = cstate.total_out; istate.avail_in = cstate.total_out;
istate.next_out = isal_out_buf; istate.next_out = isal_out_buf;
istate.avail_out = size; istate.avail_out = size;
istate.crc_flag = wrapper_type; istate.crc_flag = wrapper_type;
ret |= isal_inflate_stateless(&istate); ret |= isal_inflate_stateless(&istate);
ret |= memcmp(isal_out_buf, in_data, size); ret |= memcmp(isal_out_buf, in_data, size);
// Check trailer // Check trailer
uint32_t crc = 0; uint32_t crc = 0;
int trailer_idx = cstate.total_out - trailer_size[wrapper_type]; int trailer_idx = cstate.total_out - trailer_size[wrapper_type];
if (wrapper_type == IGZIP_GZIP || wrapper_type == IGZIP_GZIP_NO_HDR) if (wrapper_type == IGZIP_GZIP || wrapper_type == IGZIP_GZIP_NO_HDR)
crc = load_le_u32(&isal_cmp_buf[trailer_idx]); crc = load_le_u32(&isal_cmp_buf[trailer_idx]);
else if (wrapper_type == IGZIP_ZLIB || wrapper_type == IGZIP_ZLIB_NO_HDR) else if (wrapper_type == IGZIP_ZLIB || wrapper_type == IGZIP_ZLIB_NO_HDR)
crc = load_be_u32(&isal_cmp_buf[trailer_idx]); crc = load_be_u32(&isal_cmp_buf[trailer_idx]);
assert(istate.crc == crc); assert(istate.crc == crc);
free(isal_cmp_buf); free(isal_cmp_buf);
free(isal_out_buf); free(isal_out_buf);
free(isal_lev_buf); free(isal_lev_buf);
return ret; return ret;
} }