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
|
/* Determine the virtual memory area of a given address.
Copyright (C) 2002, 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. */
#include "stackvma.h"
#include <unistd.h> /* open, close */
#include <fcntl.h> /* open */
#include <sys/types.h>
#include <sys/mman.h> /* mmap, munmap */
#include <sys/procfs.h> /* PIOC*, prmap_t */
#include "stackvma-simple.c"
#if HAVE_MINCORE
# define sigsegv_get_vma mincore_get_vma
# define STATIC static
# include "stackvma-mincore.c"
# undef sigsegv_get_vma
#else
/* Cache for getpagesize(). */
static unsigned long pagesize;
/* Initialize pagesize. */
static void
init_pagesize (void)
{
pagesize = getpagesize ();
}
#endif
int
sigsegv_get_vma (unsigned long address, struct vma_struct *vma)
{
char fnamebuf[6+10+1];
char *fname;
int fd;
int nmaps;
size_t memneed;
#if HAVE_MMAP_ANON
# define zero_fd -1
# define map_flags MAP_ANON
#elif HAVE_MMAP_ANONYMOUS
# define zero_fd -1
# define map_flags MAP_ANONYMOUS
#else
int zero_fd;
# define map_flags 0
#endif
void *auxmap;
unsigned long auxmap_start;
unsigned long auxmap_end;
prmap_t* maps;
prmap_t* mp;
unsigned long start, end;
#if STACK_DIRECTION < 0
unsigned long prev;
#endif
if (pagesize == 0)
init_pagesize ();
/* Construct fname = sprintf (fnamebuf+i, "/proc/%u", getpid ()). */
fname = fnamebuf + sizeof (fnamebuf) - 1;
*fname = '\0';
{
unsigned int value = getpid ();
do
*--fname = (value % 10) + '0';
while ((value = value / 10) > 0);
}
fname -= 6;
memcpy (fname, "/proc/", 6);
fd = open (fname, O_RDONLY);
if (fd < 0)
goto failed;
if (ioctl (fd, PIOCNMAP, &nmaps) < 0)
goto fail2;
memneed = (nmaps + 10) * sizeof (prmap_t);
/* Allocate memneed bytes of memory.
We cannot use alloca here, because we are low on stack space.
We also cannot use malloc here, because a malloc() call may have been
interrupted.
So use mmap(), and ignore the resulting VMA. */
memneed = ((memneed - 1) / pagesize + 1) * pagesize;
#if !(HAVE_MMAP_ANON || HAVE_MMAP_ANONYMOUS)
zero_fd = open ("/dev/zero", O_RDONLY, 0644);
if (zero_fd < 0)
goto fail2;
#endif
auxmap = (void *) mmap ((void *) 0, memneed, PROT_READ | PROT_WRITE, map_flags | MAP_PRIVATE, zero_fd, 0);
#if !(HAVE_MMAP_ANON || HAVE_MMAP_ANONYMOUS)
close (zero_fd);
#endif
if (auxmap == (void *) -1)
goto fail2;
auxmap_start = (unsigned long) auxmap;
auxmap_end = auxmap_start + memneed;
maps = (prmap_t *) auxmap;
if (ioctl (fd, PIOCMAP, maps) < 0)
goto fail1;
#if STACK_DIRECTION < 0
prev = 0;
#endif
for (mp = maps;;)
{
start = (unsigned long) mp->pr_vaddr;
end = start + mp->pr_size;
if (start == 0 && end == 0)
break;
mp++;
if (start <= auxmap_start && auxmap_end - 1 <= end - 1)
{
/* Consider [start,end-1] \ [auxmap_start,auxmap_end-1]
= [start,auxmap_start-1] u [auxmap_end,end-1]. */
if (start != auxmap_start)
{
if (address >= start && address <= auxmap_start - 1)
{
end = auxmap_start;
goto found;
}
#if STACK_DIRECTION < 0
prev = auxmap_start;
#endif
}
if (end != auxmap_end)
{
if (address >= auxmap_end && address <= end - 1)
{
start = auxmap_end;
goto found;
}
#if STACK_DIRECTION < 0
prev = end;
#endif
}
}
else
{
if (address >= start && address <= end - 1)
goto found;
#if STACK_DIRECTION < 0
prev = end;
#endif
}
}
fail1:
munmap (auxmap, memneed);
fail2:
close (fd);
failed:
#if HAVE_MINCORE
return mincore_get_vma (address, vma);
#else
return -1;
#endif
found:
vma->start = start;
vma->end = end;
#if STACK_DIRECTION < 0
vma->prev_end = prev;
#else
vma->next_start = (unsigned long) mp->pr_vaddr;
#endif
munmap (auxmap, memneed);
close (fd);
vma->is_near_this = simple_is_near_this;
return 0;
}
|