1 // SPDX-License-Identifier: GPL-2.0
3 * Test cases for memcpy(), memmove(), and memset().
5 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 #include <kunit/test.h>
8 #include <linux/device.h>
9 #include <linux/init.h>
10 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/overflow.h>
14 #include <linux/slab.h>
15 #include <linux/types.h>
16 #include <linux/vmalloc.h>
31 #define check(instance, v) do { \
32 BUILD_BUG_ON(sizeof(instance.data) != 32); \
33 for (size_t i = 0; i < sizeof(instance.data); i++) { \
34 KUNIT_ASSERT_EQ_MSG(test, instance.data[i], v, \
35 "line %d: '%s' not initialized to 0x%02x @ %d (saw 0x%02x)\n", \
36 __LINE__, #instance, v, i, instance.data[i]); \
40 #define compare(name, one, two) do { \
41 BUILD_BUG_ON(sizeof(one) != sizeof(two)); \
42 for (size_t i = 0; i < sizeof(one); i++) { \
43 KUNIT_EXPECT_EQ_MSG(test, one.data[i], two.data[i], \
44 "line %d: %s.data[%d] (0x%02x) != %s.data[%d] (0x%02x)\n", \
45 __LINE__, #one, i, one.data[i], #two, i, two.data[i]); \
47 kunit_info(test, "ok: " TEST_OP "() " name "\n"); \
50 static void memcpy_test(struct kunit *test)
52 #define TEST_OP "memcpy"
53 struct some_bytes control = {
54 .data = { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
55 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
56 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
57 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
60 struct some_bytes zero = { };
61 struct some_bytes middle = {
62 .data = { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
63 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00,
64 0x00, 0x00, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20,
65 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
68 struct some_bytes three = {
69 .data = { 0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
70 0x20, 0x00, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20,
71 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
72 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
75 struct some_bytes dest = { };
79 /* Verify static initializers. */
82 compare("static initializers", dest, zero);
84 /* Verify assignment. */
86 compare("direct assignment", dest, control);
88 /* Verify complete overwrite. */
89 memcpy(dest.data, zero.data, sizeof(dest.data));
90 compare("complete overwrite", dest, zero);
92 /* Verify middle overwrite. */
94 memcpy(dest.data + 12, zero.data, 7);
95 compare("middle overwrite", dest, middle);
97 /* Verify argument side-effects aren't repeated. */
101 memcpy(ptr++, zero.data, count++);
103 memcpy(ptr++, zero.data, count++);
104 compare("argument side-effects", dest, three);
108 static unsigned char larger_array [2048];
110 static void memmove_test(struct kunit *test)
112 #define TEST_OP "memmove"
113 struct some_bytes control = {
114 .data = { 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
115 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
116 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
117 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
120 struct some_bytes zero = { };
121 struct some_bytes middle = {
122 .data = { 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
123 0x99, 0x99, 0x99, 0x99, 0x00, 0x00, 0x00, 0x00,
124 0x00, 0x00, 0x00, 0x99, 0x99, 0x99, 0x99, 0x99,
125 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
128 struct some_bytes five = {
129 .data = { 0x00, 0x00, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
130 0x99, 0x99, 0x00, 0x00, 0x00, 0x99, 0x99, 0x99,
131 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
132 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
135 struct some_bytes overlap = {
136 .data = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
137 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
138 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
139 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
142 struct some_bytes overlap_expected = {
143 .data = { 0x00, 0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x07,
144 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
145 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
146 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
149 struct some_bytes dest = { };
153 /* Verify static initializers. */
154 check(control, 0x99);
156 compare("static initializers", zero, dest);
158 /* Verify assignment. */
160 compare("direct assignment", dest, control);
162 /* Verify complete overwrite. */
163 memmove(dest.data, zero.data, sizeof(dest.data));
164 compare("complete overwrite", dest, zero);
166 /* Verify middle overwrite. */
168 memmove(dest.data + 12, zero.data, 7);
169 compare("middle overwrite", dest, middle);
171 /* Verify argument side-effects aren't repeated. */
175 memmove(ptr++, zero.data, count++);
177 memmove(ptr++, zero.data, count++);
178 compare("argument side-effects", dest, five);
180 /* Verify overlapping overwrite is correct. */
181 ptr = &overlap.data[2];
182 memmove(ptr, overlap.data, 5);
183 compare("overlapping write", overlap, overlap_expected);
185 /* Verify larger overlapping moves. */
186 larger_array[256] = 0xAAu;
188 * Test a backwards overlapping memmove first. 256 and 1024 are
189 * important for i386 to use rep movsl.
191 memmove(larger_array, larger_array + 256, 1024);
192 KUNIT_ASSERT_EQ(test, larger_array[0], 0xAAu);
193 KUNIT_ASSERT_EQ(test, larger_array[256], 0x00);
194 KUNIT_ASSERT_NULL(test,
195 memchr(larger_array + 1, 0xaa, ARRAY_SIZE(larger_array) - 1));
196 /* Test a forwards overlapping memmove. */
197 larger_array[0] = 0xBBu;
198 memmove(larger_array + 256, larger_array, 1024);
199 KUNIT_ASSERT_EQ(test, larger_array[0], 0xBBu);
200 KUNIT_ASSERT_EQ(test, larger_array[256], 0xBBu);
201 KUNIT_ASSERT_NULL(test, memchr(larger_array + 1, 0xBBu, 256 - 1));
202 KUNIT_ASSERT_NULL(test,
203 memchr(larger_array + 257, 0xBBu, ARRAY_SIZE(larger_array) - 257));
207 static void memset_test(struct kunit *test)
209 #define TEST_OP "memset"
210 struct some_bytes control = {
211 .data = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
212 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
213 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
214 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
217 struct some_bytes complete = {
218 .data = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
219 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
220 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
221 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
224 struct some_bytes middle = {
225 .data = { 0x30, 0x30, 0x30, 0x30, 0x31, 0x31, 0x31, 0x31,
226 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
227 0x31, 0x31, 0x31, 0x31, 0x30, 0x30, 0x30, 0x30,
228 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
231 struct some_bytes three = {
232 .data = { 0x60, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
233 0x30, 0x61, 0x61, 0x30, 0x30, 0x30, 0x30, 0x30,
234 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
235 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
238 struct some_bytes after = {
239 .data = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x72,
240 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72,
241 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72,
242 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72,
245 struct some_bytes startat = {
246 .data = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
247 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79,
248 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79,
249 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79,
252 struct some_bytes dest = { };
256 /* Verify static initializers. */
257 check(control, 0x30);
260 /* Verify assignment. */
262 compare("direct assignment", dest, control);
264 /* Verify complete overwrite. */
265 memset(dest.data, 0xff, sizeof(dest.data));
266 compare("complete overwrite", dest, complete);
268 /* Verify middle overwrite. */
270 memset(dest.data + 4, 0x31, 16);
271 compare("middle overwrite", dest, middle);
273 /* Verify argument side-effects aren't repeated. */
278 memset(ptr++, value++, count++);
280 memset(ptr++, value++, count++);
281 compare("argument side-effects", dest, three);
283 /* Verify memset_after() */
285 memset_after(&dest, 0x72, three);
286 compare("memset_after()", dest, after);
288 /* Verify memset_startat() */
290 memset_startat(&dest, 0x79, four);
291 compare("memset_startat()", dest, startat);
295 static u8 large_src[1024];
296 static u8 large_dst[2048];
297 static const u8 large_zero[2048];
299 static void set_random_nonzero(struct kunit *test, u8 *byte)
304 get_random_bytes(byte, 1);
305 KUNIT_ASSERT_LT_MSG(test, failed_rng++, 100,
306 "Is the RNG broken?");
310 static void init_large(struct kunit *test)
312 if (!IS_ENABLED(CONFIG_MEMCPY_SLOW_KUNIT_TEST))
313 kunit_skip(test, "Slow test skipped. Enable with CONFIG_MEMCPY_SLOW_KUNIT_TEST=y");
315 /* Get many bit patterns. */
316 get_random_bytes(large_src, ARRAY_SIZE(large_src));
318 /* Make sure we have non-zero edges. */
319 set_random_nonzero(test, &large_src[0]);
320 set_random_nonzero(test, &large_src[ARRAY_SIZE(large_src) - 1]);
322 /* Explicitly zero the entire destination. */
323 memset(large_dst, 0, ARRAY_SIZE(large_dst));
327 * Instead of an indirect function call for "copy" or a giant macro,
328 * use a bool to pick memcpy or memmove.
330 static void copy_large_test(struct kunit *test, bool use_memmove)
334 /* Copy a growing number of non-overlapping bytes ... */
335 for (int bytes = 1; bytes <= ARRAY_SIZE(large_src); bytes++) {
336 /* Over a shifting destination window ... */
337 for (int offset = 0; offset < ARRAY_SIZE(large_src); offset++) {
338 int right_zero_pos = offset + bytes;
339 int right_zero_size = ARRAY_SIZE(large_dst) - right_zero_pos;
343 memmove(large_dst + offset, large_src, bytes);
345 memcpy(large_dst + offset, large_src, bytes);
347 /* Did we touch anything before the copy area? */
348 KUNIT_ASSERT_EQ_MSG(test,
349 memcmp(large_dst, large_zero, offset), 0,
350 "with size %d at offset %d", bytes, offset);
351 /* Did we touch anything after the copy area? */
352 KUNIT_ASSERT_EQ_MSG(test,
353 memcmp(&large_dst[right_zero_pos], large_zero, right_zero_size), 0,
354 "with size %d at offset %d", bytes, offset);
356 /* Are we byte-for-byte exact across the copy? */
357 KUNIT_ASSERT_EQ_MSG(test,
358 memcmp(large_dst + offset, large_src, bytes), 0,
359 "with size %d at offset %d", bytes, offset);
361 /* Zero out what we copied for the next cycle. */
362 memset(large_dst + offset, 0, bytes);
364 /* Avoid stall warnings if this loop gets slow. */
369 static void memcpy_large_test(struct kunit *test)
371 copy_large_test(test, false);
374 static void memmove_large_test(struct kunit *test)
376 copy_large_test(test, true);
380 * On the assumption that boundary conditions are going to be the most
381 * sensitive, instead of taking a full step (inc) each iteration,
382 * take single index steps for at least the first "inc"-many indexes
383 * from the "start" and at least the last "inc"-many indexes before
384 * the "end". When in the middle, take full "inc"-wide steps. For
385 * example, calling next_step(idx, 1, 15, 3) with idx starting at 0
386 * would see the following pattern: 1 2 3 4 7 10 11 12 13 14 15.
388 static int next_step(int idx, int start, int end, int inc)
393 if (idx < start || idx + inc > end)
398 static void inner_loop(struct kunit *test, int bytes, int d_off, int s_off)
400 int left_zero_pos, left_zero_size;
401 int right_zero_pos, right_zero_size;
402 int src_pos, src_orig_pos, src_size;
405 /* Place the source in the destination buffer. */
406 memcpy(&large_dst[s_off], large_src, bytes);
408 /* Copy to destination offset. */
409 memmove(&large_dst[d_off], &large_dst[s_off], bytes);
411 /* Make sure destination entirely matches. */
412 KUNIT_ASSERT_EQ_MSG(test, memcmp(&large_dst[d_off], large_src, bytes), 0,
413 "with size %d at src offset %d and dest offset %d",
414 bytes, s_off, d_off);
416 /* Calculate the expected zero spans. */
419 left_zero_size = s_off;
421 right_zero_pos = d_off + bytes;
422 right_zero_size = ARRAY_SIZE(large_dst) - right_zero_pos;
426 src_size = d_off - s_off;
429 left_zero_size = d_off;
431 right_zero_pos = s_off + bytes;
432 right_zero_size = ARRAY_SIZE(large_dst) - right_zero_pos;
434 src_pos = d_off + bytes;
435 src_orig_pos = src_pos - s_off;
436 src_size = right_zero_pos - src_pos;
439 /* Check non-overlapping source is unchanged.*/
440 KUNIT_ASSERT_EQ_MSG(test,
441 memcmp(&large_dst[src_pos], &large_src[src_orig_pos], src_size), 0,
442 "with size %d at src offset %d and dest offset %d",
443 bytes, s_off, d_off);
445 /* Check leading buffer contents are zero. */
446 KUNIT_ASSERT_EQ_MSG(test,
447 memcmp(&large_dst[left_zero_pos], large_zero, left_zero_size), 0,
448 "with size %d at src offset %d and dest offset %d",
449 bytes, s_off, d_off);
450 /* Check trailing buffer contents are zero. */
451 KUNIT_ASSERT_EQ_MSG(test,
452 memcmp(&large_dst[right_zero_pos], large_zero, right_zero_size), 0,
453 "with size %d at src offset %d and dest offset %d",
454 bytes, s_off, d_off);
456 /* Zero out everything not already zeroed.*/
457 pos = left_zero_pos + left_zero_size;
458 memset(&large_dst[pos], 0, right_zero_pos - pos);
461 static void memmove_overlap_test(struct kunit *test)
464 * Running all possible offset and overlap combinations takes a
465 * very long time. Instead, only check up to 128 bytes offset
466 * into the destination buffer (which should result in crossing
467 * cachelines), with a step size of 1 through 7 to try to skip some
470 static const int offset_max = 128; /* less than ARRAY_SIZE(large_src); */
471 static const int bytes_step = 7;
472 static const int window_step = 7;
474 static const int bytes_start = 1;
475 static const int bytes_end = ARRAY_SIZE(large_src) + 1;
479 /* Copy a growing number of overlapping bytes ... */
480 for (int bytes = bytes_start; bytes < bytes_end;
481 bytes = next_step(bytes, bytes_start, bytes_end, bytes_step)) {
483 /* Over a shifting destination window ... */
484 for (int d_off = 0; d_off < offset_max; d_off++) {
485 int s_start = max(d_off - bytes, 0);
486 int s_end = min_t(int, d_off + bytes, ARRAY_SIZE(large_src));
488 /* Over a shifting source window ... */
489 for (int s_off = s_start; s_off < s_end;
490 s_off = next_step(s_off, s_start, s_end, window_step))
491 inner_loop(test, bytes, d_off, s_off);
493 /* Avoid stall warnings. */
499 static void strtomem_test(struct kunit *test)
501 static const char input[sizeof(unsigned long)] = "hi";
502 static const char truncate[] = "this is too long";
504 unsigned long canary1;
505 unsigned char output[sizeof(unsigned long)] __nonstring;
506 unsigned long canary2;
509 memset(&wrap, 0xFF, sizeof(wrap));
510 KUNIT_EXPECT_EQ_MSG(test, wrap.canary1, ULONG_MAX,
511 "bad initial canary value");
512 KUNIT_EXPECT_EQ_MSG(test, wrap.canary2, ULONG_MAX,
513 "bad initial canary value");
515 /* Check unpadded copy leaves surroundings untouched. */
516 strtomem(wrap.output, input);
517 KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
518 KUNIT_EXPECT_EQ(test, wrap.output[0], input[0]);
519 KUNIT_EXPECT_EQ(test, wrap.output[1], input[1]);
520 for (size_t i = 2; i < sizeof(wrap.output); i++)
521 KUNIT_EXPECT_EQ(test, wrap.output[i], 0xFF);
522 KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
524 /* Check truncated copy leaves surroundings untouched. */
525 memset(&wrap, 0xFF, sizeof(wrap));
526 strtomem(wrap.output, truncate);
527 KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
528 for (size_t i = 0; i < sizeof(wrap.output); i++)
529 KUNIT_EXPECT_EQ(test, wrap.output[i], truncate[i]);
530 KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
532 /* Check padded copy leaves only string padded. */
533 memset(&wrap, 0xFF, sizeof(wrap));
534 strtomem_pad(wrap.output, input, 0xAA);
535 KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
536 KUNIT_EXPECT_EQ(test, wrap.output[0], input[0]);
537 KUNIT_EXPECT_EQ(test, wrap.output[1], input[1]);
538 for (size_t i = 2; i < sizeof(wrap.output); i++)
539 KUNIT_EXPECT_EQ(test, wrap.output[i], 0xAA);
540 KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
542 /* Check truncated padded copy has no padding. */
543 memset(&wrap, 0xFF, sizeof(wrap));
544 strtomem(wrap.output, truncate);
545 KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
546 for (size_t i = 0; i < sizeof(wrap.output); i++)
547 KUNIT_EXPECT_EQ(test, wrap.output[i], truncate[i]);
548 KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
551 static struct kunit_case memcpy_test_cases[] = {
552 KUNIT_CASE(memset_test),
553 KUNIT_CASE(memcpy_test),
554 KUNIT_CASE_SLOW(memcpy_large_test),
555 KUNIT_CASE_SLOW(memmove_test),
556 KUNIT_CASE_SLOW(memmove_large_test),
557 KUNIT_CASE_SLOW(memmove_overlap_test),
558 KUNIT_CASE(strtomem_test),
562 static struct kunit_suite memcpy_test_suite = {
564 .test_cases = memcpy_test_cases,
567 kunit_test_suite(memcpy_test_suite);
569 MODULE_LICENSE("GPL");