2 * Copyright © 2013 Google
3 * Copyright © 2013 Intel Corporation
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:
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
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
25 * Kees Cook <keescook@chromium.org>
26 * Daniel Vetter <daniel.vetter@ffwll.ch>
40 #include <sys/ioctl.h>
43 #include <sys/types.h>
47 #include "intel_gpu_tools.h"
50 * Testcase: Kernel relocation overflows are caught.
56 struct drm_i915_gem_exec_object2 *execobjs;
57 struct drm_i915_gem_execbuffer2 execbuf = { 0 };
58 struct drm_i915_gem_relocation_entry *reloc;
61 uint32_t batch_handle;
64 static void source_offset_tests(void)
66 struct drm_i915_gem_relocation_entry single_reloc;
69 handle = gem_create(fd, 4096);
71 execobjs[1].handle = batch_handle;
72 execobjs[1].relocation_count = 0;
73 execobjs[1].relocs_ptr = 0;
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;
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;
89 igt_assert(ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf) == 0);
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;
100 igt_assert(ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf) != 0);
101 igt_assert(errno == EINVAL);
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;
112 igt_assert(ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf) != 0);
113 igt_assert(errno == EINVAL);
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;
124 igt_assert(ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf) != 0);
125 igt_assert(errno == EINVAL);
129 execobjs[0].handle = batch_handle;
130 execobjs[0].relocation_count = 0;
131 execobjs[0].relocs_ptr = 0;
133 execbuf.buffer_count = 1;
136 igt_subtest("batch-start-unaligend") {
137 execbuf.batch_start_offset = 1;
138 execbuf.batch_len = 8;
140 igt_assert(ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf) != 0);
141 igt_assert(errno == EINVAL);
144 igt_subtest("batch-end-unaligend") {
145 execbuf.batch_start_offset = 0;
146 execbuf.batch_len = 7;
148 igt_assert(ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf) != 0);
149 igt_assert(errno == EINVAL);
153 gem_close(fd, handle);
156 static void reloc_tests(void)
159 unsigned int total_unsigned = 0;
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;
168 ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
169 igt_assert(errno == EFAULT);
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;
179 ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
180 igt_assert(errno == EINVAL);
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];
190 /* Wraps to 1 on last count. */
191 obj->relocation_count = 1 - total_unsigned;
192 obj->relocs_ptr = (uintptr_t)reloc;
194 obj->relocation_count = entries;
195 obj->relocs_ptr = (uintptr_t)reloc;
198 total_unsigned += obj->relocation_count;
200 execbuf.buffer_count = num;
203 ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
204 igt_assert(errno == EINVAL);
208 int main(int argc, char *argv[])
210 igt_subtest_init(argc, argv);
214 uint32_t batch_data [2] = { MI_NOOP, MI_BATCH_BUFFER_END };
218 /* Create giant reloc buffer area. */
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);
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);
231 if (intel_gen(intel_get_drm_devid(fd)) >= 6)
232 ring = I915_EXEC_BLT;
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;
245 execbuf.flags = ring;
246 i915_execbuffer2_set_context_id(execbuf, 0);
249 batch_handle = gem_create(fd, 4096);
251 gem_write(fd, batch_handle, 0, batch_data, sizeof(batch_data));
256 source_offset_tests();
259 gem_close(fd, batch_handle);