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