blob: 31f72cb84e71a5d44b6715bbb541956c0ac817c2 [file] [log] [blame]
Pekka Paalanenf8124772013-11-21 16:47:02 +02001/*
2 * Copyright © 2012 Intel Corporation
3 * Copyright © 2013 Collabora, Ltd.
4 *
Bryce Harrington2cc92972015-06-11 15:39:40 -07005 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Pekka Paalanenf8124772013-11-21 16:47:02 +020012 *
Bryce Harrington2cc92972015-06-11 15:39:40 -070013 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
Pekka Paalanenf8124772013-11-21 16:47:02 +020025 */
26
Andrew Wedgbury9cd661e2014-04-07 12:40:35 +010027#include "config.h"
28
Emmanuel Gil Peyrotdeae98e2019-07-22 15:06:20 +020029#include <errno.h>
30#include <fcntl.h>
31#include <stdlib.h>
32#include <string.h>
Pekka Paalanenf8124772013-11-21 16:47:02 +020033#include <unistd.h>
Pekka Paalanenf8124772013-11-21 16:47:02 +020034
Jon Cruz4678bab2015-06-15 15:37:07 -070035#include "shared/os-compatibility.h"
Pekka Paalanenf8124772013-11-21 16:47:02 +020036#include "weston-test-client-helper.h"
Pekka Paalanen701676d2019-11-13 15:45:10 +020037#include "weston-test-fixture-compositor.h"
38
39static enum test_result_code
40fixture_setup(struct weston_test_harness *harness)
41{
42 struct compositor_setup setup;
43
44 compositor_setup_defaults(&setup);
45
46 return weston_test_harness_execute_as_client(harness, &setup);
47}
48DECLARE_FIXTURE_SETUP(fixture_setup);
Pekka Paalanenf8124772013-11-21 16:47:02 +020049
Emmanuel Gil Peyrotdeae98e2019-07-22 15:06:20 +020050/* These three functions are copied from shared/os-compatibility.c in order to
51 * behave like older clients, and allow ftruncate() to shrink the file’s size,
52 * so SIGBUS can still happen.
53 *
54 * There is no reason not to use os_create_anonymous_file() otherwise. */
55
56#ifndef HAVE_MKOSTEMP
57static int
58set_cloexec_or_close(int fd)
59{
60 if (os_fd_set_cloexec(fd) != 0) {
61 close(fd);
62 return -1;
63 }
64 return fd;
65}
66#endif
67
68static int
69create_tmpfile_cloexec(char *tmpname)
70{
71 int fd;
72
73#ifdef HAVE_MKOSTEMP
74 fd = mkostemp(tmpname, O_CLOEXEC);
75 if (fd >= 0)
76 unlink(tmpname);
77#else
78 fd = mkstemp(tmpname);
79 if (fd >= 0) {
80 fd = set_cloexec_or_close(fd);
81 unlink(tmpname);
82 }
83#endif
84
85 return fd;
86}
87
88static int
89create_anonymous_file_without_seals(off_t size)
90{
91 static const char template[] = "/weston-test-XXXXXX";
92 const char *path;
93 char *name;
94 int fd;
95 int ret;
96
97 path = getenv("XDG_RUNTIME_DIR");
98 if (!path) {
99 errno = ENOENT;
100 return -1;
101 }
102
103 name = malloc(strlen(path) + sizeof(template));
104 if (!name)
105 return -1;
106
107 strcpy(name, path);
108 strcat(name, template);
109
110 fd = create_tmpfile_cloexec(name);
111
112 free(name);
113
114 if (fd < 0)
115 return -1;
116
117#ifdef HAVE_POSIX_FALLOCATE
118 do {
119 ret = posix_fallocate(fd, 0, size);
120 } while (ret == EINTR);
121 if (ret != 0) {
122 close(fd);
123 errno = ret;
124 return -1;
125 }
126#else
127 do {
128 ret = ftruncate(fd, size);
129 } while (ret < 0 && errno == EINTR);
130 if (ret < 0) {
131 close(fd);
132 return -1;
133 }
134#endif
135
136 return fd;
137}
138
Pekka Paalanenf8124772013-11-21 16:47:02 +0200139/* tests, that attempt to crash the compositor on purpose */
140
141static struct wl_buffer *
142create_bad_shm_buffer(struct client *client, int width, int height)
143{
144 struct wl_shm *shm = client->wl_shm;
145 int stride = width * 4;
146 int size = stride * height;
147 struct wl_shm_pool *pool;
148 struct wl_buffer *buffer;
149 int fd;
150
Emmanuel Gil Peyrotdeae98e2019-07-22 15:06:20 +0200151 fd = create_anonymous_file_without_seals(size);
Pekka Paalanenf8124772013-11-21 16:47:02 +0200152 assert(fd >= 0);
153
154 pool = wl_shm_create_pool(shm, fd, size);
155 buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
156 WL_SHM_FORMAT_ARGB8888);
157 wl_shm_pool_destroy(pool);
158
159 /* Truncate the file to a small size, so that the compositor
160 * will access it out-of-bounds, and hit SIGBUS.
161 */
162 assert(ftruncate(fd, 12) == 0);
163 close(fd);
164
165 return buffer;
166}
167
Marek Chalupacfff3122014-07-16 11:40:25 +0200168TEST(test_truncated_shm_file)
Pekka Paalanenf8124772013-11-21 16:47:02 +0200169{
170 struct client *client;
171 struct wl_buffer *bad_buffer;
172 struct wl_surface *surface;
Pekka Paalanen819054c2021-06-11 16:54:12 +0300173 struct wl_callback *frame_cb;
Pekka Paalanenf8124772013-11-21 16:47:02 +0200174 int frame;
175
Pekka Paalanen4ac06ff2015-03-26 12:56:10 +0200176 client = create_client_and_test_surface(46, 76, 111, 134);
Pekka Paalanenf8124772013-11-21 16:47:02 +0200177 assert(client);
178 surface = client->surface->wl_surface;
179
180 bad_buffer = create_bad_shm_buffer(client, 200, 200);
181
Pekka Paalanenf8124772013-11-21 16:47:02 +0200182 wl_surface_attach(surface, bad_buffer, 0, 0);
183 wl_surface_damage(surface, 0, 0, 200, 200);
Pekka Paalanen819054c2021-06-11 16:54:12 +0300184 frame_cb = frame_callback_set(surface, &frame);
Pekka Paalanenf8124772013-11-21 16:47:02 +0200185 wl_surface_commit(surface);
Pekka Paalanen819054c2021-06-11 16:54:12 +0300186 if (!frame_callback_wait_nofail(client, &frame))
187 wl_callback_destroy(frame_cb);
Marek Chalupacfff3122014-07-16 11:40:25 +0200188
189 expect_protocol_error(client, &wl_buffer_interface,
190 WL_SHM_ERROR_INVALID_FD);
Pekka Paalanen819054c2021-06-11 16:54:12 +0300191
192 wl_buffer_destroy(bad_buffer);
193 client_destroy(client);
Pekka Paalanenf8124772013-11-21 16:47:02 +0200194}