Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Intel Corporation |
| 3 | * Copyright © 2013 Collabora, Ltd. |
| 4 | * |
| 5 | * Permission to use, copy, modify, distribute, and sell this software and |
| 6 | * its documentation for any purpose is hereby granted without fee, provided |
| 7 | * that the above copyright notice appear in all copies and that both that |
| 8 | * copyright notice and this permission notice appear in supporting |
| 9 | * documentation, and that the name of the copyright holders not be used in |
| 10 | * advertising or publicity pertaining to distribution of the software |
| 11 | * without specific, written prior permission. The copyright holders make |
| 12 | * no representations about the suitability of this software for any |
| 13 | * purpose. It is provided "as is" without express or implied warranty. |
| 14 | * |
| 15 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS |
| 16 | * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 18 | * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
| 19 | * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
| 20 | * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 21 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | */ |
| 23 | |
Andrew Wedgbury | 9cd661e | 2014-04-07 12:40:35 +0100 | [diff] [blame] | 24 | #include "config.h" |
| 25 | |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | #include <sys/types.h> |
Marek Chalupa | 06f2fa1 | 2014-04-11 11:48:55 +0200 | [diff] [blame^] | 28 | #include <stdio.h> |
| 29 | #include <signal.h> |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 30 | |
| 31 | #include "../shared/os-compatibility.h" |
| 32 | #include "weston-test-client-helper.h" |
| 33 | |
| 34 | /* tests, that attempt to crash the compositor on purpose */ |
| 35 | |
| 36 | static struct wl_buffer * |
| 37 | create_bad_shm_buffer(struct client *client, int width, int height) |
| 38 | { |
| 39 | struct wl_shm *shm = client->wl_shm; |
| 40 | int stride = width * 4; |
| 41 | int size = stride * height; |
| 42 | struct wl_shm_pool *pool; |
| 43 | struct wl_buffer *buffer; |
| 44 | int fd; |
| 45 | |
| 46 | fd = os_create_anonymous_file(size); |
| 47 | assert(fd >= 0); |
| 48 | |
| 49 | pool = wl_shm_create_pool(shm, fd, size); |
| 50 | buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, |
| 51 | WL_SHM_FORMAT_ARGB8888); |
| 52 | wl_shm_pool_destroy(pool); |
| 53 | |
| 54 | /* Truncate the file to a small size, so that the compositor |
| 55 | * will access it out-of-bounds, and hit SIGBUS. |
| 56 | */ |
| 57 | assert(ftruncate(fd, 12) == 0); |
| 58 | close(fd); |
| 59 | |
| 60 | return buffer; |
| 61 | } |
| 62 | |
Marek Chalupa | 06f2fa1 | 2014-04-11 11:48:55 +0200 | [diff] [blame^] | 63 | static void sighandler(int signum) |
| 64 | { |
| 65 | /* this means failure */ |
| 66 | exit(0); |
| 67 | } |
| 68 | |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 69 | FAIL_TEST(test_truncated_shm_file) |
| 70 | { |
| 71 | struct client *client; |
| 72 | struct wl_buffer *bad_buffer; |
| 73 | struct wl_surface *surface; |
| 74 | int frame; |
Marek Chalupa | 06f2fa1 | 2014-04-11 11:48:55 +0200 | [diff] [blame^] | 75 | struct sigaction new_action, old_action; |
| 76 | |
| 77 | /* until the bad buffer creation, the SIGABRT or SIGSEGV signals |
| 78 | * should fail the test. That means returning 0 */ |
| 79 | new_action.sa_handler = sighandler; |
| 80 | sigemptyset(&new_action.sa_mask); |
| 81 | new_action.sa_flags = 0; |
| 82 | |
| 83 | if (sigaction(SIGSEGV, &new_action, NULL) != 0) { |
| 84 | fprintf(stderr, "Failed setting new sigaction for SIGSEGV"); |
| 85 | exit(0); |
| 86 | } |
| 87 | if (sigaction(SIGABRT, &new_action, &old_action) != 0) { |
| 88 | fprintf(stderr, "Failed setting new sigaction for SIGABRT"); |
| 89 | exit(0); |
| 90 | } |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 91 | |
| 92 | client = client_create(46, 76, 111, 134); |
| 93 | assert(client); |
| 94 | surface = client->surface->wl_surface; |
| 95 | |
| 96 | bad_buffer = create_bad_shm_buffer(client, 200, 200); |
| 97 | |
Marek Chalupa | 06f2fa1 | 2014-04-11 11:48:55 +0200 | [diff] [blame^] | 98 | /* from this point we expect the signal */ |
| 99 | if (sigaction(SIGABRT, &old_action, NULL) != 0) { |
| 100 | fprintf(stderr, "Failed setting old sigaction for SIGABRT"); |
| 101 | exit(0); |
| 102 | } |
| 103 | |
Pekka Paalanen | f812477 | 2013-11-21 16:47:02 +0200 | [diff] [blame] | 104 | wl_surface_attach(surface, bad_buffer, 0, 0); |
| 105 | wl_surface_damage(surface, 0, 0, 200, 200); |
| 106 | frame_callback_set(surface, &frame); |
| 107 | wl_surface_commit(surface); |
| 108 | frame_callback_wait(client, &frame); |
| 109 | } |