[cairo] [PATCH V2 4/4] test: for replaying race condition.

Zhigang Gong zhigang.gong at linux.intel.com
Mon May 28 06:09:25 PDT 2012


As we may haven't protect the whole replaying procedure,
after the replaying thread get the target surface, another
thread may begin to modify the target surface, thus the
replaying thread may finally get modified data.

Signed-off-by: Zhigang Gong <zhigang.gong at linux.intel.com>
---
 test/Makefile.sources                              |    1 +
 test/record-replay-race.c                          |  145 ++++++++++++++++++++
 .../record-replay-race.image.argb32.ref.png        |  Bin 0 -> 120 bytes
 .../record-replay-race.image.rgb24.ref.png         |  Bin 0 -> 120 bytes
 4 files changed, 146 insertions(+), 0 deletions(-)
 create mode 100644 test/record-replay-race.c
 create mode 100644 test/reference/record-replay-race.image.argb32.ref.png
 create mode 100644 test/reference/record-replay-race.image.rgb24.ref.png

diff --git a/test/Makefile.sources b/test/Makefile.sources
index ddb41b5..b90478a 100644
--- a/test/Makefile.sources
+++ b/test/Makefile.sources
@@ -258,6 +258,7 @@ test_sources = \
 	random-intersections-curves-nz.c		\
 	raster-source.c					\
 	record.c					\
+	record-replay-race.c				\
 	record1414x.c					\
 	record2x.c					\
 	record90.c					\
diff --git a/test/record-replay-race.c b/test/record-replay-race.c
new file mode 100644
index 0000000..bc0ef1a
--- /dev/null
+++ b/test/record-replay-race.c
@@ -0,0 +1,145 @@
+/*
+ * Copyright © 2005 Red Hat, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software
+ * and its documentation for any purpose is hereby granted without
+ * fee, provided that the above copyright notice appear in all copies
+ * and that both that copyright notice and this permission notice
+ * appear in supporting documentation, and that the name of
+ * Red Hat, Inc. not be used in advertising or publicity pertaining to
+ * distribution of the software without specific, written prior
+ * permission. Red Hat, Inc. makes no representations about the
+ * suitability of this software for any purpose.  It is provided "as
+ * is" without express or implied warranty.
+ *
+ * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
+ * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
+ * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
+ * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ * Author: Zhigang Gong <zhigang.gong at linux.intel.com>
+ */
+
+#include <math.h>
+#include "cairo-test.h"
+#include <pthread.h>
+#include <stdio.h>
+
+#define SIZE 40
+
+#define LARGE_SIZE 4000
+typedef struct thread_data {
+    cairo_t         *source_cr;
+} thread_data_t;
+
+static void *
+source_draw_thread(void *arg)
+{
+    thread_data_t *thread_data = arg;
+    cairo_set_source_rgb (thread_data->source_cr, 0, 1, 0);	/* Green */
+    /* Simply fill the last SIZExSIZE area to green color. May win the Race with the
+     * replay thread. */
+    cairo_rectangle(thread_data->source_cr, LARGE_SIZE-SIZE, LARGE_SIZE-SIZE, SIZE, SIZE);
+    cairo_fill(thread_data->source_cr);
+    return NULL;
+}
+
+static cairo_test_status_t
+draw (cairo_t *cr, int width, int height)
+{
+    cairo_surface_t *surface, *surface2, *recording_surface;
+    cairo_t *record_cr, *source_cr, *source2_cr;
+    thread_data_t thread_data;
+    pthread_t thread;
+    int status;
+
+    /* Create two large surfaces: surface and surface2.
+     * Fill the surface's right bottom SIZE x SIZE with Red,
+     * And set the other area to white.
+     *
+     * Create recording surface and copy the whole surface
+     * to this recording surface.
+     *
+     * Then replay it on the surface2. And in the meantime,
+     * create a thread to fill the original surface's
+     * last SIZE x SIZE to green color.
+     *
+     * As recording surface has a snapshot of the surface. The
+     * previous implementation doesn't protect the replaying
+     * procedure, thus a race occur, here. The replaying procedure
+     * invoked from this thread begin to replay, first, it
+     * get a target source of the snapshot, and that is the
+     * surface's pointer. And right after it get the surface's
+     * pointer as target, the source_draw_thread begin to modify
+     * the source surface, as the COW is too late, the replaying
+     * thread still use the original surface's pointer, thus
+     * the replaying thread will get the modified data at
+     * source_draw_thread. As the source_draw_thread is to fill
+     * the last block directly, it will win, and the replaying
+     * thread will finally get a green block output which is
+     * not what we expected.
+     *
+     **/
+    recording_surface = cairo_recording_surface_create (cairo_surface_get_content (cairo_get_target (cr)), NULL);
+    record_cr = cairo_create (recording_surface);
+    cairo_surface_destroy(recording_surface);
+
+    surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
+			LARGE_SIZE, LARGE_SIZE);
+    source_cr = cairo_create (surface);
+    cairo_surface_destroy(surface);
+
+    surface2 = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
+			LARGE_SIZE, LARGE_SIZE);
+    source2_cr = cairo_create (surface2);
+    cairo_surface_destroy(surface2);
+
+    cairo_set_source_rgb (source_cr, 1, 1, 1);	/* White */
+    cairo_rectangle (source_cr, 0, 0, LARGE_SIZE, LARGE_SIZE);
+    cairo_fill (source_cr);
+    cairo_set_source_rgb (source_cr, 1, 0, 0);	/* Red */
+    cairo_rectangle (source_cr, LARGE_SIZE-SIZE,
+		LARGE_SIZE-SIZE, SIZE, SIZE);
+    cairo_fill (source_cr);
+
+    /* copy the whole surface to recording surface. */
+    cairo_set_operator (record_cr, CAIRO_OPERATOR_SOURCE);
+    cairo_set_source_surface (record_cr, surface, 0, 0);
+    cairo_paint (record_cr);
+
+    thread_data.source_cr = source_cr;
+    cairo_set_operator (source2_cr, CAIRO_OPERATOR_SOURCE);
+    cairo_set_source_surface( source2_cr, recording_surface,  0 , 0);
+    /* It's time to trigger the race. */
+    if (pthread_create(&thread, NULL, source_draw_thread,
+			&thread_data) != 0) {
+	status = CAIRO_TEST_FAILURE;
+	goto err;
+    }
+    cairo_paint( source2_cr);
+    if (pthread_join(thread, NULL) != 0) {
+	status = CAIRO_TEST_FAILURE;
+	goto err;
+    }
+    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
+    cairo_set_source_surface( cr, surface2,
+		SIZE-LARGE_SIZE , SIZE-LARGE_SIZE);
+    cairo_paint( cr);
+    status = CAIRO_TEST_SUCCESS;
+err:
+    cairo_destroy(source_cr);
+    cairo_destroy(source2_cr);
+    cairo_destroy(record_cr);
+    return CAIRO_TEST_SUCCESS;
+
+}
+
+CAIRO_TEST (record_replay_race,
+	    "Test replay racing with a modifying a source surface",
+	    "paint", /* keywords */
+	    NULL, /* requirements */
+	    SIZE, SIZE,
+	    NULL, draw)
diff --git a/test/reference/record-replay-race.image.argb32.ref.png b/test/reference/record-replay-race.image.argb32.ref.png
new file mode 100644
index 0000000000000000000000000000000000000000..d1ee90bc0aeeac2784c8aecb17046195fe7cebc7
GIT binary patch
literal 120
zcmeAS at N?(olHy`uVBq!ia0vp^8X(NU1SFZ~=vx6Pwj^(N7l!{JxM1({$v_cZPZ!6K
zh}O4f8F?8PIG7c-)~EM=JnP6<rJd}wK0-&V`)JZeDD at +?=1vWx|EsqO>_CkSp00i_
I>zopr0QJ%%y#N3J

literal 0
HcmV?d00001

diff --git a/test/reference/record-replay-race.image.rgb24.ref.png b/test/reference/record-replay-race.image.rgb24.ref.png
new file mode 100644
index 0000000000000000000000000000000000000000..d1ee90bc0aeeac2784c8aecb17046195fe7cebc7
GIT binary patch
literal 120
zcmeAS at N?(olHy`uVBq!ia0vp^8X(NU1SFZ~=vx6Pwj^(N7l!{JxM1({$v_cZPZ!6K
zh}O4f8F?8PIG7c-)~EM=JnP6<rJd}wK0-&V`)JZeDD at +?=1vWx|EsqO>_CkSp00i_
I>zopr0QJ%%y#N3J

literal 0
HcmV?d00001

-- 
1.7.4.4



More information about the cairo mailing list