This source file includes following definitions.
- test_static
- vma_iterate_callback
- is_range_mapped
- is_range_mapped
- test_heap
- do_secret_stuff
- test_stack
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <config.h>
20
21
22 #include <string.h>
23
24 #include "signature.h"
25 SIGNATURE_CHECK (explicit_bzero, void, (void *, size_t));
26
27 #include <stdbool.h>
28 #include <stdio.h>
29 #include <stdint.h>
30 #include <stdlib.h>
31
32 #include "vma-iter.h"
33 #include "macros.h"
34
35
36
37 #if 4 < __GNUC__ + (3 <= __GNUC_MINOR__)
38 # pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
39 #endif
40
41 #define SECRET "xyzzy1729"
42 #define SECRET_SIZE 9
43
44 static char zero[SECRET_SIZE] = { 0 };
45
46
47 #if 0
48 # define explicit_bzero(a, n) memset (a, '\0', n)
49 #endif
50
51
52
53 static char stbuf[SECRET_SIZE];
54
55 static void
56 test_static (void)
57 {
58 memcpy (stbuf, SECRET, SECRET_SIZE);
59 explicit_bzero (stbuf, SECRET_SIZE);
60 ASSERT (memcmp (zero, stbuf, SECRET_SIZE) == 0);
61 }
62
63
64
65
66 #if VMA_ITERATE_SUPPORTED
67
68 struct locals
69 {
70 uintptr_t range_start;
71 uintptr_t range_end;
72 };
73
74 static int
75 vma_iterate_callback (void *data, uintptr_t start, uintptr_t end,
76 unsigned int flags)
77 {
78 struct locals *lp = (struct locals *) data;
79
80
81
82 if (start <= lp->range_start && end > lp->range_start)
83 lp->range_start = (end < lp->range_end ? end : lp->range_end);
84 if (start < lp->range_end && end >= lp->range_end)
85 lp->range_end = (start > lp->range_start ? start : lp->range_start);
86
87 return 0;
88 }
89
90 static bool
91 is_range_mapped (uintptr_t range_start, uintptr_t range_end)
92 {
93 struct locals l;
94
95 l.range_start = range_start;
96 l.range_end = range_end;
97 vma_iterate (vma_iterate_callback, &l);
98 return l.range_start == l.range_end;
99 }
100
101 #else
102
103 static bool
104 is_range_mapped (uintptr_t range_start, uintptr_t range_end)
105 {
106 return true;
107 }
108
109 #endif
110
111 static void
112 test_heap (void)
113 {
114 char *heapbuf = (char *) malloc (SECRET_SIZE);
115 uintptr_t addr = (uintptr_t) heapbuf;
116 memcpy (heapbuf, SECRET, SECRET_SIZE);
117 explicit_bzero (heapbuf, SECRET_SIZE);
118 free (heapbuf);
119 if (is_range_mapped (addr, addr + SECRET_SIZE))
120 {
121
122
123 ASSERT (memcmp (heapbuf, SECRET, SECRET_SIZE) != 0);
124 printf ("test_heap: address range is still mapped after free().\n");
125 }
126 else
127 printf ("test_heap: address range is unmapped after free().\n");
128 }
129
130
131
132
133
134
135
136
137
138
139 static int _GL_ATTRIBUTE_NOINLINE
140 do_secret_stuff (volatile int pass)
141 {
142 static char *last_stackbuf;
143 char stackbuf[SECRET_SIZE];
144 if (pass == 1)
145 {
146 memcpy (stackbuf, SECRET, SECRET_SIZE);
147 explicit_bzero (stackbuf, SECRET_SIZE);
148 last_stackbuf = stackbuf;
149 return 0;
150 }
151 else
152 {
153
154
155
156
157 return memcmp (zero, last_stackbuf, SECRET_SIZE) != 0;
158 }
159 }
160
161 static void
162 test_stack (void)
163 {
164 int count = 0;
165 int repeat;
166
167 for (repeat = 2 * 1000; repeat > 0; repeat--)
168 {
169
170
171
172
173
174 if ((repeat % 2) == 0)
175 do_secret_stuff (1);
176 else
177 count += do_secret_stuff (2);
178 }
179
180
181
182
183
184 printf ("test_stack: count = %d\n", count);
185 ASSERT (count < 50);
186 }
187
188
189
190 int
191 main ()
192 {
193 test_static ();
194 test_heap ();
195 test_stack ();
196
197 return 0;
198 }