Skip to content

Commit fd2eb39

Browse files
committed
Auto merge of #144019 - RalfJung:miri-sync, r=RalfJung
Miri subtree update r? `@ghost`
2 parents 5795086 + fe090db commit fd2eb39

Some content is hidden

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

53 files changed

+1149
-590
lines changed

src/tools/miri/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,9 @@ environment variable. We first document the most relevant and most commonly used
342342
is enabled (the default), this is also used to emulate system entropy. The default seed is 0. You
343343
can increase test coverage by running Miri multiple times with different seeds.
344344
* `-Zmiri-strict-provenance` enables [strict
345-
provenance](https://github.com/rust-lang/rust/issues/95228) checking in Miri. This means that
346-
casting an integer to a pointer will stop execution because the provenance of the pointer
347-
cannot be determined.
345+
provenance](https://doc.rust-lang.org/nightly/std/ptr/index.html#strict-provenance) checking in
346+
Miri. This means that casting an integer to a pointer will stop execution because the provenance
347+
of the pointer cannot be determined.
348348
* `-Zmiri-symbolic-alignment-check` makes the alignment check more strict. By default, alignment is
349349
checked by casting the pointer to an integer, and making sure that is a multiple of the alignment.
350350
This can lead to cases where a program passes the alignment check by pure chance, because things

src/tools/miri/miri-script/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ repository = "https://github.com/rust-lang/miri"
77
version = "0.1.0"
88
default-run = "miri-script"
99
edition = "2024"
10+
rust-version = "1.85"
1011

1112
[workspace]
1213
# We make this a workspace root so that cargo does not go looking in ../Cargo.toml for the workspace root.

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,6 @@ impl Command {
702702
let mut early_flags = Vec::<OsString>::new();
703703

704704
// In `dep` mode, the target is already passed via `MIRI_TEST_TARGET`
705-
#[expect(clippy::collapsible_if)] // we need to wait until this is stable
706705
if !dep {
707706
if let Some(target) = &target {
708707
early_flags.push("--target".into());
@@ -735,7 +734,6 @@ impl Command {
735734
// Add Miri flags
736735
let mut cmd = cmd.args(&miri_flags).args(&early_flags).args(&flags);
737736
// For `--dep` we also need to set the target in the env var.
738-
#[expect(clippy::collapsible_if)] // we need to wait until this is stable
739737
if dep {
740738
if let Some(target) = &target {
741739
cmd = cmd.env("MIRI_TEST_TARGET", target);

src/tools/miri/rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
733b47ea4b1b86216f14ef56e49440c33933f230
1+
7f2065a4bae1faed5bab928c670964eafbf43b55

src/tools/miri/src/alloc/isolated_alloc.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -302,31 +302,28 @@ impl IsolatedAlloc {
302302
}
303303
}
304304

305-
/// Returns a vector of page addresses managed by the allocator.
306-
pub fn pages(&self) -> Vec<usize> {
307-
let mut pages: Vec<usize> =
308-
self.page_ptrs.iter().map(|p| p.expose_provenance().get()).collect();
309-
for (ptr, size) in self.huge_ptrs.iter() {
310-
for i in 0..size / self.page_size {
311-
pages.push(ptr.expose_provenance().get().strict_add(i * self.page_size));
312-
}
313-
}
314-
pages
305+
/// Returns a list of page addresses managed by the allocator.
306+
pub fn pages(&self) -> impl Iterator<Item = usize> {
307+
let pages = self.page_ptrs.iter().map(|p| p.expose_provenance().get());
308+
pages.chain(self.huge_ptrs.iter().flat_map(|(ptr, size)| {
309+
(0..size / self.page_size)
310+
.map(|i| ptr.expose_provenance().get().strict_add(i * self.page_size))
311+
}))
315312
}
316313

317314
/// Protects all owned memory as `PROT_NONE`, preventing accesses.
318315
///
319316
/// SAFETY: Accessing memory after this point will result in a segfault
320317
/// unless it is first unprotected.
321-
pub unsafe fn prepare_ffi(&mut self) -> Result<(), nix::errno::Errno> {
318+
pub unsafe fn start_ffi(&mut self) -> Result<(), nix::errno::Errno> {
322319
let prot = mman::ProtFlags::PROT_NONE;
323320
unsafe { self.mprotect(prot) }
324321
}
325322

326323
/// Deprotects all owned memory by setting it to RW. Erroring here is very
327324
/// likely unrecoverable, so it may panic if applying those permissions
328325
/// fails.
329-
pub fn unprep_ffi(&mut self) {
326+
pub fn end_ffi(&mut self) {
330327
let prot = mman::ProtFlags::PROT_READ | mman::ProtFlags::PROT_WRITE;
331328
unsafe {
332329
self.mprotect(prot).unwrap();

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,6 @@ impl rustc_driver::Callbacks for MiriCompilerCalls {
233233
} else {
234234
let return_code = miri::eval_entry(tcx, entry_def_id, entry_type, &config, None)
235235
.unwrap_or_else(|| {
236-
//#[cfg(target_os = "linux")]
237-
//miri::native_lib::register_retcode_sv(rustc_driver::EXIT_FAILURE);
238236
tcx.dcx().abort_if_errors();
239237
rustc_driver::EXIT_FAILURE
240238
});
@@ -337,6 +335,9 @@ impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
337335
fn exit(exit_code: i32) -> ! {
338336
// Drop the tracing guard before exiting, so tracing calls are flushed correctly.
339337
deinit_loggers();
338+
// Make sure the supervisor knows about the code code.
339+
#[cfg(target_os = "linux")]
340+
miri::native_lib::register_retcode_sv(exit_code);
340341
std::process::exit(exit_code);
341342
}
342343

@@ -355,6 +356,11 @@ fn run_compiler_and_exit(
355356
args: &[String],
356357
callbacks: &mut (dyn rustc_driver::Callbacks + Send),
357358
) -> ! {
359+
// Install the ctrlc handler that sets `rustc_const_eval::CTRL_C_RECEIVED`, even if
360+
// MIRI_BE_RUSTC is set. We do this late so that when `native_lib::init_sv` is called,
361+
// there are no other threads.
362+
rustc_driver::install_ctrlc_handler();
363+
358364
// Invoke compiler, catch any unwinding panics and handle return code.
359365
let exit_code =
360366
rustc_driver::catch_with_exit_code(move || rustc_driver::run_compiler(args, callbacks));
@@ -439,10 +445,6 @@ fn main() {
439445
let args = rustc_driver::catch_fatal_errors(|| rustc_driver::args::raw_args(&early_dcx))
440446
.unwrap_or_else(|_| std::process::exit(rustc_driver::EXIT_FAILURE));
441447

442-
// Install the ctrlc handler that sets `rustc_const_eval::CTRL_C_RECEIVED`, even if
443-
// MIRI_BE_RUSTC is set.
444-
rustc_driver::install_ctrlc_handler();
445-
446448
// If the environment asks us to actually be rustc, then do that.
447449
if let Some(crate_kind) = env::var_os("MIRI_BE_RUSTC") {
448450
// Earliest rustc setup.
@@ -750,15 +752,15 @@ fn main() {
750752

751753
debug!("rustc arguments: {:?}", rustc_args);
752754
debug!("crate arguments: {:?}", miri_config.args);
753-
#[cfg(target_os = "linux")]
754755
if !miri_config.native_lib.is_empty() && miri_config.native_lib_enable_tracing {
755-
// FIXME: This should display a diagnostic / warning on error
756-
// SAFETY: If any other threads exist at this point (namely for the ctrlc
757-
// handler), they will not interact with anything on the main rustc/Miri
758-
// thread in an async-signal-unsafe way such as by accessing shared
759-
// semaphores, etc.; the handler only calls `sleep()` and `exit()`, which
760-
// are async-signal-safe, as is accessing atomics
761-
//let _ = unsafe { miri::native_lib::init_sv() };
756+
// SAFETY: No other threads are running
757+
#[cfg(target_os = "linux")]
758+
if unsafe { miri::native_lib::init_sv() }.is_err() {
759+
eprintln!(
760+
"warning: The native-lib tracer could not be started. Is this an x86 Linux system, and does Miri have permissions to ptrace?\n\
761+
Falling back to non-tracing native-lib mode."
762+
);
763+
}
762764
}
763765
run_compiler_and_exit(
764766
&rustc_args,

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ impl GlobalStateInner {
260260
kind: MemoryKind,
261261
machine: &MiriMachine<'_>,
262262
) -> AllocState {
263+
let _span = enter_trace_span!(borrow_tracker::new_allocation, ?id, ?alloc_size, ?kind);
263264
match self.borrow_tracker_method {
264265
BorrowTrackerMethod::StackedBorrows =>
265266
AllocState::StackedBorrows(Box::new(RefCell::new(Stacks::new_allocation(
@@ -280,6 +281,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
280281
kind: RetagKind,
281282
val: &ImmTy<'tcx>,
282283
) -> InterpResult<'tcx, ImmTy<'tcx>> {
284+
let _span = enter_trace_span!(borrow_tracker::retag_ptr_value, ?kind, ?val.layout);
283285
let this = self.eval_context_mut();
284286
let method = this.machine.borrow_tracker.as_ref().unwrap().borrow().borrow_tracker_method;
285287
match method {
@@ -293,6 +295,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
293295
kind: RetagKind,
294296
place: &PlaceTy<'tcx>,
295297
) -> InterpResult<'tcx> {
298+
let _span = enter_trace_span!(borrow_tracker::retag_place_contents, ?kind, ?place);
296299
let this = self.eval_context_mut();
297300
let method = this.machine.borrow_tracker.as_ref().unwrap().borrow().borrow_tracker_method;
298301
match method {
@@ -302,6 +305,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
302305
}
303306

304307
fn protect_place(&mut self, place: &MPlaceTy<'tcx>) -> InterpResult<'tcx, MPlaceTy<'tcx>> {
308+
let _span = enter_trace_span!(borrow_tracker::protect_place, ?place);
305309
let this = self.eval_context_mut();
306310
let method = this.machine.borrow_tracker.as_ref().unwrap().borrow().borrow_tracker_method;
307311
match method {
@@ -311,6 +315,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
311315
}
312316

313317
fn expose_tag(&self, alloc_id: AllocId, tag: BorTag) -> InterpResult<'tcx> {
318+
let _span =
319+
enter_trace_span!(borrow_tracker::expose_tag, alloc_id = alloc_id.0, tag = tag.0);
314320
let this = self.eval_context_ref();
315321
let method = this.machine.borrow_tracker.as_ref().unwrap().borrow().borrow_tracker_method;
316322
match method {
@@ -354,6 +360,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
354360
&self,
355361
frame: &Frame<'tcx, Provenance, FrameExtra<'tcx>>,
356362
) -> InterpResult<'tcx> {
363+
let _span = enter_trace_span!(borrow_tracker::on_stack_pop);
357364
let this = self.eval_context_ref();
358365
let borrow_tracker = this.machine.borrow_tracker.as_ref().unwrap();
359366
// The body of this loop needs `borrow_tracker` immutably
@@ -431,6 +438,7 @@ impl AllocState {
431438
range: AllocRange,
432439
machine: &MiriMachine<'tcx>,
433440
) -> InterpResult<'tcx> {
441+
let _span = enter_trace_span!(borrow_tracker::before_memory_read, alloc_id = alloc_id.0);
434442
match self {
435443
AllocState::StackedBorrows(sb) =>
436444
sb.borrow_mut().before_memory_read(alloc_id, prov_extra, range, machine),
@@ -452,6 +460,7 @@ impl AllocState {
452460
range: AllocRange,
453461
machine: &MiriMachine<'tcx>,
454462
) -> InterpResult<'tcx> {
463+
let _span = enter_trace_span!(borrow_tracker::before_memory_write, alloc_id = alloc_id.0);
455464
match self {
456465
AllocState::StackedBorrows(sb) =>
457466
sb.get_mut().before_memory_write(alloc_id, prov_extra, range, machine),
@@ -473,6 +482,8 @@ impl AllocState {
473482
size: Size,
474483
machine: &MiriMachine<'tcx>,
475484
) -> InterpResult<'tcx> {
485+
let _span =
486+
enter_trace_span!(borrow_tracker::before_memory_deallocation, alloc_id = alloc_id.0);
476487
match self {
477488
AllocState::StackedBorrows(sb) =>
478489
sb.get_mut().before_memory_deallocation(alloc_id, prov_extra, size, machine),
@@ -482,6 +493,7 @@ impl AllocState {
482493
}
483494

484495
pub fn remove_unreachable_tags(&self, tags: &FxHashSet<BorTag>) {
496+
let _span = enter_trace_span!(borrow_tracker::remove_unreachable_tags);
485497
match self {
486498
AllocState::StackedBorrows(sb) => sb.borrow_mut().remove_unreachable_tags(tags),
487499
AllocState::TreeBorrows(tb) => tb.borrow_mut().remove_unreachable_tags(tags),
@@ -496,6 +508,11 @@ impl AllocState {
496508
tag: BorTag,
497509
alloc_id: AllocId, // diagnostics
498510
) -> InterpResult<'tcx> {
511+
let _span = enter_trace_span!(
512+
borrow_tracker::release_protector,
513+
alloc_id = alloc_id.0,
514+
tag = tag.0
515+
);
499516
match self {
500517
AllocState::StackedBorrows(_sb) => interp_ok(()),
501518
AllocState::TreeBorrows(tb) =>
@@ -506,6 +523,7 @@ impl AllocState {
506523

507524
impl VisitProvenance for AllocState {
508525
fn visit_provenance(&self, visit: &mut VisitWith<'_>) {
526+
let _span = enter_trace_span!(borrow_tracker::visit_provenance);
509527
match self {
510528
AllocState::StackedBorrows(sb) => sb.visit_provenance(visit),
511529
AllocState::TreeBorrows(tb) => tb.visit_provenance(visit),

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub type AllocState = Stacks;
3030
#[derive(Clone, Debug)]
3131
pub struct Stacks {
3232
// Even reading memory can have effects on the stack, so we need a `RefCell` here.
33-
stacks: RangeMap<Stack>,
33+
stacks: DedupRangeMap<Stack>,
3434
/// Stores past operations on this allocation
3535
history: AllocHistory,
3636
/// The set of tags that have been exposed inside this allocation.
@@ -468,7 +468,7 @@ impl<'tcx> Stacks {
468468
let stack = Stack::new(item);
469469

470470
Stacks {
471-
stacks: RangeMap::new(size, stack),
471+
stacks: DedupRangeMap::new(size, stack),
472472
history: AllocHistory::new(id, item, machine),
473473
exposed_tags: FxHashSet::default(),
474474
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ trait EvalContextPrivExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
314314
let span = this.machine.current_span();
315315

316316
// Store initial permissions and their corresponding range.
317-
let mut perms_map: RangeMap<LocationState> = RangeMap::new(
317+
let mut perms_map: DedupRangeMap<LocationState> = DedupRangeMap::new(
318318
ptr_size,
319319
LocationState::new_accessed(Permission::new_disabled(), IdempotentForeignAccess::None), // this will be overwritten
320320
);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ pub struct Tree {
247247
/// `unwrap` any `perm.get(key)`.
248248
///
249249
/// We do uphold the fact that `keys(perms)` is a subset of `keys(nodes)`
250-
pub(super) rperms: RangeMap<UniValMap<LocationState>>,
250+
pub(super) rperms: DedupRangeMap<UniValMap<LocationState>>,
251251
/// The index of the root node.
252252
pub(super) root: UniIndex,
253253
}
@@ -609,7 +609,7 @@ impl Tree {
609609
IdempotentForeignAccess::None,
610610
),
611611
);
612-
RangeMap::new(size, perms)
612+
DedupRangeMap::new(size, perms)
613613
};
614614
Self { root: root_idx, nodes, rperms, tag_mapping }
615615
}
@@ -631,7 +631,7 @@ impl<'tcx> Tree {
631631
base_offset: Size,
632632
parent_tag: BorTag,
633633
new_tag: BorTag,
634-
initial_perms: RangeMap<LocationState>,
634+
initial_perms: DedupRangeMap<LocationState>,
635635
default_perm: Permission,
636636
protected: bool,
637637
span: Span,

0 commit comments

Comments
 (0)