5
5
use std:: io:: Error ;
6
6
use std:: { ptr, slice} ;
7
7
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
+ ) {
9
18
let page_size = page_size:: get ( ) ;
10
19
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 (
124
21
ptr:: null_mut ( ) ,
125
22
page_size,
126
23
libc:: PROT_READ | libc:: PROT_WRITE ,
127
24
libc:: MAP_PRIVATE | libc:: MAP_ANONYMOUS ,
128
25
-1 ,
129
- 0 ,
26
+ Default :: default ( ) ,
130
27
)
131
28
} ;
132
29
assert ! ( !ptr. is_null( ) ) ;
@@ -147,40 +44,40 @@ fn test_mmap64() {
147
44
148
45
// Test all of our error conditions
149
46
let ptr = unsafe {
150
- libc :: mmap64 (
47
+ mmap (
151
48
ptr:: null_mut ( ) ,
152
49
page_size,
153
50
libc:: PROT_READ | libc:: PROT_WRITE ,
154
51
libc:: MAP_PRIVATE | libc:: MAP_SHARED , // Can't be both private and shared
155
52
-1 ,
156
- 0 ,
53
+ Default :: default ( ) ,
157
54
)
158
55
} ;
159
56
assert_eq ! ( ptr, libc:: MAP_FAILED ) ;
160
57
assert_eq ! ( Error :: last_os_error( ) . raw_os_error( ) . unwrap( ) , libc:: EINVAL ) ;
161
58
162
59
let ptr = unsafe {
163
- libc :: mmap64 (
60
+ mmap (
164
61
ptr:: null_mut ( ) ,
165
62
0 , // Can't map no memory
166
63
libc:: PROT_READ | libc:: PROT_WRITE ,
167
64
libc:: MAP_PRIVATE | libc:: MAP_ANONYMOUS ,
168
65
-1 ,
169
- 0 ,
66
+ Default :: default ( ) ,
170
67
)
171
68
} ;
172
69
assert_eq ! ( ptr, libc:: MAP_FAILED ) ;
173
70
assert_eq ! ( Error :: last_os_error( ) . raw_os_error( ) . unwrap( ) , libc:: EINVAL ) ;
174
71
175
72
let ptr = unsafe {
176
- libc :: mmap64 (
73
+ mmap (
177
74
ptr:: invalid_mut ( page_size * 64 ) ,
178
75
page_size,
179
76
libc:: PROT_READ | libc:: PROT_WRITE ,
180
77
// We don't support MAP_FIXED
181
78
libc:: MAP_PRIVATE | libc:: MAP_ANONYMOUS | libc:: MAP_FIXED ,
182
79
-1 ,
183
- 0 ,
80
+ Default :: default ( ) ,
184
81
)
185
82
} ;
186
83
assert_eq ! ( ptr, libc:: MAP_FAILED ) ;
@@ -189,13 +86,13 @@ fn test_mmap64() {
189
86
// We don't support protections other than read+write
190
87
for prot in [ libc:: PROT_NONE , libc:: PROT_EXEC , libc:: PROT_READ , libc:: PROT_WRITE ] {
191
88
let ptr = unsafe {
192
- libc :: mmap64 (
89
+ mmap (
193
90
ptr:: null_mut ( ) ,
194
91
page_size,
195
92
prot,
196
93
libc:: MAP_PRIVATE | libc:: MAP_ANONYMOUS ,
197
94
-1 ,
198
- 0 ,
95
+ Default :: default ( ) ,
199
96
)
200
97
} ;
201
98
assert_eq ! ( ptr, libc:: MAP_FAILED ) ;
@@ -205,13 +102,13 @@ fn test_mmap64() {
205
102
// We report an error for mappings whose length cannot be rounded up to a multiple of
206
103
// the page size.
207
104
let ptr = unsafe {
208
- libc :: mmap64 (
105
+ mmap (
209
106
ptr:: null_mut ( ) ,
210
107
usize:: MAX - 1 ,
211
108
libc:: PROT_READ | libc:: PROT_WRITE ,
212
109
libc:: MAP_PRIVATE | libc:: MAP_ANONYMOUS ,
213
110
-1 ,
214
- 0 ,
111
+ Default :: default ( ) ,
215
112
)
216
113
} ;
217
114
assert_eq ! ( ptr, libc:: MAP_FAILED ) ;
@@ -275,9 +172,9 @@ fn test_mremap() {
275
172
}
276
173
277
174
fn main ( ) {
278
- test_mmap ( ) ;
175
+ test_mmap ( libc :: mmap ) ;
279
176
#[ cfg( target_os = "linux" ) ]
280
- test_mmap64 ( ) ;
177
+ test_mmap ( libc :: mmap64 ) ;
281
178
#[ cfg( target_os = "linux" ) ]
282
179
test_mremap ( ) ;
283
180
}
0 commit comments