summaryrefslogtreecommitdiffhomepage
path: root/packages/ext-layer-shell/tests/test_client.cpp
blob: ac79253bd8977324045d31b531a6b344af183268 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>

#include <unbox/ext-layer-shell/ext_layer_shell.hpp>
#include <unbox/kernel/server.hpp>

#include <wayland-client.h>

// The generated client header (and code) name a request argument `namespace` —
// a C identifier but a C++ keyword. Same scoped-macro fix wlr.hpp documents for
// the server side: rename it across exactly these two generated includes. The
// only call that touches it (get_layer_surface's last arg) is a string literal
// we pass positionally, so the rename never leaks into our own code.
#define namespace namespace_
#include "wlr-layer-shell-unstable-v1-client-protocol.h"
// xdg-shell marshalling code: supplies xdg_popup_interface, referenced by the
// layer-shell tables (get_popup). Code-form only; we use no xdg-shell requests.
#include "xdg-shell-client-protocol-code.h"
// The protocol marshalling tables/stubs (wayland-scanner private-code), emitted
// as a header so it can be compiled by the C++ TU exactly once (see meson.build).
#include "wlr-layer-shell-unstable-v1-client-protocol-code.h"
#undef namespace

#include <cstdlib>
#include <cstring>
#include <memory>
#include <poll.h>

// REGRESSION: a real wayland CLIENT (the fuzzel reproduction, in-process) binds
// zwlr_layer_shell_v1, creates a layer surface with a NIL output, and MUST
// receive a configure. Before the fix, ext-layer-shell's output tracking was
// events-only: the headless output created during Server::create() (BEFORE
// activation) was never tracked, so an output-less surface got no output, no
// wlr_scene_layer_surface_v1_configure, no configure event — the exact symptom
// in /tmp/opencode/fuzzel-trace.log (get_layer_surface(... nil ...) then a bare
// closed() with no configure). This test would have caught it.
//
// Server and client share this single thread; we pump both loops cooperatively
// (the standard libwayland prepare_read / read_events dance, non-blocking).

namespace {

struct Client {
    wl_display* display = nullptr;
    wl_registry* registry = nullptr;
    wl_compositor* compositor = nullptr;
    wl_output* output = nullptr;
    zwlr_layer_shell_v1* layer_shell = nullptr;

    wl_surface* surface = nullptr;
    zwlr_layer_surface_v1* layer_surface = nullptr;

    bool configured = false;
    bool closed = false;
    std::uint32_t configure_serial = 0;
    int configure_count = 0;
};

void registry_global(void* data, wl_registry* reg, std::uint32_t name,
                     const char* iface, std::uint32_t version) {
    auto* c = static_cast<Client*>(data);
    if (std::strcmp(iface, wl_compositor_interface.name) == 0) {
        c->compositor = static_cast<wl_compositor*>(
            wl_registry_bind(reg, name, &wl_compositor_interface, 4));
    } else if (std::strcmp(iface, wl_output_interface.name) == 0 &&
               c->output == nullptr) {
        c->output = static_cast<wl_output*>(
            wl_registry_bind(reg, name, &wl_output_interface, version));
    } else if (std::strcmp(iface, zwlr_layer_shell_v1_interface.name) == 0) {
        c->layer_shell = static_cast<zwlr_layer_shell_v1*>(wl_registry_bind(
            reg, name, &zwlr_layer_shell_v1_interface,
            version < 4 ? version : 4));
    }
}
void registry_global_remove(void*, wl_registry*, std::uint32_t) {}

const wl_registry_listener kRegistryListener{registry_global,
                                             registry_global_remove};

void ls_configure(void* data, zwlr_layer_surface_v1* ls, std::uint32_t serial,
                  std::uint32_t, std::uint32_t) {
    auto* c = static_cast<Client*>(data);
    c->configured = true;
    c->configure_serial = serial;
    zwlr_layer_surface_v1_ack_configure(ls, serial);
}
void ls_closed(void* data, zwlr_layer_surface_v1*) {
    static_cast<Client*>(data)->closed = true;
}
const zwlr_layer_surface_v1_listener kLayerSurfaceListener{ls_configure,
                                                           ls_closed};

// A configure handler that mimics a real client (fuzzel) APPLYING its configure:
// ack, then commit the surface to apply it. Counts how many configures arrive.
void ls_configure_apply(void* data, zwlr_layer_surface_v1* ls,
                        std::uint32_t serial, std::uint32_t, std::uint32_t) {
    auto* c = static_cast<Client*>(data);
    c->configured = true;
    c->configure_serial = serial;
    c->configure_count++;
    zwlr_layer_surface_v1_ack_configure(ls, serial);
    // The apply-commit: this re-enters the server's surface commit handler.
    // Before the fix that handler unconditionally re-configured, so this commit
    // produced ANOTHER configure -> ack -> commit ... an unbounded loop that
    // overflowed the 4 KiB client connection buffer and killed the client.
    wl_surface_commit(c->surface);
}
const zwlr_layer_surface_v1_listener kLayerSurfaceApplyListener{ls_configure_apply,
                                                                ls_closed};

// Pump server and client once, without blocking the client read.
void pump(unbox::kernel::Server& server, wl_display* client) {
    wl_display_flush(client);
    server.dispatch(5);

    // Drain anything already queued, then do a guarded non-blocking read.
    while (wl_display_prepare_read(client) != 0) {
        wl_display_dispatch_pending(client);
    }
    wl_display_flush(client);

    pollfd pfd{wl_display_get_fd(client), POLLIN, 0};
    if (poll(&pfd, 1, 5) > 0 && (pfd.revents & POLLIN)) {
        wl_display_read_events(client);
    } else {
        wl_display_cancel_read(client);
    }
    wl_display_dispatch_pending(client);
}

auto make_headless_server() -> std::unique_ptr<unbox::kernel::Server> {
    setenv("WLR_BACKENDS", "headless", 1);
    setenv("WLR_RENDERER", "pixman", 1);
    setenv("WLR_HEADLESS_OUTPUTS", "1", 1); // output exists BEFORE activation
    return unbox::kernel::Server::create({});
}

} // namespace

TEST_CASE("a real client's nil-output layer surface receives a configure") {
    auto server = make_headless_server();
    server->install(unbox::ext_layer_shell::create());
    server->activate_extensions();
    REQUIRE(!server->socket_name().empty());

    Client c;
    c.display = wl_display_connect(server->socket_name().c_str());
    REQUIRE(c.display != nullptr);
    c.registry = wl_display_get_registry(c.display);
    wl_registry_add_listener(c.registry, &kRegistryListener, &c);

    // Round 1: let the registry advertise globals and our binds reach the
    // server (compositor, output, layer_shell).
    for (int i = 0; i < 50 && (c.compositor == nullptr || c.layer_shell == nullptr);
         ++i) {
        pump(*server, c.display);
    }
    REQUIRE(c.compositor != nullptr);
    REQUIRE(c.layer_shell != nullptr);

    // Create a layer surface with a NIL output (exactly fuzzel's get_layer_surface
    // call: output=nil, layer=overlay, namespace="launcher").
    c.surface = wl_compositor_create_surface(c.compositor);
    c.layer_surface = zwlr_layer_shell_v1_get_layer_surface(
        c.layer_shell, c.surface, /*output=*/nullptr,
        ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY, "launcher");
    REQUIRE(c.layer_surface != nullptr);
    zwlr_layer_surface_v1_add_listener(c.layer_surface, &kLayerSurfaceListener, &c);
    zwlr_layer_surface_v1_set_size(c.layer_surface, 382, 386);
    zwlr_layer_surface_v1_set_anchor(c.layer_surface, 0);
    // The mandatory initial empty commit (no buffer): the compositor MUST reply
    // with a configure.
    wl_surface_commit(c.surface);

    // Round 2: pump until the configure arrives (or we give up -> the bug).
    for (int i = 0; i < 200 && !c.configured && !c.closed; ++i) {
        pump(*server, c.display);
    }

    CHECK(c.configured);          // the fix: a configure was sent
    CHECK_FALSE(c.closed);        // the bug: surface closed with no configure
    CHECK(c.configure_serial != 0);

    if (c.layer_surface != nullptr) {
        zwlr_layer_surface_v1_destroy(c.layer_surface);
    }
    if (c.surface != nullptr) {
        wl_surface_destroy(c.surface);
    }
    if (c.layer_shell != nullptr) {
        zwlr_layer_shell_v1_destroy(c.layer_shell);
    }
    if (c.output != nullptr) {
        wl_output_destroy(c.output);
    }
    if (c.compositor != nullptr) {
        wl_compositor_destroy(c.compositor);
    }
    if (c.registry != nullptr) {
        wl_registry_destroy(c.registry);
    }
    wl_display_flush(c.display);
    pump(*server, c.display);
    wl_display_disconnect(c.display);
}

TEST_CASE("INVARIANT: applying configures keeps the configure count bounded") {
    // Context: the fuzzel "needs several Super presses to open" bug was a
    // configure -> ack -> commit -> configure feedback loop (the commit handler
    // reconfigured on EVERY commit) that flooded the client's connection buffer
    // and got it killed mid-handshake. The fix only re-arranges on the initial
    // commit or a layout-state change (current.committed != 0).
    //
    // HONEST CAVEAT: this cooperative in-process headless harness does NOT
    // reproduce the live loop (it passes with and without the fix) — the loop
    // needed the real DRM/event-loop timing. This case is kept as a cheap
    // INVARIANT guard: a client that faithfully acks + applies its configures
    // must not be driven into an unbounded configure storm. A future change that
    // reintroduces per-commit reconfiguration in a way the harness CAN provoke
    // will trip the bound below.
    auto server = make_headless_server();
    server->install(unbox::ext_layer_shell::create());
    server->activate_extensions();
    REQUIRE(!server->socket_name().empty());

    Client c;
    c.display = wl_display_connect(server->socket_name().c_str());
    REQUIRE(c.display != nullptr);
    c.registry = wl_display_get_registry(c.display);
    wl_registry_add_listener(c.registry, &kRegistryListener, &c);
    for (int i = 0; i < 50 && (c.compositor == nullptr || c.layer_shell == nullptr);
         ++i) {
        pump(*server, c.display);
    }
    REQUIRE(c.compositor != nullptr);
    REQUIRE(c.layer_shell != nullptr);

    c.surface = wl_compositor_create_surface(c.compositor);
    c.layer_surface = zwlr_layer_shell_v1_get_layer_surface(
        c.layer_shell, c.surface, /*output=*/nullptr,
        ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY, "launcher");
    REQUIRE(c.layer_surface != nullptr);
    // The APPLY listener: acks AND commits on every configure (real client).
    zwlr_layer_surface_v1_add_listener(c.layer_surface, &kLayerSurfaceApplyListener,
                                       &c);
    zwlr_layer_surface_v1_set_size(c.layer_surface, 382, 386);
    zwlr_layer_surface_v1_set_anchor(c.layer_surface, 0);
    wl_surface_commit(c.surface); // mandatory initial commit

    // Pump generously. With the bug, configure_count would explode (the client
    // re-commits on each configure); with the fix it settles after the first.
    for (int i = 0; i < 200 && !c.closed; ++i) {
        pump(*server, c.display);
    }

    CHECK(c.configured);   // got the initial configure
    CHECK_FALSE(c.closed); // not killed by a buffer overflow
    // The crux: a bounded number of configures. Correct behaviour is exactly 1
    // (the initial); allow slack for an incidental re-arrange. The BUG produced
    // hundreds-to-thousands here.
    CHECK(c.configure_count <= 3);

    if (c.layer_surface != nullptr) {
        zwlr_layer_surface_v1_destroy(c.layer_surface);
    }
    if (c.surface != nullptr) {
        wl_surface_destroy(c.surface);
    }
    if (c.layer_shell != nullptr) {
        zwlr_layer_shell_v1_destroy(c.layer_shell);
    }
    if (c.output != nullptr) {
        wl_output_destroy(c.output);
    }
    if (c.compositor != nullptr) {
        wl_compositor_destroy(c.compositor);
    }
    if (c.registry != nullptr) {
        wl_registry_destroy(c.registry);
    }
    wl_display_flush(c.display);
    pump(*server, c.display);
    wl_display_disconnect(c.display);
}