Skip to content

Commit c8cb78d

Browse files
Rollup merge of #156739 - RalfJung:miri, r=RalfJung
miri subtree update Subtree update of `miri` to rust-lang/miri@159054c. Created using https://github.com/rust-lang/josh-sync. r? @ghost
2 parents d14790a + 18311c6 commit c8cb78d

57 files changed

Lines changed: 1717 additions & 497 deletions

Some content is hidden

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

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

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ impl Command {
114114
Command::Check { features, flags } => Self::check(features, flags),
115115
Command::Test { bless, target, coverage, features, flags } =>
116116
Self::test(bless, target, coverage, features, flags),
117-
Command::Run { dep, quiet, target, edition, features, flags } =>
118-
Self::run(dep, quiet, target, edition, features, flags),
117+
Command::Run { dep, native, quiet, target, edition, features, flags } =>
118+
Self::run(dep, native, quiet, target, edition, features, flags),
119119
Command::Doc { features, flags } => Self::doc(features, flags),
120120
Command::Fmt { flags } => Self::fmt(flags),
121121
Command::Clippy { features, flags } => Self::clippy(features, flags),
@@ -465,15 +465,18 @@ impl Command {
465465

466466
fn run(
467467
dep: bool,
468+
native: bool,
468469
quiet: bool,
469470
target: Option<String>,
470471
edition: Option<String>,
471472
features: Vec<String>,
472473
flags: Vec<String>,
473474
) -> Result<()> {
474475
let mut e = MiriEnv::new()?;
476+
let run_via_ui_test = dep || native;
475477

476478
// Preparation: get a sysroot, and get the miri binary.
479+
// We do this even for native run as it also builds Miri itself.
477480
let miri_sysroot =
478481
e.build_miri_sysroot(/* quiet */ quiet, target.as_deref(), &features)?;
479482
let miri_bin = e
@@ -484,44 +487,45 @@ impl Command {
484487
// (because `flags` may contain `--`).
485488
let mut early_flags = Vec::<OsString>::new();
486489

487-
// In `dep` mode, the target is already passed via `MIRI_TEST_TARGET`
488-
if !dep {
490+
// In ui_test mode, the target is already passed via `MIRI_TEST_TARGET`
491+
if !run_via_ui_test {
489492
if let Some(target) = &target {
490493
early_flags.push("--target".into());
491494
early_flags.push(target.into());
492495
}
493496
}
494497
early_flags.push("--edition".into());
495498
early_flags.push(edition.as_deref().unwrap_or("2021").into());
496-
early_flags.push("--sysroot".into());
497-
early_flags.push(miri_sysroot.into());
499+
if !native {
500+
early_flags.push("--sysroot".into());
501+
early_flags.push(miri_sysroot.into());
502+
}
498503

499504
// Compute flags.
500505
let miri_flags = e.sh.var("MIRIFLAGS").unwrap_or_default();
501506
let miri_flags = flagsplit(&miri_flags);
502-
let quiet_flag = if quiet { Some("--quiet") } else { None };
503507

504508
// Run Miri.
505509
// The basic command that executes the Miri driver.
506-
let mut cmd = if dep {
510+
let mut cmd = if run_via_ui_test {
507511
// We invoke the test suite as that has all the logic for running with dependencies.
508-
e.cargo_cmd(".", "test", &features)
512+
let mut cmd = e
513+
.cargo_cmd(".", "test", &features)
509514
.args(&["--test", "ui"])
510-
.args(quiet_flag)
515+
// This does not show anything useful so we always hide it.
516+
.arg("--quiet")
511517
.arg("--")
512-
.args(&["--miri-run-dep-mode"])
518+
.env("MIRI_RUN_MODE", if native { "native" } else { "1" });
519+
if let Some(target) = &target {
520+
cmd = cmd.env("MIRI_TEST_TARGET", target);
521+
}
522+
cmd
513523
} else {
514524
cmd!(e.sh, "{miri_bin}")
515525
};
516526
cmd.set_quiet(quiet);
517527
// Add Miri flags
518-
let mut cmd = cmd.args(&miri_flags).args(&early_flags).args(&flags);
519-
// For `--dep` we also need to set the target in the env var.
520-
if dep {
521-
if let Some(target) = &target {
522-
cmd = cmd.env("MIRI_TEST_TARGET", target);
523-
}
524-
}
528+
let cmd = cmd.args(&miri_flags).args(&early_flags).args(&flags);
525529
// Finally, run the thing.
526530
Ok(cmd.run()?)
527531
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ pub enum Command {
7878
/// Build the program with the dependencies declared in `tests/deps/Cargo.toml`.
7979
#[arg(long)]
8080
dep: bool,
81+
/// Compile and run the program natively instead of via Miri. Implies `--dep`.
82+
/// All flags are passed to rustc; there is currently no way to pass flags to the program.
83+
#[arg(long)]
84+
native: bool,
8185
/// Hide build progress.
8286
#[arg(long, short)]
8387
quiet: bool,

src/tools/miri/rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
63b1dfc0e00fd6f8ad7cd8817fc712e7d9b7be59
1+
281c97c3240a9abd984ca0c6a2cd7389115e80d5

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ struct DisplayFmtWrapper {
456456
/// will show each permission line as
457457
/// ```text
458458
/// 0.. 1.. 2.. 3.. 4.. 5
459-
/// [Act|Res|Frz|Dis|___]
459+
/// [Unq|Res|Frz|Dis|___]
460460
/// ```
461461
struct DisplayFmtPermission {
462462
/// Text that starts the permission block.
@@ -467,7 +467,7 @@ struct DisplayFmtPermission {
467467
close: S,
468468
/// Text to show when a permission is not initialized.
469469
/// Should have the same width as a `Permission`'s `.short_name()`, i.e.
470-
/// 3 if using the `Res/Act/Frz/Dis` notation.
470+
/// 3 if using the `Res/Unq/Frz/Dis` notation.
471471
uninit: S,
472472
/// Text to separate the `start` and `end` values of a range.
473473
range_sep: S,
@@ -525,7 +525,7 @@ struct DisplayFmtPadding {
525525
/// ```
526526
/// will show states as
527527
/// ```text
528-
/// Act
528+
/// Unq
529529
/// ?Res
530530
/// ____
531531
/// ```
@@ -549,8 +549,8 @@ struct DisplayFmt {
549549
}
550550
impl DisplayFmt {
551551
/// Print the permission with the format
552-
/// ` Res`/` Re*`/` Act`/` Frz`/` Dis` for accessed locations
553-
/// and `?Res`/`?Re*`/`?Act`/`?Frz`/`?Dis` for unaccessed locations.
552+
/// ` Res`/` Re*`/` Unq`/` Frz`/` Dis` for accessed locations
553+
/// and `?Res`/`?Re*`/`?Unq`/`?Frz`/`?Dis` for unaccessed locations.
554554
fn print_perm(&self, perm: Option<LocationState>) -> String {
555555
if let Some(perm) = perm {
556556
format!(
@@ -801,7 +801,7 @@ impl DisplayRepr {
801801
) {
802802
let mut line = String::new();
803803
// Format the permissions on each range.
804-
// Looks like `| Act| Res| Res| Act|`.
804+
// Looks like `| Unq| Res| Res| Unq|`.
805805
line.push_str(fmt.perm.open);
806806
for (i, (perm, &pad)) in tree.rperm.iter().zip(padding.iter()).enumerate() {
807807
if i > 0 {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ pub mod diagnostics {
462462
ReservedFrz { conflicted: false } => "Res ",
463463
ReservedFrz { conflicted: true } => "ResC",
464464
ReservedIM => "ReIM",
465-
Unique => "Act ",
465+
Unique => "Unq ",
466466
Frozen => "Frz ",
467467
Disabled => "Dis ",
468468
}

src/tools/miri/src/clock.rs

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::cell::Cell;
2-
use std::time::{Duration, Instant as StdInstant};
2+
use std::time::{Duration, Instant as StdInstant, SystemTime};
3+
4+
use crate::MiriMachine;
35

46
/// When using a virtual clock, this defines how many nanoseconds we pretend are passing for each
57
/// basic block.
@@ -8,6 +10,7 @@ use std::time::{Duration, Instant as StdInstant};
810
/// (See `tests/pass/shims/time-with-isolation*.rs`.)
911
const NANOSECONDS_PER_BASIC_BLOCK: u128 = 5000;
1012

13+
/// An instant (a fixed moment in time) in Miri's monotone clock.
1114
#[derive(Debug)]
1215
pub struct Instant {
1316
kind: InstantKind,
@@ -129,3 +132,79 @@ impl MonotonicClock {
129132
}
130133
}
131134
}
135+
136+
/// A deadline for some event to occur.
137+
#[derive(Debug)]
138+
pub enum Deadline {
139+
Monotonic(Instant),
140+
RealTime(SystemTime),
141+
}
142+
143+
impl From<Instant> for Deadline {
144+
fn from(value: Instant) -> Self {
145+
Deadline::Monotonic(value)
146+
}
147+
}
148+
149+
impl Deadline {
150+
/// Will try to add `duration`, but if that overflows it may add less.
151+
fn add_lossy(&self, duration: Duration) -> Self {
152+
match self {
153+
Deadline::Monotonic(i) => Deadline::Monotonic(i.add_lossy(duration)),
154+
Deadline::RealTime(s) => {
155+
// If this overflows, try adding just 1h and assume that will not overflow.
156+
Deadline::RealTime(
157+
s.checked_add(duration)
158+
.unwrap_or_else(|| s.checked_add(Duration::from_secs(3600)).unwrap()),
159+
)
160+
}
161+
}
162+
}
163+
}
164+
165+
/// The clock to use for the timeout you are asking for.
166+
#[derive(Debug, Copy, Clone, PartialEq)]
167+
pub enum TimeoutClock {
168+
/// The timeout is measured using the monotone clock.
169+
Monotonic,
170+
/// The timeout is measured using the host's system clock.
171+
RealTime,
172+
}
173+
174+
/// Whether the timeout is relative or absolute.
175+
#[derive(Debug, Copy, Clone)]
176+
pub enum TimeoutStyle {
177+
/// The given duration is interpreted relative to "now" for the selected clock.
178+
Relative,
179+
/// The given duration is interpreted as an "absolute" time, i.e., relative to the epoch of the
180+
/// selected clock.
181+
Absolute,
182+
}
183+
184+
impl MiriMachine<'_> {
185+
/// Computes the deadline for a given timeout configuration and duration.
186+
pub fn timeout(
187+
&self,
188+
clock: TimeoutClock,
189+
style: TimeoutStyle,
190+
duration: Duration,
191+
) -> Deadline {
192+
// First let's figure out what "zero" means for the given clock and style.
193+
let zero = match clock {
194+
TimeoutClock::RealTime => {
195+
assert!(self.communicate(), "cannot have `RealTime` timeout with isolation");
196+
Deadline::RealTime(match style {
197+
TimeoutStyle::Absolute => SystemTime::UNIX_EPOCH,
198+
TimeoutStyle::Relative => SystemTime::now(),
199+
})
200+
}
201+
TimeoutClock::Monotonic =>
202+
Deadline::Monotonic(match style {
203+
TimeoutStyle::Absolute => self.monotonic_clock.epoch(),
204+
TimeoutStyle::Relative => self.monotonic_clock.now(),
205+
}),
206+
};
207+
// Then add the given duration relative to that "zero".
208+
zero.add_lossy(duration)
209+
}
210+
}

src/tools/miri/src/concurrency/blocking_io.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ pub trait EvalContextExt<'tcx>: MiriInterpCxExt<'tcx> {
355355
&mut self,
356356
source_fd: FileDescriptionRef<dyn SourceFileDescription>,
357357
interest: BlockingIoInterest,
358-
timeout: Option<(TimeoutClock, TimeoutAnchor, Duration)>,
358+
deadline: Option<Deadline>,
359359
callback: DynUnblockCallback<'tcx>,
360360
) -> InterpResult<'tcx> {
361361
let this = self.eval_context_mut();
@@ -370,7 +370,7 @@ pub trait EvalContextExt<'tcx>: MiriInterpCxExt<'tcx> {
370370
} else {
371371
// The I/O readiness is currently not fulfilled. We block the thread
372372
// until the readiness is fulfilled and execute the callback then.
373-
this.block_thread(BlockReason::IO, timeout, callback);
373+
this.block_thread(BlockReason::IO, deadline, callback);
374374
interp_ok(())
375375
}
376376
}

src/tools/miri/src/concurrency/sync.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::collections::hash_map::Entry;
55
use std::default::Default;
66
use std::ops::Not;
77
use std::rc::Rc;
8-
use std::time::Duration;
98
use std::{fmt, iter};
109

1110
use rustc_abi::Size;
@@ -684,7 +683,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
684683
&mut self,
685684
condvar_ref: CondvarRef,
686685
mutex_ref: MutexRef,
687-
timeout: Option<(TimeoutClock, TimeoutAnchor, Duration)>,
686+
deadline: Option<Deadline>,
688687
retval_succ: Scalar,
689688
retval_timeout: Scalar,
690689
dest: MPlaceTy<'tcx>,
@@ -706,7 +705,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
706705
condvar_ref.0.borrow_mut().waiters.push_back(thread);
707706
this.block_thread(
708707
BlockReason::Condvar,
709-
timeout,
708+
deadline,
710709
callback!(
711710
@capture<'tcx> {
712711
condvar_ref: CondvarRef,
@@ -763,7 +762,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
763762
&mut self,
764763
futex_ref: FutexRef,
765764
bitset: u32,
766-
timeout: Option<(TimeoutClock, TimeoutAnchor, Duration)>,
765+
deadline: Option<Deadline>,
767766
callback: DynUnblockCallback<'tcx>,
768767
) {
769768
let this = self.eval_context_mut();
@@ -776,7 +775,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
776775

777776
this.block_thread(
778777
BlockReason::Futex,
779-
timeout,
778+
deadline,
780779
callback!(
781780
@capture<'tcx> {
782781
futex_ref: FutexRef,

0 commit comments

Comments
 (0)