mmap.c 7.6 KB

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