Skip to content

Commit 1677d46

Browse files
committed
Auto merge of #142292 - RalfJung:miri-sync, r=RalfJung
Miri subtree update r? `@ghost`
2 parents 8ce2287 + 88e3e68 commit 1677d46

File tree

25 files changed

+420
-229
lines changed

25 files changed

+420
-229
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2326,7 +2326,6 @@ dependencies = [
23262326
"tempfile",
23272327
"tikv-jemalloc-sys",
23282328
"ui_test",
2329-
"windows-sys 0.59.0",
23302329
]
23312330

23322331
[[package]]

src/tools/miri/Cargo.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,6 @@ dependencies = [
555555
"tempfile",
556556
"tikv-jemalloc-sys",
557557
"ui_test",
558-
"windows-sys",
559558
]
560559

561560
[[package]]

src/tools/miri/Cargo.toml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,6 @@ libc = "0.2"
4040
libffi = "4.0.0"
4141
libloading = "0.8"
4242

43-
[target.'cfg(target_family = "windows")'.dependencies]
44-
windows-sys = { version = "0.59", features = [
45-
"Win32_Foundation",
46-
"Win32_System_IO",
47-
"Win32_Storage_FileSystem",
48-
] }
49-
5043
[dev-dependencies]
5144
ui_test = "0.29.1"
5245
colored = "2"

src/tools/miri/README.md

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

src/tools/miri/rust-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
337c11e5932275e7d450c1f2e26f289f0ddfa717
1+
c6768de2d63de7a41124a0fb8fc78f9e26111c01

src/tools/miri/src/alloc_addresses/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
132132
assert!(!matches!(info.kind, AllocKind::Dead));
133133

134134
// This allocation does not have a base address yet, pick or reuse one.
135-
if this.machine.native_lib.is_some() {
135+
if !this.machine.native_lib.is_empty() {
136136
// In native lib mode, we use the "real" address of the bytes for this allocation.
137137
// This ensures the interpreted program and native code have the same view of memory.
138138
let params = this.machine.get_default_alloc_params();
@@ -413,7 +413,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
413413
) -> InterpResult<'tcx, MiriAllocBytes> {
414414
let this = self.eval_context_ref();
415415
assert!(this.tcx.try_get_global_alloc(id).is_some());
416-
if this.machine.native_lib.is_some() {
416+
if !this.machine.native_lib.is_empty() {
417417
// In native lib mode, MiriAllocBytes for global allocations are handled via `prepared_alloc_bytes`.
418418
// This additional call ensures that some `MiriAllocBytes` are always prepared, just in case
419419
// this function gets called before the first time `addr_from_alloc_id` gets called.

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

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use std::sync::{Arc, Once};
3535

3636
use miri::{
3737
BacktraceStyle, BorrowTrackerMethod, GenmcConfig, GenmcCtx, MiriConfig, MiriEntryFnType,
38-
ProvenanceMode, RetagFields, ValidationMode,
38+
ProvenanceMode, RetagFields, TreeBorrowsParams, ValidationMode,
3939
};
4040
use rustc_abi::ExternAbi;
4141
use rustc_data_structures::sync;
@@ -554,8 +554,21 @@ fn main() {
554554
} else if arg == "-Zmiri-disable-stacked-borrows" {
555555
miri_config.borrow_tracker = None;
556556
} else if arg == "-Zmiri-tree-borrows" {
557-
miri_config.borrow_tracker = Some(BorrowTrackerMethod::TreeBorrows);
557+
miri_config.borrow_tracker =
558+
Some(BorrowTrackerMethod::TreeBorrows(TreeBorrowsParams {
559+
precise_interior_mut: true,
560+
}));
558561
miri_config.provenance_mode = ProvenanceMode::Strict;
562+
} else if arg == "-Zmiri-tree-borrows-no-precise-interior-mut" {
563+
match &mut miri_config.borrow_tracker {
564+
Some(BorrowTrackerMethod::TreeBorrows(params)) => {
565+
params.precise_interior_mut = false;
566+
}
567+
_ =>
568+
show_error!(
569+
"`-Zmiri-tree-borrows` is required before `-Zmiri-tree-borrows-no-precise-interior-mut`"
570+
),
571+
};
559572
} else if arg == "-Zmiri-disable-data-race-detector" {
560573
miri_config.data_race_detector = false;
561574
miri_config.weak_memory_emulation = false;
@@ -692,11 +705,18 @@ fn main() {
692705
};
693706
} else if let Some(param) = arg.strip_prefix("-Zmiri-native-lib=") {
694707
let filename = param.to_string();
695-
if std::path::Path::new(&filename).exists() {
696-
if let Some(other_filename) = miri_config.native_lib {
697-
show_error!("-Zmiri-native-lib is already set to {}", other_filename.display());
708+
let file_path = std::path::Path::new(&filename);
709+
if file_path.exists() {
710+
// For directories, nonrecursively add all normal files inside
711+
if let Ok(dir) = file_path.read_dir() {
712+
for lib in dir.filter_map(|res| res.ok()) {
713+
if lib.file_type().unwrap().is_file() {
714+
miri_config.native_lib.push(lib.path().to_owned());
715+
}
716+
}
717+
} else {
718+
miri_config.native_lib.push(filename.into());
698719
}
699-
miri_config.native_lib = Some(filename.into());
700720
} else {
701721
show_error!("-Zmiri-native-lib `{}` does not exist", filename);
702722
}
@@ -725,18 +745,19 @@ fn main() {
725745
}
726746
}
727747
// Tree Borrows implies strict provenance, and is not compatible with native calls.
728-
if matches!(miri_config.borrow_tracker, Some(BorrowTrackerMethod::TreeBorrows)) {
748+
if matches!(miri_config.borrow_tracker, Some(BorrowTrackerMethod::TreeBorrows { .. })) {
729749
if miri_config.provenance_mode != ProvenanceMode::Strict {
730750
show_error!(
731751
"Tree Borrows does not support integer-to-pointer casts, and hence requires strict provenance"
732752
);
733753
}
734-
if miri_config.native_lib.is_some() {
754+
if !miri_config.native_lib.is_empty() {
735755
show_error!("Tree Borrows is not compatible with calling native functions");
736756
}
737757
}
758+
738759
// Native calls and strict provenance are not compatible.
739-
if miri_config.native_lib.is_some() && miri_config.provenance_mode == ProvenanceMode::Strict {
760+
if !miri_config.native_lib.is_empty() && miri_config.provenance_mode == ProvenanceMode::Strict {
740761
show_error!("strict provenance is not compatible with calling native functions");
741762
}
742763
// You can set either one seed or many.

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,13 @@ pub enum BorrowTrackerMethod {
226226
/// Stacked Borrows, as implemented in borrow_tracker/stacked_borrows
227227
StackedBorrows,
228228
/// Tree borrows, as implemented in borrow_tracker/tree_borrows
229-
TreeBorrows,
229+
TreeBorrows(TreeBorrowsParams),
230+
}
231+
232+
/// Parameters that Tree Borrows can take.
233+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
234+
pub struct TreeBorrowsParams {
235+
pub precise_interior_mut: bool,
230236
}
231237

232238
impl BorrowTrackerMethod {
@@ -237,6 +243,13 @@ impl BorrowTrackerMethod {
237243
config.retag_fields,
238244
))
239245
}
246+
247+
pub fn get_tree_borrows_params(self) -> TreeBorrowsParams {
248+
match self {
249+
BorrowTrackerMethod::TreeBorrows(params) => params,
250+
_ => panic!("can only be called when `BorrowTrackerMethod` is `TreeBorrows`"),
251+
}
252+
}
240253
}
241254

242255
impl GlobalStateInner {
@@ -252,7 +265,7 @@ impl GlobalStateInner {
252265
AllocState::StackedBorrows(Box::new(RefCell::new(Stacks::new_allocation(
253266
id, alloc_size, self, kind, machine,
254267
)))),
255-
BorrowTrackerMethod::TreeBorrows =>
268+
BorrowTrackerMethod::TreeBorrows { .. } =>
256269
AllocState::TreeBorrows(Box::new(RefCell::new(Tree::new_allocation(
257270
id, alloc_size, self, kind, machine,
258271
)))),
@@ -271,7 +284,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
271284
let method = this.machine.borrow_tracker.as_ref().unwrap().borrow().borrow_tracker_method;
272285
match method {
273286
BorrowTrackerMethod::StackedBorrows => this.sb_retag_ptr_value(kind, val),
274-
BorrowTrackerMethod::TreeBorrows => this.tb_retag_ptr_value(kind, val),
287+
BorrowTrackerMethod::TreeBorrows { .. } => this.tb_retag_ptr_value(kind, val),
275288
}
276289
}
277290

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

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

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

@@ -319,7 +332,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
319332
this.tcx.tcx.dcx().warn("Stacked Borrows does not support named pointers; `miri_pointer_name` is a no-op");
320333
interp_ok(())
321334
}
322-
BorrowTrackerMethod::TreeBorrows =>
335+
BorrowTrackerMethod::TreeBorrows { .. } =>
323336
this.tb_give_pointer_debug_name(ptr, nth_parent, name),
324337
}
325338
}
@@ -333,7 +346,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
333346
let method = borrow_tracker.borrow().borrow_tracker_method;
334347
match method {
335348
BorrowTrackerMethod::StackedBorrows => this.print_stacks(alloc_id),
336-
BorrowTrackerMethod::TreeBorrows => this.print_tree(alloc_id, show_unnamed),
349+
BorrowTrackerMethod::TreeBorrows { .. } => this.print_tree(alloc_id, show_unnamed),
337350
}
338351
}
339352

0 commit comments

Comments
 (0)