tst-setcontext.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* Copyright (C) 2001-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <signal.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <ucontext.h>
  20. #include <unistd.h>
  21. static ucontext_t ctx[3];
  22. static int was_in_f1;
  23. static int was_in_f2;
  24. static char st2[32768];
  25. static void
  26. f1 (int a0, int a1, int a2, int a3)
  27. {
  28. printf ("start f1(a0=%x,a1=%x,a2=%x,a3=%x)\n", a0, a1, a2, a3);
  29. if (a0 != 1 || a1 != 2 || a2 != 3 || a3 != -4)
  30. {
  31. puts ("arg mismatch");
  32. exit (-1);
  33. }
  34. if (swapcontext (&ctx[1], &ctx[2]) != 0)
  35. {
  36. printf ("%s: swapcontext: %m\n", __FUNCTION__);
  37. exit (1);
  38. }
  39. puts ("finish f1");
  40. was_in_f1 = 1;
  41. }
  42. static void
  43. f2 (void)
  44. {
  45. char on_stack[1];
  46. puts ("start f2");
  47. printf ("&on_stack=%p\n", on_stack);
  48. if (on_stack < st2 || on_stack >= st2 + sizeof (st2))
  49. {
  50. printf ("%s: memory stack is not where it belongs!", __FUNCTION__);
  51. exit (1);
  52. }
  53. if (swapcontext (&ctx[2], &ctx[1]) != 0)
  54. {
  55. printf ("%s: swapcontext: %m\n", __FUNCTION__);
  56. exit (1);
  57. }
  58. puts ("finish f2");
  59. was_in_f2 = 1;
  60. }
  61. void
  62. test_stack (volatile int a, volatile int b,
  63. volatile int c, volatile int d)
  64. {
  65. volatile int e = 5;
  66. volatile int f = 6;
  67. ucontext_t uc;
  68. /* Test for cases where getcontext is clobbering the callers
  69. stack, including parameters. */
  70. getcontext (&uc);
  71. if (a != 1)
  72. {
  73. printf ("%s: getcontext clobbers parm a\n", __FUNCTION__);
  74. exit (1);
  75. }
  76. if (b != 2)
  77. {
  78. printf ("%s: getcontext clobbers parm b\n", __FUNCTION__);
  79. exit (1);
  80. }
  81. if (c != 3)
  82. {
  83. printf ("%s: getcontext clobbers parm c\n", __FUNCTION__);
  84. exit (1);
  85. }
  86. if (d != 4)
  87. {
  88. printf ("%s: getcontext clobbers parm d\n", __FUNCTION__);
  89. exit (1);
  90. }
  91. if (e != 5)
  92. {
  93. printf ("%s: getcontext clobbers varible e\n", __FUNCTION__);
  94. exit (1);
  95. }
  96. if (f != 6)
  97. {
  98. printf ("%s: getcontext clobbers variable f\n", __FUNCTION__);
  99. exit (1);
  100. }
  101. }
  102. volatile int global;
  103. static int back_in_main;
  104. static void
  105. check_called (void)
  106. {
  107. if (back_in_main == 0)
  108. {
  109. puts ("program did not reach main again");
  110. _exit (1);
  111. }
  112. }
  113. int
  114. main (void)
  115. {
  116. atexit (check_called);
  117. char st1[32768];
  118. stack_t stack_before, stack_after;
  119. sigaltstack (NULL, &stack_before);
  120. puts ("making contexts");
  121. if (getcontext (&ctx[1]) != 0)
  122. {
  123. if (errno == ENOSYS)
  124. {
  125. back_in_main = 1;
  126. exit (0);
  127. }
  128. printf ("%s: getcontext: %m\n", __FUNCTION__);
  129. exit (1);
  130. }
  131. test_stack (1, 2, 3, 4);
  132. /* Play some tricks with this context. */
  133. if (++global == 1)
  134. if (setcontext (&ctx[1]) != 0)
  135. {
  136. printf ("%s: setcontext: %m\n", __FUNCTION__);
  137. exit (1);
  138. }
  139. if (global != 2)
  140. {
  141. printf ("%s: 'global' not incremented twice\n", __FUNCTION__);
  142. exit (1);
  143. }
  144. ctx[1].uc_stack.ss_sp = st1;
  145. ctx[1].uc_stack.ss_size = sizeof st1;
  146. ctx[1].uc_link = &ctx[0];
  147. {
  148. ucontext_t tempctx = ctx[1];
  149. makecontext (&ctx[1], (void (*) (void)) f1, 4, 1, 2, 3, -4);
  150. /* Without this check, a stub makecontext can make us spin forever. */
  151. if (memcmp (&tempctx, &ctx[1], sizeof ctx[1]) == 0)
  152. {
  153. puts ("makecontext was a no-op, presuming not implemented");
  154. return 0;
  155. }
  156. }
  157. if (getcontext (&ctx[2]) != 0)
  158. {
  159. printf ("%s: second getcontext: %m\n", __FUNCTION__);
  160. exit (1);
  161. }
  162. ctx[2].uc_stack.ss_sp = st2;
  163. ctx[2].uc_stack.ss_size = sizeof st2;
  164. ctx[2].uc_link = &ctx[1];
  165. makecontext (&ctx[2], f2, 0);
  166. puts ("swapping contexts");
  167. if (swapcontext (&ctx[0], &ctx[2]) != 0)
  168. {
  169. printf ("%s: swapcontext: %m\n", __FUNCTION__);
  170. exit (1);
  171. }
  172. puts ("back at main program");
  173. back_in_main = 1;
  174. sigaltstack (NULL, &stack_after);
  175. if (was_in_f1 == 0)
  176. {
  177. puts ("didn't reach f1");
  178. exit (1);
  179. }
  180. if (was_in_f2 == 0)
  181. {
  182. puts ("didn't reach f2");
  183. exit (1);
  184. }
  185. /* Check sigaltstack state is not clobbered as in BZ #16629. */
  186. if (stack_before.ss_sp != stack_after.ss_sp)
  187. {
  188. printf ("stack ss_sp mismatch: %p %p\n",
  189. stack_before.ss_sp, stack_after.ss_sp);
  190. exit (1);
  191. }
  192. if (stack_before.ss_size != stack_after.ss_size)
  193. {
  194. printf ("stack ss_size mismatch: %zd %zd\n",
  195. stack_before.ss_size, stack_after.ss_size);
  196. exit (1);
  197. }
  198. puts ("test succeeded");
  199. return 0;
  200. }