]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
malloc: add indirection for malloc(-like) functions in tests [BZ #32366]
authorSam James <sam@gentoo.org>
Mon, 9 Dec 2024 23:11:25 +0000 (23:11 +0000)
committerSam James <sam@gentoo.org>
Tue, 10 Dec 2024 01:50:56 +0000 (01:50 +0000)
GCC 15 introduces allocation dead code removal (DCE) for PR117370 in
r15-5255-g7828dc070510f8. This breaks various glibc tests which want
to assert various properties of the allocator without doing anything
obviously useful with the allocated memory.

Alexander Monakov rightly pointed out that we can and should do better
than passing -fno-malloc-dce to paper over the problem. Not least because
GCC 14 already does such DCE where there's no testing of malloc's return
value against NULL, and LLVM has such optimisations too.

Handle this by providing malloc (and friends) wrappers with a volatile
function pointer to obscure that we're calling malloc (et. al) from the
compiler.

Reviewed-by: Paul Eggert <eggert@cs.ucla.edu>
malloc/tst-aligned-alloc.c
malloc/tst-compathooks-off.c
malloc/tst-malloc-aux.h [new file with mode: 0644]
malloc/tst-malloc-check.c
malloc/tst-malloc-too-large.c
malloc/tst-malloc.c
malloc/tst-realloc.c
support/support.h
test-skeleton.c

index 91167d1392c0e626b6522e0438ddfa30b837edf4..b0f05a8fec78d5e839e844821bb7a62ee5a60900 100644 (file)
@@ -25,6 +25,8 @@
 #include <libc-diag.h>
 #include <support/check.h>
 
+#include "tst-malloc-aux.h"
+
 static int
 do_test (void)
 {
index d0106f3fb74ff3b182631dcfe584e508c8f1c216..4cce6e5a8076f6b67464747695ba1d9504234e88 100644 (file)
@@ -25,6 +25,8 @@
 #include <support/check.h>
 #include <support/support.h>
 
+#include "tst-malloc-aux.h"
+
 extern void (*volatile __free_hook) (void *, const void *);
 extern void *(*volatile __malloc_hook)(size_t, const void *);
 extern void *(*volatile __realloc_hook)(void *, size_t, const void *);
diff --git a/malloc/tst-malloc-aux.h b/malloc/tst-malloc-aux.h
new file mode 100644 (file)
index 0000000..54908b4
--- /dev/null
@@ -0,0 +1,41 @@
+/* Wrappers for malloc-like functions to allow testing the implementation
+   without optimization.
+   Copyright (C) 2024 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; see the file COPYING.LIB.  If
+   not, see <https://www.gnu.org/licenses/>.  */
+
+#ifndef TST_MALLOC_AUX_H
+#define TST_MALLOC_AUX_H
+
+#include <stddef.h>
+#include <stdlib.h>
+
+static void *(*volatile aligned_alloc_indirect)(size_t, size_t) = aligned_alloc;
+static void *(*volatile calloc_indirect)(size_t, size_t) = calloc;
+static void *(*volatile malloc_indirect)(size_t) = malloc;
+static void *(*volatile realloc_indirect)(void*, size_t) = realloc;
+
+#undef aligned_alloc
+#undef calloc
+#undef malloc
+#undef realloc
+
+#define aligned_alloc aligned_alloc_indirect
+#define calloc calloc_indirect
+#define malloc malloc_indirect
+#define realloc realloc_indirect
+
+#endif /* TST_MALLOC_AUX_H */
index fde8863ad7561a71dc04992e83e612e2efa89a1b..cc88bff3b39a421ccf2736d73516c904392a1b7a 100644 (file)
@@ -20,6 +20,8 @@
 #include <stdlib.h>
 #include <libc-diag.h>
 
+#include "tst-malloc-aux.h"
+
 static int errors = 0;
 
 static void
index 8e9e0d5fa2b4b90707065ab5a8d214b4ead43fc2..2b91377e54cdc4852f6b92b6afdfd011286d635c 100644 (file)
@@ -43,6 +43,7 @@
 #include <unistd.h>
 #include <sys/param.h>
 
+#include "tst-malloc-aux.h"
 
 /* This function prepares for each 'too-large memory allocation' test by
    performing a small successful malloc/free and resetting errno prior to
index f7a6e4654c374d01cc76fc08b58480a4d972f232..68af399022543111c9c3e26bc0bcbfd89d2ca5a6 100644 (file)
@@ -22,6 +22,8 @@
 #include <libc-diag.h>
 #include <time.h>
 
+#include "tst-malloc-aux.h"
+
 static int errors = 0;
 
 static void
index f50499ecb114d5747ac16d5451c6b31539d7797b..74a28fb45ed80bf51f2f58c1844db020e0e18d92 100644 (file)
@@ -23,6 +23,8 @@
 #include <libc-diag.h>
 #include <support/check.h>
 
+#include "tst-malloc-aux.h"
+
 static int
 do_test (void)
 {
index ba21ec9b5add7c02dafca2b522ec234256367a08..1a77f7979330d60c3c0229529a33993de20dae6a 100644 (file)
@@ -113,7 +113,7 @@ void *xposix_memalign (size_t alignment, size_t n)
   __attribute_malloc__ __attribute_alloc_align__ ((1))
   __attribute_alloc_size__ ((2)) __attr_dealloc_free __returns_nonnull;
 char *xasprintf (const char *format, ...)
-  __attribute__ ((format (printf, 1, 2), malloc)) __attr_dealloc_free
+  __attribute__ ((format (printf, 1, 2), __malloc__)) __attr_dealloc_free
   __returns_nonnull;
 char *xstrdup (const char *) __attr_dealloc_free __returns_nonnull;
 char *xstrndup (const char *, size_t) __attr_dealloc_free __returns_nonnull;
index ae185a4f2821de0005221d85090fcd72d9e55cc2..690f26e7cf229622ac5f31fd3010a959bcb3642d 100644 (file)
@@ -27,7 +27,6 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <getopt.h>
-#include <malloc.h>
 #include <paths.h>
 #include <search.h>
 #include <signal.h>