Skip to content
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

fuzz: use thread to bypass the limitation of output #5758

Merged
merged 2 commits into from
Jan 3, 2024

Conversation

sylvestre
Copy link
Contributor

No description provided.

});

// Wait for the uumain thread to finish
let uumain_exit_status = uumain_thread.join().unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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())
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"]

@uutils uutils deleted a comment from github-actions bot Jan 1, 2024
@cakebaker cakebaker merged commit f3833bb into uutils:main Jan 3, 2024
55 of 57 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants