xref: /aosp_15_r20/external/wayland/patches/0005-server-Safe-cast-a-wl_object-to-wl_resource.diff (revision 84e872a0dc482bffdb63672969dd03a827d67c73)
1*84e872a0SLloyd PiqueFrom 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2*84e872a0SLloyd PiqueFrom: Lloyd Pique <[email protected]>
3*84e872a0SLloyd PiqueDate: Fri, 11 Mar 2022 19:10:07 -0800
4*84e872a0SLloyd PiqueSubject: [PATCH 5/6] server: Safe cast a "wl_object *" to "wl_resource *"
5*84e872a0SLloyd Pique
6*84e872a0SLloyd PiqueClient message observers 5/6
7*84e872a0SLloyd Pique
8*84e872a0SLloyd PiqueWhen given an array of wl_arguments for a wl_closure, the .o field is an
9*84e872a0SLloyd Piqueopaque wl_object pointer, which the server implementation cannot really do
10*84e872a0SLloyd Piqueanything with, without a potentially unsafe cast that assumes details about the
11*84e872a0SLloyd Piqueinternal implementation.
12*84e872a0SLloyd Pique
13*84e872a0SLloyd PiqueBy adding a wl_resource_from_object() function to the client interface, the client
14*84e872a0SLloyd Piquecan safely get the wl_resource pointer.
15*84e872a0SLloyd Pique
16*84e872a0SLloyd PiqueThis can be used by server protocol loggers in particular to get the resource id
17*84e872a0SLloyd Piqueand class name, for logging those details
18*84e872a0SLloyd Pique
19*84e872a0SLloyd PiqueSigned-off-by: Lloyd Pique <[email protected]>
20*84e872a0SLloyd Pique
21*84e872a0SLloyd Piquediff --git a/src/wayland-server-core.h b/src/wayland-server-core.h
22*84e872a0SLloyd Piqueindex df95821..63c6a62 100644
23*84e872a0SLloyd Pique--- a/src/wayland-server-core.h
24*84e872a0SLloyd Pique+++ b/src/wayland-server-core.h
25*84e872a0SLloyd Pique@@ -608,6 +608,9 @@ struct wl_listener *
26*84e872a0SLloyd Pique wl_resource_get_destroy_listener(struct wl_resource *resource,
27*84e872a0SLloyd Pique 				 wl_notify_func_t notify);
28*84e872a0SLloyd Pique
29*84e872a0SLloyd Pique+struct wl_resource *
30*84e872a0SLloyd Pique+wl_resource_from_object(struct wl_object *object);
31*84e872a0SLloyd Pique+
32*84e872a0SLloyd Pique #define wl_resource_for_each(resource, list)					\
33*84e872a0SLloyd Pique 	for (resource = 0, resource = wl_resource_from_link((list)->next);	\
34*84e872a0SLloyd Pique 	     wl_resource_get_link(resource) != (list);				\
35*84e872a0SLloyd Piquediff --git a/src/wayland-server.c b/src/wayland-server.c
36*84e872a0SLloyd Piqueindex be98f7d..468322d 100644
37*84e872a0SLloyd Pique--- a/src/wayland-server.c
38*84e872a0SLloyd Pique+++ b/src/wayland-server.c
39*84e872a0SLloyd Pique@@ -866,6 +866,28 @@ wl_resource_get_class(struct wl_resource *resource)
40*84e872a0SLloyd Pique 	return resource->object.interface->name;
41*84e872a0SLloyd Pique }
42*84e872a0SLloyd Pique
43*84e872a0SLloyd Pique+/** Safely converts an object into its corresponding resource
44*84e872a0SLloyd Pique+ *
45*84e872a0SLloyd Pique+ * \param object object to get the resource for
46*84e872a0SLloyd Pique+ * \return A corresponding resource, or NULL on failure
47*84e872a0SLloyd Pique+ *
48*84e872a0SLloyd Pique+ * Safely converts an object into its corresponding resource.
49*84e872a0SLloyd Pique+ *
50*84e872a0SLloyd Pique+ * This is useful for implementing functions that are given a \c wl_argument
51*84e872a0SLloyd Pique+ * array, and that need to do further introspection on the ".o" field, as it
52*84e872a0SLloyd Pique+ * is otherwise an opaque type.
53*84e872a0SLloyd Pique+ *
54*84e872a0SLloyd Pique+ * \memberof wl_resource
55*84e872a0SLloyd Pique+ */
56*84e872a0SLloyd Pique+WL_EXPORT struct wl_resource *
57*84e872a0SLloyd Pique+wl_resource_from_object(struct wl_object *object)
58*84e872a0SLloyd Pique+{
59*84e872a0SLloyd Pique+	struct wl_resource *resource;
60*84e872a0SLloyd Pique+	if (object == NULL)
61*84e872a0SLloyd Pique+		return NULL;
62*84e872a0SLloyd Pique+	return wl_container_of(object, resource, object);
63*84e872a0SLloyd Pique+}
64*84e872a0SLloyd Pique+
65*84e872a0SLloyd Pique /**
66*84e872a0SLloyd Pique  * Add a listener to be called at the beginning of wl_client destruction
67*84e872a0SLloyd Pique  *
68