tst-setcontext3.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* Bug 18125: Verify setcontext calls exit() and not _exit().
  2. Copyright (C) 2015-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 <errno.h>
  16. #include <signal.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <ucontext.h>
  21. #include <unistd.h>
  22. #include <limits.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. /* Please note that depending on the outcome of Bug 18135 this test
  27. may become invalid, and instead of testing for calling exit it
  28. should be reworked to test for the last context calling
  29. pthread_exit(). */
  30. static ucontext_t ctx;
  31. static char *filename;
  32. /* It is intended that this function does nothing. */
  33. static void
  34. cf (void)
  35. {
  36. printf ("called context function\n");
  37. }
  38. static void
  39. exit_called (void)
  40. {
  41. int fd;
  42. ssize_t res;
  43. const char buf[] = "Called exit function\n";
  44. fd = open (filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
  45. if (fd == -1)
  46. {
  47. printf ("FAIL: Unable to create test file %s\n", filename);
  48. exit (1);
  49. }
  50. res = write (fd, buf, sizeof (buf));
  51. if (res != sizeof (buf))
  52. {
  53. printf ("FAIL: Expected to write test file in one write call.\n");
  54. exit (1);
  55. }
  56. res = close (fd);
  57. if (res == -1)
  58. {
  59. printf ("FAIL: Failed to close test file.\n");
  60. exit (1);
  61. }
  62. printf ("PASS: %s", buf);
  63. }
  64. /* The test expects a filename given by the wrapper calling script.
  65. The test then registers an atexit handler that will create the
  66. file to indicate that the atexit handler ran. Then the test
  67. creates a context, modifies it with makecontext, and sets it.
  68. The context has only a single context which then must exit.
  69. If it incorrectly exits via _exit then the atexit handler is
  70. not run, the file is not created, and the wrapper detects this
  71. and fails the test. This test cannot be done using an _exit
  72. interposer since setcontext avoids the PLT and calls _exit
  73. directly. */
  74. static int
  75. do_test (int argc, char **argv)
  76. {
  77. int ret;
  78. char st1[32768];
  79. ucontext_t tempctx = ctx;
  80. if (argc < 2)
  81. {
  82. printf ("FAIL: Test missing filename argument.\n");
  83. exit (1);
  84. }
  85. filename = argv[1];
  86. atexit (exit_called);
  87. puts ("making contexts");
  88. if (getcontext (&ctx) != 0)
  89. {
  90. if (errno == ENOSYS)
  91. {
  92. /* Exit with 77 to mark the test as UNSUPPORTED. */
  93. printf ("UNSUPPORTED: getcontext not implemented.\n");
  94. exit (77);
  95. }
  96. printf ("FAIL: getcontext failed.\n");
  97. exit (1);
  98. }
  99. ctx.uc_stack.ss_sp = st1;
  100. ctx.uc_stack.ss_size = sizeof (st1);
  101. ctx.uc_link = 0;
  102. makecontext (&ctx, cf, 0);
  103. /* Without this check, a stub makecontext can make us spin forever. */
  104. if (memcmp (&tempctx, &ctx, sizeof ctx) == 0)
  105. {
  106. puts ("UNSUPPORTED: makecontext was a no-op, presuming not implemented");
  107. exit (77);
  108. }
  109. ret = setcontext (&ctx);
  110. if (ret != 0)
  111. {
  112. printf ("FAIL: setcontext returned with %d and errno of %d.\n", ret, errno);
  113. exit (1);
  114. }
  115. printf ("FAIL: Impossibly returned to main.\n");
  116. exit (1);
  117. }
  118. #include "../test-skeleton.c"