test_printf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 *)0xffff0123456789ab)
  180. #define PTR_STR "ffff0123456789ab"
  181. #define ZEROS "00000000" /* hex 32 zero bits */
  182. static int __init
  183. plain_format(void)
  184. {
  185. char buf[PLAIN_BUF_SIZE];
  186. int nchars;
  187. nchars = snprintf(buf, PLAIN_BUF_SIZE, "%p", PTR);
  188. if (nchars != PTR_WIDTH || strncmp(buf, ZEROS, strlen(ZEROS)) != 0)
  189. return -1;
  190. return 0;
  191. }
  192. #else
  193. #define PTR_WIDTH 8
  194. #define PTR ((void *)0x456789ab)
  195. #define PTR_STR "456789ab"
  196. static int __init
  197. plain_format(void)
  198. {
  199. /* Format is implicitly tested for 32 bit machines by plain_hash() */
  200. return 0;
  201. }
  202. #endif /* BITS_PER_LONG == 64 */
  203. static int __init
  204. plain_hash(void)
  205. {
  206. char buf[PLAIN_BUF_SIZE];
  207. int nchars;
  208. nchars = snprintf(buf, PLAIN_BUF_SIZE, "%p", PTR);
  209. if (nchars != PTR_WIDTH || strncmp(buf, PTR_STR, PTR_WIDTH) == 0)
  210. return -1;
  211. return 0;
  212. }
  213. /*
  214. * We can't use test() to test %p because we don't know what output to expect
  215. * after an address is hashed.
  216. */
  217. static void __init
  218. plain(void)
  219. {
  220. int err;
  221. err = plain_hash();
  222. if (err) {
  223. pr_warn("plain 'p' does not appear to be hashed\n");
  224. failed_tests++;
  225. return;
  226. }
  227. err = plain_format();
  228. if (err) {
  229. pr_warn("hashing plain 'p' has unexpected format\n");
  230. failed_tests++;
  231. }
  232. }
  233. static void __init
  234. symbol_ptr(void)
  235. {
  236. }
  237. static void __init
  238. kernel_ptr(void)
  239. {
  240. /* We can't test this without access to kptr_restrict. */
  241. }
  242. static void __init
  243. struct_resource(void)
  244. {
  245. }
  246. static void __init
  247. addr(void)
  248. {
  249. }
  250. static void __init
  251. escaped_str(void)
  252. {
  253. }
  254. static void __init
  255. hex_string(void)
  256. {
  257. const char buf[3] = {0xc0, 0xff, 0xee};
  258. test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
  259. "%3ph|%3phC|%3phD|%3phN", buf, buf, buf, buf);
  260. test("c0 ff ee|c0:ff:ee|c0-ff-ee|c0ffee",
  261. "%*ph|%*phC|%*phD|%*phN", 3, buf, 3, buf, 3, buf, 3, buf);
  262. }
  263. static void __init
  264. mac(void)
  265. {
  266. const u8 addr[6] = {0x2d, 0x48, 0xd6, 0xfc, 0x7a, 0x05};
  267. test("2d:48:d6:fc:7a:05", "%pM", addr);
  268. test("05:7a:fc:d6:48:2d", "%pMR", addr);
  269. test("2d-48-d6-fc-7a-05", "%pMF", addr);
  270. test("2d48d6fc7a05", "%pm", addr);
  271. test("057afcd6482d", "%pmR", addr);
  272. }
  273. static void __init
  274. ip4(void)
  275. {
  276. struct sockaddr_in sa;
  277. sa.sin_family = AF_INET;
  278. sa.sin_port = cpu_to_be16(12345);
  279. sa.sin_addr.s_addr = cpu_to_be32(0x7f000001);
  280. test("127.000.000.001|127.0.0.1", "%pi4|%pI4", &sa.sin_addr, &sa.sin_addr);
  281. test("127.000.000.001|127.0.0.1", "%piS|%pIS", &sa, &sa);
  282. sa.sin_addr.s_addr = cpu_to_be32(0x01020304);
  283. test("001.002.003.004:12345|1.2.3.4:12345", "%piSp|%pISp", &sa, &sa);
  284. }
  285. static void __init
  286. ip6(void)
  287. {
  288. }
  289. static void __init
  290. ip(void)
  291. {
  292. ip4();
  293. ip6();
  294. }
  295. static void __init
  296. uuid(void)
  297. {
  298. const char uuid[16] = {0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
  299. 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
  300. test("00010203-0405-0607-0809-0a0b0c0d0e0f", "%pUb", uuid);
  301. test("00010203-0405-0607-0809-0A0B0C0D0E0F", "%pUB", uuid);
  302. test("03020100-0504-0706-0809-0a0b0c0d0e0f", "%pUl", uuid);
  303. test("03020100-0504-0706-0809-0A0B0C0D0E0F", "%pUL", uuid);
  304. }
  305. static struct dentry test_dentry[4] __initdata = {
  306. { .d_parent = &test_dentry[0],
  307. .d_name = QSTR_INIT(test_dentry[0].d_iname, 3),
  308. .d_iname = "foo" },
  309. { .d_parent = &test_dentry[0],
  310. .d_name = QSTR_INIT(test_dentry[1].d_iname, 5),
  311. .d_iname = "bravo" },
  312. { .d_parent = &test_dentry[1],
  313. .d_name = QSTR_INIT(test_dentry[2].d_iname, 4),
  314. .d_iname = "alfa" },
  315. { .d_parent = &test_dentry[2],
  316. .d_name = QSTR_INIT(test_dentry[3].d_iname, 5),
  317. .d_iname = "romeo" },
  318. };
  319. static void __init
  320. dentry(void)
  321. {
  322. test("foo", "%pd", &test_dentry[0]);
  323. test("foo", "%pd2", &test_dentry[0]);
  324. test("romeo", "%pd", &test_dentry[3]);
  325. test("alfa/romeo", "%pd2", &test_dentry[3]);
  326. test("bravo/alfa/romeo", "%pd3", &test_dentry[3]);
  327. test("/bravo/alfa/romeo", "%pd4", &test_dentry[3]);
  328. test("/bravo/alfa", "%pd4", &test_dentry[2]);
  329. test("bravo/alfa |bravo/alfa ", "%-12pd2|%*pd2", &test_dentry[2], -12, &test_dentry[2]);
  330. test(" bravo/alfa| bravo/alfa", "%12pd2|%*pd2", &test_dentry[2], 12, &test_dentry[2]);
  331. }
  332. static void __init
  333. struct_va_format(void)
  334. {
  335. }
  336. static void __init
  337. struct_clk(void)
  338. {
  339. }
  340. static void __init
  341. large_bitmap(void)
  342. {
  343. const int nbits = 1 << 16;
  344. unsigned long *bits = kcalloc(BITS_TO_LONGS(nbits), sizeof(long), GFP_KERNEL);
  345. if (!bits)
  346. return;
  347. bitmap_set(bits, 1, 20);
  348. bitmap_set(bits, 60000, 15);
  349. test("1-20,60000-60014", "%*pbl", nbits, bits);
  350. kfree(bits);
  351. }
  352. static void __init
  353. bitmap(void)
  354. {
  355. DECLARE_BITMAP(bits, 20);
  356. const int primes[] = {2,3,5,7,11,13,17,19};
  357. int i;
  358. bitmap_zero(bits, 20);
  359. test("00000|00000", "%20pb|%*pb", bits, 20, bits);
  360. test("|", "%20pbl|%*pbl", bits, 20, bits);
  361. for (i = 0; i < ARRAY_SIZE(primes); ++i)
  362. set_bit(primes[i], bits);
  363. test("a28ac|a28ac", "%20pb|%*pb", bits, 20, bits);
  364. test("2-3,5,7,11,13,17,19|2-3,5,7,11,13,17,19", "%20pbl|%*pbl", bits, 20, bits);
  365. bitmap_fill(bits, 20);
  366. test("fffff|fffff", "%20pb|%*pb", bits, 20, bits);
  367. test("0-19|0-19", "%20pbl|%*pbl", bits, 20, bits);
  368. large_bitmap();
  369. }
  370. static void __init
  371. netdev_features(void)
  372. {
  373. }
  374. static void __init
  375. flags(void)
  376. {
  377. unsigned long flags;
  378. gfp_t gfp;
  379. char *cmp_buffer;
  380. flags = 0;
  381. test("", "%pGp", &flags);
  382. /* Page flags should filter the zone id */
  383. flags = 1UL << NR_PAGEFLAGS;
  384. test("", "%pGp", &flags);
  385. flags |= 1UL << PG_uptodate | 1UL << PG_dirty | 1UL << PG_lru
  386. | 1UL << PG_active | 1UL << PG_swapbacked;
  387. test("uptodate|dirty|lru|active|swapbacked", "%pGp", &flags);
  388. flags = VM_READ | VM_EXEC | VM_MAYREAD | VM_MAYWRITE | VM_MAYEXEC
  389. | VM_DENYWRITE;
  390. test("read|exec|mayread|maywrite|mayexec|denywrite", "%pGv", &flags);
  391. gfp = GFP_TRANSHUGE;
  392. test("GFP_TRANSHUGE", "%pGg", &gfp);
  393. gfp = GFP_ATOMIC|__GFP_DMA;
  394. test("GFP_ATOMIC|GFP_DMA", "%pGg", &gfp);
  395. gfp = __GFP_ATOMIC;
  396. test("__GFP_ATOMIC", "%pGg", &gfp);
  397. cmp_buffer = kmalloc(BUF_SIZE, GFP_KERNEL);
  398. if (!cmp_buffer)
  399. return;
  400. /* Any flags not translated by the table should remain numeric */
  401. gfp = ~__GFP_BITS_MASK;
  402. snprintf(cmp_buffer, BUF_SIZE, "%#lx", (unsigned long) gfp);
  403. test(cmp_buffer, "%pGg", &gfp);
  404. snprintf(cmp_buffer, BUF_SIZE, "__GFP_ATOMIC|%#lx",
  405. (unsigned long) gfp);
  406. gfp |= __GFP_ATOMIC;
  407. test(cmp_buffer, "%pGg", &gfp);
  408. kfree(cmp_buffer);
  409. }
  410. static void __init
  411. test_pointer(void)
  412. {
  413. plain();
  414. symbol_ptr();
  415. kernel_ptr();
  416. struct_resource();
  417. addr();
  418. escaped_str();
  419. hex_string();
  420. mac();
  421. ip();
  422. uuid();
  423. dentry();
  424. struct_va_format();
  425. struct_clk();
  426. bitmap();
  427. netdev_features();
  428. flags();
  429. }
  430. static int __init
  431. test_printf_init(void)
  432. {
  433. alloced_buffer = kmalloc(BUF_SIZE + 2*PAD_SIZE, GFP_KERNEL);
  434. if (!alloced_buffer)
  435. return -ENOMEM;
  436. test_buffer = alloced_buffer + PAD_SIZE;
  437. test_basic();
  438. test_number();
  439. test_string();
  440. test_pointer();
  441. kfree(alloced_buffer);
  442. if (failed_tests == 0)
  443. pr_info("all %u tests passed\n", total_tests);
  444. else
  445. pr_warn("failed %u out of %u tests\n", failed_tests, total_tests);
  446. return failed_tests ? -EINVAL : 0;
  447. }
  448. module_init(test_printf_init);
  449. MODULE_AUTHOR("Rasmus Villemoes <[email protected]>");
  450. MODULE_LICENSE("GPL");