[Pixman] [PATCH] Add alpha-loop test program

Søren Sandmann sandmann at daimi.au.dk
Wed Aug 4 17:09:00 PDT 2010


From: Søren Sandmann Pedersen <ssp at redhat.com>

This tests what happens if you attempt to make an image with an alpha
map that has the image as its alpha map. This results in an infinite
loop in _pixman_image_validate(), so the test sets up a SIGALRM to
exit if it runs for more than five seconds.
---
 configure.ac      |   12 +++++++++++-
 test/Makefile.am  |    4 ++++
 test/alpha-loop.c |   29 +++++++++++++++++++++++++++++
 test/utils.c      |   33 +++++++++++++++++++++++++++++++++
 test/utils.h      |    3 +++
 5 files changed, 80 insertions(+), 1 deletions(-)
 create mode 100644 test/alpha-loop.c

diff --git a/configure.ac b/configure.ac
index 98c2783..acec8a1 100644
--- a/configure.ac
+++ b/configure.ac
@@ -600,13 +600,23 @@ AC_SUBST(DEP_CFLAGS)
 AC_SUBST(DEP_LIBS)
 
 dnl =====================================
-dnl posix_memalign 
+dnl posix_memalign, sigaction, alarm
 
 AC_CHECK_FUNC(posix_memalign, have_posix_memalign=yes, have_posix_memalign=no)
 if test x$have_posix_memalign = xyes; then
    AC_DEFINE(HAVE_POSIX_MEMALIGN, 1, [Whether we have posix_memalign()])
 fi
 
+AC_CHECK_FUNC(sigaction, have_sigaction=yes, have_sigaction=no)
+if test x$have_sigaction = xyes; then
+   AC_DEFINE(HAVE_SIGACTION, 1, [Whether we have sigaction()])
+fi
+
+AC_CHECK_FUNC(alarm, have_alarm=yes, have_alarm=no)
+if test x$have_alarm = xyes; then
+   AC_DEFINE(HAVE_ALARM, 1, [Whether we have alarm()])
+fi
+
 dnl =====================================
 dnl Thread local storage
 
diff --git a/test/Makefile.am b/test/Makefile.am
index 2a7aea2..5273bec 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -13,6 +13,7 @@ TESTPROGRAMS =			\
 	gradient-crash-test	\
 	trap-crasher		\
 	alphamap		\
+	alpha-loop		\
 	scaling-crash-test	\
 	blitters-test		\
 	scaling-test		\
@@ -39,6 +40,9 @@ scaling_test_SOURCES = scaling-test.c utils.c utils.h
 alphamap_LDADD = $(TEST_LDADD)
 alphamap_SOURCES = alphamap.c utils.c utils.h
 
+alpha_loop_LDADD = $(TEST_LDADD)
+alpha_loop_SOURCES = alpha-loop.c utils.c utils.h
+
 # GTK using test programs
 
 if HAVE_GTK
diff --git a/test/alpha-loop.c b/test/alpha-loop.c
new file mode 100644
index 0000000..e4d90a9
--- /dev/null
+++ b/test/alpha-loop.c
@@ -0,0 +1,29 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "utils.h"
+
+#define WIDTH 400
+#define HEIGHT 200
+
+int
+main (int argc, char **argv)
+{
+    uint8_t *alpha = make_random_bytes (WIDTH * HEIGHT);
+    uint32_t *src = (uint32_t *)make_random_bytes (WIDTH * HEIGHT * 4);
+    uint32_t *dest = (uint32_t *)make_random_bytes (WIDTH * HEIGHT * 4);
+
+    pixman_image_t *a = pixman_image_create_bits (PIXMAN_a8, WIDTH, HEIGHT, (uint32_t *)alpha, WIDTH);
+    pixman_image_t *d = pixman_image_create_bits (PIXMAN_a8r8g8b8, WIDTH, HEIGHT, dest, WIDTH * 4);
+    pixman_image_t *s = pixman_image_create_bits (PIXMAN_a2r10g10b10, WIDTH, HEIGHT, src, WIDTH * 4);
+
+    fail_after (5, "Infinite loop detected: 5 seconds without progress\n");
+
+    pixman_image_set_alpha_map (s, a, 0, 0);
+    pixman_image_set_alpha_map (a, s, 0, 0);
+
+    pixman_image_composite (PIXMAN_OP_SRC, s, NULL, d, 0, 0, 0, 0, 0, 0, WIDTH, HEIGHT);
+
+    pixman_image_unref (s);
+
+    return 0;
+}
diff --git a/test/utils.c b/test/utils.c
index 1ee5c9c..d95cbc2 100644
--- a/test/utils.c
+++ b/test/utils.c
@@ -1,4 +1,9 @@
 #include "utils.h"
+#include <signal.h>
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
 
 /* Random number seed
  */
@@ -319,3 +324,31 @@ fuzzer_test_main (const char *test_name,
 
     return 0;
 }
+
+static const char *global_msg;
+
+static void
+on_alarm (int signo)
+{
+    printf ("%s\n", global_msg);
+    exit (1);
+}
+
+void
+fail_after (int seconds, const char *msg)
+{
+#ifdef HAVE_SIGACTION
+#ifdef HAVE_ALARM
+    struct sigaction action;
+
+    global_msg = msg;
+
+    memset (&action, 0, sizeof (action));
+    action.sa_handler = on_alarm;
+
+    alarm (seconds);
+
+    sigaction (SIGALRM, &action, NULL);
+#endif
+#endif
+}
diff --git a/test/utils.h b/test/utils.h
index 95d809a..bfb76a5 100644
--- a/test/utils.h
+++ b/test/utils.h
@@ -62,3 +62,6 @@ fuzzer_test_main (const char *test_name,
 		  uint32_t    (*test_function)(int testnum, int verbose),
 		  int         argc,
 		  const char *argv[]);
+
+void
+fail_after (int seconds, const char *msg);
-- 
1.6.0.6



More information about the Pixman mailing list