aboutsummaryrefslogtreecommitdiff
path: root/tests/ossfuzz/fuzz_encode_stream.c
diff options
context:
space:
mode:
authorJia Tan <jiat0218@gmail.com>2023-12-06 18:30:25 +0800
committerJia Tan <jiat0218@gmail.com>2023-12-07 20:06:57 +0800
commitee2f48350099201694a7586e41d7aa2f09fc74da (patch)
tree35ce97c6b55eeda46cb843195c176a400f6b63a5 /tests/ossfuzz/fuzz_encode_stream.c
parentTests: Add fuzz_encode_stream ossfuzz target. (diff)
downloadxz-ee2f48350099201694a7586e41d7aa2f09fc74da.tar.xz
Tests: Minor cleanups to OSS-Fuzz files.
Most of these fixes are small typos and tweaks. A few were caused by bad advice from me. Here is the summary of what is changed: - Author line edits - Small comment changes/additions - Using the return value in the error messages in the fuzz targets' coder initialization code - Removed fuzz_encode_stream.options. This set a max length, which may prevent some worthwhile code paths from being properly exercised. - Removed the max_len option from fuzz_decode_stream.options for the same reason as fuzz_encode_stream. The alone decoder fuzz target still has this restriction. - Altered the dictionary contents for fuzz_lzma.dict. Instead of keeping the properties static and varying the dictionary size, the properties are varied and the dictionary size is kept small. The dictionary size doesn't have much impact on the code paths but the properties do. Closes: https://github.com/tukaani-project/xz/pull/73
Diffstat (limited to '')
-rw-r--r--tests/ossfuzz/fuzz_encode_stream.c38
1 files changed, 23 insertions, 15 deletions
diff --git a/tests/ossfuzz/fuzz_encode_stream.c b/tests/ossfuzz/fuzz_encode_stream.c
index 8ae8780e..f5770baa 100644
--- a/tests/ossfuzz/fuzz_encode_stream.c
+++ b/tests/ossfuzz/fuzz_encode_stream.c
@@ -1,11 +1,10 @@
///////////////////////////////////////////////////////////////////////////////
//
/// \file fuzz_encode_stream.c
-/// \brief Fuzz test program for liblzma lzma_stream_encoder() w/ LZMA2
+/// \brief Fuzz test program for .xz encoding
//
-// Author: Maksym Vatsyk
-//
-// Based on Lasse Collin's original fuzzer for liblzma
+// Authors: Maksym Vatsyk
+// Lasse Collin
//
// This file has been put into the public domain.
// You can do whatever you want with this file.
@@ -27,9 +26,15 @@ LLVMFuzzerTestOneInput(const uint8_t *inbuf, size_t inbuf_size)
return 0;
}
- // set LZMA preset level based on the first input byte
+ // Set the LZMA options based on the first input byte. The fuzzer
+ // will learn through its mutational genetic algorithm with the
+ // code coverage feedback that the first byte must be one of the
+ // values with a switch case label. This allows us to have one fuzz
+ // target cover many critical code paths so the fuzz resources can
+ // be used efficiently.
uint32_t preset_level;
- uint8_t decider = inbuf[0];
+ const uint8_t decider = inbuf[0];
+
switch (decider) {
case 0:
case 1:
@@ -53,21 +58,24 @@ LLVMFuzzerTestOneInput(const uint8_t *inbuf, size_t inbuf_size)
abort();
}
- // Initialize filter chain for lzma_stream_decoder() call
- // Use single LZMA2 filter for encoding
- lzma_filter filters[2];
- filters[0].id = LZMA_FILTER_LZMA2;
- filters[0].options = &opt_lzma;
- filters[1].id = LZMA_VLI_UNKNOWN;
+ // Set the filter chain as only LZMA2.
+ lzma_filter filters[2] = {
+ {
+ .id = LZMA_FILTER_LZMA2,
+ .options = &opt_lzma,
+ }, {
+ .id = LZMA_VLI_UNKNOWN,
+ }
+ };
// initialize empty LZMA stream
lzma_stream strm = LZMA_STREAM_INIT;
// Initialize the stream encoder using the above
// stream, filter chain and CRC64.
- if (lzma_stream_encoder(&strm,
- filters, LZMA_CHECK_CRC64) != LZMA_OK) {
- fprintf(stderr, "lzma_stream_encoder() failed\n");
+ lzma_ret ret = lzma_stream_encoder(&strm, filters, LZMA_CHECK_CRC64);
+ if (ret != LZMA_OK) {
+ fprintf(stderr, "lzma_stream_encoder() failed (%d)\n", ret);
abort();
}