forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#113441 - RalfJung:assign-no-overlap, r=oli-obk
miri: check that assignments do not self-overlap r? ``@oli-obk``
- Loading branch information
Showing
3 changed files
with
49 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#![feature(core_intrinsics)] | ||
#![feature(custom_mir)] | ||
|
||
use std::intrinsics::mir::*; | ||
|
||
// It's not that easy to fool the MIR validity check | ||
// which wants to prevent overlapping assignments... | ||
// So we use two separate pointer arguments, and then arrange for them to alias. | ||
#[custom_mir(dialect = "runtime", phase = "optimized")] | ||
pub fn self_copy(ptr1: *mut [i32; 4], ptr2: *mut [i32; 4]) { | ||
mir! { | ||
{ | ||
*ptr1 = *ptr2; //~ERROR: overlapping ranges | ||
Return() | ||
} | ||
} | ||
} | ||
|
||
pub fn main() { | ||
let mut x = [0; 4]; | ||
let ptr = std::ptr::addr_of_mut!(x); | ||
self_copy(ptr, ptr); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
error: Undefined Behavior: `copy_nonoverlapping` called on overlapping ranges | ||
--> $DIR/overlapping_assignment.rs:LL:CC | ||
| | ||
LL | *ptr1 = *ptr2; | ||
| ^^^^^^^^^^^^^ `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 `self_copy` at $DIR/overlapping_assignment.rs:LL:CC | ||
note: inside `main` | ||
--> $DIR/overlapping_assignment.rs:LL:CC | ||
| | ||
LL | self_copy(ptr, ptr); | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
|
||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace | ||
|
||
error: aborting due to previous error | ||
|