sys_ioctl: Simplify.
[gnulib.git] / tests / test-explicit_bzero.c
blobb5698f898ff5e141a2f5ab37986f9749ba320824
1 /* Test of explicit_bzero() function.
2 Copyright (C) 2020 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2020. */
19 #include <config.h>
21 /* Specification. */
22 #include <string.h>
24 #include "signature.h"
25 SIGNATURE_CHECK (explicit_bzero, void, (void *, size_t));
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <stdint.h>
30 #include <stdlib.h>
32 #include "vma-iter.h"
33 #include "macros.h"
35 #define SECRET "xyzzy1729"
36 #define SECRET_SIZE 9
38 static char zero[SECRET_SIZE] = { 0 };
40 /* Enable this to verify that the test is effective. */
41 #if 0
42 # define explicit_bzero(a, n) memset (a, '\0', n)
43 #endif
45 /* =================== Verify operation on static memory =================== */
47 static char stbuf[SECRET_SIZE];
49 static void
50 test_static (void)
52 memcpy (stbuf, SECRET, SECRET_SIZE);
53 explicit_bzero (stbuf, SECRET_SIZE);
54 ASSERT (memcmp (zero, stbuf, SECRET_SIZE) == 0);
57 /* =============== Verify operation on heap-allocated memory =============== */
59 /* Test whether an address range is mapped in memory. */
60 #if VMA_ITERATE_SUPPORTED
62 struct locals
64 uintptr_t range_start;
65 uintptr_t range_end;
68 static int
69 vma_iterate_callback (void *data, uintptr_t start, uintptr_t end,
70 unsigned int flags)
72 struct locals *lp = (struct locals *) data;
74 /* Remove from [range_start, range_end) the part at the beginning or at the
75 end that is covered by [start, end). */
76 if (start <= lp->range_start && end > lp->range_start)
77 lp->range_start = (end < lp->range_end ? end : lp->range_end);
78 if (start < lp->range_end && end >= lp->range_end)
79 lp->range_end = (start > lp->range_start ? start : lp->range_start);
81 return 0;
84 static bool
85 is_range_mapped (uintptr_t range_start, uintptr_t range_end)
87 struct locals l;
89 l.range_start = range_start;
90 l.range_end = range_end;
91 vma_iterate (vma_iterate_callback, &l);
92 return l.range_start == l.range_end;
95 #else
97 static bool
98 is_range_mapped (uintptr_t range_start, uintptr_t range_end)
100 return true;
103 #endif
105 static void
106 test_heap (void)
108 char *heapbuf = (char *) malloc (SECRET_SIZE);
109 uintptr_t addr = (uintptr_t) heapbuf;
110 memcpy (heapbuf, SECRET, SECRET_SIZE);
111 explicit_bzero (heapbuf, SECRET_SIZE);
112 free (heapbuf);
113 if (is_range_mapped (addr, addr + SECRET_SIZE))
115 /* some implementation could override freed memory by canaries so
116 compare against secret */
117 ASSERT (memcmp (heapbuf, SECRET, SECRET_SIZE) != 0);
118 printf ("test_heap: address range is still mapped after free().\n");
120 else
121 printf ("test_heap: address range is unmapped after free().\n");
124 /* =============== Verify operation on stack-allocated memory =============== */
126 /* There are two passes:
127 1. Put a secret in memory and invoke explicit_bzero on it.
128 2. Verify that the memory has been erased.
129 Implement them in the same function, so that they access the same memory
130 range on the stack. */
131 static int _GL_ATTRIBUTE_NOINLINE
132 do_secret_stuff (volatile int pass)
134 char stackbuf[SECRET_SIZE];
135 if (pass == 1)
137 memcpy (stackbuf, SECRET, SECRET_SIZE);
138 explicit_bzero (stackbuf, SECRET_SIZE);
139 return 0;
141 else /* pass == 2 */
143 return memcmp (zero, stackbuf, SECRET_SIZE) != 0;
147 static void
148 test_stack (void)
150 int count = 0;
151 int repeat;
153 for (repeat = 1000; repeat > 0; repeat--)
155 do_secret_stuff (1);
156 count += do_secret_stuff (2);
158 /* If explicit_bzero works, count is near 0. (It may be > 0 if there were
159 some asynchronous signal invocations between the two calls of
160 do_secret_stuff.)
161 If explicit_bzero is optimized away by the compiler, count comes out as
162 approximately 1000. */
163 printf ("test_stack: count = %d\n", count);
164 ASSERT (count < 50);
167 /* ========================================================================== */
170 main ()
172 test_static ();
173 test_heap ();
174 test_stack ();
176 return 0;