Debian Codegraph @

test/explicit-bzero.c

raw
1 /* Test that explicit_bzero block clears are not optimized out.
2 Copyright (C) 2016-2020 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 /* This test is conceptually based on a test designed by Matthew
20 Dempsky for the OpenBSD regression suite:
21 <openbsd>/src/regress/lib/libc/explicit_bzero/explicit_bzero.c.
22 The basic idea is, we have a function that contains a
23 block-clearing operation (not necessarily explicit_bzero), after
24 which the block is dead, in the compiler-jargon sense. Execute
25 that function while running on a user-allocated alternative
26 stack. Then we have another pointer to the memory region affected
27 by the block clear -- namely, the original allocation for the
28 alternative stack -- and can find out whether it actually happened.
29
30 The OpenBSD test uses sigaltstack and SIGUSR1 to get onto an
31 alternative stack. This causes a number of awkward problems; some
32 operating systems (e.g. Solaris and OSX) wipe the signal stack upon
33 returning to the normal stack, there's no way to be sure that other
34 processes running on the same system will not interfere, and the
35 signal stack is very small so it's not safe to call printf there.
36 This implementation instead uses the <ucontext.h> coroutine
37 interface. The coroutine stack is still too small to safely use
38 printf, but we know the OS won't erase it, so we can do all the
39 checks and printing from the normal stack. */
40
41 #include "crypt-port.h"
42
43 #if !defined USE_SWAPCONTEXT || defined XCRYPT_USE_ASAN
44 /* We can't do this test if we don't have the ucontext API.
45 Also ASAN may have some problems with using swapcontext(). */
46 int main(void)
47 {
48 return 77;
49 }
50 #else
51
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <ucontext.h>
55
56 #ifdef HAVE_VALGRIND_VALGRIND_H
57 # include <valgrind/valgrind.h>
58 # include <valgrind/memcheck.h>
59 #else
60 # define VALGRIND_STACK_REGISTER(start, end) do {} while (0)
61 # define VALGRIND_MAKE_MEM_DEFINED(addr, len) do {} while (0)
62 #endif
63
64 /* A byte pattern that is unlikely to occur by chance: the first 16
65 prime numbers (OEIS A000040). */
66 static const unsigned char test_pattern[16] =
67 {
68 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53
69 };
70
71 /* Immediately after each subtest returns, we call swapcontext to get
72 back onto the main stack. That call might itself overwrite the
73 test pattern, so we fill a modest-sized buffer with copies of it
74 and check whether any of them survived. */
75
76 #define PATTERN_SIZE (sizeof test_pattern)
77 #define PATTERN_REPS 32
78 #define TEST_BUFFER_SIZE (PATTERN_SIZE * PATTERN_REPS)
79
80 /* There are three subtests, two of which are sanity checks.
81 Each test follows this sequence:
82
83 main coroutine
84 ---- --------
85 advance cur_subtest
86 swap
87 call setup function
88 prepare test buffer
89 swap
90 verify that buffer
91 was filled in
92 swap
93 possibly clear buffer
94 return
95 swap
96 check buffer again,
97 according to test
98 expectation
99
100 In the "no_clear" case, we don't do anything to the test buffer
101 between preparing it and letting it go out of scope, and we expect
102 to find it. This confirms that the test buffer does get filled in
103 and we can find it from the stack buffer. In the "ordinary_clear"
104 case, we clear it using memset. Depending on the target, the
105 compiler may not be able to apply dead store elimination to the
106 memset call, so the test does not fail if the memset is not
107 eliminated. Finally, the "explicit_clear" case uses explicit_bzero
108 and expects _not_ to find the test buffer, which is the real
109 test. */
110
111 static ucontext_t uc_main, uc_co;
112
113 static NO_INLINE int
114 use_test_buffer (unsigned char *buf)
115 {
116 unsigned int sum = 0;
117
118 for (unsigned int i = 0; i < PATTERN_REPS; i++)
119 sum += buf[i * PATTERN_SIZE];
120
121 return (sum == 2 * PATTERN_REPS) ? 0 : 1;
122 }
123
124 /* Always check the test buffer immediately after filling it; this
125 makes externally visible side effects depend on the buffer existing
126 and having been filled in. */
127 static inline void
128 prepare_test_buffer (unsigned char *buf)
129 {
130 for (unsigned int i = 0; i < PATTERN_REPS; i++)
131 memcpy (buf + i*PATTERN_SIZE, test_pattern, PATTERN_SIZE);
132
133 if (swapcontext (&uc_co, &uc_main))
134 abort ();
135
136 /* Force the compiler to really copy the pattern to buf. */
137 if (use_test_buffer (buf))
138 abort ();
139 }
140
141 static void
142 setup_no_clear (void)
143 {
144 unsigned char buf[TEST_BUFFER_SIZE];
145 prepare_test_buffer (buf);
146 }
147
148 static void
149 setup_ordinary_clear (void)
150 {
151 unsigned char buf[TEST_BUFFER_SIZE];
152 prepare_test_buffer (buf);
153 memset (buf, 0, TEST_BUFFER_SIZE);
154 }
155
156 static void
157 setup_explicit_clear (void)
158 {
159 unsigned char buf[TEST_BUFFER_SIZE];
160 prepare_test_buffer (buf);
161 explicit_bzero (buf, TEST_BUFFER_SIZE);
162 }
163
164 enum test_expectation
165 {
166 EXPECT_NONE = 1,
167 EXPECT_SOME,
168 EXPECT_ALL,
169 NO_EXPECTATIONS,
170 ARRAY_END = 0
171 };
172 struct subtest
173 {
174 void (*setup_subtest) (void);
175 const char *label;
176 enum test_expectation expected;
177 };
178 static const struct subtest *cur_subtest;
179
180 static const struct subtest subtests[] =
181 {
182 { setup_no_clear, "no clear", EXPECT_SOME },
183 /* The memset may happen or not, depending on compiler
184 optimizations. */
185 { setup_ordinary_clear, "ordinary clear", NO_EXPECTATIONS },
186 { setup_explicit_clear, "explicit clear", EXPECT_NONE },
187 { 0, 0, ARRAY_END }
188 };
189
190 static void
191 test_coroutine (void)
192 {
193 while (cur_subtest->setup_subtest)
194 {
195 cur_subtest->setup_subtest ();
196 if (swapcontext (&uc_co, &uc_main))
197 abort ();
198 }
199 }
200
201 /* All the code above this point runs on the coroutine stack.
202 All the code below this point runs on the main stack. */
203
204 static int test_status;
205 static unsigned char *co_stack_buffer;
206 static size_t co_stack_size;
207
208 static unsigned int
209 count_test_patterns (unsigned char *buf, size_t bufsiz)
210 {
211 VALGRIND_MAKE_MEM_DEFINED (buf, bufsiz);
212 unsigned char *first = memmem (buf, bufsiz, test_pattern, PATTERN_SIZE);
213 if (!first)
214 return 0;
215 unsigned int cnt = 0;
216 for (unsigned int i = 0; i < PATTERN_REPS; i++)
217 {
218 unsigned char *p = first + i*PATTERN_SIZE;
219 if (p + PATTERN_SIZE - buf > (ptrdiff_t)bufsiz)
220 break;
221 if (memcmp (p, test_pattern, PATTERN_SIZE) == 0)
222 cnt++;
223 }
224 return cnt;
225 }
226
227 static void
228 check_test_buffer (enum test_expectation expected,
229 const char *label, const char *stage)
230 {
231 unsigned int cnt = count_test_patterns (co_stack_buffer, co_stack_size);
232 switch (expected)
233 {
234 case EXPECT_NONE:
235 if (cnt == 0)
236 printf ("PASS: %s/%s: expected 0 got %u\n", label, stage, cnt);
237 else
238 {
239 printf ("FAIL: %s/%s: expected 0 got %u\n", label, stage, cnt);
240 test_status = 1;
241 }
242 break;
243
244 case EXPECT_SOME:
245 if (cnt > 0)
246 printf ("PASS: %s/%s: expected some got %u\n", label, stage, cnt);
247 else
248 {
249 printf ("FAIL: %s/%s: expected some got 0\n", label, stage);
250 test_status = 1;
251 }
252 break;
253
254 case EXPECT_ALL:
255 if (cnt == PATTERN_REPS)
256 printf ("PASS: %s/%s: expected %d got %u\n", label, stage,
257 PATTERN_REPS, cnt);
258 else
259 {
260 printf ("FAIL: %s/%s: expected %d got %u\n", label, stage,
261 PATTERN_REPS, cnt);
262 test_status = 1;
263 }
264 break;
265
266 case NO_EXPECTATIONS:
267 printf ("INFO: %s/%s: found %u patterns%s\n", label, stage, cnt,
268 cnt == 0 ? " (memset not eliminated)" : "");
269 break;
270
271 default:
272 printf ("ERROR: %s/%s: invalid value for 'expected' = %d\n",
273 label, stage, (int)expected);
274 test_status = 1;
275 }
276 }
277
278 static void
279 test_loop (void)
280 {
281 cur_subtest = subtests;
282 while (cur_subtest->setup_subtest)
283 {
284 if (swapcontext (&uc_main, &uc_co))
285 abort ();
286 check_test_buffer (EXPECT_ALL, cur_subtest->label, "prepare");
287 if (swapcontext (&uc_main, &uc_co))
288 abort ();
289 check_test_buffer (cur_subtest->expected, cur_subtest->label, "test");
290 cur_subtest++;
291 }
292 /* Terminate the coroutine. */
293 if (swapcontext (&uc_main, &uc_co))
294 abort ();
295 }
296
297 int
298 main (void)
299 {
300 size_t page_alignment = sizeof (void *);
301 long page_alignment_l = sysconf (_SC_PAGESIZE);
302 if (page_alignment_l > (long) sizeof (void *))
303 page_alignment = (size_t) page_alignment_l;
304
305 co_stack_size = (size_t) SIGSTKSZ + (size_t) TEST_BUFFER_SIZE;
306 if (co_stack_size < page_alignment * 4)
307 co_stack_size = page_alignment * 4;
308
309 void *p;
310 int err = posix_memalign (&p, page_alignment, co_stack_size);
311 if (err || !p)
312 {
313 printf ("ERROR: allocating alt stack: %s\n", strerror (err));
314 return 2;
315 }
316 co_stack_buffer = p;
317 memset (co_stack_buffer, 0, co_stack_size);
318 VALGRIND_STACK_REGISTER (co_stack_buffer, co_stack_buffer + co_stack_size);
319
320 if (getcontext (&uc_co))
321 {
322 printf ("ERROR: allocating coroutine context: %s\n", strerror (err));
323 return 2;
324 }
325 uc_co.uc_stack.ss_sp = co_stack_buffer;
326 uc_co.uc_stack.ss_size = co_stack_size;
327 uc_co.uc_link = &uc_main;
328 makecontext (&uc_co, test_coroutine, 0);
329
330 test_loop ();
331 return test_status;
332 }
333
334 #endif /* have ucontext.h || defined XCRYPT_USE_ASAN */