2019-08-25 07:26:56 -05:00
|
|
|
// ignore-tidy-linelength
|
2018-08-25 07:36:24 -05:00
|
|
|
#![allow(unused)]
|
2018-11-01 08:14:51 -05:00
|
|
|
#![allow(const_err)] // make sure we cannot allow away the errors tested here
|
2018-08-25 07:36:24 -05:00
|
|
|
|
2018-08-10 16:46:59 -05:00
|
|
|
// normalize-stderr-test "alignment \d+" -> "alignment N"
|
|
|
|
// normalize-stderr-test "offset \d+" -> "offset N"
|
|
|
|
// normalize-stderr-test "allocation \d+" -> "allocation N"
|
|
|
|
// normalize-stderr-test "size \d+" -> "size N"
|
|
|
|
|
2018-08-17 05:39:36 -05:00
|
|
|
union BoolTransmute {
|
|
|
|
val: u8,
|
|
|
|
bl: bool,
|
|
|
|
}
|
|
|
|
|
2018-06-04 11:32:06 -05:00
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct SliceRepr {
|
|
|
|
ptr: *const u8,
|
|
|
|
len: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct BadSliceRepr {
|
|
|
|
ptr: *const u8,
|
|
|
|
len: &'static u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
union SliceTransmute {
|
|
|
|
repr: SliceRepr,
|
|
|
|
bad: BadSliceRepr,
|
2019-08-25 07:26:56 -05:00
|
|
|
addr: usize,
|
2018-06-04 11:32:06 -05:00
|
|
|
slice: &'static [u8],
|
2019-08-25 07:26:56 -05:00
|
|
|
raw_slice: *const [u8],
|
2018-06-04 11:32:06 -05:00
|
|
|
str: &'static str,
|
2018-08-25 07:36:24 -05:00
|
|
|
my_str: &'static MyStr,
|
|
|
|
my_slice: &'static MySliceBool,
|
2018-06-04 11:32:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct DynRepr {
|
|
|
|
ptr: *const u8,
|
|
|
|
vtable: *const u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct DynRepr2 {
|
|
|
|
ptr: *const u8,
|
|
|
|
vtable: *const u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct BadDynRepr {
|
|
|
|
ptr: *const u8,
|
|
|
|
vtable: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
union DynTransmute {
|
|
|
|
repr: DynRepr,
|
|
|
|
repr2: DynRepr2,
|
|
|
|
bad: BadDynRepr,
|
2019-08-25 07:26:56 -05:00
|
|
|
addr: usize,
|
2019-05-28 13:46:13 -05:00
|
|
|
rust: &'static dyn Trait,
|
2019-08-25 07:26:56 -05:00
|
|
|
raw_rust: *const dyn Trait,
|
2018-06-04 11:32:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
trait Trait {}
|
2018-08-17 05:39:36 -05:00
|
|
|
impl Trait for bool {}
|
2018-06-04 11:32:06 -05:00
|
|
|
|
2018-08-25 07:36:24 -05:00
|
|
|
// custom unsized type
|
|
|
|
struct MyStr(str);
|
|
|
|
|
|
|
|
// custom unsized type with sized fields
|
|
|
|
struct MySlice<T: ?Sized>(bool, T);
|
|
|
|
type MySliceBool = MySlice<[bool]>;
|
2018-08-22 04:27:38 -05:00
|
|
|
|
2019-08-25 07:26:56 -05:00
|
|
|
// # str
|
2018-06-04 11:32:06 -05:00
|
|
|
// OK
|
2019-08-25 07:26:56 -05:00
|
|
|
const STR_VALID: &str = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 1 } }.str};
|
2018-08-17 05:39:36 -05:00
|
|
|
// bad str
|
2019-08-25 07:26:56 -05:00
|
|
|
const STR_TOO_LONG: &str = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 999 } }.str};
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2018-08-17 05:39:36 -05:00
|
|
|
// bad str
|
2019-08-25 07:26:56 -05:00
|
|
|
const STR_LENGTH_PTR: &str = unsafe { SliceTransmute { bad: BadSliceRepr { ptr: &42, len: &3 } }.str};
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2018-08-25 07:36:24 -05:00
|
|
|
// bad str in user-defined unsized type
|
2019-08-25 07:26:56 -05:00
|
|
|
const MY_STR_LENGTH_PTR: &MyStr = unsafe { SliceTransmute { bad: BadSliceRepr { ptr: &42, len: &3 } }.my_str};
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2018-06-04 11:32:06 -05:00
|
|
|
|
2019-08-25 07:26:56 -05:00
|
|
|
// invalid UTF-8
|
2019-08-27 11:36:11 -05:00
|
|
|
const STR_NO_UTF8: &str = unsafe { SliceTransmute { slice: &[0xFF] }.str };
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2019-08-25 07:26:56 -05:00
|
|
|
// invalid UTF-8 in user-defined str-like
|
2019-08-27 11:36:11 -05:00
|
|
|
const MYSTR_NO_UTF8: &MyStr = unsafe { SliceTransmute { slice: &[0xFF] }.my_str };
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2018-06-04 11:32:06 -05:00
|
|
|
|
2019-08-25 07:26:56 -05:00
|
|
|
// # slice
|
|
|
|
// OK
|
|
|
|
const SLICE_VALID: &[u8] = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 1 } }.slice};
|
|
|
|
// bad slice: length uninit
|
|
|
|
const SLICE_LENGTH_UNINIT: &[u8] = unsafe { SliceTransmute { addr: 42 }.slice};
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2019-08-25 07:26:56 -05:00
|
|
|
// bad slice: length too big
|
|
|
|
const SLICE_TOO_LONG: &[u8] = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 999 } }.slice};
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2019-08-25 07:26:56 -05:00
|
|
|
// bad slice: length not an int
|
|
|
|
const SLICE_LENGTH_PTR: &[u8] = unsafe { SliceTransmute { bad: BadSliceRepr { ptr: &42, len: &3 } }.slice};
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
|
|
|
|
2018-08-17 05:39:36 -05:00
|
|
|
// bad data *inside* the slice
|
2019-08-27 11:36:11 -05:00
|
|
|
const SLICE_CONTENT_INVALID: &[bool] = &[unsafe { BoolTransmute { val: 3 }.bl }];
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2018-08-17 05:39:36 -05:00
|
|
|
|
2018-08-25 07:36:24 -05:00
|
|
|
// good MySliceBool
|
2019-08-27 11:36:11 -05:00
|
|
|
const MYSLICE_GOOD: &MySliceBool = &MySlice(true, [false]);
|
2018-08-25 07:36:24 -05:00
|
|
|
// bad: sized field is not okay
|
2019-08-27 11:36:11 -05:00
|
|
|
const MYSLICE_PREFIX_BAD: &MySliceBool = &MySlice(unsafe { BoolTransmute { val: 3 }.bl }, [false]);
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2018-08-25 07:36:24 -05:00
|
|
|
// bad: unsized part is not okay
|
2019-08-27 11:36:11 -05:00
|
|
|
const MYSLICE_SUFFIX_BAD: &MySliceBool = &MySlice(true, [unsafe { BoolTransmute { val: 3 }.bl }]);
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2018-08-25 07:36:24 -05:00
|
|
|
|
2019-08-25 07:26:56 -05:00
|
|
|
// # raw slice
|
|
|
|
const RAW_SLICE_VALID: *const [u8] = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 1 } }.raw_slice}; // ok
|
|
|
|
const RAW_SLICE_TOO_LONG: *const [u8] = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: 999 } }.raw_slice}; // ok because raw
|
2019-08-26 15:19:47 -05:00
|
|
|
const RAW_SLICE_MUCH_TOO_LONG: *const [u8] = unsafe { SliceTransmute { repr: SliceRepr { ptr: &42, len: usize::max_value() } }.raw_slice}; // ok because raw
|
2019-08-25 07:26:56 -05:00
|
|
|
const RAW_SLICE_LENGTH_UNINIT: *const [u8] = unsafe { SliceTransmute { addr: 42 }.raw_slice};
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2019-08-25 07:26:56 -05:00
|
|
|
|
|
|
|
// # trait object
|
|
|
|
// bad trait object
|
2019-08-27 11:36:11 -05:00
|
|
|
const TRAIT_OBJ_SHORT_VTABLE_1: &dyn Trait = unsafe { DynTransmute { repr: DynRepr { ptr: &92, vtable: &3 } }.rust};
|
2019-08-25 07:26:56 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
|
|
|
// bad trait object
|
2019-08-27 11:36:11 -05:00
|
|
|
const TRAIT_OBJ_SHORT_VTABLE_2: &dyn Trait = unsafe { DynTransmute { repr2: DynRepr2 { ptr: &92, vtable: &3 } }.rust};
|
2019-08-25 07:26:56 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
|
|
|
// bad trait object
|
2019-08-27 11:36:11 -05:00
|
|
|
const TRAIT_OBJ_INT_VTABLE: &dyn Trait = unsafe { DynTransmute { bad: BadDynRepr { ptr: &92, vtable: 3 } }.rust};
|
2019-08-25 07:26:56 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
|
|
|
|
|
|
|
// bad data *inside* the trait object
|
2019-08-27 11:36:11 -05:00
|
|
|
const TRAIT_OBJ_CONTENT_INVALID: &dyn Trait = &unsafe { BoolTransmute { val: 3 }.bl };
|
2019-08-25 07:26:56 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
|
|
|
|
|
|
|
// # raw trait object
|
|
|
|
const RAW_TRAIT_OBJ_VTABLE_NULL: *const dyn Trait = unsafe { DynTransmute { bad: BadDynRepr { ptr: &92, vtable: 0 } }.rust};
|
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
|
|
|
const RAW_TRAIT_OBJ_VTABLE_INVALID: *const dyn Trait = unsafe { DynTransmute { repr2: DynRepr2 { ptr: &92, vtable: &3 } }.raw_rust};
|
2018-08-26 08:19:34 -05:00
|
|
|
//~^ ERROR it is undefined behavior to use this value
|
2019-08-25 07:26:56 -05:00
|
|
|
const RAW_TRAIT_OBJ_CONTENT_INVALID: *const dyn Trait = &unsafe { BoolTransmute { val: 3 }.bl } as *const _; // ok because raw
|
2018-08-25 07:36:24 -05:00
|
|
|
|
2018-06-04 11:32:06 -05:00
|
|
|
fn main() {
|
|
|
|
}
|