annotate libbacktrace/mmap.c @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
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.
kono
parents:
diff changeset
2 Copyright (C) 2012-2017 Free Software Foundation, Inc.
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 {
kono
parents:
diff changeset
72 /* Just leak small blocks. We don't have to be perfect. */
kono
parents:
diff changeset
73 if (size >= sizeof (struct backtrace_freelist_struct))
kono
parents:
diff changeset
74 {
kono
parents:
diff changeset
75 struct backtrace_freelist_struct *p;
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 p = (struct backtrace_freelist_struct *) addr;
kono
parents:
diff changeset
78 p->next = state->freelist;
kono
parents:
diff changeset
79 p->size = size;
kono
parents:
diff changeset
80 state->freelist = p;
kono
parents:
diff changeset
81 }
kono
parents:
diff changeset
82 }
kono
parents:
diff changeset
83
kono
parents:
diff changeset
84 /* Allocate memory like malloc. If ERROR_CALLBACK is NULL, don't
kono
parents:
diff changeset
85 report an error. */
kono
parents:
diff changeset
86
kono
parents:
diff changeset
87 void *
kono
parents:
diff changeset
88 backtrace_alloc (struct backtrace_state *state,
kono
parents:
diff changeset
89 size_t size, backtrace_error_callback error_callback,
kono
parents:
diff changeset
90 void *data)
kono
parents:
diff changeset
91 {
kono
parents:
diff changeset
92 void *ret;
kono
parents:
diff changeset
93 int locked;
kono
parents:
diff changeset
94 struct backtrace_freelist_struct **pp;
kono
parents:
diff changeset
95 size_t pagesize;
kono
parents:
diff changeset
96 size_t asksize;
kono
parents:
diff changeset
97 void *page;
kono
parents:
diff changeset
98
kono
parents:
diff changeset
99 ret = NULL;
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 /* If we can acquire the lock, then see if there is space on the
kono
parents:
diff changeset
102 free list. If we can't acquire the lock, drop straight into
kono
parents:
diff changeset
103 using mmap. __sync_lock_test_and_set returns the old state of
kono
parents:
diff changeset
104 the lock, so we have acquired it if it returns 0. */
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 if (!state->threaded)
kono
parents:
diff changeset
107 locked = 1;
kono
parents:
diff changeset
108 else
kono
parents:
diff changeset
109 locked = __sync_lock_test_and_set (&state->lock_alloc, 1) == 0;
kono
parents:
diff changeset
110
kono
parents:
diff changeset
111 if (locked)
kono
parents:
diff changeset
112 {
kono
parents:
diff changeset
113 for (pp = &state->freelist; *pp != NULL; pp = &(*pp)->next)
kono
parents:
diff changeset
114 {
kono
parents:
diff changeset
115 if ((*pp)->size >= size)
kono
parents:
diff changeset
116 {
kono
parents:
diff changeset
117 struct backtrace_freelist_struct *p;
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 p = *pp;
kono
parents:
diff changeset
120 *pp = p->next;
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 /* Round for alignment; we assume that no type we care about
kono
parents:
diff changeset
123 is more than 8 bytes. */
kono
parents:
diff changeset
124 size = (size + 7) & ~ (size_t) 7;
kono
parents:
diff changeset
125 if (size < p->size)
kono
parents:
diff changeset
126 backtrace_free_locked (state, (char *) p + size,
kono
parents:
diff changeset
127 p->size - size);
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 ret = (void *) p;
kono
parents:
diff changeset
130
kono
parents:
diff changeset
131 break;
kono
parents:
diff changeset
132 }
kono
parents:
diff changeset
133 }
kono
parents:
diff changeset
134
kono
parents:
diff changeset
135 if (state->threaded)
kono
parents:
diff changeset
136 __sync_lock_release (&state->lock_alloc);
kono
parents:
diff changeset
137 }
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 if (ret == NULL)
kono
parents:
diff changeset
140 {
kono
parents:
diff changeset
141 /* Allocate a new page. */
kono
parents:
diff changeset
142
kono
parents:
diff changeset
143 pagesize = getpagesize ();
kono
parents:
diff changeset
144 asksize = (size + pagesize - 1) & ~ (pagesize - 1);
kono
parents:
diff changeset
145 page = mmap (NULL, asksize, PROT_READ | PROT_WRITE,
kono
parents:
diff changeset
146 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
kono
parents:
diff changeset
147 if (page == MAP_FAILED)
kono
parents:
diff changeset
148 {
kono
parents:
diff changeset
149 if (error_callback)
kono
parents:
diff changeset
150 error_callback (data, "mmap", errno);
kono
parents:
diff changeset
151 }
kono
parents:
diff changeset
152 else
kono
parents:
diff changeset
153 {
kono
parents:
diff changeset
154 size = (size + 7) & ~ (size_t) 7;
kono
parents:
diff changeset
155 if (size < asksize)
kono
parents:
diff changeset
156 backtrace_free (state, (char *) page + size, asksize - size,
kono
parents:
diff changeset
157 error_callback, data);
kono
parents:
diff changeset
158
kono
parents:
diff changeset
159 ret = page;
kono
parents:
diff changeset
160 }
kono
parents:
diff changeset
161 }
kono
parents:
diff changeset
162
kono
parents:
diff changeset
163 return ret;
kono
parents:
diff changeset
164 }
kono
parents:
diff changeset
165
kono
parents:
diff changeset
166 /* Free memory allocated by backtrace_alloc. */
kono
parents:
diff changeset
167
kono
parents:
diff changeset
168 void
kono
parents:
diff changeset
169 backtrace_free (struct backtrace_state *state, void *addr, size_t size,
kono
parents:
diff changeset
170 backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
171 void *data ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
172 {
kono
parents:
diff changeset
173 int locked;
kono
parents:
diff changeset
174
kono
parents:
diff changeset
175 /* If we are freeing a large aligned block, just release it back to
kono
parents:
diff changeset
176 the system. This case arises when growing a vector for a large
kono
parents:
diff changeset
177 binary with lots of debug info. Calling munmap here may cause us
kono
parents:
diff changeset
178 to call mmap again if there is also a large shared library; we
kono
parents:
diff changeset
179 just live with that. */
kono
parents:
diff changeset
180 if (size >= 16 * 4096)
kono
parents:
diff changeset
181 {
kono
parents:
diff changeset
182 size_t pagesize;
kono
parents:
diff changeset
183
kono
parents:
diff changeset
184 pagesize = getpagesize ();
kono
parents:
diff changeset
185 if (((uintptr_t) addr & (pagesize - 1)) == 0
kono
parents:
diff changeset
186 && (size & (pagesize - 1)) == 0)
kono
parents:
diff changeset
187 {
kono
parents:
diff changeset
188 /* If munmap fails for some reason, just add the block to
kono
parents:
diff changeset
189 the freelist. */
kono
parents:
diff changeset
190 if (munmap (addr, size) == 0)
kono
parents:
diff changeset
191 return;
kono
parents:
diff changeset
192 }
kono
parents:
diff changeset
193 }
kono
parents:
diff changeset
194
kono
parents:
diff changeset
195 /* If we can acquire the lock, add the new space to the free list.
kono
parents:
diff changeset
196 If we can't acquire the lock, just leak the memory.
kono
parents:
diff changeset
197 __sync_lock_test_and_set returns the old state of the lock, so we
kono
parents:
diff changeset
198 have acquired it if it returns 0. */
kono
parents:
diff changeset
199
kono
parents:
diff changeset
200 if (!state->threaded)
kono
parents:
diff changeset
201 locked = 1;
kono
parents:
diff changeset
202 else
kono
parents:
diff changeset
203 locked = __sync_lock_test_and_set (&state->lock_alloc, 1) == 0;
kono
parents:
diff changeset
204
kono
parents:
diff changeset
205 if (locked)
kono
parents:
diff changeset
206 {
kono
parents:
diff changeset
207 backtrace_free_locked (state, addr, size);
kono
parents:
diff changeset
208
kono
parents:
diff changeset
209 if (state->threaded)
kono
parents:
diff changeset
210 __sync_lock_release (&state->lock_alloc);
kono
parents:
diff changeset
211 }
kono
parents:
diff changeset
212 }
kono
parents:
diff changeset
213
kono
parents:
diff changeset
214 /* Grow VEC by SIZE bytes. */
kono
parents:
diff changeset
215
kono
parents:
diff changeset
216 void *
kono
parents:
diff changeset
217 backtrace_vector_grow (struct backtrace_state *state,size_t size,
kono
parents:
diff changeset
218 backtrace_error_callback error_callback,
kono
parents:
diff changeset
219 void *data, struct backtrace_vector *vec)
kono
parents:
diff changeset
220 {
kono
parents:
diff changeset
221 void *ret;
kono
parents:
diff changeset
222
kono
parents:
diff changeset
223 if (size > vec->alc)
kono
parents:
diff changeset
224 {
kono
parents:
diff changeset
225 size_t pagesize;
kono
parents:
diff changeset
226 size_t alc;
kono
parents:
diff changeset
227 void *base;
kono
parents:
diff changeset
228
kono
parents:
diff changeset
229 pagesize = getpagesize ();
kono
parents:
diff changeset
230 alc = vec->size + size;
kono
parents:
diff changeset
231 if (vec->size == 0)
kono
parents:
diff changeset
232 alc = 16 * size;
kono
parents:
diff changeset
233 else if (alc < pagesize)
kono
parents:
diff changeset
234 {
kono
parents:
diff changeset
235 alc *= 2;
kono
parents:
diff changeset
236 if (alc > pagesize)
kono
parents:
diff changeset
237 alc = pagesize;
kono
parents:
diff changeset
238 }
kono
parents:
diff changeset
239 else
kono
parents:
diff changeset
240 {
kono
parents:
diff changeset
241 alc *= 2;
kono
parents:
diff changeset
242 alc = (alc + pagesize - 1) & ~ (pagesize - 1);
kono
parents:
diff changeset
243 }
kono
parents:
diff changeset
244 base = backtrace_alloc (state, alc, error_callback, data);
kono
parents:
diff changeset
245 if (base == NULL)
kono
parents:
diff changeset
246 return NULL;
kono
parents:
diff changeset
247 if (vec->base != NULL)
kono
parents:
diff changeset
248 {
kono
parents:
diff changeset
249 memcpy (base, vec->base, vec->size);
kono
parents:
diff changeset
250 backtrace_free (state, vec->base, vec->size + vec->alc,
kono
parents:
diff changeset
251 error_callback, data);
kono
parents:
diff changeset
252 }
kono
parents:
diff changeset
253 vec->base = base;
kono
parents:
diff changeset
254 vec->alc = alc - vec->size;
kono
parents:
diff changeset
255 }
kono
parents:
diff changeset
256
kono
parents:
diff changeset
257 ret = (char *) vec->base + vec->size;
kono
parents:
diff changeset
258 vec->size += size;
kono
parents:
diff changeset
259 vec->alc -= size;
kono
parents:
diff changeset
260 return ret;
kono
parents:
diff changeset
261 }
kono
parents:
diff changeset
262
kono
parents:
diff changeset
263 /* Finish the current allocation on VEC. */
kono
parents:
diff changeset
264
kono
parents:
diff changeset
265 void *
kono
parents:
diff changeset
266 backtrace_vector_finish (
kono
parents:
diff changeset
267 struct backtrace_state *state ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
268 struct backtrace_vector *vec,
kono
parents:
diff changeset
269 backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
270 void *data ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
271 {
kono
parents:
diff changeset
272 void *ret;
kono
parents:
diff changeset
273
kono
parents:
diff changeset
274 ret = vec->base;
kono
parents:
diff changeset
275 vec->base = (char *) vec->base + vec->size;
kono
parents:
diff changeset
276 vec->size = 0;
kono
parents:
diff changeset
277 return ret;
kono
parents:
diff changeset
278 }
kono
parents:
diff changeset
279
kono
parents:
diff changeset
280 /* Release any extra space allocated for VEC. */
kono
parents:
diff changeset
281
kono
parents:
diff changeset
282 int
kono
parents:
diff changeset
283 backtrace_vector_release (struct backtrace_state *state,
kono
parents:
diff changeset
284 struct backtrace_vector *vec,
kono
parents:
diff changeset
285 backtrace_error_callback error_callback,
kono
parents:
diff changeset
286 void *data)
kono
parents:
diff changeset
287 {
kono
parents:
diff changeset
288 size_t size;
kono
parents:
diff changeset
289 size_t alc;
kono
parents:
diff changeset
290 size_t aligned;
kono
parents:
diff changeset
291
kono
parents:
diff changeset
292 /* Make sure that the block that we free is aligned on an 8-byte
kono
parents:
diff changeset
293 boundary. */
kono
parents:
diff changeset
294 size = vec->size;
kono
parents:
diff changeset
295 alc = vec->alc;
kono
parents:
diff changeset
296 aligned = (size + 7) & ~ (size_t) 7;
kono
parents:
diff changeset
297 alc -= aligned - size;
kono
parents:
diff changeset
298
kono
parents:
diff changeset
299 backtrace_free (state, (char *) vec->base + aligned, alc,
kono
parents:
diff changeset
300 error_callback, data);
kono
parents:
diff changeset
301 vec->alc = 0;
kono
parents:
diff changeset
302 return 1;
kono
parents:
diff changeset
303 }