tst-setcontext5.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* Check multiple setcontext calls.
  2. Copyright (C) 2018-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
  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 <stdio.h>
  16. #include <stdlib.h>
  17. #include <ucontext.h>
  18. #include <unistd.h>
  19. static ucontext_t ctx[2];
  20. static volatile int done;
  21. static void f2 (void);
  22. static void
  23. __attribute__ ((noinline, noclone))
  24. f1 (void)
  25. {
  26. printf ("start f1\n");
  27. f2 ();
  28. }
  29. static void
  30. __attribute__ ((noinline, noclone))
  31. f2 (void)
  32. {
  33. printf ("start f2\n");
  34. if (setcontext (&ctx[1]) != 0)
  35. {
  36. printf ("%s: setcontext: %m\n", __FUNCTION__);
  37. exit (EXIT_FAILURE);
  38. }
  39. }
  40. static void
  41. f3 (void)
  42. {
  43. printf ("start f3\n");
  44. if (done)
  45. exit (EXIT_SUCCESS);
  46. done = 1;
  47. if (setcontext (&ctx[0]) != 0)
  48. {
  49. printf ("%s: setcontext: %m\n", __FUNCTION__);
  50. exit (EXIT_FAILURE);
  51. }
  52. }
  53. static int
  54. do_test (void)
  55. {
  56. char st1[32768];
  57. puts ("making contexts");
  58. if (getcontext (&ctx[0]) != 0)
  59. {
  60. printf ("%s: getcontext: %m\n", __FUNCTION__);
  61. exit (EXIT_FAILURE);
  62. }
  63. if (getcontext (&ctx[1]) != 0)
  64. {
  65. printf ("%s: getcontext: %m\n", __FUNCTION__);
  66. exit (EXIT_FAILURE);
  67. }
  68. ctx[1].uc_stack.ss_sp = st1;
  69. ctx[1].uc_stack.ss_size = sizeof st1;
  70. ctx[1].uc_link = &ctx[0];
  71. makecontext (&ctx[1], (void (*) (void)) f3, 0);
  72. f1 ();
  73. puts ("FAIL: returned from f1 ()");
  74. exit (EXIT_FAILURE);
  75. }
  76. #include <support/test-driver.c>