xref: /netbsd-src/external/gpl3/gdb/dist/sim/testsuite/cris/c/mmap5.c (revision 4b169a6ba595ae283ca507b26b15fdff40495b1c)
1*4b169a6bSchristos /*
2*4b169a6bSchristos #progos: linux
3*4b169a6bSchristos */
4*4b169a6bSchristos 
5*4b169a6bSchristos #define _GNU_SOURCE
6*4b169a6bSchristos #include <string.h>
7*4b169a6bSchristos #include <stdlib.h>
8*4b169a6bSchristos #include <stdio.h>
9*4b169a6bSchristos #include <sys/types.h>
10*4b169a6bSchristos #include <sys/stat.h>
11*4b169a6bSchristos #include <fcntl.h>
12*4b169a6bSchristos #include <unistd.h>
13*4b169a6bSchristos #include <sys/mman.h>
14*4b169a6bSchristos 
main(int argc,char * argv[])15*4b169a6bSchristos int main (int argc, char *argv[])
16*4b169a6bSchristos {
17*4b169a6bSchristos   int fd = open (argv[0], O_RDONLY);
18*4b169a6bSchristos   struct stat sb;
19*4b169a6bSchristos   int size;
20*4b169a6bSchristos   void *a;
21*4b169a6bSchristos   void *b;
22*4b169a6bSchristos   const char *str = "a string you'll only find in the program";
23*4b169a6bSchristos 
24*4b169a6bSchristos   if (fd == -1)
25*4b169a6bSchristos     {
26*4b169a6bSchristos       perror ("open");
27*4b169a6bSchristos       abort ();
28*4b169a6bSchristos     }
29*4b169a6bSchristos 
30*4b169a6bSchristos   if (fstat (fd, &sb) < 0)
31*4b169a6bSchristos     {
32*4b169a6bSchristos       perror ("fstat");
33*4b169a6bSchristos       abort ();
34*4b169a6bSchristos     }
35*4b169a6bSchristos 
36*4b169a6bSchristos   size = 8192;
37*4b169a6bSchristos #ifdef MMAP_SIZE1
38*4b169a6bSchristos   size = MMAP_SIZE1;
39*4b169a6bSchristos #endif
40*4b169a6bSchristos 
41*4b169a6bSchristos #ifndef MMAP_PROT1
42*4b169a6bSchristos #define MMAP_PROT1 PROT_READ | PROT_WRITE | PROT_EXEC
43*4b169a6bSchristos #endif
44*4b169a6bSchristos 
45*4b169a6bSchristos #ifndef MMAP_FLAGS1
46*4b169a6bSchristos #define MMAP_FLAGS1 MAP_PRIVATE | MAP_ANONYMOUS
47*4b169a6bSchristos #endif
48*4b169a6bSchristos 
49*4b169a6bSchristos   /* Get a page, any page.  */
50*4b169a6bSchristos   b = mmap (NULL, size, MMAP_PROT1, MMAP_FLAGS1, -1, 0);
51*4b169a6bSchristos   if (b == MAP_FAILED)
52*4b169a6bSchristos     abort ();
53*4b169a6bSchristos 
54*4b169a6bSchristos   /* Remember it, unmap it.  */
55*4b169a6bSchristos #ifndef NO_MUNMAP
56*4b169a6bSchristos   if (munmap (b, size) != 0)
57*4b169a6bSchristos     abort ();
58*4b169a6bSchristos #endif
59*4b169a6bSchristos 
60*4b169a6bSchristos #ifdef MMAP_ADDR2
61*4b169a6bSchristos   b = MMAP_ADDR2;
62*4b169a6bSchristos #endif
63*4b169a6bSchristos 
64*4b169a6bSchristos #ifndef MMAP_PROT2
65*4b169a6bSchristos #define MMAP_PROT2 PROT_READ | PROT_EXEC
66*4b169a6bSchristos #endif
67*4b169a6bSchristos 
68*4b169a6bSchristos #ifndef MMAP_FLAGS2
69*4b169a6bSchristos #define MMAP_FLAGS2 MAP_DENYWRITE | MAP_FIXED | MAP_PRIVATE
70*4b169a6bSchristos #endif
71*4b169a6bSchristos 
72*4b169a6bSchristos   size = sb.st_size;
73*4b169a6bSchristos #ifdef MMAP_SIZE2
74*4b169a6bSchristos   size = MMAP_SIZE2;
75*4b169a6bSchristos #endif
76*4b169a6bSchristos 
77*4b169a6bSchristos #define MMAP_TEST_BAD_ORIG \
78*4b169a6bSchristos  (a == MAP_FAILED || memmem (a, size, str, strlen (str) + 1) == NULL)
79*4b169a6bSchristos #ifndef MMAP_TEST_BAD
80*4b169a6bSchristos #define MMAP_TEST_BAD MMAP_TEST_BAD_ORIG
81*4b169a6bSchristos #endif
82*4b169a6bSchristos 
83*4b169a6bSchristos   /* Try mapping the now non-mapped page fixed.  */
84*4b169a6bSchristos   a = mmap (b, size, MMAP_PROT2, MMAP_FLAGS2, fd, 0);
85*4b169a6bSchristos 
86*4b169a6bSchristos   if (MMAP_TEST_BAD)
87*4b169a6bSchristos     abort ();
88*4b169a6bSchristos 
89*4b169a6bSchristos   printf ("pass\n");
90*4b169a6bSchristos   exit (0);
91*4b169a6bSchristos }
92