wayland-shm.c 16.2 KB
Newer Older
Benjamin Franzke's avatar
Benjamin Franzke committed
1 2 3
/*
 * Copyright © 2008 Kristian Høgsberg
 *
4 5 6 7 8 9 10
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
Benjamin Franzke's avatar
Benjamin Franzke committed
11
 *
12 13 14 15 16 17 18 19 20 21 22 23
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
Benjamin Franzke's avatar
Benjamin Franzke committed
24 25 26 27 28 29 30
 *
 * Authors:
 *    Kristian Høgsberg <krh@bitplanet.net>
 *    Benjamin Franzke <benjaminfranzke@googlemail.com>
 *
 */

31 32
#define _GNU_SOURCE

33
#include <stdbool.h>
Benjamin Franzke's avatar
Benjamin Franzke committed
34 35
#include <stdio.h>
#include <stdlib.h>
Yong Bakos's avatar
Yong Bakos committed
36
#include <stdint.h>
Benjamin Franzke's avatar
Benjamin Franzke committed
37 38 39
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
40 41 42
#include <assert.h>
#include <signal.h>
#include <pthread.h>
Benjamin Franzke's avatar
Benjamin Franzke committed
43

44
#include "wayland-util.h"
45
#include "wayland-private.h"
Benjamin Franzke's avatar
Benjamin Franzke committed
46 47
#include "wayland-server.h"

48 49 50 51 52 53 54
/* This once_t is used to synchronize installing the SIGBUS handler
 * and creating the TLS key. This will be done in the first call
 * wl_shm_buffer_begin_access which can happen from any thread */
static pthread_once_t wl_shm_sigbus_once = PTHREAD_ONCE_INIT;
static pthread_key_t wl_shm_sigbus_data_key;
static struct sigaction wl_shm_old_sigbus_action;

55
struct wl_shm_pool {
56
	struct wl_resource *resource;
57 58
	int internal_refcount;
	int external_refcount;
59
	char *data;
60
	int32_t size;
61
	int32_t new_size;
62 63
};

Benjamin Franzke's avatar
Benjamin Franzke committed
64
struct wl_shm_buffer {
65 66
	struct wl_resource *resource;
	int32_t width, height;
Benjamin Franzke's avatar
Benjamin Franzke committed
67
	int32_t stride;
68
	uint32_t format;
69
	int offset;
70
	struct wl_shm_pool *pool;
Benjamin Franzke's avatar
Benjamin Franzke committed
71 72
};

73 74 75 76 77 78
struct wl_shm_sigbus_data {
	struct wl_shm_pool *current_pool;
	int access_count;
	int fallback_mapping_used;
};

79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
static void
shm_pool_finish_resize(struct wl_shm_pool *pool)
{
	void *data;

	if (pool->size == pool->new_size)
		return;

	data = mremap(pool->data, pool->size, pool->new_size, MREMAP_MAYMOVE);
	if (data == MAP_FAILED) {
		wl_resource_post_error(pool->resource,
				       WL_SHM_ERROR_INVALID_FD,
				       "failed mremap");
		return;
	}

	pool->data = data;
	pool->size = pool->new_size;
}

99
static void
100
shm_pool_unref(struct wl_shm_pool *pool, bool external)
101
{
102
	if (external) {
103
		pool->external_refcount--;
104 105 106
		if (pool->external_refcount == 0)
			shm_pool_finish_resize(pool);
	} else {
107
		pool->internal_refcount--;
108
	}
109 110

	if (pool->internal_refcount + pool->external_refcount)
111 112 113 114 115 116
		return;

	munmap(pool->data, pool->size);
	free(pool);
}

Benjamin Franzke's avatar
Benjamin Franzke committed
117
static void
118
destroy_buffer(struct wl_resource *resource)
Benjamin Franzke's avatar
Benjamin Franzke committed
119
{
120
	struct wl_shm_buffer *buffer = wl_resource_get_user_data(resource);
Benjamin Franzke's avatar
Benjamin Franzke committed
121

122
	if (buffer->pool)
123
		shm_pool_unref(buffer->pool, false);
Benjamin Franzke's avatar
Benjamin Franzke committed
124 125 126 127
	free(buffer);
}

static void
128
shm_buffer_destroy(struct wl_client *client, struct wl_resource *resource)
Benjamin Franzke's avatar
Benjamin Franzke committed
129
{
130
	wl_resource_destroy(resource);
Benjamin Franzke's avatar
Benjamin Franzke committed
131 132
}

133
static const struct wl_buffer_interface shm_buffer_interface = {
Benjamin Franzke's avatar
Benjamin Franzke committed
134 135 136
	shm_buffer_destroy
};

137
static bool
138 139 140 141 142 143 144 145 146
format_is_supported(struct wl_client *client, uint32_t format)
{
	struct wl_display *display = wl_client_get_display(client);
	struct wl_array *formats;
	uint32_t *p;

	switch (format) {
	case WL_SHM_FORMAT_ARGB8888:
	case WL_SHM_FORMAT_XRGB8888:
147
		return true;
148 149 150
	default:
		formats = wl_display_get_additional_shm_formats(display);
		wl_array_for_each(p, formats)
151
			if (*p == format)
152
				return true;
153 154
	}

155
	return false;
156 157
}

158 159 160 161 162
static void
shm_pool_create_buffer(struct wl_client *client, struct wl_resource *resource,
		       uint32_t id, int32_t offset,
		       int32_t width, int32_t height,
		       int32_t stride, uint32_t format)
Benjamin Franzke's avatar
Benjamin Franzke committed
163
{
164
	struct wl_shm_pool *pool = wl_resource_get_user_data(resource);
Benjamin Franzke's avatar
Benjamin Franzke committed
165 166
	struct wl_shm_buffer *buffer;

167
	if (!format_is_supported(client, format)) {
168 169
		wl_resource_post_error(resource,
				       WL_SHM_ERROR_INVALID_FORMAT,
170
				       "invalid format 0x%x", format);
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
		return;
	}

	if (offset < 0 || width <= 0 || height <= 0 || stride < width ||
	    INT32_MAX / stride <= height ||
	    offset > pool->size - stride * height) {
		wl_resource_post_error(resource,
				       WL_SHM_ERROR_INVALID_STRIDE,
				       "invalid width, height or stride (%dx%d, %u)",
				       width, height, stride);
		return;
	}

	buffer = malloc(sizeof *buffer);
	if (buffer == NULL) {
186
		wl_client_post_no_memory(client);
187 188
		return;
	}
Benjamin Franzke's avatar
Benjamin Franzke committed
189

190 191
	buffer->width = width;
	buffer->height = height;
192
	buffer->format = format;
Benjamin Franzke's avatar
Benjamin Franzke committed
193
	buffer->stride = stride;
194
	buffer->offset = offset;
195
	buffer->pool = pool;
196
	pool->internal_refcount++;
Benjamin Franzke's avatar
Benjamin Franzke committed
197

198 199
	buffer->resource =
		wl_resource_create(client, &wl_buffer_interface, 1, id);
200 201
	if (buffer->resource == NULL) {
		wl_client_post_no_memory(client);
202
		shm_pool_unref(pool, false);
203 204 205 206
		free(buffer);
		return;
	}

207 208 209
	wl_resource_set_implementation(buffer->resource,
				       &shm_buffer_interface,
				       buffer, destroy_buffer);
Benjamin Franzke's avatar
Benjamin Franzke committed
210 211 212
}

static void
213
destroy_pool(struct wl_resource *resource)
Benjamin Franzke's avatar
Benjamin Franzke committed
214
{
215
	struct wl_shm_pool *pool = wl_resource_get_user_data(resource);
Benjamin Franzke's avatar
Benjamin Franzke committed
216

217
	shm_pool_unref(pool, false);
218
}
219

220 221 222
static void
shm_pool_destroy(struct wl_client *client, struct wl_resource *resource)
{
223
	wl_resource_destroy(resource);
224 225
}

226 227 228 229
static void
shm_pool_resize(struct wl_client *client, struct wl_resource *resource,
		int32_t size)
{
230
	struct wl_shm_pool *pool = wl_resource_get_user_data(resource);
231

232 233 234 235 236 237
	if (size < pool->size) {
		wl_resource_post_error(resource,
				       WL_SHM_ERROR_INVALID_FD,
				       "shrinking pool invalid");
		return;
	}
238

239
	pool->new_size = size;
240

241 242 243 244 245 246 247
	/* If the compositor has taken references on this pool it
	 * may be caching pointers into it. In that case we
	 * defer the resize (which may move the entire mapping)
	 * until the compositor finishes dereferencing the pool.
	 */
	if (pool->external_refcount == 0)
		shm_pool_finish_resize(pool);
248 249
}

250 251
struct wl_shm_pool_interface shm_pool_interface = {
	shm_pool_create_buffer,
252 253
	shm_pool_destroy,
	shm_pool_resize
254 255 256 257 258 259 260 261 262
};

static void
shm_create_pool(struct wl_client *client, struct wl_resource *resource,
		uint32_t id, int fd, int32_t size)
{
	struct wl_shm_pool *pool;

	if (size <= 0) {
263 264
		wl_resource_post_error(resource,
				       WL_SHM_ERROR_INVALID_STRIDE,
265
				       "invalid size (%d)", size);
266 267 268 269 270 271 272
		goto err_close;
	}

	pool = malloc(sizeof *pool);
	if (pool == NULL) {
		wl_client_post_no_memory(client);
		goto err_close;
Benjamin Franzke's avatar
Benjamin Franzke committed
273 274
	}

275 276
	pool->internal_refcount = 1;
	pool->external_refcount = 0;
277
	pool->size = size;
278
	pool->new_size = size;
279 280 281
	pool->data = mmap(NULL, size,
			  PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (pool->data == MAP_FAILED) {
282 283 284
		wl_resource_post_error(resource,
				       WL_SHM_ERROR_INVALID_FD,
				       "failed mmap fd %d", fd);
285
		goto err_free;
Benjamin Franzke's avatar
Benjamin Franzke committed
286
	}
287
	close(fd);
Benjamin Franzke's avatar
Benjamin Franzke committed
288

289 290
	pool->resource =
		wl_resource_create(client, &wl_shm_pool_interface, 1, id);
291 292 293 294 295 296
	if (!pool->resource) {
		wl_client_post_no_memory(client);
		munmap(pool->data, pool->size);
		free(pool);
		return;
	}
297

298 299 300
	wl_resource_set_implementation(pool->resource,
				       &shm_pool_interface,
				       pool, destroy_pool);
301

302 303
	return;

304 305
err_free:
	free(pool);
306 307
err_close:
	close(fd);
Benjamin Franzke's avatar
Benjamin Franzke committed
308 309
}

310
static const struct wl_shm_interface shm_interface = {
311
	shm_create_pool
Benjamin Franzke's avatar
Benjamin Franzke committed
312 313
};

314 315 316 317
static void
bind_shm(struct wl_client *client,
	 void *data, uint32_t version, uint32_t id)
{
318
	struct wl_resource *resource;
319 320 321
	struct wl_display *display = wl_client_get_display(client);
	struct wl_array *additional_formats;
	uint32_t *p;
322

323
	resource = wl_resource_create(client, &wl_shm_interface, 1, id);
324 325 326 327 328
	if (!resource) {
		wl_client_post_no_memory(client);
		return;
	}

329
	wl_resource_set_implementation(resource, &shm_interface, data, NULL);
330

331 332
	wl_shm_send_format(resource, WL_SHM_FORMAT_ARGB8888);
	wl_shm_send_format(resource, WL_SHM_FORMAT_XRGB8888);
333 334 335 336

	additional_formats = wl_display_get_additional_shm_formats(display);
	wl_array_for_each(p, additional_formats)
		wl_shm_send_format(resource, *p);
337
}
Benjamin Franzke's avatar
Benjamin Franzke committed
338

339 340
WL_EXPORT int
wl_display_init_shm(struct wl_display *display)
Benjamin Franzke's avatar
Benjamin Franzke committed
341
{
342
	if (!wl_global_create(display, &wl_shm_interface, 1, NULL, bind_shm))
343
		return -1;
Benjamin Franzke's avatar
Benjamin Franzke committed
344

345
	return 0;
Benjamin Franzke's avatar
Benjamin Franzke committed
346 347
}

348 349 350
WL_EXPORT struct wl_shm_buffer *
wl_shm_buffer_get(struct wl_resource *resource)
{
351 352 353
	if (resource == NULL)
		return NULL;

354 355 356 357 358 359 360
	if (wl_resource_instance_of(resource, &wl_buffer_interface,
				    &shm_buffer_interface))
		return wl_resource_get_user_data(resource);
	else
		return NULL;
}

Benjamin Franzke's avatar
Benjamin Franzke committed
361
WL_EXPORT int32_t
362
wl_shm_buffer_get_stride(struct wl_shm_buffer *buffer)
Benjamin Franzke's avatar
Benjamin Franzke committed
363 364 365 366
{
	return buffer->stride;
}

367 368 369 370 371 372 373 374

/** Get a pointer to the memory for the SHM buffer
 *
 * \param buffer The buffer object
 *
 * Returns a pointer which can be used to read the data contained in
 * the given SHM buffer.
 *
Derek Foreman's avatar
Derek Foreman committed
375
 * As this buffer is memory-mapped, reading from it may generate
376 377 378 379 380 381 382 383
 * SIGBUS signals. This can happen if the client claims that the
 * buffer is larger than it is or if something truncates the
 * underlying file. To prevent this signal from causing the compositor
 * to crash you should call wl_shm_buffer_begin_access and
 * wl_shm_buffer_end_access around code that reads from the memory.
 *
 * \memberof wl_shm_buffer
 */
Benjamin Franzke's avatar
Benjamin Franzke committed
384
WL_EXPORT void *
385
wl_shm_buffer_get_data(struct wl_shm_buffer *buffer)
Benjamin Franzke's avatar
Benjamin Franzke committed
386
{
387 388 389 390 391
	assert(buffer->pool);

	if (!buffer->pool)
		return NULL;

392 393 394 395 396
	if (buffer->pool->external_refcount &&
	    (buffer->pool->size != buffer->pool->new_size))
		wl_log("Buffer address requested when its parent pool "
		       "has an external reference and a deferred resize "
		       "pending.\n");
397
	return buffer->pool->data + buffer->offset;
Benjamin Franzke's avatar
Benjamin Franzke committed
398
}
399 400

WL_EXPORT uint32_t
401
wl_shm_buffer_get_format(struct wl_shm_buffer *buffer)
402 403 404
{
	return buffer->format;
}
405

406
WL_EXPORT int32_t
407
wl_shm_buffer_get_width(struct wl_shm_buffer *buffer)
408
{
409
	return buffer->width;
410 411
}

412
WL_EXPORT int32_t
413
wl_shm_buffer_get_height(struct wl_shm_buffer *buffer)
414
{
415
	return buffer->height;
416
}
417

418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
/** Get a reference to a shm_buffer's shm_pool
 *
 * \param buffer The buffer object
 *
 * Returns a pointer to a buffer's shm_pool and increases the
 * shm_pool refcount.
 *
 * The compositor must remember to call wl_shm_pool_unref when
 * it no longer needs the reference to ensure proper destruction
 * of the pool.
 *
 * \memberof wl_shm_buffer
 * \sa wl_shm_pool_unref
 */
WL_EXPORT struct wl_shm_pool *
wl_shm_buffer_ref_pool(struct wl_shm_buffer *buffer)
{
435 436
	assert(buffer->pool->internal_refcount +
	       buffer->pool->external_refcount);
437

438
	buffer->pool->external_refcount++;
439 440 441 442 443
	return buffer->pool;
}

/** Unreference a shm_pool
 *
444
 * \param pool The pool object
445 446 447 448 449 450 451 452 453 454 455 456 457
 *
 * Drops a reference to a wl_shm_pool object.
 *
 * This is only necessary if the compositor has explicitly
 * taken a reference with wl_shm_buffer_ref_pool(), otherwise
 * the pool will be automatically destroyed when appropriate.
 *
 * \memberof wl_shm_pool
 * \sa wl_shm_buffer_ref_pool
 */
WL_EXPORT void
wl_shm_pool_unref(struct wl_shm_pool *pool)
{
458
	shm_pool_unref(pool, true);
459 460
}

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
static void
reraise_sigbus(void)
{
	/* If SIGBUS is raised for some other reason than accessing
	 * the pool then we'll uninstall the signal handler so we can
	 * reraise it. This would presumably kill the process */
	sigaction(SIGBUS, &wl_shm_old_sigbus_action, NULL);
	raise(SIGBUS);
}

static void
sigbus_handler(int signum, siginfo_t *info, void *context)
{
	struct wl_shm_sigbus_data *sigbus_data =
		pthread_getspecific(wl_shm_sigbus_data_key);
	struct wl_shm_pool *pool;

	if (sigbus_data == NULL) {
		reraise_sigbus();
		return;
	}

	pool = sigbus_data->current_pool;

	/* If the offending address is outside the mapped space for
	 * the pool then the error is a real problem so we'll reraise
	 * the signal */
	if (pool == NULL ||
	    (char *) info->si_addr < pool->data ||
	    (char *) info->si_addr >= pool->data + pool->size) {
		reraise_sigbus();
		return;
	}

	sigbus_data->fallback_mapping_used = 1;

	/* This should replace the previous mapping */
	if (mmap(pool->data, pool->size,
		 PROT_READ | PROT_WRITE,
		 MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS,
		 0, 0) == (void *) -1) {
		reraise_sigbus();
		return;
	}
}

static void
destroy_sigbus_data(void *data)
{
	struct wl_shm_sigbus_data *sigbus_data = data;

	free(sigbus_data);
}

static void
init_sigbus_data_key(void)
{
	struct sigaction new_action = {
		.sa_sigaction = sigbus_handler,
		.sa_flags = SA_SIGINFO | SA_NODEFER
	};

	sigemptyset(&new_action.sa_mask);

	sigaction(SIGBUS, &new_action, &wl_shm_old_sigbus_action);

	pthread_key_create(&wl_shm_sigbus_data_key, destroy_sigbus_data);
}

530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
/** Mark that the given SHM buffer is about to be accessed
 *
 * \param buffer The SHM buffer
 *
 * An SHM buffer is a memory-mapped file given by the client.
 * According to POSIX, reading from a memory-mapped region that
 * extends off the end of the file will cause a SIGBUS signal to be
 * generated. Normally this would cause the compositor to terminate.
 * In order to make the compositor robust against clients that change
 * the size of the underlying file or lie about its size, you should
 * protect access to the buffer by calling this function before
 * reading from the memory and call wl_shm_buffer_end_access
 * afterwards. This will install a signal handler for SIGBUS which
 * will prevent the compositor from crashing.
 *
 * After calling this function the signal handler will remain
 * installed for the lifetime of the compositor process. Note that
 * this function will not work properly if the compositor is also
 * installing its own handler for SIGBUS.
 *
 * If a SIGBUS signal is received for an address within the range of
 * the SHM pool of the given buffer then the client will be sent an
 * error event when wl_shm_buffer_end_access is called. If the signal
 * is for an address outside that range then the signal handler will
 * reraise the signal which would will likely cause the compositor to
 * terminate.
 *
 * It is safe to nest calls to these functions as long as the nested
 * calls are all accessing the same buffer. The number of calls to
 * wl_shm_buffer_end_access must match the number of calls to
 * wl_shm_buffer_begin_access. These functions are thread-safe and it
 * is allowed to simultaneously access different buffers or the same
 * buffer from multiple threads.
 *
 * \memberof wl_shm_buffer
 */
566 567 568 569 570 571 572 573 574 575
WL_EXPORT void
wl_shm_buffer_begin_access(struct wl_shm_buffer *buffer)
{
	struct wl_shm_pool *pool = buffer->pool;
	struct wl_shm_sigbus_data *sigbus_data;

	pthread_once(&wl_shm_sigbus_once, init_sigbus_data_key);

	sigbus_data = pthread_getspecific(wl_shm_sigbus_data_key);
	if (sigbus_data == NULL) {
576
		sigbus_data = zalloc(sizeof *sigbus_data);
577 578 579 580 581 582 583 584 585 586 587 588 589
		if (sigbus_data == NULL)
			return;

		pthread_setspecific(wl_shm_sigbus_data_key, sigbus_data);
	}

	assert(sigbus_data->current_pool == NULL ||
	       sigbus_data->current_pool == pool);

	sigbus_data->current_pool = pool;
	sigbus_data->access_count++;
}

590 591 592 593 594 595 596 597 598 599 600
/** Ends the access to a buffer started by wl_shm_buffer_begin_access
 *
 * \param buffer The SHM buffer
 *
 * This should be called after wl_shm_buffer_begin_access once the
 * buffer is no longer being accessed. If a SIGBUS signal was
 * generated in-between these two calls then the resource for the
 * given buffer will be sent an error.
 *
 * \memberof wl_shm_buffer
 */
601 602 603 604 605 606
WL_EXPORT void
wl_shm_buffer_end_access(struct wl_shm_buffer *buffer)
{
	struct wl_shm_sigbus_data *sigbus_data =
		pthread_getspecific(wl_shm_sigbus_data_key);

607
	assert(sigbus_data && sigbus_data->access_count >= 1);
608 609 610 611 612 613 614 615 616 617 618 619

	if (--sigbus_data->access_count == 0) {
		if (sigbus_data->fallback_mapping_used) {
			wl_resource_post_error(buffer->resource,
					       WL_SHM_ERROR_INVALID_FD,
					       "error accessing SHM buffer");
			sigbus_data->fallback_mapping_used = 0;
		}

		sigbus_data->current_pool = NULL;
	}
}
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636

/** \cond */ /* Deprecated functions below. */

WL_EXPORT struct wl_shm_buffer *
wl_shm_buffer_create(struct wl_client *client,
		     uint32_t id, int32_t width, int32_t height,
		     int32_t stride, uint32_t format)
{
	return NULL;
}

/** \endcond */

/* Functions at the end of this file are deprecated.  Instead of adding new
 * code here, add it before the comment above that states:
 * Deprecated functions below.
 */