Skip to content

Commit 35a0617

Browse files
committed
Auto merge of #98674 - RalfJung:miri-stacktrace-pruning, r=Mark-Simulacrum
miri: prune some atomic operation and raw pointer details from stacktrace Since Miri removes `track_caller` frames from the stacktrace, adding that attribute can help make backtraces more readable (similar to how it makes panic locations better). I made them only show up with `cfg(miri)` to make sure the extra arguments induced by `track_caller` do not cause any runtime performance trouble. This is also testing the waters for whether the libs team is okay with having these attributes in their code, or whether you'd prefer if we find some other way to do this. If you are fine with this, we will probably want to add it to a lot more functions (all the other atomic operations, to start). Before: ``` error: Undefined Behavior: Data race detected between Atomic Load on Thread(id = 2) and Write on Thread(id = 1) at alloc1727 (current vector clock = VClock([9, 0, 6]), conflicting timestamp = VClock([0, 6])) --> /home/r/.rustup/toolchains/miri/lib/rustlib/src/rust/library/core/src/sync/atomic.rs:2594:23 | 2594 | SeqCst => intrinsics::atomic_load_seqcst(dst), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Data race detected between Atomic Load on Thread(id = 2) and Write on Thread(id = 1) at alloc1727 (current vector clock = VClock([9, 0, 6]), conflicting timestamp = VClock([0, 6])) | = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information = note: inside `std::sync::atomic::atomic_load::<usize>` at /home/r/.rustup/toolchains/miri/lib/rustlib/src/rust/library/core/src/sync/atomic.rs:2594:23 = note: inside `std::sync::atomic::AtomicUsize::load` at /home/r/.rustup/toolchains/miri/lib/rustlib/src/rust/library/core/src/sync/atomic.rs:1719:26 note: inside closure at ../miri/tests/fail/data_race/atomic_read_na_write_race1.rs:22:13 --> ../miri/tests/fail/data_race/atomic_read_na_write_race1.rs:22:13 | 22 | (&*c.0).load(Ordering::SeqCst) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ``` After: ``` error: Undefined Behavior: Data race detected between Atomic Load on Thread(id = 2) and Write on Thread(id = 1) at alloc1727 (current vector clock = VClock([9, 0, 6]), conflicting timestamp = VClock([0, 6])) --> tests/fail/data_race/atomic_read_na_write_race1.rs:22:13 | 22 | (&*c.0).load(Ordering::SeqCst) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Data race detected between Atomic Load on Thread(id = 2) and Write on Thread(id = 1) at alloc1727 (current vector clock = VClock([9, 0, 6]), conflicting timestamp = VClock([0, 6])) | = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information = note: inside closure at tests/fail/data_race/atomic_read_na_write_race1.rs:22:13 ```
2 parents 4dbc89d + 13877a9 commit 35a0617

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

library/core/src/intrinsics.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2625,6 +2625,7 @@ pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
26252625
#[cfg_attr(not(bootstrap), rustc_allowed_through_unstable_modules)]
26262626
#[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
26272627
#[inline]
2628+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
26282629
pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
26292630
extern "rust-intrinsic" {
26302631
#[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]

library/core/src/ptr/const_ptr.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,7 @@ impl<T: ?Sized> *const T {
11441144
#[stable(feature = "pointer_methods", since = "1.26.0")]
11451145
#[rustc_const_unstable(feature = "const_ptr_read", issue = "80377")]
11461146
#[inline]
1147+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
11471148
pub const unsafe fn read(self) -> T
11481149
where
11491150
T: Sized,
@@ -1164,6 +1165,7 @@ impl<T: ?Sized> *const T {
11641165
/// [`ptr::read_volatile`]: crate::ptr::read_volatile()
11651166
#[stable(feature = "pointer_methods", since = "1.26.0")]
11661167
#[inline]
1168+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
11671169
pub unsafe fn read_volatile(self) -> T
11681170
where
11691171
T: Sized,
@@ -1183,6 +1185,7 @@ impl<T: ?Sized> *const T {
11831185
#[stable(feature = "pointer_methods", since = "1.26.0")]
11841186
#[rustc_const_unstable(feature = "const_ptr_read", issue = "80377")]
11851187
#[inline]
1188+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
11861189
pub const unsafe fn read_unaligned(self) -> T
11871190
where
11881191
T: Sized,

library/core/src/ptr/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,7 @@ pub const unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
10951095
#[inline]
10961096
#[stable(feature = "rust1", since = "1.0.0")]
10971097
#[rustc_const_unstable(feature = "const_ptr_read", issue = "80377")]
1098+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
10981099
pub const unsafe fn read<T>(src: *const T) -> T {
10991100
// We are calling the intrinsics directly to avoid function calls in the generated code
11001101
// as `intrinsics::copy_nonoverlapping` is a wrapper function.
@@ -1194,6 +1195,7 @@ pub const unsafe fn read<T>(src: *const T) -> T {
11941195
#[inline]
11951196
#[stable(feature = "ptr_unaligned", since = "1.17.0")]
11961197
#[rustc_const_unstable(feature = "const_ptr_read", issue = "80377")]
1198+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
11971199
pub const unsafe fn read_unaligned<T>(src: *const T) -> T {
11981200
let mut tmp = MaybeUninit::<T>::uninit();
11991201
// SAFETY: the caller must guarantee that `src` is valid for reads.
@@ -1290,6 +1292,7 @@ pub const unsafe fn read_unaligned<T>(src: *const T) -> T {
12901292
#[inline]
12911293
#[stable(feature = "rust1", since = "1.0.0")]
12921294
#[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
1295+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
12931296
pub const unsafe fn write<T>(dst: *mut T, src: T) {
12941297
// We are calling the intrinsics directly to avoid function calls in the generated code
12951298
// as `intrinsics::copy_nonoverlapping` is a wrapper function.
@@ -1387,6 +1390,7 @@ pub const unsafe fn write<T>(dst: *mut T, src: T) {
13871390
#[inline]
13881391
#[stable(feature = "ptr_unaligned", since = "1.17.0")]
13891392
#[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
1393+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
13901394
pub const unsafe fn write_unaligned<T>(dst: *mut T, src: T) {
13911395
// SAFETY: the caller must guarantee that `dst` is valid for writes.
13921396
// `dst` cannot overlap `src` because the caller has mutable access
@@ -1460,6 +1464,7 @@ pub const unsafe fn write_unaligned<T>(dst: *mut T, src: T) {
14601464
/// ```
14611465
#[inline]
14621466
#[stable(feature = "volatile", since = "1.9.0")]
1467+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
14631468
pub unsafe fn read_volatile<T>(src: *const T) -> T {
14641469
// SAFETY: the caller must uphold the safety contract for `volatile_load`.
14651470
unsafe {
@@ -1530,6 +1535,7 @@ pub unsafe fn read_volatile<T>(src: *const T) -> T {
15301535
/// ```
15311536
#[inline]
15321537
#[stable(feature = "volatile", since = "1.9.0")]
1538+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
15331539
pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
15341540
// SAFETY: the caller must uphold the safety contract for `volatile_store`.
15351541
unsafe {

library/core/src/ptr/mut_ptr.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,7 @@ impl<T: ?Sized> *mut T {
12581258
#[stable(feature = "pointer_methods", since = "1.26.0")]
12591259
#[rustc_const_unstable(feature = "const_ptr_read", issue = "80377")]
12601260
#[inline(always)]
1261+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
12611262
pub const unsafe fn read(self) -> T
12621263
where
12631264
T: Sized,
@@ -1278,6 +1279,7 @@ impl<T: ?Sized> *mut T {
12781279
/// [`ptr::read_volatile`]: crate::ptr::read_volatile()
12791280
#[stable(feature = "pointer_methods", since = "1.26.0")]
12801281
#[inline(always)]
1282+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
12811283
pub unsafe fn read_volatile(self) -> T
12821284
where
12831285
T: Sized,
@@ -1297,6 +1299,7 @@ impl<T: ?Sized> *mut T {
12971299
#[stable(feature = "pointer_methods", since = "1.26.0")]
12981300
#[rustc_const_unstable(feature = "const_ptr_read", issue = "80377")]
12991301
#[inline(always)]
1302+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
13001303
pub const unsafe fn read_unaligned(self) -> T
13011304
where
13021305
T: Sized,
@@ -1402,6 +1405,7 @@ impl<T: ?Sized> *mut T {
14021405
#[stable(feature = "pointer_methods", since = "1.26.0")]
14031406
#[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
14041407
#[inline(always)]
1408+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
14051409
pub const unsafe fn write(self, val: T)
14061410
where
14071411
T: Sized,
@@ -1420,6 +1424,7 @@ impl<T: ?Sized> *mut T {
14201424
#[stable(feature = "pointer_methods", since = "1.26.0")]
14211425
#[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
14221426
#[inline(always)]
1427+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
14231428
pub const unsafe fn write_bytes(self, val: u8, count: usize)
14241429
where
14251430
T: Sized,
@@ -1440,6 +1445,7 @@ impl<T: ?Sized> *mut T {
14401445
/// [`ptr::write_volatile`]: crate::ptr::write_volatile()
14411446
#[stable(feature = "pointer_methods", since = "1.26.0")]
14421447
#[inline(always)]
1448+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
14431449
pub unsafe fn write_volatile(self, val: T)
14441450
where
14451451
T: Sized,
@@ -1459,6 +1465,7 @@ impl<T: ?Sized> *mut T {
14591465
#[stable(feature = "pointer_methods", since = "1.26.0")]
14601466
#[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
14611467
#[inline(always)]
1468+
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
14621469
pub const unsafe fn write_unaligned(self, val: T)
14631470
where
14641471
T: Sized,

0 commit comments

Comments
 (0)