Commit | Line | Data |
---|---|---|
09c434b8 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
707cc728 RV |
2 | /* |
3 | * Test cases for printf facility. | |
4 | */ | |
5 | ||
6 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
7 | ||
8 | #include <linux/init.h> | |
9 | #include <linux/kernel.h> | |
10 | #include <linux/module.h> | |
11 | #include <linux/printk.h> | |
12 | #include <linux/random.h> | |
4d42c447 | 13 | #include <linux/rtc.h> |
707cc728 | 14 | #include <linux/slab.h> |
39ced19b | 15 | #include <linux/sprintf.h> |
707cc728 RV |
16 | #include <linux/string.h> |
17 | ||
857cca4d | 18 | #include <linux/bitmap.h> |
251c7234 | 19 | #include <linux/dcache.h> |
707cc728 RV |
20 | #include <linux/socket.h> |
21 | #include <linux/in.h> | |
22 | ||
edf14cdb VB |
23 | #include <linux/gfp.h> |
24 | #include <linux/mm.h> | |
25 | ||
f1ce39df SA |
26 | #include <linux/property.h> |
27 | ||
6b1a4d5b TH |
28 | #include "../tools/testing/selftests/kselftest_module.h" |
29 | ||
707cc728 | 30 | #define BUF_SIZE 256 |
331e4deb | 31 | #define PAD_SIZE 16 |
707cc728 RV |
32 | #define FILL_CHAR '$' |
33 | ||
96dd9a2f JS |
34 | #define NOWARN(option, comment, block) \ |
35 | __diag_push(); \ | |
36 | __diag_ignore_all(#option, comment); \ | |
37 | block \ | |
38 | __diag_pop(); | |
39 | ||
4e89a787 TT |
40 | KSTM_MODULE_GLOBALS(); |
41 | ||
707cc728 | 42 | static char *test_buffer __initdata; |
331e4deb | 43 | static char *alloced_buffer __initdata; |
707cc728 | 44 | |
5ead723a TT |
45 | extern bool no_hash_pointers; |
46 | ||
707cc728 RV |
47 | static int __printf(4, 0) __init |
48 | do_test(int bufsize, const char *expect, int elen, | |
49 | const char *fmt, va_list ap) | |
50 | { | |
51 | va_list aq; | |
52 | int ret, written; | |
53 | ||
54 | total_tests++; | |
55 | ||
331e4deb | 56 | memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE); |
707cc728 RV |
57 | va_copy(aq, ap); |
58 | ret = vsnprintf(test_buffer, bufsize, fmt, aq); | |
59 | va_end(aq); | |
60 | ||
61 | if (ret != elen) { | |
62 | pr_warn("vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n", | |
63 | bufsize, fmt, ret, elen); | |
64 | return 1; | |
65 | } | |
66 | ||
331e4deb RV |
67 | if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) { |
68 | pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize, fmt); | |
69 | return 1; | |
70 | } | |
71 | ||
707cc728 | 72 | if (!bufsize) { |
331e4deb | 73 | if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) { |
707cc728 RV |
74 | pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n", |
75 | fmt); | |
76 | return 1; | |
77 | } | |
78 | return 0; | |
79 | } | |
80 | ||
81 | written = min(bufsize-1, elen); | |
82 | if (test_buffer[written]) { | |
83 | pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n", | |
84 | bufsize, fmt); | |
85 | return 1; | |
86 | } | |
87 | ||
9a3bfa01 | 88 | if (memchr_inv(test_buffer + written + 1, FILL_CHAR, bufsize - (written + 1))) { |
331e4deb RV |
89 | pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n", |
90 | bufsize, fmt); | |
91 | return 1; | |
92 | } | |
93 | ||
9a3bfa01 RV |
94 | if (memchr_inv(test_buffer + bufsize, FILL_CHAR, BUF_SIZE + PAD_SIZE - bufsize)) { |
95 | pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond buffer\n", bufsize, fmt); | |
96 | return 1; | |
97 | } | |
98 | ||
707cc728 RV |
99 | if (memcmp(test_buffer, expect, written)) { |
100 | pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n", | |
101 | bufsize, fmt, test_buffer, written, expect); | |
102 | return 1; | |
103 | } | |
104 | return 0; | |
105 | } | |
106 | ||
107 | static void __printf(3, 4) __init | |
108 | __test(const char *expect, int elen, const char *fmt, ...) | |
109 | { | |
110 | va_list ap; | |
111 | int rand; | |
112 | char *p; | |
113 | ||
fd0515d5 RV |
114 | if (elen >= BUF_SIZE) { |
115 | pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n", | |
116 | elen, fmt); | |
117 | failed_tests++; | |
118 | return; | |
119 | } | |
707cc728 RV |
120 | |
121 | va_start(ap, fmt); | |
122 | ||
123 | /* | |
124 | * Every fmt+args is subjected to four tests: Three where we | |
125 | * tell vsnprintf varying buffer sizes (plenty, not quite | |
126 | * enough and 0), and then we also test that kvasprintf would | |
127 | * be able to print it as expected. | |
128 | */ | |
129 | failed_tests += do_test(BUF_SIZE, expect, elen, fmt, ap); | |
e8a533cb | 130 | rand = get_random_u32_inclusive(1, elen + 1); |
707cc728 RV |
131 | /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */ |
132 | failed_tests += do_test(rand, expect, elen, fmt, ap); | |
133 | failed_tests += do_test(0, expect, elen, fmt, ap); | |
134 | ||
135 | p = kvasprintf(GFP_KERNEL, fmt, ap); | |
136 | if (p) { | |
b79a7db3 | 137 | total_tests++; |
707cc728 RV |
138 | if (memcmp(p, expect, elen+1)) { |
139 | pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n", | |
140 | fmt, p, expect); | |
141 | failed_tests++; | |
142 | } | |
143 | kfree(p); | |
144 | } | |
145 | va_end(ap); | |
146 | } | |
147 | ||
148 | #define test(expect, fmt, ...) \ | |
149 | __test(expect, strlen(expect), fmt, ##__VA_ARGS__) | |
150 | ||
151 | static void __init | |
152 | test_basic(void) | |
153 | { | |
154 | /* Work around annoying "warning: zero-length gnu_printf format string". */ | |
155 | char nul = '\0'; | |
156 | ||
157 | test("", &nul); | |
158 | test("100%", "100%%"); | |
159 | test("xxx%yyy", "xxx%cyyy", '%'); | |
160 | __test("xxx\0yyy", 7, "xxx%cyyy", '\0'); | |
161 | } | |
162 | ||
163 | static void __init | |
164 | test_number(void) | |
165 | { | |
166 | test("0x1234abcd ", "%#-12x", 0x1234abcd); | |
167 | test(" 0x1234abcd", "%#12x", 0x1234abcd); | |
168 | test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234); | |
96dd9a2f JS |
169 | NOWARN(-Wformat, "Intentionally test narrowing conversion specifiers.", { |
170 | test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1); | |
171 | test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1); | |
172 | test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627); | |
173 | }) | |
1ca8e8eb RV |
174 | /* |
175 | * POSIX/C99: »The result of converting zero with an explicit | |
176 | * precision of zero shall be no characters.« Hence the output | |
177 | * from the below test should really be "00|0||| ". However, | |
178 | * the kernel's printf also produces a single 0 in that | |
179 | * case. This test case simply documents the current | |
180 | * behaviour. | |
181 | */ | |
182 | test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0); | |
707cc728 RV |
183 | } |
184 | ||
185 | static void __init | |
186 | test_string(void) | |
187 | { | |
188 | test("", "%s%.0s", "", "123"); | |
189 | test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456"); | |
190 | test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5"); | |
f176eb4c RV |
191 | test("1234 ", "%-10.4s", "123456"); |
192 | test(" 1234", "%10.4s", "123456"); | |
707cc728 | 193 | /* |
f176eb4c RV |
194 | * POSIX and C99 say that a negative precision (which is only |
195 | * possible to pass via a * argument) should be treated as if | |
196 | * the precision wasn't present, and that if the precision is | |
197 | * omitted (as in %.s), the precision should be taken to be | |
198 | * 0. However, the kernel's printf behave exactly opposite, | |
199 | * treating a negative precision as 0 and treating an omitted | |
200 | * precision specifier as if no precision was given. | |
201 | * | |
202 | * These test cases document the current behaviour; should | |
203 | * anyone ever feel the need to follow the standards more | |
204 | * closely, this can be revisited. | |
707cc728 | 205 | */ |
f176eb4c RV |
206 | test(" ", "%4.*s", -5, "123456"); |
207 | test("123456", "%.s", "123456"); | |
707cc728 RV |
208 | test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c"); |
209 | test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c"); | |
210 | } | |
211 | ||
ad67b74d TH |
212 | #define PLAIN_BUF_SIZE 64 /* leave some space so we don't oops */ |
213 | ||
214 | #if BITS_PER_LONG == 64 | |
215 | ||
216 | #define PTR_WIDTH 16 | |
c604b407 | 217 | #define PTR ((void *)0xffff0123456789abUL) |
ad67b74d | 218 | #define PTR_STR "ffff0123456789ab" |
ce041c43 | 219 | #define PTR_VAL_NO_CRNG "(____ptrval____)" |
ad67b74d | 220 | #define ZEROS "00000000" /* hex 32 zero bits */ |
7bd57fbc | 221 | #define ONES "ffffffff" /* hex 32 one bits */ |
ad67b74d TH |
222 | |
223 | static int __init | |
224 | plain_format(void) | |
225 | { | |
226 | char buf[PLAIN_BUF_SIZE]; | |
227 | int nchars; | |
228 | ||
229 | nchars = snprintf(buf, PLAIN_BUF_SIZE, "%p", PTR); | |
230 | ||
ce041c43 TE |
231 | if (nchars != PTR_WIDTH) |
232 | return -1; | |
233 | ||
234 | if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) { | |
235 | pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"", | |
236 | PTR_VAL_NO_CRNG); | |
237 | return 0; | |
238 | } | |
239 | ||
240 | if (strncmp(buf, ZEROS, strlen(ZEROS)) != 0) | |
ad67b74d TH |
241 | return -1; |
242 | ||
243 | return 0; | |
244 | } | |
245 | ||
246 | #else | |
247 | ||
248 | #define PTR_WIDTH 8 | |
249 | #define PTR ((void *)0x456789ab) | |
250 | #define PTR_STR "456789ab" | |
ce041c43 | 251 | #define PTR_VAL_NO_CRNG "(ptrval)" |
3e5903eb | 252 | #define ZEROS "" |
7bd57fbc | 253 | #define ONES "" |
ad67b74d TH |
254 | |
255 | static int __init | |
256 | plain_format(void) | |
257 | { | |
258 | /* Format is implicitly tested for 32 bit machines by plain_hash() */ | |
259 | return 0; | |
260 | } | |
261 | ||
262 | #endif /* BITS_PER_LONG == 64 */ | |
263 | ||
264 | static int __init | |
4d42c447 | 265 | plain_hash_to_buffer(const void *p, char *buf, size_t len) |
ad67b74d | 266 | { |
ad67b74d TH |
267 | int nchars; |
268 | ||
4d42c447 | 269 | nchars = snprintf(buf, len, "%p", p); |
ad67b74d | 270 | |
ce041c43 TE |
271 | if (nchars != PTR_WIDTH) |
272 | return -1; | |
273 | ||
274 | if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) { | |
275 | pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"", | |
276 | PTR_VAL_NO_CRNG); | |
277 | return 0; | |
278 | } | |
279 | ||
4d42c447 AS |
280 | return 0; |
281 | } | |
282 | ||
4d42c447 AS |
283 | static int __init |
284 | plain_hash(void) | |
285 | { | |
286 | char buf[PLAIN_BUF_SIZE]; | |
287 | int ret; | |
288 | ||
289 | ret = plain_hash_to_buffer(PTR, buf, PLAIN_BUF_SIZE); | |
290 | if (ret) | |
291 | return ret; | |
292 | ||
ce041c43 | 293 | if (strncmp(buf, PTR_STR, PTR_WIDTH) == 0) |
ad67b74d TH |
294 | return -1; |
295 | ||
296 | return 0; | |
297 | } | |
298 | ||
299 | /* | |
300 | * We can't use test() to test %p because we don't know what output to expect | |
301 | * after an address is hashed. | |
302 | */ | |
707cc728 RV |
303 | static void __init |
304 | plain(void) | |
305 | { | |
ad67b74d | 306 | int err; |
707cc728 | 307 | |
5ead723a TT |
308 | if (no_hash_pointers) { |
309 | pr_warn("skipping plain 'p' tests"); | |
310 | skipped_tests += 2; | |
311 | return; | |
312 | } | |
313 | ||
ad67b74d TH |
314 | err = plain_hash(); |
315 | if (err) { | |
316 | pr_warn("plain 'p' does not appear to be hashed\n"); | |
317 | failed_tests++; | |
318 | return; | |
319 | } | |
320 | ||
321 | err = plain_format(); | |
322 | if (err) { | |
323 | pr_warn("hashing plain 'p' has unexpected format\n"); | |
324 | failed_tests++; | |
325 | } | |
707cc728 RV |
326 | } |
327 | ||
4d42c447 AS |
328 | static void __init |
329 | test_hashed(const char *fmt, const void *p) | |
330 | { | |
331 | char buf[PLAIN_BUF_SIZE]; | |
332 | int ret; | |
333 | ||
334 | /* | |
335 | * No need to increase failed test counter since this is assumed | |
336 | * to be called after plain(). | |
337 | */ | |
338 | ret = plain_hash_to_buffer(p, buf, PLAIN_BUF_SIZE); | |
339 | if (ret) | |
340 | return; | |
341 | ||
342 | test(buf, fmt, p); | |
343 | } | |
344 | ||
7bd57fbc ID |
345 | /* |
346 | * NULL pointers aren't hashed. | |
347 | */ | |
3e5903eb PM |
348 | static void __init |
349 | null_pointer(void) | |
350 | { | |
7bd57fbc | 351 | test(ZEROS "00000000", "%p", NULL); |
3e5903eb PM |
352 | test(ZEROS "00000000", "%px", NULL); |
353 | test("(null)", "%pE", NULL); | |
354 | } | |
355 | ||
7bd57fbc ID |
356 | /* |
357 | * Error pointers aren't hashed. | |
358 | */ | |
359 | static void __init | |
360 | error_pointer(void) | |
361 | { | |
362 | test(ONES "fffffff5", "%p", ERR_PTR(-11)); | |
363 | test(ONES "fffffff5", "%px", ERR_PTR(-11)); | |
364 | test("(efault)", "%pE", ERR_PTR(-11)); | |
365 | } | |
366 | ||
3e5903eb PM |
367 | #define PTR_INVALID ((void *)0x000000ab) |
368 | ||
369 | static void __init | |
370 | invalid_pointer(void) | |
371 | { | |
372 | test_hashed("%p", PTR_INVALID); | |
373 | test(ZEROS "000000ab", "%px", PTR_INVALID); | |
374 | test("(efault)", "%pE", PTR_INVALID); | |
375 | } | |
376 | ||
707cc728 RV |
377 | static void __init |
378 | symbol_ptr(void) | |
379 | { | |
380 | } | |
381 | ||
382 | static void __init | |
383 | kernel_ptr(void) | |
384 | { | |
ad67b74d | 385 | /* We can't test this without access to kptr_restrict. */ |
707cc728 RV |
386 | } |
387 | ||
388 | static void __init | |
389 | struct_resource(void) | |
390 | { | |
391 | } | |
392 | ||
393 | static void __init | |
394 | addr(void) | |
395 | { | |
396 | } | |
397 | ||
398 | static void __init | |
399 | escaped_str(void) | |
400 | { | |
401 | } | |
402 | ||
403 | static void __init | |
404 | hex_string(void) | |
405 | { | |
406 | const char buf[3] = {0xc0, 0xff, 0xee}; | |
407 | ||
408 | test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee", | |
409 | "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf); | |
410 | test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee", | |
411 | "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf); | |
412 | } | |
413 | ||
414 | static void __init | |
415 | mac(void) | |
416 | { | |
417 | const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05}; | |
418 | ||
419 | test("2d:48:d6:fc:7a:05", "%pM", addr); | |
420 | test("05:7a:fc:d6:48:2d", "%pMR", addr); | |
421 | test("2d-48-d6-fc-7a-05", "%pMF", addr); | |
422 | test("2d48d6fc7a05", "%pm", addr); | |
423 | test("057afcd6482d", "%pmR", addr); | |
424 | } | |
425 | ||
426 | static void __init | |
427 | ip4(void) | |
428 | { | |
429 | struct sockaddr_in sa; | |
430 | ||
431 | sa.sin_family = AF_INET; | |
432 | sa.sin_port = cpu_to_be16(12345); | |
433 | sa.sin_addr.s_addr = cpu_to_be32(0x7f000001); | |
434 | ||
435 | test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr); | |
436 | test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa); | |
437 | sa.sin_addr.s_addr = cpu_to_be32(0x01020304); | |
438 | test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa); | |
439 | } | |
440 | ||
441 | static void __init | |
442 | ip6(void) | |
443 | { | |
444 | } | |
445 | ||
446 | static void __init | |
447 | ip(void) | |
448 | { | |
449 | ip4(); | |
450 | ip6(); | |
451 | } | |
452 | ||
453 | static void __init | |
454 | uuid(void) | |
455 | { | |
456 | const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, | |
457 | 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf}; | |
458 | ||
459 | test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid); | |
460 | test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid); | |
461 | test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid); | |
462 | test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid); | |
463 | } | |
464 | ||
251c7234 RV |
465 | static struct dentry test_dentry[4] __initdata = { |
466 | { .d_parent = &test_dentry[0], | |
467 | .d_name = QSTR_INIT(test_dentry[0].d_iname, 3), | |
468 | .d_iname = "foo" }, | |
469 | { .d_parent = &test_dentry[0], | |
470 | .d_name = QSTR_INIT(test_dentry[1].d_iname, 5), | |
471 | .d_iname = "bravo" }, | |
472 | { .d_parent = &test_dentry[1], | |
473 | .d_name = QSTR_INIT(test_dentry[2].d_iname, 4), | |
474 | .d_iname = "alfa" }, | |
475 | { .d_parent = &test_dentry[2], | |
476 | .d_name = QSTR_INIT(test_dentry[3].d_iname, 5), | |
477 | .d_iname = "romeo" }, | |
478 | }; | |
479 | ||
707cc728 RV |
480 | static void __init |
481 | dentry(void) | |
482 | { | |
251c7234 RV |
483 | test("foo", "%pd", &test_dentry[0]); |
484 | test("foo", "%pd2", &test_dentry[0]); | |
485 | ||
cf6b7921 JH |
486 | test("(null)", "%pd", NULL); |
487 | test("(efault)", "%pd", PTR_INVALID); | |
cf6b7921 JH |
488 | test("(null)", "%pD", NULL); |
489 | test("(efault)", "%pD", PTR_INVALID); | |
490 | ||
251c7234 RV |
491 | test("romeo", "%pd", &test_dentry[3]); |
492 | test("alfa/romeo", "%pd2", &test_dentry[3]); | |
493 | test("bravo/alfa/romeo", "%pd3", &test_dentry[3]); | |
494 | test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]); | |
495 | test("/bravo/alfa", "%pd4", &test_dentry[2]); | |
496 | ||
497 | test("bravo/alfa |bravo/alfa ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]); | |
498 | test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]); | |
707cc728 RV |
499 | } |
500 | ||
501 | static void __init | |
502 | struct_va_format(void) | |
503 | { | |
504 | } | |
505 | ||
4d42c447 | 506 | static void __init |
7daac5b2 | 507 | time_and_date(void) |
4d42c447 AS |
508 | { |
509 | /* 1543210543 */ | |
510 | const struct rtc_time tm = { | |
511 | .tm_sec = 43, | |
512 | .tm_min = 35, | |
513 | .tm_hour = 5, | |
514 | .tm_mday = 26, | |
515 | .tm_mon = 10, | |
516 | .tm_year = 118, | |
517 | }; | |
7daac5b2 AS |
518 | /* 2019-01-04T15:32:23 */ |
519 | time64_t t = 1546615943; | |
4d42c447 | 520 | |
7daac5b2 | 521 | test("(%pt?)", "%pt", &tm); |
4d42c447 AS |
522 | test("2018-11-26T05:35:43", "%ptR", &tm); |
523 | test("0118-10-26T05:35:43", "%ptRr", &tm); | |
524 | test("05:35:43|2018-11-26", "%ptRt|%ptRd", &tm, &tm); | |
525 | test("05:35:43|0118-10-26", "%ptRtr|%ptRdr", &tm, &tm); | |
526 | test("05:35:43|2018-11-26", "%ptRttr|%ptRdtr", &tm, &tm); | |
527 | test("05:35:43 tr|2018-11-26 tr", "%ptRt tr|%ptRd tr", &tm, &tm); | |
7daac5b2 AS |
528 | |
529 | test("2019-01-04T15:32:23", "%ptT", &t); | |
530 | test("0119-00-04T15:32:23", "%ptTr", &t); | |
531 | test("15:32:23|2019-01-04", "%ptTt|%ptTd", &t, &t); | |
532 | test("15:32:23|0119-00-04", "%ptTtr|%ptTdr", &t, &t); | |
20bc8c1e AS |
533 | |
534 | test("2019-01-04 15:32:23", "%ptTs", &t); | |
535 | test("0119-00-04 15:32:23", "%ptTsr", &t); | |
536 | test("15:32:23|2019-01-04", "%ptTts|%ptTds", &t, &t); | |
537 | test("15:32:23|0119-00-04", "%ptTtrs|%ptTdrs", &t, &t); | |
4d42c447 AS |
538 | } |
539 | ||
707cc728 RV |
540 | static void __init |
541 | struct_clk(void) | |
542 | { | |
543 | } | |
544 | ||
857cca4d RV |
545 | static void __init |
546 | large_bitmap(void) | |
547 | { | |
548 | const int nbits = 1 << 16; | |
2821fd0c | 549 | unsigned long *bits = bitmap_zalloc(nbits, GFP_KERNEL); |
857cca4d RV |
550 | if (!bits) |
551 | return; | |
552 | ||
553 | bitmap_set(bits, 1, 20); | |
554 | bitmap_set(bits, 60000, 15); | |
555 | test("1-20,60000-60014", "%*pbl", nbits, bits); | |
2821fd0c | 556 | bitmap_free(bits); |
857cca4d RV |
557 | } |
558 | ||
707cc728 RV |
559 | static void __init |
560 | bitmap(void) | |
561 | { | |
562 | DECLARE_BITMAP(bits, 20); | |
563 | const int primes[] = {2,3,5,7,11,13,17,19}; | |
564 | int i; | |
565 | ||
566 | bitmap_zero(bits, 20); | |
567 | test("00000|00000", "%20pb|%*pb", bits, 20, bits); | |
568 | test("|", "%20pbl|%*pbl", bits, 20, bits); | |
569 | ||
570 | for (i = 0; i < ARRAY_SIZE(primes); ++i) | |
571 | set_bit(primes[i], bits); | |
572 | test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits); | |
573 | test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits); | |
574 | ||
575 | bitmap_fill(bits, 20); | |
576 | test("fffff|fffff", "%20pb|%*pb", bits, 20, bits); | |
577 | test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits); | |
857cca4d RV |
578 | |
579 | large_bitmap(); | |
707cc728 RV |
580 | } |
581 | ||
582 | static void __init | |
583 | netdev_features(void) | |
584 | { | |
585 | } | |
586 | ||
c244297a YS |
587 | struct page_flags_test { |
588 | int width; | |
589 | int shift; | |
590 | int mask; | |
c244297a YS |
591 | const char *fmt; |
592 | const char *name; | |
593 | }; | |
594 | ||
c666d447 | 595 | static const struct page_flags_test pft[] = { |
c244297a | 596 | {SECTIONS_WIDTH, SECTIONS_PGSHIFT, SECTIONS_MASK, |
c666d447 | 597 | "%d", "section"}, |
c244297a | 598 | {NODES_WIDTH, NODES_PGSHIFT, NODES_MASK, |
c666d447 | 599 | "%d", "node"}, |
c244297a | 600 | {ZONES_WIDTH, ZONES_PGSHIFT, ZONES_MASK, |
c666d447 | 601 | "%d", "zone"}, |
c244297a | 602 | {LAST_CPUPID_WIDTH, LAST_CPUPID_PGSHIFT, LAST_CPUPID_MASK, |
c666d447 | 603 | "%#x", "lastcpupid"}, |
c244297a | 604 | {KASAN_TAG_WIDTH, KASAN_TAG_PGSHIFT, KASAN_TAG_MASK, |
c666d447 | 605 | "%#x", "kasantag"}, |
c244297a YS |
606 | }; |
607 | ||
608 | static void __init | |
609 | page_flags_test(int section, int node, int zone, int last_cpupid, | |
a25a0854 MWO |
610 | int kasan_tag, unsigned long flags, const char *name, |
611 | char *cmp_buf) | |
c244297a YS |
612 | { |
613 | unsigned long values[] = {section, node, zone, last_cpupid, kasan_tag}; | |
23efd080 | 614 | unsigned long size; |
c244297a YS |
615 | bool append = false; |
616 | int i; | |
617 | ||
23efd080 MWO |
618 | for (i = 0; i < ARRAY_SIZE(values); i++) |
619 | flags |= (values[i] & pft[i].mask) << pft[i].shift; | |
620 | ||
621 | size = scnprintf(cmp_buf, BUF_SIZE, "%#lx(", flags); | |
a25a0854 | 622 | if (flags & PAGEFLAGS_MASK) { |
507f9860 | 623 | size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s", name); |
5b358b0d | 624 | append = true; |
c244297a YS |
625 | } |
626 | ||
c244297a YS |
627 | for (i = 0; i < ARRAY_SIZE(pft); i++) { |
628 | if (!pft[i].width) | |
629 | continue; | |
630 | ||
507f9860 MWO |
631 | if (append) |
632 | size += scnprintf(cmp_buf + size, BUF_SIZE - size, "|"); | |
c244297a | 633 | |
507f9860 MWO |
634 | size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s=", |
635 | pft[i].name); | |
636 | size += scnprintf(cmp_buf + size, BUF_SIZE - size, pft[i].fmt, | |
637 | values[i] & pft[i].mask); | |
c244297a YS |
638 | append = true; |
639 | } | |
640 | ||
23efd080 MWO |
641 | snprintf(cmp_buf + size, BUF_SIZE - size, ")"); |
642 | ||
a25a0854 | 643 | test(cmp_buf, "%pGp", &flags); |
c244297a YS |
644 | } |
645 | ||
4c85c0be HY |
646 | static void __init page_type_test(unsigned int page_type, const char *name, |
647 | char *cmp_buf) | |
648 | { | |
649 | unsigned long size; | |
650 | ||
651 | size = scnprintf(cmp_buf, BUF_SIZE, "%#x(", page_type); | |
652 | if (page_type_has_type(page_type)) | |
653 | size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s", name); | |
654 | ||
655 | snprintf(cmp_buf + size, BUF_SIZE - size, ")"); | |
656 | test(cmp_buf, "%pGt", &page_type); | |
657 | } | |
658 | ||
edf14cdb VB |
659 | static void __init |
660 | flags(void) | |
661 | { | |
662 | unsigned long flags; | |
edf14cdb | 663 | char *cmp_buffer; |
c244297a | 664 | gfp_t gfp; |
4c85c0be | 665 | unsigned int page_type; |
c244297a YS |
666 | |
667 | cmp_buffer = kmalloc(BUF_SIZE, GFP_KERNEL); | |
668 | if (!cmp_buffer) | |
669 | return; | |
edf14cdb VB |
670 | |
671 | flags = 0; | |
c244297a | 672 | page_flags_test(0, 0, 0, 0, 0, flags, "", cmp_buffer); |
edf14cdb | 673 | |
edf14cdb | 674 | flags = 1UL << NR_PAGEFLAGS; |
c244297a | 675 | page_flags_test(0, 0, 0, 0, 0, flags, "", cmp_buffer); |
edf14cdb VB |
676 | |
677 | flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru | |
678 | | 1UL << PG_active | 1UL << PG_swapbacked; | |
c244297a YS |
679 | page_flags_test(1, 1, 1, 0x1fffff, 1, flags, |
680 | "uptodate|dirty|lru|active|swapbacked", | |
681 | cmp_buffer); | |
edf14cdb | 682 | |
8d0920bd DH |
683 | flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC; |
684 | test("read|exec|mayread|maywrite|mayexec", "%pGv", &flags); | |
edf14cdb VB |
685 | |
686 | gfp = GFP_TRANSHUGE; | |
687 | test("GFP_TRANSHUGE", "%pGg", &gfp); | |
688 | ||
689 | gfp = GFP_ATOMIC|__GFP_DMA; | |
690 | test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp); | |
691 | ||
2973d822 N |
692 | gfp = __GFP_HIGH; |
693 | test("__GFP_HIGH", "%pGg", &gfp); | |
edf14cdb | 694 | |
edf14cdb VB |
695 | /* Any flags not translated by the table should remain numeric */ |
696 | gfp = ~__GFP_BITS_MASK; | |
697 | snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp); | |
698 | test(cmp_buffer, "%pGg", &gfp); | |
699 | ||
2973d822 | 700 | snprintf(cmp_buffer, BUF_SIZE, "__GFP_HIGH|%#lx", |
edf14cdb | 701 | (unsigned long) gfp); |
2973d822 | 702 | gfp |= __GFP_HIGH; |
edf14cdb VB |
703 | test(cmp_buffer, "%pGg", &gfp); |
704 | ||
4c85c0be HY |
705 | page_type = ~0; |
706 | page_type_test(page_type, "", cmp_buffer); | |
707 | ||
708 | page_type = 10; | |
709 | page_type_test(page_type, "", cmp_buffer); | |
710 | ||
711 | page_type = ~PG_buddy; | |
712 | page_type_test(page_type, "buddy", cmp_buffer); | |
713 | ||
714 | page_type = ~(PG_table | PG_buddy); | |
715 | page_type_test(page_type, "table|buddy", cmp_buffer); | |
716 | ||
edf14cdb VB |
717 | kfree(cmp_buffer); |
718 | } | |
719 | ||
f1ce39df SA |
720 | static void __init fwnode_pointer(void) |
721 | { | |
fd070e8c AS |
722 | const struct software_node first = { .name = "first" }; |
723 | const struct software_node second = { .name = "second", .parent = &first }; | |
724 | const struct software_node third = { .name = "third", .parent = &second }; | |
725 | const struct software_node *group[] = { &first, &second, &third, NULL }; | |
f1ce39df | 726 | const char * const full_name_second = "first/second"; |
fd070e8c | 727 | const char * const full_name_third = "first/second/third"; |
f1ce39df SA |
728 | const char * const second_name = "second"; |
729 | const char * const third_name = "third"; | |
730 | int rval; | |
731 | ||
fd070e8c | 732 | rval = software_node_register_node_group(group); |
f1ce39df SA |
733 | if (rval) { |
734 | pr_warn("cannot register softnodes; rval %d\n", rval); | |
735 | return; | |
736 | } | |
737 | ||
fd070e8c AS |
738 | test(full_name_second, "%pfw", software_node_fwnode(&second)); |
739 | test(full_name_third, "%pfw", software_node_fwnode(&third)); | |
740 | test(full_name_third, "%pfwf", software_node_fwnode(&third)); | |
741 | test(second_name, "%pfwP", software_node_fwnode(&second)); | |
742 | test(third_name, "%pfwP", software_node_fwnode(&third)); | |
f1ce39df | 743 | |
fd070e8c | 744 | software_node_unregister_node_group(group); |
f1ce39df SA |
745 | } |
746 | ||
af612e43 SA |
747 | static void __init fourcc_pointer(void) |
748 | { | |
749 | struct { | |
750 | u32 code; | |
751 | char *str; | |
752 | } const try[] = { | |
753 | { 0x3231564e, "NV12 little-endian (0x3231564e)", }, | |
754 | { 0xb231564e, "NV12 big-endian (0xb231564e)", }, | |
755 | { 0x10111213, ".... little-endian (0x10111213)", }, | |
756 | { 0x20303159, "Y10 little-endian (0x20303159)", }, | |
757 | }; | |
758 | unsigned int i; | |
759 | ||
760 | for (i = 0; i < ARRAY_SIZE(try); i++) | |
761 | test(try[i].str, "%p4cc", &try[i].code); | |
762 | } | |
763 | ||
57f5677e RV |
764 | static void __init |
765 | errptr(void) | |
766 | { | |
767 | test("-1234", "%pe", ERR_PTR(-1234)); | |
768 | ||
769 | /* Check that %pe with a non-ERR_PTR gets treated as ordinary %p. */ | |
770 | BUILD_BUG_ON(IS_ERR(PTR)); | |
771 | test_hashed("%pe", PTR); | |
772 | ||
773 | #ifdef CONFIG_SYMBOLIC_ERRNAME | |
774 | test("(-ENOTSOCK)", "(%pe)", ERR_PTR(-ENOTSOCK)); | |
775 | test("(-EAGAIN)", "(%pe)", ERR_PTR(-EAGAIN)); | |
776 | BUILD_BUG_ON(EAGAIN != EWOULDBLOCK); | |
777 | test("(-EAGAIN)", "(%pe)", ERR_PTR(-EWOULDBLOCK)); | |
778 | test("[-EIO ]", "[%-8pe]", ERR_PTR(-EIO)); | |
779 | test("[ -EIO]", "[%8pe]", ERR_PTR(-EIO)); | |
780 | test("-EPROBE_DEFER", "%pe", ERR_PTR(-EPROBE_DEFER)); | |
781 | #endif | |
782 | } | |
783 | ||
707cc728 RV |
784 | static void __init |
785 | test_pointer(void) | |
786 | { | |
787 | plain(); | |
3e5903eb | 788 | null_pointer(); |
7bd57fbc | 789 | error_pointer(); |
3e5903eb | 790 | invalid_pointer(); |
707cc728 RV |
791 | symbol_ptr(); |
792 | kernel_ptr(); | |
793 | struct_resource(); | |
794 | addr(); | |
795 | escaped_str(); | |
796 | hex_string(); | |
797 | mac(); | |
798 | ip(); | |
799 | uuid(); | |
800 | dentry(); | |
801 | struct_va_format(); | |
7daac5b2 | 802 | time_and_date(); |
707cc728 RV |
803 | struct_clk(); |
804 | bitmap(); | |
805 | netdev_features(); | |
edf14cdb | 806 | flags(); |
57f5677e | 807 | errptr(); |
f1ce39df | 808 | fwnode_pointer(); |
af612e43 | 809 | fourcc_pointer(); |
707cc728 RV |
810 | } |
811 | ||
6b1a4d5b | 812 | static void __init selftest(void) |
707cc728 | 813 | { |
331e4deb RV |
814 | alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL); |
815 | if (!alloced_buffer) | |
6b1a4d5b | 816 | return; |
331e4deb | 817 | test_buffer = alloced_buffer + PAD_SIZE; |
707cc728 RV |
818 | |
819 | test_basic(); | |
820 | test_number(); | |
821 | test_string(); | |
822 | test_pointer(); | |
823 | ||
331e4deb | 824 | kfree(alloced_buffer); |
707cc728 RV |
825 | } |
826 | ||
6b1a4d5b | 827 | KSTM_MODULE_LOADERS(test_printf); |
707cc728 RV |
828 | MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>"); |
829 | MODULE_LICENSE("GPL"); |