Commit | Line | Data |
---|---|---|
7da515fd KB |
1 | /* Copyright 1992-2020 Free Software Foundation, Inc. |
2 | ||
3 | This file is part of GDB. | |
4 | ||
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 3 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
17 | ||
18 | /* This test has two large memory areas buf_rw and buf_ro. | |
19 | ||
20 | buf_rw is written to by the program while buf_ro is initialized at | |
21 | compile / load time. Thus, when a core file is created, buf_rw's | |
22 | memory should reside in the core file, but buf_ro probably won't be. | |
23 | Instead, the contents of buf_ro are available from the executable. | |
24 | ||
25 | Now, for the wrinkle: We create a one page read-only mapping over | |
26 | both of these areas. This will create a one page "hole" of all | |
27 | zeros in each area. | |
28 | ||
29 | Will GDB be able to correctly read memory from each of the four | |
30 | (or six, if you count the regions on the other side of each hole) | |
31 | memory regions? */ | |
32 | ||
33 | #include <stdio.h> | |
34 | #include <sys/types.h> | |
35 | #include <fcntl.h> | |
36 | #include <sys/mman.h> | |
37 | #include <signal.h> | |
38 | #include <stdlib.h> | |
39 | #include <unistd.h> | |
40 | #include <string.h> | |
41 | #include <errno.h> | |
42 | ||
43 | /* These are globals so that we can find them easily when debugging | |
44 | the core file. */ | |
45 | long pagesize; | |
46 | unsigned long long addr; | |
47 | char *mbuf_ro; | |
48 | char *mbuf_rw; | |
49 | ||
50 | /* 24 KiB buffer. */ | |
51 | char buf_rw[24 * 1024]; | |
52 | ||
53 | /* 24 KiB worth of data. For this test case, we can't allocate a | |
54 | buffer and then fill it; we want GDB to have to read this data | |
55 | from the executable; it should NOT find it in the core file. */ | |
56 | ||
57 | #define C5_16 \ | |
58 | 0xc5, 0xc5, 0xc5, 0xc5, \ | |
59 | 0xc5, 0xc5, 0xc5, 0xc5, \ | |
60 | 0xc5, 0xc5, 0xc5, 0xc5, \ | |
61 | 0xc5, 0xc5, 0xc5, 0xc5 | |
62 | ||
63 | #define C5_256 \ | |
64 | C5_16, C5_16, C5_16, C5_16, \ | |
65 | C5_16, C5_16, C5_16, C5_16, \ | |
66 | C5_16, C5_16, C5_16, C5_16, \ | |
67 | C5_16, C5_16, C5_16, C5_16 | |
68 | ||
69 | #define C5_1k \ | |
70 | C5_256, C5_256, C5_256, C5_256 | |
71 | ||
72 | #define C5_24k \ | |
73 | C5_1k, C5_1k, C5_1k, C5_1k, \ | |
74 | C5_1k, C5_1k, C5_1k, C5_1k, \ | |
75 | C5_1k, C5_1k, C5_1k, C5_1k, \ | |
76 | C5_1k, C5_1k, C5_1k, C5_1k, \ | |
77 | C5_1k, C5_1k, C5_1k, C5_1k, \ | |
78 | C5_1k, C5_1k, C5_1k, C5_1k | |
79 | ||
80 | const char buf_ro[] = { C5_24k }; | |
81 | ||
82 | int | |
83 | main (int argc, char **argv) | |
84 | { | |
85 | int i, bitcount; | |
86 | ||
87 | #ifdef _SC_PAGESIZE | |
88 | pagesize = sysconf (_SC_PAGESIZE); | |
89 | #else | |
90 | pagesize = 8192; | |
91 | #endif | |
92 | ||
93 | /* Verify that pagesize is a power of 2. */ | |
94 | bitcount = 0; | |
95 | for (i = 0; i < 4 * sizeof (pagesize); i++) | |
96 | if (pagesize & (1 << i)) | |
97 | bitcount++; | |
98 | ||
99 | if (bitcount != 1) | |
100 | { | |
101 | fprintf (stderr, "pagesize is not a power of 2.\n"); | |
102 | exit (1); | |
103 | } | |
104 | ||
105 | /* Compute an address that should be within buf_ro. Complain if not. */ | |
106 | addr = ((unsigned long long) buf_ro + pagesize) & ~(pagesize - 1); | |
107 | ||
108 | if (addr <= (unsigned long long) buf_ro | |
109 | || addr >= (unsigned long long) buf_ro + sizeof (buf_ro)) | |
110 | { | |
111 | fprintf (stderr, "Unable to compute a suitable address within buf_ro.\n"); | |
112 | exit (1); | |
113 | } | |
114 | ||
115 | mbuf_ro = mmap ((void *) addr, pagesize, PROT_READ, | |
116 | MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0); | |
117 | ||
118 | if (mbuf_ro == MAP_FAILED) | |
119 | { | |
120 | fprintf (stderr, "mmap #1 failed: %s.\n", strerror (errno)); | |
121 | exit (1); | |
122 | } | |
123 | ||
124 | /* Write (and fill) the R/W region. */ | |
125 | for (i = 0; i < sizeof (buf_rw); i++) | |
126 | buf_rw[i] = 0x6b; | |
127 | ||
128 | /* Compute an mmap address within buf_rw. Complain if it's somewhere | |
129 | else. */ | |
130 | addr = ((unsigned long long) buf_rw + pagesize) & ~(pagesize - 1); | |
131 | ||
132 | if (addr <= (unsigned long long) buf_rw | |
133 | || addr >= (unsigned long long) buf_rw + sizeof (buf_rw)) | |
134 | { | |
135 | fprintf (stderr, "Unable to compute a suitable address within buf_rw.\n"); | |
136 | exit (1); | |
137 | } | |
138 | ||
139 | mbuf_rw = mmap ((void *) addr, pagesize, PROT_READ, | |
140 | MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0); | |
141 | ||
142 | if (mbuf_rw == MAP_FAILED) | |
143 | { | |
144 | fprintf (stderr, "mmap #2 failed: %s.\n", strerror (errno)); | |
145 | exit (1); | |
146 | } | |
147 | ||
148 | /* With correct ulimit, etc. this should cause a core dump. */ | |
149 | abort (); | |
150 | } |