Skip to content

Commit cf577f3

Browse files
committed
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
2 parents 3cd8fcb + 2022920 commit cf577f3

File tree

6 files changed

+114
-19
lines changed

6 files changed

+114
-19
lines changed

src/tools/run-make-support/src/command.rs

+55
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,61 @@ impl Command {
151151
self
152152
}
153153

154+
/// Set an auxiliary stream passed to the process, besides the stdio streams.
155+
///
156+
/// # Notes
157+
///
158+
/// Use with caution! Ideally, only set one aux fd; if there are multiple, their old `fd` may
159+
/// overlap with another's `new_fd`, and may break. The caller must make sure this is not the
160+
/// case. This function is only "safe" because the safety requirements are practically not
161+
/// possible to uphold.
162+
#[cfg(unix)]
163+
pub fn set_aux_fd<F: Into<std::os::fd::OwnedFd>>(
164+
&mut self,
165+
new_fd: std::os::fd::RawFd,
166+
fd: F,
167+
) -> &mut Self {
168+
use std::mem;
169+
// NOTE: If more than 1 auxiliary file descriptor is needed, this function should be
170+
// rewritten.
171+
use std::os::fd::AsRawFd;
172+
use std::os::unix::process::CommandExt;
173+
174+
let cvt = |x| if x == -1 { Err(std::io::Error::last_os_error()) } else { Ok(()) };
175+
176+
// Ensure fd stays open until the fork.
177+
let fd = mem::ManuallyDrop::new(fd.into());
178+
let fd = fd.as_raw_fd();
179+
180+
if fd == new_fd {
181+
// If the new file descriptor is already the same as fd, just turn off `FD_CLOEXEC`.
182+
let fd_flags = {
183+
let ret = unsafe { libc::fcntl(fd, libc::F_GETFD, 0) };
184+
if ret < 0 {
185+
panic!("failed to read fd flags: {}", std::io::Error::last_os_error());
186+
}
187+
ret
188+
};
189+
// Clear `FD_CLOEXEC`.
190+
let fd_flags = fd_flags & !libc::FD_CLOEXEC;
191+
192+
// SAFETY(io-safety): `fd` is already owned.
193+
cvt(unsafe { libc::fcntl(fd, libc::F_SETFD, fd_flags as libc::c_int) })
194+
.expect("disabling CLOEXEC failed");
195+
}
196+
let pre_exec = move || {
197+
if fd.as_raw_fd() != new_fd {
198+
// SAFETY(io-safety): it's the caller's responsibility that we won't override the
199+
// target fd.
200+
cvt(unsafe { libc::dup2(fd, new_fd) })?;
201+
}
202+
Ok(())
203+
};
204+
// SAFETY(pre-exec-safe): `dup2` is pre-exec-safe.
205+
unsafe { self.cmd.pre_exec(pre_exec) };
206+
self
207+
}
208+
154209
/// Run the constructed command and assert that it is successfully run.
155210
///
156211
/// By default, std{in,out,err} are [`Stdio::piped()`].

src/tools/run-make-support/src/macros.rs

+11
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ macro_rules! impl_common_helpers {
104104
self
105105
}
106106

107+
/// Set an auxiliary stream passed to the process, besides the stdio streams.
108+
#[cfg(unix)]
109+
pub fn set_aux_fd<F: Into<std::os::fd::OwnedFd>>(
110+
&mut self,
111+
new_fd: std::os::fd::RawFd,
112+
fd: F,
113+
) -> &mut Self {
114+
self.cmd.set_aux_fd(new_fd, fd);
115+
self
116+
}
117+
107118
/// Run the constructed command and assert that it is successfully run.
108119
#[track_caller]
109120
pub fn run(&mut self) -> crate::command::CompletedProcess {
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
run-make/jobserver-error/Makefile
21
run-make/split-debuginfo/Makefile
32
run-make/symbol-mangling-hashed/Makefile
43
run-make/translation/Makefile

tests/run-make/jobserver-error/Makefile

-17
This file was deleted.

tests/run-make/jobserver-error/cannot_open_fd.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
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)
1+
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)
22
|
33
= note: the build environment is likely misconfigured
44

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// ignore-tidy-linelength
2+
//! If the environment variables contain an invalid `jobserver-auth`, this used to cause an ICE
3+
//! until this was fixed in [do not panic on failure to acquire jobserver token
4+
//! #109694](https://github.com/rust-lang/rust/pull/109694).
5+
//!
6+
//! Proper handling has been added, and this test checks that helpful warnings and errors are
7+
//! printed instead in case of a wrong jobserver. See
8+
//! <https://github.com/rust-lang/rust/issues/46981>.
9+
10+
//@ only-linux
11+
//@ ignore-cross-compile
12+
13+
#![deny(warnings)]
14+
15+
use run_make_support::{diff, rustc};
16+
17+
fn main() {
18+
let out = rustc()
19+
.stdin_buf(("fn main() {}").as_bytes())
20+
.env("MAKEFLAGS", "--jobserver-auth=1000,1000")
21+
.run_fail()
22+
.stderr_utf8();
23+
diff().expected_file("cannot_open_fd.stderr").actual_text("actual", out).run();
24+
25+
let out = rustc()
26+
.stdin_buf(("fn main() {}").as_bytes())
27+
.input("-")
28+
.env("MAKEFLAGS", "--jobserver-auth=3,3")
29+
.set_aux_fd(3, std::fs::File::open("/dev/null").unwrap())
30+
.run()
31+
.stderr_utf8();
32+
diff().expected_file("not_a_pipe.stderr").actual_text("actual", out).run();
33+
34+
// FIXME(#110321): the Makefile version had a disabled check:
35+
//
36+
// ```makefile
37+
// bash -c 'echo "fn main() {}" | MAKEFLAGS="--jobserver-auth=3,3" $(RUSTC) - 3< <(cat /dev/null)' 2>&1 | diff poisoned_pipe.stderr -
38+
// ```
39+
//
40+
// > the jobserver helper thread launched here gets starved out and doesn't run, while the
41+
// > coordinator thread continually processes work using the implicit jobserver token, never
42+
// > yielding long enough for the jobserver helper to do its work (and process the error).
43+
//
44+
// but is not necessarily worth fixing as it might require changing coordinator behavior that
45+
// might regress performance. See discussion at
46+
// <https://github.com/rust-lang/rust/issues/110321#issuecomment-1636914956>.
47+
}

0 commit comments

Comments
 (0)