tst-makecontext2.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* Copyright (C) 2008-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 <stdlib.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <ucontext.h>
  19. #include <tst-stack-align.h>
  20. ucontext_t ucp, ucp2;
  21. char st1[262144] __attribute__((aligned (16)));
  22. void
  23. cf (int i, int j)
  24. {
  25. if (i != 78 || j != 274)
  26. {
  27. printf ("i %d j %d\n", i, j);
  28. exit (1);
  29. }
  30. else if (TEST_STACK_ALIGN ())
  31. {
  32. puts ("insufficiently aligned stack");
  33. exit (2);
  34. }
  35. }
  36. int
  37. do_test (void)
  38. {
  39. for (size_t j = 32; j < 64; j += sizeof (long))
  40. {
  41. if (getcontext (&ucp) != 0)
  42. {
  43. if (errno == ENOSYS)
  44. {
  45. puts ("context handling not supported");
  46. return 0;
  47. }
  48. puts ("getcontext failed");
  49. return 1;
  50. }
  51. ucp.uc_link = &ucp2;
  52. ucp.uc_stack.ss_sp = st1;
  53. ucp.uc_stack.ss_size = sizeof (st1) - j;
  54. memset (&st1[sizeof (st1) - j], 0x55, j);
  55. makecontext (&ucp, (void (*) (void)) cf, 2, 78, 274);
  56. if (swapcontext (&ucp2, &ucp) != 0)
  57. {
  58. puts ("setcontext failed");
  59. return 1;
  60. }
  61. for (size_t i = j; i > 0; i--)
  62. if (st1[sizeof (st1) - j + i - 1] != 0x55)
  63. { printf ("fail %zd %zd\n", i, j); break; }
  64. }
  65. return 0;
  66. }
  67. #define TEST_FUNCTION do_test ()
  68. #include "../test-skeleton.c"