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