tst-interpose-aux.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* Minimal malloc implementation for interposition tests.
  2. Copyright (C) 2016-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; see the file COPYING.LIB. If
  14. not, see <http://www.gnu.org/licenses/>. */
  15. #include "tst-interpose-aux.h"
  16. #include <errno.h>
  17. #include <stdarg.h>
  18. #include <stddef.h>
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/mman.h>
  24. #include <sys/uio.h>
  25. #include <unistd.h>
  26. #if INTERPOSE_THREADS
  27. #include <pthread.h>
  28. #endif
  29. /* Print the error message and terminate the process with status 1. */
  30. __attribute__ ((noreturn))
  31. __attribute__ ((format (printf, 1, 2)))
  32. static void *
  33. fail (const char *format, ...)
  34. {
  35. /* This assumes that vsnprintf will not call malloc. It does not do
  36. so for the format strings we use. */
  37. char message[4096];
  38. va_list ap;
  39. va_start (ap, format);
  40. vsnprintf (message, sizeof (message), format, ap);
  41. va_end (ap);
  42. enum { count = 3 };
  43. struct iovec iov[count];
  44. iov[0].iov_base = (char *) "error: ";
  45. iov[1].iov_base = (char *) message;
  46. iov[2].iov_base = (char *) "\n";
  47. for (int i = 0; i < count; ++i)
  48. iov[i].iov_len = strlen (iov[i].iov_base);
  49. int unused __attribute__ ((unused));
  50. unused = writev (STDOUT_FILENO, iov, count);
  51. _exit (1);
  52. }
  53. #if INTERPOSE_THREADS
  54. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  55. #endif
  56. static void
  57. lock (void)
  58. {
  59. #if INTERPOSE_THREADS
  60. int ret = pthread_mutex_lock (&mutex);
  61. if (ret != 0)
  62. {
  63. errno = ret;
  64. fail ("pthread_mutex_lock: %m");
  65. }
  66. #endif
  67. }
  68. static void
  69. unlock (void)
  70. {
  71. #if INTERPOSE_THREADS
  72. int ret = pthread_mutex_unlock (&mutex);
  73. if (ret != 0)
  74. {
  75. errno = ret;
  76. fail ("pthread_mutex_unlock: %m");
  77. }
  78. #endif
  79. }
  80. struct __attribute__ ((aligned (__alignof__ (max_align_t)))) allocation_header
  81. {
  82. size_t allocation_index;
  83. size_t allocation_size;
  84. };
  85. /* Array of known allocations, to track invalid frees. */
  86. enum { max_allocations = 65536 };
  87. static struct allocation_header *allocations[max_allocations];
  88. static size_t allocation_index;
  89. static size_t deallocation_count;
  90. /* Sanity check for successful malloc interposition. */
  91. __attribute__ ((destructor))
  92. static void
  93. check_for_allocations (void)
  94. {
  95. if (allocation_index == 0)
  96. {
  97. /* Make sure that malloc is called at least once from libc. */
  98. void *volatile ptr = strdup ("ptr");
  99. /* Compiler barrier. The strdup function calls malloc, which
  100. updates allocation_index, but strdup is marked __THROW, so
  101. the compiler could optimize away the reload. */
  102. __asm__ volatile ("" ::: "memory");
  103. free (ptr);
  104. /* If the allocation count is still zero, it means we did not
  105. interpose malloc successfully. */
  106. if (allocation_index == 0)
  107. fail ("malloc does not seem to have been interposed");
  108. }
  109. }
  110. static struct allocation_header *get_header (const char *op, void *ptr)
  111. {
  112. struct allocation_header *header = ((struct allocation_header *) ptr) - 1;
  113. if (header->allocation_index >= allocation_index)
  114. fail ("%s: %p: invalid allocation index: %zu (not less than %zu)",
  115. op, ptr, header->allocation_index, allocation_index);
  116. if (allocations[header->allocation_index] != header)
  117. fail ("%s: %p: allocation pointer does not point to header, but %p",
  118. op, ptr, allocations[header->allocation_index]);
  119. return header;
  120. }
  121. /* Internal helper functions. Those must be called while the lock is
  122. acquired. */
  123. static void *
  124. malloc_internal (size_t size)
  125. {
  126. if (allocation_index == max_allocations)
  127. {
  128. errno = ENOMEM;
  129. return NULL;
  130. }
  131. size_t allocation_size = size + sizeof (struct allocation_header);
  132. if (allocation_size < size)
  133. {
  134. errno = ENOMEM;
  135. return NULL;
  136. }
  137. size_t index = allocation_index++;
  138. void *result = mmap (NULL, allocation_size, PROT_READ | PROT_WRITE,
  139. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  140. if (result == MAP_FAILED)
  141. return NULL;
  142. allocations[index] = result;
  143. *allocations[index] = (struct allocation_header)
  144. {
  145. .allocation_index = index,
  146. .allocation_size = allocation_size
  147. };
  148. return allocations[index] + 1;
  149. }
  150. static void
  151. free_internal (const char *op, struct allocation_header *header)
  152. {
  153. size_t index = header->allocation_index;
  154. int result = mprotect (header, header->allocation_size, PROT_NONE);
  155. if (result != 0)
  156. fail ("%s: mprotect (%p, %zu): %m", op, header, header->allocation_size);
  157. /* Catch double-free issues. */
  158. allocations[index] = NULL;
  159. ++deallocation_count;
  160. }
  161. static void *
  162. realloc_internal (void *ptr, size_t new_size)
  163. {
  164. struct allocation_header *header = get_header ("realloc", ptr);
  165. size_t old_size = header->allocation_size - sizeof (struct allocation_header);
  166. if (old_size >= new_size)
  167. return ptr;
  168. void *newptr = malloc_internal (new_size);
  169. if (newptr == NULL)
  170. return NULL;
  171. memcpy (newptr, ptr, old_size);
  172. free_internal ("realloc", header);
  173. return newptr;
  174. }
  175. /* Public interfaces. These functions must perform locking. */
  176. size_t
  177. malloc_allocation_count (void)
  178. {
  179. lock ();
  180. size_t count = allocation_index;
  181. unlock ();
  182. return count;
  183. }
  184. size_t
  185. malloc_deallocation_count (void)
  186. {
  187. lock ();
  188. size_t count = deallocation_count;
  189. unlock ();
  190. return count;
  191. }
  192. void *
  193. malloc (size_t size)
  194. {
  195. lock ();
  196. void *result = malloc_internal (size);
  197. unlock ();
  198. return result;
  199. }
  200. void
  201. free (void *ptr)
  202. {
  203. if (ptr == NULL)
  204. return;
  205. lock ();
  206. struct allocation_header *header = get_header ("free", ptr);
  207. free_internal ("free", header);
  208. unlock ();
  209. }
  210. void *
  211. calloc (size_t a, size_t b)
  212. {
  213. if (b > 0 && a > SIZE_MAX / b)
  214. {
  215. errno = ENOMEM;
  216. return NULL;
  217. }
  218. lock ();
  219. /* malloc_internal uses mmap, so the memory is zeroed. */
  220. void *result = malloc_internal (a * b);
  221. unlock ();
  222. return result;
  223. }
  224. void *
  225. realloc (void *ptr, size_t n)
  226. {
  227. if (n ==0)
  228. {
  229. free (ptr);
  230. return NULL;
  231. }
  232. else if (ptr == NULL)
  233. return malloc (n);
  234. else
  235. {
  236. lock ();
  237. void *result = realloc_internal (ptr, n);
  238. unlock ();
  239. return result;
  240. }
  241. }