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 { \
33 BUILD_BUG_ON(sizeof(instance.data) != 32); \
34 for (i = 0; i < sizeof(instance.data); i++) { \
35 KUNIT_ASSERT_EQ_MSG(test, instance.data[i], v, \
36 "line %d: '%s' not initialized to 0x%02x @ %d (saw 0x%02x)\n", \
37 __LINE__, #instance, v, i, instance.data[i]); \
41 #define compare(name, one, two) do { \
43 BUILD_BUG_ON(sizeof(one) != sizeof(two)); \
44 for (i = 0; i < sizeof(one); i++) { \
45 KUNIT_EXPECT_EQ_MSG(test, one.data[i], two.data[i], \
46 "line %d: %s.data[%d] (0x%02x) != %s.data[%d] (0x%02x)\n", \
47 __LINE__, #one, i, one.data[i], #two, i, two.data[i]); \
49 kunit_info(test, "ok: " TEST_OP "() " name "\n"); \
52 static void memcpy_test(struct kunit *test)
54 #define TEST_OP "memcpy"
55 struct some_bytes control = {
56 .data = { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
57 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
58 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
59 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
62 struct some_bytes zero = { };
63 struct some_bytes middle = {
64 .data = { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
65 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00,
66 0x00, 0x00, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20,
67 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
70 struct some_bytes three = {
71 .data = { 0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
72 0x20, 0x00, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20,
73 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
74 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
77 struct some_bytes dest = { };
81 /* Verify static initializers. */
84 compare("static initializers", dest, zero);
86 /* Verify assignment. */
88 compare("direct assignment", dest, control);
90 /* Verify complete overwrite. */
91 memcpy(dest.data, zero.data, sizeof(dest.data));
92 compare("complete overwrite", dest, zero);
94 /* Verify middle overwrite. */
96 memcpy(dest.data + 12, zero.data, 7);
97 compare("middle overwrite", dest, middle);
99 /* Verify argument side-effects aren't repeated. */
103 memcpy(ptr++, zero.data, count++);
105 memcpy(ptr++, zero.data, count++);
106 compare("argument side-effects", dest, three);
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);
187 static void memset_test(struct kunit *test)
189 #define TEST_OP "memset"
190 struct some_bytes control = {
191 .data = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
192 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
193 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
194 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
197 struct some_bytes complete = {
198 .data = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
199 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
200 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
201 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
204 struct some_bytes middle = {
205 .data = { 0x30, 0x30, 0x30, 0x30, 0x31, 0x31, 0x31, 0x31,
206 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
207 0x31, 0x31, 0x31, 0x31, 0x30, 0x30, 0x30, 0x30,
208 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
211 struct some_bytes three = {
212 .data = { 0x60, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
213 0x30, 0x61, 0x61, 0x30, 0x30, 0x30, 0x30, 0x30,
214 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
215 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
218 struct some_bytes after = {
219 .data = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x72,
220 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72,
221 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72,
222 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72,
225 struct some_bytes startat = {
226 .data = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
227 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79,
228 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79,
229 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79,
232 struct some_bytes dest = { };
236 /* Verify static initializers. */
237 check(control, 0x30);
240 /* Verify assignment. */
242 compare("direct assignment", dest, control);
244 /* Verify complete overwrite. */
245 memset(dest.data, 0xff, sizeof(dest.data));
246 compare("complete overwrite", dest, complete);
248 /* Verify middle overwrite. */
250 memset(dest.data + 4, 0x31, 16);
251 compare("middle overwrite", dest, middle);
253 /* Verify argument side-effects aren't repeated. */
258 memset(ptr++, value++, count++);
260 memset(ptr++, value++, count++);
261 compare("argument side-effects", dest, three);
263 /* Verify memset_after() */
265 memset_after(&dest, 0x72, three);
266 compare("memset_after()", dest, after);
268 /* Verify memset_startat() */
270 memset_startat(&dest, 0x79, four);
271 compare("memset_startat()", dest, startat);
275 static struct kunit_case memcpy_test_cases[] = {
276 KUNIT_CASE(memset_test),
277 KUNIT_CASE(memcpy_test),
278 KUNIT_CASE(memmove_test),
282 static struct kunit_suite memcpy_test_suite = {
284 .test_cases = memcpy_test_cases,
287 kunit_test_suite(memcpy_test_suite);
289 MODULE_LICENSE("GPL");