1 /* IBM Corporation */
2 /* 01/02/2003 Port to LTP avenkat@us.ibm.com */
3 /* 06/30/2001 Port to Linux nsharoff@us.ibm.com */
4 /*
5 * Copyright (c) International Business Machines Corp., 2003
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /*
23 * This test mmaps over the tail of the brk segment, growing and
24 * shrinking brk over holes, while changing from small to large and
25 * large to small virtual memory representations. After mmaping over the
26 * end of the brk segment, it increases the brk which should split
27 * it into two segments (i.e. |---brk---|-mmap-|--more brk--|). Next it
28 * decreases the brk segment to the end of the map, and finally decreases
29 * it some more. Then more vmsegments are created by punching holes in
30 * the brk segments with munmap. This should cause the vm system to use a
31 * large virtual address space object to keep track of this process. The
32 * above test is then repeated using the large process object. After
33 * this, the brk is shrunk to less than 1 page before exiting in order to
34 * test the code which compacts large address space objects. It also asks
35 * for a huge mmap which is refused.
36 */
37
38 #define _KMEMUSER
39 #include <sys/types.h>
40 #include <stdio.h>
41 #include <sys/mman.h>
42 #include <errno.h>
43 #include <unistd.h>
44 #include <limits.h>
45
46 /***** LTP Port *****/
47 #include "test.h"
48 #define FAILED 0
49 #define PASSED 1
50
51 char *TCID = "mmapstress03";
52 FILE *temp;
53 int TST_TOTAL = 1;
54
55 int anyfail();
56 void ok_exit();
57 /***** ** ** *****/
58
59 #define AS_SVSM_VSEG_MAX 48UL
60 #define AS_SVSM_MMAP_MAX 16UL
61
62 #define EXTRA_VSEGS 2L
63 #define NUM_SEGS (AS_SVSM_VSEG_MAX + EXTRA_VSEGS)
64 #define ERROR(M) (void)fprintf(stderr, "%s: errno = %d: " M "\n", progname, \
65 errno)
66 #define NEG1 (char *)-1
67 #define POINTER_SIZE (sizeof(void *) << 3)
68
69 extern long sysconf(int name);
70 extern void exit(int);
71 extern time_t time(time_t *);
72 extern char *ctime(const time_t *);
73 static void do_test(caddr_t brk_max, long pagesize);
74
75 static char *progname;
76
main(int argc,char * argv[])77 /*ARGSUSED*/ int main(int argc, char *argv[])
78 {
79 char *brk_max_addr, *hole_addr, *brk_start, *hole_start;
80 size_t pagesize = (size_t) sysconf(_SC_PAGE_SIZE);
81 time_t t;
82
83 progname = argv[0];
84
85 (void)time(&t);
86 // (void)printf("%s: Started %s", argv[0], ctime(&t));
87 if ((brk_start = sbrk(0)) == NEG1) {
88 ERROR("initial sbrk failed");
89 anyfail();
90 }
91 if ((u_long) brk_start % (u_long) pagesize) {
92 if (sbrk(pagesize - ((u_long) brk_start % (u_long) pagesize))
93 == NEG1) {
94 ERROR("couldn't round up brk to a page boundary");
95 anyfail();
96 }
97 }
98 /* The brk is now at the beginning of a page. */
99
100 if ((hole_addr = hole_start = sbrk(NUM_SEGS * 2 * pagesize)) == NEG1) {
101 ERROR("couldn't brk large space for segments");
102 anyfail();
103 }
104 if ((brk_max_addr = sbrk(0)) == NEG1) {
105 ERROR("couldn't find top of brk");
106 anyfail();
107 }
108 do_test((caddr_t) brk_max_addr, pagesize);
109
110 /* now make holes and repeat test */
111 while (hole_addr + pagesize < brk_max_addr) {
112 if (munmap(hole_addr, pagesize) == -1) {
113 ERROR("failed to munmap odd hole in brk segment");
114 anyfail();
115 }
116 hole_addr += 2 * pagesize;
117 }
118
119 if (brk_max_addr != sbrk(0)) {
120 ERROR("do_test should leave the top of brk where it began");
121 anyfail();
122 }
123 do_test((caddr_t) brk_max_addr, pagesize);
124
125 /* Shrink brk */
126 if (sbrk(-NUM_SEGS * pagesize) == NEG1) {
127 ERROR("couldn't brk back over holes");
128 anyfail();
129 }
130 if ((brk_max_addr = sbrk(0)) == NEG1) {
131 ERROR("couldn't find top of break again");
132 anyfail();
133 }
134 /* sbrked over about half the holes */
135
136 hole_addr = hole_start + pagesize; /* munmap the other pages */
137 while (hole_addr + pagesize < brk_max_addr) {
138 if (munmap(hole_addr, pagesize) == -1) {
139 ERROR("failed to munmap even hole in brk segment");
140 anyfail();
141 }
142 hole_addr += 2 * pagesize;
143 }
144 /* munmaped the rest of the brk except a little at the beginning */
145
146 if (brk(brk_start) == -1) {
147 ERROR("failed to completely remove brk");
148 anyfail();
149 }
150 if (sbrk(pagesize) == NEG1 || sbrk(-pagesize) == NEG1) {
151 ERROR("failed to fiddle with brk at the end");
152 anyfail();
153 }
154 /* Ask for a ridiculously large mmap region at a high address */
155 if (mmap((caddr_t) (1UL << (POINTER_SIZE - 1)) - pagesize,
156 (size_t) ((1UL << (POINTER_SIZE - 1)) - pagesize),
157 PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_FIXED | MAP_SHARED,
158 0, 0)
159 != (caddr_t) - 1) {
160 ERROR("really large mmap didn't fail");
161 anyfail();
162 }
163 if (errno != ENOMEM && errno != EINVAL) {
164 ERROR("really large mmap didn't set errno = ENOMEM nor EINVAL");
165 anyfail();
166 }
167 (void)time(&t);
168 // (void)printf("%s: Finished %s", argv[0], ctime(&t));
169 ok_exit();
170 tst_exit();
171 }
172
173 /*
174 * do_test assumes that brk_max is a multiple of pagesize
175 */
176
177 static
do_test(caddr_t brk_max,long pagesize)178 void do_test(caddr_t brk_max, long pagesize)
179 {
180 if (mmap((caddr_t) ((long)brk_max - 3 * pagesize), (2 * pagesize),
181 PROT_READ | PROT_WRITE,
182 MAP_ANONYMOUS | MAP_FIXED | MAP_PRIVATE, 0, 0)
183 == (caddr_t) - 1) {
184 ERROR("mmap failed");
185 anyfail();
186 }
187 /* extend mmap */
188 if (mmap((caddr_t) ((long)brk_max - 2 * pagesize), (2 * pagesize),
189 PROT_READ | PROT_WRITE,
190 MAP_ANONYMOUS | MAP_FIXED | MAP_PRIVATE, 0, 0)
191 == (caddr_t) - 1) {
192 ERROR("mmap failed");
193 anyfail();
194 }
195 if (sbrk(pagesize) == NEG1) {
196 ERROR("sbrk failed to grow over mmaped region");
197 anyfail();
198 }
199 if (sbrk(-pagesize) == NEG1) {
200 ERROR("sbrk failed to shrink back to mmaped region");
201 anyfail();
202 }
203 if (sbrk(-pagesize) == NEG1) {
204 ERROR("sbrk failed to shrink over mmaped region more");
205 anyfail();
206 }
207 if (sbrk(-pagesize) == NEG1) {
208 ERROR("sbrk failed to shrink some more");
209 anyfail();
210 }
211 if (sbrk(2 * pagesize) == NEG1) {
212 ERROR("sbrk failed to change brk segment to original size");
213 anyfail();
214 }
215 }
216
217 /***** LTP Port *****/
ok_exit(void)218 void ok_exit(void)
219 {
220 tst_resm(TPASS, "Test passed");
221 tst_exit();
222 }
223
anyfail(void)224 int anyfail(void)
225 {
226 tst_brkm(TFAIL, NULL, "Test failed");
227 }
228
229 /***** ** ** *****/
230