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