util_malloc.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* Copyright (c) 2003, Roger Dingledine
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file util_malloc.c
  7. * \brief Wrappers for C malloc code, and replacements for items that
  8. * may be missing.
  9. **/
  10. #include "orconfig.h"
  11. #include <stdlib.h>
  12. #include <stddef.h>
  13. #include <string.h>
  14. #include "lib/testsupport/testsupport.h"
  15. #define UTIL_MALLOC_PRIVATE
  16. #include "lib/malloc/util_malloc.h"
  17. #include "lib/cc/torint.h"
  18. #include "lib/err/torerr.h"
  19. #ifdef __clang_analyzer__
  20. #undef MALLOC_ZERO_WORKS
  21. #endif
  22. /** Allocate a chunk of <b>size</b> bytes of memory, and return a pointer to
  23. * result. On error, log and terminate the process. (Same as malloc(size),
  24. * but never returns NULL.)
  25. */
  26. void *
  27. tor_malloc_(size_t size)
  28. {
  29. void *result;
  30. raw_assert(size < SIZE_T_CEILING);
  31. #ifndef MALLOC_ZERO_WORKS
  32. /* Some libc mallocs don't work when size==0. Override them. */
  33. if (size==0) {
  34. size=1;
  35. }
  36. #endif /* !defined(MALLOC_ZERO_WORKS) */
  37. result = raw_malloc(size);
  38. if (PREDICT_UNLIKELY(result == NULL)) {
  39. /* LCOV_EXCL_START */
  40. /* If these functions die within a worker process, they won't call
  41. * spawn_exit, but that's ok, since the parent will run out of memory soon
  42. * anyway. */
  43. raw_assert_unreached_msg("Out of memory on malloc(). Dying.");
  44. /* LCOV_EXCL_STOP */
  45. }
  46. return result;
  47. }
  48. /** Allocate a chunk of <b>size</b> bytes of memory, fill the memory with
  49. * zero bytes, and return a pointer to the result. Log and terminate
  50. * the process on error. (Same as calloc(size,1), but never returns NULL.)
  51. */
  52. void *
  53. tor_malloc_zero_(size_t size)
  54. {
  55. /* You may ask yourself, "wouldn't it be smart to use calloc instead of
  56. * malloc+memset? Perhaps libc's calloc knows some nifty optimization trick
  57. * we don't!" Indeed it does, but its optimizations are only a big win when
  58. * we're allocating something very big (it knows if it just got the memory
  59. * from the OS in a pre-zeroed state). We don't want to use tor_malloc_zero
  60. * for big stuff, so we don't bother with calloc. */
  61. void *result = tor_malloc_(size);
  62. memset(result, 0, size);
  63. return result;
  64. }
  65. /* The square root of SIZE_MAX + 1. If a is less than this, and b is less
  66. * than this, then a*b is less than SIZE_MAX. (For example, if size_t is
  67. * 32 bits, then SIZE_MAX is 0xffffffff and this value is 0x10000. If a and
  68. * b are less than this, then their product is at most (65535*65535) ==
  69. * 0xfffe0001. */
  70. #define SQRT_SIZE_MAX_P1 (((size_t)1) << (sizeof(size_t)*4))
  71. /** Return non-zero if and only if the product of the arguments is exact,
  72. * and cannot overflow. */
  73. STATIC int
  74. size_mul_check(const size_t x, const size_t y)
  75. {
  76. /* This first check is equivalent to
  77. (x < SQRT_SIZE_MAX_P1 && y < SQRT_SIZE_MAX_P1)
  78. Rationale: if either one of x or y is >= SQRT_SIZE_MAX_P1, then it
  79. will have some bit set in its most significant half.
  80. */
  81. return ((x|y) < SQRT_SIZE_MAX_P1 ||
  82. y == 0 ||
  83. x <= SIZE_MAX / y);
  84. }
  85. /** Allocate a chunk of <b>nmemb</b>*<b>size</b> bytes of memory, fill
  86. * the memory with zero bytes, and return a pointer to the result.
  87. * Log and terminate the process on error. (Same as
  88. * calloc(<b>nmemb</b>,<b>size</b>), but never returns NULL.)
  89. * The second argument (<b>size</b>) should preferably be non-zero
  90. * and a compile-time constant.
  91. */
  92. void *
  93. tor_calloc_(size_t nmemb, size_t size)
  94. {
  95. raw_assert(size_mul_check(nmemb, size));
  96. return tor_malloc_zero_((nmemb * size));
  97. }
  98. /** Change the size of the memory block pointed to by <b>ptr</b> to <b>size</b>
  99. * bytes long; return the new memory block. On error, log and
  100. * terminate. (Like realloc(ptr,size), but never returns NULL.)
  101. */
  102. void *
  103. tor_realloc_(void *ptr, size_t size)
  104. {
  105. void *result;
  106. raw_assert(size < SIZE_T_CEILING);
  107. #ifndef MALLOC_ZERO_WORKS
  108. /* Some libc mallocs don't work when size==0. Override them. */
  109. if (size==0) {
  110. size=1;
  111. }
  112. #endif /* !defined(MALLOC_ZERO_WORKS) */
  113. result = raw_realloc(ptr, size);
  114. if (PREDICT_UNLIKELY(result == NULL)) {
  115. /* LCOV_EXCL_START */
  116. raw_assert_unreached_msg("Out of memory on realloc(). Dying.");
  117. /* LCOV_EXCL_STOP */
  118. }
  119. return result;
  120. }
  121. /**
  122. * Try to realloc <b>ptr</b> so that it takes up sz1 * sz2 bytes. Check for
  123. * overflow. Unlike other allocation functions, return NULL on overflow.
  124. */
  125. void *
  126. tor_reallocarray_(void *ptr, size_t sz1, size_t sz2)
  127. {
  128. /* XXXX we can make this return 0, but we would need to check all the
  129. * reallocarray users. */
  130. raw_assert(size_mul_check(sz1, sz2));
  131. return tor_realloc(ptr, (sz1 * sz2));
  132. }
  133. /** Return a newly allocated copy of the NUL-terminated string s. On
  134. * error, log and terminate. (Like strdup(s), but never returns
  135. * NULL.)
  136. */
  137. char *
  138. tor_strdup_(const char *s)
  139. {
  140. char *duplicate;
  141. raw_assert(s);
  142. duplicate = raw_strdup(s);
  143. if (PREDICT_UNLIKELY(duplicate == NULL)) {
  144. /* LCOV_EXCL_START */
  145. raw_assert_unreached_msg("Out of memory on strdup(). Dying.");
  146. /* LCOV_EXCL_STOP */
  147. }
  148. return duplicate;
  149. }
  150. /** Allocate and return a new string containing the first <b>n</b>
  151. * characters of <b>s</b>. If <b>s</b> is longer than <b>n</b>
  152. * characters, only the first <b>n</b> are copied. The result is
  153. * always NUL-terminated. (Like strndup(s,n), but never returns
  154. * NULL.)
  155. */
  156. char *
  157. tor_strndup_(const char *s, size_t n)
  158. {
  159. char *duplicate;
  160. raw_assert(s);
  161. raw_assert(n < SIZE_T_CEILING);
  162. duplicate = tor_malloc_((n+1));
  163. /* Performance note: Ordinarily we prefer strlcpy to strncpy. But
  164. * this function gets called a whole lot, and platform strncpy is
  165. * much faster than strlcpy when strlen(s) is much longer than n.
  166. */
  167. strncpy(duplicate, s, n);
  168. duplicate[n]='\0';
  169. return duplicate;
  170. }
  171. /** Allocate a chunk of <b>len</b> bytes, with the same contents as the
  172. * <b>len</b> bytes starting at <b>mem</b>. */
  173. void *
  174. tor_memdup_(const void *mem, size_t len)
  175. {
  176. char *duplicate;
  177. raw_assert(len < SIZE_T_CEILING);
  178. raw_assert(mem);
  179. duplicate = tor_malloc_(len);
  180. memcpy(duplicate, mem, len);
  181. return duplicate;
  182. }
  183. /** As tor_memdup(), but add an extra 0 byte at the end of the resulting
  184. * memory. */
  185. void *
  186. tor_memdup_nulterm_(const void *mem, size_t len)
  187. {
  188. char *duplicate;
  189. raw_assert(len < SIZE_T_CEILING+1);
  190. raw_assert(mem);
  191. duplicate = tor_malloc_(len+1);
  192. memcpy(duplicate, mem, len);
  193. duplicate[len] = '\0';
  194. return duplicate;
  195. }
  196. /** Helper for places that need to take a function pointer to the right
  197. * spelling of "free()". */
  198. void
  199. tor_free_(void *mem)
  200. {
  201. tor_free(mem);
  202. }