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