11#![ no_main]
22
3- use fastpfor:: cpp:: { Codec32 as _ , SimdFastPFor128Codec } ;
3+ use fastpfor:: cpp:: * ;
44use libfuzzer_sys:: fuzz_target;
5- use std:: num:: NonZeroU32 ;
65
7- fuzz_target ! ( |data: ( u32 , Vec <u32 >) | {
8- let ( block_size, input) = data;
6+ type BoxedCodec = Box < dyn Codec32 > ;
97
10- let Ok ( block_size) = NonZeroU32 :: try_from( block_size) else {
11- return ;
12- } ;
13-
14- // TODO: fuzz more codecs
15- let mut codec = SimdFastPFor128Codec :: new( ) ;
16-
17- // Limit input size to avoid timeouts
18- let input: Vec <u32 > = input. into_iter( ) . take( 10_000 ) . collect( ) ;
8+ fuzz_target ! ( |data: FuzzInput | {
9+ let codec = BoxedCodec :: from( data. codec) ;
10+ let input = data. data;
1911
2012 // Allocate output buffer with generous size
2113 let mut output = vec![ 0u32 ; input. len( ) * 2 + 1024 ] ;
2214
2315 // Compress the data
2416 let enc_slice = codec. encode32( & input, & mut output) . unwrap( ) ;
25- assert!( !enc_slice. is_empty( ) , "compression should not be empty" ) ;
2617
2718 // Now decompress
2819 let mut decoded = vec![ 0u32 ; input. len( ) * 2 + 1024 ] ;
2920 let dec_slice = codec. decode32( & enc_slice, & mut decoded) . unwrap( ) ;
3021
3122 // Verify roundtrip
3223 if dec_slice. len( ) + input. len( ) < 200 {
33- assert_eq!(
34- input,
35- dec_slice,
36- "Decompressed output mismatches"
37- ) ;
24+ assert_eq!( input, dec_slice, "Decompressed output mismatches" ) ;
3825 } else {
3926 assert_eq!( dec_slice. len( ) , input. len( ) , "Decompressed length mismatch" ) ;
4027 for ( i, ( & original, & decoded) ) in input. iter( ) . zip( dec_slice. iter( ) ) . enumerate( ) {
@@ -45,3 +32,90 @@ fuzz_target!(|data: (u32, Vec<u32>)| {
4532 }
4633 }
4734} ) ;
35+
36+ #[ derive( arbitrary:: Arbitrary , Debug ) ]
37+ struct FuzzInput {
38+ data : Vec < u32 > ,
39+ codec : FuzzCodec ,
40+ }
41+
42+ #[ derive( Clone , Copy , Eq , PartialEq , arbitrary:: Arbitrary , Debug ) ]
43+ enum FuzzCodec {
44+ BP32Codec ,
45+ CopyCodec ,
46+ FastBinaryPacking8Codec ,
47+ FastPFor128Codec ,
48+ FastPFor256Codec ,
49+ FastBinaryPacking16Codec ,
50+ FastBinaryPacking32Codec ,
51+ MaskedVByteCodec ,
52+ NewPForCodec ,
53+ OptPForCodec ,
54+ PFor2008Codec ,
55+ PForCodec ,
56+ SimdBinaryPackingCodec ,
57+ SimdFastPFor128Codec ,
58+ SimdFastPFor256Codec ,
59+ SimdGroupSimpleCodec ,
60+ SimdGroupSimpleRingBufCodec ,
61+ SimdNewPForCodec ,
62+ SimdOptPForCodec ,
63+ SimdPForCodec ,
64+ SimdSimplePForCodec ,
65+ // Simple16Codec, // cannot encode arbitrary bytes
66+ // Simple8bCodec, // cannot encode arbitrary bytes
67+ // Simple8bRleCodec, // cannot encode arbitrary bytes
68+ // Simple9Codec, // cannot encode arbitrary bytes
69+ // Simple9RleCodec, // cannot encode arbitrary bytes
70+ // SimplePForCodec, // cannot encode arbitrary bytes
71+ // SnappyCodec, // Conditional with #ifdef
72+ StreamVByteCodec ,
73+ VByteCodec ,
74+ VarIntCodec ,
75+ // VarIntG8iuCodec, // Conditional with #ifdef
76+ VarIntGbCodec ,
77+ // VsEncodingCodec, // This is leaking memory
78+ }
79+
80+ impl From < FuzzCodec > for BoxedCodec {
81+ fn from ( codec : FuzzCodec ) -> Self {
82+ match codec {
83+ FuzzCodec :: BP32Codec => Box :: new ( BP32Codec :: default ( ) ) ,
84+ FuzzCodec :: CopyCodec => Box :: new ( CopyCodec :: default ( ) ) ,
85+ FuzzCodec :: FastBinaryPacking8Codec => Box :: new ( FastBinaryPacking8Codec :: default ( ) ) ,
86+ FuzzCodec :: FastPFor128Codec => Box :: new ( FastPFor128Codec :: default ( ) ) ,
87+ FuzzCodec :: FastPFor256Codec => Box :: new ( FastPFor256Codec :: default ( ) ) ,
88+ FuzzCodec :: FastBinaryPacking16Codec => Box :: new ( FastBinaryPacking16Codec :: default ( ) ) ,
89+ FuzzCodec :: FastBinaryPacking32Codec => Box :: new ( FastBinaryPacking32Codec :: default ( ) ) ,
90+ FuzzCodec :: MaskedVByteCodec => Box :: new ( MaskedVByteCodec :: default ( ) ) ,
91+ FuzzCodec :: NewPForCodec => Box :: new ( NewPForCodec :: default ( ) ) ,
92+ FuzzCodec :: OptPForCodec => Box :: new ( OptPForCodec :: default ( ) ) ,
93+ FuzzCodec :: PFor2008Codec => Box :: new ( PFor2008Codec :: default ( ) ) ,
94+ FuzzCodec :: PForCodec => Box :: new ( PForCodec :: default ( ) ) ,
95+ FuzzCodec :: SimdBinaryPackingCodec => Box :: new ( SimdBinaryPackingCodec :: default ( ) ) ,
96+ FuzzCodec :: SimdFastPFor128Codec => Box :: new ( SimdFastPFor128Codec :: default ( ) ) ,
97+ FuzzCodec :: SimdFastPFor256Codec => Box :: new ( SimdFastPFor256Codec :: default ( ) ) ,
98+ FuzzCodec :: SimdGroupSimpleCodec => Box :: new ( SimdGroupSimpleCodec :: default ( ) ) ,
99+ FuzzCodec :: SimdGroupSimpleRingBufCodec => {
100+ Box :: new ( SimdGroupSimpleRingBufCodec :: default ( ) )
101+ }
102+ FuzzCodec :: SimdNewPForCodec => Box :: new ( SimdNewPForCodec :: default ( ) ) ,
103+ FuzzCodec :: SimdOptPForCodec => Box :: new ( SimdOptPForCodec :: default ( ) ) ,
104+ FuzzCodec :: SimdPForCodec => Box :: new ( SimdPForCodec :: default ( ) ) ,
105+ FuzzCodec :: SimdSimplePForCodec => Box :: new ( SimdSimplePForCodec :: default ( ) ) ,
106+ // FuzzCodec::Simple16Codec => Box::new(Simple16Codec::default()),
107+ // FuzzCodec::Simple8bCodec => Box::new(Simple8bCodec::default()),
108+ // FuzzCodec::Simple8bRleCodec => Box::new(Simple8bRleCodec::default()),
109+ // FuzzCodec::Simple9Codec => Box::new(Simple9Codec::default()),
110+ // FuzzCodec::Simple9RleCodec => Box::new(Simple9RleCodec::default()),
111+ // FuzzCodec::SimplePForCodec => Box::new(SimplePForCodec::default()),
112+ // FuzzCodec::SnappyCodec => Box::new(SnappyCodec::default()),
113+ FuzzCodec :: StreamVByteCodec => Box :: new ( StreamVByteCodec :: default ( ) ) ,
114+ FuzzCodec :: VByteCodec => Box :: new ( VByteCodec :: default ( ) ) ,
115+ FuzzCodec :: VarIntCodec => Box :: new ( VarIntCodec :: default ( ) ) ,
116+ // FuzzCodec::VarIntG8iuCodec => Box::new(VarIntG8iuCodec::default()),
117+ FuzzCodec :: VarIntGbCodec => Box :: new ( VarIntGbCodec :: default ( ) ) ,
118+ // FuzzCodec::VsEncodingCodec => Box::new(VsEncodingCodec::default()),
119+ }
120+ }
121+ }
0 commit comments