Linux: consolidate dup2 implementation
[glibc.git] / malloc / Makefile
blob211be75e368b0541241d13696a68c3cfe5b652a3
1 # Copyright (C) 1991-2022 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 \
47 tests-static := \
48 tst-interpose-static-nothread \
49 tst-interpose-static-thread
51 # Test for the malloc_set_state symbol removed in glibc 2.25.
52 ifeq ($(have-GLIBC_2.23)$(build-shared),yesyes)
53 tests += tst-mallocstate tst-compathooks-off tst-compathooks-on
54 endif
56 tests-internal := tst-scratch_buffer
58 # The dynarray framework is only available inside glibc.
59 tests-internal += \
60 tst-dynarray \
61 tst-dynarray-fail \
62 tst-dynarray-at-fail \
64 ifneq (no,$(have-tunables))
65 tests += tst-malloc-usable-tunables tst-mxfast
66 endif
68 tests += $(tests-static)
69 test-srcs = tst-mtrace
71 # These tests either are run with MALLOC_CHECK_=3 by default or do not work
72 # with MALLOC_CHECK_=3 because they expect a specific failure.
73 tests-exclude-malloc-check = tst-malloc-check tst-malloc-usable \
74 tst-mxfast tst-safe-linking \
75 tst-compathooks-off tst-compathooks-on
77 # Run all tests with MALLOC_CHECK_=3
78 tests-malloc-check = $(filter-out $(tests-exclude-malloc-check) \
79 $(tests-static),$(tests))
81 # Run all tests with GLIBC_TUNABLES=glibc.malloc.hugetlb={1,2} which check
82 # the Transparent Huge Pages support (1) or automatic huge page support (2).
83 # We need exclude some tests that define the ENV vars.
84 tests-exclude-hugetlb1 = \
85 tst-compathooks-off \
86 tst-compathooks-on \
87 tst-interpose-nothread \
88 tst-interpose-thread \
89 tst-interpose-static-nothread \
90 tst-interpose-static-thread \
91 tst-malloc-usable \
92 tst-malloc-usable-tunables \
93 tst-mallocstate
94 # The tst-free-errno relies on the used malloc page size to mmap an
95 # overlapping region.
96 tests-exclude-hugetlb2 = \
97 $(tests-exclude-hugetlb1) \
98 tst-free-errno
99 tests-malloc-hugetlb1 = \
100 $(filter-out $(tests-exclude-hugetlb1), $(tests))
101 tests-malloc-hugetlb2 = \
102 $(filter-out $(tests-exclude-hugetlb2), $(tests))
104 # -lmcheck needs __malloc_initialize_hook, which was deprecated in 2.24.
105 ifeq ($(have-GLIBC_2.23)$(build-shared),yesyes)
106 # Tests that don't play well with mcheck. They are either bugs in mcheck or
107 # the tests expect specific internal behavior that is changed due to linking to
108 # libmcheck.a.
109 tests-exclude-mcheck = tst-mallocstate \
110 tst-safe-linking \
111 tst-malloc-backtrace \
112 tst-malloc-fork-deadlock \
113 tst-malloc-stats-cancellation \
114 tst-malloc-tcache-leak \
115 tst-malloc-thread-exit \
116 tst-malloc-thread-fail \
117 tst-malloc-usable-tunables \
118 tst-malloc_info \
119 tst-compathooks-off tst-compathooks-on \
120 tst-mxfast
122 tests-mcheck = $(filter-out $(tests-exclude-mcheck) $(tests-static), $(tests))
123 endif
125 routines = malloc mcheck mtrace obstack reallocarray \
126 scratch_buffer_grow scratch_buffer_grow_preserve \
127 scratch_buffer_set_array_size \
128 dynarray_at_failure \
129 dynarray_emplace_enlarge \
130 dynarray_finalize \
131 dynarray_resize \
132 dynarray_resize_clear \
133 alloc_buffer_alloc_array \
134 alloc_buffer_allocate \
135 alloc_buffer_copy_bytes \
136 alloc_buffer_copy_string \
137 alloc_buffer_create_failure \
139 install-lib := libmcheck.a
140 non-lib.a := libmcheck.a
142 # Additional libraries.
143 extra-libs = libmemusage libc_malloc_debug
144 extra-libs-others = $(extra-libs)
146 # Helper objects for some tests.
147 extra-test-objs += \
148 tst-interpose-aux-nothread.o \
149 tst-interpose-aux-thread.o \
151 test-extras = \
152 tst-interpose-aux-nothread \
153 tst-interpose-aux-thread \
155 libmemusage-routines = memusage
156 libmemusage-inhibit-o = $(filter-out .os,$(object-suffixes))
158 libc_malloc_debug-routines = malloc-debug $(sysdep_malloc_debug_routines)
159 libc_malloc_debug-inhibit-o = $(filter-out .os,$(object-suffixes))
161 $(objpfx)tst-malloc-backtrace: $(shared-thread-library)
162 $(objpfx)tst-malloc-thread-exit: $(shared-thread-library)
163 $(objpfx)tst-malloc-thread-fail: $(shared-thread-library)
164 $(objpfx)tst-mallocfork3: $(shared-thread-library)
165 $(objpfx)tst-mallocfork3-mcheck: $(shared-thread-library)
166 $(objpfx)tst-malloc-fork-deadlock: $(shared-thread-library)
167 $(objpfx)tst-malloc-stats-cancellation: $(shared-thread-library)
168 $(objpfx)tst-malloc-backtrace-mcheck: $(shared-thread-library)
169 $(objpfx)tst-malloc-thread-exit-mcheck: $(shared-thread-library)
170 $(objpfx)tst-malloc-thread-fail-mcheck: $(shared-thread-library)
171 $(objpfx)tst-malloc-fork-deadlock-mcheck: $(shared-thread-library)
172 $(objpfx)tst-malloc-stats-cancellation-mcheck: $(shared-thread-library)
173 $(objpfx)tst-mallocfork3-malloc-check: $(shared-thread-library)
174 $(objpfx)tst-malloc-backtrace-malloc-check: $(shared-thread-library)
175 $(objpfx)tst-malloc-thread-exit-malloc-check: $(shared-thread-library)
176 $(objpfx)tst-malloc-thread-fail-malloc-check: $(shared-thread-library)
177 $(objpfx)tst-malloc-fork-deadlock-malloc-check: $(shared-thread-library)
178 $(objpfx)tst-malloc-stats-cancellation-malloc-check: $(shared-thread-library)
179 $(objpfx)tst-malloc-thread-exit-malloc-hugetlb1: $(shared-thread-library)
180 $(objpfx)tst-malloc-thread-fail-malloc-hugetlb1: $(shared-thread-library)
181 $(objpfx)tst-mallocfork2-malloc-hugetlb1: $(shared-thread-library)
182 $(objpfx)tst-mallocfork3-malloc-hugetlb1: $(shared-thread-library)
183 $(objpfx)tst-malloc-fork-deadlock-malloc-hugetlb1: $(shared-thread-library)
184 $(objpfx)tst-malloc-stats-cancellation-malloc-hugetlb1: $(shared-thread-library)
185 $(objpfx)tst-malloc-thread-exit-malloc-hugetlb2: $(shared-thread-library)
186 $(objpfx)tst-malloc-thread-fail-malloc-hugetlb2: $(shared-thread-library)
187 $(objpfx)tst-mallocfork2-malloc-hugetlb2: $(shared-thread-library)
188 $(objpfx)tst-mallocfork3-malloc-hugetlb2: $(shared-thread-library)
189 $(objpfx)tst-malloc-fork-deadlock-malloc-hugetlb2: $(shared-thread-library)
190 $(objpfx)tst-malloc-stats-cancellation-malloc-hugetlb2: $(shared-thread-library)
192 # These should be removed by `make clean'.
193 extra-objs = mcheck-init.o libmcheck.a
194 others-extras = mcheck-init.o
196 # Include the cleanup handler.
197 aux := set-freeres thread-freeres
199 # The Perl script to analyze the output of the mtrace functions.
200 ifneq ($(PERL),no)
201 install-bin-script = mtrace
202 generated += mtrace
204 # The Perl script will print addresses and to do this nicely we must know
205 # whether we are on a 32 or 64 bit machine.
206 ifneq ($(findstring wordsize-32,$(config-sysdirs)),)
207 address-width=10
208 else
209 address-width=18
210 endif
211 endif
213 # Unless we get a test for the availability of libgd which also works
214 # for cross-compiling we disable the memusagestat generation in this
215 # situation.
216 ifneq ($(cross-compiling),yes)
217 # If the gd library is available we build the `memusagestat' program.
218 ifneq ($(LIBGD),no)
219 others: $(objpfx)memusage
220 others += memusagestat
221 install-bin = memusagestat
222 install-bin-script += memusage
223 generated += memusagestat memusage
224 extra-objs += memusagestat.o
226 # The configure.ac check for libgd and its headers did not use $SYSINCLUDES.
227 # The directory specified by --with-headers usually contains only the basic
228 # kernel interface headers, not something like libgd. So the simplest thing
229 # is to presume that the standard system headers will be ok for this file.
230 $(objpfx)memusagestat.o: sysincludes = # nothing
231 endif
232 endif
234 # Another goal which can be used to override the configure decision.
235 .PHONY: do-memusagestat
236 do-memusagestat: $(objpfx)memusagestat
238 memusagestat-modules = memusagestat
240 cpp-srcs-left := $(memusagestat-modules)
241 lib := memusagestat
242 include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left))
244 LDLIBS-memusagestat = $(libgd-LDFLAGS) -lgd -lpng -lz -lm
246 ifeq ($(run-built-tests),yes)
247 ifeq (yes,$(build-shared))
248 ifneq ($(PERL),no)
249 tests-special += $(objpfx)tst-mtrace.out
250 tests-special += $(objpfx)tst-dynarray-mem.out
251 tests-special += $(objpfx)tst-dynarray-fail-mem.out
252 endif
253 endif
254 endif
256 include ../Rules
258 CFLAGS-mcheck-init.c += $(PIC-ccflag)
259 CFLAGS-obstack.c += $(uses-callbacks)
261 $(objpfx)libmcheck.a: $(objpfx)mcheck-init.o
262 -rm -f $@
263 $(patsubst %/,cd % &&,$(objpfx)) \
264 $(LN_S) $(<F) $(@F)
266 lib: $(objpfx)libmcheck.a
268 ifeq ($(run-built-tests),yes)
269 ifeq (yes,$(build-shared))
270 ifneq ($(PERL),no)
271 $(objpfx)tst-mtrace.out: tst-mtrace.sh $(objpfx)tst-mtrace
272 $(SHELL) $< $(common-objpfx) '$(test-program-prefix-before-env)' \
273 '$(run-program-env)' '$(test-program-prefix-after-env)'; \
274 $(evaluate-test)
275 endif
276 endif
277 endif
279 tst-malloc-check-ENV = MALLOC_CHECK_=3 \
280 LD_PRELOAD=$(objpfx)/libc_malloc_debug.so
281 tst-malloc-usable-ENV = MALLOC_CHECK_=3 \
282 LD_PRELOAD=$(objpfx)/libc_malloc_debug.so
283 tst-malloc-usable-tunables-ENV = GLIBC_TUNABLES=glibc.malloc.check=3 \
284 LD_PRELOAD=$(objpfx)/libc_malloc_debug.so
286 tst-mxfast-ENV = GLIBC_TUNABLES=glibc.malloc.tcache_count=0:glibc.malloc.mxfast=0
288 CPPFLAGS-malloc-debug.c += -DUSE_TCACHE=0
289 ifeq ($(experimental-malloc),yes)
290 CPPFLAGS-malloc.c += -DUSE_TCACHE=1
291 else
292 CPPFLAGS-malloc.c += -DUSE_TCACHE=0
293 endif
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)
358 tst-compathooks-on-ENV = LD_PRELOAD=$(objpfx)libc_malloc_debug.so
359 tst-compathooks-on-mcheck-ENV = LD_PRELOAD=$(objpfx)libc_malloc_debug.so
360 tst-compathooks-on-malloc-check-ENV = \
361 LD_PRELOAD=$(objpfx)libc_malloc_debug.so
362 tst-mallocstate-ENV = LD_PRELOAD=$(objpfx)libc_malloc_debug.so
363 tst-mallocstate-malloc-check-ENV = LD_PRELOAD=$(objpfx)libc_malloc_debug.so
365 # The test needs malloc_get_state/malloc_set_state which is in
366 # libc_malloc_debug.so.
367 $(objpfx)tst-mallocstate: $(objpfx)libc_malloc_debug.so
368 $(objpfx)tst-mallocstate-malloc-check: $(objpfx)libc_malloc_debug.so