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. */
24 #include "signature.h"
25 SIGNATURE_CHECK (explicit_bzero
, void, (void *, size_t));
35 #define SECRET "xyzzy1729"
38 static char zero
[SECRET_SIZE
] = { 0 };
40 /* Enable this to verify that the test is effective. */
42 # define explicit_bzero(a, n) memset (a, '\0', n)
45 /* =================== Verify operation on static memory =================== */
47 static char stbuf
[SECRET_SIZE
];
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
64 uintptr_t range_start
;
69 vma_iterate_callback (void *data
, uintptr_t start
, uintptr_t end
,
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
);
85 is_range_mapped (uintptr_t range_start
, uintptr_t range_end
)
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
;
98 is_range_mapped (uintptr_t range_start
, uintptr_t range_end
)
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
);
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");
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
];
137 memcpy (stackbuf
, SECRET
, SECRET_SIZE
);
138 explicit_bzero (stackbuf
, SECRET_SIZE
);
143 return memcmp (zero
, stackbuf
, SECRET_SIZE
) != 0;
153 for (repeat
= 1000; repeat
> 0; repeat
--)
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
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
);
167 /* ========================================================================== */