5f7b84
commit bfdb731438206b0f70fe7afa890681155c30b419
5f7b84
Author: Stefan Liebler <stli@linux.ibm.com>
5f7b84
Date:   Wed Nov 27 12:35:40 2019 +0100
5f7b84
5f7b84
    S390: Fix handling of needles crossing a page in strstr z15 ifunc-variant. [BZ #25226]
5f7b84
    
5f7b84
    If the specified needle crosses a page-boundary, the s390-z15 ifunc variant of
5f7b84
    strstr truncates the needle which results in invalid results.
5f7b84
    
5f7b84
    This is fixed by loading the needle beyond the page boundary to v18 instead of v16.
5f7b84
    The bug is sometimes observable in test-strstr.c in check1 and check2 as the
5f7b84
    haystack and needle is stored on stack. Thus the needle can be on a page boundary.
5f7b84
    
5f7b84
    check2 is now extended to test haystack / needles located on stack, at end of page
5f7b84
    and on two pages.
5f7b84
    
5f7b84
    This bug was introduced with commit 6f47401bd5fc71209219779a0426170a9a7395b0
5f7b84
    ("S390: Add arch13 strstr ifunc variant.") and is already released in glibc 2.30.
5f7b84
5f7b84
diff --git a/string/test-strstr.c b/string/test-strstr.c
5f7b84
index 5861b01b73e4c315..e9e14c1ee605516e 100644
5f7b84
--- a/string/test-strstr.c
5f7b84
+++ b/string/test-strstr.c
5f7b84
@@ -138,16 +138,45 @@ check1 (void)
5f7b84
 static void
5f7b84
 check2 (void)
5f7b84
 {
5f7b84
-  const char s1[] = ", enable_static, \0, enable_shared, ";
5f7b84
+  const char s1_stack[] = ", enable_static, \0, enable_shared, ";
5f7b84
+  const size_t s1_byte_count = 18;
5f7b84
+  const char *s2_stack = &(s1_stack[s1_byte_count]);
5f7b84
+  const size_t s2_byte_count = 18;
5f7b84
   char *exp_result;
5f7b84
-  char *s2 = (void *) buf1 + page_size - 18;
5f7b84
+  const size_t page_size_real = getpagesize ();
5f7b84
 
5f7b84
-  strcpy (s2, s1);
5f7b84
-  exp_result = stupid_strstr (s1, s1 + 18);
5f7b84
+  /* Haystack at end of page.  The following page is protected.  */
5f7b84
+  char *s1_page_end = (void *) buf1 + page_size - s1_byte_count;
5f7b84
+  strcpy (s1_page_end, s1_stack);
5f7b84
+
5f7b84
+  /* Haystack which crosses a page boundary.
5f7b84
+     Note: page_size is at least 2 * getpagesize.  See test_init.  */
5f7b84
+  char *s1_page_cross = (void *) buf1 + page_size_real - 8;
5f7b84
+  strcpy (s1_page_cross, s1_stack);
5f7b84
+
5f7b84
+  /* Needle at end of page.  The following page is protected.  */
5f7b84
+  char *s2_page_end = (void *) buf2 + page_size - s2_byte_count;
5f7b84
+  strcpy (s2_page_end, s2_stack);
5f7b84
+
5f7b84
+  /* Needle which crosses a page boundary.
5f7b84
+     Note: page_size is at least 2 * getpagesize.  See test_init.  */
5f7b84
+  char *s2_page_cross = (void *) buf2 + page_size_real - 8;
5f7b84
+  strcpy (s2_page_cross, s2_stack);
5f7b84
+
5f7b84
+  exp_result = stupid_strstr (s1_stack, s2_stack);
5f7b84
   FOR_EACH_IMPL (impl, 0)
5f7b84
     {
5f7b84
-      check_result (impl, s1, s1 + 18, exp_result);
5f7b84
-      check_result (impl, s2, s1 + 18, exp_result);
5f7b84
+      check_result (impl, s1_stack, s2_stack, exp_result);
5f7b84
+      check_result (impl, s1_stack, s2_page_end, exp_result);
5f7b84
+      check_result (impl, s1_stack, s2_page_cross, exp_result);
5f7b84
+
5f7b84
+      check_result (impl, s1_page_end, s2_stack, exp_result);
5f7b84
+      check_result (impl, s1_page_end, s2_page_end, exp_result);
5f7b84
+      check_result (impl, s1_page_end, s2_page_cross, exp_result);
5f7b84
+
5f7b84
+      check_result (impl, s1_page_cross, s2_stack, exp_result);
5f7b84
+      check_result (impl, s1_page_cross, s2_page_end, exp_result);
5f7b84
+      check_result (impl, s1_page_cross, s2_page_cross, exp_result);
5f7b84
     }
5f7b84
 }
5f7b84
 
5f7b84
diff --git a/sysdeps/s390/strstr-arch13.S b/sysdeps/s390/strstr-arch13.S
5f7b84
index 929b026adfeba740..faa969849e09c2e1 100644
5f7b84
--- a/sysdeps/s390/strstr-arch13.S
5f7b84
+++ b/sysdeps/s390/strstr-arch13.S
5f7b84
@@ -164,7 +164,7 @@ ENTRY(STRSTR_ARCH13)
5f7b84
 	vfenezb	%v19,%v18,%v18	/* Search zero in loaded needle bytes.  */
5f7b84
 	veclb	%v19,%v21	/* Zero index <= max loaded byte index?  */
5f7b84
 	jle	.Lneedle_loaded	/* -> v18 contains full needle.  */
5f7b84
-	vl	%v16,0(%r3)	/* Load needle beyond page boundary.  */
5f7b84
+	vl	%v18,0(%r3)	/* Load needle beyond page boundary.  */
5f7b84
 	vfenezb	%v19,%v18,%v18
5f7b84
 	j	.Lneedle_loaded
5f7b84
 END(STRSTR_ARCH13)