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