rust/tests/pass/ptr_offset.rs

75 lines
2.0 KiB
Rust
Raw Normal View History

2022-07-08 11:08:32 -05:00
//@compile-flags: -Zmiri-permissive-provenance
2020-04-16 02:06:21 -05:00
use std::{mem, ptr};
2018-12-26 09:23:04 -06:00
fn main() {
2020-04-16 02:06:21 -05:00
test_offset_from();
test_vec_into_iter();
ptr_arith_offset();
ptr_arith_offset_overflow();
ptr_offset();
}
fn test_offset_from() {
unsafe {
let buf = [0u32; 4];
2020-04-16 02:06:21 -05:00
let x = buf.as_ptr() as *const u8;
let y = x.offset(12);
2020-04-16 02:06:21 -05:00
assert_eq!(y.offset_from(x), 12);
assert_eq!(x.offset_from(y), -12);
assert_eq!((y as *const u32).offset_from(x as *const u32), 12 / 4);
assert_eq!((x as *const u32).offset_from(y as *const u32), -12 / 4);
2022-04-30 12:40:35 -05:00
let x = (((x as usize) * 2) / 2) as *const u8;
assert_eq!(y.offset_from(x), 12);
assert_eq!(x.offset_from(y), -12);
}
}
2020-04-16 02:06:21 -05:00
// This also internally uses offset_from.
fn test_vec_into_iter() {
let v = Vec::<i32>::new();
let i = v.into_iter();
i.size_hint();
}
fn ptr_arith_offset() {
let v = [1i16, 2];
let x = &v as *const [i16] as *const i16;
let x = x.wrapping_offset(1);
assert_eq!(unsafe { *x }, 2);
}
fn ptr_arith_offset_overflow() {
let v = [1i16, 2];
let x = &mut ptr::null(); // going through memory as there are more sanity checks along that path
*x = v.as_ptr().wrapping_offset(1); // ptr to the 2nd element
// Adding 2*isize::max and then 1 is like substracting 1
*x = x.wrapping_offset(isize::MAX);
*x = x.wrapping_offset(isize::MAX);
*x = x.wrapping_offset(1);
assert_eq!(unsafe { **x }, 1);
}
fn ptr_offset() {
fn f() -> i32 {
42
}
2020-04-16 02:06:21 -05:00
let v = [1i16, 2];
let x = &v as *const [i16; 2] as *const i16;
let x = unsafe { x.offset(1) };
assert_eq!(unsafe { *x }, 2);
2018-12-26 09:23:04 -06:00
// fn ptr offset
unsafe {
let p = f as fn() -> i32 as usize;
let x = (p as *mut u32).offset(0) as usize;
// *cast* to ptr, then transmute to fn ptr.
// (transmuting int to [fn]ptr causes trouble.)
let f: fn() -> i32 = mem::transmute(x as *const ());
2018-12-26 09:23:04 -06:00
assert_eq!(f(), 42);
}
}