tst-mallocstate.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* Emulate Emacs heap dumping to test malloc_set_state.
  2. Copyright (C) 2001-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. #include <errno.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <libc-symbols.h>
  19. #include <shlib-compat.h>
  20. #include <support/check.h>
  21. #include <support/support.h>
  22. #include <support/test-driver.h>
  23. #include "malloc.h"
  24. /* Make the compatibility symbols availabile to this test case. */
  25. void *malloc_get_state (void);
  26. compat_symbol_reference (libc, malloc_get_state, malloc_get_state, GLIBC_2_0);
  27. int malloc_set_state (void *);
  28. compat_symbol_reference (libc, malloc_set_state, malloc_set_state, GLIBC_2_0);
  29. #define NBINS 128
  30. static struct
  31. {
  32. long magic;
  33. long version;
  34. void *av[NBINS * 2 + 2];
  35. char *sbrk_base;
  36. int sbrked_mem_bytes;
  37. unsigned long trim_threshold;
  38. unsigned long top_pad;
  39. unsigned int n_mmaps_max;
  40. unsigned long mmap_threshold;
  41. int check_action;
  42. unsigned long max_sbrked_mem;
  43. unsigned long max_total_mem;
  44. unsigned int n_mmaps;
  45. unsigned int max_n_mmaps;
  46. unsigned long mmapped_mem;
  47. unsigned long max_mmapped_mem;
  48. int using_malloc_checking;
  49. unsigned long max_fast;
  50. unsigned long arena_test;
  51. unsigned long arena_max;
  52. unsigned long narenas;
  53. } save_state;
  54. static int
  55. do_test (void)
  56. {
  57. /* Check the dummy implementations always fail. */
  58. TEST_VERIFY_EXIT (malloc_set_state (&save_state) == -1);
  59. errno = 0;
  60. TEST_VERIFY_EXIT (malloc_get_state () == NULL);
  61. TEST_VERIFY_EXIT (errno == ENOSYS);
  62. return 0;
  63. }
  64. #include <support/test-driver.c>