[PATCH v4 03/14] Do not export <alloc_buffer.h> functions from libc
Adhemerval Zanella Netto
adhemerval.zanella@linaro.org
Mon Mar 17 12:30:17 GMT 2025
On 02/02/25 18:13, Florian Weimer wrote:
> With the merge of NSS service modules into libc, external users are
> gone except in tests.
>
> To enable tests that use these functions (and the unit tests in
> malloc/tst-alloc_buffer.h), add a copy of these functions to
> libsupport. For that to work, do not call __snprintf in
> __libc_alloc_buffer_create_failure, which is not very useful
> and unavailable outside libc. All parameters to this function
> are now unused, so remove them.
>
> This also enables future use of this functionality from ld.so (using a
> separately built copy).
LGTM, thanks.
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
> ---
> include/alloc_buffer.h | 27 ++++++---------------------
> malloc/Makefile | 6 ++++--
> malloc/Versions | 7 -------
> malloc/alloc_buffer_alloc_array.c | 1 -
> malloc/alloc_buffer_allocate.c | 1 -
> malloc/alloc_buffer_copy_bytes.c | 1 -
> malloc/alloc_buffer_copy_string.c | 1 -
> malloc/alloc_buffer_create_failure.c | 9 ++-------
> malloc/tst-alloc_buffer.c | 4 ++++
> nss/Makefile | 4 ++--
> support/Makefile | 1 +
> support/support-alloc_buffer.c | 26 ++++++++++++++++++++++++++
> 12 files changed, 45 insertions(+), 43 deletions(-)
> create mode 100644 support/support-alloc_buffer.c
>
> diff --git a/include/alloc_buffer.h b/include/alloc_buffer.h
> index 54b94e66b8..6c6201d385 100644
> --- a/include/alloc_buffer.h
> +++ b/include/alloc_buffer.h
> @@ -113,10 +113,7 @@ enum
> };
>
> /* Internal function. Terminate the process using __libc_fatal. */
> -void __libc_alloc_buffer_create_failure (void *start, size_t size);
> -#ifndef _ISOMAC
> -libc_hidden_proto (__libc_alloc_buffer_create_failure)
> -#endif
> +void __libc_alloc_buffer_create_failure (void) attribute_hidden;
>
> /* Create a new allocation buffer. The byte range from START to START
> + SIZE - 1 must be valid, and the allocation buffer allocates
> @@ -128,16 +125,13 @@ alloc_buffer_create (void *start, size_t size)
> uintptr_t current = (uintptr_t) start;
> uintptr_t end = (uintptr_t) start + size;
> if (end < current)
> - __libc_alloc_buffer_create_failure (start, size);
> + __libc_alloc_buffer_create_failure ();
> return (struct alloc_buffer) { current, end };
> }
>
> /* Internal function. See alloc_buffer_allocate below. */
> struct alloc_buffer __libc_alloc_buffer_allocate (size_t size, void **pptr)
> - __attribute__ ((nonnull (2)));
> -#ifndef _ISOMAC
> -libc_hidden_proto (__libc_alloc_buffer_allocate)
> -#endif
> + attribute_hidden __attribute__ ((nonnull (2)));
>
> /* Allocate a buffer of SIZE bytes using malloc. The returned buffer
> is in a failed state if malloc fails. *PPTR points to the start of
> @@ -338,10 +332,7 @@ __alloc_buffer_next (struct alloc_buffer *buf, size_t align)
> void * __libc_alloc_buffer_alloc_array (struct alloc_buffer *buf,
> size_t size, size_t align,
> size_t count)
> - __attribute__ ((nonnull (1)));
> -#ifndef _ISOMAC
> -libc_hidden_proto (__libc_alloc_buffer_alloc_array)
> -#endif
> + attribute_hidden __attribute__ ((nonnull (1)));
>
> /* Obtain a TYPE * pointer to an array of COUNT objects in BUF of
> TYPE. Consume these bytes from the buffer. Return NULL and mark
> @@ -357,10 +348,7 @@ libc_hidden_proto (__libc_alloc_buffer_alloc_array)
> /* Internal function. See alloc_buffer_copy_bytes below. */
> struct alloc_buffer __libc_alloc_buffer_copy_bytes (struct alloc_buffer,
> const void *, size_t)
> - __attribute__ ((nonnull (2)));
> -#ifndef _ISOMAC
> -libc_hidden_proto (__libc_alloc_buffer_copy_bytes)
> -#endif
> + attribute_hidden __attribute__ ((nonnull (2)));
>
> /* Copy SIZE bytes starting at SRC into the buffer. If there is not
> enough room in the buffer, the buffer is marked as failed. No
> @@ -374,10 +362,7 @@ alloc_buffer_copy_bytes (struct alloc_buffer *buf, const void *src, size_t size)
> /* Internal function. See alloc_buffer_copy_string below. */
> struct alloc_buffer __libc_alloc_buffer_copy_string (struct alloc_buffer,
> const char *)
> - __attribute__ ((nonnull (2)));
> -#ifndef _ISOMAC
> -libc_hidden_proto (__libc_alloc_buffer_copy_string)
> -#endif
> + attribute_hidden __attribute__ ((nonnull (2)));
>
> /* Copy the string at SRC into the buffer, including its null
> terminator. If there is not enough room in the buffer, the buffer
> diff --git a/malloc/Makefile b/malloc/Makefile
> index e2b2c1ae1b..bd530d7f72 100644
> --- a/malloc/Makefile
> +++ b/malloc/Makefile
> @@ -30,7 +30,6 @@ tests := \
> tst-aligned-alloc-random \
> tst-aligned-alloc-random-thread \
> tst-aligned-alloc-random-thread-cross \
> - tst-alloc_buffer \
> tst-calloc \
> tst-free-errno \
> tst-interpose-nothread \
> @@ -83,7 +82,10 @@ tests += \
> # tests
> endif
>
> -tests-internal := tst-scratch_buffer
> +tests-internal := \
> + tst-alloc_buffer \
> + tst-scratch_buffer \
> + # tests-internal
>
> # The dynarray framework is only available inside glibc.
> tests-internal += \
> diff --git a/malloc/Versions b/malloc/Versions
> index c763395c6d..011b6a5a85 100644
> --- a/malloc/Versions
> +++ b/malloc/Versions
> @@ -88,13 +88,6 @@ libc {
> __libc_dynarray_finalize;
> __libc_dynarray_resize;
> __libc_dynarray_resize_clear;
> -
> - # struct alloc_buffer support
> - __libc_alloc_buffer_alloc_array;
> - __libc_alloc_buffer_allocate;
> - __libc_alloc_buffer_copy_bytes;
> - __libc_alloc_buffer_copy_string;
> - __libc_alloc_buffer_create_failure;
> }
> }
>
> diff --git a/malloc/alloc_buffer_alloc_array.c b/malloc/alloc_buffer_alloc_array.c
> index 165033004d..60f693e843 100644
> --- a/malloc/alloc_buffer_alloc_array.c
> +++ b/malloc/alloc_buffer_alloc_array.c
> @@ -43,4 +43,3 @@ __libc_alloc_buffer_alloc_array (struct alloc_buffer *buf, size_t element_size,
> return NULL;
> }
> }
> -libc_hidden_def (__libc_alloc_buffer_alloc_array)
> diff --git a/malloc/alloc_buffer_allocate.c b/malloc/alloc_buffer_allocate.c
> index f3f6fd7761..5ebd389664 100644
> --- a/malloc/alloc_buffer_allocate.c
> +++ b/malloc/alloc_buffer_allocate.c
> @@ -33,4 +33,3 @@ __libc_alloc_buffer_allocate (size_t size, void **pptr)
> else
> return alloc_buffer_create (*pptr, size);
> }
> -libc_hidden_def (__libc_alloc_buffer_allocate)
> diff --git a/malloc/alloc_buffer_copy_bytes.c b/malloc/alloc_buffer_copy_bytes.c
> index 77f95374dc..79ce636f55 100644
> --- a/malloc/alloc_buffer_copy_bytes.c
> +++ b/malloc/alloc_buffer_copy_bytes.c
> @@ -31,4 +31,3 @@ __libc_alloc_buffer_copy_bytes (struct alloc_buffer buf,
> memcpy (ptr, src, len);
> return buf;
> }
> -libc_hidden_def (__libc_alloc_buffer_copy_bytes)
> diff --git a/malloc/alloc_buffer_copy_string.c b/malloc/alloc_buffer_copy_string.c
> index 16068c7f60..b5c734ea6a 100644
> --- a/malloc/alloc_buffer_copy_string.c
> +++ b/malloc/alloc_buffer_copy_string.c
> @@ -27,4 +27,3 @@ __libc_alloc_buffer_copy_string (struct alloc_buffer buf, const char *src)
> {
> return __libc_alloc_buffer_copy_bytes (buf, src, strlen (src) + 1);
> }
> -libc_hidden_def (__libc_alloc_buffer_copy_string)
> diff --git a/malloc/alloc_buffer_create_failure.c b/malloc/alloc_buffer_create_failure.c
> index cbafedc36d..42c58a3aa2 100644
> --- a/malloc/alloc_buffer_create_failure.c
> +++ b/malloc/alloc_buffer_create_failure.c
> @@ -20,12 +20,7 @@
> #include <stdio.h>
>
> void
> -__libc_alloc_buffer_create_failure (void *start, size_t size)
> +__libc_alloc_buffer_create_failure (void)
> {
> - char buf[200];
> - __snprintf (buf, sizeof (buf), "Fatal glibc error: "
> - "invalid allocation buffer of size %zu\n",
> - size);
> - __libc_fatal (buf);
> + __libc_fatal ("Fatal glibc error: invalid allocation buffer\n");
> }
> -libc_hidden_def (__libc_alloc_buffer_create_failure)
> diff --git a/malloc/tst-alloc_buffer.c b/malloc/tst-alloc_buffer.c
> index 7c2a15ac90..f1ca4a1e32 100644
> --- a/malloc/tst-alloc_buffer.c
> +++ b/malloc/tst-alloc_buffer.c
> @@ -16,6 +16,10 @@
> License along with the GNU C Library; if not, see
> <https://www.gnu.org/licenses/>. */
>
> +/* Note: This test exercises the (identical) copy of the
> + <alloc_buffer.h> in libsupport, not libc.so, because the latter has
> + hidden visibility and cannot be tested from the outside. */
> +
> #include <arpa/inet.h>
> #include <alloc_buffer.h>
> #include <stdio.h>
> diff --git a/nss/Makefile b/nss/Makefile
> index 3ee51f309e..91d1bf2c4d 100644
> --- a/nss/Makefile
> +++ b/nss/Makefile
> @@ -475,9 +475,9 @@ libof-nss_test1 = extramodules
> libof-nss_test2 = extramodules
> libof-nss_test_errno = extramodules
> libof-nss_test_gai_hv2_canonname = extramodules
> -$(objpfx)/libnss_test1.so: $(objpfx)nss_test1.os $(link-libc-deps)
> +$(objpfx)/libnss_test1.so: $(objpfx)nss_test1.os $(libsupport) $(link-libc-deps)
> $(build-module)
> -$(objpfx)/libnss_test2.so: $(objpfx)nss_test2.os $(link-libc-deps)
> +$(objpfx)/libnss_test2.so: $(objpfx)nss_test2.os $(libsupport) $(link-libc-deps)
> $(build-module)
> $(objpfx)/libnss_test_errno.so: $(objpfx)nss_test_errno.os $(link-libc-deps)
> $(build-module)
> diff --git a/support/Makefile b/support/Makefile
> index 59a9974539..c55553c0cc 100644
> --- a/support/Makefile
> +++ b/support/Makefile
> @@ -41,6 +41,7 @@ libsupport-routines = \
> resolv_response_context_free \
> resolv_test \
> set_fortify_handler \
> + support-alloc_buffer \
> support-open-dev-null-range \
> support_become_root \
> support_can_chroot \
> diff --git a/support/support-alloc_buffer.c b/support/support-alloc_buffer.c
> new file mode 100644
> index 0000000000..71ffb703c5
> --- /dev/null
> +++ b/support/support-alloc_buffer.c
> @@ -0,0 +1,26 @@
> +/* Make <alloc_buffer.h> available to tests.
> + Copyright (C) 2025 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; if not, see
> + <https://www.gnu.org/licenses/>. */
> +
> +/* The implementation in libc.so has hidden visibility and is
> + therefore not usable. */
> +
> +#include <malloc/alloc_buffer_alloc_array.c>
> +#include <malloc/alloc_buffer_allocate.c>
> +#include <malloc/alloc_buffer_copy_bytes.c>
> +#include <malloc/alloc_buffer_copy_string.c>
> +#include <malloc/alloc_buffer_create_failure.c>
More information about the Libc-alpha
mailing list