setipv4sourcefilter: Avoid using alloca.
[glibc.git] / malloc / Makefile
blobce6ebcf1b3fb39296093b9a8a0b8c361fb1381bc
1 # Copyright (C) 1991-2023 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
21 subdir := malloc
23 include ../Makeconfig
25 dist-headers := malloc.h
26 headers := $(dist-headers) obstack.h mcheck.h
27 tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \
28 tst-malloc-check tst-mallocfork tst-trim1 \
29 tst-malloc-usable tst-realloc tst-reallocarray tst-posix_memalign \
30 tst-pvalloc tst-pvalloc-fortify tst-memalign tst-mallopt \
31 tst-malloc-backtrace tst-malloc-thread-exit \
32 tst-malloc-thread-fail tst-malloc-fork-deadlock \
33 tst-mallocfork2 \
34 tst-mallocfork3 \
35 tst-interpose-nothread \
36 tst-interpose-thread \
37 tst-alloc_buffer \
38 tst-free-errno \
39 tst-malloc-tcache-leak \
40 tst-malloc_info tst-mallinfo2 \
41 tst-malloc-too-large \
42 tst-malloc-stats-cancellation \
43 tst-tcfree1 tst-tcfree2 tst-tcfree3 \
44 tst-safe-linking \
45 tst-mallocalign1 \
46 tst-memalign-2 \
47 tst-memalign-3 \
48 tst-aligned-alloc
50 tests-static := \
51 tst-interpose-static-nothread \
52 tst-interpose-static-thread \
53 tst-aligned-alloc-static
55 # Test for the malloc_set_state symbol removed in glibc 2.25.
56 ifeq ($(have-GLIBC_2.23)$(build-shared),yesyes)
57 tests += tst-mallocstate tst-compathooks-off tst-compathooks-on
58 endif
60 tests-internal := tst-scratch_buffer
62 # The dynarray framework is only available inside glibc.
63 tests-internal += \
64 tst-dynarray \
65 tst-dynarray-fail \
66 tst-dynarray-at-fail \
68 tests += tst-malloc-usable-tunables tst-mxfast
70 tests += $(tests-static)
71 test-srcs = tst-mtrace
73 # These tests either are run with MALLOC_CHECK_=3 by default or do not work
74 # with MALLOC_CHECK_=3 because they expect a specific failure.
75 tests-exclude-malloc-check = tst-malloc-check tst-malloc-usable \
76 tst-mxfast tst-safe-linking \
77 tst-compathooks-off tst-compathooks-on tst-memalign-2 tst-memalign-3
79 # Run all tests with MALLOC_CHECK_=3
80 tests-malloc-check = $(filter-out $(tests-exclude-malloc-check) \
81 $(tests-static),$(tests))
83 # Run all tests with GLIBC_TUNABLES=glibc.malloc.hugetlb={1,2} which check
84 # the Transparent Huge Pages support (1) or automatic huge page support (2).
85 # We need exclude some tests that define the ENV vars.
86 tests-exclude-hugetlb1 = \
87 tst-compathooks-off \
88 tst-compathooks-on \
89 tst-interpose-nothread \
90 tst-interpose-thread \
91 tst-interpose-static-nothread \
92 tst-interpose-static-thread \
93 tst-malloc-usable \
94 tst-malloc-usable-tunables \
95 tst-mallocstate
96 # The tst-free-errno relies on the used malloc page size to mmap an
97 # overlapping region.
98 tests-exclude-hugetlb2 = \
99 $(tests-exclude-hugetlb1) \
100 tst-free-errno
101 tests-malloc-hugetlb1 = \
102 $(filter-out $(tests-exclude-hugetlb1), $(tests))
103 tests-malloc-hugetlb2 = \
104 $(filter-out $(tests-exclude-hugetlb2), $(tests))
106 # -lmcheck needs __malloc_initialize_hook, which was deprecated in 2.24.
107 ifeq ($(have-GLIBC_2.23)$(build-shared),yesyes)
108 # Tests that don't play well with mcheck. They are either bugs in mcheck or
109 # the tests expect specific internal behavior that is changed due to linking to
110 # libmcheck.a.
111 tests-exclude-mcheck = tst-mallocstate \
112 tst-safe-linking \
113 tst-malloc-backtrace \
114 tst-malloc-fork-deadlock \
115 tst-malloc-stats-cancellation \
116 tst-malloc-tcache-leak \
117 tst-malloc-thread-exit \
118 tst-malloc-thread-fail \
119 tst-malloc-usable-tunables \
120 tst-malloc_info \
121 tst-compathooks-off tst-compathooks-on \
122 tst-memalign-2 \
123 tst-memalign-3 \
124 tst-mxfast
126 tests-mcheck = $(filter-out $(tests-exclude-mcheck) $(tests-static), $(tests))
127 endif
129 routines = malloc mcheck mtrace obstack reallocarray \
130 scratch_buffer_grow scratch_buffer_grow_preserve \
131 scratch_buffer_set_array_size \
132 dynarray_at_failure \
133 dynarray_emplace_enlarge \
134 dynarray_finalize \
135 dynarray_resize \
136 dynarray_resize_clear \
137 alloc_buffer_alloc_array \
138 alloc_buffer_allocate \
139 alloc_buffer_copy_bytes \
140 alloc_buffer_copy_string \
141 alloc_buffer_create_failure \
143 install-lib := libmcheck.a
144 non-lib.a := libmcheck.a
146 # Additional libraries.
147 extra-libs = libmemusage libc_malloc_debug
148 extra-libs-others = $(extra-libs)
150 # Helper objects for some tests.
151 extra-test-objs += \
152 tst-interpose-aux-nothread.o \
153 tst-interpose-aux-thread.o \
155 test-extras = \
156 tst-interpose-aux-nothread \
157 tst-interpose-aux-thread \
159 libmemusage-routines = memusage
160 libmemusage-inhibit-o = $(filter-out .os,$(object-suffixes))
162 libc_malloc_debug-routines = malloc-debug $(sysdep_malloc_debug_routines)
163 libc_malloc_debug-inhibit-o = $(filter-out .os,$(object-suffixes))
165 $(objpfx)tst-malloc-backtrace: $(shared-thread-library)
166 $(objpfx)tst-malloc-thread-exit: $(shared-thread-library)
167 $(objpfx)tst-malloc-thread-fail: $(shared-thread-library)
168 $(objpfx)tst-mallocfork3: $(shared-thread-library)
169 $(objpfx)tst-mallocfork3-mcheck: $(shared-thread-library)
170 $(objpfx)tst-malloc-fork-deadlock: $(shared-thread-library)
171 $(objpfx)tst-malloc-stats-cancellation: $(shared-thread-library)
172 $(objpfx)tst-malloc-backtrace-mcheck: $(shared-thread-library)
173 $(objpfx)tst-malloc-thread-exit-mcheck: $(shared-thread-library)
174 $(objpfx)tst-malloc-thread-fail-mcheck: $(shared-thread-library)
175 $(objpfx)tst-malloc-fork-deadlock-mcheck: $(shared-thread-library)
176 $(objpfx)tst-malloc-stats-cancellation-mcheck: $(shared-thread-library)
177 $(objpfx)tst-mallocfork3-malloc-check: $(shared-thread-library)
178 $(objpfx)tst-malloc-backtrace-malloc-check: $(shared-thread-library)
179 $(objpfx)tst-malloc-thread-exit-malloc-check: $(shared-thread-library)
180 $(objpfx)tst-malloc-thread-fail-malloc-check: $(shared-thread-library)
181 $(objpfx)tst-malloc-fork-deadlock-malloc-check: $(shared-thread-library)
182 $(objpfx)tst-malloc-stats-cancellation-malloc-check: $(shared-thread-library)
183 $(objpfx)tst-malloc-thread-exit-malloc-hugetlb1: $(shared-thread-library)
184 $(objpfx)tst-malloc-thread-fail-malloc-hugetlb1: $(shared-thread-library)
185 $(objpfx)tst-mallocfork2-malloc-hugetlb1: $(shared-thread-library)
186 $(objpfx)tst-mallocfork3-malloc-hugetlb1: $(shared-thread-library)
187 $(objpfx)tst-malloc-fork-deadlock-malloc-hugetlb1: $(shared-thread-library)
188 $(objpfx)tst-malloc-stats-cancellation-malloc-hugetlb1: $(shared-thread-library)
189 $(objpfx)tst-malloc-thread-exit-malloc-hugetlb2: $(shared-thread-library)
190 $(objpfx)tst-malloc-thread-fail-malloc-hugetlb2: $(shared-thread-library)
191 $(objpfx)tst-mallocfork2-malloc-hugetlb2: $(shared-thread-library)
192 $(objpfx)tst-mallocfork3-malloc-hugetlb2: $(shared-thread-library)
193 $(objpfx)tst-malloc-fork-deadlock-malloc-hugetlb2: $(shared-thread-library)
194 $(objpfx)tst-malloc-stats-cancellation-malloc-hugetlb2: $(shared-thread-library)
196 # These should be removed by `make clean'.
197 extra-objs = mcheck-init.o libmcheck.a
198 others-extras = mcheck-init.o
200 # Include the cleanup handler.
201 aux := set-freeres thread-freeres
203 # The Perl script to analyze the output of the mtrace functions.
204 ifneq ($(PERL),no)
205 install-bin-script = mtrace
206 generated += mtrace
208 # The Perl script will print addresses and to do this nicely we must know
209 # whether we are on a 32 or 64 bit machine.
210 ifneq ($(findstring wordsize-32,$(config-sysdirs)),)
211 address-width=10
212 else
213 address-width=18
214 endif
215 endif
217 # Unless we get a test for the availability of libgd which also works
218 # for cross-compiling we disable the memusagestat generation in this
219 # situation.
220 ifneq ($(cross-compiling),yes)
221 # If the gd library is available we build the `memusagestat' program.
222 ifneq ($(LIBGD),no)
223 others: $(objpfx)memusage
224 others += memusagestat
225 install-bin = memusagestat
226 install-bin-script += memusage
227 generated += memusagestat memusage
228 extra-objs += memusagestat.o
230 # The configure.ac check for libgd and its headers did not use $SYSINCLUDES.
231 # The directory specified by --with-headers usually contains only the basic
232 # kernel interface headers, not something like libgd. So the simplest thing
233 # is to presume that the standard system headers will be ok for this file.
234 $(objpfx)memusagestat.o: sysincludes = # nothing
235 endif
236 endif
238 # Another goal which can be used to override the configure decision.
239 .PHONY: do-memusagestat
240 do-memusagestat: $(objpfx)memusagestat
242 memusagestat-modules = memusagestat
244 cpp-srcs-left := $(memusagestat-modules)
245 lib := memusagestat
246 include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left))
248 LDLIBS-memusagestat = $(libgd-LDFLAGS) -lgd -lpng -lz -lm
250 ifeq ($(run-built-tests),yes)
251 ifeq (yes,$(build-shared))
252 ifneq ($(PERL),no)
253 tests-special += $(objpfx)tst-mtrace.out
254 tests-special += $(objpfx)tst-dynarray-mem.out
255 tests-special += $(objpfx)tst-dynarray-fail-mem.out
256 endif
257 endif
258 endif
260 include ../Rules
262 CFLAGS-mcheck-init.c += $(PIC-ccflag)
263 CFLAGS-obstack.c += $(uses-callbacks)
265 $(objpfx)libmcheck.a: $(objpfx)mcheck-init.o
266 -rm -f $@
267 $(patsubst %/,cd % &&,$(objpfx)) \
268 $(LN_S) $(<F) $(@F)
270 lib: $(objpfx)libmcheck.a
272 ifeq ($(run-built-tests),yes)
273 ifeq (yes,$(build-shared))
274 ifneq ($(PERL),no)
275 $(objpfx)tst-mtrace.out: tst-mtrace.sh $(objpfx)tst-mtrace
276 $(SHELL) $< $(common-objpfx) '$(test-program-prefix-before-env)' \
277 '$(run-program-env)' '$(test-program-prefix-after-env)'; \
278 $(evaluate-test)
279 endif
280 endif
281 endif
283 tst-malloc-check-ENV = MALLOC_CHECK_=3 \
284 LD_PRELOAD=$(objpfx)/libc_malloc_debug.so
285 tst-malloc-usable-ENV = MALLOC_CHECK_=3 \
286 LD_PRELOAD=$(objpfx)/libc_malloc_debug.so
287 tst-malloc-usable-tunables-ENV = GLIBC_TUNABLES=glibc.malloc.check=3 \
288 LD_PRELOAD=$(objpfx)/libc_malloc_debug.so
290 tst-mxfast-ENV = GLIBC_TUNABLES=glibc.malloc.tcache_count=0:glibc.malloc.mxfast=0
292 CPPFLAGS-malloc-debug.c += -DUSE_TCACHE=0
293 CPPFLAGS-malloc.c += -DUSE_TCACHE=1
294 # Uncomment this for test releases. For public releases it is too expensive.
295 #CPPFLAGS-malloc.o += -DMALLOC_DEBUG=1
297 sLIBdir := $(shell echo $(slibdir) | sed 's,lib\(\|64\)$$,\\\\$$LIB,')
299 $(objpfx)mtrace: mtrace.pl
300 rm -f $@.new
301 sed -e 's|@PERL@|$(PERL)|' -e 's|@XXX@|$(address-width)|' \
302 -e 's|@VERSION@|$(version)|' \
303 -e 's|@PKGVERSION@|$(PKGVERSION)|' \
304 -e 's|@REPORT_BUGS_TO@|$(REPORT_BUGS_TO)|' $^ > $@.new \
305 && rm -f $@ && mv $@.new $@ && chmod +x $@
307 $(objpfx)memusage: memusage.sh
308 rm -f $@.new
309 sed -e 's|@VERSION@|$(version)|' -e 's|@SLIBDIR@|$(sLIBdir)|' \
310 -e 's|@BINDIR@|$(bindir)|' -e 's|@PKGVERSION@|$(PKGVERSION)|' \
311 -e 's|@REPORT_BUGS_TO@|$(REPORT_BUGS_TO)|' $^ > $@.new \
312 && rm -f $@ && mv $@.new $@ && chmod +x $@
314 # Compile the tests with a flag which suppresses the mallopt call in
315 # the test skeleton.
316 $(tests:%=$(objpfx)%.o): CPPFLAGS += -DTEST_NO_MALLOPT
318 $(objpfx)tst-interpose-nothread: $(objpfx)tst-interpose-aux-nothread.o
319 $(objpfx)tst-interpose-nothread-mcheck: $(objpfx)tst-interpose-aux-nothread.o
320 $(objpfx)tst-interpose-nothread-malloc-check: \
321 $(objpfx)tst-interpose-aux-nothread.o
322 $(objpfx)tst-interpose-thread: \
323 $(objpfx)tst-interpose-aux-thread.o $(shared-thread-library)
324 $(objpfx)tst-interpose-thread-mcheck: \
325 $(objpfx)tst-interpose-aux-thread.o $(shared-thread-library)
326 $(objpfx)tst-interpose-thread-malloc-check: \
327 $(objpfx)tst-interpose-aux-thread.o $(shared-thread-library)
328 $(objpfx)tst-interpose-static-nothread: $(objpfx)tst-interpose-aux-nothread.o
329 $(objpfx)tst-interpose-static-thread: \
330 $(objpfx)tst-interpose-aux-thread.o $(static-thread-library)
332 tst-dynarray-ENV = MALLOC_TRACE=$(objpfx)tst-dynarray.mtrace \
333 LD_PRELOAD=$(objpfx)libc_malloc_debug.so
334 $(objpfx)tst-dynarray-mem.out: $(objpfx)tst-dynarray.out
335 $(common-objpfx)malloc/mtrace $(objpfx)tst-dynarray.mtrace > $@; \
336 $(evaluate-test)
338 tst-dynarray-fail-ENV = MALLOC_TRACE=$(objpfx)tst-dynarray-fail.mtrace \
339 LD_PRELOAD=$(objpfx)libc_malloc_debug.so
340 $(objpfx)tst-dynarray-fail-mem.out: $(objpfx)tst-dynarray-fail.out
341 $(common-objpfx)malloc/mtrace $(objpfx)tst-dynarray-fail.mtrace > $@; \
342 $(evaluate-test)
344 $(objpfx)tst-malloc-tcache-leak: $(shared-thread-library)
345 $(objpfx)tst-malloc_info: $(shared-thread-library)
346 $(objpfx)tst-mallocfork2: $(shared-thread-library)
347 $(objpfx)tst-malloc-tcache-leak-mcheck: $(shared-thread-library)
348 $(objpfx)tst-malloc_info-mcheck: $(shared-thread-library)
349 $(objpfx)tst-mallocfork2-mcheck: $(shared-thread-library)
350 $(objpfx)tst-malloc-tcache-leak-malloc-check: $(shared-thread-library)
351 $(objpfx)tst-malloc_info-malloc-check: $(shared-thread-library)
352 $(objpfx)tst-mallocfork2-malloc-check: $(shared-thread-library)
353 $(objpfx)tst-malloc-tcache-leak-malloc-hugetlb1: $(shared-thread-library)
354 $(objpfx)tst-malloc-tcache-leak-malloc-hugetlb2: $(shared-thread-library)
355 $(objpfx)tst-malloc_info-malloc-hugetlb1: $(shared-thread-library)
356 $(objpfx)tst-malloc_info-malloc-hugetlb2: $(shared-thread-library)
357 $(objpfx)tst-memalign-3: $(shared-thread-library)
358 $(objpfx)tst-memalign-3-malloc-hugetlb1: $(shared-thread-library)
359 $(objpfx)tst-memalign-3-malloc-hugetlb2: $(shared-thread-library)
361 tst-compathooks-on-ENV = LD_PRELOAD=$(objpfx)libc_malloc_debug.so
362 tst-compathooks-on-mcheck-ENV = LD_PRELOAD=$(objpfx)libc_malloc_debug.so
363 tst-compathooks-on-malloc-check-ENV = \
364 LD_PRELOAD=$(objpfx)libc_malloc_debug.so
365 tst-mallocstate-ENV = LD_PRELOAD=$(objpfx)libc_malloc_debug.so
366 tst-mallocstate-malloc-check-ENV = LD_PRELOAD=$(objpfx)libc_malloc_debug.so
368 # The test needs malloc_get_state/malloc_set_state which is in
369 # libc_malloc_debug.so.
370 $(objpfx)tst-mallocstate: $(objpfx)libc_malloc_debug.so
371 $(objpfx)tst-mallocstate-malloc-check: $(objpfx)libc_malloc_debug.so