• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * This fuzz target performs a lz4 round-trip test (compress & decompress),
3  * compares the result with the original, and calls abort() on corruption.
4  */
5 
6 #include <stddef.h>
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include <string.h>
10 
11 #include "fuzz_helpers.h"
12 #include "lz4.h"
13 #include "lz4frame.h"
14 #include "lz4_helpers.h"
15 #include "fuzz_data_producer.h"
16 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)17 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
18 {
19     FUZZ_dataProducer_t* producer = FUZZ_dataProducer_create(data, size);
20     LZ4F_preferences_t const prefs = FUZZ_dataProducer_preferences(producer);
21     size = FUZZ_dataProducer_remainingBytes(producer);
22 
23     size_t const dstCapacity = LZ4F_compressFrameBound(LZ4_compressBound(size), &prefs);
24     char* const dst = (char*)malloc(dstCapacity);
25     char* const rt = (char*)malloc(FUZZ_dataProducer_remainingBytes(producer));
26 
27     FUZZ_ASSERT(dst);
28     FUZZ_ASSERT(rt);
29 
30     /* Compression must succeed and round trip correctly. */
31     size_t const dstSize =
32             LZ4F_compressFrame(dst, dstCapacity, data, size, &prefs);
33     FUZZ_ASSERT(!LZ4F_isError(dstSize));
34     size_t const rtSize = FUZZ_decompressFrame(rt, size, dst, dstSize);
35     FUZZ_ASSERT_MSG(rtSize == size, "Incorrect regenerated size");
36     FUZZ_ASSERT_MSG(!memcmp(data, rt, size), "Corruption!");
37 
38     free(dst);
39     free(rt);
40     FUZZ_dataProducer_free(producer);
41 
42     return 0;
43 }
44