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