1 | /* Test strlen functions. |
2 | Copyright (C) 1999-2022 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 | #define TEST_MAIN |
20 | #ifndef WIDE |
21 | # define TEST_NAME "strnlen" |
22 | #else |
23 | # define TEST_NAME "wcsnlen" |
24 | #endif /* !WIDE */ |
25 | #include "test-string.h" |
26 | |
27 | #ifndef WIDE |
28 | # define STRNLEN strnlen |
29 | # define MEMSET memset |
30 | # define CHAR char |
31 | # define BIG_CHAR CHAR_MAX |
32 | # define MIDDLE_CHAR 127 |
33 | # define SIMPLE_STRNLEN simple_strnlen |
34 | #else |
35 | # include <wchar.h> |
36 | # define STRNLEN wcsnlen |
37 | # define MEMSET wmemset |
38 | # define CHAR wchar_t |
39 | # define BIG_CHAR WCHAR_MAX |
40 | # define MIDDLE_CHAR 1121 |
41 | # define SIMPLE_STRNLEN simple_wcsnlen |
42 | #endif /* !WIDE */ |
43 | |
44 | typedef size_t (*proto_t) (const CHAR *, size_t); |
45 | size_t SIMPLE_STRNLEN (const CHAR *, size_t); |
46 | |
47 | IMPL (SIMPLE_STRNLEN, 0) |
48 | IMPL (STRNLEN, 1) |
49 | |
50 | size_t |
51 | SIMPLE_STRNLEN (const CHAR *s, size_t maxlen) |
52 | { |
53 | size_t i; |
54 | |
55 | for (i = 0; i < maxlen && s[i]; ++i); |
56 | return i; |
57 | } |
58 | |
59 | static void |
60 | do_one_test (impl_t *impl, const CHAR *s, size_t maxlen, size_t exp_len) |
61 | { |
62 | size_t len = CALL (impl, s, maxlen); |
63 | if (len != exp_len) |
64 | { |
65 | error (status: 0, errnum: 0, format: "Wrong result in function %s %zd %zd" , impl->name, |
66 | len, exp_len); |
67 | ret = 1; |
68 | return; |
69 | } |
70 | } |
71 | |
72 | static void |
73 | do_test (size_t align, size_t len, size_t maxlen, int max_char) |
74 | { |
75 | size_t i; |
76 | |
77 | align &= (getpagesize () / sizeof (CHAR) - 1); |
78 | if ((align + len) * sizeof (CHAR) >= page_size) |
79 | return; |
80 | |
81 | CHAR *buf = (CHAR *) (buf1); |
82 | |
83 | for (i = 0; i < len; ++i) |
84 | buf[align + i] = 1 + 11111 * i % max_char; |
85 | buf[align + len] = 0; |
86 | |
87 | FOR_EACH_IMPL (impl, 0) |
88 | do_one_test (impl, s: (CHAR *) (buf + align), maxlen, MIN (len, maxlen)); |
89 | } |
90 | |
91 | static void |
92 | do_overflow_tests (void) |
93 | { |
94 | size_t i, j, al_idx, repeats, len; |
95 | const size_t one = 1; |
96 | uintptr_t buf_addr = (uintptr_t) buf1; |
97 | const size_t alignments[] = { 0, 1, 7, 9, 31, 33, 63, 65, 95, 97, 127, 129 }; |
98 | |
99 | for (al_idx = 0; al_idx < sizeof (alignments) / sizeof (alignments[0]); |
100 | al_idx++) |
101 | { |
102 | for (repeats = 0; repeats < 2; ++repeats) |
103 | { |
104 | size_t align = repeats ? (getpagesize () - alignments[al_idx]) |
105 | : alignments[al_idx]; |
106 | align /= sizeof (CHAR); |
107 | for (i = 0; i < 750; ++i) |
108 | { |
109 | do_test (align, len: i, SIZE_MAX, BIG_CHAR); |
110 | |
111 | do_test (align, len: i, SIZE_MAX - i, BIG_CHAR); |
112 | do_test (align, len: i, maxlen: i - buf_addr, BIG_CHAR); |
113 | do_test (align, len: i, maxlen: -buf_addr - i, BIG_CHAR); |
114 | do_test (align, len: i, SIZE_MAX - buf_addr - i, BIG_CHAR); |
115 | do_test (align, len: i, SIZE_MAX - buf_addr + i, BIG_CHAR); |
116 | |
117 | len = 0; |
118 | for (j = 8 * sizeof (size_t) - 1; j; --j) |
119 | { |
120 | len |= one << j; |
121 | do_test (align, len: i, maxlen: len, BIG_CHAR); |
122 | do_test (align, len: i, maxlen: len - i, BIG_CHAR); |
123 | do_test (align, len: i, maxlen: len + i, BIG_CHAR); |
124 | do_test (align, len: i, maxlen: len - buf_addr - i, BIG_CHAR); |
125 | do_test (align, len: i, maxlen: len - buf_addr + i, BIG_CHAR); |
126 | |
127 | do_test (align, len: i, maxlen: ~len - i, BIG_CHAR); |
128 | do_test (align, len: i, maxlen: ~len + i, BIG_CHAR); |
129 | do_test (align, len: i, maxlen: ~len - buf_addr - i, BIG_CHAR); |
130 | do_test (align, len: i, maxlen: ~len - buf_addr + i, BIG_CHAR); |
131 | |
132 | do_test (align, len: i, maxlen: -buf_addr, BIG_CHAR); |
133 | do_test (align, len: i, maxlen: j - buf_addr, BIG_CHAR); |
134 | do_test (align, len: i, maxlen: -buf_addr - j, BIG_CHAR); |
135 | } |
136 | } |
137 | } |
138 | } |
139 | } |
140 | |
141 | static void |
142 | do_random_tests (void) |
143 | { |
144 | size_t i, j, n, align, len; |
145 | CHAR *p = (CHAR *) (buf1 + page_size - 512 * sizeof (CHAR)); |
146 | |
147 | for (n = 0; n < ITERATIONS; n++) |
148 | { |
149 | align = random () & 15; |
150 | len = random () & 511; |
151 | if (len + align > 510) |
152 | len = 511 - align - (random () & 7); |
153 | j = len + align + 64; |
154 | if (j > 512) |
155 | j = 512; |
156 | |
157 | for (i = 0; i < j; i++) |
158 | { |
159 | if (i == len + align) |
160 | p[i] = 0; |
161 | else |
162 | { |
163 | p[i] = random () & 255; |
164 | if (i >= align && i < len + align && !p[i]) |
165 | p[i] = (random () & 127) + 1; |
166 | } |
167 | } |
168 | |
169 | FOR_EACH_IMPL (impl, 1) |
170 | { |
171 | if (len > 0 |
172 | && CALL (impl, (CHAR *) (p + align), len - 1) != len - 1) |
173 | { |
174 | error (status: 0, errnum: 0, format: "Iteration %zd (limited) - wrong result in function %s (%zd) %zd != %zd, p %p" , |
175 | n, impl->name, align, |
176 | CALL (impl, (CHAR *) (p + align), len - 1), len - 1, p); |
177 | ret = 1; |
178 | } |
179 | if (CALL (impl, (CHAR *) (p + align), len) != len) |
180 | { |
181 | error (status: 0, errnum: 0, format: "Iteration %zd (exact) - wrong result in function %s (%zd) %zd != %zd, p %p" , |
182 | n, impl->name, align, |
183 | CALL (impl, (CHAR *) (p + align), len), len, p); |
184 | ret = 1; |
185 | } |
186 | if (CALL (impl, (CHAR *) (p + align), len + 1) != len) |
187 | { |
188 | error (status: 0, errnum: 0, format: "Iteration %zd (long) - wrong result in function %s (%zd) %zd != %zd, p %p" , |
189 | n, impl->name, align, |
190 | CALL (impl, (CHAR *) (p + align), len + 1), len, p); |
191 | ret = 1; |
192 | } |
193 | } |
194 | } |
195 | } |
196 | |
197 | /* Tests meant to unveil fail on implementation that does not access bytes |
198 | around the page boundary accordingly. */ |
199 | static void |
200 | do_page_tests (void) |
201 | { |
202 | size_t i, exp_len, start_offset, offset; |
203 | /* Calculate the null character offset. */ |
204 | size_t last_offset = (page_size / sizeof (CHAR)) - 1; |
205 | |
206 | CHAR *s = (CHAR *) buf2; |
207 | MEMSET (s, 65, (last_offset - 1)); |
208 | s[last_offset] = 0; |
209 | |
210 | /* Place short strings ending at page boundary. */ |
211 | offset = last_offset; |
212 | for (i = 0; i < 128; i++) |
213 | { |
214 | /* Decrease offset to stress several sizes and alignments. */ |
215 | offset--; |
216 | exp_len = last_offset - offset; |
217 | FOR_EACH_IMPL (impl, 0) |
218 | { |
219 | /* Varies maxlen value to cover the cases where it is: |
220 | - larger than length; |
221 | - slightly greater than length; |
222 | - equal to length; |
223 | - slightly less than length. */ |
224 | do_one_test (impl, s: (CHAR *) (s + offset), maxlen: page_size, exp_len); |
225 | do_one_test (impl, s: (CHAR *) (s + offset), maxlen: exp_len + 1, exp_len); |
226 | do_one_test (impl, s: (CHAR *) (s + offset), maxlen: exp_len, exp_len); |
227 | if (exp_len > 0) |
228 | do_one_test (impl, s: (CHAR *) (s + offset), maxlen: exp_len - 1, exp_len: exp_len - 1); |
229 | } |
230 | } |
231 | |
232 | /* Place long strings ending at page boundary. */ |
233 | start_offset = (last_offset + 1) / 2; |
234 | for (i = 0; i < 64; ++i) |
235 | { |
236 | /* Increase offset to stress several alignments. */ |
237 | offset = start_offset + i; |
238 | if (offset >= (last_offset + 1)) |
239 | break; |
240 | exp_len = last_offset - offset; |
241 | FOR_EACH_IMPL (impl, 0) |
242 | { |
243 | /* Checks only for maxlen much larger than length because smaller |
244 | values are already covered in do_random_tests function. */ |
245 | do_one_test (impl, s: (CHAR *) (s + offset), maxlen: page_size, exp_len); |
246 | } |
247 | } |
248 | } |
249 | |
250 | /* Tests meant to unveil fail on implementations that access bytes |
251 | beyond the maxium length. */ |
252 | |
253 | static void |
254 | do_page_2_tests (void) |
255 | { |
256 | size_t i, exp_len, offset; |
257 | size_t last_offset = page_size / sizeof (CHAR); |
258 | |
259 | CHAR *s = (CHAR *) buf2; |
260 | MEMSET (s, 65, last_offset); |
261 | |
262 | /* Place short strings ending at page boundary without the null |
263 | byte. */ |
264 | offset = last_offset; |
265 | for (i = 0; i < 128; i++) |
266 | { |
267 | /* Decrease offset to stress several sizes and alignments. */ |
268 | offset--; |
269 | exp_len = last_offset - offset; |
270 | FOR_EACH_IMPL (impl, 0) |
271 | { |
272 | /* If an implementation goes beyond EXP_LEN, it will trigger |
273 | the segfault. */ |
274 | do_one_test (impl, s: (CHAR *) (s + offset), maxlen: exp_len, exp_len); |
275 | } |
276 | } |
277 | } |
278 | |
279 | int |
280 | test_main (void) |
281 | { |
282 | size_t i, length, char_per_page; |
283 | |
284 | test_init (); |
285 | |
286 | printf (format: "%20s" , "" ); |
287 | FOR_EACH_IMPL (impl, 0) |
288 | printf (format: "\t%s" , impl->name); |
289 | putchar (c: '\n'); |
290 | |
291 | for (i = 1; i < 8; ++i) |
292 | { |
293 | do_test (align: 0, len: i, maxlen: i - 1, MIDDLE_CHAR); |
294 | do_test (align: 0, len: i, maxlen: i, MIDDLE_CHAR); |
295 | do_test (align: 0, len: i, maxlen: i + 1, MIDDLE_CHAR); |
296 | } |
297 | |
298 | for (i = 1; i < 8; ++i) |
299 | { |
300 | do_test (align: i, len: i, maxlen: i - 1, MIDDLE_CHAR); |
301 | do_test (align: i, len: i, maxlen: i, MIDDLE_CHAR); |
302 | do_test (align: i, len: i, maxlen: i + 1, MIDDLE_CHAR); |
303 | } |
304 | |
305 | for (i = 2; i <= 10; ++i) |
306 | { |
307 | do_test (align: 0, len: 1 << i, maxlen: 5000, MIDDLE_CHAR); |
308 | do_test (align: 1, len: 1 << i, maxlen: 5000, MIDDLE_CHAR); |
309 | } |
310 | |
311 | for (i = 1; i < 8; ++i) |
312 | do_test (align: 0, len: i, maxlen: 5000, BIG_CHAR); |
313 | |
314 | for (i = 1; i < 8; ++i) |
315 | do_test (align: i, len: i, maxlen: 5000, BIG_CHAR); |
316 | |
317 | for (i = 2; i <= 10; ++i) |
318 | { |
319 | do_test (align: 0, len: 1 << i, maxlen: 5000, BIG_CHAR); |
320 | do_test (align: 1, len: 1 << i, maxlen: 5000, BIG_CHAR); |
321 | } |
322 | |
323 | char_per_page = getpagesize () / sizeof (CHAR); |
324 | |
325 | for (i = 0; i <= 127; i++) |
326 | for (length = i; length <= 512; length++) |
327 | { |
328 | do_test (align: i, len: length, maxlen: 512, BIG_CHAR); |
329 | do_test (align: char_per_page - i, len: length, maxlen: 512, BIG_CHAR); |
330 | } |
331 | |
332 | do_random_tests (); |
333 | do_page_tests (); |
334 | do_page_2_tests (); |
335 | do_overflow_tests (); |
336 | return ret; |
337 | } |
338 | |
339 | #include <support/test-driver.c> |
340 | |