1 use std::env;
2 use std::ffi::OsString;
3 use std::fmt::Write as _;
4 use std::fs::{self, File};
5 use std::io::{self, BufRead, BufReader, BufWriter, Read, Write};
6 use std::iter::TakeWhile;
7 use std::ops::Not;
8 use std::path::{Path, PathBuf};
9 use std::process::{self, Command};
10 
11 use serde::{Deserialize, Serialize};
12 
13 use rustc_version::VersionMeta;
14 
15 const XARGO_MIN_VERSION: (u32, u32, u32) = (0, 3, 22);
16 
17 const CARGO_MIRI_HELP: &str = r#"Runs binary crates and tests in Miri
18 
19 Usage:
20     cargo miri [subcommand] [<cargo options>...] [--] [<program/test suite options>...]
21 
22 Subcommands:
23     run, r                   Run binaries
24     test, t                  Run tests
25     setup                    Only perform automatic setup, but without asking questions (for getting a proper libstd)
26 
27 The cargo options are exactly the same as for `cargo run` and `cargo test`, respectively.
28 
29 Examples:
30     cargo miri run
31     cargo miri test -- test-suite-filter
32 "#;
33 
34 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
35 enum MiriCommand {
36     Run,
37     Test,
38     Setup,
39 }
40 
41 /// The information to run a crate with the given environment.
42 #[derive(Serialize, Deserialize)]
43 struct CrateRunEnv {
44     /// The command-line arguments.
45     args: Vec<String>,
46     /// The environment.
47     env: Vec<(OsString, OsString)>,
48     /// The current working directory.
49     current_dir: OsString,
50     /// The contents passed via standard input.
51     stdin: Vec<u8>,
52 }
53 
54 impl CrateRunEnv {
55     /// Gather all the information we need.
collect(args: env::Args, capture_stdin: bool) -> Self56     fn collect(args: env::Args, capture_stdin: bool) -> Self {
57         let args = args.collect();
58         let env = env::vars_os().collect();
59         let current_dir = env::current_dir().unwrap().into_os_string();
60 
61         let mut stdin = Vec::new();
62         if capture_stdin {
63             std::io::stdin().lock().read_to_end(&mut stdin).expect("cannot read stdin");
64         }
65 
66         CrateRunEnv { args, env, current_dir, stdin }
67     }
68 }
69 
70 /// The information Miri needs to run a crate. Stored as JSON when the crate is "compiled".
71 #[derive(Serialize, Deserialize)]
72 enum CrateRunInfo {
73     /// Run it with the given environment.
74     RunWith(CrateRunEnv),
75     /// Skip it as Miri does not support interpreting such kind of crates.
76     SkipProcMacroTest,
77 }
78 
79 impl CrateRunInfo {
store(&self, filename: &Path)80     fn store(&self, filename: &Path) {
81         let file = File::create(filename)
82             .unwrap_or_else(|_| show_error(format!("cannot create `{}`", filename.display())));
83         let file = BufWriter::new(file);
84         serde_json::ser::to_writer(file, self)
85             .unwrap_or_else(|_| show_error(format!("cannot write to `{}`", filename.display())));
86     }
87 }
88 
show_help()89 fn show_help() {
90     println!("{}", CARGO_MIRI_HELP);
91 }
92 
show_version()93 fn show_version() {
94     let mut version = format!("miri {}", env!("CARGO_PKG_VERSION"));
95     // Only use `option_env` on vergen variables to ensure the build succeeds
96     // when vergen failed to find the git info.
97     if let Some(sha) = option_env!("VERGEN_GIT_SHA_SHORT") {
98         write!(&mut version, " ({} {})", sha, option_env!("VERGEN_GIT_COMMIT_DATE").unwrap())
99             .unwrap();
100     }
101     println!("{}", version);
102 }
103 
show_error(msg: String) -> !104 fn show_error(msg: String) -> ! {
105     eprintln!("fatal error: {}", msg);
106     std::process::exit(1)
107 }
108 
109 // Determines whether a `--flag` is present.
has_arg_flag(name: &str) -> bool110 fn has_arg_flag(name: &str) -> bool {
111     let mut args = std::env::args().take_while(|val| val != "--");
112     args.any(|val| val == name)
113 }
114 
115 /// Yields all values of command line flag `name` as `Ok(arg)`, and all other arguments except
116 /// the flag as `Err(arg)`. (The flag `name` itself is not yielded at all, only its values are.)
117 struct ArgSplitFlagValue<'a, I> {
118     args: TakeWhile<I, fn(&String) -> bool>,
119     name: &'a str,
120 }
121 
122 impl<'a, I: Iterator<Item = String>> ArgSplitFlagValue<'a, I> {
new(args: I, name: &'a str) -> Self123     fn new(args: I, name: &'a str) -> Self {
124         Self {
125             // Stop searching at `--`.
126             args: args.take_while(|val| val != "--"),
127             name,
128         }
129     }
130 }
131 
132 impl<I: Iterator<Item = String>> Iterator for ArgSplitFlagValue<'_, I> {
133     type Item = Result<String, String>;
134 
next(&mut self) -> Option<Self::Item>135     fn next(&mut self) -> Option<Self::Item> {
136         let arg = self.args.next()?;
137         if arg.starts_with(self.name) {
138             // Strip leading `name`.
139             let suffix = &arg[self.name.len()..];
140             if suffix.is_empty() {
141                 // This argument is exactly `name`; the next one is the value.
142                 return self.args.next().map(Ok);
143             } else if suffix.starts_with('=') {
144                 // This argument is `name=value`; get the value.
145                 // Strip leading `=`.
146                 return Some(Ok(suffix[1..].to_owned()));
147             }
148         }
149         Some(Err(arg))
150     }
151 }
152 
153 /// Yields all values of command line flag `name`.
154 struct ArgFlagValueIter<'a>(ArgSplitFlagValue<'a, env::Args>);
155 
156 impl<'a> ArgFlagValueIter<'a> {
new(name: &'a str) -> Self157     fn new(name: &'a str) -> Self {
158         Self(ArgSplitFlagValue::new(env::args(), name))
159     }
160 }
161 
162 impl Iterator for ArgFlagValueIter<'_> {
163     type Item = String;
164 
next(&mut self) -> Option<Self::Item>165     fn next(&mut self) -> Option<Self::Item> {
166         loop {
167             if let Ok(value) = self.0.next()? {
168                 return Some(value);
169             }
170         }
171     }
172 }
173 
174 /// Gets the value of a `--flag`.
get_arg_flag_value(name: &str) -> Option<String>175 fn get_arg_flag_value(name: &str) -> Option<String> {
176     ArgFlagValueIter::new(name).next()
177 }
178 
forward_patched_extern_arg(args: &mut impl Iterator<Item = String>, cmd: &mut Command)179 fn forward_patched_extern_arg(args: &mut impl Iterator<Item = String>, cmd: &mut Command) {
180     cmd.arg("--extern"); // always forward flag, but adjust filename:
181     let path = args.next().expect("`--extern` should be followed by a filename");
182     if let Some(lib) = path.strip_suffix(".rlib") {
183         // If this is an rlib, make it an rmeta.
184         cmd.arg(format!("{}.rmeta", lib));
185     } else {
186         // Some other extern file (e.g. a `.so`). Forward unchanged.
187         cmd.arg(path);
188     }
189 }
190 
forward_miri_sysroot(cmd: &mut Command)191 fn forward_miri_sysroot(cmd: &mut Command) {
192     let sysroot = env::var_os("MIRI_SYSROOT").expect("the wrapper should have set MIRI_SYSROOT");
193     cmd.arg("--sysroot");
194     cmd.arg(sysroot);
195 }
196 
197 /// Returns the path to the `miri` binary
find_miri() -> PathBuf198 fn find_miri() -> PathBuf {
199     if let Some(path) = env::var_os("MIRI") {
200         return path.into();
201     }
202     let mut path = std::env::current_exe().expect("current executable path invalid");
203     path.set_file_name("miri");
204     path
205 }
206 
miri() -> Command207 fn miri() -> Command {
208     Command::new(find_miri())
209 }
210 
version_info() -> VersionMeta211 fn version_info() -> VersionMeta {
212     VersionMeta::for_command(miri()).expect("failed to determine underlying rustc version of Miri")
213 }
214 
cargo() -> Command215 fn cargo() -> Command {
216     Command::new(env::var_os("CARGO").unwrap_or_else(|| OsString::from("cargo")))
217 }
218 
xargo_check() -> Command219 fn xargo_check() -> Command {
220     Command::new(env::var_os("XARGO_CHECK").unwrap_or_else(|| OsString::from("xargo-check")))
221 }
222 
223 /// Execute the command. If it fails, fail this process with the same exit code.
224 /// Otherwise, continue.
exec(mut cmd: Command)225 fn exec(mut cmd: Command) {
226     let exit_status = cmd.status().expect("failed to run command");
227     if exit_status.success().not() {
228         std::process::exit(exit_status.code().unwrap_or(-1))
229     }
230 }
231 
232 /// Execute the command and pipe `input` into its stdin.
233 /// If it fails, fail this process with the same exit code.
234 /// Otherwise, continue.
exec_with_pipe(mut cmd: Command, input: &[u8])235 fn exec_with_pipe(mut cmd: Command, input: &[u8]) {
236     cmd.stdin(process::Stdio::piped());
237     let mut child = cmd.spawn().expect("failed to spawn process");
238     {
239         let stdin = child.stdin.as_mut().expect("failed to open stdin");
240         stdin.write_all(input).expect("failed to write out test source");
241     }
242     let exit_status = child.wait().expect("failed to run command");
243     if exit_status.success().not() {
244         std::process::exit(exit_status.code().unwrap_or(-1))
245     }
246 }
247 
xargo_version() -> Option<(u32, u32, u32)>248 fn xargo_version() -> Option<(u32, u32, u32)> {
249     let out = xargo_check().arg("--version").output().ok()?;
250     if !out.status.success() {
251         return None;
252     }
253     // Parse output. The first line looks like "xargo 0.3.12 (b004f1c 2018-12-13)".
254     let line = out
255         .stderr
256         .lines()
257         .nth(0)
258         .expect("malformed `xargo --version` output: not at least one line")
259         .expect("malformed `xargo --version` output: error reading first line");
260     let (name, version) = {
261         let mut split = line.split(' ');
262         (
263             split.next().expect("malformed `xargo --version` output: empty"),
264             split.next().expect("malformed `xargo --version` output: not at least two words"),
265         )
266     };
267     if name != "xargo" {
268         // This is some fork of xargo
269         return None;
270     }
271     let mut version_pieces = version.split('.');
272     let major = version_pieces
273         .next()
274         .expect("malformed `xargo --version` output: not a major version piece")
275         .parse()
276         .expect("malformed `xargo --version` output: major version is not an integer");
277     let minor = version_pieces
278         .next()
279         .expect("malformed `xargo --version` output: not a minor version piece")
280         .parse()
281         .expect("malformed `xargo --version` output: minor version is not an integer");
282     let patch = version_pieces
283         .next()
284         .expect("malformed `xargo --version` output: not a patch version piece")
285         .parse()
286         .expect("malformed `xargo --version` output: patch version is not an integer");
287     if !version_pieces.next().is_none() {
288         panic!("malformed `xargo --version` output: more than three pieces in version");
289     }
290     Some((major, minor, patch))
291 }
292 
ask_to_run(mut cmd: Command, ask: bool, text: &str)293 fn ask_to_run(mut cmd: Command, ask: bool, text: &str) {
294     // Disable interactive prompts in CI (GitHub Actions, Travis, AppVeyor, etc).
295     // Azure doesn't set `CI` though (nothing to see here, just Microsoft being Microsoft),
296     // so we also check their `TF_BUILD`.
297     let is_ci = env::var_os("CI").is_some() || env::var_os("TF_BUILD").is_some();
298     if ask && !is_ci {
299         let mut buf = String::new();
300         print!("I will run `{:?}` to {}. Proceed? [Y/n] ", cmd, text);
301         io::stdout().flush().unwrap();
302         io::stdin().read_line(&mut buf).unwrap();
303         match buf.trim().to_lowercase().as_ref() {
304             // Proceed.
305             "" | "y" | "yes" => {}
306             "n" | "no" => show_error(format!("aborting as per your request")),
307             a => show_error(format!("invalid answer `{}`", a)),
308         };
309     } else {
310         println!("Running `{:?}` to {}.", cmd, text);
311     }
312 
313     if cmd.status().expect(&format!("failed to execute {:?}", cmd)).success().not() {
314         show_error(format!("failed to {}", text));
315     }
316 }
317 
318 /// Performs the setup required to make `cargo miri` work: Getting a custom-built libstd. Then sets
319 /// `MIRI_SYSROOT`. Skipped if `MIRI_SYSROOT` is already set, in which case we expect the user has
320 /// done all this already.
setup(subcommand: MiriCommand)321 fn setup(subcommand: MiriCommand) {
322     if std::env::var_os("MIRI_SYSROOT").is_some() {
323         if subcommand == MiriCommand::Setup {
324             println!("WARNING: MIRI_SYSROOT already set, not doing anything.")
325         }
326         return;
327     }
328 
329     // Subcommands other than `setup` will do a setup if necessary, but
330     // interactively confirm first.
331     let ask_user = subcommand != MiriCommand::Setup;
332 
333     // First, we need xargo.
334     if xargo_version().map_or(true, |v| v < XARGO_MIN_VERSION) {
335         if std::env::var_os("XARGO_CHECK").is_some() {
336             // The user manually gave us a xargo binary; don't do anything automatically.
337             show_error(format!("xargo is too old; please upgrade to the latest version"))
338         }
339         let mut cmd = cargo();
340         cmd.args(&["install", "xargo"]);
341         ask_to_run(cmd, ask_user, "install a recent enough xargo");
342     }
343 
344     // Determine where the rust sources are located.  `XARGO_RUST_SRC` env var trumps everything.
345     let rust_src = match std::env::var_os("XARGO_RUST_SRC") {
346         Some(path) => {
347             let path = PathBuf::from(path);
348             // Make path absolute if possible.
349             path.canonicalize().unwrap_or(path)
350         }
351         None => {
352             // Check for `rust-src` rustup component.
353             let sysroot = miri()
354                 .args(&["--print", "sysroot"])
355                 .output()
356                 .expect("failed to determine sysroot")
357                 .stdout;
358             let sysroot = std::str::from_utf8(&sysroot).unwrap();
359             let sysroot = Path::new(sysroot.trim_end_matches('\n'));
360             // Check for `$SYSROOT/lib/rustlib/src/rust/library`; test if that contains `std/Cargo.toml`.
361             let rustup_src =
362                 sysroot.join("lib").join("rustlib").join("src").join("rust").join("library");
363             if !rustup_src.join("std").join("Cargo.toml").exists() {
364                 // Ask the user to install the `rust-src` component, and use that.
365                 let mut cmd = Command::new("rustup");
366                 cmd.args(&["component", "add", "rust-src"]);
367                 ask_to_run(
368                     cmd,
369                     ask_user,
370                     "install the `rust-src` component for the selected toolchain",
371                 );
372             }
373             rustup_src
374         }
375     };
376     if !rust_src.exists() {
377         show_error(format!("given Rust source directory `{}` does not exist.", rust_src.display()));
378     }
379 
380     // Next, we need our own libstd. Prepare a xargo project for that purpose.
381     // We will do this work in whatever is a good cache dir for this platform.
382     let dirs = directories::ProjectDirs::from("org", "rust-lang", "miri").unwrap();
383     let dir = dirs.cache_dir();
384     if !dir.exists() {
385         fs::create_dir_all(&dir).unwrap();
386     }
387     // The interesting bit: Xargo.toml
388     File::create(dir.join("Xargo.toml"))
389         .unwrap()
390         .write_all(
391             br#"
392 [dependencies.std]
393 default_features = false
394 # We support unwinding, so enable that panic runtime.
395 features = ["panic_unwind", "backtrace"]
396 
397 [dependencies.test]
398 "#,
399         )
400         .unwrap();
401     // The boring bits: a dummy project for xargo.
402     // FIXME: With xargo-check, can we avoid doing this?
403     File::create(dir.join("Cargo.toml"))
404         .unwrap()
405         .write_all(
406             br#"
407 [package]
408 name = "miri-xargo"
409 description = "A dummy project for building libstd with xargo."
410 version = "0.0.0"
411 
412 [lib]
413 path = "lib.rs"
414 "#,
415         )
416         .unwrap();
417     File::create(dir.join("lib.rs")).unwrap();
418 
419     // Determine architectures.
420     // We always need to set a target so rustc bootstrap can tell apart host from target crates.
421     let host = version_info().host;
422     let target = get_arg_flag_value("--target");
423     let target = target.as_ref().unwrap_or(&host);
424     // Now invoke xargo.
425     let mut command = xargo_check();
426     command.arg("check").arg("-q");
427     command.arg("--target").arg(target);
428     command.current_dir(&dir);
429     command.env("XARGO_HOME", &dir);
430     command.env("XARGO_RUST_SRC", &rust_src);
431     // Use Miri as rustc to build a libstd compatible with us (and use the right flags).
432     // However, when we are running in bootstrap, we cannot just overwrite `RUSTC`,
433     // because we still need bootstrap to distinguish between host and target crates.
434     // In that case we overwrite `RUSTC_REAL` instead which determines the rustc used
435     // for target crates.
436     // We set ourselves (`cargo-miri`) instead of Miri directly to be able to patch the flags
437     // for `libpanic_abort` (usually this is done by bootstrap but we have to do it ourselves).
438     // The `MIRI_CALLED_FROM_XARGO` will mean we dispatch to `phase_setup_rustc`.
439     let cargo_miri_path = std::env::current_exe().expect("current executable path invalid");
440     if env::var_os("RUSTC_STAGE").is_some() {
441         command.env("RUSTC_REAL", &cargo_miri_path);
442     } else {
443         command.env("RUSTC", &cargo_miri_path);
444     }
445     command.env("MIRI_CALLED_FROM_XARGO", "1");
446     // Make sure there are no other wrappers or flags getting in our way
447     // (Cc https://github.com/rust-lang/miri/issues/1421).
448     // This is consistent with normal `cargo build` that does not apply `RUSTFLAGS`
449     // to the sysroot either.
450     command.env_remove("RUSTC_WRAPPER");
451     command.env_remove("RUSTFLAGS");
452     // Disable debug assertions in the standard library -- Miri is already slow enough.
453     // But keep the overflow checks, they are cheap.
454     command.env("RUSTFLAGS", "-Cdebug-assertions=off -Coverflow-checks=on");
455     // Finally run it!
456     if command.status().expect("failed to run xargo").success().not() {
457         show_error(format!("failed to run xargo"));
458     }
459 
460     // That should be it! But we need to figure out where xargo built stuff.
461     // Unfortunately, it puts things into a different directory when the
462     // architecture matches the host.
463     let sysroot = if target == &host { dir.join("HOST") } else { PathBuf::from(dir) };
464     std::env::set_var("MIRI_SYSROOT", &sysroot); // pass the env var to the processes we spawn, which will turn it into "--sysroot" flags
465     // Figure out what to print.
466     let print_sysroot = subcommand == MiriCommand::Setup && has_arg_flag("--print-sysroot"); // whether we just print the sysroot path
467     if print_sysroot {
468         // Print just the sysroot and nothing else; this way we do not need any escaping.
469         println!("{}", sysroot.display());
470     } else if subcommand == MiriCommand::Setup {
471         println!("A libstd for Miri is now available in `{}`.", sysroot.display());
472     }
473 }
474 
475 /// Detect the target directory by calling `cargo metadata`.
detect_target_dir() -> PathBuf476 fn detect_target_dir() -> PathBuf {
477     #[derive(Deserialize)]
478     struct Metadata {
479         target_directory: PathBuf,
480     }
481     let mut cmd = cargo();
482     // `-Zunstable-options` is required by `--config`.
483     cmd.args(["metadata", "--no-deps", "--format-version=1", "-Zunstable-options"]);
484     // The `build.target-dir` config can be passed by `--config` flags, so forward them to
485     // `cargo metadata`.
486     let config_flag = "--config";
487     for arg in ArgSplitFlagValue::new(
488         env::args().skip(3), // skip the program name, "miri" and "run" / "test"
489         config_flag,
490     ) {
491         if let Ok(config) = arg {
492             cmd.arg(config_flag).arg(config);
493         }
494     }
495     let mut child = cmd
496         .stdin(process::Stdio::null())
497         .stdout(process::Stdio::piped())
498         .spawn()
499         .expect("failed ro run `cargo metadata`");
500     // Check this `Result` after `status.success()` is checked, so we don't print the error
501     // to stderr if `cargo metadata` is also printing to stderr.
502     let metadata: Result<Metadata, _> = serde_json::from_reader(child.stdout.take().unwrap());
503     let status = child.wait().expect("failed to wait for `cargo metadata` to exit");
504     if !status.success() {
505         std::process::exit(status.code().unwrap_or(-1));
506     }
507     metadata
508         .unwrap_or_else(|e| show_error(format!("invalid `cargo metadata` output: {}", e)))
509         .target_directory
510 }
511 
phase_cargo_miri(mut args: env::Args)512 fn phase_cargo_miri(mut args: env::Args) {
513     // Check for version and help flags even when invoked as `cargo-miri`.
514     if has_arg_flag("--help") || has_arg_flag("-h") {
515         show_help();
516         return;
517     }
518     if has_arg_flag("--version") || has_arg_flag("-V") {
519         show_version();
520         return;
521     }
522 
523     // Require a subcommand before any flags.
524     // We cannot know which of those flags take arguments and which do not,
525     // so we cannot detect subcommands later.
526     let subcommand = match args.next().as_deref() {
527         Some("test" | "t") => MiriCommand::Test,
528         Some("run" | "r") => MiriCommand::Run,
529         Some("setup") => MiriCommand::Setup,
530         // Invalid command.
531         _ =>
532             show_error(format!(
533                 "`cargo miri` supports the following subcommands: `run`, `test`, and `setup`."
534             )),
535     };
536     let verbose = has_arg_flag("-v");
537 
538     // We always setup.
539     setup(subcommand);
540 
541     // Invoke actual cargo for the job, but with different flags.
542     // We re-use `cargo test` and `cargo run`, which makes target and binary handling very easy but
543     // requires some extra work to make the build check-only (see all the `--emit` hacks below).
544     // <https://github.com/rust-lang/miri/pull/1540#issuecomment-693553191> describes an alternative
545     // approach that uses `cargo check`, making that part easier but target and binary handling
546     // harder.
547     let cargo_miri_path = std::env::current_exe().expect("current executable path invalid");
548     let cargo_cmd = match subcommand {
549         MiriCommand::Test => "test",
550         MiriCommand::Run => "run",
551         MiriCommand::Setup => return, // `cargo miri setup` stops here.
552     };
553     let mut cmd = cargo();
554     cmd.arg(cargo_cmd);
555 
556     // Make sure we know the build target, and cargo does, too.
557     // This is needed to make the `CARGO_TARGET_*_RUNNER` env var do something,
558     // and it later helps us detect which crates are proc-macro/build-script
559     // (host crates) and which crates are needed for the program itself.
560     let host = version_info().host;
561     let target = get_arg_flag_value("--target");
562     let target = if let Some(ref target) = target {
563         target
564     } else {
565         // No target given. Pick default and tell cargo about it.
566         cmd.arg("--target");
567         cmd.arg(&host);
568         &host
569     };
570 
571     let mut target_dir = None;
572 
573     // Forward all arguments before `--` other than `--target-dir` and its value to Cargo.
574     for arg in ArgSplitFlagValue::new(&mut args, "--target-dir") {
575         match arg {
576             Ok(value) => {
577                 if target_dir.is_some() {
578                     show_error(format!("`--target-dir` is provided more than once"));
579                 }
580                 target_dir = Some(value.into());
581             }
582             Err(arg) => {
583                 cmd.arg(arg);
584             }
585         }
586     }
587 
588     // Detect the target directory if it's not specified via `--target-dir`.
589     let target_dir = target_dir.get_or_insert_with(detect_target_dir);
590 
591     // Set `--target-dir` to `miri` inside the original target directory.
592     target_dir.push("miri");
593     cmd.arg("--target-dir").arg(target_dir);
594 
595     // Forward all further arguments after `--` to cargo.
596     cmd.arg("--").args(args);
597 
598     // Set `RUSTC_WRAPPER` to ourselves.  Cargo will prepend that binary to its usual invocation,
599     // i.e., the first argument is `rustc` -- which is what we use in `main` to distinguish
600     // the two codepaths. (That extra argument is why we prefer this over setting `RUSTC`.)
601     if env::var_os("RUSTC_WRAPPER").is_some() {
602         println!(
603             "WARNING: Ignoring `RUSTC_WRAPPER` environment variable, Miri does not support wrapping."
604         );
605     }
606     cmd.env("RUSTC_WRAPPER", &cargo_miri_path);
607 
608     let runner_env_name =
609         |triple: &str| format!("CARGO_TARGET_{}_RUNNER", triple.to_uppercase().replace('-', "_"));
610     let host_runner_env_name = runner_env_name(&host);
611     let target_runner_env_name = runner_env_name(target);
612     // Set the target runner to us, so we can interpret the binaries.
613     cmd.env(&target_runner_env_name, &cargo_miri_path);
614     // Unit tests of `proc-macro` crates are run on the host, so we set the host runner to
615     // us in order to skip them.
616     cmd.env(&host_runner_env_name, &cargo_miri_path);
617 
618     // Set rustdoc to us as well, so we can run doctests.
619     cmd.env("RUSTDOC", &cargo_miri_path);
620 
621     // Run cargo.
622     if verbose {
623         eprintln!("[cargo-miri miri] RUSTC_WRAPPER={:?}", cargo_miri_path);
624         eprintln!("[cargo-miri miri] {}={:?}", target_runner_env_name, cargo_miri_path);
625         if *target != host {
626             eprintln!("[cargo-miri miri] {}={:?}", host_runner_env_name, cargo_miri_path);
627         }
628         eprintln!("[cargo-miri miri] RUSTDOC={:?}", cargo_miri_path);
629         eprintln!("[cargo-miri miri] {:?}", cmd);
630         cmd.env("MIRI_VERBOSE", ""); // This makes the other phases verbose.
631     }
632     exec(cmd)
633 }
634 
635 #[derive(Debug, Copy, Clone, PartialEq)]
636 enum RustcPhase {
637     /// `rustc` called via `xargo` for sysroot build.
638     Setup,
639     /// `rustc` called by `cargo` for regular build.
640     Build,
641     /// `rustc` called by `rustdoc` for doctest.
642     Rustdoc,
643 }
644 
phase_rustc(mut args: env::Args, phase: RustcPhase)645 fn phase_rustc(mut args: env::Args, phase: RustcPhase) {
646     /// Determines if we are being invoked (as rustc) to build a crate for
647     /// the "target" architecture, in contrast to the "host" architecture.
648     /// Host crates are for build scripts and proc macros and still need to
649     /// be built like normal; target crates need to be built for or interpreted
650     /// by Miri.
651     ///
652     /// Currently, we detect this by checking for "--target=", which is
653     /// never set for host crates. This matches what rustc bootstrap does,
654     /// which hopefully makes it "reliable enough". This relies on us always
655     /// invoking cargo itself with `--target`, which `in_cargo_miri` ensures.
656     fn is_target_crate() -> bool {
657         get_arg_flag_value("--target").is_some()
658     }
659 
660     /// Returns whether or not Cargo invoked the wrapper (this binary) to compile
661     /// the final, binary crate (either a test for 'cargo test', or a binary for 'cargo run')
662     /// Cargo does not give us this information directly, so we need to check
663     /// various command-line flags.
664     fn is_runnable_crate() -> bool {
665         let is_bin = get_arg_flag_value("--crate-type").as_deref().unwrap_or("bin") == "bin";
666         let is_test = has_arg_flag("--test");
667         is_bin || is_test
668     }
669 
670     fn out_filename(prefix: &str, suffix: &str) -> PathBuf {
671         if let Some(out_dir) = get_arg_flag_value("--out-dir") {
672             let mut path = PathBuf::from(out_dir);
673             path.push(format!(
674                 "{}{}{}{}",
675                 prefix,
676                 get_arg_flag_value("--crate-name").unwrap(),
677                 // This is technically a `-C` flag but the prefix seems unique enough...
678                 // (and cargo passes this before the filename so it should be unique)
679                 get_arg_flag_value("extra-filename").unwrap_or(String::new()),
680                 suffix,
681             ));
682             path
683         } else {
684             let out_file = get_arg_flag_value("-o").unwrap();
685             PathBuf::from(out_file)
686         }
687     }
688 
689     let verbose = std::env::var_os("MIRI_VERBOSE").is_some();
690     let target_crate = is_target_crate();
691     let print = get_arg_flag_value("--print").is_some() || has_arg_flag("-vV"); // whether this is cargo/xargo invoking rustc to get some infos
692 
693     let store_json = |info: CrateRunInfo| {
694         // Create a stub .d file to stop Cargo from "rebuilding" the crate:
695         // https://github.com/rust-lang/miri/issues/1724#issuecomment-787115693
696         // As we store a JSON file instead of building the crate here, an empty file is fine.
697         let dep_info_name = out_filename("", ".d");
698         if verbose {
699             eprintln!("[cargo-miri rustc] writing stub dep-info to `{}`", dep_info_name.display());
700         }
701         File::create(dep_info_name).expect("failed to create fake .d file");
702 
703         let filename = out_filename("", "");
704         if verbose {
705             eprintln!("[cargo-miri rustc] writing run info to `{}`", filename.display());
706         }
707         info.store(&filename);
708         // For Windows, do the same thing again with `.exe` appended to the filename.
709         // (Need to do this here as cargo moves that "binary" to a different place before running it.)
710         info.store(&out_filename("", ".exe"));
711     };
712 
713     let runnable_crate = !print && is_runnable_crate();
714 
715     if runnable_crate && target_crate {
716         assert!(
717             phase != RustcPhase::Setup,
718             "there should be no interpretation during sysroot build"
719         );
720         let inside_rustdoc = phase == RustcPhase::Rustdoc;
721         // This is the binary or test crate that we want to interpret under Miri.
722         // But we cannot run it here, as cargo invoked us as a compiler -- our stdin and stdout are not
723         // like we want them.
724         // Instead of compiling, we write JSON into the output file with all the relevant command-line flags
725         // and environment variables; this is used when cargo calls us again in the CARGO_TARGET_RUNNER phase.
726         let env = CrateRunEnv::collect(args, inside_rustdoc);
727 
728         // Rustdoc expects us to exit with an error code if the test is marked as `compile_fail`,
729         // just creating the JSON file is not enough: we need to detect syntax errors,
730         // so we need to run Miri with `MIRI_BE_RUSTC` for a check-only build.
731         if inside_rustdoc {
732             let mut cmd = miri();
733 
734             // Ensure --emit argument for a check-only build is present.
735             // We cannot use the usual helpers since we need to check specifically in `env.args`.
736             if let Some(i) = env.args.iter().position(|arg| arg.starts_with("--emit=")) {
737                 // For `no_run` tests, rustdoc passes a `--emit` flag; make sure it has the right shape.
738                 assert_eq!(env.args[i], "--emit=metadata");
739             } else {
740                 // For all other kinds of tests, we can just add our flag.
741                 cmd.arg("--emit=metadata");
742             }
743 
744             cmd.args(&env.args);
745             cmd.env("MIRI_BE_RUSTC", "target");
746 
747             if verbose {
748                 eprintln!(
749                     "[cargo-miri rustc] captured input:\n{}",
750                     std::str::from_utf8(&env.stdin).unwrap()
751                 );
752                 eprintln!("[cargo-miri rustc] {:?}", cmd);
753             }
754 
755             exec_with_pipe(cmd, &env.stdin);
756         }
757 
758         store_json(CrateRunInfo::RunWith(env));
759 
760         return;
761     }
762 
763     if runnable_crate && ArgFlagValueIter::new("--extern").any(|krate| krate == "proc_macro") {
764         // This is a "runnable" `proc-macro` crate (unit tests). We do not support
765         // interpreting that under Miri now, so we write a JSON file to (display a
766         // helpful message and) skip it in the runner phase.
767         store_json(CrateRunInfo::SkipProcMacroTest);
768         return;
769     }
770 
771     let mut cmd = miri();
772     let mut emit_link_hack = false;
773     // Arguments are treated very differently depending on whether this crate is
774     // for interpretation by Miri, or for use by a build script / proc macro.
775     if !print && target_crate {
776         // Forward arguments, but remove "link" from "--emit" to make this a check-only build.
777         let emit_flag = "--emit";
778         while let Some(arg) = args.next() {
779             if arg.starts_with(emit_flag) {
780                 // Patch this argument. First, extract its value.
781                 let val = &arg[emit_flag.len()..];
782                 assert!(val.starts_with("="), "`cargo` should pass `--emit=X` as one argument");
783                 let val = &val[1..];
784                 let mut val: Vec<_> = val.split(',').collect();
785                 // Now make sure "link" is not in there, but "metadata" is.
786                 if let Some(i) = val.iter().position(|&s| s == "link") {
787                     emit_link_hack = true;
788                     val.remove(i);
789                     if !val.iter().any(|&s| s == "metadata") {
790                         val.push("metadata");
791                     }
792                 }
793                 cmd.arg(format!("{}={}", emit_flag, val.join(",")));
794             } else if arg == "--extern" {
795                 // Patch `--extern` filenames, since Cargo sometimes passes stub `.rlib` files:
796                 // https://github.com/rust-lang/miri/issues/1705
797                 forward_patched_extern_arg(&mut args, &mut cmd);
798             } else {
799                 cmd.arg(arg);
800             }
801         }
802 
803         // Use our custom sysroot (but not if that is what we are currently building).
804         if phase != RustcPhase::Setup {
805             forward_miri_sysroot(&mut cmd);
806         }
807 
808         // During setup, patch the panic runtime for `libpanic_abort` (mirroring what bootstrap usually does).
809         if phase == RustcPhase::Setup
810             && get_arg_flag_value("--crate-name").as_deref() == Some("panic_abort")
811         {
812             cmd.arg("-C").arg("panic=abort");
813         }
814     } else {
815         // For host crates or when we are printing, just forward everything.
816         cmd.args(args);
817     }
818 
819     // We want to compile, not interpret. We still use Miri to make sure the compiler version etc
820     // are the exact same as what is used for interpretation.
821     // MIRI_DEFAULT_ARGS should not be used to build host crates, hence setting "target" or "host"
822     // as the value here to help Miri differentiate them.
823     cmd.env("MIRI_BE_RUSTC", if target_crate { "target" } else { "host" });
824 
825     // Run it.
826     if verbose {
827         eprintln!("[cargo-miri rustc] {:?}", cmd);
828     }
829     exec(cmd);
830 
831     // Create a stub .rlib file if "link" was requested by cargo.
832     // This is necessary to prevent cargo from doing rebuilds all the time.
833     if emit_link_hack {
834         // Some platforms prepend "lib", some do not... let's just create both files.
835         File::create(out_filename("lib", ".rlib")).expect("failed to create fake .rlib file");
836         File::create(out_filename("", ".rlib")).expect("failed to create fake .rlib file");
837         // Just in case this is a cdylib or staticlib, also create those fake files.
838         File::create(out_filename("lib", ".so")).expect("failed to create fake .so file");
839         File::create(out_filename("lib", ".a")).expect("failed to create fake .a file");
840         File::create(out_filename("lib", ".dylib")).expect("failed to create fake .dylib file");
841         File::create(out_filename("", ".dll")).expect("failed to create fake .dll file");
842         File::create(out_filename("", ".lib")).expect("failed to create fake .lib file");
843     }
844 }
845 
846 #[derive(Debug, Copy, Clone, PartialEq)]
847 enum RunnerPhase {
848     /// `cargo` is running a binary
849     Cargo,
850     /// `rustdoc` is running a binary
851     Rustdoc,
852 }
853 
phase_runner(binary: &Path, binary_args: env::Args, phase: RunnerPhase)854 fn phase_runner(binary: &Path, binary_args: env::Args, phase: RunnerPhase) {
855     let verbose = std::env::var_os("MIRI_VERBOSE").is_some();
856 
857     let file = File::open(&binary)
858         .unwrap_or_else(|_| show_error(format!("file {:?} not found or `cargo-miri` invoked incorrectly; please only invoke this binary through `cargo miri`", binary)));
859     let file = BufReader::new(file);
860 
861     let info = serde_json::from_reader(file).unwrap_or_else(|_| {
862         show_error(format!(
863             "file {:?} contains outdated or invalid JSON; try `cargo clean`",
864             binary
865         ))
866     });
867     let info = match info {
868         CrateRunInfo::RunWith(info) => info,
869         CrateRunInfo::SkipProcMacroTest => {
870             eprintln!(
871                 "Running unit tests of `proc-macro` crates is not currently supported by Miri."
872             );
873             return;
874         }
875     };
876 
877     let mut cmd = miri();
878 
879     // Set missing env vars. We prefer build-time env vars over run-time ones; see
880     // <https://github.com/rust-lang/miri/issues/1661> for the kind of issue that fixes.
881     for (name, val) in info.env {
882         if verbose {
883             if let Some(old_val) = env::var_os(&name) {
884                 if old_val != val {
885                     eprintln!(
886                         "[cargo-miri runner] Overwriting run-time env var {:?}={:?} with build-time value {:?}",
887                         name, old_val, val
888                     );
889                 }
890             }
891         }
892         cmd.env(name, val);
893     }
894 
895     // Forward rustc arguments.
896     // We need to patch "--extern" filenames because we forced a check-only
897     // build without cargo knowing about that: replace `.rlib` suffix by
898     // `.rmeta`.
899     // We also need to remove `--error-format` as cargo specifies that to be JSON,
900     // but when we run here, cargo does not interpret the JSON any more. `--json`
901     // then also nees to be dropped.
902     let mut args = info.args.into_iter();
903     let error_format_flag = "--error-format";
904     let json_flag = "--json";
905     while let Some(arg) = args.next() {
906         if arg == "--extern" {
907             forward_patched_extern_arg(&mut args, &mut cmd);
908         } else if arg.starts_with(error_format_flag) {
909             let suffix = &arg[error_format_flag.len()..];
910             assert!(suffix.starts_with('='));
911             // Drop this argument.
912         } else if arg.starts_with(json_flag) {
913             let suffix = &arg[json_flag.len()..];
914             assert!(suffix.starts_with('='));
915             // Drop this argument.
916         } else {
917             cmd.arg(arg);
918         }
919     }
920     // Set sysroot (if we are inside rustdoc, we already did that in `phase_cargo_rustdoc`).
921     if phase != RunnerPhase::Rustdoc {
922         forward_miri_sysroot(&mut cmd);
923     }
924     // Respect `MIRIFLAGS`.
925     if let Ok(a) = env::var("MIRIFLAGS") {
926         // This code is taken from `RUSTFLAGS` handling in cargo.
927         let args = a.split(' ').map(str::trim).filter(|s| !s.is_empty()).map(str::to_string);
928         cmd.args(args);
929     }
930 
931     // Then pass binary arguments.
932     cmd.arg("--");
933     cmd.args(binary_args);
934 
935     // Make sure we use the build-time working directory for interpreting Miri/rustc arguments.
936     // But then we need to switch to the run-time one, which we instruct Miri do do by setting `MIRI_CWD`.
937     cmd.current_dir(info.current_dir);
938     cmd.env("MIRI_CWD", env::current_dir().unwrap());
939 
940     // Run it.
941     if verbose {
942         eprintln!("[cargo-miri runner] {:?}", cmd);
943     }
944 
945     match phase {
946         RunnerPhase::Rustdoc => exec_with_pipe(cmd, &info.stdin),
947         RunnerPhase::Cargo => exec(cmd),
948     }
949 }
950 
phase_rustdoc(fst_arg: &str, mut args: env::Args)951 fn phase_rustdoc(fst_arg: &str, mut args: env::Args) {
952     let verbose = std::env::var_os("MIRI_VERBOSE").is_some();
953 
954     // phase_cargo_miri sets the RUSTDOC env var to ourselves, so we can't use that here;
955     // just default to a straight-forward invocation for now:
956     let mut cmd = Command::new("rustdoc");
957 
958     // Because of the way the main function is structured, we have to take the first argument spearately
959     // from the rest; to simplify the following argument patching loop, we'll just skip that one.
960     // This is fine for now, because cargo will never pass --extern arguments in the first position,
961     // but we should defensively assert that this will work.
962     let extern_flag = "--extern";
963     assert!(fst_arg != extern_flag);
964     cmd.arg(fst_arg);
965 
966     let runtool_flag = "--runtool";
967     // `crossmode` records if *any* argument matches `runtool_flag`; here we check the first one.
968     let mut crossmode = fst_arg == runtool_flag;
969     while let Some(arg) = args.next() {
970         if arg == extern_flag {
971             // Patch --extern arguments to use *.rmeta files, since phase_cargo_rustc only creates stub *.rlib files.
972             forward_patched_extern_arg(&mut args, &mut cmd);
973         } else if arg == runtool_flag {
974             // An existing --runtool flag indicates cargo is running in cross-target mode, which we don't support.
975             // Note that this is only passed when cargo is run with the unstable -Zdoctest-xcompile flag;
976             // otherwise, we won't be called as rustdoc at all.
977             crossmode = true;
978             break;
979         } else {
980             cmd.arg(arg);
981         }
982     }
983 
984     if crossmode {
985         show_error(format!("cross-interpreting doctests is not currently supported by Miri."));
986     }
987 
988     // Doctests of `proc-macro` crates (and their dependencies) are always built for the host,
989     // so we are not able to run them in Miri.
990     if ArgFlagValueIter::new("--crate-type").any(|crate_type| crate_type == "proc-macro") {
991         eprintln!("Running doctests of `proc-macro` crates is not currently supported by Miri.");
992         return;
993     }
994 
995     // For each doctest, rustdoc starts two child processes: first the test is compiled,
996     // then the produced executable is invoked. We want to reroute both of these to cargo-miri,
997     // such that the first time we'll enter phase_cargo_rustc, and phase_cargo_runner second.
998     //
999     // rustdoc invokes the test-builder by forwarding most of its own arguments, which makes
1000     // it difficult to determine when phase_cargo_rustc should run instead of phase_cargo_rustdoc.
1001     // Furthermore, the test code is passed via stdin, rather than a temporary file, so we need
1002     // to let phase_cargo_rustc know to expect that. We'll use this environment variable as a flag:
1003     cmd.env("MIRI_CALLED_FROM_RUSTDOC", "1");
1004 
1005     // The `--test-builder` and `--runtool` arguments are unstable rustdoc features,
1006     // which are disabled by default. We first need to enable them explicitly:
1007     cmd.arg("-Z").arg("unstable-options");
1008 
1009     // rustdoc needs to know the right sysroot.
1010     forward_miri_sysroot(&mut cmd);
1011     // make sure the 'miri' flag is set for rustdoc
1012     cmd.arg("--cfg").arg("miri");
1013 
1014     // Make rustdoc call us back.
1015     let cargo_miri_path = std::env::current_exe().expect("current executable path invalid");
1016     cmd.arg("--test-builder").arg(&cargo_miri_path); // invoked by forwarding most arguments
1017     cmd.arg("--runtool").arg(&cargo_miri_path); // invoked with just a single path argument
1018 
1019     if verbose {
1020         eprintln!("[cargo-miri rustdoc] {:?}", cmd);
1021     }
1022 
1023     exec(cmd)
1024 }
1025 
main()1026 fn main() {
1027     // Rustc does not support non-UTF-8 arguments so we make no attempt either.
1028     // (We do support non-UTF-8 environment variables though.)
1029     let mut args = std::env::args();
1030     // Skip binary name.
1031     args.next().unwrap();
1032 
1033     // Dispatch running as part of sysroot compilation.
1034     if env::var_os("MIRI_CALLED_FROM_XARGO").is_some() {
1035         phase_rustc(args, RustcPhase::Setup);
1036         return;
1037     }
1038 
1039     // The way rustdoc invokes rustc is indistuingishable from the way cargo invokes rustdoc by the
1040     // arguments alone. `phase_cargo_rustdoc` sets this environment variable to let us disambiguate.
1041     if env::var_os("MIRI_CALLED_FROM_RUSTDOC").is_some() {
1042         // ...however, we then also see this variable when rustdoc invokes us as the testrunner!
1043         // The runner is invoked as `$runtool ($runtool-arg)* output_file`;
1044         // since we don't specify any runtool-args, and rustdoc supplies multiple arguments to
1045         // the test-builder unconditionally, we can just check the number of remaining arguments:
1046         if args.len() == 1 {
1047             let arg = args.next().unwrap();
1048             let binary = Path::new(&arg);
1049             if binary.exists() {
1050                 phase_runner(binary, args, RunnerPhase::Rustdoc);
1051             } else {
1052                 show_error(format!(
1053                     "`cargo-miri` called with non-existing path argument `{}` in rustdoc mode; please invoke this binary through `cargo miri`",
1054                     arg
1055                 ));
1056             }
1057         } else {
1058             phase_rustc(args, RustcPhase::Rustdoc);
1059         }
1060 
1061         return;
1062     }
1063 
1064     // Dispatch to `cargo-miri` phase. There are three phases:
1065     // - When we are called via `cargo miri`, we run as the frontend and invoke the underlying
1066     //   cargo. We set RUSTC_WRAPPER and CARGO_TARGET_RUNNER to ourselves.
1067     // - When we are executed due to RUSTC_WRAPPER, we build crates or store the flags of
1068     //   binary crates for later interpretation.
1069     // - When we are executed due to CARGO_TARGET_RUNNER, we start interpretation based on the
1070     //   flags that were stored earlier.
1071     // On top of that, we are also called as RUSTDOC, but that is just a stub currently.
1072     match args.next().as_deref() {
1073         Some("miri") => phase_cargo_miri(args),
1074         Some("rustc") => phase_rustc(args, RustcPhase::Build),
1075         Some(arg) => {
1076             // We have to distinguish the "runner" and "rustdoc" cases.
1077             // As runner, the first argument is the binary (a file that should exist, with an absolute path);
1078             // as rustdoc, the first argument is a flag (`--something`).
1079             let binary = Path::new(arg);
1080             if binary.exists() {
1081                 assert!(!arg.starts_with("--")); // not a flag
1082                 phase_runner(binary, args, RunnerPhase::Cargo);
1083             } else if arg.starts_with("--") {
1084                 phase_rustdoc(arg, args);
1085             } else {
1086                 show_error(format!(
1087                     "`cargo-miri` called with unexpected first argument `{}`; please only invoke this binary through `cargo miri`",
1088                     arg
1089                 ));
1090             }
1091         }
1092         _ =>
1093             show_error(format!(
1094                 "`cargo-miri` called without first argument; please only invoke this binary through `cargo miri`"
1095             )),
1096     }
1097 }
1098