annotate libbacktrace/mmap.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* mmap.c -- Memory allocation with mmap.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 2012-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3 Written by Ian Lance Taylor, Google.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 Redistribution and use in source and binary forms, with or without
kono
parents:
diff changeset
6 modification, are permitted provided that the following conditions are
kono
parents:
diff changeset
7 met:
kono
parents:
diff changeset
8
kono
parents:
diff changeset
9 (1) Redistributions of source code must retain the above copyright
kono
parents:
diff changeset
10 notice, this list of conditions and the following disclaimer.
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 (2) Redistributions in binary form must reproduce the above copyright
kono
parents:
diff changeset
13 notice, this list of conditions and the following disclaimer in
kono
parents:
diff changeset
14 the documentation and/or other materials provided with the
kono
parents:
diff changeset
15 distribution.
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 (3) The name of the author may not be used to
kono
parents:
diff changeset
18 endorse or promote products derived from this software without
kono
parents:
diff changeset
19 specific prior written permission.
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
kono
parents:
diff changeset
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
kono
parents:
diff changeset
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
kono
parents:
diff changeset
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
kono
parents:
diff changeset
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
kono
parents:
diff changeset
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
kono
parents:
diff changeset
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
kono
parents:
diff changeset
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
kono
parents:
diff changeset
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
kono
parents:
diff changeset
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
kono
parents:
diff changeset
31 POSSIBILITY OF SUCH DAMAGE. */
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 #include "config.h"
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 #include <errno.h>
kono
parents:
diff changeset
36 #include <string.h>
kono
parents:
diff changeset
37 #include <stdlib.h>
kono
parents:
diff changeset
38 #include <unistd.h>
kono
parents:
diff changeset
39 #include <sys/types.h>
kono
parents:
diff changeset
40 #include <sys/mman.h>
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 #include "backtrace.h"
kono
parents:
diff changeset
43 #include "internal.h"
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 /* Memory allocation on systems that provide anonymous mmap. This
kono
parents:
diff changeset
46 permits the backtrace functions to be invoked from a signal
kono
parents:
diff changeset
47 handler, assuming that mmap is async-signal safe. */
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 #ifndef MAP_ANONYMOUS
kono
parents:
diff changeset
50 #define MAP_ANONYMOUS MAP_ANON
kono
parents:
diff changeset
51 #endif
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 #ifndef MAP_FAILED
kono
parents:
diff changeset
54 #define MAP_FAILED ((void *)-1)
kono
parents:
diff changeset
55 #endif
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 /* A list of free memory blocks. */
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 struct backtrace_freelist_struct
kono
parents:
diff changeset
60 {
kono
parents:
diff changeset
61 /* Next on list. */
kono
parents:
diff changeset
62 struct backtrace_freelist_struct *next;
kono
parents:
diff changeset
63 /* Size of this block, including this structure. */
kono
parents:
diff changeset
64 size_t size;
kono
parents:
diff changeset
65 };
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 /* Free memory allocated by backtrace_alloc. */
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 static void
kono
parents:
diff changeset
70 backtrace_free_locked (struct backtrace_state *state, void *addr, size_t size)
kono
parents:
diff changeset
71 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
72 /* Just leak small blocks. We don't have to be perfect. Don't put
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
73 more than 16 entries on the free list, to avoid wasting time
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
74 searching when allocating a block. If we have more than 16
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
75 entries, leak the smallest entry. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
76
111
kono
parents:
diff changeset
77 if (size >= sizeof (struct backtrace_freelist_struct))
kono
parents:
diff changeset
78 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
79 size_t c;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
80 struct backtrace_freelist_struct **ppsmall;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
81 struct backtrace_freelist_struct **pp;
111
kono
parents:
diff changeset
82 struct backtrace_freelist_struct *p;
kono
parents:
diff changeset
83
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
84 c = 0;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
85 ppsmall = NULL;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
86 for (pp = &state->freelist; *pp != NULL; pp = &(*pp)->next)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
87 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
88 if (ppsmall == NULL || (*pp)->size < (*ppsmall)->size)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
89 ppsmall = pp;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
90 ++c;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
91 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
92 if (c >= 16)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
93 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
94 if (size <= (*ppsmall)->size)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
95 return;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
96 *ppsmall = (*ppsmall)->next;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
97 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
98
111
kono
parents:
diff changeset
99 p = (struct backtrace_freelist_struct *) addr;
kono
parents:
diff changeset
100 p->next = state->freelist;
kono
parents:
diff changeset
101 p->size = size;
kono
parents:
diff changeset
102 state->freelist = p;
kono
parents:
diff changeset
103 }
kono
parents:
diff changeset
104 }
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 /* Allocate memory like malloc. If ERROR_CALLBACK is NULL, don't
kono
parents:
diff changeset
107 report an error. */
kono
parents:
diff changeset
108
kono
parents:
diff changeset
109 void *
kono
parents:
diff changeset
110 backtrace_alloc (struct backtrace_state *state,
kono
parents:
diff changeset
111 size_t size, backtrace_error_callback error_callback,
kono
parents:
diff changeset
112 void *data)
kono
parents:
diff changeset
113 {
kono
parents:
diff changeset
114 void *ret;
kono
parents:
diff changeset
115 int locked;
kono
parents:
diff changeset
116 struct backtrace_freelist_struct **pp;
kono
parents:
diff changeset
117 size_t pagesize;
kono
parents:
diff changeset
118 size_t asksize;
kono
parents:
diff changeset
119 void *page;
kono
parents:
diff changeset
120
kono
parents:
diff changeset
121 ret = NULL;
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 /* If we can acquire the lock, then see if there is space on the
kono
parents:
diff changeset
124 free list. If we can't acquire the lock, drop straight into
kono
parents:
diff changeset
125 using mmap. __sync_lock_test_and_set returns the old state of
kono
parents:
diff changeset
126 the lock, so we have acquired it if it returns 0. */
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 if (!state->threaded)
kono
parents:
diff changeset
129 locked = 1;
kono
parents:
diff changeset
130 else
kono
parents:
diff changeset
131 locked = __sync_lock_test_and_set (&state->lock_alloc, 1) == 0;
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 if (locked)
kono
parents:
diff changeset
134 {
kono
parents:
diff changeset
135 for (pp = &state->freelist; *pp != NULL; pp = &(*pp)->next)
kono
parents:
diff changeset
136 {
kono
parents:
diff changeset
137 if ((*pp)->size >= size)
kono
parents:
diff changeset
138 {
kono
parents:
diff changeset
139 struct backtrace_freelist_struct *p;
kono
parents:
diff changeset
140
kono
parents:
diff changeset
141 p = *pp;
kono
parents:
diff changeset
142 *pp = p->next;
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 /* Round for alignment; we assume that no type we care about
kono
parents:
diff changeset
145 is more than 8 bytes. */
kono
parents:
diff changeset
146 size = (size + 7) & ~ (size_t) 7;
kono
parents:
diff changeset
147 if (size < p->size)
kono
parents:
diff changeset
148 backtrace_free_locked (state, (char *) p + size,
kono
parents:
diff changeset
149 p->size - size);
kono
parents:
diff changeset
150
kono
parents:
diff changeset
151 ret = (void *) p;
kono
parents:
diff changeset
152
kono
parents:
diff changeset
153 break;
kono
parents:
diff changeset
154 }
kono
parents:
diff changeset
155 }
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 if (state->threaded)
kono
parents:
diff changeset
158 __sync_lock_release (&state->lock_alloc);
kono
parents:
diff changeset
159 }
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 if (ret == NULL)
kono
parents:
diff changeset
162 {
kono
parents:
diff changeset
163 /* Allocate a new page. */
kono
parents:
diff changeset
164
kono
parents:
diff changeset
165 pagesize = getpagesize ();
kono
parents:
diff changeset
166 asksize = (size + pagesize - 1) & ~ (pagesize - 1);
kono
parents:
diff changeset
167 page = mmap (NULL, asksize, PROT_READ | PROT_WRITE,
kono
parents:
diff changeset
168 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
kono
parents:
diff changeset
169 if (page == MAP_FAILED)
kono
parents:
diff changeset
170 {
kono
parents:
diff changeset
171 if (error_callback)
kono
parents:
diff changeset
172 error_callback (data, "mmap", errno);
kono
parents:
diff changeset
173 }
kono
parents:
diff changeset
174 else
kono
parents:
diff changeset
175 {
kono
parents:
diff changeset
176 size = (size + 7) & ~ (size_t) 7;
kono
parents:
diff changeset
177 if (size < asksize)
kono
parents:
diff changeset
178 backtrace_free (state, (char *) page + size, asksize - size,
kono
parents:
diff changeset
179 error_callback, data);
kono
parents:
diff changeset
180
kono
parents:
diff changeset
181 ret = page;
kono
parents:
diff changeset
182 }
kono
parents:
diff changeset
183 }
kono
parents:
diff changeset
184
kono
parents:
diff changeset
185 return ret;
kono
parents:
diff changeset
186 }
kono
parents:
diff changeset
187
kono
parents:
diff changeset
188 /* Free memory allocated by backtrace_alloc. */
kono
parents:
diff changeset
189
kono
parents:
diff changeset
190 void
kono
parents:
diff changeset
191 backtrace_free (struct backtrace_state *state, void *addr, size_t size,
kono
parents:
diff changeset
192 backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
193 void *data ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
194 {
kono
parents:
diff changeset
195 int locked;
kono
parents:
diff changeset
196
kono
parents:
diff changeset
197 /* If we are freeing a large aligned block, just release it back to
kono
parents:
diff changeset
198 the system. This case arises when growing a vector for a large
kono
parents:
diff changeset
199 binary with lots of debug info. Calling munmap here may cause us
kono
parents:
diff changeset
200 to call mmap again if there is also a large shared library; we
kono
parents:
diff changeset
201 just live with that. */
kono
parents:
diff changeset
202 if (size >= 16 * 4096)
kono
parents:
diff changeset
203 {
kono
parents:
diff changeset
204 size_t pagesize;
kono
parents:
diff changeset
205
kono
parents:
diff changeset
206 pagesize = getpagesize ();
kono
parents:
diff changeset
207 if (((uintptr_t) addr & (pagesize - 1)) == 0
kono
parents:
diff changeset
208 && (size & (pagesize - 1)) == 0)
kono
parents:
diff changeset
209 {
kono
parents:
diff changeset
210 /* If munmap fails for some reason, just add the block to
kono
parents:
diff changeset
211 the freelist. */
kono
parents:
diff changeset
212 if (munmap (addr, size) == 0)
kono
parents:
diff changeset
213 return;
kono
parents:
diff changeset
214 }
kono
parents:
diff changeset
215 }
kono
parents:
diff changeset
216
kono
parents:
diff changeset
217 /* If we can acquire the lock, add the new space to the free list.
kono
parents:
diff changeset
218 If we can't acquire the lock, just leak the memory.
kono
parents:
diff changeset
219 __sync_lock_test_and_set returns the old state of the lock, so we
kono
parents:
diff changeset
220 have acquired it if it returns 0. */
kono
parents:
diff changeset
221
kono
parents:
diff changeset
222 if (!state->threaded)
kono
parents:
diff changeset
223 locked = 1;
kono
parents:
diff changeset
224 else
kono
parents:
diff changeset
225 locked = __sync_lock_test_and_set (&state->lock_alloc, 1) == 0;
kono
parents:
diff changeset
226
kono
parents:
diff changeset
227 if (locked)
kono
parents:
diff changeset
228 {
kono
parents:
diff changeset
229 backtrace_free_locked (state, addr, size);
kono
parents:
diff changeset
230
kono
parents:
diff changeset
231 if (state->threaded)
kono
parents:
diff changeset
232 __sync_lock_release (&state->lock_alloc);
kono
parents:
diff changeset
233 }
kono
parents:
diff changeset
234 }
kono
parents:
diff changeset
235
kono
parents:
diff changeset
236 /* Grow VEC by SIZE bytes. */
kono
parents:
diff changeset
237
kono
parents:
diff changeset
238 void *
kono
parents:
diff changeset
239 backtrace_vector_grow (struct backtrace_state *state,size_t size,
kono
parents:
diff changeset
240 backtrace_error_callback error_callback,
kono
parents:
diff changeset
241 void *data, struct backtrace_vector *vec)
kono
parents:
diff changeset
242 {
kono
parents:
diff changeset
243 void *ret;
kono
parents:
diff changeset
244
kono
parents:
diff changeset
245 if (size > vec->alc)
kono
parents:
diff changeset
246 {
kono
parents:
diff changeset
247 size_t pagesize;
kono
parents:
diff changeset
248 size_t alc;
kono
parents:
diff changeset
249 void *base;
kono
parents:
diff changeset
250
kono
parents:
diff changeset
251 pagesize = getpagesize ();
kono
parents:
diff changeset
252 alc = vec->size + size;
kono
parents:
diff changeset
253 if (vec->size == 0)
kono
parents:
diff changeset
254 alc = 16 * size;
kono
parents:
diff changeset
255 else if (alc < pagesize)
kono
parents:
diff changeset
256 {
kono
parents:
diff changeset
257 alc *= 2;
kono
parents:
diff changeset
258 if (alc > pagesize)
kono
parents:
diff changeset
259 alc = pagesize;
kono
parents:
diff changeset
260 }
kono
parents:
diff changeset
261 else
kono
parents:
diff changeset
262 {
kono
parents:
diff changeset
263 alc *= 2;
kono
parents:
diff changeset
264 alc = (alc + pagesize - 1) & ~ (pagesize - 1);
kono
parents:
diff changeset
265 }
kono
parents:
diff changeset
266 base = backtrace_alloc (state, alc, error_callback, data);
kono
parents:
diff changeset
267 if (base == NULL)
kono
parents:
diff changeset
268 return NULL;
kono
parents:
diff changeset
269 if (vec->base != NULL)
kono
parents:
diff changeset
270 {
kono
parents:
diff changeset
271 memcpy (base, vec->base, vec->size);
kono
parents:
diff changeset
272 backtrace_free (state, vec->base, vec->size + vec->alc,
kono
parents:
diff changeset
273 error_callback, data);
kono
parents:
diff changeset
274 }
kono
parents:
diff changeset
275 vec->base = base;
kono
parents:
diff changeset
276 vec->alc = alc - vec->size;
kono
parents:
diff changeset
277 }
kono
parents:
diff changeset
278
kono
parents:
diff changeset
279 ret = (char *) vec->base + vec->size;
kono
parents:
diff changeset
280 vec->size += size;
kono
parents:
diff changeset
281 vec->alc -= size;
kono
parents:
diff changeset
282 return ret;
kono
parents:
diff changeset
283 }
kono
parents:
diff changeset
284
kono
parents:
diff changeset
285 /* Finish the current allocation on VEC. */
kono
parents:
diff changeset
286
kono
parents:
diff changeset
287 void *
kono
parents:
diff changeset
288 backtrace_vector_finish (
kono
parents:
diff changeset
289 struct backtrace_state *state ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
290 struct backtrace_vector *vec,
kono
parents:
diff changeset
291 backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
292 void *data ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
293 {
kono
parents:
diff changeset
294 void *ret;
kono
parents:
diff changeset
295
kono
parents:
diff changeset
296 ret = vec->base;
kono
parents:
diff changeset
297 vec->base = (char *) vec->base + vec->size;
kono
parents:
diff changeset
298 vec->size = 0;
kono
parents:
diff changeset
299 return ret;
kono
parents:
diff changeset
300 }
kono
parents:
diff changeset
301
kono
parents:
diff changeset
302 /* Release any extra space allocated for VEC. */
kono
parents:
diff changeset
303
kono
parents:
diff changeset
304 int
kono
parents:
diff changeset
305 backtrace_vector_release (struct backtrace_state *state,
kono
parents:
diff changeset
306 struct backtrace_vector *vec,
kono
parents:
diff changeset
307 backtrace_error_callback error_callback,
kono
parents:
diff changeset
308 void *data)
kono
parents:
diff changeset
309 {
kono
parents:
diff changeset
310 size_t size;
kono
parents:
diff changeset
311 size_t alc;
kono
parents:
diff changeset
312 size_t aligned;
kono
parents:
diff changeset
313
kono
parents:
diff changeset
314 /* Make sure that the block that we free is aligned on an 8-byte
kono
parents:
diff changeset
315 boundary. */
kono
parents:
diff changeset
316 size = vec->size;
kono
parents:
diff changeset
317 alc = vec->alc;
kono
parents:
diff changeset
318 aligned = (size + 7) & ~ (size_t) 7;
kono
parents:
diff changeset
319 alc -= aligned - size;
kono
parents:
diff changeset
320
kono
parents:
diff changeset
321 backtrace_free (state, (char *) vec->base + aligned, alc,
kono
parents:
diff changeset
322 error_callback, data);
kono
parents:
diff changeset
323 vec->alc = 0;
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
324 if (vec->size == 0)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
325 vec->base = NULL;
111
kono
parents:
diff changeset
326 return 1;
kono
parents:
diff changeset
327 }