AtlatestRepositorysigil-ui
1
/* Exercise the production wait body, extracted by the Sigil driver.2
* Mocked clock/Wayland I/O cover every prepared-read exit deterministically.3
* The final case also uses real poll interruptions in this process only. */4
#define _POSIX_C_SOURCE 200809L5
#include <errno.h>6
#include <poll.h>7
#include <signal.h>8
#include <stdint.h>9
#include <stdio.h>10
#include <string.h>11
#include <sys/time.h>12
#include <time.h>13
#include <unistd.h>15
typedef struct { void *display; int configured, closed; void *decoration; int decoration_mode; } Wl;16
enum Scenario { FAST, PENDING, READ, SILENT, TRAFFIC, INTERRUPT, BACKPRESSURE,17
BACKPRESSURE_TIMEOUT, FLUSH_ERROR, POLL_ERROR, HANGUP, INVALID_FD,18
ERROR_FLAG, READ_ERROR, READ_HANGUP, DISPATCH_ERROR, REAL_INTERRUPT, DECORATION, DECORATION_TIMEOUT };19
static enum Scenario scenario;20
static Wl *active;21
static int64_t clock_ms, real_start;22
static int prepared, prepares, reads, cancels, polls, queued, bad, failures;23
static int signal_pipe[2], interrupted_polls;24
static volatile sig_atomic_t signals_seen;26
static int64_t real_ms(void)27
{28
struct timespec now;29
clock_gettime(CLOCK_MONOTONIC, &now);30
return (int64_t)now.tv_sec * 1000 + now.tv_nsec / 1000000;31
}32
static int64_t now_ms(void) { return scenario == REAL_INTERRUPT ? real_ms() : clock_ms; }33
static void check(int condition, const char *name)34
{35
if (!condition) { printf("FAIL: %s\n", name); failures++; }36
}37
static int wl_display_dispatch_pending(void *display)38
{39
if (scenario == DISPATCH_ERROR) return -1;40
if (scenario == FAST || queued) { active->configured = 1; if (active->decoration) active->decoration_mode = 2; queued = 0; }41
return 0;42
}43
static int wl_display_prepare_read(void *display)44
{45
if (scenario == PENDING && !queued) { queued = 1; return -1; }46
if (prepared) bad++;47
prepared = 1; prepares++;48
return 0;49
}50
static void finish_read(int read)51
{52
if (!prepared) bad++;53
prepared = 0;54
if (read) reads++; else cancels++;55
}56
static int wl_display_read_events(void *display)57
{58
finish_read(1);59
if (scenario == READ_ERROR) return -1;60
if (scenario != TRAFFIC) queued = 1;61
return 0;62
}63
static void wl_display_cancel_read(void *display)64
{65
finish_read(0);66
/* Deliberately clobber errno: EINTR must have been saved before cancel. */67
errno = EIO;68
}69
static int wl_display_flush(void *display)70
{71
if (scenario == FLUSH_ERROR) { errno = EPIPE; return -1; }72
if ((scenario == BACKPRESSURE && prepares == 1) || scenario == BACKPRESSURE_TIMEOUT) {73
errno = EAGAIN; return -1;74
}75
return 0;76
}77
static int wl_display_get_fd(void *display)78
{ return scenario == REAL_INTERRUPT ? signal_pipe[0] : 7; }79
static int system_poll(struct pollfd *fds, nfds_t count, int timeout)80
{ return poll(fds, count, timeout); }81
static int test_poll(struct pollfd *fd, nfds_t count, int timeout)82
{83
polls++;84
if (scenario == REAL_INTERRUPT) {85
/* Fail safely even for a mutant that keeps restarting its deadline. */86
if (real_ms() - real_start >= 6500) { errno = EIO; return -1; }87
int result = system_poll(fd, count, timeout);88
if (result < 0 && errno == EINTR) interrupted_polls++;89
return result;90
}91
if (timeout < 0 || timeout > 5000) bad++;92
if (scenario == TRAFFIC || scenario == INTERRUPT || scenario == BACKPRESSURE_TIMEOUT) {93
clock_ms += 1000;94
if (scenario == INTERRUPT) {95
if (polls >= 8) { errno = EIO; return -1; }96
errno = EINTR; return -1;97
}98
if (polls >= 8) queued = 1;99
fd->revents = scenario == BACKPRESSURE_TIMEOUT ? POLLOUT : POLLIN;100
return 1;101
}102
if (scenario == SILENT || scenario == DECORATION_TIMEOUT) {103
clock_ms += timeout;104
if (polls >= 8) queued = 1;105
return 0;106
}107
if (scenario == POLL_ERROR) { errno = EIO; return -1; }108
if (scenario == HANGUP) { fd->revents = POLLHUP; return 1; }109
if (scenario == INVALID_FD) { fd->revents = POLLNVAL; return 1; }110
if (scenario == ERROR_FLAG) { fd->revents = POLLERR; return 1; }111
if (scenario == BACKPRESSURE && polls == 1) {112
if (!(fd->events & POLLOUT)) bad++;113
fd->revents = POLLOUT; clock_ms += 10; return 1;114
}115
clock_ms += 10;116
fd->revents = POLLIN | (scenario == READ_HANGUP ? POLLHUP : 0);117
return 1;118
}119
#define poll test_poll120
#include "configure-wait-under-test.h"121
#undef poll123
static void signal_handler(int signal) { signals_seen++; }124
static void run(enum Scenario which, const char *name, int expected)125
{126
scenario = which;127
Wl state = { 0 };128
if (which == DECORATION || which == DECORATION_TIMEOUT) {129
state.configured = 1; state.decoration = &state;130
}131
active = &state;132
clock_ms = 0; prepared = prepares = reads = cancels = polls = queued = bad = 0;133
int before = failures;134
int result = wait_initial_configure(&state, 5000);135
check(result == expected, name);136
check(!bad && !prepared && prepares == reads + cancels, "one read or cancel per successful prepare");137
if (which == TRAFFIC || which == INTERRUPT || which == BACKPRESSURE_TIMEOUT) {138
check(clock_ms == 5000 && polls == 5, name);139
}140
if (which == DECORATION)141
check(polls == 1 && state.decoration_mode == 2, "decoration must arrive before startup returns");142
if (which == FAST || which == PENDING || which == DISPATCH_ERROR)143
check(prepares == 0 && polls == 0, "pending events need no prepared read");144
if (which == FLUSH_ERROR)145
check(prepares == 1 && cancels == 1 && polls == 0, "flush failure cancels its prepared read");146
if (which == READ_ERROR)147
check(prepares == 1 && reads == 1 && cancels == 0, "failed read still balances prepare");148
if (which == BACKPRESSURE)149
check(prepares == 2 && cancels == 1 && reads == 1, "EAGAIN polls writable then retries");150
printf("%s: %s (prepare=%d read=%d cancel=%d poll=%d)\n",151
failures == before ? "PASS" : "FAIL", name, prepares, reads, cancels, polls);152
}153
int main(void)154
{155
run(FAST, "fast pending configure", 1);156
run(PENDING, "prepare finds pending configure", 1);157
run(READ, "read configure", 1);158
run(SILENT, "silent timeout", 0);159
run(DECORATION, "wait for negotiated decoration after surface configure", 1);160
run(DECORATION_TIMEOUT, "missing decoration uses the same total deadline", 0);161
run(TRAFFIC, "traffic total deadline", 0);162
run(INTERRUPT, "interrupted total deadline", 0);163
run(BACKPRESSURE, "flush EAGAIN", 1);164
run(BACKPRESSURE_TIMEOUT, "backpressure total deadline", 0);165
run(FLUSH_ERROR, "fatal flush", -1);166
run(POLL_ERROR, "fatal poll", -1);167
run(HANGUP, "poll hangup", -1);168
run(INVALID_FD, "invalid fd", -1);169
run(ERROR_FLAG, "poll error flag", -1);170
run(READ_ERROR, "failed read", -1);171
run(READ_HANGUP, "readable hangup", 1);172
run(DISPATCH_ERROR, "failed pending dispatch", -1);174
if (pipe(signal_pipe) < 0) return 2;175
struct sigaction action = { 0 }, previous;176
action.sa_handler = signal_handler;177
sigemptyset(&action.sa_mask);178
if (sigaction(SIGALRM, &action, &previous) < 0) return 2;179
struct itimerval interval = { { 0, 50000 }, { 0, 50000 } }, disabled = { 0 };180
real_start = real_ms();181
if (setitimer(ITIMER_REAL, &interval, NULL) < 0) return 2;182
run(REAL_INTERRUPT, "real interrupted deadline", 0);183
int64_t elapsed = real_ms() - real_start;184
setitimer(ITIMER_REAL, &disabled, NULL);185
sigaction(SIGALRM, &previous, NULL);186
close(signal_pipe[0]); close(signal_pipe[1]);187
check(signals_seen > 0 && interrupted_polls > 0, "real signals interrupted poll");188
check(elapsed >= 4800 && elapsed < 6500, "real interrupted deadline");189
printf("Real interrupted wait: %lld ms, %d interrupted polls\n", (long long)elapsed, interrupted_polls);190
return failures ? 1 : 0;191
}