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