Commit14dc409eRecorded5 Dec 2026Repositorysigil-ui
Test Wayland activation with an owned protocol peer
Changed
scripts/check-configure-peer.sgl | 4 ++--
test/wayland/configure-peer.c | 55 +++++++++++++++++++++++++++++++++++++++++++++----------
test/wayland/services-native.c | 43 +++++++++++++++++++++++++++++++++++++++++--
3 files changed, 88 insertions(+), 14 deletions(-)Diff
scripts/check-configure-peer.sglmodified
@@ -19,7 +19,7 @@
19
(define fixed (path evidence "configure-peer")) 20
($ ,compiler ,@flags ,fixture ,archive -lm -ldl -lpthread -o ,fixed 21
out: ,(path evidence "configure-compile.log") err: 'merge timeout: 40)−22
(each case '("early-close" "close-configure" "traffic" "expired-pending")+22
(each case '("early-close" "close-configure" "traffic" "expired-pending" "activation") 23
(define log (path evidence (str "configure-" case ".log"))) 24
($ ,fixed ,case out: ,log err: 'merge timeout: 15) 25
(display (read-text log))@@ -39,4 +39,4 @@
39
(display (read-text log)) 40
(unless (and (not (ok? result)) (contains? (read-text log) (str "FAIL: configure peer " case))) 41
(error "Prior source did not reproduce the configure regression" case))))−42
(println "PASS: configure close, traffic wall deadline and deterministic expired-pending controls")+42
(println "PASS: configure close, traffic wall deadline, expired-pending and activation controls")test/wayland/configure-peer.cmodified
@@ -1,4 +1,4 @@
−1
/* A minimal owned protocol peer for initial-configuration failure paths.+1
/* A minimal owned protocol peer for configuration and activation. 2
* It advertises only the globals needed to open a toplevel. No desktop 3
* compositor, input device, render node or extra display connection is used. */ 4
#include <assert.h>@@ -12,6 +12,7 @@ static int count_dispatch_polls, dispatch_polls;
12
static int observed_poll(struct pollfd *fds, nfds_t count, int timeout); 13
#define poll observed_poll 14
#include "../../native/wayland.c"+15
#include "xdg-activation-v1-client-protocol.h" 16
#include "standalone-protocols.h" 17
#undef poll 18
@@ -23,8 +24,9 @@ static int observed_poll(struct pollfd *fds, nfds_t count, int timeout)
24
} 25
26
enum Object { DISPLAY = 1, REGISTRY, COMPOSITOR, SHM, WM_BASE, SURFACE,−26
XDG_SURFACE, TOPLEVEL };−27
typedef struct { unsigned pings, pongs; int committed; } PeerStats;+27
XDG_SURFACE, TOPLEVEL, ACTIVATION };+28
typedef struct { unsigned pings, pongs, activations; int committed; } PeerStats;+29
static const char activation_token[] = "owned-activation-token"; 30
31
static int send_words(int fd, const uint32_t *words, size_t count) 32
{@@ -59,7 +61,8 @@ static int serve(int fd, const char *mode, PeerStats *stats)
61
{ 62
enum Object objects[4096] = {0}; 63
objects[1] = DISPLAY;−62
uint32_t wm_base = 0, xdg_surface = 0, toplevel = 0;+64
uint32_t wm_base = 0, surface = 0, xdg_surface = 0, toplevel = 0;+65
int activation = !strcmp(mode, "activation"); 66
uint32_t input[16384], output[12288]; 67
size_t used = 0, sent = 0, output_size = 0; 68
uint32_t serial = 1;@@ -92,6 +95,7 @@ static int serve(int fd, const char *mode, PeerStats *stats)
95
if (!peer_global(fd, registry, 1, "wl_compositor", 4) || 96
!peer_global(fd, registry, 2, "wl_shm", 1) || 97
!peer_global(fd, registry, 3, "xdg_wm_base", 1)) return 2;+98
if (activation && !peer_global(fd, registry, 4, "xdg_activation_v1", 1)) return 2; 99
} else if (opcode == 0) { 100
if (!strcmp(mode, "expired-pending") && wm_base && serial >= 3 && !stats->pings) { 101
uint32_t ping[] = {wm_base, 12u << 16, serial++};@@ -106,7 +110,13 @@ static int serve(int fd, const char *mode, PeerStats *stats)
110
case REGISTRY: { 111
uint32_t object = message[count - 1]; 112
if (object >= 4096 || count < 5) return 3;−109
objects[object] = message[2] == 1 ? COMPOSITOR : message[2] == 2 ? SHM : WM_BASE;+113
switch (message[2]) {+114
case 1: objects[object] = COMPOSITOR; break;+115
case 2: objects[object] = SHM; break;+116
case 3: objects[object] = WM_BASE; break;+117
case 4: objects[object] = ACTIVATION; break;+118
default: return 3;+119
} 120
if (objects[object] == WM_BASE) wm_base = object; 121
if (objects[object] == SHM) { 122
uint32_t formats[] = {object, 12u << 16, WL_SHM_FORMAT_ARGB8888,@@ -116,7 +126,10 @@ static int serve(int fd, const char *mode, PeerStats *stats)
126
break; 127
} 128
case COMPOSITOR:−119
if (opcode == 0 && message[2] < 4096) objects[message[2]] = SURFACE;+129
if (opcode == 0 && message[2] < 4096) {+130
surface = message[2];+131
objects[surface] = SURFACE;+132
} 133
break; 134
case WM_BASE: 135
if (opcode == 2 && message[2] < 4096) {@@ -136,6 +149,10 @@ static int serve(int fd, const char *mode, PeerStats *stats)
149
if (!strcmp(mode, "traffic")) { 150
flooding_until = now_ms() + 6700; 151
if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) < 0) return 2;+152
} else if (activation) {+153
uint32_t configure[] = {toplevel, 20u << 16, 640, 480, 0,+154
xdg_surface, 12u << 16, serial++};+155
if (!send_words(fd, configure, 8)) return 2; 156
} else { 157
if (!strcmp(mode, "close-configure")) { 158
uint32_t configure[] = {toplevel, (8u << 16) | 1,@@ -149,6 +166,15 @@ static int serve(int fd, const char *mode, PeerStats *stats)
166
} 167
} 168
break;+169
case ACTIVATION:+170
if (opcode == XDG_ACTIVATION_V1_ACTIVATE) {+171
size_t token_words = (sizeof activation_token + 3) / 4;+172
if (count != token_words + 4 || message[2] != sizeof activation_token ||+173
memcmp(&message[3], activation_token, sizeof activation_token) ||+174
message[count - 1] != surface) return 3;+175
stats->activations++;+176
}+177
break; 178
default: break; 179
} 180
offset += size;@@ -232,8 +258,9 @@ int main(int argc, char **argv)
258
struct rlimit no_core = {0, 0}; 259
assert(setrlimit(RLIMIT_CORE, &no_core) == 0); 260
const char *mode = argc > 1 ? argv[1] : "early-close";+261
int activation = !strcmp(mode, "activation"); 262
if (strcmp(mode, "early-close") && strcmp(mode, "close-configure") && strcmp(mode, "traffic") &&−236
strcmp(mode, "expired-pending")) return 2;+263
strcmp(mode, "expired-pending") && !activation) return 2; 264
signal(SIGPIPE, SIG_IGN); 265
PeerStats *stats = mmap(NULL, sizeof *stats, PROT_READ | PROT_WRITE, 266
MAP_SHARED | MAP_ANONYMOUS, -1, 0);@@ -265,17 +292,25 @@ int main(int argc, char **argv)
292
int64_t start = now_ms(); 293
sigil_eval_string(vm, "(import (sigil ui wayland)) " 294
"(define configure-peer (%wl-open #f 0 \"Configure peer\" \"ui-configure-test\" #f 640 480 #f))");+295
if (activation && !sigil_error_message(vm)) {+296
char expression[160];+297
snprintf(expression, sizeof expression,+298
"(if (%%wl-activate! configure-peer \"%s\") #t (error \"Activation refused\"))", activation_token);+299
sigil_eval_string(vm, expression);+300
} 301
int64_t elapsed = now_ms() - start; 302
char problem[1024];−270
snprintf(problem, sizeof problem, "%s", sigil_error_message(vm) ? sigil_error_message(vm) : "opened unexpectedly");+303
snprintf(problem, sizeof problem, "%s",+304
sigil_error_message(vm) ? sigil_error_message(vm) : activation ? "" : "opened unexpectedly"); 305
sigil_vm_destroy(vm); 306
unsetenv("WAYLAND_SOCKET"); 307
close(sockets[0]); 308
int status; 309
assert(waitpid(peer, &status, 0) == peer); 310
int traffic = !strcmp(mode, "traffic");−277
int passed = WIFEXITED(status) && WEXITSTATUS(status) == 0 && stats->committed &&−278
strstr(problem, traffic ? "within five seconds" : "closed the surface before showing it") &&+311
int passed = WIFEXITED(status) && WEXITSTATUS(status) == 0 && stats->committed;+312
if (activation) passed = passed && !*problem && stats->activations == 1;+313
else passed = passed && strstr(problem, traffic ? "within five seconds" : "closed the surface before showing it") && 314
(traffic ? elapsed >= 4900 && elapsed < 6200 && stats->pongs > 1000 : elapsed < 1500); 315
printf("%s: configure peer %s elapsed_ms=%lld pings=%u pongs=%u peer_status=%d: %s\n", 316
passed ? "PASS" : "FAIL", mode, (long long)elapsed, stats->pings, stats->pongs, status, problem);test/wayland/services-native.cmodified
@@ -15,9 +15,47 @@ static void check_refused(ServiceFixture *fixture)
15
assert(native_window_fullscreen_state(fixture->vm,1,&handle)==SIGIL_FALSE); 16
assert(native_window_fullscreen(fixture->vm,2,fullscreen)==SIGIL_FALSE); 17
assert(sigil_as_fixnum(native_window_fullscreen_status(fixture->vm,1,&handle))==SUI_FULLSCREEN_REFUSED);+18
assert(native_wl_activate(fixture->vm,2,text)==SIGIL_FALSE); 19
assert(!sigil_error_message(fixture->vm)); 20
} 21
+22
static void check_activation_guards(SigilVM *vm)+23
{+24
Wl window = {.service_thread = pthread_self()};+25
window.display = (struct wl_display *)(uintptr_t)1;+26
window.surface = (struct wl_surface *)(uintptr_t)1;+27
window.toplevel = (struct xdg_toplevel *)(uintptr_t)1;+28
struct xdg_activation_v1 *global = (struct xdg_activation_v1 *)(uintptr_t)1;+29
window.activation = global;+30
+31
char oversized[1025];+32
memset(oversized, 'a', sizeof oversized);+33
Value text = sigil_make_string(vm, "token", 5);+34
Value handle = sigil_make_foreign(vm, wl_tag, &window, NULL, sizeof window);+35
Value rejected[] = {SIGIL_FALSE, sigil_make_string(vm, "", 0),+36
sigil_make_string(vm, oversized, sizeof oversized),+37
sigil_make_string(vm, "a\0b", 3)};+38
for (size_t i = 0; i < sizeof rejected / sizeof rejected[0]; i++) {+39
Value args[] = {handle, rejected[i]};+40
assert(native_wl_activate(vm, 2, args) == SIGIL_FALSE);+41
assert(!sigil_error_message(vm));+42
}+43
+44
Value args[] = {handle, text};+45
window.activation = NULL;+46
assert(native_wl_activate(vm, 2, args) == SIGIL_FALSE);+47
window.activation = global;+48
Wl parent = {0};+49
window.parent = &parent;+50
assert(native_wl_activate(vm, 2, args) == SIGIL_FALSE);+51
window.parent = NULL;+52
window.closed = 1;+53
window.display = NULL;+54
assert(native_wl_activate(vm, 2, args) == SIGIL_FALSE);+55
assert(!sigil_error_message(vm));+56
puts("PASS: activation refuses invalid tokens, missing globals, subsurfaces and closed windows");+57
}+58
59
static void *wrong_thread(void *data) 60
{ 61
check_refused(data);@@ -32,7 +70,7 @@ int main(void)
70
ServiceFixture fixture={vm,sigil_make_foreign(vm,wl_tag,&window,NULL,sizeof window), 71
sigil_make_string(vm,"Owned text",10)}; 72
check_refused(&fixture);−35
puts("PASS: all six closed Wayland bindings return refusal without a bridge error");+73
puts("PASS: closed Wayland bindings return refusal without a bridge error"); 74
window.closed=0; 75
window.fullscreen_request=(SuiFullscreenRequest){0}; 76
/* Invalid protocol sentinels prove the thread guard runs before access. */@@ -42,11 +80,12 @@ int main(void)
80
assert(!pthread_create(&thread,NULL,wrong_thread,&fixture)); 81
assert(!pthread_join(thread,NULL)); 82
assert(window.fullscreen_request.status==SUI_FULLSCREEN_IDLE);−45
puts("PASS: all six Wayland bindings reject the non-owning thread before protocol access");+83
puts("PASS: Wayland bindings reject the non-owning thread before protocol access"); 84
Wl child={.service_thread=pthread_self(),.closed=1,.parent=&window}; 85
fixture.handle=sigil_make_foreign(vm,wl_tag,&child,NULL,sizeof child); 86
check_refused(&fixture); 87
puts("PASS: closed child services do not fall through to the live parent's clipboard");+88
check_activation_guards(vm); 89
window.display=NULL; window.toplevel=NULL; 90
sigil_vm_destroy(vm); 91
return 0;