Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions fuzz/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"
Comment thread
blixygetir marked this conversation as resolved.
Outdated
1 change: 1 addition & 0 deletions fuzz/uufuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ rand = { version = "0.10.1", features = ["std_rng"] }
similar = "3.0.0"
uucore = { version = "0.8.0", path = "../../src/uucore", features = ["parser"] }
tempfile = "3.15.0"
rustix = "1.1.4"
66 changes: 54 additions & 12 deletions fuzz/uufuzz/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
// file that was distributed with this source code.

use console::Style;

use libc::STDIN_FILENO;
use libc::{STDERR_FILENO, STDOUT_FILENO, close, dup, dup2, pipe};
use libc::{STDERR_FILENO, STDOUT_FILENO, close, dup2, pipe};
use rustix::io::dup;
use std::os::unix::io::IntoRawFd;
use pretty_print::{
print_diff, print_end_with_status, print_or_empty, print_section, print_with_style,
};
Expand Down Expand Up @@ -68,15 +71,31 @@ where
F: FnOnce(std::vec::IntoIter<OsString>) -> i32 + Send + 'static,
{
// Duplicate the stdout and stderr file descriptors
let original_stdout_fd = unsafe { dup(STDOUT_FILENO) };
let original_stderr_fd = unsafe { dup(STDERR_FILENO) };
if original_stdout_fd == -1 || original_stderr_fd == -1 {
return CommandResult {
stdout: "".to_string(),
stderr: "Failed to duplicate STDOUT_FILENO or STDERR_FILENO".to_string(),
exit_code: -1,
};
}
let stdout_fd = match dup(std::io::stdout()) {
Ok(fd) => fd.into_raw_fd(),
Err(_) => {
return CommandResult {
stdout: "".to_string(),
stderr: "Failed to duplicate STDOUT_FILENO".to_string(),
exit_code: -1,
};
}
};

let stderr_fd = match dup(std::io::stderr()) {
Ok(fd) => fd.into_raw_fd(),
Err(_) => {
unsafe { close(stdout_fd) };
return CommandResult {
stdout: "".to_string(),
stderr: "Failed to duplicate STDERR_FILENO".to_string(),
exit_code: -1,
Comment thread
blixygetir marked this conversation as resolved.
Outdated
};
}
};
Comment thread
blixygetir marked this conversation as resolved.
Outdated

let original_stdout_fd = stdout_fd;
let original_stderr_fd = stderr_fd;

Comment thread
blixygetir marked this conversation as resolved.
Outdated
println!("Running test {:?}", &args[0..]);
let mut pipe_stdout_fds = [-1; 2];
Expand Down Expand Up @@ -117,15 +136,38 @@ where
input_file.seek(SeekFrom::Start(0)).unwrap();

// Redirect stdin to read from the in-memory file
let original_stdin_fd = unsafe { dup(STDIN_FILENO) };
if original_stdin_fd == -1 || unsafe { dup2(input_file.as_raw_fd(), STDIN_FILENO) } == -1 {
let stdin_fd = match dup(std::io::stdin()) {
Ok(fd) => fd.into_raw_fd(),
Err(_) => {
unsafe {
close(original_stdout_fd);
close(original_stderr_fd);
}
return CommandResult {
stdout: "".to_string(),
stderr: "Failed to duplicate STDIN".to_string(),
exit_code: -1,
};
}
};

let original_stdin_fd = stdin_fd;

if unsafe { dup2(input_file.as_raw_fd(), STDIN_FILENO) } == -1 {
unsafe {
close(original_stdout_fd);
close(original_stderr_fd);
close(original_stdin_fd);
}
Comment thread
blixygetir marked this conversation as resolved.
Outdated
return CommandResult {
stdout: "".to_string(),
stderr: "Failed to set up stdin redirection".to_string(),
exit_code: -1,
};
}

Some(original_stdin_fd)

} else {
None
};
Expand Down
Loading