AtlatestRepositorysigil-ui
1
/* A minimal owned protocol peer for configuration and activation.2
* It advertises only the globals needed to open a toplevel. No desktop3
* compositor, input device, render node or extra display connection is used. */4
#include <assert.h>5
#include <signal.h>6
#include <sys/resource.h>7
#include <sys/socket.h>8
#include <sys/mman.h>9
#include <sys/wait.h>10
#include <poll.h>11
static int count_dispatch_polls, dispatch_polls;12
static int observed_poll(struct pollfd *fds, nfds_t count, int timeout);13
#define poll observed_poll14
#include "../../native/wayland.c"15
#include "standalone-protocols.h"16
#undef poll18
void sigil__init_sigil_ui_native(SigilVM *vm);19
void sigil__init_sigil_ffi_module(SigilVM *vm);20
void sigil__init_sigil_font_native(SigilVM *vm);22
/* Observe real polling without changing its result, timeout or clock. */23
static int observed_poll(struct pollfd *fds, nfds_t count, int timeout)24
{25
if (count_dispatch_polls) dispatch_polls++;26
return poll(fds, count, timeout);27
}29
enum Object { DISPLAY = 1, REGISTRY, COMPOSITOR, SHM, WM_BASE, SURFACE,30
XDG_SURFACE, TOPLEVEL, ACTIVATION };31
typedef struct { unsigned pings, pongs, activations, activation_destroys; int committed; } PeerStats;32
static const char activation_token[] = "owned-activation-token";34
static int send_words(int fd, const uint32_t *words, size_t count)35
{36
const char *bytes = (const char *)words;37
size_t size = count * sizeof *words;38
while (size) {39
ssize_t n = write(fd, bytes, size);40
if (n < 0 && errno == EINTR) continue;41
if (n <= 0) return 0;42
bytes += n;43
size -= (size_t)n;44
}45
return 1;46
}48
static int peer_global(int fd, uint32_t registry, uint32_t name,49
const char *interface, uint32_t version)50
{51
uint32_t words[32] = {registry};52
size_t length = strlen(interface) + 1;53
size_t padded = (length + 3) / 4;54
size_t count = 5 + padded;55
words[1] = (uint32_t)(count * 4) << 16;56
words[2] = name;57
words[3] = (uint32_t)length;58
memcpy(&words[4], interface, length);59
words[4 + padded] = version;60
return send_words(fd, words, count);61
}63
static int serve(int fd, const char *mode, PeerStats *stats)64
{65
enum Object objects[4096] = {0};66
objects[1] = DISPLAY;67
uint32_t registry = 0, wm_base = 0, surface = 0, xdg_surface = 0, toplevel = 0;68
int remove_activation = !strcmp(mode, "activation-removed");69
int activation = !strcmp(mode, "activation") || remove_activation;70
uint32_t input[16384], output[12288];71
size_t used = 0, sent = 0, output_size = 0;72
uint32_t serial = 1;73
int64_t started = now_ms(), flooding_until = 0;74
while (now_ms() - started < 12000) {75
int flooding = flooding_until && now_ms() < flooding_until;76
struct pollfd p = {fd, POLLIN | ((flooding || sent < output_size) ? POLLOUT : 0), 0};77
int ready = poll(&p, 1, 100);78
if (ready < 0 && errno == EINTR) continue;79
if (ready < 0) return 2;80
if (p.revents & POLLIN) {81
ssize_t n = read(fd, (char *)input + used, sizeof input - used);82
if (!n || (n < 0 && errno == ECONNRESET)) return 0;83
if (n < 0 && errno != EINTR && errno != EAGAIN) return 2;84
if (n > 0) used += (size_t)n;85
size_t offset = 0;86
while (used - offset >= 8) {87
uint32_t *message = (uint32_t *)((char *)input + offset);88
uint32_t id = message[0], opcode = message[1] & 0xffff;89
size_t size = message[1] >> 16;90
if (size < 8 || size % 4 || size > sizeof input || id >= 4096) return 3;91
if (used - offset < size) break;92
size_t count = size / 4;93
switch (objects[id]) {94
case DISPLAY:95
if (opcode == 1) {96
registry = message[2];97
if (registry >= 4096) return 3;98
objects[registry] = REGISTRY;99
if (!peer_global(fd, registry, 1, "wl_compositor", 4) ||100
!peer_global(fd, registry, 2, "wl_shm", 1) ||101
!peer_global(fd, registry, 3, "xdg_wm_base", 1)) return 2;102
if (activation && !peer_global(fd, registry, 4, "xdg_activation_v1", 1)) return 2;103
} else if (opcode == 0) {104
if (!strcmp(mode, "expired-pending") && wm_base && serial >= 3 && !stats->pings) {105
uint32_t ping[] = {wm_base, 12u << 16, serial++};106
if (!send_words(fd, ping, 3)) return 2;107
stats->pings++;108
}109
uint32_t done[] = {message[2], (12u << 16), serial++};110
uint32_t deleted[] = {1, (12u << 16) | 1, message[2]};111
if (!send_words(fd, done, 3) || !send_words(fd, deleted, 3)) return 2;112
}113
break;114
case REGISTRY: {115
uint32_t object = message[count - 1];116
if (object >= 4096 || count < 5) return 3;117
switch (message[2]) {118
case 1: objects[object] = COMPOSITOR; break;119
case 2: objects[object] = SHM; break;120
case 3: objects[object] = WM_BASE; break;121
case 4: objects[object] = ACTIVATION; break;122
default: return 3;123
}124
if (objects[object] == WM_BASE) wm_base = object;125
if (objects[object] == SHM) {126
uint32_t formats[] = {object, 12u << 16, WL_SHM_FORMAT_ARGB8888,127
object, 12u << 16, WL_SHM_FORMAT_XRGB8888};128
if (!send_words(fd, formats, 6)) return 2;129
}130
break;131
}132
case COMPOSITOR:133
if (opcode == 0 && message[2] < 4096) {134
surface = message[2];135
objects[surface] = SURFACE;136
}137
break;138
case WM_BASE:139
if (opcode == 2 && message[2] < 4096) {140
xdg_surface = message[2];141
objects[xdg_surface] = XDG_SURFACE;142
} else if (opcode == 3) stats->pongs++;143
break;144
case XDG_SURFACE:145
if (opcode == 1 && message[2] < 4096) {146
toplevel = message[2];147
objects[toplevel] = TOPLEVEL;148
}149
break;150
case SURFACE:151
if (opcode == 6 && !stats->committed) {152
stats->committed = 1;153
if (!strcmp(mode, "traffic")) {154
flooding_until = now_ms() + 6700;155
if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) < 0) return 2;156
} else if (activation) {157
uint32_t configure[] = {toplevel, 20u << 16, 640, 480, 0,158
xdg_surface, 12u << 16, serial++};159
if (!send_words(fd, configure, 8)) return 2;160
} else {161
if (!strcmp(mode, "close-configure")) {162
uint32_t configure[] = {toplevel, (8u << 16) | 1,163
toplevel, 20u << 16, 640, 480, 0,164
xdg_surface, 12u << 16, serial++};165
if (!send_words(fd, configure, 10)) return 2;166
} else {167
uint32_t close[] = {toplevel, (8u << 16) | 1};168
if (!send_words(fd, close, 2)) return 2;169
}170
}171
}172
break;173
case ACTIVATION:174
if (opcode == XDG_ACTIVATION_V1_ACTIVATE) {175
size_t token_words = (sizeof activation_token + 3) / 4;176
if (count != token_words + 4 || message[2] != sizeof activation_token ||177
memcmp(&message[3], activation_token, sizeof activation_token) ||178
message[count - 1] != surface) return 3;179
stats->activations++;180
if (remove_activation && stats->activations == 1) {181
uint32_t removed[] = {registry, (12u << 16) | 1, 4}; /* global_remove */182
if (!send_words(fd, removed, 3)) return 2;183
}184
} else if (opcode == XDG_ACTIVATION_V1_DESTROY) {185
stats->activation_destroys++;186
}187
break;188
default: break;189
}190
offset += size;191
}192
if (offset) { memmove(input, (char *)input + offset, used - offset); used -= offset; }193
}194
if (p.revents & POLLOUT) {195
if (sent == output_size && flooding) {196
/* Keep enough valid ping traffic queued that a readiness-197
* dependent timeout cannot accidentally pass on an idle gap. */198
for (size_t i = 0; i < sizeof output / sizeof *output; i += 3) {199
output[i] = wm_base;200
output[i + 1] = 12u << 16;201
output[i + 2] = serial++;202
stats->pings++;203
}204
sent = 0;205
output_size = sizeof output;206
}207
if (sent < output_size) {208
ssize_t n = write(fd, (char *)output + sent, output_size - sent);209
if (n > 0) sent += (size_t)n;210
else if (n < 0 && errno != EAGAIN && errno != EINTR && errno != EPIPE) return 2;211
}212
}213
if (p.revents & (POLLHUP | POLLERR)) return 0;214
}215
return 4;216
}218
static void expired_sync_done(void *data, struct wl_callback *callback, uint32_t serial) {}219
static const struct wl_callback_listener expired_sync_listener = {.done = expired_sync_done};221
static int check_expired_pending(pid_t peer, PeerStats *stats)222
{223
char error[256];224
assert(load_libraries(error, sizeof error));225
Wl *window = calloc(1, sizeof *window);226
assert(window);227
window->references = window->scale = 1;228
for (unsigned i = 0; i < CLIPBOARD_WRITES; i++) window->writes[i].fd = -1;229
window->display = wl_display_connect(NULL);230
assert(window->display);231
window->registry = wl_display_get_registry(window->display);232
assert(wl_registry_add_listener(window->registry, ®istry_listener, window) == 0);233
assert(wl_display_roundtrip(window->display) >= 0 && window->wm_base);234
assert(wl_display_roundtrip(window->display) >= 0);235
while (wl_display_prepare_read(window->display) != 0)236
assert(wl_display_dispatch_pending(window->display) >= 0);237
struct wl_callback *sync = wl_display_sync(window->display);238
assert(sync && wl_callback_add_listener(sync, &expired_sync_listener, NULL) == 0);239
assert(wl_display_flush(window->display) >= 0);240
struct pollfd ready = {wl_display_get_fd(window->display), POLLIN, 0};241
do {242
assert(poll(&ready, 1, 1000) > 0 && wl_display_read_events(window->display) >= 0);243
/* delete_id is handled internally; continue until a real event is244
* queued, without dispatching the ping under test. */245
} while (wl_display_prepare_read(window->display) == 0);246
/* A real ping is queued before dispatch begins with zero budget.247
* The unconfigured path must finish its pending batch and return without248
* polling again. This is independent of socket readiness or CPU speed. */249
count_dispatch_polls = 1;250
int result = dispatch(window, 0);251
count_dispatch_polls = 0;252
assert(wl_display_roundtrip(window->display) >= 0);253
wl_callback_destroy(sync);254
sui_wayland_close(window);255
sui_wayland_unref(window);256
unsetenv("WAYLAND_SOCKET");257
int status;258
assert(waitpid(peer, &status, 0) == peer);259
int passed = result == 0 && dispatch_polls == 0 && stats->pings == 1 &&260
WIFEXITED(status) && WEXITSTATUS(status) == 0;261
printf("%s: configure peer expired-pending polls=%d pings=%u pongs=%u peer_status=%d\n",262
passed ? "PASS" : "FAIL", dispatch_polls, stats->pings, stats->pongs, status);263
return passed ? 0 : 1;264
}266
static void check_activation(SigilVM *vm, int remove_activation, const PeerStats *stats)267
{268
sigil_eval_string(vm, "(import (sigil ui wayland)) "269
"(define configure-peer "270
" (open-wayland-surface title: \"Configure peer\" app-id: \"ui-configure-test\")) "271
"(when (or (wayland-activate! configure-peer) "272
" (wayland-activate! configure-peer #f)) "273
" (error \"Accepted a missing activation token\"))");274
if (sigil_error_message(vm)) return;276
char expression[192];277
snprintf(expression, sizeof expression,278
"(unless (wayland-activate! configure-peer \"%s\") (error \"Activation refused\"))",279
activation_token);280
sigil_eval_string(vm, expression);281
if (sigil_error_message(vm)) return;283
if (remove_activation) {284
Value handle = sigil_eval_string(vm, "(wayland-native-handle configure-peer)");285
assert(!sigil_error_message(vm) && sigil_is_foreign(handle));286
Wl *window = sigil_foreign_data(handle);287
assert(window && window->display);288
/* The peer sends global_remove before answering this round trip. */289
assert(wl_display_roundtrip(window->display) >= 0);290
assert(window->display && !window->closed && !window->dead);291
snprintf(expression, sizeof expression,292
"(when (wayland-activate! configure-peer \"%s\") (error \"Accepted a removed activation global\"))",293
activation_token);294
sigil_eval_string(vm, expression);295
if (sigil_error_message(vm)) return;296
/* Deliver the destroy request before closing the connection. */297
assert(wl_display_roundtrip(window->display) >= 0);298
assert(stats->activation_destroys == 1);299
}300
sigil_eval_string(vm, "(wayland-surface-close! configure-peer)");301
}303
int main(int argc, char **argv)304
{305
struct rlimit no_core = {0, 0};306
assert(setrlimit(RLIMIT_CORE, &no_core) == 0);307
const char *mode = argc > 1 ? argv[1] : "early-close";308
int remove_activation = !strcmp(mode, "activation-removed");309
int activation = !strcmp(mode, "activation") || remove_activation;310
if (strcmp(mode, "early-close") && strcmp(mode, "close-configure") && strcmp(mode, "traffic") &&311
strcmp(mode, "expired-pending") && !activation) return 2;312
signal(SIGPIPE, SIG_IGN);313
PeerStats *stats = mmap(NULL, sizeof *stats, PROT_READ | PROT_WRITE,314
MAP_SHARED | MAP_ANONYMOUS, -1, 0);315
assert(stats != MAP_FAILED);316
int sockets[2];317
assert(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == 0);318
pid_t peer = fork();319
assert(peer >= 0);320
if (!peer) {321
close(sockets[0]);322
int result = serve(sockets[1], mode, stats);323
/* The expired probe closes immediately after its synchronization324
* reply; the peer may still be posting that callback's delete_id. */325
if (!strcmp(mode, "expired-pending") && result == 2 && (errno == EPIPE || errno == ECONNRESET)) result = 0;326
_exit(result);327
}328
close(sockets[1]);329
char socket_name[32];330
snprintf(socket_name, sizeof socket_name, "%d", sockets[0]);331
assert(setenv("WAYLAND_SOCKET", socket_name, 1) == 0);332
if (!strcmp(mode, "expired-pending")) {333
int result = check_expired_pending(peer, stats);334
munmap(stats, sizeof *stats);335
return result;336
}337
SigilVM *vm = sigil_vm_create();338
assert(vm);339
int64_t start = now_ms();340
if (activation) {341
assert(argc >= 4);342
sigil__init_sigil_ui_native(vm);343
sigil__init_sigil_ffi_module(vm);344
sigil__init_sigil_font_native(vm);345
for (int i = 3; i < argc; i++) sigil_vm_add_load_path(vm, argv[i]);346
sigil_eval_file(vm, argv[2]);347
if (!sigil_error_message(vm)) check_activation(vm, remove_activation, stats);348
} else {349
sigil__register_sigil_ui_wayland(vm);350
sigil_eval_string(vm, "(import (sigil ui wayland)) "351
"(define configure-peer (%wl-open #f 0 \"Configure peer\" \"ui-configure-test\" #f 640 480 #f))");352
}353
int64_t elapsed = now_ms() - start;354
char problem[1024];355
snprintf(problem, sizeof problem, "%s",356
sigil_error_message(vm) ? sigil_error_message(vm) : activation ? "" : "opened unexpectedly");357
sigil_vm_destroy(vm);358
unsetenv("WAYLAND_SOCKET");359
close(sockets[0]);360
int status;361
assert(waitpid(peer, &status, 0) == peer);362
int traffic = !strcmp(mode, "traffic");363
int passed = WIFEXITED(status) && WEXITSTATUS(status) == 0 && stats->committed;364
if (activation) passed = passed && !*problem && stats->activations == 1 &&365
(!remove_activation || stats->activation_destroys == 1);366
else passed = passed && strstr(problem, traffic ? "within five seconds" : "closed the surface before showing it") &&367
(traffic ? elapsed >= 4900 && elapsed < 6200 && stats->pongs > 1000 : elapsed < 1500);368
printf("%s: configure peer %s elapsed_ms=%lld pings=%u pongs=%u peer_status=%d: %s\n",369
passed ? "PASS" : "FAIL", mode, (long long)elapsed, stats->pings, stats->pongs, status, problem);370
munmap(stats, sizeof *stats);371
/* Configured windows still deliver a close event to their owner. */372
Wl normal = {.configured = 1};373
toplevel_close(&normal, NULL);374
assert(!normal.closed && normal.count == 1 && normal.queue[0].type == UI_CLOSE);375
return passed ? 0 : 1;376
}