about summary refs log tree commit diff
path: root/malloc
diff options
context:
space:
mode:
authorH.J. Lu <hjl.tools@gmail.com>2021-07-08 20:48:14 -0700
committerH.J. Lu <hjl.tools@gmail.com>2021-07-09 06:39:30 -0700
commitdc76a059fded7a203c82dbb91d4fc1f43d3250db (patch)
treed77c68f593de7ebea9d5202aba84a57554a53355 /malloc
parent0ec97597c859ac6a69733cf70c6bd6fc809dcc4b (diff)
downloadglibc-dc76a059fded7a203c82dbb91d4fc1f43d3250db.tar.gz
glibc-dc76a059fded7a203c82dbb91d4fc1f43d3250db.tar.xz
glibc-dc76a059fded7a203c82dbb91d4fc1f43d3250db.zip
Add a generic malloc test for MALLOC_ALIGNMENT
1. Add sysdeps/generic/malloc-size.h to define size related macros for
malloc.
2. Move x86_64/tst-mallocalign1.c to malloc and replace ALIGN_MASK with
MALLOC_ALIGN_MASK.
3. Add tst-mallocalign1 to tests-exclude-mcheck for i386 and x32 since
mcheck doesn't honor MALLOC_ALIGNMENT.
Diffstat (limited to 'malloc')
-rw-r--r--malloc/Makefile1
-rw-r--r--malloc/malloc-internal.h41
-rw-r--r--malloc/tst-mallocalign1.c71
3 files changed, 73 insertions, 40 deletions
diff --git a/malloc/Makefile b/malloc/Makefile
index 37a9a4efab..b685ed6d61 100644
--- a/malloc/Makefile
+++ b/malloc/Makefile
@@ -42,6 +42,7 @@ tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \
 	 tst-malloc-stats-cancellation \
 	 tst-tcfree1 tst-tcfree2 tst-tcfree3 \
 	 tst-safe-linking \
+	 tst-mallocalign1 \
 
 tests-static := \
 	 tst-interpose-static-nothread \
diff --git a/malloc/malloc-internal.h b/malloc/malloc-internal.h
index 258f29584e..0c7b5a183c 100644
--- a/malloc/malloc-internal.h
+++ b/malloc/malloc-internal.h
@@ -21,46 +21,7 @@
 
 #include <malloc-machine.h>
 #include <malloc-sysdep.h>
-
-/* INTERNAL_SIZE_T is the word-size used for internal bookkeeping of
-   chunk sizes.
-
-   The default version is the same as size_t.
-
-   While not strictly necessary, it is best to define this as an
-   unsigned type, even if size_t is a signed type. This may avoid some
-   artificial size limitations on some systems.
-
-   On a 64-bit machine, you may be able to reduce malloc overhead by
-   defining INTERNAL_SIZE_T to be a 32 bit `unsigned int' at the
-   expense of not being able to handle more than 2^32 of malloced
-   space. If this limitation is acceptable, you are encouraged to set
-   this unless you are on a platform requiring 16byte alignments. In
-   this case the alignment requirements turn out to negate any
-   potential advantages of decreasing size_t word size.
-
-   Implementors: Beware of the possible combinations of:
-     - INTERNAL_SIZE_T might be signed or unsigned, might be 32 or 64 bits,
-       and might be the same width as int or as long
-     - size_t might have different width and signedness as INTERNAL_SIZE_T
-     - int and long might be 32 or 64 bits, and might be the same width
-
-   To deal with this, most comparisons and difference computations
-   among INTERNAL_SIZE_Ts should cast them to unsigned long, being
-   aware of the fact that casting an unsigned int to a wider long does
-   not sign-extend. (This also makes checking for negative numbers
-   awkward.) Some of these casts result in harmless compiler warnings
-   on some systems.  */
-#ifndef INTERNAL_SIZE_T
-# define INTERNAL_SIZE_T size_t
-#endif
-
-/* The corresponding word size.  */
-#define SIZE_SZ (sizeof (INTERNAL_SIZE_T))
-
-/* The corresponding bit mask value.  */
-#define MALLOC_ALIGN_MASK (MALLOC_ALIGNMENT - 1)
-
+#include <malloc-size.h>
 
 /* Called in the parent process before a fork.  */
 void __malloc_fork_lock_parent (void) attribute_hidden;
diff --git a/malloc/tst-mallocalign1.c b/malloc/tst-mallocalign1.c
new file mode 100644
index 0000000000..294e821afe
--- /dev/null
+++ b/malloc/tst-mallocalign1.c
@@ -0,0 +1,71 @@
+/* Verify that MALLOC_ALIGNMENT is honored by malloc.
+   Copyright (C) 2012-2021 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 <stdio.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include <malloc-size.h>
+
+static void *
+test (size_t s)
+{
+  void *p = malloc (s);
+
+  printf ("malloc: %zu, %p: %zu\n", s, p,
+	  ((uintptr_t) p) & MALLOC_ALIGN_MASK);
+  return p;
+}
+
+static int
+do_test (void)
+{
+  void *p;
+  int ret = 0;
+
+  p = test (2);
+  ret |= (uintptr_t) p & MALLOC_ALIGN_MASK;
+  free (p);
+
+  p = test (8);
+  ret |= (uintptr_t) p & MALLOC_ALIGN_MASK;
+  free (p);
+
+  p = test (13);
+  ret |= (uintptr_t) p & MALLOC_ALIGN_MASK;
+  free (p);
+
+  p = test (16);
+  ret |= (uintptr_t) p & MALLOC_ALIGN_MASK;
+  free (p);
+
+  p = test (23);
+  ret |= (uintptr_t) p & MALLOC_ALIGN_MASK;
+  free (p);
+
+  p = test (43);
+  ret |= (uintptr_t) p & MALLOC_ALIGN_MASK;
+  free (p);
+
+  p = test (123);
+  ret |= (uintptr_t) p & MALLOC_ALIGN_MASK;
+  free (p);
+
+  return ret;
+}
+
+#include <support/test-driver.c>