tst-malloc-too-large.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* Test and verify that too-large memory allocations fail with ENOMEM.
  2. Copyright (C) 2018-2019 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. <http://www.gnu.org/licenses/>. */
  15. /* Bug 22375 reported a regression in malloc where if after malloc'ing then
  16. free'ing a small block of memory, malloc is then called with a really
  17. large size argument (close to SIZE_MAX): instead of returning NULL and
  18. setting errno to ENOMEM, malloc incorrectly returns the previously
  19. allocated block instead. Bug 22343 reported a similar case where
  20. posix_memalign incorrectly returns successfully when called with an with
  21. a really large size argument.
  22. Both of these were caused by integer overflows in the allocator when it
  23. was trying to pad the requested size to allow for book-keeping or
  24. alignment. This test guards against such bugs by repeatedly allocating
  25. and freeing small blocks of memory then trying to allocate various block
  26. sizes larger than the memory bus width of 64-bit targets, or almost
  27. as large as SIZE_MAX on 32-bit targets supported by glibc. In each case,
  28. it verifies that such impossibly large allocations correctly fail. */
  29. #include <stdlib.h>
  30. #include <malloc.h>
  31. #include <errno.h>
  32. #include <stdint.h>
  33. #include <sys/resource.h>
  34. #include <libc-diag.h>
  35. #include <support/check.h>
  36. #include <unistd.h>
  37. #include <sys/param.h>
  38. /* This function prepares for each 'too-large memory allocation' test by
  39. performing a small successful malloc/free and resetting errno prior to
  40. the actual test. */
  41. static void
  42. test_setup (void)
  43. {
  44. void *volatile ptr = malloc (16);
  45. TEST_VERIFY_EXIT (ptr != NULL);
  46. free (ptr);
  47. errno = 0;
  48. }
  49. /* This function tests each of:
  50. - malloc (SIZE)
  51. - realloc (PTR_FOR_REALLOC, SIZE)
  52. - for various values of NMEMB:
  53. - calloc (NMEMB, SIZE/NMEMB)
  54. - calloc (SIZE/NMEMB, NMEMB)
  55. - reallocarray (PTR_FOR_REALLOC, NMEMB, SIZE/NMEMB)
  56. - reallocarray (PTR_FOR_REALLOC, SIZE/NMEMB, NMEMB)
  57. and precedes each of these tests with a small malloc/free before it. */
  58. static void
  59. test_large_allocations (size_t size)
  60. {
  61. void * ptr_to_realloc;
  62. test_setup ();
  63. TEST_VERIFY (malloc (size) == NULL);
  64. TEST_VERIFY (errno == ENOMEM);
  65. ptr_to_realloc = malloc (16);
  66. TEST_VERIFY_EXIT (ptr_to_realloc != NULL);
  67. test_setup ();
  68. TEST_VERIFY (realloc (ptr_to_realloc, size) == NULL);
  69. TEST_VERIFY (errno == ENOMEM);
  70. free (ptr_to_realloc);
  71. for (size_t nmemb = 1; nmemb <= 8; nmemb *= 2)
  72. if ((size % nmemb) == 0)
  73. {
  74. test_setup ();
  75. TEST_VERIFY (calloc (nmemb, size / nmemb) == NULL);
  76. TEST_VERIFY (errno == ENOMEM);
  77. test_setup ();
  78. TEST_VERIFY (calloc (size / nmemb, nmemb) == NULL);
  79. TEST_VERIFY (errno == ENOMEM);
  80. ptr_to_realloc = malloc (16);
  81. TEST_VERIFY_EXIT (ptr_to_realloc != NULL);
  82. test_setup ();
  83. TEST_VERIFY (reallocarray (ptr_to_realloc, nmemb, size / nmemb) == NULL);
  84. TEST_VERIFY (errno == ENOMEM);
  85. free (ptr_to_realloc);
  86. ptr_to_realloc = malloc (16);
  87. TEST_VERIFY_EXIT (ptr_to_realloc != NULL);
  88. test_setup ();
  89. TEST_VERIFY (reallocarray (ptr_to_realloc, size / nmemb, nmemb) == NULL);
  90. TEST_VERIFY (errno == ENOMEM);
  91. free (ptr_to_realloc);
  92. }
  93. else
  94. break;
  95. }
  96. static long pagesize;
  97. /* This function tests the following aligned memory allocation functions
  98. using several valid alignments and precedes each allocation test with a
  99. small malloc/free before it:
  100. memalign, posix_memalign, aligned_alloc, valloc, pvalloc. */
  101. static void
  102. test_large_aligned_allocations (size_t size)
  103. {
  104. /* ptr stores the result of posix_memalign but since all those calls
  105. should fail, posix_memalign should never change ptr. We set it to
  106. NULL here and later on we check that it remains NULL after each
  107. posix_memalign call. */
  108. void * ptr = NULL;
  109. size_t align;
  110. /* All aligned memory allocation functions expect an alignment that is a
  111. power of 2. Given this, we test each of them with every valid
  112. alignment from 1 thru PAGESIZE. */
  113. for (align = 1; align <= pagesize; align *= 2)
  114. {
  115. test_setup ();
  116. TEST_VERIFY (memalign (align, size) == NULL);
  117. TEST_VERIFY (errno == ENOMEM);
  118. /* posix_memalign expects an alignment that is a power of 2 *and* a
  119. multiple of sizeof (void *). */
  120. if ((align % sizeof (void *)) == 0)
  121. {
  122. test_setup ();
  123. TEST_VERIFY (posix_memalign (&ptr, align, size) == ENOMEM);
  124. TEST_VERIFY (ptr == NULL);
  125. }
  126. /* aligned_alloc expects a size that is a multiple of alignment. */
  127. if ((size % align) == 0)
  128. {
  129. test_setup ();
  130. TEST_VERIFY (aligned_alloc (align, size) == NULL);
  131. TEST_VERIFY (errno == ENOMEM);
  132. }
  133. }
  134. /* Both valloc and pvalloc return page-aligned memory. */
  135. test_setup ();
  136. TEST_VERIFY (valloc (size) == NULL);
  137. TEST_VERIFY (errno == ENOMEM);
  138. test_setup ();
  139. TEST_VERIFY (pvalloc (size) == NULL);
  140. TEST_VERIFY (errno == ENOMEM);
  141. }
  142. #define FOURTEEN_ON_BITS ((1UL << 14) - 1)
  143. #define FIFTY_ON_BITS ((1UL << 50) - 1)
  144. static int
  145. do_test (void)
  146. {
  147. #if __WORDSIZE >= 64
  148. /* This test assumes that none of the supported targets have an address
  149. bus wider than 50 bits, and that therefore allocations for sizes wider
  150. than 50 bits will fail. Here, we ensure that the assumption continues
  151. to be true in the future when we might have address buses wider than 50
  152. bits. */
  153. struct rlimit alloc_size_limit
  154. = {
  155. .rlim_cur = FIFTY_ON_BITS,
  156. .rlim_max = FIFTY_ON_BITS
  157. };
  158. setrlimit (RLIMIT_AS, &alloc_size_limit);
  159. #endif /* __WORDSIZE >= 64 */
  160. DIAG_PUSH_NEEDS_COMMENT;
  161. #if __GNUC_PREREQ (7, 0)
  162. /* GCC 7 warns about too-large allocations; here we want to test
  163. that they fail. */
  164. DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
  165. #endif
  166. /* Aligned memory allocation functions need to be tested up to alignment
  167. size equivalent to page size, which should be a power of 2. */
  168. pagesize = sysconf (_SC_PAGESIZE);
  169. TEST_VERIFY_EXIT (powerof2 (pagesize));
  170. /* Loop 1: Ensure that all allocations with SIZE close to SIZE_MAX, i.e.
  171. in the range (SIZE_MAX - 2^14, SIZE_MAX], fail.
  172. We can expect that this range of allocation sizes will always lead to
  173. an allocation failure on both 64 and 32 bit targets, because:
  174. 1. no currently supported 64-bit target has an address bus wider than
  175. 50 bits -- and (2^64 - 2^14) is much wider than that;
  176. 2. on 32-bit targets, even though 2^32 is only 4 GB and potentially
  177. addressable, glibc itself is more than 2^14 bytes in size, and
  178. therefore once glibc is loaded, less than (2^32 - 2^14) bytes remain
  179. available. */
  180. for (size_t i = 0; i <= FOURTEEN_ON_BITS; i++)
  181. {
  182. test_large_allocations (SIZE_MAX - i);
  183. test_large_aligned_allocations (SIZE_MAX - i);
  184. }
  185. #if __WORDSIZE >= 64
  186. /* On 64-bit targets, we need to test a much wider range of too-large
  187. sizes, so we test at intervals of (1 << 50) that allocation sizes
  188. ranging from SIZE_MAX down to (1 << 50) fail:
  189. The 14 MSBs are decremented starting from "all ON" going down to 1,
  190. the 50 LSBs are "all ON" and then "all OFF" during every iteration. */
  191. for (size_t msbs = FOURTEEN_ON_BITS; msbs >= 1; msbs--)
  192. {
  193. size_t size = (msbs << 50) | FIFTY_ON_BITS;
  194. test_large_allocations (size);
  195. test_large_aligned_allocations (size);
  196. size = msbs << 50;
  197. test_large_allocations (size);
  198. test_large_aligned_allocations (size);
  199. }
  200. #endif /* __WORDSIZE >= 64 */
  201. DIAG_POP_NEEDS_COMMENT;
  202. return 0;
  203. }
  204. #include <support/test-driver.c>