about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2025-01-23 05:31:12 +0000
committerbors <bors@rust-lang.org>2025-01-23 05:31:12 +0000
commitcf577f34c47937ccb9983186eca5f8719da585f4 (patch)
tree2755dc2a0122a8efa16972e0d069f1749c2c027f
parent3cd8fcbf87bd28a1f31be000ca906fb66f4d451d (diff)
parent20229200c80c7a6400f455a2cc2281045c302ba6 (diff)
downloadrust-cf577f34c47937ccb9983186eca5f8719da585f4.tar.gz
rust-cf577f34c47937ccb9983186eca5f8719da585f4.zip
Auto merge of #135461 - jieyouxu:migrate-jobserver-errors, r=Noratrieb
tests: Port `jobserver-error` to rmake.rs

Part of #121876.

This PR ports `tests/run-make/jobserver-error` to rmake.rs, and is basically #128789 slightly adjusted.

The complexity involved here is mostly how to get `/dev/null/` piping to fd 3 working with std `Command`, whereas with a shell this is much easier (as is evident with the `Makefile` version).

Supersedes #128789.
This PR is co-authored with `@Oneirical` and `@coolreader18.`

try-job: aarch64-gnu
try-job: i686-gnu-1
try-job: x86_64-gnu-debug
try-job: x86_64-gnu-llvm-18-1
-rw-r--r--src/tools/run-make-support/src/command.rs55
-rw-r--r--src/tools/run-make-support/src/macros.rs11
-rw-r--r--src/tools/tidy/src/allowed_run_make_makefiles.txt1
-rw-r--r--tests/run-make/jobserver-error/Makefile17
-rw-r--r--tests/run-make/jobserver-error/cannot_open_fd.stderr2
-rw-r--r--tests/run-make/jobserver-error/rmake.rs47
6 files changed, 114 insertions, 19 deletions
diff --git a/src/tools/run-make-support/src/command.rs b/src/tools/run-make-support/src/command.rs
index e73413085fa..b4dc753ab53 100644
--- a/src/tools/run-make-support/src/command.rs
+++ b/src/tools/run-make-support/src/command.rs
@@ -151,6 +151,61 @@ impl Command {
         self
     }
 
+    /// Set an auxiliary stream passed to the process, besides the stdio streams.
+    ///
+    /// # Notes
+    ///
+    /// Use with caution! Ideally, only set one aux fd; if there are multiple, their old `fd` may
+    /// overlap with another's `new_fd`, and may break. The caller must make sure this is not the
+    /// case. This function is only "safe" because the safety requirements are practically not
+    /// possible to uphold.
+    #[cfg(unix)]
+    pub fn set_aux_fd<F: Into<std::os::fd::OwnedFd>>(
+        &mut self,
+        new_fd: std::os::fd::RawFd,
+        fd: F,
+    ) -> &mut Self {
+        use std::mem;
+        // NOTE: If more than 1 auxiliary file descriptor is needed, this function should be
+        // rewritten.
+        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(()) };
+
+        // Ensure fd stays open until the fork.
+        let fd = mem::ManuallyDrop::new(fd.into());
+        let fd = fd.as_raw_fd();
+
+        if fd == new_fd {
+            // If the new file descriptor is already the same as fd, just turn off `FD_CLOEXEC`.
+            let fd_flags = {
+                let ret = unsafe { libc::fcntl(fd, libc::F_GETFD, 0) };
+                if ret < 0 {
+                    panic!("failed to read fd flags: {}", std::io::Error::last_os_error());
+                }
+                ret
+            };
+            // Clear `FD_CLOEXEC`.
+            let fd_flags = fd_flags & !libc::FD_CLOEXEC;
+
+            // SAFETY(io-safety): `fd` is already owned.
+            cvt(unsafe { libc::fcntl(fd, libc::F_SETFD, fd_flags as libc::c_int) })
+                .expect("disabling CLOEXEC failed");
+        }
+        let pre_exec = move || {
+            if fd.as_raw_fd() != new_fd {
+                // SAFETY(io-safety): it's the caller's responsibility that we won't override the
+                // target fd.
+                cvt(unsafe { libc::dup2(fd, new_fd) })?;
+            }
+            Ok(())
+        };
+        // SAFETY(pre-exec-safe): `dup2` is pre-exec-safe.
+        unsafe { self.cmd.pre_exec(pre_exec) };
+        self
+    }
+
     /// Run the constructed command and assert that it is successfully run.
     ///
     /// By default, std{in,out,err} are [`Stdio::piped()`].
diff --git a/src/tools/run-make-support/src/macros.rs b/src/tools/run-make-support/src/macros.rs
index cc3d1281d0a..94955aefe57 100644
--- a/src/tools/run-make-support/src/macros.rs
+++ b/src/tools/run-make-support/src/macros.rs
@@ -104,6 +104,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<F: Into<std::os::fd::OwnedFd>>(
+                &mut self,
+                new_fd: std::os::fd::RawFd,
+                fd: F,
+            ) -> &mut Self {
+                self.cmd.set_aux_fd(new_fd, 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/src/tools/tidy/src/allowed_run_make_makefiles.txt b/src/tools/tidy/src/allowed_run_make_makefiles.txt
index 567f0a256d9..6f0fd09b353 100644
--- a/src/tools/tidy/src/allowed_run_make_makefiles.txt
+++ b/src/tools/tidy/src/allowed_run_make_makefiles.txt
@@ -1,4 +1,3 @@
-run-make/jobserver-error/Makefile
 run-make/split-debuginfo/Makefile
 run-make/symbol-mangling-hashed/Makefile
 run-make/translation/Makefile
diff --git a/tests/run-make/jobserver-error/Makefile b/tests/run-make/jobserver-error/Makefile
deleted file mode 100644
index 9f34970f96f..00000000000
--- a/tests/run-make/jobserver-error/Makefile
+++ /dev/null
@@ -1,17 +0,0 @@
-include ../tools.mk
-
-# only-linux
-# ignore-cross-compile
-
-# Test compiler behavior in case environment specifies wrong jobserver.
-# Note that by default, the compiler uses file descriptors 0 (stdin), 1 (stdout), 2 (stderr),
-# but also 3 and 4 for either end of the ctrl-c signal handler self-pipe.
-
-all:
-	bash -c 'echo "fn main() {}" | MAKEFLAGS="--jobserver-auth=5,5" $(RUSTC)' 2>&1 | diff cannot_open_fd.stderr -
-	bash -c 'echo "fn main() {}" | MAKEFLAGS="--jobserver-auth=3,3" $(RUSTC) - 3</dev/null' 2>&1 | diff not_a_pipe.stderr -
-
-# 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 -
-
diff --git a/tests/run-make/jobserver-error/cannot_open_fd.stderr b/tests/run-make/jobserver-error/cannot_open_fd.stderr
index 9ac4c1c58f7..d075057b3d3 100644
--- a/tests/run-make/jobserver-error/cannot_open_fd.stderr
+++ b/tests/run-make/jobserver-error/cannot_open_fd.stderr
@@ -1,4 +1,4 @@
-warning: failed to connect to jobserver from environment variable `MAKEFLAGS="--jobserver-auth=5,5"`: cannot open file descriptor 5 from the jobserver environment variable value: Bad file descriptor (os error 9)
+warning: failed to connect to jobserver from environment variable `MAKEFLAGS="--jobserver-auth=1000,1000"`: cannot open file descriptor 1000 from the jobserver environment variable value: Bad file descriptor (os error 9)
   |
   = note: the build environment is likely misconfigured
 
diff --git a/tests/run-make/jobserver-error/rmake.rs b/tests/run-make/jobserver-error/rmake.rs
new file mode 100644
index 00000000000..14ee24c7148
--- /dev/null
+++ b/tests/run-make/jobserver-error/rmake.rs
@@ -0,0 +1,47 @@
+// ignore-tidy-linelength
+//! If the environment variables contain an invalid `jobserver-auth`, this used to cause an ICE
+//! until this was fixed in [do not panic on failure to acquire jobserver token
+//! #109694](https://github.com/rust-lang/rust/pull/109694).
+//!
+//! Proper handling has been added, and this test checks that helpful warnings and errors are
+//! printed instead in case of a wrong jobserver. See
+//! <https://github.com/rust-lang/rust/issues/46981>.
+
+//@ only-linux
+//@ ignore-cross-compile
+
+#![deny(warnings)]
+
+use run_make_support::{diff, rustc};
+
+fn main() {
+    let out = rustc()
+        .stdin_buf(("fn main() {}").as_bytes())
+        .env("MAKEFLAGS", "--jobserver-auth=1000,1000")
+        .run_fail()
+        .stderr_utf8();
+    diff().expected_file("cannot_open_fd.stderr").actual_text("actual", out).run();
+
+    let out = rustc()
+        .stdin_buf(("fn main() {}").as_bytes())
+        .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();
+
+    // FIXME(#110321): the Makefile version had a disabled check:
+    //
+    // ```makefile
+    // bash -c 'echo "fn main() {}" | MAKEFLAGS="--jobserver-auth=3,3" $(RUSTC) - 3< <(cat /dev/null)' 2>&1 | diff poisoned_pipe.stderr -
+    // ```
+    //
+    // > the jobserver helper thread launched here gets starved out and doesn't run, while the
+    // > coordinator thread continually processes work using the implicit jobserver token, never
+    // > yielding long enough for the jobserver helper to do its work (and process the error).
+    //
+    // but is not necessarily worth fixing as it might require changing coordinator behavior that
+    // might regress performance. See discussion at
+    // <https://github.com/rust-lang/rust/issues/110321#issuecomment-1636914956>.
+}