|
| 1 | +use arbitrary::{Arbitrary, Unstructured}; |
| 2 | +use clap::Parser; |
| 3 | +use std::fs::{self, File}; |
| 4 | +use std::io::Read; |
| 5 | +use std::path::Path; |
| 6 | +use thrift::protocol::{ |
| 7 | + TBinaryOutputProtocol, TCompactOutputProtocol, TOutputProtocol, TSerializable, |
| 8 | +}; |
| 9 | +use thrift::transport::TBufferChannel; |
| 10 | +use thrift_fuzz::fuzz_test::FuzzTest; |
| 11 | + |
| 12 | +#[derive(Parser)] |
| 13 | +#[command(author, version, about, long_about = None)] |
| 14 | +struct Args { |
| 15 | + /// Input directory containing raw binary files (mutually exclusive with --generate) |
| 16 | + #[arg(short, long, group = "input")] |
| 17 | + input_dir: Option<String>, |
| 18 | + |
| 19 | + /// Number of random files to generate (mutually exclusive with --input-dir) |
| 20 | + #[arg(short, long, group = "input")] |
| 21 | + generate: Option<usize>, |
| 22 | + |
| 23 | + /// Output directory for serialized FuzzTest files |
| 24 | + #[arg(short, long)] |
| 25 | + output_dir: String, |
| 26 | + |
| 27 | + /// Protocol to use for serialization (binary or compact) |
| 28 | + #[arg(short, long)] |
| 29 | + protocol: String, |
| 30 | + |
| 31 | + /// Buffer size for serialization (default: 65536) |
| 32 | + #[arg(short, long, default_value = "65536")] |
| 33 | + buffer_size: usize, |
| 34 | + |
| 35 | + /// Size of random byte vector for generation (default: 16384) |
| 36 | + #[arg(long, default_value = "16384")] |
| 37 | + random_size: usize, |
| 38 | +} |
| 39 | + |
| 40 | +fn serialize_fuzz_test( |
| 41 | + fuzz_test: &FuzzTest, |
| 42 | + protocol: &str, |
| 43 | + buffer_size: usize, |
| 44 | +) -> Result<Vec<u8>, Box<dyn std::error::Error>> { |
| 45 | + let mut mem = TBufferChannel::with_capacity(buffer_size, buffer_size); |
| 46 | + match protocol { |
| 47 | + "binary" => { |
| 48 | + let mut out_protocol = TBinaryOutputProtocol::new(&mut mem, true); |
| 49 | + fuzz_test.write_to_out_protocol(&mut out_protocol)?; |
| 50 | + out_protocol.flush()?; |
| 51 | + } |
| 52 | + "compact" => { |
| 53 | + let mut out_protocol = TCompactOutputProtocol::new(&mut mem); |
| 54 | + fuzz_test.write_to_out_protocol(&mut out_protocol)?; |
| 55 | + out_protocol.flush()?; |
| 56 | + } |
| 57 | + _ => return Err("Invalid protocol specified. Use 'binary' or 'compact'".into()), |
| 58 | + } |
| 59 | + Ok(mem.write_bytes().to_vec()) |
| 60 | +} |
| 61 | + |
| 62 | +fn convert_corpus_file( |
| 63 | + input_path: &Path, |
| 64 | + output_dir: &Path, |
| 65 | + protocol: &str, |
| 66 | + buffer_size: usize, |
| 67 | +) -> Result<(), Box<dyn std::error::Error>> { |
| 68 | + // Read input file |
| 69 | + let mut input_file = File::open(input_path)?; |
| 70 | + let mut input_data = Vec::new(); |
| 71 | + input_file.read_to_end(&mut input_data)?; |
| 72 | + |
| 73 | + // Create Unstructured instance for arbitrary |
| 74 | + let mut unstructured = Unstructured::new(&input_data); |
| 75 | + |
| 76 | + // Generate FuzzTest instance |
| 77 | + if let Ok(fuzz_test) = FuzzTest::arbitrary(&mut unstructured) { |
| 78 | + // Create output file path |
| 79 | + let file_name = input_path |
| 80 | + .file_name() |
| 81 | + .ok_or("Invalid input filename")? |
| 82 | + .to_str() |
| 83 | + .ok_or("Invalid UTF-8 in filename")?; |
| 84 | + let output_path = output_dir.join(file_name); |
| 85 | + |
| 86 | + // Serialize and write to file |
| 87 | + let serialized_data = serialize_fuzz_test(&fuzz_test, protocol, buffer_size)?; |
| 88 | + fs::write(output_path, serialized_data)?; |
| 89 | + } |
| 90 | + |
| 91 | + Ok(()) |
| 92 | +} |
| 93 | + |
| 94 | +fn generate_random_file( |
| 95 | + output_dir: &Path, |
| 96 | + index: usize, |
| 97 | + protocol: &str, |
| 98 | + buffer_size: usize, |
| 99 | + random_size: usize, |
| 100 | +) -> Result<(), Box<dyn std::error::Error>> { |
| 101 | + // Generate random bytes |
| 102 | + let random_bytes: Vec<u8> = (0..random_size).map(|_| rand::random::<u8>()).collect(); |
| 103 | + |
| 104 | + // Create Unstructured instance for arbitrary |
| 105 | + let mut unstructured = Unstructured::new(&random_bytes); |
| 106 | + |
| 107 | + // Generate FuzzTest instance |
| 108 | + if let Ok(fuzz_test) = FuzzTest::arbitrary(&mut unstructured) { |
| 109 | + // Create output file path with index |
| 110 | + let output_path = output_dir.join(format!("generated_{index}.bin")); |
| 111 | + |
| 112 | + // Serialize and write to file |
| 113 | + let serialized_data = serialize_fuzz_test(&fuzz_test, protocol, buffer_size)?; |
| 114 | + fs::write(output_path, serialized_data)?; |
| 115 | + } |
| 116 | + |
| 117 | + Ok(()) |
| 118 | +} |
| 119 | + |
| 120 | +fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 121 | + let args = Args::parse(); |
| 122 | + |
| 123 | + // Validate protocol |
| 124 | + if args.protocol != "binary" && args.protocol != "compact" { |
| 125 | + return Err("Invalid protocol specified. Use 'binary' or 'compact'".into()); |
| 126 | + } |
| 127 | + |
| 128 | + // Create output directory if it doesn't exist |
| 129 | + fs::create_dir_all(&args.output_dir)?; |
| 130 | + |
| 131 | + match (args.input_dir, args.generate) { |
| 132 | + (Some(input_dir), None) => { |
| 133 | + // Process each file in the input directory |
| 134 | + for entry in fs::read_dir(&input_dir)? { |
| 135 | + let entry = entry?; |
| 136 | + let path = entry.path(); |
| 137 | + if path.is_file() { |
| 138 | + if let Err(e) = convert_corpus_file( |
| 139 | + &path, |
| 140 | + Path::new(&args.output_dir), |
| 141 | + &args.protocol, |
| 142 | + args.buffer_size, |
| 143 | + ) { |
| 144 | + eprintln!("Error processing file {path:?}: {e}"); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + (None, Some(num_files)) => { |
| 150 | + // Generate random files |
| 151 | + for i in 0..num_files { |
| 152 | + if let Err(e) = generate_random_file( |
| 153 | + Path::new(&args.output_dir), |
| 154 | + i, |
| 155 | + &args.protocol, |
| 156 | + args.buffer_size, |
| 157 | + args.random_size, |
| 158 | + ) { |
| 159 | + eprintln!("Error generating file {i}: {e}"); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + _ => return Err("Must specify either --input-dir or --generate".into()), |
| 164 | + } |
| 165 | + |
| 166 | + Ok(()) |
| 167 | +} |
0 commit comments