Compare commits

..
14 changed files with 210 additions and 382 deletions

No files matched your search

@@ -22,7 +22,6 @@ add_library(droiddeckwayland SHARED
src/hdr_compose.c
src/blend_pass.c
src/gpu_busy.c
src/unix_listen.c
generated/color-management-v1-protocol.c
generated/xdg-shell-protocol.c
generated/linux-dmabuf-v1-protocol.c
+31 -14
View File
@@ -39,7 +39,6 @@
#include "droiddeck_color.h"
#include "compositor_internal.h"
#include "clock_domain.h"
#include "unix_listen.h"
#define WLOGI(...) __android_log_print(ANDROID_LOG_INFO, "DroidDeckWayland", __VA_ARGS__)
#define WLOGE(...) __android_log_print(ANDROID_LOG_ERROR, "DroidDeckWayland", __VA_ARGS__)
@@ -225,6 +224,8 @@ struct surface {
struct wl_resource *dmabuf;
struct dmabuf_buffer *dmabuf_buf;
struct wl_listener dmabuf_destroy;
struct wl_resource *pixel;
struct wl_listener pixel_destroy;
const void *last_dmabuf;
int buf_w, buf_h, has_content;
int buf_alpha;
@@ -445,7 +446,7 @@ static int buffer_shown_elsewhere(struct wl_resource *buffer, const struct surfa
struct surface *o;
if (!buffer) return 0;
wl_list_for_each(o, &g_surfaces, link)
if (o != except && o->dmabuf == buffer) return 1;
if (o != except && (o->dmabuf == buffer || o->pixel == buffer)) return 1;
return 0;
}
@@ -817,6 +818,27 @@ static void drop_dmabuf(struct surface *s, int paced) {
s->dmabuf_buf = NULL;
}
static void on_pixel_destroyed(struct wl_listener *l, void *data) {
struct surface *s = wl_container_of(l, s, pixel_destroy);
wl_list_remove(&s->pixel_destroy.link);
s->pixel = NULL;
}
static void hold_pixel(struct surface *s, struct wl_resource *buffer) {
if (s->pixel == buffer) return;
s->pixel = buffer;
s->pixel_destroy.notify = on_pixel_destroyed;
wl_resource_add_destroy_listener(buffer, &s->pixel_destroy);
}
static void drop_pixel(struct surface *s) {
struct wl_resource *buffer = s->pixel;
if (!buffer) return;
wl_list_remove(&s->pixel_destroy.link);
s->pixel = NULL;
if (!buffer_shown_elsewhere(buffer, s)) send_buffer_release(buffer);
}
static void on_dmabuf_destroyed(struct wl_listener *l, void *data) {
struct surface *s = wl_container_of(l, s, dmabuf_destroy);
wl_list_remove(&s->dmabuf_destroy.link);
@@ -962,14 +984,13 @@ static void take_shm(struct surface *s, struct wl_shm_buffer *shm, struct wl_res
droiddeck_on_compositor_frame();
}
static void take_pixel(struct surface *s, const uint8_t bgra[4], struct wl_resource *buffer) {
static void take_pixel(struct surface *s, const uint8_t bgra[4]) {
if (s->shm_img && (vkp_image_width(s->shm_img) != 1 || vkp_image_height(s->shm_img) != 1)) {
vkp_image_destroy(s->shm_img);
s->shm_img = NULL;
}
if (!s->shm_img) s->shm_img = vkp_image_create_shm(1, 1);
if (s->shm_img) vkp_image_upload_shm(s->shm_img, bgra, 4);
send_buffer_release(buffer);
s->buf_w = 1;
s->buf_h = 1;
s->buf_alpha = bgra[3] != 0xff;
@@ -1385,6 +1406,7 @@ static void surface_commit(struct wl_client *c, struct wl_resource *r) {
surface_drop_idle(s);
buf_trace_attach(buffer);
if (s->pixel != buffer) drop_pixel(s);
if (s->role == ROLE_CURSOR) {
if (!buffer) {
cursor_publish_hidden();
@@ -1401,7 +1423,8 @@ static void surface_commit(struct wl_client *c, struct wl_resource *r) {
take_shm(s, shm, buffer);
} else if (single_pixel_buffer_get(buffer, pixel)) {
drop_dmabuf(s, 1);
take_pixel(s, pixel, buffer);
take_pixel(s, pixel);
hold_pixel(s, buffer);
} else {
drop_dmabuf(s, 1);
s->has_content = 0;
@@ -1494,6 +1517,7 @@ static void surface_resource_destroy(struct wl_resource *r) {
if (s->pending_buffer) wl_list_remove(&s->pending_buffer_destroy.link);
pending_releases_forget_surface(s);
drop_dmabuf(s, 0);
drop_pixel(s);
ahb_swapchain_surface_gone(s);
vkp_image_destroy(s->shm_img);
wl_resource_for_each_safe(cb, cbtmp, &s->pending_frames) wl_resource_destroy(cb);
@@ -1630,6 +1654,7 @@ static void subsurface_resource_destroy(struct wl_resource *r) {
s->role = ROLE_NONE;
s->has_content = 0;
drop_dmabuf(s, 1);
drop_pixel(s);
schedule_render();
}
@@ -4369,15 +4394,7 @@ int droiddeck_wayland_run(void) {
snprintf(p, sizeof(p), "%s/wayland-0", rt); unlink(p);
snprintf(p, sizeof(p), "%s/wayland-0.lock", rt); unlink(p);
}
int listening = -1;
if (rt && *rt && !droiddeck_unix_path_fits(rt, "wayland-0")) {
int fd = droiddeck_unix_listen(rt, "wayland-0", 128);
if (fd >= 0 && wl_display_add_socket_fd(display, fd) == 0) listening = 0;
else if (fd >= 0) close(fd);
} else {
listening = wl_display_add_socket(display, "wayland-0");
}
if (listening != 0) {
if (wl_display_add_socket(display, "wayland-0") != 0) {
WLOGE("add_socket(wayland-0) failed in XDG_RUNTIME_DIR=%s (errno path/perms?)",
rt ? rt : "(null)");
return 1;
@@ -1,37 +0,0 @@
#include "unix_listen.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
bool droiddeck_unix_path_fits(const char *dir, const char *name) {
struct sockaddr_un addr;
return strlen(dir) + 1 + strlen(name) < sizeof(addr.sun_path);
}
int droiddeck_unix_listen(const char *dir, const char *name, int backlog) {
struct sockaddr_un addr = { .sun_family = AF_UNIX };
int dir_fd = open(dir, O_PATH | O_DIRECTORY | O_CLOEXEC);
if (dir_fd < 0) return -1;
int written = snprintf(addr.sun_path, sizeof(addr.sun_path), "/proc/self/fd/%d/%s", dir_fd, name);
if (written < 0 || (size_t)written >= sizeof(addr.sun_path)) {
close(dir_fd);
errno = ENAMETOOLONG;
return -1;
}
int fd = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
if (fd >= 0 && (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0 || listen(fd, backlog) != 0)) {
int saved = errno;
close(fd);
fd = -1;
errno = saved;
}
int saved = errno;
close(dir_fd);
errno = saved;
return fd;
}
@@ -1,7 +0,0 @@
#ifndef UNIX_LISTEN_H
#define UNIX_LISTEN_H
#include <stdbool.h>
bool droiddeck_unix_path_fits(const char *dir, const char *name);
int droiddeck_unix_listen(const char *dir, const char *name, int backlog);
#endif
+22 -29
View File
@@ -34,7 +34,7 @@ enum {
NR_LLISTXATTR = 12, NR_REMOVEXATTR = 14, NR_LREMOVEXATTR = 15, NR_GETCWD = 17,
NR_INOTIFY_ADD_WATCH = 27, NR_MKNODAT = 33, NR_MKDIRAT = 34, NR_UNLINKAT = 35, NR_SYMLINKAT = 36,
NR_LINKAT = 37, NR_RENAMEAT = 38, NR_STATFS = 43, NR_TRUNCATE = 45, NR_FACCESSAT = 48,
NR_CHDIR = 49, NR_CHROOT = 51, NR_FCHMODAT = 53, NR_FCHOWNAT = 54, NR_OPENAT = 56, NR_CLOSE = 57,
NR_CHDIR = 49, NR_CHROOT = 51, NR_FCHMODAT = 53, NR_FCHOWNAT = 54, NR_OPENAT = 56,
NR_READLINKAT = 78, NR_NEWFSTATAT = 79, NR_UTIMENSAT = 88, NR_BIND = 200, NR_CONNECT = 203,
NR_EXECVE = 221, NR_NAME_TO_HANDLE_AT = 264, NR_RENAMEAT2 = 276, NR_STATX = 291,
NR_OPENAT2 = 437, NR_FACCESSAT2 = 439, NR_FCHMODAT2 = 452, NR_SETREGID = 143, NR_SETGID = 144,
@@ -269,17 +269,17 @@ static i64 unmap_prefix(const struct gosys_hdr *h, char *buf, i64 n, int nul) {
// ---- Unix sockets ------------------------------------------------------------------------
static u64 put_dec(char *out, u64 v) {
char digits[20];
u64 n = 0;
do {
digits[n++] = (char)('0' + v % 10);
v /= 10;
} while (v);
for (u64 i = 0; i < n; i++) out[i] = digits[n - 1 - i];
return n;
static void put_hex16(char *out, u64 v) {
static const char digits[] = "0123456789abcdef";
for (int i = 15; i >= 0; i--) {
out[i] = digits[v & 15];
v >>= 4;
}
}
// bind() and connect() of a Unix socket with a path: mapped like hook/setup.c does, and hashed
// into /tmp when it does not fit the 108 bytes of sun_path (the same name, so a Go server and a
// program using the C library find each other)
static i64 with_sockaddr(const struct gosys_hdr *h, u32 *held, i64 nr, i64 a0, i64 a1, i64 a2) {
const unsigned char *sa = (const unsigned char *)a1;
if (!sa || a2 < 4 || a2 > 128 || sa[0] != 1 /* AF_UNIX */ || sa[1] != 0 || sa[2] != '/')
@@ -290,7 +290,7 @@ static i64 with_sockaddr(const struct gosys_hdr *h, u32 *held, i64 nr, i64 a0, i
if (max > 108) max = 108;
mcopy(orig, (const char *)sa + 2, max);
orig[max] = '\0';
char *rw = (char *)rw_path(h, orig, slot + SLOT_PATH_B, SLOT_PATH_CAP);
const char *rw = rw_path(h, orig, slot + SLOT_PATH_B, SLOT_PATH_CAP);
u64 rl = slen(rw);
char *out = slot + SLOT_ARGV;
out[0] = 1;
@@ -299,25 +299,18 @@ static i64 with_sockaddr(const struct gosys_hdr *h, u32 *held, i64 nr, i64 a0, i
mcopy(out + 2, rw, rl + 1);
return sc(nr, a0, (i64)out, (i64)(2 + rl + 1), 0, 0, 0);
}
u64 cut = rl;
while (cut > 0 && rw[cut - 1] != '/') cut--;
if (cut < 2 || cut == rl) return sc(nr, a0, a1, a2, 0, 0, 0);
rw[cut - 1] = '\0';
i64 dir = sc(NR_OPENAT, -100 /* AT_FDCWD */, (i64)rw, 0x200000 | 0x4000 | 0x80000 /* O_PATH | O_DIRECTORY | O_CLOEXEC */, 0, 0, 0);
if (dir < 0) return dir;
mcopy(out + 2, "/proc/self/fd/", 14);
u64 n = 14;
n += put_dec(out + 2 + n, (u64)dir);
out[2 + n++] = '/';
u64 name = rl - cut;
if (n + name + 1 > 108) {
sc(NR_CLOSE, dir, 0, 0, 0, 0, 0);
return -36 /* ENAMETOOLONG */;
u64 hash = 14695981039346656037ULL;
for (const char *p = rw; *p; p++) {
hash ^= (unsigned char)*p;
hash *= 1099511628211ULL;
}
mcopy(out + 2 + n, rw + cut, name + 1);
i64 r = sc(nr, a0, (i64)out, (i64)(2 + n + name + 1), 0, 0, 0);
sc(NR_CLOSE, dir, 0, 0, 0, 0, 0);
return r;
u64 n = h->root_len;
if (n + 5 + 11 + 16 + 1 > 108) return sc(nr, a0, a1, a2, 0, 0, 0);
mcopy(out + 2, h->root, n);
mcopy(out + 2 + n, "/tmp/.ctx_sock_", 15);
put_hex16(out + 2 + n + 15, hash);
out[2 + n + 15 + 16] = '\0';
return sc(nr, a0, (i64)out, (i64)(2 + n + 15 + 16 + 1), 0, 0, 0);
}
// ---- Starting programs -------------------------------------------------------------------
+5 -1
View File
@@ -113,6 +113,7 @@ int unlink(const char *pathname) {
if (!orig_unlink) orig_unlink = (int (*)(const char *))dlsym(RTLD_NEXT, "unlink");
char buf[PATH_MAX];
const char *target = rewrite_path(pathname, buf, sizeof(buf));
unlink_mapped_unix_socket(target);
dev_t hl_dev = 0;
ino_t hl_ino = 0;
int hardlinked = lookup_emulated_hardlink_at(AT_FDCWD, target, &hl_dev, &hl_ino);
@@ -136,7 +137,10 @@ int unlinkat(int dirfd, const char *pathname, int flags) {
dev_t hl_dev = 0;
ino_t hl_ino = 0;
int hardlinked = 0;
if (target) hardlinked = lookup_emulated_hardlink_at(dirfd, target, &hl_dev, &hl_ino);
if (target) {
unlink_mapped_unix_socket(target);
hardlinked = lookup_emulated_hardlink_at(dirfd, target, &hl_dev, &hl_ino);
}
int ret = orig_unlinkat ? orig_unlinkat(dirfd, target, flags) : -1;
if (ret != 0 && orig_unlinkat && refused_in_repaired_dir(dirfd, target)) {
hardlinked = target ? lookup_emulated_hardlink_at(dirfd, target, &hl_dev, &hl_ino) : 0;
+49 -33
View File
@@ -563,47 +563,63 @@ static const char *usrmerge_retry_path(const char *target, char *alt, size_t alt
return alt;
}
static int unix_socket_address(const char *sun_path, struct sockaddr_un *out, int *dir_fd) {
static const char *rewrite_unix_socket_path(const char *sun_path, char *out_buf, size_t out_size) {
if (!sun_path || out_size == 0) return sun_path;
// Abstract sockets start with null byte (\0); leave untouched
if (sun_path[0] == '\0') {
return sun_path;
}
char full[PATH_MAX];
*dir_fd = -1;
memset(out, 0, sizeof(*out));
out->sun_family = AF_UNIX;
const char *rw = (sun_path[0] == '/') ? rewrite_path(sun_path, full, sizeof(full)) : sun_path;
size_t rw_len = strlen(rw);
if (rw_len < sizeof(out->sun_path)) {
memcpy(out->sun_path, rw, rw_len + 1);
return 0;
// If rewritten path fits safely in sockaddr_un.sun_path (usually 108 bytes)
if (rw_len < out_size) {
strncpy(out_buf, rw, out_size - 1);
out_buf[out_size - 1] = '\0';
return out_buf;
}
const char *slash = strrchr(rw, '/');
if (!slash || slash == rw || !slash[1]) {
errno = ENAMETOOLONG;
return -1;
// Path exceeds sockaddr_un 108-byte limit!
// Deterministically hash the path into /tmp to guarantee it fits safely (< 80 bytes)
// while remaining 100% deterministic between server and client.
unsigned long long hash = 14695981039346656037ULL;
for (const char *p = rw; *p; p++) {
hash ^= (unsigned char)(*p);
hash *= 1099511628211ULL;
}
char dir[PATH_MAX];
size_t dir_len = (size_t)(slash - rw);
if (dir_len >= sizeof(dir)) {
errno = ENAMETOOLONG;
return -1;
init_ddnatv_hook();
if (g_ddnatv_root[0] != '\0') {
snprintf(out_buf, out_size, "%s/tmp/.ctx_sock_%016llx", g_ddnatv_root, hash);
} else {
snprintf(out_buf, out_size, "/tmp/.ctx_sock_%016llx", hash);
}
memcpy(dir, rw, dir_len);
dir[dir_len] = '\0';
int fd = (int)syscall(SYS_openat, AT_FDCWD, dir, O_PATH | O_DIRECTORY | O_CLOEXEC);
if (fd < 0) return -1;
int written = snprintf(out->sun_path, sizeof(out->sun_path), "/proc/self/fd/%d/%s", fd, slash + 1);
if (written < 0 || (size_t)written >= sizeof(out->sun_path)) {
syscall(SYS_close, fd);
errno = ENAMETOOLONG;
return -1;
}
*dir_fd = fd;
return 0;
out_buf[out_size - 1] = '\0';
return out_buf;
}
static void release_socket_dir(int dir_fd) {
if (dir_fd < 0) return;
int saved_errno = errno;
syscall(SYS_close, dir_fd);
errno = saved_errno;
static void unlink_mapped_unix_socket(const char *target) {
if (!target) return;
size_t t_len = strlen(target);
if (t_len >= 107) {
unsigned long long hash = 14695981039346656037ULL;
for (const char *p = target; *p; p++) {
hash ^= (unsigned char)(*p);
hash *= 1099511628211ULL;
}
char short_path[PATH_MAX];
init_ddnatv_hook();
if (g_ddnatv_root[0] != '\0') {
snprintf(short_path, sizeof(short_path), "%s/tmp/.ctx_sock_%016llx", g_ddnatv_root, hash);
} else {
snprintf(short_path, sizeof(short_path), "/tmp/.ctx_sock_%016llx", hash);
}
static int (*orig_unlink)(const char *) = NULL;
if (!orig_unlink) orig_unlink = (int (*)(const char *))dlsym(RTLD_NEXT, "unlink");
if (orig_unlink) orig_unlink(short_path);
}
}
#endif /* !DDNATV_HOST_TEST */
+8 -10
View File
@@ -109,13 +109,12 @@ int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
const struct sockaddr_un *sun = (const struct sockaddr_un *)addr;
if (sun->sun_path[0] != '\0') {
struct sockaddr_un mod_sun;
memset(&mod_sun, 0, sizeof(mod_sun));
mod_sun.sun_family = AF_UNIX;
char guest[sizeof(sun->sun_path) + 1];
copy_unix_path(sun, addrlen, guest);
int dir_fd;
if (unix_socket_address(guest, &mod_sun, &dir_fd) != 0) return -1;
int ret = orig_bind ? orig_bind(sockfd, (struct sockaddr *)&mod_sun, sizeof(mod_sun)) : -1;
release_socket_dir(dir_fd);
return ret;
rewrite_unix_socket_path(guest, mod_sun.sun_path, sizeof(mod_sun.sun_path));
return orig_bind ? orig_bind(sockfd, (struct sockaddr *)&mod_sun, sizeof(mod_sun)) : -1;
}
}
int ret = orig_bind ? orig_bind(sockfd, addr, addrlen) : -1;
@@ -140,15 +139,14 @@ int connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen) {
const struct sockaddr_un *sun = (const struct sockaddr_un *)addr;
if (sun->sun_path[0] != '\0') {
struct sockaddr_un mod_sun;
memset(&mod_sun, 0, sizeof(mod_sun));
mod_sun.sun_family = AF_UNIX;
char guest[sizeof(sun->sun_path) + 1];
copy_unix_path(sun, addrlen, guest);
char followed[PATH_MAX];
const char *path = guest[0] == '/' ? follow_guest_link(guest, followed, sizeof(followed)) : guest;
int dir_fd;
if (unix_socket_address(path, &mod_sun, &dir_fd) != 0) return -1;
int ret = orig_connect ? orig_connect(sockfd, (struct sockaddr *)&mod_sun, sizeof(mod_sun)) : -1;
release_socket_dir(dir_fd);
return ret;
rewrite_unix_socket_path(path, mod_sun.sun_path, sizeof(mod_sun.sun_path));
return orig_connect ? orig_connect(sockfd, (struct sockaddr *)&mod_sun, sizeof(mod_sun)) : -1;
}
}
+73 -3
View File
@@ -22,7 +22,7 @@ void wl_list_remove(struct wl_list *elm) {
int wl_list_empty(const struct wl_list *list) { return list->next == list; }
struct wl_listener { struct wl_list link; void (*notify)(struct wl_listener *, void *); };
struct wl_resource { struct wl_list listeners; int acquired, releases, errors; };
struct wl_resource { struct wl_list listeners; int acquired, releases, errors, in_flight; };
struct dmabuf_buffer { int unused; };
enum surface_role { ROLE_NONE, ROLE_TOPLEVEL, ROLE_SUBSURFACE };
struct surface {
@@ -30,6 +30,8 @@ struct surface {
struct wl_resource *dmabuf;
struct dmabuf_buffer *dmabuf_buf;
struct wl_listener dmabuf_destroy;
struct wl_resource *pixel;
struct wl_listener pixel_destroy;
int64_t next_release_ns;
int releases_pending;
enum surface_role role;
@@ -47,6 +49,7 @@ static int g_release_unpaced;
static int64_t g_now = 1000000000LL;
static int64_t g_armed;
static struct dmabuf_buffer g_buf;
static int g_late_dispatch;
static int64_t now_ns(void) { return g_now; }
static void perf_note_release(int64_t since) { (void)since; }
@@ -64,10 +67,16 @@ static int timerfd_settime(int fd, int flags, const struct itimerspec *its, stru
g_armed = (int64_t)its->it_value.tv_sec * 1000000000LL + its->it_value.tv_nsec;
return 0;
}
static void dispatch(struct wl_resource *b) {
for (; b->in_flight; b->in_flight--) {
if (b->acquired) b->acquired = 0;
else b->errors++;
}
}
static void send_buffer_release(struct wl_resource *b) {
b->releases++;
if (b->acquired) b->acquired = 0;
else b->errors++;
b->in_flight++;
if (!g_late_dispatch) dispatch(b);
}
#include "release.inc"
@@ -75,6 +84,7 @@ static void send_buffer_release(struct wl_resource *b) {
static void present(struct wl_resource *b) { b->acquired = 1; }
static void commit(struct surface *s, struct wl_resource *b) {
if (s->pixel != b) drop_pixel(s);
if (s->dmabuf != b) {
drop_dmabuf(s, 1);
if (b) {
@@ -87,6 +97,13 @@ static void commit(struct surface *s, struct wl_resource *b) {
pending_releases_apply(s);
}
static void commit_pixel(struct surface *s, struct wl_resource *b) {
if (s->pixel != b) drop_pixel(s);
drop_dmabuf(s, 1);
hold_pixel(s, b);
pending_releases_apply(s);
}
static void timer_fires(int64_t at) {
if (g_now < at) g_now = at;
release_due_buffers();
@@ -300,6 +317,57 @@ static void set_desync_frees_held_release(void) {
check(&d, 0);
}
static void backing_attached_every_frame(void) {
struct surface root, plane;
struct wl_resource black, a, b, c;
struct wl_resource *frames[3] = {&a, &b, &c};
surface_init(&root, ROLE_TOPLEVEL, NULL);
surface_init(&plane, ROLE_SUBSURFACE, &root);
buffer_init(&black); buffer_init(&a); buffer_init(&b); buffer_init(&c);
g_late_dispatch = 1;
for (int i = 0; i < 6; i += 2) {
present(&black); present(frames[i % 3]);
present(&black); present(frames[(i + 1) % 3]);
commit(&plane, frames[i % 3]); commit_pixel(&root, &black);
commit(&plane, frames[(i + 1) % 3]); commit_pixel(&root, &black);
timer_fires(g_now + 100000000LL);
dispatch(&black); dispatch(&a); dispatch(&b); dispatch(&c);
}
check(&black, 0);
check(&a, 2);
check(&b, 2);
check(&c, 1);
present(&a);
commit(&plane, NULL);
commit(&root, &a);
timer_fires(g_now + 100000000LL);
dispatch(&black); dispatch(&a); dispatch(&b); dispatch(&c);
check(&black, 1);
check(&a, 2);
check(&c, 2);
present(&black);
commit_pixel(&root, &black);
wl_list_remove(&root.link);
drop_pixel(&root);
dispatch(&black);
check(&black, 2);
}
static void backing_moved_to_another_surface(void) {
struct surface low, high;
struct wl_resource black;
surface_init(&low, ROLE_TOPLEVEL, NULL);
surface_init(&high, ROLE_TOPLEVEL, NULL);
buffer_init(&black);
present(&black);
commit_pixel(&low, &black);
commit_pixel(&high, &black);
commit(&low, NULL);
check(&black, 0);
commit(&high, NULL);
check(&black, 1);
}
int main(int argc, char **argv) {
assert(argc == 2);
wl_list_init(&g_surfaces);
@@ -313,6 +381,8 @@ int main(int argc, char **argv) {
else if (!strcmp(argv[1], "destroyed")) holder_destroyed();
else if (!strcmp(argv[1], "nested")) desync_parent_holds_sync_grandchild();
else if (!strcmp(argv[1], "set-desync")) set_desync_frees_held_release();
else if (!strcmp(argv[1], "backing")) backing_attached_every_frame();
else if (!strcmp(argv[1], "backing-moved")) backing_moved_to_another_surface();
else return 2;
return 0;
}
-144
View File
@@ -1,144 +0,0 @@
import os
from pathlib import Path
import subprocess
import tempfile
import unittest
from tools.tests.test_usr2_preload import runner
ROOT = Path(__file__).resolve().parents[2]
HOOK_BUILD = ROOT / "tools/nativeguest/build.sh"
LISTEN_SRC = ROOT / "app/src/main/cpp/waylandcomp/src/unix_listen.c"
SUN_PATH = 108
GUEST_SOCKET = "/run/droiddeck/wayland-0"
HOOKED = r"""
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
int main(int argc, char **argv) {
struct sockaddr_un addr = { .sun_family = AF_UNIX };
strncpy(addr.sun_path, argv[2], sizeof(addr.sun_path) - 1);
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (strcmp(argv[1], "serve") == 0) {
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0 || listen(fd, 4) != 0) {
perror("serve");
return 1;
}
puts("ready");
fflush(stdout);
int peer = accept(fd, NULL, NULL);
char got = 0;
if (peer < 0 || read(peer, &got, 1) != 1) return 1;
printf("got %c\n", got);
return 0;
}
if (connect(fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
perror("connect");
return 1;
}
return write(fd, "x", 1) == 1 ? 0 : 1;
}
"""
PLAIN_LISTENER = r"""
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include "unix_listen.h"
int main(int argc, char **argv) {
int fd = droiddeck_unix_listen(argv[1], "wayland-0", 4);
if (fd < 0) {
perror("listen");
return 1;
}
puts("ready");
fflush(stdout);
int peer = accept(fd, NULL, NULL);
char got = 0;
if (peer < 0 || read(peer, &got, 1) != 1) return 1;
printf("got %c\n", got);
return 0;
}
"""
class LongSocketPathTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.cc, cls.prefix = runner()
if cls.cc is None:
raise unittest.SkipTest("no aarch64 compiler and runner")
cls.tmp = tempfile.TemporaryDirectory()
tmp = Path(cls.tmp.name)
cls.hook = tmp / "hook.so"
subprocess.run(["bash", str(HOOK_BUILD), str(cls.hook), cls.cc], check=True, capture_output=True)
(tmp / "hooked.c").write_text(HOOKED)
(tmp / "listener.c").write_text(PLAIN_LISTENER)
subprocess.run([cls.cc, "-O1", "-o", str(tmp / "hooked"), str(tmp / "hooked.c")], check=True)
subprocess.run([cls.cc, "-O1", "-D_GNU_SOURCE", "-I", str(LISTEN_SRC.parent), "-o", str(tmp / "listener"),
str(tmp / "listener.c"), str(LISTEN_SRC)], check=True)
cls.hooked = tmp / "hooked"
cls.listener = tmp / "listener"
cls.root = tmp / "root"
cls.root.mkdir()
@classmethod
def tearDownClass(cls):
cls.tmp.cleanup()
def runtime_dir(self, long):
directory = Path(tempfile.mkdtemp(dir=self.tmp.name))
while long and len(str(directory)) + len("/wayland-0") < SUN_PATH + 10:
directory = directory / ("e" * 30)
directory.mkdir(parents=True, exist_ok=True)
return directory
def start(self, argv, hooked, directory):
env = {k: v for k, v in os.environ.items() if k != "LD_PRELOAD"}
hook_env = {"DDNATV_ROOT": str(self.root), "DDNATV_BINDS": f"{directory}:/run/droiddeck", "LD_PRELOAD": str(self.hook)}
cmd = list(self.prefix)
if hooked:
if self.prefix:
cmd += [arg for key, value in hook_env.items() for arg in ("-E", f"{key}={value}")]
else:
env.update(hook_env)
return subprocess.Popen(cmd + [str(a) for a in argv], env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
def exchange(self, directory, server_hooked):
server = self.start([self.hooked, "serve", GUEST_SOCKET] if server_hooked else [self.listener, directory], server_hooked, directory)
try:
self.assertEqual("ready", server.stdout.readline().strip(), server.stderr.read() if server.poll() is not None else "")
client = self.start([self.hooked, "connect", GUEST_SOCKET], True, directory)
_, client_err = client.communicate(timeout=30)
self.assertEqual(0, client.returncode, client_err)
out, err = server.communicate(timeout=30)
self.assertEqual(0, server.returncode, err)
self.assertIn("got x", out)
finally:
server.kill()
server.communicate()
def test_hooked_client_reaches_a_listener_at_a_path_longer_than_sun_path(self):
directory = self.runtime_dir(True)
self.assertGreaterEqual(len(str(directory)) + len("/wayland-0"), SUN_PATH)
self.exchange(directory, False)
def test_hooked_server_and_client_share_a_path_longer_than_sun_path(self):
directory = self.runtime_dir(True)
self.exchange(directory, True)
self.assertTrue((directory / "wayland-0").exists())
def test_a_path_that_fits_is_unchanged(self):
directory = self.runtime_dir(False)
self.assertLess(len(str(directory)) + len("/wayland-0"), SUN_PATH)
self.exchange(directory, True)
self.assertTrue((directory / "wayland-0").exists())
if __name__ == "__main__":
unittest.main()
+15 -1
View File
@@ -12,7 +12,7 @@ FUNCTIONS = (
'pending_release_free', 'pending_releases_forget_surface', 'pending_releases_cancel',
'pending_releases_apply', 'sync_holder', 'pending_releases_rehold', 'on_pending_release_buffer_destroyed',
'arm_release_timer', 'release_due_buffers', 'release_buffer', 'buffer_shown_elsewhere',
'drop_dmabuf',
'drop_dmabuf', 'on_pixel_destroyed', 'hold_pixel', 'drop_pixel',
)
@@ -83,6 +83,20 @@ class SubsurfaceReleaseTest(unittest.TestCase):
def test_set_desync_releases_what_the_old_parent_held(self):
self.check_case('set-desync')
def test_backing_attached_every_frame_is_released_once_when_replaced(self):
self.check_case('backing')
def test_backing_shown_on_two_surfaces_is_released_by_the_last(self):
self.check_case('backing-moved')
def test_compositor_holds_single_pixel_buffers_while_shown(self):
self.assertNotIn('send_buffer_release', function_body(self.source, 'take_pixel'))
commit = function_body(self.source, 'surface_commit')
self.assertLess(commit.index('if (s->pixel != buffer) drop_pixel(s);'), commit.index('if (s->role == ROLE_CURSOR)'))
self.assertLess(commit.index('take_pixel(s, pixel);'), commit.index('hold_pixel(s, buffer);'))
self.assertIn('drop_pixel(s);', function_body(self.source, 'surface_resource_destroy'))
self.assertIn('drop_pixel(s);', function_body(self.source, 'subsurface_resource_destroy'))
def test_compositor_attach_and_commit_use_the_release_hold(self):
self.assertIn('pending_releases_cancel(buffer);', function_body(self.source, 'take_dmabuf'))
commit = function_body(self.source, 'surface_commit')
-43
View File
@@ -1,43 +0,0 @@
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
#include <unistd.h>
#include "../../app/src/main/cpp/waylandcomp/src/unix_listen.h"
int main(int argc, char **argv) {
assert(argc == 3);
const char *dir = argv[1];
int expect_fits = atoi(argv[2]);
assert(droiddeck_unix_path_fits(dir, "wayland-0") == (expect_fits != 0));
int listener = droiddeck_unix_listen(dir, "wayland-0", 4);
assert(listener >= 0);
char path[4096];
snprintf(path, sizeof(path), "%s/wayland-0", dir);
struct stat st;
assert(stat(path, &st) == 0 && S_ISSOCK(st.st_mode));
int dir_fd = open(dir, O_PATH | O_DIRECTORY | O_CLOEXEC);
assert(dir_fd >= 0);
struct sockaddr_un addr = { .sun_family = AF_UNIX };
snprintf(addr.sun_path, sizeof(addr.sun_path), "/proc/self/fd/%d/wayland-0", dir_fd);
int client = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
assert(client >= 0 && connect(client, (struct sockaddr *)&addr, sizeof(addr)) == 0);
int peer = accept(listener, NULL, NULL);
assert(peer >= 0);
assert(write(client, "x", 1) == 1);
char got = 0;
assert(read(peer, &got, 1) == 1 && got == 'x');
assert(droiddeck_unix_listen(dir, "wayland-0", 4) < 0);
assert(droiddeck_unix_listen("/nonexistent-dir", "wayland-0", 4) < 0);
puts("PASS: unix listen");
return 0;
}
-53
View File
@@ -1,53 +0,0 @@
import pathlib
import shutil
import subprocess
import sys
import tempfile
import unittest
ROOT = pathlib.Path(__file__).resolve().parents[2]
SRC = ROOT / 'app/src/main/cpp/waylandcomp/src'
SUN_PATH = 108
@unittest.skipUnless(sys.platform.startswith('linux') and shutil.which('cc'), 'requires a Linux C compiler')
class UnixListenTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.folder = tempfile.TemporaryDirectory()
cls.binary = pathlib.Path(cls.folder.name) / 'unix-listen-test'
subprocess.run([
'cc', '-std=c11', '-D_GNU_SOURCE', '-Wall', '-Wextra', '-Werror',
str(ROOT / 'tools/tests/test_unix_listen.c'), str(SRC / 'unix_listen.c'),
'-o', str(cls.binary),
], check=True)
@classmethod
def tearDownClass(cls):
cls.folder.cleanup()
def run_in(self, minimum):
with tempfile.TemporaryDirectory() as base:
directory = pathlib.Path(base)
while len(str(directory)) + len('/wayland-0') < minimum:
directory = directory / ('e' * 30)
directory.mkdir(parents=True, exist_ok=True)
fits = len(str(directory)) + len('/wayland-0') < SUN_PATH
result = subprocess.run([str(self.binary), str(directory), '1' if fits else '0'],
capture_output=True, text=True, timeout=10)
return directory, result
def test_a_path_that_fits_binds_directly(self):
directory, result = self.run_in(0)
self.assertEqual(0, result.returncode, result.stderr)
self.assertIn('PASS: unix listen', result.stdout)
def test_a_path_longer_than_sun_path_still_listens_and_accepts(self):
directory, result = self.run_in(140)
self.assertGreaterEqual(len(str(directory)) + len('/wayland-0'), SUN_PATH)
self.assertEqual(0, result.returncode, result.stderr)
self.assertIn('PASS: unix listen', result.stdout)
if __name__ == '__main__':
unittest.main()
+7 -6
View File
@@ -234,28 +234,29 @@ static void upload_timeout(int single_pixel) {
struct wl_shm_buffer b = {.w = img->w, .h = img->h, .stride = img->w * 4, .data = data};
struct wl_resource resource = {0};
expected_release = &resource;
if (single_pixel) take_pixel(&s, data, &resource); else take_shm(&s, &b, &resource);
assert(bytes[3] == 0x3c && bytes[0] == 0xa5 && releases == 1);
const unsigned per_take = single_pixel ? 0 : 1;
if (single_pixel) take_pixel(&s, data); else take_shm(&s, &b, &resource);
assert(bytes[3] == 0x3c && bytes[0] == 0xa5 && releases == per_take);
memcpy(saved, bytes, sizeof(saved));
struct vkp_image dst = {.image = (VkImage)(uintptr_t)9, .blit_dst = 1, .w = img->w, .h = img->h};
fence_answer = VK_TIMEOUT;
assert(vkp_blit_image(img, &dst, -1) == -1);
assert(g_dev_state == -3 && submits == 1 && waits == 1);
memset(data, 0x7e, sizeof(data));
if (single_pixel) take_pixel(&s, data, &resource); else take_shm(&s, &b, &resource);
assert(s.shm_img == img && memcmp(saved, bytes, sizeof(saved)) == 0 && releases == 2);
if (single_pixel) take_pixel(&s, data); else take_shm(&s, &b, &resource);
assert(s.shm_img == img && memcmp(saved, bytes, sizeof(saved)) == 0 && releases == 2 * per_take);
struct wl_resource dmabuf = {.dmabuf = 1};
expected_release = &dmabuf;
assert(buffer_release_blocked(&dmabuf));
send_buffer_release(&dmabuf);
assert(releases == 2);
assert(releases == 2 * per_take);
vkp_image_destroy(img);
assert(destroys == 0 && img->map == bytes && img->image && img->mem);
assert_stopped();
g_dev_state = 1;
assert(!buffer_release_blocked(&dmabuf));
send_buffer_release(&dmabuf);
assert(releases == 3);
assert(releases == 2 * per_take + 1);
free(img); free(g_images); /* fake resources only, not production cleanup */
}