tst-compathooks-off.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* Minimal tests to verify libc_malloc_debug.so functionality.
  2. Copyright (C) 2021-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 <stdio.h>
  16. #include <stdlib.h>
  17. #include <malloc.h>
  18. #include <shlib-compat.h>
  19. #include <libc-diag.h>
  20. #include <support/check.h>
  21. #include <support/support.h>
  22. #include "tst-malloc-aux.h"
  23. extern void (*volatile __free_hook) (void *, const void *);
  24. extern void *(*volatile __malloc_hook)(size_t, const void *);
  25. extern void *(*volatile __realloc_hook)(void *, size_t, const void *);
  26. extern void *(*volatile __memalign_hook)(size_t, size_t, const void *);
  27. int hook_count, call_count;
  28. DIAG_PUSH_NEEDS_COMMENT;
  29. DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations");
  30. void
  31. free_called (void *mem, const void *address)
  32. {
  33. hook_count++;
  34. __free_hook = NULL;
  35. free (mem);
  36. __free_hook = free_called;
  37. }
  38. void *
  39. malloc_called (size_t bytes, const void *address)
  40. {
  41. hook_count++;
  42. __malloc_hook = NULL;
  43. void *mem = malloc (bytes);
  44. __malloc_hook = malloc_called;
  45. return mem;
  46. }
  47. void *
  48. realloc_called (void *oldptr, size_t bytes, const void *address)
  49. {
  50. hook_count++;
  51. __realloc_hook = NULL;
  52. void *mem = realloc (oldptr, bytes);
  53. __realloc_hook = realloc_called;
  54. return mem;
  55. }
  56. void *
  57. calloc_called (size_t n, size_t size, const void *address)
  58. {
  59. hook_count++;
  60. __malloc_hook = NULL;
  61. void *mem = calloc (n, size);
  62. __malloc_hook = malloc_called;
  63. return mem;
  64. }
  65. void *
  66. memalign_called (size_t align, size_t size, const void *address)
  67. {
  68. hook_count++;
  69. __memalign_hook = NULL;
  70. void *mem = memalign (align, size);
  71. __memalign_hook = memalign_called;
  72. return mem;
  73. }
  74. static void initialize_hooks (void)
  75. {
  76. __free_hook = free_called;
  77. __malloc_hook = malloc_called;
  78. __realloc_hook = realloc_called;
  79. __memalign_hook = memalign_called;
  80. }
  81. void (*__malloc_initialize_hook) (void) = initialize_hooks;
  82. compat_symbol_reference (libc, __malloc_initialize_hook,
  83. __malloc_initialize_hook, GLIBC_2_0);
  84. compat_symbol_reference (libc, __free_hook,
  85. __free_hook, GLIBC_2_0);
  86. compat_symbol_reference (libc, __malloc_hook,
  87. __malloc_hook, GLIBC_2_0);
  88. compat_symbol_reference (libc, __realloc_hook,
  89. __realloc_hook, GLIBC_2_0);
  90. compat_symbol_reference (libc, __memalign_hook,
  91. __memalign_hook, GLIBC_2_0);
  92. DIAG_POP_NEEDS_COMMENT;
  93. static int
  94. do_test (void)
  95. {
  96. void *p;
  97. p = malloc (0);
  98. TEST_VERIFY_EXIT (p != NULL);
  99. call_count++;
  100. p = realloc (p, 0);
  101. TEST_VERIFY_EXIT (p == NULL);
  102. call_count++;
  103. p = calloc (512, 1);
  104. TEST_VERIFY_EXIT (p != NULL);
  105. call_count++;
  106. free (p);
  107. call_count++;
  108. p = memalign (0x100, 0x100);
  109. TEST_VERIFY_EXIT (p != NULL);
  110. call_count++;
  111. free (p);
  112. call_count++;
  113. printf ("call_count: %d, hook_count: %d\n", call_count, hook_count);
  114. #ifdef HOOKS_ENABLED
  115. TEST_VERIFY_EXIT (call_count == hook_count);
  116. #else
  117. TEST_VERIFY_EXIT (hook_count == 0);
  118. #endif
  119. exit (0);
  120. }
  121. #include <support/test-driver.c>