1 /*
2 * Copyright © 2017 Red Hat
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "igt.h"
25 #include "igt_syncobj.h"
26 #include <unistd.h>
27 #include <sys/ioctl.h>
28 #include "drm.h"
29
30 IGT_TEST_DESCRIPTION("Basic check for drm sync objects.");
31
32 /* destroy a random handle */
33 static void
test_bad_destroy(int fd)34 test_bad_destroy(int fd)
35 {
36 struct drm_syncobj_destroy destroy;
37 int ret;
38
39 destroy.handle = 0xdeadbeef;
40 destroy.pad = 0;
41
42 ret = ioctl(fd, DRM_IOCTL_SYNCOBJ_DESTROY, &destroy);
43
44 igt_assert(ret == -1 && errno == EINVAL);
45 }
46
47 /* handle to fd a bad handle */
48 static void
test_bad_handle_to_fd(int fd)49 test_bad_handle_to_fd(int fd)
50 {
51 struct drm_syncobj_handle handle;
52
53 handle.handle = 0xdeadbeef;
54 handle.flags = 0;
55
56 igt_assert_eq(__syncobj_handle_to_fd(fd, &handle), -EINVAL);
57 }
58
59 /* fd to handle a bad fd */
60 static void
test_bad_fd_to_handle(int fd)61 test_bad_fd_to_handle(int fd)
62 {
63 struct drm_syncobj_handle handle;
64
65 handle.fd = -1;
66 handle.flags = 0;
67
68 igt_assert_eq(__syncobj_fd_to_handle(fd, &handle), -EINVAL);
69 }
70
71 /* fd to handle an fd but not a sync file one */
72 static void
test_illegal_fd_to_handle(int fd)73 test_illegal_fd_to_handle(int fd)
74 {
75 struct drm_syncobj_handle handle;
76
77 handle.fd = fd;
78 handle.flags = 0;
79
80 igt_assert_eq(__syncobj_fd_to_handle(fd, &handle), -EINVAL);
81 }
82
83 static void
test_bad_flags_fd_to_handle(int fd)84 test_bad_flags_fd_to_handle(int fd)
85 {
86 struct drm_syncobj_handle handle = { 0 };
87
88 handle.flags = 0xdeadbeef;
89 igt_assert_eq(__syncobj_fd_to_handle(fd, &handle), -EINVAL);
90 }
91
92 static void
test_bad_flags_handle_to_fd(int fd)93 test_bad_flags_handle_to_fd(int fd)
94 {
95 struct drm_syncobj_handle handle = { 0 };
96
97 handle.flags = 0xdeadbeef;
98 igt_assert_eq(__syncobj_handle_to_fd(fd, &handle), -EINVAL);
99 }
100
101 static void
test_bad_pad_handle_to_fd(int fd)102 test_bad_pad_handle_to_fd(int fd)
103 {
104 struct drm_syncobj_handle handle = { 0 };
105
106 handle.pad = 0xdeadbeef;
107 igt_assert_eq(__syncobj_handle_to_fd(fd, &handle), -EINVAL);
108 }
109
110 static void
test_bad_pad_fd_to_handle(int fd)111 test_bad_pad_fd_to_handle(int fd)
112 {
113 struct drm_syncobj_handle handle = { 0 };
114
115 handle.pad = 0xdeadbeef;
116 igt_assert_eq(__syncobj_fd_to_handle(fd, &handle), -EINVAL);
117 }
118
119
120
121 /* destroy with data in the padding */
122 static void
test_bad_destroy_pad(int fd)123 test_bad_destroy_pad(int fd)
124 {
125 struct drm_syncobj_destroy destroy;
126 int ret;
127
128 destroy.handle = syncobj_create(fd, 0);
129 destroy.pad = 0xdeadbeef;
130
131 ret = ioctl(fd, DRM_IOCTL_SYNCOBJ_DESTROY, &destroy);
132
133 igt_assert(ret == -1 && errno == EINVAL);
134
135 syncobj_destroy(fd, destroy.handle);
136 }
137
138 static void
test_bad_create_flags(int fd)139 test_bad_create_flags(int fd)
140 {
141 struct drm_syncobj_create create = { 0 };
142 int ret;
143
144 create.flags = 0xdeadbeef;
145 ret = ioctl(fd, DRM_IOCTL_SYNCOBJ_CREATE, &create);
146 igt_assert(ret == -1 && errno == EINVAL);
147 }
148
149 static void
test_create_signaled(int fd)150 test_create_signaled(int fd)
151 {
152 uint32_t syncobj = syncobj_create(fd, LOCAL_SYNCOBJ_CREATE_SIGNALED);
153
154 igt_assert_eq(syncobj_wait_err(fd, &syncobj, 1, 0, 0), 0);
155
156 syncobj_destroy(fd, syncobj);
157 }
158
159 /*
160 * currently don't do handle deduplication
161 * test we get a different handle back.
162 */
163 static void
test_valid_cycle(int fd)164 test_valid_cycle(int fd)
165 {
166 uint32_t first_handle, second_handle;
167 int syncobj_fd;
168
169 first_handle = syncobj_create(fd, 0);
170 syncobj_fd = syncobj_handle_to_fd(fd, first_handle, 0);
171 second_handle = syncobj_fd_to_handle(fd, syncobj_fd, 0);
172 close(syncobj_fd);
173
174 igt_assert(first_handle != second_handle);
175
176 syncobj_destroy(fd, first_handle);
177 syncobj_destroy(fd, second_handle);
178 }
179
has_syncobj(int fd)180 static bool has_syncobj(int fd)
181 {
182 uint64_t value;
183 if (drmGetCap(fd, DRM_CAP_SYNCOBJ, &value))
184 return false;
185 return value ? true : false;
186 }
187
188 igt_main
189 {
190 int fd = -1;
191
192 igt_fixture {
193 fd = drm_open_driver(DRIVER_ANY);
194 igt_require(has_syncobj(fd));
195 }
196
197
198 igt_subtest("bad-destroy")
199 test_bad_destroy(fd);
200
201 igt_subtest("bad-create-flags")
202 test_bad_create_flags(fd);
203
204 igt_subtest("bad-handle-to-fd")
205 test_bad_handle_to_fd(fd);
206
207 igt_subtest("bad-fd-to-handle")
208 test_bad_fd_to_handle(fd);
209
210 igt_subtest("bad-flags-handle-to-fd")
211 test_bad_flags_handle_to_fd(fd);
212
213 igt_subtest("bad-flags-fd-to-handle")
214 test_bad_flags_fd_to_handle(fd);
215
216 igt_subtest("bad-pad-handle-to-fd")
217 test_bad_pad_handle_to_fd(fd);
218
219 igt_subtest("bad-pad-fd-to-handle")
220 test_bad_pad_fd_to_handle(fd);
221
222 igt_subtest("illegal-fd-to-handle")
223 test_illegal_fd_to_handle(fd);
224
225 igt_subtest("bad-destroy-pad")
226 test_bad_destroy_pad(fd);
227
228 igt_subtest("create-signaled")
229 test_create_signaled(fd);
230
231 igt_subtest("test-valid-cycle")
232 test_valid_cycle(fd);
233
234 }
235