tests/gem_reloc_overflow: Extract reloc_tests
[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 static void reloc_tests(void)
157 {
158         int i;
159         unsigned int total_unsigned = 0;
160
161         igt_subtest("invalid-address") {
162                 /* Attempt unmapped single entry. */
163                 execobjs[0].relocation_count = 1;
164                 execobjs[0].relocs_ptr = 0;
165                 execbuf.buffer_count = 1;
166
167                 errno = 0;
168                 ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
169                 igt_assert(errno == EFAULT);
170         }
171
172         igt_subtest("single-overflow") {
173                 /* Attempt single overflowed entry. */
174                 execobjs[0].relocation_count = (1 << 31);
175                 execobjs[0].relocs_ptr = (uintptr_t)reloc;
176                 execbuf.buffer_count = 1;
177
178                 errno = 0;
179                 ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
180                 igt_assert(errno == EINVAL);
181         }
182
183         igt_subtest("wrapped-overflow") {
184                 /* Attempt wrapped overflow entries. */
185                 for (i = 0; i < num; i++) {
186                         struct drm_i915_gem_exec_object2 *obj = &execobjs[i];
187                         obj->handle = handles[i];
188
189                         if (i == num - 1) {
190                                 /* Wraps to 1 on last count. */
191                                 obj->relocation_count = 1 - total_unsigned;
192                                 obj->relocs_ptr = (uintptr_t)reloc;
193                         } else {
194                                 obj->relocation_count = entries;
195                                 obj->relocs_ptr = (uintptr_t)reloc;
196                         }
197
198                         total_unsigned += obj->relocation_count;
199                 }
200                 execbuf.buffer_count = num;
201
202                 errno = 0;
203                 ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
204                 igt_assert(errno == EINVAL);
205         }
206 }
207
208 int main(int argc, char *argv[])
209 {
210         igt_subtest_init(argc, argv);
211
212         igt_fixture {
213                 int ring;
214                 uint32_t batch_data [2] = { MI_NOOP, MI_BATCH_BUFFER_END };
215
216                 fd = drm_open_any();
217
218                 /* Create giant reloc buffer area. */
219                 num = 257;
220                 entries = ((1ULL << 32) / (num - 1));
221                 reloc_size = entries * sizeof(struct drm_i915_gem_relocation_entry);
222                 reloc = mmap(NULL, reloc_size, PROT_READ | PROT_WRITE,
223                              MAP_PRIVATE | MAP_ANON, -1, 0);
224                 igt_assert(reloc != MAP_FAILED);
225
226                 /* Allocate the handles we'll need to wrap. */
227                 handles = calloc(num, sizeof(*handles));
228                 for (int i = 0; i < num; i++)
229                         handles[i] = gem_create(fd, 4096);
230
231                 if (intel_gen(intel_get_drm_devid(fd)) >= 6)
232                         ring = I915_EXEC_BLT;
233                 else
234                         ring = 0;
235
236                 /* Create relocation objects. */
237                 execobjs = calloc(num, sizeof(*execobjs));
238                 execbuf.buffers_ptr = (uintptr_t)execobjs;
239                 execbuf.batch_start_offset = 0;
240                 execbuf.batch_len = 8;
241                 execbuf.cliprects_ptr = 0;
242                 execbuf.num_cliprects = 0;
243                 execbuf.DR1 = 0;
244                 execbuf.DR4 = 0;
245                 execbuf.flags = ring;
246                 i915_execbuffer2_set_context_id(execbuf, 0);
247                 execbuf.rsvd2 = 0;
248
249                 batch_handle = gem_create(fd, 4096);
250
251                 gem_write(fd, batch_handle, 0, batch_data, sizeof(batch_data));
252         }
253
254         reloc_tests();
255
256         source_offset_tests();
257
258         igt_fixture {
259                 gem_close(fd, batch_handle);
260                 close(fd);
261         }
262
263         igt_exit();
264 }