Skip to content

Miri subtree update #142292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 44 commits into from
Jun 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
c10a629
Merge pull request #4370 from Noratrieb/fix-for-real
RalfJung Jun 1, 2025
1a1d180
Preparing for merge from rustc
Jun 3, 2025
08cad5a
Merge from rustc
Jun 3, 2025
2d8ccc5
Merge pull request #4373 from rust-lang/rustup-2025-06-03
RalfJung Jun 3, 2025
80a95b7
native-lib: allow multiple libraries and/or dirs
nia-e Jun 2, 2025
beff63b
Preparing for merge from rustc
Jun 4, 2025
db72088
Merge from rustc
Jun 4, 2025
9a7255e
Merge pull request #4375 from rust-lang/rustup-2025-06-04
RalfJung Jun 4, 2025
d55a99a
Merge pull request #4372 from nia-e/multiple-native-libs
oli-obk Jun 4, 2025
869baea
Preparing for merge from rustc
Jun 5, 2025
bd898e3
Merge from rustc
Jun 5, 2025
412628d
Merge pull request #4377 from rust-lang/rustup-2025-06-05
RalfJung Jun 5, 2025
8c410ca
Preparing for merge from rustc
Jun 6, 2025
c44bc10
Merge from rustc
Jun 6, 2025
ccb3aca
Merge pull request #4380 from rust-lang/rustup-2025-06-06
oli-obk Jun 6, 2025
b1652dc
use File::lock to implement flock, and add a test for File::lock
RalfJung Jun 5, 2025
f0d0882
Merge pull request #4378 from RalfJung/flock
RalfJung Jun 6, 2025
cf5aea5
Preparing for merge from rustc
Jun 7, 2025
4c4904b
Merge from rustc
Jun 7, 2025
4007b19
fmt
Jun 7, 2025
31e9806
Merge pull request #4381 from rust-lang/rustup-2025-06-07
RalfJung Jun 7, 2025
b620d07
test_dup: ensure the FDs remain in sync even after dup'ing
RalfJung Jun 7, 2025
f413f15
Preparing for merge from rustc
Jun 8, 2025
1894b49
Merge from rustc
Jun 8, 2025
941a971
Merge pull request #4385 from rust-lang/rustup-2025-06-08
RalfJung Jun 8, 2025
ee00d0f
Preparing for merge from rustc
Jun 9, 2025
4835a9d
Merge from rustc
Jun 9, 2025
65ad80e
Merge pull request #4388 from rust-lang/rustup-2025-06-09
RalfJung Jun 9, 2025
4e8d583
Add `-Zmiri-tree-borrows-no-precise-interior-mut` flag
Jun 2, 2025
ed5a655
Merge pull request #4376 from yoctocell/tb-interior-mut-flag
RalfJung Jun 9, 2025
a096de7
native_lib: skip to next .so if function was in dependency of the first
RalfJung Jun 9, 2025
11581df
native-lib: update readme (to mention folders in header)
RalfJung Jun 9, 2025
5eb5f2a
Preparing for merge from rustc
Jun 10, 2025
c601681
Merge from rustc
Jun 10, 2025
89db777
fmt
Jun 10, 2025
fceb8d6
sync max_fundamental_align with alloc crate
RalfJung Jun 10, 2025
19f195d
Merge pull request #4393 from rust-lang/rustup-2025-06-10
RalfJung Jun 10, 2025
3e980d5
add SmallVec test
RalfJung Jun 10, 2025
7d5fac7
Merge pull request #4394 from RalfJung/smallvec
RalfJung Jun 10, 2025
a7153db
float tests: test non-determinism for more operations
RalfJung Jun 9, 2025
2d72737
Merge pull request #4391 from RalfJung/float-nondet-tests
RalfJung Jun 10, 2025
f5457e4
Merge pull request #4382 from RalfJung/dup
RalfJung Jun 10, 2025
680176a
Merge pull request #4389 from RalfJung/native-lib-search-order
RalfJung Jun 10, 2025
88e3e68
update lockfile
RalfJung Jun 10, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2326,7 +2326,6 @@ dependencies = [
"tempfile",
"tikv-jemalloc-sys",
"ui_test",
"windows-sys 0.59.0",
]

[[package]]
Expand Down
1 change: 0 additions & 1 deletion src/tools/miri/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,6 @@ dependencies = [
"tempfile",
"tikv-jemalloc-sys",
"ui_test",
"windows-sys",
]

[[package]]
Expand Down
7 changes: 0 additions & 7 deletions src/tools/miri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,6 @@ libc = "0.2"
libffi = "4.0.0"
libloading = "0.8"

[target.'cfg(target_family = "windows")'.dependencies]
windows-sys = { version = "0.59", features = [
"Win32_Foundation",
"Win32_System_IO",
"Win32_Storage_FileSystem",
] }

[dev-dependencies]
ui_test = "0.29.1"
colored = "2"
Expand Down
26 changes: 17 additions & 9 deletions src/tools/miri/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,18 +396,22 @@ to Miri failing to detect cases of undefined behavior in a program.
* `-Zmiri-force-intrinsic-fallback` forces the use of the "fallback" body for all intrinsics that
have one. This is useful to test the fallback bodies, but should not be used otherwise. It is
**unsound** since the fallback body might not be checking for all UB.
* `-Zmiri-native-lib=<path to a shared object file>` is an experimental flag for providing support
for calling native functions from inside the interpreter via FFI. The flag is supported only on
Unix systems. Functions not provided by that file are still executed via the usual Miri shims.
* `-Zmiri-native-lib=<path to a shared object file or folder>` is an experimental flag for providing
support for calling native functions from inside the interpreter via FFI. The flag is supported
only on Unix systems. Functions not provided by that file are still executed via the usual Miri
shims. If a path to a directory is specified, all files in that directory are included
non-recursively. This flag can be passed multiple times to specify multiple files and/or
directories.
**WARNING**: If an invalid/incorrect `.so` file is specified, this can cause Undefined Behavior in
Miri itself! And of course, Miri cannot do any checks on the actions taken by the native code.
Miri itself! And of course, Miri often cannot do any checks on the actions taken by the native code.
Note that Miri has its own handling of file descriptors, so if you want to replace *some*
functions working on file descriptors, you will have to replace *all* of them, or the two kinds of
file descriptors will be mixed up. This is **work in progress**; currently, only integer and
pointers arguments and return values are supported and memory allocated by the native code cannot
be accessed from Rust (only the other way around). Native code must not spawn threads that keep
running in the background after the call has returned to Rust and that access Rust-allocated
memory. Finally, the flag is **unsound** in the sense that Miri stops tracking details such as
file descriptors will be mixed up.
This is **work in progress**; currently, only integer and pointers arguments and return values are
supported and memory allocated by the native code cannot be accessed from Rust (only the other way
around). Native code must not spawn threads that keep running in the background after the call has
returned to Rust and that access Rust-allocated memory.
Finally, the flag is **unsound** in the sense that Miri stops tracking details such as
initialization and provenance on memory shared with native code, so it is easily possible to write
code that has UB which is missed by Miri.
* `-Zmiri-measureme=<name>` enables `measureme` profiling for the interpreted program.
Expand Down Expand Up @@ -458,6 +462,10 @@ to Miri failing to detect cases of undefined behavior in a program.
This is much less likely with Stacked Borrows.
Using Tree Borrows currently implies `-Zmiri-strict-provenance` because integer-to-pointer
casts are not supported in this mode, but that may change in the future.
* `-Zmiri-tree-borrows-no-precise-interior-mut` makes Tree Borrows
track interior mutable data on the level of references instead of on the
byte-level as is done by default. Therefore, with this flag, Tree
Borrows will be more permissive.
* `-Zmiri-force-page-size=<num>` overrides the default page size for an architecture, in multiples of 1k.
`4` is default for most targets. This value should always be a power of 2 and nonzero.

Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/rust-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
337c11e5932275e7d450c1f2e26f289f0ddfa717
c6768de2d63de7a41124a0fb8fc78f9e26111c01
4 changes: 2 additions & 2 deletions src/tools/miri/src/alloc_addresses/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
assert!(!matches!(info.kind, AllocKind::Dead));

// This allocation does not have a base address yet, pick or reuse one.
if this.machine.native_lib.is_some() {
if !this.machine.native_lib.is_empty() {
// In native lib mode, we use the "real" address of the bytes for this allocation.
// This ensures the interpreted program and native code have the same view of memory.
let params = this.machine.get_default_alloc_params();
Expand Down Expand Up @@ -413,7 +413,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
) -> InterpResult<'tcx, MiriAllocBytes> {
let this = self.eval_context_ref();
assert!(this.tcx.try_get_global_alloc(id).is_some());
if this.machine.native_lib.is_some() {
if !this.machine.native_lib.is_empty() {
// In native lib mode, MiriAllocBytes for global allocations are handled via `prepared_alloc_bytes`.
// This additional call ensures that some `MiriAllocBytes` are always prepared, just in case
// this function gets called before the first time `addr_from_alloc_id` gets called.
Expand Down
39 changes: 30 additions & 9 deletions src/tools/miri/src/bin/miri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use std::sync::{Arc, Once};

use miri::{
BacktraceStyle, BorrowTrackerMethod, GenmcConfig, GenmcCtx, MiriConfig, MiriEntryFnType,
ProvenanceMode, RetagFields, ValidationMode,
ProvenanceMode, RetagFields, TreeBorrowsParams, ValidationMode,
};
use rustc_abi::ExternAbi;
use rustc_data_structures::sync;
Expand Down Expand Up @@ -554,8 +554,21 @@ fn main() {
} else if arg == "-Zmiri-disable-stacked-borrows" {
miri_config.borrow_tracker = None;
} else if arg == "-Zmiri-tree-borrows" {
miri_config.borrow_tracker = Some(BorrowTrackerMethod::TreeBorrows);
miri_config.borrow_tracker =
Some(BorrowTrackerMethod::TreeBorrows(TreeBorrowsParams {
precise_interior_mut: true,
}));
miri_config.provenance_mode = ProvenanceMode::Strict;
} else if arg == "-Zmiri-tree-borrows-no-precise-interior-mut" {
match &mut miri_config.borrow_tracker {
Some(BorrowTrackerMethod::TreeBorrows(params)) => {
params.precise_interior_mut = false;
}
_ =>
show_error!(
"`-Zmiri-tree-borrows` is required before `-Zmiri-tree-borrows-no-precise-interior-mut`"
),
};
} else if arg == "-Zmiri-disable-data-race-detector" {
miri_config.data_race_detector = false;
miri_config.weak_memory_emulation = false;
Expand Down Expand Up @@ -692,11 +705,18 @@ fn main() {
};
} else if let Some(param) = arg.strip_prefix("-Zmiri-native-lib=") {
let filename = param.to_string();
if std::path::Path::new(&filename).exists() {
if let Some(other_filename) = miri_config.native_lib {
show_error!("-Zmiri-native-lib is already set to {}", other_filename.display());
let file_path = std::path::Path::new(&filename);
if file_path.exists() {
// For directories, nonrecursively add all normal files inside
if let Ok(dir) = file_path.read_dir() {
for lib in dir.filter_map(|res| res.ok()) {
if lib.file_type().unwrap().is_file() {
miri_config.native_lib.push(lib.path().to_owned());
}
}
} else {
miri_config.native_lib.push(filename.into());
}
miri_config.native_lib = Some(filename.into());
} else {
show_error!("-Zmiri-native-lib `{}` does not exist", filename);
}
Expand Down Expand Up @@ -725,18 +745,19 @@ fn main() {
}
}
// Tree Borrows implies strict provenance, and is not compatible with native calls.
if matches!(miri_config.borrow_tracker, Some(BorrowTrackerMethod::TreeBorrows)) {
if matches!(miri_config.borrow_tracker, Some(BorrowTrackerMethod::TreeBorrows { .. })) {
if miri_config.provenance_mode != ProvenanceMode::Strict {
show_error!(
"Tree Borrows does not support integer-to-pointer casts, and hence requires strict provenance"
);
}
if miri_config.native_lib.is_some() {
if !miri_config.native_lib.is_empty() {
show_error!("Tree Borrows is not compatible with calling native functions");
}
}

// Native calls and strict provenance are not compatible.
if miri_config.native_lib.is_some() && miri_config.provenance_mode == ProvenanceMode::Strict {
if !miri_config.native_lib.is_empty() && miri_config.provenance_mode == ProvenanceMode::Strict {
show_error!("strict provenance is not compatible with calling native functions");
}
// You can set either one seed or many.
Expand Down
29 changes: 21 additions & 8 deletions src/tools/miri/src/borrow_tracker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,13 @@ pub enum BorrowTrackerMethod {
/// Stacked Borrows, as implemented in borrow_tracker/stacked_borrows
StackedBorrows,
/// Tree borrows, as implemented in borrow_tracker/tree_borrows
TreeBorrows,
TreeBorrows(TreeBorrowsParams),
}

/// Parameters that Tree Borrows can take.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct TreeBorrowsParams {
pub precise_interior_mut: bool,
}

impl BorrowTrackerMethod {
Expand All @@ -237,6 +243,13 @@ impl BorrowTrackerMethod {
config.retag_fields,
))
}

pub fn get_tree_borrows_params(self) -> TreeBorrowsParams {
match self {
BorrowTrackerMethod::TreeBorrows(params) => params,
_ => panic!("can only be called when `BorrowTrackerMethod` is `TreeBorrows`"),
}
}
}

impl GlobalStateInner {
Expand All @@ -252,7 +265,7 @@ impl GlobalStateInner {
AllocState::StackedBorrows(Box::new(RefCell::new(Stacks::new_allocation(
id, alloc_size, self, kind, machine,
)))),
BorrowTrackerMethod::TreeBorrows =>
BorrowTrackerMethod::TreeBorrows { .. } =>
AllocState::TreeBorrows(Box::new(RefCell::new(Tree::new_allocation(
id, alloc_size, self, kind, machine,
)))),
Expand All @@ -271,7 +284,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let method = this.machine.borrow_tracker.as_ref().unwrap().borrow().borrow_tracker_method;
match method {
BorrowTrackerMethod::StackedBorrows => this.sb_retag_ptr_value(kind, val),
BorrowTrackerMethod::TreeBorrows => this.tb_retag_ptr_value(kind, val),
BorrowTrackerMethod::TreeBorrows { .. } => this.tb_retag_ptr_value(kind, val),
}
}

Expand All @@ -284,7 +297,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let method = this.machine.borrow_tracker.as_ref().unwrap().borrow().borrow_tracker_method;
match method {
BorrowTrackerMethod::StackedBorrows => this.sb_retag_place_contents(kind, place),
BorrowTrackerMethod::TreeBorrows => this.tb_retag_place_contents(kind, place),
BorrowTrackerMethod::TreeBorrows { .. } => this.tb_retag_place_contents(kind, place),
}
}

Expand All @@ -293,7 +306,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let method = this.machine.borrow_tracker.as_ref().unwrap().borrow().borrow_tracker_method;
match method {
BorrowTrackerMethod::StackedBorrows => this.sb_protect_place(place),
BorrowTrackerMethod::TreeBorrows => this.tb_protect_place(place),
BorrowTrackerMethod::TreeBorrows { .. } => this.tb_protect_place(place),
}
}

Expand All @@ -302,7 +315,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let method = this.machine.borrow_tracker.as_ref().unwrap().borrow().borrow_tracker_method;
match method {
BorrowTrackerMethod::StackedBorrows => this.sb_expose_tag(alloc_id, tag),
BorrowTrackerMethod::TreeBorrows => this.tb_expose_tag(alloc_id, tag),
BorrowTrackerMethod::TreeBorrows { .. } => this.tb_expose_tag(alloc_id, tag),
}
}

Expand All @@ -319,7 +332,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
this.tcx.tcx.dcx().warn("Stacked Borrows does not support named pointers; `miri_pointer_name` is a no-op");
interp_ok(())
}
BorrowTrackerMethod::TreeBorrows =>
BorrowTrackerMethod::TreeBorrows { .. } =>
this.tb_give_pointer_debug_name(ptr, nth_parent, name),
}
}
Expand All @@ -333,7 +346,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let method = borrow_tracker.borrow().borrow_tracker_method;
match method {
BorrowTrackerMethod::StackedBorrows => this.print_stacks(alloc_id),
BorrowTrackerMethod::TreeBorrows => this.print_tree(alloc_id, show_unnamed),
BorrowTrackerMethod::TreeBorrows { .. } => this.print_tree(alloc_id, show_unnamed),
}
}

Expand Down
Loading
Loading