Skip to content

Commit 00dfa3b

Browse files
committed
miri: add test for overlapping typed_swap
1 parent 6de3a2e commit 00dfa3b

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
649649
let kind = MemoryKind::Stack;
650650
let temp = self.allocate(left.layout, kind)?;
651651
self.copy_op(&left, &temp)?;
652-
self.copy_op(&right, &left)?;
652+
self.copy_op(&right, &left)?; // this checks that they are non-overlapping
653653
self.copy_op(&temp, &right)?;
654654
self.deallocate_ptr(temp.ptr(), None, kind)?;
655655
interp_ok(())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(core_intrinsics)]
2+
#![feature(rustc_attrs)]
3+
4+
use std::intrinsics::typed_swap;
5+
use std::ptr::addr_of_mut;
6+
7+
fn main() {
8+
let mut a = [0_u8; 100];
9+
unsafe {
10+
let a = addr_of_mut!(a);
11+
typed_swap(a, a); //~ERROR: called on overlapping ranges
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: `copy_nonoverlapping` called on overlapping ranges
2+
--> tests/fail/intrinsics/typed-swap-overlap.rs:LL:CC
3+
|
4+
LL | typed_swap(a, a);
5+
| ^^^^^^^^^^^^^^^^ `copy_nonoverlapping` called on overlapping ranges
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: BACKTRACE:
10+
= note: inside `main` at tests/fail/intrinsics/typed-swap-overlap.rs:LL:CC
11+
12+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
14+
error: aborting due to 1 previous error
15+

0 commit comments

Comments
 (0)