about summary refs log tree commit diff
path: root/src/tools/miri/tests/pass-dep/libc/mmap.rs
blob: bfd840d2fb89d924b3c9b2c9bd4e2fb973ca2d0d (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//@ignore-target: windows # No mmap on Windows
//@compile-flags: -Zmiri-disable-isolation -Zmiri-permissive-provenance

use std::io::Error;
use std::{ptr, slice};

fn test_mmap<Offset: Default>(
    mmap: unsafe extern "C" fn(
        *mut libc::c_void,
        libc::size_t,
        libc::c_int,
        libc::c_int,
        libc::c_int,
        Offset,
    ) -> *mut libc::c_void,
) {
    let page_size = page_size::get();
    let ptr = unsafe {
        mmap(
            ptr::null_mut(),
            page_size,
            libc::PROT_READ | libc::PROT_WRITE,
            libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
            -1,
            Default::default(),
        )
    };
    assert!(!ptr.is_null());

    // Ensure that freshly mapped allocations are zeroed
    let slice = unsafe { slice::from_raw_parts_mut(ptr as *mut u8, page_size) };
    assert!(slice.iter().all(|b| *b == 0));

    // Do some writes, make sure they worked
    for b in slice.iter_mut() {
        *b = 1;
    }
    assert!(slice.iter().all(|b| *b == 1));

    // Ensure that we can munmap
    let res = unsafe { libc::munmap(ptr, page_size) };
    assert_eq!(res, 0i32);

    // Test all of our error conditions
    let ptr = unsafe {
        mmap(
            ptr::null_mut(),
            page_size,
            libc::PROT_READ | libc::PROT_WRITE,
            libc::MAP_PRIVATE | libc::MAP_SHARED, // Can't be both private and shared
            -1,
            Default::default(),
        )
    };
    assert_eq!(ptr, libc::MAP_FAILED);
    assert_eq!(Error::last_os_error().raw_os_error().unwrap(), libc::EINVAL);

    let ptr = unsafe {
        mmap(
            ptr::null_mut(),
            0, // Can't map no memory
            libc::PROT_READ | libc::PROT_WRITE,
            libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
            -1,
            Default::default(),
        )
    };
    assert_eq!(ptr, libc::MAP_FAILED);
    assert_eq!(Error::last_os_error().raw_os_error().unwrap(), libc::EINVAL);

    // We report an error for mappings whose length cannot be rounded up to a multiple of
    // the page size.
    let ptr = unsafe {
        mmap(
            ptr::null_mut(),
            usize::MAX - 1,
            libc::PROT_READ | libc::PROT_WRITE,
            libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
            -1,
            Default::default(),
        )
    };
    assert_eq!(ptr, libc::MAP_FAILED);

    // We report an error when trying to munmap an address which is not a multiple of the page size
    let res = unsafe { libc::munmap(ptr::without_provenance_mut(1), page_size) };
    assert_eq!(res, -1);
    assert_eq!(Error::last_os_error().raw_os_error().unwrap(), libc::EINVAL);

    // We report an error when trying to munmap a length that cannot be rounded up to a multiple of
    // the page size.
    let res = unsafe { libc::munmap(ptr::without_provenance_mut(page_size), usize::MAX - 1) };
    assert_eq!(res, -1);
    assert_eq!(Error::last_os_error().raw_os_error().unwrap(), libc::EINVAL);
}

#[cfg(target_os = "linux")]
fn test_mremap() {
    let page_size = page_size::get();
    let ptr = unsafe {
        libc::mmap(
            ptr::null_mut(),
            page_size,
            libc::PROT_READ | libc::PROT_WRITE,
            libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
            -1,
            0,
        )
    };
    let slice = unsafe { slice::from_raw_parts_mut(ptr as *mut u8, page_size) };
    for b in slice.iter_mut() {
        *b = 1;
    }

    let ptr = unsafe { libc::mremap(ptr, page_size, page_size * 2, libc::MREMAP_MAYMOVE) };
    assert!(!ptr.is_null());

    let slice = unsafe { slice::from_raw_parts_mut(ptr as *mut u8, page_size * 2) };
    assert!(&slice[..page_size].iter().all(|b| *b == 1));
    assert!(&slice[page_size..].iter().all(|b| *b == 0));

    let res = unsafe { libc::munmap(ptr, page_size * 2) };
    assert_eq!(res, 0i32);

    // Test all of our error conditions
    // Not aligned
    let ptr = unsafe {
        libc::mremap(ptr::without_provenance_mut(1), page_size, page_size, libc::MREMAP_MAYMOVE)
    };
    assert_eq!(ptr, libc::MAP_FAILED);
    assert_eq!(Error::last_os_error().raw_os_error().unwrap(), libc::EINVAL);

    // Zero size
    let ptr = unsafe { libc::mremap(ptr::null_mut(), page_size, 0, libc::MREMAP_MAYMOVE) };
    assert_eq!(ptr, libc::MAP_FAILED);
    assert_eq!(Error::last_os_error().raw_os_error().unwrap(), libc::EINVAL);

    // Not setting MREMAP_MAYMOVE
    let ptr = unsafe { libc::mremap(ptr::null_mut(), page_size, page_size, 0) };
    assert_eq!(ptr, libc::MAP_FAILED);
    assert_eq!(Error::last_os_error().raw_os_error().unwrap(), libc::EINVAL);
}

fn main() {
    test_mmap(libc::mmap);
    #[cfg(target_os = "linux")]
    test_mmap(libc::mmap64);
    #[cfg(target_os = "linux")]
    test_mremap();
}