From a72db241814a8aad54129c4da8e5d02d76c2f870 Mon Sep 17 00:00:00 2001 From: Bastian Kersting Date: Fri, 24 Jan 2025 15:29:42 +0000 Subject: [PATCH 1/2] Convert more missed places --- src/intrinsics/atomic.rs | 2 +- src/operator.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/intrinsics/atomic.rs b/src/intrinsics/atomic.rs index 8507b0f49d..6d2a575ed7 100644 --- a/src/intrinsics/atomic.rs +++ b/src/intrinsics/atomic.rs @@ -189,7 +189,7 @@ trait EvalContextPrivExt<'tcx>: MiriInterpCxExt<'tcx> { let place = this.deref_pointer(place)?; let rhs = this.read_immediate(rhs)?; - if !place.layout.ty.is_integral() && !place.layout.ty.is_unsafe_ptr() { + if !place.layout.ty.is_integral() && !place.layout.ty.is_raw_ptr() { span_bug!( this.cur_span(), "atomic arithmetic operations only work on integer and raw pointer types", diff --git a/src/operator.rs b/src/operator.rs index c588b6fc7f..81f22b2d0b 100644 --- a/src/operator.rs +++ b/src/operator.rs @@ -52,8 +52,8 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> { // Some more operations are possible with atomics. // The return value always has the provenance of the *left* operand. Add | Sub | BitOr | BitAnd | BitXor => { - assert!(left.layout.ty.is_unsafe_ptr()); - assert!(right.layout.ty.is_unsafe_ptr()); + assert!(left.layout.ty.is_raw_ptr()); + assert!(right.layout.ty.is_raw_ptr()); let ptr = left.to_scalar().to_pointer(this)?; // We do the actual operation with usize-typed scalars. let left = ImmTy::from_uint(ptr.addr().bytes(), this.machine.layouts.usize); From f103ea98f20811e51649695e0501e8052fb8e3bf Mon Sep 17 00:00:00 2001 From: Ben Kimock Date: Tue, 11 Feb 2025 17:38:52 -0500 Subject: [PATCH 2/2] Change swap_nonoverlapping from lang to library UB --- tests/fail/ptr_swap_nonoverlapping.rs | 15 +++++++++++ tests/fail/ptr_swap_nonoverlapping.stderr | 31 +++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 tests/fail/ptr_swap_nonoverlapping.rs create mode 100644 tests/fail/ptr_swap_nonoverlapping.stderr diff --git a/tests/fail/ptr_swap_nonoverlapping.rs b/tests/fail/ptr_swap_nonoverlapping.rs new file mode 100644 index 0000000000..6f627c416b --- /dev/null +++ b/tests/fail/ptr_swap_nonoverlapping.rs @@ -0,0 +1,15 @@ +//! This is a regression test for : The precondition +//! check in `ptr::swap_nonoverlapping` was incorrectly disabled in Miri. +//@normalize-stderr-test: "unsafe \{ libc::abort\(\) \}|crate::intrinsics::abort\(\);" -> "ABORT();" +//@normalize-stderr-test: "\| +\^+" -> "| ^" +//@normalize-stderr-test: "\n +[0-9]+:[^\n]+" -> "" +//@normalize-stderr-test: "\n +at [^\n]+" -> "" +//@error-in-other-file: aborted execution + +fn main() { + let mut data = 0usize; + let ptr = std::ptr::addr_of_mut!(data); + unsafe { + std::ptr::swap_nonoverlapping(ptr, ptr, 1); + } +} diff --git a/tests/fail/ptr_swap_nonoverlapping.stderr b/tests/fail/ptr_swap_nonoverlapping.stderr new file mode 100644 index 0000000000..782303d5f3 --- /dev/null +++ b/tests/fail/ptr_swap_nonoverlapping.stderr @@ -0,0 +1,31 @@ + +thread 'main' panicked at RUSTLIB/core/src/panicking.rs:LL:CC: +unsafe precondition(s) violated: ptr::swap_nonoverlapping requires that both pointer arguments are aligned and non-null and the specified memory ranges do not overlap +note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace +note: in Miri, you may have to set `MIRIFLAGS=-Zmiri-env-forward=RUST_BACKTRACE` for the environment variable to have an effect +thread caused non-unwinding panic. aborting. +error: abnormal termination: the program aborted execution + --> RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC + | +LL | ABORT(); + | ^ the program aborted execution + | + = note: BACKTRACE: + = note: inside `std::sys::pal::PLATFORM::abort_internal` at RUSTLIB/std/src/sys/pal/PLATFORM/mod.rs:LL:CC + = note: inside `std::panicking::rust_panic_with_hook` at RUSTLIB/std/src/panicking.rs:LL:CC + = note: inside closure at RUSTLIB/std/src/panicking.rs:LL:CC + = note: inside `std::sys::backtrace::__rust_end_short_backtrace::<{closure@std::panicking::begin_panic_handler::{closure#0}}, !>` at RUSTLIB/std/src/sys/backtrace.rs:LL:CC + = note: inside `std::panicking::begin_panic_handler` at RUSTLIB/std/src/panicking.rs:LL:CC + = note: inside `core::panicking::panic_nounwind` at RUSTLIB/core/src/panicking.rs:LL:CC + = note: inside `std::ptr::swap_nonoverlapping::precondition_check` at RUSTLIB/core/src/ub_checks.rs:LL:CC + = note: inside `std::ptr::swap_nonoverlapping::` at RUSTLIB/core/src/ub_checks.rs:LL:CC +note: inside `main` + --> tests/fail/ptr_swap_nonoverlapping.rs:LL:CC + | +LL | std::ptr::swap_nonoverlapping(ptr, ptr, 1); + | ^ + +note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace + +error: aborting due to 1 previous error +