190885
From 8f2267e67ea924c949ca4a70a0d2c484d81a8fc9 Mon Sep 17 00:00:00 2001
190885
From: Noah Goldstein <goldstein.w.n@gmail.com>
190885
Date: Tue, 15 Feb 2022 08:18:15 -0600
190885
Subject: [PATCH] x86: Fallback {str|wcs}cmp RTM in the ncmp overflow case [BZ
190885
 #28896]
190885
190885
In the overflow fallback strncmp-avx2-rtm and wcsncmp-avx2-rtm would
190885
call strcmp-avx2 and wcscmp-avx2 respectively. This would have
190885
not checks around vzeroupper and would trigger spurious
190885
aborts. This commit fixes that.
190885
190885
test-strcmp, test-strncmp, test-wcscmp, and test-wcsncmp all pass on
190885
AVX2 machines with and without RTM.
190885
190885
Co-authored-by: H.J. Lu <hjl.tools@gmail.com>
190885
190885
(cherry picked from commit c6272098323153db373f2986c67786ea8c85f1cf)
190885
---
190885
 sysdeps/x86/Makefile                        |  2 +-
190885
 sysdeps/x86/tst-strncmp-rtm.c               | 17 ++++++++++++++++-
190885
 sysdeps/x86_64/multiarch/strcmp-avx2.S      |  2 +-
190885
 sysdeps/x86_64/multiarch/strncmp-avx2-rtm.S |  1 +
190885
 sysdeps/x86_64/multiarch/strncmp-avx2.S     |  1 +
190885
 sysdeps/x86_64/multiarch/wcsncmp-avx2-rtm.S |  2 +-
190885
 sysdeps/x86_64/multiarch/wcsncmp-avx2.S     |  2 +-
190885
 7 files changed, 22 insertions(+), 5 deletions(-)
190885
190885
diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile
190885
index 5be71ada..2d814915 100644
190885
--- a/sysdeps/x86/Makefile
190885
+++ b/sysdeps/x86/Makefile
190885
@@ -38,7 +38,7 @@ CFLAGS-tst-memset-rtm.c += -mrtm
190885
 CFLAGS-tst-strchr-rtm.c += -mrtm
190885
 CFLAGS-tst-strcpy-rtm.c += -mrtm
190885
 CFLAGS-tst-strlen-rtm.c += -mrtm
190885
-CFLAGS-tst-strncmp-rtm.c += -mrtm
190885
+CFLAGS-tst-strncmp-rtm.c += -mrtm -Wno-error
190885
 CFLAGS-tst-strrchr-rtm.c += -mrtm
190885
 endif
190885
 
190885
diff --git a/sysdeps/x86/tst-strncmp-rtm.c b/sysdeps/x86/tst-strncmp-rtm.c
190885
index 236ad951..4d0004b5 100644
190885
--- a/sysdeps/x86/tst-strncmp-rtm.c
190885
+++ b/sysdeps/x86/tst-strncmp-rtm.c
190885
@@ -16,6 +16,7 @@
190885
    License along with the GNU C Library; if not, see
190885
    <https://www.gnu.org/licenses/>.  */
190885
 
190885
+#include <stdint.h>
190885
 #include <tst-string-rtm.h>
190885
 
190885
 #define LOOP 3000
190885
@@ -45,8 +46,22 @@ function (void)
190885
     return 1;
190885
 }
190885
 
190885
+__attribute__ ((noinline, noclone))
190885
+static int
190885
+function_overflow (void)
190885
+{
190885
+  if (strncmp (string1, string2, SIZE_MAX) == 0)
190885
+    return 0;
190885
+  else
190885
+    return 1;
190885
+}
190885
+
190885
 static int
190885
 do_test (void)
190885
 {
190885
-  return do_test_1 ("strncmp", LOOP, prepare, function);
190885
+  int status = do_test_1 ("strncmp", LOOP, prepare, function);
190885
+  if (status != EXIT_SUCCESS)
190885
+    return status;
190885
+  status = do_test_1 ("strncmp", LOOP, prepare, function_overflow);
190885
+  return status;
190885
 }
190885
diff --git a/sysdeps/x86_64/multiarch/strcmp-avx2.S b/sysdeps/x86_64/multiarch/strcmp-avx2.S
190885
index 5d1c9d90..433ae047 100644
190885
--- a/sysdeps/x86_64/multiarch/strcmp-avx2.S
190885
+++ b/sysdeps/x86_64/multiarch/strcmp-avx2.S
190885
@@ -95,7 +95,7 @@ ENTRY (STRCMP)
190885
 	   length to bound a valid memory region. In these cases just use
190885
 	   'wcscmp'.  */
190885
 	shrq	$56, %rcx
190885
-	jnz	__wcscmp_avx2
190885
+	jnz	OVERFLOW_STRCMP
190885
 #  endif
190885
 	/* Convert units: from wide to byte char.  */
190885
 	shl	$2, %RDX_LP
190885
diff --git a/sysdeps/x86_64/multiarch/strncmp-avx2-rtm.S b/sysdeps/x86_64/multiarch/strncmp-avx2-rtm.S
190885
index 37d1224b..68bad365 100644
190885
--- a/sysdeps/x86_64/multiarch/strncmp-avx2-rtm.S
190885
+++ b/sysdeps/x86_64/multiarch/strncmp-avx2-rtm.S
190885
@@ -1,3 +1,4 @@
190885
 #define STRCMP	__strncmp_avx2_rtm
190885
 #define USE_AS_STRNCMP 1
190885
+#define OVERFLOW_STRCMP	__strcmp_avx2_rtm
190885
 #include "strcmp-avx2-rtm.S"
190885
diff --git a/sysdeps/x86_64/multiarch/strncmp-avx2.S b/sysdeps/x86_64/multiarch/strncmp-avx2.S
190885
index 1678bcc2..f138e9f1 100644
190885
--- a/sysdeps/x86_64/multiarch/strncmp-avx2.S
190885
+++ b/sysdeps/x86_64/multiarch/strncmp-avx2.S
190885
@@ -1,3 +1,4 @@
190885
 #define STRCMP	__strncmp_avx2
190885
 #define USE_AS_STRNCMP 1
190885
+#define OVERFLOW_STRCMP __strcmp_avx2
190885
 #include "strcmp-avx2.S"
190885
diff --git a/sysdeps/x86_64/multiarch/wcsncmp-avx2-rtm.S b/sysdeps/x86_64/multiarch/wcsncmp-avx2-rtm.S
190885
index 4e88c70c..f467582c 100644
190885
--- a/sysdeps/x86_64/multiarch/wcsncmp-avx2-rtm.S
190885
+++ b/sysdeps/x86_64/multiarch/wcsncmp-avx2-rtm.S
190885
@@ -1,5 +1,5 @@
190885
 #define STRCMP __wcsncmp_avx2_rtm
190885
 #define USE_AS_STRNCMP 1
190885
 #define USE_AS_WCSCMP 1
190885
-
190885
+#define OVERFLOW_STRCMP	__wcscmp_avx2_rtm
190885
 #include "strcmp-avx2-rtm.S"
190885
diff --git a/sysdeps/x86_64/multiarch/wcsncmp-avx2.S b/sysdeps/x86_64/multiarch/wcsncmp-avx2.S
190885
index 4fa1de4d..e9ede522 100644
190885
--- a/sysdeps/x86_64/multiarch/wcsncmp-avx2.S
190885
+++ b/sysdeps/x86_64/multiarch/wcsncmp-avx2.S
190885
@@ -1,5 +1,5 @@
190885
 #define STRCMP __wcsncmp_avx2
190885
 #define USE_AS_STRNCMP 1
190885
 #define USE_AS_WCSCMP 1
190885
-
190885
+#define OVERFLOW_STRCMP	__wcscmp_avx2
190885
 #include "strcmp-avx2.S"
190885
-- 
190885
GitLab
190885