tst-context1.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* Copyright (C) 2003-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
  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. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <error.h>
  17. #include <limits.h>
  18. #include <pthread.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <ucontext.h>
  22. #define N 4
  23. #if __WORDSIZE == 64
  24. #define GUARD_PATTERN 0xdeadbeafdeadbeaf
  25. #else
  26. #define GUARD_PATTERN 0xdeadbeaf
  27. #endif
  28. typedef struct {
  29. ucontext_t uctx;
  30. unsigned long guard[3];
  31. } tst_context_t;
  32. static char stacks[N][2 * PTHREAD_STACK_MIN];
  33. static tst_context_t ctx[N][2];
  34. static volatile int failures;
  35. static void
  36. fct (long int n)
  37. {
  38. char on_stack[1];
  39. /* Just to use the thread local descriptor. */
  40. printf ("%ld: in %s now, on_stack = %p\n", n, __FUNCTION__, on_stack);
  41. errno = 0;
  42. if (ctx[n][1].uctx.uc_link != &ctx[n][0].uctx)
  43. {
  44. printf ("context[%ld][1] uc_link damaged, = %p\n", n,
  45. ctx[n][1].uctx.uc_link);
  46. exit (1);
  47. }
  48. if ((ctx[n][0].guard[0] != GUARD_PATTERN)
  49. || (ctx[n][0].guard[1] != GUARD_PATTERN)
  50. || (ctx[n][0].guard[2] != GUARD_PATTERN))
  51. {
  52. printf ("%ld: %s context[0] overflow detected!\n", n, __FUNCTION__);
  53. ++failures;
  54. }
  55. if ((ctx[n][1].guard[0] != GUARD_PATTERN)
  56. || (ctx[n][1].guard[1] != GUARD_PATTERN)
  57. || (ctx[n][1].guard[2] != GUARD_PATTERN))
  58. {
  59. printf ("%ld: %s context[1] overflow detected!\n", n, __FUNCTION__);
  60. ++failures;
  61. }
  62. if (n < 0 || n >= N)
  63. {
  64. printf ("%ld out of range\n", n);
  65. exit (1);
  66. }
  67. if (on_stack < stacks[n] || on_stack >= stacks[n] + sizeof (stacks[0]))
  68. {
  69. printf ("%ld: on_stack not on appropriate stack\n", n);
  70. exit (1);
  71. }
  72. }
  73. static void *
  74. tf (void *arg)
  75. {
  76. int n = (int) (long int) arg;
  77. ctx[n][0].guard[0] = GUARD_PATTERN;
  78. ctx[n][0].guard[1] = GUARD_PATTERN;
  79. ctx[n][0].guard[2] = GUARD_PATTERN;
  80. ctx[n][1].guard[0] = GUARD_PATTERN;
  81. ctx[n][1].guard[1] = GUARD_PATTERN;
  82. ctx[n][1].guard[2] = GUARD_PATTERN;
  83. if (getcontext (&ctx[n][1].uctx) != 0)
  84. {
  85. printf ("%d: cannot get context: %m\n", n);
  86. exit (1);
  87. }
  88. printf ("%d: %s: before makecontext\n", n, __FUNCTION__);
  89. ctx[n][1].uctx.uc_stack.ss_sp = stacks[n];
  90. ctx[n][1].uctx.uc_stack.ss_size = sizeof (stacks[n]);
  91. ctx[n][1].uctx.uc_link = &ctx[n][0].uctx;
  92. makecontext (&ctx[n][1].uctx, (void (*) (void)) fct, 1, (long int) n);
  93. printf ("%d: %s: before swapcontext\n", n, __FUNCTION__);
  94. if (swapcontext (&ctx[n][0].uctx, &ctx[n][1].uctx) != 0)
  95. {
  96. ++failures;
  97. printf ("%d: %s: swapcontext failed\n", n, __FUNCTION__);
  98. }
  99. else
  100. printf ("%d: back in %s\n", n, __FUNCTION__);
  101. return NULL;
  102. }
  103. static volatile int global;
  104. static int
  105. do_test (void)
  106. {
  107. int n;
  108. pthread_t th[N];
  109. ucontext_t mctx;
  110. puts ("making contexts");
  111. if (getcontext (&mctx) != 0)
  112. {
  113. if (errno == ENOSYS)
  114. {
  115. puts ("context handling not supported");
  116. exit (0);
  117. }
  118. printf ("%s: getcontext: %m\n", __FUNCTION__);
  119. exit (1);
  120. }
  121. /* Play some tricks with this context. */
  122. if (++global == 1)
  123. if (setcontext (&mctx) != 0)
  124. {
  125. puts ("setcontext failed");
  126. exit (1);
  127. }
  128. if (global != 2)
  129. {
  130. puts ("global not incremented twice");
  131. exit (1);
  132. }
  133. puts ("global OK");
  134. pthread_attr_t at;
  135. if (pthread_attr_init (&at) != 0)
  136. {
  137. puts ("attr_init failed");
  138. return 1;
  139. }
  140. if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
  141. {
  142. puts ("attr_setstacksize failed");
  143. return 1;
  144. }
  145. for (n = 0; n < N; ++n)
  146. if (pthread_create (&th[n], &at, tf, (void *) (long int) n) != 0)
  147. {
  148. puts ("create failed");
  149. exit (1);
  150. }
  151. if (pthread_attr_destroy (&at) != 0)
  152. {
  153. puts ("attr_destroy failed");
  154. return 1;
  155. }
  156. for (n = 0; n < N; ++n)
  157. if (pthread_join (th[n], NULL) != 0)
  158. {
  159. puts ("join failed");
  160. exit (1);
  161. }
  162. return failures;
  163. }
  164. #define TEST_FUNCTION do_test ()
  165. #include "../test-skeleton.c"