public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Noah Goldstein <goldstein.w.n@gmail.com>
To: libc-alpha@sourceware.org
Subject: [PATCH v4] x86: Fallback {str|wcs}cmp RTM in the ncmp overflow case [BZ #28896]
Date: Thu, 17 Feb 2022 13:12:26 -0600	[thread overview]
Message-ID: <20220217191226.2960676-1-goldstein.w.n@gmail.com> (raw)
In-Reply-To: <20220215162751.281955-1-goldstein.w.n@gmail.com>

In the overflow fallback strncmp-avx2-rtm and wcsncmp-avx2-rtm would
call strcmp-avx2 and wcsncmp-avx2 respectively. This would have
not checks around vzeroupper and would trigger spurious
aborts. This commit fixes that.

test-strcmp, test-strncmp, test-wcscmp, and test-wcsncmp all pass on
AVX2 machines with and without RTM.
---
 sysdeps/x86/Makefile                        |  2 +-
 sysdeps/x86/tst-strncmp-rtm.c               | 18 +++++++++++++++++-
 sysdeps/x86_64/multiarch/strcmp-avx2.S      |  8 ++------
 sysdeps/x86_64/multiarch/strncmp-avx2-rtm.S |  1 +
 sysdeps/x86_64/multiarch/strncmp-avx2.S     |  1 +
 sysdeps/x86_64/multiarch/wcsncmp-avx2-rtm.S |  2 +-
 sysdeps/x86_64/multiarch/wcsncmp-avx2.S     |  2 +-
 7 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile
index 6cf708335c..d110f7b7f2 100644
--- a/sysdeps/x86/Makefile
+++ b/sysdeps/x86/Makefile
@@ -109,7 +109,7 @@ CFLAGS-tst-memset-rtm.c += -mrtm
 CFLAGS-tst-strchr-rtm.c += -mrtm
 CFLAGS-tst-strcpy-rtm.c += -mrtm
 CFLAGS-tst-strlen-rtm.c += -mrtm
-CFLAGS-tst-strncmp-rtm.c += -mrtm
+CFLAGS-tst-strncmp-rtm.c += -mrtm -Wno-error
 CFLAGS-tst-strrchr-rtm.c += -mrtm
 endif
 
diff --git a/sysdeps/x86/tst-strncmp-rtm.c b/sysdeps/x86/tst-strncmp-rtm.c
index 09ed6fa0d6..ba8a7ba92b 100644
--- a/sysdeps/x86/tst-strncmp-rtm.c
+++ b/sysdeps/x86/tst-strncmp-rtm.c
@@ -16,6 +16,8 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
+#include <stdint.h>
+#include <limits.h>
 #include <tst-string-rtm.h>
 
 #define LOOP 3000
@@ -45,8 +47,22 @@ function (void)
     return 1;
 }
 
+__attribute__ ((noinline, noclone))
+static int
+function_overflow (void)
+{
+  if (strncmp (string1, string2, SSIZE_MAX) == 0)
+    return 0;
+  else
+    return 1;
+}
+
 static int
 do_test (void)
 {
-  return do_test_1 ("strncmp", LOOP, prepare, function);
+  int status = do_test_1 ("strncmp", LOOP, prepare, function);
+  if (status != EXIT_SUCCESS)
+    return status;
+  status = do_test_1 ("strncmp", LOOP, prepare, function_overflow);
+  return status;
 }
diff --git a/sysdeps/x86_64/multiarch/strcmp-avx2.S b/sysdeps/x86_64/multiarch/strcmp-avx2.S
index 07a5a2c889..52ff5ad724 100644
--- a/sysdeps/x86_64/multiarch/strcmp-avx2.S
+++ b/sysdeps/x86_64/multiarch/strcmp-avx2.S
@@ -193,10 +193,10 @@ L(ret_zero):
 	.p2align 4,, 5
 L(one_or_less):
 	jb	L(ret_zero)
-#  ifdef USE_AS_WCSCMP
 	/* 'nbe' covers the case where length is negative (large
 	   unsigned).  */
-	jnbe	__wcscmp_avx2
+	jnbe	OVERFLOW_STRCMP
+#  ifdef USE_AS_WCSCMP
 	movl	(%rdi), %edx
 	xorl	%eax, %eax
 	cmpl	(%rsi), %edx
@@ -205,10 +205,6 @@ L(one_or_less):
 	negl	%eax
 	orl	$1, %eax
 #  else
-	/* 'nbe' covers the case where length is negative (large
-	   unsigned).  */
-
-	jnbe	__strcmp_avx2
 	movzbl	(%rdi), %eax
 	movzbl	(%rsi), %ecx
 	subl	%ecx, %eax
diff --git a/sysdeps/x86_64/multiarch/strncmp-avx2-rtm.S b/sysdeps/x86_64/multiarch/strncmp-avx2-rtm.S
index 37d1224bb9..68bad365ba 100644
--- a/sysdeps/x86_64/multiarch/strncmp-avx2-rtm.S
+++ b/sysdeps/x86_64/multiarch/strncmp-avx2-rtm.S
@@ -1,3 +1,4 @@
 #define STRCMP	__strncmp_avx2_rtm
 #define USE_AS_STRNCMP 1
+#define OVERFLOW_STRCMP	__strcmp_avx2_rtm
 #include "strcmp-avx2-rtm.S"
diff --git a/sysdeps/x86_64/multiarch/strncmp-avx2.S b/sysdeps/x86_64/multiarch/strncmp-avx2.S
index 1678bcc235..f138e9f1fd 100644
--- a/sysdeps/x86_64/multiarch/strncmp-avx2.S
+++ b/sysdeps/x86_64/multiarch/strncmp-avx2.S
@@ -1,3 +1,4 @@
 #define STRCMP	__strncmp_avx2
 #define USE_AS_STRNCMP 1
+#define OVERFLOW_STRCMP __strcmp_avx2
 #include "strcmp-avx2.S"
diff --git a/sysdeps/x86_64/multiarch/wcsncmp-avx2-rtm.S b/sysdeps/x86_64/multiarch/wcsncmp-avx2-rtm.S
index 4e88c70cc6..f467582cbe 100644
--- a/sysdeps/x86_64/multiarch/wcsncmp-avx2-rtm.S
+++ b/sysdeps/x86_64/multiarch/wcsncmp-avx2-rtm.S
@@ -1,5 +1,5 @@
 #define STRCMP __wcsncmp_avx2_rtm
 #define USE_AS_STRNCMP 1
 #define USE_AS_WCSCMP 1
-
+#define OVERFLOW_STRCMP	__wcscmp_avx2_rtm
 #include "strcmp-avx2-rtm.S"
diff --git a/sysdeps/x86_64/multiarch/wcsncmp-avx2.S b/sysdeps/x86_64/multiarch/wcsncmp-avx2.S
index 4fa1de4d3f..e9ede522b8 100644
--- a/sysdeps/x86_64/multiarch/wcsncmp-avx2.S
+++ b/sysdeps/x86_64/multiarch/wcsncmp-avx2.S
@@ -1,5 +1,5 @@
 #define STRCMP __wcsncmp_avx2
 #define USE_AS_STRNCMP 1
 #define USE_AS_WCSCMP 1
-
+#define OVERFLOW_STRCMP	__wcscmp_avx2
 #include "strcmp-avx2.S"
-- 
2.25.1


  parent reply	other threads:[~2022-02-17 19:12 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-15 16:27 [PATCH v1] " Noah Goldstein
2022-02-15 16:29 ` H.J. Lu
2022-02-15 16:51   ` Noah Goldstein
2022-02-15 16:59     ` H.J. Lu
2022-02-15 17:06       ` Noah Goldstein
2022-02-15 18:49         ` H.J. Lu
2022-02-16  8:10           ` Noah Goldstein
2022-02-15 16:49 ` [PATCH v2] " Noah Goldstein
2022-02-16  8:09 ` [PATCH v3] " Noah Goldstein
2022-02-16 13:17   ` H.J. Lu
2022-02-16 14:08     ` Carlos O'Donell
2022-02-16 14:26       ` H.J. Lu
2022-02-17 19:15         ` Noah Goldstein
2022-02-17 19:12 ` Noah Goldstein [this message]
2022-02-17 19:15 ` [PATCH v5] " Noah Goldstein
2022-02-17 19:20   ` H.J. Lu
2022-05-25 19:51     ` Sunil Pandey
2022-05-25 19:56       ` H.J. Lu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220217191226.2960676-1-goldstein.w.n@gmail.com \
    --to=goldstein.w.n@gmail.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).