tst-interpose-aux.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* Minimal malloc implementation for interposition tests.
  2. Copyright (C) 2016-2026 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 <https://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. #include <time.h>
  27. #if INTERPOSE_THREADS
  28. #include <pthread.h>
  29. #endif
  30. /* Print the error message and terminate the process with status 1. */
  31. __attribute__ ((noreturn))
  32. __attribute__ ((format (printf, 1, 2)))
  33. static void *
  34. fail (const char *format, ...)
  35. {
  36. /* This assumes that vsnprintf will not call malloc. It does not do
  37. so for the format strings we use. */
  38. char message[4096];
  39. va_list ap;
  40. va_start (ap, format);
  41. vsnprintf (message, sizeof (message), format, ap);
  42. va_end (ap);
  43. enum { count = 3 };
  44. struct iovec iov[count];
  45. iov[0].iov_base = (char *) "error: ";
  46. iov[1].iov_base = (char *) message;
  47. iov[2].iov_base = (char *) "\n";
  48. for (int i = 0; i < count; ++i)
  49. iov[i].iov_len = strlen (iov[i].iov_base);
  50. int unused __attribute__ ((unused));
  51. unused = writev (STDOUT_FILENO, iov, count);
  52. _exit (1);
  53. }
  54. #if INTERPOSE_THREADS
  55. static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  56. #endif
  57. static void
  58. lock (void)
  59. {
  60. #if INTERPOSE_THREADS
  61. int ret = pthread_mutex_lock (&mutex);
  62. if (ret != 0)
  63. {
  64. errno = ret;
  65. fail ("pthread_mutex_lock: %m");
  66. }
  67. #endif
  68. }
  69. static void
  70. unlock (void)
  71. {
  72. #if INTERPOSE_THREADS
  73. int ret = pthread_mutex_unlock (&mutex);
  74. if (ret != 0)
  75. {
  76. errno = ret;
  77. fail ("pthread_mutex_unlock: %m");
  78. }
  79. #endif
  80. }
  81. struct __attribute__ ((aligned (__alignof__ (max_align_t)))) allocation_header
  82. {
  83. size_t allocation_index;
  84. size_t allocation_size;
  85. struct timespec ts;
  86. };
  87. /* Array of known allocations, to track invalid frees. */
  88. enum { max_allocations = 65536 };
  89. static struct allocation_header *allocations[max_allocations];
  90. static size_t allocation_index;
  91. static size_t deallocation_count;
  92. /* Sanity check for successful malloc interposition. */
  93. __attribute__ ((destructor))
  94. static void
  95. check_for_allocations (void)
  96. {
  97. if (allocation_index == 0)
  98. {
  99. /* Make sure that malloc is called at least once from libc. */
  100. void *volatile ptr = strdup ("ptr");
  101. /* Compiler barrier. The strdup function calls malloc, which
  102. updates allocation_index, but strdup is marked __THROW, so
  103. the compiler could optimize away the reload. */
  104. __asm__ volatile ("" ::: "memory");
  105. free (ptr);
  106. /* If the allocation count is still zero, it means we did not
  107. interpose malloc successfully. */
  108. if (allocation_index == 0)
  109. fail ("malloc does not seem to have been interposed");
  110. }
  111. }
  112. static struct allocation_header *get_header (const char *op, void *ptr)
  113. {
  114. struct allocation_header *header = ((struct allocation_header *) ptr) - 1;
  115. if (header->allocation_index >= allocation_index)
  116. fail ("%s: %p: invalid allocation index: %zu (not less than %zu)",
  117. op, ptr, header->allocation_index, allocation_index);
  118. if (allocations[header->allocation_index] != header)
  119. fail ("%s: %p: allocation pointer does not point to header, but %p",
  120. op, ptr, allocations[header->allocation_index]);
  121. return header;
  122. }
  123. /* Internal helper functions. Those must be called while the lock is
  124. acquired. */
  125. static void *
  126. malloc_internal (size_t size)
  127. {
  128. if (allocation_index == max_allocations)
  129. {
  130. errno = ENOMEM;
  131. return NULL;
  132. }
  133. size_t allocation_size = size + sizeof (struct allocation_header);
  134. if (allocation_size < size)
  135. {
  136. errno = ENOMEM;
  137. return NULL;
  138. }
  139. void *result = mmap (NULL, allocation_size, PROT_READ | PROT_WRITE,
  140. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  141. if (result == MAP_FAILED)
  142. return NULL;
  143. size_t index = allocation_index++;
  144. allocations[index] = result;
  145. *allocations[index] = (struct allocation_header)
  146. {
  147. .allocation_index = index,
  148. .allocation_size = allocation_size
  149. };
  150. /* BZ#24967: Check if calling a symbol which may use the vDSO does not fail.
  151. The CLOCK_REALTIME should be supported on all systems. */
  152. clock_gettime (CLOCK_REALTIME, &allocations[index]->ts);
  153. return allocations[index] + 1;
  154. }
  155. static void
  156. free_internal (const char *op, struct allocation_header *header)
  157. {
  158. size_t index = header->allocation_index;
  159. int result = mprotect (header, header->allocation_size, PROT_NONE);
  160. if (result != 0)
  161. fail ("%s: mprotect (%p, %zu): %m", op, header, header->allocation_size);
  162. /* Catch double-free issues. */
  163. allocations[index] = NULL;
  164. ++deallocation_count;
  165. }
  166. static void *
  167. realloc_internal (void *ptr, size_t new_size)
  168. {
  169. struct allocation_header *header = get_header ("realloc", ptr);
  170. size_t old_size = header->allocation_size - sizeof (struct allocation_header);
  171. if (old_size >= new_size)
  172. return ptr;
  173. void *newptr = malloc_internal (new_size);
  174. if (newptr == NULL)
  175. return NULL;
  176. memcpy (newptr, ptr, old_size);
  177. free_internal ("realloc", header);
  178. return newptr;
  179. }
  180. /* Public interfaces. These functions must perform locking. */
  181. size_t
  182. malloc_allocation_count (void)
  183. {
  184. lock ();
  185. size_t count = allocation_index;
  186. unlock ();
  187. return count;
  188. }
  189. size_t
  190. malloc_deallocation_count (void)
  191. {
  192. lock ();
  193. size_t count = deallocation_count;
  194. unlock ();
  195. return count;
  196. }
  197. void *
  198. malloc (size_t size)
  199. {
  200. lock ();
  201. void *result = malloc_internal (size);
  202. unlock ();
  203. return result;
  204. }
  205. void
  206. free (void *ptr)
  207. {
  208. if (ptr == NULL)
  209. return;
  210. lock ();
  211. struct allocation_header *header = get_header ("free", ptr);
  212. free_internal ("free", header);
  213. unlock ();
  214. }
  215. void *
  216. calloc (size_t a, size_t b)
  217. {
  218. if (b > 0 && a > SIZE_MAX / b)
  219. {
  220. errno = ENOMEM;
  221. return NULL;
  222. }
  223. lock ();
  224. /* malloc_internal uses mmap, so the memory is zeroed. */
  225. void *result = malloc_internal (a * b);
  226. unlock ();
  227. return result;
  228. }
  229. void *
  230. realloc (void *ptr, size_t n)
  231. {
  232. if (n ==0)
  233. {
  234. free (ptr);
  235. return NULL;
  236. }
  237. else if (ptr == NULL)
  238. return malloc (n);
  239. else
  240. {
  241. lock ();
  242. void *result = realloc_internal (ptr, n);
  243. unlock ();
  244. return result;
  245. }
  246. }