public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] x86-64 strncpy: Properly handle the length parameter [BZ# 29839]
@ 2022-12-02  0:36 H.J. Lu
  2022-12-02  1:38 ` Noah Goldstein
  0 siblings, 1 reply; 2+ messages in thread
From: H.J. Lu @ 2022-12-02  0:36 UTC (permalink / raw)
  To: libc-alpha; +Cc: Noah Goldstein

On x32, the size_t parameter may be passed in the lower 32 bits of a
64-bit register with the non-zero upper 32 bits.  The string/memory
functions written in assembly can only use the lower 32 bits of a
64-bit register as length or must clear the upper 32 bits before using
the full 64-bit register for length.

This pach fixes strncpy for x32.  Tested on x86-64 and x32.  On x86-64,
libc.so is the same with and without the fix.
---
 sysdeps/x86_64/multiarch/strncpy-avx2.S | 4 ++++
 sysdeps/x86_64/multiarch/strncpy-evex.S | 4 ++++
 2 files changed, 8 insertions(+)

diff --git a/sysdeps/x86_64/multiarch/strncpy-avx2.S b/sysdeps/x86_64/multiarch/strncpy-avx2.S
index e9afd8fbed..3e6350ce4a 100644
--- a/sysdeps/x86_64/multiarch/strncpy-avx2.S
+++ b/sysdeps/x86_64/multiarch/strncpy-avx2.S
@@ -52,6 +52,10 @@
 
 	.section SECTION(.text), "ax", @progbits
 ENTRY(STRNCPY)
+# ifdef __ILP32__
+	/* Clear the upper 32 bits.  */
+	movl	%edx, %edx
+# endif
 	/* Filter zero length strings and very long strings.  Zero
 	   length strings just return, very long strings are handled by
 	   just running rep stos{b|l} to zero set (which will almost
diff --git a/sysdeps/x86_64/multiarch/strncpy-evex.S b/sysdeps/x86_64/multiarch/strncpy-evex.S
index 49eaf4cbd9..dec8cccc2b 100644
--- a/sysdeps/x86_64/multiarch/strncpy-evex.S
+++ b/sysdeps/x86_64/multiarch/strncpy-evex.S
@@ -80,6 +80,10 @@
 
 	.section SECTION(.text), "ax", @progbits
 ENTRY(STRNCPY)
+# ifdef __ILP32__
+	/* Clear the upper 32 bits.  */
+	movl	%edx, %edx
+# endif
 	/* Filter zero length strings and very long strings.  Zero
 	   length strings just return, very long strings are handled by
 	   just running rep stos{b|l} to zero set (which will almost
-- 
2.38.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] x86-64 strncpy: Properly handle the length parameter [BZ# 29839]
  2022-12-02  0:36 [PATCH] x86-64 strncpy: Properly handle the length parameter [BZ# 29839] H.J. Lu
@ 2022-12-02  1:38 ` Noah Goldstein
  0 siblings, 0 replies; 2+ messages in thread
From: Noah Goldstein @ 2022-12-02  1:38 UTC (permalink / raw)
  To: H.J. Lu; +Cc: libc-alpha

On Thu, Dec 1, 2022 at 4:36 PM H.J. Lu <hjl.tools@gmail.com> wrote:
>
> On x32, the size_t parameter may be passed in the lower 32 bits of a
> 64-bit register with the non-zero upper 32 bits.  The string/memory
> functions written in assembly can only use the lower 32 bits of a
> 64-bit register as length or must clear the upper 32 bits before using
> the full 64-bit register for length.
>
> This pach fixes strncpy for x32.  Tested on x86-64 and x32.  On x86-64,
> libc.so is the same with and without the fix.
> ---
>  sysdeps/x86_64/multiarch/strncpy-avx2.S | 4 ++++
>  sysdeps/x86_64/multiarch/strncpy-evex.S | 4 ++++
>  2 files changed, 8 insertions(+)
>
> diff --git a/sysdeps/x86_64/multiarch/strncpy-avx2.S b/sysdeps/x86_64/multiarch/strncpy-avx2.S
> index e9afd8fbed..3e6350ce4a 100644
> --- a/sysdeps/x86_64/multiarch/strncpy-avx2.S
> +++ b/sysdeps/x86_64/multiarch/strncpy-avx2.S
> @@ -52,6 +52,10 @@
>
>         .section SECTION(.text), "ax", @progbits
>  ENTRY(STRNCPY)
> +# ifdef __ILP32__
> +       /* Clear the upper 32 bits.  */
> +       movl    %edx, %edx
> +# endif
>         /* Filter zero length strings and very long strings.  Zero
>            length strings just return, very long strings are handled by
>            just running rep stos{b|l} to zero set (which will almost
> diff --git a/sysdeps/x86_64/multiarch/strncpy-evex.S b/sysdeps/x86_64/multiarch/strncpy-evex.S
> index 49eaf4cbd9..dec8cccc2b 100644
> --- a/sysdeps/x86_64/multiarch/strncpy-evex.S
> +++ b/sysdeps/x86_64/multiarch/strncpy-evex.S
> @@ -80,6 +80,10 @@
>
>         .section SECTION(.text), "ax", @progbits
>  ENTRY(STRNCPY)
> +# ifdef __ILP32__
> +       /* Clear the upper 32 bits.  */
> +       movl    %edx, %edx
> +# endif
>         /* Filter zero length strings and very long strings.  Zero
>            length strings just return, very long strings are handled by
>            just running rep stos{b|l} to zero set (which will almost
> --
> 2.38.1
>

LGTM.

Reviewed-by: Noah Goldstein <goldstein.w.n@gmail.com>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-12-02  1:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-02  0:36 [PATCH] x86-64 strncpy: Properly handle the length parameter [BZ# 29839] H.J. Lu
2022-12-02  1:38 ` Noah Goldstein

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).