1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
/* Determine the virtual memory area of a given address.
Copyright (C) 2006, 2008 Bruno Haible <bruno@clisp.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
/* mincore() is a system call that allows to inquire the status of a
range of pages of virtual memory. In particular, it allows to inquire
whether a page is mapped at all.
As of 2006, mincore() is supported by: possible bits:
- Linux, since Linux 2.4 and glibc 2.2, 1
- Solaris, since Solaris 9, 1
- MacOS X, since MacOS X 10.3 (at least), 1
- FreeBSD, since FreeBSD 6.0, MINCORE_{INCORE,REFERENCED,MODIFIED}
- NetBSD, since NetBSD 3.0 (at least), 1
- OpenBSD, since OpenBSD 2.6 (at least), 1
However, while the API allows to easily determine the bounds of mapped
virtual memory, it does not make it easy the bounds of _unmapped_ virtual
memory ranges. We try to work around this, but it may still be slow. */
#include "stackvma.h"
#include <limits.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/mman.h>
/* Cache for getpagesize(). */
static unsigned long pagesize;
/* Initialize pagesize. */
static void
init_pagesize (void)
{
#if HAVE_GETPAGESIZE
pagesize = getpagesize ();
#elif HAVE_SYSCONF_PAGESIZE
pagesize = sysconf (_SC_PAGESIZE);
#else
pagesize = PAGESIZE;
#endif
}
/* Test whether the page starting at ADDR is among the address range.
ADDR must be a multiple of pagesize. */
static int
is_mapped (unsigned long addr)
{
char vec[1];
return mincore ((void *) addr, pagesize, vec) >= 0;
}
/* Assuming that the page starting at ADDR is among the address range,
return the start of its virtual memory range.
ADDR must be a multiple of pagesize. */
static unsigned long
mapped_range_start (unsigned long addr)
{
/* Use a moderately sized VEC here, small enough that it fits on the stack
(without requiring malloc). */
char vec[2048];
unsigned long stepsize = sizeof (vec);
for (;;)
{
unsigned long max_remaining;
if (addr == 0)
return addr;
max_remaining = addr / pagesize;
if (stepsize > max_remaining)
stepsize = max_remaining;
if (mincore ((void *) (addr - stepsize * pagesize),
stepsize * pagesize, vec) < 0)
/* Time to search in smaller steps. */
break;
/* The entire range exists. Continue searching in large steps. */
addr -= stepsize * pagesize;
}
for (;;)
{
unsigned long halfstepsize1;
unsigned long halfstepsize2;
if (stepsize == 1)
return addr;
/* Here we know that less than stepsize pages exist starting at addr. */
halfstepsize1 = (stepsize + 1) / 2;
halfstepsize2 = stepsize / 2;
/* halfstepsize1 + halfstepsize2 = stepsize. */
if (mincore ((void *) (addr - halfstepsize1 * pagesize),
halfstepsize1 * pagesize, vec) < 0)
stepsize = halfstepsize1;
else
{
addr -= halfstepsize1 * pagesize;
stepsize = halfstepsize2;
}
}
}
/* Assuming that the page starting at ADDR is among the address range,
return the end of its virtual memory range + 1.
ADDR must be a multiple of pagesize. */
static unsigned long
mapped_range_end (unsigned long addr)
{
/* Use a moderately sized VEC here, small enough that it fits on the stack
(without requiring malloc). */
char vec[2048];
unsigned long stepsize = sizeof (vec);
addr += pagesize;
for (;;)
{
unsigned long max_remaining;
if (addr == 0) /* wrapped around? */
return addr;
max_remaining = (- addr) / pagesize;
if (stepsize > max_remaining)
stepsize = max_remaining;
if (mincore ((void *) addr, stepsize * pagesize, vec) < 0)
/* Time to search in smaller steps. */
break;
/* The entire range exists. Continue searching in large steps. */
addr += stepsize * pagesize;
}
for (;;)
{
unsigned long halfstepsize1;
unsigned long halfstepsize2;
if (stepsize == 1)
return addr;
/* Here we know that less than stepsize pages exist starting at addr. */
halfstepsize1 = (stepsize + 1) / 2;
halfstepsize2 = stepsize / 2;
/* halfstepsize1 + halfstepsize2 = stepsize. */
if (mincore ((void *) addr, halfstepsize1 * pagesize, vec) < 0)
stepsize = halfstepsize1;
else
{
addr += halfstepsize1 * pagesize;
stepsize = halfstepsize2;
}
}
}
/* Determine whether an address range [ADDR1..ADDR2] is completely unmapped.
ADDR1 must be <= ADDR2. */
static int
is_unmapped (unsigned long addr1, unsigned long addr2)
{
unsigned long count;
unsigned long stepsize;
/* Round addr1 down. */
addr1 = (addr1 / pagesize) * pagesize;
/* Round addr2 up and turn it into an exclusive bound. */
addr2 = ((addr2 / pagesize) + 1) * pagesize;
/* This is slow: mincore() does not provide a way to determine the bounds
of the gaps directly. So we have to use mincore() on individual pages
over and over again. Only after we've verified that all pages are
unmapped, we know that the range is completely unmapped.
If we were to traverse the pages from bottom to top or from top to bottom,
it would be slow even in the average case. To speed up the search, we
exploit the fact that mapped memory ranges are larger than one page on
average, therefore we have good chances of hitting a mapped area if we
traverse only every second, or only fourth page, etc. This doesn't
decrease the worst-case runtime, only the average runtime. */
count = (addr2 - addr1) / pagesize;
/* We have to test is_mapped (addr1 + i * pagesize) for 0 <= i < count. */
for (stepsize = 1; stepsize < count; )
stepsize = 2 * stepsize;
for (;;)
{
unsigned long addr_stepsize;
unsigned long i;
unsigned long addr;
stepsize = stepsize / 2;
if (stepsize == 0)
break;
addr_stepsize = stepsize * pagesize;
for (i = stepsize, addr = addr1 + addr_stepsize;
i < count;
i += 2 * stepsize, addr += 2 * addr_stepsize)
/* Here addr = addr1 + i * pagesize. */
if (is_mapped (addr))
return 0;
}
return 1;
}
#if STACK_DIRECTION < 0
/* Info about the gap between this VMA and the previous one.
addr must be < vma->start. */
static int
mincore_is_near_this (unsigned long addr, struct vma_struct *vma)
{
/* vma->start - addr <= (vma->start - vma->prev_end) / 2
is mathematically equivalent to
vma->prev_end <= 2 * addr - vma->start
<==> is_unmapped (2 * addr - vma->start, vma->start - 1).
But be careful about overflow: if 2 * addr - vma->start is negative,
we consider a tiny "guard page" mapping [0, 0] to be present around
NULL; it intersects the range (2 * addr - vma->start, vma->start - 1),
therefore return false. */
unsigned long testaddr = addr - (vma->start - addr);
if (testaddr > addr) /* overflow? */
return 0;
/* Here testaddr <= addr < vma->start. */
return is_unmapped (testaddr, vma->start - 1);
}
#endif
#if STACK_DIRECTION > 0
/* Info about the gap between this VMA and the next one.
addr must be > vma->end - 1. */
static int
mincore_is_near_this (unsigned long addr, struct vma_struct *vma)
{
/* addr - vma->end < (vma->next_start - vma->end) / 2
is mathematically equivalent to
vma->next_start > 2 * addr - vma->end
<==> is_unmapped (vma->end, 2 * addr - vma->end).
But be careful about overflow: if 2 * addr - vma->end is > ~0UL,
we consider a tiny "guard page" mapping [0, 0] to be present around
NULL; it intersects the range (vma->end, 2 * addr - vma->end),
therefore return false. */
unsigned long testaddr = addr + (addr - vma->end);
if (testaddr < addr) /* overflow? */
return 0;
/* Here vma->end - 1 < addr <= testaddr. */
return is_unmapped (vma->end, testaddr);
}
#endif
#ifdef STATIC
STATIC
#endif
int
sigsegv_get_vma (unsigned long address, struct vma_struct *vma)
{
if (pagesize == 0)
init_pagesize ();
address = (address / pagesize) * pagesize;
vma->start = mapped_range_start (address);
vma->end = mapped_range_end (address);
vma->is_near_this = mincore_is_near_this;
return 0;
}
|