test-rawmemchr: make more robust
authorEric Blake <eblake@redhat.com>
Tue, 14 Sep 2010 22:42:39 +0000 (16:42 -0600)
committerEric Blake <eblake@redhat.com>
Tue, 14 Sep 2010 22:55:31 +0000 (16:55 -0600)
* modules/rawmemchr-tests (Files): Add zerosize-ptr.h, mmap-anon.m4.
(Depends-on, configure.ac): Add needed prerequisites to use it.
* modules/memchr-tests (Files, Depends-on, configure.ac):
Likewise, to avoid implicit reliance on memchr module prereqs.
* tests/test-memchr.c (main): Ensure proper masking.
* tests/test-rawmemchr.c (main): Likewise.  Detect oversized
reads.

Signed-off-by: Eric Blake <eblake@redhat.com>
ChangeLog
modules/memchr-tests
modules/rawmemchr-tests
tests/test-memchr.c
tests/test-rawmemchr.c

index f4bdc7f..4b259da 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
 2010-09-14  Eric Blake  <eblake@redhat.com>
 
+       test-rawmemchr: make more robust
+       * modules/rawmemchr-tests (Files): Add zerosize-ptr.h, mmap-anon.m4.
+       (Depends-on, configure.ac): Add needed prerequisites to use it.
+       * modules/memchr-tests (Files, Depends-on, configure.ac):
+       Likewise, to avoid implicit reliance on memchr module prereqs.
+       * tests/test-memchr.c (main): Ensure proper masking.
+       * tests/test-rawmemchr.c (main): Likewise.  Detect oversized
+       reads.
+
        memchr: detect glibc Alpha bug
        Avoids http://sourceware.org/bugzilla/show_bug.cgi?id=12019.
        * m4/memchr.m4 (gl_FUNC_MEMCHR): Detect glibc 2.11.2 failure on
index 4fafa8d..3022471 100644 (file)
@@ -3,10 +3,17 @@ tests/test-memchr.c
 tests/zerosize-ptr.h
 tests/signature.h
 tests/macros.h
+m4/mmap-anon.m4
 
 Depends-on:
+extensions
+getpagesize
 
 configure.ac:
+dnl Check for prerequisites for memory fence checks.
+gl_FUNC_MMAP_ANON
+AC_CHECK_HEADERS_ONCE([sys/mman.h])
+AC_CHECK_FUNCS_ONCE([mprotect])
 
 Makefile.am:
 TESTS += test-memchr
index bca145e..f2d4c62 100644 (file)
@@ -1,11 +1,19 @@
 Files:
 tests/test-rawmemchr.c
+tests/zerosize-ptr.h
 tests/signature.h
 tests/macros.h
+m4/mmap-anon.m4
 
 Depends-on:
+extensions
+getpagesize
 
 configure.ac:
+dnl Check for prerequisites for memory fence checks.
+gl_FUNC_MMAP_ANON
+AC_CHECK_HEADERS_ONCE([sys/mman.h])
+AC_CHECK_FUNCS_ONCE([mprotect])
 
 Makefile.am:
 TESTS += test-rawmemchr
index e7b9780..05e1bfa 100644 (file)
@@ -57,6 +57,7 @@ main (void)
 
   ASSERT (MEMCHR (input + 1, 'a', n - 1) == input + n - 1);
   ASSERT (MEMCHR (input + 1, 'e', n - 1) == input + n - 2);
+  ASSERT (MEMCHR (input + 1, 0x789abc00 | 'e', n - 1) == input + n - 2);
 
   ASSERT (MEMCHR (input, 'f', n) == NULL);
   ASSERT (MEMCHR (input, '\0', n) == NULL);
index 9476818..804e157 100644 (file)
@@ -24,6 +24,7 @@ SIGNATURE_CHECK (rawmemchr, void *, (void const *, int));
 
 #include <stdlib.h>
 
+#include "zerosize-ptr.h"
 #include "macros.h"
 
 /* Calculating void * + int is not portable, so this wrapper converts
@@ -53,6 +54,7 @@ main (void)
 
   ASSERT (RAWMEMCHR (input + 1, 'a') == input + n - 1);
   ASSERT (RAWMEMCHR (input + 1, 'e') == input + n - 2);
+  ASSERT (RAWMEMCHR (input + 1, 0x789abc00 | 'e') == input + n - 2);
 
   ASSERT (RAWMEMCHR (input, '\0') == input + n);
 
@@ -70,6 +72,20 @@ main (void)
       }
   }
 
+  /* Ensure that no unaligned oversized reads occur.  */
+  {
+    char *page_boundary = (char *) zerosize_ptr ();
+    size_t i;
+
+    if (!page_boundary)
+      page_boundary = input + 4096;
+    memset (page_boundary - 512, '1', 511);
+    page_boundary[-1] = '2';
+    for (i = 1; i <= 512; i++)
+      ASSERT (RAWMEMCHR (page_boundary - i, (i * 0x01010100) | '2')
+              == page_boundary - 1);
+  }
+
   free (input);
 
   return 0;