2021-06-15 12:22:31 -05:00
|
|
|
#![stable(feature = "dummy", since = "1.0.0")]
|
|
|
|
|
2021-06-15 09:47:31 -05:00
|
|
|
// ignore-tidy-linelength
|
2021-06-15 12:22:31 -05:00
|
|
|
#![feature(intrinsics, staged_api)]
|
2022-05-22 07:29:54 -05:00
|
|
|
#![feature(const_mut_refs)]
|
2021-06-15 12:22:31 -05:00
|
|
|
use std::mem;
|
|
|
|
|
|
|
|
extern "rust-intrinsic" {
|
2022-05-22 07:29:54 -05:00
|
|
|
#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")]
|
2021-06-15 12:22:31 -05:00
|
|
|
fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
|
|
|
|
|
2022-05-22 07:29:54 -05:00
|
|
|
#[rustc_const_stable(feature = "const_intrinsic_copy", since = "1.63.0")]
|
2021-06-15 12:22:31 -05:00
|
|
|
fn copy<T>(src: *const T, dst: *mut T, count: usize);
|
|
|
|
}
|
2021-06-15 09:47:31 -05:00
|
|
|
|
|
|
|
const COPY_ZERO: () = unsafe {
|
|
|
|
// Since we are not copying anything, this should be allowed.
|
|
|
|
let src = ();
|
|
|
|
let mut dst = ();
|
2022-11-20 02:54:45 -06:00
|
|
|
copy_nonoverlapping(&src as *const _ as *const u8, &mut dst as *mut _ as *mut u8, 0);
|
2021-06-15 09:47:31 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
const COPY_OOB_1: () = unsafe {
|
|
|
|
let mut x = 0i32;
|
|
|
|
let dangle = (&mut x as *mut i32).wrapping_add(10);
|
2024-05-09 05:35:11 -05:00
|
|
|
// Zero-sized copy is fine.
|
|
|
|
copy_nonoverlapping(0x100 as *const i32, dangle, 0);
|
|
|
|
// Non-zero-sized copy is not.
|
|
|
|
copy_nonoverlapping(0x100 as *const i32, dangle, 1); //~ ERROR evaluation of constant value failed [E0080]
|
|
|
|
//~| 0x100[noalloc] is a dangling pointer
|
2021-06-15 09:47:31 -05:00
|
|
|
};
|
|
|
|
const COPY_OOB_2: () = unsafe {
|
|
|
|
let x = 0i32;
|
|
|
|
let dangle = (&x as *const i32).wrapping_add(10);
|
2024-05-09 05:35:11 -05:00
|
|
|
// Zero-sized copy is fine.
|
|
|
|
copy_nonoverlapping(dangle, 0x100 as *mut i32, 0);
|
|
|
|
// Non-zero-sized copy is not.
|
|
|
|
copy_nonoverlapping(dangle, 0x100 as *mut i32, 1); //~ ERROR evaluation of constant value failed [E0080]
|
|
|
|
//~| offset 40 is out-of-bounds
|
2021-06-15 09:47:31 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
const COPY_SIZE_OVERFLOW: () = unsafe {
|
|
|
|
let x = 0;
|
|
|
|
let mut y = 0;
|
2021-07-15 13:07:01 -05:00
|
|
|
copy(&x, &mut y, 1usize << (mem::size_of::<usize>() * 8 - 1)); //~ ERROR evaluation of constant value failed [E0080]
|
2021-06-15 09:47:31 -05:00
|
|
|
//~| overflow computing total size of `copy`
|
|
|
|
};
|
|
|
|
const COPY_NONOVERLAPPING_SIZE_OVERFLOW: () = unsafe {
|
|
|
|
let x = 0;
|
|
|
|
let mut y = 0;
|
2021-06-27 06:48:01 -05:00
|
|
|
copy_nonoverlapping(&x, &mut y, 1usize << (mem::size_of::<usize>() * 8 - 1)); //~ evaluation of constant value failed [E0080]
|
2021-06-15 09:47:31 -05:00
|
|
|
//~| overflow computing total size of `copy_nonoverlapping`
|
|
|
|
};
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
}
|