tst-interpose-skeleton.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* Test driver for malloc 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 <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <unistd.h>
  19. #if INTERPOSE_THREADS
  20. #include <pthread.h>
  21. #endif
  22. static int do_test (void);
  23. #define TEST_FUNCTION do_test ()
  24. #include "../test-skeleton.c"
  25. /* Fills BUFFER with a test string. */
  26. static void
  27. line_string (int number, char *buffer, size_t length)
  28. {
  29. for (size_t i = 0; i < length - 2; ++i)
  30. buffer[i] = 'A' + ((number + i) % 26);
  31. buffer[length - 2] = '\n';
  32. buffer[length - 1] = '\0';
  33. }
  34. /* Perform the tests. */
  35. static void *
  36. run_tests (void *closure)
  37. {
  38. char *temp_file_path;
  39. int fd = create_temp_file ("tst-malloc-interpose", &temp_file_path);
  40. if (fd < 0)
  41. _exit (1);
  42. /* Line lengths excluding the line terminator. */
  43. static const int line_lengths[] = { 0, 45, 80, 2, 8201, 0, 17, -1 };
  44. /* Fill the test file with data. */
  45. {
  46. FILE *fp = fdopen (fd, "w");
  47. for (int lineno = 0; line_lengths[lineno] >= 0; ++lineno)
  48. {
  49. char buffer[line_lengths[lineno] + 2];
  50. line_string (lineno, buffer, sizeof (buffer));
  51. fprintf (fp, "%s", buffer);
  52. }
  53. if (ferror (fp))
  54. {
  55. printf ("error: fprintf: %m\n");
  56. _exit (1);
  57. }
  58. if (fclose (fp) != 0)
  59. {
  60. printf ("error: fclose: %m\n");
  61. _exit (1);
  62. }
  63. }
  64. /* Read the test file. This tests libc-internal allocation with
  65. realloc. */
  66. {
  67. FILE *fp = fopen (temp_file_path, "r");
  68. char *actual = NULL;
  69. size_t actual_size = 0;
  70. for (int lineno = 0; ; ++lineno)
  71. {
  72. errno = 0;
  73. ssize_t result = getline (&actual, &actual_size, fp);
  74. if (result == 0)
  75. {
  76. printf ("error: invalid return value 0 from getline\n");
  77. _exit (1);
  78. }
  79. if (result < 0 && errno != 0)
  80. {
  81. printf ("error: getline: %m\n");
  82. _exit (1);
  83. }
  84. if (result < 0 && line_lengths[lineno] >= 0)
  85. {
  86. printf ("error: unexpected end of file after line %d\n", lineno);
  87. _exit (1);
  88. }
  89. if (result > 0 && line_lengths[lineno] < 0)
  90. {
  91. printf ("error: no end of file after line %d\n", lineno);
  92. _exit (1);
  93. }
  94. if (result == -1 && line_lengths[lineno] == -1)
  95. /* End of file reached as expected. */
  96. break;
  97. if (result != line_lengths[lineno] + 1)
  98. {
  99. printf ("error: line length mismatch: expected %d, got %zd\n",
  100. line_lengths[lineno], result);
  101. _exit (1);
  102. }
  103. char expected[line_lengths[lineno] + 2];
  104. line_string (lineno, expected, sizeof (expected));
  105. if (strcmp (actual, expected) != 0)
  106. {
  107. printf ("error: line mismatch\n");
  108. printf ("error: expected: [[%s]]\n", expected);
  109. printf ("error: actual: [[%s]]\n", actual);
  110. _exit (1);
  111. }
  112. }
  113. if (fclose (fp) != 0)
  114. {
  115. printf ("error: fclose (after reading): %m\n");
  116. _exit (1);
  117. }
  118. }
  119. free (temp_file_path);
  120. /* Make sure that fork is working. */
  121. pid_t pid = fork ();
  122. if (pid == -1)
  123. {
  124. printf ("error: fork: %m\n");
  125. _exit (1);
  126. }
  127. enum { exit_code = 55 };
  128. if (pid == 0)
  129. _exit (exit_code);
  130. int status;
  131. int ret = waitpid (pid, &status, 0);
  132. if (ret < 0)
  133. {
  134. printf ("error: waitpid: %m\n");
  135. _exit (1);
  136. }
  137. if (!WIFEXITED (status) || WEXITSTATUS (status) != exit_code)
  138. {
  139. printf ("error: unexpected exit status from child process: %d\n",
  140. status);
  141. _exit (1);
  142. }
  143. return NULL;
  144. }
  145. /* This is used to detect if malloc has not been successfully
  146. interposed. The interposed malloc does not use brk/sbrk. */
  147. static void *initial_brk;
  148. __attribute__ ((constructor))
  149. static void
  150. set_initial_brk (void)
  151. {
  152. initial_brk = sbrk (0);
  153. }
  154. /* Terminate the process if the break value has been changed. */
  155. __attribute__ ((destructor))
  156. static void
  157. check_brk (void)
  158. {
  159. void *current = sbrk (0);
  160. if (current != initial_brk)
  161. {
  162. printf ("error: brk changed from %p to %p; no interposition?\n",
  163. initial_brk, current);
  164. _exit (1);
  165. }
  166. }
  167. static int
  168. do_test (void)
  169. {
  170. check_brk ();
  171. #if INTERPOSE_THREADS
  172. pthread_t thr = xpthread_create (NULL, run_tests, NULL);
  173. xpthread_join (thr);
  174. #else
  175. run_tests (NULL);
  176. #endif
  177. check_brk ();
  178. return 0;
  179. }