[COMMITTED 2.40 1/2] Hide all malloc functions from compiler [BZ #32366]
Sam James
sam@gentoo.org
Fri Jan 24 01:26:47 GMT 2025
Sam James <sam@gentoo.org> writes:
> From: "H.J. Lu" <hjl.tools@gmail.com>
I backported to 2.39 too.
>
> Since -1 isn't a power of two, compiler may reject it, hide memalign from
> Clang 19 which issues an error:
>
> tst-memalign.c:86:31: error: requested alignment is not a power of 2 [-Werror,-Wnon-power-of-two-alignment]
> 86 | p = memalign (-1, pagesize);
> | ^~
> tst-memalign.c:86:31: error: requested alignment must be 4294967296 bytes or smaller; maximum alignment assumed [-Werror,-Wbuiltin-assume-aligned-alignment]
> 86 | p = memalign (-1, pagesize);
> | ^~
>
> Update tst-malloc-aux.h to hide all malloc functions and include it in
> all malloc tests to prevent compiler from optimizing out any malloc
> functions.
>
> Tested with Clang 19.1.5 and GCC 15 20241206 for BZ #32366.
>
> Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
> Reviewed-by: Sam James <sam@gentoo.org>
> (cherry picked from commit f9493a15ea9cfb63a815c00c23142369ec09d8ce)
> ---
> malloc/tst-mallinfo2.c | 2 ++
> malloc/tst-malloc-aux.h | 25 ++++++++++++++++++++-----
> malloc/tst-malloc-backtrace.c | 2 ++
> malloc/tst-memalign.c | 2 ++
> malloc/tst-safe-linking.c | 2 ++
> malloc/tst-valloc.c | 2 ++
> 6 files changed, 30 insertions(+), 5 deletions(-)
>
> diff --git a/malloc/tst-mallinfo2.c b/malloc/tst-mallinfo2.c
> index 2c02f5f700..f072b9f24b 100644
> --- a/malloc/tst-mallinfo2.c
> +++ b/malloc/tst-mallinfo2.c
> @@ -23,6 +23,8 @@
> #include <stdlib.h>
> #include <support/check.h>
>
> +#include "tst-malloc-aux.h"
> +
> /* This is not specifically needed for the test, but (1) does
> something to the data so gcc doesn't optimize it away, and (2) may
> help when developing future tests. */
> diff --git a/malloc/tst-malloc-aux.h b/malloc/tst-malloc-aux.h
> index 54908b4a24..3e1b61ce34 100644
> --- a/malloc/tst-malloc-aux.h
> +++ b/malloc/tst-malloc-aux.h
> @@ -22,20 +22,35 @@
>
> #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;
> +#include <malloc.h>
> +
> +static __typeof (aligned_alloc) * volatile aligned_alloc_indirect
> + = aligned_alloc;
> +static __typeof (calloc) * volatile calloc_indirect = calloc;
> +static __typeof (malloc) * volatile malloc_indirect = malloc;
> +static __typeof (memalign) * volatile memalign_indirect = memalign;
> +static __typeof (posix_memalign) * volatile posix_memalign_indirect
> + = posix_memalign;
> +static __typeof (pvalloc) * volatile pvalloc_indirect = pvalloc;
> +static __typeof (realloc) * volatile realloc_indirect = realloc;
> +static __typeof (valloc) * volatile valloc_indirect = valloc;
>
> #undef aligned_alloc
> #undef calloc
> #undef malloc
> +#undef memalign
> +#undef posix_memalign
> +#undef pvalloc
> #undef realloc
> +#undef valloc
>
> #define aligned_alloc aligned_alloc_indirect
> #define calloc calloc_indirect
> #define malloc malloc_indirect
> +#define memalign memalign_indirect
> +#define posix_memalign posix_memalign_indirect
> +#define pvalloc pvalloc_indirect
> #define realloc realloc_indirect
> +#define valloc valloc_indirect
>
> #endif /* TST_MALLOC_AUX_H */
> diff --git a/malloc/tst-malloc-backtrace.c b/malloc/tst-malloc-backtrace.c
> index c7b1d65e5c..65fa91f6fd 100644
> --- a/malloc/tst-malloc-backtrace.c
> +++ b/malloc/tst-malloc-backtrace.c
> @@ -22,6 +22,8 @@
> #include <support/support.h>
> #include <libc-diag.h>
>
> +#include "tst-malloc-aux.h"
> +
> #define SIZE 4096
>
> /* Wrap free with a function to prevent gcc from optimizing it out. */
> diff --git a/malloc/tst-memalign.c b/malloc/tst-memalign.c
> index 563f6413d2..ac9770d3f9 100644
> --- a/malloc/tst-memalign.c
> +++ b/malloc/tst-memalign.c
> @@ -23,6 +23,8 @@
> #include <unistd.h>
> #include <libc-diag.h>
>
> +#include "tst-malloc-aux.h"
> +
> static int errors = 0;
>
> static void
> diff --git a/malloc/tst-safe-linking.c b/malloc/tst-safe-linking.c
> index 01dd07004d..63a7e2bc8e 100644
> --- a/malloc/tst-safe-linking.c
> +++ b/malloc/tst-safe-linking.c
> @@ -26,6 +26,8 @@
> #include <support/capture_subprocess.h>
> #include <support/check.h>
>
> +#include "tst-malloc-aux.h"
> +
> /* Run CALLBACK and check that the data on standard error equals
> EXPECTED. */
> static void
> diff --git a/malloc/tst-valloc.c b/malloc/tst-valloc.c
> index 9bab8c6470..0243d3dfd4 100644
> --- a/malloc/tst-valloc.c
> +++ b/malloc/tst-valloc.c
> @@ -23,6 +23,8 @@
> #include <unistd.h>
> #include <libc-diag.h>
>
> +#include "tst-malloc-aux.h"
> +
> static int errors = 0;
>
> static void
>
> base-commit: 7d4b6bcae91f29d7b4daf15bab06b66cf1d2217c
More information about the Libc-stable
mailing list