1 /*
2 * Copyright © 2012 Jonas Ådahl
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #define _GNU_SOURCE /* For memrchr */
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdbool.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/mman.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <assert.h>
37 #include <signal.h>
38
39 #include "wayland-client.h"
40 #include "wayland-server.h"
41 #include "test-runner.h"
42 #include "test-compositor.h"
43
44 #define ARRAY_LENGTH(a) (sizeof (a) / sizeof (a)[0])
45
46 static void
registry_handle_global(void * data,struct wl_registry * registry,uint32_t id,const char * interface,uint32_t version)47 registry_handle_global(void *data, struct wl_registry *registry,
48 uint32_t id, const char *interface, uint32_t version)
49 {
50 int *pcounter = data;
51 (*pcounter)++;
52 assert(*pcounter == 1);
53 wl_registry_destroy(registry);
54 }
55
56 static const struct wl_registry_listener registry_listener = {
57 registry_handle_global,
58 NULL
59 };
60
61 /* Test that destroying a proxy object doesn't result in any more
62 * callback being invoked, even though were many queued. */
63 static void
client_test_proxy_destroy(void)64 client_test_proxy_destroy(void)
65 {
66 struct wl_display *display;
67 struct wl_registry *registry;
68 int counter = 0;
69
70 display = wl_display_connect(NULL);
71 assert(display);
72
73 registry = wl_display_get_registry(display);
74 assert(registry != NULL);
75 wl_registry_add_listener(registry, ®istry_listener,
76 &counter);
77 assert(wl_display_roundtrip(display) != -1);
78
79 assert(counter == 1);
80
81 /* don't destroy the registry, we have already destroyed them
82 * in the global handler */
83 wl_display_disconnect(display);
84 }
85
86 struct multiple_queues_state {
87 struct wl_display *display;
88 struct wl_callback* callback2;
89 bool done;
90 };
91
92 static void
sync_callback(void * data,struct wl_callback * callback,uint32_t serial)93 sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
94 {
95 struct multiple_queues_state *state = data;
96
97 state->done = true;
98 wl_callback_destroy(callback);
99
100 wl_display_dispatch_pending(state->display);
101
102 wl_callback_destroy(state->callback2);
103 }
104
105 static const struct wl_callback_listener sync_listener = {
106 sync_callback
107 };
108
109 /* Test that when receiving the first of two synchronization
110 * callback events, destroying the second one doesn't cause any
111 * errors even if the delete_id event is handled out of order. */
112 static void
client_test_multiple_queues(void)113 client_test_multiple_queues(void)
114 {
115 struct wl_event_queue *queue;
116 struct wl_callback *callback1;
117 struct multiple_queues_state state;
118 int ret = 0;
119
120 state.display = wl_display_connect(NULL);
121 assert(state.display);
122
123 queue = wl_display_create_queue(state.display);
124 assert(queue);
125
126 state.done = false;
127 callback1 = wl_display_sync(state.display);
128 assert(callback1 != NULL);
129 wl_callback_add_listener(callback1, &sync_listener, &state);
130 wl_proxy_set_queue((struct wl_proxy *) callback1, queue);
131
132 state.callback2 = wl_display_sync(state.display);
133 assert(state.callback2 != NULL);
134 wl_callback_add_listener(state.callback2, &sync_listener, NULL);
135 wl_proxy_set_queue((struct wl_proxy *) state.callback2, queue);
136
137 wl_display_flush(state.display);
138
139 while (!state.done && !ret)
140 ret = wl_display_dispatch_queue(state.display, queue);
141
142 wl_event_queue_destroy(queue);
143 wl_display_disconnect(state.display);
144
145 exit(ret == -1 ? -1 : 0);
146 }
147
148 static void
sync_callback_roundtrip(void * data,struct wl_callback * callback,uint32_t serial)149 sync_callback_roundtrip(void *data, struct wl_callback *callback, uint32_t serial)
150 {
151 bool *done = data;
152 *done = true;
153 }
154
155 static const struct wl_callback_listener sync_listener_roundtrip = {
156 sync_callback_roundtrip
157 };
158
159 /* Test that doing a roundtrip on a queue only the events on that
160 * queue get dispatched. */
161 static void
client_test_queue_roundtrip(void)162 client_test_queue_roundtrip(void)
163 {
164 struct wl_event_queue *queue;
165 struct wl_callback *callback1;
166 struct wl_callback *callback2;
167 struct wl_display *display;
168 bool done1 = false;
169 bool done2 = false;
170
171 display = wl_display_connect(NULL);
172 assert(display);
173
174 queue = wl_display_create_queue(display);
175 assert(queue);
176
177 /* arm a callback on the default queue */
178 callback1 = wl_display_sync(display);
179 assert(callback1 != NULL);
180 wl_callback_add_listener(callback1, &sync_listener_roundtrip, &done1);
181
182 /* arm a callback on the other queue */
183 callback2 = wl_display_sync(display);
184 assert(callback2 != NULL);
185 wl_callback_add_listener(callback2, &sync_listener_roundtrip, &done2);
186 wl_proxy_set_queue((struct wl_proxy *) callback2, queue);
187
188 /* roundtrip on default queue must not dispatch the other queue. */
189 wl_display_roundtrip(display);
190 assert(done1 == true);
191 assert(done2 == false);
192
193 /* re-arm the sync callback on the default queue, so we see that
194 * wl_display_roundtrip_queue() does not dispatch the default queue. */
195 wl_callback_destroy(callback1);
196 done1 = false;
197 callback1 = wl_display_sync(display);
198 assert(callback1 != NULL);
199 wl_callback_add_listener(callback1, &sync_listener_roundtrip, &done1);
200
201 wl_display_roundtrip_queue(display, queue);
202 assert(done1 == false);
203 assert(done2 == true);
204
205 wl_callback_destroy(callback1);
206 wl_callback_destroy(callback2);
207 wl_event_queue_destroy(queue);
208
209 wl_display_disconnect(display);
210 }
211
212 static void
client_test_queue_proxy_wrapper(void)213 client_test_queue_proxy_wrapper(void)
214 {
215 struct wl_event_queue *queue;
216 struct wl_display *display;
217 struct wl_display *display_wrapper;
218 struct wl_callback *callback;
219 bool done = false;
220
221 /*
222 * For an illustration of what usage would normally fail without using
223 * proxy wrappers, see the `client_test_queue_set_queue_race' test case.
224 */
225
226 display = wl_display_connect(NULL);
227 assert(display);
228
229 /* Pretend we are in a separate thread where a thread-local queue is
230 * used. */
231 queue = wl_display_create_queue(display);
232 assert(queue);
233
234 display_wrapper = wl_proxy_create_wrapper(display);
235 assert(display_wrapper);
236 wl_proxy_set_queue((struct wl_proxy *) display_wrapper, queue);
237 callback = wl_display_sync(display_wrapper);
238 wl_proxy_wrapper_destroy(display_wrapper);
239 assert(callback != NULL);
240
241 /* Pretend we are now another thread and dispatch the dispatch the main
242 * queue while also knowing our callback is read and queued. */
243 wl_display_roundtrip(display);
244
245 /* Make sure that the pretend-to-be main thread didn't dispatch our
246 * callback, behind our back. */
247 wl_callback_add_listener(callback, &sync_listener_roundtrip, &done);
248 wl_display_flush(display);
249
250 assert(!done);
251
252 /* Make sure that we eventually end up dispatching our callback. */
253 while (!done)
254 assert(wl_display_dispatch_queue(display, queue) != -1);
255
256 wl_callback_destroy(callback);
257 wl_event_queue_destroy(queue);
258
259 wl_display_disconnect(display);
260 }
261
262 static void
client_test_queue_set_queue_race(void)263 client_test_queue_set_queue_race(void)
264 {
265 struct wl_event_queue *queue;
266 struct wl_display *display;
267 struct wl_callback *callback;
268 bool done = false;
269
270 /*
271 * This test illustrates the multi threading scenario which would fail
272 * without doing what is done in the `client_test_queue_proxy_wrapper'
273 * test.
274 */
275
276 display = wl_display_connect(NULL);
277 assert(display);
278
279 /* Pretend we are in a separate thread where a thread-local queue is
280 * used. */
281 queue = wl_display_create_queue(display);
282 assert(queue);
283
284 callback = wl_display_sync(display);
285 assert(callback != NULL);
286
287 /* Pretend we are now another thread and dispatch the dispatch the main
288 * queue while also knowing our callback is read, queued on the wrong
289 * queue, and dispatched. */
290 wl_display_roundtrip(display);
291
292 /* Pretend we are back in the separate thread, and continue with setting
293 * up our callback. */
294 wl_callback_add_listener(callback, &sync_listener_roundtrip, &done);
295 wl_proxy_set_queue((struct wl_proxy *) callback, queue);
296
297 /* Roundtrip our separate thread queue to make sure any events are
298 * dispatched. */
299 wl_display_roundtrip_queue(display, queue);
300
301 /* Verify that the callback has indeed been dropped. */
302 assert(!done);
303
304 wl_callback_destroy(callback);
305 wl_event_queue_destroy(queue);
306
307 wl_display_disconnect(display);
308 }
309
310 static char *
maybe_map_file(int fd,size_t * len)311 maybe_map_file(int fd, size_t *len)
312 {
313 char *data;
314
315 *len = lseek(fd, 0, SEEK_END);
316 data = mmap(0, *len, PROT_READ, MAP_PRIVATE, fd, 0);
317
318 return data;
319 }
320
321 static char *
map_file(int fd,size_t * len)322 map_file(int fd, size_t *len)
323 {
324 char *data;
325
326 data = maybe_map_file(fd, len);
327 assert(data != MAP_FAILED && "Failed to mmap file");
328
329 return data;
330 }
331
332 static char *
last_line_of(char * s)333 last_line_of(char *s)
334 {
335 size_t len = strlen(s);
336 char *last;
337
338 last = memrchr(s, '\n', len);
339 /* If we found a newline at end of string, find the previous one. */
340 if (last && last[1] == 0)
341 last = memrchr(s, '\n', len - 1);
342 /* If we have a newline, the last line starts after the newline.
343 * Otherwise, the whole string is the last line. */
344 if (last)
345 last += 1;
346 else
347 last = s;
348
349 return last;
350 }
351
352 static void
client_test_queue_destroy_with_attached_proxies(void)353 client_test_queue_destroy_with_attached_proxies(void)
354 {
355 struct wl_event_queue *queue;
356 struct wl_display *display;
357 struct wl_display *display_wrapper;
358 struct wl_callback *callback;
359 char *log;
360 size_t log_len;
361 char callback_name[24];
362 int ret;
363
364 display = wl_display_connect(NULL);
365 assert(display);
366
367 /* Pretend we are in a separate thread where a thread-local queue is
368 * used. */
369 queue = wl_display_create_queue(display);
370 assert(queue);
371
372 /* Create a sync dispatching events on the thread-local queue. */
373 display_wrapper = wl_proxy_create_wrapper(display);
374 assert(display_wrapper);
375 wl_proxy_set_queue((struct wl_proxy *) display_wrapper, queue);
376 callback = wl_display_sync(display_wrapper);
377 wl_proxy_wrapper_destroy(display_wrapper);
378 assert(callback != NULL);
379
380 /* Destroy the queue before the attached object. */
381 wl_event_queue_destroy(queue);
382
383 /* Check that the log contains some information about the attached
384 * wl_callback proxy. */
385 log = map_file(client_log_fd, &log_len);
386 ret = snprintf(callback_name, sizeof(callback_name), "wl_callback@%u",
387 wl_proxy_get_id((struct wl_proxy *) callback));
388 assert(ret > 0 && ret < (int)sizeof(callback_name) &&
389 "callback name creation failed (possibly truncated)");
390 assert(strstr(last_line_of(log), callback_name));
391 munmap(log, log_len);
392
393 wl_callback_destroy(callback);
394
395 wl_display_disconnect(display);
396 }
397
398 static void
client_test_queue_proxy_event_to_destroyed_queue(void)399 client_test_queue_proxy_event_to_destroyed_queue(void)
400 {
401 struct wl_event_queue *queue;
402 struct wl_display *display;
403 struct wl_display *display_wrapper;
404 struct wl_callback *callback;
405
406 display = wl_display_connect(NULL);
407 assert(display);
408
409 /* Pretend we are in a separate thread where a thread-local queue is
410 * used. */
411 queue = wl_display_create_queue(display);
412 assert(queue);
413
414 /* Create a sync dispatching events on the thread-local queue. */
415 display_wrapper = wl_proxy_create_wrapper(display);
416 assert(display_wrapper);
417 wl_proxy_set_queue((struct wl_proxy *) display_wrapper, queue);
418 callback = wl_display_sync(display_wrapper);
419 wl_proxy_wrapper_destroy(display_wrapper);
420 assert(callback != NULL);
421 wl_display_flush(display);
422
423 /* Destroy the queue before the attached object. */
424 wl_event_queue_destroy(queue);
425
426 /* During this roundtrip we should receive the done event on 'callback',
427 * try to queue it to the destroyed queue, and abort. */
428 wl_display_roundtrip(display);
429
430 wl_callback_destroy(callback);
431
432 wl_display_disconnect(display);
433 }
434
435 static void
client_test_queue_destroy_default_with_attached_proxies(void)436 client_test_queue_destroy_default_with_attached_proxies(void)
437 {
438 struct wl_display *display;
439 struct wl_callback *callback;
440 char *log;
441 size_t log_len;
442 char callback_name[24];
443 int ret;
444
445 display = wl_display_connect(NULL);
446 assert(display);
447
448 /* Create a sync dispatching events on the default queue. */
449 callback = wl_display_sync(display);
450 assert(callback != NULL);
451
452 /* Destroy the default queue (by disconnecting) before the attached
453 * object. */
454 wl_display_disconnect(display);
455
456 /* Check that the log does not contain any warning about the attached
457 * wl_callback proxy. */
458 log = maybe_map_file(client_log_fd, &log_len);
459 ret = snprintf(callback_name, sizeof(callback_name), "wl_callback@%u",
460 wl_proxy_get_id((struct wl_proxy *) callback));
461 assert(ret > 0 && ret < (int)sizeof(callback_name) &&
462 "callback name creation failed (possibly truncated)");
463 assert(log == MAP_FAILED || strstr(log, callback_name) == NULL);
464 if (log != MAP_FAILED)
465 munmap(log, log_len);
466
467 /* HACK: Directly free the memory of the wl_callback proxy to appease
468 * ASan. We would normally use wl_callback_destroy(), but since we have
469 * destroyed the associated wl_display, using this function would lead
470 * to memory errors. */
471 free(callback);
472 }
473
474 static void
dummy_bind(struct wl_client * client,void * data,uint32_t version,uint32_t id)475 dummy_bind(struct wl_client *client,
476 void *data, uint32_t version, uint32_t id)
477 {
478 }
479
TEST(queue_proxy_destroy)480 TEST(queue_proxy_destroy)
481 {
482 struct display *d;
483 const struct wl_interface *dummy_interfaces[] = {
484 &wl_seat_interface,
485 &wl_pointer_interface,
486 &wl_keyboard_interface,
487 &wl_surface_interface
488 };
489 unsigned int i;
490
491 d = display_create();
492
493 for (i = 0; i < ARRAY_LENGTH(dummy_interfaces); i++)
494 wl_global_create(d->wl_display, dummy_interfaces[i],
495 dummy_interfaces[i]->version,
496 NULL, dummy_bind);
497
498 test_set_timeout(2);
499
500 client_create_noarg(d, client_test_proxy_destroy);
501 display_run(d);
502
503 display_destroy(d);
504 }
505
TEST(queue_multiple_queues)506 TEST(queue_multiple_queues)
507 {
508 struct display *d = display_create();
509
510 test_set_timeout(2);
511
512 client_create_noarg(d, client_test_multiple_queues);
513 display_run(d);
514
515 display_destroy(d);
516 }
517
TEST(queue_roundtrip)518 TEST(queue_roundtrip)
519 {
520 struct display *d = display_create();
521
522 test_set_timeout(2);
523
524 client_create_noarg(d, client_test_queue_roundtrip);
525 display_run(d);
526
527 display_destroy(d);
528 }
529
TEST(queue_set_queue_proxy_wrapper)530 TEST(queue_set_queue_proxy_wrapper)
531 {
532 struct display *d = display_create();
533
534 test_set_timeout(2);
535
536 client_create_noarg(d, client_test_queue_proxy_wrapper);
537 display_run(d);
538
539 display_destroy(d);
540 }
541
TEST(queue_set_queue_race)542 TEST(queue_set_queue_race)
543 {
544 struct display *d = display_create();
545
546 test_set_timeout(2);
547
548 client_create_noarg(d, client_test_queue_set_queue_race);
549 display_run(d);
550
551 display_destroy(d);
552 }
553
TEST(queue_destroy_with_attached_proxies)554 TEST(queue_destroy_with_attached_proxies)
555 {
556 struct display *d = display_create();
557
558 test_set_timeout(2);
559
560 client_create_noarg(d, client_test_queue_destroy_with_attached_proxies);
561 display_run(d);
562
563 display_destroy(d);
564 }
565
TEST(queue_proxy_event_to_destroyed_queue)566 TEST(queue_proxy_event_to_destroyed_queue)
567 {
568 struct display *d = display_create();
569 struct client_info *ci;
570 char *client_log;
571 size_t client_log_len;
572
573 test_set_timeout(2);
574
575 ci = client_create_noarg(d, client_test_queue_proxy_event_to_destroyed_queue);
576 display_run(d);
577
578 /* Check that the final line in the log mentions the expected reason
579 * for the abort. */
580 client_log = map_file(ci->log_fd, &client_log_len);
581 assert(!strcmp(last_line_of(client_log),
582 "Tried to add event to destroyed queue\n"));
583 munmap(client_log, client_log_len);
584
585 /* Check that the client aborted. */
586 display_destroy_expect_signal(d, SIGABRT);
587 }
588
TEST(queue_destroy_default_with_attached_proxies)589 TEST(queue_destroy_default_with_attached_proxies)
590 {
591 struct display *d = display_create();
592
593 test_set_timeout(2);
594
595 client_create_noarg(d, client_test_queue_destroy_default_with_attached_proxies);
596 display_run(d);
597
598 display_destroy(d);
599 }
600