memcpy_kunit.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 @ %zu (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[%zu] (0x%02x) != %s.data[%zu] (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 unsigned char larger_array [2048];
  98. static void memmove_test(struct kunit *test)
  99. {
  100. #define TEST_OP "memmove"
  101. struct some_bytes control = {
  102. .data = { 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. 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  106. },
  107. };
  108. struct some_bytes zero = { };
  109. struct some_bytes middle = {
  110. .data = { 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  111. 0x99, 0x99, 0x99, 0x99, 0x00, 0x00, 0x00, 0x00,
  112. 0x00, 0x00, 0x00, 0x99, 0x99, 0x99, 0x99, 0x99,
  113. 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  114. },
  115. };
  116. struct some_bytes five = {
  117. .data = { 0x00, 0x00, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  118. 0x99, 0x99, 0x00, 0x00, 0x00, 0x99, 0x99, 0x99,
  119. 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  120. 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  121. },
  122. };
  123. struct some_bytes overlap = {
  124. .data = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  125. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  126. 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  127. 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  128. },
  129. };
  130. struct some_bytes overlap_expected = {
  131. .data = { 0x00, 0x01, 0x00, 0x01, 0x02, 0x03, 0x04, 0x07,
  132. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  133. 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  134. 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  135. },
  136. };
  137. struct some_bytes dest = { };
  138. int count;
  139. u8 *ptr;
  140. /* Verify static initializers. */
  141. check(control, 0x99);
  142. check(zero, 0);
  143. compare("static initializers", zero, dest);
  144. /* Verify assignment. */
  145. dest = control;
  146. compare("direct assignment", dest, control);
  147. /* Verify complete overwrite. */
  148. memmove(dest.data, zero.data, sizeof(dest.data));
  149. compare("complete overwrite", dest, zero);
  150. /* Verify middle overwrite. */
  151. dest = control;
  152. memmove(dest.data + 12, zero.data, 7);
  153. compare("middle overwrite", dest, middle);
  154. /* Verify argument side-effects aren't repeated. */
  155. dest = control;
  156. ptr = dest.data;
  157. count = 2;
  158. memmove(ptr++, zero.data, count++);
  159. ptr += 9;
  160. memmove(ptr++, zero.data, count++);
  161. compare("argument side-effects", dest, five);
  162. /* Verify overlapping overwrite is correct. */
  163. ptr = &overlap.data[2];
  164. memmove(ptr, overlap.data, 5);
  165. compare("overlapping write", overlap, overlap_expected);
  166. /* Verify larger overlapping moves. */
  167. larger_array[256] = 0xAAu;
  168. /*
  169. * Test a backwards overlapping memmove first. 256 and 1024 are
  170. * important for i386 to use rep movsl.
  171. */
  172. memmove(larger_array, larger_array + 256, 1024);
  173. KUNIT_ASSERT_EQ(test, larger_array[0], 0xAAu);
  174. KUNIT_ASSERT_EQ(test, larger_array[256], 0x00);
  175. KUNIT_ASSERT_NULL(test,
  176. memchr(larger_array + 1, 0xaa, ARRAY_SIZE(larger_array) - 1));
  177. /* Test a forwards overlapping memmove. */
  178. larger_array[0] = 0xBBu;
  179. memmove(larger_array + 256, larger_array, 1024);
  180. KUNIT_ASSERT_EQ(test, larger_array[0], 0xBBu);
  181. KUNIT_ASSERT_EQ(test, larger_array[256], 0xBBu);
  182. KUNIT_ASSERT_NULL(test, memchr(larger_array + 1, 0xBBu, 256 - 1));
  183. KUNIT_ASSERT_NULL(test,
  184. memchr(larger_array + 257, 0xBBu, ARRAY_SIZE(larger_array) - 257));
  185. #undef TEST_OP
  186. }
  187. static void memset_test(struct kunit *test)
  188. {
  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,
  195. },
  196. };
  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,
  202. },
  203. };
  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,
  209. },
  210. };
  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,
  216. },
  217. };
  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,
  223. },
  224. };
  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,
  230. },
  231. };
  232. struct some_bytes dest = { };
  233. int count, value;
  234. u8 *ptr;
  235. /* Verify static initializers. */
  236. check(control, 0x30);
  237. check(dest, 0);
  238. /* Verify assignment. */
  239. dest = control;
  240. compare("direct assignment", dest, control);
  241. /* Verify complete overwrite. */
  242. memset(dest.data, 0xff, sizeof(dest.data));
  243. compare("complete overwrite", dest, complete);
  244. /* Verify middle overwrite. */
  245. dest = control;
  246. memset(dest.data + 4, 0x31, 16);
  247. compare("middle overwrite", dest, middle);
  248. /* Verify argument side-effects aren't repeated. */
  249. dest = control;
  250. ptr = dest.data;
  251. value = 0x60;
  252. count = 1;
  253. memset(ptr++, value++, count++);
  254. ptr += 8;
  255. memset(ptr++, value++, count++);
  256. compare("argument side-effects", dest, three);
  257. /* Verify memset_after() */
  258. dest = control;
  259. memset_after(&dest, 0x72, three);
  260. compare("memset_after()", dest, after);
  261. /* Verify memset_startat() */
  262. dest = control;
  263. memset_startat(&dest, 0x79, four);
  264. compare("memset_startat()", dest, startat);
  265. #undef TEST_OP
  266. }
  267. static u8 large_src[1024];
  268. static u8 large_dst[2048];
  269. static const u8 large_zero[2048];
  270. static void set_random_nonzero(struct kunit *test, u8 *byte)
  271. {
  272. int failed_rng = 0;
  273. while (*byte == 0) {
  274. get_random_bytes(byte, 1);
  275. KUNIT_ASSERT_LT_MSG(test, failed_rng++, 100,
  276. "Is the RNG broken?");
  277. }
  278. }
  279. static void init_large(struct kunit *test)
  280. {
  281. /* Get many bit patterns. */
  282. get_random_bytes(large_src, ARRAY_SIZE(large_src));
  283. /* Make sure we have non-zero edges. */
  284. set_random_nonzero(test, &large_src[0]);
  285. set_random_nonzero(test, &large_src[ARRAY_SIZE(large_src) - 1]);
  286. /* Explicitly zero the entire destination. */
  287. memset(large_dst, 0, ARRAY_SIZE(large_dst));
  288. }
  289. /*
  290. * Instead of an indirect function call for "copy" or a giant macro,
  291. * use a bool to pick memcpy or memmove.
  292. */
  293. static void copy_large_test(struct kunit *test, bool use_memmove)
  294. {
  295. init_large(test);
  296. /* Copy a growing number of non-overlapping bytes ... */
  297. for (int bytes = 1; bytes <= ARRAY_SIZE(large_src); bytes++) {
  298. /* Over a shifting destination window ... */
  299. for (int offset = 0; offset < ARRAY_SIZE(large_src); offset++) {
  300. int right_zero_pos = offset + bytes;
  301. int right_zero_size = ARRAY_SIZE(large_dst) - right_zero_pos;
  302. /* Copy! */
  303. if (use_memmove)
  304. memmove(large_dst + offset, large_src, bytes);
  305. else
  306. memcpy(large_dst + offset, large_src, bytes);
  307. /* Did we touch anything before the copy area? */
  308. KUNIT_ASSERT_EQ_MSG(test,
  309. memcmp(large_dst, large_zero, offset), 0,
  310. "with size %d at offset %d", bytes, offset);
  311. /* Did we touch anything after the copy area? */
  312. KUNIT_ASSERT_EQ_MSG(test,
  313. memcmp(&large_dst[right_zero_pos], large_zero, right_zero_size), 0,
  314. "with size %d at offset %d", bytes, offset);
  315. /* Are we byte-for-byte exact across the copy? */
  316. KUNIT_ASSERT_EQ_MSG(test,
  317. memcmp(large_dst + offset, large_src, bytes), 0,
  318. "with size %d at offset %d", bytes, offset);
  319. /* Zero out what we copied for the next cycle. */
  320. memset(large_dst + offset, 0, bytes);
  321. }
  322. /* Avoid stall warnings if this loop gets slow. */
  323. cond_resched();
  324. }
  325. }
  326. static void memcpy_large_test(struct kunit *test)
  327. {
  328. copy_large_test(test, false);
  329. }
  330. static void memmove_large_test(struct kunit *test)
  331. {
  332. copy_large_test(test, true);
  333. }
  334. /*
  335. * On the assumption that boundary conditions are going to be the most
  336. * sensitive, instead of taking a full step (inc) each iteration,
  337. * take single index steps for at least the first "inc"-many indexes
  338. * from the "start" and at least the last "inc"-many indexes before
  339. * the "end". When in the middle, take full "inc"-wide steps. For
  340. * example, calling next_step(idx, 1, 15, 3) with idx starting at 0
  341. * would see the following pattern: 1 2 3 4 7 10 11 12 13 14 15.
  342. */
  343. static int next_step(int idx, int start, int end, int inc)
  344. {
  345. start += inc;
  346. end -= inc;
  347. if (idx < start || idx + inc > end)
  348. inc = 1;
  349. return idx + inc;
  350. }
  351. static void inner_loop(struct kunit *test, int bytes, int d_off, int s_off)
  352. {
  353. int left_zero_pos, left_zero_size;
  354. int right_zero_pos, right_zero_size;
  355. int src_pos, src_orig_pos, src_size;
  356. int pos;
  357. /* Place the source in the destination buffer. */
  358. memcpy(&large_dst[s_off], large_src, bytes);
  359. /* Copy to destination offset. */
  360. memmove(&large_dst[d_off], &large_dst[s_off], bytes);
  361. /* Make sure destination entirely matches. */
  362. KUNIT_ASSERT_EQ_MSG(test, memcmp(&large_dst[d_off], large_src, bytes), 0,
  363. "with size %d at src offset %d and dest offset %d",
  364. bytes, s_off, d_off);
  365. /* Calculate the expected zero spans. */
  366. if (s_off < d_off) {
  367. left_zero_pos = 0;
  368. left_zero_size = s_off;
  369. right_zero_pos = d_off + bytes;
  370. right_zero_size = ARRAY_SIZE(large_dst) - right_zero_pos;
  371. src_pos = s_off;
  372. src_orig_pos = 0;
  373. src_size = d_off - s_off;
  374. } else {
  375. left_zero_pos = 0;
  376. left_zero_size = d_off;
  377. right_zero_pos = s_off + bytes;
  378. right_zero_size = ARRAY_SIZE(large_dst) - right_zero_pos;
  379. src_pos = d_off + bytes;
  380. src_orig_pos = src_pos - s_off;
  381. src_size = right_zero_pos - src_pos;
  382. }
  383. /* Check non-overlapping source is unchanged.*/
  384. KUNIT_ASSERT_EQ_MSG(test,
  385. memcmp(&large_dst[src_pos], &large_src[src_orig_pos], src_size), 0,
  386. "with size %d at src offset %d and dest offset %d",
  387. bytes, s_off, d_off);
  388. /* Check leading buffer contents are zero. */
  389. KUNIT_ASSERT_EQ_MSG(test,
  390. memcmp(&large_dst[left_zero_pos], large_zero, left_zero_size), 0,
  391. "with size %d at src offset %d and dest offset %d",
  392. bytes, s_off, d_off);
  393. /* Check trailing buffer contents are zero. */
  394. KUNIT_ASSERT_EQ_MSG(test,
  395. memcmp(&large_dst[right_zero_pos], large_zero, right_zero_size), 0,
  396. "with size %d at src offset %d and dest offset %d",
  397. bytes, s_off, d_off);
  398. /* Zero out everything not already zeroed.*/
  399. pos = left_zero_pos + left_zero_size;
  400. memset(&large_dst[pos], 0, right_zero_pos - pos);
  401. }
  402. static void memmove_overlap_test(struct kunit *test)
  403. {
  404. /*
  405. * Running all possible offset and overlap combinations takes a
  406. * very long time. Instead, only check up to 128 bytes offset
  407. * into the destination buffer (which should result in crossing
  408. * cachelines), with a step size of 1 through 7 to try to skip some
  409. * redundancy.
  410. */
  411. static const int offset_max = 128; /* less than ARRAY_SIZE(large_src); */
  412. static const int bytes_step = 7;
  413. static const int window_step = 7;
  414. static const int bytes_start = 1;
  415. static const int bytes_end = ARRAY_SIZE(large_src) + 1;
  416. init_large(test);
  417. /* Copy a growing number of overlapping bytes ... */
  418. for (int bytes = bytes_start; bytes < bytes_end;
  419. bytes = next_step(bytes, bytes_start, bytes_end, bytes_step)) {
  420. /* Over a shifting destination window ... */
  421. for (int d_off = 0; d_off < offset_max; d_off++) {
  422. int s_start = max(d_off - bytes, 0);
  423. int s_end = min_t(int, d_off + bytes, ARRAY_SIZE(large_src));
  424. /* Over a shifting source window ... */
  425. for (int s_off = s_start; s_off < s_end;
  426. s_off = next_step(s_off, s_start, s_end, window_step))
  427. inner_loop(test, bytes, d_off, s_off);
  428. /* Avoid stall warnings. */
  429. cond_resched();
  430. }
  431. }
  432. }
  433. static struct kunit_case memcpy_test_cases[] = {
  434. KUNIT_CASE(memset_test),
  435. KUNIT_CASE(memcpy_test),
  436. KUNIT_CASE_SLOW(memcpy_large_test),
  437. KUNIT_CASE_SLOW(memmove_test),
  438. KUNIT_CASE_SLOW(memmove_large_test),
  439. KUNIT_CASE_SLOW(memmove_overlap_test),
  440. {}
  441. };
  442. static struct kunit_suite memcpy_test_suite = {
  443. .name = "memcpy",
  444. .test_cases = memcpy_test_cases,
  445. };
  446. kunit_test_suite(memcpy_test_suite);
  447. MODULE_DESCRIPTION("test cases for memcpy(), memmove(), and memset()");
  448. MODULE_LICENSE("GPL");