stdlib: Optimization qsort{_r} swap implementation
[glibc.git] / malloc / Makefile
blobf6cdf7bf0be2ee2c02788bc40169fdeb9f81a98d
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 \
78 tst-mallocfork2 \
79 tst-mallocfork3 \
80 tst-malloc-tcache-leak
82 # Run all tests with MALLOC_CHECK_=3
83 tests-malloc-check = $(filter-out $(tests-exclude-malloc-check) \
84 $(tests-static),$(tests))
86 # Run all tests with GLIBC_TUNABLES=glibc.malloc.hugetlb={1,2} which check
87 # the Transparent Huge Pages support (1) or automatic huge page support (2).
88 # We need exclude some tests that define the ENV vars.
89 tests-exclude-hugetlb1 = \
90 tst-compathooks-off \
91 tst-compathooks-on \
92 tst-interpose-nothread \
93 tst-interpose-thread \
94 tst-interpose-static-nothread \
95 tst-interpose-static-thread \
96 tst-malloc-usable \
97 tst-malloc-usable-tunables \
98 tst-mallocstate \
99 tst-malloc-tcache-leak \
100 tst-mallocfork2 \
101 tst-mallocfork3
102 # The tst-free-errno relies on the used malloc page size to mmap an
103 # overlapping region.
104 tests-exclude-hugetlb2 = \
105 $(tests-exclude-hugetlb1) \
106 tst-free-errno
107 tests-malloc-hugetlb1 = \
108 $(filter-out $(tests-exclude-hugetlb1), $(tests))
109 tests-malloc-hugetlb2 = \
110 $(filter-out $(tests-exclude-hugetlb2), $(tests))
112 # -lmcheck needs __malloc_initialize_hook, which was deprecated in 2.24.
113 ifeq ($(have-GLIBC_2.23)$(build-shared),yesyes)
114 # Tests that don't play well with mcheck. They are either bugs in mcheck or
115 # the tests expect specific internal behavior that is changed due to linking to
116 # libmcheck.a.
117 tests-exclude-mcheck = tst-mallocstate \
118 tst-safe-linking \
119 tst-malloc-backtrace \
120 tst-malloc-fork-deadlock \
121 tst-malloc-stats-cancellation \
122 tst-malloc-tcache-leak \
123 tst-malloc-thread-exit \
124 tst-malloc-thread-fail \
125 tst-malloc-usable-tunables \
126 tst-malloc_info \
127 tst-compathooks-off tst-compathooks-on \
128 tst-memalign-2 \
129 tst-memalign-3 \
130 tst-mxfast \
131 tst-mallocfork2 \
132 tst-mallocfork3
134 tests-mcheck = $(filter-out $(tests-exclude-mcheck) $(tests-static), $(tests))
135 endif
137 routines = malloc mcheck mtrace obstack reallocarray \
138 scratch_buffer_grow scratch_buffer_grow_preserve \
139 scratch_buffer_set_array_size \
140 dynarray_at_failure \
141 dynarray_emplace_enlarge \
142 dynarray_finalize \
143 dynarray_resize \
144 dynarray_resize_clear \
145 alloc_buffer_alloc_array \
146 alloc_buffer_allocate \
147 alloc_buffer_copy_bytes \
148 alloc_buffer_copy_string \
149 alloc_buffer_create_failure \
151 install-lib := libmcheck.a
152 non-lib.a := libmcheck.a
154 # Additional libraries.
155 extra-libs = libmemusage libc_malloc_debug
156 extra-libs-others = $(extra-libs)
158 # Helper objects for some tests.
159 extra-test-objs += \
160 tst-interpose-aux-nothread.o \
161 tst-interpose-aux-thread.o \
163 test-extras = \
164 tst-interpose-aux-nothread \
165 tst-interpose-aux-thread \
167 libmemusage-routines = memusage
168 libmemusage-inhibit-o = $(filter-out .os,$(object-suffixes))
170 libc_malloc_debug-routines = malloc-debug $(sysdep_malloc_debug_routines)
171 libc_malloc_debug-inhibit-o = $(filter-out .os,$(object-suffixes))
173 $(objpfx)tst-malloc-backtrace: $(shared-thread-library)
174 $(objpfx)tst-malloc-thread-exit: $(shared-thread-library)
175 $(objpfx)tst-malloc-thread-fail: $(shared-thread-library)
176 $(objpfx)tst-mallocfork3: $(shared-thread-library)
177 $(objpfx)tst-mallocfork3-mcheck: $(shared-thread-library)
178 $(objpfx)tst-malloc-fork-deadlock: $(shared-thread-library)
179 $(objpfx)tst-malloc-stats-cancellation: $(shared-thread-library)
180 $(objpfx)tst-malloc-backtrace-mcheck: $(shared-thread-library)
181 $(objpfx)tst-malloc-thread-exit-mcheck: $(shared-thread-library)
182 $(objpfx)tst-malloc-thread-fail-mcheck: $(shared-thread-library)
183 $(objpfx)tst-malloc-fork-deadlock-mcheck: $(shared-thread-library)
184 $(objpfx)tst-malloc-stats-cancellation-mcheck: $(shared-thread-library)
185 $(objpfx)tst-mallocfork3-malloc-check: $(shared-thread-library)
186 $(objpfx)tst-malloc-backtrace-malloc-check: $(shared-thread-library)
187 $(objpfx)tst-malloc-thread-exit-malloc-check: $(shared-thread-library)
188 $(objpfx)tst-malloc-thread-fail-malloc-check: $(shared-thread-library)
189 $(objpfx)tst-malloc-fork-deadlock-malloc-check: $(shared-thread-library)
190 $(objpfx)tst-malloc-stats-cancellation-malloc-check: $(shared-thread-library)
191 $(objpfx)tst-malloc-thread-exit-malloc-hugetlb1: $(shared-thread-library)
192 $(objpfx)tst-malloc-thread-fail-malloc-hugetlb1: $(shared-thread-library)
193 $(objpfx)tst-malloc-fork-deadlock-malloc-hugetlb1: $(shared-thread-library)
194 $(objpfx)tst-malloc-stats-cancellation-malloc-hugetlb1: $(shared-thread-library)
195 $(objpfx)tst-malloc-thread-exit-malloc-hugetlb2: $(shared-thread-library)
196 $(objpfx)tst-malloc-thread-fail-malloc-hugetlb2: $(shared-thread-library)
197 $(objpfx)tst-malloc-fork-deadlock-malloc-hugetlb2: $(shared-thread-library)
198 $(objpfx)tst-malloc-stats-cancellation-malloc-hugetlb2: $(shared-thread-library)
200 # These should be removed by `make clean'.
201 extra-objs = mcheck-init.o libmcheck.a
202 others-extras = mcheck-init.o
204 # Include the cleanup handler.
205 aux := set-freeres thread-freeres
207 # The Perl script to analyze the output of the mtrace functions.
208 ifneq ($(PERL),no)
209 install-bin-script = mtrace
210 generated += mtrace
212 # The Perl script will print addresses and to do this nicely we must know
213 # whether we are on a 32 or 64 bit machine.
214 ifneq ($(findstring wordsize-32,$(config-sysdirs)),)
215 address-width=10
216 else
217 address-width=18
218 endif
219 endif
221 # Unless we get a test for the availability of libgd which also works
222 # for cross-compiling we disable the memusagestat generation in this
223 # situation.
224 ifneq ($(cross-compiling),yes)
225 # If the gd library is available we build the `memusagestat' program.
226 ifneq ($(LIBGD),no)
227 others: $(objpfx)memusage
228 others += memusagestat
229 install-bin = memusagestat
230 install-bin-script += memusage
231 generated += memusagestat memusage
232 extra-objs += memusagestat.o
234 # The configure.ac check for libgd and its headers did not use $SYSINCLUDES.
235 # The directory specified by --with-headers usually contains only the basic
236 # kernel interface headers, not something like libgd. So the simplest thing
237 # is to presume that the standard system headers will be ok for this file.
238 $(objpfx)memusagestat.o: sysincludes = # nothing
239 endif
240 endif
242 # Another goal which can be used to override the configure decision.
243 .PHONY: do-memusagestat
244 do-memusagestat: $(objpfx)memusagestat
246 memusagestat-modules = memusagestat
248 cpp-srcs-left := $(memusagestat-modules)
249 lib := memusagestat
250 include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left))
252 LDLIBS-memusagestat = $(libgd-LDFLAGS) -lgd -lpng -lz -lm
254 ifeq ($(run-built-tests),yes)
255 ifeq (yes,$(build-shared))
256 ifneq ($(PERL),no)
257 tests-special += $(objpfx)tst-mtrace.out
258 tests-special += $(objpfx)tst-dynarray-mem.out
259 tests-special += $(objpfx)tst-dynarray-fail-mem.out
260 endif
261 endif
262 endif
264 include ../Rules
266 CFLAGS-mcheck-init.c += $(PIC-ccflag)
267 CFLAGS-obstack.c += $(uses-callbacks)
269 $(objpfx)libmcheck.a: $(objpfx)mcheck-init.o
270 -rm -f $@
271 $(patsubst %/,cd % &&,$(objpfx)) \
272 $(LN_S) $(<F) $(@F)
274 lib: $(objpfx)libmcheck.a
276 ifeq ($(run-built-tests),yes)
277 ifeq (yes,$(build-shared))
278 ifneq ($(PERL),no)
279 $(objpfx)tst-mtrace.out: tst-mtrace.sh $(objpfx)tst-mtrace
280 $(SHELL) $< $(common-objpfx) '$(test-program-prefix-before-env)' \
281 '$(run-program-env)' '$(test-program-prefix-after-env)'; \
282 $(evaluate-test)
283 endif
284 endif
285 endif
287 tst-malloc-check-ENV = MALLOC_CHECK_=3 \
288 LD_PRELOAD=$(objpfx)/libc_malloc_debug.so
289 tst-malloc-usable-ENV = MALLOC_CHECK_=3 \
290 LD_PRELOAD=$(objpfx)/libc_malloc_debug.so
291 tst-malloc-usable-tunables-ENV = GLIBC_TUNABLES=glibc.malloc.check=3 \
292 LD_PRELOAD=$(objpfx)/libc_malloc_debug.so
294 tst-mxfast-ENV = GLIBC_TUNABLES=glibc.malloc.tcache_count=0:glibc.malloc.mxfast=0
296 CPPFLAGS-malloc-debug.c += -DUSE_TCACHE=0
297 CPPFLAGS-malloc.c += -DUSE_TCACHE=1
298 # Uncomment this for test releases. For public releases it is too expensive.
299 #CPPFLAGS-malloc.o += -DMALLOC_DEBUG=1
301 CFLAGS-tst-tcfree3.c += -fno-builtin-malloc -fno-builtin-free
303 sLIBdir := $(shell echo $(slibdir) | sed 's,lib\(\|64\)$$,\\\\$$LIB,')
305 $(objpfx)mtrace: mtrace.pl
306 rm -f $@.new
307 sed -e 's|@PERL@|$(PERL)|' -e 's|@XXX@|$(address-width)|' \
308 -e 's|@VERSION@|$(version)|' \
309 -e 's|@PKGVERSION@|$(PKGVERSION)|' \
310 -e 's|@REPORT_BUGS_TO@|$(REPORT_BUGS_TO)|' $^ > $@.new \
311 && rm -f $@ && mv $@.new $@ && chmod +x $@
313 $(objpfx)memusage: memusage.sh
314 rm -f $@.new
315 sed -e 's|@VERSION@|$(version)|' -e 's|@SLIBDIR@|$(sLIBdir)|' \
316 -e 's|@BINDIR@|$(bindir)|' -e 's|@PKGVERSION@|$(PKGVERSION)|' \
317 -e 's|@REPORT_BUGS_TO@|$(REPORT_BUGS_TO)|' $^ > $@.new \
318 && rm -f $@ && mv $@.new $@ && chmod +x $@
320 # Compile the tests with a flag which suppresses the mallopt call in
321 # the test skeleton.
322 $(tests:%=$(objpfx)%.o): CPPFLAGS += -DTEST_NO_MALLOPT
324 $(objpfx)tst-interpose-nothread: $(objpfx)tst-interpose-aux-nothread.o
325 $(objpfx)tst-interpose-nothread-mcheck: $(objpfx)tst-interpose-aux-nothread.o
326 $(objpfx)tst-interpose-nothread-malloc-check: \
327 $(objpfx)tst-interpose-aux-nothread.o
328 $(objpfx)tst-interpose-thread: \
329 $(objpfx)tst-interpose-aux-thread.o $(shared-thread-library)
330 $(objpfx)tst-interpose-thread-mcheck: \
331 $(objpfx)tst-interpose-aux-thread.o $(shared-thread-library)
332 $(objpfx)tst-interpose-thread-malloc-check: \
333 $(objpfx)tst-interpose-aux-thread.o $(shared-thread-library)
334 $(objpfx)tst-interpose-static-nothread: $(objpfx)tst-interpose-aux-nothread.o
335 $(objpfx)tst-interpose-static-thread: \
336 $(objpfx)tst-interpose-aux-thread.o $(static-thread-library)
338 tst-dynarray-ENV = MALLOC_TRACE=$(objpfx)tst-dynarray.mtrace \
339 LD_PRELOAD=$(objpfx)libc_malloc_debug.so
340 $(objpfx)tst-dynarray-mem.out: $(objpfx)tst-dynarray.out
341 $(common-objpfx)malloc/mtrace $(objpfx)tst-dynarray.mtrace > $@; \
342 $(evaluate-test)
344 tst-dynarray-fail-ENV = MALLOC_TRACE=$(objpfx)tst-dynarray-fail.mtrace \
345 LD_PRELOAD=$(objpfx)libc_malloc_debug.so
346 $(objpfx)tst-dynarray-fail-mem.out: $(objpfx)tst-dynarray-fail.out
347 $(common-objpfx)malloc/mtrace $(objpfx)tst-dynarray-fail.mtrace > $@; \
348 $(evaluate-test)
350 $(objpfx)tst-malloc-tcache-leak: $(shared-thread-library)
351 $(objpfx)tst-malloc_info: $(shared-thread-library)
352 $(objpfx)tst-mallocfork2: $(shared-thread-library)
353 $(objpfx)tst-malloc_info-mcheck: $(shared-thread-library)
354 $(objpfx)tst-malloc_info-malloc-check: $(shared-thread-library)
355 $(objpfx)tst-mallocfork2-malloc-check: $(shared-thread-library)
356 $(objpfx)tst-malloc_info-malloc-hugetlb1: $(shared-thread-library)
357 $(objpfx)tst-malloc_info-malloc-hugetlb2: $(shared-thread-library)
358 $(objpfx)tst-memalign-3: $(shared-thread-library)
359 $(objpfx)tst-memalign-3-malloc-hugetlb1: $(shared-thread-library)
360 $(objpfx)tst-memalign-3-malloc-hugetlb2: $(shared-thread-library)
362 tst-compathooks-on-ENV = LD_PRELOAD=$(objpfx)libc_malloc_debug.so
363 tst-compathooks-on-mcheck-ENV = LD_PRELOAD=$(objpfx)libc_malloc_debug.so
364 tst-compathooks-on-malloc-check-ENV = \
365 LD_PRELOAD=$(objpfx)libc_malloc_debug.so
366 tst-mallocstate-ENV = LD_PRELOAD=$(objpfx)libc_malloc_debug.so
367 tst-mallocstate-malloc-check-ENV = LD_PRELOAD=$(objpfx)libc_malloc_debug.so
369 # The test needs malloc_get_state/malloc_set_state which is in
370 # libc_malloc_debug.so.
371 $(objpfx)tst-mallocstate: $(objpfx)libc_malloc_debug.so
372 $(objpfx)tst-mallocstate-malloc-check: $(objpfx)libc_malloc_debug.so