-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fuzz printf #5556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
fuzz printf #5556
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// This file is part of the uutils coreutils package. | ||
// | ||
// For the full copyright and license information, please view the LICENSE | ||
// file that was distributed with this source code. | ||
// spell-checker:ignore parens | ||
|
||
#![no_main] | ||
use libfuzzer_sys::fuzz_target; | ||
use uu_printf::uumain; | ||
|
||
use rand::seq::SliceRandom; | ||
use rand::Rng; | ||
use std::ffi::OsString; | ||
|
||
mod fuzz_common; | ||
use crate::fuzz_common::CommandResult; | ||
use crate::fuzz_common::{ | ||
compare_result, generate_and_run_uumain, generate_random_string, run_gnu_cmd, | ||
}; | ||
|
||
static CMD_PATH: &str = "printf"; | ||
|
||
fn generate_escape_sequence(rng: &mut impl Rng) -> String { | ||
let escape_sequences = [ | ||
"\\\"", | ||
"\\\\", | ||
"\\a", | ||
"\\b", | ||
"\\c", | ||
"\\e", | ||
"\\f", | ||
"\\n", | ||
"\\r", | ||
"\\t", | ||
"\\v", | ||
"\\000", | ||
"\\x00", | ||
"\\u0000", | ||
"\\U00000000", | ||
"%%", | ||
]; | ||
escape_sequences.choose(rng).unwrap().to_string() | ||
} | ||
|
||
fn generate_printf() -> String { | ||
let mut rng = rand::thread_rng(); | ||
let format_specifiers = ["%s", "%d", "%f", "%x", "%o", "%c", "%b", "%q"]; | ||
let mut printf_str = String::new(); | ||
// Add a 20% chance of generating an invalid format specifier | ||
if rng.gen_bool(0.2) { | ||
printf_str.push_str("%z"); // Invalid format specifier | ||
} else { | ||
let specifier = *format_specifiers.choose(&mut rng).unwrap(); | ||
printf_str.push_str(specifier); | ||
|
||
// Add a 20% chance of introducing complex format strings | ||
if rng.gen_bool(0.2) { | ||
printf_str.push_str(&format!(" %{}", rng.gen_range(1..=1000))); | ||
} else { | ||
// Add a random string or number after the specifier | ||
if specifier == "%s" { | ||
printf_str.push_str(&format!( | ||
" {}", | ||
generate_random_string(rng.gen_range(1..=10)) | ||
)); | ||
} else { | ||
printf_str.push_str(&format!(" {}", rng.gen_range(1..=1000))); | ||
} | ||
} | ||
} | ||
|
||
// Add a 10% chance of including an escape sequence | ||
if rng.gen_bool(0.1) { | ||
printf_str.push_str(&generate_escape_sequence(&mut rng)); | ||
} | ||
printf_str | ||
} | ||
|
||
fuzz_target!(|_data: &[u8]| { | ||
let printf_input = generate_printf(); | ||
let mut args = vec![OsString::from("printf")]; | ||
args.extend(printf_input.split_whitespace().map(OsString::from)); | ||
let rust_result = generate_and_run_uumain(&args, uumain); | ||
|
||
let gnu_result = match run_gnu_cmd(CMD_PATH, &args[1..], false) { | ||
Ok(result) => result, | ||
Err(error_result) => { | ||
eprintln!("Failed to run GNU command:"); | ||
eprintln!("Stderr: {}", error_result.stderr); | ||
eprintln!("Exit Code: {}", error_result.exit_code); | ||
CommandResult { | ||
stdout: String::new(), | ||
stderr: error_result.stderr, | ||
exit_code: error_result.exit_code, | ||
} | ||
} | ||
}; | ||
|
||
compare_result( | ||
"printf", | ||
&format!("{:?}", &args[1..]), | ||
&rust_result.stdout, | ||
&gnu_result.stdout, | ||
&rust_result.stderr, | ||
&gnu_result.stderr, | ||
rust_result.exit_code, | ||
gnu_result.exit_code, | ||
false, // Set to true if you want to fail on stderr diff | ||
); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about the format specifiers. For example, https://www.gnu.org/savannah-checkouts/gnu/libc/manual/html_node/Table-of-Output-Conversions.html lists many more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, it is just to start :)