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 void memmove_test(struct kunit *test)
110 #define TEST_OP "memmove"
111 struct some_bytes control = {
112 .data = { 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
113 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
114 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
115 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
118 struct some_bytes zero = { };
119 struct some_bytes middle = {
120 .data = { 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
121 0x99, 0x99, 0x99, 0x99, 0x00, 0x00, 0x00, 0x00,
122 0x00, 0x00, 0x00, 0x99, 0x99, 0x99, 0x99, 0x99,
123 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
126 struct some_bytes five = {
127 .data = { 0x00, 0x00, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
128 0x99, 0x99, 0x00, 0x00, 0x00, 0x99, 0x99, 0x99,
129 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
130 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
133 struct some_bytes overlap = {
134 .data = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
135 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
136 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
137 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
140 struct some_bytes overlap_expected = {
141 .data = { 0x00, 0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x07,
142 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
143 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
144 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
147 struct some_bytes dest = { };
151 /* Verify static initializers. */
152 check(control, 0x99);
154 compare("static initializers", zero, dest);
156 /* Verify assignment. */
158 compare("direct assignment", dest, control);
160 /* Verify complete overwrite. */
161 memmove(dest.data, zero.data, sizeof(dest.data));
162 compare("complete overwrite", dest, zero);
164 /* Verify middle overwrite. */
166 memmove(dest.data + 12, zero.data, 7);
167 compare("middle overwrite", dest, middle);
169 /* Verify argument side-effects aren't repeated. */
173 memmove(ptr++, zero.data, count++);
175 memmove(ptr++, zero.data, count++);
176 compare("argument side-effects", dest, five);
178 /* Verify overlapping overwrite is correct. */
179 ptr = &overlap.data[2];
180 memmove(ptr, overlap.data, 5);
181 compare("overlapping write", overlap, overlap_expected);
185 static void memset_test(struct kunit *test)
187 #define TEST_OP "memset"
188 struct some_bytes control = {
189 .data = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
190 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
191 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
192 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
195 struct some_bytes complete = {
196 .data = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
197 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
198 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
199 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
202 struct some_bytes middle = {
203 .data = { 0x30, 0x30, 0x30, 0x30, 0x31, 0x31, 0x31, 0x31,
204 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
205 0x31, 0x31, 0x31, 0x31, 0x30, 0x30, 0x30, 0x30,
206 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
209 struct some_bytes three = {
210 .data = { 0x60, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
211 0x30, 0x61, 0x61, 0x30, 0x30, 0x30, 0x30, 0x30,
212 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
213 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
216 struct some_bytes after = {
217 .data = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x72,
218 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72,
219 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72,
220 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72,
223 struct some_bytes startat = {
224 .data = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
225 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79,
226 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79,
227 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79,
230 struct some_bytes dest = { };
234 /* Verify static initializers. */
235 check(control, 0x30);
238 /* Verify assignment. */
240 compare("direct assignment", dest, control);
242 /* Verify complete overwrite. */
243 memset(dest.data, 0xff, sizeof(dest.data));
244 compare("complete overwrite", dest, complete);
246 /* Verify middle overwrite. */
248 memset(dest.data + 4, 0x31, 16);
249 compare("middle overwrite", dest, middle);
251 /* Verify argument side-effects aren't repeated. */
256 memset(ptr++, value++, count++);
258 memset(ptr++, value++, count++);
259 compare("argument side-effects", dest, three);
261 /* Verify memset_after() */
263 memset_after(&dest, 0x72, three);
264 compare("memset_after()", dest, after);
266 /* Verify memset_startat() */
268 memset_startat(&dest, 0x79, four);
269 compare("memset_startat()", dest, startat);
273 static void strtomem_test(struct kunit *test)
275 static const char input[sizeof(unsigned long)] = "hi";
276 static const char truncate[] = "this is too long";
278 unsigned long canary1;
279 unsigned char output[sizeof(unsigned long)] __nonstring;
280 unsigned long canary2;
283 memset(&wrap, 0xFF, sizeof(wrap));
284 KUNIT_EXPECT_EQ_MSG(test, wrap.canary1, ULONG_MAX,
285 "bad initial canary value");
286 KUNIT_EXPECT_EQ_MSG(test, wrap.canary2, ULONG_MAX,
287 "bad initial canary value");
289 /* Check unpadded copy leaves surroundings untouched. */
290 strtomem(wrap.output, input);
291 KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
292 KUNIT_EXPECT_EQ(test, wrap.output[0], input[0]);
293 KUNIT_EXPECT_EQ(test, wrap.output[1], input[1]);
294 for (size_t i = 2; i < sizeof(wrap.output); i++)
295 KUNIT_EXPECT_EQ(test, wrap.output[i], 0xFF);
296 KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
298 /* Check truncated copy leaves surroundings untouched. */
299 memset(&wrap, 0xFF, sizeof(wrap));
300 strtomem(wrap.output, truncate);
301 KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
302 for (size_t i = 0; i < sizeof(wrap.output); i++)
303 KUNIT_EXPECT_EQ(test, wrap.output[i], truncate[i]);
304 KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
306 /* Check padded copy leaves only string padded. */
307 memset(&wrap, 0xFF, sizeof(wrap));
308 strtomem_pad(wrap.output, input, 0xAA);
309 KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
310 KUNIT_EXPECT_EQ(test, wrap.output[0], input[0]);
311 KUNIT_EXPECT_EQ(test, wrap.output[1], input[1]);
312 for (size_t i = 2; i < sizeof(wrap.output); i++)
313 KUNIT_EXPECT_EQ(test, wrap.output[i], 0xAA);
314 KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
316 /* Check truncated padded copy has no padding. */
317 memset(&wrap, 0xFF, sizeof(wrap));
318 strtomem(wrap.output, truncate);
319 KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
320 for (size_t i = 0; i < sizeof(wrap.output); i++)
321 KUNIT_EXPECT_EQ(test, wrap.output[i], truncate[i]);
322 KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
325 static struct kunit_case memcpy_test_cases[] = {
326 KUNIT_CASE(memset_test),
327 KUNIT_CASE(memcpy_test),
328 KUNIT_CASE(memmove_test),
329 KUNIT_CASE(strtomem_test),
333 static struct kunit_suite memcpy_test_suite = {
335 .test_cases = memcpy_test_cases,
338 kunit_test_suite(memcpy_test_suite);
340 MODULE_LICENSE("GPL");