1 /*
2 * Copyright © 2008 Kristian Høgsberg
3 * Copyright © 2013 Jason Ekstrand
4 *
5 * 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:
12 *
13 * 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.
25 */
26
27 #define _GNU_SOURCE
28
29 #include <math.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <string.h>
33 #include <stdio.h>
34 #include <errno.h>
35 #include <sys/uio.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <time.h>
41 #include <ffi.h>
42
43 #include "wayland-util.h"
44 #include "wayland-private.h"
45 #include "wayland-os.h"
46
47 static inline uint32_t
div_roundup(uint32_t n,size_t a)48 div_roundup(uint32_t n, size_t a)
49 {
50 /* The cast to uint64_t is necessary to prevent overflow when rounding
51 * values close to UINT32_MAX. After the division it is again safe to
52 * cast back to uint32_t.
53 */
54 return (uint32_t) (((uint64_t) n + (a - 1)) / a);
55 }
56
57 struct wl_buffer {
58 char data[4096];
59 uint32_t head, tail;
60 };
61
62 #define MASK(i) ((i) & 4095)
63
64 #define MAX_FDS_OUT 28
65 #define CLEN (CMSG_LEN(MAX_FDS_OUT * sizeof(int32_t)))
66
67 struct wl_connection {
68 struct wl_buffer in, out;
69 struct wl_buffer fds_in, fds_out;
70 int fd;
71 int want_flush;
72 };
73
74 static int
wl_buffer_put(struct wl_buffer * b,const void * data,size_t count)75 wl_buffer_put(struct wl_buffer *b, const void *data, size_t count)
76 {
77 uint32_t head, size;
78
79 if (count > sizeof(b->data)) {
80 wl_log("Data too big for buffer (%d > %d).\n",
81 count, sizeof(b->data));
82 errno = E2BIG;
83 return -1;
84 }
85
86 head = MASK(b->head);
87 if (head + count <= sizeof b->data) {
88 memcpy(b->data + head, data, count);
89 } else {
90 size = sizeof b->data - head;
91 memcpy(b->data + head, data, size);
92 memcpy(b->data, (const char *) data + size, count - size);
93 }
94
95 b->head += count;
96
97 return 0;
98 }
99
100 static void
wl_buffer_put_iov(struct wl_buffer * b,struct iovec * iov,int * count)101 wl_buffer_put_iov(struct wl_buffer *b, struct iovec *iov, int *count)
102 {
103 uint32_t head, tail;
104
105 head = MASK(b->head);
106 tail = MASK(b->tail);
107 if (head < tail) {
108 iov[0].iov_base = b->data + head;
109 iov[0].iov_len = tail - head;
110 *count = 1;
111 } else if (tail == 0) {
112 iov[0].iov_base = b->data + head;
113 iov[0].iov_len = sizeof b->data - head;
114 *count = 1;
115 } else {
116 iov[0].iov_base = b->data + head;
117 iov[0].iov_len = sizeof b->data - head;
118 iov[1].iov_base = b->data;
119 iov[1].iov_len = tail;
120 *count = 2;
121 }
122 }
123
124 static void
wl_buffer_get_iov(struct wl_buffer * b,struct iovec * iov,int * count)125 wl_buffer_get_iov(struct wl_buffer *b, struct iovec *iov, int *count)
126 {
127 uint32_t head, tail;
128
129 head = MASK(b->head);
130 tail = MASK(b->tail);
131 if (tail < head) {
132 iov[0].iov_base = b->data + tail;
133 iov[0].iov_len = head - tail;
134 *count = 1;
135 } else if (head == 0) {
136 iov[0].iov_base = b->data + tail;
137 iov[0].iov_len = sizeof b->data - tail;
138 *count = 1;
139 } else {
140 iov[0].iov_base = b->data + tail;
141 iov[0].iov_len = sizeof b->data - tail;
142 iov[1].iov_base = b->data;
143 iov[1].iov_len = head;
144 *count = 2;
145 }
146 }
147
148 static void
wl_buffer_copy(struct wl_buffer * b,void * data,size_t count)149 wl_buffer_copy(struct wl_buffer *b, void *data, size_t count)
150 {
151 uint32_t tail, size;
152
153 tail = MASK(b->tail);
154 if (tail + count <= sizeof b->data) {
155 memcpy(data, b->data + tail, count);
156 } else {
157 size = sizeof b->data - tail;
158 memcpy(data, b->data + tail, size);
159 memcpy((char *) data + size, b->data, count - size);
160 }
161 }
162
163 static uint32_t
wl_buffer_size(struct wl_buffer * b)164 wl_buffer_size(struct wl_buffer *b)
165 {
166 return b->head - b->tail;
167 }
168
169 struct wl_connection *
wl_connection_create(int fd)170 wl_connection_create(int fd)
171 {
172 struct wl_connection *connection;
173
174 connection = zalloc(sizeof *connection);
175 if (connection == NULL)
176 return NULL;
177
178 connection->fd = fd;
179
180 return connection;
181 }
182
183 static void
close_fds(struct wl_buffer * buffer,int max)184 close_fds(struct wl_buffer *buffer, int max)
185 {
186 int32_t fds[sizeof(buffer->data) / sizeof(int32_t)], i, count;
187 size_t size;
188
189 size = wl_buffer_size(buffer);
190 if (size == 0)
191 return;
192
193 wl_buffer_copy(buffer, fds, size);
194 count = size / sizeof fds[0];
195 if (max > 0 && max < count)
196 count = max;
197 size = count * sizeof fds[0];
198 for (i = 0; i < count; i++)
199 close(fds[i]);
200 buffer->tail += size;
201 }
202
203 void
wl_connection_close_fds_in(struct wl_connection * connection,int max)204 wl_connection_close_fds_in(struct wl_connection *connection, int max)
205 {
206 close_fds(&connection->fds_in, max);
207 }
208
209 int
wl_connection_destroy(struct wl_connection * connection)210 wl_connection_destroy(struct wl_connection *connection)
211 {
212 int fd = connection->fd;
213
214 close_fds(&connection->fds_out, -1);
215 close_fds(&connection->fds_in, -1);
216 free(connection);
217
218 return fd;
219 }
220
221 void
wl_connection_copy(struct wl_connection * connection,void * data,size_t size)222 wl_connection_copy(struct wl_connection *connection, void *data, size_t size)
223 {
224 wl_buffer_copy(&connection->in, data, size);
225 }
226
227 void
wl_connection_consume(struct wl_connection * connection,size_t size)228 wl_connection_consume(struct wl_connection *connection, size_t size)
229 {
230 connection->in.tail += size;
231 }
232
233 static void
build_cmsg(struct wl_buffer * buffer,char * data,int * clen)234 build_cmsg(struct wl_buffer *buffer, char *data, int *clen)
235 {
236 struct cmsghdr *cmsg;
237 size_t size;
238
239 size = wl_buffer_size(buffer);
240 if (size > MAX_FDS_OUT * sizeof(int32_t))
241 size = MAX_FDS_OUT * sizeof(int32_t);
242
243 if (size > 0) {
244 cmsg = (struct cmsghdr *) data;
245 cmsg->cmsg_level = SOL_SOCKET;
246 cmsg->cmsg_type = SCM_RIGHTS;
247 cmsg->cmsg_len = CMSG_LEN(size);
248 wl_buffer_copy(buffer, CMSG_DATA(cmsg), size);
249 *clen = cmsg->cmsg_len;
250 } else {
251 *clen = 0;
252 }
253 }
254
255 static int
decode_cmsg(struct wl_buffer * buffer,struct msghdr * msg)256 decode_cmsg(struct wl_buffer *buffer, struct msghdr *msg)
257 {
258 struct cmsghdr *cmsg;
259 size_t size, max, i;
260 int overflow = 0;
261
262 for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
263 cmsg = CMSG_NXTHDR(msg, cmsg)) {
264 if (cmsg->cmsg_level != SOL_SOCKET ||
265 cmsg->cmsg_type != SCM_RIGHTS)
266 continue;
267
268 size = cmsg->cmsg_len - CMSG_LEN(0);
269 max = sizeof(buffer->data) - wl_buffer_size(buffer);
270 if (size > max || overflow) {
271 overflow = 1;
272 size /= sizeof(int32_t);
273 for (i = 0; i < size; i++)
274 close(((int*)CMSG_DATA(cmsg))[i]);
275 } else if (wl_buffer_put(buffer, CMSG_DATA(cmsg), size) < 0) {
276 return -1;
277 }
278 }
279
280 if (overflow) {
281 errno = EOVERFLOW;
282 return -1;
283 }
284
285 return 0;
286 }
287
288 int
wl_connection_flush(struct wl_connection * connection)289 wl_connection_flush(struct wl_connection *connection)
290 {
291 struct iovec iov[2];
292 struct msghdr msg;
293 char cmsg[CLEN];
294 int len = 0, count, clen;
295 uint32_t tail;
296
297 if (!connection->want_flush)
298 return 0;
299
300 tail = connection->out.tail;
301 while (connection->out.head - connection->out.tail > 0) {
302 wl_buffer_get_iov(&connection->out, iov, &count);
303
304 build_cmsg(&connection->fds_out, cmsg, &clen);
305
306 msg.msg_name = NULL;
307 msg.msg_namelen = 0;
308 msg.msg_iov = iov;
309 msg.msg_iovlen = count;
310 msg.msg_control = (clen > 0) ? cmsg : NULL;
311 msg.msg_controllen = clen;
312 msg.msg_flags = 0;
313
314 do {
315 len = sendmsg(connection->fd, &msg,
316 MSG_NOSIGNAL | MSG_DONTWAIT);
317 } while (len == -1 && errno == EINTR);
318
319 if (len == -1)
320 return -1;
321
322 close_fds(&connection->fds_out, MAX_FDS_OUT);
323
324 connection->out.tail += len;
325 }
326
327 connection->want_flush = 0;
328
329 return connection->out.head - tail;
330 }
331
332 uint32_t
wl_connection_pending_input(struct wl_connection * connection)333 wl_connection_pending_input(struct wl_connection *connection)
334 {
335 return wl_buffer_size(&connection->in);
336 }
337
338 int
wl_connection_read(struct wl_connection * connection)339 wl_connection_read(struct wl_connection *connection)
340 {
341 struct iovec iov[2];
342 struct msghdr msg;
343 char cmsg[CLEN];
344 int len, count, ret;
345
346 if (wl_buffer_size(&connection->in) >= sizeof(connection->in.data)) {
347 errno = EOVERFLOW;
348 return -1;
349 }
350
351 wl_buffer_put_iov(&connection->in, iov, &count);
352
353 msg.msg_name = NULL;
354 msg.msg_namelen = 0;
355 msg.msg_iov = iov;
356 msg.msg_iovlen = count;
357 msg.msg_control = cmsg;
358 msg.msg_controllen = sizeof cmsg;
359 msg.msg_flags = 0;
360
361 do {
362 len = wl_os_recvmsg_cloexec(connection->fd, &msg, MSG_DONTWAIT);
363 } while (len < 0 && errno == EINTR);
364
365 if (len <= 0)
366 return len;
367
368 ret = decode_cmsg(&connection->fds_in, &msg);
369 if (ret)
370 return -1;
371
372 connection->in.head += len;
373
374 return wl_connection_pending_input(connection);
375 }
376
377 int
wl_connection_write(struct wl_connection * connection,const void * data,size_t count)378 wl_connection_write(struct wl_connection *connection,
379 const void *data, size_t count)
380 {
381 if (connection->out.head - connection->out.tail +
382 count > ARRAY_LENGTH(connection->out.data)) {
383 connection->want_flush = 1;
384 if (wl_connection_flush(connection) < 0)
385 return -1;
386 }
387
388 if (wl_buffer_put(&connection->out, data, count) < 0)
389 return -1;
390
391 connection->want_flush = 1;
392
393 return 0;
394 }
395
396 int
wl_connection_queue(struct wl_connection * connection,const void * data,size_t count)397 wl_connection_queue(struct wl_connection *connection,
398 const void *data, size_t count)
399 {
400 if (connection->out.head - connection->out.tail +
401 count > ARRAY_LENGTH(connection->out.data)) {
402 connection->want_flush = 1;
403 if (wl_connection_flush(connection) < 0)
404 return -1;
405 }
406
407 return wl_buffer_put(&connection->out, data, count);
408 }
409
410 int
wl_message_count_arrays(const struct wl_message * message)411 wl_message_count_arrays(const struct wl_message *message)
412 {
413 int i, arrays;
414
415 for (i = 0, arrays = 0; message->signature[i]; i++) {
416 if (message->signature[i] == 'a')
417 arrays++;
418 }
419
420 return arrays;
421 }
422
423 int
wl_connection_get_fd(struct wl_connection * connection)424 wl_connection_get_fd(struct wl_connection *connection)
425 {
426 return connection->fd;
427 }
428
429 static int
wl_connection_put_fd(struct wl_connection * connection,int32_t fd)430 wl_connection_put_fd(struct wl_connection *connection, int32_t fd)
431 {
432 if (wl_buffer_size(&connection->fds_out) == MAX_FDS_OUT * sizeof fd) {
433 connection->want_flush = 1;
434 if (wl_connection_flush(connection) < 0)
435 return -1;
436 }
437
438 return wl_buffer_put(&connection->fds_out, &fd, sizeof fd);
439 }
440
441 const char *
get_next_argument(const char * signature,struct argument_details * details)442 get_next_argument(const char *signature, struct argument_details *details)
443 {
444 details->nullable = 0;
445 for(; *signature; ++signature) {
446 switch(*signature) {
447 case 'i':
448 case 'u':
449 case 'f':
450 case 's':
451 case 'o':
452 case 'n':
453 case 'a':
454 case 'h':
455 details->type = *signature;
456 return signature + 1;
457 case '?':
458 details->nullable = 1;
459 }
460 }
461 details->type = '\0';
462 return signature;
463 }
464
465 int
arg_count_for_signature(const char * signature)466 arg_count_for_signature(const char *signature)
467 {
468 int count = 0;
469 for(; *signature; ++signature) {
470 switch(*signature) {
471 case 'i':
472 case 'u':
473 case 'f':
474 case 's':
475 case 'o':
476 case 'n':
477 case 'a':
478 case 'h':
479 ++count;
480 }
481 }
482 return count;
483 }
484
485 int
wl_message_get_since(const struct wl_message * message)486 wl_message_get_since(const struct wl_message *message)
487 {
488 int since;
489
490 since = atoi(message->signature);
491
492 if (since == 0)
493 since = 1;
494
495 return since;
496 }
497
498 void
wl_argument_from_va_list(const char * signature,union wl_argument * args,int count,va_list ap)499 wl_argument_from_va_list(const char *signature, union wl_argument *args,
500 int count, va_list ap)
501 {
502 int i;
503 const char *sig_iter;
504 struct argument_details arg;
505
506 sig_iter = signature;
507 for (i = 0; i < count; i++) {
508 sig_iter = get_next_argument(sig_iter, &arg);
509
510 switch(arg.type) {
511 case 'i':
512 args[i].i = va_arg(ap, int32_t);
513 break;
514 case 'u':
515 args[i].u = va_arg(ap, uint32_t);
516 break;
517 case 'f':
518 args[i].f = va_arg(ap, wl_fixed_t);
519 break;
520 case 's':
521 args[i].s = va_arg(ap, const char *);
522 break;
523 case 'o':
524 args[i].o = va_arg(ap, struct wl_object *);
525 break;
526 case 'n':
527 args[i].o = va_arg(ap, struct wl_object *);
528 break;
529 case 'a':
530 args[i].a = va_arg(ap, struct wl_array *);
531 break;
532 case 'h':
533 args[i].h = va_arg(ap, int32_t);
534 break;
535 case '\0':
536 return;
537 }
538 }
539 }
540
541 static void
wl_closure_clear_fds(struct wl_closure * closure)542 wl_closure_clear_fds(struct wl_closure *closure)
543 {
544 const char *signature = closure->message->signature;
545 struct argument_details arg;
546 int i;
547
548 for (i = 0; i < closure->count; i++) {
549 signature = get_next_argument(signature, &arg);
550 if (arg.type == 'h')
551 closure->args[i].h = -1;
552 }
553 }
554
555 static struct wl_closure *
wl_closure_init(const struct wl_message * message,uint32_t size,int * num_arrays,union wl_argument * args)556 wl_closure_init(const struct wl_message *message, uint32_t size,
557 int *num_arrays, union wl_argument *args)
558 {
559 struct wl_closure *closure;
560 int count;
561
562 count = arg_count_for_signature(message->signature);
563 if (count > WL_CLOSURE_MAX_ARGS) {
564 wl_log("too many args (%d)\n", count);
565 errno = EINVAL;
566 return NULL;
567 }
568
569 if (size) {
570 *num_arrays = wl_message_count_arrays(message);
571 closure = malloc(sizeof *closure + size +
572 *num_arrays * sizeof(struct wl_array));
573 } else {
574 closure = malloc(sizeof *closure);
575 }
576
577 if (!closure) {
578 errno = ENOMEM;
579 return NULL;
580 }
581
582 if (args)
583 memcpy(closure->args, args, count * sizeof *args);
584
585 closure->message = message;
586 closure->count = count;
587
588 /* Set these all to -1 so we can close any that have been
589 * set to a real value during wl_closure_destroy().
590 * We may have copied a bunch of fds into the closure with
591 * memcpy previously, but those are undup()d client fds
592 * that we would have replaced anyway.
593 */
594 wl_closure_clear_fds(closure);
595
596 return closure;
597 }
598
599 struct wl_closure *
wl_closure_marshal(struct wl_object * sender,uint32_t opcode,union wl_argument * args,const struct wl_message * message)600 wl_closure_marshal(struct wl_object *sender, uint32_t opcode,
601 union wl_argument *args,
602 const struct wl_message *message)
603 {
604 struct wl_closure *closure;
605 struct wl_object *object;
606 int i, count, fd, dup_fd;
607 const char *signature;
608 struct argument_details arg;
609
610 closure = wl_closure_init(message, 0, NULL, args);
611 if (closure == NULL)
612 return NULL;
613
614 count = closure->count;
615
616 signature = message->signature;
617 for (i = 0; i < count; i++) {
618 signature = get_next_argument(signature, &arg);
619
620 switch (arg.type) {
621 case 'f':
622 case 'u':
623 case 'i':
624 break;
625 case 's':
626 if (!arg.nullable && args[i].s == NULL)
627 goto err_null;
628 break;
629 case 'o':
630 if (!arg.nullable && args[i].o == NULL)
631 goto err_null;
632 break;
633 case 'n':
634 object = args[i].o;
635 if (!arg.nullable && object == NULL)
636 goto err_null;
637
638 closure->args[i].n = object ? object->id : 0;
639 break;
640 case 'a':
641 if (!arg.nullable && args[i].a == NULL)
642 goto err_null;
643 break;
644 case 'h':
645 fd = args[i].h;
646 dup_fd = wl_os_dupfd_cloexec(fd, 0);
647 if (dup_fd < 0) {
648 wl_closure_destroy(closure);
649 wl_log("error marshalling arguments for %s: dup failed: %s\n",
650 message->name, strerror(errno));
651 return NULL;
652 }
653 closure->args[i].h = dup_fd;
654 break;
655 default:
656 wl_abort("unhandled format code: '%c'\n", arg.type);
657 break;
658 }
659 }
660
661 closure->sender_id = sender->id;
662 closure->opcode = opcode;
663
664 return closure;
665
666 err_null:
667 wl_closure_destroy(closure);
668 wl_log("error marshalling arguments for %s (signature %s): "
669 "null value passed for arg %i\n", message->name,
670 message->signature, i);
671 errno = EINVAL;
672 return NULL;
673 }
674
675 struct wl_closure *
wl_closure_vmarshal(struct wl_object * sender,uint32_t opcode,va_list ap,const struct wl_message * message)676 wl_closure_vmarshal(struct wl_object *sender, uint32_t opcode, va_list ap,
677 const struct wl_message *message)
678 {
679 union wl_argument args[WL_CLOSURE_MAX_ARGS];
680
681 wl_argument_from_va_list(message->signature, args,
682 WL_CLOSURE_MAX_ARGS, ap);
683
684 return wl_closure_marshal(sender, opcode, args, message);
685 }
686
687 struct wl_closure *
wl_connection_demarshal(struct wl_connection * connection,uint32_t size,struct wl_map * objects,const struct wl_message * message)688 wl_connection_demarshal(struct wl_connection *connection,
689 uint32_t size,
690 struct wl_map *objects,
691 const struct wl_message *message)
692 {
693 uint32_t *p, *next, *end, length, length_in_u32, id;
694 int fd;
695 char *s;
696 int i, count, num_arrays;
697 const char *signature;
698 struct argument_details arg;
699 struct wl_closure *closure;
700 struct wl_array *array_extra;
701
702 /* Space for sender_id and opcode */
703 if (size < 2 * sizeof *p) {
704 wl_log("message too short, invalid header\n");
705 wl_connection_consume(connection, size);
706 errno = EINVAL;
707 return NULL;
708 }
709
710 closure = wl_closure_init(message, size, &num_arrays, NULL);
711 if (closure == NULL) {
712 wl_connection_consume(connection, size);
713 return NULL;
714 }
715
716 count = closure->count;
717
718 array_extra = closure->extra;
719 p = (uint32_t *)(closure->extra + num_arrays);
720 end = p + size / sizeof *p;
721
722 wl_connection_copy(connection, p, size);
723 closure->sender_id = *p++;
724 closure->opcode = *p++ & 0x0000ffff;
725
726 signature = message->signature;
727 for (i = 0; i < count; i++) {
728 signature = get_next_argument(signature, &arg);
729
730 if (arg.type != 'h' && p + 1 > end) {
731 wl_log("message too short, "
732 "object (%d), message %s(%s)\n",
733 closure->sender_id, message->name,
734 message->signature);
735 errno = EINVAL;
736 goto err;
737 }
738
739 switch (arg.type) {
740 case 'u':
741 closure->args[i].u = *p++;
742 break;
743 case 'i':
744 closure->args[i].i = *p++;
745 break;
746 case 'f':
747 closure->args[i].f = *p++;
748 break;
749 case 's':
750 length = *p++;
751
752 if (length == 0) {
753 closure->args[i].s = NULL;
754 break;
755 }
756
757 length_in_u32 = div_roundup(length, sizeof *p);
758 if ((uint32_t) (end - p) < length_in_u32) {
759 wl_log("message too short, "
760 "object (%d), message %s(%s)\n",
761 closure->sender_id, message->name,
762 message->signature);
763 errno = EINVAL;
764 goto err;
765 }
766 next = p + length_in_u32;
767
768 s = (char *) p;
769
770 if (length > 0 && s[length - 1] != '\0') {
771 wl_log("string not nul-terminated, "
772 "message %s(%s)\n",
773 message->name, message->signature);
774 errno = EINVAL;
775 goto err;
776 }
777
778 closure->args[i].s = s;
779 p = next;
780 break;
781 case 'o':
782 id = *p++;
783 closure->args[i].n = id;
784
785 if (id == 0 && !arg.nullable) {
786 wl_log("NULL object received on non-nullable "
787 "type, message %s(%s)\n", message->name,
788 message->signature);
789 errno = EINVAL;
790 goto err;
791 }
792 break;
793 case 'n':
794 id = *p++;
795 closure->args[i].n = id;
796
797 if (id == 0 && !arg.nullable) {
798 wl_log("NULL new ID received on non-nullable "
799 "type, message %s(%s)\n", message->name,
800 message->signature);
801 errno = EINVAL;
802 goto err;
803 }
804
805 if (wl_map_reserve_new(objects, id) < 0) {
806 wl_log("not a valid new object id (%u), "
807 "message %s(%s)\n",
808 id, message->name, message->signature);
809 errno = EINVAL;
810 goto err;
811 }
812
813 break;
814 case 'a':
815 length = *p++;
816
817 length_in_u32 = div_roundup(length, sizeof *p);
818 if ((uint32_t) (end - p) < length_in_u32) {
819 wl_log("message too short, "
820 "object (%d), message %s(%s)\n",
821 closure->sender_id, message->name,
822 message->signature);
823 errno = EINVAL;
824 goto err;
825 }
826 next = p + length_in_u32;
827
828 array_extra->size = length;
829 array_extra->alloc = 0;
830 array_extra->data = p;
831
832 closure->args[i].a = array_extra++;
833 p = next;
834 break;
835 case 'h':
836 if (connection->fds_in.tail == connection->fds_in.head) {
837 wl_log("file descriptor expected, "
838 "object (%d), message %s(%s)\n",
839 closure->sender_id, message->name,
840 message->signature);
841 errno = EINVAL;
842 goto err;
843 }
844
845 wl_buffer_copy(&connection->fds_in, &fd, sizeof fd);
846 connection->fds_in.tail += sizeof fd;
847 closure->args[i].h = fd;
848 break;
849 default:
850 wl_abort("unknown type\n");
851 break;
852 }
853 }
854
855 wl_connection_consume(connection, size);
856
857 return closure;
858
859 err:
860 wl_closure_destroy(closure);
861 wl_connection_consume(connection, size);
862
863 return NULL;
864 }
865
866 bool
wl_object_is_zombie(struct wl_map * map,uint32_t id)867 wl_object_is_zombie(struct wl_map *map, uint32_t id)
868 {
869 uint32_t flags;
870
871 /* Zombie objects only exist on the client side. */
872 if (map->side == WL_MAP_SERVER_SIDE)
873 return false;
874
875 /* Zombie objects can only have been created by the client. */
876 if (id >= WL_SERVER_ID_START)
877 return false;
878
879 flags = wl_map_lookup_flags(map, id);
880 return !!(flags & WL_MAP_ENTRY_ZOMBIE);
881 }
882
883 int
wl_closure_lookup_objects(struct wl_closure * closure,struct wl_map * objects)884 wl_closure_lookup_objects(struct wl_closure *closure, struct wl_map *objects)
885 {
886 struct wl_object *object;
887 const struct wl_message *message;
888 const char *signature;
889 struct argument_details arg;
890 int i, count;
891 uint32_t id;
892
893 message = closure->message;
894 signature = message->signature;
895 count = arg_count_for_signature(signature);
896 for (i = 0; i < count; i++) {
897 signature = get_next_argument(signature, &arg);
898 switch (arg.type) {
899 case 'o':
900 id = closure->args[i].n;
901 closure->args[i].o = NULL;
902
903 object = wl_map_lookup(objects, id);
904 if (wl_object_is_zombie(objects, id)) {
905 /* references object we've already
906 * destroyed client side */
907 object = NULL;
908 } else if (object == NULL && id != 0) {
909 wl_log("unknown object (%u), message %s(%s)\n",
910 id, message->name, message->signature);
911 errno = EINVAL;
912 return -1;
913 }
914
915 if (object != NULL && message->types[i] != NULL &&
916 !wl_interface_equal((object)->interface,
917 message->types[i])) {
918 wl_log("invalid object (%u), type (%s), "
919 "message %s(%s)\n",
920 id, (object)->interface->name,
921 message->name, message->signature);
922 errno = EINVAL;
923 return -1;
924 }
925 closure->args[i].o = object;
926 }
927 }
928
929 return 0;
930 }
931
932 static void
convert_arguments_to_ffi(const char * signature,uint32_t flags,union wl_argument * args,int count,ffi_type ** ffi_types,void ** ffi_args)933 convert_arguments_to_ffi(const char *signature, uint32_t flags,
934 union wl_argument *args,
935 int count, ffi_type **ffi_types, void** ffi_args)
936 {
937 int i;
938 const char *sig_iter;
939 struct argument_details arg;
940
941 sig_iter = signature;
942 for (i = 0; i < count; i++) {
943 sig_iter = get_next_argument(sig_iter, &arg);
944
945 switch(arg.type) {
946 case 'i':
947 ffi_types[i] = &ffi_type_sint32;
948 ffi_args[i] = &args[i].i;
949 break;
950 case 'u':
951 ffi_types[i] = &ffi_type_uint32;
952 ffi_args[i] = &args[i].u;
953 break;
954 case 'f':
955 ffi_types[i] = &ffi_type_sint32;
956 ffi_args[i] = &args[i].f;
957 break;
958 case 's':
959 ffi_types[i] = &ffi_type_pointer;
960 ffi_args[i] = &args[i].s;
961 break;
962 case 'o':
963 ffi_types[i] = &ffi_type_pointer;
964 ffi_args[i] = &args[i].o;
965 break;
966 case 'n':
967 if (flags & WL_CLOSURE_INVOKE_CLIENT) {
968 ffi_types[i] = &ffi_type_pointer;
969 ffi_args[i] = &args[i].o;
970 } else {
971 ffi_types[i] = &ffi_type_uint32;
972 ffi_args[i] = &args[i].n;
973 }
974 break;
975 case 'a':
976 ffi_types[i] = &ffi_type_pointer;
977 ffi_args[i] = &args[i].a;
978 break;
979 case 'h':
980 ffi_types[i] = &ffi_type_sint32;
981 ffi_args[i] = &args[i].h;
982 break;
983 default:
984 wl_abort("unknown type\n");
985 break;
986 }
987 }
988 }
989
990 void
wl_closure_invoke(struct wl_closure * closure,uint32_t flags,struct wl_object * target,uint32_t opcode,void * data)991 wl_closure_invoke(struct wl_closure *closure, uint32_t flags,
992 struct wl_object *target, uint32_t opcode, void *data)
993 {
994 int count;
995 ffi_cif cif;
996 ffi_type *ffi_types[WL_CLOSURE_MAX_ARGS + 2];
997 void * ffi_args[WL_CLOSURE_MAX_ARGS + 2];
998 void (* const *implementation)(void);
999
1000 count = arg_count_for_signature(closure->message->signature);
1001
1002 ffi_types[0] = &ffi_type_pointer;
1003 ffi_args[0] = &data;
1004 ffi_types[1] = &ffi_type_pointer;
1005 ffi_args[1] = ⌖
1006
1007 convert_arguments_to_ffi(closure->message->signature, flags, closure->args,
1008 count, ffi_types + 2, ffi_args + 2);
1009
1010 ffi_prep_cif(&cif, FFI_DEFAULT_ABI,
1011 count + 2, &ffi_type_void, ffi_types);
1012
1013 implementation = target->implementation;
1014 // OHOS fix: if listener function is not NULL, it will be call
1015 if (implementation[opcode]) {
1016 ffi_call(&cif, implementation[opcode], NULL, ffi_args);
1017 }
1018
1019 wl_closure_clear_fds(closure);
1020 }
1021
1022 void
wl_closure_dispatch(struct wl_closure * closure,wl_dispatcher_func_t dispatcher,struct wl_object * target,uint32_t opcode)1023 wl_closure_dispatch(struct wl_closure *closure, wl_dispatcher_func_t dispatcher,
1024 struct wl_object *target, uint32_t opcode)
1025 {
1026 dispatcher(target->implementation, target, opcode, closure->message,
1027 closure->args);
1028
1029 wl_closure_clear_fds(closure);
1030 }
1031
1032 static int
copy_fds_to_connection(struct wl_closure * closure,struct wl_connection * connection)1033 copy_fds_to_connection(struct wl_closure *closure,
1034 struct wl_connection *connection)
1035 {
1036 const struct wl_message *message = closure->message;
1037 uint32_t i, count;
1038 struct argument_details arg;
1039 const char *signature = message->signature;
1040 int fd;
1041
1042 count = arg_count_for_signature(signature);
1043 for (i = 0; i < count; i++) {
1044 signature = get_next_argument(signature, &arg);
1045 if (arg.type != 'h')
1046 continue;
1047
1048 fd = closure->args[i].h;
1049 if (wl_connection_put_fd(connection, fd)) {
1050 wl_log("request could not be marshaled: "
1051 "can't send file descriptor");
1052 return -1;
1053 }
1054 closure->args[i].h = -1;
1055 }
1056
1057 return 0;
1058 }
1059
1060
1061 static uint32_t
buffer_size_for_closure(struct wl_closure * closure)1062 buffer_size_for_closure(struct wl_closure *closure)
1063 {
1064 const struct wl_message *message = closure->message;
1065 int i, count;
1066 struct argument_details arg;
1067 const char *signature;
1068 uint32_t size, buffer_size = 0;
1069
1070 signature = message->signature;
1071 count = arg_count_for_signature(signature);
1072 for (i = 0; i < count; i++) {
1073 signature = get_next_argument(signature, &arg);
1074
1075 switch (arg.type) {
1076 case 'h':
1077 break;
1078 case 'u':
1079 case 'i':
1080 case 'f':
1081 case 'o':
1082 case 'n':
1083 buffer_size++;
1084 break;
1085 case 's':
1086 if (closure->args[i].s == NULL) {
1087 buffer_size++;
1088 break;
1089 }
1090
1091 size = strlen(closure->args[i].s) + 1;
1092 buffer_size += 1 + div_roundup(size, sizeof(uint32_t));
1093 break;
1094 case 'a':
1095 if (closure->args[i].a == NULL) {
1096 buffer_size++;
1097 break;
1098 }
1099
1100 size = closure->args[i].a->size;
1101 buffer_size += (1 + div_roundup(size, sizeof(uint32_t)));
1102 break;
1103 default:
1104 break;
1105 }
1106 }
1107
1108 return buffer_size + 2;
1109 }
1110
1111 static int
serialize_closure(struct wl_closure * closure,uint32_t * buffer,size_t buffer_count)1112 serialize_closure(struct wl_closure *closure, uint32_t *buffer,
1113 size_t buffer_count)
1114 {
1115 const struct wl_message *message = closure->message;
1116 unsigned int i, count, size;
1117 uint32_t *p, *end;
1118 struct argument_details arg;
1119 const char *signature;
1120
1121 if (buffer_count < 2)
1122 goto overflow;
1123
1124 p = buffer + 2;
1125 end = buffer + buffer_count;
1126
1127 signature = message->signature;
1128 count = arg_count_for_signature(signature);
1129 for (i = 0; i < count; i++) {
1130 signature = get_next_argument(signature, &arg);
1131
1132 if (arg.type == 'h')
1133 continue;
1134
1135 if (p + 1 > end)
1136 goto overflow;
1137
1138 switch (arg.type) {
1139 case 'u':
1140 *p++ = closure->args[i].u;
1141 break;
1142 case 'i':
1143 *p++ = closure->args[i].i;
1144 break;
1145 case 'f':
1146 *p++ = closure->args[i].f;
1147 break;
1148 case 'o':
1149 *p++ = closure->args[i].o ? closure->args[i].o->id : 0;
1150 break;
1151 case 'n':
1152 *p++ = closure->args[i].n;
1153 break;
1154 case 's':
1155 if (closure->args[i].s == NULL) {
1156 *p++ = 0;
1157 break;
1158 }
1159
1160 size = strlen(closure->args[i].s) + 1;
1161 *p++ = size;
1162
1163 if (p + div_roundup(size, sizeof *p) > end)
1164 goto overflow;
1165
1166 memcpy(p, closure->args[i].s, size);
1167 p += div_roundup(size, sizeof *p);
1168 break;
1169 case 'a':
1170 if (closure->args[i].a == NULL) {
1171 *p++ = 0;
1172 break;
1173 }
1174
1175 size = closure->args[i].a->size;
1176 *p++ = size;
1177
1178 if (p + div_roundup(size, sizeof *p) > end)
1179 goto overflow;
1180
1181 memcpy(p, closure->args[i].a->data, size);
1182 p += div_roundup(size, sizeof *p);
1183 break;
1184 default:
1185 break;
1186 }
1187 }
1188
1189 size = (p - buffer) * sizeof *p;
1190
1191 buffer[0] = closure->sender_id;
1192 buffer[1] = size << 16 | (closure->opcode & 0x0000ffff);
1193
1194 return size;
1195
1196 overflow:
1197 errno = ERANGE;
1198 return -1;
1199 }
1200
1201 int
wl_closure_send(struct wl_closure * closure,struct wl_connection * connection)1202 wl_closure_send(struct wl_closure *closure, struct wl_connection *connection)
1203 {
1204 int size;
1205 uint32_t buffer_size;
1206 uint32_t *buffer;
1207 int result;
1208
1209 if (copy_fds_to_connection(closure, connection))
1210 return -1;
1211
1212 buffer_size = buffer_size_for_closure(closure);
1213 buffer = zalloc(buffer_size * sizeof buffer[0]);
1214 if (buffer == NULL)
1215 return -1;
1216
1217 size = serialize_closure(closure, buffer, buffer_size);
1218 if (size < 0) {
1219 free(buffer);
1220 return -1;
1221 }
1222
1223 result = wl_connection_write(connection, buffer, size);
1224 free(buffer);
1225
1226 return result;
1227 }
1228
1229 int
wl_closure_queue(struct wl_closure * closure,struct wl_connection * connection)1230 wl_closure_queue(struct wl_closure *closure, struct wl_connection *connection)
1231 {
1232 int size;
1233 uint32_t buffer_size;
1234 uint32_t *buffer;
1235 int result;
1236
1237 if (copy_fds_to_connection(closure, connection))
1238 return -1;
1239
1240 buffer_size = buffer_size_for_closure(closure);
1241 buffer = malloc(buffer_size * sizeof buffer[0]);
1242 if (buffer == NULL)
1243 return -1;
1244
1245 size = serialize_closure(closure, buffer, buffer_size);
1246 if (size < 0) {
1247 free(buffer);
1248 return -1;
1249 }
1250
1251 result = wl_connection_queue(connection, buffer, size);
1252 free(buffer);
1253
1254 return result;
1255 }
1256
1257 void
wl_closure_print(struct wl_closure * closure,struct wl_object * target,int send)1258 wl_closure_print(struct wl_closure *closure, struct wl_object *target, int send)
1259 {
1260 int i;
1261 struct argument_details arg;
1262 const char *signature = closure->message->signature;
1263 struct timespec tp;
1264 unsigned int time;
1265
1266 clock_gettime(CLOCK_REALTIME, &tp);
1267 time = (tp.tv_sec * 1000000L) + (tp.tv_nsec / 1000);
1268
1269 fprintf(stderr, "[%10.3f] %s%s@%u.%s(",
1270 time / 1000.0,
1271 send ? " -> " : "",
1272 target->interface->name, target->id,
1273 closure->message->name);
1274
1275 for (i = 0; i < closure->count; i++) {
1276 signature = get_next_argument(signature, &arg);
1277 if (i > 0)
1278 fprintf(stderr, ", ");
1279
1280 switch (arg.type) {
1281 case 'u':
1282 fprintf(stderr, "%u", closure->args[i].u);
1283 break;
1284 case 'i':
1285 fprintf(stderr, "%d", closure->args[i].i);
1286 break;
1287 case 'f':
1288 fprintf(stderr, "%f",
1289 wl_fixed_to_double(closure->args[i].f));
1290 break;
1291 case 's':
1292 if (closure->args[i].s)
1293 fprintf(stderr, "\"%s\"", closure->args[i].s);
1294 else
1295 fprintf(stderr, "nil");
1296 break;
1297 case 'o':
1298 if (closure->args[i].o)
1299 fprintf(stderr, "%s@%u",
1300 closure->args[i].o->interface->name,
1301 closure->args[i].o->id);
1302 else
1303 fprintf(stderr, "nil");
1304 break;
1305 case 'n':
1306 fprintf(stderr, "new id %s@",
1307 (closure->message->types[i]) ?
1308 closure->message->types[i]->name :
1309 "[unknown]");
1310 if (closure->args[i].n != 0)
1311 fprintf(stderr, "%u", closure->args[i].n);
1312 else
1313 fprintf(stderr, "nil");
1314 break;
1315 case 'a':
1316 fprintf(stderr, "array");
1317 break;
1318 case 'h':
1319 fprintf(stderr, "fd %d", closure->args[i].h);
1320 break;
1321 }
1322 }
1323
1324 fprintf(stderr, ")\n");
1325 }
1326
1327 static int
wl_closure_close_fds(struct wl_closure * closure)1328 wl_closure_close_fds(struct wl_closure *closure)
1329 {
1330 int i;
1331 struct argument_details arg;
1332 const char *signature = closure->message->signature;
1333
1334 for (i = 0; i < closure->count; i++) {
1335 signature = get_next_argument(signature, &arg);
1336 if (arg.type == 'h' && closure->args[i].h != -1)
1337 close(closure->args[i].h);
1338 }
1339
1340 return 0;
1341 }
1342
1343 void
wl_closure_destroy(struct wl_closure * closure)1344 wl_closure_destroy(struct wl_closure *closure)
1345 {
1346 /* wl_closure_destroy has free() semantics */
1347 if (!closure)
1348 return;
1349
1350 wl_closure_close_fds(closure);
1351 free(closure);
1352 }
1353