From f74c9b98db49ce16e037c3012590c4a24a4fc32e Mon Sep 17 00:00:00 2001 From: Derek Foreman Date: Wed, 6 Dec 2017 11:22:24 -0600 Subject: tests: Add a test for fd leaks on zombie objects Until recently, if a client destroying a resource raced with the server generating an event on that resource that delivered a file descriptor, we would leak the fd. This tests for a leaked fd from that race condition. Reviewed-by: Daniel Stone Signed-off-by: Derek Foreman --- tests/display-test.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) (limited to 'tests') diff --git a/tests/display-test.c b/tests/display-test.c index e6f0369..0623158 100644 --- a/tests/display-test.c +++ b/tests/display-test.c @@ -47,6 +47,9 @@ #include "test-runner.h" #include "test-compositor.h" +#include "tests-server-protocol.h" +#include "tests-client-protocol.h" + struct display_destroy_listener { struct wl_listener listener; int done; @@ -1066,3 +1069,102 @@ TEST(bind_fails_on_filtered_global) display_destroy(d); } + +static void +pre_fd(void *data, struct fd_passer *fdp) +{ + fd_passer_destroy(fdp); +} + +static void +fd(void *data, struct fd_passer *fdp, int32_t fd) +{ + /* We destroyed the resource before this event */ + assert(false); +} + +struct fd_passer_listener fd_passer_listener = { + pre_fd, + fd, +}; + +static void +zombie_fd_handle_globals(void *data, struct wl_registry *registry, + uint32_t id, const char *intf, uint32_t ver) +{ + struct fd_passer *fdp; + + if (!strcmp(intf, "fd_passer")) { + fdp = wl_registry_bind(registry, id, &fd_passer_interface, 1); + fd_passer_add_listener(fdp, &fd_passer_listener, NULL); + } +} + +static const struct wl_registry_listener zombie_fd_registry_listener = { + zombie_fd_handle_globals, + NULL +}; + +static void +zombie_client(void *data) +{ + struct client *c = client_connect(); + struct wl_registry *registry; + + registry = wl_display_get_registry(c->wl_display); + wl_registry_add_listener(registry, &zombie_fd_registry_listener, NULL); + + /* Gets the registry */ + wl_display_roundtrip(c->wl_display); + + /* push out the fd_passer bind */ + wl_display_roundtrip(c->wl_display); + + /* push out our fd_passer.destroy */ + wl_display_roundtrip(c->wl_display); + + wl_registry_destroy(registry); + + client_disconnect_nocheck(c); +} + +static void +fd_passer_clobber(struct wl_client *client, struct wl_resource *res) +{ + wl_resource_destroy(res); +} + +static const struct fd_passer_interface fdp_interface = { + fd_passer_clobber, +}; + +static void +bind_fd_passer(struct wl_client *client, void *data, + uint32_t vers, uint32_t id) +{ + struct wl_resource *res; + + res = wl_resource_create(client, &fd_passer_interface, vers, id); + wl_resource_set_implementation(res, &fdp_interface, NULL, NULL); + assert(res); + fd_passer_send_pre_fd(res); + fd_passer_send_fd(res, fileno(stdin)); +} + +TEST(zombie_fd) +{ + struct display *d; + struct wl_global *g; + + d = display_create(); + + g = wl_global_create(d->wl_display, &fd_passer_interface, + 1, d, bind_fd_passer); + + client_create_noarg(d, zombie_client); + display_run(d); + + wl_global_destroy(g); + + display_destroy(d); +} -- cgit v1.2.3-70-g09d2