fuzz: use thread to bypass the limitation of output#5758
fuzz: use thread to bypass the limitation of output#5758cakebaker merged 2 commits intouutils:mainfrom sylvestre:fuzz-thread
Conversation
fuzz/fuzz_targets/fuzz_common.rs
Outdated
| }); | ||
|
|
||
| // Wait for the uumain thread to finish | ||
| let uumain_exit_status = uumain_thread.join().unwrap(); |
There was a problem hiding this comment.
I think there might be a simpler way of writing this using scoped threads, join handles, and the fact that the uumain function can run onto the current thread, without requiring MPSC channels, something like (untested):
let (uumain_exit_status, stdout, stderr) = thread::scope(|s| {
let out = s.spawn(|| read_from_fd(pipe_stdout_fds[0]));
let err = s.spawn(|| read_from_fd(pipe_stderr_fds[0]));
(uumain_function(args), out.join().unwrap(), err.join().unwrap())
});Scoped threads have been introduced in Rust 1.63, but even without using them the code using the join handle to transfer the out/err streams is simpler than using MPSC channels.
There was a problem hiding this comment.
Looks nicer indeed but still freezes
with
let (uumain_exit_status, captured_stdout, captured_stderr) = thread::scope(|s| {
let out = s.spawn(|| read_from_fd(pipe_stdout_fds[0]));
let err = s.spawn(|| read_from_fd(pipe_stderr_fds[0]));
(uumain_function(args_clone.into_iter()), out.join().unwrap(), err.join().unwrap())
});
There was a problem hiding this comment.
Yes, because stdout/stderr for the child are not closed in the main thread so the threads don't terminate and will block on join. By trying
let (uumain_exit_status, captured_stdout, captured_stderr) = thread::scope(|s| {
let out = s.spawn(|| read_from_fd(pipe_stdout_fds[0]));
let err = s.spawn(|| read_from_fd(pipe_stderr_fds[0]));
let status = uumain_function(args.to_owned().into_iter());
io::stdout().flush().unwrap();
io::stderr().flush().unwrap();
unsafe {
close(pipe_stdout_fds[1]);
close(pipe_stderr_fds[1]);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
(status, out.join().unwrap(), err.join().unwrap())
});(and removing two close() below)
it looks like it works as expected. And it fails but because the output of seq -38 -17.207520895356907 -9727 is really different between GNU coreutils and uutils.
There was a problem hiding this comment.
Much better, thanks
it found an issue (but i can't reproduce locally yet)
Test Type: seq
Input: ["0.8262287413206764"]
Rust stdout:
GNU stdout: 1
Discrepancy detected: stdout differs
thread '<unnamed>' panicked at fuzz_targets/fuzz_common.rs:360:13:
Test failed for seq: ["0.8262287413206764"]
No description provided.