12
12
#include <stdio.h>
13
13
#include <stdlib.h>
14
14
#include <unistd.h>
15
+ #include "../kselftest.h"
15
16
16
17
static void dump_maps (void )
17
18
{
@@ -28,15 +29,12 @@ static unsigned long find_base_addr(unsigned long size)
28
29
29
30
flags = MAP_PRIVATE | MAP_ANONYMOUS ;
30
31
addr = mmap (NULL , size , PROT_NONE , flags , -1 , 0 );
31
- if (addr == MAP_FAILED ) {
32
- printf ("Error: couldn't map the space we need for the test\n" );
33
- return 0 ;
34
- }
32
+ if (addr == MAP_FAILED )
33
+ ksft_exit_fail_msg ("Error: couldn't map the space we need for the test\n" );
34
+
35
+ if (munmap (addr , size ) != 0 )
36
+ ksft_exit_fail_msg ("Error: munmap failed\n" );
35
37
36
- if (munmap (addr , size ) != 0 ) {
37
- printf ("Error: couldn't map the space we need for the test\n" );
38
- return 0 ;
39
- }
40
38
return (unsigned long )addr ;
41
39
}
42
40
@@ -46,51 +44,39 @@ int main(void)
46
44
unsigned long flags , addr , size , page_size ;
47
45
char * p ;
48
46
47
+ ksft_print_header ();
48
+ ksft_set_plan (9 );
49
+
49
50
page_size = sysconf (_SC_PAGE_SIZE );
50
51
51
- // let's find a base addr that is free before we start the tests
52
+ /* let's find a base addr that is free before we start the tests */
52
53
size = 5 * page_size ;
53
54
base_addr = find_base_addr (size );
54
- if (!base_addr ) {
55
- printf ("Error: couldn't map the space we need for the test\n" );
56
- return 1 ;
57
- }
58
55
59
56
flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED_NOREPLACE ;
60
57
61
- // Check we can map all the areas we need below
62
- errno = 0 ;
58
+ /* Check we can map all the areas we need below */
63
59
addr = base_addr ;
64
60
size = 5 * page_size ;
65
61
p = mmap ((void * )addr , size , PROT_NONE , flags , -1 , 0 );
66
-
67
- printf ("mmap() @ 0x%lx-0x%lx p=%p result=%m\n" , addr , addr + size , p );
68
-
69
62
if (p == MAP_FAILED ) {
70
63
dump_maps ();
71
- printf ("Error: couldn't map the space we need for the test\n" );
72
- return 1 ;
64
+ ksft_exit_fail_msg ("Error: couldn't map the space we need for the test\n" );
73
65
}
74
-
75
- errno = 0 ;
76
66
if (munmap ((void * )addr , 5 * page_size ) != 0 ) {
77
67
dump_maps ();
78
- printf ("Error: munmap failed!?\n" );
79
- return 1 ;
68
+ ksft_exit_fail_msg ("Error: munmap failed!?\n" );
80
69
}
81
- printf ( "unmap () successful \n" );
70
+ ksft_test_result_pass ( "mmap () @ 0x%lx-0x%lx p=%p result=%m \n", addr , addr + size , p );
82
71
83
- errno = 0 ;
84
72
addr = base_addr + page_size ;
85
73
size = 3 * page_size ;
86
74
p = mmap ((void * )addr , size , PROT_NONE , flags , -1 , 0 );
87
- printf ("mmap() @ 0x%lx-0x%lx p=%p result=%m\n" , addr , addr + size , p );
88
-
89
75
if (p == MAP_FAILED ) {
90
76
dump_maps ();
91
- printf ("Error: first mmap() failed unexpectedly\n" );
92
- return 1 ;
77
+ ksft_exit_fail_msg ("Error: first mmap() failed unexpectedly\n" );
93
78
}
79
+ ksft_test_result_pass ("mmap() @ 0x%lx-0x%lx p=%p result=%m\n" , addr , addr + size , p );
94
80
95
81
/*
96
82
* Exact same mapping again:
@@ -100,17 +86,14 @@ int main(void)
100
86
* +3 | mapped | new
101
87
* +4 | free | new
102
88
*/
103
- errno = 0 ;
104
89
addr = base_addr ;
105
90
size = 5 * page_size ;
106
91
p = mmap ((void * )addr , size , PROT_NONE , flags , -1 , 0 );
107
- printf ("mmap() @ 0x%lx-0x%lx p=%p result=%m\n" , addr , addr + size , p );
108
-
109
92
if (p != MAP_FAILED ) {
110
93
dump_maps ();
111
- printf ("Error:1: mmap() succeeded when it shouldn't have\n" );
112
- return 1 ;
94
+ ksft_exit_fail_msg ("Error:1: mmap() succeeded when it shouldn't have\n" );
113
95
}
96
+ ksft_test_result_pass ("mmap() @ 0x%lx-0x%lx p=%p result=%m\n" , addr , addr + size , p );
114
97
115
98
/*
116
99
* Second mapping contained within first:
@@ -121,17 +104,14 @@ int main(void)
121
104
* +3 | mapped |
122
105
* +4 | free |
123
106
*/
124
- errno = 0 ;
125
107
addr = base_addr + (2 * page_size );
126
108
size = page_size ;
127
109
p = mmap ((void * )addr , size , PROT_NONE , flags , -1 , 0 );
128
- printf ("mmap() @ 0x%lx-0x%lx p=%p result=%m\n" , addr , addr + size , p );
129
-
130
110
if (p != MAP_FAILED ) {
131
111
dump_maps ();
132
- printf ("Error:2: mmap() succeeded when it shouldn't have\n" );
133
- return 1 ;
112
+ ksft_exit_fail_msg ("Error:2: mmap() succeeded when it shouldn't have\n" );
134
113
}
114
+ ksft_test_result_pass ("mmap() @ 0x%lx-0x%lx p=%p result=%m\n" , addr , addr + size , p );
135
115
136
116
/*
137
117
* Overlap end of existing mapping:
@@ -141,17 +121,14 @@ int main(void)
141
121
* +3 | mapped | new
142
122
* +4 | free | new
143
123
*/
144
- errno = 0 ;
145
124
addr = base_addr + (3 * page_size );
146
125
size = 2 * page_size ;
147
126
p = mmap ((void * )addr , size , PROT_NONE , flags , -1 , 0 );
148
- printf ("mmap() @ 0x%lx-0x%lx p=%p result=%m\n" , addr , addr + size , p );
149
-
150
127
if (p != MAP_FAILED ) {
151
128
dump_maps ();
152
- printf ("Error:3: mmap() succeeded when it shouldn't have\n" );
153
- return 1 ;
129
+ ksft_exit_fail_msg ("Error:3: mmap() succeeded when it shouldn't have\n" );
154
130
}
131
+ ksft_test_result_pass ("mmap() @ 0x%lx-0x%lx p=%p result=%m\n" , addr , addr + size , p );
155
132
156
133
/*
157
134
* Overlap start of existing mapping:
@@ -161,17 +138,14 @@ int main(void)
161
138
* +3 | mapped |
162
139
* +4 | free |
163
140
*/
164
- errno = 0 ;
165
141
addr = base_addr ;
166
142
size = 2 * page_size ;
167
143
p = mmap ((void * )addr , size , PROT_NONE , flags , -1 , 0 );
168
- printf ("mmap() @ 0x%lx-0x%lx p=%p result=%m\n" , addr , addr + size , p );
169
-
170
144
if (p != MAP_FAILED ) {
171
145
dump_maps ();
172
- printf ("Error:4: mmap() succeeded when it shouldn't have\n" );
173
- return 1 ;
146
+ ksft_exit_fail_msg ("Error:4: mmap() succeeded when it shouldn't have\n" );
174
147
}
148
+ ksft_test_result_pass ("mmap() @ 0x%lx-0x%lx p=%p result=%m\n" , addr , addr + size , p );
175
149
176
150
/*
177
151
* Adjacent to start of existing mapping:
@@ -181,17 +155,14 @@ int main(void)
181
155
* +3 | mapped |
182
156
* +4 | free |
183
157
*/
184
- errno = 0 ;
185
158
addr = base_addr ;
186
159
size = page_size ;
187
160
p = mmap ((void * )addr , size , PROT_NONE , flags , -1 , 0 );
188
- printf ("mmap() @ 0x%lx-0x%lx p=%p result=%m\n" , addr , addr + size , p );
189
-
190
161
if (p == MAP_FAILED ) {
191
162
dump_maps ();
192
- printf ("Error:5: mmap() failed when it shouldn't have\n" );
193
- return 1 ;
163
+ ksft_exit_fail_msg ("Error:5: mmap() failed when it shouldn't have\n" );
194
164
}
165
+ ksft_test_result_pass ("mmap() @ 0x%lx-0x%lx p=%p result=%m\n" , addr , addr + size , p );
195
166
196
167
/*
197
168
* Adjacent to end of existing mapping:
@@ -201,27 +172,22 @@ int main(void)
201
172
* +3 | mapped |
202
173
* +4 | free | new
203
174
*/
204
- errno = 0 ;
205
175
addr = base_addr + (4 * page_size );
206
176
size = page_size ;
207
177
p = mmap ((void * )addr , size , PROT_NONE , flags , -1 , 0 );
208
- printf ("mmap() @ 0x%lx-0x%lx p=%p result=%m\n" , addr , addr + size , p );
209
-
210
178
if (p == MAP_FAILED ) {
211
179
dump_maps ();
212
- printf ("Error:6: mmap() failed when it shouldn't have\n" );
213
- return 1 ;
180
+ ksft_exit_fail_msg ("Error:6: mmap() failed when it shouldn't have\n" );
214
181
}
182
+ ksft_test_result_pass ("mmap() @ 0x%lx-0x%lx p=%p result=%m\n" , addr , addr + size , p );
215
183
216
184
addr = base_addr ;
217
185
size = 5 * page_size ;
218
186
if (munmap ((void * )addr , size ) != 0 ) {
219
187
dump_maps ();
220
- printf ("Error: munmap failed!?\n" );
221
- return 1 ;
188
+ ksft_exit_fail_msg ("Error: munmap failed!?\n" );
222
189
}
223
- printf ( " unmap() successful\n" );
190
+ ksft_test_result_pass ( "Base Address unmap() successful\n" );
224
191
225
- printf ("OK\n" );
226
- return 0 ;
192
+ ksft_finished ();
227
193
}
0 commit comments