076f82
commit d093b677c36ef4b360bf30483b68b95d9f0ad1d2
076f82
Author: Noah Goldstein <goldstein.w.n@gmail.com>
076f82
Date:   Fri Feb 18 14:19:15 2022 -0600
076f82
076f82
    x86: Test wcscmp RTM in the wcsncmp 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
    Reviewed-by: H.J. Lu <hjl.tools@gmail.com>
076f82
    
076f82
    (cherry picked from commit 7835d611af0854e69a0c71e3806f8fe379282d6f)
076f82
076f82
diff --git a/sysdeps/x86/Makefile b/sysdeps/x86/Makefile
076f82
index af934d6ccf1fa337..cd94e683afd5b4a4 100644
076f82
--- a/sysdeps/x86/Makefile
076f82
+++ b/sysdeps/x86/Makefile
076f82
@@ -95,7 +95,9 @@ tests += \
076f82
   tst-strcpy-rtm \
076f82
   tst-strlen-rtm \
076f82
   tst-strncmp-rtm \
076f82
-  tst-strrchr-rtm
076f82
+  tst-strrchr-rtm \
076f82
+  tst-wcsncmp-rtm \
076f82
+# tests
076f82
 
076f82
 CFLAGS-tst-memchr-rtm.c += -mrtm
076f82
 CFLAGS-tst-memcmp-rtm.c += -mrtm
076f82
@@ -107,6 +109,7 @@ CFLAGS-tst-strcpy-rtm.c += -mrtm
076f82
 CFLAGS-tst-strlen-rtm.c += -mrtm
076f82
 CFLAGS-tst-strncmp-rtm.c += -mrtm -Wno-error
076f82
 CFLAGS-tst-strrchr-rtm.c += -mrtm
076f82
+CFLAGS-tst-wcsncmp-rtm.c += -mrtm -Wno-error
076f82
 endif
076f82
 
076f82
 ifneq ($(enable-cet),no)
076f82
diff --git a/sysdeps/x86/tst-strncmp-rtm.c b/sysdeps/x86/tst-strncmp-rtm.c
076f82
index 4d0004b58aae428d..4e9f094f39c72f67 100644
076f82
--- a/sysdeps/x86/tst-strncmp-rtm.c
076f82
+++ b/sysdeps/x86/tst-strncmp-rtm.c
076f82
@@ -19,18 +19,32 @@
076f82
 #include <stdint.h>
076f82
 #include <tst-string-rtm.h>
076f82
 
076f82
+#ifdef WIDE
076f82
+# define CHAR wchar_t
076f82
+# define MEMSET wmemset
076f82
+# define STRNCMP wcsncmp
076f82
+# define TEST_NAME wcsncmp
076f82
+#else /* !WIDE */
076f82
+# define CHAR char
076f82
+# define MEMSET memset
076f82
+# define STRNCMP strncmp
076f82
+# define TEST_NAME strncmp
076f82
+#endif /* !WIDE */
076f82
+
076f82
+
076f82
+
076f82
 #define LOOP 3000
076f82
 #define STRING_SIZE 1024
076f82
-char string1[STRING_SIZE];
076f82
-char string2[STRING_SIZE];
076f82
+CHAR string1[STRING_SIZE];
076f82
+CHAR string2[STRING_SIZE];
076f82
 
076f82
 __attribute__ ((noinline, noclone))
076f82
 static int
076f82
 prepare (void)
076f82
 {
076f82
-  memset (string1, 'a', STRING_SIZE - 1);
076f82
-  memset (string2, 'a', STRING_SIZE - 1);
076f82
-  if (strncmp (string1, string2, STRING_SIZE) == 0)
076f82
+  MEMSET (string1, 'a', STRING_SIZE - 1);
076f82
+  MEMSET (string2, 'a', STRING_SIZE - 1);
076f82
+  if (STRNCMP (string1, string2, STRING_SIZE) == 0)
076f82
     return EXIT_SUCCESS;
076f82
   else
076f82
     return EXIT_FAILURE;
076f82
@@ -40,7 +54,7 @@ __attribute__ ((noinline, noclone))
076f82
 static int
076f82
 function (void)
076f82
 {
076f82
-  if (strncmp (string1, string2, STRING_SIZE) == 0)
076f82
+  if (STRNCMP (string1, string2, STRING_SIZE) == 0)
076f82
     return 0;
076f82
   else
076f82
     return 1;
076f82
@@ -50,7 +64,7 @@ __attribute__ ((noinline, noclone))
076f82
 static int
076f82
 function_overflow (void)
076f82
 {
076f82
-  if (strncmp (string1, string2, SIZE_MAX) == 0)
076f82
+  if (STRNCMP (string1, string2, SIZE_MAX) == 0)
076f82
     return 0;
076f82
   else
076f82
     return 1;
076f82
@@ -59,9 +73,9 @@ function_overflow (void)
076f82
 static int
076f82
 do_test (void)
076f82
 {
076f82
-  int status = do_test_1 ("strncmp", LOOP, prepare, function);
076f82
+  int status = do_test_1 (TEST_NAME, LOOP, prepare, function);
076f82
   if (status != EXIT_SUCCESS)
076f82
     return status;
076f82
-  status = do_test_1 ("strncmp", LOOP, prepare, function_overflow);
076f82
+  status = do_test_1 (TEST_NAME, LOOP, prepare, function_overflow);
076f82
   return status;
076f82
 }
076f82
diff --git a/sysdeps/x86/tst-wcsncmp-rtm.c b/sysdeps/x86/tst-wcsncmp-rtm.c
076f82
new file mode 100644
076f82
index 0000000000000000..bad3b863782c5e56
076f82
--- /dev/null
076f82
+++ b/sysdeps/x86/tst-wcsncmp-rtm.c
076f82
@@ -0,0 +1,21 @@
076f82
+/* Test case for wcsncmp inside a transactionally executing RTM region.
076f82
+   Copyright (C) 2022 Free Software Foundation, Inc.
076f82
+   This file is part of the GNU C Library.
076f82
+
076f82
+   The GNU C Library is free software; you can redistribute it and/or
076f82
+   modify it under the terms of the GNU Lesser General Public
076f82
+   License as published by the Free Software Foundation; either
076f82
+   version 2.1 of the License, or (at your option) any later version.
076f82
+
076f82
+   The GNU C Library is distributed in the hope that it will be useful,
076f82
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
076f82
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
076f82
+   Lesser General Public License for more details.
076f82
+
076f82
+   You should have received a copy of the GNU Lesser General Public
076f82
+   License along with the GNU C Library; if not, see
076f82
+   <https://www.gnu.org/licenses/>.  */
076f82
+
076f82
+#define WIDE 1
076f82
+#include <wchar.h>
076f82
+#include "tst-strncmp-rtm.c"