mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 12:35:42 +00:00
167 lines
7.6 KiB
Python
167 lines
7.6 KiB
Python
"""Exercise production receiver and attachment replacement with Unix FD passing.
|
|
|
|
This Linux fixture stubs Wayland resources and only the success/status values of
|
|
SYNC_IOC_FILE_INFO. It does not claim a Vulkan or SurfaceFlinger hardware test.
|
|
"""
|
|
import os
|
|
from pathlib import Path
|
|
import platform
|
|
import subprocess
|
|
import tempfile
|
|
import unittest
|
|
|
|
from test_vk_pending_completion import function
|
|
|
|
SRC = Path(__file__).resolve().parents[2] / 'app/src/main/cpp/waylandcomp/src'
|
|
HEAD = r'''
|
|
#define _GNU_SOURCE
|
|
#include <assert.h>
|
|
#include <errno.h>
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include <string.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/ioctl.h>
|
|
#include <linux/sync_file.h>
|
|
static int query_status, query_result;
|
|
static unsigned query_count;
|
|
static int test_ioctl(int fd, unsigned long request, void *arg) {
|
|
assert(fd >= 0 && request == SYNC_IOC_FILE_INFO);
|
|
struct sync_file_info *info = arg;
|
|
assert(!info->flags && !info->sync_fence_info);
|
|
query_count++;
|
|
info->status = query_status;
|
|
info->num_fences = 1;
|
|
return query_result;
|
|
}
|
|
#define ioctl test_ioctl
|
|
#include "frame_acquire.h"
|
|
#undef ioctl
|
|
#include "wayland-util.h"
|
|
void wl_list_init(struct wl_list *list) { list->prev = list->next = list; }
|
|
void wl_list_remove(struct wl_list *list) { (void)list; }
|
|
struct wl_listener { struct wl_list link; void (*notify)(struct wl_listener *, void *); };
|
|
struct wl_client { int identity; };
|
|
struct wl_resource { struct wl_client *owner; void *data; int type; unsigned error; };
|
|
struct surface {
|
|
struct wl_resource *pending_buffer;
|
|
struct wl_listener pending_buffer_destroy;
|
|
struct dd_frame_acquire pending_frame_acquire;
|
|
int pending_attach;
|
|
};
|
|
static int wl_surface_interface, surface_impl;
|
|
static int peer_allowed=1;
|
|
static int frame_acquire_peer_allowed(const struct wl_client *c, bool verify_exec, bool bind) { (void)c; (void)verify_exec; (void)bind; return peer_allowed; }
|
|
static struct wl_client *wl_resource_get_client(struct wl_resource *r) { return r->owner; }
|
|
static void *wl_resource_get_user_data(struct wl_resource *r) { return r->data; }
|
|
static int wl_resource_instance_of(struct wl_resource *r, const void *iface, const void *impl) {
|
|
assert(iface == &wl_surface_interface && impl == &surface_impl);
|
|
return r->type == 1;
|
|
}
|
|
static int get_dmabuf(struct wl_resource *r) { return r->type == 2; }
|
|
static void wl_resource_add_destroy_listener(struct wl_resource *r, struct wl_listener *l) { (void)r; (void)l; }
|
|
static void wl_resource_post_error(struct wl_resource *r, unsigned err, const char *why) {
|
|
assert(why && !r->error);
|
|
r->error = err+1;
|
|
}
|
|
'''
|
|
TAIL = r'''
|
|
/* Real descriptor transfer and sender close; before dispatch, receiver owns it. */
|
|
static int tx(int fd) {
|
|
int conn[2]; char byte = 'F';
|
|
assert(socketpair(AF_UNIX, SOCK_DGRAM, 0, conn) == 0);
|
|
union { struct cmsghdr a; char bytes[CMSG_SPACE(sizeof(fd))]; } control = {0};
|
|
struct iovec payload = {.iov_base=&byte, .iov_len=1};
|
|
struct msghdr msg = {.msg_iov=&payload, .msg_iovlen=1, .msg_control=control.bytes,
|
|
.msg_controllen=sizeof(control.bytes)};
|
|
struct cmsghdr *h = CMSG_FIRSTHDR(&msg);
|
|
h->cmsg_len=CMSG_LEN(sizeof(fd)); h->cmsg_level=SOL_SOCKET; h->cmsg_type=SCM_RIGHTS;
|
|
memcpy(CMSG_DATA(h), &fd, sizeof(fd));
|
|
assert(sendmsg(conn[0], &msg, 0) == 1);
|
|
assert(close(fd) == 0);
|
|
memset(&control, 0, sizeof(control));
|
|
assert(recvmsg(conn[1], &msg, MSG_CMSG_CLOEXEC) == 1);
|
|
assert(!(msg.msg_flags & (MSG_CTRUNC | MSG_TRUNC)) && byte == 'F');
|
|
h=CMSG_FIRSTHDR(&msg);
|
|
assert(h && h->cmsg_len==CMSG_LEN(sizeof(fd)) && h->cmsg_level==SOL_SOCKET && h->cmsg_type==SCM_RIGHTS);
|
|
memcpy(&fd, CMSG_DATA(h), sizeof(fd));
|
|
close(conn[0]); close(conn[1]);
|
|
return fd;
|
|
}
|
|
static int transmit_fd(void) {
|
|
int p[2]; assert(pipe(p) == 0); close(p[1]); return tx(p[0]);
|
|
}
|
|
static void closed(int fd) {
|
|
assert(fcntl(fd, F_GETFD) == -1 && errno == EBADF);
|
|
}
|
|
int main(void) {
|
|
struct wl_client client={1}, unrelated={2};
|
|
struct surface s={0};
|
|
struct wl_resource manager={.owner=&client}, surf={.owner=&client,.data=&s,.type=1};
|
|
struct wl_resource buf={.owner=&client,.type=2}, other={.owner=&client,.type=2};
|
|
struct wl_resource wrong_owner={.owner=&unrelated,.type=2}, not_dmabuf={.owner=&client,.type=3};
|
|
int fd=transmit_fd();
|
|
frame_acquire_set(&client,&manager,&surf,&buf,fd);
|
|
assert(manager.error == 1 && !query_count); closed(fd);
|
|
|
|
surface_attach(&client,&surf,&buf,0,0);
|
|
struct wl_resource *bad[] = {&other, &wrong_owner, ¬_dmabuf, NULL};
|
|
for (unsigned i=0; i<sizeof(bad)/sizeof(*bad); i++) {
|
|
fd=transmit_fd(); manager.error=0;
|
|
frame_acquire_set(&client,&manager,&surf,bad[i],fd);
|
|
assert(manager.error == 1 && !query_count); closed(fd);
|
|
}
|
|
manager.error=0; query_status=1; fd=transmit_fd();
|
|
frame_acquire_set(&client,&manager,&surf,&buf,fd);
|
|
assert(!manager.error && query_count==1 && s.pending_frame_acquire.fd_plus_one==fd+1);
|
|
int display_fd=dd_frame_acquire_dup(&s.pending_frame_acquire);
|
|
assert(display_fd>=0 && display_fd!=fd && (fcntl(display_fd,F_GETFD)&FD_CLOEXEC));
|
|
close(display_fd);
|
|
int duplicate=transmit_fd();
|
|
frame_acquire_set(&client,&manager,&surf,&buf,duplicate);
|
|
assert(manager.error==2 && query_count==1); closed(duplicate);
|
|
|
|
/* The same Wayland buffer, attached a second time, is a different write. */
|
|
surface_attach(&client,&surf,&buf,0,0);
|
|
assert(dd_frame_acquire_dup(&s.pending_frame_acquire)==-1); closed(fd);
|
|
for (int status=0; status>=-2; status--) {
|
|
manager.error=0; query_status=status;
|
|
if (status==-2) query_result=-1; /* non-sync file ioctl failure */
|
|
fd=transmit_fd();
|
|
frame_acquire_set(&client,&manager,&surf,&buf,fd);
|
|
assert(manager.error == 3 && !s.pending_frame_acquire.fd_plus_one); closed(fd);
|
|
}
|
|
manager.error=0; query_result=0; query_status=1;
|
|
fd=transmit_fd();
|
|
frame_acquire_set(&client,&manager,&surf,&buf,fd);
|
|
assert(!manager.error);
|
|
on_pending_buffer_destroyed(&s.pending_buffer_destroy,NULL);
|
|
assert(!s.pending_buffer && !s.pending_frame_acquire.fd_plus_one); closed(fd);
|
|
peer_allowed=0; manager.error=0;
|
|
surface_attach(&client,&surf,&buf,0,0);
|
|
fd=transmit_fd();
|
|
frame_acquire_set(&client,&manager,&surf,&buf,fd);
|
|
assert(manager.error==4&&!s.pending_frame_acquire.fd_plus_one);closed(fd);
|
|
return 0;
|
|
}
|
|
'''
|
|
|
|
|
|
@unittest.skipUnless(platform.system() == 'Linux', 'Linux sync_file UAPI')
|
|
class FrameAcquireProtocolTest(unittest.TestCase):
|
|
def test_production_receiver_rejects_invalid_and_keeps_exact_attachment(self):
|
|
comp = (SRC / 'compositor.c').read_text()
|
|
protocol = (SRC.parent / 'generated/droiddeck-frame-acquire-v1-server-protocol.h').read_text()
|
|
errors = protocol.split('enum droiddeck_frame_acquire_v1_error {', 1)[1].split('};', 1)[0]
|
|
actual = '\n'.join(function(comp, name) for name in (
|
|
'on_pending_buffer_destroyed', 'surface_attach', 'frame_acquire_set'))
|
|
with tempfile.TemporaryDirectory(prefix='droiddeck-frame-acquire-') as tmp:
|
|
probe = Path(tmp) / 'probe.c'
|
|
exe = Path(tmp) / 'probe'
|
|
probe.write_text(HEAD + '\nenum droiddeck_frame_acquire_v1_error {' + errors + '};\n' + actual + TAIL)
|
|
subprocess.run([os.environ.get('CC', 'cc'), '-std=gnu11', '-Wall', '-Wextra',
|
|
'-Werror', '-Wno-unused-parameter', '-UNDEBUG', '-fsanitize=address,undefined',
|
|
'-I', str(SRC), '-I', str(SRC.parent / 'prebuilt/include'),
|
|
str(probe), '-o', str(exe)], check=True)
|
|
subprocess.run([str(exe)], check=True, timeout=10)
|