test-sig-rpc-interrupted.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* Test the state save/restore procedures during signal handling when an
  2. interruptible RPC is restarted.
  3. Copyright (C) 2024-2026 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <https://www.gnu.org/licenses/>. */
  16. #include <assert.h>
  17. #include <pthread.h>
  18. #include <signal.h>
  19. #include <stdbool.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <unistd.h>
  24. #include <mach/message.h>
  25. #include <mach/gnumach.h>
  26. #include <mach/mach_traps.h>
  27. #include <mach/mig_errors.h>
  28. #include <mach-shortcuts.h>
  29. #include <mach_init.h>
  30. #include <hurd/io.h>
  31. #include <hurd/io_reply.h>
  32. #include <support/check.h>
  33. #include <support/xthread.h>
  34. #include "test-xstate.h"
  35. void handler (int signum, siginfo_t *info, void *context)
  36. {
  37. printf ("signal %d setting a different CPU state\n", signum);
  38. char mmxbuf3[MMXSTATE_BUFFER_SIZE];
  39. char xbuf3[XSTATE_BUFFER_SIZE];
  40. memset (mmxbuf3, 0x77, MMXSTATE_BUFFER_SIZE);
  41. memset (xbuf3, 0x77, XSTATE_BUFFER_SIZE);
  42. SET_MMXSTATE (mmxbuf3);
  43. SET_XSTATE (xbuf3);
  44. }
  45. static const mach_msg_type_t RetCodeCheck = {
  46. .msgt_name = (unsigned char) MACH_MSG_TYPE_INTEGER_32,
  47. .msgt_size = 32,
  48. .msgt_number = 1,
  49. .msgt_inline = TRUE,
  50. .msgt_longform = FALSE,
  51. .msgt_deallocate = FALSE,
  52. .msgt_unused = 0
  53. };
  54. /* Helper thread to simulate a proper RPC interruption during dignal handling */
  55. void* fake_interruptor (void *arg)
  56. {
  57. int err;
  58. sigset_t ss;
  59. TEST_COMPARE (sigemptyset (&ss), 0);
  60. TEST_COMPARE (sigaddset (&ss, SIGUSR1), 0);
  61. TEST_COMPARE (sigprocmask (SIG_BLOCK, &ss, NULL), 0);
  62. struct {
  63. mach_msg_header_t Head;
  64. } request;
  65. mach_port_t rxport = *((mach_port_t*)arg);
  66. err = mach_msg (&request.Head, MACH_RCV_MSG, 0, sizeof (request), rxport,
  67. MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
  68. TEST_COMPARE (err, MACH_MSG_SUCCESS);
  69. TEST_COMPARE (request.Head.msgh_bits, 0x1112);
  70. TEST_COMPARE (request.Head.msgh_size, sizeof (request.Head));
  71. TEST_COMPARE (request.Head.msgh_id, 33000);
  72. mig_reply_header_t reply;
  73. reply.Head = request.Head;
  74. reply.Head.msgh_id += 100;
  75. reply.RetCodeType = RetCodeCheck;
  76. reply.RetCode = KERN_SUCCESS;
  77. err = mach_msg (&reply.Head, MACH_SEND_MSG, sizeof (reply), 0, MACH_PORT_NULL,
  78. MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
  79. TEST_COMPARE (err, MACH_MSG_SUCCESS);
  80. return NULL;
  81. }
  82. /* Helper thread to send a signal to the main thread in the middle of
  83. * an interruptible rpc */
  84. void* signal_sender (void *arg)
  85. {
  86. int err;
  87. sigset_t ss;
  88. TEST_COMPARE (sigemptyset (&ss), 0);
  89. TEST_COMPARE (sigaddset (&ss, SIGUSR1), 0);
  90. TEST_COMPARE (sigprocmask (SIG_BLOCK, &ss, NULL), 0);
  91. /* Receive the first request, we won't answer to this. */
  92. struct {
  93. mach_msg_header_t head;
  94. char data[64];
  95. } m1, m2;
  96. mach_port_t rxport = *((mach_port_t*)arg);
  97. memset (&m1, 0, sizeof (m1));
  98. memset (&m2, 0, sizeof (m2));
  99. err = mach_msg (&m1.head, MACH_RCV_MSG, 0, sizeof (m1), rxport,
  100. MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
  101. TEST_COMPARE (err, MACH_MSG_SUCCESS);
  102. /* interrupt the ongoing rpc with a signal, using the
  103. * interruptible rpc protocol */
  104. pthread_t thintr = xpthread_create (NULL, fake_interruptor, arg);
  105. TEST_COMPARE (kill (getpid (), SIGUSR1), 0);
  106. xpthread_join (thintr);
  107. /* Complete the interruption by sending EINTR */
  108. mig_reply_header_t reply;
  109. reply.Head = m1.head;
  110. reply.Head.msgh_id += 100;
  111. reply.RetCodeType = RetCodeCheck;
  112. reply.RetCode = EINTR;
  113. err = mach_msg (&reply.Head, MACH_SEND_MSG, sizeof (reply), 0, MACH_PORT_NULL,
  114. MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
  115. TEST_COMPARE (err, MACH_MSG_SUCCESS);
  116. /* Receive the retried rpc, and check that it has the same payload
  117. * as the first one. Port names might still be different. */
  118. err = mach_msg (&m2.head, MACH_RCV_MSG, 0, sizeof (m2), rxport,
  119. MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
  120. TEST_COMPARE (m1.head.msgh_bits, m2.head.msgh_bits);
  121. TEST_COMPARE (m1.head.msgh_size, m2.head.msgh_size);
  122. TEST_COMPARE (m1.head.msgh_id, m2.head.msgh_id);
  123. TEST_COMPARE_BLOB (m1.data, sizeof (m1.data), m2.data, sizeof (m2.data));
  124. /* And finally make the rpc succeed by sending a valid reply */
  125. err = io_read_reply (m2.head.msgh_remote_port, MACH_MSG_TYPE_MOVE_SEND_ONCE,
  126. KERN_SUCCESS, NULL, 0);
  127. TEST_COMPARE (err, MACH_MSG_SUCCESS);
  128. return NULL;
  129. }
  130. static int do_test (void)
  131. {
  132. #if ! XSTATE_HELPERS_SUPPORTED
  133. FAIL_UNSUPPORTED ("Test not supported on this arch.");
  134. #endif
  135. /* Setup signal handling; we need to handle the signal in the main
  136. * thread, the other ones will explicitely block SIGUSR1. */
  137. struct sigaction act = { 0 };
  138. act.sa_flags = SA_RESTART;
  139. act.sa_sigaction = &handler;
  140. TEST_COMPARE (sigaction (SIGUSR1, &act, NULL), 0);
  141. mach_port_t fakeio;
  142. int err;
  143. err = mach_port_allocate (mach_task_self (), MACH_PORT_RIGHT_RECEIVE, &fakeio);
  144. TEST_COMPARE (err, MACH_MSG_SUCCESS);
  145. err = mach_port_insert_right (mach_task_self (), fakeio, fakeio,
  146. MACH_MSG_TYPE_MAKE_SEND);
  147. TEST_COMPARE (err, MACH_MSG_SUCCESS);
  148. pthread_t thsender = xpthread_create (NULL, signal_sender, &fakeio);
  149. char *buf;
  150. mach_msg_type_number_t n;
  151. TEST_COMPARE (io_read (fakeio, &buf, &n, 1, 2), 0);
  152. xpthread_join (thsender);
  153. return EXIT_SUCCESS;
  154. }
  155. #include <support/test-driver.c>