1 /* Minimal tests to verify libc_malloc_debug.so functionality.
2 Copyright (C) 2021-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
22 #include <shlib-compat.h>
23 #include <libc-diag.h>
25 #include <support/check.h>
26 #include <support/support.h>
28 extern void (*volatile __free_hook
) (void *, const void *);
29 extern void *(*volatile __malloc_hook
)(size_t, const void *);
30 extern void *(*volatile __realloc_hook
)(void *, size_t, const void *);
31 extern void *(*volatile __memalign_hook
)(size_t, size_t, const void *);
33 int hook_count
, call_count
;
35 DIAG_PUSH_NEEDS_COMMENT
;
36 DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Wdeprecated-declarations");
39 free_called (void *mem
, const void *address
)
44 __free_hook
= free_called
;
48 malloc_called (size_t bytes
, const void *address
)
52 void *mem
= malloc (bytes
);
53 __malloc_hook
= malloc_called
;
58 realloc_called (void *oldptr
, size_t bytes
, const void *address
)
61 __realloc_hook
= NULL
;
62 void *mem
= realloc (oldptr
, bytes
);
63 __realloc_hook
= realloc_called
;
68 calloc_called (size_t n
, size_t size
, const void *address
)
72 void *mem
= calloc (n
, size
);
73 __malloc_hook
= malloc_called
;
78 memalign_called (size_t align
, size_t size
, const void *address
)
81 __memalign_hook
= NULL
;
82 void *mem
= memalign (align
, size
);
83 __memalign_hook
= memalign_called
;
87 static void initialize_hooks (void)
89 __free_hook
= free_called
;
90 __malloc_hook
= malloc_called
;
91 __realloc_hook
= realloc_called
;
92 __memalign_hook
= memalign_called
;
94 void (*__malloc_initialize_hook
) (void) = initialize_hooks
;
95 compat_symbol_reference (libc
, __malloc_initialize_hook
,
96 __malloc_initialize_hook
, GLIBC_2_0
);
97 compat_symbol_reference (libc
, __free_hook
,
98 __free_hook
, GLIBC_2_0
);
99 compat_symbol_reference (libc
, __malloc_hook
,
100 __malloc_hook
, GLIBC_2_0
);
101 compat_symbol_reference (libc
, __realloc_hook
,
102 __realloc_hook
, GLIBC_2_0
);
103 compat_symbol_reference (libc
, __memalign_hook
,
104 __memalign_hook
, GLIBC_2_0
);
106 DIAG_POP_NEEDS_COMMENT
;
113 TEST_VERIFY_EXIT (p
!= NULL
);
117 TEST_VERIFY_EXIT (p
== NULL
);
121 TEST_VERIFY_EXIT (p
!= NULL
);
127 p
= memalign (0x100, 0x100);
128 TEST_VERIFY_EXIT (p
!= NULL
);
134 printf ("call_count: %d, hook_count: %d\n", call_count
, hook_count
);
137 TEST_VERIFY_EXIT (call_count
== hook_count
);
139 TEST_VERIFY_EXIT (hook_count
== 0);
145 #include <support/test-driver.c>