From 7e32c0f9025cce42d707eef36409c2394d86c561 Mon Sep 17 00:00:00 2001 From: Aleksei Trifonov Date: Sat, 30 Jul 2022 00:14:56 +0300 Subject: [PATCH 1/3] Fetch miri flags from cargo config --- cargo-miri/src/phases.rs | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/cargo-miri/src/phases.rs b/cargo-miri/src/phases.rs index 4ba627de48..2e3ea70f11 100644 --- a/cargo-miri/src/phases.rs +++ b/cargo-miri/src/phases.rs @@ -6,6 +6,7 @@ use std::fs::{self, File}; use std::io::BufReader; use std::path::PathBuf; use std::process::Command; +use std::str; use crate::{setup::*, util::*}; @@ -517,11 +518,10 @@ pub fn phase_runner(mut binary_args: impl Iterator, phase: Runner cmd.arg(arg); } } - // Respect `MIRIFLAGS`. - if let Ok(a) = env::var("MIRIFLAGS") { - // This code is taken from `RUSTFLAGS` handling in cargo. - let args = a.split(' ').map(str::trim).filter(|s| !s.is_empty()).map(str::to_string); - cmd.args(args); + + // Respect flags. + if let Some(flags) = get_miriflags() { + cmd.args(flags); } // Then pass binary arguments. @@ -541,6 +541,27 @@ pub fn phase_runner(mut binary_args: impl Iterator, phase: Runner } } +fn get_miriflags() -> Option> { + // Fetch miri flags from cargo config. + let mut cmd = cargo(); + cmd.args(["-Zunstable-options", "config", "get", "miri.flags", "--format=json-value"]); + let output = cmd.output().expect("failed to run `cargo config`"); + let config_miriflags = + str::from_utf8(&output.stdout).expect("failed to get `cargo config` output"); + + // Respect `MIRIFLAGS` and `miri.flags` setting in cargo config. + // If MIRIFLAGS is present, flags from cargo config are ignored. + // This matches cargo behavior for RUSTFLAGS. + if let Ok(a) = env::var("MIRIFLAGS") { + // This code is taken from `RUSTFLAGS` handling in cargo. + Some(a.split(' ').map(str::trim).filter(|s| !s.is_empty()).map(str::to_string).collect()) + } else if let Ok(args) = serde_json::from_str::>(config_miriflags) { + Some(args) + } else { + None + } +} + pub fn phase_rustdoc(mut args: impl Iterator) { let verbose = std::env::var("MIRI_VERBOSE") .map_or(0, |verbose| verbose.parse().expect("verbosity flag must be an integer")); From f0559b985fedb7b2b4aa068d243246026742eab3 Mon Sep 17 00:00:00 2001 From: Aleksei Trifonov Date: Thu, 4 Aug 2022 04:59:55 +0300 Subject: [PATCH 2/3] Add tests for fetching flags from from cargo config --- test-cargo-miri/run-test.py | 46 ++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/test-cargo-miri/run-test.py b/test-cargo-miri/run-test.py index 4485d3252c..7991c7cdb9 100755 --- a/test-cargo-miri/run-test.py +++ b/test-cargo-miri/run-test.py @@ -61,7 +61,7 @@ def test(name, cmd, stdout_ref, stderr_ref, stdin=b'', env={}): stdout_matches = check_output(stdout, stdout_ref, "stdout") stderr_matches = check_output(stderr, stderr_ref, "stderr") - + if p.returncode == 0 and stdout_matches and stderr_matches: # All good! return @@ -90,6 +90,12 @@ def test_no_rebuild(name, cmd, env={}): fail("Something was being rebuilt when it should not be (or we got no output)"); def test_cargo_miri_run(): + # Try to remove cargo config to avoid unexpected settings + try: + os.remove('.cargo/config.toml') + except OSError: + pass + test("`cargo miri run` (no isolation)", cargo_miri("run"), "run.default.stdout.ref", "run.default.stderr.ref", @@ -99,6 +105,44 @@ def test_cargo_miri_run(): 'MIRITESTVAR': "wrongval", # make sure the build.rs value takes precedence }, ) + + # Create a config with isolation disabled + try: + os.mkdir('.cargo') + except OSError: + pass + + with open(".cargo/config.toml", "w") as f: + f.write('[miri]\nflags = ["-Zmiri-disable-isolation"]') + + # Testing miri.flags are taken in account + test("`cargo miri run` (no isolation, set from cargo config)", + cargo_miri("run"), + "run.default.stdout.ref", "run.default.stderr.ref", + stdin=b'12\n21\n', + env={ + 'MIRITESTVAR': "wrongval", # make sure the build.rs value takes precedence + }, + ) + + # Create an empty config + with open(".cargo/config.toml", "w") as f: + f.write('[miri]\nflags = [""]') + + # Check that MIRIFLAGS envar has higher precedence tha cargo config + test("`cargo miri run` (no isolation, ignoring cargo config)", + cargo_miri("run"), + "run.default.stdout.ref", "run.default.stderr.ref", + stdin=b'12\n21\n', + env={ + 'MIRIFLAGS': "-Zmiri-disable-isolation", + 'MIRITESTVAR': "wrongval", # make sure the build.rs value takes precedence + }, + ) + + # Cleaning up config after all config-related tests + os.remove('.cargo/config.toml') + # Special test: run it again *without* `-q` to make sure nothing is being rebuilt (Miri issue #1722) test_no_rebuild("`cargo miri run` (no rebuild)", cargo_miri("run", quiet=False) + ["--", ""], From 56ee2671e29441cdf7789081e5cfdfb70190b9e6 Mon Sep 17 00:00:00 2001 From: Aleksei Trifonov Date: Thu, 4 Aug 2022 06:26:20 +0300 Subject: [PATCH 3/3] Add info on setting miriflags in cargo config --- README.md | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5fbf89c86b..4648a1350c 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,8 @@ their name. You can pass arguments to Miri via `MIRIFLAGS`. For example, `MIRIFLAGS="-Zmiri-disable-stacked-borrows" cargo miri run` runs the program -without checking the aliasing of references. +without checking the aliasing of references. Also, you can set the `miri.flags` +setting in [cargo configuration](https://doc.rust-lang.org/cargo/reference/config.html). When compiling code via `cargo miri`, the `cfg(miri)` config flag is set for code that will be interpret under Miri. You can use this to ignore test cases that fail @@ -262,7 +263,22 @@ up the sysroot. If you are using `miri` (the Miri driver) directly, see the [miri-flags]: #miri--z-flags-and-environment-variables Miri adds its own set of `-Z` flags, which are usually set via the `MIRIFLAGS` -environment variable. We first document the most relevant and most commonly used flags: +environment variable or using the `miri.flags` setting in +[cargo configuration](https://doc.rust-lang.org/cargo/reference/config.html), +they are mutually exclusive with priority of `MIRIFLAGS` environment variable: + +```bash +MIRIFLAGS="-Zmiri-disable-stacked-borrows" cargo miri run +``` + +```toml +# .cargo/config.toml + +[miri] +flags = ["-Zmiri-disable-isolation", "-Zmiri-report-progress"] +``` + +We first document the most relevant and most commonly used flags: * `-Zmiri-compare-exchange-weak-failure-rate=` changes the failure rate of `compare_exchange_weak` operations. The default is `0.8` (so 4 out of 5 weak ops will fail).