Skip to content

Commit b1ba7b7

Browse files
committed
Clarify implementation (or try to)
1 parent f4a9d0b commit b1ba7b7

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

cargo-miri/bin.rs

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,14 @@ fn xargo_check() -> Command {
249249
Command::new(env::var_os("XARGO_CHECK").unwrap_or_else(|| OsString::from("xargo-check")))
250250
}
251251

252-
/// Execute the command. If it fails, fail this process with the same exit code.
253-
/// Otherwise, continue.
252+
/// Execute the `Command`, where possible by replacing the current process with a new process
253+
/// described by the `Command`. Then exit this process with the exit code of the new process.
254254
fn exec(mut cmd: Command) -> ! {
255255
// On non-Unix imitate POSIX exec as closely as we can
256256
#[cfg(not(unix))]
257257
{
258258
let exit_status = cmd.status().expect("failed to run command");
259-
std::process::exit(exit_status.code().unwrap_or(-1));
259+
std::process::exit(exit_status.code().unwrap_or(-1))
260260
}
261261
// On Unix targets, actually exec
262262
// If exec returns, process setup has failed. This is the same error condition as the expect in
@@ -269,10 +269,10 @@ fn exec(mut cmd: Command) -> ! {
269269
}
270270
}
271271

272-
/// Execute the command and pipe `input` into its stdin.
273-
/// If it fails, fail this process with the same exit code.
274-
/// Otherwise, continue.
275-
fn exec_with_pipe(mut cmd: Command, input: &[u8]) {
272+
/// Execute the `Command`, where possible by replacing the current process with a new process
273+
/// described by the `Command`. Then exit this process with the exit code of the new process.
274+
/// `input` is also piped to the new process's stdin.
275+
fn exec_with_pipe(mut cmd: Command, input: &[u8]) -> ! {
276276
#[cfg(unix)]
277277
{
278278
use std::os::unix::io::FromRawFd;
@@ -283,25 +283,15 @@ fn exec_with_pipe(mut cmd: Command, input: &[u8]) {
283283
let res = unsafe { libc::pipe(fds.as_mut_ptr()) };
284284
assert_eq!(res, 0, "failed to create pipe");
285285

286-
// We need to set close-on-exec, otherwise our pipe isn't readable after exec
287-
// SAFETY: fcntl has no preconditions
288-
unsafe {
289-
for fd in &fds {
290-
let res = libc::fcntl(
291-
*fd,
292-
libc::F_SETFD,
293-
libc::fcntl(*fd, libc::F_GETFD) | libc::FD_CLOEXEC,
294-
);
295-
assert_eq!(res, 0, "failed to set close-on-exec for pipe");
296-
}
297-
}
298-
299-
// SAFETY: Both elements of fds are open file descriptors, because pipe2 returned 0
286+
// SAFETY: Both elements of fds are open file descriptors, because pipe returned 0
300287
let dst = unsafe { Stdio::from_raw_fd(fds[0]) };
301288
let mut src = unsafe { File::from_raw_fd(fds[1]) };
302289

303290
src.write_all(input).expect("failed to write out test source");
304291

292+
// Close the writing part of the pipe in the parent process
293+
drop(src);
294+
305295
cmd.stdin(dst);
306296
exec(cmd)
307297
}
@@ -936,10 +926,11 @@ fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
936926
cmd.arg("--emit=metadata");
937927
}
938928

929+
// Alter the `-o` parameter so that it does not overwrite the JSON file we stored above.
939930
let mut args = env.args.clone();
940931
for i in 0..args.len() {
941932
if args[i] == "-o" {
942-
args[i + 1] = format!("{}_miri", args[i + 1]);
933+
args[i + 1].push_str("_miri");
943934
}
944935
}
945936

0 commit comments

Comments
 (0)