Add another test for fclose on an unopened file
[glibc.git] / malloc / Makefile
blob1630aaf6ac6d9c676ac8b134de3394284cd9ed8f
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
21 subdir := malloc
23 include ../Makeconfig
25 dist-headers := malloc.h
26 headers := $(dist-headers) obstack.h mcheck.h
27 tests := \
28 mallocbug \
29 tst-aligned-alloc \
30 tst-aligned-alloc-random \
31 tst-aligned-alloc-random-thread \
32 tst-aligned-alloc-random-thread-cross \
33 tst-alloc_buffer \
34 tst-calloc \
35 tst-free-errno \
36 tst-interpose-nothread \
37 tst-interpose-thread \
38 tst-malloc \
39 tst-malloc-alternate-path \
40 tst-malloc-backtrace \
41 tst-malloc-check \
42 tst-malloc-fork-deadlock \
43 tst-malloc-random \
44 tst-malloc-stats-cancellation \
45 tst-malloc-tcache-leak \
46 tst-malloc-thread-exit \
47 tst-malloc-thread-fail \
48 tst-malloc-too-large \
49 tst-malloc-usable \
50 tst-malloc_info tst-mallinfo2 \
51 tst-mallocalign1 \
52 tst-mallocfork \
53 tst-mallocfork2 \
54 tst-mallocfork3 \
55 tst-mallopt \
56 tst-memalign \
57 tst-memalign-2 \
58 tst-memalign-3 \
59 tst-obstack \
60 tst-posix_memalign \
61 tst-pvalloc \
62 tst-pvalloc-fortify \
63 tst-realloc \
64 tst-reallocarray \
65 tst-safe-linking \
66 tst-tcfree1 tst-tcfree2 tst-tcfree3 \
67 tst-trim1 \
68 tst-valloc \
69 # tests
71 tests-static := \
72 tst-aligned-alloc-static \
73 tst-interpose-static-nothread \
74 tst-interpose-static-thread \
75 # tests-static
77 # Test for the malloc_set_state symbol removed in glibc 2.25.
78 ifeq ($(have-GLIBC_2.23)$(build-shared),yesyes)
79 tests += \
80 tst-compathooks-off \
81 tst-compathooks-on \
82 tst-mallocstate \
83 # tests
84 endif
86 tests-internal := tst-scratch_buffer
88 # The dynarray framework is only available inside glibc.
89 tests-internal += \
90 tst-dynarray \
91 tst-dynarray-at-fail \
92 tst-dynarray-fail \
93 # tests-internal
95 tests += \
96 tst-malloc-usable-tunables \
97 tst-mxfast \
98 # tests
100 tests += $(tests-static)
101 test-srcs = tst-mtrace
103 # These tests either are run with MALLOC_CHECK_=3 by default or do not work
104 # with MALLOC_CHECK_=3 because they expect a specific failure.
105 tests-exclude-malloc-check = \
106 tst-compathooks-off \
107 tst-compathooks-on \
108 tst-malloc-check \
109 tst-malloc-tcache-leak \
110 tst-malloc-usable \
111 tst-mallocfork2 \
112 tst-mallocfork3 \
113 tst-memalign-2 \
114 tst-memalign-3 \
115 tst-mxfast \
116 tst-safe-linking \
117 # tests-exclude-malloc-check
119 # Run all tests with MALLOC_CHECK_=3
120 tests-malloc-check = $(filter-out $(tests-exclude-malloc-check) \
121 $(tests-static),$(tests))
123 # Run all tests with GLIBC_TUNABLES=glibc.malloc.hugetlb={1,2} which check
124 # the Transparent Huge Pages support (1) or automatic huge page support (2).
125 # We need exclude some tests that define the ENV vars.
126 tests-exclude-hugetlb1 = \
127 tst-compathooks-off \
128 tst-compathooks-on \
129 tst-interpose-nothread \
130 tst-interpose-static-nothread \
131 tst-interpose-static-thread \
132 tst-interpose-thread \
133 tst-malloc-tcache-leak \
134 tst-malloc-usable \
135 tst-malloc-usable-tunables \
136 tst-mallocfork2 \
137 tst-mallocfork3 \
138 tst-mallocstate \
139 # tests-exclude-hugetlb1
140 # The tst-free-errno relies on the used malloc page size to mmap an
141 # overlapping region.
142 tests-exclude-hugetlb2 = \
143 $(tests-exclude-hugetlb1) \
144 tst-free-errno
145 tests-malloc-hugetlb1 = \
146 $(filter-out $(tests-exclude-hugetlb1), $(tests))
147 tests-malloc-hugetlb2 = \
148 $(filter-out $(tests-exclude-hugetlb2), $(tests))
150 # -lmcheck needs __malloc_initialize_hook, which was deprecated in 2.24.
151 ifeq ($(have-GLIBC_2.23)$(build-shared),yesyes)
152 # Tests that don't play well with mcheck. They are either bugs in mcheck or
153 # the tests expect specific internal behavior that is changed due to linking to
154 # libmcheck.a.
155 tests-exclude-mcheck = \
156 tst-aligned-alloc-random-thread \
157 tst-aligned-alloc-random-thread-cross \
158 tst-compathooks-off \
159 tst-compathooks-on \
160 tst-malloc-backtrace \
161 tst-malloc-fork-deadlock \
162 tst-malloc-stats-cancellation \
163 tst-malloc-tcache-leak \
164 tst-malloc-thread-exit \
165 tst-malloc-thread-fail \
166 tst-malloc-usable-tunables \
167 tst-malloc_info \
168 tst-mallocfork2 \
169 tst-mallocfork3 \
170 tst-mallocstate \
171 tst-memalign-2 \
172 tst-memalign-3 \
173 tst-mxfast \
174 tst-safe-linking \
175 # tests-exclude-mcheck
177 tests-mcheck = $(filter-out $(tests-exclude-mcheck) $(tests-static), $(tests))
178 endif
180 routines = malloc mcheck mtrace obstack reallocarray \
181 scratch_buffer_grow scratch_buffer_grow_preserve \
182 scratch_buffer_set_array_size \
183 dynarray_at_failure \
184 dynarray_emplace_enlarge \
185 dynarray_finalize \
186 dynarray_resize \
187 dynarray_resize_clear \
188 alloc_buffer_alloc_array \
189 alloc_buffer_allocate \
190 alloc_buffer_copy_bytes \
191 alloc_buffer_copy_string \
192 alloc_buffer_create_failure \
194 install-lib := libmcheck.a
195 non-lib.a := libmcheck.a
197 # Additional libraries.
198 extra-libs = libmemusage libc_malloc_debug
199 extra-libs-others = $(extra-libs)
201 # Helper objects for some tests.
202 extra-test-objs += \
203 tst-aligned_alloc-lib.so \
204 tst-interpose-aux-nothread.o \
205 tst-interpose-aux-thread.o \
206 # extra-test-objs
208 test-extras = \
209 tst-interpose-aux-nothread \
210 tst-interpose-aux-thread \
211 # test-extras
213 modules-names = \
214 tst-aligned_alloc-lib
216 libmemusage-routines = memusage
217 libmemusage-inhibit-o = $(filter-out .os,$(object-suffixes))
219 libc_malloc_debug-routines = malloc-debug $(sysdep_malloc_debug_routines)
220 libc_malloc_debug-inhibit-o = $(filter-out .os,$(object-suffixes))
222 $(objpfx)tst-malloc-backtrace: $(shared-thread-library)
223 $(objpfx)tst-malloc-thread-exit: $(shared-thread-library)
224 $(objpfx)tst-malloc-thread-fail: $(shared-thread-library)
225 $(objpfx)tst-mallocfork3: $(shared-thread-library)
226 $(objpfx)tst-mallocfork3-mcheck: $(shared-thread-library)
227 $(objpfx)tst-malloc-fork-deadlock: $(shared-thread-library)
228 $(objpfx)tst-malloc-stats-cancellation: $(shared-thread-library)
229 $(objpfx)tst-malloc-backtrace-mcheck: $(shared-thread-library)
230 $(objpfx)tst-malloc-thread-exit-mcheck: $(shared-thread-library)
231 $(objpfx)tst-malloc-thread-fail-mcheck: $(shared-thread-library)
232 $(objpfx)tst-malloc-fork-deadlock-mcheck: $(shared-thread-library)
233 $(objpfx)tst-malloc-stats-cancellation-mcheck: $(shared-thread-library)
234 $(objpfx)tst-mallocfork3-malloc-check: $(shared-thread-library)
235 $(objpfx)tst-malloc-backtrace-malloc-check: $(shared-thread-library)
236 $(objpfx)tst-malloc-thread-exit-malloc-check: $(shared-thread-library)
237 $(objpfx)tst-malloc-thread-fail-malloc-check: $(shared-thread-library)
238 $(objpfx)tst-malloc-fork-deadlock-malloc-check: $(shared-thread-library)
239 $(objpfx)tst-malloc-stats-cancellation-malloc-check: $(shared-thread-library)
240 $(objpfx)tst-malloc-thread-exit-malloc-hugetlb1: $(shared-thread-library)
241 $(objpfx)tst-malloc-thread-fail-malloc-hugetlb1: $(shared-thread-library)
242 $(objpfx)tst-malloc-fork-deadlock-malloc-hugetlb1: $(shared-thread-library)
243 $(objpfx)tst-malloc-stats-cancellation-malloc-hugetlb1: $(shared-thread-library)
244 $(objpfx)tst-malloc-thread-exit-malloc-hugetlb2: $(shared-thread-library)
245 $(objpfx)tst-malloc-thread-fail-malloc-hugetlb2: $(shared-thread-library)
246 $(objpfx)tst-malloc-fork-deadlock-malloc-hugetlb2: $(shared-thread-library)
247 $(objpfx)tst-malloc-stats-cancellation-malloc-hugetlb2: $(shared-thread-library)
249 # These should be removed by `make clean'.
250 extra-objs = mcheck-init.o libmcheck.a
251 others-extras = mcheck-init.o
253 # Include the cleanup handler.
254 aux := set-freeres thread-freeres
256 # The Perl script to analyze the output of the mtrace functions.
257 install-bin-script = mtrace
258 generated += mtrace
260 # The Perl script will print addresses and to do this nicely we must know
261 # whether we are on a 32 or 64 bit machine.
262 ifneq ($(findstring wordsize-32,$(config-sysdirs)),)
263 address-width=10
264 else
265 address-width=18
266 endif
268 # Unless we get a test for the availability of libgd which also works
269 # for cross-compiling we disable the memusagestat generation in this
270 # situation.
271 ifneq ($(cross-compiling),yes)
272 # If the gd library is available we build the `memusagestat' program.
273 ifneq ($(LIBGD),no)
274 others: $(objpfx)memusage
275 others += memusagestat
276 install-bin = memusagestat
277 install-bin-script += memusage
278 generated += memusagestat memusage
279 extra-objs += memusagestat.o
281 # The configure.ac check for libgd and its headers did not use $SYSINCLUDES.
282 # The directory specified by --with-headers usually contains only the basic
283 # kernel interface headers, not something like libgd. So the simplest thing
284 # is to presume that the standard system headers will be ok for this file.
285 $(objpfx)memusagestat.o: sysincludes = # nothing
286 endif
287 endif
289 # Another goal which can be used to override the configure decision.
290 .PHONY: do-memusagestat
291 do-memusagestat: $(objpfx)memusagestat
293 memusagestat-modules = memusagestat
295 cpp-srcs-left := $(memusagestat-modules)
296 lib := memusagestat
297 include $(patsubst %,$(..)libof-iterator.mk,$(cpp-srcs-left))
299 LDLIBS-memusagestat = $(libgd-LDFLAGS) -lgd -lpng -lz -lm
301 ifeq ($(run-built-tests),yes)
302 ifeq (yes,$(build-shared))
303 ifneq ($(PERL),no)
304 tests-special += $(objpfx)tst-mtrace.out
305 tests-special += $(objpfx)tst-dynarray-mem.out
306 tests-special += $(objpfx)tst-dynarray-fail-mem.out
307 endif
308 endif
309 endif
311 include ../Rules
313 CFLAGS-mcheck-init.c += $(PIC-ccflag)
314 CFLAGS-obstack.c += $(uses-callbacks)
316 $(objpfx)libmcheck.a: $(objpfx)mcheck-init.o
317 -rm -f $@
318 $(patsubst %/,cd % &&,$(objpfx)) \
319 $(LN_S) $(<F) $(@F)
321 lib: $(objpfx)libmcheck.a
323 ifeq ($(run-built-tests),yes)
324 ifeq (yes,$(build-shared))
325 ifneq ($(PERL),no)
326 $(objpfx)tst-mtrace.out: tst-mtrace.sh $(objpfx)tst-mtrace
327 $(SHELL) $< $(common-objpfx) '$(test-program-prefix-before-env)' \
328 '$(run-program-env)' '$(test-program-prefix-after-env)'; \
329 $(evaluate-test)
330 endif
331 endif
332 endif
334 tst-malloc-check-ENV = MALLOC_CHECK_=3 \
335 LD_PRELOAD=$(objpfx)/libc_malloc_debug.so
336 tst-malloc-usable-ENV = MALLOC_CHECK_=3 \
337 LD_PRELOAD=$(objpfx)/libc_malloc_debug.so
338 tst-malloc-usable-tunables-ENV = GLIBC_TUNABLES=glibc.malloc.check=3 \
339 LD_PRELOAD=$(objpfx)/libc_malloc_debug.so
341 tst-mxfast-ENV = GLIBC_TUNABLES=glibc.malloc.tcache_count=0:glibc.malloc.mxfast=0
343 CPPFLAGS-malloc-debug.c += -DUSE_TCACHE=0
344 CPPFLAGS-malloc.c += -DUSE_TCACHE=1
345 # Uncomment this for test releases. For public releases it is too expensive.
346 #CPPFLAGS-malloc.o += -DMALLOC_DEBUG=1
348 CFLAGS-tst-tcfree3.c += -fno-builtin-malloc -fno-builtin-free
350 sLIBdir := $(shell echo $(slibdir) | sed 's,lib\(\|64\)$$,\\\\$$LIB,')
352 $(objpfx)mtrace: mtrace.pl
353 rm -f $@.new
354 sed -e 's|@XXX@|$(address-width)|' \
355 -e 's|@VERSION@|$(version)|' \
356 -e 's|@PKGVERSION@|$(PKGVERSION)|' \
357 -e 's|@REPORT_BUGS_TO@|$(REPORT_BUGS_TO)|' $^ > $@.new \
358 && rm -f $@ && mv $@.new $@ && chmod +x $@
360 $(objpfx)memusage: memusage.sh
361 rm -f $@.new
362 sed -e 's|@VERSION@|$(version)|' -e 's|@SLIBDIR@|$(sLIBdir)|' \
363 -e 's|@BINDIR@|$(bindir)|' -e 's|@PKGVERSION@|$(PKGVERSION)|' \
364 -e 's|@REPORT_BUGS_TO@|$(REPORT_BUGS_TO)|' $^ > $@.new \
365 && rm -f $@ && mv $@.new $@ && chmod +x $@
367 # Compile the tests with a flag which suppresses the mallopt call in
368 # the test skeleton.
369 $(tests:%=$(objpfx)%.o): CPPFLAGS += -DTEST_NO_MALLOPT
371 $(objpfx)tst-interpose-nothread: $(objpfx)tst-interpose-aux-nothread.o
372 $(objpfx)tst-interpose-nothread-mcheck: $(objpfx)tst-interpose-aux-nothread.o
373 $(objpfx)tst-interpose-nothread-malloc-check: \
374 $(objpfx)tst-interpose-aux-nothread.o
375 $(objpfx)tst-interpose-thread: \
376 $(objpfx)tst-interpose-aux-thread.o $(shared-thread-library)
377 $(objpfx)tst-interpose-thread-mcheck: \
378 $(objpfx)tst-interpose-aux-thread.o $(shared-thread-library)
379 $(objpfx)tst-interpose-thread-malloc-check: \
380 $(objpfx)tst-interpose-aux-thread.o $(shared-thread-library)
381 $(objpfx)tst-interpose-static-nothread: $(objpfx)tst-interpose-aux-nothread.o
382 $(objpfx)tst-interpose-static-thread: \
383 $(objpfx)tst-interpose-aux-thread.o $(static-thread-library)
385 tst-dynarray-ENV = MALLOC_TRACE=$(objpfx)tst-dynarray.mtrace \
386 LD_PRELOAD=$(objpfx)libc_malloc_debug.so
387 $(objpfx)tst-dynarray-mem.out: $(objpfx)tst-dynarray.out
388 $(common-objpfx)malloc/mtrace $(objpfx)tst-dynarray.mtrace > $@; \
389 $(evaluate-test)
391 tst-dynarray-fail-ENV = MALLOC_TRACE=$(objpfx)tst-dynarray-fail.mtrace \
392 LD_PRELOAD=$(objpfx)libc_malloc_debug.so
393 $(objpfx)tst-dynarray-fail-mem.out: $(objpfx)tst-dynarray-fail.out
394 $(common-objpfx)malloc/mtrace $(objpfx)tst-dynarray-fail.mtrace > $@; \
395 $(evaluate-test)
397 $(objpfx)tst-malloc-tcache-leak: $(shared-thread-library)
398 $(objpfx)tst-malloc_info: $(shared-thread-library)
399 $(objpfx)tst-mallocfork2: $(shared-thread-library)
400 $(objpfx)tst-malloc_info-mcheck: $(shared-thread-library)
401 $(objpfx)tst-malloc_info-malloc-check: $(shared-thread-library)
402 $(objpfx)tst-mallocfork2-malloc-check: $(shared-thread-library)
403 $(objpfx)tst-malloc_info-malloc-hugetlb1: $(shared-thread-library)
404 $(objpfx)tst-malloc_info-malloc-hugetlb2: $(shared-thread-library)
405 $(objpfx)tst-memalign-3: $(shared-thread-library)
406 $(objpfx)tst-memalign-3-malloc-hugetlb1: $(shared-thread-library)
407 $(objpfx)tst-memalign-3-malloc-hugetlb2: $(shared-thread-library)
409 tst-compathooks-on-ENV = LD_PRELOAD=$(objpfx)libc_malloc_debug.so
410 tst-compathooks-on-mcheck-ENV = LD_PRELOAD=$(objpfx)libc_malloc_debug.so
411 tst-compathooks-on-malloc-check-ENV = \
412 LD_PRELOAD=$(objpfx)libc_malloc_debug.so
413 tst-mallocstate-ENV = LD_PRELOAD=$(objpfx)libc_malloc_debug.so
414 tst-mallocstate-malloc-check-ENV = LD_PRELOAD=$(objpfx)libc_malloc_debug.so
416 # The test needs malloc_get_state/malloc_set_state which is in
417 # libc_malloc_debug.so.
418 $(objpfx)tst-mallocstate: $(objpfx)libc_malloc_debug.so
419 $(objpfx)tst-mallocstate-malloc-check: $(objpfx)libc_malloc_debug.so
421 $(objpfx)tst-aligned-alloc-random.out: $(objpfx)tst-aligned_alloc-lib.so
422 $(objpfx)tst-aligned-alloc-random-thread: $(shared-thread-library)
423 $(objpfx)tst-aligned-alloc-random-thread-malloc-check: $(shared-thread-library)
424 $(objpfx)tst-aligned-alloc-random-thread-malloc-hugetlb1: $(shared-thread-library)
425 $(objpfx)tst-aligned-alloc-random-thread-malloc-hugetlb2: $(shared-thread-library)
426 $(objpfx)tst-aligned-alloc-random-thread.out: $(objpfx)tst-aligned_alloc-lib.so
427 $(objpfx)tst-aligned-alloc-random-thread-cross: $(shared-thread-library)
428 $(objpfx)tst-aligned-alloc-random-thread-cross-malloc-check: $(shared-thread-library)
429 $(objpfx)tst-aligned-alloc-random-thread-cross-malloc-hugetlb1: $(shared-thread-library)
430 $(objpfx)tst-aligned-alloc-random-thread-cross-malloc-hugetlb2: $(shared-thread-library)
431 $(objpfx)tst-aligned-alloc-random-thread-cross.out: $(objpfx)tst-aligned_alloc-lib.so
432 $(objpfx)tst-malloc-random.out: $(objpfx)tst-aligned_alloc-lib.so
434 tst-aligned-alloc-random-ENV = LD_PRELOAD=$(objpfx)tst-aligned_alloc-lib.so
435 tst-aligned-alloc-random-thread-ENV = LD_PRELOAD=$(objpfx)tst-aligned_alloc-lib.so
436 tst-aligned-alloc-random-thread-cross-ENV = LD_PRELOAD=$(objpfx)tst-aligned_alloc-lib.so
437 tst-malloc-random-ENV = LD_PRELOAD=$(objpfx)tst-aligned_alloc-lib.so