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