From 8ed919c54b4434a6bfb35aa0c0d604a76606f156 Mon Sep 17 00:00:00 2001 From: Noa Date: Thu, 12 Sep 2024 16:21:54 -0400 Subject: [PATCH 1/2] Add fd3 support to runmake --- Cargo.lock | 1 + src/tools/run-make-support/Cargo.toml | 1 + src/tools/run-make-support/src/command.rs | 25 ++++++++++++++++++++++ src/tools/run-make-support/src/macros.rs | 11 ++++++++++ tests/run-make/jobserver-error/rmake.rs | 26 ++++++++++++----------- 5 files changed, 52 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a4b4e49f82c2e..e458c7a5e3bba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3149,6 +3149,7 @@ dependencies = [ "bstr", "build_helper", "gimli 0.31.0", + "libc", "object 0.36.2", "regex", "similar", diff --git a/src/tools/run-make-support/Cargo.toml b/src/tools/run-make-support/Cargo.toml index c4d5446a2481e..d60f45eee46ee 100644 --- a/src/tools/run-make-support/Cargo.toml +++ b/src/tools/run-make-support/Cargo.toml @@ -10,4 +10,5 @@ similar = "2.5.0" wasmparser = { version = "0.214", default-features = false, features = ["std"] } regex = "1.8" # 1.8 to avoid memchr 2.6.0, as 2.5.0 is pinned in the workspace gimli = "0.31.0" +libc = "0.2" build_helper = { path = "../build_helper" } diff --git a/src/tools/run-make-support/src/command.rs b/src/tools/run-make-support/src/command.rs index a0b96e25a0cba..719b49e33dc85 100644 --- a/src/tools/run-make-support/src/command.rs +++ b/src/tools/run-make-support/src/command.rs @@ -104,6 +104,31 @@ impl Command { self } + /// Set an auxiliary stream passed to the process, besides the stdio streams. + #[cfg(unix)] + pub fn set_aux_fd>( + &mut self, + newfd: std::os::fd::RawFd, + fd: F, + ) -> &mut Self { + use std::os::fd::AsRawFd; + use std::os::unix::process::CommandExt; + + let fd = fd.into(); + unsafe { + self.cmd.pre_exec(move || { + let fd = fd.as_raw_fd(); + let ret = if fd == newfd { + libc::fcntl(fd, libc::F_SETFD, 0) + } else { + libc::dup2(fd, newfd) + }; + if ret == -1 { Err(std::io::Error::last_os_error()) } else { Ok(()) } + }); + } + self + } + /// Run the constructed command and assert that it is successfully run. #[track_caller] pub fn run(&mut self) -> CompletedProcess { diff --git a/src/tools/run-make-support/src/macros.rs b/src/tools/run-make-support/src/macros.rs index f7fe4f5422399..322ee5314e239 100644 --- a/src/tools/run-make-support/src/macros.rs +++ b/src/tools/run-make-support/src/macros.rs @@ -80,6 +80,17 @@ macro_rules! impl_common_helpers { self } + /// Set an auxiliary stream passed to the process, besides the stdio streams. + #[cfg(unix)] + pub fn set_aux_fd>( + &mut self, + newfd: std::os::fd::RawFd, + fd: F, + ) -> &mut Self { + self.cmd.set_aux_fd(newfd, fd); + self + } + /// Run the constructed command and assert that it is successfully run. #[track_caller] pub fn run(&mut self) -> crate::command::CompletedProcess { diff --git a/tests/run-make/jobserver-error/rmake.rs b/tests/run-make/jobserver-error/rmake.rs index a17add2d81fb8..c18c226285740 100644 --- a/tests/run-make/jobserver-error/rmake.rs +++ b/tests/run-make/jobserver-error/rmake.rs @@ -19,22 +19,24 @@ fn main() { .run_fail() .stderr_utf8(); diff().expected_file("cannot_open_fd.stderr").actual_text("actual", out).run(); - // FIXME(Oneirical): Find how to use file descriptor "3" with run-make-support - // and pipe /dev/null into it. - // Original lines: - // - // bash -c 'echo "fn main() {}" | makeflags="--jobserver-auth=3,3" $(rustc) - 3&1 | diff not_a_pipe.stderr - + + let out = rustc() + .stdin("fn main() {}") + .input("-") + .env("MAKEFLAGS", "--jobserver-auth=3,3") + .set_aux_fd(3, std::fs::File::open("/dev/null").unwrap()) + .run() + .stderr_utf8(); + diff().expected_file("not_a_pipe.stderr").actual_text("actual", out).run(); + // # this test randomly fails, see https://github.com/rust-lang/rust/issues/110321 - // disabled: - // bash -c 'echo "fn main() {}" | makeflags="--jobserver-auth=3,3" $(rustc) - \ - // 3< <(cat /dev/null)' 2>&1 | diff poisoned_pipe.stderr - - // + // let (readpipe, _) = std::pipe::pipe().unwrap(); // let out = rustc() // .stdin("fn main() {}") // .input("-") - // .env("MAKEFLAGS", "--jobserver-auth=0,0") + // .env("MAKEFLAGS", "--jobserver-auth=3,3") + // .set_fd3(readpipe) // .run() // .stderr_utf8(); - // diff().expected_file("not_a_pipe.stderr").actual_text("actual", out).run(); + // diff().expected_file("poisoned_pipe.stderr").actual_text("actual", out).run(); } From 0eb50a0911a5b7e24a45d466a4d277f242a90355 Mon Sep 17 00:00:00 2001 From: Noa Date: Fri, 13 Sep 2024 11:51:37 -0400 Subject: [PATCH 2/2] Better comments for set_aux_fd --- src/tools/run-make-support/src/command.rs | 35 ++++++++++++++++------- src/tools/run-make-support/src/macros.rs | 5 ++++ 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/tools/run-make-support/src/command.rs b/src/tools/run-make-support/src/command.rs index 719b49e33dc85..c651bdda82b60 100644 --- a/src/tools/run-make-support/src/command.rs +++ b/src/tools/run-make-support/src/command.rs @@ -105,6 +105,11 @@ impl Command { } /// Set an auxiliary stream passed to the process, besides the stdio streams. + /// + /// Use with caution - ideally, only set one aux fd; if there are multiple, + /// their old `fd` may overlap with another's `newfd`, and thus will break. + /// If you need more than 1 auxiliary file descriptor, rewrite this function + /// to be able to support that. #[cfg(unix)] pub fn set_aux_fd>( &mut self, @@ -114,18 +119,28 @@ impl Command { use std::os::fd::AsRawFd; use std::os::unix::process::CommandExt; + let cvt = |x| if x == -1 { Err(std::io::Error::last_os_error()) } else { Ok(()) }; + let fd = fd.into(); - unsafe { - self.cmd.pre_exec(move || { - let fd = fd.as_raw_fd(); - let ret = if fd == newfd { - libc::fcntl(fd, libc::F_SETFD, 0) - } else { - libc::dup2(fd, newfd) - }; - if ret == -1 { Err(std::io::Error::last_os_error()) } else { Ok(()) } - }); + if fd.as_raw_fd() == newfd { + // if fd is already where we want it, just turn off FD_CLOEXEC + // SAFETY: io-safe: we have ownership over fd + cvt(unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_SETFD, 0) }) + .expect("disabling CLOEXEC failed"); + // we still unconditionally set the pre_exec function, since it captures + // `fd`, and we want to ensure that it stays open until the fork } + let pre_exec = move || { + if fd.as_raw_fd() != newfd { + // set newfd to point to the same file as fd + // SAFETY: io-"safe": newfd is. not necessarily an unused fd. + // however, we're + cvt(unsafe { libc::dup2(fd.as_raw_fd(), newfd) })?; + } + Ok(()) + }; + // SAFETY: dup2 is pre-exec-safe + unsafe { self.cmd.pre_exec(pre_exec) }; self } diff --git a/src/tools/run-make-support/src/macros.rs b/src/tools/run-make-support/src/macros.rs index 322ee5314e239..43844edf4127d 100644 --- a/src/tools/run-make-support/src/macros.rs +++ b/src/tools/run-make-support/src/macros.rs @@ -81,6 +81,11 @@ macro_rules! impl_common_helpers { } /// Set an auxiliary stream passed to the process, besides the stdio streams. + /// + /// Use with caution - ideally, only set one aux fd; if there are multiple, + /// their old `fd` may overlap with another's `newfd`, and thus will break. + /// If you need more than 1 auxiliary file descriptor, rewrite this function + /// to be able to support that. #[cfg(unix)] pub fn set_aux_fd>( &mut self,