blob: 0a3153f162c7e9d64334bdb0d3c82aaec8366b6b [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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/string.h>
16
17#include <linux/bitmap.h>
18#include <linux/dcache.h>
19#include <linux/socket.h>
20#include <linux/in.h>
21
22#include <linux/gfp.h>
23#include <linux/mm.h>
24
25#include "../tools/testing/selftests/kselftest_module.h"
26
27#define BUF_SIZE 256
28#define PAD_SIZE 16
29#define FILL_CHAR '$'
30
31KSTM_MODULE_GLOBALS();
32
33static char *test_buffer __initdata;
34static char *alloced_buffer __initdata;
35
36extern bool no_hash_pointers;
37
38static int __printf(4, 0) __init
39do_test(int bufsize, const char *expect, int elen,
40 const char *fmt, va_list ap)
41{
42 va_list aq;
43 int ret, written;
44
45 total_tests++;
46
47 memset(alloced_buffer, FILL_CHAR, BUF_SIZE + 2*PAD_SIZE);
48 va_copy(aq, ap);
49 ret = vsnprintf(test_buffer, bufsize, fmt, aq);
50 va_end(aq);
51
52 if (ret != elen) {
53 pr_warn("vsnprintf(buf, %d, \"%s\", ...) returned %d, expected %d\n",
54 bufsize, fmt, ret, elen);
55 return 1;
56 }
57
58 if (memchr_inv(alloced_buffer, FILL_CHAR, PAD_SIZE)) {
59 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote before buffer\n", bufsize, fmt);
60 return 1;
61 }
62
63 if (!bufsize) {
64 if (memchr_inv(test_buffer, FILL_CHAR, BUF_SIZE + PAD_SIZE)) {
65 pr_warn("vsnprintf(buf, 0, \"%s\", ...) wrote to buffer\n",
66 fmt);
67 return 1;
68 }
69 return 0;
70 }
71
72 written = min(bufsize-1, elen);
73 if (test_buffer[written]) {
74 pr_warn("vsnprintf(buf, %d, \"%s\", ...) did not nul-terminate buffer\n",
75 bufsize, fmt);
76 return 1;
77 }
78
79 if (memchr_inv(test_buffer + written + 1, FILL_CHAR, BUF_SIZE + PAD_SIZE - (written + 1))) {
80 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote beyond the nul-terminator\n",
81 bufsize, fmt);
82 return 1;
83 }
84
85 if (memcmp(test_buffer, expect, written)) {
86 pr_warn("vsnprintf(buf, %d, \"%s\", ...) wrote '%s', expected '%.*s'\n",
87 bufsize, fmt, test_buffer, written, expect);
88 return 1;
89 }
90 return 0;
91}
92
93static void __printf(3, 4) __init
94__test(const char *expect, int elen, const char *fmt, ...)
95{
96 va_list ap;
97 int rand;
98 char *p;
99
100 if (elen >= BUF_SIZE) {
101 pr_err("error in test suite: expected output length %d too long. Format was '%s'.\n",
102 elen, fmt);
103 failed_tests++;
104 return;
105 }
106
107 va_start(ap, fmt);
108
109 /*
110 * Every fmt+args is subjected to four tests: Three where we
111 * tell vsnprintf varying buffer sizes (plenty, not quite
112 * enough and 0), and then we also test that kvasprintf would
113 * be able to print it as expected.
114 */
115 failed_tests += do_test(BUF_SIZE, expect, elen, fmt, ap);
116 rand = 1 + prandom_u32_max(elen+1);
117 /* Since elen < BUF_SIZE, we have 1 <= rand <= BUF_SIZE. */
118 failed_tests += do_test(rand, expect, elen, fmt, ap);
119 failed_tests += do_test(0, expect, elen, fmt, ap);
120
121 p = kvasprintf(GFP_KERNEL, fmt, ap);
122 if (p) {
123 total_tests++;
124 if (memcmp(p, expect, elen+1)) {
125 pr_warn("kvasprintf(..., \"%s\", ...) returned '%s', expected '%s'\n",
126 fmt, p, expect);
127 failed_tests++;
128 }
129 kfree(p);
130 }
131 va_end(ap);
132}
133
134#define test(expect, fmt, ...) \
135 __test(expect, strlen(expect), fmt, ##__VA_ARGS__)
136
137static void __init
138test_basic(void)
139{
140 /* Work around annoying "warning: zero-length gnu_printf format string". */
141 char nul = '\0';
142
143 test("", &nul);
144 test("100%", "100%%");
145 test("xxx%yyy", "xxx%cyyy", '%');
146 __test("xxx\0yyy", 7, "xxx%cyyy", '\0');
147}
148
149static void __init
150test_number(void)
151{
152 test("0x1234abcd ", "%#-12x", 0x1234abcd);
153 test(" 0x1234abcd", "%#12x", 0x1234abcd);
154 test("0|001| 12|+123| 1234|-123|-1234", "%d|%03d|%3d|%+d|% d|%+d|% d", 0, 1, 12, 123, 1234, -123, -1234);
155 test("0|1|1|128|255", "%hhu|%hhu|%hhu|%hhu|%hhu", 0, 1, 257, 128, -1);
156 test("0|1|1|-128|-1", "%hhd|%hhd|%hhd|%hhd|%hhd", 0, 1, 257, 128, -1);
157 test("2015122420151225", "%ho%ho%#ho", 1037, 5282, -11627);
158 /*
159 * POSIX/C99: »The result of converting zero with an explicit
160 * precision of zero shall be no characters.« Hence the output
161 * from the below test should really be "00|0||| ". However,
162 * the kernel's printf also produces a single 0 in that
163 * case. This test case simply documents the current
164 * behaviour.
165 */
166 test("00|0|0|0|0", "%.2d|%.1d|%.0d|%.*d|%1.0d", 0, 0, 0, 0, 0, 0);
167#ifndef __CHAR_UNSIGNED__
168 {
169 /*
170 * Passing a 'char' to a %02x specifier doesn't do
171 * what was presumably the intention when char is
172 * signed and the value is negative. One must either &
173 * with 0xff or cast to u8.
174 */
175 char val = -16;
176 test("0xfffffff0|0xf0|0xf0", "%#02x|%#02x|%#02x", val, val & 0xff, (u8)val);
177 }
178#endif
179}
180
181static void __init
182test_string(void)
183{
184 test("", "%s%.0s", "", "123");
185 test("ABCD|abc|123", "%s|%.3s|%.*s", "ABCD", "abcdef", 3, "123456");
186 test("1 | 2|3 | 4|5 ", "%-3s|%3s|%-*s|%*s|%*s", "1", "2", 3, "3", 3, "4", -3, "5");
187 test("1234 ", "%-10.4s", "123456");
188 test(" 1234", "%10.4s", "123456");
189 /*
190 * POSIX and C99 say that a negative precision (which is only
191 * possible to pass via a * argument) should be treated as if
192 * the precision wasn't present, and that if the precision is
193 * omitted (as in %.s), the precision should be taken to be
194 * 0. However, the kernel's printf behave exactly opposite,
195 * treating a negative precision as 0 and treating an omitted
196 * precision specifier as if no precision was given.
197 *
198 * These test cases document the current behaviour; should
199 * anyone ever feel the need to follow the standards more
200 * closely, this can be revisited.
201 */
202 test(" ", "%4.*s", -5, "123456");
203 test("123456", "%.s", "123456");
204 test("a||", "%.s|%.0s|%.*s", "a", "b", 0, "c");
205 test("a | | ", "%-3.s|%-3.0s|%-3.*s", "a", "b", 0, "c");
206}
207
208#define PLAIN_BUF_SIZE 64 /* leave some space so we don't oops */
209
210#if BITS_PER_LONG == 64
211
212#define PTR_WIDTH 16
213#define PTR ((void *)0xffff0123456789abUL)
214#define PTR_STR "ffff0123456789ab"
215#define PTR_VAL_NO_CRNG "(____ptrval____)"
216#define ZEROS "00000000" /* hex 32 zero bits */
217#define ONES "ffffffff" /* hex 32 one bits */
218
219static int __init
220plain_format(void)
221{
222 char buf[PLAIN_BUF_SIZE];
223 int nchars;
224
225 nchars = snprintf(buf, PLAIN_BUF_SIZE, "%p", PTR);
226
227 if (nchars != PTR_WIDTH)
228 return -1;
229
230 if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
231 pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
232 PTR_VAL_NO_CRNG);
233 return 0;
234 }
235
236 if (strncmp(buf, ZEROS, strlen(ZEROS)) != 0)
237 return -1;
238
239 return 0;
240}
241
242#else
243
244#define PTR_WIDTH 8
245#define PTR ((void *)0x456789ab)
246#define PTR_STR "456789ab"
247#define PTR_VAL_NO_CRNG "(ptrval)"
248#define ZEROS ""
249#define ONES ""
250
251static int __init
252plain_format(void)
253{
254 /* Format is implicitly tested for 32 bit machines by plain_hash() */
255 return 0;
256}
257
258#endif /* BITS_PER_LONG == 64 */
259
260static int __init
261plain_hash_to_buffer(const void *p, char *buf, size_t len)
262{
263 int nchars;
264
265 nchars = snprintf(buf, len, "%p", p);
266
267 if (nchars != PTR_WIDTH)
268 return -1;
269
270 if (strncmp(buf, PTR_VAL_NO_CRNG, PTR_WIDTH) == 0) {
271 pr_warn("crng possibly not yet initialized. plain 'p' buffer contains \"%s\"",
272 PTR_VAL_NO_CRNG);
273 return 0;
274 }
275
276 return 0;
277}
278
279static int __init
280plain_hash(void)
281{
282 char buf[PLAIN_BUF_SIZE];
283 int ret;
284
285 ret = plain_hash_to_buffer(PTR, buf, PLAIN_BUF_SIZE);
286 if (ret)
287 return ret;
288
289 if (strncmp(buf, PTR_STR, PTR_WIDTH) == 0)
290 return -1;
291
292 return 0;
293}
294
295/*
296 * We can't use test() to test %p because we don't know what output to expect
297 * after an address is hashed.
298 */
299static void __init
300plain(void)
301{
302 int err;
303
304 if (no_hash_pointers) {
305 pr_warn("skipping plain 'p' tests");
306 skipped_tests += 2;
307 return;
308 }
309
310 err = plain_hash();
311 if (err) {
312 pr_warn("plain 'p' does not appear to be hashed\n");
313 failed_tests++;
314 return;
315 }
316
317 err = plain_format();
318 if (err) {
319 pr_warn("hashing plain 'p' has unexpected format\n");
320 failed_tests++;
321 }
322}
323
324static void __init
325test_hashed(const char *fmt, const void *p)
326{
327 char buf[PLAIN_BUF_SIZE];
328 int ret;
329
330 /*
331 * No need to increase failed test counter since this is assumed
332 * to be called after plain().
333 */
334 ret = plain_hash_to_buffer(p, buf, PLAIN_BUF_SIZE);
335 if (ret)
336 return;
337
338 test(buf, fmt, p);
339}
340
341/*
342 * NULL pointers aren't hashed.
343 */
344static void __init
345null_pointer(void)
346{
347 test(ZEROS "00000000", "%p", NULL);
348 test(ZEROS "00000000", "%px", NULL);
349 test("(null)", "%pE", NULL);
350}
351
352/*
353 * Error pointers aren't hashed.
354 */
355static void __init
356error_pointer(void)
357{
358 test(ONES "fffffff5", "%p", ERR_PTR(-11));
359 test(ONES "fffffff5", "%px", ERR_PTR(-11));
360 test("(efault)", "%pE", ERR_PTR(-11));
361}
362
363#define PTR_INVALID ((void *)0x000000ab)
364
365static void __init
366invalid_pointer(void)
367{
368 test_hashed("%p", PTR_INVALID);
369 test(ZEROS "000000ab", "%px", PTR_INVALID);
370 test("(efault)", "%pE", PTR_INVALID);
371}
372
373static void __init
374symbol_ptr(void)
375{
376}
377
378static void __init
379kernel_ptr(void)
380{
381 /* We can't test this without access to kptr_restrict. */
382}
383
384static void __init
385struct_resource(void)
386{
387}
388
389static void __init
390addr(void)
391{
392}
393
394static void __init
395escaped_str(void)
396{
397}
398
399static void __init
400hex_string(void)
401{
402 const char buf[3] = {0xc0, 0xff, 0xee};
403
404 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
405 "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf);
406 test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
407 "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);
408}
409
410static void __init
411mac(void)
412{
413 const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
414
415 test("2d:48:d6:fc:7a:05", "%pM", addr);
416 test("05:7a:fc:d6:48:2d", "%pMR", addr);
417 test("2d-48-d6-fc-7a-05", "%pMF", addr);
418 test("2d48d6fc7a05", "%pm", addr);
419 test("057afcd6482d", "%pmR", addr);
420}
421
422static void __init
423ip4(void)
424{
425 struct sockaddr_in sa;
426
427 sa.sin_family = AF_INET;
428 sa.sin_port = cpu_to_be16(12345);
429 sa.sin_addr.s_addr = cpu_to_be32(0x7f000001);
430
431 test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr);
432 test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
433 sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
434 test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
435}
436
437static void __init
438ip6(void)
439{
440}
441
442static void __init
443ip(void)
444{
445 ip4();
446 ip6();
447}
448
449static void __init
450uuid(void)
451{
452 const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
453 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
454
455 test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid);
456 test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid);
457 test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid);
458 test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
459}
460
461static struct dentry test_dentry[4] __initdata = {
462 { .d_parent = &test_dentry[0],
463 .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),
464 .d_iname = "foo" },
465 { .d_parent = &test_dentry[0],
466 .d_name = QSTR_INIT(test_dentry[1].d_iname, 5),
467 .d_iname = "bravo" },
468 { .d_parent = &test_dentry[1],
469 .d_name = QSTR_INIT(test_dentry[2].d_iname, 4),
470 .d_iname = "alfa" },
471 { .d_parent = &test_dentry[2],
472 .d_name = QSTR_INIT(test_dentry[3].d_iname, 5),
473 .d_iname = "romeo" },
474};
475
476static void __init
477dentry(void)
478{
479 test("foo", "%pd", &test_dentry[0]);
480 test("foo", "%pd2", &test_dentry[0]);
481
482 test("(null)", "%pd", NULL);
483 test("(efault)", "%pd", PTR_INVALID);
484 test("(null)", "%pD", NULL);
485 test("(efault)", "%pD", PTR_INVALID);
486
487 test("romeo", "%pd", &test_dentry[3]);
488 test("alfa/romeo", "%pd2", &test_dentry[3]);
489 test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
490 test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]);
491 test("/bravo/alfa", "%pd4", &test_dentry[2]);
492
493 test("bravo/alfa |bravo/alfa ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
494 test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
495}
496
497static void __init
498struct_va_format(void)
499{
500}
501
502static void __init
503struct_rtc_time(void)
504{
505 /* 1543210543 */
506 const struct rtc_time tm = {
507 .tm_sec = 43,
508 .tm_min = 35,
509 .tm_hour = 5,
510 .tm_mday = 26,
511 .tm_mon = 10,
512 .tm_year = 118,
513 };
514
515 test("(%ptR?)", "%pt", &tm);
516 test("2018-11-26T05:35:43", "%ptR", &tm);
517 test("0118-10-26T05:35:43", "%ptRr", &tm);
518 test("05:35:43|2018-11-26", "%ptRt|%ptRd", &tm, &tm);
519 test("05:35:43|0118-10-26", "%ptRtr|%ptRdr", &tm, &tm);
520 test("05:35:43|2018-11-26", "%ptRttr|%ptRdtr", &tm, &tm);
521 test("05:35:43 tr|2018-11-26 tr", "%ptRt tr|%ptRd tr", &tm, &tm);
522}
523
524static void __init
525struct_clk(void)
526{
527}
528
529static void __init
530large_bitmap(void)
531{
532 const int nbits = 1 << 16;
533 unsigned long *bits = bitmap_zalloc(nbits, GFP_KERNEL);
534 if (!bits)
535 return;
536
537 bitmap_set(bits, 1, 20);
538 bitmap_set(bits, 60000, 15);
539 test("1-20,60000-60014", "%*pbl", nbits, bits);
540 bitmap_free(bits);
541}
542
543static void __init
544bitmap(void)
545{
546 DECLARE_BITMAP(bits, 20);
547 const int primes[] = {2,3,5,7,11,13,17,19};
548 int i;
549
550 bitmap_zero(bits, 20);
551 test("00000|00000", "%20pb|%*pb", bits, 20, bits);
552 test("|", "%20pbl|%*pbl", bits, 20, bits);
553
554 for (i = 0; i < ARRAY_SIZE(primes); ++i)
555 set_bit(primes[i], bits);
556 test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits);
557 test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits);
558
559 bitmap_fill(bits, 20);
560 test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
561 test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
562
563 large_bitmap();
564}
565
566static void __init
567netdev_features(void)
568{
569}
570
571static void __init
572flags(void)
573{
574 unsigned long flags;
575 gfp_t gfp;
576 char *cmp_buffer;
577
578 flags = 0;
579 test("", "%pGp", &flags);
580
581 /* Page flags should filter the zone id */
582 flags = 1UL << NR_PAGEFLAGS;
583 test("", "%pGp", &flags);
584
585 flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru
586 | 1UL << PG_active | 1UL << PG_swapbacked;
587 test("uptodate|dirty|lru|active|swapbacked", "%pGp", &flags);
588
589
590 flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC
591 | VM_DENYWRITE;
592 test("read|exec|mayread|maywrite|mayexec|denywrite", "%pGv", &flags);
593
594 gfp = GFP_TRANSHUGE;
595 test("GFP_TRANSHUGE", "%pGg", &gfp);
596
597 gfp = GFP_ATOMIC|__GFP_DMA;
598 test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp);
599
600 gfp = __GFP_ATOMIC;
601 test("__GFP_ATOMIC", "%pGg", &gfp);
602
603 cmp_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
604 if (!cmp_buffer)
605 return;
606
607 /* Any flags not translated by the table should remain numeric */
608 gfp = ~__GFP_BITS_MASK;
609 snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp);
610 test(cmp_buffer, "%pGg", &gfp);
611
612 snprintf(cmp_buffer, BUF_SIZE, "__GFP_ATOMIC|%#lx",
613 (unsigned long) gfp);
614 gfp |= __GFP_ATOMIC;
615 test(cmp_buffer, "%pGg", &gfp);
616
617 kfree(cmp_buffer);
618}
619
620static void __init
621test_pointer(void)
622{
623 plain();
624 null_pointer();
625 error_pointer();
626 invalid_pointer();
627 symbol_ptr();
628 kernel_ptr();
629 struct_resource();
630 addr();
631 escaped_str();
632 hex_string();
633 mac();
634 ip();
635 uuid();
636 dentry();
637 struct_va_format();
638 struct_rtc_time();
639 struct_clk();
640 bitmap();
641 netdev_features();
642 flags();
643}
644
645static void __init selftest(void)
646{
647 alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
648 if (!alloced_buffer)
649 return;
650 test_buffer = alloced_buffer + PAD_SIZE;
651
652 test_basic();
653 test_number();
654 test_string();
655 test_pointer();
656
657 kfree(alloced_buffer);
658}
659
660KSTM_MODULE_LOADERS(test_printf);
661MODULE_AUTHOR("Rasmus Villemoes <linux@rasmusvillemoes.dk>");
662MODULE_LICENSE("GPL");