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