AtlatestRepositorysigil-ui

sigil-ui / tree / test / waylanddmabuf-feedback-peer.c

1/* Owned socketpair peer: exercise the real client wire decoder, FD transfer,
2 * atomic feedback and snapshot lifetime without a GPU or desktop compositor. */
3#include <assert.h>
4#include <dirent.h>
5#include <signal.h>
6#include <sys/resource.h>
7#include <sys/socket.h>
8#include <sys/wait.h>
9#include "../../native/wayland.c"
10#include "standalone-protocols.h"
12enum PeerObject { PEER_DISPLAY = 1, PEER_REGISTRY, PEER_COMPOSITOR, PEER_DMABUF,
13 PEER_SUBCOMPOSITOR, PEER_SURFACE, PEER_FEEDBACK };
14typedef struct { unsigned binds, feedbacks, destroys; } PeerStats;
16#include "owned-peer.h"
18static int send_table(int fd, uint32_t object, uint64_t modifier, const char *fault)
20 uint8_t table[32] = {0};
21 uint32_t format = 0x34325241;
22 memcpy(table, &format, 4);
23 memcpy(table + 16, &format, 4);
24 memcpy(table + 24, &modifier, 8);
25 int table_fd = memfd_create("feedback-table", MFD_CLOEXEC);
26 if (table_fd < 0) return 0;
27 size_t stored = !strcmp(fault, "short-table") ? 16 : sizeof table;
28 if (!send_bytes(table_fd, table, stored)) { close(table_fd); return 0; }
29 uint32_t size = !strcmp(fault, "oversized-table") ? DMABUF_TABLE_BYTES + 16 :
30 !strcmp(fault, "odd-table") ? 31 : 32;
31 uint32_t words[] = {object, (12u << 16) | 1, size};
32 char control[CMSG_SPACE(sizeof(int))] = {0};
33 struct iovec vector = {words, sizeof words};
34 struct msghdr message = {.msg_iov = &vector, .msg_iovlen = 1,
35 .msg_control = control, .msg_controllen = sizeof control};
36 struct cmsghdr *header = CMSG_FIRSTHDR(&message);
37 header->cmsg_level = SOL_SOCKET;
38 header->cmsg_type = SCM_RIGHTS;
39 header->cmsg_len = CMSG_LEN(sizeof(int));
40 memcpy(CMSG_DATA(header), &table_fd, sizeof table_fd);
41 ssize_t n;
42 do { n = sendmsg(fd, &message, MSG_NOSIGNAL); } while (n < 0 && errno == EINTR);
43 close(table_fd);
44 return n > 0 && ((size_t)n == sizeof words || send_bytes(fd, (uint8_t *)words + n, sizeof words - n));
47static int feedback_batch(int fd, uint32_t object, uint64_t modifier, const char *fault, int done)
49 dev_t main_device = 0x1234, second_device = 0x5678;
50 uint16_t first[] = {1, 0}, second[] = {0};
51 uint32_t flags = 1;
52 if (strcmp(fault, "reuse-table") && !send_table(fd, object, modifier, fault)) return 0;
53 if (strcmp(fault, "missing-main") &&
54 !array_event(fd, object, 2, &main_device, !strcmp(fault, "bad-device") ? 1 : sizeof main_device)) return 0;
55 if (!array_event(fd, object, 4, &main_device, sizeof main_device) ||
56 !event(fd, object, 6, &flags, 4)) return 0;
57 if (!strcmp(fault, "duplicate-flags") && !event(fd, object, 6, &flags, 4)) return 0;
58 if (!strcmp(fault, "bad-index")) first[0] = 9;
59 if (!strcmp(fault, "duplicate-pair")) first[0] = 0;
60 if (!strcmp(fault, "format-limit")) {
61 /* Stay within libwayland's wire-message limit while exceeding the
62 * aggregate parser bound across several tranche_formats events. */
63 uint16_t indices[1024] = {0};
64 size_t remaining = DMABUF_FORMATS + 1;
65 while (remaining) {
66 size_t count = remaining < 1024 ? remaining : 1024;
67 if (!array_event(fd, object, 5, indices, count * sizeof *indices)) return 0;
68 remaining -= count;
69 }
70 } else if (!array_event(fd, object, 5, first, !strcmp(fault, "odd-index") ? 1 : sizeof first)) return 0;
71 if (strcmp(fault, "unfinished-tranche") && !event(fd, object, 3, NULL, 0)) return 0;
72 flags = 0;
73 if (!array_event(fd, object, 4, &second_device, sizeof second_device) ||
74 !event(fd, object, 6, &flags, 4) || !array_event(fd, object, 5, second, sizeof second) ||
75 !event(fd, object, 3, NULL, 0)) return 0;
76 if (!strcmp(fault, "tranche-limit")) {
77 for (unsigned i = 2; i <= DMABUF_TRANCHES; i++) {
78 dev_t device = second_device + i;
79 if (!array_event(fd, object, 4, &device, sizeof device) ||
80 !event(fd, object, 6, &flags, 4) || !array_event(fd, object, 5, second, sizeof second) ||
81 !event(fd, object, 3, NULL, 0)) return 0;
82 }
83 }
84 return !done || event(fd, object, 0, NULL, 0);
87static int serve(int fd, const char *mode, PeerStats *stats)
89 enum PeerObject objects[4096] = {0};
90 objects[1] = PEER_DISPLAY;
91 uint32_t input[16384], feedback = 0, registry = 0, serial = 1;
92 size_t used = 0;
93 unsigned syncs = 0;
94 while (1) {
95 ssize_t n = read(fd, (uint8_t *)input + used, sizeof input - used);
96 if (!n || (n < 0 && errno == ECONNRESET)) return 0;
97 if (n < 0 && errno == EINTR) continue;
98 if (n < 0) return 2;
99 used += (size_t)n;
100 size_t offset = 0;
101 while (used - offset >= 8) {
102 uint32_t *message = (uint32_t *)((uint8_t *)input + offset);
103 uint32_t id = message[0], opcode = message[1] & 0xffff;
104 size_t size = message[1] >> 16;
105 if (size < 8 || size % 4 || size > sizeof input || id >= 4096) return 3;
106 if (used - offset < size) break;
107 size_t count = size / 4;
108 if (objects[id] == PEER_DISPLAY && opcode == 1) {
109 registry = message[2];
110 if (registry >= 4096) return 3;
111 objects[registry] = PEER_REGISTRY;
112 if (!advertise(fd, registry, 1, "wl_compositor", 4) ||
113 !advertise(fd, registry, 3, "wl_subcompositor", 1)) return 2;
114 if (strcmp(mode, "missing") && !advertise(fd, registry, 2, "zwp_linux_dmabuf_v1",
115 !strcmp(mode, "legacy") ? 3 : 4)) return 2;
116 } else if (objects[id] == PEER_DISPLAY && opcode == 0) {
117 /* Pause between updates at real display.sync barriers. */
118 if (feedback) {
119 if (syncs == 4 && !strcmp(mode, "disconnect")) return 0;
120 if (syncs == 3 && !feedback_batch(fd, feedback, 0x0100000000000001, "valid", 0)) return 2;
121 if (syncs == 4 && !event(fd, feedback, 0, NULL, 0)) return 2;
122 if (syncs == 5 && !feedback_batch(fd, feedback, 17, mode, 1)) return 2;
123 if (syncs == 6 && !feedback_batch(fd, feedback, 0x00ffffffffffffff, "valid", 1)) return 2;
124 if (syncs == 7 && !event(fd, registry, 1, &(uint32_t){2}, 4)) return 2;
125 }
126 if (message[2] >= 4096) return 3;
127 uint32_t done[] = {message[2], 12u << 16, serial++};
128 uint32_t deleted[] = {1, (12u << 16) | 1, message[2]};
129 if (!send_bytes(fd, done, sizeof done) || !send_bytes(fd, deleted, sizeof deleted)) return 2;
130 syncs++;
131 } else if (objects[id] == PEER_REGISTRY && opcode == 0) {
132 if (count < 5 || message[count - 1] >= 4096) return 3;
133 uint32_t created = message[count - 1];
134 objects[created] = message[2] == 1 ? PEER_COMPOSITOR :
135 message[2] == 3 ? PEER_SUBCOMPOSITOR : PEER_DMABUF;
136 if (objects[created] == PEER_DMABUF) { assert(message[count - 2] == 4); stats->binds++; }
137 } else if (objects[id] == PEER_COMPOSITOR && opcode == 0 && message[2] < 4096)
138 objects[message[2]] = PEER_SURFACE;
139 else if (objects[id] == PEER_DMABUF && opcode == 3) {
140 feedback = message[2];
141 if (feedback >= 4096) return 3;
142 objects[feedback] = PEER_FEEDBACK;
143 stats->feedbacks++;
144 if (!feedback_batch(fd, feedback, 0x00ffffffffffffff, "valid", 1)) return 2;
145 } else if (objects[id] == PEER_FEEDBACK && opcode == 0) stats->destroys++;
146 offset += size;
147 }
148 if (offset) { memmove(input, (uint8_t *)input + offset, used - offset); used -= offset; }
149 }
152static unsigned descriptor_count(void)
154 DIR *directory = opendir("/proc/self/fd");
155 assert(directory);
156 unsigned count = 0;
157 while (readdir(directory)) count++;
158 closedir(directory);
159 return count;
162static void check_snapshot(SuiWaylandDmabufFeedback *feedback, uint64_t generation, uint64_t modifier)
164 assert(feedback && sui_wayland_dmabuf_feedback_generation(feedback) == generation);
165 assert(sui_wayland_dmabuf_feedback_main_device(feedback) == (dev_t)0x1234);
166 assert(sui_wayland_dmabuf_feedback_tranche_count(feedback) == 2);
167 dev_t device = 0;
168 uint32_t flags = 0, fourcc = 0;
169 uint64_t actual = 0;
170 size_t formats = 0;
171 assert(sui_wayland_dmabuf_feedback_tranche(feedback, 0, &device, &flags, &formats));
172 assert(device == (dev_t)0x1234 && flags == 1 && formats == 2);
173 assert(sui_wayland_dmabuf_feedback_format(feedback, 0, 0, &fourcc, &actual));
174 assert(fourcc == 0x34325241 && actual == modifier);
175 assert(sui_wayland_dmabuf_feedback_format(feedback, 0, 1, &fourcc, &actual) && actual == 0);
176 assert(sui_wayland_dmabuf_feedback_tranche(feedback, 1, &device, &flags, &formats));
177 assert(device == (dev_t)0x5678 && flags == 0 && formats == 1);
178 assert(!sui_wayland_dmabuf_feedback_tranche(feedback, 2, NULL, NULL, NULL));
179 assert(!sui_wayland_dmabuf_feedback_format(feedback, 0, 2, NULL, NULL));
182int main(int argc, char **argv)
184 struct rlimit no_core = {0, 0};
185 assert(setrlimit(RLIMIT_CORE, &no_core) == 0);
186 assert(argc == 2);
187 signal(SIGPIPE, SIG_IGN);
188 PeerStats *stats = mmap(NULL, sizeof *stats, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
189 assert(stats != MAP_FAILED);
190 int sockets[2];
191 assert(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == 0);
192 pid_t peer = fork();
193 assert(peer >= 0);
194 if (!peer) { close(sockets[0]); _exit(serve(sockets[1], argv[1], stats)); }
195 close(sockets[1]);
196 char socket_name[32], error[256];
197 snprintf(socket_name, sizeof socket_name, "%d", sockets[0]);
198 assert(setenv("WAYLAND_SOCKET", socket_name, 1) == 0);
199 assert(load_libraries(error, sizeof error));
200 Wl *root = calloc(1, sizeof *root);
201 assert(root);
202 root->references = 1;
203 root->configured = root->scale = 1;
204 root->width = 640;
205 root->height = 480;
206 for (unsigned i = 0; i < CLIPBOARD_WRITES; i++) root->writes[i].fd = -1;
207 root->display = wl_display_connect(NULL);
208 assert(root->display);
209 root->registry = wl_display_get_registry(root->display);
210 assert(wl_registry_add_listener(root->registry, &registry_listener, root) == 0);
211 assert(wl_display_roundtrip(root->display) >= 0);
212 root->surface = wl_compositor_create_surface(root->compositor);
213 Wl *child = sui_wayland_child(root, 0, 0, 320, 240, 0);
214 assert(child);
215 assert(wl_display_roundtrip(root->display) >= 0);
216 assert(stats->binds == 0); /* Default SHM never binds optional feedback. */
217 unsigned descriptors = descriptor_count();
218 uint64_t generation = 99;
219 int unavailable = !strcmp(argv[1], "missing") || !strcmp(argv[1], "legacy");
220 assert(sui_wayland_dmabuf_feedback_enable(child) == !unavailable);
221 if (!unavailable) {
222 assert(sui_wayland_dmabuf_feedback_status(child, &generation) == SUI_DMABUF_PENDING && generation == 0);
223 assert(wl_display_roundtrip(root->display) >= 0);
224 SuiWaylandDmabufFeedback *first = sui_wayland_dmabuf_feedback_acquire(child);
225 check_snapshot(first, 1, 0x00ffffffffffffff);
226 assert(sui_wayland_dmabuf_feedback_enable(child));
227 assert(stats->binds == 1 && stats->feedbacks == 1);
228 assert(wl_display_roundtrip(root->display) >= 0); /* Partial update. */
229 SuiWaylandDmabufFeedback *partial = sui_wayland_dmabuf_feedback_acquire(child);
230 assert(partial == first);
231 sui_wayland_dmabuf_feedback_unref(partial);
232 if (!strcmp(argv[1], "close-partial")) {
233 sui_wayland_close(root);
234 check_snapshot(first, 1, 0x00ffffffffffffff);
235 sui_wayland_dmabuf_feedback_unref(first);
236 goto cleanup;
237 }
238 if (!strcmp(argv[1], "disconnect")) {
239 assert(wl_display_roundtrip(root->display) < 0);
240 assert(dispatch(root, 0) < 0);
241 assert(sui_wayland_dmabuf_feedback_status(child, NULL) == SUI_DMABUF_UNAVAILABLE);
242 assert(!sui_wayland_dmabuf_feedback_acquire(child));
243 sui_wayland_close(root);
244 check_snapshot(first, 1, 0x00ffffffffffffff);
245 sui_wayland_dmabuf_feedback_unref(first);
246 goto cleanup;
247 }
248 assert(wl_display_roundtrip(root->display) >= 0);
249 SuiWaylandDmabufFeedback *second = sui_wayland_dmabuf_feedback_acquire(child);
250 check_snapshot(second, 2, 0x0100000000000001);
251 check_snapshot(first, 1, 0x00ffffffffffffff);
252 if (!strcmp(argv[1], "close-active")) {
253 sui_wayland_close(root);
254 check_snapshot(first, 1, 0x00ffffffffffffff);
255 check_snapshot(second, 2, 0x0100000000000001);
256 sui_wayland_dmabuf_feedback_unref(first);
257 sui_wayland_dmabuf_feedback_unref(second);
258 goto cleanup;
259 }
260 assert(wl_display_roundtrip(root->display) >= 0);
261 int valid = !strcmp(argv[1], "valid") || !strcmp(argv[1], "reuse-table");
262 assert(sui_wayland_dmabuf_feedback_status(child, &generation) ==
263 (valid ? SUI_DMABUF_READY : SUI_DMABUF_INVALID));
264 assert(generation == 3);
265 SuiWaylandDmabufFeedback *third = sui_wayland_dmabuf_feedback_acquire(child);
266 if (valid) check_snapshot(third, 3, !strcmp(argv[1], "reuse-table") ? 0x0100000000000001 : 17);
267 else assert(!third);
268 sui_wayland_dmabuf_feedback_unref(third);
269 assert(wl_display_roundtrip(root->display) >= 0);
270 SuiWaylandDmabufFeedback *recovered = sui_wayland_dmabuf_feedback_acquire(child);
271 check_snapshot(recovered, 4, 0x00ffffffffffffff);
272 assert(wl_display_roundtrip(root->display) >= 0); /* Global removal. */
273 assert(sui_wayland_dmabuf_feedback_status(child, &generation) == SUI_DMABUF_UNAVAILABLE && generation == 5);
274 assert(!sui_wayland_dmabuf_feedback_acquire(child));
275 assert(descriptor_count() == descriptors);
276 sui_wayland_close(root);
277 check_snapshot(first, 1, 0x00ffffffffffffff);
278 check_snapshot(second, 2, 0x0100000000000001);
279 check_snapshot(recovered, 4, 0x00ffffffffffffff);
280 sui_wayland_dmabuf_feedback_unref(sui_wayland_dmabuf_feedback_ref(first));
281 sui_wayland_dmabuf_feedback_unref(first);
282 sui_wayland_dmabuf_feedback_unref(second);
283 sui_wayland_dmabuf_feedback_unref(recovered);
284 } else {
285 assert(sui_wayland_dmabuf_feedback_status(child, &generation) == SUI_DMABUF_UNAVAILABLE);
286 assert(generation == 0 && !sui_wayland_dmabuf_feedback_acquire(child));
287 assert(stats->binds == 0 && descriptor_count() == descriptors);
288 sui_wayland_close(root);
289 }
290cleanup:
291 assert(sui_wayland_dmabuf_feedback_status(child, &generation) == SUI_DMABUF_UNAVAILABLE && generation == 0);
292 sui_wayland_unref(child);
293 sui_wayland_unref(root);
294 unsetenv("WAYLAND_SOCKET");
295 int status;
296 assert(waitpid(peer, &status, 0) == peer && WIFEXITED(status) && WEXITSTATUS(status) == 0);
297 assert(unavailable || stats->destroys == (!strcmp(argv[1], "disconnect") ? 0u : 1u));
298 munmap(stats, sizeof *stats);
299 printf("PASS: feedback wire %s, atomic snapshots, immutable readers, FD ownership and teardown\n", argv[1]);
300 return 0;