public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH v9] string: test strncasecmp and strncpy near page boundaries
@ 2020-08-21 15:10 Raphael Moreira Zinsly
  2020-08-24 21:31 ` Paul E Murphy
  2020-08-25 19:45 ` Lucas A. M. Magalhaes
  0 siblings, 2 replies; 4+ messages in thread
From: Raphael Moreira Zinsly @ 2020-08-21 15:10 UTC (permalink / raw)
  To: libc-alpha; +Cc: murphyp, Raphael Moreira Zinsly

Changes since v8:
	- Title reworded.
	- Fixed spelling on commit message.
string/test-strncpy.c
	- Removed unnecessary parenthesis.
	- Changed 'n' to be maxoffset + (maxoffset - off2)
	  in order to always touch the s2 page boundary.

--- >8 ---

Add tests to check if strings placed at page boundaries are
handled correctly by strncasecmp and strncpy similar to tests
for strncmp and strnlen.
---
 string/test-strncasecmp.c | 43 +++++++++++++++++++++++++++++++++++++++
 string/test-strncpy.c     | 35 +++++++++++++++++++++++++++++++
 2 files changed, 78 insertions(+)

diff --git a/string/test-strncasecmp.c b/string/test-strncasecmp.c
index 6a9c27beae..502222ed1d 100644
--- a/string/test-strncasecmp.c
+++ b/string/test-strncasecmp.c
@@ -137,6 +137,48 @@ do_test (size_t align1, size_t align2, size_t n, size_t len, int max_char,
     do_one_test (impl, s1, s2, n, exp_result);
 }
 
+static void
+do_page_tests (void)
+{
+  char *s1, *s2;
+  int exp_result;
+  const size_t maxoffset = 64;
+
+  s1 = (char *) buf1 + BUF1PAGES * page_size - maxoffset;
+  memset (s1, 'a', maxoffset - 1);
+  s1[maxoffset - 1] = '\0';
+
+  s2 = (char *) buf2 + page_size - maxoffset;
+  memset (s2, 'a', maxoffset - 1);
+  s2[maxoffset - 1] = '\0';
+
+  /* At this point s1 and s2 point to distinct memory regions containing
+     "aa..." with size of 63 plus '\0'.  Also, both strings are bounded to a
+     page with read/write access and the next page is protected with PROT_NONE
+     (meaning that any access outside of the page regions will trigger an
+     invalid memory access).
+
+     The loop checks for all possible offsets up to maxoffset for both
+     inputs with a size larger than the string (so memory access outside
+     the expected memory regions might trigger invalid access).  */
+
+  for (size_t off1 = 0; off1 < maxoffset; off1++)
+    {
+      for (size_t off2 = 0; off2 < maxoffset; off2++)
+	{
+	  exp_result = (off1 == off2)
+			? 0
+			: off1 < off2
+			  ? 'a'
+			  : -'a';
+
+	  FOR_EACH_IMPL (impl, 0)
+	    check_result (impl, s1 + off1, s2 + off2, maxoffset + 1,
+			  exp_result);
+	}
+    }
+}
+
 static void
 do_random_tests (void)
 {
@@ -334,6 +376,7 @@ test_locale (const char *locale)
     }
 
   do_random_tests ();
+  do_page_tests ();
 }
 
 int
diff --git a/string/test-strncpy.c b/string/test-strncpy.c
index c978753ad8..e902c1764e 100644
--- a/string/test-strncpy.c
+++ b/string/test-strncpy.c
@@ -155,6 +155,40 @@ do_test (size_t align1, size_t align2, size_t len, size_t n, int max_char)
     do_one_test (impl, s2, s1, len, n);
 }
 
+static void
+do_page_tests (void)
+{
+  CHAR *s1, *s2;
+  const size_t maxoffset = 64;
+
+  /* Put s1 at the maxoffset from the edge of buf1's last page.  */
+  s1 = (CHAR *) buf1 + BUF1PAGES * page_size / sizeof(CHAR) - maxoffset;
+  /* s2 needs room to put a string with size of maxoffset + 1 at s2 +
+     (maxoffset - 1).  */
+  s2 = (CHAR *) buf2 + page_size / sizeof(CHAR) - maxoffset * 2;
+
+  MEMSET (s1, 'a', maxoffset - 1);
+  s1[maxoffset - 1] = '\0';
+
+  /* Both strings are bounded to a page with read/write access and the next
+     page is protected with PROT_NONE (meaning that any access outside of the
+     page regions will trigger an invalid memory access).
+
+     The loop copies the string s1 for all possible offsets up to maxoffset
+     for both inputs with a size larger than s1 (so memory access outside the
+     expected memory regions might trigger invalid access).  */
+
+  for (size_t off1 = 0; off1 < maxoffset; off1++)
+    {
+      for (size_t off2 = 0; off2 < maxoffset; off2++)
+	{
+	  FOR_EACH_IMPL (impl, 0)
+	    do_one_test (impl, s2 + off2, s1 + off1, maxoffset - off1 - 1,
+			 maxoffset + (maxoffset - off2));
+	}
+    }
+}
+
 static void
 do_random_tests (void)
 {
@@ -317,6 +351,7 @@ test_main (void)
     }
 
   do_random_tests ();
+  do_page_tests ();
   return ret;
 }
 
-- 
2.26.2


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v9] string: test strncasecmp and strncpy near page boundaries
  2020-08-21 15:10 [PATCH v9] string: test strncasecmp and strncpy near page boundaries Raphael Moreira Zinsly
@ 2020-08-24 21:31 ` Paul E Murphy
  2020-08-25 19:45 ` Lucas A. M. Magalhaes
  1 sibling, 0 replies; 4+ messages in thread
From: Paul E Murphy @ 2020-08-24 21:31 UTC (permalink / raw)
  To: Raphael Moreira Zinsly, libc-alpha



On 8/21/20 10:10 AM, Raphael Moreira Zinsly wrote:
> Changes since v8:
> 	- Title reworded.
> 	- Fixed spelling on commit message.
> string/test-strncpy.c
> 	- Removed unnecessary parenthesis.
> 	- Changed 'n' to be maxoffset + (maxoffset - off2)
> 	  in order to always touch the s2 page boundary.

This version LGTM.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v9] string: test strncasecmp and strncpy near page boundaries
  2020-08-21 15:10 [PATCH v9] string: test strncasecmp and strncpy near page boundaries Raphael Moreira Zinsly
  2020-08-24 21:31 ` Paul E Murphy
@ 2020-08-25 19:45 ` Lucas A. M. Magalhaes
  2020-08-26 20:58   ` Paul E Murphy
  1 sibling, 1 reply; 4+ messages in thread
From: Lucas A. M. Magalhaes @ 2020-08-25 19:45 UTC (permalink / raw)
  To: Raphael Moreira Zinsly, libc-alpha; +Cc: murphyp, Raphael Moreira Zinsly

Quoting Raphael Moreira Zinsly via Libc-alpha (2020-08-21 12:10:22)
> Changes since v8:
>         - Title reworded.
>         - Fixed spelling on commit message.
> string/test-strncpy.c
>         - Removed unnecessary parenthesis.
>         - Changed 'n' to be maxoffset + (maxoffset - off2)
>           in order to always touch the s2 page boundary.
> 

LGTM. Tests passed on a POWER8.

--
Lucas A. M. Magalhães

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v9] string: test strncasecmp and strncpy near page boundaries
  2020-08-25 19:45 ` Lucas A. M. Magalhaes
@ 2020-08-26 20:58   ` Paul E Murphy
  0 siblings, 0 replies; 4+ messages in thread
From: Paul E Murphy @ 2020-08-26 20:58 UTC (permalink / raw)
  To: Lucas A. M. Magalhaes, Raphael Moreira Zinsly, libc-alpha



On 8/25/20 2:45 PM, Lucas A. M. Magalhaes wrote:
> Quoting Raphael Moreira Zinsly via Libc-alpha (2020-08-21 12:10:22)
>> Changes since v8:
>>          - Title reworded.
>>          - Fixed spelling on commit message.
>> string/test-strncpy.c
>>          - Removed unnecessary parenthesis.
>>          - Changed 'n' to be maxoffset + (maxoffset - off2)
>>            in order to always touch the s2 page boundary.
>>
> 
> LGTM. Tests passed on a POWER8.
> 
> --
> Lucas A. M. Magalhães
>

Thanks, pushed to master with verification on x86-64.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-08-26 20:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-21 15:10 [PATCH v9] string: test strncasecmp and strncpy near page boundaries Raphael Moreira Zinsly
2020-08-24 21:31 ` Paul E Murphy
2020-08-25 19:45 ` Lucas A. M. Magalhaes
2020-08-26 20:58   ` Paul E Murphy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).