1 // SPDX-License-Identifier: GPL-2.0-only
3 * Test cases for printf facility.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
18 #include <linux/bitmap.h>
19 #include <linux/dcache.h>
20 #include <linux/socket.h>
23 #include <linux/gfp.h>
26 #include <linux/property.h>
28 #include "../tools/testing/selftests/kselftest_module.h"
34 #define NOWARN(option, comment, block) \
36 __diag_ignore_all(#option, comment); \
40 KSTM_MODULE_GLOBALS();
42 static char *test_buffer __initdata
;
43 static char *alloced_buffer __initdata
;
45 static int __printf(4, 0) __init
46 do_test(int bufsize
, const char *expect
, int elen
,
47 const char *fmt
, va_list ap
)
54 memset(alloced_buffer
, FILL_CHAR
, BUF_SIZE
+ 2*PAD_SIZE
);
56 ret
= vsnprintf(test_buffer
, bufsize
, fmt
, aq
);
60 pr_warn("vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
61 bufsize
, fmt
, ret
, elen
);
65 if (memchr_inv(alloced_buffer
, FILL_CHAR
, PAD_SIZE
)) {
66 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize
, fmt
);
71 if (memchr_inv(test_buffer
, FILL_CHAR
, BUF_SIZE
+ PAD_SIZE
)) {
72 pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
79 written
= min(bufsize
-1, elen
);
80 if (test_buffer
[written
]) {
81 pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
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",
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
);
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
);
105 static void __printf(3, 4) __init
106 __test(const char *expect
, int elen
, const char *fmt
, ...)
112 if (elen
>= BUF_SIZE
) {
113 pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n",
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.
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
);
133 p
= kvasprintf(GFP_KERNEL
, fmt
, ap
);
136 if (memcmp(p
, expect
, elen
+1)) {
137 pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
146 #define test(expect, fmt, ...) \
147 __test(expect, strlen(expect), fmt, ##__VA_ARGS__)
152 /* Work around annoying "warning: zero-length gnu_printf format string". */
156 test("100%", "100%%");
157 test("xxx%yyy", "xxx%cyyy", '%');
158 __test("xxx\0yyy", 7, "xxx%cyyy", '\0');
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);
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
180 test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
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");
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.
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.
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");
210 #define PLAIN_BUF_SIZE 64 /* leave some space so we don't oops */
212 #if BITS_PER_LONG == 64
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 */
224 char buf
[PLAIN_BUF_SIZE
];
227 nchars
= snprintf(buf
, PLAIN_BUF_SIZE
, "%p", PTR
);
229 if (nchars
!= PTR_WIDTH
)
232 if (strncmp(buf
, PTR_VAL_NO_CRNG
, PTR_WIDTH
) == 0) {
233 pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
238 if (strncmp(buf
, ZEROS
, strlen(ZEROS
)) != 0)
247 #define PTR ((void *)0x456789ab)
248 #define PTR_STR "456789ab"
249 #define PTR_VAL_NO_CRNG "(ptrval)"
256 /* Format is implicitly tested for 32 bit machines by plain_hash() */
260 #endif /* BITS_PER_LONG == 64 */
263 plain_hash_to_buffer(const void *p
, char *buf
, size_t len
)
267 nchars
= snprintf(buf
, len
, "%p", p
);
269 if (nchars
!= PTR_WIDTH
)
272 if (strncmp(buf
, PTR_VAL_NO_CRNG
, PTR_WIDTH
) == 0) {
273 pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
284 char buf
[PLAIN_BUF_SIZE
];
287 ret
= plain_hash_to_buffer(PTR
, buf
, PLAIN_BUF_SIZE
);
291 if (strncmp(buf
, PTR_STR
, PTR_WIDTH
) == 0)
298 * We can't use test() to test %p because we don't know what output to expect
299 * after an address is hashed.
306 if (no_hash_pointers
) {
307 pr_warn("skipping plain 'p' tests");
314 pr_warn("plain 'p' does not appear to be hashed\n");
319 err
= plain_format();
321 pr_warn("hashing plain 'p' has unexpected format\n");
327 test_hashed(const char *fmt
, const void *p
)
329 char buf
[PLAIN_BUF_SIZE
];
333 * No need to increase failed test counter since this is assumed
334 * to be called after plain().
336 ret
= plain_hash_to_buffer(p
, buf
, PLAIN_BUF_SIZE
);
344 * NULL pointers aren't hashed.
349 test(ZEROS
"00000000", "%p", NULL
);
350 test(ZEROS
"00000000", "%px", NULL
);
351 test("(null)", "%pE", NULL
);
355 * Error pointers aren't hashed.
360 test(ONES
"fffffff5", "%p", ERR_PTR(-11));
361 test(ONES
"fffffff5", "%px", ERR_PTR(-11));
362 test("(efault)", "%pE", ERR_PTR(-11));
365 #define PTR_INVALID ((void *)0x000000ab)
368 invalid_pointer(void)
370 test_hashed("%p", PTR_INVALID
);
371 test(ZEROS
"000000ab", "%px", PTR_INVALID
);
372 test("(efault)", "%pE", PTR_INVALID
);
383 /* We can't test this without access to kptr_restrict. */
387 struct_resource(void)
404 const char buf
[3] = {0xc0, 0xff, 0xee};
406 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
407 "%3ph|%3phC|%3phD|%3phN", buf
, buf
, buf
, buf
);
408 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
409 "%*ph|%*phC|%*phD|%*phN", 3, buf
, 3, buf
, 3, buf
, 3, buf
);
415 const u8 addr
[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
417 test("2d:48:d6:fc:7a:05", "%pM", addr
);
418 test("05:7a:fc:d6:48:2d", "%pMR", addr
);
419 test("2d-48-d6-fc-7a-05", "%pMF", addr
);
420 test("2d48d6fc7a05", "%pm", addr
);
421 test("057afcd6482d", "%pmR", addr
);
427 struct sockaddr_in sa
;
429 sa
.sin_family
= AF_INET
;
430 sa
.sin_port
= cpu_to_be16(12345);
431 sa
.sin_addr
.s_addr
= cpu_to_be32(0x7f000001);
433 test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa
.sin_addr
, &sa
.sin_addr
);
434 test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa
, &sa
);
435 sa
.sin_addr
.s_addr
= cpu_to_be32(0x01020304);
436 test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa
, &sa
);
454 const char uuid
[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
455 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
457 test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid
);
458 test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid
);
459 test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid
);
460 test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid
);
463 static struct dentry test_dentry
[4] __initdata
= {
464 { .d_parent
= &test_dentry
[0],
465 .d_name
= QSTR_INIT(test_dentry
[0].d_iname
, 3),
467 { .d_parent
= &test_dentry
[0],
468 .d_name
= QSTR_INIT(test_dentry
[1].d_iname
, 5),
469 .d_iname
= "bravo" },
470 { .d_parent
= &test_dentry
[1],
471 .d_name
= QSTR_INIT(test_dentry
[2].d_iname
, 4),
473 { .d_parent
= &test_dentry
[2],
474 .d_name
= QSTR_INIT(test_dentry
[3].d_iname
, 5),
475 .d_iname
= "romeo" },
481 test("foo", "%pd", &test_dentry
[0]);
482 test("foo", "%pd2", &test_dentry
[0]);
484 test("(null)", "%pd", NULL
);
485 test("(efault)", "%pd", PTR_INVALID
);
486 test("(null)", "%pD", NULL
);
487 test("(efault)", "%pD", PTR_INVALID
);
489 test("romeo", "%pd", &test_dentry
[3]);
490 test("alfa/romeo", "%pd2", &test_dentry
[3]);
491 test("bravo/alfa/romeo", "%pd3", &test_dentry
[3]);
492 test("/bravo/alfa/romeo", "%pd4", &test_dentry
[3]);
493 test("/bravo/alfa", "%pd4", &test_dentry
[2]);
495 test("bravo/alfa |bravo/alfa ", "%-12pd2|%*pd2", &test_dentry
[2], -12, &test_dentry
[2]);
496 test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry
[2], 12, &test_dentry
[2]);
500 struct_va_format(void)
508 const struct rtc_time tm
= {
516 /* 2019-01-04T15:32:23 */
517 time64_t t
= 1546615943;
519 test("(%pt?)", "%pt", &tm
);
520 test("2018-11-26T05:35:43", "%ptR", &tm
);
521 test("0118-10-26T05:35:43", "%ptRr", &tm
);
522 test("05:35:43|2018-11-26", "%ptRt|%ptRd", &tm
, &tm
);
523 test("05:35:43|0118-10-26", "%ptRtr|%ptRdr", &tm
, &tm
);
524 test("05:35:43|2018-11-26", "%ptRttr|%ptRdtr", &tm
, &tm
);
525 test("05:35:43 tr|2018-11-26 tr", "%ptRt tr|%ptRd tr", &tm
, &tm
);
527 test("2019-01-04T15:32:23", "%ptT", &t
);
528 test("0119-00-04T15:32:23", "%ptTr", &t
);
529 test("15:32:23|2019-01-04", "%ptTt|%ptTd", &t
, &t
);
530 test("15:32:23|0119-00-04", "%ptTtr|%ptTdr", &t
, &t
);
532 test("2019-01-04 15:32:23", "%ptTs", &t
);
533 test("0119-00-04 15:32:23", "%ptTsr", &t
);
534 test("15:32:23|2019-01-04", "%ptTts|%ptTds", &t
, &t
);
535 test("15:32:23|0119-00-04", "%ptTtrs|%ptTdrs", &t
, &t
);
546 const int nbits
= 1 << 16;
547 unsigned long *bits
= bitmap_zalloc(nbits
, GFP_KERNEL
);
551 bitmap_set(bits
, 1, 20);
552 bitmap_set(bits
, 60000, 15);
553 test("1-20,60000-60014", "%*pbl", nbits
, bits
);
560 DECLARE_BITMAP(bits
, 20);
561 const int primes
[] = {2,3,5,7,11,13,17,19};
564 bitmap_zero(bits
, 20);
565 test("00000|00000", "%20pb|%*pb", bits
, 20, bits
);
566 test("|", "%20pbl|%*pbl", bits
, 20, bits
);
568 for (i
= 0; i
< ARRAY_SIZE(primes
); ++i
)
569 set_bit(primes
[i
], bits
);
570 test("a28ac|a28ac", "%20pb|%*pb", bits
, 20, bits
);
571 test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits
, 20, bits
);
573 bitmap_fill(bits
, 20);
574 test("fffff|fffff", "%20pb|%*pb", bits
, 20, bits
);
575 test("0-19|0-19", "%20pbl|%*pbl", bits
, 20, bits
);
581 netdev_features(void)
585 struct page_flags_test
{
593 static const struct page_flags_test pft
[] = {
594 {SECTIONS_WIDTH
, SECTIONS_PGSHIFT
, SECTIONS_MASK
,
596 {NODES_WIDTH
, NODES_PGSHIFT
, NODES_MASK
,
598 {ZONES_WIDTH
, ZONES_PGSHIFT
, ZONES_MASK
,
600 {LAST_CPUPID_WIDTH
, LAST_CPUPID_PGSHIFT
, LAST_CPUPID_MASK
,
601 "%#x", "lastcpupid"},
602 {KASAN_TAG_WIDTH
, KASAN_TAG_PGSHIFT
, KASAN_TAG_MASK
,
607 page_flags_test(int section
, int node
, int zone
, int last_cpupid
,
608 int kasan_tag
, unsigned long flags
, const char *name
,
611 unsigned long values
[] = {section
, node
, zone
, last_cpupid
, kasan_tag
};
616 for (i
= 0; i
< ARRAY_SIZE(values
); i
++)
617 flags
|= (values
[i
] & pft
[i
].mask
) << pft
[i
].shift
;
619 size
= scnprintf(cmp_buf
, BUF_SIZE
, "%#lx(", flags
);
620 if (flags
& PAGEFLAGS_MASK
) {
621 size
+= scnprintf(cmp_buf
+ size
, BUF_SIZE
- size
, "%s", name
);
625 for (i
= 0; i
< ARRAY_SIZE(pft
); i
++) {
630 size
+= scnprintf(cmp_buf
+ size
, BUF_SIZE
- size
, "|");
632 size
+= scnprintf(cmp_buf
+ size
, BUF_SIZE
- size
, "%s=",
634 size
+= scnprintf(cmp_buf
+ size
, BUF_SIZE
- size
, pft
[i
].fmt
,
635 values
[i
] & pft
[i
].mask
);
639 snprintf(cmp_buf
+ size
, BUF_SIZE
- size
, ")");
641 test(cmp_buf
, "%pGp", &flags
);
651 cmp_buffer
= kmalloc(BUF_SIZE
, GFP_KERNEL
);
656 page_flags_test(0, 0, 0, 0, 0, flags
, "", cmp_buffer
);
658 flags
= 1UL << NR_PAGEFLAGS
;
659 page_flags_test(0, 0, 0, 0, 0, flags
, "", cmp_buffer
);
661 flags
|= 1UL << PG_uptodate
| 1UL << PG_dirty
| 1UL << PG_lru
662 | 1UL << PG_active
| 1UL << PG_swapbacked
;
663 page_flags_test(1, 1, 1, 0x1fffff, 1, flags
,
664 "uptodate|dirty|lru|active|swapbacked",
667 flags
= VM_READ
| VM_EXEC
| VM_MAYREAD
| VM_MAYWRITE
| VM_MAYEXEC
;
668 test("read|exec|mayread|maywrite|mayexec", "%pGv", &flags
);
671 test("GFP_TRANSHUGE", "%pGg", &gfp
);
673 gfp
= GFP_ATOMIC
|__GFP_DMA
;
674 test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp
);
677 test("__GFP_HIGH", "%pGg", &gfp
);
679 /* Any flags not translated by the table should remain numeric */
680 gfp
= ~__GFP_BITS_MASK
;
681 snprintf(cmp_buffer
, BUF_SIZE
, "%#lx", (unsigned long) gfp
);
682 test(cmp_buffer
, "%pGg", &gfp
);
684 snprintf(cmp_buffer
, BUF_SIZE
, "__GFP_HIGH|%#lx",
685 (unsigned long) gfp
);
687 test(cmp_buffer
, "%pGg", &gfp
);
692 static void __init
fwnode_pointer(void)
694 const struct software_node first
= { .name
= "first" };
695 const struct software_node second
= { .name
= "second", .parent
= &first
};
696 const struct software_node third
= { .name
= "third", .parent
= &second
};
697 const struct software_node
*group
[] = { &first
, &second
, &third
, NULL
};
698 const char * const full_name_second
= "first/second";
699 const char * const full_name_third
= "first/second/third";
700 const char * const second_name
= "second";
701 const char * const third_name
= "third";
704 rval
= software_node_register_node_group(group
);
706 pr_warn("cannot register softnodes; rval %d\n", rval
);
710 test(full_name_second
, "%pfw", software_node_fwnode(&second
));
711 test(full_name_third
, "%pfw", software_node_fwnode(&third
));
712 test(full_name_third
, "%pfwf", software_node_fwnode(&third
));
713 test(second_name
, "%pfwP", software_node_fwnode(&second
));
714 test(third_name
, "%pfwP", software_node_fwnode(&third
));
716 software_node_unregister_node_group(group
);
719 static void __init
fourcc_pointer(void)
725 { 0x3231564e, "NV12 little-endian (0x3231564e)", },
726 { 0xb231564e, "NV12 big-endian (0xb231564e)", },
727 { 0x10111213, ".... little-endian (0x10111213)", },
728 { 0x20303159, "Y10 little-endian (0x20303159)", },
732 for (i
= 0; i
< ARRAY_SIZE(try); i
++)
733 test(try[i
].str
, "%p4cc", &try[i
].code
);
739 test("-1234", "%pe", ERR_PTR(-1234));
741 /* Check that %pe with a non-ERR_PTR gets treated as ordinary %p. */
742 BUILD_BUG_ON(IS_ERR(PTR
));
743 test_hashed("%pe", PTR
);
745 #ifdef CONFIG_SYMBOLIC_ERRNAME
746 test("(-ENOTSOCK)", "(%pe)", ERR_PTR(-ENOTSOCK
));
747 test("(-EAGAIN)", "(%pe)", ERR_PTR(-EAGAIN
));
748 BUILD_BUG_ON(EAGAIN
!= EWOULDBLOCK
);
749 test("(-EAGAIN)", "(%pe)", ERR_PTR(-EWOULDBLOCK
));
750 test("[-EIO ]", "[%-8pe]", ERR_PTR(-EIO
));
751 test("[ -EIO]", "[%8pe]", ERR_PTR(-EIO
));
752 test("-EPROBE_DEFER", "%pe", ERR_PTR(-EPROBE_DEFER
));
784 static void __init
selftest(void)
786 alloced_buffer
= kmalloc(BUF_SIZE
+ 2*PAD_SIZE
, GFP_KERNEL
);
789 test_buffer
= alloced_buffer
+ PAD_SIZE
;
796 kfree(alloced_buffer
);
799 KSTM_MODULE_LOADERS(test_printf
);
800 MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
801 MODULE_DESCRIPTION("Test cases for printf facility");
802 MODULE_LICENSE("GPL");