test_printf.c 13 KB

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