diff --git a/fuzz/fuzz_targets/compress.rs b/fuzz/fuzz_targets/compress.rs index 006668e..dc311a9 100644 --- a/fuzz/fuzz_targets/compress.rs +++ b/fuzz/fuzz_targets/compress.rs @@ -1,5 +1,7 @@ //! Compresses arbitrary data with arbitrary parameters (level -7 to 22, -//! window log, long distance matching, workers, job size and overlap, +//! window log, the advanced compression parameters (strategy, hash, chain +//! and search log, minimum match, target length, literal compression +//! mode), long distance matching, workers, job size and overlap, //! checksum, content size and dictionary ID flags, //! loaded or prepared dictionaries from arbitrary bytes), one-shot with a //! reused context or streaming with arbitrary input pieces, flushes, @@ -45,7 +47,7 @@ fuzz_target!(|input: Input| { // The content of the frames, and the output of each library. let (content, want, got): (Vec, Vec, Vec); - if input.one_shot && p.window_log() == 0 && !p.ldm { + if input.one_shot && !p.ldm { // ZSTD_compress2 against Compressor, twice on the same contexts (the // second frame reuses the tables). let mut nc = native_compressor(p); @@ -130,5 +132,7 @@ fn native_compressor(p: &EncParams) -> zstd_native::Compressor { c.set_checksum(p.checksum()); c.set_content_size_flag(p.content_size()); c.set_dict_id_flag(p.dict_id()); + c.set_window_log(p.window_log()).unwrap(); + p.cparams.apply(&mut c); c } diff --git a/fuzz/src/lib.rs b/fuzz/src/lib.rs index 775b482..9811db4 100644 --- a/fuzz/src/lib.rs +++ b/fuzz/src/lib.rs @@ -298,6 +298,103 @@ pub struct EncParams { /// no dictionary ID. pub frame_flags_n: u8, pub dict: EncDict, + /// The advanced compression parameters (`ZSTD_c_strategy` ... + /// `ZSTD_c_literalCompressionMode`), after the dictionary so that older + /// corpus entries keep their meaning. + pub cparams: CParamsN, +} + +/// Raw bytes for [`EncParams::cparams`]: each parameter is the level's a +/// third of the time. +#[derive(Arbitrary, Debug, Clone, Default)] +pub struct CParamsN { + pub strategy_n: u8, + pub hash_log_n: u8, + pub chain_log_n: u8, + pub search_log_n: u8, + pub min_match_n: u8, + pub target_length_n: u16, + pub lit_n: u8, +} + +/// 0 (the level's) a third of the time, else `lo..=hi`. +fn param_from(n: u32, lo: u32, hi: u32) -> u32 { + if n.is_multiple_of(3) { + 0 + } else { + lo + (n / 3) % (hi - lo + 1) + } +} + +impl CParamsN { + /// `ZSTD_c_strategy`: 0 or 1 (fast) to 9 (btultra2). + pub fn strategy(&self) -> u32 { + param_from(self.strategy_n.into(), 1, 9) + } + /// `ZSTD_c_hashLog`, up to 20 (bigger tables only cost time). + pub fn hash_log(&self) -> u32 { + param_from(self.hash_log_n.into(), 6, 20) + } + /// `ZSTD_c_chainLog`, up to 20. + pub fn chain_log(&self) -> u32 { + param_from(self.chain_log_n.into(), 6, 20) + } + /// `ZSTD_c_searchLog`, up to 10. + pub fn search_log(&self) -> u32 { + param_from(self.search_log_n.into(), 1, 10) + } + /// `ZSTD_c_minMatch` + pub fn min_match(&self) -> u32 { + param_from(self.min_match_n.into(), 3, 7) + } + /// `ZSTD_c_targetLength`: 0, small values, or up to its maximum. + pub fn target_length(&self) -> u32 { + match self.target_length_n % 4 { + 0 => 0, + 1 => 1 + u32::from(self.target_length_n >> 2) % 16, + 2 => 1 + u32::from(self.target_length_n >> 2) % 1024, + _ => 131072 >> ((self.target_length_n >> 2) % 8), + } + } + /// `ZSTD_c_literalCompressionMode`: 0 auto, 1 enable, 2 disable. + pub fn lit(&self) -> u32 { + u32::from(self.lit_n % 3) + } + + fn native_strategy(&self) -> Option { + use zstd_native::Strategy::*; + let all = [ + Fast, DFast, Greedy, Lazy, Lazy2, BtLazy2, BtOpt, BtUltra, BtUltra2, + ]; + (self.strategy() != 0).then(|| all[self.strategy() as usize - 1]) + } + + fn native_lit(&self) -> zstd_native::LiteralCompression { + use zstd_native::LiteralCompression::*; + [Auto, Enabled, Disabled][self.lit() as usize] + } + + /// Sets the parameters on a stream encoder (before its first frame). + pub fn apply_stream(&self, e: &mut zstd_native::StreamEncoder) { + e.set_strategy(self.native_strategy()).unwrap(); + e.set_hash_log(self.hash_log()).unwrap(); + e.set_chain_log(self.chain_log()).unwrap(); + e.set_search_log(self.search_log()).unwrap(); + e.set_min_match(self.min_match()).unwrap(); + e.set_target_length(self.target_length()).unwrap(); + e.set_literal_compression(self.native_lit()).unwrap(); + } + + /// Sets the parameters on a one-shot compressor. + pub fn apply(&self, c: &mut zstd_native::Compressor) { + c.set_strategy(self.native_strategy()); + c.set_hash_log(self.hash_log()).unwrap(); + c.set_chain_log(self.chain_log()).unwrap(); + c.set_search_log(self.search_log()).unwrap(); + c.set_min_match(self.min_match()).unwrap(); + c.set_target_length(self.target_length()).unwrap(); + c.set_literal_compression(self.native_lit()); + } } impl EncParams { @@ -344,9 +441,10 @@ impl EncParams { self.frame_flags_n & 4 == 0 } - /// Longest input for the level (the optimal parser is slow). + /// Longest input for the level and strategy (the optimal parser and + /// the binary trees are slow). pub fn data_cap(&self) -> usize { - if self.level() >= 16 { + if self.level() >= 16 || self.cparams.strategy() >= 6 { 48 << 10 } else { 256 << 10 @@ -434,6 +532,28 @@ impl RawCCtx { .map(drop) } + /// `ZSTD_CCtx_setParameter` for the advanced compression parameters + /// (0 keeps the level's). + pub fn set_cparams(&mut self, c: &CParamsN) -> Result<(), usize> { + use zstd_sys::ZSTD_cParameter::*; + for (param, value) in [ + (ZSTD_c_strategy, c.strategy()), + (ZSTD_c_hashLog, c.hash_log()), + (ZSTD_c_chainLog, c.chain_log()), + (ZSTD_c_searchLog, c.search_log()), + (ZSTD_c_minMatch, c.min_match()), + (ZSTD_c_targetLength, c.target_length()), + // ZSTD_c_literalCompressionMode + (ZSTD_c_experimentalParam5, c.lit()), + ] { + // SAFETY: a valid context. + c_result(unsafe { + zstd_sys::ZSTD_CCtx_setParameter(self.cctx.as_ptr(), param, value as i32) + })?; + } + Ok(()) + } + /// `ZSTD_CCtx_loadDictionary` (libzstd copies `dict`). pub fn load_dictionary(&mut self, dict: &[u8]) -> Result<(), usize> { // SAFETY: a valid context; `dict` is only read during the call. @@ -541,6 +661,7 @@ pub fn c_context(p: &EncParams) -> RawCCtx { .unwrap(); c.set_parameter(CParameter::DictIdFlag(p.dict_id())) .unwrap(); + c.set_cparams(&p.cparams).unwrap(); match &p.dict { EncDict::None => {} EncDict::Loaded(s) => { @@ -621,6 +742,7 @@ pub fn native_encoder(p: &EncParams) -> Option { e.set_checksum(p.checksum()).unwrap(); e.set_content_size_flag(p.content_size()).unwrap(); e.set_dict_id_flag(p.dict_id()).unwrap(); + p.cparams.apply_stream(&mut e); Some(e) }