memcpy_kunit.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Test cases for memcpy(), memmove(), and memset().
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <kunit/test.h>
  7. #include <linux/device.h>
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/mm.h>
  11. #include <linux/module.h>
  12. #include <linux/overflow.h>
  13. #include <linux/slab.h>
  14. #include <linux/types.h>
  15. #include <linux/vmalloc.h>
  16. struct some_bytes {
  17. union {
  18. u8 data[32];
  19. struct {
  20. u32 one;
  21. u16 two;
  22. u8 three;
  23. /* 1 byte hole */
  24. u32 four[4];
  25. };
  26. };
  27. };
  28. #define check(instance, v) do { \
  29. BUILD_BUG_ON(sizeof(instance.data) != 32); \
  30. for (size_t i = 0; i < sizeof(instance.data); i++) { \
  31. KUNIT_ASSERT_EQ_MSG(test, instance.data[i], v, \
  32. "line %d: '%s' not initialized to 0x%02x @ %d (saw 0x%02x)\n", \
  33. __LINE__, #instance, v, i, instance.data[i]); \
  34. } \
  35. } while (0)
  36. #define compare(name, one, two) do { \
  37. BUILD_BUG_ON(sizeof(one) != sizeof(two)); \
  38. for (size_t i = 0; i < sizeof(one); i++) { \
  39. KUNIT_EXPECT_EQ_MSG(test, one.data[i], two.data[i], \
  40. "line %d: %s.data[%d] (0x%02x) != %s.data[%d] (0x%02x)\n", \
  41. __LINE__, #one, i, one.data[i], #two, i, two.data[i]); \
  42. } \
  43. kunit_info(test, "ok: " TEST_OP "() " name "\n"); \
  44. } while (0)
  45. static void memcpy_test(struct kunit *test)
  46. {
  47. #define TEST_OP "memcpy"
  48. struct some_bytes control = {
  49. .data = { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  50. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  51. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  52. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  53. },
  54. };
  55. struct some_bytes zero = { };
  56. struct some_bytes middle = {
  57. .data = { 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  58. 0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0x00,
  59. 0x00, 0x00, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20,
  60. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  61. },
  62. };
  63. struct some_bytes three = {
  64. .data = { 0x00, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  65. 0x20, 0x00, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20,
  66. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  67. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  68. },
  69. };
  70. struct some_bytes dest = { };
  71. int count;
  72. u8 *ptr;
  73. /* Verify static initializers. */
  74. check(control, 0x20);
  75. check(zero, 0);
  76. compare("static initializers", dest, zero);
  77. /* Verify assignment. */
  78. dest = control;
  79. compare("direct assignment", dest, control);
  80. /* Verify complete overwrite. */
  81. memcpy(dest.data, zero.data, sizeof(dest.data));
  82. compare("complete overwrite", dest, zero);
  83. /* Verify middle overwrite. */
  84. dest = control;
  85. memcpy(dest.data + 12, zero.data, 7);
  86. compare("middle overwrite", dest, middle);
  87. /* Verify argument side-effects aren't repeated. */
  88. dest = control;
  89. ptr = dest.data;
  90. count = 1;
  91. memcpy(ptr++, zero.data, count++);
  92. ptr += 8;
  93. memcpy(ptr++, zero.data, count++);
  94. compare("argument side-effects", dest, three);
  95. #undef TEST_OP
  96. }
  97. static void memmove_test(struct kunit *test)
  98. {
  99. #define TEST_OP "memmove"
  100. struct some_bytes control = {
  101. .data = { 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  102. 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  103. 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  104. 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  105. },
  106. };
  107. struct some_bytes zero = { };
  108. struct some_bytes middle = {
  109. .data = { 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  110. 0x99, 0x99, 0x99, 0x99, 0x00, 0x00, 0x00, 0x00,
  111. 0x00, 0x00, 0x00, 0x99, 0x99, 0x99, 0x99, 0x99,
  112. 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  113. },
  114. };
  115. struct some_bytes five = {
  116. .data = { 0x00, 0x00, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  117. 0x99, 0x99, 0x00, 0x00, 0x00, 0x99, 0x99, 0x99,
  118. 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  119. 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  120. },
  121. };
  122. struct some_bytes overlap = {
  123. .data = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  124. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  125. 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  126. 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  127. },
  128. };
  129. struct some_bytes overlap_expected = {
  130. .data = { 0x00, 0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x07,
  131. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  132. 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  133. 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  134. },
  135. };
  136. struct some_bytes dest = { };
  137. int count;
  138. u8 *ptr;
  139. /* Verify static initializers. */
  140. check(control, 0x99);
  141. check(zero, 0);
  142. compare("static initializers", zero, dest);
  143. /* Verify assignment. */
  144. dest = control;
  145. compare("direct assignment", dest, control);
  146. /* Verify complete overwrite. */
  147. memmove(dest.data, zero.data, sizeof(dest.data));
  148. compare("complete overwrite", dest, zero);
  149. /* Verify middle overwrite. */
  150. dest = control;
  151. memmove(dest.data + 12, zero.data, 7);
  152. compare("middle overwrite", dest, middle);
  153. /* Verify argument side-effects aren't repeated. */
  154. dest = control;
  155. ptr = dest.data;
  156. count = 2;
  157. memmove(ptr++, zero.data, count++);
  158. ptr += 9;
  159. memmove(ptr++, zero.data, count++);
  160. compare("argument side-effects", dest, five);
  161. /* Verify overlapping overwrite is correct. */
  162. ptr = &overlap.data[2];
  163. memmove(ptr, overlap.data, 5);
  164. compare("overlapping write", overlap, overlap_expected);
  165. #undef TEST_OP
  166. }
  167. static void memset_test(struct kunit *test)
  168. {
  169. #define TEST_OP "memset"
  170. struct some_bytes control = {
  171. .data = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
  172. 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
  173. 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
  174. 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
  175. },
  176. };
  177. struct some_bytes complete = {
  178. .data = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  179. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  180. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  181. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  182. },
  183. };
  184. struct some_bytes middle = {
  185. .data = { 0x30, 0x30, 0x30, 0x30, 0x31, 0x31, 0x31, 0x31,
  186. 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
  187. 0x31, 0x31, 0x31, 0x31, 0x30, 0x30, 0x30, 0x30,
  188. 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
  189. },
  190. };
  191. struct some_bytes three = {
  192. .data = { 0x60, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
  193. 0x30, 0x61, 0x61, 0x30, 0x30, 0x30, 0x30, 0x30,
  194. 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
  195. 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
  196. },
  197. };
  198. struct some_bytes after = {
  199. .data = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x72,
  200. 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72,
  201. 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72,
  202. 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72, 0x72,
  203. },
  204. };
  205. struct some_bytes startat = {
  206. .data = { 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
  207. 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79,
  208. 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79,
  209. 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79, 0x79,
  210. },
  211. };
  212. struct some_bytes dest = { };
  213. int count, value;
  214. u8 *ptr;
  215. /* Verify static initializers. */
  216. check(control, 0x30);
  217. check(dest, 0);
  218. /* Verify assignment. */
  219. dest = control;
  220. compare("direct assignment", dest, control);
  221. /* Verify complete overwrite. */
  222. memset(dest.data, 0xff, sizeof(dest.data));
  223. compare("complete overwrite", dest, complete);
  224. /* Verify middle overwrite. */
  225. dest = control;
  226. memset(dest.data + 4, 0x31, 16);
  227. compare("middle overwrite", dest, middle);
  228. /* Verify argument side-effects aren't repeated. */
  229. dest = control;
  230. ptr = dest.data;
  231. value = 0x60;
  232. count = 1;
  233. memset(ptr++, value++, count++);
  234. ptr += 8;
  235. memset(ptr++, value++, count++);
  236. compare("argument side-effects", dest, three);
  237. /* Verify memset_after() */
  238. dest = control;
  239. memset_after(&dest, 0x72, three);
  240. compare("memset_after()", dest, after);
  241. /* Verify memset_startat() */
  242. dest = control;
  243. memset_startat(&dest, 0x79, four);
  244. compare("memset_startat()", dest, startat);
  245. #undef TEST_OP
  246. }
  247. static void strtomem_test(struct kunit *test)
  248. {
  249. static const char input[sizeof(unsigned long)] = "hi";
  250. static const char truncate[] = "this is too long";
  251. struct {
  252. unsigned long canary1;
  253. unsigned char output[sizeof(unsigned long)] __nonstring;
  254. unsigned long canary2;
  255. } wrap;
  256. memset(&wrap, 0xFF, sizeof(wrap));
  257. KUNIT_EXPECT_EQ_MSG(test, wrap.canary1, ULONG_MAX,
  258. "bad initial canary value");
  259. KUNIT_EXPECT_EQ_MSG(test, wrap.canary2, ULONG_MAX,
  260. "bad initial canary value");
  261. /* Check unpadded copy leaves surroundings untouched. */
  262. strtomem(wrap.output, input);
  263. KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
  264. KUNIT_EXPECT_EQ(test, wrap.output[0], input[0]);
  265. KUNIT_EXPECT_EQ(test, wrap.output[1], input[1]);
  266. for (size_t i = 2; i < sizeof(wrap.output); i++)
  267. KUNIT_EXPECT_EQ(test, wrap.output[i], 0xFF);
  268. KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
  269. /* Check truncated copy leaves surroundings untouched. */
  270. memset(&wrap, 0xFF, sizeof(wrap));
  271. strtomem(wrap.output, truncate);
  272. KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
  273. for (size_t i = 0; i < sizeof(wrap.output); i++)
  274. KUNIT_EXPECT_EQ(test, wrap.output[i], truncate[i]);
  275. KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
  276. /* Check padded copy leaves only string padded. */
  277. memset(&wrap, 0xFF, sizeof(wrap));
  278. strtomem_pad(wrap.output, input, 0xAA);
  279. KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
  280. KUNIT_EXPECT_EQ(test, wrap.output[0], input[0]);
  281. KUNIT_EXPECT_EQ(test, wrap.output[1], input[1]);
  282. for (size_t i = 2; i < sizeof(wrap.output); i++)
  283. KUNIT_EXPECT_EQ(test, wrap.output[i], 0xAA);
  284. KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
  285. /* Check truncated padded copy has no padding. */
  286. memset(&wrap, 0xFF, sizeof(wrap));
  287. strtomem(wrap.output, truncate);
  288. KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
  289. for (size_t i = 0; i < sizeof(wrap.output); i++)
  290. KUNIT_EXPECT_EQ(test, wrap.output[i], truncate[i]);
  291. KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
  292. }
  293. static struct kunit_case memcpy_test_cases[] = {
  294. KUNIT_CASE(memset_test),
  295. KUNIT_CASE(memcpy_test),
  296. KUNIT_CASE(memmove_test),
  297. KUNIT_CASE(strtomem_test),
  298. {}
  299. };
  300. static struct kunit_suite memcpy_test_suite = {
  301. .name = "memcpy",
  302. .test_cases = memcpy_test_cases,
  303. };
  304. kunit_test_suite(memcpy_test_suite);
  305. MODULE_LICENSE("GPL");