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