From 1120433eef758194fc42c886cda6e3912b5f2991 Mon Sep 17 00:00:00 2001 From: Pavel Grigorenko Date: Fri, 23 Feb 2024 14:09:37 +0300 Subject: [PATCH 1/3] Get rid of some `#[allow(static_mut_refs)]` --- tests/fail/tls/tls_static_dealloc.rs | 6 +++--- tests/pass/static_mut.rs | 6 +++--- tests/pass/tls/tls_static.rs | 17 ++++++++--------- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/tests/fail/tls/tls_static_dealloc.rs b/tests/fail/tls/tls_static_dealloc.rs index d47a05d847..c72cc8114d 100644 --- a/tests/fail/tls/tls_static_dealloc.rs +++ b/tests/fail/tls/tls_static_dealloc.rs @@ -1,8 +1,8 @@ //! Ensure that thread-local statics get deallocated when the thread dies. #![feature(thread_local)] -// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint -#![allow(static_mut_refs)] + +use std::ptr::addr_of; #[thread_local] static mut TLS: u8 = 0; @@ -12,7 +12,7 @@ unsafe impl Send for SendRaw {} fn main() { unsafe { - let dangling_ptr = std::thread::spawn(|| SendRaw(&TLS as *const u8)).join().unwrap(); + let dangling_ptr = std::thread::spawn(|| SendRaw(addr_of!(TLS))).join().unwrap(); let _val = *dangling_ptr.0; //~ ERROR: has been freed } } diff --git a/tests/pass/static_mut.rs b/tests/pass/static_mut.rs index 6b0c029772..1b416cc4e9 100644 --- a/tests/pass/static_mut.rs +++ b/tests/pass/static_mut.rs @@ -1,8 +1,8 @@ +use std::ptr::addr_of; + static mut FOO: i32 = 42; -// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint -#[allow(static_mut_refs)] -static BAR: Foo = Foo(unsafe { &FOO as *const _ }); +static BAR: Foo = Foo(unsafe { addr_of!(FOO) }); #[allow(dead_code)] struct Foo(*const i32); diff --git a/tests/pass/tls/tls_static.rs b/tests/pass/tls/tls_static.rs index fea5bb1db5..8d0e5089d4 100644 --- a/tests/pass/tls/tls_static.rs +++ b/tests/pass/tls/tls_static.rs @@ -8,9 +8,8 @@ //! test, we also check that thread-locals act as per-thread statics. #![feature(thread_local)] -// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint -#![allow(static_mut_refs)] +use std::ptr::addr_of_mut; use std::thread; #[thread_local] @@ -23,8 +22,8 @@ static mut C: u8 = 0; #[thread_local] static READ_ONLY: u8 = 42; -unsafe fn get_a_ref() -> *mut u8 { - &mut A +unsafe fn get_a_ptr() -> *mut u8 { + addr_of_mut!(A) } struct Sender(*mut u8); @@ -35,12 +34,12 @@ fn main() { let _val = READ_ONLY; let ptr = unsafe { - let x = get_a_ref(); + let x = get_a_ptr(); *x = 5; assert_eq!(A, 5); B = 15; C = 25; - Sender(&mut A) + Sender(addr_of_mut!(A)) }; thread::spawn(move || unsafe { @@ -51,18 +50,18 @@ fn main() { assert_eq!(C, 25); B = 14; C = 24; - let y = get_a_ref(); + let y = get_a_ptr(); assert_eq!(*y, 0); *y = 4; assert_eq!(*ptr.0, 5); assert_eq!(A, 4); - assert_eq!(*get_a_ref(), 4); + assert_eq!(*get_a_ptr(), 4); }) .join() .unwrap(); unsafe { - assert_eq!(*get_a_ref(), 5); + assert_eq!(*get_a_ptr(), 5); assert_eq!(A, 5); assert_eq!(B, 15); assert_eq!(C, 24); From 4cea356aeeb8d1ff74fbb85e5198ba2e98958ae6 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 23 Feb 2024 19:34:17 +0100 Subject: [PATCH 2/3] interpret: do no ICE on OOB shuffle/insert/extract indices --- src/shims/intrinsics/simd.rs | 10 ++++------ tests/fail/intrinsics/simd-extract.rs | 8 ++++++++ tests/fail/intrinsics/simd-extract.stderr | 15 +++++++++++++++ 3 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 tests/fail/intrinsics/simd-extract.rs create mode 100644 tests/fail/intrinsics/simd-extract.stderr diff --git a/src/shims/intrinsics/simd.rs b/src/shims/intrinsics/simd.rs index ea2d104694..ca8773cac1 100644 --- a/src/shims/intrinsics/simd.rs +++ b/src/shims/intrinsics/simd.rs @@ -563,9 +563,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let right_idx = src_index.checked_sub(left_len).unwrap(); this.read_immediate(&this.project_index(&right, right_idx)?)? } else { - span_bug!( - this.cur_span(), - "simd_shuffle index {src_index} is out of bounds for 2 vectors of size {left_len}", + throw_ub_format!( + "`simd_shuffle_generic` index {src_index} is out-of-bounds for 2 vectors with length {dest_len}" ); }; this.write_immediate(*val, &dest)?; @@ -604,9 +603,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { let right_idx = src_index.checked_sub(left_len).unwrap(); this.read_immediate(&this.project_index(&right, right_idx)?)? } else { - span_bug!( - this.cur_span(), - "simd_shuffle index {src_index} is out of bounds for 2 vectors of size {left_len}", + throw_ub_format!( + "`simd_shuffle` index {src_index} is out-of-bounds for 2 vectors with length {dest_len}" ); }; this.write_immediate(*val, &dest)?; diff --git a/tests/fail/intrinsics/simd-extract.rs b/tests/fail/intrinsics/simd-extract.rs new file mode 100644 index 0000000000..02b9d30df5 --- /dev/null +++ b/tests/fail/intrinsics/simd-extract.rs @@ -0,0 +1,8 @@ +#![feature(portable_simd, core_intrinsics)] +use std::simd::*; + +fn main() { + let v = i32x4::splat(0); + let _x: i32 = unsafe { std::intrinsics::simd::simd_extract(v, 4) }; + //~^ERROR: index 4 is out-of-bounds +} diff --git a/tests/fail/intrinsics/simd-extract.stderr b/tests/fail/intrinsics/simd-extract.stderr new file mode 100644 index 0000000000..dc6b22de49 --- /dev/null +++ b/tests/fail/intrinsics/simd-extract.stderr @@ -0,0 +1,15 @@ +error: Undefined Behavior: `simd_extract` index 4 is out-of-bounds of vector with length 4 + --> $DIR/simd-extract.rs:LL:CC + | +LL | let _x: i32 = unsafe { std::intrinsics::simd::simd_extract(v, 4) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `simd_extract` index 4 is out-of-bounds of vector with length 4 + | + = 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: BACKTRACE: + = note: inside `main` at $DIR/simd-extract.rs:LL:CC + +note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace + +error: aborting due to 1 previous error + From fd65260a19dc70422df6b20617f07a5671033e7b Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sun, 25 Feb 2024 10:41:14 +0100 Subject: [PATCH 3/3] Preparing for merge from rustc --- rust-version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-version b/rust-version index 02ab748c44..044f5cf9ca 100644 --- a/rust-version +++ b/rust-version @@ -1 +1 @@ -c5f69bdd5173a948e0131f934fa7c4cbf5e0b55f +a2f3c0cf880ad819c4eab2b320525b6a31ac6513