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