test_printf.c 12 KB

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