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