[Intel-gfx] [PATCH] igt/drm_read: Abuse read(drm)

Chris Wilson chris at chris-wilson.co.uk
Fri Dec 5 05:14:40 PST 2014


Check that the more obvious userspace error conditions are handled by
the kernel, ideally without loss of data. These include nonblocking
waits, passing invalid buffers and passing buffers of the incorrect
length.

Signed-off-by: Chris Wilson <chris at chris-wilson.co.uk>
---
 tests/.gitignore       |   1 +
 tests/Makefile.sources |   1 +
 tests/drm_read.c       | 192 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 194 insertions(+)
 create mode 100644 tests/drm_read.c

diff --git a/tests/.gitignore b/tests/.gitignore
index 869a80c..38f6f96 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -4,6 +4,7 @@ core_getclient
 core_getstats
 core_getversion
 drm_import_export
+drm_read
 drm_vma_limiter
 drm_vma_limiter_cached
 drm_vma_limiter_cpu
diff --git a/tests/Makefile.sources b/tests/Makefile.sources
index 42bfed9..da9e742 100644
--- a/tests/Makefile.sources
+++ b/tests/Makefile.sources
@@ -90,6 +90,7 @@ TESTS_progs = \
 	core_getstats \
 	core_getversion \
 	drm_import_export \
+	drm_read \
 	drm_vma_limiter \
 	drm_vma_limiter_cached \
 	drm_vma_limiter_cpu \
diff --git a/tests/drm_read.c b/tests/drm_read.c
new file mode 100644
index 0000000..79cf64d
--- /dev/null
+++ b/tests/drm_read.c
@@ -0,0 +1,192 @@
+/*
+ * Copyright © 2014 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ * Authors:
+ *    Chris Wilson <chris at chris-wilson.co.uk>
+ *
+ */
+
+/*
+ * Testcase: boundary testing of read(drm_fd)
+ */
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <signal.h>
+#include <fcntl.h>
+#include <inttypes.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <sys/time.h>
+#include <sys/poll.h>
+#include "drm.h"
+#include "ioctl_wrappers.h"
+#include "drmtest.h"
+#include "igt_aux.h"
+
+IGT_TEST_DESCRIPTION("Call read(drm) and see if it behaves.");
+
+static void sighandler(int sig)
+{
+}
+
+static void assert_empty(int fd)
+{
+	struct pollfd pfd = {fd, POLLIN };
+	igt_assert(poll(&pfd, 1, 0) == 0);
+}
+
+static void generate_event(int fd)
+{
+	union drm_wait_vblank vbl;
+
+	/* We assume that pipe 0 is running */
+
+	vbl.request.type =
+		DRM_VBLANK_RELATIVE |
+		DRM_VBLANK_EVENT;
+	vbl.request.sequence = 0;
+
+	igt_assert(drmIoctl(fd, DRM_IOCTL_WAIT_VBLANK, &vbl) == 0);
+}
+
+static void wait_for_event(int fd)
+{
+	struct pollfd pfd = {fd, POLLIN };
+	igt_assert(poll(&pfd, 1, -1) == 1);
+}
+
+static void teardown(int fd)
+{
+	alarm(0);
+	assert_empty(fd);
+	close(fd);
+	errno = 0;
+}
+
+static void test_invalid_buffer(int in)
+{
+	int fd = dup(in);
+	int ret;
+
+	alarm(1);
+	ret = read(fd, NULL, 4096);
+
+	igt_assert_eq(ret, -1);
+	igt_assert_eq(errno, EFAULT);
+
+	teardown(fd);
+}
+
+static void test_empty(int in, int nonblock, int expected)
+{
+	char buffer[1024];
+	int fd = dup(in);
+	int ret;
+
+	if (nonblock) {
+		ret = -1;
+		if (fd != -1)
+			ret = fcntl(fd, F_GETFL);
+		if (ret != -1) {
+			ret |= O_NONBLOCK;
+			ret = fcntl(fd, F_SETFL, ret);
+		}
+		igt_require(ret != -1);
+	}
+
+	assert_empty(fd);
+
+	alarm(1);
+	ret = read(fd, buffer, sizeof(buffer));
+	igt_assert_eq(ret, -1);
+	igt_assert_eq(errno, expected);
+
+	teardown(fd);
+}
+
+static void test_short_buffer(int in, int nonblock)
+{
+	char buffer[1024]; /* events are typically 32 bytes */
+	int fd = dup(in);
+	int ret;
+
+	if (nonblock) {
+		ret = -1;
+		if (fd != -1)
+			ret = fcntl(fd, F_GETFL);
+		if (ret != -1) {
+			ret |= O_NONBLOCK;
+			ret = fcntl(fd, F_SETFL, ret);
+		}
+		igt_require(ret != -1);
+	}
+
+	generate_event(fd);
+	generate_event(fd);
+
+	wait_for_event(fd);
+
+	alarm(3);
+
+	ret = read(fd, buffer, 4);
+	igt_assert(ret > 0);
+
+	ret = read(fd, buffer, 40);
+	igt_assert(ret > 0);
+
+	ret = read(fd, buffer, 40);
+	igt_assert(ret > 0);
+
+	teardown(fd);
+}
+
+igt_main
+{
+	int fd;
+
+	signal(SIGALRM, sighandler);
+	siginterrupt(SIGALRM, 1);
+
+	igt_fixture {
+		fd = drm_open_any_master();
+	}
+
+	igt_subtest("invalid-buffer")
+		test_invalid_buffer(fd);
+
+	igt_subtest("empty-block")
+		test_empty(fd, 0, EINTR);
+
+	igt_subtest("empty-nonblock")
+		test_empty(fd, 1, EAGAIN);
+
+	igt_subtest("short-buffer-block")
+		test_short_buffer(fd, 0);
+
+	igt_subtest("short-buffer-nonblock")
+		test_short_buffer(fd, 1);
+}
-- 
2.1.3




More information about the Intel-gfx mailing list