]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
aligned_alloc: conform to C17
authorDJ Delorie <dj@redhat.com>
Tue, 21 Mar 2023 04:46:43 +0000 (00:46 -0400)
committerDJ Delorie <dj@redhat.com>
Mon, 8 May 2023 20:40:10 +0000 (16:40 -0400)
This patch adds the strict checking for power-of-two alignments
in aligned_alloc(), and updates the manual accordingly.

Reviewed-by: Carlos O'Donell <carlos@redhat.com>
malloc/Makefile
malloc/malloc-debug.c
malloc/malloc.c
malloc/tst-aligned-alloc-static.c [new file with mode: 0644]
malloc/tst-aligned-alloc.c [new file with mode: 0644]
manual/memory.texi

index 21c5a7c0d7386d67a0ac69ae9534426d60fe18aa..ce6ebcf1b3fb39296093b9a8a0b8c361fb1381bc 100644 (file)
@@ -44,11 +44,13 @@ tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \
         tst-safe-linking \
         tst-mallocalign1 \
         tst-memalign-2 \
-        tst-memalign-3
+        tst-memalign-3 \
+        tst-aligned-alloc
 
 tests-static := \
         tst-interpose-static-nothread \
-        tst-interpose-static-thread
+        tst-interpose-static-thread \
+        tst-aligned-alloc-static
 
 # Test for the malloc_set_state symbol removed in glibc 2.25.
 ifeq ($(have-GLIBC_2.23)$(build-shared),yesyes)
index 3867d156989131101ce8d112d2c23d0dacfd1795..da9d2340d3343e5bea1cb48761e482828892db38 100644 (file)
@@ -299,7 +299,14 @@ __debug_memalign (size_t alignment, size_t bytes)
   return _debug_mid_memalign (alignment, bytes, RETURN_ADDRESS (0));
 }
 strong_alias (__debug_memalign, memalign)
-strong_alias (__debug_memalign, aligned_alloc)
+static void *
+__debug_aligned_alloc (size_t alignment, size_t bytes)
+{
+  if (!powerof2 (alignment) || alignment == 0)
+    return NULL;
+  return _debug_mid_memalign (alignment, bytes, RETURN_ADDRESS (0));
+}
+strong_alias (__debug_aligned_alloc, aligned_alloc)
 
 static void *
 __debug_pvalloc (size_t bytes)
index e33ed665db02d81ef97fe4f3f2bba56516eac802..5d8b61d66caa374c649047d4114746585a3fe501 100644 (file)
@@ -3528,6 +3528,29 @@ __libc_memalign (size_t alignment, size_t bytes)
   void *address = RETURN_ADDRESS (0);
   return _mid_memalign (alignment, bytes, address);
 }
+libc_hidden_def (__libc_memalign)
+
+/* For ISO C17.  */
+void *
+weak_function
+aligned_alloc (size_t alignment, size_t bytes)
+{
+  if (!__malloc_initialized)
+    ptmalloc_init ();
+
+/* Similar to memalign, but starting with ISO C17 the standard
+   requires an error for alignments that are not supported by the
+   implementation.  Valid alignments for the current implementation
+   are non-negative powers of two.  */
+  if (!powerof2 (alignment) || alignment == 0)
+    {
+      __set_errno (EINVAL);
+      return 0;
+    }
+
+  void *address = RETURN_ADDRESS (0);
+  return _mid_memalign (alignment, bytes, address);
+}
 
 static void *
 _mid_memalign (size_t alignment, size_t bytes, void *address)
@@ -3618,9 +3641,6 @@ _mid_memalign (size_t alignment, size_t bytes, void *address)
           ar_ptr == arena_for_chunk (mem2chunk (p)));
   return tag_new_usable (p);
 }
-/* For ISO C11.  */
-weak_alias (__libc_memalign, aligned_alloc)
-libc_hidden_def (__libc_memalign)
 
 void *
 __libc_valloc (size_t bytes)
diff --git a/malloc/tst-aligned-alloc-static.c b/malloc/tst-aligned-alloc-static.c
new file mode 100644 (file)
index 0000000..d504473
--- /dev/null
@@ -0,0 +1 @@
+#include "tst-aligned-alloc.c"
diff --git a/malloc/tst-aligned-alloc.c b/malloc/tst-aligned-alloc.c
new file mode 100644 (file)
index 0000000..8bd6527
--- /dev/null
@@ -0,0 +1,80 @@
+/* Test for C17 alignment requirements.
+   Copyright (C) 2023 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/>.  */
+
+#include <errno.h>
+#include <malloc.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <libc-diag.h>
+#include <support/check.h>
+
+static int
+do_test (void)
+{
+  void *p1;
+  void *p2;
+  void *p3;
+  void *p4;
+  void *p5;
+
+  errno = 0;
+
+  /* The implementation supports alignments that are non-negative powers of 2.
+     We test 5 distinct conditions here:
+     - A non-negative power of 2 alignment e.g. 64.
+     - A degenerate zero power of 2 alignment e.g. 1.
+     - A non-power-of-2 alignment e.g. 65.
+     - A zero alignment.
+     - A corner case SIZE_MAX / 2 + 1 alignment.
+  */
+
+  p1 = aligned_alloc (64, 64);
+
+  if (p1 == NULL)
+    FAIL_EXIT1 ("aligned_alloc(64, 64) failed");
+
+  p2 = aligned_alloc (1, 64);
+
+  if (p2 == NULL)
+    FAIL_EXIT1 ("aligned_alloc(1, 64) failed");
+
+  p3 = aligned_alloc (65, 64);
+
+  if (p3 != NULL)
+    FAIL_EXIT1 ("aligned_alloc(65, 64) did not fail");
+
+  p4 = aligned_alloc (0, 64);
+
+  if (p4 != NULL)
+    FAIL_EXIT1 ("aligned_alloc(0, 64) did not fail");
+
+  /* This is an alignment like 0x80000000...UL */
+  p5 = aligned_alloc (SIZE_MAX / 2 + 1, 64);
+
+  if (p5 != NULL)
+    FAIL_EXIT1 ("aligned_alloc(SIZE_MAX/2+1, 64) did not fail");
+
+  free (p1);
+  free (p2);
+  return 0;
+}
+
+#define TEST_FUNCTION do_test ()
+#include "../test-skeleton.c"
index 9d3398a326300f9ae693fe00ff3fecaff5fce014..8952ff2bfa28b4f7b3fd398a656b3f9ae73ee0f7 100644 (file)
@@ -995,7 +995,7 @@ power of two than that, use @code{aligned_alloc} or @code{posix_memalign}.
 @c Alias to memalign.
 The @code{aligned_alloc} function allocates a block of @var{size} bytes whose
 address is a multiple of @var{alignment}.  The @var{alignment} must be a
-power of two and @var{size} must be a multiple of @var{alignment}.
+power of two.
 
 The @code{aligned_alloc} function returns a null pointer on error and sets
 @code{errno} to one of the following values: