XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
xnu-drm: fence lifetime and error fixes for the sandbox paths, and a fence test
Heyang Zhouf694a9014786
diff --git a/LICENSE b/LICENSEindex de08783..3c792c9 100644--- a/LICENSE+++ b/LICENSE@@ -6,7 +6,7 @@ Each source file names its license in an SPDX-License-Identifier line; the fullAPSL-2.0 xnu-ko/, xnu-patches/, kexts/BSD-2-Clause-Patent kexts/XnuDisplay/, kexts/XnuEthernet/, kexts/XnuInput/ (from macOS-DroidVM),gpu/xnu-cpu-metal/src/xnu-cpu-metal-bundle/XnuCpuMetalShim.h (from UniXNU/AdrenoMetal)- MIT gpu/xnu-drm/darwin/, gpu/xnu-drm/xnudrm_proto.h; the patches to Mesa and virglrenderer+ MIT gpu/xnu-drm/darwin/, gpu/xnu-drm/test/, gpu/xnu-drm/xnudrm_proto.h; the patches to Mesa and virglrenderer(under their upstream's license, as are the patches to metal2vulkan: GPL-2.0-or-later)docs/images/ holds screenshots of macOS running on xnulinux; the macOS interface and wallpaper in them are Apple's.diff --git a/gpu/venus/patches/0005-venus-private-fence-fds.patch b/gpu/venus/patches/0005-venus-private-fence-fds.patchnew file mode 100644index 0000000..ab8ab0d--- /dev/null+++ b/gpu/venus/patches/0005-venus-private-fence-fds.patch@@ -0,0 +1,107 @@+Upstream: Mesa 26.3.1 (https://archive.mesa3d.org/mesa-26.3.1.tar.xz)+Upstream-License: MIT+Patch-License: MIT (provided under the same license as upstream)+Subject: venus: fence fds over the xnulinux DRM bridge are private, and their errors are errors++A fence fd is a stand-in that only this driver can wait on (gpu/xnu-drm/darwin), so no external sync fds are offered over+the bridge. A poll() on one that fails is no answer: report it (VK_ERROR_DEVICE_LOST) instead of waiting on.++--- a/src/virtio/vulkan/vn_renderer_sim_syncobj.c++++ b/src/virtio/vulkan/vn_renderer_sim_syncobj.c+@@ -89,7 +89,8 @@+ return 0;+ }++-static bool++/* 1 signaled, 0 not yet, -errno when it cannot be told */++static int+ sim_syncobj_poll(int fd, int poll_timeout)+ {+ struct pollfd pollfd = {+@@ -101,19 +102,27 @@+ ret = poll(&pollfd, 1, poll_timeout);+ } while (ret == -1 && (errno == EINTR || errno == EAGAIN));+++ if (ret < 0)++ return -errno;+++ return ret > 0 && (pollfd.revents & POLLIN);+ }++-static void++static int+ sim_syncobj_update_locked(struct sim_syncobj *syncobj, int poll_timeout)+ {+ if (syncobj->pending_fd >= 0) {+- if (sim_syncobj_poll(syncobj->pending_fd, poll_timeout)) {++ const int ret = sim_syncobj_poll(syncobj->pending_fd, poll_timeout);++ if (ret < 0)++ return ret;++ if (ret) {+ close(syncobj->pending_fd);+ syncobj->pending_fd = -1;+ syncobj->signaled = true;+ }+ }++++ return 0;+ }++ static struct sim_syncobj *+@@ -171,9 +180,13 @@+ return -1;++ mtx_lock(&syncobj->mutex);+- sim_syncobj_update_locked(syncobj, 0);++ const int ret = sim_syncobj_update_locked(syncobj, 0);+ points[i] = syncobj->signaled;+ mtx_unlock(&syncobj->mutex);++ if (ret < 0) {++ errno = -ret;++ return ret;++ }+ }++ return 0;+@@ -203,8 +216,14 @@+ return -1;++ mtx_lock(&syncobj->mutex);+- if (!syncobj->signaled)+- sim_syncobj_update_locked(syncobj, poll_timeout);++ const int ret = syncobj->signaled++ ? 0++ : sim_syncobj_update_locked(syncobj, poll_timeout);++ if (ret < 0) {++ mtx_unlock(&syncobj->mutex);++ errno = -ret;++ return ret;++ }++ if (!syncobj->signaled) {+ if (!wait_all && i < num_handles - 1 && syncobj->pending_fd < 0) {+--- a/src/virtio/vulkan/vn_renderer_virtgpu.c++++ b/src/virtio/vulkan/vn_renderer_virtgpu.c+@@ -320,6 +320,10 @@+ ret = gpu->sync->query(gpu->sync, &sync->syncobj_handle, val, 1, 0);+ } else {+ ret = gpu->sync->wait(gpu->sync, &sync->syncobj_handle, 1, 0, 0, NULL);++#ifdef VN_XNUDRM /* (a DRM syncobj with no fence yet is EINVAL here) */++ if (ret && errno != ETIME)++ return VK_ERROR_DEVICE_LOST;++#endif+ *val = !ret;+ ret = 0;+ }+@@ -889,7 +893,11 @@+ }++ info->has_dma_buf_import = true;++#ifdef VN_XNUDRM /* a fence fd means something to this driver only: nothing to export or import */++ info->has_external_sync = false;++#else+ info->has_external_sync = true;++#endif++ assert(gpu->sync);+ info->has_timeline_sync =diff --git a/gpu/xnu-drm/darwin/xnudrm.c b/gpu/xnu-drm/darwin/xnudrm.cindex 2fa181d..fa7aa6a 100644--- a/gpu/xnu-drm/darwin/xnudrm.c+++ b/gpu/xnu-drm/darwin/xnudrm.c@@ -55,12 +55,30 @@ extern int os_dupfd_cloexec(int fd);/* Open a harmless placeholder fd for a render node or sandbox fence.* Strict sandboxes (e.g. com.apple.WebKit.GPUProcess.sb) deny open("/dev/null")* with EPERM while allowing read-only opens of /private/etc/passwd. */-static int open_dummy_fd(void)+static int open_dummy_fd(void) /* under lk */{- int fd = open("/dev/null", O_RDWR | O_CLOEXEC);- if (fd < 0 && errno == EPERM)- fd = open("/etc/passwd", O_RDONLY | O_CLOEXEC);- return fd;+ static int denied; /* a sandbox is for good: ask (and be logged) once */+ if (!denied) {+ int fd = open("/dev/null", O_RDWR | O_CLOEXEC);+ if (fd >= 0 || errno != EPERM) return fd;+ denied = 1;+ }+ return open("/etc/passwd", O_RDONLY | O_CLOEXEC);+}++/* A new descriptor has this number: an entry still there is of one closed behind our back (an exported fence fd, closed by+ * the application itself), and would answer for the new one. No host call here (buf may hold a reply being copied out):+ * an unsignalled fence stays with xnudrmd until the node closes. Under lk. */+static void forget_sfd(int fd)+{+ for (unsigned i = 0; i < nsfds; i++) {+ if (sfds[i].fd == fd) {+ struct xsfence *sf = sfds[i].f;+ sfds[i] = sfds[--nsfds];+ if (--sf->refs == 0) free(sf);+ return;+ }+ }}static int debug(void) { static int d = -1; if (d < 0) d = getenv("XNUDRM_DEBUG") != NULL; return d; }@@ -138,11 +156,10 @@ int xnudrm_close(int fd)for (unsigned j = 0; j < nfences; j++) {if (fences[j].conn == c && fences[j].id) {fences[j].id = 0;- if (fences[j].wfd >= 0) {- (void)!write(fences[j].wfd, "s", 1);- close(fences[j].wfd);- fences[j].wfd = -1;- }+ /* end of file makes the read end readable; no write: without a reader left (a fence+ * nobody waited for) it would raise SIGPIPE */+ if (fences[j].wfd >= 0) close(fences[j].wfd);+ fences[j].wfd = -1;}}for (unsigned j = 0; j < nsfds; j++)@@ -172,13 +189,22 @@ int xnudrm_close(int fd)int xnudrm_dupfd_cloexec(int fd){- int nfd = os_dupfd_cloexec(fd);- /* Strict sandboxes (e.g. com.apple.WebKit.GPUProcess.sb) deny fcntl(F_DUPFD_CLOEXEC)- * with EPERM while allowing SYS_dup. Note: dup() drops FD_CLOEXEC and minfd=3. */- if (nfd < 0 && errno == EPERM)- nfd = dup(fd);- if (nfd < 0) return -1;+ static int denied;pthread_mutex_lock(&lk);+ /* Strict sandboxes (e.g. com.apple.WebKit.GPUProcess.sb) deny fcntl(F_DUPFD_CLOEXEC)+ * with EPERM while allowing SYS_dup (and F_SETFD). */+ int nfd = denied ? -1 : os_dupfd_cloexec(fd);+ if (denied || (nfd < 0 && errno == EPERM)) {+ int low[3], nlow = 0;+ denied = 1;+ while ((nfd = dup(fd)) >= 0 && nfd < 3) low[nlow++] = nfd; /* never stdin, stdout or stderr */+ int e = errno;+ while (nlow) close(low[--nlow]);+ if (nfd >= 0 && fcntl(nfd, F_SETFD, FD_CLOEXEC)) { e = errno; close(nfd); nfd = -1; }+ errno = e;+ }+ if (nfd < 0) { pthread_mutex_unlock(&lk); return -1; }+ forget_sfd(nfd);for (unsigned i = 0; i < nsfds; i++) {if (sfds[i].fd == fd) {struct xsfence *sf = sfds[i].f;@@ -227,8 +253,9 @@ static int put(uint32_t *plen, uint64_t *field, const void *p, uint64_t len)static int new_fence(uint64_t id) /* for conn */{+ static int denied;int p[2];- if (pipe(p) == 0) {+ if (!denied && pipe(p) == 0) {fcntl(p[0], F_SETFD, FD_CLOEXEC); fcntl(p[1], F_SETFD, FD_CLOEXEC);struct stat st; fstat(p[0], &st);/* a pipe's inode number is reused once every descriptor of an earlier pipe is closed: an entry for that dead pipe@@ -242,20 +269,22 @@ static int new_fence(uint64_t id) /* for conn */} else i++;}struct xfence *nf = realloc(fences, (nfences + 1) * sizeof(*fences));- if (!nf) { close(p[0]); close(p[1]); return -1; }+ if (!nf) { close(p[0]); close(p[1]); errno = ENOMEM; return -1; }fences = nf;fences[nfences++] = (struct xfence){ st.st_dev, st.st_ino, p[1], id, conn };return p[0];}- if (errno != EPERM) return -1;+ if (!denied && errno != EPERM) return -1;+ denied = 1;/* Strict sandboxes (e.g. com.apple.WebKit.GPUProcess.sb) deny SYS_pipe:* use a dummy fd and track fence identity and readiness in userspace. */int fd = open_dummy_fd();if (fd < 0) return -1;struct xsfence *sf = malloc(sizeof(*sf));struct xsfd *ns = sf ? realloc(sfds, (nsfds + 1) * sizeof(*sfds)) : NULL;- if (!ns) { free(sf); close(fd); return -1; }+ if (!ns) { free(sf); close(fd); errno = ENOMEM; return -1; }sfds = ns;+ forget_sfd(fd);*sf = (struct xsfence){ id, conn, 1 };sfds[nsfds++] = (struct xsfd){ fd, sf };return fd;@@ -265,7 +294,7 @@ static int do_ioctl(unsigned long request, void *arg){uint32_t nr = request & 0xff, size = (request >> 16) & 0x1fff, plen = 0;uint8_t *pl = buf + sizeof(struct xnudrm_hdr);- uint64_t f0 = 0, f1 = 0;+ uint64_t f0 = 0, f1 = 0, lost = 0;int r;if (size > 256) return -EINVAL;@@ -339,7 +368,11 @@ static int do_ioctl(unsigned long request, void *arg)if (nr == DRM_COMMAND_BASE + DRM_VIRTGPU_EXECBUFFER) {struct drm_virtgpu_execbuffer *a = (void *)pl, *s = (void *)saved;a->command = s->command; a->bo_handles = s->bo_handles;- if (!r && (a->flags & VIRTGPU_EXECBUF_FENCE_FD_OUT)) a->fence_fd = new_fence((uint32_t)a->fence_fd);+ if (!r && (a->flags & VIRTGPU_EXECBUF_FENCE_FD_OUT)) {+ uint32_t id = (uint32_t)a->fence_fd;+ errno = EMFILE;+ if ((a->fence_fd = new_fence(id)) < 0) { r = -errno; lost = id; }+ }if (debug()) fprintf(stderr, "xnudrm: execbuffer flags %#x size %u ring %u -> %d, fence fd %d\n", a->flags, a->size, a->ring_idx, r, a->fence_fd);}break;@@ -347,6 +380,7 @@ static int do_ioctl(unsigned long request, void *arg)}(void)f1;memcpy(arg, pl, size);+ if (lost) { hdr(XNUDRM_FENCE_CLOSE, lost, 0); call(0); } /* no descriptor for it here (buf is free now) */return r;}@@ -448,25 +482,28 @@ static struct xsfence *sfd_of(int fd)return NULL;}-/* 1 signalled, 0 not yet, <0 error. Called with lk held. */+/* 1 signalled, 0 not yet, <0 error (not known to be signalled: the fence is kept). Called with lk held. */static int fence_wait_id(uint64_t *id, io_connect_t c){if (!*id) return 1;conn = c;hdr(XNUDRM_FENCE_WAIT, *id, 0);int r = call(0);- if (r == -XNUDRM_ETIME) return 0;- if (debug() && r) fprintf(stderr, "xnudrm: fence %llu wait: %d\n", (unsigned long long)*id, r);+ if (r == -XNUDRM_ETIME || r == -EINTR) return 0;+ if (r) {+ if (debug()) fprintf(stderr, "xnudrm: fence %llu wait: %d\n", (unsigned long long)*id, r);+ return -EIO;+ }hdr(XNUDRM_FENCE_CLOSE, *id, 0);call(0);*id = 0;- return r ? r : 1;+ return 1;}static int fence_check(struct xfence *f){int r = fence_wait_id(&f->id, f->conn);- if (r && f->wfd >= 0) {+ if (r > 0 && f->wfd >= 0) {(void)!write(f->wfd, "s", 1); /* signalled for good */close(f->wfd);f->wfd = -1;@@ -477,25 +514,33 @@ static int fence_check(struct xfence *f)int xnudrm_poll(struct pollfd *fds, nfds_t n, int timeout){struct timespec t0, now;+ struct pollfd rest[n + 1]; /* fds without the sandbox fences */long delay_ns = 100000; /* 100 us -> 2 ms */clock_gettime(CLOCK_MONOTONIC, &t0);for (;;) {- int pending = 0, n_sf = 0, n_valid = 0;+ int pending = 0, ready = 0, err = 0, n_sf = 0, n_rest = 0;pthread_mutex_lock(&lk);for (nfds_t i = 0; i < n; i++) {+ rest[i] = fds[i];+ fds[i].revents = 0;if (fds[i].fd < 0) continue;- n_valid++;struct xsfence *sf = nsfds ? sfd_of(fds[i].fd) : NULL;+ int r;if (sf) {n_sf++;- if (sf->id && fence_wait_id(&sf->id, sf->conn) == 0) pending = 1;- continue;+ rest[i].fd = -1;+ r = fence_wait_id(&sf->id, sf->conn);+ if (r > 0 && (fds[i].revents = fds[i].events & (POLLIN | POLLRDNORM))) ready++;+ } else {+ struct xfence *f = nfences ? fence_of(fds[i].fd) : NULL;+ n_rest++;+ r = f && f->id ? fence_check(f) : 1;}- struct xfence *f = nfences ? fence_of(fds[i].fd) : NULL;- if (f && f->id && fence_check(f) == 0) pending = 1;+ if (r == 0) pending = 1;+ if (r < 0) err = -r;}if (debug()) {- fprintf(stderr, "xnudrm: poll %u fds, timeout %d: %s;", (unsigned)n, timeout, pending ? "pending" : "ready");+ fprintf(stderr, "xnudrm: poll %u fds, timeout %d: %s;", (unsigned)n, timeout, err ? "error" : pending ? "pending" : "ready");for (nfds_t i = 0; i < n; i++) {struct xsfence *sf = nsfds ? sfd_of(fds[i].fd) : NULL;struct xfence *f = (!sf && nfences) ? fence_of(fds[i].fd) : NULL;@@ -505,27 +550,18 @@ int xnudrm_poll(struct pollfd *fds, nfds_t n, int timeout)fprintf(stderr, "\n");}pthread_mutex_unlock(&lk);- if (!pending) {- if (n_sf > 0 && n_sf == n_valid) {- /* All non-negative fds are sandbox fences; SYS_poll is denied in strict sandboxes. */- int ready = 0;- for (nfds_t i = 0; i < n; i++) {- fds[i].revents = (fds[i].fd >= 0) ? (fds[i].events & (POLLIN | POLLRDNORM)) : 0;- if (fds[i].revents) ready++;- }- return ready;- }- return poll(fds, n, timeout); /* signalled pipe fences are readable now */+ if (err) { errno = err; return -1; }+ if (!n_sf && !pending) return poll(fds, n, timeout); /* signalled pipe fences are readable now */+ if (n_rest) { /* not for sandbox fences: where there are any, SYS_poll is denied */+ int r = poll(rest, n, 0);+ if (r < 0 && !ready) return -1;+ for (nfds_t i = 0; r > 0 && i < n; i++) if (rest[i].fd >= 0) fds[i].revents = rest[i].revents;+ if (r > 0) ready += r;}+ if (ready) return ready;clock_gettime(CLOCK_MONOTONIC, &now);long ms = (now.tv_sec - t0.tv_sec) * 1000 + (now.tv_nsec - t0.tv_nsec) / 1000000;- if (timeout >= 0 && ms >= timeout) {- if (n_sf > 0 && n_sf == n_valid) {- for (nfds_t i = 0; i < n; i++) fds[i].revents = 0;- return 0;- }- return poll(fds, n, 0);- }+ if (timeout >= 0 && ms >= timeout) return 0;/* each check stops the hosted CPU for one host call: back off exponentially up to 2 ms */struct timespec d = { 0, delay_ns };nanosleep(&d, NULL);diff --git a/gpu/xnu-drm/test/IOKit/IOKitLib.h b/gpu/xnu-drm/test/IOKit/IOKitLib.hnew file mode 100644index 0000000..4975abd--- /dev/null+++ b/gpu/xnu-drm/test/IOKit/IOKitLib.h@@ -0,0 +1,23 @@+/* SPDX-License-Identifier: MIT */+/* The little of IOKit that libxnudrm uses, for fence.c: the host call ends in the test. */+#ifndef XNUDRM_TEST_IOKITLIB_H+#define XNUDRM_TEST_IOKITLIB_H+#include <stddef.h>+#include <stdint.h>+#ifndef __APPLE__+#include <sys/sysmacros.h>+#endif+typedef uint32_t io_connect_t, io_service_t;+#define kern_return_t int+#undef KERN_SUCCESS+#define KERN_SUCCESS 0+#define kIOMainPortDefault 0+#define IOServiceMatching(name) (name)+#define IOServiceGetMatchingService(port, match) ((void)(match), 1u)+#undef mach_task_self+#define mach_task_self() 0+int IOServiceOpen(io_service_t sv, int task, uint32_t type, io_connect_t *c);+int IOServiceClose(io_connect_t c);+#define IOObjectRelease(o) ((void)(o))+int IOConnectCallScalarMethod(io_connect_t c, uint32_t sel, const uint64_t *in, uint32_t nin, uint64_t *out, uint32_t *nout);+#endifdiff --git a/gpu/xnu-drm/test/fence.c b/gpu/xnu-drm/test/fence.cnew file mode 100644index 0000000..e9f7f75--- /dev/null+++ b/gpu/xnu-drm/test/fence.c@@ -0,0 +1,234 @@+// SPDX-License-Identifier: MIT+/*+ * libxnudrm's fences against a stub of xnudrmd, with and without a strict sandbox (its denials are simulated). Runs on+ * Linux and macOS: see run.sh.+ */+#include <errno.h>+#include <fcntl.h>+#include <poll.h>+#include <signal.h>+#include <stdio.h>+#include <string.h>+#include <sys/mman.h>+#include <sys/stat.h>+#include <sys/wait.h>+#include <unistd.h>++static int sandbox; /* deny what com.apple.WebKit.GPUProcess.sb denies */+static int denials, full; /* full: no descriptor to be had */+static int deny(void)+{+ if (sandbox) { denials++; errno = EPERM; }+ return sandbox;+}+static int no_fd(void) { if (full) errno = EMFILE; return full; }+static int t_pipe(int *p) { return deny() || no_fd() ? -1 : pipe(p); }+static int t_poll(struct pollfd *f, nfds_t n, int ms) { return deny() ? -1 : poll(f, n, ms); }+static int t_open(const char *path, int flags, ...)+{+ return !strcmp(path, "/dev/null") && deny() ? -1 : no_fd() ? -1 : open(path, flags, 0600);+}+static int t_fcntl(int fd, int cmd, int arg) { return cmd == F_SETFD && no_fd() ? -1 : fcntl(fd, cmd, arg); }+int os_dupfd_cloexec(int fd) { return deny() ? -1 : fcntl(fd, F_DUPFD_CLOEXEC, 3); }++int xnudrm_munmap(void *addr, size_t len); /* in the driver, the compat header declares it */+#define pipe t_pipe+#define poll t_poll+#define open t_open+#define fcntl t_fcntl+#include "../darwin/xnudrm.c"+#undef pipe+#undef poll+#undef open+#undef fcntl++/* ---- the stub: fence ids count up, signalled[] says which are */+static int next_id = 1, signalled[4096], closed[4096], broken;+int IOServiceOpen(io_service_t sv, int task, uint32_t type, io_connect_t *c)+{+ static io_connect_t next = 1;+ (void)sv; (void)task; (void)type;+ *c = next++;+ return 0;+}+int IOServiceClose(io_connect_t c) { (void)c; return 0; }+int IOConnectCallScalarMethod(io_connect_t c, uint32_t sel, const uint64_t *in, uint32_t nin, uint64_t *out, uint32_t *nout)+{+ struct xnudrm_hdr *h = (void *)buf;+ (void)c; (void)sel; (void)in; (void)nin; (void)out; (void)nout;+ h->status = 0;+ if (h->op == XNUDRM_IOCTL) ((struct drm_virtgpu_execbuffer *)(h + 1))->fence_fd = next_id++;+ if (h->op == XNUDRM_FENCE_WAIT && !signalled[h->arg0]) h->status = -XNUDRM_ETIME;+ if (h->op == XNUDRM_FENCE_WAIT && broken) return 1;+ if (h->op == XNUDRM_FENCE_CLOSE) closed[h->arg0]++;+ return 0;+}++#define CHECK(x) do { if (!(x)) { fprintf(stderr, "fence.c:%d: %s\n", __LINE__, #x); _exit(1); } } while (0)+static int node(void)+{+ int n = xnudrm_open("/dev/dri/renderD128", O_RDWR);+ CHECK(n >= 0);+ return n;+}+static int raw_submit(int n)+{+ struct drm_virtgpu_execbuffer a = { .flags = VIRTGPU_EXECBUF_FENCE_FD_OUT };+ CHECK(drmIoctl(n, DRM_IOCTL_VIRTGPU_EXECBUFFER, &a) == 0 && a.fence_fd >= 0);+ return a.fence_fd;+}+static int submit(int n) /* a fence the way Venus keeps one: imported (dup'ed), the original closed */+{+ int f = raw_submit(n), fd = xnudrm_dupfd_cloexec(f);+ CHECK(fd >= 0 && xnudrm_close(f) == 0);+ return fd;+}+#ifdef __APPLE__+#define READY POLLIN+#else+#define READY (POLLIN | POLLHUP) /* a pipe at end of file: POLLIN as well on macOS, POLLHUP alone on Linux */+#endif+static int wait_fd(int fd, int ms)+{+ struct pollfd p = { .fd = fd, .events = POLLIN };+ int r = xnudrm_poll(&p, 1, ms);+ CHECK(r >= 0 && (r == 1) == !!(p.revents & READY));+ return r;+}++static void t_signal(void)+{+ int n = node(), f = submit(n), g = xnudrm_dupfd_cloexec(f);+ CHECK(wait_fd(f, 0) == 0 && wait_fd(g, 10) == 0);+ signalled[1] = 1;+ CHECK(wait_fd(f, 0) == 1 && wait_fd(g, -1) == 1 && wait_fd(f, 0) == 1);+ CHECK(closed[1] == 1);+ CHECK(xnudrm_close(f) == 0 && xnudrm_close(g) == 0 && closed[1] == 1);+}+static void t_close(void) /* a sandbox fence's last descriptor closes it in xnudrmd */+{+ int n = node(), f = submit(n), g = xnudrm_dupfd_cloexec(f);+ xnudrm_close(f);+ CHECK(closed[1] == 0 && wait_fd(g, 0) == 0);+ xnudrm_close(g);+ CHECK(closed[1] == sandbox && nsfds == 0);+}+static void t_node_close(void) /* the order of vkDestroyInstance: fences nobody waited for, then the node */+{+ int n = node(), f = submit(n), kept = submit(n);+ xnudrm_close(f);+ xnudrm_close(n);+ CHECK(wait_fd(kept, 1000) == 1); /* a kept one is ready, not stuck */+ xnudrm_close(kept);+}+static void t_stale_new(void) /* an exported fence fd closed by the application itself, its number used again */+{+ int n = node(), f = raw_submit(n), g;+ signalled[1] = 1;+ close(f);+ g = raw_submit(n);+ CHECK(wait_fd(g, 0) == 0);+ CHECK(xnudrm_close(g) == 0);+ CHECK(nsfds == 0);+}+static void t_stale_dup(void) /* the same, the number used again by a dup */+{+ int n = node(), f = raw_submit(n), g = raw_submit(n), h;+ signalled[1] = 1;+ close(f);+ h = xnudrm_dupfd_cloexec(g);+ CHECK(h == f || !sandbox);+ CHECK(wait_fd(h, 0) == 0);+}+static void t_multi(void)+{+ int n = node();+ struct pollfd p[3] = { { .events = POLLIN }, { .fd = -1, .events = POLLIN, .revents = POLLIN }, { .events = POLLIN } };+ p[0].fd = submit(n);+ p[2].fd = submit(n);+ CHECK(xnudrm_poll(p, 3, 5) == 0 && !p[0].revents && !p[1].revents && !p[2].revents);+ signalled[2] = 1;+ CHECK(xnudrm_poll(p, 3, -1) == 1 && !p[0].revents && !p[1].revents && (p[2].revents & POLLIN));+}+static void t_mixed(void) /* a sandbox fence beside another descriptor: native poll() says nothing of the fence */+{+ if (!sandbox) return;+ int n = node(), other[2];+ struct pollfd p[2] = { { .fd = submit(n), .events = POLLIN }, { .events = POLLIN } };+ CHECK(pipe(other) == 0);+ p[1].fd = other[0];+ sandbox = 0;+ CHECK(xnudrm_poll(p, 2, 5) == 0);+ CHECK(write(other[1], "x", 1) == 1);+ CHECK(xnudrm_poll(p, 2, -1) == 1 && !p[0].revents && (p[1].revents & POLLIN));+ signalled[1] = 1;+ CHECK(xnudrm_poll(p, 2, -1) == 2 && (p[0].revents & POLLIN) && (p[1].revents & POLLIN));+ p[0].events = 0;+ CHECK(xnudrm_poll(p, 1, 5) == 0);+}+static void t_error(void) /* a failed wait is neither completion nor the end of the fence */+{+ int n = node(), f = submit(n);+ struct pollfd p = { .fd = f, .events = POLLIN };+ broken = 1;+ for (int i = 0; i < 2; i++) CHECK(xnudrm_poll(&p, 1, -1) == -1 && errno == EIO && !p.revents);+ broken = 0;+ CHECK(closed[1] == 0 && wait_fd(f, 0) == 0);+ signalled[1] = 1;+ CHECK(wait_fd(f, 0) == 1);+}+static void t_full(void) /* no descriptor for the fence: the ioctl fails, and xnudrmd's fence is closed */+{+ int n = node();+ struct drm_virtgpu_execbuffer a = { .flags = VIRTGPU_EXECBUF_FENCE_FD_OUT };+ full = 1;+ errno = 0;+ CHECK(drmIoctl(n, DRM_IOCTL_VIRTGPU_EXECBUFFER, &a) == -1 && errno == EMFILE && closed[1] == 1);+ full = 0;+ CHECK(wait_fd(submit(n), 0) == 0);+}+static void t_dup_flags(void)+{+ int n = node(), f = raw_submit(n), err = fcntl(2, F_DUPFD_CLOEXEC, 100);+ full = sandbox; /* F_SETFD fails: no descriptor rather than one without the flag */+ if (full) CHECK(xnudrm_dupfd_cloexec(f) == -1 && errno == EMFILE && nsfds == 1 && sfds[0].f->refs == 1);+ full = 0;+ close(0);+ close(2);+ int g = xnudrm_dupfd_cloexec(f), flags = fcntl(g, F_GETFD), low = fcntl(0, F_GETFD) < 0 && fcntl(2, F_GETFD) < 0;+ dup2(err, 2);+ CHECK(g >= 3 && (flags & FD_CLOEXEC) && low && wait_fd(g, 0) == 0);+}+static void t_denials(void) /* a denied call is not tried again (and logged) for every fence */+{+ int n = node();+ for (int i = 0; i < 100; i++) xnudrm_close(submit(n));+ CHECK(denials <= 3);+}++int main(void)+{+ int fd;+ while ((fd = open("/dev/null", O_RDWR)) >= 0 && fd < 3) ; /* the tests count on descriptors 0-2 being taken */+ if (fd > 2) close(fd);+ static const struct { const char *name; void (*f)(void); } tests[] = {+ { "signal", t_signal }, { "close", t_close }, { "node close", t_node_close }, { "stale fd, new", t_stale_new },+ { "stale fd, dup", t_stale_dup }, { "multi", t_multi }, { "mixed", t_mixed },+ { "error", t_error }, { "full", t_full }, { "dup flags", t_dup_flags }, { "denials", t_denials },+ };+ int bad = 0;+ for (int s = 0; s < 2; s++) {+ for (unsigned i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {+ int st, r;+ fflush(stdout);+ pid_t pid = fork();+ if (pid < 0) { perror("fork"); return 1; }+ if (!pid) { sandbox = s; alarm(10); tests[i].f(); _exit(0); }+ do r = waitpid(pid, &st, 0); while (r < 0 && errno == EINTR);+ if (r != pid) { perror("waitpid"); return 1; }+ printf("%s%s: %s\n", tests[i].name, s ? " (sandbox)" : "", !st ? "ok" : WIFSIGNALED(st) ? strsignal(WTERMSIG(st)) : "FAIL");+ bad |= st;+ }+ }+ return !!bad;+}diff --git a/gpu/xnu-drm/test/run.sh b/gpu/xnu-drm/test/run.shnew file mode 100755index 0000000..2135a4a--- /dev/null+++ b/gpu/xnu-drm/test/run.sh@@ -0,0 +1,10 @@+#!/bin/bash+# SPDX-License-Identifier: MIT+# Build and run fence.c, on Linux (the kernel's DRM headers) or on a Mac with DRM=<a Mesa tree>/include/drm-uapi.+set -euo pipefail+cd "$(dirname "$0")"+T=$(mktemp -d); trap 'rm -rf "$T"' EXIT+ln -s "${DRM:-/usr/include/drm}" "$T/drm-uapi"+# (Linux's __u64 is unsigned long long, not uint64_t)+${CC:-cc} -g -O1 -D_GNU_SOURCE -Wall -Wno-incompatible-pointer-types ${CFLAGS:-} -I. -I"$T" -I.. -I../darwin fence.c -o "$T/fence" -lpthread+"$T/fence"