tst-chk-cancel.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* Test for required cancellation points in fortified functions (BZ #29274)
  2. Copyright (C) 2022-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
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the 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; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <array_length.h>
  16. #include <errno.h>
  17. #include <poll.h>
  18. #include <stdbool.h>
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <support/check.h>
  22. #include <support/xthread.h>
  23. #include <support/xunistd.h>
  24. #include <sys/socket.h>
  25. /* Cleanup handling test. */
  26. static int cl_called;
  27. static void
  28. cl (void *arg)
  29. {
  30. ++cl_called;
  31. }
  32. static int fds[2];
  33. static pthread_barrier_t barrier;
  34. static void *
  35. tf_read (void *n)
  36. {
  37. pthread_cleanup_push (cl, NULL);
  38. xpthread_barrier_wait (&barrier);
  39. /* This call should be forwarded to __read_chk because the buffer size
  40. is known, but the read length is non-constant. */
  41. char c;
  42. if (read (fds[0], &c, (uintptr_t) n) != 1)
  43. return (void *) -1L;
  44. pthread_cleanup_pop (0);
  45. return 0;
  46. }
  47. static void *
  48. tf_pread (void *n)
  49. {
  50. pthread_cleanup_push (cl, NULL);
  51. xpthread_barrier_wait (&barrier);
  52. /* This call should be forwarded to __pread_chk because the buffer size
  53. is known, but the read length is non-constant. */
  54. char c;
  55. if (pread (fds[0], &c, (uintptr_t) n, 0) != 1)
  56. return (void *) -1L;
  57. pthread_cleanup_pop (0);
  58. return 0;
  59. }
  60. static void *
  61. tf_pread64 (void *n)
  62. {
  63. pthread_cleanup_push (cl, NULL);
  64. xpthread_barrier_wait (&barrier);
  65. /* This call should be forwarded to __pread64_chk because the buffer size
  66. is known, but the read length is non-constant. */
  67. char c;
  68. if (pread64 (fds[0], &c, (uintptr_t) n, 0) != 1)
  69. return (void *) -1L;
  70. pthread_cleanup_pop (0);
  71. return 0;
  72. }
  73. static void *
  74. tf_poll (void *n)
  75. {
  76. pthread_cleanup_push (cl, NULL);
  77. xpthread_barrier_wait (&barrier);
  78. /* This call should be forwarded to __poll_chk because the pollfd size
  79. is known, but the number of entries is non-constant. */
  80. struct pollfd pfd = { fds[0], POLLIN, 0 };
  81. if (poll (&pfd, (uintptr_t) n, -1) != 1)
  82. return (void *) -1L;
  83. pthread_cleanup_pop (0);
  84. return 0;
  85. }
  86. static void *
  87. tf_ppoll (void *n)
  88. {
  89. pthread_cleanup_push (cl, NULL);
  90. xpthread_barrier_wait (&barrier);
  91. /* This call should be forwarded to __ppoll_chk because the pollfd size
  92. is known, but the number of entries is non-constant. */
  93. struct pollfd pfd = { fds[0], POLLIN, 0 };
  94. if (ppoll (&pfd, (uintptr_t) n, 0, 0) != 1)
  95. return (void *) -1L;
  96. pthread_cleanup_pop (0);
  97. return 0;
  98. }
  99. static void *
  100. tf_recv (void *n)
  101. {
  102. pthread_cleanup_push (cl, NULL);
  103. xpthread_barrier_wait (&barrier);
  104. /* This call should be forwarded to __ppoll_chk because the pollfd size
  105. is known, but the number of entries is non-constant. */
  106. char c;
  107. if (recv (fds[0], &c, (uintptr_t) n, 0) != 1)
  108. return (void *) -1L;
  109. pthread_cleanup_pop (0);
  110. return 0;
  111. }
  112. static void *
  113. tf_recvfrom (void *n)
  114. {
  115. pthread_cleanup_push (cl, NULL);
  116. xpthread_barrier_wait (&barrier);
  117. /* This call should be forwarded to __ppoll_chk because the pollfd size
  118. is known, but the number of entries is non-constant. */
  119. char c;
  120. if (recvfrom (fds[0], &c, (uintptr_t) n, 0, NULL, NULL) != 1)
  121. return (void *) -1L;
  122. pthread_cleanup_pop (0);
  123. return 0;
  124. }
  125. static struct cancel_tests
  126. {
  127. const char *name;
  128. void *(*tf) (void *);
  129. bool only_early;
  130. #define ADD_TEST(name, early) { #name, tf_##name, early }
  131. } tests[] =
  132. {
  133. ADD_TEST (poll, false),
  134. ADD_TEST (ppoll, false),
  135. ADD_TEST (pread, true),
  136. ADD_TEST (pread64, true),
  137. ADD_TEST (read, false),
  138. ADD_TEST (recv, false),
  139. ADD_TEST (recvfrom, false),
  140. };
  141. /* Set the send buffer of socket S to 1 byte so any send operation
  142. done with WRITE_BUFFER_SIZE bytes will force syscall blocking. */
  143. static void
  144. set_socket_buffer (int s)
  145. {
  146. int val = 1;
  147. socklen_t len = sizeof (val);
  148. TEST_VERIFY_EXIT (setsockopt (s, SOL_SOCKET, SO_SNDBUF, &val,
  149. sizeof (val)) == 0);
  150. TEST_VERIFY_EXIT (getsockopt (s, SOL_SOCKET, SO_SNDBUF, &val, &len) == 0);
  151. printf ("%s: got size %d\n", __func__, val);
  152. }
  153. static int
  154. do_test (void)
  155. {
  156. xpthread_barrier_init (&barrier, 0, 2);
  157. if (socketpair (AF_UNIX, SOCK_STREAM, 0, fds) != 0)
  158. FAIL_EXIT1 ("socketpair: %m");
  159. set_socket_buffer (fds[1]);
  160. /* This is the !only_early test. It is a late cancel test that has a sleep
  161. in the main thread in an attempt to allow the child thread to reach and
  162. block on the syscall. The cancellation should happen with high
  163. probability when the child thread blocked on the syscall, and that is
  164. the intent of the test (syscall cancellation registration complete). */
  165. for (int i = 0; i < array_length (tests); i++)
  166. {
  167. if (tests[i].only_early)
  168. continue;
  169. xpthread_barrier_init (&barrier, NULL, 2);
  170. /* Reset the counter for the cleanup handler. */
  171. cl_called = 0;
  172. pthread_t thr = xpthread_create (0, tests[i].tf, (void *) 1L);
  173. /* After this wait the threads cancellation handler is installed. */
  174. xpthread_barrier_wait (&barrier);
  175. struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
  176. TEMP_FAILURE_RETRY (clock_nanosleep (CLOCK_REALTIME, 0, &ts, &ts));
  177. xpthread_cancel (thr);
  178. void *status = xpthread_join (thr);
  179. TEST_VERIFY (status == PTHREAD_CANCELED);
  180. TEST_COMPARE (cl_called, 1);
  181. printf ("in-time cancel test of '%s' successful\n", tests[i].name);
  182. }
  183. /* This is a early cancel test that happens before the syscall is issued.
  184. In this case there is no signal involved, pthread_cancel will just mark
  185. the target thread canceled, since asynchronous mode is not set, and the
  186. cancellable entrypoint will check if the thread is set as cancelled and
  187. exit early.
  188. Keep in mind that neither pthread_barrier_wait nor pthread_cleanup_push
  189. act as cancellation entrypoints. */
  190. for (int i = 0; i < array_length (tests); i++)
  191. {
  192. xpthread_barrier_init (&barrier, NULL, 2);
  193. /* Reset the counter for the cleanup handler. */
  194. cl_called = 0;
  195. /* After this wait the cancellation handler is in place. */
  196. pthread_t thr = xpthread_create (0, tests[i].tf, NULL);
  197. xpthread_cancel (thr);
  198. xpthread_barrier_wait (&barrier);
  199. void *status = xpthread_join (thr);
  200. TEST_VERIFY (status == PTHREAD_CANCELED);
  201. TEST_COMPARE (cl_called, 1);
  202. printf ("early cancel test of '%s' successful\n", tests[i].name);
  203. }
  204. xpthread_barrier_destroy (&barrier);
  205. return 0;
  206. }
  207. #include <support/test-driver.c>