xref: /aosp_15_r20/external/wayland/tests/connection-test.c (revision 84e872a0dc482bffdb63672969dd03a827d67c73)
1 /*
2  * Copyright © 2012 Intel Corporation
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 #include <math.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <assert.h>
33 #include <sys/socket.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <poll.h>
39 
40 #include "wayland-private.h"
41 #include "test-runner.h"
42 #include "test-compositor.h"
43 
44 static const char message[] = "Hello, world";
45 
46 static struct wl_connection *
setup(int * s)47 setup(int *s)
48 {
49 	struct wl_connection *connection;
50 
51 	assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
52 
53 	connection = wl_connection_create(s[0]);
54 	assert(connection);
55 
56 	return connection;
57 }
58 
TEST(connection_create)59 TEST(connection_create)
60 {
61 	struct wl_connection *connection;
62 	int s[2];
63 
64 	connection = setup(s);
65 	wl_connection_destroy(connection);
66 	close(s[0]);
67 	close(s[1]);
68 }
69 
TEST(connection_write)70 TEST(connection_write)
71 {
72 	struct wl_connection *connection;
73 	int s[2];
74 	char buffer[64];
75 
76 	connection = setup(s);
77 
78 	assert(wl_connection_write(connection, message, sizeof message) == 0);
79 	assert(wl_connection_flush(connection) == sizeof message);
80 	assert(read(s[1], buffer, sizeof buffer) == sizeof message);
81 	assert(memcmp(message, buffer, sizeof message) == 0);
82 
83 	wl_connection_destroy(connection);
84 	close(s[0]);
85 	close(s[1]);
86 }
87 
TEST(connection_data)88 TEST(connection_data)
89 {
90 	struct wl_connection *connection;
91 	int s[2];
92 	char buffer[64];
93 
94 	connection = setup(s);
95 
96 	assert(write(s[1], message, sizeof message) == sizeof message);
97 	assert(wl_connection_read(connection) == sizeof message);
98 	wl_connection_copy(connection, buffer, sizeof message);
99 	assert(memcmp(message, buffer, sizeof message) == 0);
100 	wl_connection_consume(connection, sizeof message);
101 
102 	wl_connection_destroy(connection);
103 	close(s[0]);
104 	close(s[1]);
105 }
106 
TEST(connection_queue)107 TEST(connection_queue)
108 {
109 	struct wl_connection *connection;
110 	int s[2];
111 	char buffer[64];
112 
113 	connection = setup(s);
114 
115 	/* Test that wl_connection_queue() puts data in the output
116 	 * buffer without flush it.  Verify that the data did get in
117 	 * the buffer by writing another message and making sure that
118 	 * we receive the two messages on the other fd. */
119 
120 	assert(wl_connection_queue(connection, message, sizeof message) == 0);
121 	assert(wl_connection_flush(connection) == 0);
122 	assert(wl_connection_write(connection, message, sizeof message) == 0);
123 	assert(wl_connection_flush(connection) == 2 * sizeof message);
124 	assert(read(s[1], buffer, sizeof buffer) == 2 * sizeof message);
125 	assert(memcmp(message, buffer, sizeof message) == 0);
126 	assert(memcmp(message, buffer + sizeof message, sizeof message) == 0);
127 
128 	wl_connection_destroy(connection);
129 	close(s[0]);
130 	close(s[1]);
131 }
132 
133 static void
va_list_wrapper(const char * signature,union wl_argument * args,int count,...)134 va_list_wrapper(const char *signature, union wl_argument *args, int count, ...)
135 {
136 	va_list ap;
137 	va_start(ap, count);
138 	wl_argument_from_va_list(signature, args, count, ap);
139 	va_end(ap);
140 }
141 
TEST(argument_from_va_list)142 TEST(argument_from_va_list)
143 {
144 	union wl_argument args[WL_CLOSURE_MAX_ARGS];
145 	struct wl_object fake_object, fake_new_object;
146 	struct wl_array fake_array;
147 
148 	va_list_wrapper("i", args, 1, 100);
149 	assert(args[0].i == 100);
150 
151 	va_list_wrapper("is", args, 2, 101, "value");
152 	assert(args[0].i == 101);
153 	assert(strcmp(args[1].s, "value") == 0);
154 
155 	va_list_wrapper("?iuf?sonah", args, 8,
156 			102, 103, wl_fixed_from_int(104), "value",
157 			&fake_object, &fake_new_object, &fake_array, 106);
158 	assert(args[0].i == 102);
159 	assert(args[1].u == 103);
160 	assert(args[2].f == wl_fixed_from_int(104));
161 	assert(strcmp(args[3].s, "value") == 0);
162 	assert(args[4].o == &fake_object);
163 	assert(args[5].o == &fake_new_object);
164 	assert(args[6].a == &fake_array);
165 	assert(args[7].h == 106);
166 }
167 
168 struct marshal_data {
169 	struct wl_connection *read_connection;
170 	struct wl_connection *write_connection;
171 	int s[2];
172 	uint32_t buffer[10];
173 	union {
174 		uint32_t u;
175 		int32_t i;
176 		const char *s;
177 		int h;
178 	} value;
179 };
180 
181 static void
setup_marshal_data(struct marshal_data * data)182 setup_marshal_data(struct marshal_data *data)
183 {
184 	assert(socketpair(AF_UNIX,
185 			  SOCK_STREAM | SOCK_CLOEXEC, 0, data->s) == 0);
186 	data->read_connection = wl_connection_create(data->s[0]);
187 	assert(data->read_connection);
188 	data->write_connection = wl_connection_create(data->s[1]);
189 	assert(data->write_connection);
190 }
191 
192 static void
release_marshal_data(struct marshal_data * data)193 release_marshal_data(struct marshal_data *data)
194 {
195 	close(wl_connection_destroy(data->read_connection));
196 	close(wl_connection_destroy(data->write_connection));
197 }
198 
199 static void
marshal(struct marshal_data * data,const char * format,int size,...)200 marshal(struct marshal_data *data, const char *format, int size, ...)
201 {
202 	struct wl_closure *closure;
203 	static const uint32_t opcode = 4444;
204 	static struct wl_object sender = { NULL, NULL, 1234 };
205 	struct wl_message message = { "test", format, NULL };
206 	va_list ap;
207 
208 	va_start(ap, size);
209 	closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
210 	va_end(ap);
211 
212 	assert(closure);
213 	assert(wl_closure_send(closure, data->write_connection) == 0);
214 	wl_closure_destroy(closure);
215 	assert(wl_connection_flush(data->write_connection) == size);
216 	assert(read(data->s[0], data->buffer, sizeof data->buffer) == size);
217 
218 	assert(data->buffer[0] == sender.id);
219 	assert(data->buffer[1] == (opcode | (size << 16)));
220 }
221 
TEST(connection_marshal)222 TEST(connection_marshal)
223 {
224 	struct marshal_data data;
225 	struct wl_object object;
226 	struct wl_array array;
227 	static const char text[] = "curry";
228 
229 	setup_marshal_data(&data);
230 
231 	marshal(&data, "i", 12, 42);
232 	assert(data.buffer[2] == 42);
233 
234 	marshal(&data, "u", 12, 55);
235 	assert(data.buffer[2] == 55);
236 
237 	marshal(&data, "s", 20, "frappo");
238 	assert(data.buffer[2] == 7);
239 	assert(strcmp((char *) &data.buffer[3], "frappo") == 0);
240 
241 	object.id = 557799;
242 	marshal(&data, "o", 12, &object);
243 	assert(data.buffer[2] == object.id);
244 
245 	marshal(&data, "n", 12, &object);
246 	assert(data.buffer[2] == object.id);
247 
248 	array.data = (void *) text;
249 	array.size = sizeof text;
250 	marshal(&data, "a", 20, &array);
251 	assert(data.buffer[2] == array.size);
252 	assert(memcmp(&data.buffer[3], text, array.size) == 0);
253 
254 	release_marshal_data(&data);
255 }
256 
257 static void
expected_fail_marshal(int expected_error,const char * format,...)258 expected_fail_marshal(int expected_error, const char *format, ...)
259 {
260 	struct wl_closure *closure;
261 	static const uint32_t opcode = 4444;
262 	static const struct wl_interface test_interface = {
263 		.name = "test_object"
264 	};
265 	static struct wl_object sender = { 0 };
266 	struct wl_message message = { "test", format, NULL };
267 
268 	sender.interface = &test_interface;
269 	sender.id = 1234;
270 	va_list ap;
271 
272 	va_start(ap, format);
273 	closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
274 	va_end(ap);
275 
276 	assert(closure == NULL);
277 	assert(errno == expected_error);
278 }
279 
280 static void
expected_fail_marshal_send(struct marshal_data * data,int expected_error,const char * format,...)281 expected_fail_marshal_send(struct marshal_data *data, int expected_error,
282 			   const char *format, ...)
283 {
284 	struct wl_closure *closure;
285 	static const uint32_t opcode = 4444;
286 	static struct wl_object sender = { NULL, NULL, 1234 };
287 	struct wl_message message = { "test", format, NULL };
288 	va_list ap;
289 
290 	va_start(ap, format);
291 	closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
292 	va_end(ap);
293 
294 	assert(closure);
295 	assert(wl_closure_send(closure, data->write_connection) < 0);
296 	assert(errno == expected_error);
297 
298 	wl_closure_destroy(closure);
299 }
300 
TEST(connection_marshal_nullables)301 TEST(connection_marshal_nullables)
302 {
303 	struct marshal_data data;
304 	struct wl_object object;
305 	const char text[] = "curry";
306 
307 	setup_marshal_data(&data);
308 
309 	expected_fail_marshal(EINVAL, "o", NULL);
310 	expected_fail_marshal(EINVAL, "s", NULL);
311 	expected_fail_marshal(EINVAL, "a", NULL);
312 
313 	marshal(&data, "?o", 12, NULL);
314 	assert(data.buffer[2] == 0);
315 
316 	marshal(&data, "?s", 12, NULL);
317 	assert(data.buffer[2] == 0);
318 
319 	object.id = 55293;
320 	marshal(&data, "?o", 12, &object);
321 	assert(data.buffer[2] == object.id);
322 
323 	marshal(&data, "?s", 20, text);
324 	assert(data.buffer[2] == sizeof text);
325 	assert(strcmp((char *) &data.buffer[3], text) == 0);
326 
327 	release_marshal_data(&data);
328 }
329 
330 static void
validate_demarshal_u(struct marshal_data * data,struct wl_object * object,uint32_t u)331 validate_demarshal_u(struct marshal_data *data,
332 		     struct wl_object *object, uint32_t u)
333 {
334 	assert(data->value.u == u);
335 }
336 
337 static void
validate_demarshal_i(struct marshal_data * data,struct wl_object * object,int32_t i)338 validate_demarshal_i(struct marshal_data *data,
339 		     struct wl_object *object, int32_t i)
340 {
341 	assert(data->value.i == i);
342 }
343 
344 static void
validate_demarshal_s(struct marshal_data * data,struct wl_object * object,const char * s)345 validate_demarshal_s(struct marshal_data *data,
346 		     struct wl_object *object, const char *s)
347 {
348 	if (data->value.s != NULL)
349 		assert(strcmp(data->value.s, s) == 0);
350 	else
351 		assert(s == NULL);
352 }
353 
354 static void
validate_demarshal_h(struct marshal_data * data,struct wl_object * object,int fd)355 validate_demarshal_h(struct marshal_data *data,
356 		     struct wl_object *object, int fd)
357 {
358 	struct stat buf1, buf2;
359 
360 	assert(fd != data->value.h);
361 	fstat(fd, &buf1);
362 	fstat(data->value.h, &buf2);
363 	assert(buf1.st_dev == buf2.st_dev);
364 	assert(buf1.st_ino == buf2.st_ino);
365 	close(fd);
366 	close(data->value.h);
367 }
368 
369 static void
validate_demarshal_f(struct marshal_data * data,struct wl_object * object,wl_fixed_t f)370 validate_demarshal_f(struct marshal_data *data,
371 		     struct wl_object *object, wl_fixed_t f)
372 {
373 	assert(data->value.i == f);
374 }
375 
376 static void
demarshal(struct marshal_data * data,const char * format,uint32_t * msg,void (* func)(void))377 demarshal(struct marshal_data *data, const char *format,
378 	  uint32_t *msg, void (*func)(void))
379 {
380 	struct wl_message message = { "test", format, NULL };
381 	struct wl_closure *closure;
382 	struct wl_map objects;
383 	struct wl_object object = { NULL, &func, 0 };
384 	int size = msg[1] >> 16;
385 
386 	assert(write(data->s[1], msg, size) == size);
387 	assert(wl_connection_read(data->read_connection) == size);
388 
389 	wl_map_init(&objects, WL_MAP_SERVER_SIDE);
390 	object.id = msg[0];
391 	closure = wl_connection_demarshal(data->read_connection,
392 					  size, &objects, &message);
393 	assert(closure);
394 	wl_closure_invoke(closure, WL_CLOSURE_INVOKE_SERVER, &object, 0, data);
395 	wl_closure_destroy(closure);
396 }
397 
TEST(connection_demarshal)398 TEST(connection_demarshal)
399 {
400 	struct marshal_data data;
401 	uint32_t msg[10];
402 
403 	setup_marshal_data(&data);
404 
405 	data.value.u = 8000;
406 	msg[0] = 400200;	/* object id */
407 	msg[1] = 12 << 16;		/* size = 12, opcode = 0 */
408 	msg[2] = data.value.u;
409 	demarshal(&data, "u", msg, (void *) validate_demarshal_u);
410 
411 	data.value.i = -557799;
412 	msg[0] = 400200;
413 	msg[1] = 12 << 16;
414 	msg[2] = data.value.i;
415 	demarshal(&data, "i", msg, (void *) validate_demarshal_i);
416 
417 	data.value.s = "superdude";
418 	msg[0] = 400200;
419 	msg[1] = 24 << 16;
420 	msg[2] = 10;
421 	msg[3 + msg[2]/4] = 0;
422 	memcpy(&msg[3], data.value.s, msg[2]);
423 	demarshal(&data, "s", msg, (void *) validate_demarshal_s);
424 
425 	data.value.s = "superdude";
426 	msg[0] = 400200;
427 	msg[1] = 24 << 16;
428 	msg[2] = 10;
429 	msg[3 + msg[2]/4] = 0;
430 	memcpy(&msg[3], data.value.s, msg[2]);
431 	demarshal(&data, "?s", msg, (void *) validate_demarshal_s);
432 
433 	data.value.i = wl_fixed_from_double(-90000.2390);
434 	msg[0] = 400200;
435 	msg[1] = 12 << 16;
436 	msg[2] = data.value.i;
437 	demarshal(&data, "f", msg, (void *) validate_demarshal_f);
438 
439 	data.value.s = NULL;
440 	msg[0] = 400200;
441 	msg[1] = 12 << 16;
442 	msg[2] = 0;
443 	demarshal(&data, "?s", msg, (void *) validate_demarshal_s);
444 
445 	release_marshal_data(&data);
446 }
447 
448 static void
marshal_demarshal(struct marshal_data * data,void (* func)(void),int size,const char * format,...)449 marshal_demarshal(struct marshal_data *data,
450 		  void (*func)(void), int size, const char *format, ...)
451 {
452 	struct wl_closure *closure;
453 	static const int opcode = 4444;
454 	static struct wl_object sender = { NULL, NULL, 1234 };
455 	struct wl_message message = { "test", format, NULL };
456 	struct wl_map objects;
457 	struct wl_object object = { NULL, &func, 0 };
458 	va_list ap;
459 	uint32_t msg[1] = { 1234 };
460 
461 	va_start(ap, format);
462 	closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
463 	va_end(ap);
464 
465 	assert(closure);
466 	assert(wl_closure_send(closure, data->write_connection) == 0);
467 	wl_closure_destroy(closure);
468 	assert(wl_connection_flush(data->write_connection) == size);
469 
470 	assert(wl_connection_read(data->read_connection) == size);
471 
472 	wl_map_init(&objects, WL_MAP_SERVER_SIDE);
473 	object.id = msg[0];
474 	closure = wl_connection_demarshal(data->read_connection,
475 					  size, &objects, &message);
476 	assert(closure);
477 	wl_closure_invoke(closure, WL_CLOSURE_INVOKE_SERVER, &object, 0, data);
478 	wl_closure_destroy(closure);
479 }
480 
TEST(connection_marshal_demarshal)481 TEST(connection_marshal_demarshal)
482 {
483 	struct marshal_data data;
484 	char f[] = "/tmp/wayland-tests-XXXXXX";
485 
486 	setup_marshal_data(&data);
487 
488 	data.value.u = 889911;
489 	marshal_demarshal(&data, (void *) validate_demarshal_u,
490 			  12, "u", data.value.u);
491 
492 	data.value.i = -13;
493 	marshal_demarshal(&data, (void *) validate_demarshal_i,
494 			  12, "i", data.value.i);
495 
496 	data.value.s = "cookie robots";
497 	marshal_demarshal(&data, (void *) validate_demarshal_s,
498 			  28, "s", data.value.s);
499 
500 	data.value.s = "cookie robots";
501 	marshal_demarshal(&data, (void *) validate_demarshal_s,
502 			  28, "?s", data.value.s);
503 
504 	data.value.h = mkstemp(f);
505 	assert(data.value.h >= 0);
506 	unlink(f);
507 	marshal_demarshal(&data, (void *) validate_demarshal_h,
508 			  8, "h", data.value.h);
509 
510 	data.value.i = wl_fixed_from_double(1234.5678);
511 	marshal_demarshal(&data, (void *) validate_demarshal_f,
512 	                  12, "f", data.value.i);
513 
514 	data.value.i = wl_fixed_from_double(-90000.2390);
515 	marshal_demarshal(&data, (void *) validate_demarshal_f,
516 	                  12, "f", data.value.i);
517 
518 	data.value.i = wl_fixed_from_double((1 << 23) - 1 + 0.0941);
519 	marshal_demarshal(&data, (void *) validate_demarshal_f,
520 	                  12, "f", data.value.i);
521 
522 	release_marshal_data(&data);
523 }
524 
525 static void
expected_fail_demarshal(struct marshal_data * data,const char * format,const uint32_t * msg,int expected_error)526 expected_fail_demarshal(struct marshal_data *data, const char *format,
527                         const uint32_t *msg, int expected_error)
528 {
529 	struct wl_message message = { "test", format, NULL };
530 	struct wl_closure *closure;
531 	struct wl_map objects;
532 	int size = (msg[1] >> 16);
533 
534 	assert(write(data->s[1], msg, size) == size);
535 	assert(wl_connection_read(data->read_connection) == size);
536 
537 	wl_map_init(&objects, WL_MAP_SERVER_SIDE);
538 	closure = wl_connection_demarshal(data->read_connection,
539 					    size, &objects, &message);
540 
541 	assert(closure == NULL);
542 	assert(errno == expected_error);
543 }
544 
TEST(connection_demarshal_null_strings)545 TEST(connection_demarshal_null_strings)
546 {
547 	struct marshal_data data;
548 	uint32_t msg[3];
549 
550 	setup_marshal_data(&data);
551 
552 	data.value.s = NULL;
553 	msg[0] = 400200;	/* object id */
554 	msg[1] = 12 << 16;	/* size = 12, opcode = 0 */
555 	msg[2] = 0;		/* string length = 0 */
556 	demarshal(&data, "?s", msg, (void *) validate_demarshal_s);
557 
558 	expected_fail_demarshal(&data, "s", msg, EINVAL);
559 
560 	release_marshal_data(&data);
561 }
562 
563 /* These tests are verifying that the demarshaling code will gracefully handle
564  * clients lying about string and array lengths and giving values near
565  * UINT32_MAX. Before fixes f7fdface and f5b9e3b9 this test would crash on
566  * 32bit systems.
567  */
TEST(connection_demarshal_failures)568 TEST(connection_demarshal_failures)
569 {
570 	struct marshal_data data;
571 	unsigned int i;
572 	uint32_t msg[3];
573 
574 	const uint32_t overflowing_values[] = {
575 		/* Values very close to UINT32_MAX. Before f5b9e3b9 these
576 		 * would cause integer overflow in DIV_ROUNDUP. */
577 		0xffffffff, 0xfffffffe, 0xfffffffd, 0xfffffffc,
578 
579 		/* Values at various offsets from UINT32_MAX. Before f7fdface
580 		 * these would overflow the "p" pointer on 32bit systems,
581 		 * effectively subtracting the offset from it. It had good
582 		 * chance to cause crash depending on what was stored at that
583 		 * offset before "p". */
584 		0xfffff000, 0xffffd000, 0xffffc000, 0xffffb000
585 	};
586 
587 	setup_marshal_data(&data);
588 
589 	/* sender_id, does not matter */
590 	msg[0] = 0;
591 
592 	/* (size << 16 | opcode), opcode is 0, does not matter */
593 	msg[1] = sizeof(msg) << 16;
594 
595 	for (i = 0; i < ARRAY_LENGTH(overflowing_values); i++) {
596 		/* length of the string or array */
597 		msg[2] = overflowing_values[i];
598 
599 		expected_fail_demarshal(&data, "s", msg, EINVAL);
600 		expected_fail_demarshal(&data, "a", msg, EINVAL);
601 	}
602 
603 	release_marshal_data(&data);
604 }
605 
TEST(connection_marshal_alot)606 TEST(connection_marshal_alot)
607 {
608 	struct marshal_data data;
609 	char f[64];
610 	int i;
611 
612 	setup_marshal_data(&data);
613 
614 	/* We iterate enough to make sure we wrap the circular buffers
615 	 * for both regular data an fds. */
616 
617 	for (i = 0; i < 2000; i++) {
618 		strcpy(f, "/tmp/wayland-tests-XXXXXX");
619 		data.value.h = mkstemp(f);
620 		assert(data.value.h >= 0);
621 		unlink(f);
622 		marshal_demarshal(&data, (void *) validate_demarshal_h,
623 				  8, "h", data.value.h);
624 	}
625 
626 	release_marshal_data(&data);
627 }
628 
TEST(connection_marshal_too_big)629 TEST(connection_marshal_too_big)
630 {
631 	struct marshal_data data;
632 	char *big_string = malloc(5000);
633 
634 	assert(big_string);
635 
636 	memset(big_string, ' ', 4999);
637 	big_string[4999] = '\0';
638 
639 	setup_marshal_data(&data);
640 
641 	expected_fail_marshal_send(&data, E2BIG, "s", big_string);
642 
643 	release_marshal_data(&data);
644 	free(big_string);
645 }
646 
647 static void
marshal_helper(const char * format,void * handler,...)648 marshal_helper(const char *format, void *handler, ...)
649 {
650 	struct wl_closure *closure;
651 	static struct wl_object sender = { NULL, NULL, 1234 };
652 	struct wl_object object = { NULL, &handler, 0 };
653 	static const int opcode = 4444;
654 	struct wl_message message = { "test", format, NULL };
655 	va_list ap;
656 	int done;
657 
658 	va_start(ap, handler);
659 	closure = wl_closure_vmarshal(&sender, opcode, ap, &message);
660 	va_end(ap);
661 
662 	assert(closure);
663 	done = 0;
664 	wl_closure_invoke(closure, WL_CLOSURE_INVOKE_SERVER, &object, 0, &done);
665 	wl_closure_destroy(closure);
666 	assert(done);
667 }
668 
669 static void
suu_handler(void * data,struct wl_object * object,const char * s,uint32_t u1,uint32_t u2)670 suu_handler(void *data, struct wl_object *object,
671 	    const char *s, uint32_t u1, uint32_t u2)
672 {
673 	int *done = data;
674 
675 	assert(strcmp(s, "foo") == 0);
676 	assert(u1 == 500);
677 	assert(u2 == 404040);
678 	*done = 1;
679 }
680 
TEST(invoke_closure)681 TEST(invoke_closure)
682 {
683 	marshal_helper("suu", suu_handler, "foo", 500, 404040);
684 }
685 
686 static void
leak_closure(void)687 leak_closure(void)
688 {
689 	struct wl_callback *cb;
690 	struct pollfd pfd;
691 	struct client *c = client_connect();
692 
693 	cb = wl_display_sync(c->wl_display);
694 	assert(cb);
695 	assert(wl_display_flush(c->wl_display) > 0);
696 
697 	/* we don't need it, it is referenced */
698 	wl_callback_destroy(cb);
699 
700 	pfd.fd = wl_display_get_fd(c->wl_display);
701 	pfd.events = POLLIN;
702 
703 	test_set_timeout(2);
704 	assert(poll(&pfd, 1, -1) == 1);
705 
706 	/* read events, but do not dispatch them */
707 	assert(wl_display_prepare_read(c->wl_display) == 0);
708 	assert(wl_display_read_events(c->wl_display) == 0);
709 
710 	/*
711 	 * now we have wl_callback.done and wl_display.delete_id queued;
712 	 * if we now release the queue (in wl_display_disconnect())
713 	 * we should not leak memory
714 	 */
715 
716 	client_disconnect(c);
717 }
718 
TEST(closure_leaks)719 TEST(closure_leaks)
720 {
721 	struct display *d = display_create();
722 
723 	client_create_noarg(d, leak_closure);
724 	display_run(d);
725 
726 	display_destroy(d);
727 }
728 
729 static void
leak_after_error(void)730 leak_after_error(void)
731 {
732 	struct client *c = client_connect();
733 
734 	/* this should return -1, because we'll send error
735 	 * from server. */
736 	assert(stop_display(c, 1) == -1);
737 	assert(wl_display_dispatch_pending(c->wl_display) == -1);
738 	assert(wl_display_get_error(c->wl_display) == ENOMEM);
739 
740 	/* after we got error, we have display_resume event
741 	 * in the queue. It should be freed in wl_display_disconnect().
742 	 * Let's see! */
743 
744 	wl_proxy_destroy((struct wl_proxy *) c->tc);
745 	wl_display_disconnect(c->wl_display);
746 	free(c);
747 }
748 
TEST(closure_leaks_after_error)749 TEST(closure_leaks_after_error)
750 {
751 	struct display *d = display_create();
752 	struct client_info *cl;
753 
754 	cl = client_create_noarg(d, leak_after_error);
755 	display_run(d);
756 
757 	wl_client_post_no_memory(cl->wl_client);
758 	display_resume(d);
759 
760 	display_destroy(d);
761 }
762 
763 /** Raw read from socket expecting wl_display.error
764  *
765  * \param sockfd The socket to read from.
766  * \param expected_error The expected wl_display error code.
767  *
768  * Reads the socket and manually parses one message, expecting it to be a
769  * wl_display.error with the wl_display as the originating object.
770  * Asserts that the received error code is expected_error.
771  */
772 static void
expect_error_recv(int sockfd,uint32_t expected_error)773 expect_error_recv(int sockfd, uint32_t expected_error)
774 {
775 	uint32_t buf[1024];
776 	ssize_t slen;
777 	uint32_t opcode;
778 	int str_len;
779 
780 	slen = recv(sockfd, buf, sizeof buf, 0);
781 	assert(slen >= 2 * (ssize_t)sizeof (uint32_t));
782 	opcode = buf[1] & 0xffff;
783 	fprintf(stderr, "Received %zd bytes, object %u, opcode %u\n",
784 		slen, buf[0], opcode);
785 
786 	/* check error event */
787 	assert(buf[0] == 1);
788 	assert(opcode == WL_DISPLAY_ERROR);
789 
790 	str_len = buf[4];
791 	assert(str_len > 0);
792 	assert(str_len <= slen - 5 * (ssize_t)sizeof (uint32_t));
793 	fprintf(stderr, "Error event on object %u, code %u, message \"%*s\"\n",
794 		buf[2], buf[3], str_len, (const char *)&buf[5]);
795 
796 	assert(buf[3] == expected_error);
797 }
798 
799 /* A test for https://gitlab.freedesktop.org/wayland/wayland/issues/52
800  * trying to provoke a read from uninitialized memory in
801  * wl_connection_demarshal() for sender_id and opcode.
802  *
803  * This test might not fail as is even with #52 unfixed, since there is no way
804  * to detect what happens and the crash with zero size depends on stack content.
805  * However, running under Valgrind would point out invalid reads and use of
806  * uninitialized values.
807  */
TEST(request_bogus_size)808 TEST(request_bogus_size)
809 {
810 	struct wl_display *display;
811 	struct wl_client *client;
812 	int s[2];
813 	uint32_t msg[3];
814 	int bogus_size;
815 
816 	test_set_timeout(1);
817 
818 	/*
819 	 * The manufactured message has real size 12. Test all bogus sizes
820 	 * smaller than that, and zero as the last one since wl_closure_init
821 	 * handles zero specially and having garbage in the stack makes it more
822 	 * likely to crash in wl_connection_demarshal.
823 	 */
824 	for (bogus_size = 11; bogus_size >= 0; bogus_size--) {
825 		fprintf(stderr, "* bogus size %d\n", bogus_size);
826 
827 		assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, s) == 0);
828 		display = wl_display_create();
829 		assert(display);
830 		client = wl_client_create(display, s[0]);
831 		assert(client);
832 
833 		/* manufacture a request that lies about its size */
834 		msg[0] = 1; /* sender id: wl_display */
835 		msg[1] = (bogus_size << 16) | WL_DISPLAY_SYNC; /* size and opcode */
836 		msg[2] = 2; /* sync argument: new_id for wl_callback */
837 
838 		assert(send(s[1], msg, sizeof msg, 0) == sizeof msg);
839 
840 		wl_event_loop_dispatch(wl_display_get_event_loop(display), 0);
841 
842 		expect_error_recv(s[1], WL_DISPLAY_ERROR_INVALID_METHOD);
843 
844 		/* Do not wl_client_destroy, the error already caused it. */
845 		close(s[1]);
846 		wl_display_destroy(display);
847 	}
848 }
849