tst-memmove-overflow.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* Test for signed comparison bug in memmove (bug 25620).
  2. Copyright (C) 2020-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. /* This test shifts a memory region which is a bit larger than 2 GiB
  16. by one byte. In order to make it more likely that the memory
  17. allocation succeeds on 32-bit systems, most of the allocation
  18. consists of shared pages. Only a portion at the start and end of
  19. the allocation are unshared, and contain a specific non-repeating
  20. bit pattern. */
  21. #include <array_length.h>
  22. #include <libc-diag.h>
  23. #include <stdint.h>
  24. #include <string.h>
  25. #include <support/blob_repeat.h>
  26. #include <support/check.h>
  27. #include <support/xunistd.h>
  28. #include <sys/mman.h>
  29. #include <unistd.h>
  30. #define TEST_MAIN
  31. #define TEST_NAME "memmove"
  32. #include "test-string.h"
  33. #include <support/test-driver.h>
  34. IMPL (memmove, 1)
  35. /* Size of the part of the allocation which is not shared, at the
  36. start and the end of the overall allocation. 4 MiB. */
  37. enum { unshared_size = (size_t) 4U << 20 };
  38. /* The allocation is 2 GiB plus 8 MiB. This should work with all page
  39. sizes that occur in practice. */
  40. enum { allocation_size = ((size_t) 2U << 30) + 2 * unshared_size };
  41. /* Compute the expected byte at the given index. This is used to
  42. produce a non-repeating pattern. */
  43. static inline unsigned char
  44. expected_value (size_t index)
  45. {
  46. uint32_t randomized = 0x9e3779b9 * index; /* Based on golden ratio. */
  47. return randomized >> 25; /* Result is in the range [0, 127]. */
  48. }
  49. /* Used to count mismatches up to a limit, to avoid creating a huge
  50. test output file. */
  51. static unsigned int mismatch_count;
  52. /* Check ACTUAL == EXPECTED. Use INDEX for error reporting. Exit the
  53. process after too many errors. */
  54. static inline void
  55. check_one_index (size_t index, unsigned char actual, unsigned char expected)
  56. {
  57. if (actual != expected)
  58. {
  59. printf ("error: mismatch at index %zu: expected 0x%02x, got 0x%02x\n",
  60. index, actual, expected);
  61. ++mismatch_count;
  62. if (mismatch_count > 200)
  63. FAIL_EXIT1 ("bailing out due to too many errors");
  64. }
  65. }
  66. static int
  67. test_main (void)
  68. {
  69. test_init ();
  70. FOR_EACH_IMPL (impl, 0)
  71. {
  72. printf ("info: testing %s\n", impl->name);
  73. /* Check that the allocation sizes are multiples of the page
  74. size. */
  75. TEST_COMPARE (allocation_size % xsysconf (_SC_PAGESIZE), 0);
  76. TEST_COMPARE (unshared_size % xsysconf (_SC_PAGESIZE), 0);
  77. /* The repeating pattern has the MSB set in all bytes. */
  78. unsigned char repeating_pattern[128];
  79. for (unsigned int i = 0; i < array_length (repeating_pattern); ++i)
  80. repeating_pattern[i] = 0x80 | i;
  81. struct support_blob_repeat repeat
  82. = support_blob_repeat_allocate_shared (repeating_pattern,
  83. sizeof (repeating_pattern),
  84. (allocation_size
  85. / sizeof (repeating_pattern)));
  86. if (repeat.start == NULL)
  87. FAIL_UNSUPPORTED ("repeated blob allocation failed: %m");
  88. TEST_COMPARE (repeat.size, allocation_size);
  89. /* Unshared the start and the end of the allocation. */
  90. unsigned char *start = repeat.start;
  91. xmmap (start, unshared_size,
  92. PROT_READ | PROT_WRITE,
  93. MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1);
  94. xmmap (start + allocation_size - unshared_size, unshared_size,
  95. PROT_READ | PROT_WRITE,
  96. MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1);
  97. /* Initialize the non-repeating pattern. */
  98. for (size_t i = 0; i < unshared_size; ++i)
  99. start[i] = expected_value (i);
  100. for (size_t i = allocation_size - unshared_size; i < allocation_size;
  101. ++i)
  102. start[i] = expected_value (i);
  103. /* Make sure that there was really no sharing. */
  104. asm volatile ("" ::: "memory");
  105. for (size_t i = 0; i < unshared_size; ++i)
  106. TEST_COMPARE (start[i], expected_value (i));
  107. for (size_t i = allocation_size - unshared_size; i < allocation_size;
  108. ++i)
  109. TEST_COMPARE (start[i], expected_value (i));
  110. /* Used for a nicer error diagnostic using
  111. TEST_COMPARE_BLOB. */
  112. unsigned char expected_start[128];
  113. memcpy (expected_start, start + 1, sizeof (expected_start));
  114. unsigned char expected_end[128];
  115. memcpy (expected_end,
  116. start + allocation_size - sizeof (expected_end),
  117. sizeof (expected_end));
  118. /* Move the entire allocation forward by one byte. */
  119. DIAG_PUSH_NEEDS_COMMENT;
  120. #if __GNUC_PREREQ (8, 0)
  121. /* GCC 8 warns about string function argument overflows. */
  122. DIAG_IGNORE_NEEDS_COMMENT (8, "-Warray-bounds");
  123. DIAG_IGNORE_NEEDS_COMMENT (8, "-Wstringop-overflow");
  124. #endif
  125. memmove (start, start + 1, allocation_size - 1);
  126. DIAG_POP_NEEDS_COMMENT;
  127. /* Check that the unshared of the memory region have been
  128. shifted as expected. The TEST_COMPARE_BLOB checks are
  129. redundant, but produce nicer diagnostics. */
  130. asm volatile ("" ::: "memory");
  131. TEST_COMPARE_BLOB (expected_start, sizeof (expected_start),
  132. start, sizeof (expected_start));
  133. TEST_COMPARE_BLOB (expected_end, sizeof (expected_end),
  134. start + allocation_size - sizeof (expected_end) - 1,
  135. sizeof (expected_end));
  136. for (size_t i = 0; i < unshared_size - 1; ++i)
  137. check_one_index (i, start[i], expected_value (i + 1));
  138. /* The gap between the checked start and end area of the mapping
  139. has shared mappings at unspecified boundaries, so do not
  140. check the expected values in the middle. */
  141. for (size_t i = allocation_size - unshared_size; i < allocation_size - 1;
  142. ++i)
  143. check_one_index (i, start[i], expected_value (i + 1));
  144. support_blob_repeat_free (&repeat);
  145. }
  146. return 0;
  147. }
  148. #include <support/test-driver.c>