tst-setcontext8.c 2.1 KB

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