summaryrefslogtreecommitdiffstats
path: root/ossfuzz/lz4_helpers.c
diff options
context:
space:
mode:
authorNick Terrell <terrelln@fb.com>2019-07-19 01:49:40 (GMT)
committerNick Terrell <terrelln@fb.com>2019-07-19 01:54:59 (GMT)
commitd28159c025829fc70a295b727e32f899a9e0c7c5 (patch)
tree5e178908dd7d63670366c78176b8621dba9d394c /ossfuzz/lz4_helpers.c
parentb487660309d4245eec87e3ada4712bc2a19df791 (diff)
downloadlz4-d28159c025829fc70a295b727e32f899a9e0c7c5.zip
lz4-d28159c025829fc70a295b727e32f899a9e0c7c5.tar.gz
lz4-d28159c025829fc70a295b727e32f899a9e0c7c5.tar.bz2
[fuzz] Add LZ4 frame fuzzers
* Round trip fuzzer * Compress fuzzer * Decompress fuzzer
Diffstat (limited to 'ossfuzz/lz4_helpers.c')
-rw-r--r--ossfuzz/lz4_helpers.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/ossfuzz/lz4_helpers.c b/ossfuzz/lz4_helpers.c
new file mode 100644
index 0000000..9471630
--- /dev/null
+++ b/ossfuzz/lz4_helpers.c
@@ -0,0 +1,51 @@
+#include "fuzz_helpers.h"
+#include "lz4_helpers.h"
+#include "lz4hc.h"
+
+LZ4F_frameInfo_t FUZZ_randomFrameInfo(uint32_t* seed)
+{
+ LZ4F_frameInfo_t info = LZ4F_INIT_FRAMEINFO;
+ info.blockSizeID = FUZZ_rand32(seed, LZ4F_max64KB - 1, LZ4F_max4MB);
+ if (info.blockSizeID < LZ4F_max64KB) {
+ info.blockSizeID = LZ4F_default;
+ }
+ info.blockMode = FUZZ_rand32(seed, LZ4F_blockLinked, LZ4F_blockIndependent);
+ info.contentChecksumFlag = FUZZ_rand32(seed, LZ4F_noContentChecksum,
+ LZ4F_contentChecksumEnabled);
+ info.blockChecksumFlag = FUZZ_rand32(seed, LZ4F_noBlockChecksum,
+ LZ4F_blockChecksumEnabled);
+ return info;
+}
+
+LZ4F_preferences_t FUZZ_randomPreferences(uint32_t* seed)
+{
+ LZ4F_preferences_t prefs = LZ4F_INIT_PREFERENCES;
+ prefs.frameInfo = FUZZ_randomFrameInfo(seed);
+ prefs.compressionLevel = FUZZ_rand32(seed, 0, LZ4HC_CLEVEL_MAX + 3) - 3;
+ prefs.autoFlush = FUZZ_rand32(seed, 0, 1);
+ prefs.favorDecSpeed = FUZZ_rand32(seed, 0, 1);
+ return prefs;
+}
+
+size_t FUZZ_decompressFrame(void* dst, const size_t dstCapacity,
+ const void* src, const size_t srcSize)
+{
+ LZ4F_decompressOptions_t opts;
+ memset(&opts, 0, sizeof(opts));
+ opts.stableDst = 1;
+ LZ4F_dctx* dctx;
+ LZ4F_createDecompressionContext(&dctx, LZ4F_VERSION);
+ FUZZ_ASSERT(dctx);
+
+ size_t dstSize = dstCapacity;
+ size_t srcConsumed = srcSize;
+ size_t const rc =
+ LZ4F_decompress(dctx, dst, &dstSize, src, &srcConsumed, &opts);
+ FUZZ_ASSERT(!LZ4F_isError(rc));
+ FUZZ_ASSERT(rc == 0);
+ FUZZ_ASSERT(srcConsumed == srcSize);
+
+ LZ4F_freeDecompressionContext(dctx);
+
+ return dstSize;
+}