vm: fix region reporting bug
[minix.git] / test / test44.c
blob1f6dfa210ef28008b6b2d3fc129102683f7c185f
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>
10 #define MAX_ERROR 2
11 #include "common.c"
13 int subtest = 0;
15 int
16 main(int argc, char *argv[])
18 #define CHUNKSIZE 8192
19 #define CHUNKS1 3
20 #define CHUNKS2 2
21 #define CHUNKS (CHUNKS1+CHUNKS2)
22 #define LARGESIZE 262144
23 int i, fd;
24 char *v[CHUNKS];
25 #define STARTV 0x90000000
26 char *vaddr = (char *) STARTV;
27 ssize_t l;
28 pid_t f;
30 start(44);
32 for(i = 0; i < CHUNKS; i++) {
33 v[i] = minix_mmap(vaddr, CHUNKSIZE, PROT_READ|PROT_WRITE, 0,
34 -1, 0);
35 if(v[i] == MAP_FAILED) {
36 perror("minix_mmap");
37 fprintf(stderr, "minix_mmap failed\n");
38 quit();
40 if(v[i] != vaddr) {
41 fprintf(stderr,
42 "minix_mmap said 0x%p but i wanted 0x%p\n",
43 v[i], vaddr);
44 quit();
46 vaddr += CHUNKSIZE;
49 #define DEV_ZERO "/dev/zero"
50 if((fd=open(DEV_ZERO, O_RDONLY)) < 0) {
51 perror("open");
52 fprintf(stderr, "open failed for %s\n", DEV_ZERO);
53 quit();
56 #define TOTAL1 (CHUNKS1*CHUNKSIZE)
57 /* Make single read cross region boundary. */
58 if((l=read(fd, v[0], TOTAL1)) != TOTAL1) {
59 fprintf(stderr, "read %d but expected %d\n", l, TOTAL1);
60 quit();
63 /* Force single copy to cross region boundary. */
65 char *t;
66 t = v[CHUNKS1]+CHUNKSIZE-2;
67 if((l=read(fd, t, CHUNKSIZE)) != CHUNKSIZE) {
68 fprintf(stderr, "read %d but expected %d\n", l, CHUNKSIZE);
69 quit();
73 /* Now start a child to test bogus memory access */
74 if((f = fork()) == -1) {
75 perror("fork");
76 quit();
79 if(f > 0) {
80 int st;
81 /* Parent waits. */
82 if(waitpid(f, &st, 0) < 0) {
83 perror("waitpid");
84 quit();
86 if(!WIFEXITED(st)) {
87 fprintf(stderr, "child not signaled\n");
88 quit();
90 if(WEXITSTATUS(st) != 0) {
91 fprintf(stderr, "child exited with nonzero status\n");
92 quit();
94 } else {
95 /* Child performs bogus read */
96 int res;
97 char *buf = v[CHUNKS-1];
98 errno = 0;
99 res = read(fd, buf, LARGESIZE);
100 if(res >= 0) {
101 fprintf(stderr, "res %d\n", res);
102 quit();
104 if(errno != EFAULT) {
105 fprintf(stderr, "errno %d\n", errno);
106 quit();
108 return(0);
111 quit();
112 return(-1);