127b27ec6Sopenharmony_ci/** 227b27ec6Sopenharmony_ci * This fuzz target attempts to compress the fuzzed data with the simple 327b27ec6Sopenharmony_ci * compression function with an output buffer that may be too small to 427b27ec6Sopenharmony_ci * ensure that the compressor never crashes. 527b27ec6Sopenharmony_ci */ 627b27ec6Sopenharmony_ci 727b27ec6Sopenharmony_ci#include <stddef.h> 827b27ec6Sopenharmony_ci#include <stdint.h> 927b27ec6Sopenharmony_ci#include <stdlib.h> 1027b27ec6Sopenharmony_ci#include <string.h> 1127b27ec6Sopenharmony_ci 1227b27ec6Sopenharmony_ci#include "fuzz_helpers.h" 1327b27ec6Sopenharmony_ci#include "fuzz_data_producer.h" 1427b27ec6Sopenharmony_ci#include "lz4.h" 1527b27ec6Sopenharmony_ci#include "lz4hc.h" 1627b27ec6Sopenharmony_ci 1727b27ec6Sopenharmony_ciint LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) 1827b27ec6Sopenharmony_ci{ 1927b27ec6Sopenharmony_ci FUZZ_dataProducer_t *producer = FUZZ_dataProducer_create(data, size); 2027b27ec6Sopenharmony_ci size_t const dstCapacitySeed = FUZZ_dataProducer_retrieve32(producer); 2127b27ec6Sopenharmony_ci size_t const levelSeed = FUZZ_dataProducer_retrieve32(producer); 2227b27ec6Sopenharmony_ci size = FUZZ_dataProducer_remainingBytes(producer); 2327b27ec6Sopenharmony_ci 2427b27ec6Sopenharmony_ci size_t const dstCapacity = FUZZ_getRange_from_uint32(dstCapacitySeed, 0, size); 2527b27ec6Sopenharmony_ci int const level = FUZZ_getRange_from_uint32(levelSeed, LZ4HC_CLEVEL_MIN, LZ4HC_CLEVEL_MAX); 2627b27ec6Sopenharmony_ci 2727b27ec6Sopenharmony_ci char* const dst = (char*)malloc(dstCapacity); 2827b27ec6Sopenharmony_ci char* const rt = (char*)malloc(size); 2927b27ec6Sopenharmony_ci 3027b27ec6Sopenharmony_ci FUZZ_ASSERT(dst); 3127b27ec6Sopenharmony_ci FUZZ_ASSERT(rt); 3227b27ec6Sopenharmony_ci 3327b27ec6Sopenharmony_ci /* If compression succeeds it must round trip correctly. */ 3427b27ec6Sopenharmony_ci { 3527b27ec6Sopenharmony_ci int const dstSize = LZ4_compress_HC((const char*)data, dst, size, 3627b27ec6Sopenharmony_ci dstCapacity, level); 3727b27ec6Sopenharmony_ci if (dstSize > 0) { 3827b27ec6Sopenharmony_ci int const rtSize = LZ4_decompress_safe(dst, rt, dstSize, size); 3927b27ec6Sopenharmony_ci FUZZ_ASSERT_MSG(rtSize == size, "Incorrect regenerated size"); 4027b27ec6Sopenharmony_ci FUZZ_ASSERT_MSG(!memcmp(data, rt, size), "Corruption!"); 4127b27ec6Sopenharmony_ci } 4227b27ec6Sopenharmony_ci } 4327b27ec6Sopenharmony_ci 4427b27ec6Sopenharmony_ci if (dstCapacity > 0) { 4527b27ec6Sopenharmony_ci /* Compression succeeds and must round trip correctly. */ 4627b27ec6Sopenharmony_ci void* state = malloc(LZ4_sizeofStateHC()); 4727b27ec6Sopenharmony_ci FUZZ_ASSERT(state); 4827b27ec6Sopenharmony_ci int compressedSize = size; 4927b27ec6Sopenharmony_ci int const dstSize = LZ4_compress_HC_destSize(state, (const char*)data, 5027b27ec6Sopenharmony_ci dst, &compressedSize, 5127b27ec6Sopenharmony_ci dstCapacity, level); 5227b27ec6Sopenharmony_ci FUZZ_ASSERT(dstSize > 0); 5327b27ec6Sopenharmony_ci int const rtSize = LZ4_decompress_safe(dst, rt, dstSize, size); 5427b27ec6Sopenharmony_ci FUZZ_ASSERT_MSG(rtSize == compressedSize, "Incorrect regenerated size"); 5527b27ec6Sopenharmony_ci FUZZ_ASSERT_MSG(!memcmp(data, rt, compressedSize), "Corruption!"); 5627b27ec6Sopenharmony_ci free(state); 5727b27ec6Sopenharmony_ci } 5827b27ec6Sopenharmony_ci 5927b27ec6Sopenharmony_ci free(dst); 6027b27ec6Sopenharmony_ci free(rt); 6127b27ec6Sopenharmony_ci FUZZ_dataProducer_free(producer); 6227b27ec6Sopenharmony_ci 6327b27ec6Sopenharmony_ci return 0; 6427b27ec6Sopenharmony_ci} 65