Rizin
unix-like reverse engineering framework and cli tools
compress_fuzzer.c File Reference
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "fuzz_helpers.h"
#include "fuzz_data_producer.h"
#include "lz4.h"

Go to the source code of this file.

Functions

int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size)
 

Function Documentation

◆ LLVMFuzzerTestOneInput()

int LLVMFuzzerTestOneInput ( const uint8_t data,
size_t  size 
)

This fuzz target attempts to compress the fuzzed data with the simple compression function with an output buffer that may be too small to ensure that the compressor never crashes.

Definition at line 16 of file compress_fuzzer.c.

17 {
19  size_t const dstCapacitySeed = FUZZ_dataProducer_retrieve32(producer);
21 
22  size_t const compressBound = LZ4_compressBound(size);
23  size_t const dstCapacity = FUZZ_getRange_from_uint32(dstCapacitySeed, 0, compressBound);
24 
25  char* const dst = (char*)malloc(dstCapacity);
26  char* const rt = (char*)malloc(size);
27 
29  FUZZ_ASSERT(rt);
30 
31  /* If compression succeeds it must round trip correctly. */
32  {
33  int const dstSize = LZ4_compress_default((const char*)data, dst,
34  size, dstCapacity);
35  if (dstSize > 0) {
36  int const rtSize = LZ4_decompress_safe(dst, rt, dstSize, size);
37  FUZZ_ASSERT_MSG(rtSize == size, "Incorrect regenerated size");
38  FUZZ_ASSERT_MSG(!memcmp(data, rt, size), "Corruption!");
39  }
40  }
41 
42  if (dstCapacity > 0) {
43  /* Compression succeeds and must round trip correctly. */
44  int compressedSize = size;
45  int const dstSize = LZ4_compress_destSize((const char*)data, dst,
46  &compressedSize, dstCapacity);
47  FUZZ_ASSERT(dstSize > 0);
48  int const rtSize = LZ4_decompress_safe(dst, rt, dstSize, size);
49  FUZZ_ASSERT_MSG(rtSize == compressedSize, "Incorrect regenerated size");
50  FUZZ_ASSERT_MSG(!memcmp(data, rt, compressedSize), "Corruption!");
51  }
52 
53  free(dst);
54  free(rt);
55  FUZZ_dataProducer_free(producer);
56 
57  return 0;
58 }
uLong ZEXPORT compressBound(uLong sourceLen)
Definition: compress.c:81
void FUZZ_dataProducer_free(FUZZ_dataProducer_t *producer)
uint32_t FUZZ_getRange_from_uint32(uint32_t seed, uint32_t min, uint32_t max)
size_t FUZZ_dataProducer_remainingBytes(FUZZ_dataProducer_t *producer)
FUZZ_dataProducer_t * FUZZ_dataProducer_create(const uint8_t *data, size_t size)
uint32_t FUZZ_dataProducer_retrieve32(FUZZ_dataProducer_t *producer)
#define FUZZ_ASSERT(cond)
Definition: fuzz_helpers.h:46
#define FUZZ_ASSERT_MSG(cond, msg)
Definition: fuzz_helpers.h:41
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void uLong size
Definition: ioapi.h:138
void * malloc(size_t size)
Definition: malloc.c:123
int LZ4_compressBound(int isize)
Definition: lz4.c:674
int LZ4_compress_default(const char *src, char *dst, int srcSize, int maxOutputSize)
Definition: lz4.c:1373
int LZ4_compress_destSize(const char *src, char *dst, int *srcSizePtr, int targetDstSize)
Definition: lz4.c:1399
LZ4_FORCE_O2 int LZ4_decompress_safe(const char *source, char *dest, int compressedSize, int maxDecompressedSize)
Definition: lz4.c:2171
char int compressedSize
Definition: lz4.h:724
char * dst
Definition: lz4.h:724

References compressBound(), compressedSize, dst, free(), FUZZ_ASSERT, FUZZ_ASSERT_MSG, FUZZ_dataProducer_create(), FUZZ_dataProducer_free(), FUZZ_dataProducer_remainingBytes(), FUZZ_dataProducer_retrieve32(), FUZZ_getRange_from_uint32(), LZ4_compress_default(), LZ4_compress_destSize(), LZ4_compressBound(), LZ4_decompress_safe(), and malloc().