1 # Copyright (C) 1991-2024 Free Software Foundation, Inc.
2 # 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.
9 # The GNU C Library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with the GNU C Library; if not, see
16 # <https://www.gnu.org/licenses/>.
19 # Makefile for malloc routines
25 dist-headers
:= malloc.h
26 headers
:= $(dist-headers
) obstack.h mcheck.h
33 tst-interpose-nothread \
34 tst-interpose-thread \
36 tst-malloc-backtrace \
38 tst-malloc-fork-deadlock \
39 tst-malloc-stats-cancellation \
40 tst-malloc-tcache-leak \
41 tst-malloc-thread-exit \
42 tst-malloc-thread-fail \
43 tst-malloc-too-large \
45 tst-malloc_info tst-mallinfo2 \
61 tst-tcfree1 tst-tcfree2 tst-tcfree3 \
67 tst-aligned-alloc-static \
68 tst-interpose-static-nothread \
69 tst-interpose-static-thread \
72 # Test for the malloc_set_state symbol removed in glibc 2.25.
73 ifeq ($(have-GLIBC_2.23
)$(build-shared
),yesyes
)
81 tests-internal
:= tst-scratch_buffer
83 # The dynarray framework is only available inside glibc.
86 tst-dynarray-at-fail \
91 tst-malloc-usable-tunables \
95 tests
+= $(tests-static
)
96 test-srcs
= tst-mtrace
98 # These tests either are run with MALLOC_CHECK_=3 by default or do not work
99 # with MALLOC_CHECK_=3 because they expect a specific failure.
100 tests-exclude-malloc-check
= \
101 tst-compathooks-off \
104 tst-malloc-tcache-leak \
112 # tests-exclude-malloc-check
114 # Run all tests with MALLOC_CHECK_=3
115 tests-malloc-check
= $(filter-out $(tests-exclude-malloc-check
) \
116 $(tests-static
),$(tests
))
118 # Run all tests with GLIBC_TUNABLES=glibc.malloc.hugetlb={1,2} which check
119 # the Transparent Huge Pages support (1) or automatic huge page support (2).
120 # We need exclude some tests that define the ENV vars.
121 tests-exclude-hugetlb1
= \
122 tst-compathooks-off \
124 tst-interpose-nothread \
125 tst-interpose-static-nothread \
126 tst-interpose-static-thread \
127 tst-interpose-thread \
128 tst-malloc-tcache-leak \
130 tst-malloc-usable-tunables \
134 # tests-exclude-hugetlb1
135 # The tst-free-errno relies on the used malloc page size to mmap an
136 # overlapping region.
137 tests-exclude-hugetlb2
= \
138 $(tests-exclude-hugetlb1
) \
140 tests-malloc-hugetlb1
= \
141 $(filter-out $(tests-exclude-hugetlb1
), $(tests
))
142 tests-malloc-hugetlb2
= \
143 $(filter-out $(tests-exclude-hugetlb2
), $(tests
))
145 # -lmcheck needs __malloc_initialize_hook, which was deprecated in 2.24.
146 ifeq ($(have-GLIBC_2.23
)$(build-shared
),yesyes
)
147 # Tests that don't play well with mcheck. They are either bugs in mcheck or
148 # the tests expect specific internal behavior that is changed due to linking to
150 tests-exclude-mcheck
= \
151 tst-compathooks-off \
153 tst-malloc-backtrace \
154 tst-malloc-fork-deadlock \
155 tst-malloc-stats-cancellation \
156 tst-malloc-tcache-leak \
157 tst-malloc-thread-exit \
158 tst-malloc-thread-fail \
159 tst-malloc-usable-tunables \
168 # tests-exclude-mcheck
170 tests-mcheck
= $(filter-out $(tests-exclude-mcheck
) $(tests-static
), $(tests
))
173 routines
= malloc mcheck mtrace obstack reallocarray \
174 scratch_buffer_grow scratch_buffer_grow_preserve \
175 scratch_buffer_set_array_size \
176 dynarray_at_failure \
177 dynarray_emplace_enlarge \
180 dynarray_resize_clear \
181 alloc_buffer_alloc_array \
182 alloc_buffer_allocate \
183 alloc_buffer_copy_bytes \
184 alloc_buffer_copy_string \
185 alloc_buffer_create_failure \
187 install-lib
:= libmcheck.a
188 non-lib.a
:= libmcheck.a
190 # Additional libraries.
191 extra-libs
= libmemusage libc_malloc_debug
192 extra-libs-others
= $(extra-libs
)
194 # Helper objects for some tests.
196 tst-interpose-aux-nothread.o \
197 tst-interpose-aux-thread.o \
201 tst-interpose-aux-nothread \
202 tst-interpose-aux-thread \
205 libmemusage-routines
= memusage
206 libmemusage-inhibit-o
= $(filter-out .os
,$(object-suffixes
))
208 libc_malloc_debug-routines
= malloc-debug
$(sysdep_malloc_debug_routines
)
209 libc_malloc_debug-inhibit-o
= $(filter-out .os
,$(object-suffixes
))
211 $(objpfx
)tst-malloc-backtrace
: $(shared-thread-library
)
212 $(objpfx
)tst-malloc-thread-exit
: $(shared-thread-library
)
213 $(objpfx
)tst-malloc-thread-fail
: $(shared-thread-library
)
214 $(objpfx
)tst-mallocfork3
: $(shared-thread-library
)
215 $(objpfx
)tst-mallocfork3-mcheck
: $(shared-thread-library
)
216 $(objpfx
)tst-malloc-fork-deadlock
: $(shared-thread-library
)
217 $(objpfx
)tst-malloc-stats-cancellation
: $(shared-thread-library
)
218 $(objpfx
)tst-malloc-backtrace-mcheck
: $(shared-thread-library
)
219 $(objpfx
)tst-malloc-thread-exit-mcheck
: $(shared-thread-library
)
220 $(objpfx
)tst-malloc-thread-fail-mcheck
: $(shared-thread-library
)
221 $(objpfx
)tst-malloc-fork-deadlock-mcheck
: $(shared-thread-library
)
222 $(objpfx
)tst-malloc-stats-cancellation-mcheck
: $(shared-thread-library
)
223 $(objpfx
)tst-mallocfork3-malloc-check
: $(shared-thread-library
)
224 $(objpfx
)tst-malloc-backtrace-malloc-check
: $(shared-thread-library
)
225 $(objpfx
)tst-malloc-thread-exit-malloc-check
: $(shared-thread-library
)
226 $(objpfx
)tst-malloc-thread-fail-malloc-check
: $(shared-thread-library
)
227 $(objpfx
)tst-malloc-fork-deadlock-malloc-check
: $(shared-thread-library
)
228 $(objpfx
)tst-malloc-stats-cancellation-malloc-check
: $(shared-thread-library
)
229 $(objpfx
)tst-malloc-thread-exit-malloc-hugetlb1
: $(shared-thread-library
)
230 $(objpfx
)tst-malloc-thread-fail-malloc-hugetlb1
: $(shared-thread-library
)
231 $(objpfx
)tst-malloc-fork-deadlock-malloc-hugetlb1
: $(shared-thread-library
)
232 $(objpfx
)tst-malloc-stats-cancellation-malloc-hugetlb1
: $(shared-thread-library
)
233 $(objpfx
)tst-malloc-thread-exit-malloc-hugetlb2
: $(shared-thread-library
)
234 $(objpfx
)tst-malloc-thread-fail-malloc-hugetlb2
: $(shared-thread-library
)
235 $(objpfx
)tst-malloc-fork-deadlock-malloc-hugetlb2
: $(shared-thread-library
)
236 $(objpfx
)tst-malloc-stats-cancellation-malloc-hugetlb2
: $(shared-thread-library
)
238 # These should be removed by `make clean'.
239 extra-objs
= mcheck-init.o libmcheck.a
240 others-extras
= mcheck-init.o
242 # Include the cleanup handler.
243 aux
:= set-freeres thread-freeres
245 # The Perl script to analyze the output of the mtrace functions.
247 install-bin-script
= mtrace
250 # The Perl script will print addresses and to do this nicely we must know
251 # whether we are on a 32 or 64 bit machine.
252 ifneq ($(findstring wordsize-32
,$(config-sysdirs
)),)
259 # Unless we get a test for the availability of libgd which also works
260 # for cross-compiling we disable the memusagestat generation in this
262 ifneq ($(cross-compiling
),yes
)
263 # If the gd library is available we build the `memusagestat' program.
265 others
: $(objpfx
)memusage
266 others
+= memusagestat
267 install-bin
= memusagestat
268 install-bin-script
+= memusage
269 generated
+= memusagestat memusage
270 extra-objs
+= memusagestat.o
272 # The configure.ac check for libgd and its headers did not use $SYSINCLUDES.
273 # The directory specified by --with-headers usually contains only the basic
274 # kernel interface headers, not something like libgd. So the simplest thing
275 # is to presume that the standard system headers will be ok for this file.
276 $(objpfx
)memusagestat.o
: sysincludes
= # nothing
280 # Another goal which can be used to override the configure decision.
281 .PHONY
: do-memusagestat
282 do-memusagestat
: $(objpfx
)memusagestat
284 memusagestat-modules
= memusagestat
286 cpp-srcs-left
:= $(memusagestat-modules
)
288 include $(patsubst %,$(..
)libof-iterator.mk
,$(cpp-srcs-left
))
290 LDLIBS-memusagestat
= $(libgd-LDFLAGS
) -lgd
-lpng
-lz
-lm
292 ifeq ($(run-built-tests
),yes
)
293 ifeq (yes
,$(build-shared
))
295 tests-special
+= $(objpfx
)tst-mtrace.out
296 tests-special
+= $(objpfx
)tst-dynarray-mem.out
297 tests-special
+= $(objpfx
)tst-dynarray-fail-mem.out
304 CFLAGS-mcheck-init.c
+= $(PIC-ccflag
)
305 CFLAGS-obstack.c
+= $(uses-callbacks
)
307 $(objpfx
)libmcheck.a
: $(objpfx
)mcheck-init.o
309 $(patsubst %/,cd
% &&,$(objpfx
)) \
312 lib
: $(objpfx
)libmcheck.a
314 ifeq ($(run-built-tests
),yes
)
315 ifeq (yes
,$(build-shared
))
317 $(objpfx
)tst-mtrace.out
: tst-mtrace.sh
$(objpfx
)tst-mtrace
318 $(SHELL
) $< $(common-objpfx
) '$(test-program-prefix-before-env)' \
319 '$(run-program-env)' '$(test-program-prefix-after-env)'; \
325 tst-malloc-check-ENV
= MALLOC_CHECK_
=3 \
326 LD_PRELOAD
=$(objpfx
)/libc_malloc_debug.so
327 tst-malloc-usable-ENV
= MALLOC_CHECK_
=3 \
328 LD_PRELOAD
=$(objpfx
)/libc_malloc_debug.so
329 tst-malloc-usable-tunables-ENV
= GLIBC_TUNABLES
=glibc.malloc.
check=3 \
330 LD_PRELOAD
=$(objpfx
)/libc_malloc_debug.so
332 tst-mxfast-ENV
= GLIBC_TUNABLES
=glibc.malloc.tcache_count
=0:glibc.malloc.mxfast
=0
334 CPPFLAGS-malloc-debug.c
+= -DUSE_TCACHE
=0
335 CPPFLAGS-malloc.c
+= -DUSE_TCACHE
=1
336 # Uncomment this for test releases. For public releases it is too expensive.
337 #CPPFLAGS-malloc.o += -DMALLOC_DEBUG=1
339 CFLAGS-tst-tcfree3.c
+= -fno-builtin-malloc
-fno-builtin-free
341 sLIBdir
:= $(shell echo
$(slibdir
) | sed
's,lib\(\|64\)$$,\\\\$$LIB,')
343 $(objpfx
)mtrace
: mtrace.pl
345 sed
-e
's|@PERL@|$(PERL)|' -e
's|@XXX@|$(address-width)|' \
346 -e
's|@VERSION@|$(version)|' \
347 -e
's|@PKGVERSION@|$(PKGVERSION)|' \
348 -e
's|@REPORT_BUGS_TO@|$(REPORT_BUGS_TO)|' $^
> $@.new \
349 && rm -f
$@
&& mv
$@.new
$@
&& chmod
+x
$@
351 $(objpfx
)memusage
: memusage.sh
353 sed
-e
's|@VERSION@|$(version)|' -e
's|@SLIBDIR@|$(sLIBdir)|' \
354 -e
's|@BINDIR@|$(bindir)|' -e
's|@PKGVERSION@|$(PKGVERSION)|' \
355 -e
's|@REPORT_BUGS_TO@|$(REPORT_BUGS_TO)|' $^
> $@.new \
356 && rm -f
$@
&& mv
$@.new
$@
&& chmod
+x
$@
358 # Compile the tests with a flag which suppresses the mallopt call in
360 $(tests
:%=$(objpfx
)%.o
): CPPFLAGS
+= -DTEST_NO_MALLOPT
362 $(objpfx
)tst-interpose-nothread
: $(objpfx
)tst-interpose-aux-nothread.o
363 $(objpfx
)tst-interpose-nothread-mcheck
: $(objpfx
)tst-interpose-aux-nothread.o
364 $(objpfx
)tst-interpose-nothread-malloc-check
: \
365 $(objpfx
)tst-interpose-aux-nothread.o
366 $(objpfx
)tst-interpose-thread
: \
367 $(objpfx
)tst-interpose-aux-thread.o
$(shared-thread-library
)
368 $(objpfx
)tst-interpose-thread-mcheck
: \
369 $(objpfx
)tst-interpose-aux-thread.o
$(shared-thread-library
)
370 $(objpfx
)tst-interpose-thread-malloc-check
: \
371 $(objpfx
)tst-interpose-aux-thread.o
$(shared-thread-library
)
372 $(objpfx
)tst-interpose-static-nothread
: $(objpfx
)tst-interpose-aux-nothread.o
373 $(objpfx
)tst-interpose-static-thread
: \
374 $(objpfx
)tst-interpose-aux-thread.o
$(static-thread-library
)
376 tst-dynarray-ENV
= MALLOC_TRACE
=$(objpfx
)tst-dynarray.mtrace \
377 LD_PRELOAD
=$(objpfx
)libc_malloc_debug.so
378 $(objpfx
)tst-dynarray-mem.out
: $(objpfx
)tst-dynarray.out
379 $(common-objpfx
)malloc
/mtrace
$(objpfx
)tst-dynarray.mtrace
> $@
; \
382 tst-dynarray-fail-ENV
= MALLOC_TRACE
=$(objpfx
)tst-dynarray-fail.mtrace \
383 LD_PRELOAD
=$(objpfx
)libc_malloc_debug.so
384 $(objpfx
)tst-dynarray-fail-mem.out
: $(objpfx
)tst-dynarray-fail.out
385 $(common-objpfx
)malloc
/mtrace
$(objpfx
)tst-dynarray-fail.mtrace
> $@
; \
388 $(objpfx
)tst-malloc-tcache-leak
: $(shared-thread-library
)
389 $(objpfx
)tst-malloc_info
: $(shared-thread-library
)
390 $(objpfx
)tst-mallocfork2
: $(shared-thread-library
)
391 $(objpfx
)tst-malloc_info-mcheck
: $(shared-thread-library
)
392 $(objpfx
)tst-malloc_info-malloc-check
: $(shared-thread-library
)
393 $(objpfx
)tst-mallocfork2-malloc-check
: $(shared-thread-library
)
394 $(objpfx
)tst-malloc_info-malloc-hugetlb1
: $(shared-thread-library
)
395 $(objpfx
)tst-malloc_info-malloc-hugetlb2
: $(shared-thread-library
)
396 $(objpfx
)tst-memalign-3
: $(shared-thread-library
)
397 $(objpfx
)tst-memalign-3-malloc-hugetlb1
: $(shared-thread-library
)
398 $(objpfx
)tst-memalign-3-malloc-hugetlb2
: $(shared-thread-library
)
400 tst-compathooks-on-ENV
= LD_PRELOAD
=$(objpfx
)libc_malloc_debug.so
401 tst-compathooks-on-mcheck-ENV
= LD_PRELOAD
=$(objpfx
)libc_malloc_debug.so
402 tst-compathooks-on-malloc-check-ENV
= \
403 LD_PRELOAD
=$(objpfx
)libc_malloc_debug.so
404 tst-mallocstate-ENV
= LD_PRELOAD
=$(objpfx
)libc_malloc_debug.so
405 tst-mallocstate-malloc-check-ENV
= LD_PRELOAD
=$(objpfx
)libc_malloc_debug.so
407 # The test needs malloc_get_state/malloc_set_state which is in
408 # libc_malloc_debug.so.
409 $(objpfx
)tst-mallocstate
: $(objpfx
)libc_malloc_debug.so
410 $(objpfx
)tst-mallocstate-malloc-check
: $(objpfx
)libc_malloc_debug.so