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