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