[2/3] x86_64: Replace inline asm rotates in pointer_guard with stdc_rotate_{left, right}

Message ID 20260506184522.3278011-3-ubizjak@gmail.com (mailing list archive)
State Committed
Commit 63545c6d2c5c5a9b5259761b23eb6996bb71187f
Headers
Series stdlib, x86: Add internal stdc_rotate_left and use it in x86 pointer (de-)mangling |

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 fail Patch failed to apply
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 fail Patch failed to apply

Commit Message

Uros Bizjak May 6, 2026, 6:42 p.m. UTC
  Use the C23 <stdbit.h> rotation helpers instead of inline assembly
for pointer mangling and demangling on x86_64.

The PTR_MANGLE and PTR_DEMANGLE macros previously used rol/ror
inline asm with a constant rotation of 2 * LP_SIZE + 1. Replace
these with stdc_rotate_left and stdc_rotate_right operating on
uintptr_t, preserving the exact rotation count via
2 * sizeof (uintptr_t) + 1.

This change removes inline assembly, improves portability and
readability and lets the compiler select optimal code generation.

No functional change intended.

Signed-off-by: Uros Bizjak <ubizjak@gmail.com>
---
 sysdeps/unix/sysv/linux/x86_64/pointer_guard.h | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
  

Comments

Adhemerval Zanella Netto May 7, 2026, 1:33 p.m. UTC | #1
On 06/05/26 15:42, Uros Bizjak wrote:
> Use the C23 <stdbit.h> rotation helpers instead of inline assembly
> for pointer mangling and demangling on x86_64.
> 
> The PTR_MANGLE and PTR_DEMANGLE macros previously used rol/ror
> inline asm with a constant rotation of 2 * LP_SIZE + 1. Replace
> these with stdc_rotate_left and stdc_rotate_right operating on
> uintptr_t, preserving the exact rotation count via
> 2 * sizeof (uintptr_t) + 1.
> 
> This change removes inline assembly, improves portability and
> readability and lets the compiler select optimal code generation.
> 
> No functional change intended.
> 
> Signed-off-by: Uros Bizjak <ubizjak@gmail.com>

LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>


> ---
>  sysdeps/unix/sysv/linux/x86_64/pointer_guard.h | 14 ++++++++++----
>  1 file changed, 10 insertions(+), 4 deletions(-)
> 
> diff --git a/sysdeps/unix/sysv/linux/x86_64/pointer_guard.h b/sysdeps/unix/sysv/linux/x86_64/pointer_guard.h
> index 898a2dd3ed..4f7abc64f4 100644
> --- a/sysdeps/unix/sysv/linux/x86_64/pointer_guard.h
> +++ b/sysdeps/unix/sysv/linux/x86_64/pointer_guard.h
> @@ -31,17 +31,20 @@
>  #  define PTR_DEMANGLE(reg)	ror $2*LP_SIZE+1, reg;			      \
>  				xor __pointer_chk_guard_local(%rip), reg
>  # else
> +#  include <stdbit.h>
>  #  include <stdint.h>
>  extern uintptr_t __pointer_chk_guard_local attribute_relro attribute_hidden;
>  #  define PTR_MANGLE(var)						      \
>      do {								      \
>        (var) = (__typeof (var)) ((uintptr_t) (var)			      \
>  				^ __pointer_chk_guard_local);		      \
> -      asm ("rol $2*" LP_SIZE "+1, %0" : "+r" (var));			      \
> +      (var) = (__typeof (var)) stdc_rotate_left ((uintptr_t) (var),	      \
> +						 2 * sizeof (uintptr_t) + 1); \
>      } while (0)
>  #  define PTR_DEMANGLE(var)						      \
>      do {								      \
> -      asm ("ror $2*" LP_SIZE "+1, %0" : "+r" (var));			      \
> +      (var) = (__typeof (var)) stdc_rotate_right ((uintptr_t) (var),	      \
> +						  2 * sizeof (uintptr_t) + 1); \
>        (var) = (__typeof (var)) ((uintptr_t) (var)			      \
>  				^ __pointer_chk_guard_local);		      \
>      } while (0)
> @@ -53,16 +56,19 @@ extern uintptr_t __pointer_chk_guard_local attribute_relro attribute_hidden;
>  #  define PTR_DEMANGLE(reg)	ror $2*LP_SIZE+1, reg;			      \
>  				xor %fs:POINTER_GUARD, reg
>  # else
> +#  include <stdbit.h>
>  #  include <tls.h>
>  #  define PTR_MANGLE(var)						      \
>      do {								      \
>        (var) = (__typeof (var)) ((uintptr_t) (var)			      \
>  				^ ((tcbhead_t __seg_fs *)0)->pointer_guard);  \
> -      asm ("rol $2*" LP_SIZE "+1, %0" : "+r" (var));			      \
> +      (var) = (__typeof (var)) stdc_rotate_left ((uintptr_t) (var),	      \
> +						 2 * sizeof (uintptr_t) + 1); \
>      } while (0)
>  #  define PTR_DEMANGLE(var)						      \
>      do {								      \
> -      asm ("ror $2*" LP_SIZE "+1, %0" : "+r" (var));			      \
> +      (var) = (__typeof (var)) stdc_rotate_right ((uintptr_t) (var),	      \
> +						  2 * sizeof (uintptr_t) + 1); \
>        (var) = (__typeof (var)) ((uintptr_t) (var)			      \
>  				^ ((tcbhead_t __seg_fs *)0)->pointer_guard);  \
>      } while (0)
  

Patch

diff --git a/sysdeps/unix/sysv/linux/x86_64/pointer_guard.h b/sysdeps/unix/sysv/linux/x86_64/pointer_guard.h
index 898a2dd3ed..4f7abc64f4 100644
--- a/sysdeps/unix/sysv/linux/x86_64/pointer_guard.h
+++ b/sysdeps/unix/sysv/linux/x86_64/pointer_guard.h
@@ -31,17 +31,20 @@ 
 #  define PTR_DEMANGLE(reg)	ror $2*LP_SIZE+1, reg;			      \
 				xor __pointer_chk_guard_local(%rip), reg
 # else
+#  include <stdbit.h>
 #  include <stdint.h>
 extern uintptr_t __pointer_chk_guard_local attribute_relro attribute_hidden;
 #  define PTR_MANGLE(var)						      \
     do {								      \
       (var) = (__typeof (var)) ((uintptr_t) (var)			      \
 				^ __pointer_chk_guard_local);		      \
-      asm ("rol $2*" LP_SIZE "+1, %0" : "+r" (var));			      \
+      (var) = (__typeof (var)) stdc_rotate_left ((uintptr_t) (var),	      \
+						 2 * sizeof (uintptr_t) + 1); \
     } while (0)
 #  define PTR_DEMANGLE(var)						      \
     do {								      \
-      asm ("ror $2*" LP_SIZE "+1, %0" : "+r" (var));			      \
+      (var) = (__typeof (var)) stdc_rotate_right ((uintptr_t) (var),	      \
+						  2 * sizeof (uintptr_t) + 1); \
       (var) = (__typeof (var)) ((uintptr_t) (var)			      \
 				^ __pointer_chk_guard_local);		      \
     } while (0)
@@ -53,16 +56,19 @@  extern uintptr_t __pointer_chk_guard_local attribute_relro attribute_hidden;
 #  define PTR_DEMANGLE(reg)	ror $2*LP_SIZE+1, reg;			      \
 				xor %fs:POINTER_GUARD, reg
 # else
+#  include <stdbit.h>
 #  include <tls.h>
 #  define PTR_MANGLE(var)						      \
     do {								      \
       (var) = (__typeof (var)) ((uintptr_t) (var)			      \
 				^ ((tcbhead_t __seg_fs *)0)->pointer_guard);  \
-      asm ("rol $2*" LP_SIZE "+1, %0" : "+r" (var));			      \
+      (var) = (__typeof (var)) stdc_rotate_left ((uintptr_t) (var),	      \
+						 2 * sizeof (uintptr_t) + 1); \
     } while (0)
 #  define PTR_DEMANGLE(var)						      \
     do {								      \
-      asm ("ror $2*" LP_SIZE "+1, %0" : "+r" (var));			      \
+      (var) = (__typeof (var)) stdc_rotate_right ((uintptr_t) (var),	      \
+						  2 * sizeof (uintptr_t) + 1); \
       (var) = (__typeof (var)) ((uintptr_t) (var)			      \
 				^ ((tcbhead_t __seg_fs *)0)->pointer_guard);  \
     } while (0)