tst-makecontext-align.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* Check stack alignment provided by 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 <stdint.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <support/check.h>
  19. #include <support/namespace.h>
  20. #include <support/xunistd.h>
  21. #include <sys/mman.h>
  22. #include <ucontext.h>
  23. /* Used for error reporting. */
  24. static const char *context;
  25. /* Check that ADDRESS is aligned to ALIGNMENT bytes, behind a compiler
  26. barrier. */
  27. __attribute__ ((noinline, noclone, weak))
  28. void
  29. check_align (void *address, size_t alignment)
  30. {
  31. uintptr_t uaddress = (uintptr_t) address;
  32. if ((uaddress % alignment) != 0)
  33. {
  34. support_record_failure ();
  35. printf ("error: %s: object at address %p is not aligned to %zu bytes\n",
  36. context, address, alignment);
  37. }
  38. }
  39. /* Various alignment checking functions. */
  40. __attribute__ ((noinline, noclone, weak))
  41. void
  42. check_align_int (void)
  43. {
  44. int a;
  45. check_align (&a, __alignof__ (a));
  46. }
  47. __attribute__ ((noinline, noclone, weak))
  48. void
  49. check_align_long (void)
  50. {
  51. long a;
  52. check_align (&a, __alignof__ (a));
  53. }
  54. __attribute__ ((noinline, noclone, weak))
  55. void
  56. check_align_long_long (void)
  57. {
  58. long long a;
  59. check_align (&a, __alignof__ (a));
  60. }
  61. __attribute__ ((noinline, noclone, weak))
  62. void
  63. check_align_double (void)
  64. {
  65. double a;
  66. check_align (&a, __alignof__ (a));
  67. }
  68. __attribute__ ((noinline, noclone, weak))
  69. void
  70. check_align_4 (void)
  71. {
  72. int a __attribute__ ((aligned (4)));
  73. check_align (&a, 4);
  74. }
  75. __attribute__ ((noinline, noclone, weak))
  76. void
  77. check_align_8 (void)
  78. {
  79. double a __attribute__ ((aligned (8)));
  80. check_align (&a, 8);
  81. }
  82. __attribute__ ((noinline, noclone, weak))
  83. void
  84. check_align_16 (void)
  85. {
  86. struct aligned
  87. {
  88. double x0 __attribute__ ((aligned (16)));
  89. double x1;
  90. } a;
  91. check_align (&a, 16);
  92. }
  93. __attribute__ ((noinline, noclone, weak))
  94. void
  95. check_align_32 (void)
  96. {
  97. struct aligned
  98. {
  99. double x0 __attribute__ ((aligned (32)));
  100. double x1;
  101. double x2;
  102. double x3;
  103. } a;
  104. check_align (&a, 32);
  105. }
  106. /* Call all the alignment checking functions. */
  107. __attribute__ ((noinline, noclone, weak))
  108. void
  109. check_alignments (void)
  110. {
  111. check_align_int ();
  112. check_align_long ();
  113. check_align_long_long ();
  114. check_align_double ();
  115. check_align_4 ();
  116. check_align_8 ();
  117. check_align_16 ();
  118. check_align_32 ();
  119. }
  120. /* Callback functions for makecontext and their invokers (to be used
  121. with support_isolate_in_subprocess). */
  122. static ucontext_t ucp;
  123. static void
  124. callback_0 (void)
  125. {
  126. context = "callback_0";
  127. check_alignments ();
  128. context = "after return from callback_0";
  129. }
  130. static void
  131. invoke_callback_0 (void *closure)
  132. {
  133. makecontext (&ucp, (void *) callback_0, 0);
  134. if (setcontext (&ucp) != 0)
  135. FAIL_EXIT1 ("setcontext");
  136. FAIL_EXIT1 ("setcontext returned");
  137. }
  138. static void
  139. callback_1 (int arg1)
  140. {
  141. context = "callback_1";
  142. check_alignments ();
  143. TEST_COMPARE (arg1, 101);
  144. context = "after return from callback_1";
  145. }
  146. static void
  147. invoke_callback_1 (void *closure)
  148. {
  149. makecontext (&ucp, (void *) callback_1, 1, 101);
  150. if (setcontext (&ucp) != 0)
  151. FAIL_EXIT1 ("setcontext");
  152. FAIL_EXIT1 ("setcontext returned");
  153. }
  154. static void
  155. callback_2 (int arg1, int arg2)
  156. {
  157. context = "callback_2";
  158. check_alignments ();
  159. TEST_COMPARE (arg1, 201);
  160. TEST_COMPARE (arg2, 202);
  161. context = "after return from callback_2";
  162. }
  163. static void
  164. invoke_callback_2 (void *closure)
  165. {
  166. makecontext (&ucp, (void *) callback_2, 2, 201, 202);
  167. if (setcontext (&ucp) != 0)
  168. FAIL_EXIT1 ("setcontext");
  169. FAIL_EXIT1 ("setcontext returned");
  170. }
  171. static void
  172. callback_3 (int arg1, int arg2, int arg3)
  173. {
  174. context = "callback_3";
  175. check_alignments ();
  176. TEST_COMPARE (arg1, 301);
  177. TEST_COMPARE (arg2, 302);
  178. TEST_COMPARE (arg3, 303);
  179. context = "after return from callback_3";
  180. }
  181. static void
  182. invoke_callback_3 (void *closure)
  183. {
  184. makecontext (&ucp, (void *) callback_3, 3, 301, 302, 303);
  185. if (setcontext (&ucp) != 0)
  186. FAIL_EXIT1 ("setcontext");
  187. FAIL_EXIT1 ("setcontext returned");
  188. }
  189. static int
  190. do_test (void)
  191. {
  192. context = "direct call";
  193. check_alignments ();
  194. atexit (check_alignments);
  195. if (getcontext (&ucp) != 0)
  196. FAIL_UNSUPPORTED ("getcontext");
  197. ucp.uc_link = NULL;
  198. ucp.uc_stack.ss_size = 512 * 1024;
  199. ucp.uc_stack.ss_sp = xmmap (NULL, ucp.uc_stack.ss_size,
  200. PROT_READ | PROT_WRITE,
  201. MAP_PRIVATE | MAP_ANONYMOUS, -1);
  202. support_isolate_in_subprocess (invoke_callback_0, NULL);
  203. support_isolate_in_subprocess (invoke_callback_1, NULL);
  204. support_isolate_in_subprocess (invoke_callback_2, NULL);
  205. support_isolate_in_subprocess (invoke_callback_3, NULL);
  206. return 0;
  207. }
  208. #include <support/test-driver.c>