tst-xbzero-opt.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /* Test that explicit_bzero block clears are not optimized out.
  2. Copyright (C) 2016-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. /* This test is conceptually based on a test designed by Matthew
  16. Dempsky for the OpenBSD regression suite:
  17. <openbsd>/src/regress/lib/libc/explicit_bzero/explicit_bzero.c.
  18. The basic idea is, we have a function that contains a
  19. block-clearing operation (not necessarily explicit_bzero), after
  20. which the block is dead, in the compiler-jargon sense. Execute
  21. that function while running on a user-allocated alternative
  22. stack. Then we have another pointer to the memory region affected
  23. by the block clear -- namely, the original allocation for the
  24. alternative stack -- and can find out whether it actually happened.
  25. The OpenBSD test uses sigaltstack and SIGUSR1 to get onto an
  26. alternative stack. This causes a number of awkward problems; some
  27. operating systems (e.g. Solaris and OSX) wipe the signal stack upon
  28. returning to the normal stack, there's no way to be sure that other
  29. processes running on the same system will not interfere, and the
  30. signal stack is very small so it's not safe to call printf there.
  31. This implementation instead uses the <ucontext.h> coroutine
  32. interface. The coroutine stack is still too small to safely use
  33. printf, but we know the OS won't erase it, so we can do all the
  34. checks and printing from the normal stack. */
  35. #define _GNU_SOURCE 1
  36. #include <errno.h>
  37. #include <signal.h>
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <ucontext.h>
  42. #include <unistd.h>
  43. /* A byte pattern that is unlikely to occur by chance: the first 16
  44. prime numbers (OEIS A000040). */
  45. static const unsigned char test_pattern[16] =
  46. {
  47. 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53
  48. };
  49. /* Immediately after each subtest returns, we call swapcontext to get
  50. back onto the main stack. That call might itself overwrite the
  51. test pattern, so we fill a modest-sized buffer with copies of it
  52. and check whether any of them survived. */
  53. #define PATTERN_SIZE (sizeof test_pattern)
  54. #define PATTERN_REPS 32
  55. #define TEST_BUFFER_SIZE (PATTERN_SIZE * PATTERN_REPS)
  56. /* There are three subtests, two of which are sanity checks.
  57. Each test follows this sequence:
  58. main coroutine
  59. ---- --------
  60. advance cur_subtest
  61. swap
  62. call setup function
  63. prepare test buffer
  64. swap
  65. verify that buffer
  66. was filled in
  67. swap
  68. possibly clear buffer
  69. return
  70. swap
  71. check buffer again,
  72. according to test
  73. expectation
  74. In the "no_clear" case, we don't do anything to the test buffer
  75. between preparing it and letting it go out of scope, and we expect
  76. to find it. This confirms that the test buffer does get filled in
  77. and we can find it from the stack buffer. In the "ordinary_clear"
  78. case, we clear it using memset. Depending on the target, the
  79. compiler may not be able to apply dead store elimination to the
  80. memset call, so the test does not fail if the memset is not
  81. eliminated. Finally, the "explicit_clear" case uses explicit_bzero
  82. and expects _not_ to find the test buffer, which is the real
  83. test. */
  84. static ucontext_t uc_main, uc_co;
  85. static __attribute__ ((noinline, noclone)) int
  86. use_test_buffer (unsigned char *buf)
  87. {
  88. unsigned int sum = 0;
  89. for (unsigned int i = 0; i < PATTERN_REPS; i++)
  90. sum += buf[i * PATTERN_SIZE];
  91. return (sum == 2 * PATTERN_REPS) ? 0 : 1;
  92. }
  93. /* Always check the test buffer immediately after filling it; this
  94. makes externally visible side effects depend on the buffer existing
  95. and having been filled in. */
  96. #if defined __CET__ && !__glibc_has_attribute (__indirect_return__)
  97. /* Note: swapcontext returns via indirect branch when SHSTK is enabled.
  98. Without indirect_return attribute, swapcontext is marked with
  99. returns_twice attribute, which prevents always_inline to work. */
  100. # define ALWAYS_INLINE
  101. #else
  102. # define ALWAYS_INLINE __attribute__ ((always_inline))
  103. #endif
  104. static inline ALWAYS_INLINE void
  105. prepare_test_buffer (unsigned char *buf)
  106. {
  107. for (unsigned int i = 0; i < PATTERN_REPS; i++)
  108. memcpy (buf + i*PATTERN_SIZE, test_pattern, PATTERN_SIZE);
  109. if (swapcontext (&uc_co, &uc_main))
  110. abort ();
  111. /* Force the compiler to really copy the pattern to buf. */
  112. if (use_test_buffer (buf))
  113. abort ();
  114. }
  115. static void
  116. setup_no_clear (void)
  117. {
  118. unsigned char buf[TEST_BUFFER_SIZE];
  119. prepare_test_buffer (buf);
  120. }
  121. static void
  122. setup_ordinary_clear (void)
  123. {
  124. unsigned char buf[TEST_BUFFER_SIZE];
  125. prepare_test_buffer (buf);
  126. memset (buf, 0, TEST_BUFFER_SIZE);
  127. }
  128. static void
  129. setup_explicit_clear (void)
  130. {
  131. unsigned char buf[TEST_BUFFER_SIZE];
  132. prepare_test_buffer (buf);
  133. explicit_bzero (buf, TEST_BUFFER_SIZE);
  134. }
  135. enum test_expectation
  136. {
  137. EXPECT_NONE, EXPECT_SOME, EXPECT_ALL, NO_EXPECTATIONS
  138. };
  139. struct subtest
  140. {
  141. void (*setup_subtest) (void);
  142. const char *label;
  143. enum test_expectation expected;
  144. };
  145. static const struct subtest *cur_subtest;
  146. static const struct subtest subtests[] =
  147. {
  148. { setup_no_clear, "no clear", EXPECT_SOME },
  149. /* The memset may happen or not, depending on compiler
  150. optimizations. */
  151. { setup_ordinary_clear, "ordinary clear", NO_EXPECTATIONS },
  152. { setup_explicit_clear, "explicit clear", EXPECT_NONE },
  153. { 0, 0, -1 }
  154. };
  155. static void
  156. test_coroutine (void)
  157. {
  158. while (cur_subtest->setup_subtest)
  159. {
  160. cur_subtest->setup_subtest ();
  161. if (swapcontext (&uc_co, &uc_main))
  162. abort ();
  163. }
  164. }
  165. /* All the code above this point runs on the coroutine stack.
  166. All the code below this point runs on the main stack. */
  167. static int test_status;
  168. static unsigned char *co_stack_buffer;
  169. static size_t co_stack_size;
  170. static unsigned int
  171. count_test_patterns (unsigned char *buf, size_t bufsiz)
  172. {
  173. unsigned char *first = memmem (buf, bufsiz, test_pattern, PATTERN_SIZE);
  174. if (!first)
  175. return 0;
  176. unsigned int cnt = 0;
  177. for (unsigned int i = 0; i < PATTERN_REPS; i++)
  178. {
  179. unsigned char *p = first + i*PATTERN_SIZE;
  180. if (p + PATTERN_SIZE - buf > bufsiz)
  181. break;
  182. if (memcmp (p, test_pattern, PATTERN_SIZE) == 0)
  183. cnt++;
  184. }
  185. return cnt;
  186. }
  187. static void
  188. check_test_buffer (enum test_expectation expected,
  189. const char *label, const char *stage)
  190. {
  191. unsigned int cnt = count_test_patterns (co_stack_buffer, co_stack_size);
  192. switch (expected)
  193. {
  194. case EXPECT_NONE:
  195. if (cnt == 0)
  196. printf ("PASS: %s/%s: expected 0 got %d\n", label, stage, cnt);
  197. else
  198. {
  199. printf ("FAIL: %s/%s: expected 0 got %d\n", label, stage, cnt);
  200. test_status = 1;
  201. }
  202. break;
  203. case EXPECT_SOME:
  204. if (cnt > 0)
  205. printf ("PASS: %s/%s: expected some got %d\n", label, stage, cnt);
  206. else
  207. {
  208. printf ("FAIL: %s/%s: expected some got 0\n", label, stage);
  209. test_status = 1;
  210. }
  211. break;
  212. case EXPECT_ALL:
  213. if (cnt == PATTERN_REPS)
  214. printf ("PASS: %s/%s: expected %d got %d\n", label, stage,
  215. PATTERN_REPS, cnt);
  216. else
  217. {
  218. printf ("FAIL: %s/%s: expected %d got %d\n", label, stage,
  219. PATTERN_REPS, cnt);
  220. test_status = 1;
  221. }
  222. break;
  223. case NO_EXPECTATIONS:
  224. printf ("INFO: %s/%s: found %d patterns%s\n", label, stage, cnt,
  225. cnt == 0 ? " (memset not eliminated)" : "");
  226. break;
  227. default:
  228. printf ("ERROR: %s/%s: invalid value for 'expected' = %d\n",
  229. label, stage, (int)expected);
  230. test_status = 1;
  231. }
  232. }
  233. static void
  234. test_loop (void)
  235. {
  236. cur_subtest = subtests;
  237. while (cur_subtest->setup_subtest)
  238. {
  239. if (swapcontext (&uc_main, &uc_co))
  240. abort ();
  241. check_test_buffer (EXPECT_ALL, cur_subtest->label, "prepare");
  242. if (swapcontext (&uc_main, &uc_co))
  243. abort ();
  244. check_test_buffer (cur_subtest->expected, cur_subtest->label, "test");
  245. cur_subtest++;
  246. }
  247. /* Terminate the coroutine. */
  248. if (swapcontext (&uc_main, &uc_co))
  249. abort ();
  250. }
  251. int
  252. do_test (void)
  253. {
  254. size_t page_alignment = sysconf (_SC_PAGESIZE);
  255. if (page_alignment < sizeof (void *))
  256. page_alignment = sizeof (void *);
  257. co_stack_size = SIGSTKSZ + TEST_BUFFER_SIZE;
  258. if (co_stack_size < page_alignment * 4)
  259. co_stack_size = page_alignment * 4;
  260. void *p;
  261. int err = posix_memalign (&p, page_alignment, co_stack_size);
  262. if (err || !p)
  263. {
  264. printf ("ERROR: allocating alt stack: %s\n", strerror (err));
  265. return 2;
  266. }
  267. co_stack_buffer = p;
  268. if (getcontext (&uc_co))
  269. {
  270. printf ("ERROR: allocating coroutine context: %s\n", strerror (err));
  271. return 2;
  272. }
  273. uc_co.uc_stack.ss_sp = co_stack_buffer;
  274. uc_co.uc_stack.ss_size = co_stack_size;
  275. uc_co.uc_link = &uc_main;
  276. makecontext (&uc_co, test_coroutine, 0);
  277. test_loop ();
  278. return test_status;
  279. }
  280. #include <support/test-driver.c>