aboutsummaryrefslogtreecommitdiff
path: root/tests/tcg/test-mmap.c
blob: cdefadfa4c66a358dee45fac8a35493cfa50a7b4 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
 * Small test program to verify simulated mmap behaviour.
 *
 * When running qemu-linux-user with the -p flag, you may need to tell
 * this test program about the pagesize because getpagesize() will not reflect
 * the -p choice. Simply pass one argument being the pagesize.
 *
 * Copyright (c) 2007 AXIS Communications AB
 * Written by Edgar E. Iglesias.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>

#include <sys/mman.h>

#define D(x)

#define fail_unless(x)                                         \
do                                                             \
{                                                              \
  if (!(x)) {                                                  \
    fprintf (stderr, "FAILED at %s:%d\n", __FILE__, __LINE__); \
    exit (EXIT_FAILURE);                                       \
  }                                                            \
} while (0)

unsigned char *dummybuf;
static unsigned int pagesize;
static unsigned int pagemask;
int test_fd;
size_t test_fsize;

void check_aligned_anonymous_unfixed_mmaps(void)
{
	void *p1;
	void *p2;
	void *p3;
	void *p4;
	void *p5;
	uintptr_t p;
	int i;

	fprintf (stderr, "%s", __func__);
	for (i = 0; i < 0x1fff; i++)
	{
		size_t len;

		len = pagesize + (pagesize * i & 7);
		p1 = mmap(NULL, len, PROT_READ, 
			  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
		p2 = mmap(NULL, len, PROT_READ, 
			  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
		p3 = mmap(NULL, len, PROT_READ, 
			  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
		p4 = mmap(NULL, len, PROT_READ, 
			  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
		p5 = mmap(NULL, len, PROT_READ, 
			  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);

		/* Make sure we get pages aligned with the pagesize. The
		   target expects this.  */
		fail_unless (p1 != MAP_FAILED);
		fail_unless (p2 != MAP_FAILED);
		fail_unless (p3 != MAP_FAILED);
		fail_unless (p4 != MAP_FAILED);
		fail_unless (p5 != MAP_FAILED);
		p = (uintptr_t) p1;
		D(printf ("p=%x\n", p));
		fail_unless ((p & pagemask) == 0);
		p = (uintptr_t) p2;
		fail_unless ((p & pagemask) == 0);
		p = (uintptr_t) p3;
		fail_unless ((p & pagemask) == 0);
		p = (uintptr_t) p4;
		fail_unless ((p & pagemask) == 0);
		p = (uintptr_t) p5;
		fail_unless ((p & pagemask) == 0);

		/* Make sure we can read from the entire area.  */
		memcpy (dummybuf, p1, pagesize);
		memcpy (dummybuf, p2, pagesize);
		memcpy (dummybuf, p3, pagesize);
		memcpy (dummybuf, p4, pagesize);
		memcpy (dummybuf, p5, pagesize);

		munmap (p1, len);
		munmap (p2, len);
		munmap (p3, len);
		munmap (p4, len);
		munmap (p5, len);
	}
	fprintf (stderr, " passed\n");
}

void check_large_anonymous_unfixed_mmap(void)
{
	void *p1;
	uintptr_t p;
	size_t len;

	fprintf (stderr, "%s", __func__);

	len = 0x02000000;
	p1 = mmap(NULL, len, PROT_READ, 
		  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);

	/* Make sure we get pages aligned with the pagesize. The
	   target expects this.  */
	fail_unless (p1 != MAP_FAILED);
	p = (uintptr_t) p1;
	fail_unless ((p & pagemask) == 0);
	
	/* Make sure we can read from the entire area.  */
	memcpy (dummybuf, p1, pagesize);
	munmap (p1, len);
	fprintf (stderr, " passed\n");
}

void check_aligned_anonymous_unfixed_colliding_mmaps(void)
{
	char *p1;
	char *p2;
	char *p3;
	uintptr_t p;
	int i;

	fprintf (stderr, "%s", __func__);
	for (i = 0; i < 0x2fff; i++)
	{
		int nlen;
		p1 = mmap(NULL, pagesize, PROT_READ, 
			  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
		fail_unless (p1 != MAP_FAILED);
		p = (uintptr_t) p1;
		fail_unless ((p & pagemask) == 0);
		memcpy (dummybuf, p1, pagesize);

		p2 = mmap(NULL, pagesize, PROT_READ, 
			  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
		fail_unless (p2 != MAP_FAILED);
		p = (uintptr_t) p2;
		fail_unless ((p & pagemask) == 0);
		memcpy (dummybuf, p2, pagesize);


		munmap (p1, pagesize);
		nlen = pagesize * 8;
		p3 = mmap(NULL, nlen, PROT_READ, 
			  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
		fail_unless (p3 != MAP_FAILED);

		/* Check if the mmaped areas collide.  */
		if (p3 < p2 
		    && (p3 + nlen) > p2)
			fail_unless (0);

		memcpy (dummybuf, p3, pagesize);

		/* Make sure we get pages aligned with the pagesize. The
		   target expects this.  */
		p = (uintptr_t) p3;
		fail_unless ((p & pagemask) == 0);
		munmap (p2, pagesize);
		munmap (p3, nlen);
	}
	fprintf (stderr, " passed\n");
}

void check_aligned_anonymous_fixed_mmaps(void)
{
	char *addr;
	void *p1;
	uintptr_t p;
	int i;

	/* Find a suitable address to start with.  */
	addr = mmap(NULL, pagesize * 40, PROT_READ | PROT_WRITE, 
		    MAP_PRIVATE | MAP_ANONYMOUS,
		    -1, 0);
	fprintf (stderr, "%s addr=%p", __func__, addr);
	fail_unless (addr != MAP_FAILED);

	for (i = 0; i < 40; i++)
	{
		/* Create submaps within our unfixed map.  */
		p1 = mmap(addr, pagesize, PROT_READ, 
			  MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
			  -1, 0);
		/* Make sure we get pages aligned with the pagesize. 
		   The target expects this.  */
		p = (uintptr_t) p1;
		fail_unless (p1 == addr);
		fail_unless ((p & pagemask) == 0);		
		memcpy (dummybuf, p1, pagesize);
		munmap (p1, pagesize);
		addr += pagesize;
	}
	fprintf (stderr, " passed\n");
}

void check_aligned_anonymous_fixed_mmaps_collide_with_host(void)
{
	char *addr;
	void *p1;
	uintptr_t p;
	int i;

	/* Find a suitable address to start with.  Right were the x86 hosts
	 stack is.  */
	addr = ((void *)0x80000000);
	fprintf (stderr, "%s addr=%p", __func__, addr);
	fprintf (stderr, "FIXME: QEMU fails to track pages used by the host.");

	for (i = 0; i < 20; i++)
	{
		/* Create submaps within our unfixed map.  */
		p1 = mmap(addr, pagesize, PROT_READ | PROT_WRITE, 
			  MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED,
			  -1, 0);
		/* Make sure we get pages aligned with the pagesize. 
		   The target expects this.  */
		p = (uintptr_t) p1;
		fail_unless (p1 == addr);
		fail_unless ((p & pagemask) == 0);		
		memcpy (p1, dummybuf, pagesize);
		munmap (p1, pagesize);
		addr += pagesize;
	}
	fprintf (stderr, " passed\n");
}

void check_file_unfixed_mmaps(void)
{
	unsigned int *p1, *p2, *p3;
	uintptr_t p;
	int i;

	fprintf (stderr, "%s", __func__);
	for (i = 0; i < 0x10; i++)
	{
		size_t len;

		len = pagesize;
		p1 = mmap(NULL, len, PROT_READ, 
			  MAP_PRIVATE, 
			  test_fd, 0);
		p2 = mmap(NULL, len, PROT_READ, 
			  MAP_PRIVATE, 
			  test_fd, pagesize);
		p3 = mmap(NULL, len, PROT_READ, 
			  MAP_PRIVATE, 
			  test_fd, pagesize * 2);

		fail_unless (p1 != MAP_FAILED);
		fail_unless (p2 != MAP_FAILED);
		fail_unless (p3 != MAP_FAILED);

		/* Make sure we get pages aligned with the pagesize. The
		   target expects this.  */
		p = (uintptr_t) p1;
		fail_unless ((p & pagemask) == 0);
		p = (uintptr_t) p2;
		fail_unless ((p & pagemask) == 0);
		p = (uintptr_t) p3;
		fail_unless ((p & pagemask) == 0);

		/* Verify that the file maps was made correctly.  */
		D(printf ("p1=%d p2=%d p3=%d\n", *p1, *p2, *p3));
		fail_unless (*p1 == 0);
		fail_unless (*p2 == (pagesize / sizeof *p2));
		fail_unless (*p3 == ((pagesize * 2) / sizeof *p3));

		memcpy (dummybuf, p1, pagesize);
		memcpy (dummybuf, p2, pagesize);
		memcpy (dummybuf, p3, pagesize);
		munmap (p1, len);
		munmap (p2, len);
		munmap (p3, len);
	}
	fprintf (stderr, " passed\n");
}

void check_file_unfixed_eof_mmaps(void)
{
	char *cp;
	unsigned int *p1;
	uintptr_t p;
	int i;

	fprintf (stderr, "%s", __func__);
	for (i = 0; i < 0x10; i++)
	{
		p1 = mmap(NULL, pagesize, PROT_READ, 
			  MAP_PRIVATE, 
			  test_fd, 
			  (test_fsize - sizeof *p1) & ~pagemask);

		fail_unless (p1 != MAP_FAILED);

		/* Make sure we get pages aligned with the pagesize. The
		   target expects this.  */
		p = (uintptr_t) p1;
		fail_unless ((p & pagemask) == 0);
		/* Verify that the file maps was made correctly.  */
		fail_unless (p1[(test_fsize & pagemask) / sizeof *p1 - 1]
			     == ((test_fsize - sizeof *p1) / sizeof *p1));

		/* Verify that the end of page is accessible and zeroed.  */
		cp = (void *) p1;
		fail_unless (cp[pagesize - 4] == 0);
		munmap (p1, pagesize);
	}
	fprintf (stderr, " passed\n");
}

void check_file_fixed_eof_mmaps(void)
{
	char *addr;
	char *cp;
	unsigned int *p1;
	uintptr_t p;
	int i;

	/* Find a suitable address to start with.  */
	addr = mmap(NULL, pagesize * 44, PROT_READ, 
		    MAP_PRIVATE | MAP_ANONYMOUS,
		    -1, 0);

	fprintf (stderr, "%s addr=%p", __func__, (void *)addr);
	fail_unless (addr != MAP_FAILED);

	for (i = 0; i < 0x10; i++)
	{
		/* Create submaps within our unfixed map.  */
		p1 = mmap(addr, pagesize, PROT_READ, 
			  MAP_PRIVATE | MAP_FIXED, 
			  test_fd, 
			  (test_fsize - sizeof *p1) & ~pagemask);

		fail_unless (p1 != MAP_FAILED);

		/* Make sure we get pages aligned with the pagesize. The
		   target expects this.  */
		p = (uintptr_t) p1;
		fail_unless ((p & pagemask) == 0);

		/* Verify that the file maps was made correctly.  */
		fail_unless (p1[(test_fsize & pagemask) / sizeof *p1 - 1]
			     == ((test_fsize - sizeof *p1) / sizeof *p1));

		/* Verify that the end of page is accessible and zeroed.  */
		cp = (void *)p1;
		fail_unless (cp[pagesize - 4] == 0);
		munmap (p1, pagesize);
		addr += pagesize;
	}
	fprintf (stderr, " passed\n");
}

void check_file_fixed_mmaps(void)
{
	unsigned char *addr;
	unsigned int *p1, *p2, *p3, *p4;
	int i;

	/* Find a suitable address to start with.  */
	addr = mmap(NULL, pagesize * 40 * 4, PROT_READ, 
		    MAP_PRIVATE | MAP_ANONYMOUS,
		    -1, 0);
	fprintf (stderr, "%s addr=%p", __func__, (void *)addr);
	fail_unless (addr != MAP_FAILED);

	for (i = 0; i < 40; i++)
	{
		p1 = mmap(addr, pagesize, PROT_READ, 
			  MAP_PRIVATE | MAP_FIXED,
			  test_fd, 0);
		p2 = mmap(addr + pagesize, pagesize, PROT_READ, 
			  MAP_PRIVATE | MAP_FIXED,
			  test_fd, pagesize);
		p3 = mmap(addr + pagesize * 2, pagesize, PROT_READ, 
			  MAP_PRIVATE | MAP_FIXED,
			  test_fd, pagesize * 2);
		p4 = mmap(addr + pagesize * 3, pagesize, PROT_READ, 
			  MAP_PRIVATE | MAP_FIXED,
			  test_fd, pagesize * 3);

		/* Make sure we get pages aligned with the pagesize. 
		   The target expects this.  */
		fail_unless (p1 == (void *)addr);
		fail_unless (p2 == (void *)addr + pagesize);
		fail_unless (p3 == (void *)addr + pagesize * 2);
		fail_unless (p4 == (void *)addr + pagesize * 3);

		/* Verify that the file maps was made correctly.  */
		fail_unless (*p1 == 0);
		fail_unless (*p2 == (pagesize / sizeof *p2));
		fail_unless (*p3 == ((pagesize * 2) / sizeof *p3));
		fail_unless (*p4 == ((pagesize * 3) / sizeof *p4));

		memcpy (dummybuf, p1, pagesize);
		memcpy (dummybuf, p2, pagesize);
		memcpy (dummybuf, p3, pagesize);
		memcpy (dummybuf, p4, pagesize);

		munmap (p1, pagesize);
		munmap (p2, pagesize);
		munmap (p3, pagesize);
		munmap (p4, pagesize);
		addr += pagesize * 4;
	}
	fprintf (stderr, " passed\n");
}

void checked_write(int fd, const void *buf, size_t count)
{
    ssize_t rc = write(fd, buf, count);
    fail_unless(rc == count);
}

int main(int argc, char **argv)
{
	char tempname[] = "/tmp/.cmmapXXXXXX";
	unsigned int i;

	/* Trust the first argument, otherwise probe the system for our
	   pagesize.  */
	if (argc > 1)
		pagesize = strtoul(argv[1], NULL, 0);
	else
		pagesize = sysconf(_SC_PAGESIZE);

	/* Assume pagesize is a power of two.  */
	pagemask = pagesize - 1;
	dummybuf = malloc (pagesize);
	printf ("pagesize=%u pagemask=%x\n", pagesize, pagemask);

	test_fd = mkstemp(tempname);
	unlink(tempname);

	/* Fill the file with int's counting from zero and up.  */
    for (i = 0; i < (pagesize * 4) / sizeof i; i++) {
        checked_write(test_fd, &i, sizeof i);
    }

	/* Append a few extra writes to make the file end at non 
	   page boundary.  */
    checked_write(test_fd, &i, sizeof i); i++;
    checked_write(test_fd, &i, sizeof i); i++;
    checked_write(test_fd, &i, sizeof i); i++;

	test_fsize = lseek(test_fd, 0, SEEK_CUR);

	/* Run the tests.  */
	check_aligned_anonymous_unfixed_mmaps();
	check_aligned_anonymous_unfixed_colliding_mmaps();
	check_aligned_anonymous_fixed_mmaps();
	check_file_unfixed_mmaps();
	check_file_fixed_mmaps();
	check_file_fixed_eof_mmaps();
	check_file_unfixed_eof_mmaps();

	/* Fails at the moment.  */
	/* check_aligned_anonymous_fixed_mmaps_collide_with_host(); */

	return EXIT_SUCCESS;
}