Skip to content

Commit 46bbf28

Browse files
authored
Rollup merge of #148734 - RalfJung:miri, r=RalfJung
miri subtree update Subtree update of `miri` to rust-lang/miri@7d5ae36. Created using https://github.com/rust-lang/josh-sync. r? `@ghost`
2 parents 25b3db8 + e14780a commit 46bbf28

File tree

63 files changed

+793
-673
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+793
-673
lines changed

src/tools/miri/cargo-miri/src/phases.rs

Lines changed: 8 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,6 @@ fn show_version() {
5252
println!();
5353
}
5454

55-
fn forward_patched_extern_arg(args: &mut impl Iterator<Item = String>, cmd: &mut Command) {
56-
cmd.arg("--extern"); // always forward flag, but adjust filename:
57-
let path = args.next().expect("`--extern` should be followed by a filename");
58-
if let Some(lib) = path.strip_suffix(".rlib") {
59-
// If this is an rlib, make it an rmeta.
60-
cmd.arg(format!("{lib}.rmeta"));
61-
} else {
62-
// Some other extern file (e.g. a `.so`). Forward unchanged.
63-
cmd.arg(path);
64-
}
65-
}
66-
6755
pub fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
6856
// Require a subcommand before any flags.
6957
// We cannot know which of those flags take arguments and which do not,
@@ -276,7 +264,7 @@ pub enum RustcPhase {
276264
Rustdoc,
277265
}
278266

279-
pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
267+
pub fn phase_rustc(args: impl Iterator<Item = String>, phase: RustcPhase) {
280268
/// Determines if we are being invoked (as rustc) to build a crate for
281269
/// the "target" architecture, in contrast to the "host" architecture.
282270
/// Host crates are for build scripts and proc macros and still need to
@@ -444,7 +432,6 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
444432
}
445433

446434
let mut cmd = miri();
447-
let mut emit_link_hack = false;
448435
// Arguments are treated very differently depending on whether this crate is
449436
// for interpretation by Miri, or for use by a build script / proc macro.
450437
if target_crate {
@@ -455,7 +442,7 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
455442
}
456443

457444
// Forward arguments, but patched.
458-
let emit_flag = "--emit";
445+
459446
// This hack helps bootstrap run standard library tests in Miri. The issue is as follows:
460447
// when running `cargo miri test` on libcore, cargo builds a local copy of core and makes it
461448
// a dependency of the integration test crate. This copy duplicates all the lang items, so
@@ -471,30 +458,7 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
471458
let replace_librs = env::var_os("MIRI_REPLACE_LIBRS_IF_NOT_TEST").is_some()
472459
&& !runnable_crate
473460
&& phase == RustcPhase::Build;
474-
while let Some(arg) = args.next() {
475-
// Patch `--emit`: remove "link" from "--emit" to make this a check-only build.
476-
if let Some(val) = arg.strip_prefix(emit_flag) {
477-
// Patch this argument. First, extract its value.
478-
let val =
479-
val.strip_prefix('=').expect("`cargo` should pass `--emit=X` as one argument");
480-
let mut val: Vec<_> = val.split(',').collect();
481-
// Now make sure "link" is not in there, but "metadata" is.
482-
if let Some(i) = val.iter().position(|&s| s == "link") {
483-
emit_link_hack = true;
484-
val.remove(i);
485-
if !val.contains(&"metadata") {
486-
val.push("metadata");
487-
}
488-
}
489-
cmd.arg(format!("{emit_flag}={}", val.join(",")));
490-
continue;
491-
}
492-
// Patch `--extern` filenames, since Cargo sometimes passes stub `.rlib` files:
493-
// https://github.com/rust-lang/miri/issues/1705
494-
if arg == "--extern" {
495-
forward_patched_extern_arg(&mut args, &mut cmd);
496-
continue;
497-
}
461+
for arg in args {
498462
// If the REPLACE_LIBRS hack is enabled and we are building a `lib.rs` file, and a
499463
// `lib.miri.rs` file exists, then build that instead.
500464
if replace_librs {
@@ -543,17 +507,6 @@ pub fn phase_rustc(mut args: impl Iterator<Item = String>, phase: RustcPhase) {
543507
eprintln!("[cargo-miri rustc] target_crate={target_crate} runnable_crate={runnable_crate}");
544508
}
545509

546-
// Create a stub .rlib file if "link" was requested by cargo.
547-
// This is necessary to prevent cargo from doing rebuilds all the time.
548-
if emit_link_hack {
549-
for filename in out_filenames() {
550-
if verbose > 0 {
551-
eprintln!("[cargo-miri rustc] creating fake lib file at `{}`", filename.display());
552-
}
553-
File::create(filename).expect("failed to create fake lib file");
554-
}
555-
}
556-
557510
debug_cmd("[cargo-miri rustc]", verbose, &cmd);
558511
exec(cmd);
559512
}
@@ -624,17 +577,11 @@ pub fn phase_runner(mut binary_args: impl Iterator<Item = String>, phase: Runner
624577
cmd.arg("--sysroot").arg(env::var_os("MIRI_SYSROOT").unwrap());
625578
}
626579
// Forward rustc arguments.
627-
// We need to patch "--extern" filenames because we forced a check-only
628-
// build without cargo knowing about that: replace `.rlib` suffix by
629-
// `.rmeta`.
630-
// We also need to remove `--error-format` as cargo specifies that to be JSON,
580+
// We need to remove `--error-format` as cargo specifies that to be JSON,
631581
// but when we run here, cargo does not interpret the JSON any more. `--json`
632582
// then also needs to be dropped.
633-
let mut args = info.args.iter();
634-
while let Some(arg) = args.next() {
635-
if arg == "--extern" {
636-
forward_patched_extern_arg(&mut (&mut args).cloned(), &mut cmd);
637-
} else if let Some(suffix) = arg.strip_prefix("--error-format") {
583+
for arg in &info.args {
584+
if let Some(suffix) = arg.strip_prefix("--error-format") {
638585
assert!(suffix.starts_with('='));
639586
// Drop this argument.
640587
} else if let Some(suffix) = arg.strip_prefix("--json") {
@@ -668,23 +615,15 @@ pub fn phase_runner(mut binary_args: impl Iterator<Item = String>, phase: Runner
668615
}
669616
}
670617

671-
pub fn phase_rustdoc(mut args: impl Iterator<Item = String>) {
618+
pub fn phase_rustdoc(args: impl Iterator<Item = String>) {
672619
let verbose = env::var("MIRI_VERBOSE")
673620
.map_or(0, |verbose| verbose.parse().expect("verbosity flag must be an integer"));
674621

675622
// phase_cargo_miri sets the RUSTDOC env var to ourselves, and puts a backup
676623
// of the old value into MIRI_ORIG_RUSTDOC. So that's what we have to invoke now.
677624
let rustdoc = env::var("MIRI_ORIG_RUSTDOC").unwrap_or("rustdoc".to_string());
678625
let mut cmd = Command::new(rustdoc);
679-
680-
while let Some(arg) = args.next() {
681-
if arg == "--extern" {
682-
// Patch --extern arguments to use *.rmeta files, since phase_cargo_rustc only creates stub *.rlib files.
683-
forward_patched_extern_arg(&mut args, &mut cmd);
684-
} else {
685-
cmd.arg(arg);
686-
}
687-
}
626+
cmd.args(args);
688627

689628
// Doctests of `proc-macro` crates (and their dependencies) are always built for the host,
690629
// so we are not able to run them in Miri.

src/tools/miri/cargo-miri/src/setup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ pub fn setup(
160160

161161
// Do the build.
162162
let status = SysrootBuilder::new(&sysroot_dir, target)
163-
.build_mode(BuildMode::Check)
163+
.build_mode(BuildMode::Build) // not a real build, since we use dummy codegen
164164
.rustc_version(rustc_version.clone())
165165
.sysroot_config(sysroot_config)
166166
.rustflags(rustflags)

src/tools/miri/miri-script/src/commands.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ impl MiriEnv {
5757
.arg("--")
5858
.args(&["miri", "setup", "--print-sysroot"])
5959
.args(target_flag);
60-
cmd.set_quiet(quiet);
60+
if quiet {
61+
cmd = cmd.arg("--quiet");
62+
}
6163
let output = cmd.read()?;
6264
self.sh.set_var("MIRI_SYSROOT", &output);
6365
Ok(output.into())
@@ -112,8 +114,8 @@ impl Command {
112114
Command::Check { features, flags } => Self::check(features, flags),
113115
Command::Test { bless, target, coverage, features, flags } =>
114116
Self::test(bless, target, coverage, features, flags),
115-
Command::Run { dep, verbose, target, edition, features, flags } =>
116-
Self::run(dep, verbose, target, edition, features, flags),
117+
Command::Run { dep, quiet, target, edition, features, flags } =>
118+
Self::run(dep, quiet, target, edition, features, flags),
117119
Command::Doc { features, flags } => Self::doc(features, flags),
118120
Command::Fmt { flags } => Self::fmt(flags),
119121
Command::Clippy { features, flags } => Self::clippy(features, flags),
@@ -458,7 +460,7 @@ impl Command {
458460

459461
fn run(
460462
dep: bool,
461-
verbose: bool,
463+
quiet: bool,
462464
target: Option<String>,
463465
edition: Option<String>,
464466
features: Vec<String>,
@@ -468,7 +470,7 @@ impl Command {
468470

469471
// Preparation: get a sysroot, and get the miri binary.
470472
let miri_sysroot =
471-
e.build_miri_sysroot(/* quiet */ !verbose, target.as_deref(), &features)?;
473+
e.build_miri_sysroot(/* quiet */ quiet, target.as_deref(), &features)?;
472474
let miri_bin = e
473475
.build_get_binary(".", &features)
474476
.context("failed to get filename of miri executable")?;
@@ -492,7 +494,7 @@ impl Command {
492494
// Compute flags.
493495
let miri_flags = e.sh.var("MIRIFLAGS").unwrap_or_default();
494496
let miri_flags = flagsplit(&miri_flags);
495-
let quiet_flag = if verbose { None } else { Some("--quiet") };
497+
let quiet_flag = if quiet { Some("--quiet") } else { None };
496498

497499
// Run Miri.
498500
// The basic command that executes the Miri driver.
@@ -506,7 +508,7 @@ impl Command {
506508
} else {
507509
cmd!(e.sh, "{miri_bin}")
508510
};
509-
cmd.set_quiet(!verbose);
511+
cmd.set_quiet(quiet);
510512
// Add Miri flags
511513
let mut cmd = cmd.args(&miri_flags).args(&early_flags).args(&flags);
512514
// For `--dep` we also need to set the target in the env var.

src/tools/miri/miri-script/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ pub enum Command {
7878
/// Build the program with the dependencies declared in `tests/deps/Cargo.toml`.
7979
#[arg(long)]
8080
dep: bool,
81-
/// Show build progress.
81+
/// Hide build progress.
8282
#[arg(long, short)]
83-
verbose: bool,
83+
quiet: bool,
8484
/// The cross-interpretation target.
8585
#[arg(long)]
8686
target: Option<String>,

src/tools/miri/rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5f9dd05862d2e4bceb3be1031b6c936e35671501
1+
ceb7df7e6f17c92c7d49f7e4f02df0e68bc9b38b

src/tools/miri/src/bin/miri.rs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ extern crate rustc_hir;
1616
extern crate rustc_hir_analysis;
1717
extern crate rustc_interface;
1818
extern crate rustc_log;
19-
extern crate rustc_metadata;
2019
extern crate rustc_middle;
2120
extern crate rustc_session;
2221
extern crate rustc_span;
@@ -26,10 +25,8 @@ mod log;
2625
use std::env;
2726
use std::num::NonZero;
2827
use std::ops::Range;
29-
use std::path::PathBuf;
3028
use std::rc::Rc;
3129
use std::str::FromStr;
32-
use std::sync::Arc;
3330
use std::sync::atomic::{AtomicI32, AtomicU32, Ordering};
3431

3532
use miri::{
@@ -51,10 +48,8 @@ use rustc_middle::middle::exported_symbols::{
5148
use rustc_middle::query::LocalCrate;
5249
use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
5350
use rustc_middle::ty::{self, Ty, TyCtxt};
54-
use rustc_middle::util::Providers;
5551
use rustc_session::EarlyDiagCtxt;
5652
use rustc_session::config::{CrateType, ErrorOutputType, OptLevel};
57-
use rustc_session::search_paths::PathKind;
5853
use rustc_span::def_id::DefId;
5954

6055
use crate::log::setup::{deinit_loggers, init_early_loggers, init_late_loggers};
@@ -126,21 +121,6 @@ fn entry_fn(tcx: TyCtxt<'_>) -> (DefId, MiriEntryFnType) {
126121
}
127122

128123
impl rustc_driver::Callbacks for MiriCompilerCalls {
129-
fn config(&mut self, config: &mut Config) {
130-
config.override_queries = Some(|_, providers| {
131-
providers.extern_queries.used_crate_source = |tcx, cnum| {
132-
let mut providers = Providers::default();
133-
rustc_metadata::provide(&mut providers);
134-
let mut crate_source = (providers.extern_queries.used_crate_source)(tcx, cnum);
135-
// HACK: rustc will emit "crate ... required to be available in rlib format, but
136-
// was not found in this form" errors once we use `tcx.dependency_formats()` if
137-
// there's no rlib provided, so setting a dummy path here to workaround those errors.
138-
Arc::make_mut(&mut crate_source).rlib = Some((PathBuf::new(), PathKind::All));
139-
crate_source
140-
};
141-
});
142-
}
143-
144124
fn after_analysis<'tcx>(
145125
&mut self,
146126
_: &rustc_interface::interface::Compiler,
@@ -253,12 +233,26 @@ impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
253233
#[allow(rustc::potential_query_instability)] // rustc_codegen_ssa (where this code is copied from) also allows this lint
254234
fn config(&mut self, config: &mut Config) {
255235
if config.opts.prints.is_empty() && self.target_crate {
236+
#[allow(rustc::bad_opt_access)] // tcx does not exist yet
237+
{
238+
let any_crate_types = !config.opts.crate_types.is_empty();
239+
// Avoid warnings about unsupported crate types.
240+
config
241+
.opts
242+
.crate_types
243+
.retain(|&c| c == CrateType::Executable || c == CrateType::Rlib);
244+
if any_crate_types {
245+
// Assert that we didn't remove all crate types if any crate type was passed on
246+
// the cli. Otherwise we might silently change what kind of crate we are building.
247+
assert!(!config.opts.crate_types.is_empty());
248+
}
249+
}
250+
256251
// Queries overridden here affect the data stored in `rmeta` files of dependencies,
257252
// which will be used later in non-`MIRI_BE_RUSTC` mode.
258253
config.override_queries = Some(|_, local_providers| {
259-
// `exported_non_generic_symbols` and `reachable_non_generics` provided by rustc always returns
260-
// an empty result if `tcx.sess.opts.output_types.should_codegen()` is false.
261-
// In addition we need to add #[used] symbols to exported_symbols for `lookup_link_section`.
254+
// We need to add #[used] symbols to exported_symbols for `lookup_link_section`.
255+
// FIXME handle this somehow in rustc itself to avoid this hack.
262256
local_providers.exported_non_generic_symbols = |tcx, LocalCrate| {
263257
let reachable_set = tcx.with_stable_hashing_context(|hcx| {
264258
tcx.reachable_set(()).to_sorted(&hcx, true)

src/tools/miri/src/borrow_tracker/mod.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ use crate::*;
1111
pub mod stacked_borrows;
1212
pub mod tree_borrows;
1313

14+
/// Indicates which kind of access is being performed.
15+
#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug)]
16+
pub enum AccessKind {
17+
Read,
18+
Write,
19+
}
20+
21+
impl fmt::Display for AccessKind {
22+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23+
match self {
24+
AccessKind::Read => write!(f, "read access"),
25+
AccessKind::Write => write!(f, "write access"),
26+
}
27+
}
28+
}
29+
1430
/// Tracking pointer provenance
1531
#[derive(Copy, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
1632
pub struct BorTag(NonZero<u64>);
@@ -115,15 +131,6 @@ impl VisitProvenance for GlobalStateInner {
115131
/// We need interior mutable access to the global state.
116132
pub type GlobalState = RefCell<GlobalStateInner>;
117133

118-
impl fmt::Display for AccessKind {
119-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
120-
match self {
121-
AccessKind::Read => write!(f, "read access"),
122-
AccessKind::Write => write!(f, "write access"),
123-
}
124-
}
125-
}
126-
127134
/// Policy on whether to recurse into fields to retag
128135
#[derive(Copy, Clone, Debug)]
129136
pub enum RetagFields {

src/tools/miri/src/borrow_tracker/stacked_borrows/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_data_structures::fx::FxHashSet;
55
use rustc_span::{Span, SpanData};
66
use smallvec::SmallVec;
77

8-
use crate::borrow_tracker::{GlobalStateInner, ProtectorKind};
8+
use crate::borrow_tracker::{AccessKind, GlobalStateInner, ProtectorKind};
99
use crate::*;
1010

1111
/// Error reporting

src/tools/miri/src/borrow_tracker/stacked_borrows/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub use self::stack::Stack;
2121
use crate::borrow_tracker::stacked_borrows::diagnostics::{
2222
AllocHistory, DiagnosticCx, DiagnosticCxBuilder,
2323
};
24-
use crate::borrow_tracker::{GlobalStateInner, ProtectorKind};
24+
use crate::borrow_tracker::{AccessKind, GlobalStateInner, ProtectorKind};
2525
use crate::concurrency::data_race::{NaReadType, NaWriteType};
2626
use crate::*;
2727

src/tools/miri/src/borrow_tracker/tree_borrows/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use std::ops::Range;
44
use rustc_data_structures::fx::FxHashMap;
55
use rustc_span::{Span, SpanData};
66

7-
use crate::borrow_tracker::ProtectorKind;
87
use crate::borrow_tracker::tree_borrows::perms::{PermTransition, Permission};
98
use crate::borrow_tracker::tree_borrows::tree::LocationState;
109
use crate::borrow_tracker::tree_borrows::unimap::UniIndex;
10+
use crate::borrow_tracker::{AccessKind, ProtectorKind};
1111
use crate::*;
1212

1313
/// Cause of an access: either a real access or one

0 commit comments

Comments
 (0)