[v2] support: add support_address_diff function

Message ID 20260326135049.2254482-1-yury.khrustalev@arm.com (mailing list archive)
State Under Review
Delegated to: Wilco Dijkstra
Headers
Series [v2] support: add support_address_diff function |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Build passed
redhat-pt-bot/TryBot-32bit success Build for i686
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Test passed

Commit Message

Yury Khrustalev March 26, 2026, 1:50 p.m. UTC
  Some malloc tests compare pointers meaning to compare addresses.
On AArch64, the 64-bit value of the pointer may contain metadata
along with the values of the address.

In order to correctly compare addresses, we add new function for
AArch64 target that will use the AArch64 64 SUBP (subtract pointer)
instruction when it is available. This instruction uses the 56-bit
addresses ignoring top-byte metadata.

Best implementation is selected using ifunc resolver.

On other targets and also on AArch64 when MTE is not available this
function defaults to PTR_DIFF defined in libc-pointer-arith.h.

Three malloc tests are modified accordingly:
 - tst-memalign-2.c
 - tst-memalign-3.c
 - tst-realloc.c

---
base-commit: 4a97c12302
passes regression on aarch64 and x86-64

Changes in v2:
 - Use sysdeps files rather than arch-specific macro for conditional
   compilation of code.
 - Renamed the support_address_diff and added comment explaining what
   it does.
 - v1: https://inbox.sourceware.org/libc-alpha/20260325105900.828958-1-yury.khrustalev@arm.com/

---
 malloc/tst-memalign-2.c                | 10 +++---
 malloc/tst-memalign-3.c                | 10 +++---
 malloc/tst-realloc.c                   |  5 +--
 support/Makefile                       |  2 +-
 support/address-diff.h                 | 27 ++++++++++++++
 sysdeps/aarch64/Makefile               |  6 ++++
 sysdeps/aarch64/support-address-diff.c | 50 ++++++++++++++++++++++++++
 sysdeps/generic/Makefile               |  6 ++++
 sysdeps/generic/support-address-diff.c | 26 ++++++++++++++
 9 files changed, 131 insertions(+), 11 deletions(-)
 create mode 100644 support/address-diff.h
 create mode 100644 sysdeps/aarch64/support-address-diff.c
 create mode 100644 sysdeps/generic/support-address-diff.c
  

Comments

Yury Khrustalev April 10, 2026, 10:26 a.m. UTC | #1
On Thu, Mar 26, 2026 at 01:50:49PM +0000, Yury Khrustalev wrote:
> Some malloc tests compare pointers meaning to compare addresses.
> On AArch64, the 64-bit value of the pointer may contain metadata
> along with the values of the address.
> 
> In order to correctly compare addresses, we add new function for
> AArch64 target that will use the AArch64 64 SUBP (subtract pointer)
> instruction when it is available. This instruction uses the 56-bit
> addresses ignoring top-byte metadata.
> 
> Best implementation is selected using ifunc resolver.
> 
> On other targets and also on AArch64 when MTE is not available this
> function defaults to PTR_DIFF defined in libc-pointer-arith.h.
> 
> Three malloc tests are modified accordingly:
>  - tst-memalign-2.c
>  - tst-memalign-3.c
>  - tst-realloc.c
> 
> ---
> base-commit: 4a97c12302
> passes regression on aarch64 and x86-64
> 
> Changes in v2:
>  - Use sysdeps files rather than arch-specific macro for conditional
>    compilation of code.
>  - Renamed the support_address_diff and added comment explaining what
>    it does.
>  - v1: https://inbox.sourceware.org/libc-alpha/20260325105900.828958-1-yury.khrustalev@arm.com/
> 

Polite ping :)
  

Patch

diff --git a/malloc/tst-memalign-2.c b/malloc/tst-memalign-2.c
index b3e393c249..2dd8808236 100644
--- a/malloc/tst-memalign-2.c
+++ b/malloc/tst-memalign-2.c
@@ -23,6 +23,7 @@ 
 #include <unistd.h>
 #include <array_length.h>
 #include <libc-pointer-arith.h>
+#include <support/address-diff.h>
 #include <support/check.h>
 #include "tst-malloc-aux.h"
 
@@ -82,9 +83,9 @@  do_test (void)
       /* This should return the same chunk as was just free'd.  */
       tcache_allocs[i].ptr2 = memalign (tcache_allocs[i].alignment, sz2);
       CHECK (tcache_allocs[i].ptr2, tcache_allocs[i].alignment);
+      TEST_VERIFY (support_address_diff (
+	tcache_allocs[i].ptr1, tcache_allocs[i].ptr2) == 0);
       free (tcache_allocs[i].ptr2);
-
-      TEST_VERIFY (tcache_allocs[i].ptr1 == tcache_allocs[i].ptr2);
     }
 
   /* Test for non-head tcache hits.  This exercises the memalign
@@ -110,7 +111,7 @@  do_test (void)
 
   count = 0;
   for (i = 0; i < 10; ++ i)
-    if (ptr[i] == p)
+    if (support_address_diff (ptr[i], p) == 0)
       ++ count;
   free (p);
   TEST_VERIFY (count > 0);
@@ -146,7 +147,8 @@  do_test (void)
     {
       int ok = 0;
       for (j = 0; j < LN; ++ j)
-	if (large_allocs[i].ptr1 == large_allocs[j].ptr2)
+	if (support_address_diff (large_allocs[i].ptr1, large_allocs[j].ptr2)
+	    == 0)
 	  ok = 1;
       if (ok == 1)
 	count ++;
diff --git a/malloc/tst-memalign-3.c b/malloc/tst-memalign-3.c
index 435e1c94d5..f1b045b095 100644
--- a/malloc/tst-memalign-3.c
+++ b/malloc/tst-memalign-3.c
@@ -24,6 +24,7 @@ 
 #include <unistd.h>
 #include <array_length.h>
 #include <libc-pointer-arith.h>
+#include <support/address-diff.h>
 #include <support/check.h>
 #include <support/xthread.h>
 #include "tst-malloc-aux.h"
@@ -85,9 +86,9 @@  mem_test (void *closure)
       /* This should return the same chunk as was just free'd.  */
       tcache_allocs[i].ptr2 = memalign (tcache_allocs[i].alignment, sz2);
       CHECK (tcache_allocs[i].ptr2, tcache_allocs[i].alignment);
+      TEST_VERIFY (support_address_diff (
+	tcache_allocs[i].ptr1, tcache_allocs[i].ptr2) == 0);
       free (tcache_allocs[i].ptr2);
-
-      TEST_VERIFY (tcache_allocs[i].ptr1 == tcache_allocs[i].ptr2);
     }
 
   /* Test for non-head tcache hits.  */
@@ -112,7 +113,7 @@  mem_test (void *closure)
 
   count = 0;
   for (i = 0; i < 10; ++ i)
-    if (ptr[i] == p)
+    if (support_address_diff (ptr[i], p) == 0)
       ++ count;
   free (p);
   TEST_VERIFY (count > 0);
@@ -146,7 +147,8 @@  mem_test (void *closure)
     {
       int ok = 0;
       for (j = 0; j < LN; ++ j)
-	if (large_allocs[i].ptr1 == large_allocs[j].ptr2)
+	if (support_address_diff (large_allocs[i].ptr1, large_allocs[j].ptr2)
+	    == 0)
 	  ok = 1;
       if (ok == 1)
 	count ++;
diff --git a/malloc/tst-realloc.c b/malloc/tst-realloc.c
index e02b28d982..0e616d3827 100644
--- a/malloc/tst-realloc.c
+++ b/malloc/tst-realloc.c
@@ -22,6 +22,7 @@ 
 #include <string.h>
 #include <libc-diag.h>
 #include <support/check.h>
+#include <support/address-diff.h>
 
 #include "tst-malloc-aux.h"
 
@@ -155,9 +156,9 @@  do_test (void)
       size_t newsz = malloc_usable_size (p);
       printf ("size: %zu, usable size: %zu, extra: %zu\n",
 	      sz, newsz, newsz - sz);
-      uintptr_t oldp = (uintptr_t) p;
+      void *oldp = p;
       void *new_p = realloc (p, newsz);
-      if ((uintptr_t) new_p != oldp)
+      if (support_address_diff (new_p, oldp) != 0)
 	FAIL_EXIT1 ("Expanding (%zu bytes) to usable size (%zu) moved block",
 		    sz, newsz);
       free (new_p);
diff --git a/support/Makefile b/support/Makefile
index 1dae2802cf..3f19a98bdc 100644
--- a/support/Makefile
+++ b/support/Makefile
@@ -254,7 +254,7 @@  libsupport-routines = \
   xwrite \
   # libsupport-routines
 
-libsupport-static-only-routines := $(libsupport-routines)
+libsupport-static-only-routines = $(libsupport-routines) $(libsupport-sysdep_routines)
 # Only build one variant of the library.
 libsupport-inhibit-o := .os
 ifeq ($(build-shared),yes)
diff --git a/support/address-diff.h b/support/address-diff.h
new file mode 100644
index 0000000000..ef58b9d8a6
--- /dev/null
+++ b/support/address-diff.h
@@ -0,0 +1,27 @@ 
+/* Support functions for pointer arithmetic.
+   Copyright (C) 2026 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/>.  */
+
+#ifndef _POINTER_ARITH_H
+#define _POINTER_ARITH_H 1
+
+#include <stddef.h>
+
+/* Returns difference in bytes between addresses of two pointers.  */
+ptrdiff_t support_address_diff (const void *lhs, const void *rhs);
+
+#endif /* _POINTER_ARITH_H */
diff --git a/sysdeps/aarch64/Makefile b/sysdeps/aarch64/Makefile
index d6c5cc96ca..d3146ddced 100644
--- a/sysdeps/aarch64/Makefile
+++ b/sysdeps/aarch64/Makefile
@@ -90,3 +90,9 @@  endif
 ifeq ($(subdir),malloc)
 sysdep_malloc_debug_routines = __mtag_tag_zero_region __mtag_tag_region
 endif # malloc directory
+
+ifeq ($(subdir),support)
+libsupport-sysdep_routines += \
+  support-address-diff \
+  # libsupport-sysdep_routines
+endif
diff --git a/sysdeps/aarch64/support-address-diff.c b/sysdeps/aarch64/support-address-diff.c
new file mode 100644
index 0000000000..fd8d3ed920
--- /dev/null
+++ b/sysdeps/aarch64/support-address-diff.c
@@ -0,0 +1,50 @@ 
+/* Support functions for pointer arithmetic for AArch64.
+   Copyright (C) 2026 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 "address-diff.h"
+
+#include <sys/ifunc.h>
+#include <sys/auxv.h>
+#include <libc-pointer-arith.h>
+
+static ptrdiff_t
+address_diff_mte (const void *lhs, const void *rhs)
+{
+  register const void *x0 asm ("x0") = lhs;
+  register const void *x1 asm ("x1") = rhs;
+  asm (".inst 0x9ac10000 /* subp x0, x0, x1 */" : "+r" (x0) : "r" (x1));
+  return (ptrdiff_t)x0;
+}
+
+static ptrdiff_t
+address_diff_generic (const void *lhs, const void *rhs)
+{
+  return PTR_DIFF (lhs, rhs);
+}
+
+static void * __attribute__ ((unused))
+address_diff_resolver (unsigned long a0, const unsigned long *a1)
+{
+  unsigned long hwcap2 = __ifunc_hwcap (_IFUNC_ARG_AT_HWCAP2, a0, a1);
+  if (hwcap2 & HWCAP2_MTE)
+    return (void *)address_diff_mte;
+  return (void *)address_diff_generic;
+}
+
+ptrdiff_t support_address_diff (const void *lhs, const void *rhs)
+__attribute__ ((ifunc ("address_diff_resolver")));
diff --git a/sysdeps/generic/Makefile b/sysdeps/generic/Makefile
index 0c25592f35..1f60c29694 100644
--- a/sysdeps/generic/Makefile
+++ b/sysdeps/generic/Makefile
@@ -38,3 +38,9 @@  endif
 ifeq ($(subdir),misc)
 sysdep_routines += malloc-hugepages
 endif
+
+ifeq ($(subdir),support)
+libsupport-sysdep_routines += \
+  support-address-diff \
+  # libsupport-sysdep_routines
+endif
diff --git a/sysdeps/generic/support-address-diff.c b/sysdeps/generic/support-address-diff.c
new file mode 100644
index 0000000000..51b5640acd
--- /dev/null
+++ b/sysdeps/generic/support-address-diff.c
@@ -0,0 +1,26 @@ 
+/* Support functions for pointer arithmetic.
+   Copyright (C) 2026 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 "address-diff.h"
+
+#include <libc-pointer-arith.h>
+
+ptrdiff_t support_address_diff (const void *lhs, const void *rhs)
+{
+  return PTR_DIFF (lhs, rhs);
+}