From f0d5854c5e9fd6ca78797696ea992ab0998bc9d9 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 25 Dec 2024 10:45:48 +0100 Subject: [PATCH 1/5] miri: add test for overlapping typed_swap --- tests/fail/intrinsics/typed-swap-overlap.rs | 13 +++++++++++++ tests/fail/intrinsics/typed-swap-overlap.stderr | 15 +++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 tests/fail/intrinsics/typed-swap-overlap.rs create mode 100644 tests/fail/intrinsics/typed-swap-overlap.stderr diff --git a/tests/fail/intrinsics/typed-swap-overlap.rs b/tests/fail/intrinsics/typed-swap-overlap.rs new file mode 100644 index 0000000000..7a9ad63ad7 --- /dev/null +++ b/tests/fail/intrinsics/typed-swap-overlap.rs @@ -0,0 +1,13 @@ +#![feature(core_intrinsics)] +#![feature(rustc_attrs)] + +use std::intrinsics::typed_swap; +use std::ptr::addr_of_mut; + +fn main() { + let mut a = [0_u8; 100]; + unsafe { + let a = addr_of_mut!(a); + typed_swap(a, a); //~ERROR: called on overlapping ranges + } +} diff --git a/tests/fail/intrinsics/typed-swap-overlap.stderr b/tests/fail/intrinsics/typed-swap-overlap.stderr new file mode 100644 index 0000000000..5c9cbb78c1 --- /dev/null +++ b/tests/fail/intrinsics/typed-swap-overlap.stderr @@ -0,0 +1,15 @@ +error: Undefined Behavior: `copy_nonoverlapping` called on overlapping ranges + --> tests/fail/intrinsics/typed-swap-overlap.rs:LL:CC + | +LL | typed_swap(a, a); + | ^^^^^^^^^^^^^^^^ `copy_nonoverlapping` called on overlapping ranges + | + = 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 tests/fail/intrinsics/typed-swap-overlap.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 2d1f266149ceebfedeb5b3fde19f27e6d53971f3 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 25 Dec 2024 10:49:23 +0100 Subject: [PATCH 2/5] =?UTF-8?q?rename=20typed=5Fswap=20=E2=86=92=20typed?= =?UTF-8?q?=5Fswap=5Fnonoverlapping?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/fail/intrinsics/typed-swap-invalid-array.rs | 4 ++-- tests/fail/intrinsics/typed-swap-invalid-array.stderr | 4 ++-- tests/fail/intrinsics/typed-swap-invalid-scalar.rs | 4 ++-- tests/fail/intrinsics/typed-swap-invalid-scalar.stderr | 4 ++-- tests/fail/intrinsics/typed-swap-overlap.rs | 4 ++-- tests/fail/intrinsics/typed-swap-overlap.stderr | 4 ++-- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/fail/intrinsics/typed-swap-invalid-array.rs b/tests/fail/intrinsics/typed-swap-invalid-array.rs index 89fdd2a01e..de154d771a 100644 --- a/tests/fail/intrinsics/typed-swap-invalid-array.rs +++ b/tests/fail/intrinsics/typed-swap-invalid-array.rs @@ -1,7 +1,7 @@ #![feature(core_intrinsics)] #![feature(rustc_attrs)] -use std::intrinsics::typed_swap; +use std::intrinsics::typed_swap_nonoverlapping; use std::ptr::addr_of_mut; fn invalid_array() { @@ -10,7 +10,7 @@ fn invalid_array() { unsafe { let a = addr_of_mut!(a).cast::<[bool; 100]>(); let b = addr_of_mut!(b).cast::<[bool; 100]>(); - typed_swap(a, b); //~ERROR: constructing invalid value + typed_swap_nonoverlapping(a, b); //~ERROR: constructing invalid value } } diff --git a/tests/fail/intrinsics/typed-swap-invalid-array.stderr b/tests/fail/intrinsics/typed-swap-invalid-array.stderr index 20b20412e7..5884d13a2a 100644 --- a/tests/fail/intrinsics/typed-swap-invalid-array.stderr +++ b/tests/fail/intrinsics/typed-swap-invalid-array.stderr @@ -1,8 +1,8 @@ error: Undefined Behavior: constructing invalid value at [0]: encountered 0x02, but expected a boolean --> tests/fail/intrinsics/typed-swap-invalid-array.rs:LL:CC | -LL | typed_swap(a, b); - | ^^^^^^^^^^^^^^^^ constructing invalid value at [0]: encountered 0x02, but expected a boolean +LL | typed_swap_nonoverlapping(a, b); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at [0]: encountered 0x02, but expected a boolean | = 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 diff --git a/tests/fail/intrinsics/typed-swap-invalid-scalar.rs b/tests/fail/intrinsics/typed-swap-invalid-scalar.rs index 9d014a523f..3cc96e79fe 100644 --- a/tests/fail/intrinsics/typed-swap-invalid-scalar.rs +++ b/tests/fail/intrinsics/typed-swap-invalid-scalar.rs @@ -1,7 +1,7 @@ #![feature(core_intrinsics)] #![feature(rustc_attrs)] -use std::intrinsics::typed_swap; +use std::intrinsics::typed_swap_nonoverlapping; use std::ptr::addr_of_mut; fn invalid_scalar() { @@ -10,7 +10,7 @@ fn invalid_scalar() { unsafe { let a = addr_of_mut!(a).cast::(); let b = addr_of_mut!(b).cast::(); - typed_swap(a, b); //~ERROR: constructing invalid value + typed_swap_nonoverlapping(a, b); //~ERROR: constructing invalid value } } diff --git a/tests/fail/intrinsics/typed-swap-invalid-scalar.stderr b/tests/fail/intrinsics/typed-swap-invalid-scalar.stderr index 6062465f36..9804233c7f 100644 --- a/tests/fail/intrinsics/typed-swap-invalid-scalar.stderr +++ b/tests/fail/intrinsics/typed-swap-invalid-scalar.stderr @@ -1,8 +1,8 @@ error: Undefined Behavior: constructing invalid value: encountered 0x02, but expected a boolean --> tests/fail/intrinsics/typed-swap-invalid-scalar.rs:LL:CC | -LL | typed_swap(a, b); - | ^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x02, but expected a boolean +LL | typed_swap_nonoverlapping(a, b); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x02, but expected a boolean | = 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 diff --git a/tests/fail/intrinsics/typed-swap-overlap.rs b/tests/fail/intrinsics/typed-swap-overlap.rs index 7a9ad63ad7..7b1be4abb1 100644 --- a/tests/fail/intrinsics/typed-swap-overlap.rs +++ b/tests/fail/intrinsics/typed-swap-overlap.rs @@ -1,13 +1,13 @@ #![feature(core_intrinsics)] #![feature(rustc_attrs)] -use std::intrinsics::typed_swap; +use std::intrinsics::typed_swap_nonoverlapping; use std::ptr::addr_of_mut; fn main() { let mut a = [0_u8; 100]; unsafe { let a = addr_of_mut!(a); - typed_swap(a, a); //~ERROR: called on overlapping ranges + typed_swap_nonoverlapping(a, a); //~ERROR: called on overlapping ranges } } diff --git a/tests/fail/intrinsics/typed-swap-overlap.stderr b/tests/fail/intrinsics/typed-swap-overlap.stderr index 5c9cbb78c1..6d578841fe 100644 --- a/tests/fail/intrinsics/typed-swap-overlap.stderr +++ b/tests/fail/intrinsics/typed-swap-overlap.stderr @@ -1,8 +1,8 @@ error: Undefined Behavior: `copy_nonoverlapping` called on overlapping ranges --> tests/fail/intrinsics/typed-swap-overlap.rs:LL:CC | -LL | typed_swap(a, a); - | ^^^^^^^^^^^^^^^^ `copy_nonoverlapping` called on overlapping ranges +LL | typed_swap_nonoverlapping(a, a); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `copy_nonoverlapping` called on overlapping ranges | = 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 From 23190dd7c4abfefffdde4a7ed7042a066e34d26b Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 25 Dec 2024 15:58:57 +0100 Subject: [PATCH 3/5] swap_typed_nonoverlapping: properly detect overlap even when swapping scalar values --- ... => typed-swap-invalid-scalar.left.stderr} | 0 .../typed-swap-invalid-scalar.right.stderr | 20 +++++++++++++++++++ .../intrinsics/typed-swap-invalid-scalar.rs | 6 ++++-- tests/fail/intrinsics/typed-swap-overlap.rs | 2 +- 4 files changed, 25 insertions(+), 3 deletions(-) rename tests/fail/intrinsics/{typed-swap-invalid-scalar.stderr => typed-swap-invalid-scalar.left.stderr} (100%) create mode 100644 tests/fail/intrinsics/typed-swap-invalid-scalar.right.stderr diff --git a/tests/fail/intrinsics/typed-swap-invalid-scalar.stderr b/tests/fail/intrinsics/typed-swap-invalid-scalar.left.stderr similarity index 100% rename from tests/fail/intrinsics/typed-swap-invalid-scalar.stderr rename to tests/fail/intrinsics/typed-swap-invalid-scalar.left.stderr diff --git a/tests/fail/intrinsics/typed-swap-invalid-scalar.right.stderr b/tests/fail/intrinsics/typed-swap-invalid-scalar.right.stderr new file mode 100644 index 0000000000..54b21f155c --- /dev/null +++ b/tests/fail/intrinsics/typed-swap-invalid-scalar.right.stderr @@ -0,0 +1,20 @@ +error: Undefined Behavior: constructing invalid value: encountered 0x03, but expected a boolean + --> tests/fail/intrinsics/typed-swap-invalid-scalar.rs:LL:CC + | +LL | typed_swap_nonoverlapping(a, b); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x03, but expected a boolean + | + = 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 `invalid_scalar` at tests/fail/intrinsics/typed-swap-invalid-scalar.rs:LL:CC +note: inside `main` + --> tests/fail/intrinsics/typed-swap-invalid-scalar.rs:LL:CC + | +LL | invalid_scalar(); + | ^^^^^^^^^^^^^^^^ + +note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace + +error: aborting due to 1 previous error + diff --git a/tests/fail/intrinsics/typed-swap-invalid-scalar.rs b/tests/fail/intrinsics/typed-swap-invalid-scalar.rs index 3cc96e79fe..d5a72ea861 100644 --- a/tests/fail/intrinsics/typed-swap-invalid-scalar.rs +++ b/tests/fail/intrinsics/typed-swap-invalid-scalar.rs @@ -1,3 +1,4 @@ +//@revisions: left right #![feature(core_intrinsics)] #![feature(rustc_attrs)] @@ -5,8 +6,9 @@ use std::intrinsics::typed_swap_nonoverlapping; use std::ptr::addr_of_mut; fn invalid_scalar() { - let mut a = 1_u8; - let mut b = 2_u8; + // We run the test twice, with either the left or the right side being invalid. + let mut a = if cfg!(left) { 2_u8} else { 1_u8 }; + let mut b = if cfg!(right) { 3_u8} else { 1_u8 }; unsafe { let a = addr_of_mut!(a).cast::(); let b = addr_of_mut!(b).cast::(); diff --git a/tests/fail/intrinsics/typed-swap-overlap.rs b/tests/fail/intrinsics/typed-swap-overlap.rs index 7b1be4abb1..e643091a02 100644 --- a/tests/fail/intrinsics/typed-swap-overlap.rs +++ b/tests/fail/intrinsics/typed-swap-overlap.rs @@ -5,7 +5,7 @@ use std::intrinsics::typed_swap_nonoverlapping; use std::ptr::addr_of_mut; fn main() { - let mut a = [0_u8; 100]; + let mut a = 0_u8; unsafe { let a = addr_of_mut!(a); typed_swap_nonoverlapping(a, a); //~ERROR: called on overlapping ranges From 53d6d530fcba22a5202da89e0c73aba27f5875c9 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Mon, 30 Dec 2024 19:30:08 +0100 Subject: [PATCH 4/5] fmt Co-authored-by: scottmcm --- tests/fail/intrinsics/typed-swap-invalid-scalar.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/fail/intrinsics/typed-swap-invalid-scalar.rs b/tests/fail/intrinsics/typed-swap-invalid-scalar.rs index d5a72ea861..f5f9c7efbe 100644 --- a/tests/fail/intrinsics/typed-swap-invalid-scalar.rs +++ b/tests/fail/intrinsics/typed-swap-invalid-scalar.rs @@ -7,8 +7,8 @@ use std::ptr::addr_of_mut; fn invalid_scalar() { // We run the test twice, with either the left or the right side being invalid. - let mut a = if cfg!(left) { 2_u8} else { 1_u8 }; - let mut b = if cfg!(right) { 3_u8} else { 1_u8 }; + let mut a = if cfg!(left) { 2_u8 } else { 1_u8 }; + let mut b = if cfg!(right) { 3_u8 } else { 1_u8 }; unsafe { let a = addr_of_mut!(a).cast::(); let b = addr_of_mut!(b).cast::(); From 74c8aa58b32b237bba407513e8092c432f461d89 Mon Sep 17 00:00:00 2001 From: The Miri Cronjob Bot Date: Tue, 31 Dec 2024 05:05:17 +0000 Subject: [PATCH 5/5] Preparing for merge from rustc --- rust-version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust-version b/rust-version index 96ff7aae00..bbd3ed9439 100644 --- a/rust-version +++ b/rust-version @@ -1 +1 @@ -917bfa78478cbcc77406e5ea37b24c3eedefacf4 +4e5fec2f1ea4b1cfecaa14304c9f56de59b344cb