tst-malloc-alternate-path.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* Test that malloc uses mmap when sbrk or brk fails.
  2. Copyright (C) 2024-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 sets up an obstruction to ensure that brk/sbrk fails to
  16. grow the heap, then verifies that malloc uses mmap for allocations
  17. instead. */
  18. #include <unistd.h>
  19. #include <sys/mman.h>
  20. #include <stdlib.h>
  21. #include <libc-pointer-arith.h>
  22. #include <support/check.h>
  23. #include <stddef.h>
  24. #include <stdalign.h>
  25. #define LARGE_SIZE (10 * (1 << 20)) // 10 MB
  26. static long page_size;
  27. static int
  28. do_test (void)
  29. {
  30. /* Get current program break. */
  31. void *current_brk = sbrk (0);
  32. page_size = sysconf (_SC_PAGESIZE);
  33. /* Round up to the next page boundary. */
  34. void *next_page_boundary = PTR_ALIGN_UP (current_brk, page_size);
  35. /* Place a mapping using mmap at the next page boundary. */
  36. void *obstruction_addr
  37. = mmap (next_page_boundary, page_size, PROT_READ,
  38. MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
  39. /* Check if memory obstruction is set up correctly. */
  40. TEST_VERIFY_EXIT (obstruction_addr == next_page_boundary);
  41. /* Try to extend the heap beyond the obstruction using sbrk */
  42. int *ptr = sbrk (page_size);
  43. TEST_VERIFY_EXIT (ptr == (void *) -1);
  44. /* Attempt multiple small allocations using malloc. */
  45. for (size_t i = 0; i < page_size / alignof (max_align_t); i++)
  46. {
  47. TEST_VERIFY (malloc (alignof (max_align_t)));
  48. }
  49. /* Attempt to allocate a large block of memory using malloc. */
  50. TEST_VERIFY_EXIT (malloc (LARGE_SIZE) != NULL);
  51. /* Check if malloc changed current program break. */
  52. TEST_VERIFY_EXIT (current_brk == sbrk (0));
  53. return 0;
  54. }
  55. #include <support/test-driver.c>