AtlatestRepositorysigil-ui

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

1/* Owned wire/FD controls only. The peer accepts memfd fixtures, not real GPU
2 * allocations; successful hardware import/presentation needs a 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#ifndef SUI_TEST_WAYLAND_SOURCE
10#define SUI_TEST_WAYLAND_SOURCE "../../native/wayland.c"
11#endif
12#include SUI_TEST_WAYLAND_SOURCE
13#include "standalone-protocols.h"
14#include "owned-peer.h"
16#define LARGE_SPAN_SIZE 70778880
18enum ImportPeerObject { IP_DISPLAY = 1, IP_REGISTRY, IP_COMPOSITOR, IP_DMABUF,
19 IP_SUBCOMPOSITOR, IP_SURFACE, IP_PARAMS, IP_FRAME };
20typedef struct {
21 unsigned binds, adds, creates, params_live, buffers_live, buffers_destroyed, attaches;
22 unsigned commits, frames, releases;
23} ImportStats;
24typedef struct { unsigned planes; int pending; } ImportParams;
26static int import_reply(int fd, uint32_t params, uint32_t *server_id, const char *mode, ImportStats *stats)
28 if (!strcmp(mode, "failed")) return event(fd, params, 1, NULL, 0);
29 uint32_t buffer = (*server_id)++;
30 stats->buffers_live++;
31 return event(fd, params, 0, &buffer, 4);
34static int import_serve(int fd, const char *mode, ImportStats *stats)
36 enum ImportPeerObject objects[4096] = {0};
37 ImportParams params[4096] = {{0}};
38 uint32_t frames[4096] = {0}, attached[4096] = {0};
39 unsigned submitted[4096] = {0};
40 unsigned buffer_released[128] = {0};
41 objects[1] = IP_DISPLAY;
42 uint32_t input[16384], registry = 0, serial = 1, server_id = 0xff000000;
43 int descriptors[512];
44 size_t used = 0, n_descriptors = 0;
45 unsigned syncs = 0;
46 while (1) {
47 char control[CMSG_SPACE(256 * sizeof(int))];
48 struct iovec vector = {(uint8_t *)input + used, sizeof input - used};
49 struct msghdr incoming = {.msg_iov = &vector, .msg_iovlen = 1,
50 .msg_control = control, .msg_controllen = sizeof control};
51 ssize_t n = recvmsg(fd, &incoming, MSG_CMSG_CLOEXEC);
52 if (n < 0 && errno == EINTR) continue;
53 if (n <= 0) {
54 for (size_t i = 0; i < n_descriptors; i++) close(descriptors[i]);
55 return n == 0 || errno == ECONNRESET ? 0 : 2;
56 }
57 if (incoming.msg_flags & MSG_CTRUNC) return 3;
58 for (struct cmsghdr *header = CMSG_FIRSTHDR(&incoming); header; header = CMSG_NXTHDR(&incoming, header)) {
59 if (header->cmsg_level != SOL_SOCKET || header->cmsg_type != SCM_RIGHTS) continue;
60 size_t count = (header->cmsg_len - CMSG_LEN(0)) / sizeof(int);
61 if (count > 512 - n_descriptors) return 3;
62 memcpy(descriptors + n_descriptors, CMSG_DATA(header), count * sizeof(int));
63 n_descriptors += count;
64 }
65 used += (size_t)n;
66 size_t offset = 0, consumed_fds = 0;
67 while (used - offset >= 8) {
68 uint32_t *message = (uint32_t *)((uint8_t *)input + offset);
69 uint32_t id = message[0], opcode = message[1] & 0xffff;
70 size_t size = message[1] >> 16;
71 if (size < 8 || size % 4 || size > sizeof input) return 3;
72 if (used - offset < size) break;
73 size_t count = size / 4;
74 if (id >= 0xff000000) {
75 if (id >= server_id || opcode != 0 || !stats->buffers_live) return 3;
76 if (strncmp(mode, "p-", 2) == 0 && strcmp(mode, "p-parent-close") &&
77 strcmp(mode, "p-disconnect") && strcmp(mode, "p-callback-owned") &&
78 strcmp(mode, "p-frame-close-parent") && strcmp(mode, "p-cancel-close-parent") &&
79 strcmp(mode, "p-frame-child-dispatch"))
80 assert(buffer_released[id - 0xff000000]);
81 stats->buffers_live--;
82 stats->buffers_destroyed++;
83 } else if (id >= 4096) return 3;
84 else if (objects[id] == IP_DISPLAY && opcode == 1) {
85 registry = message[2];
86 if (registry >= 4096) return 3;
87 objects[registry] = IP_REGISTRY;
88 if (!advertise(fd, registry, 1, "wl_compositor", 4) ||
89 !advertise(fd, registry, 3, "wl_subcompositor", 1)) return 2;
90 if (strcmp(mode, "missing") && !advertise(fd, registry, 2, "zwp_linux_dmabuf_v1",
91 !strcmp(mode, "legacy") ? 3 : 4)) return 2;
92 } else if (objects[id] == IP_DISPLAY && opcode == 0) {
93 for (uint32_t i = 0; i < 4096; i++) {
94 if (!submitted[i] || submitted[i] > 2) continue;
95 if (!strcmp(mode, "p-disconnect")) return 0;
96 int frame_first = !strcmp(mode, "p-frame-first") || !strcmp(mode, "p-busy-buffer") ||
97 !strcmp(mode, "p-frame-unref") || !strcmp(mode, "p-frame-close-parent") ||
98 !strcmp(mode, "p-frame-child-dispatch");
99 int frame = (submitted[i] == 1) == frame_first;
100 if (frame && frames[i]) {
101 uint32_t time = 1234;
102 if (!event(fd, frames[i], 0, &time, 4)) return 2;
103 uint32_t deleted[] = {1, 12u << 16 | 1, frames[i]};
104 if (!send_bytes(fd, deleted, sizeof deleted)) return 2;
105 objects[frames[i]] = 0;
106 frames[i] = 0;
107 stats->frames++;
108 } else if (!frame) {
109 if (!event(fd, attached[i], 0, NULL, 0)) return 2;
110 buffer_released[attached[i] - 0xff000000] = 1;
111 stats->releases++;
112 }
113 submitted[i]++;
114 }
115 if (syncs == 2 && !strcmp(mode, "disconnect")) return 0;
116 if (syncs == 2 && (!strcmp(mode, "factory-pending") || !strcmp(mode, "factory-ready")))
117 if (!event(fd, registry, 1, &(uint32_t){2}, 4)) return 2;
118 if (syncs == 3) {
119 for (uint32_t i = 0; i < 4096; i++) {
120 if (!params[i].pending) continue;
121 if (!import_reply(fd, i, &server_id, mode, stats)) return 2;
122 params[i].pending = 0;
123 }
124 }
125 uint32_t done[] = {message[2], 12u << 16, serial++};
126 uint32_t deleted[] = {1, (12u << 16) | 1, message[2]};
127 if (!send_bytes(fd, done, sizeof done) || !send_bytes(fd, deleted, sizeof deleted)) return 2;
128 syncs++;
129 } else if (objects[id] == IP_REGISTRY && opcode == 0) {
130 if (count < 5 || message[count - 1] >= 4096) return 3;
131 objects[message[count - 1]] = message[2] == 1 ? IP_COMPOSITOR :
132 message[2] == 3 ? IP_SUBCOMPOSITOR : IP_DMABUF;
133 if (message[2] == 2) { assert(message[count - 2] == 4); stats->binds++; }
134 } else if (objects[id] == IP_COMPOSITOR && opcode == 0 && message[2] < 4096)
135 objects[message[2]] = IP_SURFACE;
136 else if (objects[id] == IP_SURFACE && opcode == 1) {
137 assert(count == 5 && message[2] >= 0xff000000 && message[2] < server_id);
138 attached[id] = message[2];
139 stats->attaches++;
140 } else if (objects[id] == IP_SURFACE && opcode == 3) {
141 assert(count == 3 && message[2] < 4096);
142 frames[id] = message[2];
143 objects[message[2]] = IP_FRAME;
144 } else if (objects[id] == IP_SURFACE && opcode == 6 && attached[id]) {
145 submitted[id] = 1;
146 stats->commits++;
147 } else if (objects[id] == IP_FRAME && opcode == 0) {
148 for (unsigned i = 0; i < 4096; i++) if (frames[i] == id) frames[i] = 0;
149 objects[id] = 0;
150 uint32_t deleted[] = {1, 12u << 16 | 1, id};
151 if (!send_bytes(fd, deleted, sizeof deleted)) return 2;
152 }
153 else if (objects[id] == IP_DMABUF && opcode == 1) {
154 if (message[2] >= 4096) return 3;
155 objects[message[2]] = IP_PARAMS;
156 params[message[2]] = (ImportParams){0};
157 stats->params_live++;
158 } else if (objects[id] == IP_PARAMS && opcode == 1) {
159 if (count != 7 || consumed_fds >= n_descriptors) return 3;
160 int plane_fd = descriptors[consumed_fds++];
161 struct stat info;
162 uint32_t marker = 0;
163 unsigned index = params[id].planes++;
164 int large = !strcmp(mode, "large-span");
165 assert(fstat(plane_fd, &info) == 0 && info.st_size == (large ? LARGE_SPAN_SIZE : 4096));
166 assert(fcntl(plane_fd, F_GETFD) & FD_CLOEXEC);
167 assert(pread(plane_fd, &marker, sizeof marker, 0) == 4 && marker == 0x22334455);
168 close(plane_fd);
169 assert(message[2] == index && message[3] == index * 1024 &&
170 message[4] == (large ? 20480u : !strcmp(mode, "p-scale2") ? 128u : 64u));
171 assert(message[5] == 0x01000000 && message[6] == 1);
172 stats->adds++;
173 } else if (objects[id] == IP_PARAMS && opcode == 2) {
174 assert(count == 6 && (!strcmp(mode, "large-span") ?
175 message[2] == 5120 && message[3] == 3456 :
176 (message[2] == 16 || message[2] == 32) && message[3] == message[2]) &&
177 message[4] == 0x34325241 && message[5] == 0);
178 assert(params[id].planes == (!strcmp(mode, "four-planes") ? 4u : 1u));
179 stats->creates++;
180 int delayed = !strcmp(mode, "delayed") || !strcmp(mode, "cancel-pending") ||
181 !strcmp(mode, "unref-pending") || !strcmp(mode, "close-pending") ||
182 !strcmp(mode, "factory-pending") || !strcmp(mode, "disconnect");
183 if (delayed) params[id].pending = 1;
184 else if (!import_reply(fd, id, &server_id, mode, stats)) return 2;
185 } else if (objects[id] == IP_PARAMS && opcode == 0) {
186 assert(stats->params_live);
187 stats->params_live--;
188 params[id].pending = 0;
189 objects[id] = 0;
190 uint32_t deleted[] = {1, (12u << 16) | 1, id};
191 if (!send_bytes(fd, deleted, sizeof deleted)) return 2;
192 }
193 offset += size;
194 }
195 if (offset) { memmove(input, (uint8_t *)input + offset, used - offset); used -= offset; }
196 if (consumed_fds) {
197 memmove(descriptors, descriptors + consumed_fds, (n_descriptors - consumed_fds) * sizeof(int));
198 n_descriptors -= consumed_fds;
199 }
200 }
203static unsigned import_fd_count(void)
205 DIR *directory = opendir("/proc/self/fd");
206 assert(directory);
207 unsigned count = 0;
208 while (readdir(directory)) count++;
209 closedir(directory);
210 return count;
213int main(int argc, char **argv)
215 assert(argc == 2);
216 const char *mode = argv[1];
217 struct rlimit no_core = {0, 0};
218 assert(setrlimit(RLIMIT_CORE, &no_core) == 0);
219 signal(SIGPIPE, SIG_IGN);
220 ImportStats *stats = mmap(NULL, sizeof *stats, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
221 assert(stats != MAP_FAILED);
222 int sockets[2];
223 assert(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == 0);
224 pid_t peer = fork();
225 assert(peer >= 0);
226 if (!peer) {
227 close(sockets[0]);
228 int result = import_serve(sockets[1], mode, stats);
229 if (result == 2 && (errno == EPIPE || errno == ECONNRESET)) result = 0;
230 _exit(result);
231 }
232 close(sockets[1]);
233 char socket_name[32], error[256];
234 snprintf(socket_name, sizeof socket_name, "%d", sockets[0]);
235 assert(setenv("WAYLAND_SOCKET", socket_name, 1) == 0);
236 assert(load_libraries(error, sizeof error));
237 Wl *root = calloc(1, sizeof *root);
238 assert(root);
239 root->references = root->configured = root->scale = 1;
240 root->width = 640;
241 root->height = 480;
242 for (unsigned i = 0; i < CLIPBOARD_WRITES; i++) root->writes[i].fd = -1;
243 root->display = wl_display_connect(NULL);
244 assert(root->display);
245 root->registry = wl_display_get_registry(root->display);
246 assert(wl_registry_add_listener(root->registry, &registry_listener, root) == 0);
247 assert(wl_display_roundtrip(root->display) >= 0);
248 root->surface = wl_compositor_create_surface(root->compositor);
249 Wl *child = sui_wayland_child(root, 0, 0, 320, 240, 0);
250 assert(child && wl_display_roundtrip(root->display) >= 0 && stats->binds == 0);
251 int plane = memfd_create("import-fixture", MFD_CLOEXEC);
252 uint32_t marker = 0x22334455;
253 assert(plane >= 0 && ftruncate(plane, 4096) == 0 && pwrite(plane, &marker, 4, 0) == 4);
254 int original_flags = fcntl(plane, F_GETFD);
255 unsigned descriptors = import_fd_count();
256 SuiWaylandDmabufSpec spec = {.width = 16, .height = 16, .fourcc = 0x34325241,
257 .n_planes = !strcmp(mode, "four-planes") ? 4 : 1, .modifier = 0x0100000000000001};
258 for (unsigned i = 0; i < 4; i++) spec.planes[i] = (SuiWaylandDmabufPlane){plane, i * 1024, 64};
259 if (!strcmp(mode, "large-span")) {
260 /* 5120 x 3456 x 4 bytes is above the former fixed 64 MiB plane
261 * bound and within this sparse allocation, so it must import. */
262 assert(ftruncate(plane, LARGE_SPAN_SIZE) == 0);
263 spec.width = 5120;
264 spec.height = 3456;
265 spec.planes[0].stride = 20480;
266 }
267 SuiWaylandDmabufImport *buffer = NULL;
268 if (!strcmp(mode, "invalid")) {
269 SuiWaylandDmabufSpec bad = spec;
270 bad.width = 0;
271 assert(!sui_wayland_dmabuf_import(child, &bad) && errno == EINVAL);
272 bad = spec; bad.n_planes = 5;
273 assert(!sui_wayland_dmabuf_import(child, &bad) && errno == EINVAL);
274 bad = spec; bad.planes[0].stride = 0;
275 assert(!sui_wayland_dmabuf_import(child, &bad) && errno == EINVAL);
276 bad = spec; bad.height = 16385;
277 assert(!sui_wayland_dmabuf_import(child, &bad) && errno == EINVAL);
278 bad = spec; bad.planes[0].offset = UINT32_MAX;
279 assert(!sui_wayland_dmabuf_import(child, &bad) && errno == EINVAL);
280 bad = spec; bad.planes[0].stride = UINT32_MAX;
281 assert(!sui_wayland_dmabuf_import(child, &bad) && errno == EINVAL);
282 /* Real-size bounds: 64 x 128 rows exceed the 4096-byte allocation,
283 * as does a later plane whose first row runs past its end. The
284 * descriptor's file position survives the size query. */
285 assert(lseek(plane, 100, SEEK_SET) == 100);
286 bad = spec; bad.height = 128;
287 assert(!sui_wayland_dmabuf_import(child, &bad) && errno == EINVAL);
288 bad = spec; bad.n_planes = 2; bad.planes[1].offset = 4096 - 32;
289 assert(!sui_wayland_dmabuf_import(child, &bad) && errno == EINVAL);
290 assert(lseek(plane, 0, SEEK_CUR) == 100);
291 /* A pipe cannot report its size, but a span beyond 32 bits is still
292 * refused locally: compositors end the connection over it. */
293 int pipe_fds[2];
294 assert(pipe2(pipe_fds, O_CLOEXEC) == 0);
295 bad = spec; bad.height = 16384;
296 for (unsigned i = 0; i < 4; i++) bad.planes[i].fd = pipe_fds[0];
297 bad.planes[0].stride = 0x80000000u;
298 assert(!sui_wayland_dmabuf_import(child, &bad) && errno == EINVAL);
299 bad.planes[0].stride = 64; bad.planes[0].offset = UINT32_MAX - 32;
300 assert(!sui_wayland_dmabuf_import(child, &bad) && errno == EINVAL);
301 assert(close(pipe_fds[0]) == 0 && close(pipe_fds[1]) == 0);
302 int stale = dup(plane);
303 assert(stale >= 0 && close(stale) == 0);
304 bad = spec; bad.n_planes = 2; bad.planes[1].fd = stale;
305 assert(!sui_wayland_dmabuf_import(child, &bad) && errno == EBADF);
306 assert(stats->binds == 0 && import_fd_count() == descriptors);
307 } else if (!strcmp(mode, "missing") || !strcmp(mode, "legacy")) {
308 assert(!sui_wayland_dmabuf_import(child, &spec) && errno == ENOTSUP);
309 assert(stats->binds == 0 && import_fd_count() == descriptors);
310 } else if (!strcmp(mode, "limit")) {
311 SuiWaylandDmabufImport *buffers[DMABUF_IMPORT_LIMIT];
312 for (unsigned i = 0; i < DMABUF_IMPORT_LIMIT; i++) {
313 buffers[i] = sui_wayland_dmabuf_import(child, &spec);
314 assert(buffers[i]);
315 }
316 assert(!sui_wayland_dmabuf_import(child, &spec) && errno == ENOSPC);
317 assert(wl_display_roundtrip(root->display) >= 0);
318 for (unsigned i = 0; i < DMABUF_IMPORT_LIMIT; i++) {
319 assert(sui_wayland_dmabuf_import_status(buffers[i]) == SUI_DMABUF_IMPORT_READY);
320 sui_wayland_dmabuf_import_unref(buffers[i]);
321 }
322 assert(wl_display_roundtrip(root->display) >= 0);
323 assert(!stats->params_live && !stats->buffers_live && import_fd_count() == descriptors);
324 } else {
325 buffer = sui_wayland_dmabuf_import(child, &spec);
326 assert(buffer && sui_wayland_dmabuf_import_status(buffer) == SUI_DMABUF_IMPORT_PENDING);
327 sui_wayland_dmabuf_import_unref(sui_wayland_dmabuf_import_ref(buffer));
328 if (!strcmp(mode, "close-unflushed")) goto closing;
329 if (!strcmp(mode, "cancel-pending")) sui_wayland_dmabuf_import_cancel(buffer);
330 if (!strcmp(mode, "unref-pending")) { sui_wayland_dmabuf_import_unref(buffer); buffer = NULL; }
331 if (!strcmp(mode, "late-created")) {
332 assert(wl_display_flush(root->display) >= 0);
333 struct pollfd ready = {wl_display_get_fd(root->display), POLLIN, 0};
334 assert(poll(&ready, 1, 1000) > 0);
335 sui_wayland_dmabuf_import_unref(buffer); buffer = NULL;
336 }
337 if (!strcmp(mode, "child-close")) sui_wayland_close(child);
338 int result = wl_display_roundtrip(root->display);
339 if (!strcmp(mode, "disconnect")) {
340 assert(result < 0 && dispatch(root, 0) < 0);
341 assert(sui_wayland_dmabuf_import_status(buffer) == SUI_DMABUF_IMPORT_CANCELED);
342 } else {
343 assert(result >= 0);
344 if (!strcmp(mode, "close-pending"))
345 assert(sui_wayland_dmabuf_import_status(buffer) == SUI_DMABUF_IMPORT_PENDING);
346 else {
347 if (!strcmp(mode, "delayed"))
348 assert(sui_wayland_dmabuf_import_status(buffer) == SUI_DMABUF_IMPORT_PENDING);
349 assert(wl_display_roundtrip(root->display) >= 0);
350 if (buffer) {
351 int expected = !strcmp(mode, "failed") ? SUI_DMABUF_IMPORT_FAILED :
352 (!strcmp(mode, "cancel-pending") || !strcmp(mode, "factory-pending")) ?
353 SUI_DMABUF_IMPORT_CANCELED : SUI_DMABUF_IMPORT_READY;
354 assert(sui_wayland_dmabuf_import_status(buffer) == expected);
355 if (!strcmp(mode, "cancel-ready")) {
356 sui_wayland_dmabuf_import_cancel(buffer);
357 assert(sui_wayland_dmabuf_import_status(buffer) == SUI_DMABUF_IMPORT_CANCELED);
358 }
359 if (!strcmp(mode, "factory-ready"))
360 assert(!sui_wayland_dmabuf_import(child, &spec) && errno == ENOTSUP);
361 }
362 }
363 }
364 if (strcmp(mode, "close-ready") && strcmp(mode, "close-pending") && strcmp(mode, "disconnect")) {
365 sui_wayland_dmabuf_import_unref(buffer); buffer = NULL;
366 assert(wl_display_roundtrip(root->display) >= 0);
367 assert(!stats->params_live && !stats->buffers_live && import_fd_count() == descriptors);
368 }
369 }
370closing:
371 assert(fcntl(plane, F_GETFD) == original_flags && stats->attaches == 0);
372 sui_wayland_close(root);
373 sui_wayland_unref(child);
374 sui_wayland_unref(root);
375 assert(sui_wayland_dmabuf_import_status(buffer) == SUI_DMABUF_IMPORT_CANCELED);
376 sui_wayland_dmabuf_import_unref(buffer);
377 assert(fcntl(plane, F_GETFD) == original_flags && import_fd_count() + 1 == descriptors);
378 close(plane);
379 unsetenv("WAYLAND_SOCKET");
380 int status;
381 assert(waitpid(peer, &status, 0) == peer && WIFEXITED(status) && WEXITSTATUS(status) == 0);
382 munmap(stats, sizeof *stats);
383 printf("PASS: import wire %s, borrowed FDs, late-response cleanup and connection ownership\n", mode);
384 return 0;