tst-align-clone.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Copyright (C) 2004-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 <sched.h>
  15. #include <stdbool.h>
  16. #include <stdint.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <sys/wait.h>
  20. #include <unistd.h>
  21. #include <tst-stack-align.h>
  22. #include <stackinfo.h>
  23. static int
  24. f (void *arg)
  25. {
  26. bool ok = true;
  27. puts ("in f");
  28. if (TEST_STACK_ALIGN ())
  29. ok = false;
  30. return ok ? 0 : 1;
  31. }
  32. static int
  33. do_test (void)
  34. {
  35. bool ok = true;
  36. puts ("in main");
  37. if (TEST_STACK_ALIGN ())
  38. ok = false;
  39. #ifdef __ia64__
  40. extern int __clone2 (int (*__fn) (void *__arg), void *__child_stack_base,
  41. size_t __child_stack_size, int __flags,
  42. void *__arg, ...);
  43. char st[256 * 1024];
  44. pid_t p = __clone2 (f, st, sizeof (st), 0, 0);
  45. #else
  46. char st[128 * 1024] __attribute__ ((aligned));
  47. # if _STACK_GROWS_DOWN
  48. pid_t p = clone (f, st + sizeof (st), 0, 0);
  49. # elif _STACK_GROWS_UP
  50. pid_t p = clone (f, st, 0, 0);
  51. # else
  52. # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
  53. # endif
  54. #endif
  55. if (p == -1)
  56. {
  57. printf("clone failed: %m\n");
  58. return 1;
  59. }
  60. int e;
  61. if (waitpid (p, &e, __WCLONE) != p)
  62. {
  63. puts ("waitpid failed");
  64. kill (p, SIGKILL);
  65. return 1;
  66. }
  67. if (!WIFEXITED (e))
  68. {
  69. if (WIFSIGNALED (e))
  70. printf ("died from signal %s\n", strsignal (WTERMSIG (e)));
  71. else
  72. puts ("did not terminate correctly");
  73. return 1;
  74. }
  75. if (WEXITSTATUS (e) != 0)
  76. ok = false;
  77. return ok ? 0 : 1;
  78. }
  79. #define TEST_FUNCTION do_test ()
  80. #include "../test-skeleton.c"