1 /* mmap.c -- Memory allocation with mmap.
2 Copyright (C) 2012-2017 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Google.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 (2) Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
17 (3) The name of the author may not be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE. */
39 #include <sys/types.h>
42 #include "backtrace.h"
45 /* Memory allocation on systems that provide anonymous mmap. This
46 permits the backtrace functions to be invoked from a signal
47 handler, assuming that mmap is async-signal safe. */
50 #define MAP_ANONYMOUS MAP_ANON
54 #define MAP_FAILED ((void *)-1)
57 /* A list of free memory blocks. */
59 struct backtrace_freelist_struct
62 struct backtrace_freelist_struct
*next
;
63 /* Size of this block, including this structure. */
67 /* Free memory allocated by backtrace_alloc. */
70 backtrace_free_locked (struct backtrace_state
*state
, void *addr
, size_t size
)
72 /* Just leak small blocks. We don't have to be perfect. */
73 if (size
>= sizeof (struct backtrace_freelist_struct
))
75 struct backtrace_freelist_struct
*p
;
77 p
= (struct backtrace_freelist_struct
*) addr
;
78 p
->next
= state
->freelist
;
84 /* Allocate memory like malloc. If ERROR_CALLBACK is NULL, don't
88 backtrace_alloc (struct backtrace_state
*state
,
89 size_t size
, backtrace_error_callback error_callback
,
94 struct backtrace_freelist_struct
**pp
;
101 /* If we can acquire the lock, then see if there is space on the
102 free list. If we can't acquire the lock, drop straight into
103 using mmap. __sync_lock_test_and_set returns the old state of
104 the lock, so we have acquired it if it returns 0. */
106 if (!state
->threaded
)
109 locked
= __sync_lock_test_and_set (&state
->lock_alloc
, 1) == 0;
113 for (pp
= &state
->freelist
; *pp
!= NULL
; pp
= &(*pp
)->next
)
115 if ((*pp
)->size
>= size
)
117 struct backtrace_freelist_struct
*p
;
122 /* Round for alignment; we assume that no type we care about
123 is more than 8 bytes. */
124 size
= (size
+ 7) & ~ (size_t) 7;
126 backtrace_free_locked (state
, (char *) p
+ size
,
136 __sync_lock_release (&state
->lock_alloc
);
141 /* Allocate a new page. */
143 pagesize
= getpagesize ();
144 asksize
= (size
+ pagesize
- 1) & ~ (pagesize
- 1);
145 page
= mmap (NULL
, asksize
, PROT_READ
| PROT_WRITE
,
146 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
147 if (page
== MAP_FAILED
)
150 error_callback (data
, "mmap", errno
);
154 size
= (size
+ 7) & ~ (size_t) 7;
156 backtrace_free (state
, (char *) page
+ size
, asksize
- size
,
157 error_callback
, data
);
166 /* Free memory allocated by backtrace_alloc. */
169 backtrace_free (struct backtrace_state
*state
, void *addr
, size_t size
,
170 backtrace_error_callback error_callback ATTRIBUTE_UNUSED
,
171 void *data ATTRIBUTE_UNUSED
)
175 /* If we are freeing a large aligned block, just release it back to
176 the system. This case arises when growing a vector for a large
177 binary with lots of debug info. Calling munmap here may cause us
178 to call mmap again if there is also a large shared library; we
179 just live with that. */
180 if (size
>= 16 * 4096)
184 pagesize
= getpagesize ();
185 if (((uintptr_t) addr
& (pagesize
- 1)) == 0
186 && (size
& (pagesize
- 1)) == 0)
188 /* If munmap fails for some reason, just add the block to
190 if (munmap (addr
, size
) == 0)
195 /* If we can acquire the lock, add the new space to the free list.
196 If we can't acquire the lock, just leak the memory.
197 __sync_lock_test_and_set returns the old state of the lock, so we
198 have acquired it if it returns 0. */
200 if (!state
->threaded
)
203 locked
= __sync_lock_test_and_set (&state
->lock_alloc
, 1) == 0;
207 backtrace_free_locked (state
, addr
, size
);
210 __sync_lock_release (&state
->lock_alloc
);
214 /* Grow VEC by SIZE bytes. */
217 backtrace_vector_grow (struct backtrace_state
*state
,size_t size
,
218 backtrace_error_callback error_callback
,
219 void *data
, struct backtrace_vector
*vec
)
229 pagesize
= getpagesize ();
230 alc
= vec
->size
+ size
;
233 else if (alc
< pagesize
)
242 alc
= (alc
+ pagesize
- 1) & ~ (pagesize
- 1);
244 base
= backtrace_alloc (state
, alc
, error_callback
, data
);
247 if (vec
->base
!= NULL
)
249 memcpy (base
, vec
->base
, vec
->size
);
250 backtrace_free (state
, vec
->base
, vec
->size
+ vec
->alc
,
251 error_callback
, data
);
254 vec
->alc
= alc
- vec
->size
;
257 ret
= (char *) vec
->base
+ vec
->size
;
263 /* Finish the current allocation on VEC. */
266 backtrace_vector_finish (
267 struct backtrace_state
*state ATTRIBUTE_UNUSED
,
268 struct backtrace_vector
*vec
,
269 backtrace_error_callback error_callback ATTRIBUTE_UNUSED
,
270 void *data ATTRIBUTE_UNUSED
)
275 vec
->base
= (char *) vec
->base
+ vec
->size
;
280 /* Release any extra space allocated for VEC. */
283 backtrace_vector_release (struct backtrace_state
*state
,
284 struct backtrace_vector
*vec
,
285 backtrace_error_callback error_callback
,
292 /* Make sure that the block that we free is aligned on an 8-byte
296 aligned
= (size
+ 7) & ~ (size_t) 7;
297 alc
-= aligned
- size
;
299 backtrace_free (state
, (char *) vec
->base
+ aligned
, alc
,
300 error_callback
, data
);