|
| 1 | +use std::path::{Path, PathBuf}; |
| 2 | + |
| 3 | +use crate::setup::SetupRunner; |
| 4 | +use anyhow::Result; |
| 5 | +use clap::Parser; |
| 6 | +use onefuzz::{libfuzzer::LibFuzzer, machine_id::MachineIdentity}; |
| 7 | +use uuid::Uuid; |
| 8 | + |
| 9 | +#[derive(Parser, Debug)] |
| 10 | +#[clap(rename_all = "snake_case")] |
| 11 | +pub enum ValidationCommand { |
| 12 | + /// Run the setup script |
| 13 | + RunSetup { setup_folder: PathBuf }, |
| 14 | + /// Validate the libfuzzer target |
| 15 | + ValidateLibfuzzer(ValidationConfig), |
| 16 | + /// Get the execution logs to debug loading issues |
| 17 | + ExecutionLog(ValidationConfig), |
| 18 | +} |
| 19 | + |
| 20 | +fn parse_key_val<T, U>( |
| 21 | + s: &str, |
| 22 | +) -> Result<(T, U), Box<dyn std::error::Error + Send + Sync + 'static>> |
| 23 | +where |
| 24 | + T: std::str::FromStr, |
| 25 | + T::Err: std::error::Error + Send + Sync + 'static, |
| 26 | + U: std::str::FromStr, |
| 27 | + U::Err: std::error::Error + Send + Sync + 'static, |
| 28 | +{ |
| 29 | + let pos = s |
| 30 | + .find('=') |
| 31 | + .ok_or_else(|| format!("invalid KEY=value: no `=` found in `{s}`"))?; |
| 32 | + |
| 33 | + println!("******** pos: {}", pos); |
| 34 | + |
| 35 | + Ok((s[..pos].parse()?, s[pos + 1..].parse()?)) |
| 36 | +} |
| 37 | + |
| 38 | +#[derive(Parser, Debug, Deserialize)] |
| 39 | +#[clap(rename_all = "snake_case")] |
| 40 | +pub struct ValidationConfig { |
| 41 | + #[clap(long = "seeds")] |
| 42 | + pub seeds: Option<PathBuf>, |
| 43 | + #[clap(long = "target_exe")] |
| 44 | + pub target_exe: PathBuf, |
| 45 | + #[clap(long = "setup_folder")] |
| 46 | + pub setup_folder: Option<PathBuf>, |
| 47 | + #[clap(long = "target_options")] |
| 48 | + pub target_options: Vec<String>, |
| 49 | + #[arg(value_parser = parse_key_val::<String, String>, long = "target_env")] |
| 50 | + pub target_env: Vec<(String, String)>, |
| 51 | +} |
| 52 | + |
| 53 | +pub async fn validate(command: ValidationCommand) -> Result<()> { |
| 54 | + match command { |
| 55 | + ValidationCommand::RunSetup { setup_folder } => run_setup(setup_folder).await, |
| 56 | + ValidationCommand::ValidateLibfuzzer(validation_config) => { |
| 57 | + validate_libfuzzer(validation_config).await |
| 58 | + } |
| 59 | + ValidationCommand::ExecutionLog(validation_config) => get_logs(validation_config).await, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +async fn validate_libfuzzer(config: ValidationConfig) -> Result<()> { |
| 64 | + let libfuzzer = LibFuzzer::new( |
| 65 | + &config.target_exe, |
| 66 | + config.target_options.clone(), |
| 67 | + config.target_env.into_iter().collect(), |
| 68 | + config |
| 69 | + .setup_folder |
| 70 | + .unwrap_or(config.target_exe.parent().unwrap().to_path_buf()), |
| 71 | + None::<&PathBuf>, |
| 72 | + MachineIdentity { |
| 73 | + machine_id: Uuid::nil(), |
| 74 | + machine_name: "".to_string(), |
| 75 | + scaleset_name: None, |
| 76 | + }, |
| 77 | + ); |
| 78 | + |
| 79 | + if let Some(seeds) = config.seeds { |
| 80 | + libfuzzer.verify(true, Some(vec![seeds])).await?; |
| 81 | + } |
| 82 | + |
| 83 | + Ok(()) |
| 84 | +} |
| 85 | + |
| 86 | +async fn run_setup(setup_folder: impl AsRef<Path>) -> Result<()> { |
| 87 | + let output = SetupRunner::run_setup_script(setup_folder.as_ref()).await?; |
| 88 | + match output { |
| 89 | + Some(output) => { |
| 90 | + if !output.exit_status.success { |
| 91 | + let error = "error running target setup script".to_owned(); |
| 92 | + bail!("{}", error); |
| 93 | + } |
| 94 | + } |
| 95 | + None => { |
| 96 | + println!("no setup script to run") |
| 97 | + } |
| 98 | + } |
| 99 | + Ok(()) |
| 100 | +} |
| 101 | + |
| 102 | +async fn get_logs(config: ValidationConfig) -> Result<()> { |
| 103 | + let libfuzzer = LibFuzzer::new( |
| 104 | + &config.target_exe, |
| 105 | + config.target_options.clone(), |
| 106 | + config.target_env.into_iter().collect(), |
| 107 | + config |
| 108 | + .setup_folder |
| 109 | + .unwrap_or(config.target_exe.parent().unwrap().to_path_buf()), |
| 110 | + None::<&PathBuf>, |
| 111 | + MachineIdentity { |
| 112 | + machine_id: Uuid::nil(), |
| 113 | + machine_name: "".to_string(), |
| 114 | + scaleset_name: None, |
| 115 | + }, |
| 116 | + ); |
| 117 | + let cmd = libfuzzer.build_std_command(None, None, None).await?; |
| 118 | + print_logs(cmd)?; |
| 119 | + Ok(()) |
| 120 | +} |
| 121 | + |
| 122 | +#[cfg(target_os = "windows")] |
| 123 | +fn print_logs(cmd: std::process::Command) -> Result<(), anyhow::Error> { |
| 124 | + let logs = dynamic_library::windows::get_logs(cmd)?; |
| 125 | + for log in logs { |
| 126 | + println!("{log:x?}"); |
| 127 | + } |
| 128 | + |
| 129 | + Ok(()) |
| 130 | +} |
| 131 | + |
| 132 | +#[cfg(target_os = "linux")] |
| 133 | +fn print_logs(cmd: std::process::Command) -> Result<(), anyhow::Error> { |
| 134 | + let logs = dynamic_library::linux::get_linked_library_logs(&cmd)?; |
| 135 | + for log in logs.stdout { |
| 136 | + println!("{log:x?}"); |
| 137 | + } |
| 138 | + |
| 139 | + let logs2 = dynamic_library::linux::get_loaded_libraries_logs(cmd)?; |
| 140 | + for log in logs2.stderr { |
| 141 | + println!("{log:x?}"); |
| 142 | + } |
| 143 | + |
| 144 | + Ok(()) |
| 145 | +} |
0 commit comments