1 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <fcntl.h> 5 #include <unistd.h> 6 #include <errno.h> 7 #include <sys/mman.h> 8 #include <sys/wait.h> 9 10 int max_error = 2; 11 #include "common.h" 12 13 14 int subtest = 0; 15 16 int 17 main(int argc, char *argv[]) 18 { 19 #define CHUNKSIZE 8192 20 #define CHUNKS1 3 21 #define CHUNKS2 2 22 #define CHUNKS (CHUNKS1+CHUNKS2) 23 #define LARGESIZE 262144 24 int i, fd; 25 char *v[CHUNKS]; 26 #define STARTV 0x90000000 27 char *vaddr = (char *) STARTV; 28 ssize_t l; 29 pid_t f; 30 31 start(44); 32 33 for(i = 0; i < CHUNKS; i++) { 34 v[i] = mmap(vaddr, CHUNKSIZE, PROT_READ|PROT_WRITE, 0, 35 -1, 0); 36 if(v[i] == MAP_FAILED) { 37 perror("mmap"); 38 fprintf(stderr, "mmap failed\n"); 39 quit(); 40 } 41 if(v[i] != vaddr) { 42 fprintf(stderr, 43 "mmap said 0x%p but i wanted 0x%p\n", 44 v[i], vaddr); 45 quit(); 46 } 47 vaddr += CHUNKSIZE; 48 } 49 50 #define DEV_ZERO "/dev/zero" 51 if((fd=open(DEV_ZERO, O_RDONLY)) < 0) { 52 perror("open"); 53 fprintf(stderr, "open failed for %s\n", DEV_ZERO); 54 quit(); 55 } 56 57 #define TOTAL1 (CHUNKS1*CHUNKSIZE) 58 /* Make single read cross region boundary. */ 59 if((l=read(fd, v[0], TOTAL1)) != TOTAL1) { 60 fprintf(stderr, "read %d but expected %d\n", l, TOTAL1); 61 quit(); 62 } 63 64 /* Force single copy to cross region boundary. */ 65 { 66 char *t; 67 t = v[CHUNKS1]+CHUNKSIZE-2; 68 if((l=read(fd, t, CHUNKSIZE)) != CHUNKSIZE) { 69 fprintf(stderr, "read %d but expected %d\n", l, CHUNKSIZE); 70 quit(); 71 } 72 } 73 74 /* Now start a child to test bogus memory access */ 75 if((f = fork()) == -1) { 76 perror("fork"); 77 quit(); 78 } 79 80 if(f > 0) { 81 int st; 82 /* Parent waits. */ 83 if(waitpid(f, &st, 0) < 0) { 84 perror("waitpid"); 85 quit(); 86 } 87 if(!WIFEXITED(st)) { 88 fprintf(stderr, "child not signaled\n"); 89 quit(); 90 } 91 if(WEXITSTATUS(st) != 0) { 92 fprintf(stderr, "child exited with nonzero status\n"); 93 quit(); 94 } 95 } else { 96 /* Child performs bogus read */ 97 int res; 98 char *buf = v[CHUNKS-1]; 99 errno = 0; 100 res = read(fd, buf, LARGESIZE); 101 if(res >= 0) { 102 fprintf(stderr, "res %d\n", res); 103 quit(); 104 } 105 if(errno != EFAULT) { 106 fprintf(stderr, "errno %d\n", errno); 107 quit(); 108 } 109 return(0); 110 } 111 112 quit(); 113 return(-1); 114 } 115