Skip to content

Commit aa40634

Browse files
committed
Factor out the redundancy between test_mmap and test_mmap64.
1 parent a6f1dba commit aa40634

File tree

1 file changed

+24
-127
lines changed
  • src/tools/miri/tests/pass-dep/shims

1 file changed

+24
-127
lines changed

src/tools/miri/tests/pass-dep/shims/mmap.rs

+24-127
Original file line numberDiff line numberDiff line change
@@ -5,128 +5,25 @@
55
use std::io::Error;
66
use std::{ptr, slice};
77

8-
fn test_mmap() {
8+
fn test_mmap<Offset: Default>(
9+
mmap: unsafe extern "C" fn(
10+
*mut libc::c_void,
11+
libc::size_t,
12+
libc::c_int,
13+
libc::c_int,
14+
libc::c_int,
15+
Offset,
16+
) -> *mut libc::c_void,
17+
) {
918
let page_size = page_size::get();
1019
let ptr = unsafe {
11-
libc::mmap(
12-
ptr::null_mut(),
13-
page_size,
14-
libc::PROT_READ | libc::PROT_WRITE,
15-
libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
16-
-1,
17-
0,
18-
)
19-
};
20-
assert!(!ptr.is_null());
21-
22-
// Ensure that freshly mapped allocations are zeroed
23-
let slice = unsafe { slice::from_raw_parts_mut(ptr as *mut u8, page_size) };
24-
assert!(slice.iter().all(|b| *b == 0));
25-
26-
// Do some writes, make sure they worked
27-
for b in slice.iter_mut() {
28-
*b = 1;
29-
}
30-
assert!(slice.iter().all(|b| *b == 1));
31-
32-
// Ensure that we can munmap
33-
let res = unsafe { libc::munmap(ptr, page_size) };
34-
assert_eq!(res, 0i32);
35-
36-
// Test all of our error conditions
37-
let ptr = unsafe {
38-
libc::mmap(
39-
ptr::null_mut(),
40-
page_size,
41-
libc::PROT_READ | libc::PROT_WRITE,
42-
libc::MAP_PRIVATE | libc::MAP_SHARED, // Can't be both private and shared
43-
-1,
44-
0,
45-
)
46-
};
47-
assert_eq!(ptr, libc::MAP_FAILED);
48-
assert_eq!(Error::last_os_error().raw_os_error().unwrap(), libc::EINVAL);
49-
50-
let ptr = unsafe {
51-
libc::mmap(
52-
ptr::null_mut(),
53-
0, // Can't map no memory
54-
libc::PROT_READ | libc::PROT_WRITE,
55-
libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
56-
-1,
57-
0,
58-
)
59-
};
60-
assert_eq!(ptr, libc::MAP_FAILED);
61-
assert_eq!(Error::last_os_error().raw_os_error().unwrap(), libc::EINVAL);
62-
63-
let ptr = unsafe {
64-
libc::mmap(
65-
ptr::invalid_mut(page_size * 64),
66-
page_size,
67-
libc::PROT_READ | libc::PROT_WRITE,
68-
// We don't support MAP_FIXED
69-
libc::MAP_PRIVATE | libc::MAP_ANONYMOUS | libc::MAP_FIXED,
70-
-1,
71-
0,
72-
)
73-
};
74-
assert_eq!(ptr, libc::MAP_FAILED);
75-
assert_eq!(Error::last_os_error().raw_os_error().unwrap(), libc::ENOTSUP);
76-
77-
// We don't support protections other than read+write
78-
for prot in [libc::PROT_NONE, libc::PROT_EXEC, libc::PROT_READ, libc::PROT_WRITE] {
79-
let ptr = unsafe {
80-
libc::mmap(
81-
ptr::null_mut(),
82-
page_size,
83-
prot,
84-
libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
85-
-1,
86-
0,
87-
)
88-
};
89-
assert_eq!(ptr, libc::MAP_FAILED);
90-
assert_eq!(Error::last_os_error().raw_os_error().unwrap(), libc::ENOTSUP);
91-
}
92-
93-
// We report an error for mappings whose length cannot be rounded up to a multiple of
94-
// the page size.
95-
let ptr = unsafe {
96-
libc::mmap(
97-
ptr::null_mut(),
98-
usize::MAX - 1,
99-
libc::PROT_READ | libc::PROT_WRITE,
100-
libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
101-
-1,
102-
0,
103-
)
104-
};
105-
assert_eq!(ptr, libc::MAP_FAILED);
106-
107-
// We report an error when trying to munmap an address which is not a multiple of the page size
108-
let res = unsafe { libc::munmap(ptr::invalid_mut(1), page_size) };
109-
assert_eq!(res, -1);
110-
assert_eq!(Error::last_os_error().raw_os_error().unwrap(), libc::EINVAL);
111-
112-
// We report an error when trying to munmap a length that cannot be rounded up to a multiple of
113-
// the page size.
114-
let res = unsafe { libc::munmap(ptr::invalid_mut(page_size), usize::MAX - 1) };
115-
assert_eq!(res, -1);
116-
assert_eq!(Error::last_os_error().raw_os_error().unwrap(), libc::EINVAL);
117-
}
118-
119-
#[cfg(target_os = "linux")]
120-
fn test_mmap64() {
121-
let page_size = page_size::get();
122-
let ptr = unsafe {
123-
libc::mmap64(
20+
mmap(
12421
ptr::null_mut(),
12522
page_size,
12623
libc::PROT_READ | libc::PROT_WRITE,
12724
libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
12825
-1,
129-
0,
26+
Default::default(),
13027
)
13128
};
13229
assert!(!ptr.is_null());
@@ -147,40 +44,40 @@ fn test_mmap64() {
14744

14845
// Test all of our error conditions
14946
let ptr = unsafe {
150-
libc::mmap64(
47+
mmap(
15148
ptr::null_mut(),
15249
page_size,
15350
libc::PROT_READ | libc::PROT_WRITE,
15451
libc::MAP_PRIVATE | libc::MAP_SHARED, // Can't be both private and shared
15552
-1,
156-
0,
53+
Default::default(),
15754
)
15855
};
15956
assert_eq!(ptr, libc::MAP_FAILED);
16057
assert_eq!(Error::last_os_error().raw_os_error().unwrap(), libc::EINVAL);
16158

16259
let ptr = unsafe {
163-
libc::mmap64(
60+
mmap(
16461
ptr::null_mut(),
16562
0, // Can't map no memory
16663
libc::PROT_READ | libc::PROT_WRITE,
16764
libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
16865
-1,
169-
0,
66+
Default::default(),
17067
)
17168
};
17269
assert_eq!(ptr, libc::MAP_FAILED);
17370
assert_eq!(Error::last_os_error().raw_os_error().unwrap(), libc::EINVAL);
17471

17572
let ptr = unsafe {
176-
libc::mmap64(
73+
mmap(
17774
ptr::invalid_mut(page_size * 64),
17875
page_size,
17976
libc::PROT_READ | libc::PROT_WRITE,
18077
// We don't support MAP_FIXED
18178
libc::MAP_PRIVATE | libc::MAP_ANONYMOUS | libc::MAP_FIXED,
18279
-1,
183-
0,
80+
Default::default(),
18481
)
18582
};
18683
assert_eq!(ptr, libc::MAP_FAILED);
@@ -189,13 +86,13 @@ fn test_mmap64() {
18986
// We don't support protections other than read+write
19087
for prot in [libc::PROT_NONE, libc::PROT_EXEC, libc::PROT_READ, libc::PROT_WRITE] {
19188
let ptr = unsafe {
192-
libc::mmap64(
89+
mmap(
19390
ptr::null_mut(),
19491
page_size,
19592
prot,
19693
libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
19794
-1,
198-
0,
95+
Default::default(),
19996
)
20097
};
20198
assert_eq!(ptr, libc::MAP_FAILED);
@@ -205,13 +102,13 @@ fn test_mmap64() {
205102
// We report an error for mappings whose length cannot be rounded up to a multiple of
206103
// the page size.
207104
let ptr = unsafe {
208-
libc::mmap64(
105+
mmap(
209106
ptr::null_mut(),
210107
usize::MAX - 1,
211108
libc::PROT_READ | libc::PROT_WRITE,
212109
libc::MAP_PRIVATE | libc::MAP_ANONYMOUS,
213110
-1,
214-
0,
111+
Default::default(),
215112
)
216113
};
217114
assert_eq!(ptr, libc::MAP_FAILED);
@@ -275,9 +172,9 @@ fn test_mremap() {
275172
}
276173

277174
fn main() {
278-
test_mmap();
175+
test_mmap(libc::mmap);
279176
#[cfg(target_os = "linux")]
280-
test_mmap64();
177+
test_mmap(libc::mmap64);
281178
#[cfg(target_os = "linux")]
282179
test_mremap();
283180
}

0 commit comments

Comments
 (0)