fitzhardinge | d5605a0 | 2003-12-19 21:56:04 +0000 | [diff] [blame] | 1 | #include <stdio.h> |
njn | 83b62cb | 2009-04-15 03:12:43 +0000 | [diff] [blame] | 2 | #include "tests/sys_mman.h" |
fitzhardinge | d5605a0 | 2003-12-19 21:56:04 +0000 | [diff] [blame] | 3 | #include <stdlib.h> |
| 4 | #include <unistd.h> |
| 5 | |
| 6 | static unsigned int pagesize; |
| 7 | |
| 8 | #define PAGES 1024u |
| 9 | #define LEN (PAGES*pagesize) |
| 10 | |
| 11 | static void *domap(void) |
| 12 | { |
njn | 83b62cb | 2009-04-15 03:12:43 +0000 | [diff] [blame] | 13 | void *ret = mmap(0, LEN, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
fitzhardinge | d5605a0 | 2003-12-19 21:56:04 +0000 | [diff] [blame] | 14 | |
| 15 | if (ret == (void *)-1) { |
| 16 | perror("mmap"); |
| 17 | exit(1); |
| 18 | } |
| 19 | |
| 20 | return ret; |
| 21 | } |
| 22 | |
| 23 | /* unmap in pieces to exercise munmap more */ |
| 24 | static void nibblemap(void *p) |
| 25 | { |
| 26 | int off; |
| 27 | int i; |
| 28 | |
fitzhardinge | e3632c6 | 2003-12-22 10:58:06 +0000 | [diff] [blame] | 29 | off = (random() % LEN) & ~(pagesize-1); |
fitzhardinge | d5605a0 | 2003-12-19 21:56:04 +0000 | [diff] [blame] | 30 | |
| 31 | for(i = 0; i < PAGES; i++) { |
fitzhardinge | e3632c6 | 2003-12-22 10:58:06 +0000 | [diff] [blame] | 32 | /* printf("unmapping off=%d\n", off/pagesize); */ |
fitzhardinge | d5605a0 | 2003-12-19 21:56:04 +0000 | [diff] [blame] | 33 | munmap((char *)p + off, pagesize); |
| 34 | off += 619*pagesize; |
| 35 | off %= LEN; |
| 36 | } |
| 37 | } |
| 38 | |
fitzhardinge | e3632c6 | 2003-12-22 10:58:06 +0000 | [diff] [blame] | 39 | static void prmaps() |
| 40 | { |
| 41 | char buf[100]; |
| 42 | sprintf(buf, "/bin/cat /proc/%d/maps", getpid()); |
| 43 | system(buf); |
| 44 | exit(1); |
| 45 | } |
| 46 | |
fitzhardinge | d5605a0 | 2003-12-19 21:56:04 +0000 | [diff] [blame] | 47 | int main() |
| 48 | { |
| 49 | int i; |
| 50 | void *expect1, *expect2; |
| 51 | |
| 52 | pagesize = getpagesize(); |
| 53 | |
| 54 | expect1 = domap(); |
| 55 | expect2 = domap(); |
| 56 | munmap(expect1, LEN); |
| 57 | munmap(expect2, LEN); |
fitzhardinge | e3632c6 | 2003-12-22 10:58:06 +0000 | [diff] [blame] | 58 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 59 | for(i = 0; i < 5; i++) { |
fitzhardinge | d5605a0 | 2003-12-19 21:56:04 +0000 | [diff] [blame] | 60 | void *m1, *m2; |
| 61 | |
| 62 | m1 = domap(); |
| 63 | if (m1 != expect1) { |
fitzhardinge | e3632c6 | 2003-12-22 10:58:06 +0000 | [diff] [blame] | 64 | printf("FAIL i=%d: m1=%p expect1=%p\n", |
| 65 | i, m1, expect1); |
| 66 | prmaps(); |
fitzhardinge | d5605a0 | 2003-12-19 21:56:04 +0000 | [diff] [blame] | 67 | return 1; |
| 68 | } |
| 69 | m2 = domap(); |
| 70 | if (m2 != expect2) { |
fitzhardinge | e3632c6 | 2003-12-22 10:58:06 +0000 | [diff] [blame] | 71 | printf("FAIL i=%d: m2=%p expect2=%p\n", |
| 72 | i, m2, expect2); |
| 73 | prmaps(); |
fitzhardinge | d5605a0 | 2003-12-19 21:56:04 +0000 | [diff] [blame] | 74 | return 1; |
| 75 | } |
| 76 | nibblemap(m2); |
| 77 | munmap(m1, LEN); |
| 78 | } |
| 79 | |
| 80 | printf("PASS\n"); |
| 81 | return 0; |
| 82 | } |