tests/gem_reloc_overflow: Add more checks
[platform/upstream/intel-gpu-tools.git] / tests / gem_reloc_overflow.c
1 /*
2  * Copyright © 2013 Google
3  * Copyright © 2013 Intel Corporation
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  *
24  * Authors:
25  *    Kees Cook <keescook@chromium.org>
26  *    Daniel Vetter <daniel.vetter@ffwll.ch>
27  *
28  */
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <inttypes.h>
36 #include <errno.h>
37 #include <unistd.h>
38 #include <malloc.h>
39 #include <limits.h>
40 #include <sys/ioctl.h>
41 #include <sys/stat.h>
42 #include <sys/time.h>
43 #include <sys/types.h>
44 #include "drm.h"
45 #include "i915_drm.h"
46 #include "drmtest.h"
47 #include "intel_gpu_tools.h"
48
49 /*
50  * Testcase: Kernel relocation overflows are caught.
51  */
52
53 int fd, entries, num;
54 size_t reloc_size;
55 uint32_t *handles;
56 struct drm_i915_gem_exec_object2 *execobjs;
57 struct drm_i915_gem_execbuffer2 execbuf = { 0 };
58 struct drm_i915_gem_relocation_entry *reloc;
59
60 uint32_t handle;
61 uint32_t batch_handle;
62
63
64 static void source_offset_tests(void)
65 {
66         struct drm_i915_gem_relocation_entry single_reloc;
67
68         igt_fixture {
69                 handle = gem_create(fd, 4096);
70
71                 execobjs[1].handle = batch_handle;
72                 execobjs[1].relocation_count = 0;
73                 execobjs[1].relocs_ptr = 0;
74
75                 execobjs[0].handle = handle;
76                 execobjs[0].relocation_count = 1;
77                 execobjs[0].relocs_ptr = (uintptr_t) &single_reloc;
78                 execbuf.buffer_count = 2;
79         }
80
81         igt_subtest("source-offset-end") {
82                 single_reloc.offset = 4096 - 4;
83                 single_reloc.delta = 0;
84                 single_reloc.target_handle = handle;
85                 single_reloc.read_domains = I915_GEM_DOMAIN_RENDER;
86                 single_reloc.write_domain = I915_GEM_DOMAIN_RENDER;
87                 single_reloc.presumed_offset = 0;
88
89                 igt_assert(ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf) == 0);
90         }
91
92         igt_subtest("source-offset-big") {
93                 single_reloc.offset = 4096;
94                 single_reloc.delta = 0;
95                 single_reloc.target_handle = handle;
96                 single_reloc.read_domains = I915_GEM_DOMAIN_RENDER;
97                 single_reloc.write_domain = I915_GEM_DOMAIN_RENDER;
98                 single_reloc.presumed_offset = 0;
99
100                 igt_assert(ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf) != 0);
101                 igt_assert(errno == EINVAL);
102         }
103
104         igt_subtest("source-offset-negative") {
105                 single_reloc.offset = (int64_t) -4;
106                 single_reloc.delta = 0;
107                 single_reloc.target_handle = handle;
108                 single_reloc.read_domains = I915_GEM_DOMAIN_RENDER;
109                 single_reloc.write_domain = I915_GEM_DOMAIN_RENDER;
110                 single_reloc.presumed_offset = 0;
111
112                 igt_assert(ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf) != 0);
113                 igt_assert(errno == EINVAL);
114         }
115
116         igt_subtest("source-offset-unaligned") {
117                 single_reloc.offset = 1;
118                 single_reloc.delta = 0;
119                 single_reloc.target_handle = handle;
120                 single_reloc.read_domains = I915_GEM_DOMAIN_RENDER;
121                 single_reloc.write_domain = I915_GEM_DOMAIN_RENDER;
122                 single_reloc.presumed_offset = 0;
123
124                 igt_assert(ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf) != 0);
125                 igt_assert(errno == EINVAL);
126         }
127
128         igt_fixture {
129                 execobjs[0].handle = batch_handle;
130                 execobjs[0].relocation_count = 0;
131                 execobjs[0].relocs_ptr = 0;
132
133                 execbuf.buffer_count = 1;
134         }
135
136         igt_subtest("batch-start-unaligend") {
137                 execbuf.batch_start_offset = 1;
138                 execbuf.batch_len = 8;
139
140                 igt_assert(ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf) != 0);
141                 igt_assert(errno == EINVAL);
142         }
143
144         igt_subtest("batch-end-unaligend") {
145                 execbuf.batch_start_offset = 0;
146                 execbuf.batch_len = 7;
147
148                 igt_assert(ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf) != 0);
149                 igt_assert(errno == EINVAL);
150         }
151
152         igt_fixture
153                 gem_close(fd, handle);
154 }
155
156 int main(int argc, char *argv[])
157 {
158         int i;
159         size_t total_actual = 0;
160         unsigned int total_unsigned = 0;
161         int total_signed = 0;
162         igt_subtest_init(argc, argv);
163
164         igt_fixture {
165                 int ring;
166                 uint32_t batch_data [2] = { MI_NOOP, MI_BATCH_BUFFER_END };
167
168                 fd = drm_open_any();
169
170                 /* Create giant reloc buffer area. */
171                 num = 257;
172                 entries = ((1ULL << 32) / (num - 1));
173                 reloc_size = entries * sizeof(struct drm_i915_gem_relocation_entry);
174                 reloc = mmap(NULL, reloc_size, PROT_READ | PROT_WRITE,
175                              MAP_PRIVATE | MAP_ANON, -1, 0);
176                 igt_assert(reloc != MAP_FAILED);
177
178                 /* Allocate the handles we'll need to wrap. */
179                 handles = calloc(num, sizeof(*handles));
180                 for (i = 0; i < num; i++)
181                         handles[i] = gem_create(fd, 4096);
182
183                 if (intel_gen(intel_get_drm_devid(fd)) >= 6)
184                         ring = I915_EXEC_BLT;
185                 else
186                         ring = 0;
187
188                 /* Create relocation objects. */
189                 execobjs = calloc(num, sizeof(*execobjs));
190                 execbuf.buffers_ptr = (uintptr_t)execobjs;
191                 execbuf.batch_start_offset = 0;
192                 execbuf.batch_len = 8;
193                 execbuf.cliprects_ptr = 0;
194                 execbuf.num_cliprects = 0;
195                 execbuf.DR1 = 0;
196                 execbuf.DR4 = 0;
197                 execbuf.flags = ring;
198                 i915_execbuffer2_set_context_id(execbuf, 0);
199                 execbuf.rsvd2 = 0;
200
201                 batch_handle = gem_create(fd, 4096);
202
203                 gem_write(fd, batch_handle, 0, batch_data, sizeof(batch_data));
204         }
205
206         igt_subtest("invalid-address") {
207                 /* Attempt unmapped single entry. */
208                 execobjs[0].relocation_count = 1;
209                 execobjs[0].relocs_ptr = 0;
210                 execbuf.buffer_count = 1;
211
212                 errno = 0;
213                 ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
214                 igt_assert(errno == EFAULT);
215         }
216
217         igt_subtest("single-overflow") {
218                 /* Attempt single overflowed entry. */
219                 execobjs[0].relocation_count = (1 << 31);
220                 execobjs[0].relocs_ptr = (uintptr_t)reloc;
221                 execbuf.buffer_count = 1;
222
223                 errno = 0;
224                 ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
225                 igt_assert(errno == EINVAL);
226         }
227
228         igt_subtest("wrapped-overflow") {
229                 /* Attempt wrapped overflow entries. */
230                 for (i = 0; i < num; i++) {
231                         struct drm_i915_gem_exec_object2 *obj = &execobjs[i];
232                         obj->handle = handles[i];
233
234                         if (i == num - 1) {
235                                 /* Wraps to 1 on last count. */
236                                 obj->relocation_count = 1 - total_unsigned;
237                                 obj->relocs_ptr = (uintptr_t)reloc;
238                         } else {
239                                 obj->relocation_count = entries;
240                                 obj->relocs_ptr = (uintptr_t)reloc;
241                         }
242
243                         total_unsigned += obj->relocation_count;
244                         total_signed += obj->relocation_count;
245                         total_actual += obj->relocation_count;
246                 }
247                 execbuf.buffer_count = num;
248
249                 errno = 0;
250                 ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
251                 igt_assert(errno == EINVAL);
252         }
253
254         source_offset_tests();
255
256         igt_fixture {
257                 gem_close(fd, batch_handle);
258                 close(fd);
259         }
260
261         igt_exit();
262 }