tests: nested xdg popup chain client + kill-mid-chain coverage

Add a --popup wltest mode (toplevel + popup P1 + nested sub-popup P2,
each buffer-backed so all three map) and a headless smoke test proving
a full chain maps, the client can be kill -9'd mid-chain, and a fresh
chain maps afterwards with the compositor alive and crash-free.

Writing the mode exposed two client-side protocol bugs in wltest
itself (both fixed here): the final get_popup+commit batch was never
flushed (dispatch_pending+poll neither sends), and the hold loop never
READ new events (dispatch_pending drains only the already-read queue),
so the nested popup's configure -- proven sent via server WAYLAND_DEBUG
-- starved unread and P2 never mapped. The hold loop now roundtrips
periodically, which reads, dispatches and flushes.

Investigated as a potential live_popups use-after-free (submenu scene
outliving its parent): wlroots cascades child-popup destruction on
parent teardown and kick removes entries on popup destroy, so no stale
scene reference survives; 6 kill cycles under ASan/UBSan stayed clean.
No compositor change needed.
This commit is contained in:
Andrew Hamilton 2026-09-04 08:05:23 +10:00
parent 6c5df47bf0
commit 35ce107a23
2 changed files with 225 additions and 0 deletions

View file

@ -3781,3 +3781,56 @@ def test_standalone_headless_or_menu_owner_closed(tmp_path):
except Exception:
pass
s.close()
def test_standalone_headless_popup_chain_kill(tmp_path):
"""A nested popup chain (toplevel + P1 + sub-popup P2) maps fully, the
client can be kill -9'd mid-chain, and a fresh chain maps afterwards:
popup teardown leaves no dangling scene references behind."""
s = _spawn_standalone(
{}, "%s --app-id keep '#0000ff' 320x240" % WL)
a = b = None
try:
s.wait_any_active()
def pop_client(hold):
return subprocess.Popen(
[WL, "--popup", "--popup-hold", str(hold)],
env=dict(os.environ, WAYLAND_DISPLAY=s.socket),
stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL, start_new_session=True,
preexec_fn=_preexec_death(os.getpid()))
# Client A: wait until the nested sub-popup maps, then kill -9.
a = pop_client(30)
assert _wait_log(s, r"geo=200x100", timeout=15.0), \
f"P1 never mapped:\n{s.read_log()[-500:]}"
assert _wait_log(s, r"geo=150x80", timeout=15.0), \
f"nested P2 never mapped:\n{s.read_log()[-500:]}"
a.kill()
a.wait(timeout=10.0)
a = None
time.sleep(1.0)
assert s.proc.poll() is None, \
f"compositor died on popup client kill; log tail:\n{s.read_log()[-400:]}"
# Client B: a fresh chain maps again on the same compositor.
b = pop_client(4)
assert _wait_log(s, r"geo=150x80", timeout=15.0), \
f"fresh nested popup never mapped:\n{s.read_log()[-500:]}"
b.wait(timeout=15.0)
b = None
time.sleep(0.5)
assert s.proc.poll() is None, \
f"compositor died after popup chains; log tail:\n{s.read_log()[-400:]}"
log = s.read_log()
for pat in ("Segmentation fault", "Aborted", "BACKTRACE",
"Assertion `", "AddressSanitizer"):
assert pat not in log, f"crash indicator {pat!r} in log"
finally:
for p in (a, b):
try:
if p is not None:
p.kill()
except Exception:
pass
s.close()

View file

@ -8,6 +8,7 @@
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <time.h>
#include <unistd.h>
#include <sys/shm.h>
@ -1719,6 +1720,162 @@ static void activate_run(void) {
}
}
/* --- Nested xdg popups ---
--popup [--popup-hold <s>]: open a toplevel with a popup P1 on it and a
nested sub-popup P2 on P1 (menu->submenu shape), each with a real buffer
so all three map. Holds <s> seconds (default: until killed), then exits.
The driver kills this client mid-chain and opens a fresh popup to prove
the compositor tore the whole popup tree down without dangling scene
references. */
static bool popup_mode;
static int popup_hold = -1;
static struct wl_surface *pop_surf[3];
static struct xdg_surface *pop_xdg[3];
static struct xdg_toplevel *pop_top;
static struct xdg_popup *pop_popup[2];
static struct wl_buffer *pop_buf[3];
static void pop_make_buf(int i, int w, int h, uint8_t shade) {
if (pop_buf[i]) return;
int stride = w * 4;
size_t size = (size_t)stride * (size_t)h;
int fd;
struct wl_shm_pool *pool = make_pool(size, &fd);
if (!pool) return;
uint8_t *d = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (d != MAP_FAILED) {
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++) {
d[(y * stride) + x * 4 + 0] = shade;
d[(y * stride) + x * 4 + 1] = shade;
d[(y * stride) + x * 4 + 2] = shade;
d[(y * stride) + x * 4 + 3] = 0xff;
}
munmap(d, size);
}
close(fd);
pop_buf[i] = wl_shm_pool_create_buffer(pool, 0, w, h, stride,
WL_SHM_FORMAT_XRGB8888);
wl_shm_pool_destroy(pool);
}
static void pop_xdg_configure(void *data, struct xdg_surface *xs,
uint32_t serial) {
int i = (int)(intptr_t)data;
static const int ws[3] = { 64, 200, 150 };
static const int hs[3] = { 64, 100, 80 };
xdg_surface_ack_configure(xs, serial);
pop_make_buf(i, ws[i], hs[i], (uint8_t)(0x40 + i * 0x30));
wl_surface_attach(pop_surf[i], pop_buf[i], 0, 0);
wl_surface_commit(pop_surf[i]);
}
static const struct xdg_surface_listener pop_xdg_listener = {
.configure = pop_xdg_configure,
};
static void pop_top_configure(void *data, struct xdg_toplevel *xp, int32_t w,
int32_t h, struct wl_array *states) {
(void)data; (void)xp; (void)w; (void)h; (void)states;
}
static const struct xdg_toplevel_listener pop_top_listener = {
.configure = pop_top_configure,
.close = xdg_toplevel_close,
};
static void pop_popup_configure(void *data, struct xdg_popup *p,
int32_t x, int32_t y, int32_t w, int32_t h) {
(void)data; (void)p; (void)x; (void)y; (void)w; (void)h;
}
static void pop_popup_done(void *data, struct xdg_popup *p) {
(void)data; (void)p;
}
static void pop_popup_repositioned(void *data, struct xdg_popup *p,
uint32_t token) {
(void)data; (void)p; (void)token;
}
static const struct xdg_popup_listener pop_popup_listener = {
.configure = pop_popup_configure,
.popup_done = pop_popup_done,
.repositioned = pop_popup_repositioned,
};
static void pop_open_popup(int i, struct xdg_surface *parent, int w, int h) {
pop_surf[i] = wl_compositor_create_surface(compositor);
pop_xdg[i] = xdg_wm_base_get_xdg_surface(wm_base, pop_surf[i]);
struct xdg_positioner *pos =
xdg_wm_base_create_positioner(wm_base);
xdg_positioner_set_size(pos, w, h);
xdg_positioner_set_anchor_rect(pos, 0, 0, 10, 10);
xdg_positioner_set_anchor(pos, XDG_POSITIONER_ANCHOR_BOTTOM_LEFT);
xdg_positioner_set_gravity(pos, XDG_POSITIONER_GRAVITY_BOTTOM_RIGHT);
xdg_positioner_set_constraint_adjustment(
pos, XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_X |
XDG_POSITIONER_CONSTRAINT_ADJUSTMENT_SLIDE_Y);
pop_popup[i - 1] = xdg_surface_get_popup(pop_xdg[i], parent, pos);
xdg_positioner_destroy(pos);
xdg_surface_add_listener(pop_xdg[i], &pop_xdg_listener,
(void *)(intptr_t)i);
xdg_popup_add_listener(pop_popup[i - 1], &pop_popup_listener,
(void *)(intptr_t)i);
wl_surface_commit(pop_surf[i]);
}
static void popup_run(void) {
display = wl_display_connect(NULL);
if (!display) {
fprintf(stderr, "wltest: popup: cannot connect\n");
exit(1);
}
struct wl_registry *reg = wl_display_get_registry(display);
wl_registry_add_listener(reg, &registry_listener, NULL);
wl_display_roundtrip(display);
xdg_wm_base_add_listener(wm_base, &wm_base_listener, NULL);
/* Toplevel first so the popups have a mapped parent chain. */
pop_surf[0] = wl_compositor_create_surface(compositor);
pop_xdg[0] = xdg_wm_base_get_xdg_surface(wm_base, pop_surf[0]);
pop_top = xdg_surface_get_toplevel(pop_xdg[0]);
xdg_toplevel_set_title(pop_top, "wltest-popup-parent");
xdg_surface_add_listener(pop_xdg[0], &pop_xdg_listener,
(void *)(intptr_t)0);
xdg_toplevel_add_listener(pop_top, &pop_top_listener, NULL);
wl_surface_commit(pop_surf[0]);
wl_display_roundtrip(display);
pop_open_popup(1, pop_xdg[0], 200, 100);
wl_display_roundtrip(display);
/* A nested popup is only configured once its parent is mapped: let P1
settle server-side before creating P2 on it. */
sleep(1);
wl_display_roundtrip(display);
pop_open_popup(2, pop_xdg[1], 150, 80);
/* The final popup batch has no roundtrip after it; flush so the server
actually receives get_popup before the hold loop (which only polls). */
wl_display_flush(display);
if (popup_hold < 0) {
for (;;) {
if (wl_display_dispatch(display) < 0)
break;
}
} else {
/* Hold the chain open, servicing the connection: a plain
dispatch_pending+poll never READS new events (pending only drains
the already-read queue), so late configures would stall forever.
Periodic roundtrips read, dispatch (buffer attach on configure)
and flush. */
time_t t0 = time(NULL);
while (time(NULL) - t0 < popup_hold) {
if (wl_display_roundtrip(display) < 0)
break;
struct pollfd pfd = { .fd = -1 };
poll(&pfd, 0, 200);
}
}
}
/* Pull the current offer and act on it. In --primary-demand mode write the
payload to the file and exit; in --primary-demand-expect mode keep waiting
until the payload matches the expected text (the host may already hold its
@ -2068,10 +2225,25 @@ int main(int argc, char **argv) {
}
activate_out = argv[i + 1];
i += 2;
} else if (strcmp(argv[i], "--popup") == 0) {
popup_mode = true;
i += 1;
} else if (strcmp(argv[i], "--popup-hold") == 0) {
if (i + 1 >= argc) {
fprintf(stderr, "wltest: --popup-hold needs seconds\n");
return 1;
}
popup_hold = atoi(argv[i + 1]);
i += 2;
} else {
break;
}
}
/* Nested popups: toplevel + popup + sub-popup chain, then exit/hold. */
if (popup_mode) {
popup_run();
return 0;
}
/* Xdg-activation: focus an inactive window by token, then exit. */
if (activate_mode) {
activate_run();