mmap.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* mmap.c -- Memory allocation with mmap.
  2. Copyright (C) 2012-2021 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. #ifndef HAVE_DECL_GETPAGESIZE
  37. extern int getpagesize (void);
  38. #endif
  39. /* Memory allocation on systems that provide anonymous mmap. This
  40. permits the backtrace functions to be invoked from a signal
  41. handler, assuming that mmap is async-signal safe. */
  42. #ifndef MAP_ANONYMOUS
  43. #define MAP_ANONYMOUS MAP_ANON
  44. #endif
  45. #ifndef MAP_FAILED
  46. #define MAP_FAILED ((void *)-1)
  47. #endif
  48. /* A list of free memory blocks. */
  49. struct backtrace_freelist_struct
  50. {
  51. /* Next on list. */
  52. struct backtrace_freelist_struct *next;
  53. /* Size of this block, including this structure. */
  54. size_t size;
  55. };
  56. /* Free memory allocated by backtrace_alloc. */
  57. static void
  58. backtrace_free_locked (struct backtrace_state *state, void *addr, size_t size)
  59. {
  60. /* Just leak small blocks. We don't have to be perfect. Don't put
  61. more than 16 entries on the free list, to avoid wasting time
  62. searching when allocating a block. If we have more than 16
  63. entries, leak the smallest entry. */
  64. if (size >= sizeof (struct backtrace_freelist_struct))
  65. {
  66. size_t c;
  67. struct backtrace_freelist_struct **ppsmall;
  68. struct backtrace_freelist_struct **pp;
  69. struct backtrace_freelist_struct *p;
  70. c = 0;
  71. ppsmall = NULL;
  72. for (pp = &state->freelist; *pp != NULL; pp = &(*pp)->next)
  73. {
  74. if (ppsmall == NULL || (*pp)->size < (*ppsmall)->size)
  75. ppsmall = pp;
  76. ++c;
  77. }
  78. if (c >= 16)
  79. {
  80. if (size <= (*ppsmall)->size)
  81. return;
  82. *ppsmall = (*ppsmall)->next;
  83. }
  84. p = (struct backtrace_freelist_struct *) addr;
  85. p->next = state->freelist;
  86. p->size = size;
  87. state->freelist = p;
  88. }
  89. }
  90. /* Allocate memory like malloc. If ERROR_CALLBACK is NULL, don't
  91. report an error. */
  92. void *
  93. backtrace_alloc (struct backtrace_state *state,
  94. size_t size, backtrace_error_callback error_callback,
  95. void *data)
  96. {
  97. void *ret;
  98. int locked;
  99. struct backtrace_freelist_struct **pp;
  100. size_t pagesize;
  101. size_t asksize;
  102. void *page;
  103. ret = NULL;
  104. /* If we can acquire the lock, then see if there is space on the
  105. free list. If we can't acquire the lock, drop straight into
  106. using mmap. __sync_lock_test_and_set returns the old state of
  107. the lock, so we have acquired it if it returns 0. */
  108. if (!state->threaded)
  109. locked = 1;
  110. else
  111. locked = __sync_lock_test_and_set (&state->lock_alloc, 1) == 0;
  112. if (locked)
  113. {
  114. for (pp = &state->freelist; *pp != NULL; pp = &(*pp)->next)
  115. {
  116. if ((*pp)->size >= size)
  117. {
  118. struct backtrace_freelist_struct *p;
  119. p = *pp;
  120. *pp = p->next;
  121. /* Round for alignment; we assume that no type we care about
  122. is more than 8 bytes. */
  123. size = (size + 7) & ~ (size_t) 7;
  124. if (size < p->size)
  125. backtrace_free_locked (state, (char *) p + size,
  126. p->size - size);
  127. ret = (void *) p;
  128. break;
  129. }
  130. }
  131. if (state->threaded)
  132. __sync_lock_release (&state->lock_alloc);
  133. }
  134. if (ret == NULL)
  135. {
  136. /* Allocate a new page. */
  137. pagesize = getpagesize ();
  138. asksize = (size + pagesize - 1) & ~ (pagesize - 1);
  139. page = mmap (NULL, asksize, PROT_READ | PROT_WRITE,
  140. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  141. if (page == MAP_FAILED)
  142. {
  143. if (error_callback)
  144. error_callback (data, "mmap", errno);
  145. }
  146. else
  147. {
  148. size = (size + 7) & ~ (size_t) 7;
  149. if (size < asksize)
  150. backtrace_free (state, (char *) page + size, asksize - size,
  151. error_callback, data);
  152. ret = page;
  153. }
  154. }
  155. return ret;
  156. }
  157. /* Free memory allocated by backtrace_alloc. */
  158. void
  159. backtrace_free (struct backtrace_state *state, void *addr, size_t size,
  160. backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
  161. void *data ATTRIBUTE_UNUSED)
  162. {
  163. int locked;
  164. /* If we are freeing a large aligned block, just release it back to
  165. the system. This case arises when growing a vector for a large
  166. binary with lots of debug info. Calling munmap here may cause us
  167. to call mmap again if there is also a large shared library; we
  168. just live with that. */
  169. if (size >= 16 * 4096)
  170. {
  171. size_t pagesize;
  172. pagesize = getpagesize ();
  173. if (((uintptr_t) addr & (pagesize - 1)) == 0
  174. && (size & (pagesize - 1)) == 0)
  175. {
  176. /* If munmap fails for some reason, just add the block to
  177. the freelist. */
  178. if (munmap (addr, size) == 0)
  179. return;
  180. }
  181. }
  182. /* If we can acquire the lock, add the new space to the free list.
  183. If we can't acquire the lock, just leak the memory.
  184. __sync_lock_test_and_set returns the old state of the lock, so we
  185. have acquired it if it returns 0. */
  186. if (!state->threaded)
  187. locked = 1;
  188. else
  189. locked = __sync_lock_test_and_set (&state->lock_alloc, 1) == 0;
  190. if (locked)
  191. {
  192. backtrace_free_locked (state, addr, size);
  193. if (state->threaded)
  194. __sync_lock_release (&state->lock_alloc);
  195. }
  196. }
  197. /* Grow VEC by SIZE bytes. */
  198. void *
  199. backtrace_vector_grow (struct backtrace_state *state,size_t size,
  200. backtrace_error_callback error_callback,
  201. void *data, struct backtrace_vector *vec)
  202. {
  203. void *ret;
  204. if (size > vec->alc)
  205. {
  206. size_t pagesize;
  207. size_t alc;
  208. void *base;
  209. pagesize = getpagesize ();
  210. alc = vec->size + size;
  211. if (vec->size == 0)
  212. alc = 16 * size;
  213. else if (alc < pagesize)
  214. {
  215. alc *= 2;
  216. if (alc > pagesize)
  217. alc = pagesize;
  218. }
  219. else
  220. {
  221. alc *= 2;
  222. alc = (alc + pagesize - 1) & ~ (pagesize - 1);
  223. }
  224. base = backtrace_alloc (state, alc, error_callback, data);
  225. if (base == NULL)
  226. return NULL;
  227. if (vec->base != NULL)
  228. {
  229. memcpy (base, vec->base, vec->size);
  230. backtrace_free (state, vec->base, vec->size + vec->alc,
  231. error_callback, data);
  232. }
  233. vec->base = base;
  234. vec->alc = alc - vec->size;
  235. }
  236. ret = (char *) vec->base + vec->size;
  237. vec->size += size;
  238. vec->alc -= size;
  239. return ret;
  240. }
  241. /* Finish the current allocation on VEC. */
  242. void *
  243. backtrace_vector_finish (
  244. struct backtrace_state *state ATTRIBUTE_UNUSED,
  245. struct backtrace_vector *vec,
  246. backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
  247. void *data ATTRIBUTE_UNUSED)
  248. {
  249. void *ret;
  250. ret = vec->base;
  251. vec->base = (char *) vec->base + vec->size;
  252. vec->size = 0;
  253. return ret;
  254. }
  255. /* Release any extra space allocated for VEC. */
  256. int
  257. backtrace_vector_release (struct backtrace_state *state,
  258. struct backtrace_vector *vec,
  259. backtrace_error_callback error_callback,
  260. void *data)
  261. {
  262. size_t size;
  263. size_t alc;
  264. size_t aligned;
  265. /* Make sure that the block that we free is aligned on an 8-byte
  266. boundary. */
  267. size = vec->size;
  268. alc = vec->alc;
  269. aligned = (size + 7) & ~ (size_t) 7;
  270. alc -= aligned - size;
  271. backtrace_free (state, (char *) vec->base + aligned, alc,
  272. error_callback, data);
  273. vec->alc = 0;
  274. if (vec->size == 0)
  275. vec->base = NULL;
  276. return 1;
  277. }