about summary refs log tree commit diff
path: root/src/tools/miri/tests/pass/ptr_offset.rs
blob: 89aff7bf635050682c5cd65a5573f7f4922ffcff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//@compile-flags: -Zmiri-permissive-provenance
use std::{mem, ptr};

fn main() {
    smoke();
    test_offset_from();
    test_vec_into_iter();
    ptr_arith_offset();
    ptr_arith_offset_overflow();
    ptr_offset();
}

fn smoke() {
    // Smoke-test various offsetting operations.
    let ptr = &5;
    let ptr = ptr as *const i32;
    let _val = ptr.wrapping_offset(0);
    let _val = unsafe { ptr.offset(0) };
    let _val = ptr.wrapping_add(0);
    let _val = unsafe { ptr.add(0) };
    let _val = ptr.wrapping_sub(0);
    let _val = unsafe { ptr.sub(0) };
    let _val = unsafe { ptr.offset_from(ptr) };
    let _val = unsafe { ptr.offset_from_unsigned(ptr) };
}

fn test_offset_from() {
    unsafe {
        let buf = [0u32; 4];

        let x = buf.as_ptr() as *const u8;
        let y = x.offset(12);

        assert_eq!(y.offset_from(x), 12);
        assert_eq!(y.offset_from_unsigned(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);

        let x = (((x as usize) * 2) / 2) as *const u8;
        assert_eq!(y.offset_from(x), 12);
        assert_eq!(y.offset_from_unsigned(x), 12);
        assert_eq!(x.offset_from(y), -12);
    }
}

// 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 subtracting 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
    }

    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);

    // 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 ());
        assert_eq!(f(), 42);
    }
}